2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
13 static inline uint32_t htonl(uint32_t v)
15 return (((v >> 24) & 0xFF) << 0)
16 | (((v >> 16) & 0xFF) << 8)
17 | (((v >> 8) & 0xFF) << 16)
18 | (((v >> 0) & 0xFF) << 24);
20 static inline uint16_t htons(uint16_t v)
22 return (((v >> 8) & 0xFF) << 0)
23 | (((v >> 0) & 0xFF) << 8);
26 #define ntohl(v) htonl(v)
27 #define ntohs(v) htons(v)
30 #define __thread // Disable TLS
33 * \brief Read an IPv4 Address
34 * \param String IPv4 dotted decimal address
35 * \param Addr Output 32-bit representation of IP address
36 * \return Boolean success
38 static int Net_ParseIPv4Addr(const char *String, uint8_t *Addr)
44 for( j = 0; String[i] && j < 4; j ++ )
47 for( ; String[i] && String[i] != '.'; i++ )
49 if('0' > String[i] || String[i] > '9') {
51 printf("0<c<9 expected, '%c' found\n", String[i]);
55 val = val*10 + String[i] - '0';
59 printf("val > 255 (%i)\n", val);
70 printf("4 parts expected, %i found\n", j);
74 if(String[i] != '\0') {
76 printf("EOS != '\\0', '%c'\n", String[i]);
84 * \brief Read an IPv6 Address
85 * \param String IPv6 colon-hex representation
86 * \param Addr Output 128-bit representation of IP address
87 * \return Boolean success
89 static int Net_ParseIPv6Addr(const char *String, uint8_t *Addr)
93 int val, split = -1, end;
94 uint16_t hi[8], low[8];
96 for( j = 0; String[i] && j < 8; j ++ )
98 if(String[i] == ':') {
101 printf("Two '::'s\n");
111 for( k = 0; String[i] && String[i] != ':'; i++, k++ )
114 if('0' <= String[i] && String[i] <= '9')
115 val += String[i] - '0';
116 else if('A' <= String[i] && String[i] <= 'F')
117 val += String[i] - 'A' + 10;
118 else if('a' <= String[i] && String[i] <= 'f')
119 val += String[i] - 'a' + 10;
122 printf("%c unexpected\n", String[i]);
130 printf("val (0x%x) > 0xFFFF\n", val);
140 if( String[i] == ':' ) {
146 // Create final address
148 for( j = 0; j < split; j ++ )
150 Addr[j*2] = hi[j]>>8;
151 Addr[j*2+1] = hi[j]&0xFF;
154 for( ; j < 8 - (end - split); j++ )
161 for( ; j < 8; j ++, k++)
163 Addr[j*2] = hi[k]>>8;
164 Addr[j*2+1] = hi[k]&0xFF;
171 * \brief Parse an address from a string
172 * \param String String containing an IPv4/IPv6 address
173 * \param Addr Buffer for the address (must be >= 16 bytes)
174 * \return Address type
175 * \retval 0 Unknown address type
176 * \retval 4 IPv4 Address
177 * \retval 6 IPv6 Address
179 int Net_ParseAddress(const char *String, void *Addr)
181 if( Net_ParseIPv4Addr(String, Addr) )
184 if( Net_ParseIPv6Addr(String, Addr) )
190 static const char *Net_PrintIPv4Address(uint8_t *Address)
192 static __thread char ret[4*3+3+1]; // '255.255.255.255\0'
194 sprintf(ret, "%i.%i.%i.%i", Address[0], Address[1], Address[2], Address[3]);
199 static const char *Net_PrintIPv6Address(uint16_t *Address)
201 static __thread char ret[8*4+7+1]; // 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF\0'
203 // TODO: Zero compression
204 int zeroStart = 0, zeroEnd = 8;
205 for( i = 0; i < 8; i ++ ) {
210 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
211 ntohs(Address[0]), ntohs(Address[1]), ntohs(Address[2]), ntohs(Address[3]),
212 ntohs(Address[4]), ntohs(Address[5]), ntohs(Address[6]), ntohs(Address[7])
218 const char *Net_PrintAddress(int AddressType, void *Address)
220 switch( AddressType )
225 return Net_PrintIPv4Address(Address);
228 return Net_PrintIPv6Address(Address);