2b58444d04b41a8ad093c311da5a832f8989506e
[tpg/acess2.git] / Usermode / Libraries / libnet.so_src / address.c
1 /*
2  * Acess2 Networking Toolkit
3  * By John Hodge (thePowersGang)
4  * 
5  * address.c
6  * - Address Parsing
7  */
8 #include <net.h>
9 #include <stdint.h>
10 #define DEBUG   0
11
12 /**
13  * \brief Read an IPv4 Address
14  * \param String        IPv4 dotted decimal address
15  * \param Addr  Output 32-bit representation of IP address
16  * \return Boolean success
17  */
18 static int Net_ParseIPv4Addr(const char *String, uint8_t *Addr)
19 {
20          int    i = 0;
21          int    j;
22          int    val;
23         
24         for( j = 0; String[i] && j < 4; j ++ )
25         {
26                 val = 0;
27                 for( ; String[i] && String[i] != '.'; i++ )
28                 {
29                         if('0' > String[i] || String[i] > '9') {
30                                 #if DEBUG
31                                 printf("0<c<9 expected, '%c' found\n", String[i]);
32                                 #endif
33                                 return 0;
34                         }
35                         val = val*10 + String[i] - '0';
36                 }
37                 if(val > 255) {
38                         #if DEBUG
39                         printf("val > 255 (%i)\n", val);
40                         #endif
41                         return 0;
42                 }
43                 Addr[j] = val;
44                 
45                 if(String[i] == '.')
46                         i ++;
47         }
48         if( j != 4 ) {
49                 #if DEBUG
50                 printf("4 parts expected, %i found\n", j);
51                 #endif
52                 return 0;
53         }
54         if(String[i] != '\0') {
55                 #if DEBUG
56                 printf("EOS != '\\0', '%c'\n", String[i]);
57                 #endif
58                 return 0;
59         }
60         return 1;
61 }
62
63 /**
64  * \brief Read an IPv6 Address
65  * \param String        IPv6 colon-hex representation
66  * \param Addr  Output 128-bit representation of IP address
67  * \return Boolean success
68  */
69 static int Net_ParseIPv6Addr(const char *String, uint8_t *Addr)
70 {
71          int    i = 0;
72          int    j, k;
73          int    val, split = -1, end;
74         uint16_t        hi[8], low[8];
75         
76         for( j = 0; String[i] && j < 8; j ++ )
77         {
78                 if(String[i] == ':') {
79                         if(split != -1) {
80                                 #if DEBUG
81                                 printf("Two '::'s\n");
82                                 #endif
83                                 return 0;
84                         }
85                         split = j;
86                         i ++;
87                         continue;
88                 }
89                 
90                 val = 0;
91                 for( k = 0; String[i] && String[i] != ':'; i++, k++ )
92                 {
93                         val *= 16;
94                         if('0' <= String[i] && String[i] <= '9')
95                                 val += String[i] - '0';
96                         else if('A' <= String[i] && String[i] <= 'F')
97                                 val += String[i] - 'A' + 10;
98                         else if('a' <= String[i] && String[i] <= 'f')
99                                 val += String[i] - 'a' + 10;
100                         else {
101                                 #if DEBUG
102                                 printf("%c unexpected\n", String[i]);
103                                 #endif
104                                 return 0;
105                         }
106                 }
107                 
108                 if(val > 0xFFFF) {
109                         #if DEBUG
110                         printf("val (0x%x) > 0xFFFF\n", val);
111                         #endif
112                         return 0;
113                 }
114                 
115                 if(split == -1)
116                         hi[j] = val;
117                 else
118                         low[j-split] = val;
119                 
120                 if( String[i] == ':' ) {
121                         i ++;
122                 }
123         }
124         end = j;
125         
126         // Create final address
127         // - First section
128         for( j = 0; j < split; j ++ )
129         {
130                 Addr[j*2] = hi[j]>>8;
131                 Addr[j*2+1] = hi[j]&0xFF;
132         }
133         // - Zero region
134         for( ; j < 8 - (end - split); j++ )
135         {
136                 Addr[j*2] = 0;
137                 Addr[j*2+1] = 0;
138         }
139         // - Tail section
140         k = 0;
141         for( ; j < 8; j ++, k++)
142         {
143                 Addr[j*2] = hi[k]>>8;
144                 Addr[j*2+1] = hi[k]&0xFF;
145         }
146         
147         return 1;
148 }
149
150 /**
151  * \brief Parse an address from a string
152  * \param String        String containing an IPv4/IPv6 address
153  * \param Addr  Buffer for the address (must be >= 16 bytes)
154  * \return Address type
155  * \retval 0    Unknown address type
156  * \retval 4    IPv4 Address
157  * \retval 6    IPv6 Address
158  */
159 int Net_ParseAddress(const char *String, void *Addr)
160 {
161         if( Net_ParseIPv4Addr(String, Addr) )
162                 return 4;
163         
164         if( Net_ParseIPv6Addr(String, Addr) )
165                 return 6;
166         
167         return 0;
168 }

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