- Removed prefix.c and the prefix list. Adapter and route netmasks are now
[reactos.git] / reactos / drivers / lib / ip / network / router.c
index e9b62fc..7eeb7eb 100644 (file)
@@ -153,133 +153,9 @@ UINT CommonPrefixLength(
     for (j = 0; (Addr1[i] & Bitmask) == (Addr2[i] & Bitmask); j++)
         Bitmask >>= 1;
 
-    return 8 * i + j;
-}
-
-
-BOOLEAN HasPrefix(
-    PIP_ADDRESS Address,
-    PIP_ADDRESS Prefix,
-    UINT Length)
-/*
- * FUNCTION: Determines wether an address has an given prefix
- * ARGUMENTS:
- *     Address = Pointer to address to use
- *     Prefix  = Pointer to prefix to check for
- *     Length  = Length of prefix
- * RETURNS:
- *     TRUE if the address has the prefix, FALSE if not
- * NOTES:
- *     The two addresses must be of the same type
- */
-{
-    PUCHAR pAddress = (PUCHAR)&Address->Address;
-    PUCHAR pPrefix  = (PUCHAR)&Prefix->Address;
-
-    TI_DbgPrint(DEBUG_ROUTER, ("Called. Address (0x%X)  Prefix (0x%X)  Length (%d).\n", Address, Prefix, Length));
-
-#if 0
-    TI_DbgPrint(DEBUG_ROUTER, ("Address (%s)  Prefix (%s).\n",
-        A2S(Address), A2S(Prefix)));
-#endif
-
-    /* Check that initial integral bytes match */
-    while (Length > 8) {
-        if (*pAddress++ != *pPrefix++)
-            return FALSE;
-        Length -= 8;
-    } 
-
-    /* Check any remaining bits */
-    if ((Length > 0) && ((*pAddress >> (8 - Length)) != (*pPrefix >> (8 - Length))))
-        return FALSE;
-
-    return TRUE;
-}
-
-
-PNET_TABLE_ENTRY RouterFindBestNTE(
-    PIP_INTERFACE Interface,
-    PIP_ADDRESS Destination)
-/*
- * FUNCTION: Checks all on-link prefixes to find out if an address is on-link
- * ARGUMENTS:
- *     Interface   = Pointer to interface to use
- *     Destination = Pointer to destination address
- * NOTES:
- *     If found the NTE if referenced
- * RETURNS:
- *     Pointer to NTE if found, NULL if not
- */
-{
-    KIRQL OldIrql;
-    PLIST_ENTRY CurrentEntry;
-    PNET_TABLE_ENTRY Current;
-    UINT Length, BestLength  = 0;
-    PNET_TABLE_ENTRY BestNTE = NULL;
-
-    TI_DbgPrint(DEBUG_ROUTER, ("Called. Interface (0x%X)  Destination (0x%X).\n", Interface, Destination));
-
-    TI_DbgPrint(DEBUG_ROUTER, ("Destination (%s).\n", A2S(Destination)));
-
-    TcpipAcquireSpinLock(&Interface->Lock, &OldIrql);
-
-    CurrentEntry = Interface->NTEListHead.Flink;
-    while (CurrentEntry != &Interface->NTEListHead) {
-       Current = CONTAINING_RECORD(CurrentEntry, NET_TABLE_ENTRY, IFListEntry);
-       TI_DbgPrint(DEBUG_ROUTER, ("Looking at NTE %s\n", A2S(Current->Address)));
-
-        Length = CommonPrefixLength(Destination, Current->Address);
-        if (BestNTE) {
-            if (Length > BestLength) {
-                BestNTE    = Current;
-                BestLength = Length;
-            }
-        } else {
-            BestNTE    = Current;
-            BestLength = Length;
-        }
-        CurrentEntry = CurrentEntry->Flink;
-    }
-
-    TcpipReleaseSpinLock(&Interface->Lock, OldIrql);
+    TI_DbgPrint(DEBUG_ROUTER, ("Returning %d\n", 8 * i + j));
 
-    return BestNTE;
-}
-
-
-PIP_INTERFACE RouterFindOnLinkInterface(
-    PIP_ADDRESS Address,
-    PNET_TABLE_ENTRY NTE)
-/*
- * FUNCTION: Checks all on-link prefixes to find out if an address is on-link
- * ARGUMENTS:
- *     Address = Pointer to address to check
- *     NTE     = Pointer to NTE to check (NULL = check all interfaces)
- * RETURNS:
- *     Pointer to interface if address is on-link, NULL if not
- */
-{
-    PLIST_ENTRY CurrentEntry;
-    PPREFIX_LIST_ENTRY Current;
-
-    TI_DbgPrint(DEBUG_ROUTER, ("Called. Address (0x%X)  NTE (0x%X).\n", Address, NTE));
-
-    TI_DbgPrint(DEBUG_ROUTER, ("Address (%s)  NTE (%s).\n",
-                              A2S(Address), NTE ? A2S(NTE->Address) : ""));
-
-    CurrentEntry = PrefixListHead.Flink;
-    while (CurrentEntry != &PrefixListHead) {
-           Current = CONTAINING_RECORD(CurrentEntry, PREFIX_LIST_ENTRY, ListEntry);
-
-        if (HasPrefix(Address, &Current->Prefix, Current->PrefixLength) &&
-            ((!NTE) || (NTE->Interface == Current->Interface)))
-            return Current->Interface;
-
-        CurrentEntry = CurrentEntry->Flink;
-    }
-
-    return NULL;
+    return 8 * i + j;
 }
 
 
