mirror of
https://github.com/apache/nuttx.git
synced 2025-12-13 23:17:11 +08:00
Fix confusion in listening socket vs accepted sockets
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@395 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -46,4 +46,18 @@ examples/null
|
|||||||
^^^^^^^^^^^^^
|
^^^^^^^^^^^^^
|
||||||
|
|
||||||
This is the do nothing application. It is only used for bringing
|
This is the do nothing application. It is only used for bringing
|
||||||
up new NuttX architectures
|
up new NuttX architectures.
|
||||||
|
|
||||||
|
examples/uip
|
||||||
|
^^^^^^^^^^^^
|
||||||
|
|
||||||
|
This is a port of uIP example application. It includes
|
||||||
|
conditionally compiled logic to exercise the uIP webserver,
|
||||||
|
webclient, telnet, smtp, dncpc, and resolver.
|
||||||
|
|
||||||
|
examples/netttest
|
||||||
|
^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
This is a simple network test for verifying client- and server-
|
||||||
|
functionality in a TCP/IP connection.
|
||||||
|
|
||||||
|
|||||||
@@ -572,25 +572,13 @@ extern int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in6 *addr
|
|||||||
extern int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in *addr);
|
extern int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in *addr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Start listening to the specified port.
|
/* Start listening to the port bound to the specified TCP connection */
|
||||||
*
|
|
||||||
* Note: Since this function expects the port number in network byte
|
|
||||||
* order, a conversion using HTONS() or htons() is necessary.
|
|
||||||
*
|
|
||||||
* port A 16-bit port number in network byte order.
|
|
||||||
*/
|
|
||||||
|
|
||||||
extern int uip_listen(uint16 port);
|
extern int uip_listen(struct uip_conn *conn);
|
||||||
|
|
||||||
/* Stop listening to the specified port.
|
/* Stop listening to the port bound to the specified TCP connection */
|
||||||
*
|
|
||||||
* Note: Since this function expects the port number in network byte
|
|
||||||
* order, a conversion using HTONS() or htons() is necessary.
|
|
||||||
*
|
|
||||||
* port A 16-bit port number in network byte order.
|
|
||||||
*/
|
|
||||||
|
|
||||||
extern int uip_unlisten(uint16 port);
|
extern int uip_unlisten(struct uip_conn *conn);
|
||||||
|
|
||||||
/* Check if a connection has outstanding (i.e., unacknowledged) data */
|
/* Check if a connection has outstanding (i.e., unacknowledged) data */
|
||||||
|
|
||||||
|
|||||||
@@ -126,9 +126,9 @@ int listen(int sockfd, int backlog)
|
|||||||
* is called; and someday should enable post() or select() logic.
|
* is called; and someday should enable post() or select() logic.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uip_listen(conn->lport);
|
uip_listen(conn);
|
||||||
psock->s_flags |= _SF_LISTENING;
|
psock->s_flags |= _SF_LISTENING;
|
||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
errout:
|
errout:
|
||||||
*get_errno_ptr() = err;
|
*get_errno_ptr() = err;
|
||||||
|
|||||||
@@ -57,7 +57,7 @@
|
|||||||
|
|
||||||
/* The uip_listenports list all currently listening ports. */
|
/* The uip_listenports list all currently listening ports. */
|
||||||
|
|
||||||
static uint16 uip_listenports[CONFIG_NET_MAX_LISTENPORTS];
|
static struct uip_conn *uip_listenports[CONFIG_NET_MAX_LISTENPORTS];
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
@@ -84,7 +84,7 @@ void uip_listeninit(void)
|
|||||||
int ndx;
|
int ndx;
|
||||||
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
||||||
{
|
{
|
||||||
uip_listenports[ndx] = 0;
|
uip_listenports[ndx] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,14 +92,14 @@ void uip_listeninit(void)
|
|||||||
* Function: uip_unlisten
|
* Function: uip_unlisten
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Stop listening on a port
|
* Stop listening to the port bound to the specified TCP connection
|
||||||
*
|
*
|
||||||
* Assumptions:
|
* Assumptions:
|
||||||
* Called from normal user code.
|
* Called from normal user code.
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int uip_unlisten(uint16 port)
|
int uip_unlisten(struct uip_conn *conn)
|
||||||
{
|
{
|
||||||
irqstate_t flags;
|
irqstate_t flags;
|
||||||
int ndx;
|
int ndx;
|
||||||
@@ -108,13 +108,14 @@ int uip_unlisten(uint16 port)
|
|||||||
flags = irqsave();
|
flags = irqsave();
|
||||||
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
||||||
{
|
{
|
||||||
if (uip_listenports[ndx] == port)
|
if (uip_listenports[ndx] == conn)
|
||||||
{
|
{
|
||||||
uip_listenports[ndx] = 0;
|
uip_listenports[ndx] = NULL;
|
||||||
ret = OK;
|
ret = OK;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
irqrestore(flags);
|
irqrestore(flags);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -123,29 +124,58 @@ int uip_unlisten(uint16 port)
|
|||||||
* Function: uip_listen
|
* Function: uip_listen
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Start listening on a port
|
* Start listening to the port bound to the specified TCP connection
|
||||||
*
|
*
|
||||||
* Assumptions:
|
* Assumptions:
|
||||||
* Called from normal user code.
|
* Called from normal user code.
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int uip_listen(uint16 port)
|
int uip_listen(struct uip_conn *conn)
|
||||||
{
|
{
|
||||||
irqstate_t flags;
|
irqstate_t flags;
|
||||||
int ndx;
|
int ndx;
|
||||||
int ret = -ENOBUFS;
|
int ret;
|
||||||
|
|
||||||
|
/* This must be done with interrupts disabled because the listener table
|
||||||
|
* is accessed from interrupt level as well.
|
||||||
|
*/
|
||||||
|
|
||||||
flags = irqsave();
|
flags = irqsave();
|
||||||
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
|
||||||
|
/* First, check if there is already a socket listening on this port */
|
||||||
|
|
||||||
|
if (uip_islistener(conn->lport))
|
||||||
{
|
{
|
||||||
if (uip_listenports[ndx] == 0)
|
/* Yes, then we must refuse this request */
|
||||||
|
|
||||||
|
ret = -EADDRINUSE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Otherwise, save a reference to the connection structure in the
|
||||||
|
* "listener" list.
|
||||||
|
*/
|
||||||
|
|
||||||
|
ret = -ENOBUFS; /* Assume failure */
|
||||||
|
|
||||||
|
/* Search all slots until an available slot is found */
|
||||||
|
|
||||||
|
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
||||||
{
|
{
|
||||||
uip_listenports[ndx] = port;
|
/* Is the next slot available? */
|
||||||
ret = OK;
|
|
||||||
break;
|
if (!uip_listenports[ndx])
|
||||||
|
{
|
||||||
|
/* Yes.. we found it */
|
||||||
|
|
||||||
|
uip_listenports[ndx] = conn;
|
||||||
|
ret = OK;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
irqrestore(flags);
|
irqrestore(flags);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -164,13 +194,26 @@ int uip_listen(uint16 port)
|
|||||||
boolean uip_islistener(uint16 portno)
|
boolean uip_islistener(uint16 portno)
|
||||||
{
|
{
|
||||||
int ndx;
|
int ndx;
|
||||||
|
|
||||||
|
/* Examine each connection structure in each slot of the listener list */
|
||||||
|
|
||||||
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
for (ndx = 0; ndx < CONFIG_NET_MAX_LISTENPORTS; ndx++)
|
||||||
{
|
{
|
||||||
if (uip_listenports[ndx] == portno)
|
/* Is this slot assigned? If so, does the connection have the same
|
||||||
|
* local port number?
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct uip_conn *conn = uip_listenports[ndx];
|
||||||
|
if (conn && conn->lport == portno)
|
||||||
{
|
{
|
||||||
|
/* Yes.. we found a listener on this port */
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* No listener for this port */
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user