Networking - Reworked route table management
[tpg/acess2.git] / Modules / IPStack / main.c
index 6dac938..d5d1ebc 100644 (file)
@@ -22,13 +22,19 @@ extern tVFS_Node    *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
 extern int     IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
 extern tInterface      gIP_LoopInterface;
 extern tInterface      *IPStack_AddInterface(const char *Device, const char *Name);
+extern tRoute  *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
 
 // === PROTOTYPES ===
  int   IPStack_Install(char **Arguments);
- int   IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits);
+ int   IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits);
 
 // === GLOBALS ===
 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
+tVFS_NodeType  gIP_RootNodeType = {
+       .ReadDir = IPStack_Root_ReadDir,
+       .FindDir = IPStack_Root_FindDir,
+       .IOCtl = IPStack_Root_IOCtl
+};
 tDevFS_Driver  gIP_DriverInfo = {
        NULL, "ip",
        {
@@ -36,9 +42,7 @@ tDevFS_Driver gIP_DriverInfo = {
        .NumACLs = 1,
        .ACLs = &gVFS_ACL_EveryoneRX,
        .Flags = VFS_FFLAG_DIRECTORY,
-       .ReadDir = IPStack_Root_ReadDir,
-       .FindDir = IPStack_Root_FindDir,
-       .IOCtl = IPStack_Root_IOCtl
+       .Type = &gIP_RootNodeType
        }
 };
 
@@ -111,14 +115,79 @@ int IPStack_Install(char **Arguments)
                                        UnHex(addrData, size, addr);
                                        
                                        tInterface      *iface = IPStack_AddInterface(dev, "");
+                                       if( !iface ) {
+                                               Log_Warning("IPStack", "Unable to add interface on '%s'", dev);
+                                               continue ;
+                                       }
                                        iface->Type = iType;
                                        memcpy(iface->Address, addrData, size);
                                        iface->SubnetBits = iBits;
+                                       
+                                       // Route for addrData/iBits, no next hop, default metric
+                                       IPStack_AddRoute(iface->Name, iface->Address, iBits, NULL, 0);
+
+                                       Log_Notice("IPStack", "Boot interface %s/%i on %s",
+                                               IPStack_PrintAddress(iType, addrData), iBits,
+                                               dev);
                                }
+                               
+                               continue;
                        }
                        
-                       // I could also define routes using <Interface>,<HexStreamNetwork>,<Bits>,<HexStreamGateway>
+                       // I could also define routes using <Interface>:<HexStreamNetwork>:<Bits>[:<HexStreamGateway>]
                        // Example: 1:00000000:0:0A000201
+                       if( '0' <= Arguments[i][0] && Arguments[i][0] <= '9' )
+                       {
+                               // Define Interface
+                               char    *ifaceName, *network, *bits, *gateway;
+                               
+                               // Read definition
+                               ifaceName = Arguments[i];
+                               
+                               network = strchr(ifaceName, ':');
+                               if( !network ) {
+                                       Log_Warning("IPStack", "<iface>:<HexStreamNetwork>:<Bits>:<HexStreamGateway>");
+                                       continue;
+                               }
+                               *network = '\0';        network ++;
+                               
+                               bits = strchr(network, ':');
+                               if( !bits ) {
+                                       Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+                                       continue;
+                               }
+                               *bits = '\0';   bits ++;
+                               
+                               gateway = strchr(bits, ':');
+                               if( gateway ) {
+                                       *gateway = '\0';        gateway ++;
+                               }
+                               
+                               // Define route
+                               {
+                                       tVFS_Node       *node = IPStack_Root_FindDir(NULL, ifaceName);
+                                       if( !node ) {
+                                               Log_Warning("IPStack", "Unknown interface '%s' in arg %i", ifaceName, i);
+                                               continue ;
+                                       }
+                                       tInterface      *iface = node->ImplPtr;
+                                       
+                                        int    size = IPStack_GetAddressSize(iface->Type);
+                                       Uint8   netData[size];
+                                       Uint8   gwData[size];
+                                        int    iBits = atoi(bits);
+                                       
+                                       UnHex(netData, size, network);
+                                       if( gateway )
+                                               UnHex(gwData, size, gateway);
+                                       else
+                                               memset(gwData, 0, size);
+                                       
+                                       IPStack_AddRoute(ifaceName, netData, iBits, gwData, 30);
+                               }
+                               
+                               continue;
+                       }
                }
        }
        
@@ -156,17 +225,17 @@ int IPStack_GetAddressSize(int AddressType)
 /**
  * \brief Compare two IP Addresses masked by CheckBits
  */
-int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits)
+int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits)
 {
         int    size = IPStack_GetAddressSize(AddressType);
        Uint8   mask;
-       Uint8   *addr1 = Address1, *addr2 = Address2;
+       const Uint8     *addr1 = Address1, *addr2 = Address2;
        
        // Sanity check size
-       if( CheckBits < 0 )     CheckBits = 0;
+       if( CheckBits < 0 )     CheckBits = size*8;
        if( CheckBits > size*8 )        CheckBits = size*8;
        
-       if( CheckBits == 0 )    return 1;       // /0 matches anythin0
+       if( CheckBits == 0 )    return 1;       // /0 matches anything
        
        // Check first bits/8 bytes
        if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
@@ -182,23 +251,23 @@ int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int
        return 0;
 }
 
-const char *IPStack_PrintAddress(int AddressType, void *Address)
+const char *IPStack_PrintAddress(int AddressType, const void *Address)
 {
        switch( AddressType )
        {
        case 4: {
                static char     ret[4*3+3+1];
-               Uint8   *addr = Address;
+               const Uint8     *addr = Address;
                sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
                return ret;
                }
        
-       case 6: {
+       case 6: {       // TODO: address compression
                static char     ret[8*4+7+1];
-               Uint16  *addr = Address;
+               const 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]
+                       ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
+                       ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])
                        );
                return ret;
                }

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