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.

206 lines
4.8 KiB

  1. //=============================================================================
  2. // Copyright (c) 1997 Microsoft Corporation
  3. // File Name: if.c
  4. // Abstract:
  5. //
  6. // Author: K.S.Lokesh (lokeshs@) 1-1-98
  7. //=============================================================================
  8. #include "pchdvmrp.h"
  9. #pragma hdrstop
  10. //-----------------------------------------------------------------------------
  11. // _InitializeIfTable
  12. //-----------------------------------------------------------------------------
  13. DWORD
  14. InitializeIfTable(
  15. )
  16. {
  17. DWORD Error = NO_ERROR;
  18. DWORD i;
  19. BEGIN_BREAKOUT_BLOCK1 {
  20. //
  21. // allocate memory for the interface table
  22. //
  23. G_pIfTable = DVMRP_ALLOC_AND_ZERO(sizeof(DVMRP_IF_TABLE));
  24. PROCESS_ALLOC_FAILURE2(G_pIfTable, "interface table",
  25. Error, sizeof(DVMRP_IF_TABLE), GOTO_END_BLOCK1);
  26. // Initialize IfTable list
  27. InitializeListHead(&G_pIfTable->IfList);
  28. //
  29. // Initialize the IfList_CS and PeerLists_CS
  30. //
  31. try {
  32. InitializeCriticalSection(&G_pIfTable->IfList_CS);
  33. InitializeCriticalSection(&G_pIfTable->PeerLists_CS);
  34. }
  35. HANDLE_CRITICAL_SECTION_EXCEPTION(Error, GOTO_END_BLOCK1);
  36. //
  37. // allocate memory for the different buckets
  38. //
  39. G_pIfTable->IfHashTable
  40. = DVMRP_ALLOC(sizeof(LIST_ENTRY)*IF_HASHTABLE_SIZE);
  41. PROCESS_ALLOC_FAILURE2(G_pIfTable->IfHashTable, "interface table",
  42. Error, sizeof(LIST_ENTRY)*IF_HASHTABLE_SIZE, GOTO_END_BLOCK1);
  43. //
  44. // allocate memory for the array of pointers to If dynamic RWLs
  45. //
  46. G_pIfTable->aIfDRWL
  47. = DVMRP_ALLOC(sizeof(PDYNAMIC_RW_LOCK)*IF_HASHTABLE_SIZE);
  48. PROCESS_ALLOC_FAILURE2(G_pIfTable->aIfDRWL, "interface table",
  49. Error, sizeof(PDYNAMIC_RW_LOCK)*IF_HASHTABLE_SIZE,
  50. GOTO_END_BLOCK1);
  51. //
  52. // init locks to NULL, implying that the dynamic locks have not been
  53. // acquired. and initialize the list heads.
  54. //
  55. for (i=0; i<IF_HASHTABLE_SIZE; i++) {
  56. InitializeListHead(&G_pIfTable->IfHashTable[i]);
  57. G_pIfTable->aIfDRWL[i] = NULL;
  58. }
  59. } END_BREAKOUT_BLOCK1;
  60. if (Error != NO_ERROR) {
  61. DeinitializeIfTable();
  62. }
  63. return Error;
  64. }//end _InitializeIfTable
  65. //-----------------------------------------------------------------------------
  66. // _DeInitializeIfTable
  67. //-----------------------------------------------------------------------------
  68. VOID
  69. DeinitializeIfTable(
  70. )
  71. {
  72. PLIST_ENTRY pHead, ple;
  73. PIF_TABLE_ENTRY pite;
  74. if (G_pIfTable==NULL)
  75. return;
  76. //
  77. // go through the interface list and delete all interfaces
  78. //
  79. pHead = &G_pIfTable->IfList;
  80. for (ple=pHead->Flink; ple!=pHead; ple=ple->Flink) {
  81. ple = ple->Flink;
  82. pite = CONTAINING_RECORD(ple, IF_TABLE_ENTRY, Link);
  83. DeleteInterface(pite->IfIndex);
  84. }
  85. // delete the IfList_CS and PeerLists_CS
  86. DeleteCriticalSection(&G_pIfTable->IfList_CS);
  87. DeleteCriticalSection(&G_pIfTable->PeerLists_CS);
  88. // free array of If buckets and If DRWLocks, and the IfTable
  89. DVMRP_FREE(G_pIfTable->IfHashTable);
  90. DVMRP_FREE(G_pIfTable->aIfDRWL);
  91. DVMRP_FREE_AND_NULL(G_pIfTable);
  92. return;
  93. }
  94. //-----------------------------------------------------------------------------
  95. // _GetIfEntry
  96. //
  97. // returns the interface with the given index.
  98. // assumes the interface bucket is either read or write locked
  99. //-----------------------------------------------------------------------------
  100. PIF_TABLE_ENTRY
  101. GetIfEntry(
  102. DWORD IfIndex
  103. )
  104. {
  105. PIF_TABLE_ENTRY pite = NULL;
  106. PLIST_ENTRY pHead, ple;
  107. pHead = GET_IF_HASH_BUCKET(Index);
  108. for (ple=pHead->Flink; ple!=pHead; ple=ple->Flink) {
  109. pite = CONTAINING_RECORD(ple, IF_TABLE_ENTRY, HTLink);
  110. if (pite->IfIndex == IfIndex) {
  111. break;
  112. }
  113. }
  114. return (ple == pHead) ? NULL: pite;
  115. }
  116. //-----------------------------------------------------------------------------
  117. // _GetIfByIndex
  118. //
  119. // returns the interface with the given index.
  120. // assumes the interface bucket is either read or write locked
  121. //-----------------------------------------------------------------------------
  122. PIF_TABLE_ENTRY
  123. GetIfByIndex(
  124. DWORD IfIndex
  125. )
  126. {
  127. PIF_TABLE_ENTRY pite = NULL;
  128. PLIST_ENTRY pHead, ple;
  129. pHead = &G_pIfTable->IfHashTable[IF_HASH_VALUE(IfIndex)];
  130. for (ple=pHead->Flink; ple!=pHead; ple=ple->Flink) {
  131. pite = CONTAINING_RECORD(ple, IF_TABLE_ENTRY, HTLink);
  132. if (pite->IfIndex == IfIndex) {
  133. break;
  134. }
  135. }
  136. return (ple == pHead) ? NULL: pite;
  137. }