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 const char *IPStack_PrintAddress(int AddressType, const void *Address)
113 switch( AddressType )
116 static char ret[4*3+3+1];
117 const Uint8 *addr = Address;
118 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
122 case 6: { // TODO: address compression
123 static char ret[8*4+7+1];
124 const Uint16 *addr = Address;
125 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
126 ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
127 ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])