3 * - Stack Initialisation
6 #define VERSION VER2(0,10)
13 extern int ARP_Initialise();
14 extern void UDP_Initialise();
15 extern void TCP_Initialise();
16 extern int IPv4_Initialise();
17 extern int IPv6_Initialise();
19 extern tAdapter *IPStack_GetAdapter(const char *Path);
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, void *Address1, void *Address2, int CheckBits);
32 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
33 tDevFS_Driver gIP_DriverInfo = {
36 .Size = -1, // Number of interfaces
38 .ACLs = &gVFS_ACL_EveryoneRX,
39 .Flags = VFS_FFLAG_DIRECTORY,
40 .ReadDir = IPStack_Root_ReadDir,
41 .FindDir = IPStack_Root_FindDir,
42 .IOCtl = IPStack_Root_IOCtl
48 * \fn int IPStack_Install(char **Arguments)
49 * \brief Intialise the relevant parts of the stack and register with DevFS
51 int IPStack_Install(char **Arguments)
55 // Layer 3 - Network Layer Protocols
59 // Layer 4 - Transport Layer Protocols
65 // Parse module arguments
66 for( i = 0; Arguments[i]; i++ )
69 // Define interfaces by <Device>:<Type>:<HexStreamAddress>:<Bits>
71 // - <Device> is the device path (E.g. /Devices/ne2k/0)
72 // - <Type> is a number (e.g. 4) or symbol (e.g. AF_INET4)
73 // - <HexStreamAddress> is a condensed hexadecimal stream (in big endian)
74 // (E.g. 0A000201 for 10.0.2.1 IPv4)
75 // - <Bits> is the number of subnet bits (E.g. 24 for an IPv4 Class C)
76 // Example: /Devices/ne2k/0:4:0A00020A:24
77 // would define an interface with the address 10.0.2.10/24
78 if( Arguments[i][0] == '/' ) {
80 char *dev, *type, *addr, *bits;
84 type = strchr(dev, ':');
86 Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
89 *type = '\0'; type ++;
91 addr = strchr(type, ':');
93 Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
96 *addr = '\0'; addr ++;
98 bits = strchr(addr, ':');
100 Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
103 *bits = '\0'; bits ++;
107 int iType = atoi(type);
108 int size = IPStack_GetAddressSize(iType);
109 Uint8 addrData[size];
110 int iBits = atoi(bits);
112 UnHex(addrData, size, addr);
114 tInterface *iface = IPStack_AddInterface(dev, "");
116 memcpy(iface->Address, addrData, size);
117 iface->SubnetBits = iBits;
119 // Route for addrData/iBits, no next hop, default metric
120 IPStack_AddRoute(iface->Name, iface->Address, iBits, NULL, 0);
124 // I could also define routes using <Interface>,<HexStreamNetwork>,<Bits>,<HexStreamGateway>
125 // Example: 1:00000000:0:0A000201
129 // Initialise loopback interface
130 gIP_LoopInterface.Adapter = IPStack_GetAdapter("LOOPBACK");
132 DevFS_AddDevice( &gIP_DriverInfo );
134 return MODULE_ERR_OK;
138 * \brief Gets the size (in bytes) of a specified form of address
140 int IPStack_GetAddressSize(int AddressType)
144 case -1: // -1 = maximum
145 return sizeof(tIPv6);
151 return sizeof(tIPv4);
153 return sizeof(tIPv6);
161 * \brief Compare two IP Addresses masked by CheckBits
163 int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits)
165 int size = IPStack_GetAddressSize(AddressType);
167 Uint8 *addr1 = Address1, *addr2 = Address2;
170 if( CheckBits < 0 ) CheckBits = 0;
171 if( CheckBits > size*8 ) CheckBits = size*8;
173 if( CheckBits == 0 ) return 1; // /0 matches anythin0
175 // Check first bits/8 bytes
176 if( memcmp(Address1, Address2, CheckBits/8) != 0 ) return 0;
178 // Check if the mask is a multiple of 8
179 if( CheckBits % 8 == 0 ) return 1;
182 mask = 0xFF << (8 - (CheckBits % 8));
183 if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
189 const char *IPStack_PrintAddress(int AddressType, void *Address)
191 switch( AddressType )
194 static char ret[4*3+3+1];
195 Uint8 *addr = Address;
196 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
201 static char ret[8*4+7+1];
202 Uint16 *addr = Address;
203 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
204 addr[0], addr[1], addr[2], addr[3],
205 addr[4], addr[5], addr[6], addr[7]