Added explicit functions uiplib_ip6addrconv and uiplib_ip4addrconv for converting IPv6 and IPv4 addresses, regardless of whether uIP was configured to run as an IPv4 or an IPv6 stack.

This commit is contained in:
Adam Dunkels 2012-11-26 19:43:17 +01:00
parent 0bad2b1748
commit 289a01b389
2 changed files with 37 additions and 18 deletions

View File

@ -42,9 +42,8 @@
/*-----------------------------------------------------------------------------------*/
int
uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *ipaddr)
{
#if UIP_CONF_IPV6
uint16_t value;
int tmp, zero;
unsigned int len;
@ -54,7 +53,7 @@ uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
zero = -1;
if(*addrstr == '[') addrstr++;
for(len = 0; len < sizeof(uip_ipaddr_t) - 1; addrstr++) {
for(len = 0; len < sizeof(uip_ip6addr_t) - 1; addrstr++) {
c = *addrstr;
if(c == ':' || c == '\0' || c == ']' || c == '/') {
ipaddr->u8[len] = (value >> 8) & 0xff;
@ -91,21 +90,28 @@ uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
PRINTF("uiplib: too large address\n");
return 0;
}
if(len < sizeof(uip_ipaddr_t)) {
if(len < sizeof(uip_ip6addr_t)) {
if(zero < 0) {
PRINTF("uiplib: too short address\n");
return 0;
}
memmove(&ipaddr->u8[zero + sizeof(uip_ipaddr_t) - len],
memmove(&ipaddr->u8[zero + sizeof(uip_ip6addr_t) - len],
&ipaddr->u8[zero], len - zero);
memset(&ipaddr->u8[zero], 0, sizeof(uip_ipaddr_t) - len);
memset(&ipaddr->u8[zero], 0, sizeof(uip_ip6addr_t) - len);
}
#else /* UIP_CONF_IPV6 */
return 1;
}
/*-----------------------------------------------------------------------------------*/
/* Parse a IPv4-address from a string. Returns the number of characters read
* for the address. */
int
uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *ipaddr)
{
unsigned char tmp;
char c;
unsigned char i, j;
uint8_t charsread = 0;
tmp = 0;
@ -117,7 +123,7 @@ uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
if(j > 4) {
return 0;
}
if(c == '.' || c == 0) {
if(c == '.' || c == 0 || c == ' ') {
ipaddr->u8[i] = tmp;
tmp = 0;
} else if(c >= '0' && c <= '9') {
@ -126,10 +132,21 @@ uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
return 0;
}
++addrstr;
} while(c != '.' && c != 0);
++charsread;
} while(c != '.' && c != 0 && c != ' ');
}
return charsread-1;
}
/*-----------------------------------------------------------------------------------*/
int
uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
{
#if UIP_CONF_IPV6
return uiplib_ip6addrconv(addrstr, ipaddr);
#else /* UIP_CONF_IPV6 */
return uiplib_ip4addrconv(addrstr, ipaddr);
#endif /* UIP_CONF_IPV6 */
return 1;
}
/*-----------------------------------------------------------------------------------*/

View File

@ -60,7 +60,7 @@
* \param addrstr A pointer to a string containing the IP address in
* textual form.
*
* \param addr A pointer to a uip_ipaddr_t that will be filled in with
* \param addr A pointer to a uip_ip4addr_t that will be filled in with
* the numerical representation of the address.
*
* \retval 0 If the IP address could not be parsed.
@ -68,6 +68,8 @@
*/
CCIF int uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *addr);
CCIF int uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *addr);
CCIF int uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *addr);
/** @} */
#endif /* __UIPLIB_H__ */