Add svn:eol-style property to source files, so that the line endings are correctly...
[pub/USBasp.git] / Projects / Webserver / Lib / uip / uip.c
index b2c4228..22a7204 100644 (file)
-#define DEBUG_PRINTF(...) /*printf(__VA_ARGS__)*/\r
-\r
-/**\r
- * \addtogroup uip\r
- * @{\r
- */\r
-\r
-/**\r
- * \file\r
- * The uIP TCP/IP stack code.\r
- * \author Adam Dunkels <adam@dunkels.com>\r
- */\r
-\r
-/*\r
- * Copyright (c) 2001-2003, Adam Dunkels.\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- * 3. The name of the author may not be used to endorse or promote\r
- *    products derived from this software without specific prior\r
- *    written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * This file is part of the uIP TCP/IP stack.\r
- *\r
- * $Id: uip.c,v 1.15 2008/10/15 08:08:32 adamdunkels Exp $\r
- *\r
- */\r
-\r
-/*\r
- * uIP is a small implementation of the IP, UDP and TCP protocols (as\r
- * well as some basic ICMP stuff). The implementation couples the IP,\r
- * UDP, TCP and the application layers very tightly. To keep the size\r
- * of the compiled code down, this code frequently uses the goto\r
- * statement. While it would be possible to break the uip_process()\r
- * function into many smaller functions, this would increase the code\r
- * size because of the overhead of parameter passing and the fact that\r
- * the optimier would not be as efficient.\r
- *\r
- * The principle is that we have a small buffer, called the uip_buf,\r
- * in which the device driver puts an incoming packet. The TCP/IP\r
- * stack parses the headers in the packet, and calls the\r
- * application. If the remote host has sent data to the application,\r
- * this data is present in the uip_buf and the application read the\r
- * data from there. It is up to the application to put this data into\r
- * a byte stream if needed. The application will not be fed with data\r
- * that is out of sequence.\r
- *\r
- * If the application whishes to send data to the peer, it should put\r
- * its data into the uip_buf. The uip_appdata pointer points to the\r
- * first available byte. The TCP/IP stack will calculate the\r
- * checksums, and fill in the necessary header fields and finally send\r
- * the packet back to the peer.\r
-*/\r
-\r
-#include "uip.h"\r
-#include "uipopt.h"\r
-#include "uip_arp.h"\r
-\r
-#if !UIP_CONF_IPV6 /* If UIP_CONF_IPV6 is defined, we compile the\r
-                     uip6.c file instead of this one. Therefore\r
-                     this #ifndef removes the entire compilation\r
-                     output of the uip.c file */\r
-\r
-\r
-#if UIP_CONF_IPV6\r
-#include "net/uip-neighbor.h"\r
-#endif /* UIP_CONF_IPV6 */\r
-\r
-#include <string.h>\r
-\r
-/*---------------------------------------------------------------------------*/\r
-/* Variable definitions. */\r
-\r
-\r
-/* The IP address of this host. If it is defined to be fixed (by\r
-   setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set\r
-   here. Otherwise, the address */\r
-#if UIP_FIXEDADDR > 0\r
-const uip_ipaddr_t uip_hostaddr =\r
-  { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 };\r
-const uip_ipaddr_t uip_draddr =\r
-  { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 };\r
-const uip_ipaddr_t uip_netmask =\r
-  { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 };\r
-#else\r
-uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask;\r
-#endif /* UIP_FIXEDADDR */\r
-\r
-const uip_ipaddr_t uip_broadcast_addr =\r
-#if UIP_CONF_IPV6\r
-  { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
-      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };\r
-#else /* UIP_CONF_IPV6 */\r
-  { { 0xff, 0xff, 0xff, 0xff } };\r
-#endif /* UIP_CONF_IPV6 */\r
-const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, /* rest is 0 */ } };\r
-\r
-#if UIP_FIXEDETHADDR\r
-const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,\r
-                                         UIP_ETHADDR1,\r
-                                         UIP_ETHADDR2,\r
-                                         UIP_ETHADDR3,\r
-                                         UIP_ETHADDR4,\r
-                                         UIP_ETHADDR5}};\r
-#else\r
-struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}};\r
-#endif\r
-\r
-#ifndef UIP_CONF_EXTERNAL_BUFFER\r
-u8_t uip_buf[UIP_BUFSIZE + 2];   /* The packet buffer that contains\r
-                                   incoming packets. */\r
-#endif /* UIP_CONF_EXTERNAL_BUFFER */\r
-\r
-void *uip_appdata;               /* The uip_appdata pointer points to\r
-                                   application data. */\r
-void *uip_sappdata;              /* The uip_appdata pointer points to\r
-                                   the application data which is to\r
-                                   be sent. */\r
-#if UIP_URGDATA > 0\r
-void *uip_urgdata;               /* The uip_urgdata pointer points to\r
-                                   urgent data (out-of-band data), if\r
-                                   present. */\r
-u16_t uip_urglen, uip_surglen;\r
-#endif /* UIP_URGDATA > 0 */\r
-\r
-u16_t uip_len, uip_slen;\r
-                             /* The uip_len is either 8 or 16 bits,\r
-                               depending on the maximum packet\r
-                               size. */\r
-\r
-u8_t uip_flags;     /* The uip_flags variable is used for\r
-                               communication between the TCP/IP stack\r
-                               and the application program. */\r
-struct uip_conn *uip_conn;   /* uip_conn always points to the current\r
-                               connection. */\r
-\r
-struct uip_conn uip_conns[UIP_CONNS];\r
-                             /* The uip_conns array holds all TCP\r
-                               connections. */\r
-u16_t uip_listenports[UIP_LISTENPORTS];\r
-                             /* The uip_listenports list all currently\r
-                               listning ports. */\r
-#if UIP_UDP\r
-struct uip_udp_conn *uip_udp_conn;\r
-struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];\r
-#endif /* UIP_UDP */\r
-\r
-static u16_t ipid;           /* Ths ipid variable is an increasing\r
-                               number that is used for the IP ID\r
-                               field. */\r
-\r
-void uip_setipid(u16_t id) { ipid = id; }\r
-\r
-static u8_t iss[4];          /* The iss variable is used for the TCP\r
-                               initial sequence number. */\r
-\r
-#if UIP_ACTIVE_OPEN\r
-static u16_t lastport;       /* Keeps track of the last port used for\r
-                               a new connection. */\r
-#endif /* UIP_ACTIVE_OPEN */\r
-\r
-/* Temporary variables. */\r
-u8_t uip_acc32[4];\r
-static u8_t c, opt;\r
-static u16_t tmp16;\r
-\r
-/* Structures and definitions. */\r
-#define TCP_FIN 0x01\r
-#define TCP_SYN 0x02\r
-#define TCP_RST 0x04\r
-#define TCP_PSH 0x08\r
-#define TCP_ACK 0x10\r
-#define TCP_URG 0x20\r
-#define TCP_CTL 0x3f\r
-\r
-#define TCP_OPT_END     0   /* End of TCP options list */\r
-#define TCP_OPT_NOOP    1   /* "No-operation" TCP option */\r
-#define TCP_OPT_MSS     2   /* Maximum segment size TCP option */\r
-\r
-#define TCP_OPT_MSS_LEN 4   /* Length of TCP MSS option. */\r
-\r
-#define ICMP_ECHO_REPLY 0\r
-#define ICMP_ECHO       8\r
-\r
-#define ICMP_DEST_UNREACHABLE        3\r
-#define ICMP_PORT_UNREACHABLE        3\r
-\r
-#define ICMP6_ECHO_REPLY             129\r
-#define ICMP6_ECHO                   128\r
-#define ICMP6_NEIGHBOR_SOLICITATION  135\r
-#define ICMP6_NEIGHBOR_ADVERTISEMENT 136\r
-\r
-#define ICMP6_FLAG_S (1 << 6)\r
-\r
-#define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1\r
-#define ICMP6_OPTION_TARGET_LINK_ADDRESS 2\r
-\r
-\r
-/* Macros. */\r
-#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])\r
-#define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])\r
-#define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])\r
-#define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])\r
-\r
-\r
-#if UIP_STATISTICS == 1\r
-struct uip_stats uip_stat;\r
-#define UIP_STAT(s) s\r
-#else\r
-#define UIP_STAT(s)\r
-#endif /* UIP_STATISTICS == 1 */\r
-\r
-#if UIP_LOGGING == 1\r
-#include <stdio.h>\r
-void uip_log(char *msg);\r
-#define UIP_LOG(m) uip_log(m)\r
-#else\r
-#define UIP_LOG(m)\r
-#endif /* UIP_LOGGING == 1 */\r
-\r
-#if ! UIP_ARCH_ADD32\r
-void\r
-uip_add32(u8_t *op32, u16_t op16)\r
-{\r
-  uip_acc32[3] = op32[3] + (op16 & 0xff);\r
-  uip_acc32[2] = op32[2] + (op16 >> 8);\r
-  uip_acc32[1] = op32[1];\r
-  uip_acc32[0] = op32[0];\r
-  \r
-  if(uip_acc32[2] < (op16 >> 8)) {\r
-    ++uip_acc32[1];\r
-    if(uip_acc32[1] == 0) {\r
-      ++uip_acc32[0];\r
-    }\r
-  }\r
-  \r
-  \r
-  if(uip_acc32[3] < (op16 & 0xff)) {\r
-    ++uip_acc32[2];\r
-    if(uip_acc32[2] == 0) {\r
-      ++uip_acc32[1];\r
-      if(uip_acc32[1] == 0) {\r
-       ++uip_acc32[0];\r
-      }\r
-    }\r
-  }\r
-}\r
-\r
-#endif /* UIP_ARCH_ADD32 */\r
-\r
-#if ! UIP_ARCH_CHKSUM\r
-/*---------------------------------------------------------------------------*/\r
-static u16_t\r
-chksum(u16_t sum, const u8_t *data, u16_t len)\r
-{\r
-  u16_t t;\r
-  const u8_t *dataptr;\r
-  const u8_t *last_byte;\r
-\r
-  dataptr = data;\r
-  last_byte = data + len - 1;\r
-  \r
-  while(dataptr < last_byte) { /* At least two more bytes */\r
-    t = (dataptr[0] << 8) + dataptr[1];\r
-    sum += t;\r
-    if(sum < t) {\r
-      sum++;           /* carry */\r
-    }\r
-    dataptr += 2;\r
-  }\r
-  \r
-  if(dataptr == last_byte) {\r
-    t = (dataptr[0] << 8) + 0;\r
-    sum += t;\r
-    if(sum < t) {\r
-      sum++;           /* carry */\r
-    }\r
-  }\r
-\r
-  /* Return sum in host byte order. */\r
-  return sum;\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-u16_t\r
-uip_chksum(u16_t *data, u16_t len)\r
-{\r
-  return htons(chksum(0, (u8_t *)data, len));\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-#ifndef UIP_ARCH_IPCHKSUM\r
-u16_t\r
-uip_ipchksum(void)\r
-{\r
-  u16_t sum;\r
-\r
-  sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);\r
-  DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum);\r
-  return (sum == 0) ? 0xffff : htons(sum);\r
-}\r
-#endif\r
-/*---------------------------------------------------------------------------*/\r
-static u16_t\r
-upper_layer_chksum(u8_t proto)\r
-{\r
-  u16_t upper_layer_len;\r
-  u16_t sum;\r
-  \r
-#if UIP_CONF_IPV6\r
-  upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]);\r
-#else /* UIP_CONF_IPV6 */\r
-  upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN;\r
-#endif /* UIP_CONF_IPV6 */\r
-  \r
-  /* First sum pseudoheader. */\r
-  \r
-  /* IP protocol and length fields. This addition cannot carry. */\r
-  sum = upper_layer_len + proto;\r
-  /* Sum IP source and destination addresses. */\r
-  sum = chksum(sum, (u8_t *)&BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t));\r
-\r
-  /* Sum TCP header and data. */\r
-  sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN],\r
-              upper_layer_len);\r
-    \r
-  return (sum == 0) ? 0xffff : htons(sum);\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-#if UIP_CONF_IPV6\r
-u16_t\r
-uip_icmp6chksum(void)\r
-{\r
-  return upper_layer_chksum(UIP_PROTO_ICMP6);\r
-  \r
-}\r
-#endif /* UIP_CONF_IPV6 */\r
-/*---------------------------------------------------------------------------*/\r
-u16_t\r
-uip_tcpchksum(void)\r
-{\r
-  return upper_layer_chksum(UIP_PROTO_TCP);\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-#if UIP_UDP_CHECKSUMS\r
-u16_t\r
-uip_udpchksum(void)\r
-{\r
-  return upper_layer_chksum(UIP_PROTO_UDP);\r
-}\r
-#endif /* UIP_UDP_CHECKSUMS */\r
-#endif /* UIP_ARCH_CHKSUM */\r
-/*---------------------------------------------------------------------------*/\r
-void\r
-uip_init(void)\r
-{\r
-  for(c = 0; c < UIP_LISTENPORTS; ++c) {\r
-    uip_listenports[c] = 0;\r
-  }\r
-  for(c = 0; c < UIP_CONNS; ++c) {\r
-    uip_conns[c].tcpstateflags = UIP_CLOSED;\r
-  }\r
-#if UIP_ACTIVE_OPEN\r
-  lastport = 1024;\r
-#endif /* UIP_ACTIVE_OPEN */\r
-\r
-#if UIP_UDP\r
-  for(c = 0; c < UIP_UDP_CONNS; ++c) {\r
-    uip_udp_conns[c].lport = 0;\r
-  }\r
-#endif /* UIP_UDP */\r
-  \r
-\r
-  /* IPv4 initialization. */\r
-#if UIP_FIXEDADDR == 0\r
-  /*  uip_hostaddr[0] = uip_hostaddr[1] = 0;*/\r
-#endif /* UIP_FIXEDADDR */\r
-\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-#if UIP_ACTIVE_OPEN\r
-struct uip_conn *\r
-uip_connect(uip_ipaddr_t *ripaddr, u16_t rport)\r
-{\r
-  register struct uip_conn *conn, *cconn;\r
-  \r
-  /* Find an unused local port. */\r
- again:\r
-  ++lastport;\r
-\r
-  if(lastport >= 32000) {\r
-    lastport = 4096;\r
-  }\r
-\r
-  /* Check if this port is already in use, and if so try to find\r
-     another one. */\r
-  for(c = 0; c < UIP_CONNS; ++c) {\r
-    conn = &uip_conns[c];\r
-    if(conn->tcpstateflags != UIP_CLOSED &&\r
-       conn->lport == htons(lastport)) {\r
-      goto again;\r
-    }\r
-  }\r
-\r
-  conn = 0;\r
-  for(c = 0; c < UIP_CONNS; ++c) {\r
-    cconn = &uip_conns[c];\r
-    if(cconn->tcpstateflags == UIP_CLOSED) {\r
-      conn = cconn;\r
-      break;\r
-    }\r
-    if(cconn->tcpstateflags == UIP_TIME_WAIT) {\r
-      if(conn == 0 ||\r
-        cconn->timer > conn->timer) {\r
-       conn = cconn;\r
-      }\r
-    }\r
-  }\r
-\r
-  if(conn == 0) {\r
-    return 0;\r
-  }\r
-  \r
-  conn->tcpstateflags = UIP_SYN_SENT;\r
-\r
-  conn->snd_nxt[0] = iss[0];\r
-  conn->snd_nxt[1] = iss[1];\r
-  conn->snd_nxt[2] = iss[2];\r
-  conn->snd_nxt[3] = iss[3];\r
-\r
-  conn->initialmss = conn->mss = UIP_TCP_MSS;\r
-  \r
-  conn->len = 1;   /* TCP length of the SYN is one. */\r
-  conn->nrtx = 0;\r
-  conn->timer = 1; /* Send the SYN next time around. */\r
-  conn->rto = UIP_RTO;\r
-  conn->sa = 0;\r
-  conn->sv = 16;   /* Initial value of the RTT variance. */\r
-  conn->lport = htons(lastport);\r
-  conn->rport = rport;\r
-  uip_ipaddr_copy(&conn->ripaddr, ripaddr);\r
-  \r
-  return conn;\r
-}\r
-#endif /* UIP_ACTIVE_OPEN */\r
-/*---------------------------------------------------------------------------*/\r
-#if UIP_UDP\r
-struct uip_udp_conn *\r
-uip_udp_new(const uip_ipaddr_t *ripaddr, u16_t rport)\r
-{\r
-  register struct uip_udp_conn *conn;\r
-  \r
-  /* Find an unused local port. */\r
- again:\r
-  ++lastport;\r
-\r
-  if(lastport >= 32000) {\r
-    lastport = 4096;\r
-  }\r
-  \r
-  for(c = 0; c < UIP_UDP_CONNS; ++c) {\r
-    if(uip_udp_conns[c].lport == htons(lastport)) {\r
-      goto again;\r
-    }\r
-  }\r
-\r
-\r
-  conn = 0;\r
-  for(c = 0; c < UIP_UDP_CONNS; ++c) {\r
-    if(uip_udp_conns[c].lport == 0) {\r
-      conn = &uip_udp_conns[c];\r
-      break;\r
-    }\r
-  }\r
-\r
-  if(conn == 0) {\r
-    return 0;\r
-  }\r
-  \r
-  conn->lport = HTONS(lastport);\r
-  conn->rport = rport;\r
-  if(ripaddr == NULL) {\r
-    memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t));\r
-  } else {\r
-    uip_ipaddr_copy(&conn->ripaddr, ripaddr);\r
-  }\r
-  conn->ttl = UIP_TTL;\r
-  \r
-  return conn;\r
-}\r
-#endif /* UIP_UDP */\r
-/*---------------------------------------------------------------------------*/\r
-void\r
-uip_unlisten(u16_t port)\r
-{\r
-  for(c = 0; c < UIP_LISTENPORTS; ++c) {\r
-    if(uip_listenports[c] == port) {\r
-      uip_listenports[c] = 0;\r
-      return;\r
-    }\r
-  }\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-void\r
-uip_listen(u16_t port)\r
-{\r
-  for(c = 0; c < UIP_LISTENPORTS; ++c) {\r
-    if(uip_listenports[c] == 0) {\r
-      uip_listenports[c] = port;\r
-      return;\r
-    }\r
-  }\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-/* XXX: IP fragment reassembly: not well-tested. */\r
-\r
-#if UIP_REASSEMBLY && !UIP_CONF_IPV6\r
-#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)\r
-static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];\r
-static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];\r
-static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,\r
-                                   0x0f, 0x07, 0x03, 0x01};\r
-static u16_t uip_reasslen;\r
-static u8_t uip_reassflags;\r
-#define UIP_REASS_FLAG_LASTFRAG 0x01\r
-static u8_t uip_reasstmr;\r
-\r
-#define IP_MF   0x20\r
-\r
-static u8_t\r
-uip_reass(void)\r
-{\r
-  u16_t offset, len;\r
-  u16_t i;\r
-\r
-  /* If ip_reasstmr is zero, no packet is present in the buffer, so we\r
-     write the IP header of the fragment into the reassembly\r
-     buffer. The timer is updated with the maximum age. */\r
-  if(uip_reasstmr == 0) {\r
-    memcpy(uip_reassbuf, &BUF->vhl, UIP_IPH_LEN);\r
-    uip_reasstmr = UIP_REASS_MAXAGE;\r
-    uip_reassflags = 0;\r
-    /* Clear the bitmap. */\r
-    memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap));\r
-  }\r
-\r
-  /* Check if the incoming fragment matches the one currently present\r
-     in the reasembly buffer. If so, we proceed with copying the\r
-     fragment into the buffer. */\r
-  if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&\r
-     BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&\r
-     BUF->destipaddr[0] == FBUF->destipaddr[0] &&\r
-     BUF->destipaddr[1] == FBUF->destipaddr[1] &&\r
-     BUF->ipid[0] == FBUF->ipid[0] &&\r
-     BUF->ipid[1] == FBUF->ipid[1]) {\r
-\r
-    len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;\r
-    offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;\r
-\r
-    /* If the offset or the offset + fragment length overflows the\r
-       reassembly buffer, we discard the entire packet. */\r
-    if(offset > UIP_REASS_BUFSIZE ||\r
-       offset + len > UIP_REASS_BUFSIZE) {\r
-      uip_reasstmr = 0;\r
-      goto nullreturn;\r
-    }\r
-\r
-    /* Copy the fragment into the reassembly buffer, at the right\r
-       offset. */\r
-    memcpy(&uip_reassbuf[UIP_IPH_LEN + offset],\r
-          (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),\r
-          len);\r
-      \r
-    /* Update the bitmap. */\r
-    if(offset / (8 * 8) == (offset + len) / (8 * 8)) {\r
-      /* If the two endpoints are in the same byte, we only update\r
-        that byte. */\r
-            \r
-      uip_reassbitmap[offset / (8 * 8)] |=\r
-            bitmap_bits[(offset / 8 ) & 7] &\r
-            ~bitmap_bits[((offset + len) / 8 ) & 7];\r
-    } else {\r
-      /* If the two endpoints are in different bytes, we update the\r
-        bytes in the endpoints and fill the stuff inbetween with\r
-        0xff. */\r
-      uip_reassbitmap[offset / (8 * 8)] |=\r
-       bitmap_bits[(offset / 8 ) & 7];\r
-      for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {\r
-       uip_reassbitmap[i] = 0xff;\r
-      }\r
-      uip_reassbitmap[(offset + len) / (8 * 8)] |=\r
-       ~bitmap_bits[((offset + len) / 8 ) & 7];\r
-    }\r
-    \r
-    /* If this fragment has the More Fragments flag set to zero, we\r
-       know that this is the last fragment, so we can calculate the\r
-       size of the entire packet. We also set the\r
-       IP_REASS_FLAG_LASTFRAG flag to indicate that we have received\r
-       the final fragment. */\r
-\r
-    if((BUF->ipoffset[0] & IP_MF) == 0) {\r
-      uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;\r
-      uip_reasslen = offset + len;\r
-    }\r
-    \r
-    /* Finally, we check if we have a full packet in the buffer. We do\r
-       this by checking if we have the last fragment and if all bits\r
-       in the bitmap are set. */\r
-    if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {\r
-      /* Check all bytes up to and including all but the last byte in\r
-        the bitmap. */\r
-      for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {\r
-       if(uip_reassbitmap[i] != 0xff) {\r
-         goto nullreturn;\r
-       }\r
-      }\r
-      /* Check the last byte in the bitmap. It should contain just the\r
-        right amount of bits. */\r
-      if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=\r
-        (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {\r
-       goto nullreturn;\r
-      }\r
-\r
-      /* If we have come this far, we have a full packet in the\r
-        buffer, so we allocate a pbuf and copy the packet into it. We\r
-        also reset the timer. */\r
-      uip_reasstmr = 0;\r
-      memcpy(BUF, FBUF, uip_reasslen);\r
-\r
-      /* Pretend to be a "normal" (i.e., not fragmented) IP packet\r
-        from now on. */\r
-      BUF->ipoffset[0] = BUF->ipoffset[1] = 0;\r
-      BUF->len[0] = uip_reasslen >> 8;\r
-      BUF->len[1] = uip_reasslen & 0xff;\r
-      BUF->ipchksum = 0;\r
-      BUF->ipchksum = ~(uip_ipchksum());\r
-\r
-      return uip_reasslen;\r
-    }\r
-  }\r
-\r
- nullreturn:\r
-  return 0;\r
-}\r
-#endif /* UIP_REASSEMBLY */\r
-/*---------------------------------------------------------------------------*/\r
-static void\r
-uip_add_rcv_nxt(u16_t n)\r
-{\r
-  uip_add32(uip_conn->rcv_nxt, n);\r
-  uip_conn->rcv_nxt[0] = uip_acc32[0];\r
-  uip_conn->rcv_nxt[1] = uip_acc32[1];\r
-  uip_conn->rcv_nxt[2] = uip_acc32[2];\r
-  uip_conn->rcv_nxt[3] = uip_acc32[3];\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-void\r
-uip_process(u8_t flag)\r
-{\r
-  register struct uip_conn *uip_connr = uip_conn;\r
-\r
-#if UIP_UDP\r
-  if(flag == UIP_UDP_SEND_CONN) {\r
-    goto udp_send;\r
-  }\r
-#endif /* UIP_UDP */\r
-  \r
-  uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];\r
-\r
-  /* Check if we were invoked because of a poll request for a\r
-     particular connection. */\r
-  if(flag == UIP_POLL_REQUEST) {\r
-    if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&\r
-       !uip_outstanding(uip_connr)) {\r
-       uip_len = uip_slen = 0;\r
-       uip_flags = UIP_POLL;\r
-       UIP_APPCALL();\r
-       goto appsend;\r
-    }\r
-    goto drop;\r
-    \r
-    /* Check if we were invoked because of the perodic timer fireing. */\r
-  } else if(flag == UIP_TIMER) {\r
-#if UIP_REASSEMBLY\r
-    if(uip_reasstmr != 0) {\r
-      --uip_reasstmr;\r
-    }\r
-#endif /* UIP_REASSEMBLY */\r
-    /* Increase the initial sequence number. */\r
-    if(++iss[3] == 0) {\r
-      if(++iss[2] == 0) {\r
-       if(++iss[1] == 0) {\r
-         ++iss[0];\r
-       }\r
-      }\r
-    }\r
-\r
-    /* Reset the length variables. */\r
-    uip_len = 0;\r
-    uip_slen = 0;\r
-\r
-    /* Check if the connection is in a state in which we simply wait\r
-       for the connection to time out. If so, we increase the\r
-       connection's timer and remove the connection if it times\r
-       out. */\r
-    if(uip_connr->tcpstateflags == UIP_TIME_WAIT ||\r
-       uip_connr->tcpstateflags == UIP_FIN_WAIT_2) {\r
-      ++(uip_connr->timer);\r
-      if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {\r
-       uip_connr->tcpstateflags = UIP_CLOSED;\r
-      }\r
-    } else if(uip_connr->tcpstateflags != UIP_CLOSED) {\r
-      /* If the connection has outstanding data, we increase the\r
-        connection's timer and see if it has reached the RTO value\r
-        in which case we retransmit. */\r
-      if(uip_outstanding(uip_connr)) {\r
-       if(uip_connr->timer-- == 0) {\r
-         if(uip_connr->nrtx == UIP_MAXRTX ||\r
-            ((uip_connr->tcpstateflags == UIP_SYN_SENT ||\r
-              uip_connr->tcpstateflags == UIP_SYN_RCVD) &&\r
-             uip_connr->nrtx == UIP_MAXSYNRTX)) {\r
-           uip_connr->tcpstateflags = UIP_CLOSED;\r
-\r
-           /* We call UIP_APPCALL() with uip_flags set to\r
-              UIP_TIMEDOUT to inform the application that the\r
-              connection has timed out. */\r
-           uip_flags = UIP_TIMEDOUT;\r
-           UIP_APPCALL();\r
-\r
-           /* We also send a reset packet to the remote host. */\r
-           BUF->flags = TCP_RST | TCP_ACK;\r
-           goto tcp_send_nodata;\r
-         }\r
-\r
-         /* Exponential backoff. */\r
-         uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?\r
-                                        4:\r
-                                        uip_connr->nrtx);\r
-         ++(uip_connr->nrtx);\r
-         \r
-         /* Ok, so we need to retransmit. We do this differently\r
-            depending on which state we are in. In ESTABLISHED, we\r
-            call upon the application so that it may prepare the\r
-            data for the retransmit. In SYN_RCVD, we resend the\r
-            SYNACK that we sent earlier and in LAST_ACK we have to\r
-            retransmit our FINACK. */\r
-         UIP_STAT(++uip_stat.tcp.rexmit);\r
-         switch(uip_connr->tcpstateflags & UIP_TS_MASK) {\r
-         case UIP_SYN_RCVD:\r
-           /* In the SYN_RCVD state, we should retransmit our\r
-               SYNACK. */\r
-           goto tcp_send_synack;\r
-           \r
-#if UIP_ACTIVE_OPEN\r
-         case UIP_SYN_SENT:\r
-           /* In the SYN_SENT state, we retransmit out SYN. */\r
-           BUF->flags = 0;\r
-           goto tcp_send_syn;\r
-#endif /* UIP_ACTIVE_OPEN */\r
-           \r
-         case UIP_ESTABLISHED:\r
-           /* In the ESTABLISHED state, we call upon the application\r
-               to do the actual retransmit after which we jump into\r
-               the code for sending out the packet (the apprexmit\r
-               label). */\r
-           uip_flags = UIP_REXMIT;\r
-           UIP_APPCALL();\r
-           goto apprexmit;\r
-           \r
-         case UIP_FIN_WAIT_1:\r
-         case UIP_CLOSING:\r
-         case UIP_LAST_ACK:\r
-           /* In all these states we should retransmit a FINACK. */\r
-           goto tcp_send_finack;\r
-           \r
-         }\r
-       }\r
-      } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {\r
-       /* If there was no need for a retransmission, we poll the\r
-           application for new data. */\r
-       uip_len = uip_slen = 0;            \r
-       uip_flags = UIP_POLL;\r
-       UIP_APPCALL();\r
-       goto appsend;\r
-      }\r
-    }\r
-    goto drop;\r
-  }\r
-#if UIP_UDP\r
-  if(flag == UIP_UDP_TIMER) {\r
-    if(uip_udp_conn->lport != 0) {\r
-      uip_conn = NULL;\r
-      uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];\r
-      uip_len = uip_slen = 0;\r
-      uip_flags = UIP_POLL;\r
-      UIP_UDP_APPCALL();\r
-      goto udp_send;\r
-    } else {\r
-      goto drop;\r
-    }\r
-  }\r
-#endif\r
-\r
-  /* This is where the input processing starts. */\r
-  UIP_STAT(++uip_stat.ip.recv);\r
-\r
-  /* Start of IP input header processing code. */\r
-  \r
-#if UIP_CONF_IPV6\r
-  /* Check validity of the IP header. */\r
-  if((BUF->vtc & 0xf0) != 0x60)  { /* IP version and header length. */\r
-    UIP_STAT(++uip_stat.ip.drop);\r
-    UIP_STAT(++uip_stat.ip.vhlerr);\r
-    UIP_LOG("ipv6: invalid version.");\r
-    goto drop;\r
-  }\r
-#else /* UIP_CONF_IPV6 */\r
-  /* Check validity of the IP header. */\r
-  if(BUF->vhl != 0x45)  { /* IP version and header length. */\r
-    UIP_STAT(++uip_stat.ip.drop);\r
-    UIP_STAT(++uip_stat.ip.vhlerr);\r
-    UIP_LOG("ip: invalid version or header length.");\r
-    goto drop;\r
-  }\r
-#endif /* UIP_CONF_IPV6 */\r
-  \r
-  /* Check the size of the packet. If the size reported to us in\r
-     uip_len is smaller the size reported in the IP header, we assume\r
-     that the packet has been corrupted in transit. If the size of\r
-     uip_len is larger than the size reported in the IP packet header,\r
-     the packet has been padded and we set uip_len to the correct\r
-     value.. */\r
-\r
-  if((BUF->len[0] << 8) + BUF->len[1] <= uip_len) {\r
-    uip_len = (BUF->len[0] << 8) + BUF->len[1];\r
-#if UIP_CONF_IPV6\r
-    uip_len += 40; /* The length reported in the IPv6 header is the\r
-                     length of the payload that follows the\r
-                     header. However, uIP uses the uip_len variable\r
-                     for holding the size of the entire packet,\r
-                     including the IP header. For IPv4 this is not a\r
-                     problem as the length field in the IPv4 header\r
-                     contains the length of the entire packet. But\r
-                     for IPv6 we need to add the size of the IPv6\r
-                     header (40 bytes). */\r
-#endif /* UIP_CONF_IPV6 */\r
-  } else {\r
-    UIP_LOG("ip: packet shorter than reported in IP header.");\r
-    goto drop;\r
-  }\r
-\r
-#if !UIP_CONF_IPV6\r
-  /* Check the fragment flag. */\r
-  if((BUF->ipoffset[0] & 0x3f) != 0 ||\r
-     BUF->ipoffset[1] != 0) {\r
-#if UIP_REASSEMBLY\r
-    uip_len = uip_reass();\r
-    if(uip_len == 0) {\r
-      goto drop;\r
-    }\r
-#else /* UIP_REASSEMBLY */\r
-    UIP_STAT(++uip_stat.ip.drop);\r
-    UIP_STAT(++uip_stat.ip.fragerr);\r
-    UIP_LOG("ip: fragment dropped.");\r
-    goto drop;\r
-#endif /* UIP_REASSEMBLY */\r
-  }\r
-#endif /* UIP_CONF_IPV6 */\r
-\r
-  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {\r
-    /* If we are configured to use ping IP address configuration and\r
-       hasn't been assigned an IP address yet, we accept all ICMP\r
-       packets. */\r
-#if UIP_PINGADDRCONF && !UIP_CONF_IPV6\r
-    if(BUF->proto == UIP_PROTO_ICMP) {\r
-      UIP_LOG("ip: possible ping config packet received.");\r
-      goto icmp_input;\r
-    } else {\r
-      UIP_LOG("ip: packet dropped since no address assigned.");\r
-      goto drop;\r
-    }\r
-#endif /* UIP_PINGADDRCONF */\r
-\r
-  } else {\r
-    /* If IP broadcast support is configured, we check for a broadcast\r
-       UDP packet, which may be destined to us. */\r
-#if UIP_BROADCAST\r
-    DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum());\r
-    if(BUF->proto == UIP_PROTO_UDP &&\r
-       uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)\r
-       /*&&\r
-        uip_ipchksum() == 0xffff*/) {\r
-      goto udp_input;\r
-    }\r
-#endif /* UIP_BROADCAST */\r
-    \r
-    /* Check if the packet is destined for our IP address. */\r
-#if !UIP_CONF_IPV6\r
-    if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {\r
-      UIP_STAT(++uip_stat.ip.drop);\r
-      goto drop;\r
-    }\r
-#else /* UIP_CONF_IPV6 */\r
-    /* For IPv6, packet reception is a little trickier as we need to\r
-       make sure that we listen to certain multicast addresses (all\r
-       hosts multicast address, and the solicited-node multicast\r
-       address) as well. However, we will cheat here and accept all\r
-       multicast packets that are sent to the ff02::/16 addresses. */\r
-    if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr) &&\r
-       BUF->destipaddr.u16[0] != HTONS(0xff02)) {\r
-      UIP_STAT(++uip_stat.ip.drop);\r
-      goto drop;\r
-    }\r
-#endif /* UIP_CONF_IPV6 */\r
-  }\r
-\r
-#if !UIP_CONF_IPV6\r
-  if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header\r
-                                   checksum. */\r
-    UIP_STAT(++uip_stat.ip.drop);\r
-    UIP_STAT(++uip_stat.ip.chkerr);\r
-    UIP_LOG("ip: bad checksum.");\r
-    goto drop;\r
-  }\r
-#endif /* UIP_CONF_IPV6 */\r
-\r
-  if(BUF->proto == UIP_PROTO_TCP) { /* Check for TCP packet. If so,\r
-                                      proceed with TCP input\r
-                                      processing. */\r
-    goto tcp_input;\r
-  }\r
-\r
-#if UIP_UDP\r
-  if(BUF->proto == UIP_PROTO_UDP) {\r
-    goto udp_input;\r
-  }\r
-#endif /* UIP_UDP */\r
-\r
-#if !UIP_CONF_IPV6\r
-  /* ICMPv4 processing code follows. */\r
-  if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from\r
-                                       here. */\r
-    UIP_STAT(++uip_stat.ip.drop);\r
-    UIP_STAT(++uip_stat.ip.protoerr);\r
-    UIP_LOG("ip: neither tcp nor icmp.");\r
-    goto drop;\r
-  }\r
-\r
-#if UIP_PINGADDRCONF\r
- icmp_input:\r
-#endif /* UIP_PINGADDRCONF */\r
-  UIP_STAT(++uip_stat.icmp.recv);\r
-\r
-  /* ICMP echo (i.e., ping) processing. This is simple, we only change\r
-     the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP\r
-     checksum before we return the packet. */\r
-  if(ICMPBUF->type != ICMP_ECHO) {\r
-    UIP_STAT(++uip_stat.icmp.drop);\r
-    UIP_STAT(++uip_stat.icmp.typeerr);\r
-    UIP_LOG("icmp: not icmp echo.");\r
-    goto drop;\r
-  }\r
-\r
-  /* If we are configured to use ping IP address assignment, we use\r
-     the destination IP address of this ping packet and assign it to\r
-     ourself. */\r
-#if UIP_PINGADDRCONF\r
-  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {\r
-    uip_hostaddr = BUF->destipaddr;\r
-  }\r
-#endif /* UIP_PINGADDRCONF */\r
-\r
-  ICMPBUF->type = ICMP_ECHO_REPLY;\r
-\r
-  if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {\r
-    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;\r
-  } else {\r
-    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);\r
-  }\r
-\r
-  /* Swap IP addresses. */\r
-  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);\r
-  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);\r
-\r
-  UIP_STAT(++uip_stat.icmp.sent);\r
-  BUF->ttl = UIP_TTL;\r
-  goto ip_send_nolen;\r
-\r
-  /* End of IPv4 input header processing code. */\r
-#else /* !UIP_CONF_IPV6 */\r
-\r
-  /* This is IPv6 ICMPv6 processing code. */\r
-  DEBUG_PRINTF("icmp6_input: length %d\n", uip_len);\r
-\r
-  if(BUF->proto != UIP_PROTO_ICMP6) { /* We only allow ICMPv6 packets from\r
-                                        here. */\r
-    UIP_STAT(++uip_stat.ip.drop);\r
-    UIP_STAT(++uip_stat.ip.protoerr);\r
-    UIP_LOG("ip: neither tcp nor icmp6.");\r
-    goto drop;\r
-  }\r
-\r
-  UIP_STAT(++uip_stat.icmp.recv);\r
-\r
-  /* If we get a neighbor solicitation for our address we should send\r
-     a neighbor advertisement message back. */\r
-  if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {\r
-    if(uip_ipaddr_cmp(&ICMPBUF->icmp6data, &uip_hostaddr)) {\r
-\r
-      if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {\r
-       /* Save the sender's address in our neighbor list. */\r
-       uip_neighbor_add(&ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));\r
-      }\r
-      \r
-      /* We should now send a neighbor advertisement back to where the\r
-        neighbor solicication came from. */\r
-      ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT;\r
-      ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */\r
-      \r
-      ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;\r
-      \r
-      uip_ipaddr_copy(&ICMPBUF->destipaddr, &ICMPBUF->srcipaddr);\r
-      uip_ipaddr_copy(&ICMPBUF->srcipaddr, &uip_hostaddr);\r
-      ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;\r
-      ICMPBUF->options[1] = 1;  /* Options length, 1 = 8 bytes. */\r
-      memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr));\r
-      ICMPBUF->icmpchksum = 0;\r
-      ICMPBUF->icmpchksum = ~uip_icmp6chksum();\r
-      \r
-      goto send;\r
-      \r
-    }\r
-    goto drop;\r
-  } else if(ICMPBUF->type == ICMP6_ECHO) {\r
-    /* ICMP echo (i.e., ping) processing. This is simple, we only\r
-       change the ICMP type from ECHO to ECHO_REPLY and update the\r
-       ICMP checksum before we return the packet. */\r
-\r
-    ICMPBUF->type = ICMP6_ECHO_REPLY;\r
-    \r
-    uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);\r
-    uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);\r
-    ICMPBUF->icmpchksum = 0;\r
-    ICMPBUF->icmpchksum = ~uip_icmp6chksum();\r
-    \r
-    UIP_STAT(++uip_stat.icmp.sent);\r
-    goto send;\r
-  } else {\r
-    DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type);\r
-    UIP_STAT(++uip_stat.icmp.drop);\r
-    UIP_STAT(++uip_stat.icmp.typeerr);\r
-    UIP_LOG("icmp: unknown ICMP message.");\r
-    goto drop;\r
-  }\r
-\r
-  /* End of IPv6 ICMP processing. */\r
-  \r
-#endif /* !UIP_CONF_IPV6 */\r
-\r
-#if UIP_UDP\r
-  /* UDP input processing. */\r
- udp_input:\r
-  /* UDP processing is really just a hack. We don't do anything to the\r
-     UDP/IP headers, but let the UDP application do all the hard\r
-     work. If the application sets uip_slen, it has a packet to\r
-     send. */\r
-#if UIP_UDP_CHECKSUMS\r
-  uip_len = uip_len - UIP_IPUDPH_LEN;\r
-  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];\r
-  if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {\r
-    UIP_STAT(++uip_stat.udp.drop);\r
-    UIP_STAT(++uip_stat.udp.chkerr);\r
-    UIP_LOG("udp: bad checksum.");\r
-    goto drop;\r
-  }\r
-#else /* UIP_UDP_CHECKSUMS */\r
-  uip_len = uip_len - UIP_IPUDPH_LEN;\r
-#endif /* UIP_UDP_CHECKSUMS */\r
-\r
-  /* Demultiplex this UDP packet between the UDP "connections". */\r
-  for(uip_udp_conn = &uip_udp_conns[0];\r
-      uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];\r
-      ++uip_udp_conn) {\r
-    /* If the local UDP port is non-zero, the connection is considered\r
-       to be used. If so, the local port number is checked against the\r
-       destination port number in the received packet. If the two port\r
-       numbers match, the remote port number is checked if the\r
-       connection is bound to a remote port. Finally, if the\r
-       connection is bound to a remote IP address, the source IP\r
-       address of the packet is checked. */\r
-    if(uip_udp_conn->lport != 0 &&\r
-       UDPBUF->destport == uip_udp_conn->lport &&\r
-       (uip_udp_conn->rport == 0 ||\r
-        UDPBUF->srcport == uip_udp_conn->rport) &&\r
-       (uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) ||\r
-       uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) ||\r
-       uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr))) {\r
-      goto udp_found;\r
-    }\r
-  }\r
-  UIP_LOG("udp: no matching connection found");\r
-#if UIP_CONF_ICMP_DEST_UNREACH && !UIP_CONF_IPV6\r
-  /* Copy fields from packet header into payload of this ICMP packet. */\r
-  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);\r
-\r
-  /* Set the ICMP type and code. */\r
-  ICMPBUF->type = ICMP_DEST_UNREACHABLE;\r
-  ICMPBUF->icode = ICMP_PORT_UNREACHABLE;\r
-\r
-  /* Calculate the ICMP checksum. */\r
-  ICMPBUF->icmpchksum = 0;\r
-  ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36);\r
-\r
-  /* Set the IP destination address to be the source address of the\r
-     original packet. */\r
-  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);\r
-\r
-  /* Set our IP address as the source address. */\r
-  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);\r
-\r
-  /* The size of the ICMP destination unreachable packet is 36 + the\r
-     size of the IP header (20) = 56. */\r
-  uip_len = 36 + UIP_IPH_LEN;\r
-  ICMPBUF->len[0] = 0;\r
-  ICMPBUF->len[1] = (u8_t)uip_len;\r
-  ICMPBUF->ttl = UIP_TTL;\r
-  ICMPBUF->proto = UIP_PROTO_ICMP;\r
-\r
-  goto ip_send_nolen;\r
-#else /* UIP_CONF_ICMP_DEST_UNREACH */\r
-  goto drop;\r
-#endif /* UIP_CONF_ICMP_DEST_UNREACH */\r
-  \r
- udp_found:\r
-  uip_conn = NULL;\r
-  uip_flags = UIP_NEWDATA;\r
-  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];\r
-  uip_slen = 0;\r
-  UIP_UDP_APPCALL();\r
-\r
- udp_send:\r
-  if(uip_slen == 0) {\r
-    goto drop;\r
-  }\r
-  uip_len = uip_slen + UIP_IPUDPH_LEN;\r
-\r
-#if UIP_CONF_IPV6\r
-  /* For IPv6, the IP length field does not include the IPv6 IP header\r
-     length. */\r
-  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);\r
-  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);\r
-#else /* UIP_CONF_IPV6 */\r
-  BUF->len[0] = (uip_len >> 8);\r
-  BUF->len[1] = (uip_len & 0xff);\r
-#endif /* UIP_CONF_IPV6 */\r
-\r
-  BUF->ttl = uip_udp_conn->ttl;\r
-  BUF->proto = UIP_PROTO_UDP;\r
-\r
-  UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);\r
-  UDPBUF->udpchksum = 0;\r
-\r
-  BUF->srcport  = uip_udp_conn->lport;\r
-  BUF->destport = uip_udp_conn->rport;\r
-\r
-  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);\r
-  uip_ipaddr_copy(&BUF->destipaddr, &uip_udp_conn->ripaddr);\r
-   \r
-  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];\r
-\r
-#if UIP_UDP_CHECKSUMS\r
-  /* Calculate UDP checksum. */\r
-  UDPBUF->udpchksum = ~(uip_udpchksum());\r
-  if(UDPBUF->udpchksum == 0) {\r
-    UDPBUF->udpchksum = 0xffff;\r
-  }\r
-#endif /* UIP_UDP_CHECKSUMS */\r
-  \r
-  goto ip_send_nolen;\r
-#endif /* UIP_UDP */\r
-  \r
-  /* TCP input processing. */\r
- tcp_input:\r
-  UIP_STAT(++uip_stat.tcp.recv);\r
-\r
-  /* Start of TCP input header processing code. */\r
-  \r
-  if(uip_tcpchksum() != 0xffff) {   /* Compute and check the TCP\r
-                                      checksum. */\r
-    UIP_STAT(++uip_stat.tcp.drop);\r
-    UIP_STAT(++uip_stat.tcp.chkerr);\r
-    UIP_LOG("tcp: bad checksum.");\r
-    goto drop;\r
-  }\r
-  \r
-  /* Demultiplex this segment. */\r
-  /* First check any active connections. */\r
-  for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];\r
-      ++uip_connr) {\r
-    if(uip_connr->tcpstateflags != UIP_CLOSED &&\r
-       BUF->destport == uip_connr->lport &&\r
-       BUF->srcport == uip_connr->rport &&\r
-       uip_ipaddr_cmp(&BUF->srcipaddr, &uip_connr->ripaddr)) {\r
-      goto found;\r
-    }\r
-  }\r
-\r
-  /* If we didn't find and active connection that expected the packet,\r
-     either this packet is an old duplicate, or this is a SYN packet\r
-     destined for a connection in LISTEN. If the SYN flag isn't set,\r
-     it is an old packet and we send a RST. */\r
-  if((BUF->flags & TCP_CTL) != TCP_SYN) {\r
-    goto reset;\r
-  }\r
-  \r
-  tmp16 = BUF->destport;\r
-  /* Next, check listening connections. */\r
-  for(c = 0; c < UIP_LISTENPORTS; ++c) {\r
-    if(tmp16 == uip_listenports[c]) {\r
-      goto found_listen;\r
-    }\r
-  }\r
-  \r
-  /* No matching connection found, so we send a RST packet. */\r
-  UIP_STAT(++uip_stat.tcp.synrst);\r
-\r
- reset:\r
-  /* We do not send resets in response to resets. */\r
-  if(BUF->flags & TCP_RST) {\r
-    goto drop;\r
-  }\r
-\r
-  UIP_STAT(++uip_stat.tcp.rst);\r
-  \r
-  BUF->flags = TCP_RST | TCP_ACK;\r
-  uip_len = UIP_IPTCPH_LEN;\r
-  BUF->tcpoffset = 5 << 4;\r
-\r
-  /* Flip the seqno and ackno fields in the TCP header. */\r
-  c = BUF->seqno[3];\r
-  BUF->seqno[3] = BUF->ackno[3];\r
-  BUF->ackno[3] = c;\r
-  \r
-  c = BUF->seqno[2];\r
-  BUF->seqno[2] = BUF->ackno[2];\r
-  BUF->ackno[2] = c;\r
-  \r
-  c = BUF->seqno[1];\r
-  BUF->seqno[1] = BUF->ackno[1];\r
-  BUF->ackno[1] = c;\r
-  \r
-  c = BUF->seqno[0];\r
-  BUF->seqno[0] = BUF->ackno[0];\r
-  BUF->ackno[0] = c;\r
-\r
-  /* We also have to increase the sequence number we are\r
-     acknowledging. If the least significant byte overflowed, we need\r
-     to propagate the carry to the other bytes as well. */\r
-  if(++BUF->ackno[3] == 0) {\r
-    if(++BUF->ackno[2] == 0) {\r
-      if(++BUF->ackno[1] == 0) {\r
-       ++BUF->ackno[0];\r
-      }\r
-    }\r
-  }\r
\r
-  /* Swap port numbers. */\r
-  tmp16 = BUF->srcport;\r
-  BUF->srcport = BUF->destport;\r
-  BUF->destport = tmp16;\r
-  \r
-  /* Swap IP addresses. */\r
-  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);\r
-  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);\r
-  \r
-  /* And send out the RST packet! */\r
-  goto tcp_send_noconn;\r
-\r
-  /* This label will be jumped to if we matched the incoming packet\r
-     with a connection in LISTEN. In that case, we should create a new\r
-     connection and send a SYNACK in return. */\r
- found_listen:\r
-  /* First we check if there are any connections avaliable. Unused\r
-     connections are kept in the same table as used connections, but\r
-     unused ones have the tcpstate set to CLOSED. Also, connections in\r
-     TIME_WAIT are kept track of and we'll use the oldest one if no\r
-     CLOSED connections are found. Thanks to Eddie C. Dost for a very\r
-     nice algorithm for the TIME_WAIT search. */\r
-  uip_connr = 0;\r
-  for(c = 0; c < UIP_CONNS; ++c) {\r
-    if(uip_conns[c].tcpstateflags == UIP_CLOSED) {\r
-      uip_connr = &uip_conns[c];\r
-      break;\r
-    }\r
-    if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {\r
-      if(uip_connr == 0 ||\r
-        uip_conns[c].timer > uip_connr->timer) {\r
-       uip_connr = &uip_conns[c];\r
-      }\r
-    }\r
-  }\r
-\r
-  if(uip_connr == 0) {\r
-    /* All connections are used already, we drop packet and hope that\r
-       the remote end will retransmit the packet at a time when we\r
-       have more spare connections. */\r
-    UIP_STAT(++uip_stat.tcp.syndrop);\r
-    UIP_LOG("tcp: found no unused connections.");\r
-    goto drop;\r
-  }\r
-  uip_conn = uip_connr;\r
-  \r
-  /* Fill in the necessary fields for the new connection. */\r
-  uip_connr->rto = uip_connr->timer = UIP_RTO;\r
-  uip_connr->sa = 0;\r
-  uip_connr->sv = 4;\r
-  uip_connr->nrtx = 0;\r
-  uip_connr->lport = BUF->destport;\r
-  uip_connr->rport = BUF->srcport;\r
-  uip_ipaddr_copy(&uip_connr->ripaddr, &BUF->srcipaddr);\r
-  uip_connr->tcpstateflags = UIP_SYN_RCVD;\r
-\r
-  uip_connr->snd_nxt[0] = iss[0];\r
-  uip_connr->snd_nxt[1] = iss[1];\r
-  uip_connr->snd_nxt[2] = iss[2];\r
-  uip_connr->snd_nxt[3] = iss[3];\r
-  uip_connr->len = 1;\r
-\r
-  /* rcv_nxt should be the seqno from the incoming packet + 1. */\r
-  uip_connr->rcv_nxt[3] = BUF->seqno[3];\r
-  uip_connr->rcv_nxt[2] = BUF->seqno[2];\r
-  uip_connr->rcv_nxt[1] = BUF->seqno[1];\r
-  uip_connr->rcv_nxt[0] = BUF->seqno[0];\r
-  uip_add_rcv_nxt(1);\r
-\r
-  /* Parse the TCP MSS option, if present. */\r
-  if((BUF->tcpoffset & 0xf0) > 0x50) {\r
-    for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {\r
-      opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];\r
-      if(opt == TCP_OPT_END) {\r
-       /* End of options. */\r
-       break;\r
-      } else if(opt == TCP_OPT_NOOP) {\r
-       ++c;\r
-       /* NOP option. */\r
-      } else if(opt == TCP_OPT_MSS &&\r
-               uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {\r
-       /* An MSS option with the right option length. */\r
-       tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |\r
-         (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];\r
-       uip_connr->initialmss = uip_connr->mss =\r
-         tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;\r
-       \r
-       /* And we are done processing options. */\r
-       break;\r
-      } else {\r
-       /* All other options have a length field, so that we easily\r
-          can skip past them. */\r
-       if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {\r
-         /* If the length field is zero, the options are malformed\r
-            and we don't process them further. */\r
-         break;\r
-       }\r
-       c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];\r
-      }\r
-    }\r
-  }\r
-  \r
-  /* Our response will be a SYNACK. */\r
-#if UIP_ACTIVE_OPEN\r
- tcp_send_synack:\r
-  BUF->flags = TCP_ACK;\r
-  \r
- tcp_send_syn:\r
-  BUF->flags |= TCP_SYN;\r
-#else /* UIP_ACTIVE_OPEN */\r
- tcp_send_synack:\r
-  BUF->flags = TCP_SYN | TCP_ACK;\r
-#endif /* UIP_ACTIVE_OPEN */\r
-  \r
-  /* We send out the TCP Maximum Segment Size option with our\r
-     SYNACK. */\r
-  BUF->optdata[0] = TCP_OPT_MSS;\r
-  BUF->optdata[1] = TCP_OPT_MSS_LEN;\r
-  BUF->optdata[2] = (UIP_TCP_MSS) / 256;\r
-  BUF->optdata[3] = (UIP_TCP_MSS) & 255;\r
-  uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;\r
-  BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;\r
-  goto tcp_send;\r
-\r
-  /* This label will be jumped to if we found an active connection. */\r
- found:\r
-  uip_conn = uip_connr;\r
-  uip_flags = 0;\r
-  /* We do a very naive form of TCP reset processing; we just accept\r
-     any RST and kill our connection. We should in fact check if the\r
-     sequence number of this reset is wihtin our advertised window\r
-     before we accept the reset. */\r
-  if(BUF->flags & TCP_RST) {\r
-    uip_connr->tcpstateflags = UIP_CLOSED;\r
-    UIP_LOG("tcp: got reset, aborting connection.");\r
-    uip_flags = UIP_ABORT;\r
-    UIP_APPCALL();\r
-    goto drop;\r
-  }\r
-  /* Calculate the length of the data, if the application has sent\r
-     any data to us. */\r
-  c = (BUF->tcpoffset >> 4) << 2;\r
-  /* uip_len will contain the length of the actual TCP data. This is\r
-     calculated by subtracing the length of the TCP header (in\r
-     c) and the length of the IP header (20 bytes). */\r
-  uip_len = uip_len - c - UIP_IPH_LEN;\r
-\r
-  /* First, check if the sequence number of the incoming packet is\r
-     what we're expecting next. If not, we send out an ACK with the\r
-     correct numbers in. */\r
-  if(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&\r
-       ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) {\r
-    if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&\r
-       (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||\r
-       BUF->seqno[1] != uip_connr->rcv_nxt[1] ||\r
-       BUF->seqno[2] != uip_connr->rcv_nxt[2] ||\r
-       BUF->seqno[3] != uip_connr->rcv_nxt[3])) {\r
-      goto tcp_send_ack;\r
-    }\r
-  }\r
-\r
-  /* Next, check if the incoming segment acknowledges any outstanding\r
-     data. If so, we update the sequence number, reset the length of\r
-     the outstanding data, calculate RTT estimations, and reset the\r
-     retransmission timer. */\r
-  if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {\r
-    uip_add32(uip_connr->snd_nxt, uip_connr->len);\r
-\r
-    if(BUF->ackno[0] == uip_acc32[0] &&\r
-       BUF->ackno[1] == uip_acc32[1] &&\r
-       BUF->ackno[2] == uip_acc32[2] &&\r
-       BUF->ackno[3] == uip_acc32[3]) {\r
-      /* Update sequence number. */\r
-      uip_connr->snd_nxt[0] = uip_acc32[0];\r
-      uip_connr->snd_nxt[1] = uip_acc32[1];\r
-      uip_connr->snd_nxt[2] = uip_acc32[2];\r
-      uip_connr->snd_nxt[3] = uip_acc32[3];\r
-       \r
-      /* Do RTT estimation, unless we have done retransmissions. */\r
-      if(uip_connr->nrtx == 0) {\r
-       signed char m;\r
-       m = uip_connr->rto - uip_connr->timer;\r
-       /* This is taken directly from VJs original code in his paper */\r
-       m = m - (uip_connr->sa >> 3);\r
-       uip_connr->sa += m;\r
-       if(m < 0) {\r
-         m = -m;\r
-       }\r
-       m = m - (uip_connr->sv >> 2);\r
-       uip_connr->sv += m;\r
-       uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;\r
-\r
-      }\r
-      /* Set the acknowledged flag. */\r
-      uip_flags = UIP_ACKDATA;\r
-      /* Reset the retransmission timer. */\r
-      uip_connr->timer = uip_connr->rto;\r
-\r
-      /* Reset length of outstanding data. */\r
-      uip_connr->len = 0;\r
-    }\r
-    \r
-  }\r
-\r
-  /* Do different things depending on in what state the connection is. */\r
-  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {\r
-    /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not\r
-       implemented, since we force the application to close when the\r
-       peer sends a FIN (hence the application goes directly from\r
-       ESTABLISHED to LAST_ACK). */\r
-  case UIP_SYN_RCVD:\r
-    /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and\r
-       we are waiting for an ACK that acknowledges the data we sent\r
-       out the last time. Therefore, we want to have the UIP_ACKDATA\r
-       flag set. If so, we enter the ESTABLISHED state. */\r
-    if(uip_flags & UIP_ACKDATA) {\r
-      uip_connr->tcpstateflags = UIP_ESTABLISHED;\r
-      uip_flags = UIP_CONNECTED;\r
-      uip_connr->len = 0;\r
-      if(uip_len > 0) {\r
-        uip_flags |= UIP_NEWDATA;\r
-        uip_add_rcv_nxt(uip_len);\r
-      }\r
-      uip_slen = 0;\r
-      UIP_APPCALL();\r
-      goto appsend;\r
-    }\r
-    goto drop;\r
-#if UIP_ACTIVE_OPEN\r
-  case UIP_SYN_SENT:\r
-    /* In SYN_SENT, we wait for a SYNACK that is sent in response to\r
-       our SYN. The rcv_nxt is set to sequence number in the SYNACK\r
-       plus one, and we send an ACK. We move into the ESTABLISHED\r
-       state. */\r
-    if((uip_flags & UIP_ACKDATA) &&\r
-       (BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) {\r
-\r
-      /* Parse the TCP MSS option, if present. */\r
-      if((BUF->tcpoffset & 0xf0) > 0x50) {\r
-       for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {\r
-         opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c];\r
-         if(opt == TCP_OPT_END) {\r
-           /* End of options. */\r
-           break;\r
-         } else if(opt == TCP_OPT_NOOP) {\r
-           ++c;\r
-           /* NOP option. */\r
-         } else if(opt == TCP_OPT_MSS &&\r
-                   uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {\r
-           /* An MSS option with the right option length. */\r
-           tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |\r
-             uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];\r
-           uip_connr->initialmss =\r
-             uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;\r
-\r
-           /* And we are done processing options. */\r
-           break;\r
-         } else {\r
-           /* All other options have a length field, so that we easily\r
-              can skip past them. */\r
-           if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {\r
-             /* If the length field is zero, the options are malformed\r
-                and we don't process them further. */\r
-             break;\r
-           }\r
-           c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];\r
-         }\r
-       }\r
-      }\r
-      uip_connr->tcpstateflags = UIP_ESTABLISHED;\r
-      uip_connr->rcv_nxt[0] = BUF->seqno[0];\r
-      uip_connr->rcv_nxt[1] = BUF->seqno[1];\r
-      uip_connr->rcv_nxt[2] = BUF->seqno[2];\r
-      uip_connr->rcv_nxt[3] = BUF->seqno[3];\r
-      uip_add_rcv_nxt(1);\r
-      uip_flags = UIP_CONNECTED | UIP_NEWDATA;\r
-      uip_connr->len = 0;\r
-      uip_len = 0;\r
-      uip_slen = 0;\r
-      UIP_APPCALL();\r
-      goto appsend;\r
-    }\r
-    /* Inform the application that the connection failed */\r
-    uip_flags = UIP_ABORT;\r
-    UIP_APPCALL();\r
-    /* The connection is closed after we send the RST */\r
-    uip_conn->tcpstateflags = UIP_CLOSED;\r
-    goto reset;\r
-#endif /* UIP_ACTIVE_OPEN */\r
-    \r
-  case UIP_ESTABLISHED:\r
-    /* In the ESTABLISHED state, we call upon the application to feed\r
-    data into the uip_buf. If the UIP_ACKDATA flag is set, the\r
-    application should put new data into the buffer, otherwise we are\r
-    retransmitting an old segment, and the application should put that\r
-    data into the buffer.\r
-\r
-    If the incoming packet is a FIN, we should close the connection on\r
-    this side as well, and we send out a FIN and enter the LAST_ACK\r
-    state. We require that there is no outstanding data; otherwise the\r
-    sequence numbers will be screwed up. */\r
-\r
-    if(BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) {\r
-      if(uip_outstanding(uip_connr)) {\r
-       goto drop;\r
-      }\r
-      uip_add_rcv_nxt(1 + uip_len);\r
-      uip_flags |= UIP_CLOSE;\r
-      if(uip_len > 0) {\r
-       uip_flags |= UIP_NEWDATA;\r
-      }\r
-      UIP_APPCALL();\r
-      uip_connr->len = 1;\r
-      uip_connr->tcpstateflags = UIP_LAST_ACK;\r
-      uip_connr->nrtx = 0;\r
-    tcp_send_finack:\r
-      BUF->flags = TCP_FIN | TCP_ACK;\r
-      goto tcp_send_nodata;\r
-    }\r
-\r
-    /* Check the URG flag. If this is set, the segment carries urgent\r
-       data that we must pass to the application. */\r
-    if((BUF->flags & TCP_URG) != 0) {\r
-#if UIP_URGDATA > 0\r
-      uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];\r
-      if(uip_urglen > uip_len) {\r
-       /* There is more urgent data in the next segment to come. */\r
-       uip_urglen = uip_len;\r
-      }\r
-      uip_add_rcv_nxt(uip_urglen);\r
-      uip_len -= uip_urglen;\r
-      uip_urgdata = uip_appdata;\r
-      uip_appdata += uip_urglen;\r
-    } else {\r
-      uip_urglen = 0;\r
-#else /* UIP_URGDATA > 0 */\r
-      uip_appdata = ((char *)uip_appdata) + ((BUF->urgp[0] << 8) | BUF->urgp[1]);\r
-      uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];\r
-#endif /* UIP_URGDATA > 0 */\r
-    }\r
-\r
-    /* If uip_len > 0 we have TCP data in the packet, and we flag this\r
-       by setting the UIP_NEWDATA flag and update the sequence number\r
-       we acknowledge. If the application has stopped the dataflow\r
-       using uip_stop(), we must not accept any data packets from the\r
-       remote host. */\r
-    if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {\r
-      uip_flags |= UIP_NEWDATA;\r
-      uip_add_rcv_nxt(uip_len);\r
-    }\r
-\r
-    /* Check if the available buffer space advertised by the other end\r
-       is smaller than the initial MSS for this connection. If so, we\r
-       set the current MSS to the window size to ensure that the\r
-       application does not send more data than the other end can\r
-       handle.\r
-\r
-       If the remote host advertises a zero window, we set the MSS to\r
-       the initial MSS so that the application will send an entire MSS\r
-       of data. This data will not be acknowledged by the receiver,\r
-       and the application will retransmit it. This is called the\r
-       "persistent timer" and uses the retransmission mechanim.\r
-    */\r
-    tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];\r
-    if(tmp16 > uip_connr->initialmss ||\r
-       tmp16 == 0) {\r
-      tmp16 = uip_connr->initialmss;\r
-    }\r
-    uip_connr->mss = tmp16;\r
-\r
-    /* If this packet constitutes an ACK for outstanding data (flagged\r
-       by the UIP_ACKDATA flag, we should call the application since it\r
-       might want to send more data. If the incoming packet had data\r
-       from the peer (as flagged by the UIP_NEWDATA flag), the\r
-       application must also be notified.\r
-\r
-       When the application is called, the global variable uip_len\r
-       contains the length of the incoming data. The application can\r
-       access the incoming data through the global pointer\r
-       uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN\r
-       bytes into the uip_buf array.\r
-\r
-       If the application wishes to send any data, this data should be\r
-       put into the uip_appdata and the length of the data should be\r
-       put into uip_len. If the application don't have any data to\r
-       send, uip_len must be set to 0. */\r
-    if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {\r
-      uip_slen = 0;\r
-      UIP_APPCALL();\r
-\r
-    appsend:\r
-      \r
-      if(uip_flags & UIP_ABORT) {\r
-       uip_slen = 0;\r
-       uip_connr->tcpstateflags = UIP_CLOSED;\r
-       BUF->flags = TCP_RST | TCP_ACK;\r
-       goto tcp_send_nodata;\r
-      }\r
-\r
-      if(uip_flags & UIP_CLOSE) {\r
-       uip_slen = 0;\r
-       uip_connr->len = 1;\r
-       uip_connr->tcpstateflags = UIP_FIN_WAIT_1;\r
-       uip_connr->nrtx = 0;\r
-       BUF->flags = TCP_FIN | TCP_ACK;\r
-       goto tcp_send_nodata;\r
-      }\r
-\r
-      /* If uip_slen > 0, the application has data to be sent. */\r
-      if(uip_slen > 0) {\r
-\r
-       /* If the connection has acknowledged data, the contents of\r
-          the ->len variable should be discarded. */\r
-       if((uip_flags & UIP_ACKDATA) != 0) {\r
-         uip_connr->len = 0;\r
-       }\r
-\r
-       /* If the ->len variable is non-zero the connection has\r
-          already data in transit and cannot send anymore right\r
-          now. */\r
-       if(uip_connr->len == 0) {\r
-\r
-         /* The application cannot send more than what is allowed by\r
-            the mss (the minumum of the MSS and the available\r
-            window). */\r
-         if(uip_slen > uip_connr->mss) {\r
-           uip_slen = uip_connr->mss;\r
-         }\r
-\r
-         /* Remember how much data we send out now so that we know\r
-            when everything has been acknowledged. */\r
-         uip_connr->len = uip_slen;\r
-       } else {\r
-\r
-         /* If the application already had unacknowledged data, we\r
-            make sure that the application does not send (i.e.,\r
-            retransmit) out more than it previously sent out. */\r
-         uip_slen = uip_connr->len;\r
-       }\r
-      }\r
-      uip_connr->nrtx = 0;\r
-    apprexmit:\r
-      uip_appdata = uip_sappdata;\r
-      \r
-      /* If the application has data to be sent, or if the incoming\r
-         packet had new data in it, we must send out a packet. */\r
-      if(uip_slen > 0 && uip_connr->len > 0) {\r
-       /* Add the length of the IP and TCP headers. */\r
-       uip_len = uip_connr->len + UIP_TCPIP_HLEN;\r
-       /* We always set the ACK flag in response packets. */\r
-       BUF->flags = TCP_ACK | TCP_PSH;\r
-       /* Send the packet. */\r
-       goto tcp_send_noopts;\r
-      }\r
-      /* If there is no data to send, just send out a pure ACK if\r
-        there is newdata. */\r
-      if(uip_flags & UIP_NEWDATA) {\r
-       uip_len = UIP_TCPIP_HLEN;\r
-       BUF->flags = TCP_ACK;\r
-       goto tcp_send_noopts;\r
-      }\r
-    }\r
-    goto drop;\r
-  case UIP_LAST_ACK:\r
-    /* We can close this connection if the peer has acknowledged our\r
-       FIN. This is indicated by the UIP_ACKDATA flag. */\r
-    if(uip_flags & UIP_ACKDATA) {\r
-      uip_connr->tcpstateflags = UIP_CLOSED;\r
-      uip_flags = UIP_CLOSE;\r
-      UIP_APPCALL();\r
-    }\r
-    break;\r
-    \r
-  case UIP_FIN_WAIT_1:\r
-    /* The application has closed the connection, but the remote host\r
-       hasn't closed its end yet. Thus we do nothing but wait for a\r
-       FIN from the other side. */\r
-    if(uip_len > 0) {\r
-      uip_add_rcv_nxt(uip_len);\r
-    }\r
-    if(BUF->flags & TCP_FIN) {\r
-      if(uip_flags & UIP_ACKDATA) {\r
-       uip_connr->tcpstateflags = UIP_TIME_WAIT;\r
-       uip_connr->timer = 0;\r
-       uip_connr->len = 0;\r
-      } else {\r
-       uip_connr->tcpstateflags = UIP_CLOSING;\r
-      }\r
-      uip_add_rcv_nxt(1);\r
-      uip_flags = UIP_CLOSE;\r
-      UIP_APPCALL();\r
-      goto tcp_send_ack;\r
-    } else if(uip_flags & UIP_ACKDATA) {\r
-      uip_connr->tcpstateflags = UIP_FIN_WAIT_2;\r
-      uip_connr->len = 0;\r
-      goto drop;\r
-    }\r
-    if(uip_len > 0) {\r
-      goto tcp_send_ack;\r
-    }\r
-    goto drop;\r
-      \r
-  case UIP_FIN_WAIT_2:\r
-    if(uip_len > 0) {\r
-      uip_add_rcv_nxt(uip_len);\r
-    }\r
-    if(BUF->flags & TCP_FIN) {\r
-      uip_connr->tcpstateflags = UIP_TIME_WAIT;\r
-      uip_connr->timer = 0;\r
-      uip_add_rcv_nxt(1);\r
-      uip_flags = UIP_CLOSE;\r
-      UIP_APPCALL();\r
-      goto tcp_send_ack;\r
-    }\r
-    if(uip_len > 0) {\r
-      goto tcp_send_ack;\r
-    }\r
-    goto drop;\r
-\r
-  case UIP_TIME_WAIT:\r
-    goto tcp_send_ack;\r
-    \r
-  case UIP_CLOSING:\r
-    if(uip_flags & UIP_ACKDATA) {\r
-      uip_connr->tcpstateflags = UIP_TIME_WAIT;\r
-      uip_connr->timer = 0;\r
-    }\r
-  }\r
-  goto drop;\r
-  \r
-  /* We jump here when we are ready to send the packet, and just want\r
-     to set the appropriate TCP sequence numbers in the TCP header. */\r
- tcp_send_ack:\r
-  BUF->flags = TCP_ACK;\r
-\r
- tcp_send_nodata:\r
-  uip_len = UIP_IPTCPH_LEN;\r
-\r
- tcp_send_noopts:\r
-  BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;\r
-\r
-  /* We're done with the input processing. We are now ready to send a\r
-     reply. Our job is to fill in all the fields of the TCP and IP\r
-     headers before calculating the checksum and finally send the\r
-     packet. */\r
- tcp_send:\r
-  BUF->ackno[0] = uip_connr->rcv_nxt[0];\r
-  BUF->ackno[1] = uip_connr->rcv_nxt[1];\r
-  BUF->ackno[2] = uip_connr->rcv_nxt[2];\r
-  BUF->ackno[3] = uip_connr->rcv_nxt[3];\r
-  \r
-  BUF->seqno[0] = uip_connr->snd_nxt[0];\r
-  BUF->seqno[1] = uip_connr->snd_nxt[1];\r
-  BUF->seqno[2] = uip_connr->snd_nxt[2];\r
-  BUF->seqno[3] = uip_connr->snd_nxt[3];\r
-\r
-  BUF->proto = UIP_PROTO_TCP;\r
-  \r
-  BUF->srcport  = uip_connr->lport;\r
-  BUF->destport = uip_connr->rport;\r
-\r
-  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);\r
-  uip_ipaddr_copy(&BUF->destipaddr, &uip_connr->ripaddr);\r
-\r
-  if(uip_connr->tcpstateflags & UIP_STOPPED) {\r
-    /* If the connection has issued uip_stop(), we advertise a zero\r
-       window so that the remote host will stop sending data. */\r
-    BUF->wnd[0] = BUF->wnd[1] = 0;\r
-  } else {\r
-    BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);\r
-    BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);\r
-  }\r
-\r
- tcp_send_noconn:\r
-  BUF->ttl = UIP_TTL;\r
-#if UIP_CONF_IPV6\r
-  /* For IPv6, the IP length field does not include the IPv6 IP header\r
-     length. */\r
-  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);\r
-  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);\r
-#else /* UIP_CONF_IPV6 */\r
-  BUF->len[0] = (uip_len >> 8);\r
-  BUF->len[1] = (uip_len & 0xff);\r
-#endif /* UIP_CONF_IPV6 */\r
-\r
-  BUF->urgp[0] = BUF->urgp[1] = 0;\r
-  \r
-  /* Calculate TCP checksum. */\r
-  BUF->tcpchksum = 0;\r
-  BUF->tcpchksum = ~(uip_tcpchksum());\r
-\r
- ip_send_nolen:\r
-#if UIP_CONF_IPV6\r
-  BUF->vtc = 0x60;\r
-  BUF->tcflow = 0x00;\r
-  BUF->flow = 0x00;\r
-#else /* UIP_CONF_IPV6 */\r
-  BUF->vhl = 0x45;\r
-  BUF->tos = 0;\r
-  BUF->ipoffset[0] = BUF->ipoffset[1] = 0;\r
-  ++ipid;\r
-  BUF->ipid[0] = ipid >> 8;\r
-  BUF->ipid[1] = ipid & 0xff;\r
-  /* Calculate IP checksum. */\r
-  BUF->ipchksum = 0;\r
-  BUF->ipchksum = ~(uip_ipchksum());\r
-  DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum());\r
-#endif /* UIP_CONF_IPV6 */   \r
-  UIP_STAT(++uip_stat.tcp.sent);\r
-#if UIP_CONF_IPV6\r
- send:\r
-#endif /* UIP_CONF_IPV6 */\r
-  DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len,\r
-              (BUF->len[0] << 8) | BUF->len[1]);\r
-  \r
-  UIP_STAT(++uip_stat.ip.sent);\r
-  /* Return and let the caller do the actual transmission. */\r
-  uip_flags = 0;\r
-  return;\r
-\r
- drop:\r
-  uip_len = 0;\r
-  uip_flags = 0;\r
-  return;\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-u16_t\r
-htons(u16_t val)\r
-{\r
-  return HTONS(val);\r
-}\r
-\r
-u32_t\r
-htonl(u32_t val)\r
-{\r
-  return HTONL(val);\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-void\r
-uip_send(const void *data, int len)\r
-{\r
-  int copylen;\r
-#define MIN(a,b) ((a) < (b)? (a): (b))\r
-  copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN -\r
-               (int)((char *)uip_sappdata - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN]));\r
-  if(copylen > 0) {\r
-    uip_slen = copylen;\r
-    if(data != uip_sappdata) {\r
-      memcpy(uip_sappdata, (data), uip_slen);\r
-    }\r
-  }\r
-}\r
-/*---------------------------------------------------------------------------*/\r
-/** @} */\r
-#endif /* UIP_CONF_IPV6 */\r
+#define DEBUG_PRINTF(...) /*printf(__VA_ARGS__)*/
+
+/**
+ * \addtogroup uip
+ * @{
+ */
+
+/**
+ * \file
+ * The uIP TCP/IP stack code.
+ * \author Adam Dunkels <adam@dunkels.com>
+ */
+
+/*
+ * 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.
+ *
+ * This file is part of the uIP TCP/IP stack.
+ *
+ * $Id: uip.c,v 1.15 2008/10/15 08:08:32 adamdunkels Exp $
+ *
+ */
+
+/*
+ * uIP is a small implementation of the IP, UDP and TCP protocols (as
+ * well as some basic ICMP stuff). The implementation couples the IP,
+ * UDP, TCP and the application layers very tightly. To keep the size
+ * of the compiled code down, this code frequently uses the goto
+ * statement. While it would be possible to break the uip_process()
+ * function into many smaller functions, this would increase the code
+ * size because of the overhead of parameter passing and the fact that
+ * the optimier would not be as efficient.
+ *
+ * The principle is that we have a small buffer, called the uip_buf,
+ * in which the device driver puts an incoming packet. The TCP/IP
+ * stack parses the headers in the packet, and calls the
+ * application. If the remote host has sent data to the application,
+ * this data is present in the uip_buf and the application read the
+ * data from there. It is up to the application to put this data into
+ * a byte stream if needed. The application will not be fed with data
+ * that is out of sequence.
+ *
+ * If the application whishes to send data to the peer, it should put
+ * its data into the uip_buf. The uip_appdata pointer points to the
+ * first available byte. The TCP/IP stack will calculate the
+ * checksums, and fill in the necessary header fields and finally send
+ * the packet back to the peer.
+*/
+
+#include "uip.h"
+#include "uipopt.h"
+#include "uip_arp.h"
+
+#if !UIP_CONF_IPV6 /* If UIP_CONF_IPV6 is defined, we compile the
+                     uip6.c file instead of this one. Therefore
+                     this #ifndef removes the entire compilation
+                     output of the uip.c file */
+
+
+#if UIP_CONF_IPV6
+#include "net/uip-neighbor.h"
+#endif /* UIP_CONF_IPV6 */
+
+#include <string.h>
+
+/*---------------------------------------------------------------------------*/
+/* Variable definitions. */
+
+
+/* The IP address of this host. If it is defined to be fixed (by
+   setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set
+   here. Otherwise, the address */
+#if UIP_FIXEDADDR > 0
+const uip_ipaddr_t uip_hostaddr =
+  { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 };
+const uip_ipaddr_t uip_draddr =
+  { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 };
+const uip_ipaddr_t uip_netmask =
+  { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 };
+#else
+uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask;
+#endif /* UIP_FIXEDADDR */
+
+const uip_ipaddr_t uip_broadcast_addr =
+#if UIP_CONF_IPV6
+  { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
+#else /* UIP_CONF_IPV6 */
+  { { 0xff, 0xff, 0xff, 0xff } };
+#endif /* UIP_CONF_IPV6 */
+const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, /* rest is 0 */ } };
+
+#if UIP_FIXEDETHADDR
+const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
+                                         UIP_ETHADDR1,
+                                         UIP_ETHADDR2,
+                                         UIP_ETHADDR3,
+                                         UIP_ETHADDR4,
+                                         UIP_ETHADDR5}};
+#else
+struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}};
+#endif
+
+#ifndef UIP_CONF_EXTERNAL_BUFFER
+u8_t uip_buf[UIP_BUFSIZE + 2];   /* The packet buffer that contains
+                                   incoming packets. */
+#endif /* UIP_CONF_EXTERNAL_BUFFER */
+
+void *uip_appdata;               /* The uip_appdata pointer points to
+                                   application data. */
+void *uip_sappdata;              /* The uip_appdata pointer points to
+                                   the application data which is to
+                                   be sent. */
+#if UIP_URGDATA > 0
+void *uip_urgdata;               /* The uip_urgdata pointer points to
+                                   urgent data (out-of-band data), if
+                                   present. */
+u16_t uip_urglen, uip_surglen;
+#endif /* UIP_URGDATA > 0 */
+
+u16_t uip_len, uip_slen;
+                             /* The uip_len is either 8 or 16 bits,
+                               depending on the maximum packet
+                               size. */
+
+u8_t uip_flags;     /* The uip_flags variable is used for
+                               communication between the TCP/IP stack
+                               and the application program. */
+struct uip_conn *uip_conn;   /* uip_conn always points to the current
+                               connection. */
+
+struct uip_conn uip_conns[UIP_CONNS];
+                             /* The uip_conns array holds all TCP
+                               connections. */
+u16_t uip_listenports[UIP_LISTENPORTS];
+                             /* The uip_listenports list all currently
+                               listning ports. */
+#if UIP_UDP
+struct uip_udp_conn *uip_udp_conn;
+struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
+#endif /* UIP_UDP */
+
+static u16_t ipid;           /* Ths ipid variable is an increasing
+                               number that is used for the IP ID
+                               field. */
+
+void uip_setipid(u16_t id) { ipid = id; }
+
+static u8_t iss[4];          /* The iss variable is used for the TCP
+                               initial sequence number. */
+
+#if UIP_ACTIVE_OPEN
+static u16_t lastport;       /* Keeps track of the last port used for
+                               a new connection. */
+#endif /* UIP_ACTIVE_OPEN */
+
+/* Temporary variables. */
+u8_t uip_acc32[4];
+static u8_t c, opt;
+static u16_t tmp16;
+
+/* Structures and definitions. */
+#define TCP_FIN 0x01
+#define TCP_SYN 0x02
+#define TCP_RST 0x04
+#define TCP_PSH 0x08
+#define TCP_ACK 0x10
+#define TCP_URG 0x20
+#define TCP_CTL 0x3f
+
+#define TCP_OPT_END     0   /* End of TCP options list */
+#define TCP_OPT_NOOP    1   /* "No-operation" TCP option */
+#define TCP_OPT_MSS     2   /* Maximum segment size TCP option */
+
+#define TCP_OPT_MSS_LEN 4   /* Length of TCP MSS option. */
+
+#define ICMP_ECHO_REPLY 0
+#define ICMP_ECHO       8
+
+#define ICMP_DEST_UNREACHABLE        3
+#define ICMP_PORT_UNREACHABLE        3
+
+#define ICMP6_ECHO_REPLY             129
+#define ICMP6_ECHO                   128
+#define ICMP6_NEIGHBOR_SOLICITATION  135
+#define ICMP6_NEIGHBOR_ADVERTISEMENT 136
+
+#define ICMP6_FLAG_S (1 << 6)
+
+#define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1
+#define ICMP6_OPTION_TARGET_LINK_ADDRESS 2
+
+
+/* Macros. */
+#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
+#define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])
+#define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
+#define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
+
+
+#if UIP_STATISTICS == 1
+struct uip_stats uip_stat;
+#define UIP_STAT(s) s
+#else
+#define UIP_STAT(s)
+#endif /* UIP_STATISTICS == 1 */
+
+#if UIP_LOGGING == 1
+#include <stdio.h>
+void uip_log(char *msg);
+#define UIP_LOG(m) uip_log(m)
+#else
+#define UIP_LOG(m)
+#endif /* UIP_LOGGING == 1 */
+
+#if ! UIP_ARCH_ADD32
+void
+uip_add32(u8_t *op32, u16_t op16)
+{
+  uip_acc32[3] = op32[3] + (op16 & 0xff);
+  uip_acc32[2] = op32[2] + (op16 >> 8);
+  uip_acc32[1] = op32[1];
+  uip_acc32[0] = op32[0];
+  
+  if(uip_acc32[2] < (op16 >> 8)) {
+    ++uip_acc32[1];
+    if(uip_acc32[1] == 0) {
+      ++uip_acc32[0];
+    }
+  }
+  
+  
+  if(uip_acc32[3] < (op16 & 0xff)) {
+    ++uip_acc32[2];
+    if(uip_acc32[2] == 0) {
+      ++uip_acc32[1];
+      if(uip_acc32[1] == 0) {
+       ++uip_acc32[0];
+      }
+    }
+  }
+}
+
+#endif /* UIP_ARCH_ADD32 */
+
+#if ! UIP_ARCH_CHKSUM
+/*---------------------------------------------------------------------------*/
+static u16_t
+chksum(u16_t sum, const u8_t *data, u16_t len)
+{
+  u16_t t;
+  const u8_t *dataptr;
+  const u8_t *last_byte;
+
+  dataptr = data;
+  last_byte = data + len - 1;
+  
+  while(dataptr < last_byte) { /* At least two more bytes */
+    t = (dataptr[0] << 8) + dataptr[1];
+    sum += t;
+    if(sum < t) {
+      sum++;           /* carry */
+    }
+    dataptr += 2;
+  }
+  
+  if(dataptr == last_byte) {
+    t = (dataptr[0] << 8) + 0;
+    sum += t;
+    if(sum < t) {
+      sum++;           /* carry */
+    }
+  }
+
+  /* Return sum in host byte order. */
+  return sum;
+}
+/*---------------------------------------------------------------------------*/
+u16_t
+uip_chksum(u16_t *data, u16_t len)
+{
+  return htons(chksum(0, (u8_t *)data, len));
+}
+/*---------------------------------------------------------------------------*/
+#ifndef UIP_ARCH_IPCHKSUM
+u16_t
+uip_ipchksum(void)
+{
+  u16_t sum;
+
+  sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
+  DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum);
+  return (sum == 0) ? 0xffff : htons(sum);
+}
+#endif
+/*---------------------------------------------------------------------------*/
+static u16_t
+upper_layer_chksum(u8_t proto)
+{
+  u16_t upper_layer_len;
+  u16_t sum;
+  
+#if UIP_CONF_IPV6
+  upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]);
+#else /* UIP_CONF_IPV6 */
+  upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN;
+#endif /* UIP_CONF_IPV6 */
+  
+  /* First sum pseudoheader. */
+  
+  /* IP protocol and length fields. This addition cannot carry. */
+  sum = upper_layer_len + proto;
+  /* Sum IP source and destination addresses. */
+  sum = chksum(sum, (u8_t *)&BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t));
+
+  /* Sum TCP header and data. */
+  sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN],
+              upper_layer_len);
+    
+  return (sum == 0) ? 0xffff : htons(sum);
+}
+/*---------------------------------------------------------------------------*/
+#if UIP_CONF_IPV6
+u16_t
+uip_icmp6chksum(void)
+{
+  return upper_layer_chksum(UIP_PROTO_ICMP6);
+  
+}
+#endif /* UIP_CONF_IPV6 */
+/*---------------------------------------------------------------------------*/
+u16_t
+uip_tcpchksum(void)
+{
+  return upper_layer_chksum(UIP_PROTO_TCP);
+}
+/*---------------------------------------------------------------------------*/
+#if UIP_UDP_CHECKSUMS
+u16_t
+uip_udpchksum(void)
+{
+  return upper_layer_chksum(UIP_PROTO_UDP);
+}
+#endif /* UIP_UDP_CHECKSUMS */
+#endif /* UIP_ARCH_CHKSUM */
+/*---------------------------------------------------------------------------*/
+void
+uip_init(void)
+{
+  for(c = 0; c < UIP_LISTENPORTS; ++c) {
+    uip_listenports[c] = 0;
+  }
+  for(c = 0; c < UIP_CONNS; ++c) {
+    uip_conns[c].tcpstateflags = UIP_CLOSED;
+  }
+#if UIP_ACTIVE_OPEN
+  lastport = 1024;
+#endif /* UIP_ACTIVE_OPEN */
+
+#if UIP_UDP
+  for(c = 0; c < UIP_UDP_CONNS; ++c) {
+    uip_udp_conns[c].lport = 0;
+  }
+#endif /* UIP_UDP */
+  
+
+  /* IPv4 initialization. */
+#if UIP_FIXEDADDR == 0
+  /*  uip_hostaddr[0] = uip_hostaddr[1] = 0;*/
+#endif /* UIP_FIXEDADDR */
+
+}
+/*---------------------------------------------------------------------------*/
+#if UIP_ACTIVE_OPEN
+struct uip_conn *
+uip_connect(uip_ipaddr_t *ripaddr, u16_t rport)
+{
+  register struct uip_conn *conn, *cconn;
+  
+  /* Find an unused local port. */
+ again:
+  ++lastport;
+
+  if(lastport >= 32000) {
+    lastport = 4096;
+  }
+
+  /* Check if this port is already in use, and if so try to find
+     another one. */
+  for(c = 0; c < UIP_CONNS; ++c) {
+    conn = &uip_conns[c];
+    if(conn->tcpstateflags != UIP_CLOSED &&
+       conn->lport == htons(lastport)) {
+      goto again;
+    }
+  }
+
+  conn = 0;
+  for(c = 0; c < UIP_CONNS; ++c) {
+    cconn = &uip_conns[c];
+    if(cconn->tcpstateflags == UIP_CLOSED) {
+      conn = cconn;
+      break;
+    }
+    if(cconn->tcpstateflags == UIP_TIME_WAIT) {
+      if(conn == 0 ||
+        cconn->timer > conn->timer) {
+       conn = cconn;
+      }
+    }
+  }
+
+  if(conn == 0) {
+    return 0;
+  }
+  
+  conn->tcpstateflags = UIP_SYN_SENT;
+
+  conn->snd_nxt[0] = iss[0];
+  conn->snd_nxt[1] = iss[1];
+  conn->snd_nxt[2] = iss[2];
+  conn->snd_nxt[3] = iss[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. */
+  conn->lport = htons(lastport);
+  conn->rport = rport;
+  uip_ipaddr_copy(&conn->ripaddr, ripaddr);
+  
+  return conn;
+}
+#endif /* UIP_ACTIVE_OPEN */
+/*---------------------------------------------------------------------------*/
+#if UIP_UDP
+struct uip_udp_conn *
+uip_udp_new(const uip_ipaddr_t *ripaddr, u16_t rport)
+{
+  register struct uip_udp_conn *conn;
+  
+  /* Find an unused local port. */
+ again:
+  ++lastport;
+
+  if(lastport >= 32000) {
+    lastport = 4096;
+  }
+  
+  for(c = 0; c < UIP_UDP_CONNS; ++c) {
+    if(uip_udp_conns[c].lport == htons(lastport)) {
+      goto again;
+    }
+  }
+
+
+  conn = 0;
+  for(c = 0; c < UIP_UDP_CONNS; ++c) {
+    if(uip_udp_conns[c].lport == 0) {
+      conn = &uip_udp_conns[c];
+      break;
+    }
+  }
+
+  if(conn == 0) {
+    return 0;
+  }
+  
+  conn->lport = HTONS(lastport);
+  conn->rport = rport;
+  if(ripaddr == NULL) {
+    memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t));
+  } else {
+    uip_ipaddr_copy(&conn->ripaddr, ripaddr);
+  }
+  conn->ttl = UIP_TTL;
+  
+  return conn;
+}
+#endif /* UIP_UDP */
+/*---------------------------------------------------------------------------*/
+void
+uip_unlisten(u16_t port)
+{
+  for(c = 0; c < UIP_LISTENPORTS; ++c) {
+    if(uip_listenports[c] == port) {
+      uip_listenports[c] = 0;
+      return;
+    }
+  }
+}
+/*---------------------------------------------------------------------------*/
+void
+uip_listen(u16_t port)
+{
+  for(c = 0; c < UIP_LISTENPORTS; ++c) {
+    if(uip_listenports[c] == 0) {
+      uip_listenports[c] = port;
+      return;
+    }
+  }
+}
+/*---------------------------------------------------------------------------*/
+/* XXX: IP fragment reassembly: not well-tested. */
+
+#if UIP_REASSEMBLY && !UIP_CONF_IPV6
+#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
+static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
+static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
+static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
+                                   0x0f, 0x07, 0x03, 0x01};
+static u16_t uip_reasslen;
+static u8_t uip_reassflags;
+#define UIP_REASS_FLAG_LASTFRAG 0x01
+static u8_t uip_reasstmr;
+
+#define IP_MF   0x20
+
+static u8_t
+uip_reass(void)
+{
+  u16_t offset, len;
+  u16_t i;
+
+  /* If ip_reasstmr is zero, no packet is present in the buffer, so we
+     write the IP header of the fragment into the reassembly
+     buffer. The timer is updated with the maximum age. */
+  if(uip_reasstmr == 0) {
+    memcpy(uip_reassbuf, &BUF->vhl, UIP_IPH_LEN);
+    uip_reasstmr = UIP_REASS_MAXAGE;
+    uip_reassflags = 0;
+    /* Clear the bitmap. */
+    memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap));
+  }
+
+  /* Check if the incoming fragment matches the one currently present
+     in the reasembly buffer. If so, we proceed with copying the
+     fragment into the buffer. */
+  if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
+     BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
+     BUF->destipaddr[0] == FBUF->destipaddr[0] &&
+     BUF->destipaddr[1] == FBUF->destipaddr[1] &&
+     BUF->ipid[0] == FBUF->ipid[0] &&
+     BUF->ipid[1] == FBUF->ipid[1]) {
+
+    len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
+    offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
+
+    /* If the offset or the offset + fragment length overflows the
+       reassembly buffer, we discard the entire packet. */
+    if(offset > UIP_REASS_BUFSIZE ||
+       offset + len > UIP_REASS_BUFSIZE) {
+      uip_reasstmr = 0;
+      goto nullreturn;
+    }
+
+    /* Copy the fragment into the reassembly buffer, at the right
+       offset. */
+    memcpy(&uip_reassbuf[UIP_IPH_LEN + offset],
+          (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
+          len);
+      
+    /* Update the bitmap. */
+    if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
+      /* If the two endpoints are in the same byte, we only update
+        that byte. */
+            
+      uip_reassbitmap[offset / (8 * 8)] |=
+            bitmap_bits[(offset / 8 ) & 7] &
+            ~bitmap_bits[((offset + len) / 8 ) & 7];
+    } else {
+      /* If the two endpoints are in different bytes, we update the
+        bytes in the endpoints and fill the stuff inbetween with
+        0xff. */
+      uip_reassbitmap[offset / (8 * 8)] |=
+       bitmap_bits[(offset / 8 ) & 7];
+      for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
+       uip_reassbitmap[i] = 0xff;
+      }
+      uip_reassbitmap[(offset + len) / (8 * 8)] |=
+       ~bitmap_bits[((offset + len) / 8 ) & 7];
+    }
+    
+    /* If this fragment has the More Fragments flag set to zero, we
+       know that this is the last fragment, so we can calculate the
+       size of the entire packet. We also set the
+       IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
+       the final fragment. */
+
+    if((BUF->ipoffset[0] & IP_MF) == 0) {
+      uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
+      uip_reasslen = offset + len;
+    }
+    
+    /* Finally, we check if we have a full packet in the buffer. We do
+       this by checking if we have the last fragment and if all bits
+       in the bitmap are set. */
+    if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
+      /* Check all bytes up to and including all but the last byte in
+        the bitmap. */
+      for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
+       if(uip_reassbitmap[i] != 0xff) {
+         goto nullreturn;
+       }
+      }
+      /* Check the last byte in the bitmap. It should contain just the
+        right amount of bits. */
+      if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
+        (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
+       goto nullreturn;
+      }
+
+      /* If we have come this far, we have a full packet in the
+        buffer, so we allocate a pbuf and copy the packet into it. We
+        also reset the timer. */
+      uip_reasstmr = 0;
+      memcpy(BUF, FBUF, uip_reasslen);
+
+      /* Pretend to be a "normal" (i.e., not fragmented) IP packet
+        from now on. */
+      BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
+      BUF->len[0] = uip_reasslen >> 8;
+      BUF->len[1] = uip_reasslen & 0xff;
+      BUF->ipchksum = 0;
+      BUF->ipchksum = ~(uip_ipchksum());
+
+      return uip_reasslen;
+    }
+  }
+
+ nullreturn:
+  return 0;
+}
+#endif /* UIP_REASSEMBLY */
+/*---------------------------------------------------------------------------*/
+static void
+uip_add_rcv_nxt(u16_t n)
+{
+  uip_add32(uip_conn->rcv_nxt, n);
+  uip_conn->rcv_nxt[0] = uip_acc32[0];
+  uip_conn->rcv_nxt[1] = uip_acc32[1];
+  uip_conn->rcv_nxt[2] = uip_acc32[2];
+  uip_conn->rcv_nxt[3] = uip_acc32[3];
+}
+/*---------------------------------------------------------------------------*/
+void
+uip_process(u8_t flag)
+{
+  register struct uip_conn *uip_connr = uip_conn;
+
+#if UIP_UDP
+  if(flag == UIP_UDP_SEND_CONN) {
+    goto udp_send;
+  }
+#endif /* UIP_UDP */
+  
+  uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
+
+  /* Check if we were invoked because of a poll request for a
+     particular connection. */
+  if(flag == UIP_POLL_REQUEST) {
+    if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&
+       !uip_outstanding(uip_connr)) {
+       uip_len = uip_slen = 0;
+       uip_flags = UIP_POLL;
+       UIP_APPCALL();
+       goto appsend;
+    }
+    goto drop;
+    
+    /* Check if we were invoked because of the perodic timer fireing. */
+  } else if(flag == UIP_TIMER) {
+#if UIP_REASSEMBLY
+    if(uip_reasstmr != 0) {
+      --uip_reasstmr;
+    }
+#endif /* UIP_REASSEMBLY */
+    /* Increase the initial sequence number. */
+    if(++iss[3] == 0) {
+      if(++iss[2] == 0) {
+       if(++iss[1] == 0) {
+         ++iss[0];
+       }
+      }
+    }
+
+    /* Reset the length variables. */
+    uip_len = 0;
+    uip_slen = 0;
+
+    /* Check if the connection is in a state in which we simply wait
+       for the connection to time out. If so, we increase the
+       connection's timer and remove the connection if it times
+       out. */
+    if(uip_connr->tcpstateflags == UIP_TIME_WAIT ||
+       uip_connr->tcpstateflags == UIP_FIN_WAIT_2) {
+      ++(uip_connr->timer);
+      if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
+       uip_connr->tcpstateflags = UIP_CLOSED;
+      }
+    } else if(uip_connr->tcpstateflags != UIP_CLOSED) {
+      /* If the connection has outstanding data, we increase the
+        connection's timer and see if it has reached the RTO value
+        in which case we retransmit. */
+      if(uip_outstanding(uip_connr)) {
+       if(uip_connr->timer-- == 0) {
+         if(uip_connr->nrtx == UIP_MAXRTX ||
+            ((uip_connr->tcpstateflags == UIP_SYN_SENT ||
+              uip_connr->tcpstateflags == UIP_SYN_RCVD) &&
+             uip_connr->nrtx == UIP_MAXSYNRTX)) {
+           uip_connr->tcpstateflags = UIP_CLOSED;
+
+           /* We call UIP_APPCALL() with uip_flags set to
+              UIP_TIMEDOUT to inform the application that the
+              connection has timed out. */
+           uip_flags = UIP_TIMEDOUT;
+           UIP_APPCALL();
+
+           /* We also send a reset packet to the remote host. */
+           BUF->flags = TCP_RST | TCP_ACK;
+           goto tcp_send_nodata;
+         }
+
+         /* Exponential backoff. */
+         uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
+                                        4:
+                                        uip_connr->nrtx);
+         ++(uip_connr->nrtx);
+         
+         /* Ok, so we need to retransmit. We do this differently
+            depending on which state we are in. In ESTABLISHED, we
+            call upon the application so that it may prepare the
+            data for the retransmit. In SYN_RCVD, we resend the
+            SYNACK that we sent earlier and in LAST_ACK we have to
+            retransmit our FINACK. */
+         UIP_STAT(++uip_stat.tcp.rexmit);
+         switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
+         case UIP_SYN_RCVD:
+           /* In the SYN_RCVD state, we should retransmit our
+               SYNACK. */
+           goto tcp_send_synack;
+           
+#if UIP_ACTIVE_OPEN
+         case UIP_SYN_SENT:
+           /* In the SYN_SENT state, we retransmit out SYN. */
+           BUF->flags = 0;
+           goto tcp_send_syn;
+#endif /* UIP_ACTIVE_OPEN */
+           
+         case UIP_ESTABLISHED:
+           /* In the ESTABLISHED state, we call upon the application
+               to do the actual retransmit after which we jump into
+               the code for sending out the packet (the apprexmit
+               label). */
+           uip_flags = UIP_REXMIT;
+           UIP_APPCALL();
+           goto apprexmit;
+           
+         case UIP_FIN_WAIT_1:
+         case UIP_CLOSING:
+         case UIP_LAST_ACK:
+           /* In all these states we should retransmit a FINACK. */
+           goto tcp_send_finack;
+           
+         }
+       }
+      } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {
+       /* If there was no need for a retransmission, we poll the
+           application for new data. */
+       uip_len = uip_slen = 0;            
+       uip_flags = UIP_POLL;
+       UIP_APPCALL();
+       goto appsend;
+      }
+    }
+    goto drop;
+  }
+#if UIP_UDP
+  if(flag == UIP_UDP_TIMER) {
+    if(uip_udp_conn->lport != 0) {
+      uip_conn = NULL;
+      uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
+      uip_len = uip_slen = 0;
+      uip_flags = UIP_POLL;
+      UIP_UDP_APPCALL();
+      goto udp_send;
+    } else {
+      goto drop;
+    }
+  }
+#endif
+
+  /* This is where the input processing starts. */
+  UIP_STAT(++uip_stat.ip.recv);
+
+  /* Start of IP input header processing code. */
+  
+#if UIP_CONF_IPV6
+  /* Check validity of the IP header. */
+  if((BUF->vtc & 0xf0) != 0x60)  { /* IP version and header length. */
+    UIP_STAT(++uip_stat.ip.drop);
+    UIP_STAT(++uip_stat.ip.vhlerr);
+    UIP_LOG("ipv6: invalid version.");
+    goto drop;
+  }
+#else /* UIP_CONF_IPV6 */
+  /* Check validity of the IP header. */
+  if(BUF->vhl != 0x45)  { /* IP version and header length. */
+    UIP_STAT(++uip_stat.ip.drop);
+    UIP_STAT(++uip_stat.ip.vhlerr);
+    UIP_LOG("ip: invalid version or header length.");
+    goto drop;
+  }
+#endif /* UIP_CONF_IPV6 */
+  
+  /* Check the size of the packet. If the size reported to us in
+     uip_len is smaller the size reported in the IP header, we assume
+     that the packet has been corrupted in transit. If the size of
+     uip_len is larger than the size reported in the IP packet header,
+     the packet has been padded and we set uip_len to the correct
+     value.. */
+
+  if((BUF->len[0] << 8) + BUF->len[1] <= uip_len) {
+    uip_len = (BUF->len[0] << 8) + BUF->len[1];
+#if UIP_CONF_IPV6
+    uip_len += 40; /* The length reported in the IPv6 header is the
+                     length of the payload that follows the
+                     header. However, uIP uses the uip_len variable
+                     for holding the size of the entire packet,
+                     including the IP header. For IPv4 this is not a
+                     problem as the length field in the IPv4 header
+                     contains the length of the entire packet. But
+                     for IPv6 we need to add the size of the IPv6
+                     header (40 bytes). */
+#endif /* UIP_CONF_IPV6 */
+  } else {
+    UIP_LOG("ip: packet shorter than reported in IP header.");
+    goto drop;
+  }
+
+#if !UIP_CONF_IPV6
+  /* Check the fragment flag. */
+  if((BUF->ipoffset[0] & 0x3f) != 0 ||
+     BUF->ipoffset[1] != 0) {
+#if UIP_REASSEMBLY
+    uip_len = uip_reass();
+    if(uip_len == 0) {
+      goto drop;
+    }
+#else /* UIP_REASSEMBLY */
+    UIP_STAT(++uip_stat.ip.drop);
+    UIP_STAT(++uip_stat.ip.fragerr);
+    UIP_LOG("ip: fragment dropped.");
+    goto drop;
+#endif /* UIP_REASSEMBLY */
+  }
+#endif /* UIP_CONF_IPV6 */
+
+  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
+    /* If we are configured to use ping IP address configuration and
+       hasn't been assigned an IP address yet, we accept all ICMP
+       packets. */
+#if UIP_PINGADDRCONF && !UIP_CONF_IPV6
+    if(BUF->proto == UIP_PROTO_ICMP) {
+      UIP_LOG("ip: possible ping config packet received.");
+      goto icmp_input;
+    } else {
+      UIP_LOG("ip: packet dropped since no address assigned.");
+      goto drop;
+    }
+#endif /* UIP_PINGADDRCONF */
+
+  } else {
+    /* If IP broadcast support is configured, we check for a broadcast
+       UDP packet, which may be destined to us. */
+#if UIP_BROADCAST
+    DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum());
+    if(BUF->proto == UIP_PROTO_UDP &&
+       uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)
+       /*&&
+        uip_ipchksum() == 0xffff*/) {
+      goto udp_input;
+    }
+#endif /* UIP_BROADCAST */
+    
+    /* Check if the packet is destined for our IP address. */
+#if !UIP_CONF_IPV6
+    if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
+      UIP_STAT(++uip_stat.ip.drop);
+      goto drop;
+    }
+#else /* UIP_CONF_IPV6 */
+    /* For IPv6, packet reception is a little trickier as we need to
+       make sure that we listen to certain multicast addresses (all
+       hosts multicast address, and the solicited-node multicast
+       address) as well. However, we will cheat here and accept all
+       multicast packets that are sent to the ff02::/16 addresses. */
+    if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr) &&
+       BUF->destipaddr.u16[0] != HTONS(0xff02)) {
+      UIP_STAT(++uip_stat.ip.drop);
+      goto drop;
+    }
+#endif /* UIP_CONF_IPV6 */
+  }
+
+#if !UIP_CONF_IPV6
+  if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
+                                   checksum. */
+    UIP_STAT(++uip_stat.ip.drop);
+    UIP_STAT(++uip_stat.ip.chkerr);
+    UIP_LOG("ip: bad checksum.");
+    goto drop;
+  }
+#endif /* UIP_CONF_IPV6 */
+
+  if(BUF->proto == UIP_PROTO_TCP) { /* Check for TCP packet. If so,
+                                      proceed with TCP input
+                                      processing. */
+    goto tcp_input;
+  }
+
+#if UIP_UDP
+  if(BUF->proto == UIP_PROTO_UDP) {
+    goto udp_input;
+  }
+#endif /* UIP_UDP */
+
+#if !UIP_CONF_IPV6
+  /* ICMPv4 processing code follows. */
+  if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
+                                       here. */
+    UIP_STAT(++uip_stat.ip.drop);
+    UIP_STAT(++uip_stat.ip.protoerr);
+    UIP_LOG("ip: neither tcp nor icmp.");
+    goto drop;
+  }
+
+#if UIP_PINGADDRCONF
+ icmp_input:
+#endif /* UIP_PINGADDRCONF */
+  UIP_STAT(++uip_stat.icmp.recv);
+
+  /* ICMP echo (i.e., ping) processing. This is simple, we only change
+     the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
+     checksum before we return the packet. */
+  if(ICMPBUF->type != ICMP_ECHO) {
+    UIP_STAT(++uip_stat.icmp.drop);
+    UIP_STAT(++uip_stat.icmp.typeerr);
+    UIP_LOG("icmp: not icmp echo.");
+    goto drop;
+  }
+
+  /* If we are configured to use ping IP address assignment, we use
+     the destination IP address of this ping packet and assign it to
+     ourself. */
+#if UIP_PINGADDRCONF
+  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
+    uip_hostaddr = BUF->destipaddr;
+  }
+#endif /* UIP_PINGADDRCONF */
+
+  ICMPBUF->type = ICMP_ECHO_REPLY;
+
+  if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
+    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
+  } else {
+    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
+  }
+
+  /* Swap IP addresses. */
+  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
+  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
+
+  UIP_STAT(++uip_stat.icmp.sent);
+  BUF->ttl = UIP_TTL;
+  goto ip_send_nolen;
+
+  /* End of IPv4 input header processing code. */
+#else /* !UIP_CONF_IPV6 */
+
+  /* This is IPv6 ICMPv6 processing code. */
+  DEBUG_PRINTF("icmp6_input: length %d\n", uip_len);
+
+  if(BUF->proto != UIP_PROTO_ICMP6) { /* We only allow ICMPv6 packets from
+                                        here. */
+    UIP_STAT(++uip_stat.ip.drop);
+    UIP_STAT(++uip_stat.ip.protoerr);
+    UIP_LOG("ip: neither tcp nor icmp6.");
+    goto drop;
+  }
+
+  UIP_STAT(++uip_stat.icmp.recv);
+
+  /* If we get a neighbor solicitation for our address we should send
+     a neighbor advertisement message back. */
+  if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {
+    if(uip_ipaddr_cmp(&ICMPBUF->icmp6data, &uip_hostaddr)) {
+
+      if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {
+       /* Save the sender's address in our neighbor list. */
+       uip_neighbor_add(&ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));
+      }
+      
+      /* We should now send a neighbor advertisement back to where the
+        neighbor solicication came from. */
+      ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT;
+      ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */
+      
+      ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;
+      
+      uip_ipaddr_copy(&ICMPBUF->destipaddr, &ICMPBUF->srcipaddr);
+      uip_ipaddr_copy(&ICMPBUF->srcipaddr, &uip_hostaddr);
+      ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;
+      ICMPBUF->options[1] = 1;  /* Options length, 1 = 8 bytes. */
+      memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr));
+      ICMPBUF->icmpchksum = 0;
+      ICMPBUF->icmpchksum = ~uip_icmp6chksum();
+      
+      goto send;
+      
+    }
+    goto drop;
+  } else if(ICMPBUF->type == ICMP6_ECHO) {
+    /* ICMP echo (i.e., ping) processing. This is simple, we only
+       change the ICMP type from ECHO to ECHO_REPLY and update the
+       ICMP checksum before we return the packet. */
+
+    ICMPBUF->type = ICMP6_ECHO_REPLY;
+    
+    uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
+    uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
+    ICMPBUF->icmpchksum = 0;
+    ICMPBUF->icmpchksum = ~uip_icmp6chksum();
+    
+    UIP_STAT(++uip_stat.icmp.sent);
+    goto send;
+  } else {
+    DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type);
+    UIP_STAT(++uip_stat.icmp.drop);
+    UIP_STAT(++uip_stat.icmp.typeerr);
+    UIP_LOG("icmp: unknown ICMP message.");
+    goto drop;
+  }
+
+  /* End of IPv6 ICMP processing. */
+  
+#endif /* !UIP_CONF_IPV6 */
+
+#if UIP_UDP
+  /* UDP input processing. */
+ udp_input:
+  /* UDP processing is really just a hack. We don't do anything to the
+     UDP/IP headers, but let the UDP application do all the hard
+     work. If the application sets uip_slen, it has a packet to
+     send. */
+#if UIP_UDP_CHECKSUMS
+  uip_len = uip_len - UIP_IPUDPH_LEN;
+  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
+  if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {
+    UIP_STAT(++uip_stat.udp.drop);
+    UIP_STAT(++uip_stat.udp.chkerr);
+    UIP_LOG("udp: bad checksum.");
+    goto drop;
+  }
+#else /* UIP_UDP_CHECKSUMS */
+  uip_len = uip_len - UIP_IPUDPH_LEN;
+#endif /* UIP_UDP_CHECKSUMS */
+
+  /* Demultiplex this UDP packet between the UDP "connections". */
+  for(uip_udp_conn = &uip_udp_conns[0];
+      uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
+      ++uip_udp_conn) {
+    /* If the local UDP port is non-zero, the connection is considered
+       to be used. If so, the local port number is checked against the
+       destination port number in the received packet. If the two port
+       numbers match, the remote port number is checked if the
+       connection is bound to a remote port. Finally, if the
+       connection is bound to a remote IP address, the source IP
+       address of the packet is checked. */
+    if(uip_udp_conn->lport != 0 &&
+       UDPBUF->destport == uip_udp_conn->lport &&
+       (uip_udp_conn->rport == 0 ||
+        UDPBUF->srcport == uip_udp_conn->rport) &&
+       (uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) ||
+       uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) ||
+       uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr))) {
+      goto udp_found;
+    }
+  }
+  UIP_LOG("udp: no matching connection found");
+#if UIP_CONF_ICMP_DEST_UNREACH && !UIP_CONF_IPV6
+  /* Copy fields from packet header into payload of this ICMP packet. */
+  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);
+
+  /* Set the ICMP type and code. */
+  ICMPBUF->type = ICMP_DEST_UNREACHABLE;
+  ICMPBUF->icode = ICMP_PORT_UNREACHABLE;
+
+  /* Calculate the ICMP checksum. */
+  ICMPBUF->icmpchksum = 0;
+  ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36);
+
+  /* Set the IP destination address to be the source address of the
+     original packet. */
+  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
+
+  /* Set our IP address as the source address. */
+  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
+
+  /* The size of the ICMP destination unreachable packet is 36 + the
+     size of the IP header (20) = 56. */
+  uip_len = 36 + UIP_IPH_LEN;
+  ICMPBUF->len[0] = 0;
+  ICMPBUF->len[1] = (u8_t)uip_len;
+  ICMPBUF->ttl = UIP_TTL;
+  ICMPBUF->proto = UIP_PROTO_ICMP;
+
+  goto ip_send_nolen;
+#else /* UIP_CONF_ICMP_DEST_UNREACH */
+  goto drop;
+#endif /* UIP_CONF_ICMP_DEST_UNREACH */
+  
+ udp_found:
+  uip_conn = NULL;
+  uip_flags = UIP_NEWDATA;
+  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
+  uip_slen = 0;
+  UIP_UDP_APPCALL();
+
+ udp_send:
+  if(uip_slen == 0) {
+    goto drop;
+  }
+  uip_len = uip_slen + UIP_IPUDPH_LEN;
+
+#if UIP_CONF_IPV6
+  /* For IPv6, the IP length field does not include the IPv6 IP header
+     length. */
+  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
+  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
+#else /* UIP_CONF_IPV6 */
+  BUF->len[0] = (uip_len >> 8);
+  BUF->len[1] = (uip_len & 0xff);
+#endif /* UIP_CONF_IPV6 */
+
+  BUF->ttl = uip_udp_conn->ttl;
+  BUF->proto = UIP_PROTO_UDP;
+
+  UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);
+  UDPBUF->udpchksum = 0;
+
+  BUF->srcport  = uip_udp_conn->lport;
+  BUF->destport = uip_udp_conn->rport;
+
+  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
+  uip_ipaddr_copy(&BUF->destipaddr, &uip_udp_conn->ripaddr);
+   
+  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
+
+#if UIP_UDP_CHECKSUMS
+  /* Calculate UDP checksum. */
+  UDPBUF->udpchksum = ~(uip_udpchksum());
+  if(UDPBUF->udpchksum == 0) {
+    UDPBUF->udpchksum = 0xffff;
+  }
+#endif /* UIP_UDP_CHECKSUMS */
+  
+  goto ip_send_nolen;
+#endif /* UIP_UDP */
+  
+  /* TCP input processing. */
+ tcp_input:
+  UIP_STAT(++uip_stat.tcp.recv);
+
+  /* Start of TCP input header processing code. */
+  
+  if(uip_tcpchksum() != 0xffff) {   /* Compute and check the TCP
+                                      checksum. */
+    UIP_STAT(++uip_stat.tcp.drop);
+    UIP_STAT(++uip_stat.tcp.chkerr);
+    UIP_LOG("tcp: bad checksum.");
+    goto drop;
+  }
+  
+  /* Demultiplex this segment. */
+  /* First check any active connections. */
+  for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];
+      ++uip_connr) {
+    if(uip_connr->tcpstateflags != UIP_CLOSED &&
+       BUF->destport == uip_connr->lport &&
+       BUF->srcport == uip_connr->rport &&
+       uip_ipaddr_cmp(&BUF->srcipaddr, &uip_connr->ripaddr)) {
+      goto found;
+    }
+  }
+
+  /* If we didn't find and active connection that expected the packet,
+     either this packet is an old duplicate, or 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) {
+    goto reset;
+  }
+  
+  tmp16 = BUF->destport;
+  /* Next, check listening connections. */
+  for(c = 0; c < UIP_LISTENPORTS; ++c) {
+    if(tmp16 == uip_listenports[c]) {
+      goto found_listen;
+    }
+  }
+  
+  /* No matching connection found, so we send a RST packet. */
+  UIP_STAT(++uip_stat.tcp.synrst);
+
+ reset:
+  /* We do not send resets in response to resets. */
+  if(BUF->flags & TCP_RST) {
+    goto drop;
+  }
+
+  UIP_STAT(++uip_stat.tcp.rst);
+  
+  BUF->flags = TCP_RST | TCP_ACK;
+  uip_len = UIP_IPTCPH_LEN;
+  BUF->tcpoffset = 5 << 4;
+
+  /* Flip the seqno and ackno fields in the TCP header. */
+  c = BUF->seqno[3];
+  BUF->seqno[3] = BUF->ackno[3];
+  BUF->ackno[3] = c;
+  
+  c = BUF->seqno[2];
+  BUF->seqno[2] = BUF->ackno[2];
+  BUF->ackno[2] = c;
+  
+  c = BUF->seqno[1];
+  BUF->seqno[1] = BUF->ackno[1];
+  BUF->ackno[1] = c;
+  
+  c = BUF->seqno[0];
+  BUF->seqno[0] = BUF->ackno[0];
+  BUF->ackno[0] = c;
+
+  /* We also have to increase the sequence number we are
+     acknowledging. If the least significant byte overflowed, we need
+     to propagate the carry to the other bytes as well. */
+  if(++BUF->ackno[3] == 0) {
+    if(++BUF->ackno[2] == 0) {
+      if(++BUF->ackno[1] == 0) {
+       ++BUF->ackno[0];
+      }
+    }
+  }
+  /* Swap port numbers. */
+  tmp16 = BUF->srcport;
+  BUF->srcport = BUF->destport;
+  BUF->destport = tmp16;
+  
+  /* Swap IP addresses. */
+  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
+  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
+  
+  /* And send out the RST packet! */
+  goto tcp_send_noconn;
+
+  /* This label will be jumped to if we matched the incoming packet
+     with a connection in LISTEN. In that case, we should create a new
+     connection and send a SYNACK in return. */
+ found_listen:
+  /* First we check if there are any connections avaliable. Unused
+     connections are kept in the same table as used connections, but
+     unused ones have the tcpstate set to CLOSED. Also, connections in
+     TIME_WAIT are kept track of and we'll use the oldest one if no
+     CLOSED connections are found. Thanks to Eddie C. Dost for a very
+     nice algorithm for the TIME_WAIT search. */
+  uip_connr = 0;
+  for(c = 0; c < UIP_CONNS; ++c) {
+    if(uip_conns[c].tcpstateflags == UIP_CLOSED) {
+      uip_connr = &uip_conns[c];
+      break;
+    }
+    if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {
+      if(uip_connr == 0 ||
+        uip_conns[c].timer > uip_connr->timer) {
+       uip_connr = &uip_conns[c];
+      }
+    }
+  }
+
+  if(uip_connr == 0) {
+    /* All connections are used already, we drop packet and hope that
+       the remote end will retransmit the packet at a time when we
+       have more spare connections. */
+    UIP_STAT(++uip_stat.tcp.syndrop);
+    UIP_LOG("tcp: found no unused connections.");
+    goto drop;
+  }
+  uip_conn = uip_connr;
+  
+  /* Fill in the necessary fields for the new connection. */
+  uip_connr->rto = uip_connr->timer = UIP_RTO;
+  uip_connr->sa = 0;
+  uip_connr->sv = 4;
+  uip_connr->nrtx = 0;
+  uip_connr->lport = BUF->destport;
+  uip_connr->rport = BUF->srcport;
+  uip_ipaddr_copy(&uip_connr->ripaddr, &BUF->srcipaddr);
+  uip_connr->tcpstateflags = UIP_SYN_RCVD;
+
+  uip_connr->snd_nxt[0] = iss[0];
+  uip_connr->snd_nxt[1] = iss[1];
+  uip_connr->snd_nxt[2] = iss[2];
+  uip_connr->snd_nxt[3] = iss[3];
+  uip_connr->len = 1;
+
+  /* rcv_nxt should be the seqno from the incoming packet + 1. */
+  uip_connr->rcv_nxt[3] = BUF->seqno[3];
+  uip_connr->rcv_nxt[2] = BUF->seqno[2];
+  uip_connr->rcv_nxt[1] = BUF->seqno[1];
+  uip_connr->rcv_nxt[0] = BUF->seqno[0];
+  uip_add_rcv_nxt(1);
+
+  /* Parse the TCP MSS option, if present. */
+  if((BUF->tcpoffset & 0xf0) > 0x50) {
+    for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
+      opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
+      if(opt == TCP_OPT_END) {
+       /* End of options. */
+       break;
+      } else if(opt == TCP_OPT_NOOP) {
+       ++c;
+       /* NOP option. */
+      } else if(opt == TCP_OPT_MSS &&
+               uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
+       /* An MSS option with the right option length. */
+       tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
+         (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];
+       uip_connr->initialmss = uip_connr->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(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
+         /* If the length field is zero, the options are malformed
+            and we don't process them further. */
+         break;
+       }
+       c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
+      }
+    }
+  }
+  
+  /* Our response will be a SYNACK. */
+#if UIP_ACTIVE_OPEN
+ tcp_send_synack:
+  BUF->flags = TCP_ACK;
+  
+ tcp_send_syn:
+  BUF->flags |= TCP_SYN;
+#else /* UIP_ACTIVE_OPEN */
+ tcp_send_synack:
+  BUF->flags = TCP_SYN | TCP_ACK;
+#endif /* UIP_ACTIVE_OPEN */
+  
+  /* We send out the TCP Maximum Segment Size option with our
+     SYNACK. */
+  BUF->optdata[0] = TCP_OPT_MSS;
+  BUF->optdata[1] = TCP_OPT_MSS_LEN;
+  BUF->optdata[2] = (UIP_TCP_MSS) / 256;
+  BUF->optdata[3] = (UIP_TCP_MSS) & 255;
+  uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;
+  BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;
+  goto tcp_send;
+
+  /* This label will be jumped to if we found an active connection. */
+ found:
+  uip_conn = uip_connr;
+  uip_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) {
+    uip_connr->tcpstateflags = UIP_CLOSED;
+    UIP_LOG("tcp: got reset, aborting connection.");
+    uip_flags = UIP_ABORT;
+    UIP_APPCALL();
+    goto drop;
+  }
+  /* Calculate the length of the data, if the application has sent
+     any data to us. */
+  c = (BUF->tcpoffset >> 4) << 2;
+  /* uip_len will contain the length of the actual TCP data. This is
+     calculated by subtracing the length of the TCP header (in
+     c) and the length of the IP header (20 bytes). */
+  uip_len = uip_len - c - 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(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
+       ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) {
+    if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&
+       (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
+       BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
+       BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
+       BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
+      goto tcp_send_ack;
+    }
+  }
+
+  /* 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(uip_connr)) {
+    uip_add32(uip_connr->snd_nxt, uip_connr->len);
+
+    if(BUF->ackno[0] == uip_acc32[0] &&
+       BUF->ackno[1] == uip_acc32[1] &&
+       BUF->ackno[2] == uip_acc32[2] &&
+       BUF->ackno[3] == uip_acc32[3]) {
+      /* Update sequence number. */
+      uip_connr->snd_nxt[0] = uip_acc32[0];
+      uip_connr->snd_nxt[1] = uip_acc32[1];
+      uip_connr->snd_nxt[2] = uip_acc32[2];
+      uip_connr->snd_nxt[3] = uip_acc32[3];
+       
+      /* Do RTT estimation, unless we have done retransmissions. */
+      if(uip_connr->nrtx == 0) {
+       signed char m;
+       m = uip_connr->rto - uip_connr->timer;
+       /* This is taken directly from VJs original code in his paper */
+       m = m - (uip_connr->sa >> 3);
+       uip_connr->sa += m;
+       if(m < 0) {
+         m = -m;
+       }
+       m = m - (uip_connr->sv >> 2);
+       uip_connr->sv += m;
+       uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
+
+      }
+      /* Set the acknowledged flag. */
+      uip_flags = UIP_ACKDATA;
+      /* Reset the retransmission timer. */
+      uip_connr->timer = uip_connr->rto;
+
+      /* Reset length of outstanding data. */
+      uip_connr->len = 0;
+    }
+    
+  }
+
+  /* Do different things depending on in what state the connection is. */
+  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
+    /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
+       implemented, since we force the application to close when the
+       peer sends a FIN (hence the application goes directly from
+       ESTABLISHED to LAST_ACK). */
+  case UIP_SYN_RCVD:
+    /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
+       we are waiting for an ACK that acknowledges the data we sent
+       out the last time. Therefore, we want to have the UIP_ACKDATA
+       flag set. If so, we enter the ESTABLISHED state. */
+    if(uip_flags & UIP_ACKDATA) {
+      uip_connr->tcpstateflags = UIP_ESTABLISHED;
+      uip_flags = UIP_CONNECTED;
+      uip_connr->len = 0;
+      if(uip_len > 0) {
+        uip_flags |= UIP_NEWDATA;
+        uip_add_rcv_nxt(uip_len);
+      }
+      uip_slen = 0;
+      UIP_APPCALL();
+      goto appsend;
+    }
+    goto drop;
+#if UIP_ACTIVE_OPEN
+  case UIP_SYN_SENT:
+    /* In SYN_SENT, we wait for a SYNACK that is sent in response to
+       our SYN. The rcv_nxt is set to sequence number in the SYNACK
+       plus one, and we send an ACK. We move into the ESTABLISHED
+       state. */
+    if((uip_flags & UIP_ACKDATA) &&
+       (BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) {
+
+      /* Parse the TCP MSS option, if present. */
+      if((BUF->tcpoffset & 0xf0) > 0x50) {
+       for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
+         opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c];
+         if(opt == TCP_OPT_END) {
+           /* End of options. */
+           break;
+         } else if(opt == TCP_OPT_NOOP) {
+           ++c;
+           /* NOP option. */
+         } else if(opt == TCP_OPT_MSS &&
+                   uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
+           /* An MSS option with the right option length. */
+           tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
+             uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
+           uip_connr->initialmss =
+             uip_connr->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(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
+             /* If the length field is zero, the options are malformed
+                and we don't process them further. */
+             break;
+           }
+           c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
+         }
+       }
+      }
+      uip_connr->tcpstateflags = UIP_ESTABLISHED;
+      uip_connr->rcv_nxt[0] = BUF->seqno[0];
+      uip_connr->rcv_nxt[1] = BUF->seqno[1];
+      uip_connr->rcv_nxt[2] = BUF->seqno[2];
+      uip_connr->rcv_nxt[3] = BUF->seqno[3];
+      uip_add_rcv_nxt(1);
+      uip_flags = UIP_CONNECTED | UIP_NEWDATA;
+      uip_connr->len = 0;
+      uip_len = 0;
+      uip_slen = 0;
+      UIP_APPCALL();
+      goto appsend;
+    }
+    /* Inform the application that the connection failed */
+    uip_flags = UIP_ABORT;
+    UIP_APPCALL();
+    /* The connection is closed after we send the RST */
+    uip_conn->tcpstateflags = UIP_CLOSED;
+    goto reset;
+#endif /* UIP_ACTIVE_OPEN */
+    
+  case UIP_ESTABLISHED:
+    /* In the ESTABLISHED state, we call upon the application to feed
+    data into the uip_buf. If the UIP_ACKDATA flag is set, the
+    application should put new data into the buffer, otherwise we are
+    retransmitting an old segment, and the application should put that
+    data into the buffer.
+
+    If the incoming packet is a FIN, we should close the connection on
+    this side as well, and we send out a FIN and enter the LAST_ACK
+    state. We require that there is no outstanding data; otherwise the
+    sequence numbers will be screwed up. */
+
+    if(BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
+      if(uip_outstanding(uip_connr)) {
+       goto drop;
+      }
+      uip_add_rcv_nxt(1 + uip_len);
+      uip_flags |= UIP_CLOSE;
+      if(uip_len > 0) {
+       uip_flags |= UIP_NEWDATA;
+      }
+      UIP_APPCALL();
+      uip_connr->len = 1;
+      uip_connr->tcpstateflags = UIP_LAST_ACK;
+      uip_connr->nrtx = 0;
+    tcp_send_finack:
+      BUF->flags = TCP_FIN | TCP_ACK;
+      goto tcp_send_nodata;
+    }
+
+    /* Check the URG flag. If this is set, the segment carries urgent
+       data that we must pass to the application. */
+    if((BUF->flags & TCP_URG) != 0) {
+#if UIP_URGDATA > 0
+      uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
+      if(uip_urglen > uip_len) {
+       /* There is more urgent data in the next segment to come. */
+       uip_urglen = uip_len;
+      }
+      uip_add_rcv_nxt(uip_urglen);
+      uip_len -= uip_urglen;
+      uip_urgdata = uip_appdata;
+      uip_appdata += uip_urglen;
+    } else {
+      uip_urglen = 0;
+#else /* UIP_URGDATA > 0 */
+      uip_appdata = ((char *)uip_appdata) + ((BUF->urgp[0] << 8) | BUF->urgp[1]);
+      uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
+#endif /* UIP_URGDATA > 0 */
+    }
+
+    /* If uip_len > 0 we have TCP data in the packet, and we flag this
+       by setting the UIP_NEWDATA flag and update the sequence number
+       we acknowledge. If the application has stopped the dataflow
+       using uip_stop(), we must not accept any data packets from the
+       remote host. */
+    if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
+      uip_flags |= UIP_NEWDATA;
+      uip_add_rcv_nxt(uip_len);
+    }
+
+    /* Check if the available buffer space advertised by the other end
+       is smaller than the initial MSS for this connection. If so, we
+       set the current MSS to the window size to ensure that the
+       application does not send more data than the other end can
+       handle.
+
+       If the remote host advertises a zero window, we set the MSS to
+       the initial MSS so that the application will send an entire MSS
+       of data. This data will not be acknowledged by the receiver,
+       and the application will retransmit it. This is called the
+       "persistent timer" and uses the retransmission mechanim.
+    */
+    tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
+    if(tmp16 > uip_connr->initialmss ||
+       tmp16 == 0) {
+      tmp16 = uip_connr->initialmss;
+    }
+    uip_connr->mss = tmp16;
+
+    /* If this packet constitutes an ACK for outstanding data (flagged
+       by the UIP_ACKDATA flag, we should call the application since it
+       might want to send more data. If the incoming packet had data
+       from the peer (as flagged by the UIP_NEWDATA flag), the
+       application must also be notified.
+
+       When the application is called, the global variable uip_len
+       contains the length of the incoming data. The application can
+       access the incoming data through the global pointer
+       uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN
+       bytes into the uip_buf array.
+
+       If the application wishes to send any data, this data should be
+       put into the uip_appdata and the length of the data should be
+       put into uip_len. If the application don't have any data to
+       send, uip_len must be set to 0. */
+    if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
+      uip_slen = 0;
+      UIP_APPCALL();
+
+    appsend:
+      
+      if(uip_flags & UIP_ABORT) {
+       uip_slen = 0;
+       uip_connr->tcpstateflags = UIP_CLOSED;
+       BUF->flags = TCP_RST | TCP_ACK;
+       goto tcp_send_nodata;
+      }
+
+      if(uip_flags & UIP_CLOSE) {
+       uip_slen = 0;
+       uip_connr->len = 1;
+       uip_connr->tcpstateflags = UIP_FIN_WAIT_1;
+       uip_connr->nrtx = 0;
+       BUF->flags = TCP_FIN | TCP_ACK;
+       goto tcp_send_nodata;
+      }
+
+      /* If uip_slen > 0, the application has data to be sent. */
+      if(uip_slen > 0) {
+
+       /* If the connection has acknowledged data, the contents of
+          the ->len variable should be discarded. */
+       if((uip_flags & UIP_ACKDATA) != 0) {
+         uip_connr->len = 0;
+       }
+
+       /* If the ->len variable is non-zero the connection has
+          already data in transit and cannot send anymore right
+          now. */
+       if(uip_connr->len == 0) {
+
+         /* The application cannot send more than what is allowed by
+            the mss (the minumum of the MSS and the available
+            window). */
+         if(uip_slen > uip_connr->mss) {
+           uip_slen = uip_connr->mss;
+         }
+
+         /* Remember how much data we send out now so that we know
+            when everything has been acknowledged. */
+         uip_connr->len = uip_slen;
+       } else {
+
+         /* If the application already had unacknowledged data, we
+            make sure that the application does not send (i.e.,
+            retransmit) out more than it previously sent out. */
+         uip_slen = uip_connr->len;
+       }
+      }
+      uip_connr->nrtx = 0;
+    apprexmit:
+      uip_appdata = uip_sappdata;
+      
+      /* If the application has data to be sent, or if the incoming
+         packet had new data in it, we must send out a packet. */
+      if(uip_slen > 0 && uip_connr->len > 0) {
+       /* Add the length of the IP and TCP headers. */
+       uip_len = uip_connr->len + UIP_TCPIP_HLEN;
+       /* We always set the ACK flag in response packets. */
+       BUF->flags = TCP_ACK | TCP_PSH;
+       /* Send the packet. */
+       goto tcp_send_noopts;
+      }
+      /* If there is no data to send, just send out a pure ACK if
+        there is newdata. */
+      if(uip_flags & UIP_NEWDATA) {
+       uip_len = UIP_TCPIP_HLEN;
+       BUF->flags = TCP_ACK;
+       goto tcp_send_noopts;
+      }
+    }
+    goto drop;
+  case UIP_LAST_ACK:
+    /* We can close this connection if the peer has acknowledged our
+       FIN. This is indicated by the UIP_ACKDATA flag. */
+    if(uip_flags & UIP_ACKDATA) {
+      uip_connr->tcpstateflags = UIP_CLOSED;
+      uip_flags = UIP_CLOSE;
+      UIP_APPCALL();
+    }
+    break;
+    
+  case UIP_FIN_WAIT_1:
+    /* The application has closed the connection, but the remote host
+       hasn't closed its end yet. Thus we do nothing but wait for a
+       FIN from the other side. */
+    if(uip_len > 0) {
+      uip_add_rcv_nxt(uip_len);
+    }
+    if(BUF->flags & TCP_FIN) {
+      if(uip_flags & UIP_ACKDATA) {
+       uip_connr->tcpstateflags = UIP_TIME_WAIT;
+       uip_connr->timer = 0;
+       uip_connr->len = 0;
+      } else {
+       uip_connr->tcpstateflags = UIP_CLOSING;
+      }
+      uip_add_rcv_nxt(1);
+      uip_flags = UIP_CLOSE;
+      UIP_APPCALL();
+      goto tcp_send_ack;
+    } else if(uip_flags & UIP_ACKDATA) {
+      uip_connr->tcpstateflags = UIP_FIN_WAIT_2;
+      uip_connr->len = 0;
+      goto drop;
+    }
+    if(uip_len > 0) {
+      goto tcp_send_ack;
+    }
+    goto drop;
+      
+  case UIP_FIN_WAIT_2:
+    if(uip_len > 0) {
+      uip_add_rcv_nxt(uip_len);
+    }
+    if(BUF->flags & TCP_FIN) {
+      uip_connr->tcpstateflags = UIP_TIME_WAIT;
+      uip_connr->timer = 0;
+      uip_add_rcv_nxt(1);
+      uip_flags = UIP_CLOSE;
+      UIP_APPCALL();
+      goto tcp_send_ack;
+    }
+    if(uip_len > 0) {
+      goto tcp_send_ack;
+    }
+    goto drop;
+
+  case UIP_TIME_WAIT:
+    goto tcp_send_ack;
+    
+  case UIP_CLOSING:
+    if(uip_flags & UIP_ACKDATA) {
+      uip_connr->tcpstateflags = UIP_TIME_WAIT;
+      uip_connr->timer = 0;
+    }
+  }
+  goto drop;
+  
+  /* We jump here when we are ready to send the packet, and just want
+     to set the appropriate TCP sequence numbers in the TCP header. */
+ tcp_send_ack:
+  BUF->flags = TCP_ACK;
+
+ tcp_send_nodata:
+  uip_len = UIP_IPTCPH_LEN;
+
+ tcp_send_noopts:
+  BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;
+
+  /* We're done with the input processing. We are now ready to send a
+     reply. Our job is to fill in all the fields of the TCP and IP
+     headers before calculating the checksum and finally send the
+     packet. */
+ tcp_send:
+  BUF->ackno[0] = uip_connr->rcv_nxt[0];
+  BUF->ackno[1] = uip_connr->rcv_nxt[1];
+  BUF->ackno[2] = uip_connr->rcv_nxt[2];
+  BUF->ackno[3] = uip_connr->rcv_nxt[3];
+  
+  BUF->seqno[0] = uip_connr->snd_nxt[0];
+  BUF->seqno[1] = uip_connr->snd_nxt[1];
+  BUF->seqno[2] = uip_connr->snd_nxt[2];
+  BUF->seqno[3] = uip_connr->snd_nxt[3];
+
+  BUF->proto = UIP_PROTO_TCP;
+  
+  BUF->srcport  = uip_connr->lport;
+  BUF->destport = uip_connr->rport;
+
+  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
+  uip_ipaddr_copy(&BUF->destipaddr, &uip_connr->ripaddr);
+
+  if(uip_connr->tcpstateflags & UIP_STOPPED) {
+    /* If the connection has issued uip_stop(), we advertise a zero
+       window so that the remote host will stop sending data. */
+    BUF->wnd[0] = BUF->wnd[1] = 0;
+  } else {
+    BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
+    BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
+  }
+
+ tcp_send_noconn:
+  BUF->ttl = UIP_TTL;
+#if UIP_CONF_IPV6
+  /* For IPv6, the IP length field does not include the IPv6 IP header
+     length. */
+  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
+  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
+#else /* UIP_CONF_IPV6 */
+  BUF->len[0] = (uip_len >> 8);
+  BUF->len[1] = (uip_len & 0xff);
+#endif /* UIP_CONF_IPV6 */
+
+  BUF->urgp[0] = BUF->urgp[1] = 0;
+  
+  /* Calculate TCP checksum. */
+  BUF->tcpchksum = 0;
+  BUF->tcpchksum = ~(uip_tcpchksum());
+
+ ip_send_nolen:
+#if UIP_CONF_IPV6
+  BUF->vtc = 0x60;
+  BUF->tcflow = 0x00;
+  BUF->flow = 0x00;
+#else /* UIP_CONF_IPV6 */
+  BUF->vhl = 0x45;
+  BUF->tos = 0;
+  BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
+  ++ipid;
+  BUF->ipid[0] = ipid >> 8;
+  BUF->ipid[1] = ipid & 0xff;
+  /* Calculate IP checksum. */
+  BUF->ipchksum = 0;
+  BUF->ipchksum = ~(uip_ipchksum());
+  DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum());
+#endif /* UIP_CONF_IPV6 */   
+  UIP_STAT(++uip_stat.tcp.sent);
+#if UIP_CONF_IPV6
+ send:
+#endif /* UIP_CONF_IPV6 */
+  DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len,
+              (BUF->len[0] << 8) | BUF->len[1]);
+  
+  UIP_STAT(++uip_stat.ip.sent);
+  /* Return and let the caller do the actual transmission. */
+  uip_flags = 0;
+  return;
+
+ drop:
+  uip_len = 0;
+  uip_flags = 0;
+  return;
+}
+/*---------------------------------------------------------------------------*/
+u16_t
+htons(u16_t val)
+{
+  return HTONS(val);
+}
+
+u32_t
+htonl(u32_t val)
+{
+  return HTONL(val);
+}
+/*---------------------------------------------------------------------------*/
+void
+uip_send(const void *data, int len)
+{
+  int copylen;
+#define MIN(a,b) ((a) < (b)? (a): (b))
+  copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN -
+               (int)((char *)uip_sappdata - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN]));
+  if(copylen > 0) {
+    uip_slen = copylen;
+    if(data != uip_sappdata) {
+      memcpy(uip_sappdata, (data), uip_slen);
+    }
+  }
+}
+/*---------------------------------------------------------------------------*/
+/** @} */
+#endif /* UIP_CONF_IPV6 */