2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
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
18 static int Net_ParseIPv4Addr(const char *String, uint8_t *Addr)
24 for( j = 0; String[i] && j < 4; j ++ )
27 for( ; String[i] && String[i] != '.'; i++ )
29 if('0' > String[i] || String[i] > '9') {
31 printf("0<c<9 expected, '%c' found\n", String[i]);
35 val = val*10 + String[i] - '0';
39 printf("val > 255 (%i)\n", val);
50 printf("4 parts expected, %i found\n", j);
54 if(String[i] != '\0') {
56 printf("EOS != '\\0', '%c'\n", String[i]);
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
69 static int Net_ParseIPv6Addr(const char *String, uint8_t *Addr)
73 int val, split = -1, end;
74 uint16_t hi[8], low[8];
76 for( j = 0; String[i] && j < 8; j ++ )
78 if(String[i] == ':') {
81 printf("Two '::'s\n");
91 for( k = 0; String[i] && String[i] != ':'; i++, k++ )
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;
102 printf("%c unexpected\n", String[i]);
110 printf("val (0x%x) > 0xFFFF\n", val);
120 if( String[i] == ':' ) {
126 // Create final address
128 for( j = 0; j < split; j ++ )
130 Addr[j*2] = hi[j]>>8;
131 Addr[j*2+1] = hi[j]&0xFF;
134 for( ; j < 8 - (end - split); j++ )
141 for( ; j < 8; j ++, k++)
143 Addr[j*2] = hi[k]>>8;
144 Addr[j*2+1] = hi[k]&0xFF;
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
159 int Net_ParseAddress(const char *String, void *Addr)
161 if( Net_ParseIPv4Addr(String, Addr) )
164 if( Net_ParseIPv6Addr(String, Addr) )