Fiddling with IPStack
[tpg/acess2.git] / Modules / IPStack / main.c
1 /*
2  * Acess2 IP Stack
3  * - Stack Initialisation
4  */
5 #define DEBUG   0
6 #define VERSION VER2(0,10)
7 #include "ipstack.h"
8 #include "link.h"
9 #include <modules.h>
10 #include <fs_devfs.h>
11
12 // === IMPORTS ===
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();
18
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
26 // === PROTOTYPES ===
27  int    IPStack_Install(char **Arguments);
28  int    IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits);
29
30 // === GLOBALS ===
31 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
32 tDevFS_Driver   gIP_DriverInfo = {
33         NULL, "ip",
34         {
35         .Size = -1,     // Number of interfaces
36         .NumACLs = 1,
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
42         }
43 };
44
45 // === CODE ===
46 /**
47  * \fn int IPStack_Install(char **Arguments)
48  * \brief Intialise the relevant parts of the stack and register with DevFS
49  */
50 int IPStack_Install(char **Arguments)
51 {
52          int    i = 0;
53         
54         // Layer 3 - Network Layer Protocols
55         ARP_Initialise();
56         IPv4_Initialise();
57         IPv6_Initialise();
58         // Layer 4 - Transport Layer Protocols
59         TCP_Initialise();
60         UDP_Initialise();
61         
62         if(Arguments)
63         {
64                 // Parse module arguments
65                 for( i = 0; Arguments[i]; i++ )
66                 {
67                         // TODO:
68                         // Define interfaces by <Device>:<Type>:<HexStreamAddress>:<Bits>
69                         // Where:
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] == '/' ) {
78                                 // Define Interface
79                                 char    *dev, *type, *addr, *bits;
80                                 
81                                 // Read definition
82                                 dev = Arguments[i];
83                                 type = strchr(dev, ':');
84                                 if( !type ) {
85                                         Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
86                                         continue;
87                                 }
88                                 *type = '\0';   type ++;
89                                 
90                                 addr = strchr(type, ':');
91                                 if( !addr ) {
92                                         Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
93                                         continue;
94                                 }
95                                 *addr = '\0';   addr ++;
96                                 
97                                 bits = strchr(addr, ':');
98                                 if( !bits ) {
99                                         Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
100                                         continue;
101                                 }
102                                 *bits = '\0';   bits ++;
103                                 
104                                 // Define interface
105                                 {
106                                          int    iType = atoi(type);
107                                          int    size = IPStack_GetAddressSize(iType);
108                                         Uint8   addrData[size];
109                                          int    iBits = atoi(bits);
110                                         
111                                         UnHex(addrData, size, addr);
112                                         
113                                         tInterface      *iface = IPStack_AddInterface(dev, "");
114                                         iface->Type = iType;
115                                         memcpy(iface->Address, addrData, size);
116                                         iface->SubnetBits = iBits;
117                                 }
118                         }
119                         
120                         // I could also define routes using <Interface>,<HexStreamNetwork>,<Bits>,<HexStreamGateway>
121                         // Example: 1:00000000:0:0A000201
122                 }
123         }
124         
125         // Initialise loopback interface
126         gIP_LoopInterface.Adapter = IPStack_GetAdapter("LOOPBACK");
127         
128         DevFS_AddDevice( &gIP_DriverInfo );
129         
130         return MODULE_ERR_OK;
131 }
132
133 /**
134  * \brief Gets the size (in bytes) of a specified form of address
135  */
136 int IPStack_GetAddressSize(int AddressType)
137 {
138         switch(AddressType)
139         {
140         case -1:        // -1 = maximum
141                 return sizeof(tIPv6);
142         
143         case AF_NULL:
144                 return 0;
145         
146         case AF_INET4:
147                 return sizeof(tIPv4);
148         case AF_INET6:
149                 return sizeof(tIPv6);
150                 
151         default:
152                 return 0;
153         }
154 }
155
156 /**
157  * \brief Compare two IP Addresses masked by CheckBits
158  */
159 int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits)
160 {
161          int    size = IPStack_GetAddressSize(AddressType);
162         Uint8   mask;
163         Uint8   *addr1 = Address1, *addr2 = Address2;
164         
165         // Sanity check size
166         if( CheckBits < 0 )     CheckBits = 0;
167         if( CheckBits > size*8 )        CheckBits = size*8;
168         
169         if( CheckBits == 0 )    return 1;       // /0 matches anythin0
170         
171         // Check first bits/8 bytes
172         if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
173         
174         // Check if the mask is a multiple of 8
175         if( CheckBits % 8 == 0 )        return 1;
176         
177         // Check last bits
178         mask = 0xFF << (8 - (CheckBits % 8));
179         if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
180                 return 1;
181         
182         return 0;
183 }
184
185 const char *IPStack_PrintAddress(int AddressType, void *Address)
186 {
187         switch( AddressType )
188         {
189         case 4: {
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]);
193                 return ret;
194                 }
195         
196         case 6: {
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]
202                         );
203                 return ret;
204                 }
205         
206         default:
207                 return "";
208         }
209 }

UCC git Repository :: git.ucc.asn.au