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.

715 lines
18 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. File contains the following functions
  6. LocateIfRow
  7. LocateIpAddrRow
  8. LocateIpForwardRow
  9. LocateIpNetRow
  10. LocateUdpRow
  11. LocateUdp6Row
  12. LocateTcpRow
  13. LocateTcp6Row
  14. The LocateXXXRow Functions are passed a variable sized array of indices, a count of
  15. the number of indices passed and the type of query for which the location is being
  16. done (GET_FIRST, NEXT or GET/SET/CREATE/DELETE - there are only three type of
  17. behaviours of the locators). They fill in the index of the corresponding row and return
  18. ERROR_NO_DATA, ERROR_INVALID_INDEX, NO_ERROR or ERROR_NO_MORE_ITEMS
  19. The general search algorithm is this;
  20. If the table is empty, return ERROR_NO_DATA
  21. else
  22. If the query is a GET_FIRST
  23. Return the first row
  24. else
  25. Build the index as follows:
  26. Set the Index to all 0s
  27. From the number of indices passed figure out how much of the index you can build
  28. keeping the rest 0. If the query is a GET, SET, CREATE_ENTRY or DELETE_ENTRY
  29. then the complete index must be given. This check is, however, supposed to be done
  30. at the agent.
  31. If the full index has not been given, the index is deemed to be modified (Again
  32. this can only happen in the NEXT case).
  33. After this a search is done. We try for an exact match with the index. For all
  34. queries other than NEXT there is no problem. For NEXT there are two cases:
  35. If the complete index was given and and you get an exact match, then you
  36. return the next entry. If you dont get an exact match you return the next
  37. higher entry
  38. If an incomplete index was given and you modified it by padding 0s, and if
  39. an exact match is found, then you return the matching entry (Of course if an
  40. exact match is not found you again return the next higher entry)
  41. Revision History:
  42. Amritansh Raghav 6/8/95 Created
  43. --*/
  44. #include "allinc.h"
  45. PMIB_IFROW
  46. LocateIfRow(
  47. DWORD dwQueryType,
  48. AsnAny *paaIfIndex
  49. )
  50. {
  51. DWORD i;
  52. DWORD dwIndex;
  53. //
  54. // If there is no index the type is ASN_NULL. This causes the macro to return the
  55. // default value (0 in this case)
  56. //
  57. dwIndex = GetAsnInteger(paaIfIndex, 0);
  58. TraceEnter("LocateIfRow");
  59. if (g_Cache.pRpcIfTable->dwNumEntries is 0)
  60. {
  61. TraceLeave("LocateIfRow");
  62. return NULL;
  63. }
  64. if (dwQueryType is GET_FIRST)
  65. {
  66. TraceLeave("LocateIfRow");
  67. return &(g_Cache.pRpcIfTable->table[0]);
  68. }
  69. for (i = 0; i < g_Cache.pRpcIfTable->dwNumEntries; i++)
  70. {
  71. if ((g_Cache.pRpcIfTable->table[i].dwIndex is dwIndex) and dwQueryType is GET_EXACT)
  72. {
  73. TraceLeave("LocateIfRow");
  74. return &(g_Cache.pRpcIfTable->table[i]);
  75. }
  76. if (g_Cache.pRpcIfTable->table[i].dwIndex > dwIndex)
  77. {
  78. if (dwQueryType is GET_NEXT)
  79. {
  80. TraceLeave("LocateIfRow");
  81. return &(g_Cache.pRpcIfTable->table[i]);
  82. }
  83. else
  84. {
  85. TraceLeave("LocateIfRow");
  86. return NULL;
  87. }
  88. }
  89. }
  90. TraceLeave("LocateIfRow");
  91. return NULL;
  92. }
  93. PMIB_IPADDRROW
  94. LocateIpAddrRow(
  95. DWORD dwQueryType,
  96. AsnAny *paaIpAddr
  97. )
  98. {
  99. LONG lCompare;
  100. DWORD i, dwAddr;
  101. BOOL bNext, bModified;
  102. TraceEnter("LocateIpAddrRow");
  103. if (g_Cache.pRpcIpAddrTable->dwNumEntries is 0)
  104. {
  105. TraceLeave("LocateIpAddrRow");
  106. return NULL;
  107. }
  108. if (dwQueryType is GET_FIRST)
  109. {
  110. TraceLeave("LocateIpAddrRow");
  111. return &(g_Cache.pRpcIpAddrTable->table[0]);
  112. }
  113. dwAddr = GetAsnIPAddress(paaIpAddr, 0x00000000);
  114. bModified = FALSE;
  115. if (IsAsnIPAddressTypeNull(paaIpAddr))
  116. {
  117. bModified = TRUE;
  118. }
  119. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  120. for (i = 0; i < g_Cache.pRpcIpAddrTable->dwNumEntries; i++)
  121. {
  122. if ((InetCmp(dwAddr, g_Cache.pRpcIpAddrTable->table[i].dwAddr, lCompare) is 0) and !bNext)
  123. {
  124. TraceLeave("LocateIpAddrRow");
  125. return &(g_Cache.pRpcIpAddrTable->table[i]);
  126. }
  127. if (lCompare < 0)
  128. {
  129. if (dwQueryType is GET_NEXT)
  130. {
  131. TraceLeave("LocateIpAddrRow");
  132. return &(g_Cache.pRpcIpAddrTable->table[i]);
  133. }
  134. else
  135. {
  136. TraceLeave("LocateIpAddrRow");
  137. return NULL;
  138. }
  139. }
  140. }
  141. TraceLeave("LocateIpAddrRow");
  142. return NULL;
  143. }
  144. PMIB_IPFORWARDROW
  145. LocateIpRouteRow(
  146. DWORD dwQueryType,
  147. AsnAny *paaIpDest
  148. )
  149. {
  150. DWORD dwDest;
  151. LONG lCompare;
  152. DWORD i;
  153. BOOL bNext, bModified;
  154. TraceEnter("LocateIpRouteRow");
  155. if (g_Cache.pRpcIpForwardTable->dwNumEntries is 0)
  156. {
  157. TraceLeave("LocateIpRouteRow");
  158. return NULL;
  159. }
  160. if (dwQueryType is GET_FIRST)
  161. {
  162. TraceLeave("LocateIpRouteRow");
  163. return &(g_Cache.pRpcIpForwardTable->table[0]);
  164. }
  165. dwDest = GetAsnIPAddress(paaIpDest, 0x00000000);
  166. bModified = FALSE;
  167. if (IsAsnIPAddressTypeNull(paaIpDest))
  168. {
  169. bModified = TRUE;
  170. }
  171. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  172. for (i = 0; i < g_Cache.pRpcIpForwardTable->dwNumEntries; i++)
  173. {
  174. if ((InetCmp(dwDest, g_Cache.pRpcIpForwardTable->table[i].dwForwardDest, lCompare) is 0) and !bNext)
  175. {
  176. TraceLeave("LocateIpRouteRow");
  177. return &(g_Cache.pRpcIpForwardTable->table[i]);
  178. }
  179. if (lCompare < 0)
  180. {
  181. if (dwQueryType is GET_NEXT)
  182. {
  183. TraceLeave("LocateIpRouteRow");
  184. return &(g_Cache.pRpcIpForwardTable->table[i]);
  185. }
  186. else
  187. {
  188. TraceLeave("LocateIpRouteRow");
  189. return NULL;
  190. }
  191. }
  192. }
  193. TraceLeave("LocateIpRouteRow");
  194. return NULL;
  195. }
  196. PMIB_IPFORWARDROW
  197. LocateIpForwardRow(
  198. DWORD dwQueryType,
  199. AsnAny *paaDest,
  200. AsnAny *paaProto,
  201. AsnAny *paaPolicy,
  202. AsnAny *paaNextHop
  203. )
  204. {
  205. DWORD dwIpForwardIndex[4];
  206. LONG lCompare;
  207. DWORD i;
  208. BOOL bNext, bModified;
  209. TraceEnter("LocateIpForwardRow");
  210. if (g_Cache.pRpcIpForwardTable->dwNumEntries is 0)
  211. {
  212. TraceLeave("LocateIpForwardRow");
  213. return NULL;
  214. }
  215. if (dwQueryType is GET_FIRST)
  216. {
  217. TraceLeave("LocateIpForwardRow");
  218. return &(g_Cache.pRpcIpForwardTable->table[0]);
  219. }
  220. dwIpForwardIndex[0] = GetAsnIPAddress(paaDest, 0x00000000);
  221. dwIpForwardIndex[1] = GetAsnInteger(paaProto, 0);
  222. dwIpForwardIndex[2] = GetAsnInteger(paaPolicy, 0);
  223. dwIpForwardIndex[3] = GetAsnIPAddress(paaNextHop, 0x00000000);
  224. bModified = FALSE;
  225. if (IsAsnIPAddressTypeNull(paaDest) or
  226. IsAsnTypeNull(paaProto) or
  227. IsAsnTypeNull(paaPolicy) or
  228. IsAsnIPAddressTypeNull(paaNextHop))
  229. {
  230. bModified = TRUE;
  231. }
  232. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  233. for (i = 0; i < g_Cache.pRpcIpForwardTable->dwNumEntries; i++)
  234. {
  235. lCompare = IpForwardCmp(dwIpForwardIndex[0],
  236. dwIpForwardIndex[1],
  237. dwIpForwardIndex[2],
  238. dwIpForwardIndex[3],
  239. g_Cache.pRpcIpForwardTable->table[i].dwForwardDest,
  240. g_Cache.pRpcIpForwardTable->table[i].dwForwardProto,
  241. g_Cache.pRpcIpForwardTable->table[i].dwForwardPolicy,
  242. g_Cache.pRpcIpForwardTable->table[i].dwForwardNextHop);
  243. if ((lCompare is 0) and !bNext)
  244. {
  245. TraceLeave("LocateIpForwardRow");
  246. return &(g_Cache.pRpcIpForwardTable->table[i]);
  247. }
  248. if (lCompare < 0)
  249. {
  250. if (dwQueryType is GET_NEXT)
  251. {
  252. TraceLeave("LocateIpForwardRow");
  253. return &(g_Cache.pRpcIpForwardTable->table[i]);
  254. }
  255. else
  256. {
  257. TraceLeave("LocateIpForwardRow");
  258. return NULL;
  259. }
  260. }
  261. }
  262. TraceLeave("LocateIpForwardRow");
  263. return NULL;
  264. }
  265. PMIB_IPNETROW
  266. LocateIpNetRow(
  267. DWORD dwQueryType,
  268. AsnAny *paaIndex,
  269. AsnAny *paaAddr
  270. )
  271. {
  272. DWORD i;
  273. LONG lCompare;
  274. DWORD dwIpNetIfIndex, dwIpNetIpAddr;
  275. BOOL bNext, bModified;
  276. TraceEnter("LocateIfNetRow");
  277. if (g_Cache.pRpcIpNetTable->dwNumEntries is 0)
  278. {
  279. TraceLeave("LocateIpNetRow");
  280. return NULL;
  281. }
  282. if (dwQueryType is GET_FIRST)
  283. {
  284. TraceLeave("LocateIpNetRow");
  285. return &(g_Cache.pRpcIpNetTable->table[0]);
  286. }
  287. bModified = FALSE;
  288. dwIpNetIfIndex = GetAsnInteger(paaIndex, 0);
  289. dwIpNetIpAddr = GetAsnIPAddress(paaAddr, 0x00000000);
  290. if (IsAsnTypeNull(paaIndex) or
  291. IsAsnIPAddressTypeNull(paaAddr))
  292. {
  293. bModified = TRUE;
  294. }
  295. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  296. for (i = 0; i < g_Cache.pRpcIpNetTable->dwNumEntries; i++)
  297. {
  298. lCompare = IpNetCmp(dwIpNetIfIndex,
  299. dwIpNetIpAddr,
  300. g_Cache.pRpcIpNetTable->table[i].dwIndex,
  301. g_Cache.pRpcIpNetTable->table[i].dwAddr);
  302. if ((lCompare is 0) and !bNext)
  303. {
  304. TraceLeave("LocateIpNetRow");
  305. return &(g_Cache.pRpcIpNetTable->table[i]);
  306. }
  307. if (lCompare < 0)
  308. {
  309. if (dwQueryType is GET_NEXT)
  310. {
  311. TraceLeave("LocateIpNetRow");
  312. return &(g_Cache.pRpcIpNetTable->table[i]);
  313. }
  314. else
  315. {
  316. TraceLeave("LocateIpNetRow");
  317. return NULL;
  318. }
  319. }
  320. }
  321. TraceLeave("LocateIpNetRow");
  322. return NULL;
  323. }
  324. PMIB_UDPROW
  325. LocateUdpRow(
  326. DWORD dwQueryType,
  327. AsnAny *paaLocalAddr,
  328. AsnAny *paaLocalPort
  329. )
  330. {
  331. DWORD i;
  332. LONG lCompare;
  333. DWORD dwIndex[2];
  334. BOOL bNext, bModified;
  335. TraceEnter("LocateUdpRow");
  336. if (g_Cache.pRpcUdpTable->dwNumEntries is 0)
  337. {
  338. TraceLeave("LocateUdpRow");
  339. return NULL;
  340. }
  341. if (dwQueryType is GET_FIRST)
  342. {
  343. TraceLeave("LocateUdpRow");
  344. return &(g_Cache.pRpcUdpTable->table[0]);
  345. }
  346. #define LOCAL_ADDR 0
  347. #define LOCAL_PORT 1
  348. dwIndex[LOCAL_ADDR] = GetAsnIPAddress(paaLocalAddr, 0x00000000);
  349. dwIndex[LOCAL_PORT] = GetAsnInteger(paaLocalPort, 0);
  350. bModified = FALSE;
  351. if (IsAsnIPAddressTypeNull(paaLocalAddr) or
  352. IsAsnTypeNull(paaLocalPort))
  353. {
  354. bModified = TRUE;
  355. }
  356. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  357. for (i = 0; i < g_Cache.pRpcUdpTable->dwNumEntries; i++)
  358. {
  359. lCompare = UdpCmp(dwIndex[LOCAL_ADDR],
  360. dwIndex[LOCAL_PORT],
  361. g_Cache.pRpcUdpTable->table[i].dwLocalAddr,
  362. g_Cache.pRpcUdpTable->table[i].dwLocalPort);
  363. if ((lCompare is 0) and !bNext)
  364. {
  365. TraceLeave("LocateUdpRow");
  366. return &(g_Cache.pRpcUdpTable->table[i]);
  367. }
  368. if (lCompare < 0)
  369. {
  370. if (dwQueryType is GET_NEXT)
  371. {
  372. TraceLeave("LocateUdpRow");
  373. return &(g_Cache.pRpcUdpTable->table[i]);
  374. }
  375. else
  376. {
  377. TraceLeave("LocateUdpRow");
  378. return NULL;
  379. }
  380. }
  381. }
  382. TraceLeave("LocateUdpRow");
  383. return NULL;
  384. }
  385. PUDP6ListenerEntry
  386. LocateUdp6Row(
  387. DWORD dwQueryType,
  388. AsnAny *paaLocalAddr,
  389. AsnAny *paaLocalPort
  390. )
  391. {
  392. DWORD i;
  393. LONG lCompare;
  394. DWORD dwLocalPort;
  395. BOOL bNext, bModified;
  396. // address plus scope id
  397. BYTE rgbyLocalAddrEx[20];
  398. TraceEnter("LocateUdp6Row");
  399. if (g_Cache.pRpcUdp6ListenerTable->dwNumEntries is 0)
  400. {
  401. TraceLeave("LocateUdp6Row");
  402. return NULL;
  403. }
  404. if (dwQueryType is GET_FIRST)
  405. {
  406. TraceLeave("LocateUdp6Row");
  407. return &(g_Cache.pRpcUdp6ListenerTable->table[0]);
  408. }
  409. // zero out scope id in case the octet string doesn't include it
  410. ZeroMemory(rgbyLocalAddrEx, sizeof(rgbyLocalAddrEx));
  411. GetAsnOctetString(rgbyLocalAddrEx, paaLocalAddr);
  412. dwLocalPort = GetAsnInteger(paaLocalPort, 0);
  413. bModified = FALSE;
  414. if (IsAsnIPAddressTypeNull(paaLocalAddr) or
  415. IsAsnTypeNull(paaLocalPort))
  416. {
  417. bModified = TRUE;
  418. }
  419. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  420. for (i = 0; i < g_Cache.pRpcUdp6ListenerTable->dwNumEntries; i++)
  421. {
  422. lCompare = Udp6Cmp(rgbyLocalAddrEx,
  423. dwLocalPort,
  424. g_Cache.pRpcUdp6ListenerTable->table[i].ule_localaddr.s6_bytes,
  425. g_Cache.pRpcUdp6ListenerTable->table[i].ule_localport);
  426. if ((lCompare is 0) and !bNext)
  427. {
  428. TraceLeave("LocateUdp6Row");
  429. return &(g_Cache.pRpcUdp6ListenerTable->table[i]);
  430. }
  431. if (lCompare < 0)
  432. {
  433. if (dwQueryType is GET_NEXT)
  434. {
  435. TraceLeave("LocateUdp6Row");
  436. return &(g_Cache.pRpcUdp6ListenerTable->table[i]);
  437. }
  438. else
  439. {
  440. TraceLeave("LocateUdp6Row");
  441. return NULL;
  442. }
  443. }
  444. }
  445. TraceLeave("LocateUdp6Row");
  446. return NULL;
  447. }
  448. PMIB_TCPROW
  449. LocateTcpRow(
  450. DWORD dwQueryType,
  451. AsnAny *paaLocalAddr,
  452. AsnAny *paaLocalPort,
  453. AsnAny *paaRemoteAddr,
  454. AsnAny *paaRemotePort
  455. )
  456. {
  457. LONG lCompare;
  458. DWORD dwIndex[4];
  459. BOOL bNext, bModified;
  460. DWORD startIndex, stopIndex, i;
  461. TraceEnter("LocateTcpRow");
  462. if (g_Cache.pRpcTcpTable->dwNumEntries is 0)
  463. {
  464. TraceLeave("LocateTcpRow");
  465. return NULL;
  466. }
  467. if (dwQueryType is GET_FIRST)
  468. {
  469. TraceLeave("LocateTcpRow");
  470. return &(g_Cache.pRpcTcpTable->table[0]);
  471. }
  472. #define REM_ADDR 2
  473. #define REM_PORT 3
  474. dwIndex[LOCAL_ADDR] = GetAsnIPAddress(paaLocalAddr, 0x00000000);
  475. dwIndex[LOCAL_PORT] = GetAsnInteger(paaLocalPort, 0);
  476. dwIndex[REM_ADDR] = GetAsnIPAddress(paaRemoteAddr, 0x00000000);
  477. dwIndex[REM_PORT] = GetAsnInteger(paaRemotePort, 0);
  478. bModified = FALSE;
  479. if (IsAsnIPAddressTypeNull(paaLocalAddr) or
  480. IsAsnTypeNull(paaLocalPort) or
  481. IsAsnIPAddressTypeNull(paaRemoteAddr) or
  482. IsAsnTypeNull(paaRemotePort))
  483. {
  484. bModified = TRUE;
  485. }
  486. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  487. for (i = 0; i < g_Cache.pRpcTcpTable->dwNumEntries; i++)
  488. {
  489. lCompare = TcpCmp(dwIndex[LOCAL_ADDR],
  490. dwIndex[LOCAL_PORT],
  491. dwIndex[REM_ADDR],
  492. dwIndex[REM_PORT],
  493. g_Cache.pRpcTcpTable->table[i].dwLocalAddr,
  494. g_Cache.pRpcTcpTable->table[i].dwLocalPort,
  495. g_Cache.pRpcTcpTable->table[i].dwRemoteAddr,
  496. g_Cache.pRpcTcpTable->table[i].dwRemotePort);
  497. if ((lCompare is 0) and !bNext)
  498. {
  499. TraceLeave("LocateTcpRow");
  500. return &(g_Cache.pRpcTcpTable->table[i]);
  501. }
  502. if (lCompare < 0)
  503. {
  504. if (dwQueryType is GET_NEXT)
  505. {
  506. TraceLeave("LocateTcpRow");
  507. return &(g_Cache.pRpcTcpTable->table[i]);
  508. }
  509. else
  510. {
  511. TraceLeave("LocateTcpRow");
  512. return NULL;
  513. }
  514. }
  515. }
  516. TraceLeave("LocateTcpRow");
  517. return NULL;
  518. }
  519. PTCP6ConnTableEntry
  520. LocateTcp6Row(
  521. DWORD dwQueryType,
  522. AsnAny *paaLocalAddr,
  523. AsnAny *paaLocalPort,
  524. AsnAny *paaRemoteAddr,
  525. AsnAny *paaRemotePort
  526. )
  527. {
  528. LONG lCompare;
  529. DWORD dwLocalPort, dwRemotePort;
  530. BOOL bNext, bModified;
  531. DWORD startIndex, stopIndex, i;
  532. // address plus scope id
  533. BYTE rgbyLocalAddrEx[20];
  534. BYTE rgbyRemoteAddrEx[20];
  535. TraceEnter("LocateTcp6Row");
  536. if (g_Cache.pRpcTcp6Table->dwNumEntries is 0)
  537. {
  538. TraceLeave("LocateTcp6Row");
  539. return NULL;
  540. }
  541. if (dwQueryType is GET_FIRST)
  542. {
  543. TraceLeave("LocateTcp6Row");
  544. return &(g_Cache.pRpcTcp6Table->table[0]);
  545. }
  546. // zero out scope id in case the octet string doesn't include it
  547. ZeroMemory(rgbyLocalAddrEx, sizeof(rgbyLocalAddrEx));
  548. ZeroMemory(rgbyRemoteAddrEx, sizeof(rgbyRemoteAddrEx));
  549. GetAsnOctetString(rgbyLocalAddrEx, paaLocalAddr);
  550. dwLocalPort = GetAsnInteger(paaLocalPort, 0);
  551. GetAsnOctetString(rgbyRemoteAddrEx, paaRemoteAddr);
  552. dwRemotePort = GetAsnInteger(paaRemotePort, 0);
  553. bModified = FALSE;
  554. if (IsAsnOctetStringTypeNull(paaLocalAddr) or
  555. IsAsnTypeNull(paaLocalPort) or
  556. IsAsnOctetStringTypeNull(paaRemoteAddr) or
  557. IsAsnTypeNull(paaRemotePort))
  558. {
  559. bModified = TRUE;
  560. }
  561. bNext = (dwQueryType is GET_NEXT) and (bModified is FALSE);
  562. for (i = 0; i < g_Cache.pRpcTcp6Table->dwNumEntries; i++)
  563. {
  564. lCompare = Tcp6Cmp(rgbyLocalAddrEx,
  565. dwLocalPort,
  566. rgbyRemoteAddrEx,
  567. dwRemotePort,
  568. g_Cache.pRpcTcp6Table->table[i].tct_localaddr.s6_bytes,
  569. g_Cache.pRpcTcp6Table->table[i].tct_localport,
  570. g_Cache.pRpcTcp6Table->table[i].tct_remoteaddr.s6_bytes,
  571. g_Cache.pRpcTcp6Table->table[i].tct_remoteport);
  572. if ((lCompare is 0) and !bNext)
  573. {
  574. TraceLeave("LocateTcp6Row");
  575. return &(g_Cache.pRpcTcp6Table->table[i]);
  576. }
  577. if (lCompare < 0)
  578. {
  579. if (dwQueryType is GET_NEXT)
  580. {
  581. TraceLeave("LocateTcp6Row");
  582. return &(g_Cache.pRpcTcp6Table->table[i]);
  583. }
  584. else
  585. {
  586. TraceLeave("LocateTcp6Row");
  587. return NULL;
  588. }
  589. }
  590. }
  591. TraceLeave("LocateTcp6Row");
  592. return NULL;
  593. }