diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2015-01-15 12:19:44 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2015-01-15 12:19:44 -0600 |
commit | c16ad40ac4e49dce5846763d2c31f571c978e355 (patch) | |
tree | 9c937d99fd955942764af7212621fa627afe425e /nuttx/libc | |
parent | 413912e8554701cd97f84cd8d98f3e98999cb520 (diff) | |
download | px4-nuttx-c16ad40ac4e49dce5846763d2c31f571c978e355.tar.gz px4-nuttx-c16ad40ac4e49dce5846763d2c31f571c978e355.tar.bz2 px4-nuttx-c16ad40ac4e49dce5846763d2c31f571c978e355.zip |
Networking: Misck IPv6 detanglement
Diffstat (limited to 'nuttx/libc')
-rw-r--r-- | nuttx/libc/libc.csv | 4 | ||||
-rw-r--r-- | nuttx/libc/net/lib_inetntoa.c | 7 | ||||
-rw-r--r-- | nuttx/libc/net/lib_inetntop.c | 170 | ||||
-rw-r--r-- | nuttx/libc/net/lib_inetpton.c | 176 |
4 files changed, 248 insertions, 109 deletions
diff --git a/nuttx/libc/libc.csv b/nuttx/libc/libc.csv index 1a6e38cac..605989c2a 100644 --- a/nuttx/libc/libc.csv +++ b/nuttx/libc/libc.csv @@ -1,4 +1,4 @@ -"_inet_ntoa","arpa/inet.h","!defined(CONFIG_NET_IPv6) && !defined(CONFIG_CAN_PASS_STRUCTS)","FAR char","in_addr_t" +"_inet_ntoa","arpa/inet.h","defined(CONFIG_NET_IPv4) && !defined(CONFIG_CAN_PASS_STRUCTS)","FAR char","in_addr_t" "abort","stdlib.h","","void" "abs","stdlib.h","","int","int" "aio_error","aio.h","defined(CONFIG_FS_AIO)","int","FAR struct aiocb *" @@ -56,7 +56,7 @@ "htons","arpa/inet.h","","uint16_t","uint16_t" "imaxabs","inttypes.h","","intmax_t","intmax_t" "inet_addr","arpa/inet.h","","in_addr_t","FAR const char " -"inet_ntoa","arpa/inet.h","!defined(CONFIG_NET_IPv6) && defined(CONFIG_CAN_PASS_STRUCTS)","FAR char","struct in_addr" +"inet_ntoa","arpa/inet.h","defined(CONFIG_NET_IPv4) && defined(CONFIG_CAN_PASS_STRUCTS)","FAR char","struct in_addr" "inet_ntop","arpa/inet.h","","FAR const char","int","FAR const void *","FAR char *","socklen_t" "inet_pton","arpa/inet.h","","int","int","FAR const char *","FAR void *" "labs","stdlib.h","","long int","long int" diff --git a/nuttx/libc/net/lib_inetntoa.c b/nuttx/libc/net/lib_inetntoa.c index f7152d154..f04c3a48f 100644 --- a/nuttx/libc/net/lib_inetntoa.c +++ b/nuttx/libc/net/lib_inetntoa.c @@ -1,7 +1,7 @@ /**************************************************************************** * libc/net/lib_inetntoa.c * - * Copyright (C) 2007-2008, 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2008, 2011-2012, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -44,7 +44,7 @@ #include <arpa/inet.h> #include <netinet/in.h> -#ifndef CONFIG_NET_IPv6 +#ifdef CONFIG_NET_IPv4 /**************************************************************************** * Global Functions @@ -78,5 +78,4 @@ FAR char *_inet_ntoa(in_addr_t in) return buffer; } #endif -#endif /* !CONFIG_NET_IPv6 */ - +#endif /* CONFIG_NET_IPv4 */ diff --git a/nuttx/libc/net/lib_inetntop.c b/nuttx/libc/net/lib_inetntop.c index ffa7ba26d..1f3d33c6f 100644 --- a/nuttx/libc/net/lib_inetntop.c +++ b/nuttx/libc/net/lib_inetntop.c @@ -1,7 +1,7 @@ /**************************************************************************** * libc/net/lib_inetntop.c * - * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2012, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Includes some logic extracted from hwport_ftpd, written by Jaehyuk Cho @@ -54,22 +54,20 @@ #include <arpa/inet.h> /**************************************************************************** - * Public Functions + * Private Functions ****************************************************************************/ /**************************************************************************** - * Name: inet_ntop + * Name: inet_ipv4_ntop * * Description: - * The inet_ntop() function converts a numeric address into a text string - * suitable for presentation. + * The inet_ipv4_ntop() function converts a numeric IPv4 address into a + * text string suitable for presentation. * * Input Parameters: - * af - The af argument specifies the family of the address. This can be - * AF_INET or AF_INET6. * src - The src argument points to a buffer holding an address of the * specified type. The address must be in network byte order. - * dst - The dst argument points to a buffer where the function stores + * dest - The dest argument points to a buffer where the function stores * the resulting text string; it shall not be NULL. * size - The size argument specifies the size of this buffer, which must * be large enough to hold the text string (INET_ADDRSTRLEN @@ -80,35 +78,57 @@ * if the conversion succeeds. Otherwise, NULL is returned and the errno * is set to indicate the error. There follow errno values may be set: * - * EAFNOSUPPORT - The af argument is invalid. * ENOSPC - The size of the inet_ntop() result buffer is inadequate * ****************************************************************************/ -FAR const char *inet_ntop(int af, FAR const void *src, FAR char *dst, socklen_t size) +#ifdef CONFIG_NET_IPv4 +static int inet_ipv4_ntop(FAR const void *src, FAR char *dest, socklen_t size) { - int errval; -#ifndef CONFIG_NET_IPv6 FAR char *ptr; - DEBUGASSERT(src && dst); - - if (af != AF_INET) - { - errval = EAFNOSUPPORT; - goto errout; - } - if (size < INET_ADDRSTRLEN) { - errval = ENOSPC; - goto errout; + return -ENOSPC; } ptr = (FAR char*)src; - sprintf(dst, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]); - return dst; -#else + sprintf(dest, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]); + return OK; +} +#endif + +/**************************************************************************** + * Name: inet_ipv6_ntop + * + * Description: + * The inet_ipv6_ntop() function converts a numeric IPv6 address into a + * text string suitable for presentation. + * + * Input Parameters: + * af - The af argument specifies the family of the address. This can be + * AF_INET or AF_INET6. + * src - The src argument points to a buffer holding an address of the + * specified type. The address must be in network byte order. + * dest - The dest argument points to a buffer where the function stores + * the resulting text string; it shall not be NULL. + * size - The size argument specifies the size of this buffer, which must + * be large enough to hold the text string (INET_ADDRSTRLEN + * characters for IPv4, INET6_ADDRSTRLEN characters for IPv6). + * + * Returned Value: + * inet_ntop() returns a pointer to the buffer containing the text string + * if the conversion succeeds. Otherwise, NULL is returned and the errno + * is set to indicate the error. There follow errno values may be set: + * + * EAFNOSUPPORT - The af argument is invalid. + * ENOSPC - The size of the inet_ntop() result buffer is inadequate + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv6 +static int inet_ipv6_ntop(FAR const void *src, FAR char *dest, socklen_t size) +{ FAR const struct in6_addr *in6_addr; uint16_t warray[8]; int offset; @@ -117,18 +137,9 @@ FAR const char *inet_ntop(int af, FAR const void *src, FAR char *dst, socklen_t int maxentry; int maxcount; - DEBUGASSERT(src && dst); - - if (af != AF_INET6) - { - errval = EAFNOSUPPORT; - goto errout; - } - if (size < INET6_ADDRSTRLEN) { - errval = ENOSPC; - goto errout; + return -ENOSPC; } in6_addr = (FAR const struct in6_addr *)src; @@ -167,36 +178,105 @@ FAR const char *inet_ntop(int af, FAR const void *src, FAR char *dst, socklen_t } offset = 0; - dst[0] = '\0'; + dest[0] = '\0'; while (offset < 8) { if (offset == maxentry) { - size -= snprintf(&dst[strlen(dst)], size, ":"); + size -= snprintf(&dest[strlen(dest)], size, ":"); offset += maxcount; if (offset >= 8) { - size -= snprintf(&dst[strlen(dst)], size, ":"); + size -= snprintf(&dest[strlen(dest)], size, ":"); } } else { if (offset > 0) { - size -= snprintf(&dst[strlen(dst)], size, ":"); + size -= snprintf(&dest[strlen(dest)], size, ":"); } - size -= snprintf(&dst[strlen(dst)], size, "%x", warray[offset]); + size -= snprintf(&dest[strlen(dest)], size, "%x", warray[offset]); offset++; } } - return dst; + return OK; +} #endif -errout: - set_errno(errval); - memset(dst, 0, size); - return NULL; +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: inet_ntop + * + * Description: + * The inet_ntop() function converts a numeric address into a text string + * suitable for presentation. + * + * Input Parameters: + * af - The af argument specifies the family of the address. This can be + * AF_INET or AF_INET6. + * src - The src argument points to a buffer holding an address of the + * specified type. The address must be in network byte order. + * dest - The dest argument points to a buffer where the function stores + * the resulting text string; it shall not be NULL. + * size - The size argument specifies the size of this buffer, which must + * be large enough to hold the text string (INET_ADDRSTRLEN + * characters for IPv4, INET6_ADDRSTRLEN characters for IPv6). + * + * Returned Value: + * inet_ntop() returns a pointer to the buffer containing the text string + * if the conversion succeeds. Otherwise, NULL is returned and the errno + * is set to indicate the error. There follow errno values may be set: + * + * EAFNOSUPPORT - The af argument is invalid. + * ENOSPC - The size of the inet_ntop() result buffer is inadequate + * + ****************************************************************************/ + +FAR const char *inet_ntop(int af, FAR const void *src, FAR char *dest, + socklen_t size) +{ + int ret; + + DEBUGASSERT(src && dest); + + /* Do the conversion according to the IP version */ + + switch (af) + { +#ifdef CONFIG_NET_IPv4 + case AF_INET: + ret = inet_ipv4_ntop(src, dest, size); + break; +#endif + +#ifdef CONFIG_NET_IPv6 + case AF_INET6: + ret = inet_ipv6_ntop(src, dest, size); + break; +#endif + + default: + ret = -EAFNOSUPPORT; + break; + } + + /* Handle errors in the conversion */ + + if (ret < 0) + { + set_errno(-ret); + memset(dest, 0, size); + return NULL; + } + + /* Return success */ + + return dest; } diff --git a/nuttx/libc/net/lib_inetpton.c b/nuttx/libc/net/lib_inetpton.c index 8b058e044..72229d2f2 100644 --- a/nuttx/libc/net/lib_inetpton.c +++ b/nuttx/libc/net/lib_inetpton.c @@ -56,61 +56,33 @@ #include <netinet/in.h> /**************************************************************************** - * Public Functions + * Private Functions ****************************************************************************/ /**************************************************************************** - * Name: inet_pton + * Name: inet_ipv4_pton * * Description: - * The inet_pton() function converts an address in its standard text - * presentation form into its numeric binary form. - * - * If the af argument of inet_pton() is AF_INET, the src string will be - * in the standard IPv4 dotted-decimal form: - * - * ddd.ddd.ddd.ddd - * - * where "ddd" is a one to three digit decimal number between 0 and 255. - * - * If the af argument of inet_pton() is AF_INET6, the src string will be in - * one of the following standard IPv6 text forms: - * - * 1. The preferred form is "x:x:x:x:x:x:x:x", where the 'x' s are the - * hexadecimal values of the eight 16-bit pieces of the address. Leading - * zeros in individual fields can be omitted, but there must be at least - * one numeral in every field. - * - * 2. A string of contiguous zero fields in the preferred form can be shown - * as "::". The "::" can only appear once in an address. Unspecified - * addresses ( "0:0:0:0:0:0:0:0" ) may be represented simply as "::". - * - * 3. A third form that is sometimes more convenient when dealing with a - * mixed environment of IPv4 and IPv6 nodes is "x:x:x:x:x:x:d.d.d.d", - * where the 'x' s are the hexadecimal values of the six high-order - * 16-bit pieces of the address, and the 'd' s are the decimal values - * of the four low-order 8-bit pieces of the address (standard IPv4 - * representation). + * The inet_ipv4_pton() function converts an IPv4 address in its standard + * text presentation form into its numeric binary form. * * Input Parameters: - * af - The af argument specifies the family of the address. This can be - * AF_INET or AF_INET6. * src - The src argument points to the string being passed in. - * dst - The dst argument points to a numstr into which the function stores + * dest - The dest argument points to a numstr into which the function stores * the numeric address; this must be large enough to hold the numeric * address (32 bits for AF_INET, 128 bits for AF_INET6). * * Returned Value: * The inet_pton() function returns 1 if the conversion succeeds, with the - * address pointed to by dst in network byte order. It will return 0 if the + * address pointed to by dest in network byte order. It will return 0 if the * input is not a valid IPv4 dotted-decimal string or a valid IPv6 address * string, or -1 with errno set to EAFNOSUPPOR] if the af argument is unknown. * ****************************************************************************/ -int inet_pton(int af, FAR const char *src, FAR void *dst) +#ifdef CONFIG_NET_IPv4 +static int inet_ipv4_pton(FAR const char *src, FAR void *dest) { -#ifndef CONFIG_NET_IPv6 size_t srcoffset; size_t numoffset; int value; @@ -119,17 +91,9 @@ int inet_pton(int af, FAR const char *src, FAR void *dst) char numstr[4]; uint8_t *ip; - DEBUGASSERT(src && dst); - - if (af != AF_INET) - { - set_errno(EAFNOSUPPORT); - return -1; - } - - (void)memset(dst, 0, sizeof(struct in_addr)); + (void)memset(dest, 0, sizeof(struct in_addr)); - ip = (uint8_t *)dst; + ip = (uint8_t *)dest; srcoffset = 0; numoffset = 0; ndots = 0; @@ -204,7 +168,33 @@ int inet_pton(int af, FAR const char *src, FAR void *dst) /* Return zero if there is any problem parsing the input */ return 0; -#else +} +#endif + +/**************************************************************************** + * Name: inet_ipv6_pton + * + * Description: + * The inet_ipv6_pton() function converts an IPv6 address in its standard + * text presentation form into its numeric binary form. + * + * Input Parameters: + * src - The src argument points to the string being passed in. + * dest - The dest argument points to a numstr into which the function stores + * the numeric address; this must be large enough to hold the numeric + * address (32 bits for AF_INET, 128 bits for AF_INET6). + * + * Returned Value: + * The inet_pton() function returns 1 if the conversion succeeds, with the + * address pointed to by dest in network byte order. It will return 0 if the + * input is not a valid IPv4 dotted-decimal string or a valid IPv6 address + * string, or -1 with errno set to EAFNOSUPPOR] if the af argument is unknown. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv6 +static int inet_ipv6_pton(FAR const char *src, FAR void *dest) +{ size_t srcoffset; size_t numoffset; long value; @@ -216,15 +206,7 @@ int inet_pton(int af, FAR const char *src, FAR void *dst) uint8_t rip[sizeof(struct in6_addr)]; bool rtime; - DEBUGASSERT(src && dst); - - if (af != AF_INET6) - { - set_errno(EAFNOSUPPORT); - return -1; - } - - (void)memset(dst, 0, sizeof(struct in6_addr)); + (void)memset(dest, 0, sizeof(struct in6_addr)); srcoffset = 0; numoffset = 0; @@ -298,12 +280,12 @@ int inet_pton(int af, FAR const char *src, FAR void *dst) if (nsep > 0) { - memcpy(dst, &ip[0], nsep << 1); + memcpy(dest, &ip[0], nsep << 1); } if (nrsep > 0) { - memcpy(dst + (16 - (nrsep << 1)), &rip[0], nrsep << 1); + memcpy(dest + (16 - (nrsep << 1)), &rip[0], nrsep << 1); } /* Return 1 if the conversion succeeds */ @@ -335,5 +317,83 @@ int inet_pton(int af, FAR const char *src, FAR void *dst) /* Return zero if there is any problem parsing the input */ return 0; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: inet_pton + * + * Description: + * The inet_pton() function converts an address in its standard text + * presentation form into its numeric binary form. + * + * If the af argument of inet_pton() is AF_INET, the src string will be + * in the standard IPv4 dotted-decimal form: + * + * ddd.ddd.ddd.ddd + * + * where "ddd" is a one to three digit decimal number between 0 and 255. + * + * If the af argument of inet_pton() is AF_INET6, the src string will be in + * one of the following standard IPv6 text forms: + * + * 1. The preferred form is "x:x:x:x:x:x:x:x", where the 'x' s are the + * hexadecimal values of the eight 16-bit pieces of the address. Leading + * zeros in individual fields can be omitted, but there must be at least + * one numeral in every field. + * + * 2. A string of contiguous zero fields in the preferred form can be shown + * as "::". The "::" can only appear once in an address. Unspecified + * addresses ( "0:0:0:0:0:0:0:0" ) may be represented simply as "::". + * + * 3. A third form that is sometimes more convenient when dealing with a + * mixed environment of IPv4 and IPv6 nodes is "x:x:x:x:x:x:d.d.d.d", + * where the 'x' s are the hexadecimal values of the six high-order + * 16-bit pieces of the address, and the 'd' s are the decimal values + * of the four low-order 8-bit pieces of the address (standard IPv4 + * representation). + * + * Input Parameters: + * af - The af argument specifies the family of the address. This can be + * AF_INET or AF_INET6. + * src - The src argument points to the string being passed in. + * dest - The dest argument points to a numstr into which the function stores + * the numeric address; this must be large enough to hold the numeric + * address (32 bits for AF_INET, 128 bits for AF_INET6). + * + * Returned Value: + * The inet_pton() function returns 1 if the conversion succeeds, with the + * address pointed to by dest in network byte order. It will return 0 if the + * input is not a valid IPv4 dotted-decimal string or a valid IPv6 address + * string, or -1 with errno set to EAFNOSUPPORT] if the af argument is + * unknown. + * + ****************************************************************************/ + +int inet_pton(int af, FAR const char *src, FAR void *dest) +{ + DEBUGASSERT(src && dest); + + /* Do the conversion according to the IP version */ + + switch (af) + { +#ifdef CONFIG_NET_IPv4 + case AF_INET: + return inet_ipv4_pton(src, dest); +#endif + +#ifdef CONFIG_NET_IPv6 + case AF_INET6: + return inet_ipv6_pton(src, dest); #endif + + default: + set_errno(EAFNOSUPPORT); + return ERROR; + } } |