summaryrefslogtreecommitdiff
path: root/nuttx/net/socket
diff options
context:
space:
mode:
Diffstat (limited to 'nuttx/net/socket')
-rw-r--r--nuttx/net/socket/net_poll.c340
-rw-r--r--nuttx/net/socket/recvfrom.c153
2 files changed, 461 insertions, 32 deletions
diff --git a/nuttx/net/socket/net_poll.c b/nuttx/net/socket/net_poll.c
index 37f990bd2..d2cacf13a 100644
--- a/nuttx/net/socket/net_poll.c
+++ b/nuttx/net/socket/net_poll.c
@@ -1,7 +1,7 @@
/****************************************************************************
* net/socket/net_poll.c
*
- * Copyright (C) 2008-2009, 2011-2014 Gregory Nutt. All rights reserved.
+ * Copyright (C) 2008-2009, 2011-2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -56,6 +56,7 @@
#include <devif/devif.h>
#include "tcp/tcp.h"
+#include "udp/udp.h"
#include "socket/socket.h"
/****************************************************************************
@@ -91,7 +92,7 @@ struct net_poll_s
****************************************************************************/
/****************************************************************************
- * Function: poll_interrupt
+ * Function: tcp_poll_interrupt
*
* Description:
* This function is called from the interrupt level to perform the actual
@@ -111,8 +112,8 @@ struct net_poll_s
****************************************************************************/
#ifdef HAVE_NETPOLL
-static uint16_t poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn,
- FAR void *pvpriv, uint16_t flags)
+static uint16_t tcp_poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn,
+ FAR void *pvpriv, uint16_t flags)
{
FAR struct net_poll_s *info = (FAR struct net_poll_s *)pvpriv;
@@ -164,13 +165,76 @@ static uint16_t poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn,
#endif /* HAVE_NETPOLL */
/****************************************************************************
- * Function: net_pollsetup
+ * Function: udp_poll_interrupt
+ *
+ * Description:
+ * This function is called from the interrupt level to perform the actual
+ * UDP receive operation via by the device interface layer.
+ *
+ * Parameters:
+ * dev The structure of the network driver that caused the interrupt
+ * conn The connection structure associated with the socket
+ * flags Set of events describing why the callback was invoked
+ *
+ * Returned Value:
+ * None
+ *
+ * Assumptions:
+ * Running at the interrupt level
+ *
+ ****************************************************************************/
+
+#ifdef HAVE_NETPOLL
+static uint16_t udp_poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn,
+ FAR void *pvpriv, uint16_t flags)
+{
+ FAR struct net_poll_s *info = (FAR struct net_poll_s *)pvpriv;
+
+ nllvdbg("flags: %04x\n", flags);
+
+ DEBUGASSERT(!info || (info->psock && info->fds));
+
+ /* 'priv' might be null in some race conditions (?) */
+
+ if (info)
+ {
+ pollevent_t eventset = 0;
+
+ /* Check for data or connection availability events. */
+
+ if ((flags & (UDP_NEWDATA)) != 0)
+ {
+ eventset |= (POLLIN & info->fds->events);
+ }
+
+ /* A poll is a sign that we are free to send data. */
+
+ if ((flags & UDP_POLL) != 0)
+ {
+ eventset |= (POLLOUT & info->fds->events);
+ }
+
+ /* Awaken the caller of poll() is requested event occurred. */
+
+ if (eventset)
+ {
+ info->fds->revents |= eventset;
+ sem_post(info->fds->sem);
+ }
+ }
+
+ return flags;
+}
+#endif /* HAVE_NETPOLL */
+
+/****************************************************************************
+ * Function: tcp_pollsetup
*
* Description:
* Setup to monitor events on one TCP/IP socket
*
* Input Parameters:
- * conn - The TCP/IP connection of interest
+ * psock - The TCP/IP socket of interest
* fds - The structure describing the events to be monitored, OR NULL if
* this is a request to stop monitoring events.
*
@@ -180,7 +244,7 @@ static uint16_t poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn,
****************************************************************************/
#ifdef HAVE_NETPOLL
-static inline int net_pollsetup(FAR struct socket *psock,
+static inline int tcp_pollsetup(FAR struct socket *psock,
FAR struct pollfd *fds)
{
FAR struct tcp_conn_s *conn = psock->s_conn;
@@ -233,7 +297,7 @@ static inline int net_pollsetup(FAR struct socket *psock,
cb->flags = (TCP_NEWDATA | TCP_BACKLOG | TCP_POLL | TCP_CLOSE |
TCP_ABORT | TCP_TIMEDOUT);
cb->priv = (FAR void *)info;
- cb->event = poll_interrupt;
+ cb->event = tcp_poll_interrupt;
/* Save the reference in the poll info structure as fds private as well
* for use durring poll teardown as well.
@@ -323,16 +387,169 @@ errout_with_lock:
net_unlock(flags);
return ret;
}
+
+/****************************************************************************
+ * Function: udp_pollsetup
+ *
+ * Description:
+ * Setup to monitor events on one UDP/IP socket
+ *
+ * Input Parameters:
+ * psock - The UDP/IP socket of interest
+ * fds - The structure describing the events to be monitored, OR NULL if
+ * this is a request to stop monitoring events.
+ *
+ * Returned Value:
+ * 0: Success; Negated errno on failure
+ *
+ ****************************************************************************/
+
+static inline int udp_pollsetup(FAR struct socket *psock,
+ FAR struct pollfd *fds)
+{
+ FAR struct udp_conn_s *conn = psock->s_conn;
+ FAR struct net_poll_s *info;
+ FAR struct devif_callback_s *cb;
+ net_lock_t flags;
+ int ret;
+
+ /* Sanity check */
+
+#ifdef CONFIG_DEBUG
+ if (!conn || !fds)
+ {
+ return -EINVAL;
+ }
+#endif
+
+ /* Allocate a container to hold the poll information */
+
+ info = (FAR struct net_poll_s *)kmm_malloc(sizeof(struct net_poll_s));
+ if (!info)
+ {
+ return -ENOMEM;
+ }
+
+ /* Some of the following must be atomic */
+
+ flags = net_lock();
+
+ /* Setup the UDP remote connection */
+
+ ret = udp_connect(conn, NULL);
+ if (ret)
+ {
+ goto errout_with_lock;
+ }
+
+ /* Allocate a TCP/IP callback structure */
+
+ cb = udp_callback_alloc(conn);
+ if (!cb)
+ {
+ ret = -EBUSY;
+ goto errout_with_lock;
+ }
+
+ /* Initialize the poll info container */
+
+ info->psock = psock;
+ info->fds = fds;
+ info->cb = cb;
+
+ /* Initialize the callback structure. Save the reference to the info
+ * structure as callback private data so that it will be available during
+ * callback processing.
+ */
+
+ cb->flags = (0);
+ cb->priv = (FAR void *)info;
+ cb->event = udp_poll_interrupt;
+
+ if (info->fds->events & POLLOUT)
+ cb->flags |= UDP_POLL;
+ if (info->fds->events & POLLIN)
+ cb->flags |= UDP_NEWDATA;
+
+ /* Save the reference in the poll info structure as fds private as well
+ * for use durring poll teardown as well.
+ */
+
+ fds->priv = (FAR void *)info;
+
+ /* Check for read data availability now */
+
+ if (!IOB_QEMPTY(&conn->readahead))
+ {
+ /* Normal data may be read without blocking. */
+
+ fds->revents |= (POLLRDNORM & fds->events);
+ }
+
+ /* Check if any requested events are already in effect */
+
+ if (fds->revents != 0)
+ {
+ /* Yes.. then signal the poll logic */
+ sem_post(fds->sem);
+ }
+
+ net_unlock(flags);
+ return OK;
+
+errout_with_lock:
+ kmm_free(info);
+ net_unlock(flags);
+ return ret;
+}
+
+/****************************************************************************
+ * Function: net_pollsetup
+ *
+ * Description:
+ * Setup to monitor events on one socket
+ *
+ * Input Parameters:
+ * psock - The socket of interest
+ * fds - The structure describing the events to be monitored, OR NULL if
+ * this is a request to stop monitoring events.
+ *
+ * Returned Value:
+ * 0: Success; Negated errno on failure
+ *
+ ****************************************************************************/
+
+static inline int net_pollsetup(FAR struct socket *psock,
+ FAR struct pollfd *fds)
+{
+#ifdef CONFIG_NET_TCP
+ if (psock->s_type == SOCK_STREAM)
+ {
+ return tcp_pollsetup(psock, fds);
+ }
+#endif
+
+#ifdef CONFIG_NET_UDP
+ if (psock->s_type != SOCK_STREAM)
+ {
+ return udp_pollsetup(psock, fds);
+ }
+#endif
+
+ return -ENOSYS;
+}
#endif /* HAVE_NETPOLL */
/****************************************************************************
- * Function: net_pollteardown
+ * Function: tcp_pollteardown
*
* Description:
* Teardown monitoring of events on an TCP/IP socket
*
* Input Parameters:
- * conn - The TCP/IP connection of interest
+ * psock - The TCP/IP socket of interest
+ * fds - The structure describing the events to be monitored, OR NULL if
+ * this is a request to stop monitoring events.
*
* Returned Value:
* 0: Success; Negated errno on failure
@@ -340,7 +557,7 @@ errout_with_lock:
****************************************************************************/
#ifdef HAVE_NETPOLL
-static inline int net_pollteardown(FAR struct socket *psock,
+static inline int tcp_pollteardown(FAR struct socket *psock,
FAR struct pollfd *fds)
{
FAR struct tcp_conn_s *conn = psock->s_conn;
@@ -379,6 +596,98 @@ static inline int net_pollteardown(FAR struct socket *psock,
return OK;
}
+
+/****************************************************************************
+ * Function: udp_pollteardown
+ *
+ * Description:
+ * Teardown monitoring of events on an UDP/IP socket
+ *
+ * Input Parameters:
+ * psock - The TCP/IP socket of interest
+ * fds - The structure describing the events to be monitored, OR NULL if
+ * this is a request to stop monitoring events.
+ *
+ * Returned Value:
+ * 0: Success; Negated errno on failure
+ *
+ ****************************************************************************/
+
+static inline int udp_pollteardown(FAR struct socket *psock,
+ FAR struct pollfd *fds)
+{
+ FAR struct udp_conn_s *conn = psock->s_conn;
+ FAR struct net_poll_s *info;
+ net_lock_t flags;
+
+ /* Sanity check */
+
+#ifdef CONFIG_DEBUG
+ if (!conn || !fds->priv)
+ {
+ return -EINVAL;
+ }
+#endif
+
+ /* Recover the socket descriptor poll state info from the poll structure */
+
+ info = (FAR struct net_poll_s *)fds->priv;
+ DEBUGASSERT(info && info->fds && info->cb);
+ if (info)
+ {
+ /* Release the callback */
+
+ flags = net_lock();
+ udp_callback_free(conn, info->cb);
+ net_unlock(flags);
+
+ /* Release the poll/select data slot */
+
+ info->fds->priv = NULL;
+
+ /* Then free the poll info container */
+
+ kmm_free(info);
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Function: net_pollteardown
+ *
+ * Description:
+ * Teardown monitoring of events on an socket
+ *
+ * Input Parameters:
+ * psock - The TCP/IP socket of interest
+ * fds - The structure describing the events to be monitored, OR NULL if
+ * this is a request to stop monitoring events.
+ *
+ * Returned Value:
+ * 0: Success; Negated errno on failure
+ *
+ ****************************************************************************/
+
+static inline int net_pollteardown(FAR struct socket *psock,
+ FAR struct pollfd *fds)
+{
+#ifdef CONFIG_NET_TCP
+ if (psock->s_type == SOCK_STREAM)
+ {
+ return tcp_pollteardown(psock, fds);
+ }
+#endif
+
+#ifdef CONFIG_NET_UDP
+ if (psock->s_type != SOCK_STREAM)
+ {
+ return udp_pollteardown(psock, fds);
+ }
+#endif
+
+ return -ENOSYS;
+}
#endif /* HAVE_NETPOLL */
/****************************************************************************
@@ -408,15 +717,6 @@ int psock_poll(FAR struct socket *psock, FAR struct pollfd *fds, bool setup)
{
int ret;
-#ifdef CONFIG_NET_UDP
- /* poll() not supported for UDP */
-
- if (psock->s_type != SOCK_STREAM)
- {
- return -ENOSYS;
- }
-#endif
-
/* Check if we are setting up or tearing down the poll */
if (setup)
diff --git a/nuttx/net/socket/recvfrom.c b/nuttx/net/socket/recvfrom.c
index e9fcd7a83..cdb018093 100644
--- a/nuttx/net/socket/recvfrom.c
+++ b/nuttx/net/socket/recvfrom.c
@@ -100,6 +100,7 @@ struct recvfrom_s
size_t rf_buflen; /* Length of receive buffer */
uint8_t *rf_buffer; /* Pointer to receive buffer */
FAR struct sockaddr *rf_from; /* Address of sender */
+ FAR socklen_t *rf_fromlen; /* Number of bytes allocated for address of sender */
size_t rf_recvlen; /* The received length */
int rf_result; /* Success:OK, failure:negated errno */
};
@@ -310,7 +311,7 @@ static inline void recvfrom_newudpdata(FAR struct net_driver_s *dev,
#endif /* CONFIG_NET_TCP */
/****************************************************************************
- * Function: recvfrom_readahead
+ * Function: recvfrom_tcpreadahead
*
* Description:
* Copy the read data from the packet
@@ -328,7 +329,7 @@ static inline void recvfrom_newudpdata(FAR struct net_driver_s *dev,
****************************************************************************/
#if defined(CONFIG_NET_TCP) && defined(CONFIG_NET_TCP_READAHEAD)
-static inline void recvfrom_readahead(struct recvfrom_s *pstate)
+static inline void recvfrom_tcpreadahead(struct recvfrom_s *pstate)
{
FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)pstate->rf_sock->s_conn;
FAR struct iob_s *iob;
@@ -391,6 +392,83 @@ static inline void recvfrom_readahead(struct recvfrom_s *pstate)
}
#endif /* CONFIG_NET_UDP || CONFIG_NET_TCP */
+#if defined(CONFIG_NET_UDP) && defined(CONFIG_NET_UDP_READAHEAD)
+
+static inline void recvfrom_udpreadahead(struct recvfrom_s *pstate)
+{
+ FAR struct udp_conn_s *conn = (FAR struct udp_conn_s *)pstate->rf_sock->s_conn;
+ FAR struct iob_s *iob;
+ int recvlen;
+
+ /* Check there is any UDP datagram already buffered in a read-ahead
+ * buffer.
+ */
+
+ if ((iob = iob_peek_queue(&conn->readahead)) != NULL &&
+ pstate->rf_buflen > 0)
+ {
+ FAR struct iob_s *tmp;
+ uint8_t src_addr_size;
+
+ DEBUGASSERT(iob->io_pktlen > 0);
+
+ /* Transfer that buffered data from the I/O buffer chain into
+ * the user buffer.
+ */
+
+ recvlen = iob_copyout(&src_addr_size, iob, sizeof(uint8_t), 0);
+ if (recvlen != sizeof(uint8_t))
+ {
+ goto out;
+ }
+
+ if ( 0
+#ifdef CONFIG_NET_IPv6
+ || src_addr_size == 16
+#endif
+#ifdef CONFIG_NET_IPv4
+ || src_addr_size == 4
+#endif
+ )
+ {
+ if (pstate->rf_from)
+ {
+ socklen_t len = *pstate->rf_fromlen;
+ len = (socklen_t)src_addr_size > len ? len : (socklen_t)src_addr_size;
+
+ recvlen = iob_copyout(pstate->rf_from, iob, len, sizeof(uint8_t));
+ if (recvlen != len)
+ {
+ goto out;
+ }
+ }
+ }
+
+ recvlen = iob_copyout(pstate->rf_buffer, iob, pstate->rf_buflen, src_addr_size + sizeof(uint8_t));
+ nllvdbg("Received %d bytes (of %d)\n", recvlen, iob->io_pktlen);
+
+ /* Update the accumulated size of the data read */
+
+ pstate->rf_recvlen += recvlen;
+ pstate->rf_buffer += recvlen;
+ pstate->rf_buflen -= recvlen;
+
+out:
+ /* Remove the I/O buffer chain from the head of the read-ahead
+ * buffer queue.
+ */
+
+ tmp = iob_remove_queue(&conn->readahead);
+ DEBUGASSERT(tmp == iob);
+ UNUSED(tmp);
+
+ /* And free the I/O buffer chain */
+
+ (void)iob_free_chain(iob);
+ }
+}
+#endif
+
/****************************************************************************
* Function: recvfrom_timeout
*
@@ -1015,6 +1093,7 @@ static uint16_t recvfrom_udpinterrupt(struct net_driver_s *dev, void *pvconn,
#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_TCP)
static void recvfrom_init(FAR struct socket *psock, FAR void *buf,
size_t len, FAR struct sockaddr *infrom,
+ FAR socklen_t *fromlen,
FAR struct recvfrom_s *pstate)
{
/* Initialize the state structure. */
@@ -1024,6 +1103,7 @@ static void recvfrom_init(FAR struct socket *psock, FAR void *buf,
pstate->rf_buflen = len;
pstate->rf_buffer = buf;
pstate->rf_from = infrom;
+ pstate->rf_fromlen = fromlen;
/* Set up the start time for the timeout */
@@ -1184,7 +1264,7 @@ static inline void recvfrom_udp_rxnotify(FAR struct socket *psock,
#ifdef CONFIG_NET_PKT
static ssize_t pkt_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
- FAR struct sockaddr *from)
+ FAR struct sockaddr *from, FAR socklen_t *fromlen)
{
FAR struct pkt_conn_s *conn = (FAR struct pkt_conn_s *)psock->s_conn;
struct recvfrom_s state;
@@ -1199,7 +1279,7 @@ static ssize_t pkt_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
*/
save = net_lock();
- recvfrom_init(psock, buf, len, from, &state);
+ recvfrom_init(psock, buf, len, from, fromlen, &state);
/* TODO recvfrom_init() expects from to be of type sockaddr_in, but
* in our case is sockaddr_ll
@@ -1277,7 +1357,7 @@ errout_with_state:
#ifdef CONFIG_NET_UDP
static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
- FAR struct sockaddr *from)
+ FAR struct sockaddr *from, FAR socklen_t *fromlen)
{
FAR struct udp_conn_s *conn = (FAR struct udp_conn_s *)psock->s_conn;
struct recvfrom_s state;
@@ -1292,7 +1372,7 @@ static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
*/
save = net_lock();
- recvfrom_init(psock, buf, len, from, &state);
+ recvfrom_init(psock, buf, len, from, fromlen, &state);
/* Setup the UDP remote connection */
@@ -1302,6 +1382,48 @@ static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
goto errout_with_state;
}
+#ifdef CONFIG_NET_UDP_READAHEAD
+ recvfrom_udpreadahead(&state);
+
+ /* The default return value is the number of bytes that we just copied
+ * into the user buffer. We will return this if the socket has become
+ * disconnected or if the user request was completely satisfied with
+ * data from the readahead buffers.
+ */
+
+ ret = state.rf_recvlen;
+
+#else
+ /* Otherwise, the default return value of zero is used (only for the case
+ * where len == state.rf_buflen is zero).
+ */
+
+ ret = 0;
+#endif
+
+#ifdef CONFIG_NET_UDP_READAHEAD
+ if (_SS_ISNONBLOCK(psock->s_flags))
+ {
+ /* Return the number of bytes read from the read-ahead buffer if
+ * something was received (already in 'ret'); EAGAIN if not.
+ */
+
+ if (ret <= 0)
+ {
+ /* Nothing was received */
+
+ ret = -EAGAIN;
+ }
+ }
+
+ /* It is okay to block if we need to. If there is space to receive anything
+ * more, then we will wait to receive the data. Otherwise return the number
+ * of bytes read from the read-ahead buffer (already in 'ret').
+ */
+
+ else if (state.rf_recvlen == 0)
+#endif
+ {
/* Set up the callback in the connection */
state.rf_cb = udp_callback_alloc(conn);
@@ -1334,6 +1456,7 @@ static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
{
ret = -EBUSY;
}
+ }
errout_with_state:
net_unlock(save);
@@ -1364,7 +1487,7 @@ errout_with_state:
#ifdef CONFIG_NET_TCP
static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
- FAR struct sockaddr *from)
+ FAR struct sockaddr *from, FAR socklen_t *fromlen)
{
struct recvfrom_s state;
net_lock_t save;
@@ -1376,7 +1499,7 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
*/
save = net_lock();
- recvfrom_init(psock, buf, len, from, &state);
+ recvfrom_init(psock, buf, len, from, fromlen, &state);
/* Handle any any TCP data already buffered in a read-ahead buffer. NOTE
* that there may be read-ahead data to be retrieved even after the
@@ -1384,7 +1507,7 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
*/
#ifdef CONFIG_NET_TCP_READAHEAD
- recvfrom_readahead(&state);
+ recvfrom_tcpreadahead(&state);
/* The default return value is the number of bytes that we just copied
* into the user buffer. We will return this if the socket has become
@@ -1597,6 +1720,12 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
}
#endif
+ if (from && !fromlen)
+ {
+ err = EINVAL;
+ goto errout;
+ }
+
/* Verify that the sockfd corresponds to valid, allocated socket */
if (!psock || psock->s_crefs <= 0)
@@ -1666,7 +1795,7 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
#ifdef CONFIG_NET_PKT
case SOCK_RAW:
{
- ret = pkt_recvfrom(psock, buf, len, from);
+ ret = pkt_recvfrom(psock, buf, len, from, fromlen);
}
break;
#endif /* CONFIG_NET_PKT */
@@ -1689,7 +1818,7 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
else
#endif
{
- ret = tcp_recvfrom(psock, buf, len, from);
+ ret = tcp_recvfrom(psock, buf, len, from, fromlen);
}
#endif /* CONFIG_NET_TCP */
}
@@ -1714,7 +1843,7 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
else
#endif
{
- ret = udp_recvfrom(psock, buf, len, from);
+ ret = udp_recvfrom(psock, buf, len, from, fromlen);
}
#endif /* CONFIG_NET_UDP */
}