Leaked source code of windows server 2003
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.

457 lines
14 KiB

  1. #include "ipxdefs.h"
  2. #if DBG
  3. #define ASSERTERR(exp) \
  4. if (!(exp)) { \
  5. DbgPrint("Get last error= %d\n", GetLastError ()); \
  6. RtlAssert( #exp, __FILE__, __LINE__, NULL ); \
  7. }
  8. #else
  9. #define ASSERTERR(exp)
  10. #endif
  11. #if DBG
  12. #define ASSERTERRMSG(msg,exp) \
  13. if (!(exp)) { \
  14. DbgPrint("Get last error= %d\n", GetLastError ()); \
  15. RtlAssert( #exp, __FILE__, __LINE__, msg ); \
  16. }
  17. #else
  18. #define ASSERTERRMSG(msg,exp)
  19. #endif
  20. typedef struct _SERVICE_ENTRY {
  21. USHORT type;
  22. BYTE name[48];
  23. IPX_ADDRESS_BLOCK addr;
  24. USHORT hops;
  25. } SERVICE_ENTRY, *PSERVICE_ENTRY;
  26. typedef struct _SAP_PACKET {
  27. OVERLAPPED ovrp;
  28. LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine;
  29. ADDRESS_RESERVED rsvd;
  30. IPX_HEADER hdr;
  31. union {
  32. UCHAR data[576-sizeof(IPX_HEADER)];
  33. struct {
  34. USHORT operation;
  35. SERVICE_ENTRY entry[7];
  36. } sap;
  37. };
  38. } SAP_PACKET, *PSAP_PACKET;
  39. #define SAP_SOCKET_NUM 0x0452
  40. VOID CALLBACK
  41. SendCompletion (
  42. DWORD status,
  43. DWORD cbBytes,
  44. LPOVERLAPPED context
  45. );
  46. VOID CALLBACK
  47. RecvCompletion (
  48. DWORD status,
  49. DWORD cbBytes,
  50. LPOVERLAPPED context
  51. );
  52. BOOL WINAPI
  53. CtrlHandler (
  54. DWORD fdwCtrlType
  55. );
  56. static BOOL *AdapterArr;
  57. static SAP_PACKET recvPacket;
  58. static HANDLE hdl[3];
  59. static ULONG CurAdapter;
  60. static LONG NumActiveAdapters;
  61. static ADAPTERS_GLOBAL_PARAMETERS gParam;
  62. static HANDLE sockPort=INVALID_HANDLE_VALUE;
  63. static BOOL Stop;
  64. int __cdecl main (
  65. int argc,
  66. char **argv
  67. ) {
  68. HANDLE cfgHdl;
  69. WCHAR name[64];
  70. ULONG i, len=sizeof (name);
  71. ADAPTER_INFO params;
  72. DWORD status;
  73. ULONG adpStatus;
  74. PSAP_PACKET sendPacket;
  75. LONG nObjects=2;
  76. HANDLE compHdl;
  77. printf ("**** Starting Up...\n\n");
  78. hdl[0] = CreateEvent (NULL, // def security
  79. TRUE, // manual reset
  80. FALSE, // initially non-signaled
  81. NULL);
  82. ASSERTERRMSG ("Can't create Ipx event. ", hdl[0]!=NULL);
  83. hdl[1] = CreateEvent (NULL, // def security
  84. TRUE, // manual reset
  85. FALSE, // initially non-signaled
  86. NULL);
  87. ASSERTERRMSG ("Can't create console event. ", hdl[1]!=NULL);
  88. Stop = FALSE;
  89. status = SetConsoleCtrlHandler (
  90. &CtrlHandler,
  91. TRUE
  92. );
  93. ASSERTERRMSG ("Can't set console ctrl handler. ", status);
  94. printf ("Press CTRL+C at any time to terminate this program\n.");
  95. cfgHdl = IpxCreateAdapterConfigurationPort (
  96. hdl[0],
  97. &gParam);
  98. ASSERTERRMSG ("Can't create config port. ",
  99. cfgHdl!=INVALID_HANDLE_VALUE);
  100. ASSERTMSG ("No adapters available. ", gParam.AdaptersCount>0);
  101. AdapterArr = (BOOL *)GlobalAlloc (GMEM_FIXED, sizeof (BOOL)*(gParam.AdaptersCount+1));
  102. ASSERTERRMSG ("Can't allocate adapter array. ", AdapterArr!=NULL);
  103. for (i=0; i<=gParam.AdaptersCount; i++)
  104. AdapterArr[i] = FALSE;
  105. CurAdapter = 0xFFFFFFFF;
  106. NumActiveAdapters = -1;
  107. while (!Stop) {
  108. status=WaitForMultipleObjectsEx (
  109. nObjects,
  110. hdl,
  111. FALSE, // Wait for any object to complete
  112. INFINITE,
  113. TRUE // Alertable wait
  114. );
  115. switch (status) {
  116. case WAIT_IO_COMPLETION:
  117. break;
  118. case WAIT_OBJECT_0:
  119. while (IpxGetQueuedAdapterConfigurationStatus (
  120. cfgHdl,
  121. &i,
  122. &adpStatus,
  123. &params)==NO_ERROR) {
  124. switch (adpStatus) {
  125. case ADAPTER_DELETED:
  126. case ADAPTER_DOWN:
  127. printf ("\n*****Adapter # %d %s.*****", i,
  128. (adpStatus==ADAPTER_DELETED)
  129. ? "deleted" : "down");
  130. if (AdapterArr[i]) {
  131. AdapterArr[i] = FALSE;
  132. if (i!=0) {
  133. printf ("\n******Stopping adapter # %d******\n\n", i);
  134. if (InterlockedDecrement (&NumActiveAdapters)<0) {
  135. printf ("\n******Deleting socket port******\n\n");
  136. DeleteSocketPort (sockPort);
  137. sockPort = INVALID_HANDLE_VALUE;
  138. nObjects = 2;
  139. }
  140. }
  141. }
  142. break;
  143. case ADAPTER_CREATED:
  144. case ADAPTER_UP:
  145. len = sizeof (name);
  146. status = GetAdapterNameW (i, &len, name);
  147. ASSERTMSG ("GetAdapterNameW failed ", status==NO_ERROR);
  148. printf ("\n*****Adapter # %d (%ls) %s!*****\n\n",i,name,
  149. (adpStatus==ADAPTER_CREATED)
  150. ? "created" : "up");
  151. AdapterArr[i] = TRUE;
  152. if (i!=0) {
  153. printf ("\n******Starting adapter # %d******\n\n", i);
  154. if (InterlockedIncrement (&NumActiveAdapters)==0) {
  155. USHORT sockNum;
  156. printf ("\n******Creating socket port******\n\n");
  157. PUTUSHORT2SHORT (&sockNum, SAP_SOCKET_NUM);
  158. sockPort = CreateSocketPort (sockNum);
  159. ASSERTERR (sockPort!=INVALID_HANDLE_VALUE);
  160. compHdl = CreateIoCompletionPort (sockPort,
  161. NULL,
  162. 0,
  163. 0);
  164. ASSERTERR (compHdl!=NULL);
  165. hdl[2] = sockPort;
  166. nObjects = 3;
  167. recvPacket.ovrp.hEvent = NULL;
  168. recvPacket.CompletionRoutine = RecvCompletion;
  169. status = IpxRecvPacket (sockPort,
  170. (PUCHAR)&recvPacket.hdr,
  171. sizeof(SAP_PACKET)
  172. -FIELD_OFFSET (SAP_PACKET, hdr),
  173. &recvPacket.rsvd,
  174. &recvPacket.ovrp,
  175. NULL);
  176. ASSERTMSG ("Failed to start receive. ",
  177. status==NO_ERROR);
  178. }
  179. sendPacket = (PSAP_PACKET)GlobalAlloc (GMEM_FIXED,
  180. sizeof(SAP_PACKET));
  181. ASSERTERR (sendPacket!=NULL);
  182. PUTUSHORT2SHORT (&sendPacket->hdr.checksum,0xFFFF);
  183. PUTUSHORT2SHORT (&sendPacket->hdr.length, 34);
  184. sendPacket->hdr.transportctl = 0;
  185. sendPacket->hdr.pkttype = 0x04;
  186. memcpy (&sendPacket->hdr.dst.net, &params.Network, 4);
  187. if (params.NdisMedium==NdisMediumWan)
  188. memcpy (sendPacket->hdr.dst.node, params.RemoteNode, 6);
  189. else
  190. memset (sendPacket->hdr.dst.node, 0xFF, 6);
  191. PUTUSHORT2SHORT (&sendPacket->hdr.dst.socket, SAP_SOCKET_NUM);
  192. memcpy (sendPacket->hdr.src.net, params.Network, 4);
  193. memcpy (sendPacket->hdr.src.node, params.LocalNode, 6);
  194. PUTUSHORT2SHORT (&sendPacket->hdr.src.socket, SAP_SOCKET_NUM);
  195. PUTUSHORT2SHORT (&sendPacket->sap.operation, 3);
  196. PUTUSHORT2SHORT (&sendPacket->sap.entry[0].type, 0x0003);
  197. sendPacket->ovrp.hEvent = NULL;
  198. sendPacket->CompletionRoutine = SendCompletion;
  199. status = IpxSendPacket (sockPort,
  200. i,
  201. (PUCHAR)&sendPacket->hdr,
  202. 34,
  203. &sendPacket->rsvd,
  204. &sendPacket->ovrp,
  205. NULL);
  206. ASSERTMSG ("Failed to start send. ",
  207. status==NO_ERROR);
  208. }
  209. break;
  210. default:
  211. ASSERTMSG ("Unknown adapter status reported ", FALSE);
  212. }
  213. }
  214. break;
  215. case WAIT_OBJECT_0+1:
  216. break;
  217. case WAIT_OBJECT_0+2:
  218. if (IpxGetQueuedCompletionStatus (
  219. compHdl,
  220. &len,
  221. &i,
  222. (LPOVERLAPPED *)&sendPacket,
  223. 0) || (sendPacket!=NULL))
  224. (*sendPacket->CompletionRoutine) (
  225. sendPacket->ovrp.Internal,
  226. len,
  227. &sendPacket->ovrp);
  228. else
  229. fprintf (stderr,
  230. "Completion port is signalled,"
  231. " but no packets are waiting.\n");
  232. break;
  233. }
  234. }
  235. printf ("\n\n**** Exiting with status: %lX ****\n\n", status);
  236. NumActiveAdapters = -1;
  237. if (sockPort!=INVALID_HANDLE_VALUE)
  238. DeleteSocketPort (sockPort);
  239. IpxDeleteAdapterConfigurationPort (cfgHdl);
  240. GlobalFree (AdapterArr);
  241. NtClose (hdl[2]);
  242. CloseHandle (hdl[1]);
  243. CloseHandle (hdl[0]);
  244. return status;
  245. }
  246. BOOL WINAPI
  247. CtrlHandler (
  248. DWORD fdwCtrlType
  249. ) {
  250. switch (fdwCtrlType) {
  251. case CTRL_C_EVENT:
  252. Stop = TRUE;
  253. SetEvent (hdl[1]);
  254. return TRUE;
  255. /* CTRL+CLOSE: confirm that the user wants to exit. */
  256. case CTRL_CLOSE_EVENT:
  257. Stop = TRUE;
  258. SetEvent (hdl[1]);
  259. return TRUE;
  260. /* Pass other signals to the next handler. */
  261. case CTRL_BREAK_EVENT:
  262. case CTRL_LOGOFF_EVENT:
  263. case CTRL_SHUTDOWN_EVENT:
  264. default:
  265. Stop = TRUE;
  266. SetEvent (hdl[1]);
  267. return FALSE;
  268. }
  269. }
  270. VOID CALLBACK
  271. SendCompletion (
  272. DWORD status,
  273. DWORD cbBytes,
  274. LPOVERLAPPED context
  275. ) {
  276. #define sendPacket ((PSAP_PACKET)context)
  277. printf ("Status : %ld\n", status);
  278. printf ("Bytes sent : %d\n", cbBytes);
  279. printf ("Checksum : %04X\n", GETSHORT2USHORTdirect(&sendPacket->hdr.checksum));
  280. printf ("Length : %d\n", GETSHORT2USHORTdirect (&sendPacket->hdr.length));
  281. printf ("Hop count : %d\n", sendPacket->hdr.transportctl);
  282. printf ("Packet type : %02X\n", sendPacket->hdr.pkttype);
  283. printf ("Dest. net : %02X%02X%02X%02X\n",
  284. sendPacket->hdr.dst.net[0],
  285. sendPacket->hdr.dst.net[1],
  286. sendPacket->hdr.dst.net[2],
  287. sendPacket->hdr.dst.net[3]);
  288. printf ("Dest. node : %02X%02X%02X%02X%02X%02X\n",
  289. sendPacket->hdr.dst.node[0],
  290. sendPacket->hdr.dst.node[1],
  291. sendPacket->hdr.dst.node[2],
  292. sendPacket->hdr.dst.node[3],
  293. sendPacket->hdr.dst.node[4],
  294. sendPacket->hdr.dst.node[5]);
  295. printf ("Dest. socket : %04X\n", GETSHORT2USHORTdirect (&sendPacket->hdr.dst.socket));
  296. printf ("Source net : %02X%02X%02X%02X\n",
  297. sendPacket->hdr.src.net[0],
  298. sendPacket->hdr.src.net[1],
  299. sendPacket->hdr.src.net[2],
  300. sendPacket->hdr.src.net[3]);
  301. printf ("Source node : %02X%02X%02X%02X%02X%02X\n",
  302. sendPacket->hdr.src.node[0],
  303. sendPacket->hdr.src.node[1],
  304. sendPacket->hdr.src.node[2],
  305. sendPacket->hdr.src.node[3],
  306. sendPacket->hdr.src.node[4],
  307. sendPacket->hdr.src.node[5]);
  308. printf ("Source socket : %04X\n", GETSHORT2USHORTdirect (&sendPacket->hdr.src.socket));
  309. printf ("\n\n");
  310. GlobalFree (sendPacket);
  311. #undef sendPacket
  312. }
  313. VOID CALLBACK
  314. RecvCompletion (
  315. DWORD status,
  316. DWORD cbBytes,
  317. LPOVERLAPPED context
  318. ) {
  319. #define recvPacket ((PSAP_PACKET)context)
  320. CurAdapter = GetNicId(&recvPacket->rsvd);
  321. if ((CurAdapter>=0)
  322. && (CurAdapter<=gParam.AdaptersCount)
  323. && AdapterArr[CurAdapter]) {
  324. printf ("Adapter # : %d\n", CurAdapter);
  325. printf ("Status : %ld\n", status);
  326. printf ("Bytes received : %d\n", cbBytes);
  327. if (cbBytes>=(DWORD)(FIELD_OFFSET (SAP_PACKET, data)
  328. -FIELD_OFFSET(SAP_PACKET,hdr))) {
  329. printf ("Checksum : %04X\n", GETSHORT2USHORTdirect(&recvPacket->hdr.checksum));
  330. printf ("Length : %d\n", GETSHORT2USHORTdirect (&recvPacket->hdr.length));
  331. printf ("Hop count : %d\n", recvPacket->hdr.transportctl);
  332. printf ("Packet type : %02X\n", recvPacket->hdr.pkttype);
  333. printf ("Dest. net : %02X%02X%02X%02X\n",
  334. recvPacket->hdr.dst.net[0],
  335. recvPacket->hdr.dst.net[1],
  336. recvPacket->hdr.dst.net[2],
  337. recvPacket->hdr.dst.net[3]);
  338. printf ("Dest. node : %02X%02X%02X%02X%02X%02X\n",
  339. recvPacket->hdr.dst.node[0],
  340. recvPacket->hdr.dst.node[1],
  341. recvPacket->hdr.dst.node[2],
  342. recvPacket->hdr.dst.node[3],
  343. recvPacket->hdr.dst.node[4],
  344. recvPacket->hdr.dst.node[5]);
  345. printf ("Dest. socket : %04X\n", GETSHORT2USHORTdirect (&recvPacket->hdr.dst.socket));
  346. printf ("Source net : %02X%02X%02X%02X\n",
  347. recvPacket->hdr.src.net[0],
  348. recvPacket->hdr.src.net[1],
  349. recvPacket->hdr.src.net[2],
  350. recvPacket->hdr.src.net[3]);
  351. printf ("Source node : %02X%02X%02X%02X%02X%02X\n",
  352. recvPacket->hdr.src.node[0],
  353. recvPacket->hdr.src.node[1],
  354. recvPacket->hdr.src.node[2],
  355. recvPacket->hdr.src.node[3],
  356. recvPacket->hdr.src.node[4],
  357. recvPacket->hdr.src.node[5]);
  358. printf ("Source socket : %04X\n", GETSHORT2USHORTdirect (&recvPacket->hdr.src.socket));
  359. if (cbBytes>=(DWORD)(FIELD_OFFSET(SAP_PACKET,sap.entry[0])
  360. -FIELD_OFFSET(SAP_PACKET,hdr))) {
  361. INT j;
  362. printf ("SAP Operation : %d\n", GETSHORT2USHORTdirect (&recvPacket->sap.operation));
  363. for (j=0; (j<7) && (cbBytes>=(DWORD)FIELD_OFFSET (SAP_PACKET, sap.entry[j+1])); j++) {
  364. printf ("Server type : %04X\n", GETSHORT2USHORTdirect (&recvPacket->sap.entry[j].type));
  365. printf ("Server name : %.48s\n", recvPacket->sap.entry[j].name);
  366. printf ("Server net : %02X%02X%02X%02X\n",
  367. recvPacket->sap.entry[j].addr.net[0],
  368. recvPacket->sap.entry[j].addr.net[1],
  369. recvPacket->sap.entry[j].addr.net[2],
  370. recvPacket->sap.entry[j].addr.net[3]);
  371. printf ("Server node : %02X%02X%02X%02X%02X%02X\n",
  372. recvPacket->sap.entry[j].addr.node[0],
  373. recvPacket->sap.entry[j].addr.node[1],
  374. recvPacket->sap.entry[j].addr.node[2],
  375. recvPacket->sap.entry[j].addr.node[3],
  376. recvPacket->sap.entry[j].addr.node[4],
  377. recvPacket->sap.entry[j].addr.node[5]);
  378. printf ("Server socket : %04X\n", GETSHORT2USHORTdirect (&recvPacket->sap.entry[j].addr.socket));
  379. printf ("Server hops : %d\n", GETSHORT2USHORTdirect (&recvPacket->sap.entry[j].hops));
  380. }
  381. }
  382. }
  383. else
  384. printf ("**************INVALID BYTE COUNT********");
  385. printf ("\n\n");
  386. }
  387. else
  388. printf ("Adapter # : %d - ignored \n", CurAdapter);
  389. if ((NumActiveAdapters>=0) && !Stop) {
  390. status = IpxRecvPacket (sockPort,
  391. (PUCHAR)&recvPacket->hdr,
  392. sizeof(SAP_PACKET)
  393. -FIELD_OFFSET (SAP_PACKET, hdr),
  394. &recvPacket->rsvd,
  395. &recvPacket->ovrp,
  396. NULL);
  397. ASSERTMSG ("Failed to start receive. ", status==NO_ERROR);
  398. }
  399. #undef recvPacket
  400. }