Usermode/libc - Fix strchr and strrchr behavior
[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 bool IPStack_AddressIsBroadcast(int AddrType, const void *Addr, int SubnetBits)
112 {
113         const size_t    addrsize = IPStack_GetAddressSize(AddrType);
114         const Uint8     *addr = Addr;
115         
116         ASSERTC( SubnetBits, >=, 0 );
117         ASSERTC( SubnetBits, <=, addrsize * 8 );
118         const size_t    host_bits = addrsize*8 - SubnetBits;
119         
120         for( int i = 0; i < host_bits/8; i ++ )
121         {
122                 if( addr[addrsize-i-1] != 0xFF )
123                         return false;
124         }
125         Uint8   mask = 0xFF >> (8-(host_bits%8));
126         if( (addr[addrsize-host_bits/8-1] & mask) != mask )
127                 return false;
128         return true;
129 }
130
131 const char *IPStack_PrintAddress(int AddressType, const void *Address)
132 {
133         switch( AddressType )
134         {
135         case 4: {
136                 static char     ret[4*3+3+1];
137                 const Uint8     *addr = Address;
138                 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
139                 return ret;
140                 }
141         
142         case 6: {       // TODO: address compression
143                 static char     ret[8*4+7+1];
144                 const Uint16    *addr = Address;
145                 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
146                         ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
147                         ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])
148                         );
149                 return ret;
150                 }
151         
152         default:
153                 return "";
154         }
155 }

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