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.

332 lines
8.1 KiB

  1. typedef unsigned int UINT;
  2. #include <ntosp.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <wchar.h>
  6. #include <ndispnp.h>
  7. WCHAR LCBuf[256];
  8. WCHAR UCBuf[256];
  9. WCHAR BLBuf[4096];
  10. CHAR ASBuf[128];
  11. NIC_STATISTICS Stats;
  12. UNICODE_STRING LC = {0, sizeof(LCBuf), LCBuf};
  13. UNICODE_STRING UC = {0, sizeof(UCBuf), UCBuf};
  14. UNICODE_STRING BL = {0, sizeof(BLBuf), BLBuf};
  15. ANSI_STRING AS = {0, sizeof(ASBuf), ASBuf};
  16. PNDIS_ENUM_INTF Interfaces = (PNDIS_ENUM_INTF)BLBuf;
  17. int Layer = -1, Operation = -1;
  18. extern LONG xxGetLastError();
  19. char * Ops[9] = { "", "BIND", "UNBIND", "RECONFIGURE", "", "", "", "STATS", "ENUM"};
  20. char * Intf[3] = { "", "NDIS", "TDI" };
  21. #define STATS 0x7
  22. #define ENUM 0x8
  23. #define MACADDR 0x9
  24. void
  25. help(
  26. void
  27. )
  28. {
  29. fprintf(stderr, "netpnp -u<UpperComponent> -l<LowerComponent> -i<Interface> -a<Action> [-b<BindList>]\n");
  30. fprintf(stderr, " where Interface is either NDIS or TDI\n");
  31. fprintf(stderr, " where Action is BIND, UNBIND, ENUM, STATS, MACADDR or RECONFIGURE\n");
  32. fprintf(stderr, " e.g. netpnp -uTCPIP -l\\Device\\{GUID} -iNDIS -aUNBIND\n");
  33. fprintf(stderr, " e.g. netpnp -uSRV -l\\Device\\NETBT_{GUID} -iTDI -aBIND -b\"\\Device\\NETBT_{GUID} \\Device\\NETBT_{GUID}\n");
  34. fprintf(stderr, " e.g. netpnp -l\\Device\\{GUID} -iNDIS -aSTATS\n");
  35. }
  36. BOOLEAN
  37. icmp(
  38. char *s1,
  39. char *s2
  40. )
  41. {
  42. char c1, c2;
  43. while (((c1 = *s1) != 0) && ((c2 = *s2) != 0))
  44. {
  45. c1 |= 0x20; c2 |= 0x20;
  46. if (c1 != c2)
  47. {
  48. break;
  49. }
  50. s1 ++; s2++;
  51. }
  52. return((*s1 == 0) && (*s2 == 0));
  53. }
  54. BOOLEAN
  55. GetOptions(
  56. int argc,
  57. char ** argv
  58. )
  59. {
  60. int i;
  61. char c;
  62. if (argc < 2)
  63. {
  64. help();
  65. return(FALSE);
  66. }
  67. //
  68. // Default upper and lower component to NONE
  69. //
  70. UC.Length = 0;
  71. LC.Length = 0;
  72. for (i = 1; i < argc; i++)
  73. {
  74. c = argv[i][0];
  75. if (c == '-' || c == '/')
  76. {
  77. c = argv[i][1];
  78. switch (c)
  79. {
  80. case 'u':
  81. case 'U':
  82. RtlInitAnsiString(&AS, &argv[i][2]);
  83. RtlAnsiStringToUnicodeString(&UC, &AS, FALSE);
  84. break;
  85. case 'l':
  86. case 'L':
  87. RtlInitAnsiString(&AS, &argv[i][2]);
  88. RtlAnsiStringToUnicodeString(&LC, &AS, FALSE);
  89. break;
  90. case 'i':
  91. case 'I':
  92. if (icmp(&argv[i][2], "ndis"))
  93. {
  94. Layer = NDIS;
  95. }
  96. else if (icmp(&argv[i][2], "tdi"))
  97. {
  98. Layer = TDI;
  99. }
  100. break;
  101. case 'a':
  102. case 'A':
  103. if (icmp(&argv[i][2], "bind"))
  104. {
  105. Operation = BIND;
  106. }
  107. else if (icmp(&argv[i][2], "unbind"))
  108. {
  109. Operation = UNBIND;
  110. }
  111. else if (icmp(&argv[i][2], "reconfigure"))
  112. {
  113. Operation = RECONFIGURE;
  114. }
  115. else if (icmp(&argv[i][2], "stats"))
  116. {
  117. Operation = STATS;
  118. }
  119. else if (icmp(&argv[i][2], "enum"))
  120. {
  121. Operation = ENUM;
  122. }
  123. else if (icmp(&argv[i][2], "macaddr"))
  124. {
  125. Operation = MACADDR;
  126. }
  127. break;
  128. case 'b':
  129. case 'B':
  130. RtlInitAnsiString(&AS, &argv[i][2]);
  131. RtlAnsiStringToUnicodeString(&BL, &AS, FALSE);
  132. //
  133. // Convert the space separated strings to MULTI_SZ format.
  134. //
  135. for (i = 0; i < BL.Length; i++)
  136. {
  137. if (BL.Buffer[i] == L' ')
  138. BL.Buffer[i] = 0;
  139. }
  140. break;
  141. case '?':
  142. default:
  143. help();
  144. break;
  145. }
  146. }
  147. }
  148. if ((Operation <= 0) ||
  149. (Operation < STATS) && ((Layer <= 0) || (LC.Length == 0)))
  150. {
  151. help();
  152. fprintf(stderr, "Current parameters:\n");
  153. fprintf(stderr, "UpperComp %Z\n", &UC);
  154. fprintf(stderr, "LowerComp %Z\n", &LC);
  155. fprintf(stderr, "BindList %Z\n", &BL);
  156. fprintf(stderr, "Action %s\n", (Operation <= 0) ? "INVALID" : Ops[Operation]);
  157. fprintf(stderr, "Interface %s\n", (Layer <= 0) ? "INVALID" : Intf[Layer]);
  158. return(FALSE);
  159. }
  160. return(TRUE);
  161. }
  162. VOID
  163. UTOA(
  164. IN PUNICODE_STRING U,
  165. OUT PANSI_STRING A
  166. )
  167. {
  168. A->Length = 0;
  169. RtlUnicodeStringToAnsiString(A, U, FALSE);
  170. }
  171. #define SECS_PER_DAY (24*60*60)
  172. #define SECS_PER_HOUR (60*60)
  173. #define SECS_PER_MIN 60
  174. VOID _cdecl
  175. main(
  176. int argc,
  177. char ** argv
  178. )
  179. {
  180. NTSTATUS Status;
  181. UINT i;
  182. if (!GetOptions(argc, argv))
  183. return;
  184. if (Operation == ENUM)
  185. {
  186. if (NdisEnumerateInterfaces(Interfaces, sizeof(BLBuf)))
  187. {
  188. ANSI_STRING A;
  189. UCHAR Buf[256];
  190. A.MaximumLength = sizeof(Buf);
  191. A.Buffer = Buf;
  192. for (i = 0; i < Interfaces->TotalInterfaces; i++)
  193. {
  194. UTOA(&Interfaces->Interface[i].DeviceName, &A),
  195. fprintf(stderr, "Device: %s\n\t",
  196. A.Buffer);
  197. UTOA(&Interfaces->Interface[i].DeviceDescription, &A);
  198. fprintf(stderr, "Description: %s\n",
  199. A.Buffer);
  200. }
  201. }
  202. else
  203. {
  204. Status = xxGetLastError();
  205. fprintf(stderr, "Enumerate failed %lx\n", Status);
  206. }
  207. }
  208. else if (Operation == MACADDR)
  209. {
  210. UCHAR MacAddr[6];
  211. UCHAR PMacAddr[6];
  212. UCHAR VendorId[3];
  213. UINT i;
  214. if (NdisQueryHwAddress(&LC, MacAddr, PMacAddr, VendorId))
  215. {
  216. fprintf(stderr, "HW Address:");
  217. for (i = 0; i < 5; i++)
  218. {
  219. fprintf(stderr, "%02x-", MacAddr[i]);
  220. }
  221. fprintf(stderr, "%02x\n", MacAddr[i]);
  222. fprintf(stderr, "Vendor Id:");
  223. for (i = 0; i < 2; i++)
  224. {
  225. fprintf(stderr, "%02x-", VendorId[i]);
  226. }
  227. fprintf(stderr, "%02x\n", VendorId[i]);
  228. }
  229. else
  230. {
  231. Status = xxGetLastError();
  232. fprintf(stderr, "Operation failed %lx\n", Status);
  233. }
  234. }
  235. else if (Operation == STATS)
  236. {
  237. Stats.Size = sizeof(NIC_STATISTICS);
  238. if (NdisQueryStatistics(&LC, &Stats))
  239. {
  240. fprintf(stderr, "Device Status: %s\n",
  241. (Stats.DeviceState == DEVICE_STATE_CONNECTED) ? "On" : "Off");
  242. fprintf(stderr, "Link Status: %s\n",
  243. (Stats.MediaState == MEDIA_STATE_CONNECTED) ? "On" : "Off");
  244. fprintf(stderr, "Init Time : %d ms\n", Stats.InitTime);
  245. fprintf(stderr, "Connect Time: ");
  246. if (Stats.ConnectTime > SECS_PER_DAY)
  247. {
  248. fprintf(stderr, "%d days, ", Stats.ConnectTime / SECS_PER_DAY);
  249. Stats.ConnectTime %= SECS_PER_DAY;
  250. }
  251. fprintf(stderr, "%02d:", Stats.ConnectTime / SECS_PER_HOUR);
  252. Stats.ConnectTime %= SECS_PER_HOUR;
  253. fprintf(stderr, "%02d:", Stats.ConnectTime / SECS_PER_MIN);
  254. Stats.ConnectTime %= SECS_PER_MIN;
  255. fprintf(stderr, "%02d\n", Stats.ConnectTime);
  256. Stats.LinkSpeed *= 100;
  257. if (Stats.LinkSpeed >= 1000000000)
  258. fprintf(stderr, "Media Speed: %d Gbps\n", Stats.LinkSpeed / 1000000);
  259. else if (Stats.LinkSpeed >= 1000000)
  260. fprintf(stderr, "Media Speed: %d Mbps\n", Stats.LinkSpeed / 1000000);
  261. else if (Stats.LinkSpeed >= 1000)
  262. fprintf(stderr, "Media Speed: %d Kbps\n", Stats.LinkSpeed / 1000);
  263. else
  264. fprintf(stderr, "Media Speed: %d bps\n", Stats.LinkSpeed);
  265. fprintf(stderr, "\n");
  266. fprintf(stderr, "Packets Sent: %I64d\n", Stats.PacketsSent);
  267. fprintf(stderr, "Bytes Sent: %I64d\n", Stats.BytesSent);
  268. fprintf(stderr, "\n");
  269. fprintf(stderr, "Packets Received: %I64d\n", Stats.PacketsReceived);
  270. fprintf(stderr, "Directed Pkts Recd: %I64d\n", Stats.DirectedPacketsReceived);
  271. fprintf(stderr, "Bytes Received: %I64d\n", Stats.BytesReceived);
  272. fprintf(stderr, "Directed Bytes Recd:%I64d\n", Stats.DirectedBytesReceived);
  273. fprintf(stderr, "\n");
  274. if (Stats.PacketsSendErrors != 0)
  275. fprintf(stderr, "Packets SendError: %d\n", Stats.PacketsSendErrors);
  276. if (Stats.PacketsReceiveErrors != 0)
  277. fprintf(stderr, "Packets RecvError: %d\n", Stats.PacketsReceiveErrors);
  278. if (Stats.ResetCount != 0)
  279. fprintf(stderr, "Reset Count : %d\n", Stats.ResetCount);
  280. if (Stats.MediaSenseConnectCount != 0)
  281. fprintf(stderr, "Media Connects : %d\n", Stats.MediaSenseConnectCount);
  282. if (Stats.MediaSenseDisconnectCount != 0)
  283. fprintf(stderr, "Media Disconnects: %d\n", Stats.MediaSenseDisconnectCount);
  284. }
  285. else
  286. {
  287. Status = xxGetLastError();
  288. fprintf(stderr, "GetStats failed %lx\n", Status);
  289. }
  290. }
  291. else if (!NdisHandlePnPEvent(Layer, Operation, &LC, &UC, &BL, NULL, 0))
  292. {
  293. Status = xxGetLastError();
  294. fprintf(stderr, "NdisHandlePnPEvent failed %lx\n", Status);
  295. }
  296. }