Multiple IPStack Related changes (and other bugfixes)
[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
25 // === PROTOTYPES ===
26  int    IPStack_Install(char **Arguments);
27  int    IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits);
28
29 // === GLOBALS ===
30 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
31 tDevFS_Driver   gIP_DriverInfo = {
32         NULL, "ip",
33         {
34         .Size = -1,     // Number of interfaces
35         .NumACLs = 1,
36         .ACLs = &gVFS_ACL_EveryoneRX,
37         .Flags = VFS_FFLAG_DIRECTORY,
38         .ReadDir = IPStack_Root_ReadDir,
39         .FindDir = IPStack_Root_FindDir,
40         .IOCtl = IPStack_Root_IOCtl
41         }
42 };
43
44 // === CODE ===
45 /**
46  * \fn int IPStack_Install(char **Arguments)
47  * \brief Intialise the relevant parts of the stack and register with DevFS
48  */
49 int IPStack_Install(char **Arguments)
50 {
51          int    i = 0;
52         
53         // Layer 3 - Network Layer Protocols
54         ARP_Initialise();
55         IPv4_Initialise();
56         IPv6_Initialise();
57         // Layer 4 - Transport Layer Protocols
58         TCP_Initialise();
59         UDP_Initialise();
60         
61         if(Arguments)
62         {
63                 // Parse module arguments
64                 for( i = 0; Arguments[i]; i++ )
65                 {
66                         // TODO:
67                         // Define interfaces by <Device>,<Type>,<HexStreamAddress>,<Bits>
68                         // Where:
69                         // - <Device> is the device path (E.g. /Devices/ne2k/0)
70                         // - <Type> is a number (e.g. 4) or symbol (e.g. AF_INET4)
71                         // - <HexStreamAddress> is a condensed hexadecimal stream (in big endian)
72                         //      (E.g. 0A000201 for 10.0.2.1 IPv4)
73                         // - <Bits> is the number of subnet bits (E.g. 24 for an IPv4 Class C)
74                         // Example: /Devices/ne2k/0,4,0A00020A,24
75                         
76                         // I could also define routes using <Interface>,<HexStreamNetwork>,<Bits>,<HexStreamGateway>
77                         // Example: 1,00000000,0,0A000201
78                 }
79         }
80         
81         // Initialise loopback interface
82         gIP_LoopInterface.Adapter = IPStack_GetAdapter("LOOPBACK");
83         
84         DevFS_AddDevice( &gIP_DriverInfo );
85         
86         return MODULE_ERR_OK;
87 }
88
89 /**
90  * \brief Gets the size (in bytes) of a specified form of address
91  */
92 int IPStack_GetAddressSize(int AddressType)
93 {
94         switch(AddressType)
95         {
96         case -1:        // -1 = maximum
97                 return sizeof(tIPv6);
98         
99         case AF_NULL:
100                 return 0;
101         
102         case AF_INET4:
103                 return sizeof(tIPv4);
104         case AF_INET6:
105                 return sizeof(tIPv6);
106                 
107         default:
108                 return 0;
109         }
110 }
111
112 /**
113  * \brief Compare two IP Addresses masked by CheckBits
114  */
115 int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits)
116 {
117          int    size = IPStack_GetAddressSize(AddressType);
118         Uint8   mask;
119         Uint8   *addr1 = Address1, *addr2 = Address2;
120         
121         // Sanity check size
122         if( CheckBits < 0 )     CheckBits = 0;
123         if( CheckBits > size*8 )        CheckBits = size*8;
124         
125         // Check first bits/8 bytes
126         if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
127         
128         // Check if the mask is a multiple of 8
129         if( CheckBits % 8 == 0 )        return 1;
130         
131         // Check last bits
132         mask = 0xFF << (8 - (CheckBits % 8));
133         if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
134                 return 1;
135         
136         return 0;
137 }

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