You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/01/31 19:30:48 UTC

[incubator-nuttx] 04/10: Move _SF_BOUND and _SF_CONNECTED modification to common place

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr192
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 644f78fb444d48da582a937f6aa99424dab58131
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Fri Jan 31 17:34:28 2020 +0800

    Move _SF_BOUND and _SF_CONNECTED modification to common place
    
    Change-Id: I516e61818006221c97d223f8b401949846ee4460
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 net/bluetooth/bluetooth_sockif.c   | 10 +---------
 net/ieee802154/ieee802154_sockif.c | 10 +---------
 net/inet/inet_sockif.c             | 20 ++------------------
 net/inet/ipv4_getpeername.c        |  2 +-
 net/inet/ipv6_getpeername.c        |  2 +-
 net/local/local_sockif.c           |  7 -------
 net/netlink/netlink_sockif.c       |  1 -
 net/pkt/pkt_sockif.c               |  3 ---
 net/socket/bind.c                  |  4 ++++
 net/socket/connect.c               |  9 +++++++++
 10 files changed, 19 insertions(+), 49 deletions(-)

diff --git a/net/bluetooth/bluetooth_sockif.c b/net/bluetooth/bluetooth_sockif.c
index 350208e..adf6125 100644
--- a/net/bluetooth/bluetooth_sockif.c
+++ b/net/bluetooth/bluetooth_sockif.c
@@ -271,7 +271,7 @@ static int bluetooth_connect(FAR struct socket *psock,
 {
   FAR struct bluetooth_conn_s *conn;
   FAR struct sockaddr_bt_s *btaddr;
-  int ret;
+  int ret = OK;
 
   DEBUGASSERT(psock != NULL || addr != NULL);
   conn = (FAR struct bluetooth_conn_s *)psock->s_conn;
@@ -286,11 +286,6 @@ static int bluetooth_connect(FAR struct socket *psock,
       btaddr = (FAR struct sockaddr_bt_s *)addr;
       memcpy(&conn->bc_raddr, &btaddr->bt_bdaddr, sizeof(bt_addr_t));
       conn->bc_channel = btaddr->bt_channel;
-
-      /* Mark the socket as connected. */
-
-      psock->s_flags |= _SF_CONNECTED;
-      ret = OK;
     }
   else
     {
@@ -437,9 +432,6 @@ static int bluetooth_bind(FAR struct socket *psock,
 
   memcpy(&conn->bc_laddr, &iaddr->bt_bdaddr, sizeof(bt_addr_t));
 
-  /* Mark the socket bound */
-
-  psock->s_flags |= _SF_BOUND;
   return OK;
 }
 
diff --git a/net/ieee802154/ieee802154_sockif.c b/net/ieee802154/ieee802154_sockif.c
index 597673e..0acad43 100644
--- a/net/ieee802154/ieee802154_sockif.c
+++ b/net/ieee802154/ieee802154_sockif.c
@@ -275,7 +275,7 @@ static int ieee802154_connect(FAR struct socket *psock,
 {
   FAR struct ieee802154_conn_s *conn;
   FAR struct sockaddr_ieee802154_s *ieeeaddr;
-  int ret;
+  int ret = OK;
 
   DEBUGASSERT(psock != NULL || addr != NULL);
   conn = (FAR struct ieee802154_conn_s *)psock->s_conn;
@@ -290,11 +290,6 @@ static int ieee802154_connect(FAR struct socket *psock,
       ieeeaddr = (FAR struct sockaddr_ieee802154_s *)addr;
       memcpy(&conn->raddr, &ieeeaddr->sa_addr,
              sizeof(struct ieee802154_saddr_s));
-
-      /* Mark the socket as connected. */
-
-      psock->s_flags |= _SF_CONNECTED;
-      ret = OK;
     }
   else
     {
@@ -444,9 +439,6 @@ static int ieee802154_bind(FAR struct socket *psock,
 
   memcpy(&conn->laddr, &iaddr->sa_addr, sizeof(struct ieee802154_saddr_s));
 
-  /* Mark the socket bound */
-
-  psock->s_flags |= _SF_BOUND;
   return OK;
 }
 
diff --git a/net/inet/inet_sockif.c b/net/inet/inet_sockif.c
index c42fdbb..27f2acf 100644
--- a/net/inet/inet_sockif.c
+++ b/net/inet/inet_sockif.c
@@ -409,13 +409,6 @@ static int inet_bind(FAR struct socket *psock,
           /* Bind a TCP/IP stream socket. */
 
           ret = tcp_bind(psock->s_conn, addr);
-
-          /* Mark the socket bound */
-
-          if (ret >= 0)
-            {
-              psock->s_flags |= _SF_BOUND;
-            }
 #else
           nwarn("WARNING: TCP/IP stack is not available in this configuration\n");
           return -ENOSYS;
@@ -431,13 +424,6 @@ static int inet_bind(FAR struct socket *psock,
           /* Bind a UDP/IP datagram socket */
 
           ret = udp_bind(psock->s_conn, addr);
-
-          /* Mark the socket bound */
-
-          if (ret >= 0)
-            {
-              psock->s_flags |= _SF_BOUND;
-            }
 #else
           nwarn("WARNING: UDP stack is not available in this configuration\n");
           ret = -ENOSYS;
@@ -757,15 +743,13 @@ static int inet_connect(FAR struct socket *psock,
             {
               /* Failed to connect or explicitly disconnected */
 
-              psock->s_flags &= ~_SF_CONNECTED;
-              conn->flags    &= ~_UDP_FLAG_CONNECTMODE;
+              conn->flags &= ~_UDP_FLAG_CONNECTMODE;
             }
           else
             {
               /* Successfully connected */
 
-              psock->s_flags |= _SF_CONNECTED;
-              conn->flags    |= _UDP_FLAG_CONNECTMODE;
+              conn->flags |= _UDP_FLAG_CONNECTMODE;
             }
 
           return ret;
diff --git a/net/inet/ipv4_getpeername.c b/net/inet/ipv4_getpeername.c
index 1560b2f..c63480e 100644
--- a/net/inet/ipv4_getpeername.c
+++ b/net/inet/ipv4_getpeername.c
@@ -102,7 +102,7 @@ int ipv4_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr,
 
   /* Verify that the socket has been connected */
 
-  if ((psock->s_flags & _SF_CONNECTED) == 0)
+  if (_SS_ISCONNECTED(psock->s_flags) == 0)
     {
       return -ENOTCONN;
     }
diff --git a/net/inet/ipv6_getpeername.c b/net/inet/ipv6_getpeername.c
index ab6329c..9728e28 100644
--- a/net/inet/ipv6_getpeername.c
+++ b/net/inet/ipv6_getpeername.c
@@ -101,7 +101,7 @@ int ipv6_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr,
 
   /* Verify that the socket has been connected */
 
-  if ((psock->s_flags & _SF_CONNECTED) == 0)
+  if (_SS_ISCONNECTED(psock->s_flags) == 0)
     {
       return -ENOTCONN;
     }
diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c
index 97f0922..5b2f533 100644
--- a/net/local/local_sockif.c
+++ b/net/local/local_sockif.c
@@ -309,13 +309,6 @@ static int local_bind(FAR struct socket *psock,
           /* Bind the Unix domain connection structure */
 
           ret = psock_local_bind(psock, addr, addrlen);
-
-          /* Mark the socket bound */
-
-          if (ret >= 0)
-            {
-              psock->s_flags |= _SF_BOUND;
-            }
         }
         break;
 #endif /* CONFIG_NET_LOCAL_STREAM || CONFIG_NET_LOCAL_DGRAM */
diff --git a/net/netlink/netlink_sockif.c b/net/netlink/netlink_sockif.c
index 40d8d3d..cff507a 100644
--- a/net/netlink/netlink_sockif.c
+++ b/net/netlink/netlink_sockif.c
@@ -287,7 +287,6 @@ static int netlink_bind(FAR struct socket *psock,
   conn->pid       = nladdr->nl_pid;
   conn->groups    = nladdr->nl_groups;
 
-  psock->s_flags |= _SF_BOUND;
   return OK;
 }
 
diff --git a/net/pkt/pkt_sockif.c b/net/pkt/pkt_sockif.c
index a6429c3..6541dbc 100644
--- a/net/pkt/pkt_sockif.c
+++ b/net/pkt/pkt_sockif.c
@@ -387,9 +387,6 @@ static int pkt_bind(FAR struct socket *psock, FAR const struct sockaddr *addr,
       conn->ifindex = ifindex;
       memcpy(conn->lmac, dev->d_mac.ether.ether_addr_octet, 6);
 
-      /* Mark the socket bound */
-
-      psock->s_flags |= _SF_BOUND;
       return OK;
     }
   else
diff --git a/net/socket/bind.c b/net/socket/bind.c
index 00e0796..572622d 100644
--- a/net/socket/bind.c
+++ b/net/socket/bind.c
@@ -112,6 +112,10 @@ int psock_bind(FAR struct socket *psock, const struct sockaddr *addr,
       return ret;
     }
 
+  /* Mark the socket bound */
+
+  psock->s_flags |= _SF_BOUND;
+
   return OK;
 }
 
diff --git a/net/socket/connect.c b/net/socket/connect.c
index 9e3ff5b..ed8b78a 100644
--- a/net/socket/connect.c
+++ b/net/socket/connect.c
@@ -156,6 +156,15 @@ int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr,
       return ret;
     }
 
+  if (addr != NULL)
+    {
+      psock->s_flags |= _SF_CONNECTED;
+    }
+  else
+    {
+      psock->s_flags &= ~_SF_CONNECTED;
+    }
+
   return OK;
 }