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.

292 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. ifdbase.c
  5. Abstract:
  6. Functions to manipulate the interfaces database
  7. Author:
  8. Stefan Solomon 04/10/1995
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #define ifhashindex(IfIndex) (IfIndex) % IF_HASH_TABLE_SIZE
  14. // THESE FUNCTIONS ASSUME THE ROUTER MANAGER IS IN CRITICAL SECTION WHEN CALLED
  15. //***
  16. //
  17. // Function: InitIfDB
  18. //
  19. // Descr:
  20. //
  21. //***
  22. VOID
  23. InitIfDB(VOID)
  24. {
  25. int i;
  26. PLIST_ENTRY IfHtBucketp;
  27. IfHtBucketp = IndexIfHt;
  28. for(i=0; i<IF_HASH_TABLE_SIZE; i++, IfHtBucketp++) {
  29. InitializeListHead(IfHtBucketp);
  30. }
  31. InitializeListHead(&IndexIfList);
  32. }
  33. //***
  34. //
  35. // Function: AddIfToDB
  36. //
  37. // Descr: Inserts a new interface in the Data Base
  38. // The new if is inserted in the hash table of interface indices
  39. // and the linked list of interfaces ordered by index
  40. //
  41. //***
  42. VOID
  43. AddIfToDB(PICB icbp)
  44. {
  45. int hv;
  46. PLIST_ENTRY lep;
  47. PICB list_icbp;
  48. // insert in index hash table
  49. hv = ifhashindex(icbp->InterfaceIndex);
  50. InsertTailList(&IndexIfHt[hv], &icbp->IndexHtLinkage);
  51. // insert in the list ordered by index
  52. lep = IndexIfList.Flink;
  53. while(lep != &IndexIfList)
  54. {
  55. list_icbp = CONTAINING_RECORD(lep, ICB, IndexListLinkage);
  56. if (list_icbp->InterfaceIndex > icbp->InterfaceIndex) {
  57. InsertTailList(lep, &icbp->IndexListLinkage);
  58. return;
  59. }
  60. lep = list_icbp->IndexListLinkage.Flink;
  61. }
  62. InsertTailList(lep, &icbp->IndexListLinkage);
  63. }
  64. //***
  65. //
  66. // Function: RemoveIfFromDB
  67. //
  68. // Descr: Removes an interface from the interface data base
  69. //
  70. //***
  71. VOID
  72. RemoveIfFromDB(PICB icbp)
  73. {
  74. RemoveEntryList(&icbp->IndexHtLinkage);
  75. RemoveEntryList(&icbp->IndexListLinkage);
  76. }
  77. PICB
  78. GetInterfaceByIndex(ULONG IfIndex)
  79. {
  80. PICB icbp;
  81. PLIST_ENTRY lep;
  82. int hv;
  83. hv = ifhashindex(IfIndex);
  84. lep = IndexIfHt[hv].Flink;
  85. while(lep != &IndexIfHt[hv])
  86. {
  87. icbp = CONTAINING_RECORD(lep, ICB, IndexHtLinkage);
  88. if (icbp->InterfaceIndex == IfIndex) {
  89. return icbp;
  90. }
  91. lep = icbp->IndexHtLinkage.Flink;
  92. }
  93. return NULL;
  94. }
  95. PICB
  96. GetInterfaceByName(LPWSTR IfName)
  97. {
  98. PICB icbp;
  99. PLIST_ENTRY lep;
  100. lep = IndexIfList.Flink;
  101. while(lep != &IndexIfList)
  102. {
  103. icbp = CONTAINING_RECORD(lep, ICB, IndexListLinkage);
  104. if(!_wcsicmp(IfName, icbp->InterfaceNamep)) {
  105. // found !
  106. return icbp;
  107. }
  108. lep = icbp->IndexListLinkage.Flink;
  109. }
  110. return NULL;
  111. }
  112. /*++
  113. Function: GetInterfaceByAdapterName
  114. Descr: scans the list of interfaces looking for the adapter name
  115. on dedicated interfaces
  116. --*/
  117. PICB
  118. GetInterfaceByAdapterName(LPWSTR AdapterName)
  119. {
  120. PICB icbp;
  121. PLIST_ENTRY lep;
  122. lep = IndexIfList.Flink;
  123. while(lep != &IndexIfList)
  124. {
  125. icbp = CONTAINING_RECORD(lep, ICB, IndexListLinkage);
  126. if(icbp->MIBInterfaceType == IF_TYPE_LAN) {
  127. if(!_wcsicmp(AdapterName, icbp->AdapterNamep)) {
  128. // found !
  129. return icbp;
  130. }
  131. }
  132. lep = icbp->IndexListLinkage.Flink;
  133. }
  134. return NULL;
  135. }
  136. /*++
  137. Function: GetInterfaceByAdptNameAndPktType
  138. Descr: Iterates through all interfaces looking for one that matches the
  139. given packet type and name.
  140. [pmay] I added this because some interface bindings weren't taking place
  141. during pnp because there would be multiple interfaces with the same
  142. adapter name whose binding depended on the packet type.
  143. --*/
  144. PICB
  145. GetInterfaceByAdptNameAndPktType(LPWSTR AdapterName, DWORD dwType)
  146. {
  147. PICB icbp;
  148. PLIST_ENTRY lep;
  149. lep = IndexIfList.Flink;
  150. while(lep != &IndexIfList) {
  151. icbp = CONTAINING_RECORD(lep, ICB, IndexListLinkage);
  152. if(icbp->MIBInterfaceType == IF_TYPE_LAN) {
  153. if((_wcsicmp(AdapterName, icbp->AdapterNamep) == 0) &&
  154. ((icbp->PacketType == AUTO_DETECT_PACKET_TYPE) || (icbp->PacketType == dwType)))
  155. {
  156. return icbp;
  157. }
  158. }
  159. lep = icbp->IndexListLinkage.Flink;
  160. }
  161. return NULL;
  162. }
  163. /*++
  164. Function: EnumerateFirstInterfaceIndex
  165. Descr: returns the first interface index, if any
  166. Note: called with database lock held
  167. --*/
  168. DWORD
  169. EnumerateFirstInterfaceIndex(PULONG InterfaceIndexp)
  170. {
  171. PICB icbp;
  172. if(!IsListEmpty(&IndexIfList)) {
  173. icbp = CONTAINING_RECORD(IndexIfList.Flink, ICB, IndexListLinkage);
  174. *InterfaceIndexp = icbp->InterfaceIndex;
  175. return NO_ERROR;
  176. }
  177. else
  178. {
  179. return ERROR_NO_MORE_ITEMS;
  180. }
  181. }
  182. /*++
  183. Function: EnumerateNextInterfaceIndex
  184. Descr: returns next if index in the database
  185. Descr: called with database lock held
  186. --*/
  187. DWORD
  188. EnumerateNextInterfaceIndex(PULONG InterfaceIndexp)
  189. {
  190. PLIST_ENTRY lep;
  191. PICB icbp;
  192. // scan the index if list until we find the next if index
  193. lep = IndexIfList.Flink;
  194. while(lep != &IndexIfList)
  195. {
  196. icbp = CONTAINING_RECORD(lep, ICB, IndexListLinkage);
  197. if (icbp->InterfaceIndex > *InterfaceIndexp) {
  198. *InterfaceIndexp = icbp->InterfaceIndex;
  199. return NO_ERROR;
  200. }
  201. lep = lep->Flink;
  202. }
  203. return ERROR_NO_MORE_ITEMS;
  204. }