From 4848dd3d3c775335518ee7bebd09eec2444e4475 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 17 Jan 2015 15:17:35 -0600 Subject: Networking: Straighten up use if IPv6/IPv4 in TCP connection logic --- nuttx/net/socket/connect.c | 66 ++++++++++++++++++++++++++-------------------- nuttx/net/tcp/tcp.h | 11 +++----- nuttx/net/tcp/tcp_conn.c | 51 ++++++++++++++++++++++++++--------- 3 files changed, 78 insertions(+), 50 deletions(-) (limited to 'nuttx') diff --git a/nuttx/net/socket/connect.c b/nuttx/net/socket/connect.c index 9e996093f..a412fa3f7 100644 --- a/nuttx/net/socket/connect.c +++ b/nuttx/net/socket/connect.c @@ -84,12 +84,8 @@ static inline void psock_teardown_callbacks(FAR struct tcp_connect_s *pstate, static uint16_t psock_connect_interrupt(FAR struct net_driver_s *dev, FAR void *pvconn, FAR void *pvpriv, uint16_t flags); -#ifdef CONFIG_NET_IPv6 static inline int psock_tcp_connect(FAR struct socket *psock, - FAR const struct sockaddr_in6 *inaddr); -#else -static inline int psock_tcp_connect(FAR struct socket *psock, const struct sockaddr_in *inaddr); -#endif + FAR const struct sockaddr *addr); #endif /* CONFIG_NET_TCP */ /**************************************************************************** @@ -278,8 +274,8 @@ static uint16_t psock_connect_interrupt(FAR struct net_driver_s *dev, * Perform a TCP connection * * Parameters: - * psock A reference to the socket structure of the socket to be connected - * inaddr The address of the remote server to connect to + * psock - A reference to the socket structure of the socket to be connected + * addr - The address of the remote server to connect to * * Returned Value: * None @@ -290,13 +286,8 @@ static uint16_t psock_connect_interrupt(FAR struct net_driver_s *dev, ****************************************************************************/ #ifdef CONFIG_NET_TCP -#ifdef CONFIG_NET_IPv6 -static inline int psock_tcp_connect(FAR struct socket *psock, - FAR const struct sockaddr_in6 *inaddr) -#else static inline int psock_tcp_connect(FAR struct socket *psock, - FAR const struct sockaddr_in *inaddr) -#endif + FAR const struct sockaddr *addr) { struct tcp_connect_s state; net_lock_t flags; @@ -319,7 +310,7 @@ static inline int psock_tcp_connect(FAR struct socket *psock, { /* Perform the TCP connection operation */ - ret = tcp_connect(psock->s_conn, inaddr); + ret = tcp_connect(psock->s_conn, addr); } if (ret >= 0) @@ -370,8 +361,8 @@ static inline int psock_tcp_connect(FAR struct socket *psock, } } - net_unlock(flags); - return ret; + net_unlock(flags); + return ret; } #endif /* CONFIG_NET_TCP */ @@ -450,15 +441,10 @@ static inline int psock_tcp_connect(FAR struct socket *psock, int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, socklen_t addrlen) { + FAR const struct sockaddr_in *inaddr = (FAR const struct sockaddr_in *)addr; #if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP) -#ifdef CONFIG_NET_IPv6 - FAR const struct sockaddr_in6 *inaddr = (const struct sockaddr_in6 *)addr; -#else - FAR const struct sockaddr_in *inaddr = (const struct sockaddr_in *)addr; -#endif int ret; #endif - int err; /* Verify that the psock corresponds to valid, allocated socket */ @@ -471,15 +457,37 @@ int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, /* Verify that a valid address has been provided */ + switch (inaddr->sin_family) + { +#ifdef CONFIG_NET_IPv4 + case AF_INET: + { + if (addrlen < sizeof(struct sockaddr_in)) + { + err = EBADF; + goto errout; + } + } + break; +#endif + #ifdef CONFIG_NET_IPv6 - if (addr->sa_family != AF_INET6 || addrlen < sizeof(struct sockaddr_in6)) -#else - if (addr->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in)) + case AF_INET6: + { + if (addrlen < sizeof(struct sockaddr_in6)) + { + err = EBADF; + goto errout; + } + } + break; #endif - { - err = EBADF; + + default: + DEBUGPANIC(); + err = EAFNOSUPPORT; goto errout; - } + } /* Perform the connection depending on the protocol type */ @@ -498,7 +506,7 @@ int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, /* Its not ... connect it */ - ret = psock_tcp_connect(psock, inaddr); + ret = psock_tcp_connect(psock, addr); if (ret < 0) { err = -ret; diff --git a/nuttx/net/tcp/tcp.h b/nuttx/net/tcp/tcp.h index 70e11369b..53c7f7257 100644 --- a/nuttx/net/tcp/tcp.h +++ b/nuttx/net/tcp/tcp.h @@ -1,7 +1,7 @@ /**************************************************************************** * net/tcp/tcp.h * - * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -269,6 +269,7 @@ extern "C" /* Defined in tcp_conn.c ****************************************************/ +struct sockaddr; /* Forward reference */ struct tcp_iphdr_s; /* Forward reference */ /**************************************************************************** @@ -393,13 +394,7 @@ int tcp_bind(FAR struct tcp_conn_s *conn, * ****************************************************************************/ -#ifdef CONFIG_NET_IPv6 -int tcp_connect(FAR struct tcp_conn_s *conn, - FAR const struct sockaddr_in6 *addr); -#else -int tcp_connect(FAR struct tcp_conn_s *conn, - FAR const struct sockaddr_in *addr); -#endif +int tcp_connect(FAR struct tcp_conn_s *conn, FAR const struct sockaddr *addr); /* Defined in tcp_ipselect.c ************************************************/ /**************************************************************************** diff --git a/nuttx/net/tcp/tcp_conn.c b/nuttx/net/tcp/tcp_conn.c index a150ac2ac..a8283278b 100644 --- a/nuttx/net/tcp/tcp_conn.c +++ b/nuttx/net/tcp/tcp_conn.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/tcp/tcp_conn.c * - * Copyright (C) 2007-2011, 2013-2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2011, 2013-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Large parts of this file were leveraged from uIP logic: @@ -739,13 +739,7 @@ int tcp_bind(FAR struct tcp_conn_s *conn, * ****************************************************************************/ -#ifdef CONFIG_NET_IPv6 -int tcp_connect(FAR struct tcp_conn_s *conn, - FAR const struct sockaddr_in6 *addr) -#else -int tcp_connect(FAR struct tcp_conn_s *conn, - FAR const struct sockaddr_in *addr) -#endif +int tcp_connect(FAR struct tcp_conn_s *conn, FAR const struct sockaddr *addr) { net_lock_t flags; int port; @@ -787,7 +781,6 @@ int tcp_connect(FAR struct tcp_conn_s *conn, conn->tcpstateflags = TCP_SYN_SENT; tcp_initsequence(conn->sndseq); - conn->mss = MIN_IPv4_TCP_INITIAL_MSS; conn->unacked = 1; /* TCP length of the SYN is one. */ conn->nrtx = 0; conn->timer = 1; /* Send the SYN next time around. */ @@ -801,13 +794,45 @@ int tcp_connect(FAR struct tcp_conn_s *conn, conn->sent = 0; #endif - /* The sockaddr port is 16 bits and already in network order */ + /* Save values that are specific to the IP address domain */ - conn->rport = addr->sin_port; +#ifdef CONFIG_NET_IPv4 +#ifdef CONFIG_NET_IPv6 + if (conn->domain == PF_INET) +#endif + { + FAR const struct sockaddr_in *inaddr = + (FAR const struct sockaddr_in *)addr; + + /* Save MSS and the port from the sockaddr (already in network order) */ + + conn->mss = MIN_IPv4_TCP_INITIAL_MSS; + conn->rport = inaddr->sin_port; - /* The sockaddr address is 32-bits in network order. */ + /* The sockaddr address is 32-bits in network order. */ + + net_ipv4addr_copy(conn->u.ipv4.raddr, inaddr->sin_addr.s_addr); + } +#endif /* CONFIG_NET_IPv4 */ - net_ipv4addr_copy(conn->u.ipv4.raddr, addr->sin_addr.s_addr); +#ifdef CONFIG_NET_IPv6 +#ifdef CONFIG_NET_IPv4 + else +#endif + { + FAR const struct sockaddr_in6 *inaddr = + (FAR const struct sockaddr_in6 *)addr; + + /* Save MSS and the port from the sockaddr (already in network order) */ + + conn->mss = MIN_IPv6_TCP_INITIAL_MSS; + conn->rport = inaddr->sin_port; + + /* The sockaddr address is 32-bits in network order. */ + + net_ipv6addr_copy(conn->u.ipv6.raddr, inaddr->sin6_addr.s6_addr16); + } +#endif /* CONFIG_NET_IPv6 */ #ifdef CONFIG_NET_TCP_READAHEAD /* Initialize the list of TCP read-ahead buffers */ -- cgit v1.2.3