General fixes
authorJohn Hodge <[email protected]>
Sat, 20 Nov 2010 03:50:39 +0000 (11:50 +0800)
committerJohn Hodge <[email protected]>
Sat, 20 Nov 2010 03:50:39 +0000 (11:50 +0800)
- Fixed recursion in ARP_Resolve when there is not a direct route
  to the destinaton
 > ARP now does not use routes if the destination address is on the
   current subnet.
- Added debug to routing code, debug to IRC client too

Kernel/arch/x86/lib.c
Kernel/debug.c
Modules/IPStack/arp.c
Modules/IPStack/ipstack.h
Modules/IPStack/main.c
Modules/IPStack/routing.c
Usermode/Applications/ifconfig_src/main.c
Usermode/Applications/irc_src/main.c

index 2fa1e51..9287d9c 100644 (file)
@@ -189,6 +189,8 @@ void *memsetd(void *Dest, Uint32 Val, size_t Num)
  */
 int memcmp(const void *m1, const void *m2, size_t Num)
 {
  */
 int memcmp(const void *m1, const void *m2, size_t Num)
 {
+       if( Num == 0 )  return 1;       // No bytes are always identical
+       
        while(Num--)
        {
                if(*(Uint8*)m1 != *(Uint8*)m2)  break;
        while(Num--)
        {
                if(*(Uint8*)m1 != *(Uint8*)m2)  break;
index ce87d9d..6bea250 100644 (file)
@@ -399,7 +399,7 @@ void Debug_Leave(char *FuncName, char RetType, ...)
        while(i--)      Debug_Putchar(' ');
 
        Debug_Puts(1, FuncName);
        while(i--)      Debug_Putchar(' ');
 
        Debug_Puts(1, FuncName);
-       Debug_FmtS("(%i)", tid);
+       Debug_FmtS("[%i]", tid);
        Debug_Puts(1, ": RETURN");
 
        // No Return
        Debug_Puts(1, ": RETURN");
 
        // No Return
index 9e92414..476557e 100644 (file)
@@ -76,7 +76,8 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
        
        ENTER("pInterface xAddress", Interface, Address);
        
        
        ENTER("pInterface xAddress", Interface, Address);
        
-       // Check routing tables
+       // Check routing tables if not on this subnet
+       if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
        {
                tRoute  *route = IPStack_FindRoute(4, Interface, &Address);
                if( route ) {
        {
                tRoute  *route = IPStack_FindRoute(4, Interface, &Address);
                if( route ) {
index 071c62b..e6ded4b 100644 (file)
@@ -115,6 +115,7 @@ static const tMacAddr cMAC_BROADCAST = {{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
 extern int     IPStack_AddFile(tSocketFile *File);
 extern int     IPStack_GetAddressSize(int AddressType);
 extern int     IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits);
 extern int     IPStack_AddFile(tSocketFile *File);
 extern int     IPStack_GetAddressSize(int AddressType);
 extern int     IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits);
+extern const char      *IPStack_PrintAddress(int AddressType, void *Address);
 
 extern tRoute  *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address);
 
 
 extern tRoute  *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address);
 
index bec82b3..ef1a2cd 100644 (file)
@@ -122,6 +122,8 @@ int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int
        if( CheckBits < 0 )     CheckBits = 0;
        if( CheckBits > size*8 )        CheckBits = size*8;
        
        if( CheckBits < 0 )     CheckBits = 0;
        if( CheckBits > size*8 )        CheckBits = size*8;
        
+       if( CheckBits == 0 )    return 1;       // /0 matches anythin
+       
        // Check first bits/8 bytes
        if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
        
        // Check first bits/8 bytes
        if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
        
@@ -135,3 +137,29 @@ int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int
        
        return 0;
 }
        
        return 0;
 }
+
+const char *IPStack_PrintAddress(int AddressType, void *Address)
+{
+       switch( AddressType )
+       {
+       case 4: {
+               static char     ret[4*3+3+1];
+               Uint8   *addr = Address;
+               sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
+               return ret;
+               }
+       
+       case 6: {
+               static char     ret[8*4+7+1];
+               Uint16  *addr = Address;
+               sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
+                       addr[0], addr[1], addr[2], addr[3],
+                       addr[4], addr[5], addr[6], addr[7]
+                       );
+               return ret;
+               }
+       
+       default:
+               return "";
+       }
+}
index cae3b98..c106034 100644 (file)
@@ -94,6 +94,7 @@ static const char *casIOCtls_RouteDir[] = {
 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
 {
         int    tmp;
 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
 {
         int    tmp;
+       ENTER("pNode iID pData", Node, ID, Data);
        switch(ID)
        {
        // --- Standard IOCtls (0-3) ---
        switch(ID)
        {
        // --- Standard IOCtls (0-3) ---
@@ -116,8 +117,10 @@ int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
                return tmp;
        
        case 4: // Add Route
                return tmp;
        
        case 4: // Add Route
-               if( !CheckString(Data) )        return -1;
-               return IPStack_Route_Create(Data);
+               if( !CheckString(Data) )        LEAVE_RET('i', -1);
+               tmp = IPStack_Route_Create(Data);
+               LEAVE('i', tmp);
+               return tmp;
        
        case 5: // Locate Route
                {
        
        case 5: // Locate Route
                {
@@ -128,19 +131,23 @@ int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
                        tRoute  *rt;
                        
                        if( !CheckMem(Data, sizeof(int)) )
                        tRoute  *rt;
                        
                        if( !CheckMem(Data, sizeof(int)) )
-                               return -1;
+                               LEAVE_RET('i', -1);
                        if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
                        if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
-                               return -1;
+                               LEAVE_RET('i', -1);
                        
                        
+                       Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s\n",
+                               data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
                        rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
                        
                        if( !rt )
                        rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
                        
                        if( !rt )
-                               return 0;
+                               LEAVE_RET('i', 0);
                        
                        
+                       LEAVE('i', rt->Node.Inode);
                        return rt->Node.Inode;
                }
                break;
        }
                        return rt->Node.Inode;
                }
                break;
        }
+       LEAVE('i', 0);
        return 0;
 }
 
        return 0;
 }
 
@@ -158,7 +165,10 @@ int IPStack_Route_Create(const char *InterfaceName)
        // Note: Oh man! This is such a hack
        {
                tVFS_Node       *node = IPStack_Root_FindDir(NULL, InterfaceName);
        // Note: Oh man! This is such a hack
        {
                tVFS_Node       *node = IPStack_Root_FindDir(NULL, InterfaceName);
-               if( !node )     return 0;
+               if( !node ) {
+                       Log_Debug("IPStack", "IPStack_Route_Create - Unknown interface '%s'\n", InterfaceName);
+                       return 0;
+               }
                iface = node->ImplPtr;
        }
        
                iface = node->ImplPtr;
        }
        
@@ -195,6 +205,8 @@ int IPStack_Route_Create(const char *InterfaceName)
                gIP_Routes = gIP_RoutesEnd = rt;
        }
        
                gIP_Routes = gIP_RoutesEnd = rt;
        }
        
+       Log_Log("IPStack", "Route entry for '%s' created\n", InterfaceName);
+       
        return rt->Node.Inode;
 }
 
        return rt->Node.Inode;
 }
 
