Kernel - VFS API Update - ReadDir caller provided buffer
[tpg/acess2.git] / KernelLand / 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 #include "include/adapters.h"
12 #include "interface.h"
13 #include "init.h"
14
15 // === IMPORTS ===
16 extern tRoute   *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
17
18 // === PROTOTYPES ===
19  int    IPStack_Install(char **Arguments);
20  int    IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits);
21
22 // === GLOBALS ===
23 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
24 tDevFS_Driver   gIP_DriverInfo = {
25         NULL, "ip",
26         {
27         .Size = -1,     // Number of interfaces
28         .NumACLs = 1,
29         .ACLs = &gVFS_ACL_EveryoneRX,
30         .Flags = VFS_FFLAG_DIRECTORY,
31         .Type = &gIP_RootNodeType
32         }
33 };
34
35 // === CODE ===
36 /**
37  * \fn int IPStack_Install(char **Arguments)
38  * \brief Intialise the relevant parts of the stack and register with DevFS
39  */
40 int IPStack_Install(char **Arguments)
41 {
42         // TODO: different Layer 2 protocols
43         // Layer 3 - Network Layer Protocols
44         ARP_Initialise();
45         IPv4_Initialise();
46         IPv6_Initialise();
47         // Layer 4 - Transport Layer Protocols
48         TCP_Initialise();
49         UDP_Initialise();
50         
51         // Initialise loopback interface
52         gIP_LoopInterface.Adapter = Adapter_GetByName("lo");
53         
54         DevFS_AddDevice( &gIP_DriverInfo );
55         
56         return MODULE_ERR_OK;
57 }
58
59 /**
60  * \brief Gets the size (in bytes) of a specified form of address
61  */
62 int IPStack_GetAddressSize(int AddressType)
63 {
64         switch(AddressType)
65         {
66         case -1:        // -1 = maximum
67                 return sizeof(tIPv6);
68         
69         case AF_NULL:
70                 return 0;
71         
72         case AF_INET4:
73                 return sizeof(tIPv4);
74         case AF_INET6:
75                 return sizeof(tIPv6);
76                 
77         default:
78                 return 0;
79         }
80 }
81
82 /**
83  * \brief Compare two IP Addresses masked by CheckBits
84  */
85 int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits)
86 {
87          int    size = IPStack_GetAddressSize(AddressType);
88         Uint8   mask;
89         const Uint8     *addr1 = Address1, *addr2 = Address2;
90         
91         // Sanity check size
92         if( CheckBits < 0 )     CheckBits = size*8;
93         if( CheckBits > size*8 )        CheckBits = size*8;
94         
95         if( CheckBits == 0 )    return 1;       // /0 matches anything
96         
97         // Check first bits/8 bytes
98         if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
99         
100         // Check if the mask is a multiple of 8
101         if( CheckBits % 8 == 0 )        return 1;
102         
103         // Check last bits
104         mask = 0xFF << (8 - (CheckBits % 8));
105         if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
106                 return 1;
107         
108         return 0;
109 }
110
111 const char *IPStack_PrintAddress(int AddressType, const void *Address)
112 {
113         switch( AddressType )
114         {
115         case 4: {
116                 static char     ret[4*3+3+1];
117                 const Uint8     *addr = Address;
118                 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
119                 return ret;
120                 }
121         
122         case 6: {       // TODO: address compression
123                 static char     ret[8*4+7+1];
124                 const Uint16    *addr = Address;
125                 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
126                         ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
127                         ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])
128                         );
129                 return ret;
130                 }
131         
132         default:
133                 return "";
134         }
135 }

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