ReactOS  0.4.13-dev-464-g6b95727
connect.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS kernel-mode tests
3  * LICENSE: LGPLv2+ - See COPYING.LIB in the top level directory
4  * PURPOSE: Kernel-Mode Test Suite for TCPIP.sys
5  * PROGRAMMER: Jérôme Gardou <jerome.gardou@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 #include <tdikrnl.h>
10 #include <ndk/rtlfuncs.h>
11 
12 #include <sys/param.h>
13 
14 #include "tcpip.h"
15 
16 #define TAG_TEST 'tseT'
17 
18 #if BYTE_ORDER == LITTLE_ENDIAN
19 USHORT
21 {
22  return ((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8);
23 }
24 #else
25 #define htons(x) (x)
26 #endif
27 
28 static
30 NTAPI
33  _In_ PIRP Irp,
35 {
38 
40 
42 }
43 
44 static
45 VOID
47 {
48  PIRP Irp;
49  HANDLE AddressHandle, ConnectionHandle;
50  FILE_OBJECT* ConnectionFileObject;
52  UNICODE_STRING TcpDeviceName = RTL_CONSTANT_STRING(L"\\Device\\Tcp");
55  TA_IP_ADDRESS* IpAddress;
56  TA_IP_ADDRESS ConnectAddress, ReturnAddress;
58  IO_STATUS_BLOCK StatusBlock;
59  ULONG FileInfoSize;
60  IN_ADDR InAddr;
61  LPCWSTR AddressTerminator;
62  CONNECTION_CONTEXT ConnectionContext = (CONNECTION_CONTEXT)(ULONG_PTR)0xC0CAC01AC0CAC01AULL;
63  KEVENT Event;
64  TDI_CONNECTION_INFORMATION RequestInfo, ReturnInfo;
65 
66  /* Create a TCP address file */
69  FileInfoSize,
70  TAG_TEST);
71  ok(FileInfo != NULL, "FileInfo is NULL!\n");
72  RtlZeroMemory(FileInfo, FileInfoSize);
73 
74  FileInfo->EaNameLength = TDI_TRANSPORT_ADDRESS_LENGTH;
75  FileInfo->EaValueLength = sizeof(TA_IP_ADDRESS);
77 
78  IpAddress = (PTA_IP_ADDRESS)(&FileInfo->EaName[TDI_TRANSPORT_ADDRESS_LENGTH + 1]);
79  IpAddress->TAAddressCount = 1;
80  IpAddress->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
81  IpAddress->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
82  IpAddress->Address[0].Address[0].sin_port = htons(TEST_CONNECT_CLIENT_PORT);
83  Status = RtlIpv4StringToAddressW(L"127.0.0.1", TRUE, &AddressTerminator, &InAddr);
85  IpAddress->Address[0].Address[0].in_addr = InAddr.S_un.S_addr;
86 
88  &TcpDeviceName,
90  NULL,
91  NULL);
92 
93  Status = ZwCreateFile(
94  &AddressHandle,
97  &StatusBlock,
98  0,
101  FILE_OPEN_IF,
102  0L,
103  FileInfo,
104  FileInfoSize);
106 
108 
109  /* Create a TCP connection file */
112  FileInfoSize,
113  TAG_TEST);
114  ok(FileInfo != NULL, "FileInfo is NULL!\n");
115  RtlZeroMemory(FileInfo, FileInfoSize);
116 
117  FileInfo->EaNameLength = TDI_CONNECTION_CONTEXT_LENGTH;
118  FileInfo->EaValueLength = sizeof(CONNECTION_CONTEXT);
120  *((CONNECTION_CONTEXT*)&FileInfo->EaName[TDI_CONNECTION_CONTEXT_LENGTH + 1]) = ConnectionContext;
121 
122  Status = ZwCreateFile(
126  &StatusBlock,
127  0,
130  FILE_OPEN_IF,
131  0L,
132  FileInfo,
133  FileInfoSize);
135 
137 
138  /* Get the file and device object for the upcoming IRPs */
141  GENERIC_READ,
143  KernelMode,
144  (PVOID*)&ConnectionFileObject,
145  NULL);
147  DeviceObject = IoGetRelatedDeviceObject(ConnectionFileObject);
148  ok(DeviceObject != NULL, "Device object is NULL!\n");
149 
150  /* Associate the connection file and the address */
153  ok(Irp != NULL, "IoAllocateIrp failed.\n");
154 
155  TdiBuildAssociateAddress(Irp, DeviceObject, ConnectionFileObject, NULL, NULL, AddressHandle);
157 
159  if (Status == STATUS_PENDING)
160  {
161  trace("Associate address IRP is pending.\n");
163  &Event,
164  Executive,
165  KernelMode,
166  FALSE,
167  NULL);
168  Status = Irp->IoStatus.Status;
169  trace("Associate address IRP completed.\n");
170  }
172  IoFreeIrp(Irp);
173 
174 
176 
177  /* Build the connect IRP. */
179  ok(Irp != NULL, "IoAllocateIrp failed.\n");
180 
181  /* Prepare the request */
182  RtlZeroMemory(&RequestInfo, sizeof(RequestInfo));
183  RtlZeroMemory(&ConnectAddress, sizeof(ConnectAddress));
184  RequestInfo.RemoteAddressLength = sizeof(TA_IP_ADDRESS);
185  RequestInfo.RemoteAddress = &ConnectAddress;
186  ConnectAddress.TAAddressCount = 1;
187  ConnectAddress.Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
188  ConnectAddress.Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
189  ConnectAddress.Address[0].Address[0].sin_port = htons(TEST_CONNECT_SERVER_PORT);
190  Status = RtlIpv4StringToAddressW(L"127.0.0.1", TRUE, &AddressTerminator, &InAddr);
191  ConnectAddress.Address[0].Address[0].in_addr = InAddr.S_un.S_addr;
192 
193  /* See what we will get in exchange */
194  RtlZeroMemory(&ReturnInfo, sizeof(ReturnInfo));
195  RtlZeroMemory(&ReturnAddress, sizeof(ReturnAddress));
196  ReturnInfo.RemoteAddressLength = sizeof(TA_IP_ADDRESS);
197  ReturnInfo.RemoteAddress = &ReturnAddress;
198 
200  DeviceObject,
201  ConnectionFileObject,
202  NULL,
203  NULL,
204  NULL,
205  &RequestInfo,
206  &ReturnInfo);
208 
210  if (Status == STATUS_PENDING)
211  {
212  trace("Connect IRP is pending.\n");
214  &Event,
215  Executive,
216  KernelMode,
217  FALSE,
218  NULL);
219  Status = Irp->IoStatus.Status;
220  trace("Connect IRP completed.\n");
221  }
223  IoFreeIrp(Irp);
224 
225  /* The IRP doesn't touch the return info */
226  ok_eq_long(ReturnInfo.RemoteAddressLength, sizeof(TA_IP_ADDRESS));
227  ok_eq_pointer(ReturnInfo.RemoteAddress, &ReturnAddress);
228  ok_eq_long(ReturnInfo.OptionsLength, 0);
229  ok_eq_pointer(ReturnInfo.Options, NULL);
230  ok_eq_long(ReturnInfo.UserDataLength, 0);
231  ok_eq_pointer(ReturnInfo.UserData, NULL);
232 
233  ok_eq_long(ReturnAddress.TAAddressCount, 1);
234  ok_eq_hex(ReturnAddress.Address[0].AddressType, TDI_ADDRESS_TYPE_IP);
235  ok_eq_hex(ReturnAddress.Address[0].AddressLength, TDI_ADDRESS_LENGTH_IP);
236  ok_eq_hex(ReturnAddress.Address[0].Address[0].sin_port, htons(TEST_CONNECT_SERVER_PORT));
237  ok_eq_hex(ReturnAddress.Address[0].Address[0].in_addr, InAddr.S_un.S_addr);
238 
239  ObDereferenceObject(ConnectionFileObject);
240 
242  ZwClose(AddressHandle);
243 }
244 
245 static KSTART_ROUTINE RunTest;
246 static
247 VOID
248 NTAPI
251 {
253 
254  TestTcpConnect();
255 }
256 
257 KMT_MESSAGE_HANDLER TestConnect;
258 NTSTATUS
263  _In_ SIZE_T InLength,
264  _Inout_ PSIZE_T OutLength
265 )
266 {
268 
271 
272  return STATUS_SUCCESS;
273 }
union in_addr::@985 S_un
LONG TAAddressCount
Definition: tdi.h:523
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define trace(...)
Definition: kmt_test.h:217
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL)
static VOID TestTcpConnect(void)
Definition: connect.c:46
#define FILE_OPEN_IF
Definition: from_kernel.h:56
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
static NTSTATUS NTAPI IrpCompletionRoutine(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp, _In_ PVOID Context)
Definition: connect.c:31
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:63
_In_ PIRP Irp
Definition: csq.h:116
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define ok_eq_pointer(value, expected)
struct _TA_ADDRESS_IP * PTA_IP_ADDRESS
LONG NTSTATUS
Definition: precomp.h:26
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
#define TDI_CONNECTION_CONTEXT_LENGTH
Definition: tdi.h:373
PDEVICE_OBJECT NTAPI IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject)
Definition: device.c:1538
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define TEST_CONNECT_CLIENT_PORT
Definition: tcpip.h:7
#define FILE_SHARE_READ
Definition: compat.h:125
#define _In_opt_
Definition: no_sal2.h:213
KMT_MESSAGE_HANDLER TestConnect
Definition: connect.c:257
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:515
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define TAG_TEST
Definition: connect.c:16
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:496
ULONG_PTR * PSIZE_T
Definition: typedefs.h:78
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define GENERIC_WRITE
Definition: nt_native.h:90
PVOID CONNECTION_CONTEXT
Definition: tdi.h:37
#define ok(value,...)
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:434
smooth NULL
Definition: ftsmooth.c:416
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
Definition: bufpool.h:45
struct _TA_ADDRESS_IP::_AddrIp Address[1]
#define ULL(a, b)
Definition: format_msg.c:27
#define IO_NETWORK_INCREMENT
Definition: tcpip.h:43
#define STATUS_PENDING
Definition: ntstatus.h:82
Definition: tcpip.h:125
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define _Inout_
Definition: no_sal2.h:244
struct _TA_ADDRESS_IP TA_IP_ADDRESS
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1250
#define TDI_ADDRESS_TYPE_IP
Definition: tdi.h:345
USHORT htons(USHORT x)
Definition: connect.c:20
#define GENERIC_READ
Definition: compat.h:124
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL)
Status
Definition: gdiplustypes.h:24
#define TdiBuildConnect( Irp, DevObj, FileObj, CompRoutine, Contxt, Time, RequestConnectionInfo, ReturnConnectionInfo)
Definition: tdikrnl.h:494
u_long S_addr
Definition: tcpip.h:131
#define _In_
Definition: no_sal2.h:204
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define TEST_CONNECT_SERVER_PORT
Definition: tcpip.h:6
unsigned short USHORT
Definition: pedump.c:61
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
#define TdiTransportAddress
Definition: tdi.h:370
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define TdiConnectionContext
Definition: tdi.h:371
#define TDI_TRANSPORT_ADDRESS_LENGTH
Definition: tdi.h:372
VOID NTAPI IoFreeIrp(IN PIRP Irp)
Definition: irp.c:1666
struct FileInfo FileInfo
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define TdiBuildAssociateAddress( Irp, DevObj, FileObj, CompRoutine, Contxt, AddrHandle)
Definition: tdikrnl.h:467
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
#define ok_eq_hex(value, expected)
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define TDI_ADDRESS_LENGTH_IP
Definition: tdi.h:413
_In_ PKSPIN_CONNECT _In_ ACCESS_MASK _Out_ PHANDLE ConnectionHandle
Definition: ks.h:4565
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
NTSYSAPI NTSTATUS NTAPI RtlIpv4StringToAddressW(_In_ PCWSTR String, _In_ BOOLEAN Strict, _Out_ PCWSTR *Terminator, _Out_ struct in_addr *Addr)
Definition: network.c:405
return STATUS_SUCCESS
Definition: btrfs.c:2777
static KSTART_ROUTINE RunTest
Definition: connect.c:245
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define ok_eq_long(value, expected)
Definition: kmt_test.h:240
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14