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.

743 lines
21 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. conn.c
  5. Abstract:
  6. Implements the conn command.
  7. Author:
  8. Keith Moore (keithmo) 19-Apr-1995
  9. Environment:
  10. User Mode.
  11. Revision History:
  12. --*/
  13. #include "afdkdp.h"
  14. #pragma hdrstop
  15. BOOL
  16. DumpConnectionCallback(
  17. ULONG64 ActualAddress,
  18. ULONG64 Context
  19. );
  20. BOOL
  21. FindRemotePortCallback(
  22. ULONG64 ActualAddress,
  23. ULONG64 Context
  24. );
  25. //
  26. // Public functions.
  27. //
  28. DECLARE_API( conn )
  29. /*++
  30. Routine Description:
  31. Dumps the AFD_CONNECTION structure at the specified address.
  32. Arguments:
  33. None.
  34. Return Value:
  35. None.
  36. --*/
  37. {
  38. ULONG result;
  39. INT i;
  40. CHAR expr[MAX_ADDRESS_EXPRESSION];
  41. PCHAR argp;
  42. ULONG64 address;
  43. gClient = pClient;
  44. if (!CheckKmGlobals ()) {
  45. return E_INVALIDARG;
  46. }
  47. argp = ProcessOptions ((PCHAR)args);
  48. if (argp==NULL)
  49. return E_INVALIDARG;
  50. if (Options&AFDKD_BRIEF_DISPLAY) {
  51. dprintf (AFDKD_BRIEF_CONNECTION_DISPLAY_HEADER);
  52. }
  53. //
  54. // Snag the address from the command line.
  55. //
  56. if ((argp[0]==0) || (Options & AFDKD_ENDPOINT_SCAN)) {
  57. EnumEndpoints(
  58. DumpConnectionCallback,
  59. 0
  60. );
  61. dprintf ("\nTotal connections: %ld", EntityCount);
  62. }
  63. else {
  64. while (sscanf( argp, "%s%n", expr, &i )==1) {
  65. if( CheckControlC() ) {
  66. break;
  67. }
  68. argp += i;
  69. address = GetExpression (expr);
  70. result = (ULONG)InitTypeRead (address, AFD!AFD_CONNECTION);
  71. if (result!=0) {
  72. dprintf ("\nconn: Could not read AFD_CONNECTION @ %p, err: %d",
  73. address, result);
  74. break;
  75. }
  76. if (Options & AFDKD_BRIEF_DISPLAY) {
  77. DumpAfdConnectionBrief(
  78. address
  79. );
  80. }
  81. else {
  82. DumpAfdConnection(
  83. address
  84. );
  85. }
  86. if (Options & AFDKD_FIELD_DISPLAY) {
  87. ProcessFieldOutput (address, "AFD!AFD_CONNECTION");
  88. }
  89. }
  90. }
  91. if (Options&AFDKD_BRIEF_DISPLAY) {
  92. dprintf (AFDKD_BRIEF_CONNECTION_DISPLAY_TRAILER);
  93. }
  94. else {
  95. dprintf ("\n");
  96. }
  97. return S_OK;
  98. } // conn
  99. DECLARE_API( rport )
  100. /*++
  101. Routine Description:
  102. Dumps all AFD_ENDPOINT structures connected to the given port.
  103. Arguments:
  104. None.
  105. Return Value:
  106. None.
  107. --*/
  108. {
  109. INT i;
  110. CHAR expr[MAX_ADDRESS_EXPRESSION];
  111. PCHAR argp;
  112. ULONG64 val;
  113. gClient = pClient;
  114. if (!CheckKmGlobals ()) {
  115. return E_INVALIDARG;
  116. }
  117. argp = ProcessOptions ((PCHAR)args);
  118. if (argp==NULL)
  119. return E_INVALIDARG;
  120. if (Options&AFDKD_BRIEF_DISPLAY) {
  121. dprintf (AFDKD_BRIEF_CONNECTION_DISPLAY_HEADER);
  122. }
  123. //
  124. // Snag the port from the command line.
  125. //
  126. while (sscanf( argp, "%s%n", expr, &i)==1) {
  127. if( CheckControlC() ) {
  128. break;
  129. }
  130. argp+=i;
  131. val = GetExpression (expr);
  132. dprintf ("\nLooking for connections connected to port 0x%I64X(0d%I64d) ", val, val);
  133. EnumEndpoints(
  134. FindRemotePortCallback,
  135. val
  136. );
  137. dprintf ("\nTotal connections: %ld", EntityCount);
  138. }
  139. if (Options&AFDKD_BRIEF_DISPLAY) {
  140. dprintf (AFDKD_BRIEF_CONNECTION_DISPLAY_HEADER);
  141. }
  142. else {
  143. dprintf ("\n");
  144. }
  145. return S_OK;
  146. } // rport
  147. BOOL
  148. DumpConnectionCallback(
  149. ULONG64 ActualAddress,
  150. ULONG64 Context
  151. )
  152. /*++
  153. Routine Description:
  154. EnumEndpoints() callback for dumping AFD_ENDPOINTs.
  155. Arguments:
  156. Endpoint - The current AFD_ENDPOINT.
  157. ActualAddress - The actual address where the structure resides on the
  158. debugee.
  159. Context - The context value passed into EnumEndpoints().
  160. Return Value:
  161. BOOL - TRUE if enumeration should continue, FALSE if it should be
  162. terminated.
  163. --*/
  164. {
  165. ULONG result;
  166. AFD_ENDPOINT endpoint;
  167. ULONG64 connAddr;
  168. endpoint.Type = (USHORT)ReadField (Type);
  169. endpoint.State = (UCHAR)ReadField (State);
  170. if (((endpoint.Type & AfdBlockTypeVcConnecting)==AfdBlockTypeVcConnecting) &&
  171. ( (connAddr=ReadField (Common.VirtualCircuit.Connection))!=0 ||
  172. ((endpoint.State==AfdEndpointStateClosing || endpoint.State==AfdEndpointStateTransmitClosing) &&
  173. (connAddr=ReadField(WorkItem.Context))!=0) ) ) {
  174. result = (ULONG)InitTypeRead (connAddr, AFD!AFD_CONNECTION);
  175. if (result!=0) {
  176. dprintf(
  177. "\nDumpConnectionCallback: Could not read AFD_CONNECTION @ %p, err:%d\n",
  178. connAddr, result
  179. );
  180. return TRUE;
  181. }
  182. if (!(Options & AFDKD_CONDITIONAL) ||
  183. CheckConditional (connAddr, "AFD!AFD_CONNECTION")) {
  184. if (Options & AFDKD_NO_DISPLAY)
  185. dprintf ("+");
  186. else {
  187. if (Options & AFDKD_BRIEF_DISPLAY) {
  188. DumpAfdConnectionBrief(
  189. connAddr
  190. );
  191. }
  192. else {
  193. DumpAfdConnection(
  194. connAddr
  195. );
  196. }
  197. if (Options & AFDKD_FIELD_DISPLAY) {
  198. ProcessFieldOutput (connAddr, "AFD!AFD_CONNECTION");
  199. }
  200. }
  201. EntityCount += 1;
  202. }
  203. else
  204. dprintf (",");
  205. }
  206. else if ((endpoint.Type & AfdBlockTypeVcListening)==AfdBlockTypeVcListening) {
  207. ULONG64 nextEntry;
  208. ULONG64 listHead;
  209. LIST_ENTRY64 listEntry;
  210. listHead = ActualAddress+UnacceptedConnListOffset;
  211. if( !ReadListEntry(
  212. listHead,
  213. &listEntry) ) {
  214. dprintf(
  215. "\nDumpConnectionCallback: Could not read UnacceptedConnectionListHead for endpoint @ %p\n",
  216. ActualAddress
  217. );
  218. return TRUE;
  219. }
  220. nextEntry = listEntry.Flink;
  221. while (nextEntry!=listHead) {
  222. if( CheckControlC() ) {
  223. break;
  224. }
  225. connAddr = nextEntry - ConnectionLinkOffset;
  226. result = (ULONG)InitTypeRead (connAddr, AFD!AFD_CONNECTION);
  227. if (result!=0) {
  228. dprintf(
  229. "\nDumpConnectionCallback: Could not read AFD_CONNECTION @ %p, err:%d\n",
  230. connAddr, result
  231. );
  232. return TRUE;
  233. }
  234. nextEntry = ReadField (ListEntry.Flink);
  235. if (nextEntry==0) {
  236. dprintf(
  237. "\nDumpConnectionCallback: ListEntry.Flink is 0 for AFD_CONNECTION @ %p, err:%d\n",
  238. connAddr, result
  239. );
  240. return TRUE;
  241. }
  242. if (!(Options & AFDKD_CONDITIONAL) ||
  243. CheckConditional (connAddr, "AFD!AFD_CONNECTION")) {
  244. if (Options & AFDKD_NO_DISPLAY)
  245. dprintf ("+");
  246. else {
  247. if (Options & AFDKD_BRIEF_DISPLAY) {
  248. DumpAfdConnectionBrief(
  249. connAddr
  250. );
  251. }
  252. else {
  253. DumpAfdConnection(
  254. connAddr
  255. );
  256. }
  257. if (Options & AFDKD_FIELD_DISPLAY) {
  258. ProcessFieldOutput (connAddr, "AFD!AFD_CONNECTION");
  259. }
  260. }
  261. EntityCount += 1;
  262. }
  263. else
  264. dprintf (",");
  265. }
  266. listHead = ActualAddress + ReturnedConnListOffset;
  267. if( !ReadListEntry(
  268. listHead,
  269. &listEntry) ) {
  270. dprintf(
  271. "\nDumpConnectionCallback: Could not read ReturnedConnectionListHead for endpoint @ %p\n",
  272. ActualAddress
  273. );
  274. return TRUE;
  275. }
  276. nextEntry = listEntry.Flink;
  277. while (nextEntry!=listHead) {
  278. if( CheckControlC() ) {
  279. break;
  280. }
  281. connAddr = nextEntry - ConnectionLinkOffset;
  282. result = (ULONG)InitTypeRead (connAddr, AFD!AFD_CONNECTION);
  283. if (result!=0) {
  284. dprintf(
  285. "\nDumpConnectionCallback: Could not read AFD_CONNECTION @ %p, err:%d\n",
  286. connAddr, result
  287. );
  288. return TRUE;
  289. }
  290. nextEntry = ReadField (ListEntry.Flink);
  291. if (nextEntry==0) {
  292. dprintf(
  293. "\nDumpConnectionCallback: ListEntry.Flink is 0 for AFD_CONNECTION @ %p, err:%d\n",
  294. connAddr, result
  295. );
  296. return TRUE;
  297. }
  298. if (!(Options & AFDKD_CONDITIONAL) ||
  299. CheckConditional (connAddr, "AFD!AFD_CONNECTION")) {
  300. if (Options & AFDKD_NO_DISPLAY)
  301. dprintf ("+");
  302. else {
  303. if (Options & AFDKD_BRIEF_DISPLAY) {
  304. DumpAfdConnectionBrief(
  305. connAddr
  306. );
  307. }
  308. else {
  309. DumpAfdConnection(
  310. connAddr
  311. );
  312. }
  313. if (Options & AFDKD_FIELD_DISPLAY) {
  314. ProcessFieldOutput (connAddr, "AFD!AFD_CONNECTION");
  315. }
  316. }
  317. EntityCount += 1;
  318. }
  319. else
  320. dprintf (",");
  321. }
  322. }
  323. else {
  324. dprintf (".");
  325. }
  326. return TRUE;
  327. } // DumpConnectionCallback
  328. BOOLEAN
  329. PortMatch (
  330. PTRANSPORT_ADDRESS TransportAddress,
  331. USHORT Port
  332. )
  333. {
  334. PTA_IP_ADDRESS ipAddress;
  335. USHORT port;
  336. ipAddress = (PTA_IP_ADDRESS)TransportAddress;
  337. if( ( ipAddress->TAAddressCount != 1 ) ||
  338. ( ipAddress->Address[0].AddressLength < sizeof(TDI_ADDRESS_IP) ) ||
  339. ( ipAddress->Address[0].AddressType != TDI_ADDRESS_TYPE_IP ) ) {
  340. dprintf (",");
  341. return FALSE;
  342. }
  343. port = NTOHS(ipAddress->Address[0].Address[0].sin_port);
  344. return Port == port;
  345. }
  346. BOOL
  347. FindRemotePortCallback(
  348. ULONG64 ActualAddress,
  349. ULONG64 Context
  350. )
  351. /*++
  352. Routine Description:
  353. EnumEndpoints() callback for finding AFD_CONNECTION connected to a specific
  354. port.
  355. Arguments:
  356. Endpoint - The current AFD_ENDPOINT.
  357. ActualAddress - The actual address where the structure resides on the
  358. debugee.
  359. Context - The context value passed into EnumEndpoints().
  360. Return Value:
  361. BOOL - TRUE if enumeration should continue, FALSE if it should be
  362. terminated.
  363. --*/
  364. {
  365. ULONG result;
  366. AFD_ENDPOINT endpoint;
  367. ULONG64 connAddr;
  368. ULONG64 remoteAddr;
  369. ULONG remoteAddrLength;
  370. UCHAR transportAddress[MAX_TRANSPORT_ADDR];
  371. endpoint.Type = (USHORT)ReadField (Type);
  372. endpoint.State = (UCHAR)ReadField (State);
  373. if (((endpoint.Type & AfdBlockTypeVcConnecting)==AfdBlockTypeVcConnecting) &&
  374. ( (connAddr=ReadField (Common.VirtualCircuit.Connection))!=0 ||
  375. ((endpoint.State==AfdEndpointStateClosing || endpoint.State==AfdEndpointStateTransmitClosing) &&
  376. (connAddr=ReadField(WorkItem.Context))!=0) ) ) {
  377. result = (ULONG)InitTypeRead (connAddr, AFD!AFD_CONNECTION);
  378. if (result!=0) {
  379. dprintf(
  380. "\nFindRemotePortCallback: Could not read AFD_CONNECTION @ %p, err:%d\n",
  381. connAddr, result
  382. );
  383. return TRUE;
  384. }
  385. remoteAddr = ReadField (RemoteAddress);
  386. remoteAddrLength = (ULONG)ReadField (RemoteAddressLength);
  387. if (remoteAddr!=0) {
  388. if (!ReadMemory (remoteAddr,
  389. transportAddress,
  390. remoteAddrLength<sizeof (transportAddress)
  391. ? remoteAddrLength
  392. : sizeof (transportAddress),
  393. &remoteAddrLength)) {
  394. dprintf(
  395. "\nFindRemotePortCallback: Could not read remote address for connection @ %p\n",
  396. connAddr
  397. );
  398. return TRUE;
  399. }
  400. }
  401. else {
  402. ULONG64 contextAddr;
  403. //
  404. // Attempt to read user mode data stored as the context
  405. //
  406. result = GetRemoteAddressFromContext (ActualAddress,
  407. transportAddress,
  408. sizeof (transportAddress),
  409. &contextAddr);
  410. if (result!=0) {
  411. dprintf(
  412. "\nFindRemotePortCallback: Could not read remote address for connection @ %p of endpoint context @ %p err:%ld\n",
  413. connAddr, contextAddr, result
  414. );
  415. return TRUE;
  416. }
  417. }
  418. if (PortMatch ((PVOID)transportAddress, (USHORT)Context) &&
  419. (!(Options & AFDKD_CONDITIONAL) ||
  420. CheckConditional (connAddr, "AFD!AFD_CONNECTION")) ) {
  421. if (Options & AFDKD_NO_DISPLAY)
  422. dprintf ("+");
  423. else {
  424. if (Options & AFDKD_BRIEF_DISPLAY) {
  425. DumpAfdConnectionBrief(
  426. connAddr
  427. );
  428. }
  429. else {
  430. DumpAfdConnection(
  431. connAddr
  432. );
  433. }
  434. if (Options & AFDKD_FIELD_DISPLAY) {
  435. ProcessFieldOutput (connAddr, "AFD!AFD_CONNECTION");
  436. }
  437. }
  438. EntityCount += 1;
  439. }
  440. else
  441. dprintf (",");
  442. }
  443. else if ((endpoint.Type & AfdBlockTypeVcListening)==AfdBlockTypeVcListening) {
  444. ULONG64 nextEntry;
  445. ULONG64 listHead;
  446. LIST_ENTRY64 listEntry;
  447. listHead = ActualAddress+ UnacceptedConnListOffset;
  448. if( !ReadListEntry(
  449. listHead,
  450. &listEntry) ) {
  451. dprintf(
  452. "\nFindRemotePortCallback: Could not read UnacceptedConnectionListHead for endpoint @ %p\n",
  453. ActualAddress
  454. );
  455. return TRUE;
  456. }
  457. nextEntry = listEntry.Flink;
  458. while (nextEntry!=listHead) {
  459. if( CheckControlC() ) {
  460. break;
  461. }
  462. connAddr = nextEntry - ConnectionLinkOffset;
  463. result = (ULONG)InitTypeRead (connAddr, AFD!AFD_CONNECTION);
  464. if (result!=0) {
  465. dprintf(
  466. "\nFindRemotePortCallback: Could not read AFD_CONNECTION @ %p, err:%d\n",
  467. connAddr, result
  468. );
  469. return TRUE;
  470. }
  471. nextEntry = ReadField (ListEntry.Flink);
  472. if (nextEntry==0) {
  473. dprintf(
  474. "\nFindRemotePortCallback: ListEntry.Flink is 0 for AFD_CONNECTION @ %p, err:%d\n",
  475. connAddr, result
  476. );
  477. return TRUE;
  478. }
  479. remoteAddr = ReadField (RemoteAddress);
  480. remoteAddrLength = (ULONG)ReadField (RemoteAddressLength);
  481. if (remoteAddr!=0) {
  482. if (!ReadMemory (remoteAddr,
  483. transportAddress,
  484. remoteAddrLength<sizeof (transportAddress)
  485. ? remoteAddrLength
  486. : sizeof (transportAddress),
  487. &remoteAddrLength)) {
  488. dprintf(
  489. "\nFindRemotePortCallback: Could not read remote address for connection @ %p\n",
  490. connAddr
  491. );
  492. continue;
  493. }
  494. }
  495. if (PortMatch ((PVOID)transportAddress, (USHORT)Context) &&
  496. (!(Options & AFDKD_CONDITIONAL) ||
  497. CheckConditional (connAddr, "AFD!AFD_CONNECTION")) ) {
  498. if (Options & AFDKD_NO_DISPLAY)
  499. dprintf ("+");
  500. else {
  501. if (Options & AFDKD_BRIEF_DISPLAY) {
  502. DumpAfdConnectionBrief(
  503. connAddr
  504. );
  505. }
  506. else {
  507. DumpAfdConnection(
  508. connAddr
  509. );
  510. }
  511. if (Options & AFDKD_FIELD_DISPLAY) {
  512. ProcessFieldOutput (connAddr, "AFD!AFD_CONNECTION");
  513. }
  514. }
  515. EntityCount += 1;
  516. }
  517. else {
  518. dprintf (",");
  519. }
  520. }
  521. listHead = ActualAddress + ReturnedConnListOffset;
  522. if( !ReadListEntry(
  523. listHead,
  524. &listEntry) ) {
  525. dprintf(
  526. "\nFindRemotePortCallback: Could not read ReturnedConnectionListHead for endpoint @ %p\n",
  527. ActualAddress
  528. );
  529. return TRUE;
  530. }
  531. nextEntry = listEntry.Flink;
  532. while (nextEntry!=listHead) {
  533. if( CheckControlC() ) {
  534. break;
  535. }
  536. connAddr = nextEntry - ConnectionLinkOffset;
  537. result = (ULONG)InitTypeRead (connAddr, AFD!AFD_CONNECTION);
  538. if (result!=0) {
  539. dprintf(
  540. "\nDumpConnectionCallback: cannot read AFD_CONNECTION @ %p, err:%d\n",
  541. connAddr, result
  542. );
  543. return TRUE;
  544. }
  545. nextEntry = ReadField (ListEntry.Flink);
  546. if (nextEntry==0) {
  547. dprintf(
  548. "\nFindRemotePortCallback: ListEntry.Flink is 0 for AFD_CONNECTION @ %p, err:%d\n",
  549. connAddr, result
  550. );
  551. return TRUE;
  552. }
  553. remoteAddr = ReadField (RemoteAddress);
  554. remoteAddrLength = (ULONG)ReadField (RemoteAddressLength);
  555. if (remoteAddr!=0) {
  556. if (!ReadMemory (remoteAddr,
  557. transportAddress,
  558. remoteAddrLength<sizeof (transportAddress)
  559. ? remoteAddrLength
  560. : sizeof (transportAddress),
  561. &remoteAddrLength)) {
  562. dprintf(
  563. "\nFindRemotePortCallback: Could not read remote address for connection @ %p\n",
  564. connAddr
  565. );
  566. continue;
  567. }
  568. }
  569. if (PortMatch ((PVOID)transportAddress, (USHORT)Context) &&
  570. (!(Options & AFDKD_CONDITIONAL) ||
  571. CheckConditional (connAddr, "AFD!AFD_CONNECTION")) ) {
  572. if (Options & AFDKD_NO_DISPLAY)
  573. dprintf ("+");
  574. else {
  575. if (Options & AFDKD_BRIEF_DISPLAY) {
  576. DumpAfdConnectionBrief(
  577. connAddr
  578. );
  579. }
  580. else {
  581. DumpAfdConnection(
  582. connAddr
  583. );
  584. }
  585. if (Options & AFDKD_FIELD_DISPLAY) {
  586. ProcessFieldOutput (connAddr, "AFD!AFD_CONNECTION");
  587. }
  588. }
  589. EntityCount += 1;
  590. }
  591. else {
  592. dprintf (",");
  593. }
  594. }
  595. }
  596. else {
  597. dprintf (".");
  598. }
  599. return TRUE;
  600. } // FindRemotePortCallback