Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1466 lines
45 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. testtdi.c
  5. Abstract:
  6. Kernel Mode test program for any Tdi network provider. This routine is an
  7. example of how to use the TDI interface at the kernel level.
  8. Author:
  9. Dave Beaver (DBeaver) 5 June 1991
  10. Revision History:
  11. --*/
  12. #include "nbf.h"
  13. #include <ctype.h>
  14. #define TRANSPORT_NAME L"\\Device\\Nbf"
  15. PSZ ServerName = "DCTDISERVER ";
  16. PSZ ClientName = "DCTDICLIENT ";
  17. PSZ AnyName = "* ";
  18. static PUCHAR TextBuffer; // dynamically allocated non-paged buffer.
  19. ULONG c9_Xmt = 0xff;
  20. ULONG c9_Rcv = 0xff;
  21. ULONG c9_Iteration = 0xffffffff;
  22. static ULONG TextBufferLength; // size of the above in bytes.
  23. #define BUFFER_SIZE 0xffff
  24. PUCHAR RBuff;
  25. PUCHAR XBuff;
  26. UCHAR c9_ListBlock[512];
  27. UCHAR c9_ConnBlock[512];
  28. extern KEVENT TdiSendEvent;
  29. extern KEVENT TdiReceiveEvent;
  30. extern KEVENT TdiServerEvent;
  31. ULONG ApcContext;
  32. NTSTATUS
  33. TSTRCVCompletion(
  34. IN PDEVICE_OBJECT DeviceObject,
  35. IN PIRP Irp,
  36. IN PVOID Context
  37. )
  38. {
  39. DbgPrint ("TSTRCVCompletion event: %lx\n" , Context);
  40. // KeSetEvent ((PKEVENT)Context, 0, TRUE);
  41. return STATUS_MORE_PROCESSING_REQUIRED;
  42. UNREFERENCED_PARAMETER( DeviceObject );
  43. UNREFERENCED_PARAMETER( Irp );
  44. }
  45. #define InitWaitObject(_event)\
  46. KeInitializeEvent (\
  47. _event,\
  48. SynchronizationEvent,\
  49. FALSE)
  50. VOID
  51. NbfTestTimer(
  52. IN PKDPC Dpc,
  53. IN PVOID DeferredContext,
  54. IN PVOID SystemArgument1,
  55. IN PVOID SystemArgument2
  56. );
  57. NTSTATUS
  58. TtdiOpenAddress (
  59. IN PHANDLE FileHandle,
  60. IN PSZ Name
  61. );
  62. NTSTATUS
  63. TtdiOpenConnection (
  64. IN PHANDLE FileHandle,
  65. IN ULONG ConnectionContext
  66. );
  67. NTSTATUS
  68. TtdiOpenAddress (
  69. IN PHANDLE FileHandle,
  70. IN PSZ Name)
  71. {
  72. IO_STATUS_BLOCK IoStatusBlock;
  73. NTSTATUS Status;
  74. UNICODE_STRING NameString;
  75. OBJECT_ATTRIBUTES ObjectAttributes;
  76. PFILE_FULL_EA_INFORMATION EaBuffer;
  77. PTDI_ADDRESS_NETBIOS AddressName;
  78. PTRANSPORT_ADDRESS Address;
  79. PTA_ADDRESS AddressType;
  80. int i;
  81. DbgPrint ("TtdiOpenAddress: Opening ");
  82. DbgPrint (Name);
  83. DbgPrint (".\n");
  84. RtlInitUnicodeString (&NameString, TRANSPORT_NAME);
  85. InitializeObjectAttributes (
  86. &ObjectAttributes,
  87. &NameString,
  88. 0,
  89. NULL,
  90. NULL);
  91. EaBuffer = (PFILE_FULL_EA_INFORMATION)ExAllocatePool (NonPagedPool, 100);
  92. if (EaBuffer == NULL) {
  93. DbgBreakPoint ();
  94. }
  95. EaBuffer->NextEntryOffset =0;
  96. EaBuffer->Flags = 0;
  97. EaBuffer->EaNameLength = TDI_TRANSPORT_ADDRESS_LENGTH;
  98. EaBuffer->EaValueLength = sizeof (TDI_ADDRESS_NETBIOS);
  99. for (i=0;i<(int)EaBuffer->EaNameLength;i++) {
  100. EaBuffer->EaName[i] = TdiTransportAddress[i];
  101. }
  102. Address = (PTRANSPORT_ADDRESS)&EaBuffer->EaName[EaBuffer->EaNameLength+1];
  103. Address->TAAddressCount = 1;
  104. AddressType = (PTA_ADDRESS)((PUCHAR)Address + sizeof (Address->TAAddressCount));
  105. AddressType->AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  106. AddressType->AddressLength = TDI_ADDRESS_LENGTH_NETBIOS;
  107. AddressName = (PTDI_ADDRESS_NETBIOS)((PUCHAR)AddressType +
  108. sizeof (AddressType->AddressType) + sizeof (AddressType->AddressLength));
  109. AddressName->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  110. AddressName->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  111. for (i=0;i<16;i++) {
  112. AddressName->NetbiosName[i] = Name[i];
  113. }
  114. Status = IoCreateFile (
  115. FileHandle,
  116. 0, // desired access.
  117. &ObjectAttributes, // object attributes.
  118. &IoStatusBlock, // returned status information.
  119. 0, // block size (unused).
  120. FO_SYNCHRONOUS_IO, // file attributes.
  121. 0,
  122. 0,
  123. 0, // create options.
  124. EaBuffer, // EA buffer.
  125. (PUCHAR)&AddressName->NetbiosName[i] - (PUCHAR)EaBuffer + 1, // ea length
  126. CreateFileTypeNone,
  127. (PVOID)NULL,
  128. 0 ); // EA length.
  129. if (!NT_SUCCESS( Status )) {
  130. DbgPrint ("TtdiOpenAddress: FAILURE, NtCreateFile returned status code=%lC.\n", Status);
  131. return Status;
  132. }
  133. Status = IoStatusBlock.Status;
  134. if (!(NT_SUCCESS( Status ))) {
  135. DbgPrint ("TtdiOpenAddress: FAILURE, IoStatusBlock.Status contains status code=%lC.\n", Status);
  136. }
  137. DbgPrint ("TtdiOpenAddress: returning\n");
  138. return Status;
  139. } /* TtdiOpenAddress */
  140. NTSTATUS
  141. TtdiOpenConnection (IN PHANDLE FileHandle, IN ULONG ConnectionContext)
  142. {
  143. IO_STATUS_BLOCK IoStatusBlock;
  144. NTSTATUS Status;
  145. UNICODE_STRING NameString;
  146. OBJECT_ATTRIBUTES ObjectAttributes;
  147. PFILE_FULL_EA_INFORMATION EaBuffer;
  148. int i;
  149. DbgPrint ("TtdiOpenConnection: Opening Context %lx...\n ",
  150. ConnectionContext);
  151. RtlInitUnicodeString (&NameString, TRANSPORT_NAME);
  152. InitializeObjectAttributes (
  153. &ObjectAttributes,
  154. &NameString,
  155. 0,
  156. NULL,
  157. NULL);
  158. EaBuffer = (PFILE_FULL_EA_INFORMATION)ExAllocatePool (NonPagedPool, 100);
  159. if (EaBuffer == NULL) {
  160. DbgBreakPoint ();
  161. }
  162. EaBuffer->NextEntryOffset =0;
  163. EaBuffer->Flags = 0;
  164. EaBuffer->EaNameLength = TDI_CONNECTION_CONTEXT_LENGTH;
  165. EaBuffer->EaValueLength = sizeof (ULONG);
  166. for (i=0;i<(int)EaBuffer->EaNameLength;i++) {
  167. EaBuffer->EaName[i] = TdiConnectionContext[i];
  168. }
  169. RtlMoveMemory (
  170. &EaBuffer->EaName[EaBuffer->EaValueLength + 1],
  171. &ConnectionContext,
  172. sizeof (PVOID));
  173. Status = NtCreateFile (
  174. FileHandle,
  175. 0,
  176. &ObjectAttributes, // object attributes.
  177. &IoStatusBlock, // returned status information.
  178. 0, // block size (unused).
  179. FO_SYNCHRONOUS_IO, // file attributes.
  180. 0,
  181. 0,
  182. 0, // create options.
  183. EaBuffer, // EA buffer.
  184. 100); // EA length.
  185. if (!NT_SUCCESS( Status )) {
  186. DbgPrint ("TtdiOpenConnection: FAILURE, NtCreateFile returned status code=%lC.\n", Status);
  187. return Status;
  188. }
  189. Status = IoStatusBlock.Status;
  190. if (!(NT_SUCCESS( Status ))) {
  191. DbgPrint ("TtdiOpenConnection: FAILURE, IoStatusBlock.Status contains status code=%lC.\n", Status);
  192. }
  193. DbgPrint ("TtdiOpenConnection: returning\n");
  194. return Status;
  195. } /* TtdiOpenEndpoint */
  196. NTSTATUS
  197. CloseAddress (IN HANDLE FileHandle)
  198. {
  199. NTSTATUS Status;
  200. Status = NtClose (FileHandle);
  201. if (!(NT_SUCCESS( Status ))) {
  202. DbgPrint ("CloseAddress: FAILURE, NtClose returned status code=%lC.\n", Status);
  203. } else {
  204. DbgPrint ("CloseAddress: NT_SUCCESS.\n");
  205. }
  206. return Status;
  207. } /* CloseAddress */
  208. BOOLEAN
  209. TtdiSend()
  210. {
  211. USHORT i, Iteration, Increment;
  212. HANDLE RdrHandle, RdrConnectionHandle;
  213. KEVENT Event1;
  214. PFILE_OBJECT AddressObject, ConnectionObject;
  215. PDEVICE_OBJECT DeviceObject;
  216. NTSTATUS Status;
  217. PMDL SendMdl, ReceiveMdl;
  218. IO_STATUS_BLOCK Iosb1;
  219. TDI_CONNECTION_INFORMATION RequestInformation;
  220. TDI_CONNECTION_INFORMATION ReturnInformation;
  221. PTRANSPORT_ADDRESS ListenBlock;
  222. PTRANSPORT_ADDRESS ConnectBlock;
  223. PTDI_ADDRESS_NETBIOS temp;
  224. PUCHAR MessageBuffer;
  225. ULONG MessageBufferLength;
  226. ULONG CurrentBufferLength;
  227. PUCHAR SendBuffer;
  228. ULONG SendBufferLength;
  229. PIRP Irp;
  230. Status = KeWaitForSingleObject (&TdiSendEvent, Suspended, KernelMode, FALSE, NULL);
  231. SendBufferLength = (ULONG)BUFFER_SIZE;
  232. MessageBufferLength = (ULONG)BUFFER_SIZE;
  233. DbgPrint( "\n****** Start of Send Test ******\n" );
  234. XBuff = ExAllocatePool (NonPagedPool, BUFFER_SIZE);
  235. if (XBuff == (PVOID)NULL) {
  236. DbgPrint ("Unable to allocate nonpaged pool for send buffer exiting\n");
  237. return FALSE;
  238. }
  239. RBuff = ExAllocatePool (NonPagedPool, BUFFER_SIZE);
  240. if (RBuff == (PVOID)NULL) {
  241. DbgPrint ("Unable to allocate nonpaged pool for receive buffer exiting\n");
  242. return FALSE;
  243. }
  244. ListenBlock = ExAllocatePool (NonPagedPool, sizeof (TRANSPORT_ADDRESS) +
  245. sizeof (TDI_ADDRESS_NETBIOS));
  246. ConnectBlock = ExAllocatePool (NonPagedPool, sizeof (TRANSPORT_ADDRESS) +
  247. sizeof (TDI_ADDRESS_NETBIOS));
  248. ListenBlock->TAAddressCount = 1;
  249. ListenBlock->Address[0].AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  250. ListenBlock->Address[0].AddressLength = sizeof (TDI_ADDRESS_NETBIOS);
  251. temp = (PTDI_ADDRESS_NETBIOS)ListenBlock->Address[0].Address;
  252. temp->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  253. for (i=0;i<16;i++) {
  254. temp->NetbiosName[i] = ClientName[i];
  255. }
  256. ConnectBlock->TAAddressCount = 1;
  257. ConnectBlock->Address[0].AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  258. ConnectBlock->Address[0].AddressLength = sizeof (TDI_ADDRESS_NETBIOS);
  259. temp = (PTDI_ADDRESS_NETBIOS)ConnectBlock->Address[0].Address;
  260. temp->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  261. for (i=0;i<16;i++) {
  262. temp->NetbiosName[i] = ServerName[i];
  263. }
  264. //
  265. // Create an event for the synchronous I/O requests that we'll be issuing.
  266. //
  267. KeInitializeEvent (
  268. &Event1,
  269. SynchronizationEvent,
  270. FALSE);
  271. Status = TtdiOpenAddress (&RdrHandle, AnyName);
  272. if (!NT_SUCCESS(Status)) {
  273. DbgPrint( "\n****** Send Test: FAILED on open of client: %lC ******\n", Status );
  274. return FALSE;
  275. }
  276. Status = ObReferenceObjectByHandle (
  277. RdrHandle,
  278. 0L,
  279. NULL,
  280. KernelMode,
  281. (PVOID *) &AddressObject,
  282. NULL);
  283. //
  284. // Open the connection on the transport.
  285. //
  286. Status = TtdiOpenConnection (&RdrConnectionHandle, 1);
  287. if (!NT_SUCCESS(Status)) {
  288. DbgPrint( "\n****** Send Test: FAILED on open of server Connection: %lC ******\n", Status );
  289. return FALSE;
  290. }
  291. Status = ObReferenceObjectByHandle (
  292. RdrConnectionHandle,
  293. 0L,
  294. NULL,
  295. KernelMode,
  296. (PVOID *) &ConnectionObject,
  297. NULL);
  298. if (!NT_SUCCESS(Status)) {
  299. DbgPrint( "\n****** Send Test: FAILED on open of server Connection: %lC ******\n", Status );
  300. return FALSE;
  301. }
  302. //
  303. // Get a pointer to the stack location for the first driver. This will be
  304. // used to pass the original function codes and parameters.
  305. //
  306. DeviceObject = IoGetRelatedDeviceObject( ConnectionObject );
  307. Irp = TdiBuildInternalDeviceControlIrp (
  308. TDI_ASSOCIATE_ADDRESS,
  309. DeviceObject,
  310. ConnectionObject,
  311. &Event1,
  312. &Iosb1);
  313. //
  314. // Get a pointer to the stack location for the first driver. This will be
  315. // used to pass the original function codes and parameters.
  316. //
  317. TdiBuildAssociateAddress (Irp,
  318. DeviceObject,
  319. ConnectionObject,
  320. TSTRCVCompletion,
  321. &Event1,
  322. RdrHandle);
  323. Status = IoCallDriver (DeviceObject, Irp);
  324. // IoFreeIrp (Irp);
  325. if (Status == STATUS_PENDING) {
  326. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  327. if (!NT_SUCCESS(Status)) {
  328. DbgPrint( "\n****** Send Test: FAILED Event1 Wait Associate: %lC ******\n", Status );
  329. return FALSE;
  330. }
  331. if (!NT_SUCCESS(Iosb1.Status)) {
  332. DbgPrint( "\n****** Send Test: FAILED Associate Iosb status: %lC ******\n", Status );
  333. return FALSE;
  334. }
  335. } else {
  336. if (!NT_SUCCESS (Status)) {
  337. DbgPrint( "\n****** Send Test: AssociateAddress FAILED Status: %lC ******\n", Status );
  338. return FALSE;
  339. } else {
  340. DbgPrint ("********** Send Test: Success AssociateAddress\n");
  341. }
  342. }
  343. //
  344. // Post a TdiConnect to the client endpoint.
  345. //
  346. RequestInformation.RemoteAddress = ConnectBlock;
  347. RequestInformation.RemoteAddressLength = sizeof (TRANSPORT_ADDRESS) +
  348. sizeof (TDI_ADDRESS_NETBIOS);
  349. KeInitializeEvent (
  350. &Event1,
  351. SynchronizationEvent,
  352. FALSE);
  353. Irp = TdiBuildInternalDeviceControlIrp (
  354. TDI_CONNECT,
  355. DeviceObject,
  356. ConnectionObject,
  357. &Event1,
  358. &Iosb1);
  359. TdiBuildConnect (
  360. Irp,
  361. DeviceObject,
  362. ConnectionObject,
  363. TSTRCVCompletion,
  364. &Event1,
  365. 0,
  366. &RequestInformation,
  367. &ReturnInformation);
  368. InitWaitObject (&Event1);
  369. Status = IoCallDriver (DeviceObject, Irp);
  370. // IoFreeIrp (Irp);
  371. if (Status == STATUS_PENDING) {
  372. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  373. if (!NT_SUCCESS(Status)) {
  374. DbgPrint( "\n****** Send Test: FAILED Event1 Wait Connect: %lC ******\n", Status );
  375. return FALSE;
  376. }
  377. if (!NT_SUCCESS(Iosb1.Status)) {
  378. DbgPrint( "\n****** Send Test: FAILED Iosb status Connect: %lC ******\n", Status );
  379. return FALSE;
  380. } else {
  381. DbgPrint ("********** Send Test: Success Connect Iosb\n");
  382. }
  383. } else {
  384. if (!NT_SUCCESS (Status)) {
  385. DbgPrint( "\n****** Send Test: Connect FAILED Status: %lC ******\n", Status );
  386. return FALSE;
  387. } else {
  388. DbgPrint ("********** Send Test: Success Connect Immediate\n");
  389. }
  390. }
  391. DbgPrint( "\n****** Send Test: SUCCESSFUL TdiConnect: ******\n");
  392. //
  393. // Send/receive 1 or 10 messages.
  394. //
  395. SendBuffer = (PUCHAR)ExAllocatePool (NonPagedPool, SendBufferLength);
  396. if (SendBuffer == NULL) {
  397. DbgPrint ("\n****** Send Test: ExAllocatePool failed! ******\n");
  398. }
  399. SendMdl = IoAllocateMdl (SendBuffer, SendBufferLength, FALSE, FALSE, NULL);
  400. MmBuildMdlForNonPagedPool (SendMdl);
  401. MessageBuffer=(PUCHAR)ExAllocatePool (NonPagedPool, MessageBufferLength);
  402. if (MessageBuffer == NULL) {
  403. DbgPrint ("\n****** Send Test: ExAllocatePool failed! ******\n");
  404. }
  405. ReceiveMdl = IoAllocateMdl (MessageBuffer, MessageBufferLength, FALSE, FALSE, NULL);
  406. MmBuildMdlForNonPagedPool (ReceiveMdl);
  407. //
  408. // Cycle the buffer length from 0 up through the maximum for Tdi. after a
  409. // couple of shots at the full range in one byte steps, increment by ever
  410. // increasing amounts to get to the max.
  411. //
  412. CurrentBufferLength = 0;
  413. Increment = 1;
  414. for (Iteration=1; Iteration<(USHORT)c9_Iteration; Iteration++) {
  415. CurrentBufferLength += Increment;
  416. if (CurrentBufferLength > MessageBufferLength) {
  417. CurrentBufferLength = 0;
  418. Increment = 1;
  419. }
  420. if (CurrentBufferLength > 7500) {
  421. Increment++;
  422. }
  423. if ((USHORT)((Iteration / 100) * 100) == Iteration) {
  424. DbgPrint ("Iteration #%d Buffer Length: %lx Buffer Start: %x\n",
  425. Iteration, CurrentBufferLength,Iteration % 256);
  426. }
  427. for (i=0; i<(USHORT)CurrentBufferLength; i++) {
  428. SendBuffer [i] = (UCHAR)(i + Iteration % 256 );
  429. MessageBuffer [i] = 0; // zap this with something.
  430. }
  431. //
  432. // Now issue a send on the client side.
  433. //
  434. KeInitializeEvent (
  435. &Event1,
  436. SynchronizationEvent,
  437. FALSE);
  438. Irp = TdiBuildInternalDeviceControlIrp (
  439. TDI_SEND,
  440. DeviceObject,
  441. ConnectionObject,
  442. &Event1,
  443. &Iosb1);
  444. TdiBuildSend (Irp,
  445. DeviceObject,
  446. ConnectionObject,
  447. TSTRCVCompletion,
  448. &Event1,
  449. ReceiveMdl,
  450. 0,
  451. CurrentBufferLength);
  452. InitWaitObject (&Event1);
  453. Status = IoCallDriver (DeviceObject, Irp);
  454. // IoFreeIrp (Irp);
  455. if (Status == STATUS_PENDING) {
  456. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  457. if (!NT_SUCCESS(Status)) {
  458. DbgPrint( "\n****** Send Test: FAILED Event1 Wait Send: %lC %d ******\n",
  459. Status, Iteration );
  460. return FALSE;
  461. }
  462. if (!NT_SUCCESS(Iosb1.Status)) {
  463. DbgPrint( "\n****** Send Test: FAILED Iosb status Send: %lC %d ******\n",
  464. Status, Iteration );
  465. return FALSE;
  466. } else {
  467. DbgPrint ("********** Send Test: Success SendIosb\n");
  468. }
  469. } else {
  470. if (!NT_SUCCESS (Status)) {
  471. DbgPrint( "\n****** Send Test: Send FAILED Status: %lC %d ******\n",
  472. Status, Iteration );
  473. return FALSE;
  474. } else {
  475. DbgPrint ("********** Send Test: Success Send Immediate\n");
  476. }
  477. }
  478. if (Iosb1.Information != CurrentBufferLength) {
  479. DbgPrint ("SendTest: Bytes sent <> Send buffer size.\n");
  480. DbgPrint ("SendTest: BytesToSend=%ld. BytesSent=%ld.\n",
  481. CurrentBufferLength, Iosb1.Information);
  482. }
  483. }
  484. //
  485. // We're done with this endpoint. Close it and get out.
  486. //
  487. Status = CloseAddress (RdrHandle);
  488. if (!NT_SUCCESS(Status)) {
  489. DbgPrint( "\n****** Send Test: FAILED on 2nd Close: %lC ******\n", Status );
  490. return FALSE;
  491. }
  492. DbgPrint( "\n****** End of Send Test ******\n" );
  493. return TRUE;
  494. } /* Send */
  495. BOOLEAN
  496. TtdiReceive()
  497. {
  498. USHORT i, Iteration, Increment;
  499. SHORT j,k;
  500. HANDLE SvrHandle, SvrConnectionHandle;
  501. PFILE_OBJECT AddressObject, ConnectionObject;
  502. PDEVICE_OBJECT DeviceObject;
  503. NTSTATUS Status;
  504. PMDL SendMdl, ReceiveMdl;
  505. IO_STATUS_BLOCK Iosb1;
  506. TDI_CONNECTION_INFORMATION RequestInformation;
  507. TDI_CONNECTION_INFORMATION ReturnInformation;
  508. PTRANSPORT_ADDRESS ListenBlock;
  509. PTRANSPORT_ADDRESS ConnectBlock;
  510. PTDI_ADDRESS_NETBIOS temp;
  511. PUCHAR MessageBuffer;
  512. ULONG MessageBufferLength;
  513. ULONG CurrentBufferLength;
  514. PUCHAR SendBuffer;
  515. ULONG SendBufferLength;
  516. PIRP Irp;
  517. KEVENT Event1;
  518. Status = KeWaitForSingleObject (&TdiReceiveEvent, Suspended, KernelMode, FALSE, NULL);
  519. SendBufferLength = (ULONG)BUFFER_SIZE;
  520. MessageBufferLength = (ULONG)BUFFER_SIZE;
  521. DbgPrint( "\n****** Start of Receive Test ******\n" );
  522. XBuff = ExAllocatePool (NonPagedPool, BUFFER_SIZE);
  523. if (XBuff == (PVOID)NULL) {
  524. DbgPrint ("Unable to allocate nonpaged pool for send buffer exiting\n");
  525. return FALSE;
  526. }
  527. RBuff = ExAllocatePool (NonPagedPool, BUFFER_SIZE);
  528. if (RBuff == (PVOID)NULL) {
  529. DbgPrint ("Unable to allocate nonpaged pool for receive buffer exiting\n");
  530. return FALSE;
  531. }
  532. ListenBlock = ExAllocatePool (NonPagedPool, sizeof (TRANSPORT_ADDRESS) +
  533. sizeof (TDI_ADDRESS_NETBIOS));
  534. ConnectBlock = ExAllocatePool (NonPagedPool, sizeof (TRANSPORT_ADDRESS) +
  535. sizeof (TDI_ADDRESS_NETBIOS));
  536. ListenBlock->TAAddressCount = 1;
  537. ListenBlock->Address[0].AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  538. ListenBlock->Address[0].AddressLength = sizeof (TDI_ADDRESS_NETBIOS);
  539. temp = (PTDI_ADDRESS_NETBIOS)ListenBlock->Address[0].Address;
  540. temp->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  541. for (i=0;i<16;i++) {
  542. temp->NetbiosName[i] = ClientName[i];
  543. }
  544. ConnectBlock->TAAddressCount = 1;
  545. ConnectBlock->Address[0].AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  546. ConnectBlock->Address[0].AddressLength = sizeof (TDI_ADDRESS_NETBIOS);
  547. temp = (PTDI_ADDRESS_NETBIOS)ConnectBlock->Address[0].Address;
  548. temp->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  549. for (i=0;i<16;i++) {
  550. temp->NetbiosName[i] = ServerName[i];
  551. }
  552. //
  553. // Create an event for the synchronous I/O requests that we'll be issuing.
  554. //
  555. KeInitializeEvent (
  556. &Event1,
  557. SynchronizationEvent,
  558. FALSE);
  559. Status = TtdiOpenAddress (&SvrHandle, ServerName);
  560. if (!NT_SUCCESS(Status)) {
  561. DbgPrint( "\n****** Receive Test: FAILED on open of server Address: %lC ******\n", Status );
  562. return FALSE;
  563. }
  564. Status = ObReferenceObjectByHandle (
  565. SvrHandle,
  566. 0L,
  567. NULL,
  568. KernelMode,
  569. (PVOID *) &AddressObject,
  570. NULL);
  571. if (!NT_SUCCESS(Status)) {
  572. DbgPrint( "\n****** Receive Test: FAILED on open of server Address: %lC ******\n", Status );
  573. return FALSE;
  574. }
  575. Status = TtdiOpenConnection (&SvrConnectionHandle, 2);
  576. if (!NT_SUCCESS(Status)) {
  577. DbgPrint( "\n****** Receive Test: FAILED on open of server Connection: %lC ******\n", Status );
  578. return FALSE;
  579. }
  580. Status = ObReferenceObjectByHandle (
  581. SvrConnectionHandle,
  582. 0L,
  583. NULL,
  584. KernelMode,
  585. (PVOID *) &ConnectionObject,
  586. NULL);
  587. if (!NT_SUCCESS(Status)) {
  588. DbgPrint( "\n****** Receive Test: FAILED on open of server Connection: %lC ******\n", Status );
  589. return FALSE;
  590. }
  591. //
  592. // Get a pointer to the stack location for the first driver. This will be
  593. // used to pass the original function codes and parameters.
  594. //
  595. DeviceObject = IoGetRelatedDeviceObject( ConnectionObject );
  596. Irp = TdiBuildInternalDeviceControlIrp (
  597. TDI_ASSOCIATE_ADDRESS,
  598. DeviceObject,
  599. ConnectionObject,
  600. &Event1,
  601. &Iosb1);
  602. DbgPrint ("Build Irp %lx, Handle %lx \n",
  603. Irp, SvrHandle);
  604. TdiBuildAssociateAddress (
  605. Irp,
  606. DeviceObject,
  607. ConnectionObject,
  608. TSTRCVCompletion,
  609. &Event1,
  610. SvrHandle);
  611. InitWaitObject (&Event1);
  612. {
  613. PULONG Temp=(PULONG)IoGetNextIrpStackLocation (Irp);
  614. DbgPrint ("Built IrpSp %lx %lx %lx %lx %lx \n", *(Temp++), *(Temp++),
  615. *(Temp++), *(Temp++), *(Temp++));
  616. }
  617. Status = IoCallDriver (DeviceObject, Irp);
  618. // IoFreeIrp (Irp);
  619. if (Status == STATUS_PENDING) {
  620. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  621. if (!NT_SUCCESS(Status)) {
  622. DbgPrint( "\n****** Receive Test: FAILED Event1 Wait Associate: %lC ******\n", Status );
  623. return FALSE;
  624. }
  625. if (!NT_SUCCESS(Iosb1.Status)) {
  626. DbgPrint( "\n****** Receive Test: FAILED Associate Iosb status: %lC ******\n", Status );
  627. return FALSE;
  628. }
  629. } else {
  630. if (!NT_SUCCESS (Status)) {
  631. DbgPrint( "\n****** Receive Test: AssociateAddress FAILED Status: %lC ******\n", Status );
  632. return FALSE;
  633. } else {
  634. DbgPrint ("********** Receive Test: Success AssociateAddress\n");
  635. }
  636. }
  637. RequestInformation.RemoteAddress = ConnectBlock;
  638. RequestInformation.RemoteAddressLength = sizeof (TRANSPORT_ADDRESS) +
  639. sizeof (TDI_ADDRESS_NETBIOS);
  640. //
  641. // Post a TdiListen to the server endpoint.
  642. //
  643. KeInitializeEvent (
  644. &Event1,
  645. SynchronizationEvent,
  646. FALSE);
  647. Irp = TdiBuildInternalDeviceControlIrp (
  648. TDI_LISTEN,
  649. DeviceObject,
  650. ConnectionObject,
  651. &Event1,
  652. &Iosb1);
  653. TdiBuildListen (
  654. Irp,
  655. DeviceObject,
  656. ConnectionObject,
  657. TSTRCVCompletion,
  658. &Event1,
  659. 0,
  660. &RequestInformation,
  661. &ReturnInformation);
  662. InitWaitObject (&Event1);
  663. Status = IoCallDriver (DeviceObject, Irp);
  664. // IoFreeIrp (Irp);
  665. if (Status == STATUS_PENDING) {
  666. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  667. if (!NT_SUCCESS(Status)) {
  668. DbgPrint( "\n****** Receive Test: FAILED Event1 Wait Listen: %lC ******\n", Status );
  669. return FALSE;
  670. }
  671. if (!NT_SUCCESS(Iosb1.Status)) {
  672. DbgPrint( "\n****** Receive Test: FAILED Listen Iosb status: %lC ******\n", Status );
  673. return FALSE;
  674. } else {
  675. DbgPrint ("********** Receive Test: Success Listen IOSB\n");
  676. }
  677. } else {
  678. if (!NT_SUCCESS (Status)) {
  679. DbgPrint( "\n****** Receive Test: Listen FAILED Status: %lC ******\n", Status );
  680. return FALSE;
  681. } else {
  682. DbgPrint ("********** Receive Test: Success Listen Immediate\n");
  683. }
  684. }
  685. DbgPrint ("\n****** Receive Test: LISTEN just completed! ******\n");
  686. KeInitializeEvent (
  687. &Event1,
  688. SynchronizationEvent,
  689. FALSE);
  690. Irp = TdiBuildInternalDeviceControlIrp (
  691. TDI_ACCEPT,
  692. DeviceObject,
  693. ConnectionObject,
  694. &Event1,
  695. &Iosb1);
  696. TdiBuildAccept (Irp, DeviceObject, ConnectionObject, NULL, NULL, &RequestInformation, NULL, 0);
  697. InitWaitObject (&Event1);
  698. Status = IoCallDriver (DeviceObject, Irp);
  699. // IoFreeIrp (Irp);
  700. if (Status == STATUS_PENDING) {
  701. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  702. if (!NT_SUCCESS(Status)) {
  703. DbgPrint( "\n****** Receive Test: FAILED Event1 Wait Accept: %lC ******\n", Status );
  704. return FALSE;
  705. }
  706. if (!NT_SUCCESS(Iosb1.Status)) {
  707. DbgPrint( "\n****** Receive Test: FAILED Accept Iosb status: %lC ******\n", Status );
  708. return FALSE;
  709. }
  710. } else {
  711. if (!NT_SUCCESS (Status)) {
  712. DbgPrint( "\n****** Receive Test: Accept FAILED Status: %lC ******\n", Status );
  713. return FALSE;
  714. }
  715. }
  716. //
  717. // We have the connection data now. Sanity check it.
  718. //
  719. DbgPrint ("\n****** Receive Test: LISTEN completed successfully! ******\n");
  720. //
  721. // Receive/receive 1 or 10 messages.
  722. //
  723. SendBuffer = (PUCHAR)ExAllocatePool (NonPagedPool, SendBufferLength);
  724. if (SendBuffer == NULL) {
  725. DbgPrint ("\n****** Send Test: ExAllocatePool failed! ******\n");
  726. }
  727. SendMdl = IoAllocateMdl (SendBuffer, SendBufferLength, FALSE, FALSE, NULL);
  728. MmBuildMdlForNonPagedPool (SendMdl);
  729. MessageBuffer=(PUCHAR)ExAllocatePool (NonPagedPool, MessageBufferLength);
  730. if (MessageBuffer == NULL) {
  731. DbgPrint ("\n****** Send Test: ExAllocatePool failed! ******\n");
  732. }
  733. ReceiveMdl = IoAllocateMdl (MessageBuffer, MessageBufferLength, FALSE, FALSE, NULL);
  734. MmBuildMdlForNonPagedPool (ReceiveMdl);
  735. //
  736. // Cycle the buffer length from 0 up through the maximum for Tdi. after a
  737. // couple of shots at the full range in one byte steps, increment by ever
  738. // increasing amounts to get to the max.
  739. //
  740. CurrentBufferLength = 0;
  741. Increment = 1;
  742. for (Iteration=1; Iteration<(USHORT)c9_Iteration; Iteration++) {
  743. CurrentBufferLength += Increment;
  744. if (CurrentBufferLength > MessageBufferLength) {
  745. CurrentBufferLength = 0;
  746. Increment = 1;
  747. }
  748. if (CurrentBufferLength > 7500) {
  749. Increment++;
  750. }
  751. if ((USHORT)((Iteration / 100) * 100) == Iteration) {
  752. DbgPrint ("Iteration #%d Buffer Length: %lx Buffer Start: %x\n",
  753. Iteration, CurrentBufferLength,Iteration % 256);
  754. }
  755. for (i=0; i<(USHORT)CurrentBufferLength; i++) {
  756. SendBuffer [i] = (UCHAR)(i + Iteration % 256 );
  757. MessageBuffer [i] = 0; // zap this with something.
  758. }
  759. KeInitializeEvent (
  760. &Event1,
  761. SynchronizationEvent,
  762. FALSE);
  763. Irp = TdiBuildInternalDeviceControlIrp (
  764. TDI_RECEIVE,
  765. DeviceObject,
  766. ConnectionObject,
  767. &Event1,
  768. &Iosb1);
  769. TdiBuildReceive (Irp,
  770. DeviceObject,
  771. ConnectionObject,
  772. TSTRCVCompletion,
  773. &Event1,
  774. ReceiveMdl,
  775. MessageBufferLength);
  776. InitWaitObject (&Event1);
  777. Status = IoCallDriver (DeviceObject, Irp);
  778. // IoFreeIrp (Irp);
  779. if (Status == STATUS_PENDING) {
  780. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  781. if (!NT_SUCCESS(Status)) {
  782. DbgPrint( "\n****** Receive Test: FAILED Event1 Wait Receive: %lC ******\n", Status );
  783. return FALSE;
  784. }
  785. if (!NT_SUCCESS(Iosb1.Status)) {
  786. DbgPrint( "\n****** Receive Test: FAILED Receive Iosb status: %lC ******\n", Status );
  787. return FALSE;
  788. }
  789. } else {
  790. if (!NT_SUCCESS (Status)) {
  791. DbgPrint( "\n****** Receive Test: Listen FAILED Status: %lC ******\n", Status );
  792. return FALSE;
  793. }
  794. }
  795. //
  796. // The receive completed. Make sure the data is correct.
  797. //
  798. if (Iosb1.Information != CurrentBufferLength) {
  799. DbgPrint ("Iteration #%d Buffer Length: %lx Buffer Start: %x\n",
  800. Iteration, CurrentBufferLength,Iteration % 256);
  801. DbgPrint ("ReceiveTest: Bytes received <> bytes sent.\n");
  802. DbgPrint ("ReceiveTest: BytesToSend=%ld. BytesReceived=%ld.\n",
  803. CurrentBufferLength, Iosb1.Information);
  804. }
  805. if (i == (USHORT)CurrentBufferLength) {
  806. // DbgPrint ("ReceiveTest: Message contains correct data.\n");
  807. } else {
  808. DbgPrint ("ReceiveTest: Message data corrupted at offset %lx of %lx.\n", (ULONG)i, (ULONG)SendBufferLength);
  809. DbgPrint ("ReceiveTest: Data around corrupted location:\n");
  810. for (j=-4;j<=3;j++) {
  811. DbgPrint ("%08lx ", (ULONG) i+j*16);
  812. for (k=(SHORT)i+(j*(SHORT)16);k<(SHORT)i+((j+(SHORT)1)*(SHORT)16);k++) {
  813. DbgPrint ("%02x",MessageBuffer [k]);
  814. }
  815. for (k=(SHORT)i+(j*(SHORT)16);k<(SHORT)i+((j+(SHORT)1)*(SHORT)16);k++) {
  816. DbgPrint ("%c",MessageBuffer [k]);
  817. }
  818. DbgPrint ("\n");
  819. }
  820. DbgPrint ("ReceiveTest: End of Corrupt Data.\n");
  821. }
  822. }
  823. //
  824. // We're done with this endpoint. Close it and get out.
  825. //
  826. Status = CloseAddress (SvrHandle);
  827. if (!NT_SUCCESS(Status)) {
  828. DbgPrint( "\n****** Receive Test: FAILED on 1st Close: %lC ******\n", Status );
  829. return FALSE;
  830. }
  831. DbgPrint( "\n****** End of Receive Test ******\n" );
  832. return TRUE;
  833. } /* Receive */
  834. BOOLEAN
  835. TtdiServer()
  836. {
  837. USHORT i;
  838. HANDLE RdrHandle, SrvConnectionHandle;
  839. KEVENT Event1;
  840. PFILE_OBJECT AddressObject, ConnectionObject;
  841. PDEVICE_OBJECT DeviceObject;
  842. NTSTATUS Status;
  843. PMDL ReceiveMdl;
  844. IO_STATUS_BLOCK Iosb1;
  845. TDI_CONNECTION_INFORMATION RequestInformation;
  846. TDI_CONNECTION_INFORMATION ReturnInformation;
  847. PTRANSPORT_ADDRESS ListenBlock;
  848. PTRANSPORT_ADDRESS ConnectBlock;
  849. PTDI_ADDRESS_NETBIOS temp;
  850. PUCHAR MessageBuffer;
  851. ULONG MessageBufferLength;
  852. ULONG CurrentBufferLength;
  853. PIRP Irp;
  854. Status = KeWaitForSingleObject (&TdiServerEvent, Suspended, KernelMode, FALSE, NULL);
  855. MessageBufferLength = (ULONG)BUFFER_SIZE;
  856. DbgPrint( "\n****** Start of Server Test ******\n" );
  857. RBuff = ExAllocatePool (NonPagedPool, BUFFER_SIZE);
  858. if (RBuff == (PVOID)NULL) {
  859. DbgPrint ("Unable to allocate nonpaged pool for receive buffer exiting\n");
  860. return FALSE;
  861. }
  862. ListenBlock = ExAllocatePool (NonPagedPool, sizeof (TRANSPORT_ADDRESS) +
  863. sizeof (TDI_ADDRESS_NETBIOS));
  864. ConnectBlock = ExAllocatePool (NonPagedPool, sizeof (TRANSPORT_ADDRESS) +
  865. sizeof (TDI_ADDRESS_NETBIOS));
  866. ListenBlock->TAAddressCount = 1;
  867. ListenBlock->Address[0].AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  868. ListenBlock->Address[0].AddressLength = sizeof (TDI_ADDRESS_NETBIOS);
  869. temp = (PTDI_ADDRESS_NETBIOS)ListenBlock->Address[0].Address;
  870. temp->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  871. for (i=0;i<16;i++) {
  872. temp->NetbiosName[i] = AnyName[i];
  873. }
  874. ConnectBlock->TAAddressCount = 1;
  875. ConnectBlock->Address[0].AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  876. ConnectBlock->Address[0].AddressLength = sizeof (TDI_ADDRESS_NETBIOS);
  877. temp = (PTDI_ADDRESS_NETBIOS)ConnectBlock->Address[0].Address;
  878. temp->NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  879. for (i=0;i<16;i++) {
  880. temp->NetbiosName[i] = ServerName[i];
  881. }
  882. //
  883. // Create an event for the synchronous I/O requests that we'll be issuing.
  884. //
  885. KeInitializeEvent (
  886. &Event1,
  887. SynchronizationEvent,
  888. FALSE);
  889. Status = TtdiOpenAddress (&RdrHandle, ServerName);
  890. if (!NT_SUCCESS(Status)) {
  891. DbgPrint( "\n****** Server Test: FAILED on open of client: %lC ******\n", Status );
  892. return FALSE;
  893. }
  894. Status = ObReferenceObjectByHandle (
  895. RdrHandle,
  896. 0L,
  897. NULL,
  898. KernelMode,
  899. (PVOID *) &AddressObject,
  900. NULL);
  901. //
  902. // Now loop forever trying to get a connection from a remote client to
  903. // this server. We will create connections until we run out of resources,
  904. // and we will echo the data we are sent back along the same connection.
  905. // Sends and Receives are always asynchronous, while listens are
  906. // synchronous.
  907. //
  908. while (TRUE) {
  909. //
  910. // Open the connection on the transport.
  911. //
  912. Status = TtdiOpenConnection (&SrvConnectionHandle, 1);
  913. if (!NT_SUCCESS(Status)) {
  914. DbgPrint( "\n****** Server Test: FAILED on open of server Connection: %lC ******\n", Status );
  915. return FALSE;
  916. }
  917. Status = ObReferenceObjectByHandle (
  918. SrvConnectionHandle,
  919. 0L,
  920. NULL,
  921. KernelMode,
  922. (PVOID *) &ConnectionObject,
  923. NULL);
  924. if (!NT_SUCCESS(Status)) {
  925. DbgPrint( "\n****** Server Test: FAILED on open of server Connection: %lC ******\n", Status );
  926. return FALSE;
  927. }
  928. //
  929. // Get a pointer to the stack location for the first driver. This will be
  930. // used to pass the original function codes and parameters.
  931. //
  932. DeviceObject = IoGetRelatedDeviceObject( ConnectionObject );
  933. //
  934. // Now register the device handler for receives
  935. //
  936. // Irp = TdiBuildInternalDeviceControlIrp (
  937. // TDI_SET_EVENT_HANDLER,
  938. // DeviceObject,
  939. // ConnectionObject,
  940. // &Event1,
  941. // &Iosb1);
  942. // TdiBuildSetEventHandler (Irp,
  943. // DeviceObject,
  944. // ConnectionObject,
  945. // TSTRCVCompletion,
  946. // &Event1,
  947. // TDI_RECEIVE_HANDLER,
  948. // TdiTestReceiveHandler,
  949. // ConnectionObject);
  950. // Status = IoCallDriver (DeviceObject, Irp);
  951. // if (Status == STATUS_PENDING) {
  952. // Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  953. // if (!NT_SUCCESS(Status)) {
  954. // DbgPrint( "\n****** Server Test: FAILED Event1 Wait Register: %lC ******\n", Status );
  955. // return FALSE;
  956. // }
  957. // if (!NT_SUCCESS(Iosb1.Status)) {
  958. // DbgPrint( "\n****** Server Test: FAILED Register Iosb status: %lC ******\n", Status );
  959. // return FALSE;
  960. // }
  961. //
  962. // } else {
  963. // if (!NT_SUCCESS (Status)) {
  964. // DbgPrint( "\n****** Server Test: RegisterHandler FAILED Status: %lC ******\n", Status );
  965. // return FALSE;
  966. // }
  967. // }
  968. Irp = TdiBuildInternalDeviceControlIrp (
  969. TDI_ASSOCIATE_ADDRESS,
  970. DeviceObject,
  971. ConnectionObject,
  972. &Event1,
  973. &Iosb1);
  974. TdiBuildAssociateAddress (Irp,
  975. DeviceObject,
  976. ConnectionObject,
  977. TSTRCVCompletion,
  978. &Event1,
  979. RdrHandle);
  980. Status = IoCallDriver (DeviceObject, Irp);
  981. // IoFreeIrp (Irp);
  982. if (Status == STATUS_PENDING) {
  983. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  984. if (!NT_SUCCESS(Status)) {
  985. DbgPrint( "\n****** Server Test: FAILED Event1 Wait Associate: %lC ******\n", Status );
  986. return FALSE;
  987. }
  988. if (!NT_SUCCESS(Iosb1.Status)) {
  989. DbgPrint( "\n****** Server Test: FAILED Associate Iosb status: %lC ******\n", Status );
  990. return FALSE;
  991. }
  992. } else {
  993. if (!NT_SUCCESS (Status)) {
  994. DbgPrint( "\n****** Server Test: AssociateAddress FAILED Status: %lC ******\n", Status );
  995. return FALSE;
  996. }
  997. }
  998. //
  999. // Post a TdiListen to the server endpoint.
  1000. //
  1001. RequestInformation.RemoteAddress = ListenBlock;
  1002. RequestInformation.RemoteAddressLength = sizeof (TRANSPORT_ADDRESS) +
  1003. sizeof (TDI_ADDRESS_NETBIOS);
  1004. KeInitializeEvent (
  1005. &Event1,
  1006. SynchronizationEvent,
  1007. FALSE);
  1008. Irp = TdiBuildInternalDeviceControlIrp (
  1009. TDI_LISTEN,
  1010. DeviceObject,
  1011. ConnectionObject,
  1012. &Event1,
  1013. &Iosb1);
  1014. TdiBuildListen (
  1015. Irp,
  1016. DeviceObject,
  1017. ConnectionObject,
  1018. TSTRCVCompletion,
  1019. &Event1,
  1020. 0,
  1021. &RequestInformation,
  1022. NULL);
  1023. Status = IoCallDriver (DeviceObject, Irp);
  1024. if (Status == STATUS_PENDING) {
  1025. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  1026. if (!NT_SUCCESS(Status)) {
  1027. DbgPrint( "\n****** Server Test: FAILED Event1 Wait Listen: %lC ******\n", Status );
  1028. return FALSE;
  1029. }
  1030. if (!NT_SUCCESS(Iosb1.Status)) {
  1031. DbgPrint( "\n****** Server Test: FAILED Listen Iosb status: %lC ******\n", Status );
  1032. return FALSE;
  1033. }
  1034. } else {
  1035. if (!NT_SUCCESS (Status)) {
  1036. DbgPrint( "\n****** Server Test: Listen FAILED Status: %lC ******\n", Status );
  1037. return FALSE;
  1038. }
  1039. }
  1040. DbgPrint ("\n****** Server Test: LISTEN just completed! ******\n");
  1041. //
  1042. // accept the connection from the remote
  1043. //
  1044. KeInitializeEvent (
  1045. &Event1,
  1046. SynchronizationEvent,
  1047. FALSE);
  1048. Irp = TdiBuildInternalDeviceControlIrp (
  1049. TDI_ACCEPT,
  1050. DeviceObject,
  1051. ConnectionObject,
  1052. &Event1,
  1053. &Iosb1);
  1054. TdiBuildAccept (
  1055. Irp,
  1056. DeviceObject,
  1057. ConnectionObject,
  1058. NULL,
  1059. NULL,
  1060. &RequestInformation,
  1061. NULL,
  1062. 0);
  1063. Status = IoCallDriver (DeviceObject, Irp);
  1064. // IoFreeIrp (Irp);
  1065. if (Status == STATUS_PENDING) {
  1066. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  1067. if (!NT_SUCCESS(Status)) {
  1068. DbgPrint( "\n****** Receive Test: FAILED Event1 Wait Accept: %lC ******\n", Status );
  1069. return FALSE;
  1070. }
  1071. if (!NT_SUCCESS(Iosb1.Status)) {
  1072. DbgPrint( "\n****** Receive Test: FAILED Accept Iosb status: %lC ******\n", Status );
  1073. return FALSE;
  1074. }
  1075. } else {
  1076. if (!NT_SUCCESS (Status)) {
  1077. DbgPrint( "\n****** Accept Test: Listen FAILED Status: %lC ******\n", Status );
  1078. return FALSE;
  1079. }
  1080. }
  1081. //
  1082. // Get a buffer for the continued read/write loop.
  1083. //
  1084. MessageBuffer=(PUCHAR)ExAllocatePool (NonPagedPool, MessageBufferLength);
  1085. if (MessageBuffer == NULL) {
  1086. DbgPrint ("\n****** Send Test: ExAllocatePool failed! ******\n");
  1087. }
  1088. ReceiveMdl = IoAllocateMdl (MessageBuffer, MessageBufferLength, FALSE, FALSE, NULL);
  1089. MmBuildMdlForNonPagedPool (ReceiveMdl);
  1090. //
  1091. // have a receive buffer, and a connection; go ahead and read and write
  1092. // until the remote disconnects.
  1093. //
  1094. while (TRUE) {
  1095. KeInitializeEvent (
  1096. &Event1,
  1097. SynchronizationEvent,
  1098. FALSE);
  1099. Irp = TdiBuildInternalDeviceControlIrp (
  1100. TDI_RECEIVE,
  1101. DeviceObject,
  1102. ConnectionObject,
  1103. &Event1,
  1104. &Iosb1);
  1105. TdiBuildReceive (Irp,
  1106. DeviceObject,
  1107. ConnectionObject,
  1108. TSTRCVCompletion,
  1109. &Event1,
  1110. ReceiveMdl,
  1111. MessageBufferLength);
  1112. InitWaitObject (&Event1);
  1113. Status = IoCallDriver (DeviceObject, Irp);
  1114. if (Status == STATUS_PENDING) {
  1115. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  1116. if (!NT_SUCCESS(Status)) {
  1117. DbgPrint( "\n****** Receive Test: FAILED Event1 Wait Receive: %lC ******\n", Status );
  1118. return FALSE;
  1119. }
  1120. if (!NT_SUCCESS(Iosb1.Status)) {
  1121. DbgPrint( "\n****** Receive Test: FAILED Receive Iosb status: %lC ******\n", Status );
  1122. return FALSE;
  1123. }
  1124. } else {
  1125. if (!NT_SUCCESS (Status)) {
  1126. //
  1127. // Check to see if the remote has disconnected, which is
  1128. // the only reason for us shutting down/
  1129. //
  1130. if (Status == STATUS_REMOTE_DISCONNECT) {
  1131. //
  1132. // We've been disconnected from; get out
  1133. //
  1134. NtClose (SrvConnectionHandle);
  1135. break;
  1136. }
  1137. DbgPrint( "\n****** Receive Test: Listen FAILED Status: %lC ******\n", Status );
  1138. return FALSE;
  1139. } else {
  1140. //
  1141. // successful return, what length is the data?
  1142. //
  1143. CurrentBufferLength = Iosb1.Information;
  1144. }
  1145. }
  1146. //
  1147. // send the data back
  1148. //
  1149. KeInitializeEvent (
  1150. &Event1,
  1151. SynchronizationEvent,
  1152. FALSE);
  1153. Irp = TdiBuildInternalDeviceControlIrp (
  1154. TDI_SEND,
  1155. DeviceObject,
  1156. ConnectionObject,
  1157. &Event1,
  1158. &Iosb1);
  1159. TdiBuildSend (Irp,
  1160. DeviceObject,
  1161. ConnectionObject,
  1162. TSTRCVCompletion,
  1163. &Event1,
  1164. ReceiveMdl,
  1165. 0,
  1166. CurrentBufferLength);
  1167. Status = IoCallDriver (DeviceObject, Irp);
  1168. if (Status == STATUS_PENDING) {
  1169. Status = KeWaitForSingleObject (&Event1, Suspended, KernelMode, TRUE, NULL);
  1170. if (!NT_SUCCESS(Status)) {
  1171. DbgPrint( "\n****** Receive Test: FAILED Event1 Wait Send: %lC ******\n", Status );
  1172. return FALSE;
  1173. }
  1174. if (!NT_SUCCESS(Iosb1.Status)) {
  1175. DbgPrint( "\n****** Receive Test: FAILED Send Iosb status: %lC ******\n", Status );
  1176. return FALSE;
  1177. }
  1178. } else {
  1179. if (!NT_SUCCESS (Status)) {
  1180. DbgPrint( "\n****** Receive Test: Send FAILED Status: %lC ******\n", Status );
  1181. NtClose (SrvConnectionHandle);
  1182. break;
  1183. }
  1184. }
  1185. } // end of receive/send while
  1186. IoFreeMdl (ReceiveMdl);
  1187. ExFreePool (MessageBuffer);
  1188. }
  1189. //
  1190. // We're done with this address. Close it and get out.
  1191. //
  1192. Status = CloseAddress (RdrHandle);
  1193. if (!NT_SUCCESS(Status)) {
  1194. DbgPrint( "\n****** Send Test: FAILED on 2nd Close: %lC ******\n", Status );
  1195. return FALSE;
  1196. }
  1197. DbgPrint( "\n****** End of Send Test ******\n" );
  1198. return TRUE;
  1199. } /* Server */