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);
27 int IPStack_Install(char **Arguments);
28 int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits);
31 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
32 tDevFS_Driver gIP_DriverInfo = {
35 .Size = -1, // Number of interfaces
37 .ACLs = &gVFS_ACL_EveryoneRX,
38 .Flags = VFS_FFLAG_DIRECTORY,
39 .ReadDir = IPStack_Root_ReadDir,
40 .FindDir = IPStack_Root_FindDir,
41 .IOCtl = IPStack_Root_IOCtl
47 * \fn int IPStack_Install(char **Arguments)
48 * \brief Intialise the relevant parts of the stack and register with DevFS
50 int IPStack_Install(char **Arguments)
54 // Layer 3 - Network Layer Protocols
58 // Layer 4 - Transport Layer Protocols
64 // Parse module arguments
65 for( i = 0; Arguments[i]; i++ )
68 // Define interfaces by <Device>:<Type>:<HexStreamAddress>:<Bits>
70 // - <Device> is the device path (E.g. /Devices/ne2k/0)
71 // - <Type> is a number (e.g. 4) or symbol (e.g. AF_INET4)
72 // - <HexStreamAddress> is a condensed hexadecimal stream (in big endian)
73 // (E.g. 0A000201 for 10.0.2.1 IPv4)
74 // - <Bits> is the number of subnet bits (E.g. 24 for an IPv4 Class C)
75 // Example: /Devices/ne2k/0:4:0A00020A:24
76 // would define an interface with the address 10.0.2.10/24
77 if( Arguments[i][0] == '/' ) {
79 char *dev, *type, *addr, *bits;
83 type = strchr(dev, ':');
85 Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
88 *type = '\0'; type ++;
90 addr = strchr(type, ':');
92 Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
95 *addr = '\0'; addr ++;
97 bits = strchr(addr, ':');
99 Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
102 *bits = '\0'; bits ++;
106 int iType = atoi(type);
107 int size = IPStack_GetAddressSize(iType);
108 Uint8 addrData[size];
109 int iBits = atoi(bits);
111 UnHex(addrData, size, addr);
113 tInterface *iface = IPStack_AddInterface(dev, "");
115 memcpy(iface->Address, addrData, size);
116 iface->SubnetBits = iBits;
120 // I could also define routes using <Interface>,<HexStreamNetwork>,<Bits>,<HexStreamGateway>
121 // Example: 1:00000000:0:0A000201
125 // Initialise loopback interface
126 gIP_LoopInterface.Adapter = IPStack_GetAdapter("LOOPBACK");
128 DevFS_AddDevice( &gIP_DriverInfo );
130 return MODULE_ERR_OK;
134 * \brief Gets the size (in bytes) of a specified form of address
136 int IPStack_GetAddressSize(int AddressType)
140 case -1: // -1 = maximum
141 return sizeof(tIPv6);
147 return sizeof(tIPv4);
149 return sizeof(tIPv6);
157 * \brief Compare two IP Addresses masked by CheckBits
159 int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits)
161 int size = IPStack_GetAddressSize(AddressType);
163 Uint8 *addr1 = Address1, *addr2 = Address2;
166 if( CheckBits < 0 ) CheckBits = 0;
167 if( CheckBits > size*8 ) CheckBits = size*8;
169 if( CheckBits == 0 ) return 1; // /0 matches anythin0
171 // Check first bits/8 bytes
172 if( memcmp(Address1, Address2, CheckBits/8) != 0 ) return 0;
174 // Check if the mask is a multiple of 8
175 if( CheckBits % 8 == 0 ) return 1;
178 mask = 0xFF << (8 - (CheckBits % 8));
179 if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
185 const char *IPStack_PrintAddress(int AddressType, void *Address)
187 switch( AddressType )
190 static char ret[4*3+3+1];
191 Uint8 *addr = Address;
192 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
197 static char ret[8*4+7+1];
198 Uint16 *addr = Address;
199 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
200 addr[0], addr[1], addr[2], addr[3],
201 addr[4], addr[5], addr[6], addr[7]