net: correct in*_addr parameter of net_addr_pton()
Currently, the function accepts a struct sockaddr * but the code immediately type casts this to either in_addr or in6_addr. This is incorrect behavior as the first field in a sockaddr is sa_family_t and not address data. So without special knowledge, a developer will use a sockaddr structure as the parameter and then wonder why the address information isn't being set correctly. Let's change this parameter to void * which makes this function similar to inet_pton(). Jira: ZEP-1616 Change-Id: I1fc9368da999d90feb07c03fac55dcc749d4eba6 Signed-off-by: Michael Scott <michael.scott@linaro.org>
This commit is contained in:
parent
6d50b47987
commit
ca6a686495
13 changed files with 27 additions and 29 deletions
|
@ -793,8 +793,7 @@ struct sockaddr_in_ptr *net_sin_ptr(const struct sockaddr_ptr *addr)
|
|||
*
|
||||
* @return 0 if ok, < 0 if error
|
||||
*/
|
||||
int net_addr_pton(sa_family_t family, const char *src,
|
||||
struct sockaddr *dst);
|
||||
int net_addr_pton(sa_family_t family, const char *src, void *dst);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ static inline void init_app(void)
|
|||
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR,
|
||||
(struct sockaddr *)&in6addr_my) < 0) {
|
||||
&in6addr_my) < 0) {
|
||||
printk("Invalid IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR);
|
||||
}
|
||||
|
|
|
@ -183,7 +183,7 @@ int udp_init(struct udp_context *ctx)
|
|||
#if defined(CONFIG_NET_SAMPLES_PEER_IPV6_ADDR)
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_PEER_IPV6_ADDR,
|
||||
(struct sockaddr *)&server_addr) < 0) {
|
||||
&server_addr) < 0) {
|
||||
printk("Invalid peer IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_PEER_IPV6_ADDR);
|
||||
}
|
||||
|
|
|
@ -657,7 +657,7 @@ static inline int init_app(void)
|
|||
{
|
||||
#if defined(CONFIG_NET_SAMPLES_MY_IPV6_ADDR)
|
||||
if (net_addr_pton(AF_INET6, CONFIG_NET_SAMPLES_MY_IPV6_ADDR,
|
||||
(struct sockaddr *)&server_addr) < 0) {
|
||||
&server_addr) < 0) {
|
||||
mbedtls_printf("Invalid IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR);
|
||||
}
|
||||
|
|
|
@ -169,7 +169,7 @@ static inline void init_app(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_IP_ADDRESSES)
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR,
|
||||
(struct sockaddr *)&my_addr6.sin6_addr) < 0) {
|
||||
&my_addr6.sin6_addr) < 0) {
|
||||
NET_ERR("Invalid IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR);
|
||||
|
||||
|
@ -180,7 +180,7 @@ static inline void init_app(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_IP_ADDRESSES)
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_PEER_IPV6_ADDR,
|
||||
(struct sockaddr *)&peer_addr6.sin6_addr) < 0) {
|
||||
&peer_addr6.sin6_addr) < 0) {
|
||||
NET_ERR("Invalid peer IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_PEER_IPV6_ADDR);
|
||||
|
||||
|
@ -211,7 +211,7 @@ static inline void init_app(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_IP_ADDRESSES)
|
||||
if (net_addr_pton(AF_INET,
|
||||
CONFIG_NET_SAMPLES_MY_IPV4_ADDR,
|
||||
(struct sockaddr *)&my_addr4.sin_addr) < 0) {
|
||||
&my_addr4.sin_addr) < 0) {
|
||||
NET_ERR("Invalid IPv4 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV4_ADDR);
|
||||
|
||||
|
@ -222,7 +222,7 @@ static inline void init_app(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_IP_ADDRESSES)
|
||||
if (net_addr_pton(AF_INET,
|
||||
CONFIG_NET_SAMPLES_PEER_IPV4_ADDR,
|
||||
(struct sockaddr *)&peer_addr4.sin_addr) < 0) {
|
||||
&peer_addr4.sin_addr) < 0) {
|
||||
NET_ERR("Invalid peer IPv4 address %s",
|
||||
CONFIG_NET_SAMPLES_PEER_IPV4_ADDR);
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ static inline void init_app(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_MY_IPV6_ADDR)
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR,
|
||||
(struct sockaddr *)&in6addr_my) < 0) {
|
||||
&in6addr_my) < 0) {
|
||||
NET_ERR("Invalid IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR);
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ static inline void init_app(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_MY_IPV4_ADDR)
|
||||
if (net_addr_pton(AF_INET,
|
||||
CONFIG_NET_SAMPLES_MY_IPV4_ADDR,
|
||||
(struct sockaddr *)&in4addr_my) < 0) {
|
||||
&in4addr_my) < 0) {
|
||||
NET_ERR("Invalid IPv4 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV4_ADDR);
|
||||
}
|
||||
|
|
|
@ -300,7 +300,7 @@ static inline int init_app(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_MY_IPV6_ADDR)
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR,
|
||||
(struct sockaddr *)&client_addr) < 0) {
|
||||
&client_addr) < 0) {
|
||||
mbedtls_printf("Invalid IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR);
|
||||
}
|
||||
|
@ -314,7 +314,7 @@ static inline int init_app(void)
|
|||
#else
|
||||
#if defined(CONFIG_NET_SAMPLES_MY_IPV4_ADDR)
|
||||
if (net_addr_pton(AF_INET, CONFIG_NET_SAMPLES_MY_IPV4_ADDR,
|
||||
(struct sockaddr *)&client_addr) < 0) {
|
||||
&client_addr) < 0) {
|
||||
mbedtls_printf("Invalid IPv4 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV4_ADDR);
|
||||
}
|
||||
|
|
|
@ -192,7 +192,7 @@ int udp_init(struct udp_context *ctx)
|
|||
#if defined(CONFIG_NET_SAMPLES_PEER_IPV6_ADDR)
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_PEER_IPV6_ADDR,
|
||||
(struct sockaddr *)&server_addr) < 0) {
|
||||
&server_addr) < 0) {
|
||||
printk("Invalid peer IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_PEER_IPV6_ADDR);
|
||||
}
|
||||
|
@ -242,7 +242,7 @@ int udp_init(struct udp_context *ctx)
|
|||
|
||||
#if defined(CONFIG_NET_SAMPLES_PEER_IPV4_ADDR)
|
||||
if (net_addr_pton(AF_INET, CONFIG_NET_SAMPLES_PEER_IPV4_ADDR,
|
||||
(struct sockaddr *)&server_addr) < 0) {
|
||||
&server_addr) < 0) {
|
||||
printk("Invalid IPv4 address %s",
|
||||
CONFIG_NET_SAMPLES_PEER_IPV4_ADDR);
|
||||
}
|
||||
|
|
|
@ -375,7 +375,7 @@ static inline int init_app(void)
|
|||
#if defined(CONFIG_NET_IPV6)
|
||||
#if defined(CONFIG_NET_SAMPLES_MY_IPV6_ADDR)
|
||||
if (net_addr_pton(AF_INET6, CONFIG_NET_SAMPLES_MY_IPV6_ADDR,
|
||||
(struct sockaddr *)&server_addr) < 0) {
|
||||
&server_addr) < 0) {
|
||||
mbedtls_printf("Invalid IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR);
|
||||
}
|
||||
|
@ -390,7 +390,7 @@ static inline int init_app(void)
|
|||
#else
|
||||
#if defined(CONFIG_NET_SAMPLES_MY_IPV4_ADDR)
|
||||
if (net_addr_pton(AF_INET, CONFIG_NET_SAMPLES_MY_IPV4_ADDR,
|
||||
(struct sockaddr *)&server_addr) < 0) {
|
||||
&server_addr) < 0) {
|
||||
mbedtls_printf("Invalid IPv4 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV4_ADDR);
|
||||
}
|
||||
|
|
|
@ -876,7 +876,7 @@ static bool join_coap_multicast_group(void)
|
|||
#if defined(CONFIG_NET_SAMPLES_IP_ADDRESSES)
|
||||
if (net_addr_pton(AF_INET6,
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR,
|
||||
(struct sockaddr *)&my_addr) < 0) {
|
||||
&my_addr) < 0) {
|
||||
NET_ERR("Invalid IPv6 address %s",
|
||||
CONFIG_NET_SAMPLES_MY_IPV6_ADDR);
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ int zperf_get_ipv6_addr(char *host, char *prefix_str, struct in6_addr *addr,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret = net_addr_pton(AF_INET6, host, (struct sockaddr *)addr);
|
||||
ret = net_addr_pton(AF_INET6, host, addr);
|
||||
if (ret < 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -147,7 +147,7 @@ int zperf_get_ipv4_addr(char *host, struct in_addr *addr, const char *str)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret = net_addr_pton(AF_INET, host, (struct sockaddr *)addr);
|
||||
ret = net_addr_pton(AF_INET, host, addr);
|
||||
if (ret < 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -211,7 +211,7 @@ static int shell_cmd_setip(int argc, char *argv[])
|
|||
|
||||
#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
|
||||
if (net_addr_pton(AF_INET6, argv[start + 1],
|
||||
(struct sockaddr *)&ipv6) < 0) {
|
||||
&ipv6) < 0) {
|
||||
if (argc != 2) {
|
||||
/* Print usage */
|
||||
printk("\n%s:\n", CMD_STR_SETIP);
|
||||
|
@ -937,7 +937,7 @@ static void zperf_init(void)
|
|||
}
|
||||
|
||||
ret = net_addr_pton(AF_INET6, DST_IP6ADDR,
|
||||
(struct sockaddr *)&in6_addr_dst.sin6_addr);
|
||||
&in6_addr_dst.sin6_addr);
|
||||
if (ret < 0) {
|
||||
printk("[%s] ERROR! Unable to set IP %s\n", __func__,
|
||||
DST_IP6ADDR);
|
||||
|
@ -958,7 +958,7 @@ static void zperf_init(void)
|
|||
}
|
||||
|
||||
ret = net_addr_pton(AF_INET, DST_IP4ADDR,
|
||||
(struct sockaddr *)&in4_addr_dst.sin_addr);
|
||||
&in4_addr_dst.sin_addr);
|
||||
if (ret < 0) {
|
||||
printk("[%s] ERROR! Unable to set IP %s\n", __func__,
|
||||
DST_IP4ADDR);
|
||||
|
|
|
@ -630,7 +630,7 @@ static int shell_cmd_ping(int argc, char *argv[])
|
|||
}
|
||||
|
||||
#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4)
|
||||
ret = net_addr_pton(AF_INET6, host, (struct sockaddr *)&ipv6_target);
|
||||
ret = net_addr_pton(AF_INET6, host, &ipv6_target);
|
||||
if (ret < 0) {
|
||||
printk("Invalid IPv6 address\n");
|
||||
return 0;
|
||||
|
@ -646,7 +646,7 @@ static int shell_cmd_ping(int argc, char *argv[])
|
|||
#endif
|
||||
|
||||
#if defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6)
|
||||
ret = net_addr_pton(AF_INET, host, (struct sockaddr *)&ipv4_target);
|
||||
ret = net_addr_pton(AF_INET, host, &ipv4_target);
|
||||
if (ret < 0) {
|
||||
printk("Invalid IPv4 address\n");
|
||||
return 0;
|
||||
|
@ -662,10 +662,9 @@ static int shell_cmd_ping(int argc, char *argv[])
|
|||
#endif
|
||||
|
||||
#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
|
||||
ret = net_addr_pton(AF_INET6, host, (struct sockaddr *)&ipv6_target);
|
||||
ret = net_addr_pton(AF_INET6, host, &ipv6_target);
|
||||
if (ret < 0) {
|
||||
ret = net_addr_pton(AF_INET, host,
|
||||
(struct sockaddr *)&ipv4_target);
|
||||
ret = net_addr_pton(AF_INET, host, &ipv4_target);
|
||||
if (ret < 0) {
|
||||
printk("Invalid IP address\n");
|
||||
return 0;
|
||||
|
|
|
@ -231,7 +231,7 @@ char *net_sprint_ip_addr_buf(const uint8_t *ip, int ip_len,
|
|||
}
|
||||
|
||||
int net_addr_pton(sa_family_t family, const char *src,
|
||||
struct sockaddr *dst)
|
||||
void *dst)
|
||||
{
|
||||
if (family == AF_INET) {
|
||||
struct in_addr *addr = (struct in_addr *)dst;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue