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.

580 lines
11 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Contains utilities for kd ext and parsing.
  7. Author:
  8. Scott Holden (sholden) 24-Apr-1999
  9. Revision History:
  10. --*/
  11. #include "tcpipxp.h"
  12. #include "tcpipkd.h"
  13. PTCPIP_SRCH
  14. ParseSrch(
  15. PCSTR args,
  16. ULONG ulDefaultOp,
  17. ULONG ulAllowedOps
  18. )
  19. {
  20. PTCPIP_SRCH pSrch = NULL;
  21. char *pszArgs = NULL;
  22. char *pszSrch;
  23. ULONG cbArgs;
  24. ULONG Status = STATUS_SUCCESS;
  25. //
  26. // Make a copy of the argument string to parse with.
  27. //
  28. cbArgs = strlen(args);
  29. pszArgs = LocalAlloc(LPTR, cbArgs + 1);
  30. if (pszArgs == NULL)
  31. {
  32. Status = STATUS_NO_MEMORY;
  33. goto done;
  34. }
  35. strcpy(pszArgs, args);
  36. //
  37. // Allocate the return srch. Set default::assume default has no params.
  38. //
  39. pSrch = LocalAlloc(LPTR, sizeof(TCPIP_SRCH));
  40. if (pSrch == NULL)
  41. {
  42. Status = STATUS_NO_MEMORY;
  43. goto done;
  44. }
  45. pSrch->ulOp = ulDefaultOp;
  46. // If we need to parse out an addr, do it first...
  47. // Get first token in arg list.
  48. pszSrch = mystrtok(pszArgs, " \t\n");
  49. if (pszSrch == NULL)
  50. {
  51. // If default is invalid (i.e. NO default), return error.
  52. if (pSrch->ulOp == 0 ||
  53. (ulAllowedOps & TCPIP_SRCH_PTR_LIST))
  54. {
  55. Status = STATUS_INVALID_PARAMETER;
  56. dprintf("xxx1\n");
  57. }
  58. goto done;
  59. }
  60. if (ulAllowedOps & TCPIP_SRCH_PTR_LIST)
  61. {
  62. pSrch->ListAddr = GetExpression(pszSrch);
  63. pszSrch = mystrtok(NULL, " \t\n");
  64. if (pszSrch == NULL)
  65. {
  66. if (pSrch->ulOp == 0)
  67. {
  68. Status = STATUS_INVALID_PARAMETER;
  69. }
  70. goto done;
  71. }
  72. }
  73. if (strcmp(pszSrch, "all") == 0)
  74. {
  75. pSrch->ulOp = TCPIP_SRCH_ALL;
  76. }
  77. else if (strcmp(pszSrch, "ipaddr") == 0)
  78. {
  79. CHAR szIP[20];
  80. CHAR *p;
  81. ULONG i;
  82. pSrch->ulOp = TCPIP_SRCH_IPADDR;
  83. pSrch->ipaddr = 0;
  84. pszSrch = mystrtok(NULL, " \t\n");
  85. if (pszSrch == NULL || strlen(pszSrch) >= 15)
  86. {
  87. Status = STATUS_INVALID_PARAMETER;
  88. goto done;
  89. }
  90. strcpy(szIP, pszSrch);
  91. p = mystrtok(szIP, ".");
  92. for (i = 0; i < 4; i++)
  93. {
  94. pSrch->ipaddr |= (atoi(p) << (i*8));
  95. p = mystrtok(NULL, ".");
  96. if (p == NULL)
  97. {
  98. break;
  99. }
  100. }
  101. if (i != 3)
  102. {
  103. Status = STATUS_INVALID_PARAMETER;
  104. goto done;
  105. }
  106. }
  107. else if (strcmp(pszSrch, "port") == 0)
  108. {
  109. pSrch->ulOp = TCPIP_SRCH_PORT;
  110. pszSrch = mystrtok(NULL, " \t\n");
  111. if (pszSrch == NULL)
  112. {
  113. Status = STATUS_INVALID_PARAMETER;
  114. goto done;
  115. }
  116. pSrch->port = (USHORT)atoi(pszSrch);
  117. }
  118. else if (strcmp(pszSrch, "prot") == 0)
  119. {
  120. pSrch->ulOp = TCPIP_SRCH_PROT;
  121. pszSrch = mystrtok(NULL, " \t\n");
  122. if (pszSrch == NULL)
  123. {
  124. Status = STATUS_INVALID_PARAMETER;
  125. goto done;
  126. }
  127. if (strcmp(pszSrch, "raw") == 0)
  128. {
  129. pSrch->prot = PROTOCOL_RAW;
  130. }
  131. else if (strcmp(pszSrch, "udp") == 0)
  132. {
  133. pSrch->prot = PROTOCOL_UDP;
  134. }
  135. else if (strcmp(pszSrch, "tcp") == 0)
  136. {
  137. pSrch->prot = PROTOCOL_TCP;
  138. }
  139. else
  140. {
  141. Status = STATUS_INVALID_PARAMETER;
  142. goto done;
  143. }
  144. }
  145. else if (strcmp(pszSrch, "context") == 0)
  146. {
  147. pSrch->ulOp = TCPIP_SRCH_CONTEXT;
  148. pszSrch = mystrtok(NULL, " \t\n");
  149. if (pszSrch == NULL)
  150. {
  151. Status = STATUS_INVALID_PARAMETER;
  152. goto done;
  153. }
  154. pSrch->context = atoi(pszSrch);
  155. }
  156. else if (strcmp(pszSrch, "stats") == 0)
  157. {
  158. pSrch->ulOp = TCPIP_SRCH_STATS;
  159. }
  160. else
  161. {
  162. // Invalid srch request. Fail.
  163. Status = STATUS_INVALID_PARAMETER;
  164. goto done;
  165. }
  166. // Now see if this is an expected type!!!
  167. if ((pSrch->ulOp & ulAllowedOps) == 0)
  168. {
  169. dprintf("invalid operation for current srch" ENDL);
  170. Status = STATUS_INVALID_PARAMETER;
  171. goto done;
  172. }
  173. done:
  174. if (pszArgs)
  175. {
  176. LocalFree(pszArgs);
  177. }
  178. if (Status != STATUS_SUCCESS)
  179. {
  180. if (pSrch)
  181. {
  182. LocalFree(pSrch);
  183. pSrch = NULL;
  184. }
  185. }
  186. return (pSrch);
  187. }
  188. char *mystrtok ( char *string, char * control )
  189. {
  190. static unsigned char *str;
  191. char *p, *s;
  192. if( string )
  193. str = string;
  194. if( str == NULL || *str == '\0' )
  195. return NULL;
  196. //
  197. // Skip leading delimiters...
  198. //
  199. for( ; *str; str++ ) {
  200. for( s=control; *s; s++ ) {
  201. if( *str == *s )
  202. break;
  203. }
  204. if( *s == '\0' )
  205. break;
  206. }
  207. //
  208. // Was it was all delimiters?
  209. //
  210. if( *str == '\0' ) {
  211. str = NULL;
  212. return NULL;
  213. }
  214. //
  215. // We've got a string, terminate it at first delimeter
  216. //
  217. for( p = str+1; *p; p++ ) {
  218. for( s = control; *s; s++ ) {
  219. if( *p == *s ) {
  220. s = str;
  221. *p = '\0';
  222. str = p+1;
  223. return s;
  224. }
  225. }
  226. }
  227. //
  228. // We've got a string that ends with the NULL
  229. //
  230. s = str;
  231. str = NULL;
  232. return s;
  233. }
  234. ULONG
  235. GetUlongValue (
  236. PCHAR String
  237. )
  238. {
  239. ULONG Location;
  240. ULONG Value;
  241. ULONG result;
  242. Location = GetExpression( String );
  243. if (!Location) {
  244. dprintf("unable to get %s\n",String);
  245. return 0;
  246. }
  247. if ((!ReadMemory((DWORD)Location,&Value,sizeof(ULONG),&result)) ||
  248. (result < sizeof(ULONG))) {
  249. dprintf("unable to get %s\n",String);
  250. return 0;
  251. }
  252. return Value;
  253. }
  254. BOOL
  255. GetData(
  256. PVOID pvData,
  257. ULONG cbData,
  258. ULONG_PTR Address,
  259. PCSTR pszDataType
  260. )
  261. {
  262. BOOL b;
  263. ULONG cbRead;
  264. ULONG count = cbData;
  265. while(cbData > 0)
  266. {
  267. if (count >= 3000)
  268. count = 3000;
  269. b = ReadMemory(Address, pvData, count, &cbRead );
  270. if (!b || cbRead != count )
  271. {
  272. dprintf( "Unable to read %u bytes at %X, for %s\n", cbData, Address, pszDataType );
  273. return (FALSE);
  274. }
  275. Address += count;
  276. cbData -= count;
  277. pvData = (PVOID)((ULONG_PTR)pvData + count);
  278. // if (CheckControlC())
  279. // {
  280. // dprintf("ctrl-c\n");
  281. // return (FALSE);
  282. // }
  283. }
  284. return (TRUE);
  285. }
  286. BOOL
  287. KDDump_ULONG(
  288. PCHAR pVar,
  289. PCHAR pName
  290. )
  291. {
  292. ULONG_PTR Addr;
  293. ULONG Value;
  294. BOOL fStatus;
  295. Addr = GetExpression(pVar);
  296. if (Addr == 0)
  297. {
  298. dprintf("Failed to get %s" ENDL, pVar);
  299. return (FALSE);
  300. }
  301. fStatus = GetData(&Value, sizeof(Value), Addr, "ULONG");
  302. if (fStatus == FALSE)
  303. {
  304. dprintf("Failed to read %s @ %x" ENDL, pVar, Addr);
  305. return (FALSE);
  306. }
  307. PrintFieldName(pName);
  308. dprintf("%-10u", Value);
  309. return (TRUE);
  310. }
  311. BOOL
  312. KDDump_LONG(
  313. PCHAR pVar,
  314. PCHAR pName
  315. )
  316. {
  317. ULONG_PTR Addr;
  318. LONG Value;
  319. BOOL fStatus;
  320. Addr = GetExpression(pVar);
  321. if (Addr == 0)
  322. {
  323. dprintf("Failed to get %s" ENDL, pVar);
  324. return (FALSE);
  325. }
  326. fStatus = GetData(&Value, sizeof(Value), Addr, "LONG");
  327. if (fStatus == FALSE)
  328. {
  329. dprintf("Failed to read %s @ %x" ENDL, pVar, Addr);
  330. return (FALSE);
  331. }
  332. PrintFieldName(pName);
  333. dprintf("%-10d", Value);
  334. return (TRUE);
  335. }
  336. BOOL
  337. KDDump_BOOLEAN(
  338. PCHAR pVar,
  339. PCHAR pName
  340. )
  341. {
  342. ULONG_PTR Addr;
  343. BOOLEAN Value;
  344. BOOL fStatus;
  345. Addr = GetExpression(pVar);
  346. if (Addr == 0)
  347. {
  348. dprintf("Failed to get %s" ENDL, pVar);
  349. return (FALSE);
  350. }
  351. fStatus = GetData(&Value, sizeof(Value), Addr, "BOOLEAN");
  352. if (fStatus == FALSE)
  353. {
  354. dprintf("Failed to read %s @ %x" ENDL, pVar, Addr);
  355. return (FALSE);
  356. }
  357. PrintFieldName(pName);
  358. dprintf("%-10s", Value == TRUE ? "TRUE" : "FALSE");
  359. return (TRUE);
  360. }
  361. BOOL
  362. KDDump_uchar(
  363. PCHAR pVar,
  364. PCHAR pName
  365. )
  366. {
  367. ULONG_PTR Addr;
  368. uchar Value;
  369. BOOL fStatus;
  370. Addr = GetExpression(pVar);
  371. if (Addr == 0)
  372. {
  373. dprintf("Failed to get %s" ENDL, pVar);
  374. return (FALSE);
  375. }
  376. fStatus = GetData(&Value, sizeof(Value), Addr, "uchar");
  377. if (fStatus == FALSE)
  378. {
  379. dprintf("Failed to read %s @ %x" ENDL, pVar, Addr);
  380. return (FALSE);
  381. }
  382. PrintFieldName(pName);
  383. dprintf("%-10u", Value);
  384. return (TRUE);
  385. }
  386. BOOL
  387. KDDump_ushort(
  388. PCHAR pVar,
  389. PCHAR pName
  390. )
  391. {
  392. ULONG_PTR Addr;
  393. ushort Value;
  394. BOOL fStatus;
  395. Addr = GetExpression(pVar);
  396. if (Addr == 0)
  397. {
  398. dprintf("Failed to get %s" ENDL, pVar);
  399. return (FALSE);
  400. }
  401. fStatus = GetData(&Value, sizeof(Value), Addr, "ushort");
  402. if (fStatus == FALSE)
  403. {
  404. dprintf("Failed to read %s @ %x" ENDL, pVar, Addr);
  405. return (FALSE);
  406. }
  407. PrintFieldName(pName);
  408. dprintf("%-10hu", Value);
  409. return (TRUE);
  410. }
  411. BOOL
  412. KDDump_PtrSymbol(
  413. PCHAR pVar,
  414. PCHAR pName
  415. )
  416. {
  417. ULONG_PTR Addr;
  418. ULONG_PTR Value;
  419. BOOL fStatus;
  420. Addr = GetExpression(pVar);
  421. if (Addr == 0)
  422. {
  423. dprintf("Failed to get %s" ENDL, pVar);
  424. return (FALSE);
  425. }
  426. fStatus = GetData(&Value, sizeof(Value), Addr, "ULONG_PTR");
  427. if (fStatus == FALSE)
  428. {
  429. dprintf("Failed to read %s @ %x" ENDL, pVar, Addr);
  430. return (FALSE);
  431. }
  432. PrintFieldName(pName);
  433. DumpPtrSymbol((PVOID) Value);
  434. return (TRUE);
  435. }
  436. BOOL
  437. KDDump_Queue(
  438. PCHAR pVar,
  439. PCHAR pName
  440. )
  441. {
  442. ULONG_PTR Addr;
  443. Queue Value;
  444. BOOL fStatus;
  445. Addr = GetExpression(pVar);
  446. if (Addr == 0)
  447. {
  448. dprintf("Failed to get %s" ENDL, pVar);
  449. return (FALSE);
  450. }
  451. fStatus = GetData(&Value, sizeof(Value), Addr, "Queue");
  452. if (fStatus == FALSE)
  453. {
  454. dprintf("Failed to read %s @ %x" ENDL, pVar, Addr);
  455. return (FALSE);
  456. }
  457. PrintFieldName(pName);
  458. dprintf("q_next = %-10lx", Value.q_next);
  459. dprintf("q_prev = %-10lx", Value.q_prev);
  460. dprintf("%s", (Value.q_next == (Queue *)Addr) ? "[Empty]" : "");
  461. return (TRUE);
  462. }