Kernel - Implementing SysSpawn
[tpg/acess2.git] / Modules / IPStack / routing.c
index fd234da..0145f21 100644 (file)
@@ -5,7 +5,7 @@
 #define DEBUG  0
 #define VERSION        VER2(0,10)
 #include <acess.h>
-#include <tpl_drv_common.h>
+#include <api_drv_common.h>
 #include "ipstack.h"
 #include "link.h"
 
@@ -31,14 +31,20 @@ tRoute      *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
  int   giIP_NextRouteId = 1;
 tRoute *gIP_Routes;
 tRoute *gIP_RoutesEnd;
+tVFS_NodeType  gIP_RouteNodeType = {
+       .IOCtl = IPStack_Route_IOCtl
+};
+tVFS_NodeType  gIP_RouteDirNodeType = {
+       .ReadDir = IPStack_RouteDir_ReadDir,
+       .FindDir = IPStack_RouteDir_FindDir,
+       .IOCtl = IPStack_RouteDir_IOCtl
+};
 tVFS_Node      gIP_RouteNode = {
-       Flags: VFS_FFLAG_DIRECTORY,
-       Size: -1,
-       NumACLs: 1,
-       ACLs: &gVFS_ACL_EveryoneRX,
-       ReadDir: IPStack_RouteDir_ReadDir,
-       FindDir: IPStack_RouteDir_FindDir,
-       IOCtl: IPStack_RouteDir_IOCtl
+       .Flags = VFS_FFLAG_DIRECTORY,
+       .Size = -1,
+       .NumACLs = 1,
+       .ACLs = &gVFS_ACL_EveryoneRX,
+       .Type = &gIP_RouteDirNodeType
 };
 
 // === CODE ===
@@ -56,9 +62,9 @@ char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
        }
        
        {
-                int    len = sprintf(NULL, "%i", rt->Node.Inode);
+                int    len = sprintf(NULL, "%i", (int)rt->Node.Inode);
                char    buf[len+1];
-               sprintf(buf, "%i", rt->Node.Inode);
+               sprintf(buf, "%i", (int)rt->Node.Inode);
                return strdup(buf);
        }
 }
@@ -74,10 +80,39 @@ tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
        // Zero is invalid, sorry :)
        if( !num )      return NULL;
        
+       // Interpret the name as <type>:<addr>, returning the interface for
+       // needed to access that address.
+       //   E.g. '4:0A02000A'  - 10.2.0.10
+       // Hm... It could do with a way to have a better address type representation
+       if( Name[1] == ':' )    // TODO: Allow variable length type codes
+       {
+                int    addrSize = IPStack_GetAddressSize(num);
+               Uint8   addrData[addrSize];
+               
+               // Errof if the size is invalid
+               if( strlen(Name) != 2 + addrSize*2 )
+                       return NULL;
+               
+               // Parse the address
+               // - Error if the address data is not fully hex
+               if( UnHex(addrData, addrSize, Name + 2) != addrSize )
+                       return NULL;
+               
+               // Find the route
+               rt = IPStack_FindRoute(num, NULL, addrData);
+               if(!rt) return NULL;
+               
+               // Return the interface node
+               // - Sure it's hijacking it from inteface.c's area, but it's
+               //   simpler this way
+               return &rt->Interface->Node;
+       }
+       
        // The list is inherently sorted, so we can do a quick search
        for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
-       // Fast fail on larger number
-       if( rt->Node.Inode > num )
+       
+       // Fast fail end of list / larger number
+       if( !rt || rt->Node.Inode > num )
                return NULL;
        
        return &rt->Node;
@@ -163,7 +198,7 @@ tRoute *IPStack_Route_Create(const char *InterfaceName)
                        return NULL;
                }
                iface = node->ImplPtr;
-               if(node->Close) node->Close(node);
+               if(node->Type->Close)   node->Type->Close(node);
        }
        
        // Get the size of the specified address type
@@ -181,7 +216,7 @@ tRoute *IPStack_Route_Create(const char *InterfaceName)
        rt->Node.Size = 0;
        rt->Node.NumACLs = 1,
        rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
-       rt->Node.IOCtl = IPStack_Route_IOCtl;
+       rt->Node.Type = &gIP_RouteNodeType;
        
        // Set up state
        rt->AddressType = iface->Type;
@@ -319,10 +354,10 @@ tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
        {
                rt = &Interface->Route;
                // Make sure route is up to date
-               memcpy(rt->Network, iface->Address, addrSize);
+               memcpy(rt->Network, Interface->Address, addrSize);
                memset(rt->NextHop, 0, addrSize);
                rt->Metric = DEFAUTL_METRIC;
-               rt->SubnetBits = iface->SubnetBits;
+               rt->SubnetBits = Interface->SubnetBits;
                
                if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
                {

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