diff --git a/fs/procfs/fs_procfsproc.c b/fs/procfs/fs_procfsproc.c index 7b4f24ac29..83e4ae9d4b 100644 --- a/fs/procfs/fs_procfsproc.c +++ b/fs/procfs/fs_procfsproc.c @@ -1177,11 +1177,11 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile, if (file->f_inode && INODE_IS_SOCKET(file->f_inode)) { FAR struct socket *socket = file->f_priv; + FAR struct socket_conn_s *conn = socket->s_conn; linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%3d %3d %02x", i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + - j, socket->s_type, - socket->s_flags); + j, socket->s_type, conn->s_flags); copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset); diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h index 203239c1d7..778104b823 100644 --- a/include/nuttx/net/net.h +++ b/include/nuttx/net/net.h @@ -229,6 +229,10 @@ struct socket_conn_s FAR struct devif_callback_s *list; FAR struct devif_callback_s *list_tail; + /* Definitions of 8-bit socket flags */ + + uint8_t s_flags; /* See _SF_* definitions */ + /* Connection-specific content may follow */ }; @@ -243,7 +247,6 @@ struct socket uint8_t s_domain; /* IP domain */ uint8_t s_type; /* Protocol type */ uint8_t s_proto; /* Socket Protocol */ - uint8_t s_flags; /* See _SF_* definitions */ /* Socket options */ diff --git a/net/bluetooth/bluetooth_sendmsg.c b/net/bluetooth/bluetooth_sendmsg.c index 6c6f766da3..14d995da3a 100644 --- a/net/bluetooth/bluetooth_sendmsg.c +++ b/net/bluetooth/bluetooth_sendmsg.c @@ -426,7 +426,7 @@ static ssize_t bluetooth_l2cap_send(FAR struct socket *psock, conn = (FAR struct bluetooth_conn_s *)psock->s_conn; DEBUGASSERT(conn != NULL); - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->bc_conn.s_flags)) { ret = -ENOTCONN; } diff --git a/net/bluetooth/bluetooth_sockif.c b/net/bluetooth/bluetooth_sockif.c index d87d6ea8ac..b224a03d20 100644 --- a/net/bluetooth/bluetooth_sockif.c +++ b/net/bluetooth/bluetooth_sockif.c @@ -447,15 +447,18 @@ static int bluetooth_l2cap_bind(FAR struct socket *psock, * Only SOCK_RAW is supported */ - if (psock->s_type != SOCK_RAW) + if (psock == NULL || psock->s_conn == NULL || + psock->s_type != SOCK_RAW) { nerr("ERROR: Invalid socket type: %u\n", psock->s_type); return -EBADF; } + conn = (FAR struct bluetooth_conn_s *)psock->s_conn; + /* Verify that the socket is not already bound. */ - if (_SS_ISBOUND(psock->s_flags)) + if (_SS_ISBOUND(conn->bc_conn.s_flags)) { nerr("ERROR: Already bound\n"); return -EINVAL; @@ -467,8 +470,6 @@ static int bluetooth_l2cap_bind(FAR struct socket *psock, * support some moral equivalent to INADDR_ANY? */ - conn = (FAR struct bluetooth_conn_s *)psock->s_conn; - conn->bc_proto = psock->s_proto; /* Find the device associated with the requested address */ @@ -519,22 +520,23 @@ static int bluetooth_hci_bind(FAR struct socket *psock, * Only SOCK_RAW is supported */ - if (psock->s_type != SOCK_RAW) + if (psock == NULL || psock->s_conn == NULL || + psock->s_type != SOCK_RAW) { nerr("ERROR: Invalid socket type: %u\n", psock->s_type); return -EBADF; } + conn = (FAR struct bluetooth_conn_s *)psock->s_conn; + /* Verify that the socket is not already bound. */ - if (_SS_ISBOUND(psock->s_flags)) + if (_SS_ISBOUND(conn->bc_conn.s_flags)) { nerr("ERROR: Already bound\n"); return -EINVAL; } - conn = (FAR struct bluetooth_conn_s *)psock->s_conn; - conn->bc_proto = psock->s_proto; conn->bc_channel = hciaddr->hci_channel; conn->bc_ldev = hciaddr->hci_dev; diff --git a/net/can/can_recvmsg.c b/net/can/can_recvmsg.c index 540f19eaaf..5f091946d1 100644 --- a/net/can/can_recvmsg.c +++ b/net/can/can_recvmsg.c @@ -652,7 +652,7 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, /* Handle non-blocking CAN sockets */ - if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + if (_SS_ISNONBLOCK(conn->sconn.s_flags) || (flags & MSG_DONTWAIT) != 0) { /* Return the number of bytes read from the read-ahead buffer if * something was received (already in 'ret'); EAGAIN if not. diff --git a/net/icmp/icmp_recvmsg.c b/net/icmp/icmp_recvmsg.c index 4ceb06886b..3f4649667f 100644 --- a/net/icmp/icmp_recvmsg.c +++ b/net/icmp/icmp_recvmsg.c @@ -410,7 +410,8 @@ ssize_t icmp_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, ret = icmp_readahead(conn, buf, len, (FAR struct sockaddr_in *)from, fromlen); } - else if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + else if (_SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0) { /* Handle non-blocking ICMP sockets */ diff --git a/net/icmpv6/icmpv6_recvmsg.c b/net/icmpv6/icmpv6_recvmsg.c index 5507eea806..addcdb29bc 100644 --- a/net/icmpv6/icmpv6_recvmsg.c +++ b/net/icmpv6/icmpv6_recvmsg.c @@ -417,7 +417,8 @@ ssize_t icmpv6_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, ret = icmpv6_readahead(conn, buf, len, (FAR struct sockaddr_in6 *)from, fromlen); } - else if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + else if (_SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0) { /* Handle non-blocking ICMP sockets */ diff --git a/net/ieee802154/ieee802154_sendmsg.c b/net/ieee802154/ieee802154_sendmsg.c index 6b4fbdf6bc..a005fdef1c 100644 --- a/net/ieee802154/ieee802154_sendmsg.c +++ b/net/ieee802154/ieee802154_sendmsg.c @@ -582,7 +582,7 @@ static ssize_t ieee802154_send(FAR struct socket *psock, FAR const void *buf, { /* send() may be used only if the socket has been connected. */ - if (!_SS_ISCONNECTED(psock->s_flags) || + if (!_SS_ISCONNECTED(conn->sconn.s_flags) || conn->raddr.s_mode == IEEE802154_ADDRMODE_NONE) { ret = -ENOTCONN; diff --git a/net/ieee802154/ieee802154_sockif.c b/net/ieee802154/ieee802154_sockif.c index d9f789bcf5..d2b0303499 100644 --- a/net/ieee802154/ieee802154_sockif.c +++ b/net/ieee802154/ieee802154_sockif.c @@ -357,8 +357,8 @@ static int ieee802154_bind(FAR struct socket *psock, socklen_t addrlen) { FAR const struct sockaddr_ieee802154_s *iaddr; - FAR struct radio_driver_s *radio; FAR struct ieee802154_conn_s *conn; + FAR struct radio_driver_s *radio; DEBUGASSERT(psock != NULL && addr != NULL); @@ -373,9 +373,11 @@ static int ieee802154_bind(FAR struct socket *psock, return -EBADF; } + conn = (FAR struct ieee802154_conn_s *)psock->s_conn; + /* Bind a PF_IEEE802154 socket to an network device. */ - if (psock->s_type != SOCK_DGRAM) + if (conn == NULL || psock->s_type != SOCK_DGRAM) { nerr("ERROR: Invalid socket type: %u\n", psock->s_type); return -EBADF; @@ -383,7 +385,7 @@ static int ieee802154_bind(FAR struct socket *psock, /* Verify that the socket is not already bound. */ - if (_SS_ISBOUND(psock->s_flags)) + if (_SS_ISBOUND(conn->sconn.s_flags)) { nerr("ERROR: Already bound\n"); return -EINVAL; @@ -403,8 +405,6 @@ static int ieee802154_bind(FAR struct socket *psock, return -EADDRNOTAVAIL; } - conn = (FAR struct ieee802154_conn_s *)psock->s_conn; - /* Find the device associated with the requested address */ radio = ieee802154_find_device(conn, &iaddr->sa_addr); diff --git a/net/inet/inet_sockif.c b/net/inet/inet_sockif.c index 0fd102e415..d6384a1163 100644 --- a/net/inet/inet_sockif.c +++ b/net/inet/inet_sockif.c @@ -716,9 +716,11 @@ static int inet_connect(FAR struct socket *psock, #if defined(CONFIG_NET_TCP) && defined(NET_TCP_HAVE_STACK) case SOCK_STREAM: { + FAR struct socket_conn_s *conn = psock->s_conn; + /* Verify that the socket is not already connected */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->s_flags)) { return -EISCONN; } @@ -1069,6 +1071,9 @@ static int inet_poll(FAR struct socket *psock, FAR struct pollfd *fds, static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf, size_t len, int flags) { +#ifdef NET_UDP_HAVE_STACK + FAR struct socket_conn_s *conn = psock->s_conn; +#endif ssize_t ret; switch (psock->s_type) @@ -1112,7 +1117,7 @@ static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf, { /* UDP/IP packet send */ - ret = _SS_ISCONNECTED(psock->s_flags) ? + ret = _SS_ISCONNECTED(conn->s_flags) ? psock_udp_sendto(psock, buf, len, 0, NULL, 0) : -ENOTCONN; } #endif /* NET_UDP_HAVE_STACK */ @@ -1120,7 +1125,7 @@ static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf, #elif defined(NET_UDP_HAVE_STACK) /* Only UDP/IP packet send */ - ret = _SS_ISCONNECTED(psock->s_flags) ? + ret = _SS_ISCONNECTED(conn->s_flags) ? psock_udp_sendto(psock, buf, len, 0, NULL, 0) : -ENOTCONN; #else ret = -ENOSYS; diff --git a/net/inet/ipv4_getpeername.c b/net/inet/ipv4_getpeername.c index 27126bdb50..405f86ee7b 100644 --- a/net/inet/ipv4_getpeername.c +++ b/net/inet/ipv4_getpeername.c @@ -71,6 +71,7 @@ int ipv4_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr, { #if defined(NET_TCP_HAVE_STACK) || defined(NET_UDP_HAVE_STACK) FAR struct sockaddr_in *outaddr = (FAR struct sockaddr_in *)addr; + FAR struct socket_conn_s *conn = psock->s_conn; in_addr_t ripaddr; /* Check if enough space has been provided for the full address */ @@ -87,7 +88,7 @@ int ipv4_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr, /* Verify that the socket has been connected */ - if (_SS_ISCONNECTED(psock->s_flags) == 0) + if (_SS_ISCONNECTED(conn->s_flags) == 0) { return -ENOTCONN; } diff --git a/net/inet/ipv6_getpeername.c b/net/inet/ipv6_getpeername.c index b982e0acac..f3f4d557ee 100644 --- a/net/inet/ipv6_getpeername.c +++ b/net/inet/ipv6_getpeername.c @@ -69,6 +69,7 @@ int ipv6_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr, FAR socklen_t *addrlen) { + FAR struct socket_conn_s *conn = psock->s_conn; FAR struct sockaddr_in6 *outaddr = (FAR struct sockaddr_in6 *)addr; net_ipv6addr_t *ripaddr; @@ -86,7 +87,7 @@ int ipv6_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr, /* Verify that the socket has been connected */ - if (_SS_ISCONNECTED(psock->s_flags) == 0) + if (_SS_ISCONNECTED(conn->s_flags) == 0) { return -ENOTCONN; } diff --git a/net/local/local_accept.c b/net/local/local_accept.c index c951b8852a..835e7af338 100644 --- a/net/local/local_accept.c +++ b/net/local/local_accept.c @@ -181,8 +181,8 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr, * block. */ - ret = local_open_server_tx(conn, - _SS_ISNONBLOCK(psock->s_flags)); + ret = local_open_server_tx( + conn, _SS_ISNONBLOCK(conn->lc_conn.s_flags)); if (ret < 0) { nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n", @@ -201,8 +201,8 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr, * for writing. */ - ret = local_open_server_rx(conn, - _SS_ISNONBLOCK(psock->s_flags)); + ret = local_open_server_rx( + conn, _SS_ISNONBLOCK(conn->lc_conn.s_flags)); if (ret < 0) { nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n", @@ -254,7 +254,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr, /* Was the socket opened non-blocking? */ - if (_SS_ISNONBLOCK(psock->s_flags)) + if (_SS_ISNONBLOCK(server->lc_conn.s_flags)) { /* Yes.. return EAGAIN */ diff --git a/net/local/local_connect.c b/net/local/local_connect.c index b3aff310a1..7153374930 100644 --- a/net/local/local_connect.c +++ b/net/local/local_connect.c @@ -311,8 +311,8 @@ int psock_local_connect(FAR struct socket *psock, if (conn->lc_proto == SOCK_STREAM) { ret = - local_stream_connect(client, conn, - _SS_ISNONBLOCK(psock->s_flags)); + local_stream_connect( + client, conn, _SS_ISNONBLOCK(conn->lc_conn.s_flags)); } net_unlock(); diff --git a/net/local/local_recvmsg.c b/net/local/local_recvmsg.c index 9ef2287879..fd6b87f590 100644 --- a/net/local/local_recvmsg.c +++ b/net/local/local_recvmsg.c @@ -82,8 +82,8 @@ static int psock_fifo_read(FAR struct socket *psock, FAR void *buf, * eventually be reported as ENOTCONN. */ - psock->s_flags &= ~(_SF_CONNECTED | _SF_CLOSED); - conn->lc_state = LOCAL_STATE_DISCONNECTED; + conn->lc_conn.s_flags &= ~(_SF_CONNECTED | _SF_CLOSED); + conn->lc_state = LOCAL_STATE_DISCONNECTED; /* Did we receive any data? */ @@ -330,7 +330,7 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, /* Open the receiving side of the transfer */ - ret = local_open_receiver(conn, _SS_ISNONBLOCK(psock->s_flags) || + ret = local_open_receiver(conn, _SS_ISNONBLOCK(conn->lc_conn.s_flags) || (flags & MSG_DONTWAIT) != 0); if (ret < 0) { diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c index 31c211415c..0ad0fb6d44 100644 --- a/net/local/local_sendmsg.c +++ b/net/local/local_sendmsg.c @@ -324,7 +324,7 @@ static ssize_t local_sendto(FAR struct socket *psock, /* Open the sending side of the transfer */ ret = local_open_sender(conn, unaddr->sun_path, - _SS_ISNONBLOCK(psock->s_flags) || + _SS_ISNONBLOCK(conn->lc_conn.s_flags) || (flags & MSG_DONTWAIT) != 0); if (ret < 0) { diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index c074ce5e22..e21dd7ddcc 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -515,9 +515,11 @@ static int local_connect(FAR struct socket *psock, #ifdef CONFIG_NET_LOCAL_STREAM case SOCK_STREAM: { + FAR struct socket_conn_s *conn = psock->s_conn; + /* Verify that the socket is not already connected */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->s_flags)) { return -EISCONN; } @@ -797,7 +799,7 @@ static int local_socketpair(FAR struct socket *psocks[2]) goto errout; } - nonblock = _SS_ISNONBLOCK(psocks[0]->s_flags); + nonblock = _SS_ISNONBLOCK(conns[0]->lc_conn.s_flags); /* Open the client-side write-only FIFO. */ diff --git a/net/netlink/netlink_sockif.c b/net/netlink/netlink_sockif.c index e6456c875c..e1351bf4bf 100644 --- a/net/netlink/netlink_sockif.c +++ b/net/netlink/netlink_sockif.c @@ -761,6 +761,7 @@ static ssize_t netlink_recvmsg(FAR struct socket *psock, FAR struct sockaddr *from = msg->msg_name; FAR socklen_t *fromlen = &msg->msg_namelen; FAR struct netlink_response_s *entry; + FAR struct socket_conn_s *conn; DEBUGASSERT(psock != NULL && psock->s_conn != NULL && buf != NULL); DEBUGASSERT(from == NULL || @@ -771,11 +772,13 @@ static ssize_t netlink_recvmsg(FAR struct socket *psock, entry = netlink_tryget_response(psock->s_conn); if (entry == NULL) { + conn = psock->s_conn; + /* No response is variable, but presumably, one is expected. Check * if the socket has been configured for non-blocking operation. */ - if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + if (_SS_ISNONBLOCK(conn->s_flags) || (flags & MSG_DONTWAIT) != 0) { return -EAGAIN; } diff --git a/net/rpmsg/rpmsg_sockif.c b/net/rpmsg/rpmsg_sockif.c index 638a3cd8c5..86f9bcba49 100644 --- a/net/rpmsg/rpmsg_sockif.c +++ b/net/rpmsg/rpmsg_sockif.c @@ -80,6 +80,10 @@ begin_packed_struct struct rpmsg_socket_data_s struct rpmsg_socket_conn_s { + /* Common prologue of all connection structures. */ + + struct socket_conn_s sconn; + struct rpmsg_endpoint ept; struct sockaddr_rpmsg rpaddr; @@ -305,7 +309,7 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, if (conn->psock) { - conn->psock->s_flags |= _SF_CONNECTED; + conn->sconn.s_flags |= _SF_CONNECTED; _SO_SETERRNO(conn->psock, OK); } @@ -649,7 +653,7 @@ static int rpmsg_socket_listen(FAR struct socket *psock, int backlog) return -ENOSYS; } - if (!_SS_ISBOUND(psock->s_flags) || backlog <= 0) + if (!_SS_ISBOUND(server->sconn.s_flags) || backlog <= 0) { return -EINVAL; } @@ -683,7 +687,7 @@ static int rpmsg_socket_connect_internal(FAR struct socket *psock) if (conn->sendsize == 0) { - if (_SS_ISNONBLOCK(psock->s_flags)) + if (_SS_ISNONBLOCK(conn->sconn.s_flags)) { return -EINPROGRESS; } @@ -710,7 +714,7 @@ static int rpmsg_socket_connect(FAR struct socket *psock, FAR struct rpmsg_socket_conn_s *conn = psock->s_conn; int ret; - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { return -EISCONN; } @@ -738,7 +742,7 @@ static int rpmsg_socket_accept(FAR struct socket *psock, return -ECONNRESET; } - if (!_SS_ISLISTENING(psock->s_flags)) + if (!_SS_ISLISTENING(server->sconn.s_flags)) { return -EINVAL; } @@ -782,7 +786,7 @@ static int rpmsg_socket_accept(FAR struct socket *psock, } else { - if (_SS_ISNONBLOCK(psock->s_flags)) + if (_SS_ISNONBLOCK(conn->sconn.s_flags)) { ret = -EAGAIN; break; @@ -839,7 +843,7 @@ static int rpmsg_socket_poll(FAR struct socket *psock, /* Immediately notify on any of the requested events */ - if (_SS_ISLISTENING(psock->s_flags)) + if (_SS_ISLISTENING(conn->sconn.s_flags)) { if (conn->backlog == -1) { @@ -852,7 +856,7 @@ static int rpmsg_socket_poll(FAR struct socket *psock, eventset |= (fds->events & POLLIN); } } - else if (_SS_ISCONNECTED(psock->s_flags)) + else if (_SS_ISCONNECTED(conn->sconn.s_flags)) { if (!conn->ept.rdev) { @@ -878,8 +882,8 @@ static int rpmsg_socket_poll(FAR struct socket *psock, rpmsg_socket_unlock(&conn->recvlock); } - else if (!_SS_ISCONNECTED(psock->s_flags) && - _SS_ISNONBLOCK(psock->s_flags)) + else if (!_SS_ISCONNECTED(conn->sconn.s_flags) && + _SS_ISNONBLOCK(conn->sconn.s_flags)) { ret = OK; } @@ -1121,7 +1125,7 @@ static ssize_t rpmsg_socket_sendmsg(FAR struct socket *psock, bool nonblock; ssize_t ret; - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { if (to == NULL) { @@ -1142,7 +1146,8 @@ static ssize_t rpmsg_socket_sendmsg(FAR struct socket *psock, return -ECONNRESET; } - nonblock = _SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0; + nonblock = _SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0; if (psock->s_type == SOCK_STREAM) { @@ -1164,8 +1169,8 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, FAR struct rpmsg_socket_conn_s *conn = psock->s_conn; ssize_t ret; - if (psock->s_type != SOCK_STREAM && _SS_ISBOUND(psock->s_flags) - && !_SS_ISCONNECTED(psock->s_flags)) + if (psock->s_type != SOCK_STREAM && _SS_ISBOUND(conn->sconn.s_flags) + && !_SS_ISCONNECTED(conn->sconn.s_flags)) { ret = rpmsg_socket_connect_internal(psock); if (ret < 0) @@ -1174,7 +1179,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, } } - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { return -EISCONN; } @@ -1216,7 +1221,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, goto out; } - if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + if (_SS_ISNONBLOCK(conn->sconn.s_flags) || (flags & MSG_DONTWAIT) != 0) { ret = -EAGAIN; goto out; diff --git a/net/sixlowpan/sixlowpan_tcpsend.c b/net/sixlowpan/sixlowpan_tcpsend.c index 202d2f194e..b958908e8d 100644 --- a/net/sixlowpan/sixlowpan_tcpsend.c +++ b/net/sixlowpan/sixlowpan_tcpsend.c @@ -401,11 +401,11 @@ static uint16_t tcp_send_eventhandler(FAR struct net_driver_s *dev, */ DEBUGASSERT(psock != NULL); - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Report the disconnection event to all socket clones */ - tcp_lost_connection(psock, sinfo->s_cb, flags); + tcp_lost_connection(conn, sinfo->s_cb, flags); } /* Report not connected to the sender */ @@ -730,18 +730,18 @@ ssize_t psock_6lowpan_tcp_send(FAR struct socket *psock, FAR const void *buf, return (ssize_t)-EBADF; } + /* Get the underlying TCP connection structure */ + + conn = (FAR struct tcp_conn_s *)psock->s_conn; + /* Make sure that this is a connected TCP socket */ - if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags)) + if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: Not connected\n"); return (ssize_t)-ENOTCONN; } - /* Get the underlying TCP connection structure */ - - conn = (FAR struct tcp_conn_s *)psock->s_conn; - #ifdef CONFIG_NET_IPv4 /* Ignore if not IPv6 domain */ diff --git a/net/sixlowpan/sixlowpan_udpsend.c b/net/sixlowpan/sixlowpan_udpsend.c index 7670242ab2..7d11c04a72 100644 --- a/net/sixlowpan/sixlowpan_udpsend.c +++ b/net/sixlowpan/sixlowpan_udpsend.c @@ -293,8 +293,8 @@ ssize_t psock_6lowpan_udp_sendto(FAR struct socket *psock, */ ret = sixlowpan_send(dev, - &conn->list, - &conn->list_tail, + &conn->sconn.list, + &conn->sconn.list_tail, (FAR const struct ipv6_hdr_s *)&ipv6udp, buf, buflen, &destmac, _SO_TIMEOUT(psock->s_sndtimeo)); @@ -349,19 +349,19 @@ ssize_t psock_6lowpan_udp_send(FAR struct socket *psock, FAR const void *buf, return (ssize_t)-EBADF; } + /* Get the underlying UDP "connection" structure */ + + conn = (FAR struct udp_conn_s *)psock->s_conn; + /* Was the UDP socket connected via connect()? */ - if (psock->s_type != SOCK_DGRAM || !_SS_ISCONNECTED(psock->s_flags)) + if (psock->s_type != SOCK_DGRAM || !_SS_ISCONNECTED(conn->sconn.s_flags)) { /* No, then it is not legal to call send() with this socket. */ return -ENOTCONN; } - /* Get the underlying UDP "connection" structure */ - - conn = (FAR struct udp_conn_s *)psock->s_conn; - /* Ignore if not IPv6 domain */ if (conn->domain != PF_INET6) diff --git a/net/socket/accept.c b/net/socket/accept.c index 2431285704..5c24707069 100644 --- a/net/socket/accept.c +++ b/net/socket/accept.c @@ -113,6 +113,7 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, FAR socklen_t *addrlen, FAR struct socket *newsock) { + FAR struct socket_conn_s *conn; int ret; DEBUGASSERT(psock != NULL && psock->s_conn != NULL && newsock != NULL); @@ -127,7 +128,8 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, /* Is the socket listening for a connection? */ - if (!_SS_ISLISTENING(psock->s_flags)) + conn = psock->s_conn; + if (!_SS_ISLISTENING(conn->s_flags)) { nerr("ERROR: Socket is not listening for a connection.\n"); return -EINVAL; @@ -139,20 +141,19 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, net_lock(); ret = psock->s_sockif->si_accept(psock, addr, addrlen, newsock); - if (ret < 0) + if (ret >= 0) + { + /* Mark the new socket as connected. */ + + conn = newsock->s_conn; + conn->s_flags |= _SF_CONNECTED; + conn->s_flags &= ~_SF_CLOSED; + } + else { nerr("ERROR: si_accept failed: %d\n", ret); - goto errout_with_lock; } - /* Mark the new socket as connected. */ - - newsock->s_flags |= _SF_CONNECTED; - newsock->s_flags &= ~_SF_CLOSED; - - ret = OK; - -errout_with_lock: net_unlock(); return ret; } diff --git a/net/socket/bind.c b/net/socket/bind.c index b029811ec1..f11262c986 100644 --- a/net/socket/bind.c +++ b/net/socket/bind.c @@ -92,16 +92,16 @@ int psock_bind(FAR struct socket *psock, const struct sockaddr *addr, /* Was the bind successful */ - if (ret < 0) + if (ret >= 0) { - return ret; + FAR struct socket_conn_s *conn = psock->s_conn; + + /* Mark the socket bound */ + + conn->s_flags |= _SF_BOUND; } - /* Mark the socket bound */ - - psock->s_flags |= _SF_BOUND; - - return OK; + return ret; } /**************************************************************************** diff --git a/net/socket/connect.c b/net/socket/connect.c index 39d71d3028..8b14821f88 100644 --- a/net/socket/connect.c +++ b/net/socket/connect.c @@ -137,21 +137,21 @@ int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_connect != NULL); ret = psock->s_sockif->si_connect(psock, addr, addrlen); - if (ret < 0) + if (ret >= 0) { - return ret; + FAR struct socket_conn_s *conn = psock->s_conn; + + if (addr != NULL) + { + conn->s_flags |= _SF_CONNECTED; + } + else + { + conn->s_flags &= ~_SF_CONNECTED; + } } - if (addr != NULL) - { - psock->s_flags |= _SF_CONNECTED; - } - else - { - psock->s_flags &= ~_SF_CONNECTED; - } - - return OK; + return ret; } /**************************************************************************** diff --git a/net/socket/getsockopt.c b/net/socket/getsockopt.c index 5d46bd5fdf..34142311e5 100644 --- a/net/socket/getsockopt.c +++ b/net/socket/getsockopt.c @@ -80,6 +80,8 @@ static int psock_socketlevel_option(FAR struct socket *psock, int option, FAR void *value, FAR socklen_t *value_len) { + FAR struct socket_conn_s *conn = psock->s_conn; + /* Verify that the socket option if valid (but might not be supported ) */ if (!_SO_GETVALID(option) || !value || !value_len) @@ -134,11 +136,11 @@ static int psock_socketlevel_option(FAR struct socket *psock, int option, { if (option == SO_TYPE) { - FAR struct usrsock_conn_s *conn = psock->s_conn; + FAR struct usrsock_conn_s *uconn = psock->s_conn; /* Return the actual socket type */ - *(FAR int *)value = conn->type; + *(FAR int *)value = uconn->type; *value_len = sizeof(int); return OK; @@ -156,7 +158,7 @@ static int psock_socketlevel_option(FAR struct socket *psock, int option, return -EINVAL; } - *(FAR int *)value = _SS_ISLISTENING(psock->s_flags); + *(FAR int *)value = _SS_ISLISTENING(conn->s_flags); *value_len = sizeof(int); break; diff --git a/net/socket/listen.c b/net/socket/listen.c index 83f88a7cd3..69b75fc1a6 100644 --- a/net/socket/listen.c +++ b/net/socket/listen.c @@ -84,14 +84,17 @@ int psock_listen(FAR struct socket *psock, int backlog) DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_listen != NULL); ret = psock->s_sockif->si_listen(psock, backlog); - if (ret < 0) + if (ret >= 0) + { + FAR struct socket_conn_s *conn = psock->s_conn; + conn->s_flags |= _SF_LISTENING; + } + else { nerr("ERROR: si_listen failed: %d\n", ret); - return ret; } - psock->s_flags |= _SF_LISTENING; - return OK; + return ret; } /**************************************************************************** diff --git a/net/socket/net_close.c b/net/socket/net_close.c index 5b47dcb64b..4ffc56b5c5 100644 --- a/net/socket/net_close.c +++ b/net/socket/net_close.c @@ -81,6 +81,8 @@ int psock_close(FAR struct socket *psock) if (psock->s_conn != NULL) { + FAR struct socket_conn_s *conn = psock->s_conn; + /* Assume that the socket close operation will be successful. Save * the current flags and mark the socket uninitialized. This avoids * race conditions in the SMP case. We save the flags as a type @@ -88,9 +90,9 @@ int psock_close(FAR struct socket *psock) * (currently uint8_t). */ - unsigned int saveflags = psock->s_flags; + unsigned int saveflags = conn->s_flags; - psock->s_flags &= ~_SF_INITD; + conn->s_flags &= ~_SF_INITD; /* Let the address family's close() method handle the operation */ @@ -105,7 +107,7 @@ int psock_close(FAR struct socket *psock) { /* No.. restore the socket flags */ - psock->s_flags = saveflags; + conn->s_flags = saveflags; return ret; } } diff --git a/net/socket/net_dup2.c b/net/socket/net_dup2.c index 2482936105..0bbbc1aa38 100644 --- a/net/socket/net_dup2.c +++ b/net/socket/net_dup2.c @@ -77,7 +77,6 @@ int psock_dup2(FAR struct socket *psock1, FAR struct socket *psock2) psock2->s_domain = psock1->s_domain; /* IP domain: PF_INET, PF_INET6, or PF_PACKET */ psock2->s_type = psock1->s_type; /* Protocol type: Only SOCK_STREAM or SOCK_DGRAM */ psock2->s_sockif = psock1->s_sockif; /* Socket interface */ - psock2->s_flags = psock1->s_flags; /* See _SF_* definitions */ #ifdef CONFIG_NET_SOCKOPTS psock2->s_options = psock1->s_options; /* Selected socket options */ psock2->s_rcvtimeo = psock1->s_rcvtimeo; /* Receive timeout value (in deciseconds) */ diff --git a/net/socket/net_fstat.c b/net/socket/net_fstat.c index 7c75b8545c..7a26a5e3e9 100644 --- a/net/socket/net_fstat.c +++ b/net/socket/net_fstat.c @@ -58,6 +58,7 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf) { + FAR struct socket_conn_s *conn; int ret = OK; if (psock == NULL) @@ -82,7 +83,9 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf) * devices, each supporting a different MSS. */ - if (psock->s_conn == NULL || !_SS_ISCONNECTED(psock->s_flags)) + conn = psock->s_conn; + + if (conn == NULL || !_SS_ISCONNECTED(conn->s_flags)) { /* Not connected.. Return an optimal blocksize of zero (or, perhaps, * even an error?) @@ -105,14 +108,14 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf) #if defined(NET_TCP_HAVE_STACK) case SOCK_STREAM: { - FAR struct tcp_conn_s *conn = + FAR struct tcp_conn_s *tcp_conn = (FAR struct tcp_conn_s *)psock->s_conn; /* For TCP, the MSS is a dynamic value that maintained in the * connection structure. */ - buf->st_blksize = conn->mss; + buf->st_blksize = tcp_conn->mss; } break; #endif @@ -120,7 +123,7 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf) #if defined(NET_UDP_HAVE_STACK) case SOCK_DGRAM: { - FAR struct udp_conn_s *conn = + FAR struct udp_conn_s *udp_conn = (FAR struct udp_conn_s *)psock->s_conn; FAR struct net_driver_s *dev; uint16_t iplen; @@ -133,7 +136,7 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf) * order to get the MTU and LL_HDRLEN: */ - dev = udp_find_raddr_device(conn, NULL); + dev = udp_find_raddr_device(udp_conn, NULL); if (dev == NULL) { /* This should never happen except perhaps in some rare race @@ -150,7 +153,8 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf) /* We need the length of the IP header */ #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) - iplen = (conn->domain == PF_INET) ? IPv4_HDRLEN : IPv6_HDRLEN; + iplen = (udp_conn->domain == PF_INET) ? IPv4_HDRLEN : + IPv6_HDRLEN; #elif defined(CONFIG_NET_IPv4) iplen = IPv4_HDRLEN; #else diff --git a/net/socket/net_vfcntl.c b/net/socket/net_vfcntl.c index 9e5ff4884e..d8610c302a 100644 --- a/net/socket/net_vfcntl.c +++ b/net/socket/net_vfcntl.c @@ -60,6 +60,7 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap) { + FAR struct socket_conn_s *conn; int ret = -EINVAL; ninfo("sockfd=%p cmd=%d\n", psock, cmd); @@ -71,6 +72,8 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap) return -EBADF; } + conn = psock->s_conn; + /* Interrupts must be disabled in order to perform operations on socket * structures */ @@ -104,7 +107,7 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap) sockcaps = psock->s_sockif->si_sockcaps(psock); if ((sockcaps & SOCKCAP_NONBLOCKING) != 0 && - _SS_ISNONBLOCK(psock->s_flags)) + _SS_ISNONBLOCK(conn->s_flags)) { ret |= O_NONBLOCK; } @@ -138,11 +141,11 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap) { if ((mode & O_NONBLOCK) != 0) { - psock->s_flags |= _SF_NONBLOCK; + conn->s_flags |= _SF_NONBLOCK; } else { - psock->s_flags &= ~_SF_NONBLOCK; + conn->s_flags &= ~_SF_NONBLOCK; } ret = OK; diff --git a/net/socket/socket.c b/net/socket/socket.c index 01117e8191..61eab6a638 100644 --- a/net/socket/socket.c +++ b/net/socket/socket.c @@ -87,14 +87,7 @@ int psock_socket(int domain, int type, int protocol, psock->s_domain = domain; psock->s_proto = protocol; psock->s_conn = NULL; - - if (type & SOCK_NONBLOCK) - { - psock->s_flags |= _SF_NONBLOCK; - } - - type &= SOCK_TYPE_MASK; - psock->s_type = type; + psock->s_type = type & SOCK_TYPE_MASK; #ifdef CONFIG_NET_USRSOCK if (domain != PF_LOCAL && domain != PF_UNSPEC && domain != PF_RPMSG) @@ -111,7 +104,7 @@ int psock_socket(int domain, int type, int protocol, { /* Get the socket interface */ - sockif = net_sockif(domain, type, protocol); + sockif = net_sockif(domain, psock->s_type, protocol); if (sockif == NULL) { nerr("ERROR: socket address family unsupported: %d\n", domain); @@ -126,16 +119,26 @@ int psock_socket(int domain, int type, int protocol, psock->s_sockif = sockif; ret = sockif->si_setup(psock, protocol); - if (ret < 0) - { - nerr("ERROR: socket si_setup() failed: %d\n", ret); - return ret; - } } - /* The socket has been successfully initialized */ + if (ret >= 0) + { + FAR struct socket_conn_s *conn = psock->s_conn; + + if (type & SOCK_NONBLOCK) + { + conn->s_flags |= _SF_NONBLOCK; + } + + /* The socket has been successfully initialized */ + + conn->s_flags |= _SF_INITD; + } + else + { + nerr("ERROR: socket si_setup() failed: %d\n", ret); + } - psock->s_flags |= _SF_INITD; return ret; } diff --git a/net/tcp/tcp_accept.c b/net/tcp/tcp_accept.c index 2a0c6e47f0..3da94d0b6d 100644 --- a/net/tcp/tcp_accept.c +++ b/net/tcp/tcp_accept.c @@ -242,7 +242,7 @@ int psock_tcp_accept(FAR struct socket *psock, FAR struct sockaddr *addr, * return EAGAIN if there is no pending connection in the backlog. */ - else if (_SS_ISNONBLOCK(psock->s_flags)) + else if (_SS_ISNONBLOCK(conn->sconn.s_flags)) { return -EAGAIN; } diff --git a/net/tcp/tcp_connect.c b/net/tcp/tcp_connect.c index d72f3660bc..8e9122ddc6 100644 --- a/net/tcp/tcp_connect.c +++ b/net/tcp/tcp_connect.c @@ -219,15 +219,18 @@ static uint16_t psock_connect_eventhandler(FAR struct net_driver_s *dev, else if ((flags & TCP_CONNECTED) != 0) { FAR struct socket *psock = pstate->tc_psock; + FAR struct socket_conn_s *conn; DEBUGASSERT(psock); + conn = psock->s_conn; + /* Mark the connection bound and connected. NOTE this is * is done here (vs. later) in order to avoid any race condition * in the socket state. It is known to connected here and now, * but not necessarily at any time later. */ - psock->s_flags |= (_SF_BOUND | _SF_CONNECTED); + conn->s_flags |= (_SF_BOUND | _SF_CONNECTED); /* Indicate that the socket is no longer connected */ @@ -293,8 +296,9 @@ static uint16_t psock_connect_eventhandler(FAR struct net_driver_s *dev, int psock_tcp_connect(FAR struct socket *psock, FAR const struct sockaddr *addr) { - struct tcp_connect_s state; - int ret = OK; + FAR struct tcp_conn_s *conn; + struct tcp_connect_s state; + int ret = OK; /* Interrupts must be disabled through all of the following because * we cannot allow the network callback to occur until we are completely @@ -303,9 +307,11 @@ int psock_tcp_connect(FAR struct socket *psock, net_lock(); + conn = psock->s_conn; + /* Get the connection reference from the socket */ - if (!psock->s_conn) /* Should always be non-NULL */ + if (conn == NULL) /* Should always be non-NULL */ { ret = -EINVAL; } @@ -313,20 +319,20 @@ int psock_tcp_connect(FAR struct socket *psock, { /* Perform the TCP connection operation */ - ret = tcp_connect(psock->s_conn, addr); + ret = tcp_connect(conn, addr); } if (ret >= 0) { /* Notify the device driver that new connection is available. */ - netdev_txnotify_dev(((FAR struct tcp_conn_s *)psock->s_conn)->dev); + netdev_txnotify_dev(conn->dev); /* Non-blocking connection ? set the socket error * and start the monitor */ - if (_SS_ISNONBLOCK(psock->s_flags)) + if (_SS_ISNONBLOCK(conn->sconn.s_flags)) { ret = -EINPROGRESS; } @@ -385,7 +391,7 @@ int psock_tcp_connect(FAR struct socket *psock, * happen in this context, but just in case... */ - tcp_stop_monitor(psock->s_conn, TCP_ABORT); + tcp_stop_monitor(conn, TCP_ABORT); ret = ret2; } } diff --git a/net/tcp/tcp_monitor.c b/net/tcp/tcp_monitor.c index 04a1e7b12c..5e4eb6f10f 100644 --- a/net/tcp/tcp_monitor.c +++ b/net/tcp/tcp_monitor.c @@ -69,6 +69,8 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev, static void tcp_close_connection(FAR struct socket *psock, uint16_t flags) { + FAR struct socket_conn_s *conn = psock->s_conn; + /* These loss-of-connection events may be reported: * * TCP_CLOSE: The remote host has closed the connection @@ -92,8 +94,8 @@ static void tcp_close_connection(FAR struct socket *psock, uint16_t flags) * not handle as an error but as an "end-of-file" */ - psock->s_flags &= ~_SF_CONNECTED; - psock->s_flags |= _SF_CLOSED; + conn->s_flags &= ~_SF_CONNECTED; + conn->s_flags |= _SF_CLOSED; } else if ((flags & (TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN)) != 0) { @@ -101,7 +103,7 @@ static void tcp_close_connection(FAR struct socket *psock, uint16_t flags) * (eventually) be reported as an ENOTCONN error. */ - psock->s_flags &= ~(_SF_CONNECTED | _SF_CLOSED); + conn->s_flags &= ~(_SF_CONNECTED | _SF_CLOSED); } } @@ -129,10 +131,11 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev, uint16_t flags) { FAR struct socket *psock = (FAR struct socket *)pvpriv; + FAR struct socket_conn_s *conn = psock->s_conn; if (psock != NULL) { - ninfo("flags: %04x s_flags: %02x\n", flags, psock->s_flags); + ninfo("flags: %04x s_flags: %02x\n", flags, conn->s_flags); /* TCP_DISCONN_EVENTS: TCP_CLOSE, TCP_ABORT, TCP_TIMEDOUT, or * NETDEV_DOWN. All loss-of-connection events. @@ -170,8 +173,8 @@ static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev, /* Indicate that the socket is now connected */ - psock->s_flags |= (_SF_BOUND | _SF_CONNECTED); - psock->s_flags &= ~_SF_CLOSED; + sconn->s_flags |= (_SF_BOUND | _SF_CONNECTED); + sconn->s_flags &= ~_SF_CLOSED; } } @@ -259,7 +262,7 @@ int tcp_start_monitor(FAR struct socket *psock) /* Non-blocking connection ? */ nonblock_conn = (conn->tcpstateflags == TCP_SYN_SENT && - _SS_ISNONBLOCK(psock->s_flags)); + _SS_ISNONBLOCK(conn->sconn.s_flags)); /* Check if the connection has already been closed before any callbacks * have been registered. (Maybe the connection is lost before accept has diff --git a/net/tcp/tcp_netpoll.c b/net/tcp/tcp_netpoll.c index 4d64797df3..9326aa394b 100644 --- a/net/tcp/tcp_netpoll.c +++ b/net/tcp/tcp_netpoll.c @@ -217,7 +217,7 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds) /* Non-blocking connection ? */ nonblock_conn = (conn->tcpstateflags == TCP_SYN_SENT && - _SS_ISNONBLOCK(psock->s_flags)); + _SS_ISNONBLOCK(conn->sconn.s_flags)); /* Find a container to hold the poll information */ @@ -331,8 +331,8 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds) * Action: Return with POLLHUP|POLLERR events */ - if (!nonblock_conn && !_SS_ISCONNECTED(psock->s_flags) && - !_SS_ISLISTENING(psock->s_flags)) + if (!nonblock_conn && !_SS_ISCONNECTED(conn->sconn.s_flags) && + !_SS_ISLISTENING(conn->sconn.s_flags)) { /* We were previously connected but lost the connection either due * to a graceful shutdown by the remote peer or because of some @@ -341,7 +341,8 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds) fds->revents |= (POLLERR | POLLHUP); } - else if (_SS_ISCONNECTED(psock->s_flags) && psock_tcp_cansend(psock) >= 0) + else if (_SS_ISCONNECTED(conn->sconn.s_flags) && + psock_tcp_cansend(psock) >= 0) { fds->revents |= (POLLWRNORM & fds->events); } diff --git a/net/tcp/tcp_recvfrom.c b/net/tcp/tcp_recvfrom.c index ab8bb077ad..ccc148b991 100644 --- a/net/tcp/tcp_recvfrom.c +++ b/net/tcp/tcp_recvfrom.c @@ -462,6 +462,7 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev, else if ((flags & TCP_DISCONN_EVENTS) != 0) { FAR struct socket *psock = pstate->ir_sock; + FAR struct socket_conn_s *conn; nwarn("WARNING: Lost connection\n"); @@ -471,7 +472,8 @@ static uint16_t tcp_recvhandler(FAR struct net_driver_s *dev, */ DEBUGASSERT(psock != NULL); - if (_SS_ISCONNECTED(psock->s_flags)) + conn = psock->s_conn; + if (_SS_ISCONNECTED(conn->s_flags)) { /* Handle loss-of-connection event */ @@ -660,7 +662,7 @@ ssize_t psock_tcp_recvfrom(FAR struct socket *psock, FAR void *buf, /* Verify that the SOCK_STREAM has been and still is connected */ - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Was any data transferred from the readahead buffer after we were * disconnected? If so, then return the number of bytes received. We @@ -673,7 +675,7 @@ ssize_t psock_tcp_recvfrom(FAR struct socket *psock, FAR void *buf, * recvfrom() will get an end-of-file indication. */ - if (ret <= 0 && !_SS_ISCLOSED(psock->s_flags)) + if (ret <= 0 && !_SS_ISCLOSED(conn->sconn.s_flags)) { /* Nothing was previously received from the read-ahead buffers. * The SOCK_STREAM must be (re-)connected in order to receive any @@ -690,7 +692,8 @@ ssize_t psock_tcp_recvfrom(FAR struct socket *psock, FAR void *buf, * return EAGAIN if no data was obtained from the read-ahead buffers. */ - else if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + else if (_SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0) { /* Return the number of bytes read from the read-ahead buffer if * something was received (already in 'ret'); EAGAIN if not. diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c index a3fb67d3e0..0266911f4d 100644 --- a/net/tcp/tcp_send_buffered.c +++ b/net/tcp/tcp_send_buffered.c @@ -410,7 +410,7 @@ static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev, * already been disconnected. */ - if (psock->s_conn != NULL && _SS_ISCONNECTED(psock->s_flags)) + if (psock->s_conn != NULL && _SS_ISCONNECTED(conn->sconn.s_flags)) { /* Report not connected */ @@ -1069,7 +1069,9 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, goto errout; } - if (!_SS_ISCONNECTED(psock->s_flags)) + conn = (FAR struct tcp_conn_s *)psock->s_conn; + + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: Not connected\n"); ret = -ENOTCONN; @@ -1078,8 +1080,6 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, /* Make sure that we have the IP address mapping */ - conn = (FAR struct tcp_conn_s *)psock->s_conn; - #if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR) #ifdef CONFIG_NET_ARP_SEND #ifdef CONFIG_NET_ICMPv6_NEIGHBOR @@ -1113,7 +1113,8 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, } #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */ - nonblock = _SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0; + nonblock = _SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0; /* Dump the incoming buffer */ @@ -1133,7 +1134,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, * state again to ensure the connection is still valid. */ - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: No longer connected\n"); ret = -ENOTCONN; @@ -1420,6 +1421,8 @@ errout: int psock_tcp_cansend(FAR struct socket *psock) { + FAR struct socket_conn_s *conn; + /* Verify that we received a valid socket */ if (!psock || !psock->s_conn) @@ -1428,9 +1431,11 @@ int psock_tcp_cansend(FAR struct socket *psock) return -EBADF; } + conn = psock->s_conn; + /* Verify that this is connected TCP socket */ - if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags)) + if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(conn->s_flags)) { nerr("ERROR: Not connected\n"); return -ENOTCONN; diff --git a/net/tcp/tcp_send_unbuffered.c b/net/tcp/tcp_send_unbuffered.c index 8145ae5153..b123d5fcfc 100644 --- a/net/tcp/tcp_send_unbuffered.c +++ b/net/tcp/tcp_send_unbuffered.c @@ -370,7 +370,7 @@ static uint16_t tcpsend_eventhandler(FAR struct net_driver_s *dev, * already been disconnected. */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Report not connected */ @@ -545,12 +545,14 @@ ssize_t psock_tcp_send(FAR struct socket *psock, goto errout; } + conn = (FAR struct tcp_conn_s *)psock->s_conn; + /* Check early if this is an un-connected socket, if so, then * return -ENOTCONN. Note, we will have to check this again, as we can't * guarantee the state won't change until we have the network locked. */ - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: Not connected\n"); ret = -ENOTCONN; @@ -559,8 +561,6 @@ ssize_t psock_tcp_send(FAR struct socket *psock, /* Make sure that we have the IP address mapping */ - conn = (FAR struct tcp_conn_s *)psock->s_conn; - #if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR) #ifdef CONFIG_NET_ARP_SEND #ifdef CONFIG_NET_ICMPv6_NEIGHBOR @@ -607,7 +607,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, * state again to ensure the connection is still valid. */ - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: No longer connected\n"); net_unlock(); diff --git a/net/tcp/tcp_sendfile.c b/net/tcp/tcp_sendfile.c index c8f1bcf176..4a4463f68e 100644 --- a/net/tcp/tcp_sendfile.c +++ b/net/tcp/tcp_sendfile.c @@ -329,7 +329,7 @@ static uint16_t sendfile_eventhandler(FAR struct net_driver_s *dev, * already been disconnected. */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Report not connected */ @@ -457,9 +457,12 @@ ssize_t tcp_sendfile(FAR struct socket *psock, FAR struct file *infile, off_t startpos; int ret; + conn = psock->s_conn; + DEBUGASSERT(conn != NULL); + /* If this is an un-connected socket, then return ENOTCONN */ - if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags)) + if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: Not connected\n"); return -ENOTCONN; @@ -467,9 +470,6 @@ ssize_t tcp_sendfile(FAR struct socket *psock, FAR struct file *infile, /* Make sure that we have the IP address mapping */ - conn = (FAR struct tcp_conn_s *)psock->s_conn; - DEBUGASSERT(conn != NULL); - #if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR) #ifdef CONFIG_NET_ARP_SEND #ifdef CONFIG_NET_ICMPv6_NEIGHBOR diff --git a/net/udp/udp_recvfrom.c b/net/udp/udp_recvfrom.c index 7abc2a7e7f..a17d495d27 100644 --- a/net/udp/udp_recvfrom.c +++ b/net/udp/udp_recvfrom.c @@ -627,7 +627,7 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR void *buf, /* Handle non-blocking UDP sockets */ - if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + if (_SS_ISNONBLOCK(conn->sconn.s_flags) || (flags & MSG_DONTWAIT) != 0) { /* Return the number of bytes read from the read-ahead buffer if * something was received (already in 'ret'); EAGAIN if not. diff --git a/net/udp/udp_sendto_buffered.c b/net/udp/udp_sendto_buffered.c index e45b4e2b6c..07adf714c8 100644 --- a/net/udp/udp_sendto_buffered.c +++ b/net/udp/udp_sendto_buffered.c @@ -539,19 +539,24 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, size_t len, int flags, FAR const struct sockaddr *to, socklen_t tolen) { - FAR struct udp_conn_s *conn; FAR struct udp_wrbuffer_s *wrb; + FAR struct udp_conn_s *conn; bool nonblock; bool empty; int ret = OK; + /* Get the underlying the UDP connection structure. */ + + conn = psock->s_conn; + DEBUGASSERT(conn); + /* If the UDP socket was previously assigned a remote peer address via * connect(), then as with connection-mode socket, sendto() may not be * used with a non-NULL destination address. Normally send() would be * used with such connected UDP sockets. */ - if (to != NULL && _SS_ISCONNECTED(psock->s_flags)) + if (to != NULL && _SS_ISCONNECTED(conn->sconn.s_flags)) { /* EISCONN - A destination address was specified and the socket is * already connected. @@ -564,7 +569,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, * must be provided. */ - else if (to == NULL && !_SS_ISCONNECTED(psock->s_flags)) + else if (to == NULL && !_SS_ISCONNECTED(conn->sconn.s_flags)) { /* EDESTADDRREQ - The socket is not connection-mode and no peer * address is set. @@ -573,11 +578,6 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, return -EDESTADDRREQ; } - /* Get the underlying the UDP connection structure. */ - - conn = (FAR struct udp_conn_s *)psock->s_conn; - DEBUGASSERT(conn); - #if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR) #ifdef CONFIG_NET_ARP_SEND /* Assure the the IPv4 destination address maps to a valid MAC address in @@ -592,7 +592,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, /* Check if the socket is connection mode */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Yes.. use the connected remote address (the 'to' address is * null). @@ -631,7 +631,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, /* Check if the socket is connection mode */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Yes.. use the connected remote address (the 'to' address is * null). @@ -666,7 +666,8 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, } #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */ - nonblock = _SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0; + nonblock = _SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0; /* Dump the incoming buffer */ @@ -720,7 +721,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, * Check if the socket is connected */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Yes.. get the connection address from the connection structure */ diff --git a/net/udp/udp_sendto_unbuffered.c b/net/udp/udp_sendto_unbuffered.c index 12d5f81ced..5760406273 100644 --- a/net/udp/udp_sendto_unbuffered.c +++ b/net/udp/udp_sendto_unbuffered.c @@ -264,13 +264,24 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, struct sendto_s state; int ret; + /* Verify that the sockfd corresponds to valid, allocated socket */ + + if (psock == NULL || psock->s_type != SOCK_DGRAM || + psock->s_conn == NULL) + { + nerr("ERROR: Invalid socket\n"); + return -EBADF; + } + /* If the UDP socket was previously assigned a remote peer address via * connect(), then as with connection-mode socket, sendto() may not be * used with a non-NULL destination address. Normally send() would be * used with such connected UDP sockets. */ - if (to != NULL && _SS_ISCONNECTED(psock->s_flags)) + conn = psock->s_conn; + + if (to != NULL && _SS_ISCONNECTED(conn->sconn.s_flags)) { /* EISCONN - A destination address was specified and the socket is * already connected. @@ -283,7 +294,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, * must be provided. */ - else if (to == NULL && !_SS_ISCONNECTED(psock->s_flags)) + else if (to == NULL && !_SS_ISCONNECTED(conn->sconn.s_flags)) { /* EDESTADDRREQ - The socket is not connection-mode and no peer\ * address is set. @@ -292,11 +303,6 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, return -EDESTADDRREQ; } - /* Get the underlying the UDP connection structure. */ - - conn = (FAR struct udp_conn_s *)psock->s_conn; - DEBUGASSERT(conn); - #if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR) #ifdef CONFIG_NET_ARP_SEND /* Assure the the IPv4 destination address maps to a valid MAC address in @@ -311,7 +317,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, /* Check if the socket is connection mode */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Yes.. use the connected remote address (the 'to' address is * null). @@ -350,7 +356,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, /* Check if the socket is connection mode */ - if (_SS_ISCONNECTED(psock->s_flags)) + if (_SS_ISCONNECTED(conn->sconn.s_flags)) { /* Yes.. use the connected remote address (the 'to' address is * null). @@ -413,7 +419,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf, /* Check if the socket is connected */ - if (!_SS_ISCONNECTED(psock->s_flags)) + if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { /* No.. Call udp_connect() to set the remote address in the connection * structure to the sendto() destination address. diff --git a/net/usrsock/usrsock_accept.c b/net/usrsock/usrsock_accept.c index 9133f0a776..cfaa61fcca 100644 --- a/net/usrsock/usrsock_accept.c +++ b/net/usrsock/usrsock_accept.c @@ -304,7 +304,7 @@ int usrsock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, if (!(conn->flags & USRSOCK_EVENT_RECVFROM_AVAIL)) { - if (_SS_ISNONBLOCK(psock->s_flags)) + if (_SS_ISNONBLOCK(conn->sconn.s_flags)) { /* Nothing to receive from daemon side. */ diff --git a/net/usrsock/usrsock_connect.c b/net/usrsock/usrsock_connect.c index e96384a12f..1a671b66a5 100644 --- a/net/usrsock/usrsock_connect.c +++ b/net/usrsock/usrsock_connect.c @@ -214,7 +214,7 @@ int usrsock_connect(FAR struct socket *psock, /* Do not block on waiting for request completion if nonblocking socket. */ - if (!conn->resp.inprogress || !_SS_ISNONBLOCK(psock->s_flags)) + if (!conn->resp.inprogress || !_SS_ISNONBLOCK(conn->sconn.s_flags)) { /* Wait for completion of request (or signal). */ diff --git a/net/usrsock/usrsock_recvmsg.c b/net/usrsock/usrsock_recvmsg.c index 980e349330..c6e08ced5d 100644 --- a/net/usrsock/usrsock_recvmsg.c +++ b/net/usrsock/usrsock_recvmsg.c @@ -302,7 +302,8 @@ ssize_t usrsock_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, if (!(conn->flags & USRSOCK_EVENT_RECVFROM_AVAIL)) { - if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + if (_SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0) { /* Nothing to receive from daemon side. */ diff --git a/net/usrsock/usrsock_sendmsg.c b/net/usrsock/usrsock_sendmsg.c index 332817e38a..695dc6465e 100644 --- a/net/usrsock/usrsock_sendmsg.c +++ b/net/usrsock/usrsock_sendmsg.c @@ -283,7 +283,8 @@ ssize_t usrsock_sendmsg(FAR struct socket *psock, if (!(conn->flags & USRSOCK_EVENT_SENDTO_READY)) { - if (_SS_ISNONBLOCK(psock->s_flags) || (flags & MSG_DONTWAIT) != 0) + if (_SS_ISNONBLOCK(conn->sconn.s_flags) || + (flags & MSG_DONTWAIT) != 0) { /* Send busy at daemon side. */