⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uip-tcpconn.c

📁 這是一個實時嵌入式作業系統 實作了MCS51 ARM等MCU
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************** * uip_tcpconn.c * *   Copyright (C) 2007 Gregory Nutt. All rights reserved. *   Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Large parts of this file were leveraged from uIP logic: * *   Copyright (c) 2001-2003, Adam Dunkels. *   All rights reserved. * * 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 *    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. * * 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. * ****************************************************************************//**************************************************************************** * Compilation Switches ****************************************************************************//**************************************************************************** * Included Files ****************************************************************************/#include <nuttx/config.h>#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)#include <sys/types.h>#include <string.h>#include <errno.h>#include <debug.h>#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 ****************************************************************************//* The array containing all uIP TCP connections. */static struct uip_conn g_tcp_connections[CONFIG_NET_TCP_CONNS];/* 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;/* Last port used by a TCP connection connection. */static uint16 g_last_tcp_port;/* g_tcp_sequence[] is used to generate TCP sequence numbers */static uint8 g_tcp_sequence[4];/**************************************************************************** * Private Functions ****************************************************************************//**************************************************************************** * 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: *   0 on success, negated errno on failure: * *   EADDRINUSE *     The given address is already in use. *   EADDRNOTAVAIL *     Cannot assign requested address (unlikely) * * 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       * 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).       */      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;            }        }      while (uip_tcplistener(htons(g_last_tcp_port)));    }  else    {      /* A port number has been supplied.  Verify that no other TCP/IP       * connection is using this local port.       */      if (uip_tcplistener(portno))        {          /* It is in use... return EADDRINUSE */          return -EADDRINUSE;        }    }  /* Return the selected or verified port number */  return portno;}/**************************************************************************** * Public Functions ****************************************************************************//**************************************************************************** * Name: uip_tcpinit() * * Description: *   Initialize the TCP/IP connection structures.  Called only once and only *   from the UIP layer at startup in normal user mode. * ****************************************************************************/void uip_tcpinit(void){  int i;  /* Initialize the queues */  dq_init(&g_free_tcp_connections);  dq_init(&g_active_tcp_connections);  /* Now initialize each connection structure */  for (i = 0; i < CONFIG_NET_TCP_CONNS; i++)    {      /* Mark the connection closed and move it to the free list */      g_tcp_connections[i].tcpstateflags = UIP_CLOSED;      dq_addlast(&g_tcp_connections[i].node, &g_free_tcp_connections);    }  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){  struct uip_conn *conn;  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   * while accessing g_free_tcp_connections[];   */  flags = irqsave();  /* Return the entry from the head of the free list */  conn = (struct uip_conn *)dq_remfirst(&g_free_tcp_connections);#if 0 /* Revisit */  /* Is the free list empty? */  if (!conn)    {      /* As a fallback, check for connection structures in the TIME_WAIT       * state.  If no CLOSED connections are found, then take the oldest       */      struct uip_conn *tmp = g_active_tcp_connections.head;      while (tmp)        {          /* Is this connectin in the UIP_TIME_WAIT state? */          if (tmp->tcpstateflags == UIP_TIME_WAIT)            {              /* Is it the oldest one we have seen so far? */              if (!conn || tmp->timer > conn->timer)                {                  /* Yes.. remember it */                  conn = tmp;                }            }          /* Look at the next active connection */          tmp = tmp->node.flink;        }      /* If we found one, remove it from the active connection list */      dq_rem(&conn->node, &g_active_tcp_connections);    }#endif  irqrestore(flags);  /* Mark the connection allocated */  if (conn)    {      conn->tcpstateflags = UIP_ALLOCATED;    }  return conn;}/**************************************************************************** * 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){#if CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0  struct uip_readahead_s *readahead;#endif  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 */      dq_rem(&conn->node, &g_active_tcp_connections);    }  /* 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  /* Mark the connection available and put it into the free list */  conn->tcpstateflags = UIP_CLOSED;  dq_addlast(&conn->node, &g_free_tcp_connections);  irqrestore(flags);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -