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.

284 lines
8.1 KiB

  1. //==========================================================================
  2. // Copyright (c) 1995, Microsoft Corporation
  3. //
  4. // File: rtquery.c
  5. //
  6. // test program for routing table api
  7. //==========================================================================
  8. #include <windows.h>
  9. #include <winsock.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "routetab.h"
  13. #define MAX_COMMAND 32
  14. #define STR_QUIT "quit"
  15. #define STR_ADD "add"
  16. #define STR_DELETE "delete"
  17. #define STR_ROUTES "routelist"
  18. #define STR_ADDRESSES "addrlist"
  19. #define STR_IFSTATS "ifstats"
  20. #define POS_ADDR_EVENT 0
  21. #define POS_CONS_EVENT 1
  22. #define POS_LAST_EVENT 2
  23. void DoAddRoute();
  24. void DoDeleteRoute();
  25. void DoListRoutes();
  26. void DoListAddresses();
  27. void DoIfStats();
  28. void PrintInstructions();
  29. DWORD ProcessCommand();
  30. void _cdecl main() {
  31. DWORD dwErr;
  32. HANDLE hStdout;
  33. HANDLE hEvents[POS_LAST_EVENT];
  34. hEvents[POS_CONS_EVENT] = GetStdHandle(STD_INPUT_HANDLE);
  35. hEvents[POS_ADDR_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
  36. SetAddrChangeNotifyEvent(hEvents[POS_ADDR_EVENT]);
  37. PrintInstructions();
  38. while(TRUE) {
  39. printf("\nrtquery> ");
  40. // dwErr = WaitForMultipleObjects(POS_LAST_EVENT, hEvents,
  41. // TRUE, INFINITE);
  42. // if (dwErr == (WAIT_OBJECT_0 + POS_CONS_EVENT)) {
  43. dwErr = ProcessCommand();
  44. if (dwErr != 0) { break; }
  45. // }
  46. // else
  47. // if (dwErr == (WAIT_OBJECT_0 + POS_ADDR_EVENT)) {
  48. // printf("\n****an IP address has changed****");
  49. // DoListInterfaces();
  50. // }
  51. }
  52. CloseHandle(hEvents[POS_ADDR_EVENT]);
  53. return;
  54. }
  55. DWORD ProcessCommand() {
  56. DWORD dwErr;
  57. CHAR szCommand[MAX_COMMAND];
  58. scanf("%s", szCommand);
  59. dwErr = 0;
  60. if (_stricmp(szCommand, STR_ADD) == 0) {
  61. DoAddRoute();
  62. }
  63. else
  64. if (_stricmp(szCommand, STR_DELETE) == 0) {
  65. DoDeleteRoute();
  66. }
  67. else
  68. if (_stricmp(szCommand, STR_ROUTES) == 0) {
  69. DoListRoutes();
  70. }
  71. else
  72. if (_stricmp(szCommand, STR_ADDRESSES) == 0) {
  73. DoListAddresses();
  74. }
  75. else
  76. if (_stricmp(szCommand, STR_IFSTATS) == 0) {
  77. DoIfStats();
  78. }
  79. else
  80. if (_stricmp(szCommand, STR_QUIT) == 0) {
  81. dwErr = 1;
  82. }
  83. else {
  84. CHAR szUnusedLine[256];
  85. fgets(szUnusedLine, 256, stdin);
  86. PrintInstructions();
  87. }
  88. return dwErr;
  89. }
  90. void DoAddRoute() {
  91. DWORD dwErr, dwIndex, dwType, dwDest, dwMask, dwGate, dwMetric;
  92. CHAR szDest[MAX_COMMAND], szMask[MAX_COMMAND], szGate[MAX_COMMAND];
  93. scanf("%d %d %s %s %s %d", &dwIndex, &dwType,
  94. szDest, szMask, szGate, &dwMetric);
  95. dwDest = inet_addr(szDest);
  96. dwMask = inet_addr(szMask);
  97. dwGate = inet_addr(szGate);
  98. dwErr = AddRoute(IRE_PROTO_RIP, dwType, dwIndex, dwDest,
  99. dwMask, dwGate, dwMetric);
  100. printf("\nreturn value: %x", dwErr);
  101. }
  102. void DoDeleteRoute() {
  103. DWORD dwErr, dwIndex, dwDest, dwMask, dwGate;
  104. CHAR szDest[MAX_COMMAND], szMask[MAX_COMMAND], szGate[MAX_COMMAND];
  105. scanf("%d %s %s %s", &dwIndex, szDest, szMask, szGate);
  106. dwDest = inet_addr(szDest);
  107. dwMask = inet_addr(szMask);
  108. dwGate = inet_addr(szGate);
  109. dwErr = DeleteRoute(dwIndex, dwDest, dwMask, dwGate);
  110. printf("\nreturn value: %x", dwErr);
  111. }
  112. void DoListRoutes() {
  113. struct in_addr addr;
  114. DWORD dwErr, dwCount;
  115. LPIPROUTE_ENTRY lpTable, lpentry, lpend;
  116. dwErr = GetRouteTable(&lpTable, &dwCount);
  117. if (dwErr == 0 && lpTable != NULL) {
  118. printf("ROUTE TABLE CONTENTS:");
  119. printf("\n%-10s%-18s%-18s%-18s%-10s", "index", "address",
  120. "subnet mask",
  121. "next hop",
  122. "metric");
  123. lpend = lpTable + dwCount;
  124. for (lpentry = lpTable; lpentry < lpend; lpentry++) {
  125. printf("\n%-10d", lpentry->ire_index);
  126. addr.s_addr = lpentry->ire_dest;
  127. printf("%-18s", inet_ntoa(addr));
  128. addr.s_addr = lpentry->ire_mask;
  129. printf("%-18s", inet_ntoa(addr));
  130. addr.s_addr = lpentry->ire_nexthop;
  131. printf("%-18s", inet_ntoa(addr));
  132. printf("%-10d", lpentry->ire_metric1);
  133. }
  134. FreeRouteTable(lpTable);
  135. }
  136. printf("\nreturn value: %x", dwErr);
  137. }
  138. void DoListAddresses() {
  139. struct in_addr addr;
  140. DWORD dwErr, dwCount;
  141. LPIPADDRESS_ENTRY lpTable, lpentry, lpend;
  142. dwErr = GetIPAddressTable(&lpTable, &dwCount);
  143. if (dwErr == 0 && lpTable != NULL) {
  144. printf("ADDRESS TABLE CONTENTS:");
  145. printf("\n%-10s%-18s%-18s", "index", "address", "subnet mask");
  146. lpend = lpTable + dwCount;
  147. for (lpentry = lpTable; lpentry < lpend; lpentry++) {
  148. printf("\n%-10d", lpentry->iae_index);
  149. addr.s_addr = lpentry->iae_address;
  150. printf("%-18s", inet_ntoa(addr));
  151. addr.s_addr = lpentry->iae_netmask;
  152. printf("%-18s", inet_ntoa(addr));
  153. }
  154. FreeIPAddressTable(lpTable);
  155. }
  156. printf("\nreturn value: %x", dwErr);
  157. }
  158. void DoIfStats() {
  159. INT i;
  160. IF_ENTRY ife;
  161. LPSTR lpszAddr;
  162. DWORD dwErr, dwIndex;
  163. LPBYTE lpbAddr, lpbAddrEnd;
  164. CHAR szHexDigits[] = "0123456789ABCDEF";
  165. CHAR szType[32], szAddr[64], szAstatus[16], szOstatus[16];
  166. scanf("%d", &dwIndex);
  167. dwErr = GetIfEntry(dwIndex, &ife);
  168. if (dwErr == 0) {
  169. switch(ife.ife_type) {
  170. case IF_TYPE_OTHER:
  171. strcpy(szType, "OTHER"); break;
  172. case IF_TYPE_ETHERNET:
  173. strcpy(szType, "ETHERNET"); break;
  174. case IF_TYPE_TOKENRING:
  175. strcpy(szType, "TOKENRING"); break;
  176. case IF_TYPE_FDDI:
  177. strcpy(szType, "FDDI"); break;
  178. case IF_TYPE_PPP:
  179. strcpy(szType, "PPP"); break;
  180. case IF_TYPE_LOOPBACK:
  181. strcpy(szType, "LOOPBACK"); break;
  182. case IF_TYPE_SLIP:
  183. strcpy(szType, "SLIP"); break;
  184. }
  185. switch(ife.ife_adminstatus) {
  186. case IF_STATUS_UP:
  187. strcpy(szAstatus, "up"); break;
  188. case IF_STATUS_DOWN:
  189. strcpy(szAstatus, "down"); break;
  190. case IF_STATUS_TESTING:
  191. strcpy(szAstatus, "testing"); break;
  192. }
  193. switch(ife.ife_operstatus) {
  194. case IF_STATUS_UP:
  195. strcpy(szOstatus, "up"); break;
  196. case IF_STATUS_DOWN:
  197. strcpy(szOstatus, "down"); break;
  198. case IF_STATUS_TESTING:
  199. strcpy(szOstatus, "testing"); break;
  200. }
  201. lpszAddr = szAddr;
  202. lpbAddrEnd = ife.ife_physaddr + ife.ife_physaddrlen;
  203. for (lpbAddr = ife.ife_physaddr; lpbAddr < lpbAddrEnd; lpbAddr++) {
  204. *lpszAddr++ = szHexDigits[*lpbAddr / 16];
  205. *lpszAddr++ = szHexDigits[*lpbAddr % 16];
  206. *lpszAddr++ = '-';
  207. }
  208. if (lpszAddr > szAddr) { *(--lpszAddr) = '\0'; }
  209. printf("\nstatistics for interface %d: ", dwIndex);
  210. printf("\n\tdescription: %s", ife.ife_descr);
  211. printf("\n\ttype: %s", szType);
  212. printf("\n\tphysical address: %s", szAddr);
  213. printf("\n\toperational status: %s", szOstatus);
  214. printf("\n\tadministrative status: %s", szAstatus);
  215. }
  216. printf("\nreturn value: %x", dwErr);
  217. }
  218. void PrintInstructions() {
  219. printf("\nRouting Table Query Test Program");
  220. printf("\n\tCommands:");
  221. printf("\n\t\tadd <index> <type> <address> <netmask> <gateway> <metric>");
  222. printf("\n\t\tdelete <index> <address> <netmask> <gateway>");
  223. printf("\n\t\troutelist");
  224. printf("\n\t\taddrlist");
  225. printf("\n\t\tifstats <index>");
  226. printf("\n\t\tquit");
  227. printf("\n\n\tRoute types for 'add' command:");
  228. printf("\n\t\tDIRECT==3, INDIRECT==4");
  229. }