@@ -335,15 +211,11 @@ PFIB_ENTRY RouterAddRoute(
 }
 
 
-PNEIGHBOR_CACHE_ENTRY RouterGetRoute(
-    PIP_ADDRESS Destination,
-    PNET_TABLE_ENTRY NTE)
+PNEIGHBOR_CACHE_ENTRY RouterGetRoute(PIP_ADDRESS Destination)
 /*
  * FUNCTION: Finds a router to use to get to Destination
  * ARGUMENTS:
  *     Destination = Pointer to destination address (NULL means don't care)
- *     NTE         = Pointer to NTE describing net to send on
- *                   (NULL means don't care)
  * RETURNS:
  *     Pointer to NCE for router, NULL if none was found
  * NOTES:
@@ -358,11 +230,9 @@ PNEIGHBOR_CACHE_ENTRY RouterGetRoute(
     UINT Length, BestLength = 0;
     PNEIGHBOR_CACHE_ENTRY NCE, BestNCE = NULL;
 
-    TI_DbgPrint(DEBUG_ROUTER, ("Called. Destination (0x%X)  NTE (0x%X).\n", Destination, NTE));
+    TI_DbgPrint(DEBUG_ROUTER, ("Called. Destination (0x%X)\n", Destination));
 
     TI_DbgPrint(DEBUG_ROUTER, ("Destination (%s)\n", A2S(Destination)));
-    if( NTE )
-       TI_DbgPrint(DEBUG_ROUTER, ("NTE (%s).\n", A2S(NTE->Address)));
 
     TcpipAcquireSpinLock(&FIBLock, &OldIrql);
 
@@ -374,28 +244,27 @@ PNEIGHBOR_CACHE_ENTRY RouterGetRoute(
         NCE   = Current->Router;
         State = NCE->State;
 
-        if ((!NTE) || (NTE->Interface == NCE->Interface)) {
-            if (Destination)
-                Length = CommonPrefixLength(Destination, &NCE->Address);
-            else
-                Length = 0;
-
-            if (BestNCE) {
-                if ((State  > BestState)  || 
-                    ((State == BestState) &&
-                    (Length > BestLength))) {
-                    /* This seems to be a better router */
-                    BestNCE    = NCE;
-                    BestLength = Length;
-                    BestState  = State;
-                }
-            } else {
-                /* First suitable router found, save it */
-                BestNCE    = NCE;
-                BestLength = Length;
-                BestState  = State;
-            }
-        }
+       if (Destination)
+           Length = CommonPrefixLength(Destination, &NCE->Address);
+       else
+           Length = 0;
+       
+       if (BestNCE) {
+           if ((State  > BestState)  || 
+               ((State == BestState) &&
+                (Length > BestLength))) {
+               /* This seems to be a better router */
+               BestNCE    = NCE;
+               BestLength = Length;
+               BestState  = State;
+           }
+       } else {
+           /* First suitable router found, save it */
+           BestNCE    = NCE;
+           BestLength = Length;
+           BestState  = State;
+       }
+
         CurrentEntry = NextEntry;
     }
 
@@ -425,11 +294,11 @@ VOID RouterRemoveRoute(
 }
 
 
-PFIB_ENTRY RouterCreateRouteIPv4(
-    IPv4_RAW_ADDRESS NetworkAddress,
-    IPv4_RAW_ADDRESS Netmask,
-    IPv4_RAW_ADDRESS RouterAddress,
-    PNET_TABLE_ENTRY NTE,
+PFIB_ENTRY RouterCreateRoute(
+    IP_ADDRESS NetworkAddress,
+    IP_ADDRESS Netmask,
+    IP_ADDRESS RouterAddress,
+    PIP_INTERFACE Interface,
     UINT Metric)
 /*
  * FUNCTION: Creates a route with IPv4 addresses as parameters
@@ -445,42 +314,21 @@ PFIB_ENTRY RouterCreateRouteIPv4(
  *     for providing this reference
  */
 {
-    PIP_ADDRESS pNetworkAddress;
-    PIP_ADDRESS pNetmask;
-    PIP_ADDRESS pRouterAddress;
     PNEIGHBOR_CACHE_ENTRY NCE;
     PFIB_ENTRY FIBE;
 
-    pNetworkAddress = AddrBuildIPv4(NetworkAddress);
-    if (!pNetworkAddress) {
-        TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
-        return NULL;
-    }
-
-    pNetmask = AddrBuildIPv4(Netmask);
-    if (!pNetmask) {
-        TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
-        return NULL;
-    }
-
-    pRouterAddress = AddrBuildIPv4(RouterAddress);
-    if (!pRouterAddress) {
-        TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
-        return NULL;
-    }
-
     /* The NCE references RouterAddress. The NCE is referenced for us */
-    NCE = NBAddNeighbor(NTE->Interface,
-                        pRouterAddress,
+    NCE = NBAddNeighbor(Interface,
+                        &RouterAddress,
                         NULL,
-                        NTE->Interface->AddressLength,
+                        Interface->AddressLength,
                         NUD_PROBE);
     if (!NCE) {
         /* Not enough free resources */
         return NULL;
     }
 
-    FIBE = RouterAddRoute(pNetworkAddress, pNetmask, NCE, 1);
+    FIBE = RouterAddRoute(&NetworkAddress, &Netmask, NCE, 1);
     if (!FIBE) {
         /* Not enough free resources */
         NBRemoveNeighbor(NCE);