3 * - Stack Initialisation
6 #define VERSION VER2(0,10)
11 #include "include/adapters.h"
14 extern int ARP_Initialise();
15 extern void UDP_Initialise();
16 extern void TCP_Initialise();
17 extern int IPv4_Initialise();
18 extern int IPv6_Initialise();
20 extern char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos);
21 extern tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
22 extern int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
23 extern tInterface gIP_LoopInterface;
24 extern tInterface *IPStack_AddInterface(const char *Device, const char *Name);
25 extern tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
28 int IPStack_Install(char **Arguments);
29 int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits);
32 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
33 tVFS_NodeType gIP_RootNodeType = {
34 .ReadDir = IPStack_Root_ReadDir,
35 .FindDir = IPStack_Root_FindDir,
36 .IOCtl = IPStack_Root_IOCtl
38 tDevFS_Driver gIP_DriverInfo = {
41 .Size = -1, // Number of interfaces
43 .ACLs = &gVFS_ACL_EveryoneRX,
44 .Flags = VFS_FFLAG_DIRECTORY,
45 .Type = &gIP_RootNodeType
51 * \fn int IPStack_Install(char **Arguments)
52 * \brief Intialise the relevant parts of the stack and register with DevFS
54 int IPStack_Install(char **Arguments)
56 // TODO: different Layer 2 protocols
57 // Layer 3 - Network Layer Protocols
61 // Layer 4 - Transport Layer Protocols
65 // Initialise loopback interface
66 gIP_LoopInterface.Adapter = Adapter_GetByName("lo");
68 DevFS_AddDevice( &gIP_DriverInfo );
74 * \brief Gets the size (in bytes) of a specified form of address
76 int IPStack_GetAddressSize(int AddressType)
80 case -1: // -1 = maximum
97 * \brief Compare two IP Addresses masked by CheckBits
99 int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits)
101 int size = IPStack_GetAddressSize(AddressType);
103 const Uint8 *addr1 = Address1, *addr2 = Address2;
106 if( CheckBits < 0 ) CheckBits = size*8;
107 if( CheckBits > size*8 ) CheckBits = size*8;
109 if( CheckBits == 0 ) return 1; // /0 matches anything
111 // Check first bits/8 bytes
112 if( memcmp(Address1, Address2, CheckBits/8) != 0 ) return 0;
114 // Check if the mask is a multiple of 8
115 if( CheckBits % 8 == 0 ) return 1;
118 mask = 0xFF << (8 - (CheckBits % 8));
119 if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
125 const char *IPStack_PrintAddress(int AddressType, const void *Address)
127 switch( AddressType )
130 static char ret[4*3+3+1];
131 const Uint8 *addr = Address;
132 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
136 case 6: { // TODO: address compression
137 static char ret[8*4+7+1];
138 const Uint16 *addr = Address;
139 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
140 ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
141 ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])