summaryrefslogtreecommitdiff
path: root/nuttx/net/uip/uip-listen.c
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-11-21 23:30:24 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-11-21 23:30:24 +0000
commit1e17d1ae425452aecad6f1fa99cf9b5903ee3880 (patch)
tree5ae4e64aeb6098defb4cd2f8dc165283085a9658 /nuttx/net/uip/uip-listen.c
parent7a8e90a29697e9e19ba4c27236be2061f8c6b734 (diff)
downloadpx4-nuttx-1e17d1ae425452aecad6f1fa99cf9b5903ee3880.tar.gz
px4-nuttx-1e17d1ae425452aecad6f1fa99cf9b5903ee3880.tar.bz2
px4-nuttx-1e17d1ae425452aecad6f1fa99cf9b5903ee3880.zip
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
Diffstat (limited to 'nuttx/net/uip/uip-listen.c')
-rw-r--r--nuttx/net/uip/uip-listen.c73
1 files changed, 58 insertions, 15 deletions
diff --git a/nuttx/net/uip/uip-listen.c b/nuttx/net/uip/uip-listen.c
index 643d054cb..82d4238cd 100644
--- a/nuttx/net/uip/uip-listen.c
+++ b/nuttx/net/uip/uip-listen.c
@@ -57,7 +57,7 @@
/* 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
@@ -84,7 +84,7 @@ void uip_listeninit(void)
int 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
*
* Description:
- * Stop listening on a port
+ * Stop listening to the port bound to the specified TCP connection
*
* Assumptions:
* Called from normal user code.
*
****************************************************************************/
-int uip_unlisten(uint16 port)
+int uip_unlisten(struct uip_conn *conn)
{
irqstate_t flags;
int ndx;
@@ -108,13 +108,14 @@ int uip_unlisten(uint16 port)
flags = irqsave();
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;
break;
}
}
+
irqrestore(flags);
return ret;
}
@@ -123,29 +124,58 @@ int uip_unlisten(uint16 port)
* Function: uip_listen
*
* Description:
- * Start listening on a port
+ * Start listening to the port bound to the specified TCP connection
*
* Assumptions:
* Called from normal user code.
*
****************************************************************************/
-int uip_listen(uint16 port)
+int uip_listen(struct uip_conn *conn)
{
irqstate_t flags;
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();
- 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;
- ret = OK;
- break;
+ /* Is the next slot available? */
+
+ if (!uip_listenports[ndx])
+ {
+ /* Yes.. we found it */
+
+ uip_listenports[ndx] = conn;
+ ret = OK;
+ break;
+ }
}
}
+
irqrestore(flags);
return ret;
}
@@ -164,13 +194,26 @@ int uip_listen(uint16 port)
boolean uip_islistener(uint16 portno)
{
int ndx;
+
+ /* Examine each connection structure in each slot of the listener list */
+
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;
}
}
+
+ /* No listener for this port */
+
return FALSE;
}