3 * - Stack Initialisation
6 #define VERSION VER2(0,10)
11 #include "include/adapters.h"
12 #include "interface.h"
16 extern tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
19 int IPStack_Install(char **Arguments);
20 int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits);
23 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
24 tDevFS_Driver gIP_DriverInfo = {
27 .Size = -1, // Number of interfaces
29 .ACLs = &gVFS_ACL_EveryoneRX,
30 .Flags = VFS_FFLAG_DIRECTORY,
31 .Type = &gIP_RootNodeType
37 * \fn int IPStack_Install(char **Arguments)
38 * \brief Intialise the relevant parts of the stack and register with DevFS
40 int IPStack_Install(char **Arguments)
42 // TODO: different Layer 2 protocols
43 // Layer 3 - Network Layer Protocols
47 // Layer 4 - Transport Layer Protocols
51 // Initialise loopback interface
52 gIP_LoopInterface.Adapter = Adapter_GetByName("lo");
54 DevFS_AddDevice( &gIP_DriverInfo );
60 * \brief Gets the size (in bytes) of a specified form of address
62 int IPStack_GetAddressSize(int AddressType)
66 case -1: // -1 = maximum
83 * \brief Compare two IP Addresses masked by CheckBits
85 int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits)
87 int size = IPStack_GetAddressSize(AddressType);
89 const Uint8 *addr1 = Address1, *addr2 = Address2;
92 if( CheckBits < 0 ) CheckBits = size*8;
93 if( CheckBits > size*8 ) CheckBits = size*8;
95 if( CheckBits == 0 ) return 1; // /0 matches anything
97 // Check first bits/8 bytes
98 if( memcmp(Address1, Address2, CheckBits/8) != 0 ) return 0;
100 // Check if the mask is a multiple of 8
101 if( CheckBits % 8 == 0 ) return 1;
104 mask = 0xFF << (8 - (CheckBits % 8));
105 if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
111 bool IPStack_AddressIsBroadcast(int AddrType, const void *Addr, int SubnetBits)
113 const size_t addrsize = IPStack_GetAddressSize(AddrType);
114 const Uint8 *addr = Addr;
116 ASSERTC( SubnetBits, >=, 0 );
117 ASSERTC( SubnetBits, <=, addrsize * 8 );
118 const size_t host_bits = addrsize*8 - SubnetBits;
120 for( int i = 0; i < host_bits/8; i ++ )
122 if( addr[addrsize-i-1] != 0xFF )
125 Uint8 mask = 0xFF >> (8-(host_bits%8));
126 if( (addr[addrsize-host_bits/8-1] & mask) != mask )
131 const char *IPStack_PrintAddress(int AddressType, const void *Address)
133 switch( AddressType )
136 static char ret[4*3+3+1];
137 const Uint8 *addr = Address;
138 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
142 case 6: { // TODO: address compression
143 static char ret[8*4+7+1];
144 const Uint16 *addr = Address;
145 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
146 ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
147 ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])