2007-09-02 23:58:35 +02:00
|
|
|
/****************************************************************************
|
2007-09-03 01:11:10 +02:00
|
|
|
* uip_tcpconn.c
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
2007-09-03 01:11:10 +02:00
|
|
|
* Large parts of this file were leveraged from uIP logic:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2007-09-02 23:58:35 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2007-09-03 01:11:10 +02:00
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2007-09-03 01:11:10 +02:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Compilation Switches
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-11-22 19:36:46 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
2007-09-03 01:11:10 +02:00
|
|
|
#include <errno.h>
|
2007-11-06 00:04:16 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#include <net/uip/uipopt.h>
|
|
|
|
#include <net/uip/uip.h>
|
|
|
|
#include <net/uip/uip-arch.h>
|
|
|
|
|
|
|
|
#include "uip-internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-03 01:11:10 +02:00
|
|
|
/* The array containing all uIP TCP connections. */
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
static struct uip_conn g_tcp_connections[CONFIG_NET_TCP_CONNS];
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* A list of all free TCP connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_free_tcp_connections;
|
|
|
|
|
|
|
|
/* A list of all connected TCP connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_active_tcp_connections;
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/* Last port used by a TCP connection connection. */
|
|
|
|
|
|
|
|
static uint16 g_last_tcp_port;
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* g_tcp_sequence[] is used to generate TCP sequence numbers */
|
|
|
|
|
|
|
|
static uint8 g_tcp_sequence[4];
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_selectport()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If the portnumber is zero; select an unused port for the connection.
|
|
|
|
* If the portnumber is non-zero, verify that no other connection has
|
|
|
|
* been created with this port number.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* portno -- the selected port number in host order. Zero means no port
|
|
|
|
* selected.
|
|
|
|
*
|
|
|
|
* Return:
|
2007-11-16 19:48:39 +01:00
|
|
|
* 0 on success, negated errno on failure:
|
|
|
|
*
|
|
|
|
* EADDRINUSE
|
|
|
|
* The given address is already in use.
|
|
|
|
* EADDRNOTAVAIL
|
|
|
|
* Cannot assign requested address (unlikely)
|
2007-09-09 19:20:56 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uip_selectport(uint16 portno)
|
|
|
|
{
|
|
|
|
if (portno == 0)
|
|
|
|
{
|
|
|
|
/* No local port assigned. Loop until we find a valid listen port number
|
2007-11-16 19:48:39 +01:00
|
|
|
* that is not being used by any other connection. NOTE the following loop
|
|
|
|
* is assumed to terminate but could not if all 32000-4096+1 ports are
|
|
|
|
* in used (unlikely).
|
2007-09-09 19:20:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Guess that the next available port number will be the one after
|
|
|
|
* the last port number assigned.
|
|
|
|
*/
|
|
|
|
portno = ++g_last_tcp_port;
|
|
|
|
|
|
|
|
/* Make sure that the port number is within range */
|
|
|
|
|
|
|
|
if (g_last_tcp_port >= 32000)
|
|
|
|
{
|
|
|
|
g_last_tcp_port = 4096;
|
|
|
|
}
|
|
|
|
}
|
2007-11-16 19:48:39 +01:00
|
|
|
while (uip_tcplistener(htons(g_last_tcp_port)));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A port number has been supplied. Verify that no other TCP/IP
|
|
|
|
* connection is using this local port.
|
|
|
|
*/
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
if (uip_tcplistener(portno))
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
|
|
|
/* It is in use... return EADDRINUSE */
|
|
|
|
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
/* Return the selected or verified port number */
|
2007-09-09 19:20:56 +02:00
|
|
|
|
|
|
|
return portno;
|
|
|
|
}
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_tcpinit()
|
|
|
|
*
|
|
|
|
* Description:
|
2007-09-03 01:11:10 +02:00
|
|
|
* Initialize the TCP/IP connection structures. Called only once and only
|
2007-09-03 22:34:44 +02:00
|
|
|
* from the UIP layer at startup in normal user mode.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void uip_tcpinit(void)
|
|
|
|
{
|
|
|
|
int i;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Initialize the queues */
|
|
|
|
|
|
|
|
dq_init(&g_free_tcp_connections);
|
|
|
|
dq_init(&g_active_tcp_connections);
|
|
|
|
|
|
|
|
/* Now initialize each connection structure */
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_TCP_CONNS; i++)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Mark the connection closed and move it to the free list */
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
g_tcp_connections[i].tcpstateflags = UIP_CLOSED;
|
2007-09-03 22:34:44 +02:00
|
|
|
dq_addlast(&g_tcp_connections[i].node, &g_free_tcp_connections);
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_last_tcp_port = 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_tcpalloc()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a free TCP/IP connection structure and allocate it
|
|
|
|
* for use. This is normally something done by the implementation of the
|
|
|
|
* socket() API but is also called from the interrupt level when a TCP
|
|
|
|
* packet is received while "listening"
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct uip_conn *uip_tcpalloc(void)
|
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
struct uip_conn *conn;
|
2007-09-02 23:58:35 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Because this routine is called from both interrupt level and
|
|
|
|
* and from user level, we have not option but to disable interrupts
|
2007-09-03 22:34:44 +02:00
|
|
|
* while accessing g_free_tcp_connections[];
|
2007-09-02 23:58:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Return the entry from the head of the free list */
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
conn = (struct uip_conn *)dq_remfirst(&g_free_tcp_connections);
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
#if 0 /* Revisit */
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Is the free list empty? */
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
2007-09-02 23:58:35 +02:00
|
|
|
/* As a fallback, check for connection structures in the TIME_WAIT
|
|
|
|
* state. If no CLOSED connections are found, then take the oldest
|
|
|
|
*/
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
struct uip_conn *tmp = g_active_tcp_connections.head;
|
|
|
|
while (tmp)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Is this connectin in the UIP_TIME_WAIT state? */
|
|
|
|
|
|
|
|
if (tmp->tcpstateflags == UIP_TIME_WAIT)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Is it the oldest one we have seen so far? */
|
|
|
|
|
|
|
|
if (!conn || tmp->timer > conn->timer)
|
|
|
|
{
|
|
|
|
/* Yes.. remember it */
|
|
|
|
|
|
|
|
conn = tmp;
|
|
|
|
}
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Look at the next active connection */
|
|
|
|
|
|
|
|
tmp = tmp->node.flink;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* If we found one, remove it from the active connection list */
|
|
|
|
|
|
|
|
dq_rem(&conn->node, &g_active_tcp_connections);
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
#endif
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
irqrestore(flags);
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Mark the connection allocated */
|
|
|
|
|
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
conn->tcpstateflags = UIP_ALLOCATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_tcpfree()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a connection structure that is no longer in use. This should be
|
|
|
|
* done by the implementation of close()
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void uip_tcpfree(struct uip_conn *conn)
|
|
|
|
{
|
2007-11-20 00:35:42 +01:00
|
|
|
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
|
|
|
|
struct uip_readahead_s *readahead;
|
|
|
|
#endif
|
2007-09-03 22:34:44 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Because g_free_tcp_connections is accessed from user level and interrupt
|
|
|
|
* level, code, it is necessary to keep interrupts disabled during this
|
|
|
|
* operation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
|
|
|
|
/* UIP_ALLOCATED means that that the connection is not in the active list
|
|
|
|
* yet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->tcpstateflags != UIP_ALLOCATED)
|
|
|
|
{
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
|
2007-11-20 22:55:06 +01:00
|
|
|
dq_rem(&conn->node, &g_active_tcp_connections);
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 00:35:42 +01:00
|
|
|
/* Release any read-ahead buffers attached to the connection */
|
|
|
|
|
|
|
|
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
|
|
|
|
while ((readahead = (struct uip_readahead_s *)sq_remfirst(&conn->readahead)) != NULL)
|
|
|
|
{
|
|
|
|
uip_tcpreadaheadrelease(readahead);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Mark the connection available and put it into the free list */
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
conn->tcpstateflags = UIP_CLOSED;
|
2007-09-03 22:34:44 +02:00
|
|
|
dq_addlast(&conn->node, &g_free_tcp_connections);
|
|
|
|
irqrestore(flags);
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_tcpactive()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a connection structure that is the appropriate
|
|
|
|
* connection to be used withi the provided TCP/IP header
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct uip_conn *uip_tcpactive(struct uip_tcpip_hdr *buf)
|
|
|
|
{
|
2007-11-06 00:04:16 +01:00
|
|
|
struct uip_conn *conn = (struct uip_conn *)g_active_tcp_connections.head;
|
|
|
|
in_addr_t srcipaddr = uip_ip4addr_conv(buf->srcipaddr);
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
while (conn)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
|
|
|
/* Find an open connection matching the tcp input */
|
|
|
|
|
|
|
|
if (conn->tcpstateflags != UIP_CLOSED &&
|
2007-11-06 00:04:16 +01:00
|
|
|
buf->destport == conn->lport && buf->srcport == conn->rport &&
|
|
|
|
uip_ipaddr_cmp(srcipaddr, conn->ripaddr))
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Matching connection found.. break out of the loop and return a
|
|
|
|
* reference to it.
|
|
|
|
*/
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
break;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Look at the next active connection */
|
|
|
|
|
|
|
|
conn = (struct uip_conn *)conn->node.flink;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
return conn;
|
|
|
|
}
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-11-06 00:04:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_nexttcpconn()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the list of active TCP connections
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level (or with
|
|
|
|
* interrupts disabled).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct uip_conn *uip_nexttcpconn(struct uip_conn *conn)
|
|
|
|
{
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return (struct uip_conn *)g_active_tcp_connections.head;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (struct uip_conn *)conn->node.flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2007-09-23 22:45:30 +02:00
|
|
|
* Name: uip_tcplistener()
|
|
|
|
*
|
|
|
|
* Description:
|
2007-11-16 19:48:39 +01:00
|
|
|
* Given a local port number (in network byte order), find the TCP
|
|
|
|
* connection that listens on this this port.
|
2007-09-23 22:45:30 +02:00
|
|
|
*
|
|
|
|
* Primary uses: (1) to determine if a port number is available, (2) to
|
|
|
|
* To idenfity the socket that will accept new connections on a local port.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct uip_conn *uip_tcplistener(uint16 portno)
|
|
|
|
{
|
|
|
|
struct uip_conn *conn;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Check if this port number is in use by any active UIP TCP connection */
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_TCP_CONNS; i++)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
|
|
|
conn = &g_tcp_connections[i];
|
2007-11-16 19:48:39 +01:00
|
|
|
if (conn->tcpstateflags != UIP_CLOSED && conn->lport == portno)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
|
|
|
/* The portnumber is in use, return the connection */
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_tcpaccept()
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when uip_interupt matches the incoming packet with a connection
|
|
|
|
* in LISTEN. In that case, this function will create a new connection and
|
|
|
|
* initialize it to send a SYNACK in return.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
struct uip_conn *uip_tcpaccept(struct uip_tcpip_hdr *buf)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
|
|
|
struct uip_conn *conn = uip_tcpalloc();
|
|
|
|
if (conn)
|
|
|
|
{
|
|
|
|
/* Fill in the necessary fields for the new connection. */
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
conn->rto = UIP_RTO;
|
|
|
|
conn->timer = UIP_RTO;
|
|
|
|
conn->sa = 0;
|
|
|
|
conn->sv = 4;
|
|
|
|
conn->nrtx = 0;
|
|
|
|
conn->lport = buf->destport;
|
|
|
|
conn->rport = buf->srcport;
|
|
|
|
uip_ipaddr_copy(conn->ripaddr, uip_ip4addr_conv(buf->srcipaddr));
|
2007-09-03 22:34:44 +02:00
|
|
|
conn->tcpstateflags = UIP_SYN_RCVD;
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
conn->snd_nxt[0] = g_tcp_sequence[0];
|
|
|
|
conn->snd_nxt[1] = g_tcp_sequence[1];
|
|
|
|
conn->snd_nxt[2] = g_tcp_sequence[2];
|
|
|
|
conn->snd_nxt[3] = g_tcp_sequence[3];
|
|
|
|
conn->len = 1;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* rcv_nxt should be the seqno from the incoming packet + 1. */
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
conn->rcv_nxt[3] = buf->seqno[3];
|
|
|
|
conn->rcv_nxt[2] = buf->seqno[2];
|
|
|
|
conn->rcv_nxt[1] = buf->seqno[1];
|
|
|
|
conn->rcv_nxt[0] = buf->seqno[0];
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
/* Initialize the list of TCP read-ahead buffers */
|
|
|
|
|
|
|
|
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
|
|
|
|
sq_init(&conn->readahead);
|
|
|
|
#endif
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
/* And, finally, put the connection structure into the active list.
|
|
|
|
* Interrupts should already be disabled in this context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dq_addlast(&conn->node, &g_active_tcp_connections);
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
return conn;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2007-09-03 01:11:10 +02:00
|
|
|
* Name: uip_tcpnextsequence()
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Increment the TCP/IP sequence number
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from the interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void uip_tcpnextsequence(void)
|
|
|
|
{
|
|
|
|
/* This inplements a byte-by-byte big-endian increment */
|
|
|
|
|
|
|
|
if (++g_tcp_sequence[3] == 0)
|
|
|
|
{
|
|
|
|
if (++g_tcp_sequence[2] == 0)
|
|
|
|
{
|
|
|
|
if (++g_tcp_sequence[1] == 0)
|
|
|
|
{
|
|
|
|
++g_tcp_sequence[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uip_tcpbind()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the UIP specific parts of the standard TCP
|
|
|
|
* bind() operation.
|
|
|
|
*
|
2007-11-16 19:48:39 +01:00
|
|
|
* Return:
|
|
|
|
* 0 on success or -EADDRINUSE on failure
|
|
|
|
*
|
2007-09-02 23:58:35 +02:00
|
|
|
* Assumptions:
|
|
|
|
* This function is called from normal user level code.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
int uip_tcpbind(struct uip_conn *conn, const struct sockaddr_in6 *addr)
|
|
|
|
#else
|
|
|
|
int uip_tcpbind(struct uip_conn *conn, const struct sockaddr_in *addr)
|
|
|
|
#endif
|
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
int port;
|
|
|
|
|
|
|
|
/* Verify or select a local port */
|
|
|
|
|
|
|
|
flags = irqsave();
|
2007-11-16 19:48:39 +01:00
|
|
|
port = uip_selectport(ntohs(addr->sin_port));
|
2007-09-09 19:20:56 +02:00
|
|
|
irqrestore(flags);
|
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
{
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
/* Save the local address in the connection structure. Note that the requested
|
|
|
|
* local IP address is saved but not used. At present, only a single network
|
|
|
|
* interface is supported, the IP address is not of importance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn->lport = addr->sin_port;
|
2007-11-22 15:42:52 +01:00
|
|
|
|
|
|
|
#if 0 /* Not used */
|
2007-11-16 19:48:39 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
uip_ipaddr_copy(conn->lipaddr, addr->sin6_addr.in6_u.u6_addr16);
|
|
|
|
#else
|
|
|
|
uip_ipaddr_copy(conn->lipaddr, addr->sin_addr.s_addr);
|
2007-11-22 15:42:52 +01:00
|
|
|
#endif
|
2007-11-16 19:48:39 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2007-09-03 01:11:10 +02:00
|
|
|
* Name: uip_tcpconnect()
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the UIP specific parts of the standard
|
|
|
|
* TCP connect() operation: It connects to a remote host using TCP.
|
|
|
|
*
|
|
|
|
* This function is used to start a new connection to the specified
|
|
|
|
* port on the specied host. It uses the connection structure that was
|
|
|
|
* allocated by a preceding socket() call. It sets the connection to
|
|
|
|
* the SYN_SENT state and sets the retransmission timer to 0. This will
|
|
|
|
* cause a TCP SYN segment to be sent out the next time this connection
|
|
|
|
* is periodically processed, which usually is done within 0.5 seconds
|
|
|
|
* after the call to uip_tcpconnect().
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from normal user level code.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2007-09-03 22:34:44 +02:00
|
|
|
int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in6 *addr)
|
2007-09-02 23:58:35 +02:00
|
|
|
#else
|
2007-09-03 22:34:44 +02:00
|
|
|
int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in *addr)
|
2007-09-02 23:58:35 +02:00
|
|
|
#endif
|
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
irqstate_t flags;
|
2007-09-09 19:20:56 +02:00
|
|
|
int port;
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* The connection is expected to be in the UIP_ALLOCATED state.. i.e.,
|
|
|
|
* allocated via up_tcpalloc(), but not yet put into the active connections
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!conn || conn->tcpstateflags != UIP_ALLOCATED)
|
|
|
|
{
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
/* If the TCP port has not alread been bound to a local port, then select
|
|
|
|
* one now.
|
|
|
|
*/
|
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
flags = irqsave();
|
|
|
|
port = uip_selectport(ntohs(conn->lport));
|
|
|
|
irqrestore(flags);
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
if (port < 0)
|
|
|
|
{
|
|
|
|
return port;
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize and return the connection structure, bind it to the port number */
|
|
|
|
|
|
|
|
conn->tcpstateflags = UIP_SYN_SENT;
|
|
|
|
|
|
|
|
conn->snd_nxt[0] = g_tcp_sequence[0];
|
|
|
|
conn->snd_nxt[1] = g_tcp_sequence[1];
|
|
|
|
conn->snd_nxt[2] = g_tcp_sequence[2];
|
|
|
|
conn->snd_nxt[3] = g_tcp_sequence[3];
|
|
|
|
|
|
|
|
conn->initialmss = conn->mss = UIP_TCP_MSS;
|
|
|
|
|
|
|
|
conn->len = 1; /* TCP length of the SYN is one. */
|
|
|
|
conn->nrtx = 0;
|
|
|
|
conn->timer = 1; /* Send the SYN next time around. */
|
|
|
|
conn->rto = UIP_RTO;
|
|
|
|
conn->sa = 0;
|
|
|
|
conn->sv = 16; /* Initial value of the RTT variance. */
|
2007-09-09 19:20:56 +02:00
|
|
|
conn->lport = htons((uint16)port);
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
/* The sockaddr port is 16 bits and already in network order */
|
|
|
|
|
|
|
|
conn->rport = addr->sin_port;
|
|
|
|
|
|
|
|
/* The sockaddr address is 32-bits in network order. */
|
|
|
|
|
2007-09-17 00:12:04 +02:00
|
|
|
uip_ipaddr_copy(conn->ripaddr, addr->sin_addr.s_addr);
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
/* Initialize the list of TCP read-ahead buffers */
|
|
|
|
|
|
|
|
#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
|
|
|
|
sq_init(&conn->readahead);
|
|
|
|
#endif
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* And, finally, put the connection structure into the active
|
|
|
|
* list. Because g_active_tcp_connections is accessed from user level and
|
|
|
|
* interrupt level, code, it is necessary to keep interrupts disabled during
|
|
|
|
* this operation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
dq_addlast(&conn->node, &g_active_tcp_connections);
|
|
|
|
irqrestore(flags);
|
|
|
|
|
2007-09-02 23:58:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|