@@ -205,7 +217,14 @@ tRoute     *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
        tRoute  *rt;
        tRoute  *best = NULL;
        
        tRoute  *rt;
        tRoute  *best = NULL;
        
-       if( Interface && AddressType != Interface->Type )       return NULL;
+       ENTER("iAddressType pInterface sAddress",
+               AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
+       
+       if( Interface && AddressType != Interface->Type ) {
+               LOG("Interface->Type (%i) != AddressType", Interface->Type);
+               LEAVE('n');
+               return NULL;
+       }
        
        for( rt = gIP_Routes; rt; rt = rt->Next )
        {
        
        for( rt = gIP_Routes; rt; rt = rt->Next )
        {
@@ -214,20 +233,29 @@ tRoute    *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
                // Check address type
                if( rt->AddressType != AddressType )    continue;
                
                // Check address type
                if( rt->AddressType != AddressType )    continue;
                
+               LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
+               
                // Check if the address matches
                if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
                        continue;
                
                if( best ) {
                        // More direct routes are preferred
                // Check if the address matches
                if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
                        continue;
                
                if( best ) {
                        // More direct routes are preferred
-                       if( best->SubnetBits > rt->SubnetBits ) continue;
+                       if( best->SubnetBits > rt->SubnetBits ) {
+                               LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
+                               continue;
+                       }
                        // If equally direct, choose the best metric
                        // If equally direct, choose the best metric
-                       if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric )   continue;
+                       if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
+                               LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
+                               continue;
+                       }
                }
                
                best = rt;
        }
        
                }
                
                best = rt;
        }
        
