X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FModules%2FIPStack%2Fmain.c;h=82373ece03c5344ad5ecf97078dc6d090e09a0dd;hb=c1b33e91984102c1aa9a2ffe19f02c315b481726;hp=3aecfb30bb0aed9c6b6192e3b182f72af18f3557;hpb=33495f1efd207f0af4f804858f247db0983fcb8f;p=tpg%2Facess2.git diff --git a/KernelLand/Modules/IPStack/main.c b/KernelLand/Modules/IPStack/main.c index 3aecfb30..82373ece 100644 --- a/KernelLand/Modules/IPStack/main.c +++ b/KernelLand/Modules/IPStack/main.c @@ -9,19 +9,10 @@ #include #include #include "include/adapters.h" +#include "interface.h" +#include "init.h" // === IMPORTS === -extern int ARP_Initialise(); -extern void UDP_Initialise(); -extern void TCP_Initialise(); -extern int IPv4_Initialise(); -extern int IPv6_Initialise(); - -extern char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos); -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 === @@ -30,11 +21,6 @@ extern tRoute *IPStack_AddRoute(const char *Interface, void *Network, int Subnet // === 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", { @@ -53,8 +39,7 @@ tDevFS_Driver gIP_DriverInfo = { */ int IPStack_Install(char **Arguments) { - int i = 0; - + // TODO: different Layer 2 protocols // Layer 3 - Network Layer Protocols ARP_Initialise(); IPv4_Initialise(); @@ -63,134 +48,6 @@ int IPStack_Install(char **Arguments) TCP_Initialise(); UDP_Initialise(); - if(Arguments) - { - // Parse module arguments - for( i = 0; Arguments[i]; i++ ) - { - // TODO: - // Define interfaces by ::: - // Where: - // - is the device path (E.g. /Devices/ne2k/0) - // - is a number (e.g. 4) or symbol (e.g. AF_INET4) - // - is a condensed hexadecimal stream (in big endian) - // (E.g. 0A000201 for 10.0.2.1 IPv4) - // - is the number of subnet bits (E.g. 24 for an IPv4 Class C) - // Example: /Devices/ne2k/0:4:0A00020A:24 - // would define an interface with the address 10.0.2.10/24 - if( Arguments[i][0] == '/' ) { - // Define Interface - char *dev, *type, *addr, *bits; - - // Read definition - dev = Arguments[i]; - type = strchr(dev, ':'); - if( !type ) { - Log_Warning("IPStack", ":::"); - continue; - } - *type = '\0'; type ++; - - addr = strchr(type, ':'); - if( !addr ) { - Log_Warning("IPStack", ":::"); - continue; - } - *addr = '\0'; addr ++; - - bits = strchr(addr, ':'); - if( !bits ) { - Log_Warning("IPStack", ":::"); - continue; - } - *bits = '\0'; bits ++; - - // Define interface - { - int iType = atoi(type); - int size = IPStack_GetAddressSize(iType); - Uint8 addrData[size]; - int iBits = atoi(bits); - - 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 ::[:] - // 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", ":::"); - continue; - } - *network = '\0'; network ++; - - bits = strchr(network, ':'); - if( !bits ) { - Log_Warning("IPStack", ":::"); - 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; - } - } - } - // Initialise loopback interface gIP_LoopInterface.Adapter = Adapter_GetByName("lo"); @@ -251,6 +108,26 @@ int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Ad return 0; } +bool IPStack_AddressIsBroadcast(int AddrType, const void *Addr, int SubnetBits) +{ + const size_t addrsize = IPStack_GetAddressSize(AddrType); + const Uint8 *addr = Addr; + + ASSERTC( SubnetBits, >=, 0 ); + ASSERTC( SubnetBits, <=, addrsize * 8 ); + const size_t host_bits = addrsize*8 - SubnetBits; + + for( int i = 0; i < host_bits/8; i ++ ) + { + if( addr[addrsize-i-1] != 0xFF ) + return false; + } + Uint8 mask = 0xFF >> (8-(host_bits%8)); + if( (addr[addrsize-host_bits/8-1] & mask) != mask ) + return false; + return true; +} + const char *IPStack_PrintAddress(int AddressType, const void *Address) { switch( AddressType )