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.

473 lines
11 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. routing\monitor2\ip\utils.c
  5. Abstract:
  6. Utility functions
  7. Revision History:
  8. Anand Mahalingam 7/10/98 Created
  9. --*/
  10. #include "precomp.h"
  11. #pragma hdrstop
  12. #define MIB_REFRESH_EVENT L"MIBEvent"
  13. DWORD
  14. GetDisplayStringT (
  15. IN HANDLE hModule,
  16. IN DWORD dwValue,
  17. IN PVALUE_TOKEN ptvTable,
  18. IN DWORD dwNumArgs,
  19. OUT PWCHAR *ppwszString
  20. )
  21. {
  22. DWORD i, dwErr = NO_ERROR ;
  23. for (i=0; i<dwNumArgs; i++)
  24. {
  25. if ( dwValue == ptvTable[i].dwValue )
  26. {
  27. *ppwszString = HeapAlloc( GetProcessHeap(), 0,
  28. (wcslen(ptvTable[i].pwszToken)+1) * sizeof(WCHAR) );
  29. wcscpy(*ppwszString, ptvTable[i].pwszToken);
  30. break;
  31. }
  32. }
  33. if (i == dwNumArgs)
  34. *ppwszString = MakeString( hModule, STRING_UNKNOWN ) ;
  35. if (!ppwszString)
  36. {
  37. dwErr = ERROR_NOT_ENOUGH_MEMORY ;
  38. DisplayError( hModule, dwErr ) ;
  39. }
  40. return dwErr ;
  41. }
  42. DWORD
  43. GetDisplayString (
  44. IN HANDLE hModule,
  45. IN DWORD dwValue,
  46. IN PVALUE_STRING ptvTable,
  47. IN DWORD dwNumArgs,
  48. OUT PWCHAR *ppwszString
  49. )
  50. {
  51. DWORD i, dwErr = NO_ERROR ;
  52. for (i=0; i<dwNumArgs; i++)
  53. {
  54. if ( dwValue == ptvTable[i].dwValue )
  55. {
  56. *ppwszString = MakeString( hModule, ptvTable[i].dwStringId ) ;
  57. break;
  58. }
  59. }
  60. if (i == dwNumArgs)
  61. *ppwszString = MakeString( hModule, STRING_UNKNOWN ) ;
  62. if (!ppwszString)
  63. {
  64. dwErr = ERROR_NOT_ENOUGH_MEMORY ;
  65. DisplayError( hModule, dwErr ) ;
  66. }
  67. return dwErr ;
  68. }
  69. DWORD
  70. GetAltDisplayString(
  71. HANDLE hModule,
  72. HANDLE hFile,
  73. DWORD dwValue,
  74. PVALUE_TOKEN vtTable,
  75. PVALUE_STRING vsTable,
  76. DWORD dwNumArgs,
  77. PTCHAR *pptszString)
  78. {
  79. if (hFile)
  80. {
  81. return GetDisplayStringT(hModule,
  82. dwValue,
  83. vtTable,
  84. dwNumArgs,
  85. pptszString) ;
  86. }
  87. else
  88. {
  89. return GetDisplayString(hModule,
  90. dwValue,
  91. vsTable,
  92. dwNumArgs,
  93. pptszString) ;
  94. }
  95. }
  96. #if 0
  97. DWORD
  98. DispTokenErrMsg(
  99. IN HANDLE hModule,
  100. IN DWORD dwMsgId,
  101. IN DWORD dwTagId,
  102. IN LPCWSTR pwszValue
  103. )
  104. /*++
  105. Routine Description:
  106. Displays error message with token arguments.
  107. Arguments:
  108. dwMsgId - Message to be printed
  109. dwTagId - The tag string id
  110. pwszValue - the value specified for the tag in the command
  111. Return Value:
  112. NO_ERROR
  113. --*/
  114. {
  115. PWCHAR pwszTag;
  116. pwszTag = MakeString(hModule,
  117. dwTagId);
  118. DisplayMessage(hModule,
  119. dwMsgId,
  120. pwszValue,
  121. pwszTag);
  122. FreeString(pwszTag);
  123. return NO_ERROR;
  124. }
  125. #endif
  126. DWORD
  127. GetMibTagToken(
  128. IN LPCWSTR *ppwcArguments,
  129. IN DWORD dwArgCount,
  130. IN DWORD dwNumIndices,
  131. OUT PDWORD pdwRR,
  132. OUT PBOOL pbIndex,
  133. OUT PDWORD pdwIndex
  134. )
  135. /*++
  136. Routine Description:
  137. Looks for indices and refresh rate arguments in the command. If index
  138. tag is present, it would be of the form index= index1 index2 ....
  139. The index= is removed by this function. So is rr= if it is there in
  140. the command. If pdwRR is 0 then, no refresh sought.
  141. Arguments:
  142. pptcArguments - The argument array. Each argument has tag=value form
  143. dwCurrentIndex - pptcArguments[dwCurrentIndex] is first arg.
  144. dwArgCount - pptcArguments[dwArgCount - 1] is last arg.
  145. pttTagToken - Array of tag token ids that are allowed in the args
  146. dwNumTags - Size of pttTagToken
  147. pdwOut - Array identifying the type of each argument.
  148. Return Value:
  149. NO_ERROR, ERROR_INVALID_PARAMETER, ERROR_INVALID_OPTION_TAG
  150. --*/
  151. {
  152. DWORD i;
  153. BOOL bTag;
  154. if (dwArgCount is 0)
  155. {
  156. *pdwRR = 0;
  157. *pbIndex = FALSE;
  158. return NO_ERROR;
  159. }
  160. if (dwArgCount < dwNumIndices)
  161. {
  162. //
  163. // No index
  164. //
  165. *pbIndex = FALSE;
  166. if (dwArgCount > 1)
  167. {
  168. *pdwRR = 0;
  169. return ERROR_INVALID_PARAMETER;
  170. }
  171. //
  172. // No Index specified. Make sure refresh rate is specified
  173. // with tag.
  174. //
  175. if (_wcsnicmp(ppwcArguments[0],L"RR=",3) == 0)
  176. {
  177. //
  178. // remove tag and get the refresh rate
  179. //
  180. wcscpy(ppwcArguments[0], &ppwcArguments[0][3]);
  181. *pdwRR = wcstoul(ppwcArguments[0], NULL, 10);
  182. }
  183. else
  184. {
  185. return ERROR_INVALID_PARAMETER;
  186. }
  187. }
  188. else
  189. {
  190. //
  191. // Check for index tag
  192. //
  193. if (_wcsnicmp(ppwcArguments[0],L"INDEX=",6) == 0)
  194. {
  195. *pbIndex = TRUE;
  196. *pdwIndex = 0;
  197. //
  198. // remove tag and see if refresh rate is specified
  199. //
  200. wcscpy(ppwcArguments[0], &ppwcArguments[0][6]);
  201. if (dwArgCount > dwNumIndices)
  202. {
  203. //
  204. // Make sure that argument has RR tag
  205. //
  206. if (_wcsnicmp(ppwcArguments[dwNumIndices],L"RR=",3) == 0)
  207. {
  208. //
  209. // remove tag and get the refresh rate
  210. //
  211. wcscpy(ppwcArguments[dwNumIndices],
  212. &ppwcArguments[dwNumIndices][3]);
  213. *pdwRR = wcstoul(ppwcArguments[dwNumIndices], NULL , 10);
  214. }
  215. else
  216. {
  217. return ERROR_INVALID_PARAMETER;
  218. }
  219. }
  220. else
  221. {
  222. //
  223. // No refresh rate specified
  224. //
  225. *pdwRR = 0;
  226. return NO_ERROR;
  227. }
  228. }
  229. else
  230. {
  231. //
  232. // Not index tag, See if it has an RR tag
  233. //
  234. if (_wcsnicmp(ppwcArguments[0],L"RR=",3) == 0)
  235. {
  236. //
  237. // remove tag and get the refresh rate
  238. //
  239. wcscpy(ppwcArguments[0], &ppwcArguments[0][3]);
  240. *pdwRR = wcstoul(ppwcArguments[0], NULL , 10);
  241. //
  242. // See if the index follows
  243. //
  244. if (dwArgCount > dwNumIndices)
  245. {
  246. if (dwArgCount > 1)
  247. {
  248. if (_wcsnicmp(ppwcArguments[1],L"INDEX=",6) == 0)
  249. {
  250. wcscpy(ppwcArguments[1], &ppwcArguments[1][6]);
  251. *pbIndex = TRUE;
  252. *pdwIndex = 1;
  253. return NO_ERROR;
  254. }
  255. else
  256. {
  257. *pdwRR = 0;
  258. return ERROR_INVALID_PARAMETER;
  259. }
  260. }
  261. else
  262. {
  263. return NO_ERROR;
  264. }
  265. }
  266. }
  267. //
  268. // No RR Tag either
  269. //
  270. else if (dwArgCount > dwNumIndices)
  271. {
  272. //
  273. // Assume ppwcArguments[dwNumIndices] is the refresh rate
  274. //
  275. *pdwRR = wcstoul(ppwcArguments[dwNumIndices], NULL , 10);
  276. if (dwNumIndices != 0)
  277. {
  278. *pbIndex = TRUE;
  279. *pdwIndex = 0;
  280. }
  281. }
  282. else
  283. {
  284. //
  285. // only index present with no tag
  286. //
  287. *pbIndex = TRUE;
  288. *pdwIndex = 0;
  289. }
  290. }
  291. }
  292. return NO_ERROR;
  293. }
  294. DWORD
  295. GetIpAddress(
  296. PTCHAR pptcArg
  297. )
  298. /*++
  299. Routine Description:
  300. Gets the ip address from the string.
  301. Arguments:
  302. pwszIpAddr - Ip address string
  303. Return Value:
  304. ip address
  305. --*/
  306. {
  307. CHAR pszIpAddr[ADDR_LENGTH+1];
  308. WideCharToMultiByte(GetConsoleOutputCP(),
  309. 0,
  310. pptcArg,
  311. -1,
  312. pszIpAddr,
  313. ADDR_LENGTH,
  314. NULL,
  315. NULL);
  316. pszIpAddr[ADDR_LENGTH] = '\0';
  317. return (DWORD) inet_addr(pszIpAddr);
  318. }
  319. BOOL WINAPI HandlerRoutine(
  320. DWORD dwCtrlType // control signal type
  321. )
  322. {
  323. HANDLE hMib;
  324. if (dwCtrlType == CTRL_C_EVENT)
  325. {
  326. hMib = OpenEvent(EVENT_ALL_ACCESS,FALSE,MIB_REFRESH_EVENT);
  327. SetEvent(hMib);
  328. }
  329. return TRUE;
  330. }
  331. DWORD
  332. GetInfoBlockFromInterfaceInfoEx(
  333. IN LPCWSTR pwszIfName,
  334. IN DWORD dwType,
  335. OUT BYTE **ppbInfoBlk, OPTIONAL
  336. OUT PDWORD pdwSize, OPTIONAL
  337. OUT PDWORD pdwCount, OPTIONAL
  338. OUT PDWORD pdwIfType OPTIONAL
  339. )
  340. /*++
  341. Routine Description:
  342. calls GetInfoBlockFromInterfaceInfo and dumps error message if there is an
  343. error.
  344. --*/
  345. {
  346. DWORD dwErr;
  347. //
  348. // get current interface config
  349. //
  350. dwErr = IpmontrGetInfoBlockFromInterfaceInfo(pwszIfName,
  351. dwType,
  352. ppbInfoBlk,
  353. pdwSize,
  354. pdwCount,
  355. pdwIfType);
  356. switch(dwErr)
  357. {
  358. case NO_ERROR:
  359. break;
  360. case ERROR_NOT_FOUND:
  361. DisplayMessage(g_hModule,EMSG_PROTO_NO_IF_INFO);
  362. break;
  363. case ERROR_INVALID_PARAMETER:
  364. DisplayMessage(g_hModule,EMSG_CORRUPT_INFO);
  365. break;
  366. case ERROR_NOT_ENOUGH_MEMORY:
  367. DisplayMessage(g_hModule,EMSG_NOT_ENOUGH_MEMORY);
  368. break;
  369. default:
  370. DisplayError(g_hModule, dwErr);
  371. break;
  372. }
  373. return dwErr;
  374. }