+       LEAVE('p', best);
        return best;
 }
 
        return best;
 }
 
index 7a7fd07..c3ae316 100644 (file)
@@ -223,7 +223,7 @@ void DumpRoute(const char *Name)
 {
         int    fd;
         int    type;
 {
         int    fd;
         int    type;
-       char    path[sizeof(IPSTACK_ROOT)+7+FILENAME_MAX+1] = IPSTACK_ROOT"/route/";
+       char    path[sizeof(IPSTACK_ROOT)+8+FILENAME_MAX+1] = IPSTACK_ROOT"/routes/";
        
        strcat(path, Name);
        
        
        strcat(path, Name);
        
@@ -346,7 +346,7 @@ void AddRoute(const char *Interface, void *Dest, int MaskBits, void *NextHop)
 int DoAutoConfig(const char *Device)
 {
         int    tmp, fd;
 int DoAutoConfig(const char *Device)
 {
         int    tmp, fd;
-       char    path[sizeof(IPSTACK_ROOT)+5+1]; // ip000
+       char    path[sizeof(IPSTACK_ROOT)+1+4+1];       // /0000
        uint8_t addr[4] = {10,0,2,55};
        uint8_t gw[4] = {10,0,2,1};
         int    subnet = 8;
        uint8_t addr[4] = {10,0,2,55};
        uint8_t gw[4] = {10,0,2,1};
         int    subnet = 8;
@@ -373,10 +373,11 @@ int DoAutoConfig(const char *Device)
        // Set Subnet
        ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
        
        // Set Subnet
        ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
        
-       // Set Gateway
+       // Set routes
        {
                uint8_t net[4] = {0,0,0,0};
        {
                uint8_t net[4] = {0,0,0,0};
-               AddRoute(path + sizeof(IPSTACK_ROOT) + 1, net, 0, gw);
+               AddRoute(path + sizeof(IPSTACK_ROOT), addr, 8, net);    // This interface
+               AddRoute(path + sizeof(IPSTACK_ROOT), net, 0, gw);      // Gateway
        }
        
        close(fd);
        }
        
        close(fd);
index 119e025..30c579c 100644 (file)
@@ -48,6 +48,8 @@ int main(int argc, const char *argv[], const char *envp[])
                return -1;\r
        }\r
        \r
                return -1;\r
        }\r
        \r
+       printf("Connection opened\n");\r
+       \r
        writef(srv.FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, gsRemoteAddress, gsRealName);\r
        writef(srv.FD, "NICK %s", gsNickname);\r
        \r
        writef(srv.FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, gsRemoteAddress, gsRealName);\r
        writef(srv.FD, "NICK %s", gsNickname);\r
        \r
@@ -211,6 +213,8 @@ int OpenTCP(const char *AddressString, short PortNumber)
                return -1;\r
        }\r
        \r
                return -1;\r
        }\r
        \r
+       printf("iface = '%s'\n", iface);\r
+       \r
        // Open client socket\r
        // TODO: Move this out to libnet?\r
        {\r
        // Open client socket\r
        // TODO: Move this out to libnet?\r
        {\r
@@ -223,16 +227,20 @@ int OpenTCP(const char *AddressString, short PortNumber)
        free(iface);\r
        \r
        if( fd == -1 ) {\r
        free(iface);\r
        \r
        if( fd == -1 ) {\r
+               fprintf(stderr, "Unable to open TCP Client for reading\n");\r
                return -1;\r
        }\r
                return -1;\r
        }\r
-\r
+       \r
        // Set remote port and address\r
        // Set remote port and address\r
+       printf("Setting port and remote address");\r
        ioctl(fd, 5, &PortNumber);\r
        ioctl(fd, 6, addrBuffer);\r
        \r
        // Connect\r
        ioctl(fd, 5, &PortNumber);\r
        ioctl(fd, 6, addrBuffer);\r
        \r
        // Connect\r
+       printf("Initiating connection");\r
        if( ioctl(fd, 7, NULL) == 0 ) {\r
                // Shouldn't happen :(\r
        if( ioctl(fd, 7, NULL) == 0 ) {\r
                // Shouldn't happen :(\r
+               fprintf(stderr, "Unable to start connection\n");\r
                return -1;\r
        }\r
        \r
                return -1;\r
        }\r
        \r

UCC git Repository :: git.ucc.asn.au