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.

387 lines
12 KiB

  1. /*++
  2. Copyright (c) 1999, Microsoft Corporation
  3. Module Name:
  4. sample\samplemib.c
  5. Abstract:
  6. The file contains functions to display SAMPLE ip protocol's MIB.
  7. --*/
  8. #include "precomp.h"
  9. #pragma hdrstop
  10. VOID
  11. PrintGlobalStats(
  12. IN HANDLE hConsole,
  13. IN HANDLE hMibServer,
  14. IN PVOID pvOutput,
  15. IN FORMAT fFormat
  16. );
  17. VOID
  18. PrintIfStats(
  19. IN HANDLE hConsole,
  20. IN HANDLE hMibServer,
  21. IN PVOID pvOutput,
  22. IN FORMAT fFormat
  23. );
  24. VOID
  25. PrintIfBinding(
  26. IN HANDLE hConsole,
  27. IN HANDLE hMibServer,
  28. IN PVOID pvOutput,
  29. IN FORMAT fFormat
  30. );
  31. MIB_OBJECT_ENTRY rgMibObjectTable[] =
  32. {
  33. {TOKEN_GLOBALSTATS, IPSAMPLE_GLOBAL_STATS_ID, NULL,
  34. 0, PrintGlobalStats},
  35. {TOKEN_IFSTATS, IPSAMPLE_IF_STATS_ID, GetIfIndex,
  36. MSG_SAMPLE_MIB_IFSTATS_HEADER, PrintIfStats},
  37. {TOKEN_IFBINDING, IPSAMPLE_IF_BINDING_ID, GetIfIndex,
  38. MSG_SAMPLE_MIB_IFBINDING_HEADER, PrintIfBinding},
  39. };
  40. #define MAX_MIB_OBJECTS \
  41. (sizeof(rgMibObjectTable) / sizeof(MIB_OBJECT_ENTRY))
  42. #define MAX_GLOBAL_MIB_OBJECTS 1
  43. VOID
  44. PrintGlobalStats(
  45. IN HANDLE hConsole,
  46. IN HANDLE hMibServer,
  47. IN PVOID pvOutput,
  48. IN FORMAT fFormat
  49. )
  50. /*++
  51. Routine Description:
  52. Prints sample global statistics
  53. --*/
  54. {
  55. PIPSAMPLE_MIB_GET_OUTPUT_DATA pimgod =
  56. ((PIPSAMPLE_MIB_GET_OUTPUT_DATA) pvOutput);
  57. PIPSAMPLE_GLOBAL_STATS pGlobalStats =
  58. ((PIPSAMPLE_GLOBAL_STATS) pimgod->IMGOD_Buffer);
  59. DisplayMessageToConsole(g_hModule,
  60. hConsole,
  61. MSG_SAMPLE_MIB_GS,
  62. pGlobalStats->ulNumInterfaces);
  63. }
  64. VOID
  65. PrintIfStats(
  66. IN HANDLE hConsole,
  67. IN HANDLE hMibServer,
  68. IN PVOID pvOutput,
  69. IN FORMAT fFormat
  70. )
  71. /*++
  72. Routine Description:
  73. Prints SAMPLE interface statistics
  74. --*/
  75. {
  76. PIPSAMPLE_MIB_GET_OUTPUT_DATA pimgod =
  77. ((PIPSAMPLE_MIB_GET_OUTPUT_DATA) pvOutput);
  78. PIPSAMPLE_IF_STATS pIfStats =
  79. ((PIPSAMPLE_IF_STATS) pimgod->IMGOD_Buffer);
  80. WCHAR pwszIfName[MAX_INTERFACE_NAME_LEN + 1] = L"\0";
  81. InterfaceNameFromIndex(hMibServer,
  82. pimgod->IMGOD_IfIndex,
  83. pwszIfName,
  84. sizeof(pwszIfName));
  85. DisplayMessageToConsole(g_hModule,
  86. hConsole,
  87. (fFormat is FORMAT_VERBOSE)
  88. ? MSG_SAMPLE_MIB_IFSTATS
  89. : MSG_SAMPLE_MIB_IFSTATS_ENTRY,
  90. pwszIfName,
  91. pIfStats->ulNumPackets);
  92. }
  93. VOID
  94. PrintIfBinding(
  95. IN HANDLE hConsole,
  96. IN HANDLE hMibServer,
  97. IN PVOID pvOutput,
  98. IN FORMAT fFormat
  99. )
  100. /*++
  101. Routine Description:
  102. Prints SAMPLE interface binding
  103. --*/
  104. {
  105. PIPSAMPLE_MIB_GET_OUTPUT_DATA pimgod =
  106. ((PIPSAMPLE_MIB_GET_OUTPUT_DATA) pvOutput);
  107. PIPSAMPLE_IF_BINDING pIfBinding =
  108. ((PIPSAMPLE_IF_BINDING) pimgod->IMGOD_Buffer);
  109. WCHAR pwszIfName[MAX_INTERFACE_NAME_LEN + 1] = L"\0";
  110. PWCHAR pwszBindState = L"\0", pwszActiveState = L"\0";
  111. PIPSAMPLE_IP_ADDRESS pBinding = NULL;
  112. WCHAR pwszAddress[ADDR_LENGTH + 1] = L"\0",
  113. pwszMask[ADDR_LENGTH + 1] = L"\0";
  114. ULONG i;
  115. InterfaceNameFromIndex(hMibServer,
  116. pimgod->IMGOD_IfIndex,
  117. pwszIfName,
  118. sizeof(pwszIfName));
  119. pwszBindState = MakeString(g_hModule,
  120. ((pIfBinding->dwState&IPSAMPLE_STATE_BOUND)
  121. ? STRING_BOUND : STRING_UNBOUND));
  122. pwszActiveState = MakeString(g_hModule,
  123. ((pIfBinding->dwState&IPSAMPLE_STATE_ACTIVE)
  124. ? STRING_ACTIVE : STRING_INACTIVE));
  125. DisplayMessageToConsole(g_hModule,
  126. hConsole,
  127. (fFormat is FORMAT_VERBOSE)
  128. ? MSG_SAMPLE_MIB_IFBINDING
  129. : MSG_SAMPLE_MIB_IFBINDING_ENTRY,
  130. pwszIfName,
  131. pwszBindState,
  132. pwszActiveState);
  133. pBinding = IPSAMPLE_IF_ADDRESS_TABLE(pIfBinding);
  134. for(i = 0; i < pIfBinding->ulCount; i++)
  135. {
  136. UnicodeIpAddress(pwszAddress, INET_NTOA(pBinding[i].dwAddress));
  137. UnicodeIpAddress(pwszMask, INET_NTOA(pBinding[i].dwMask));
  138. DisplayMessageToConsole(g_hModule,
  139. hConsole,
  140. MSG_SAMPLE_MIB_IFBINDING_ADDR,
  141. pwszAddress,
  142. pwszMask);
  143. }
  144. if (pwszBindState) FreeString(pwszBindState);
  145. if (pwszActiveState) FreeString(pwszActiveState);
  146. }
  147. DWORD
  148. WINAPI
  149. HandleSampleMibShowObject(
  150. IN PWCHAR pwszMachineName,
  151. IN PWCHAR *ppwcArguments,
  152. IN DWORD dwCurrentIndex,
  153. IN DWORD dwArgCount,
  154. IN DWORD dwFlags,
  155. IN MIB_SERVER_HANDLE hMibServer,
  156. IN BOOL *pbDone
  157. )
  158. /*++
  159. Routine Description:
  160. Gets options for SHOW GLOBALSTATS/IFSTATS/IFBINDING
  161. A single command handler is used for all MIB objects since there is a
  162. lot common in the way processing takes place. However feel free to
  163. write a handler per object if u find the code a bit chaotic.
  164. --*/
  165. {
  166. DWORD dwErr = NO_ERROR;
  167. TAG_TYPE pttAllTags[] =
  168. {
  169. {TOKEN_INDEX, FALSE, FALSE}, // INDEX tag optional
  170. {TOKEN_RR, FALSE, FALSE}, // RR tag optional
  171. };
  172. DWORD pdwTagType[NUM_TAGS_IN_TABLE(pttAllTags)];
  173. DWORD dwNumArg;
  174. ULONG i;
  175. TAG_TYPE *pttTags;
  176. DWORD dwNumTags;
  177. BOOL bGlobalObject = FALSE;
  178. DWORD dwFirstGlobalArgument = 1;
  179. DWORD dwMibObject;
  180. BOOL bIndexSpecified = FALSE;
  181. DWORD dwIndex = 0;
  182. DWORD dwRR = 0;
  183. HANDLE hMib, hConsole;
  184. MODE mMode;
  185. IPSAMPLE_MIB_GET_INPUT_DATA imgid;
  186. PIPSAMPLE_MIB_GET_OUTPUT_DATA pimgod;
  187. BOOL bSomethingDisplayed = FALSE;
  188. // SAMPLE should be installed for this command to complete
  189. VerifyInstalled(MS_IP_SAMPLE, STRING_PROTO_SAMPLE);
  190. // figure mib object to display
  191. for (i = 0; i < MAX_MIB_OBJECTS; i++)
  192. if (MatchToken(ppwcArguments[dwCurrentIndex - 1],
  193. rgMibObjectTable[i].pwszObjectName))
  194. break;
  195. dwMibObject = i;
  196. if (dwMibObject is MAX_MIB_OBJECTS)
  197. return ERROR_CMD_NOT_FOUND;
  198. bGlobalObject = (dwMibObject < MAX_GLOBAL_MIB_OBJECTS);
  199. // for global objects, offset tags by index of the first global arguments
  200. pttTags = pttAllTags
  201. + bGlobalObject*dwFirstGlobalArgument;
  202. dwNumTags = NUM_TAGS_IN_TABLE(pttAllTags)
  203. - bGlobalObject*dwFirstGlobalArgument,
  204. dwErr = PreprocessCommand(g_hModule,
  205. ppwcArguments,
  206. dwCurrentIndex,
  207. dwArgCount,
  208. pttTags,
  209. dwNumTags,
  210. 0,
  211. dwNumTags,
  212. pdwTagType);
  213. if (dwErr isnot NO_ERROR)
  214. return dwErr;
  215. // process all arguments
  216. dwNumArg = dwArgCount - dwCurrentIndex;
  217. for (i = 0; i < dwNumArg; i++)
  218. {
  219. // for global objects, offset tag type by first global argument index
  220. pdwTagType[i] += bGlobalObject*dwFirstGlobalArgument;
  221. switch (pdwTagType[i])
  222. {
  223. case 0:
  224. // tag INDEX
  225. bIndexSpecified = TRUE;
  226. dwErr = (*rgMibObjectTable[dwMibObject].pfnGetIndex)(
  227. hMibServer,
  228. ppwcArguments[i+dwCurrentIndex],
  229. &dwIndex);
  230. break;
  231. case 1:
  232. // tag RR
  233. dwRR = wcstoul(ppwcArguments[i+dwCurrentIndex],
  234. NULL,
  235. 10);
  236. dwRR *= 1000; // convert refresh rate to milliseconds
  237. break;
  238. default:
  239. dwErr = ERROR_INVALID_SYNTAX;
  240. break;
  241. } // switch
  242. if (dwErr isnot NO_ERROR)
  243. break ;
  244. } // for
  245. // process errors
  246. if (dwErr isnot NO_ERROR)
  247. {
  248. ProcessError();
  249. return dwErr;
  250. }
  251. if (!InitializeConsole(&dwRR, &hMib, &hConsole))
  252. return ERROR_INIT_DISPLAY;
  253. // now display the specified mib object
  254. for(ever) // refresh loop
  255. {
  256. // initialize to default values
  257. bSomethingDisplayed = FALSE;
  258. imgid.IMGID_TypeID = rgMibObjectTable[dwMibObject].dwObjectId;
  259. imgid.IMGID_IfIndex = 0;
  260. mMode = GET_EXACT;
  261. // override defaults for interface objects
  262. if (!bGlobalObject)
  263. {
  264. if (bIndexSpecified)
  265. imgid.IMGID_IfIndex = dwIndex;
  266. else
  267. mMode = GET_FIRST;
  268. }
  269. for(ever) // display all interfaces loop
  270. {
  271. dwErr = MibGet(hMibServer,
  272. mMode,
  273. (PVOID) &imgid,
  274. sizeof(imgid),
  275. &pimgod);
  276. if (dwErr isnot NO_ERROR)
  277. {
  278. if ((mMode is GET_NEXT) and (dwErr is ERROR_NO_MORE_ITEMS))
  279. dwErr = NO_ERROR; // not really an error
  280. break;
  281. }
  282. // print table heading
  283. if (!bSomethingDisplayed and (mMode isnot GET_EXACT))
  284. {
  285. DisplayMessageToConsole(
  286. g_hModule,
  287. hConsole,
  288. rgMibObjectTable[dwMibObject].dwHeaderMessageId);
  289. bSomethingDisplayed = TRUE;
  290. }
  291. (*rgMibObjectTable[dwMibObject].pfnPrint)(hConsole,
  292. hMibServer,
  293. (PVOID) pimgod,
  294. (mMode is GET_EXACT)
  295. ? FORMAT_VERBOSE
  296. : FORMAT_TABLE);
  297. // prepare for next request
  298. imgid.IMGID_IfIndex = pimgod->IMGOD_IfIndex;
  299. MprAdminMIBBufferFree(pimgod);
  300. if (mMode is GET_EXACT)
  301. break;
  302. else // prepare for next request
  303. mMode = GET_NEXT;
  304. } // display all interfaces
  305. if (dwErr isnot NO_ERROR)
  306. {
  307. dwErr = bSomethingDisplayed ? NO_ERROR : ERROR_OKAY;
  308. break;
  309. }
  310. if (!RefreshConsole(hMib, hConsole, dwRR))
  311. break;
  312. } // refresh
  313. return dwErr;
  314. }