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

📄 uip-tcpinput.c

📁 這是一個實時嵌入式作業系統 實作了MCS51 ARM等MCU
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************** * net/uip/uip-tcpinput.c * Handling incoming TCP input * *   Copyright (C) 2007 Gregory Nutt. All rights reserved. *   Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Adapted for NuttX from logic in uIP which also has a BSD-like license: * *   Original author Adam Dunkels <adam@dunkels.com> *   Copyright () 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. * ****************************************************************************//**************************************************************************** * Included Files ****************************************************************************/#include <nuttx/config.h>#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)#include <sys/types.h>#include <string.h>#include <debug.h>#include <net/uip/uipopt.h>#include <net/uip/uip.h>#include <net/uip/uip-arch.h>#include "uip-internal.h"/**************************************************************************** * Definitions ****************************************************************************/#define BUF ((struct uip_tcpip_hdr *)&dev->d_buf[UIP_LLH_LEN])/**************************************************************************** * Public Variables ****************************************************************************//**************************************************************************** * Private Variables ****************************************************************************//**************************************************************************** * Private Functions ****************************************************************************//**************************************************************************** * Public Functions ****************************************************************************//**************************************************************************** * Name: uip_tcpinput * * Description: *   Handle incoming TCP input * * Parameters: *   dev - The device driver structure containing the received TCP packet. * * Return: *   None * * Assumptions: *   Called from the interrupt level or with interrupts disabled. * ****************************************************************************/void uip_tcpinput(struct uip_driver_s *dev){  struct uip_conn *conn = NULL;  uint16 tmp16;  uint8  opt;  uint8  flags;  uint8  result;  int    len;  int    i;  dev->d_snddata = &dev->d_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];  dev->d_appdata = &dev->d_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];#ifdef CONFIG_NET_STATISTICS  uip_stat.tcp.recv++;#endif  /* Start of TCP input header processing code. */  if (uip_tcpchksum(dev) != 0xffff)    {      /* Compute and check the TCP checksum. */#ifdef CONFIG_NET_STATISTICS      uip_stat.tcp.drop++;      uip_stat.tcp.chkerr++;#endif      ndbg("Bad TCP checksum\n");      goto drop;    }  /* Demultiplex this segment. First check any active connections. */  conn = uip_tcpactive(BUF);  if (conn)    {      goto found;    }  /* If we didn't find and active connection that expected the packet,   * either (1) this packet is an old duplicate, or (2) this is a SYN packet   * destined for a connection in LISTEN. If the SYN flag isn't set,   * it is an old packet and we send a RST.   */  if ((BUF->flags & TCP_CTL) == TCP_SYN)    {      /* This is a SYN packet for a connection.  Find the connection       * listening on this port.       */      tmp16 = BUF->destport;      if (uip_islistener(tmp16))        {          /* We matched the incoming packet with a connection in LISTEN.           * We now need to create a new connection and send a SYNACK in           * response.           */          /* First allocate a new connection structure and see if there is any           * user application to accept it.           */          conn = uip_tcpaccept(BUF);          if (conn)            {              /* The connection structure was successfully allocated.  Now see               * there is an application waiting to accept the connection (or at               * least queue it it for acceptance).               */              if (uip_accept(conn, tmp16) != OK)                {                  /* No, then we have to give the connection back */                  uip_tcpfree(conn);                  conn = NULL;                }            }          if (!conn)            {              /* Either (1) all available connections are in use, or (2) there is no               * application in place to accept the connection.  We drop packet and hope that               * the remote end will retransmit the packet at a time when we               * have more spare connections or someone waiting to accept the connection.               */#ifdef CONFIG_NET_STATISTICS              uip_stat.tcp.syndrop++;#endif              ndbg("No free TCP connections\n");              goto drop;            }          uip_incr32(conn->rcv_nxt, 1);          /* Parse the TCP MSS option, if present. */          if ((BUF->tcpoffset & 0xf0) > 0x50)            {              for (i = 0; i < ((BUF->tcpoffset >> 4) - 5) << 2 ;)                {                  opt = dev->d_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + i];                  if (opt == TCP_OPT_END)                    {                      /* End of options. */                      break;                    }                  else if (opt == TCP_OPT_NOOP)                    {                      /* NOP option. */                      ++i;                    }                  else if (opt == TCP_OPT_MSS &&                          dev->d_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + i] == TCP_OPT_MSS_LEN)                    {                      /* An MSS option with the right option length. */                      tmp16 = ((uint16)dev->d_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + i] << 8) |                               (uint16)dev->d_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + i];                      conn->initialmss = conn->mss =                              tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;                      /* And we are done processing options. */                      break;                    }                  else                    {                      /* All other options have a length field, so that we easily                       * can skip past them.                       */                      if (dev->d_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + i] == 0)                        {                          /* If the length field is zero, the options are malformed                           * and we don't process them further.                           */                          break;                        }                      i += dev->d_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + i];                    }                }            }          /* Our response will be a SYNACK. */          uip_tcpack(dev, conn, TCP_ACK | TCP_SYN);          return;        }    }  /* This is (1) an old duplicate packet or (2) a SYN packet but with   * no matching listener found.  Send RST packet in either case.   */  /* We do not send resets in response to resets. */  if (BUF->flags & TCP_RST)    {      goto drop;    }#ifdef CONFIG_NET_STATISTICS  uip_stat.tcp.synrst++;#endif  uip_tcpreset(dev);  return;found:  flags = 0;  /* We do a very naive form of TCP reset processing; we just accept   * any RST and kill our connection. We should in fact check if the   * sequence number of this reset is wihtin our advertised window   * before we accept the reset.   */  if (BUF->flags & TCP_RST)    {      conn->tcpstateflags = UIP_CLOSED;      ndbg("RESET - TCP state: UIP_CLOSED\n");      (void)uip_tcpcallback(dev, conn, UIP_ABORT);      goto drop;    }  /* Calculated the length of the data, if the application has sent   * any data to us.   */  len = (BUF->tcpoffset >> 4) << 2;  /* d_len will contain the length of the actual TCP data. This is   * calculated by subtracting the length of the TCP header (in   * len) and the length of the IP header (20 bytes).   */  dev->d_len -= (len + UIP_IPH_LEN);  /* First, check if the sequence number of the incoming packet is   * what we're expecting next. If not, we send out an ACK with the   * correct numbers in.   */  if (!(((conn->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&      ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))))    {      if ((dev->d_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&          memcmp(BUF->seqno, conn->rcv_nxt, 4) != 0)        {            uip_tcpsend(dev, conn, TCP_ACK, UIP_IPTCPH_LEN);            return;        }    }  /* Next, check if the incoming segment acknowledges any outstanding   * data. If so, we update the sequence number, reset the length of   * the outstanding data, calculate RTT estimations, and reset the   * retransmission timer.   */  if ((BUF->flags & TCP_ACK) && uip_outstanding(conn))    {      /* Temporary variables. */      uint8 acc32[4];      uip_add32(conn->snd_nxt, conn->len, acc32);      if (memcmp(BUF->ackno, acc32, 4) == 0)        {          /* Update sequence number. */          memcpy(conn->snd_nxt, acc32, 4);          /* Do RTT estimation, unless we have done retransmissions. */          if (conn->nrtx == 0)            {              signed char m;              m = conn->rto - conn->timer;              /* This is taken directly from VJs original code in his paper */              m = m - (conn->sa >> 3);              conn->sa += m;              if (m < 0)                {                  m = -m;                }              m = m - (conn->sv >> 2);              conn->sv += m;              conn->rto = (conn->sa >> 3) + conn->sv;            }          /* Set the acknowledged flag. */          flags = UIP_ACKDATA;          /* Reset the retransmission timer. */          conn->timer = conn->rto;          /* Reset length of outstanding data. */          conn->len = 0;        }    }

⌨️ 快捷键说明

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