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.

343 lines
9.0 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. tnetbios.c
  5. Abstract:
  6. This module contains code which exercises the NetBIOS dll and driver.
  7. Author:
  8. Colin Watson (ColinW) 13-Mar-1991
  9. Environment:
  10. Application mode
  11. Revision History:
  12. Dave Beaver (DBeaver) 10 August 1991
  13. Modify to support multiple LAN numbers
  14. Jerome Nantel (w-jeromn) 23 August 1991
  15. Add Event Signaling testing
  16. --*/
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #define WIN32_CONSOLE_APP
  21. #include <windows.h>
  22. #include <nb30.h>
  23. #include <stdio.h>
  24. // 1234567890123456
  25. #define SPACES " "
  26. #define TIMEOUT 60000 // Time out for wait, set at 1 minute
  27. #define Hi "Come here Dave, I need you"
  28. #define ClearNcb( PNCB ) { \
  29. RtlZeroMemory( PNCB , sizeof (NCB) ); \
  30. RtlMoveMemory( (PNCB)->ncb_name, SPACES, sizeof(SPACES)-1 );\
  31. RtlMoveMemory( (PNCB)->ncb_callname, SPACES, sizeof(SPACES)-1 );\
  32. }
  33. // Hard code lana-num that is mapped to XNS
  34. int Limit = 20;
  35. VOID
  36. usage (
  37. VOID
  38. )
  39. {
  40. printf("usage: tnetbios [-n:lan number][-h][-e] <remote computername> <my computername>\n");
  41. printf(" -n specifies the lan number (0 is the default)\n");
  42. printf(" -h specifies that addresses are hexadecimal numbers \n");
  43. printf(" rather than strings.\n");
  44. printf(" -e test event signaling for ASYNC calls. \n");
  45. printf(" final two arguments are the remote and local computer names.\n");
  46. }
  47. int
  48. main (argc, argv)
  49. int argc;
  50. char *argv[];
  51. {
  52. NCB myncb;
  53. CHAR Buffer[128];
  54. CHAR Buffer2[128];
  55. int i,j;
  56. CHAR localName[16];
  57. CHAR remoteName[16];
  58. CHAR localTemp[32];
  59. CHAR remoteTemp[32];
  60. ULONG lanNumber=0;
  61. BOOLEAN gotFirst=FALSE;
  62. BOOLEAN asHex=FALSE;
  63. BOOLEAN tevent=FALSE;
  64. UCHAR lsn;
  65. UCHAR name_number;
  66. HANDLE event;
  67. if ( argc < 3 || argc > 6) {
  68. usage ();
  69. return 1;
  70. }
  71. //
  72. // dbeaver: added switch to allow 32 byte hex string as name to facilitate
  73. // testing under unusual circumstances
  74. //
  75. for (j=1;j<16;j++ ) {
  76. localTemp[j] = ' ';
  77. remoteTemp[j] = ' ';
  78. }
  79. //
  80. // parse the switches
  81. //
  82. for (i=1;i<argc ;i++ ) {
  83. if (argv[i][0] == '-') {
  84. switch (argv[i][1]) {
  85. case 'n':
  86. if (!NT_SUCCESS(RtlCharToInteger (&argv[i][3], 10, &lanNumber))) {
  87. usage ();
  88. return 1;
  89. }
  90. break;
  91. case 'h':
  92. asHex = TRUE;
  93. break;
  94. case 'e':
  95. tevent = TRUE;
  96. break;
  97. default:
  98. usage ();
  99. return 1;
  100. break;
  101. }
  102. } else {
  103. //
  104. // not a switch must be a name
  105. //
  106. if (gotFirst != TRUE) {
  107. RtlMoveMemory (remoteTemp, argv[i], lstrlen( argv[i] ));
  108. gotFirst = TRUE;
  109. } else {
  110. RtlMoveMemory (localTemp, argv[i], lstrlen( argv[i] ));
  111. }
  112. }
  113. }
  114. if (asHex) {
  115. RtlZeroMemory (localName, 16);
  116. RtlZeroMemory (remoteName, 16);
  117. for (j=0;j<16 ;j+=4) {
  118. RtlCharToInteger (&localTemp[j*2], 16, (PULONG)&localName[j]);
  119. }
  120. for (j=0;j<16 ;j+=4) {
  121. RtlCharToInteger (&remoteTemp[j*2], 16, (PULONG)&remoteName[j]);
  122. }
  123. } else {
  124. for (j=1;j<16;j++ ) {
  125. localName[j] = ' ';
  126. remoteName[j] = ' ';
  127. }
  128. RtlMoveMemory( localName, localTemp, 16);
  129. RtlMoveMemory( remoteName, remoteTemp, 16);
  130. }
  131. if (tevent) {
  132. /* Testing event signaling to handle async calls */
  133. if (( event = CreateEvent( NULL, FALSE, FALSE, NULL )) == NULL ) {
  134. /* Could not get event handle. Abort */
  135. printf("Could not test event signaling.\n");
  136. tevent=FALSE;
  137. }
  138. }
  139. printf( "Starting NetBios\n" );
  140. {
  141. LANA_ENUM Enum;
  142. ClearNcb( &myncb );
  143. myncb.ncb_command = NCBENUM;
  144. myncb.ncb_lana_num = (UCHAR)lanNumber;
  145. myncb.ncb_length = sizeof(Enum);
  146. myncb.ncb_buffer = &Enum;
  147. Netbios( &myncb );
  148. if ( myncb.ncb_retcode != NRC_GOODRET ) {
  149. printf( " Enum Failed %x\n", myncb.ncb_retcode );
  150. return 1;
  151. }
  152. }
  153. // Reset
  154. ClearNcb( &myncb );
  155. myncb.ncb_command = NCBRESET;
  156. myncb.ncb_lsn = 0; // Request resources
  157. myncb.ncb_lana_num = (UCHAR)lanNumber;
  158. myncb.ncb_callname[0] = 0; // 16 sessions
  159. myncb.ncb_callname[1] = 0; // 16 commands
  160. myncb.ncb_callname[2] = 0; // 8 names
  161. Netbios( &myncb );
  162. // Add name
  163. ClearNcb( &myncb );
  164. myncb.ncb_command = NCBADDNAME;
  165. RtlMoveMemory( myncb.ncb_name, localName, lstrlen(localName));
  166. myncb.ncb_lana_num = (UCHAR)lanNumber;
  167. Netbios( &myncb );
  168. if ( myncb.ncb_retcode != NRC_GOODRET ) {
  169. printf( " Addname Failed %x\n", myncb.ncb_retcode );
  170. return 1;
  171. }
  172. name_number = myncb.ncb_num;
  173. {
  174. struct {
  175. ADAPTER_STATUS AdapterInfo;
  176. NAME_BUFFER Names[16];
  177. } AdapterStatus;
  178. // Adapter Status
  179. ClearNcb( &myncb );
  180. myncb.ncb_command = NCBASTAT;
  181. myncb.ncb_lana_num = (UCHAR)lanNumber;
  182. myncb.ncb_length = sizeof( AdapterStatus );
  183. myncb.ncb_buffer = (CHAR *)&AdapterStatus;
  184. Netbios( &myncb );
  185. }
  186. if ( myncb.ncb_retcode != NRC_GOODRET ) {
  187. printf( " Adapter Status Failed %x\n", myncb.ncb_retcode );
  188. return 1;
  189. }
  190. for ( j = 0; j <= Limit; j++ ) {
  191. // Call
  192. ClearNcb( &myncb );
  193. myncb.ncb_command = NCBCALL | ASYNCH;
  194. RtlMoveMemory( myncb.ncb_name, localName, lstrlen(localName ));
  195. RtlMoveMemory( myncb.ncb_callname, remoteName, lstrlen( remoteName ));
  196. myncb.ncb_lana_num = (UCHAR)lanNumber;
  197. myncb.ncb_rto = myncb.ncb_rto = 0;//10; 10*500 milliseconds timeout
  198. myncb.ncb_num = name_number;
  199. if ( tevent ) myncb.ncb_event = event;
  200. while ( TRUE) {
  201. printf("\nStart NCB CALL ");
  202. Netbios( &myncb );
  203. printf( " Call returned " );
  204. if ( tevent ) {
  205. if ( WaitForSingleObject( event, TIMEOUT ) ) {
  206. // Wait timed out, no return
  207. printf("ERROR: Wait timed out, event not signaled.\n");
  208. }
  209. } else {
  210. while ( myncb.ncb_cmd_cplt == NRC_PENDING ) {
  211. printf( "." );
  212. Sleep(500);
  213. }
  214. }
  215. lsn = myncb.ncb_lsn;
  216. if ( myncb.ncb_retcode == NRC_GOODRET ) {
  217. // Success
  218. printf( " Call completed\n" );
  219. break;
  220. }
  221. printf( " Call completed with error %lx, retry\n", myncb.ncb_retcode );
  222. }
  223. for ( i = 0; i <=j ; i++ ) {
  224. // Send
  225. ClearNcb( &myncb );
  226. myncb.ncb_command = NCBSEND;
  227. myncb.ncb_lana_num = (UCHAR)lanNumber;
  228. myncb.ncb_length = sizeof( Hi );
  229. myncb.ncb_buffer = Buffer;
  230. myncb.ncb_lsn = lsn;
  231. RtlMoveMemory( Buffer, Hi, sizeof( Hi ));
  232. Netbios( &myncb );
  233. if ( myncb.ncb_retcode != NRC_GOODRET ) {
  234. break;
  235. }
  236. // Receive
  237. ClearNcb( &myncb );
  238. myncb.ncb_command = NCBRECV | ASYNCH;
  239. myncb.ncb_lana_num = (UCHAR)lanNumber;
  240. myncb.ncb_length = sizeof( Buffer2 );
  241. myncb.ncb_buffer = Buffer2;
  242. myncb.ncb_lsn = lsn;
  243. if ( tevent ) myncb.ncb_event = event;
  244. Netbios( &myncb );
  245. printf( "R" );
  246. if ( tevent ) {
  247. if ( WaitForSingleObject( event, TIMEOUT ) ) {
  248. // Wait timed out, no return
  249. printf("ERROR: Wait timed out, event not signaled.\n");
  250. }
  251. } else {
  252. while ( myncb.ncb_cmd_cplt == NRC_PENDING ) {
  253. printf( "." );
  254. Sleep(500);
  255. }
  256. }
  257. printf( "r" );
  258. if ( myncb.ncb_retcode != NRC_GOODRET ) {
  259. break;
  260. }
  261. // printf( ":%s\n", Buffer2);
  262. }
  263. // Hangup
  264. ClearNcb( &myncb );
  265. myncb.ncb_command = NCBHANGUP;
  266. myncb.ncb_lana_num = (UCHAR)lanNumber;
  267. myncb.ncb_lsn = lsn;
  268. Netbios( &myncb );
  269. if ( myncb.ncb_retcode != NRC_GOODRET ) {
  270. break;
  271. }
  272. }
  273. // Reset
  274. ClearNcb( &myncb );
  275. myncb.ncb_command = NCBRESET;
  276. myncb.ncb_lsn = 1; // Free resources
  277. myncb.ncb_lana_num = (UCHAR)lanNumber;
  278. Netbios( &myncb );
  279. printf( "Ending NetBios\n" );
  280. if (tevent ) {
  281. // Close handle
  282. CloseHandle( event );
  283. }
  284. return 0;
  285. }