585f8c3e207f9726ab07529f3fb14cf8e471633b
[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                 }
75         }
76         
77         gIP_LoopInterface.Adapter = IPStack_GetAdapter("/Devices/fifo/anon");
78         
79         DevFS_AddDevice( &gIP_DriverInfo );
80         
81         return MODULE_ERR_OK;
82 }
83
84 /**
85  * \brief Gets the size (in bytes) of a specified form of address
86  */
87 int IPStack_GetAddressSize(int AddressType)
88 {
89         switch(AddressType)
90         {
91         case -1:        // -1 = maximum
92                 return sizeof(tIPv6);
93         
94         case AF_NULL:
95                 return 0;
96         
97         case AF_INET4:
98                 return sizeof(tIPv4);
99         case AF_INET6:
100                 return sizeof(tIPv6);
101                 
102         default:
103                 return 0;
104         }
105 }
106
107 /**
108  * \brief Compare two IP Addresses masked by CheckBits
109  */
110 int IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits)
111 {
112          int    size = IPStack_GetAddressSize(AddressType);
113         Uint8   mask;
114         Uint8   *addr1 = Address1, *addr2 = Address2;
115         
116         // Sanity check size
117         if( CheckBits < 0 )     CheckBits = 0;
118         if( CheckBits > size*8 )        CheckBits = size*8;
119         
120         // Check first bits/8 bytes
121         if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
122         
123         // Check if the mask is a multiple of 8
124         if( CheckBits % 8 == 0 )        return 1;
125         
126         // Check last bits
127         mask = 0xFF << (8 - (CheckBits % 8));
128         if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
129                 return 1;
130         
131         return 0;
132 }

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