Statistics
| Branch: | Tag: | Revision:

root / fon-flash / uip.cpp @ master

History | View | Annotate | Download (57.4 KB)

1
#ifndef WIN32
2
#define DEBUG_PRINTF(...) /*printf(__VA_ARGS__)*/
3
#endif
4
5
/**
6
 * \defgroup uip The uIP TCP/IP stack
7
 * @{
8
 *
9
 * uIP is an implementation of the TCP/IP protocol stack intended for
10
 * small 8-bit and 16-bit microcontrollers.
11
 *
12
 * uIP provides the necessary protocols for Internet communication,
13
 * with a very small code footprint and RAM requirements - the uIP
14
 * code size is on the order of a few kilobytes and RAM usage is on
15
 * the order of a few hundred bytes.
16
 */
17
18
/**
19
 * \file
20
 * The uIP TCP/IP stack code.
21
 * \author Adam Dunkels <adam@dunkels.com>
22
 */
23
24
/*
25
 * Copyright (c) 2001-2003, Adam Dunkels.
26
 * All rights reserved.
27
 *
28
 * Redistribution and use in source and binary forms, with or without
29
 * modification, are permitted provided that the following conditions
30
 * are met:
31
 * 1. Redistributions of source code must retain the above copyright
32
 *    notice, this list of conditions and the following disclaimer.
33
 * 2. Redistributions in binary form must reproduce the above copyright
34
 *    notice, this list of conditions and the following disclaimer in the
35
 *    documentation and/or other materials provided with the distribution.
36
 * 3. The name of the author may not be used to endorse or promote
37
 *    products derived from this software without specific prior
38
 *    written permission.
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
41
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
44
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
46
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
48
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 * This file is part of the uIP TCP/IP stack.
53
 *
54
 * $Id: uip.c,v 1.1 2008/05/07 06:59:32 sven-ola Exp $
55
 *
56
 */
57
58
/*
59
 * uIP is a small implementation of the IP, UDP and TCP protocols (as
60
 * well as some basic ICMP stuff). The implementation couples the IP,
61
 * UDP, TCP and the application layers very tightly. To keep the size
62
 * of the compiled code down, this code frequently uses the goto
63
 * statement. While it would be possible to break the uip_process()
64
 * function into many smaller functions, this would increase the code
65
 * size because of the overhead of parameter passing and the fact that
66
 * the optimier would not be as efficient.
67
 *
68
 * The principle is that we have a small buffer, called the uip_buf,
69
 * in which the device driver puts an incoming packet. The TCP/IP
70
 * stack parses the headers in the packet, and calls the
71
 * application. If the remote host has sent data to the application,
72
 * this data is present in the uip_buf and the application read the
73
 * data from there. It is up to the application to put this data into
74
 * a byte stream if needed. The application will not be fed with data
75
 * that is out of sequence.
76
 *
77
 * If the application whishes to send data to the peer, it should put
78
 * its data into the uip_buf. The uip_appdata pointer points to the
79
 * first available byte. The TCP/IP stack will calculate the
80
 * checksums, and fill in the necessary header fields and finally send
81
 * the packet back to the peer.
82
*/
83
84
#include "uip.h"
85
#include "uipopt.h"
86
#include "uip_arch.h"
87
88
#if UIP_CONF_IPV6
89
#include "uip-neighbor.h"
90
#endif /* UIP_CONF_IPV6 */
91
92
#include <string.h>
93
94
/*---------------------------------------------------------------------------*/
95
/* Variable definitions. */
96
97
98
/* The IP address of this host. If it is defined to be fixed (by
99
   setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set
100
   here. Otherwise, the address */
101
#if UIP_FIXEDADDR > 0
102
const uip_ipaddr_t uip_hostaddr =
103
  {UIP_HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
104
   UIP_HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
105
const uip_ipaddr_t uip_draddr =
106
  {UIP_HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
107
   UIP_HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
108
const uip_ipaddr_t uip_netmask =
109
  {UIP_HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
110
   UIP_HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
111
#else
112
uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask;
113
#endif /* UIP_FIXEDADDR */
114
115
static const uip_ipaddr_t all_ones_addr =
116
#if UIP_CONF_IPV6
117
  {0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff};
118
#else /* UIP_CONF_IPV6 */
119
  {0xffff,0xffff};
120
#endif /* UIP_CONF_IPV6 */
121
static const uip_ipaddr_t all_zeroes_addr =
122
#if UIP_CONF_IPV6
123
  {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
124
#else /* UIP_CONF_IPV6 */
125
  {0x0000,0x0000};
126
#endif /* UIP_CONF_IPV6 */
127
128
129
#if UIP_FIXEDETHADDR
130
const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
131
                                          UIP_ETHADDR1,
132
                                          UIP_ETHADDR2,
133
                                          UIP_ETHADDR3,
134
                                          UIP_ETHADDR4,
135
                                          UIP_ETHADDR5}};
136
#else
137
struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}};
138
#endif
139
140
#ifndef UIP_CONF_EXTERNAL_BUFFER
141
u8_t uip_buf[UIP_BUFSIZE + 2];   /* The packet buffer that contains
142
                                    incoming packets. */
143
#endif /* UIP_CONF_EXTERNAL_BUFFER */
144
145
void *uip_appdata;               /* The uip_appdata pointer points to
146
                                    application data. */
147
void *uip_sappdata;              /* The uip_appdata pointer points to
148
                                    the application data which is to
149
                                    be sent. */
150
#if UIP_URGDATA > 0
151
void *uip_urgdata;               /* The uip_urgdata pointer points to
152
                                       urgent data (out-of-band data), if
153
                                       present. */
154
u16_t uip_urglen, uip_surglen;
155
#endif /* UIP_URGDATA > 0 */
156
157
u16_t uip_len, uip_slen;
158
                             /* The uip_len is either 8 or 16 bits,
159
                                depending on the maximum packet
160
                                size. */
161
162
u8_t uip_flags;     /* The uip_flags variable is used for
163
                                communication between the TCP/IP stack
164
                                and the application program. */
165
struct uip_conn *uip_conn;   /* uip_conn always points to the current
166
                                connection. */
167
168
struct uip_conn uip_conns[UIP_CONNS];
169
                             /* The uip_conns array holds all TCP
170
                                connections. */
171
u16_t uip_listenports[UIP_LISTENPORTS];
172
                             /* The uip_listenports list all currently
173
                                listning ports. */
174
#if UIP_UDP
175
struct uip_udp_conn *uip_udp_conn;
176
struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
177
#endif /* UIP_UDP */
178
179
static u16_t ipid;           /* Ths ipid variable is an increasing
180
                                number that is used for the IP ID
181
                                field. */
182
183
void uip_setipid(u16_t id) { ipid = id; }
184
185
static u8_t iss[4];          /* The iss variable is used for the TCP
186
                                initial sequence number. */
187
188
#if UIP_ACTIVE_OPEN
189
static u16_t lastport;       /* Keeps track of the last port used for
190
                                a new connection. */
191
#endif /* UIP_ACTIVE_OPEN */
192
193
/* Temporary variables. */
194
u8_t uip_acc32[4];
195
static u8_t c, opt;
196
static u16_t tmp16;
197
198
/* Structures and definitions. */
199
#define TCP_FIN 0x01
200
#define TCP_SYN 0x02
201
#define TCP_RST 0x04
202
#define TCP_PSH 0x08
203
#define TCP_ACK 0x10
204
#define TCP_URG 0x20
205
#define TCP_CTL 0x3f
206
207
#define TCP_OPT_END     0   /* End of TCP options list */
208
#define TCP_OPT_NOOP    1   /* "No-operation" TCP option */
209
#define TCP_OPT_MSS     2   /* Maximum segment size TCP option */
210
211
#define TCP_OPT_MSS_LEN 4   /* Length of TCP MSS option. */
212
213
#define ICMP_ECHO_REPLY 0
214
#define ICMP_ECHO       8
215
216
#define ICMP6_ECHO_REPLY             129
217
#define ICMP6_ECHO                   128
218
#define ICMP6_NEIGHBOR_SOLICITATION  135
219
#define ICMP6_NEIGHBOR_ADVERTISEMENT 136
220
221
#define ICMP6_FLAG_S (1 << 6)
222
223
#define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1
224
#define ICMP6_OPTION_TARGET_LINK_ADDRESS 2
225
226
227
/* Macros. */
228
#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
229
#define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])
230
#define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
231
#define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
232
233
234
#if UIP_STATISTICS == 1
235
struct uip_stats uip_stat;
236
#define UIP_STAT(s) s
237
#else
238
#define UIP_STAT(s)
239
#endif /* UIP_STATISTICS == 1 */
240
241
#if UIP_LOGGING == 1
242
#include <stdio.h>
243
void uip_log(const char *msg);
244
#define UIP_LOG(m) uip_log(m)
245
#else
246
#define UIP_LOG(m)
247
#endif /* UIP_LOGGING == 1 */
248
249
#if ! UIP_ARCH_ADD32
250
void
251
uip_add32(u8_t *op32, u16_t op16)
252
{
253
  uip_acc32[3] = op32[3] + (op16 & 0xff);
254
  uip_acc32[2] = op32[2] + (op16 >> 8);
255
  uip_acc32[1] = op32[1];
256
  uip_acc32[0] = op32[0];
257
  
258
  if(uip_acc32[2] < (op16 >> 8)) {
259
    ++uip_acc32[1];
260
    if(uip_acc32[1] == 0) {
261
      ++uip_acc32[0];
262
    }
263
  }
264
  
265
  
266
  if(uip_acc32[3] < (op16 & 0xff)) {
267
    ++uip_acc32[2];
268
    if(uip_acc32[2] == 0) {
269
      ++uip_acc32[1];
270
      if(uip_acc32[1] == 0) {
271
        ++uip_acc32[0];
272
      }
273
    }
274
  }
275
}
276
277
#endif /* UIP_ARCH_ADD32 */
278
279
#if ! UIP_ARCH_CHKSUM
280
/*---------------------------------------------------------------------------*/
281
static u16_t
282
chksum(u16_t sum, const u8_t *data, u16_t len)
283
{
284
  u16_t t;
285
  const u8_t *dataptr;
286
  const u8_t *last_byte;
287
288
  dataptr = data;
289
  last_byte = data + len - 1;
290
  
291
  while(dataptr < last_byte) {        /* At least two more bytes */
292
    t = (dataptr[0] << 8) + dataptr[1];
293
    sum += t;
294
    if(sum < t) {
295
      sum++;                /* carry */
296
    }
297
    dataptr += 2;
298
  }
299
  
300
  if(dataptr == last_byte) {
301
    t = (dataptr[0] << 8) + 0;
302
    sum += t;
303
    if(sum < t) {
304
      sum++;                /* carry */
305
    }
306
  }
307
308
  /* Return sum in host byte order. */
309
  return sum;
310
}
311
/*---------------------------------------------------------------------------*/
312
u16_t
313
uip_chksum(u16_t *data, u16_t len)
314
{
315
  return myhtons(chksum(0, (u8_t *)data, len));
316
}
317
/*---------------------------------------------------------------------------*/
318
#ifndef UIP_ARCH_IPCHKSUM
319
u16_t
320
uip_ipchksum(void)
321
{
322
  u16_t sum;
323
324
  sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
325
#ifndef WIN32
326
  DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum);
327
#endif
328
  return (sum == 0) ? 0xffff : myhtons(sum);
329
}
330
#endif
331
/*---------------------------------------------------------------------------*/
332
static u16_t
333
upper_layer_chksum(u8_t proto)
334
{
335
  u16_t upper_layer_len;
336
  u16_t sum;
337
  
338
#if UIP_CONF_IPV6
339
  upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]);
340
#else /* UIP_CONF_IPV6 */
341
  upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN;
342
#endif /* UIP_CONF_IPV6 */
343
  
344
  /* First sum pseudoheader. */
345
  
346
  /* IP protocol and length fields. This addition cannot carry. */
347
  sum = upper_layer_len + proto;
348
  /* Sum IP source and destination addresses. */
349
  sum = chksum(sum, (u8_t *)&BUF->srcipaddr[0], 2 * sizeof(uip_ipaddr_t));
350
351
  /* Sum TCP header and data. */
352
  sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN],
353
               upper_layer_len);
354
    
355
  return (sum == 0) ? 0xffff : myhtons(sum);
356
}
357
/*---------------------------------------------------------------------------*/
358
#if UIP_CONF_IPV6
359
u16_t
360
uip_icmp6chksum(void)
361
{
362
  return upper_layer_chksum(UIP_PROTO_ICMP6);
363
  
364
}
365
#endif /* UIP_CONF_IPV6 */
366
/*---------------------------------------------------------------------------*/
367
u16_t
368
uip_tcpchksum(void)
369
{
370
  return upper_layer_chksum(UIP_PROTO_TCP);
371
}
372
/*---------------------------------------------------------------------------*/
373
#if UIP_UDP_CHECKSUMS
374
u16_t
375
uip_udpchksum(void)
376
{
377
  return upper_layer_chksum(UIP_PROTO_UDP);
378
}
379
#endif /* UIP_UDP_CHECKSUMS */
380
#endif /* UIP_ARCH_CHKSUM */
381
/*---------------------------------------------------------------------------*/
382
void
383
uip_init(void)
384
{
385
  for(c = 0; c < UIP_LISTENPORTS; ++c) {
386
    uip_listenports[c] = 0;
387
  }
388
  for(c = 0; c < UIP_CONNS; ++c) {
389
    uip_conns[c].tcpstateflags = UIP_CLOSED;
390
  }
391
#if UIP_ACTIVE_OPEN
392
  lastport = 1024;
393
#endif /* UIP_ACTIVE_OPEN */
394
395
#if UIP_UDP
396
  for(c = 0; c < UIP_UDP_CONNS; ++c) {
397
    uip_udp_conns[c].lport = 0;
398
  }
399
#endif /* UIP_UDP */
400
  
401
402
  /* IPv4 initialization. */
403
#if UIP_FIXEDADDR == 0
404
  /*  uip_hostaddr[0] = uip_hostaddr[1] = 0;*/
405
#endif /* UIP_FIXEDADDR */
406
407
}
408
/*---------------------------------------------------------------------------*/
409
#if UIP_ACTIVE_OPEN
410
struct uip_conn *
411
uip_connect(uip_ipaddr_t *ripaddr, u16_t rport)
412
{
413
  register struct uip_conn *conn, *cconn;
414
  
415
  /* Find an unused local port. */
416
 again:
417
  ++lastport;
418
419
  if(lastport >= 32000) {
420
    lastport = 4096;
421
  }
422
423
  /* Check if this port is already in use, and if so try to find
424
     another one. */
425
  for(c = 0; c < UIP_CONNS; ++c) {
426
    conn = &uip_conns[c];
427
    if(conn->tcpstateflags != UIP_CLOSED &&
428
       conn->lport == myhtons(lastport)) {
429
      goto again;
430
    }
431
  }
432
433
  conn = 0;
434
  for(c = 0; c < UIP_CONNS; ++c) {
435
    cconn = &uip_conns[c];
436
    if(cconn->tcpstateflags == UIP_CLOSED) {
437
      conn = cconn;
438
      break;
439
    }
440
    if(cconn->tcpstateflags == UIP_TIME_WAIT) {
441
      if(conn == 0 ||
442
         cconn->timer > conn->timer) {
443
        conn = cconn;
444
      }
445
    }
446
  }
447
448
  if(conn == 0) {
449
    return 0;
450
  }
451
  
452
  conn->tcpstateflags = UIP_SYN_SENT;
453
454
  conn->snd_nxt[0] = iss[0];
455
  conn->snd_nxt[1] = iss[1];
456
  conn->snd_nxt[2] = iss[2];
457
  conn->snd_nxt[3] = iss[3];
458
459
  conn->initialmss = conn->mss = UIP_TCP_MSS;
460
  
461
  conn->len = 1;   /* TCP length of the SYN is one. */
462
  conn->nrtx = 0;
463
  conn->timer = 1; /* Send the SYN next time around. */
464
  conn->rto = UIP_RTO;
465
  conn->sa = 0;
466
  conn->sv = 16;   /* Initial value of the RTT variance. */
467
  conn->lport = myhtons(lastport);
468
  conn->rport = rport;
469
  uip_ipaddr_copy(&conn->ripaddr, ripaddr);
470
  
471
  return conn;
472
}
473
#endif /* UIP_ACTIVE_OPEN */
474
/*---------------------------------------------------------------------------*/
475
#if UIP_UDP
476
struct uip_udp_conn *
477
uip_udp_new(uip_ipaddr_t *ripaddr, u16_t rport)
478
{
479
  register struct uip_udp_conn *conn;
480
  
481
  /* Find an unused local port. */
482
 again:
483
  ++lastport;
484
485
  if(lastport >= 32000) {
486
    lastport = 4096;
487
  }
488
  
489
  for(c = 0; c < UIP_UDP_CONNS; ++c) {
490
    if(uip_udp_conns[c].lport == myhtons(lastport)) {
491
      goto again;
492
    }
493
  }
494
495
496
  conn = 0;
497
  for(c = 0; c < UIP_UDP_CONNS; ++c) {
498
    if(uip_udp_conns[c].lport == 0) {
499
      conn = &uip_udp_conns[c];
500
      break;
501
    }
502
  }
503
504
  if(conn == 0) {
505
    return 0;
506
  }
507
  
508
  conn->lport = UIP_HTONS(lastport);
509
  conn->rport = rport;
510
  if(ripaddr == NULL) {
511
    memset(conn->ripaddr, 0, sizeof(uip_ipaddr_t));
512
  } else {
513
    uip_ipaddr_copy(&conn->ripaddr, ripaddr);
514
  }
515
  conn->ttl = UIP_TTL;
516
  
517
  return conn;
518
}
519
#endif /* UIP_UDP */
520
/*---------------------------------------------------------------------------*/
521
void
522
uip_unlisten(u16_t port)
523
{
524
  for(c = 0; c < UIP_LISTENPORTS; ++c) {
525
    if(uip_listenports[c] == port) {
526
      uip_listenports[c] = 0;
527
      return;
528
    }
529
  }
530
}
531
/*---------------------------------------------------------------------------*/
532
void
533
uip_listen(u16_t port)
534
{
535
  for(c = 0; c < UIP_LISTENPORTS; ++c) {
536
    if(uip_listenports[c] == 0) {
537
      uip_listenports[c] = port;
538
      return;
539
    }
540
  }
541
}
542
/*---------------------------------------------------------------------------*/
543
/* XXX: IP fragment reassembly: not well-tested. */
544
545
#if UIP_REASSEMBLY && !UIP_CONF_IPV6
546
#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
547
static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
548
static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
549
static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
550
                                    0x0f, 0x07, 0x03, 0x01};
551
static u16_t uip_reasslen;
552
static u8_t uip_reassflags;
553
#define UIP_REASS_FLAG_LASTFRAG 0x01
554
static u8_t uip_reasstmr;
555
556
#define IP_MF   0x20
557
558
static u8_t
559
uip_reass(void)
560
{
561
  u16_t offset, len;
562
  u16_t i;
563
564
  /* If ip_reasstmr is zero, no packet is present in the buffer, so we
565
     write the IP header of the fragment into the reassembly
566
     buffer. The timer is updated with the maximum age. */
567
  if(uip_reasstmr == 0) {
568
    memcpy(uip_reassbuf, &BUF->vhl, UIP_IPH_LEN);
569
    uip_reasstmr = UIP_REASS_MAXAGE;
570
    uip_reassflags = 0;
571
    /* Clear the bitmap. */
572
    memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap));
573
  }
574
575
  /* Check if the incoming fragment matches the one currently present
576
     in the reasembly buffer. If so, we proceed with copying the
577
     fragment into the buffer. */
578
  if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
579
     BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
580
     BUF->destipaddr[0] == FBUF->destipaddr[0] &&
581
     BUF->destipaddr[1] == FBUF->destipaddr[1] &&
582
     BUF->ipid[0] == FBUF->ipid[0] &&
583
     BUF->ipid[1] == FBUF->ipid[1]) {
584
585
    len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
586
    offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
587
588
    /* If the offset or the offset + fragment length overflows the
589
       reassembly buffer, we discard the entire packet. */
590
    if(offset > UIP_REASS_BUFSIZE ||
591
       offset + len > UIP_REASS_BUFSIZE) {
592
      uip_reasstmr = 0;
593
      goto nullreturn;
594
    }
595
596
    /* Copy the fragment into the reassembly buffer, at the right
597
       offset. */
598
    memcpy(&uip_reassbuf[UIP_IPH_LEN + offset],
599
           (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
600
           len);
601
      
602
    /* Update the bitmap. */
603
    if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
604
      /* If the two endpoints are in the same byte, we only update
605
         that byte. */
606
             
607
      uip_reassbitmap[offset / (8 * 8)] |=
608
             bitmap_bits[(offset / 8 ) & 7] &
609
             ~bitmap_bits[((offset + len) / 8 ) & 7];
610
    } else {
611
      /* If the two endpoints are in different bytes, we update the
612
         bytes in the endpoints and fill the stuff inbetween with
613
         0xff. */
614
      uip_reassbitmap[offset / (8 * 8)] |=
615
        bitmap_bits[(offset / 8 ) & 7];
616
      for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
617
        uip_reassbitmap[i] = 0xff;
618
      }
619
      uip_reassbitmap[(offset + len) / (8 * 8)] |=
620
        ~bitmap_bits[((offset + len) / 8 ) & 7];
621
    }
622
    
623
    /* If this fragment has the More Fragments flag set to zero, we
624
       know that this is the last fragment, so we can calculate the
625
       size of the entire packet. We also set the
626
       IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
627
       the final fragment. */
628
629
    if((BUF->ipoffset[0] & IP_MF) == 0) {
630
      uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
631
      uip_reasslen = offset + len;
632
    }
633
    
634
    /* Finally, we check if we have a full packet in the buffer. We do
635
       this by checking if we have the last fragment and if all bits
636
       in the bitmap are set. */
637
    if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
638
      /* Check all bytes up to and including all but the last byte in
639
         the bitmap. */
640
      for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
641
        if(uip_reassbitmap[i] != 0xff) {
642
          goto nullreturn;
643
        }
644
      }
645
      /* Check the last byte in the bitmap. It should contain just the
646
         right amount of bits. */
647
      if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
648
         (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
649
        goto nullreturn;
650
      }
651
652
      /* If we have come this far, we have a full packet in the
653
         buffer, so we allocate a pbuf and copy the packet into it. We
654
         also reset the timer. */
655
      uip_reasstmr = 0;
656
      memcpy(BUF, FBUF, uip_reasslen);
657
658
      /* Pretend to be a "normal" (i.e., not fragmented) IP packet
659
         from now on. */
660
      BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
661
      BUF->len[0] = uip_reasslen >> 8;
662
      BUF->len[1] = uip_reasslen & 0xff;
663
      BUF->ipchksum = 0;
664
      BUF->ipchksum = ~(uip_ipchksum());
665
666
      return uip_reasslen;
667
    }
668
  }
669
670
 nullreturn:
671
  return 0;
672
}
673
#endif /* UIP_REASSEMBLY */
674
/*---------------------------------------------------------------------------*/
675
static void
676
uip_add_rcv_nxt(u16_t n)
677
{
678
  uip_add32(uip_conn->rcv_nxt, n);
679
  uip_conn->rcv_nxt[0] = uip_acc32[0];
680
  uip_conn->rcv_nxt[1] = uip_acc32[1];
681
  uip_conn->rcv_nxt[2] = uip_acc32[2];
682
  uip_conn->rcv_nxt[3] = uip_acc32[3];
683
}
684
/*---------------------------------------------------------------------------*/
685
void
686
uip_process(u8_t flag)
687
{
688
  register struct uip_conn *uip_connr = uip_conn;
689
690
#if UIP_UDP
691
  if(flag == UIP_UDP_SEND_CONN) {
692
    goto udp_send;
693
  }
694
#endif /* UIP_UDP */
695
  
696
  uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
697
698
  /* Check if we were invoked because of a poll request for a
699
     particular connection. */
700
  if(flag == UIP_POLL_REQUEST) {
701
    if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&
702
       !uip_outstanding(uip_connr)) {
703
        uip_flags = UIP_POLL;
704
        UIP_APPCALL();
705
        goto appsend;
706
    }
707
    goto drop;
708
    
709
    /* Check if we were invoked because of the perodic timer fireing. */
710
  } else if(flag == UIP_TIMER) {
711
#if UIP_REASSEMBLY
712
    if(uip_reasstmr != 0) {
713
      --uip_reasstmr;
714
    }
715
#endif /* UIP_REASSEMBLY */
716
    /* Increase the initial sequence number. */
717
    if(++iss[3] == 0) {
718
      if(++iss[2] == 0) {
719
        if(++iss[1] == 0) {
720
          ++iss[0];
721
        }
722
      }
723
    }
724
725
    /* Reset the length variables. */
726
    uip_len = 0;
727
    uip_slen = 0;
728
729
    /* Check if the connection is in a state in which we simply wait
730
       for the connection to time out. If so, we increase the
731
       connection's timer and remove the connection if it times
732
       out. */
733
    if(uip_connr->tcpstateflags == UIP_TIME_WAIT ||
734
       uip_connr->tcpstateflags == UIP_FIN_WAIT_2) {
735
      ++(uip_connr->timer);
736
      if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
737
        uip_connr->tcpstateflags = UIP_CLOSED;
738
      }
739
    } else if(uip_connr->tcpstateflags != UIP_CLOSED) {
740
      /* If the connection has outstanding data, we increase the
741
         connection's timer and see if it has reached the RTO value
742
         in which case we retransmit. */
743
      if(uip_outstanding(uip_connr)) {
744
        if(uip_connr->timer-- == 0) {
745
          if(uip_connr->nrtx == UIP_MAXRTX ||
746
             ((uip_connr->tcpstateflags == UIP_SYN_SENT ||
747
               uip_connr->tcpstateflags == UIP_SYN_RCVD) &&
748
              uip_connr->nrtx == UIP_MAXSYNRTX)) {
749
            uip_connr->tcpstateflags = UIP_CLOSED;
750
751
            /* We call UIP_APPCALL() with uip_flags set to
752
               UIP_TIMEDOUT to inform the application that the
753
               connection has timed out. */
754
            uip_flags = UIP_TIMEDOUT;
755
            UIP_APPCALL();
756
757
            /* We also send a reset packet to the remote host. */
758
            BUF->flags = TCP_RST | TCP_ACK;
759
            goto tcp_send_nodata;
760
          }
761
762
          /* Exponential backoff. */
763
          uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
764
                                         4:
765
                                         uip_connr->nrtx);
766
          ++(uip_connr->nrtx);
767
          
768
          /* Ok, so we need to retransmit. We do this differently
769
             depending on which state we are in. In ESTABLISHED, we
770
             call upon the application so that it may prepare the
771
             data for the retransmit. In SYN_RCVD, we resend the
772
             SYNACK that we sent earlier and in LAST_ACK we have to
773
             retransmit our FINACK. */
774
          UIP_STAT(++uip_stat.tcp.rexmit);
775
          switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
776
          case UIP_SYN_RCVD:
777
            /* In the SYN_RCVD state, we should retransmit our
778
               SYNACK. */
779
            goto tcp_send_synack;
780
            
781
#if UIP_ACTIVE_OPEN
782
          case UIP_SYN_SENT:
783
            /* In the SYN_SENT state, we retransmit out SYN. */
784
            BUF->flags = 0;
785
            goto tcp_send_syn;
786
#endif /* UIP_ACTIVE_OPEN */
787
            
788
          case UIP_ESTABLISHED:
789
            /* In the ESTABLISHED state, we call upon the application
790
               to do the actual retransmit after which we jump into
791
               the code for sending out the packet (the apprexmit
792
               label). */
793
            uip_flags = UIP_REXMIT;
794
            UIP_APPCALL();
795
            goto apprexmit;
796
            
797
          case UIP_FIN_WAIT_1:
798
          case UIP_CLOSING:
799
          case UIP_LAST_ACK:
800
            /* In all these states we should retransmit a FINACK. */
801
            goto tcp_send_finack;
802
            
803
          }
804
        }
805
      } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {
806
        /* If there was no need for a retransmission, we poll the
807
           application for new data. */
808
        uip_flags = UIP_POLL;
809
        UIP_APPCALL();
810
        goto appsend;
811
      }
812
    }
813
    goto drop;
814
  }
815
#if UIP_UDP
816
  if(flag == UIP_UDP_TIMER) {
817
    if(uip_udp_conn->lport != 0) {
818
      uip_conn = NULL;
819
      uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
820
      uip_len = uip_slen = 0;
821
      uip_flags = UIP_POLL;
822
      UIP_UDP_APPCALL();
823
      goto udp_send;
824
    } else {
825
      goto drop;
826
    }
827
  }
828
#endif
829
830
  /* This is where the input processing starts. */
831
  UIP_STAT(++uip_stat.ip.recv);
832
833
  /* Start of IP input header processing code. */
834
  
835
#if UIP_CONF_IPV6
836
  /* Check validity of the IP header. */
837
  if((BUF->vtc & 0xf0) != 0x60)  { /* IP version and header length. */
838
    UIP_STAT(++uip_stat.ip.drop);
839
    UIP_STAT(++uip_stat.ip.vhlerr);
840
    UIP_LOG("ipv6: invalid version.");
841
    goto drop;
842
  }
843
#else /* UIP_CONF_IPV6 */
844
  /* Check validity of the IP header. */
845
  if(BUF->vhl != 0x45)  { /* IP version and header length. */
846
    UIP_STAT(++uip_stat.ip.drop);
847
    UIP_STAT(++uip_stat.ip.vhlerr);
848
    UIP_LOG("ip: invalid version or header length.");
849
    goto drop;
850
  }
851
#endif /* UIP_CONF_IPV6 */
852
  
853
  /* Check the size of the packet. If the size reported to us in
854
     uip_len is smaller the size reported in the IP header, we assume
855
     that the packet has been corrupted in transit. If the size of
856
     uip_len is larger than the size reported in the IP packet header,
857
     the packet has been padded and we set uip_len to the correct
858
     value.. */
859
860
  if((BUF->len[0] << 8) + BUF->len[1] <= uip_len) {
861
    uip_len = (BUF->len[0] << 8) + BUF->len[1];
862
#if UIP_CONF_IPV6
863
    uip_len += 40; /* The length reported in the IPv6 header is the
864
                      length of the payload that follows the
865
                      header. However, uIP uses the uip_len variable
866
                      for holding the size of the entire packet,
867
                      including the IP header. For IPv4 this is not a
868
                      problem as the length field in the IPv4 header
869
                      contains the length of the entire packet. But
870
                      for IPv6 we need to add the size of the IPv6
871
                      header (40 bytes). */
872
#endif /* UIP_CONF_IPV6 */
873
  } else {
874
    UIP_LOG("ip: packet shorter than reported in IP header.");
875
    goto drop;
876
  }
877
878
#if !UIP_CONF_IPV6
879
  /* Check the fragment flag. */
880
  if((BUF->ipoffset[0] & 0x3f) != 0 ||
881
     BUF->ipoffset[1] != 0) {
882
#if UIP_REASSEMBLY
883
    uip_len = uip_reass();
884
    if(uip_len == 0) {
885
      goto drop;
886
    }
887
#else /* UIP_REASSEMBLY */
888
    UIP_STAT(++uip_stat.ip.drop);
889
    UIP_STAT(++uip_stat.ip.fragerr);
890
    UIP_LOG("ip: fragment dropped.");
891
    goto drop;
892
#endif /* UIP_REASSEMBLY */
893
  }
894
#endif /* UIP_CONF_IPV6 */
895
896
  if(uip_ipaddr_cmp(uip_hostaddr, all_zeroes_addr)) {
897
    /* If we are configured to use ping IP address configuration and
898
       hasn't been assigned an IP address yet, we accept all ICMP
899
       packets. */
900
#if UIP_PINGADDRCONF && !UIP_CONF_IPV6
901
    if(BUF->proto == UIP_PROTO_ICMP) {
902
      UIP_LOG("ip: possible ping config packet received.");
903
      goto icmp_input;
904
    } else {
905
      UIP_LOG("ip: packet dropped since no address assigned.");
906
      goto drop;
907
    }
908
#endif /* UIP_PINGADDRCONF */
909
910
  } else {
911
    /* If IP broadcast support is configured, we check for a broadcast
912
       UDP packet, which may be destined to us. */
913
#if UIP_BROADCAST
914
#ifndef WIN32
915
    DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum());
916
#endif
917
    if(BUF->proto == UIP_PROTO_UDP &&
918
       uip_ipaddr_cmp(BUF->destipaddr, all_ones_addr)
919
       /*&&
920
         uip_ipchksum() == 0xffff*/) {
921
      goto udp_input;
922
    }
923
#endif /* UIP_BROADCAST */
924
    
925
    /* Check if the packet is destined for our IP address. */
926
#if !UIP_CONF_IPV6
927
    if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr)) {
928
      UIP_STAT(++uip_stat.ip.drop);
929
      goto drop;
930
    }
931
#else /* UIP_CONF_IPV6 */
932
    /* For IPv6, packet reception is a little trickier as we need to
933
       make sure that we listen to certain multicast addresses (all
934
       hosts multicast address, and the solicited-node multicast
935
       address) as well. However, we will cheat here and accept all
936
       multicast packets that are sent to the ff02::/16 addresses. */
937
    if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr) &&
938
       BUF->destipaddr[0] != UIP_HTONS(0xff02)) {
939
      UIP_STAT(++uip_stat.ip.drop);
940
      goto drop;
941
    }
942
#endif /* UIP_CONF_IPV6 */
943
  }
944
945
#if !UIP_CONF_IPV6
946
  if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
947
                                    checksum. */
948
    UIP_STAT(++uip_stat.ip.drop);
949
    UIP_STAT(++uip_stat.ip.chkerr);
950
    UIP_LOG("ip: bad checksum.");
951
    goto drop;
952
  }
953
#endif /* UIP_CONF_IPV6 */
954
955
  if(BUF->proto == UIP_PROTO_TCP) { /* Check for TCP packet. If so,
956
                                       proceed with TCP input
957
                                       processing. */
958
    goto tcp_input;
959
  }
960
961
#if UIP_UDP
962
  if(BUF->proto == UIP_PROTO_UDP) {
963
    goto udp_input;
964
  }
965
#endif /* UIP_UDP */
966
967
#if !UIP_CONF_IPV6
968
  /* ICMPv4 processing code follows. */
969
  if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
970
                                        here. */
971
    UIP_STAT(++uip_stat.ip.drop);
972
    UIP_STAT(++uip_stat.ip.protoerr);
973
    UIP_LOG("ip: neither tcp nor icmp.");
974
    goto drop;
975
  }
976
977
#if UIP_PINGADDRCONF
978
 icmp_input:
979
#endif /* UIP_PINGADDRCONF */
980
  UIP_STAT(++uip_stat.icmp.recv);
981
982
  /* ICMP echo (i.e., ping) processing. This is simple, we only change
983
     the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
984
     checksum before we return the packet. */
985
  if(ICMPBUF->type != ICMP_ECHO) {
986
    UIP_STAT(++uip_stat.icmp.drop);
987
    UIP_STAT(++uip_stat.icmp.typeerr);
988
    UIP_LOG("icmp: not icmp echo.");
989
    goto drop;
990
  }
991
992
  /* If we are configured to use ping IP address assignment, we use
993
     the destination IP address of this ping packet and assign it to
994
     ourself. */
995
#if UIP_PINGADDRCONF
996
  if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
997
    uip_hostaddr[0] = BUF->destipaddr[0];
998
    uip_hostaddr[1] = BUF->destipaddr[1];
999
  }
1000
#endif /* UIP_PINGADDRCONF */
1001
1002
  ICMPBUF->type = ICMP_ECHO_REPLY;
1003
1004
  if(ICMPBUF->icmpchksum >= UIP_HTONS(0xffff - (ICMP_ECHO << 8))) {
1005
    ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8) + 1;
1006
  } else {
1007
    ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8);
1008
  }
1009
1010
  /* Swap IP addresses. */
1011
  uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
1012
  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
1013
1014
  UIP_STAT(++uip_stat.icmp.sent);
1015
  goto send;
1016
1017
  /* End of IPv4 input header processing code. */
1018
#else /* !UIP_CONF_IPV6 */
1019
1020
  /* This is IPv6 ICMPv6 processing code. */
1021
#ifndef WIN32
1022
  DEBUG_PRINTF("icmp6_input: length %d\n", uip_len);
1023
#endif
1024
1025
  if(BUF->proto != UIP_PROTO_ICMP6) { /* We only allow ICMPv6 packets from
1026
                                         here. */
1027
    UIP_STAT(++uip_stat.ip.drop);
1028
    UIP_STAT(++uip_stat.ip.protoerr);
1029
    UIP_LOG("ip: neither tcp nor icmp6.");
1030
    goto drop;
1031
  }
1032
1033
  UIP_STAT(++uip_stat.icmp.recv);
1034
1035
  /* If we get a neighbor solicitation for our address we should send
1036
     a neighbor advertisement message back. */
1037
  if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {
1038
    if(uip_ipaddr_cmp(ICMPBUF->icmp6data, uip_hostaddr)) {
1039
1040
      if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {
1041
        /* Save the sender's address in our neighbor list. */
1042
        uip_neighbor_add(ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));
1043
      }
1044
      
1045
      /* We should now send a neighbor advertisement back to where the
1046
         neighbor solicication came from. */
1047
      ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT;
1048
      ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */
1049
      
1050
      ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;
1051
      
1052
      uip_ipaddr_copy(ICMPBUF->destipaddr, ICMPBUF->srcipaddr);
1053
      uip_ipaddr_copy(ICMPBUF->srcipaddr, uip_hostaddr);
1054
      ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;
1055
      ICMPBUF->options[1] = 1;  /* Options length, 1 = 8 bytes. */
1056
      memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr));
1057
      ICMPBUF->icmpchksum = 0;
1058
      ICMPBUF->icmpchksum = ~uip_icmp6chksum();
1059
      goto send;
1060
      
1061
    }
1062
    goto drop;
1063
  } else if(ICMPBUF->type == ICMP6_ECHO) {
1064
    /* ICMP echo (i.e., ping) processing. This is simple, we only
1065
       change the ICMP type from ECHO to ECHO_REPLY and update the
1066
       ICMP checksum before we return the packet. */
1067
1068
    ICMPBUF->type = ICMP6_ECHO_REPLY;
1069
    
1070
    uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
1071
    uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
1072
    ICMPBUF->icmpchksum = 0;
1073
    ICMPBUF->icmpchksum = ~uip_icmp6chksum();
1074
    
1075
    UIP_STAT(++uip_stat.icmp.sent);
1076
    goto send;
1077
  } else {
1078
#ifndef WIN32
1079
    DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type);
1080
#endif
1081
    UIP_STAT(++uip_stat.icmp.drop);
1082
    UIP_STAT(++uip_stat.icmp.typeerr);
1083
    UIP_LOG("icmp: unknown ICMP message.");
1084
    goto drop;
1085
  }
1086
1087
  /* End of IPv6 ICMP processing. */
1088
  
1089
#endif /* !UIP_CONF_IPV6 */
1090
1091
#if UIP_UDP
1092
  /* UDP input processing. */
1093
 udp_input:
1094
  /* UDP processing is really just a hack. We don't do anything to the
1095
     UDP/IP headers, but let the UDP application do all the hard
1096
     work. If the application sets uip_slen, it has a packet to
1097
     send. */
1098
#if UIP_UDP_CHECKSUMS
1099
  uip_len = uip_len - UIP_IPUDPH_LEN;
1100
  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
1101
  if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {
1102
    UIP_STAT(++uip_stat.udp.drop);
1103
    UIP_STAT(++uip_stat.udp.chkerr);
1104
    UIP_LOG("udp: bad checksum.");
1105
    goto drop;
1106
  }
1107
#else /* UIP_UDP_CHECKSUMS */
1108
  uip_len = uip_len - UIP_IPUDPH_LEN;
1109
#endif /* UIP_UDP_CHECKSUMS */
1110
1111
  /* Demultiplex this UDP packet between the UDP "connections". */
1112
  for(uip_udp_conn = &uip_udp_conns[0];
1113
      uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
1114
      ++uip_udp_conn) {
1115
    /* Sven-Ola: If the remote port is 0xffff this is a new connection */
1116
    if(0xffff == uip_udp_conn->rport) {
1117
       uip_udp_conn->rport = UDPBUF->srcport;
1118
       memmove(&uip_udp_conn->ripaddr, &BUF->srcipaddr, sizeof(uip_udp_conn->ripaddr));
1119
    }
1120
    /* If the local UDP port is non-zero, the connection is considered
1121
       to be used. If so, the local port number is checked against the
1122
       destination port number in the received packet. If the two port
1123
       numbers match, the remote port number is checked if the
1124
       connection is bound to a remote port. Finally, if the
1125
       connection is bound to a remote IP address, the source IP
1126
       address of the packet is checked. */
1127
    if(uip_udp_conn->lport != 0 &&
1128
       UDPBUF->destport == uip_udp_conn->lport &&
1129
       (uip_udp_conn->rport == 0 ||
1130
        UDPBUF->srcport == uip_udp_conn->rport) &&
1131
       (uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_zeroes_addr) ||
1132
        uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_ones_addr) ||
1133
        uip_ipaddr_cmp(BUF->srcipaddr, uip_udp_conn->ripaddr))) {
1134
      goto udp_found;
1135
    }
1136
  }
1137
  UIP_LOG("udp: no matching connection found");
1138
  goto drop;
1139
  
1140
 udp_found:
1141
  uip_conn = NULL;
1142
  uip_flags = UIP_NEWDATA;
1143
  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
1144
  uip_slen = 0;
1145
  UIP_UDP_APPCALL();
1146
 udp_send:
1147
  if(uip_slen == 0) {
1148
    goto drop;
1149
  }
1150
  uip_len = uip_slen + UIP_IPUDPH_LEN;
1151
1152
#if UIP_CONF_IPV6
1153
  /* For IPv6, the IP length field does not include the IPv6 IP header
1154
     length. */
1155
  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
1156
  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
1157
#else /* UIP_CONF_IPV6 */
1158
  BUF->len[0] = (uip_len >> 8);
1159
  BUF->len[1] = (uip_len & 0xff);
1160
#endif /* UIP_CONF_IPV6 */
1161
1162
  BUF->ttl = uip_udp_conn->ttl;
1163
  BUF->proto = UIP_PROTO_UDP;
1164
1165
  UDPBUF->udplen = UIP_HTONS(uip_slen + UIP_UDPH_LEN);
1166
  UDPBUF->udpchksum = 0;
1167
1168
  BUF->srcport  = uip_udp_conn->lport;
1169
  BUF->destport = uip_udp_conn->rport;
1170
1171
  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
1172
  uip_ipaddr_copy(BUF->destipaddr, uip_udp_conn->ripaddr);
1173
   
1174
  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
1175
1176
#if UIP_UDP_CHECKSUMS
1177
  /* Calculate UDP checksum. */
1178
  UDPBUF->udpchksum = ~(uip_udpchksum());
1179
  if(UDPBUF->udpchksum == 0) {
1180
    UDPBUF->udpchksum = 0xffff;
1181
  }
1182
#endif /* UIP_UDP_CHECKSUMS */
1183
  
1184
  goto ip_send_nolen;
1185
#endif /* UIP_UDP */
1186
  
1187
  /* TCP input processing. */
1188
 tcp_input:
1189
  UIP_STAT(++uip_stat.tcp.recv);
1190
1191
  /* Start of TCP input header processing code. */
1192
  
1193
  if(uip_tcpchksum() != 0xffff) {   /* Compute and check the TCP
1194
                                       checksum. */
1195
    UIP_STAT(++uip_stat.tcp.drop);
1196
    UIP_STAT(++uip_stat.tcp.chkerr);
1197
    UIP_LOG("tcp: bad checksum.");
1198
    goto drop;
1199
  }
1200
  
1201
  
1202
  /* Demultiplex this segment. */
1203
  /* First check any active connections. */
1204
  for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];
1205
      ++uip_connr) {
1206
    if(uip_connr->tcpstateflags != UIP_CLOSED &&
1207
       BUF->destport == uip_connr->lport &&
1208
       BUF->srcport == uip_connr->rport &&
1209
       uip_ipaddr_cmp(BUF->srcipaddr, uip_connr->ripaddr)) {
1210
      goto found;
1211
    }
1212
  }
1213
1214
  /* If we didn't find and active connection that expected the packet,
1215
     either this packet is an old duplicate, or this is a SYN packet
1216
     destined for a connection in LISTEN. If the SYN flag isn't set,
1217
     it is an old packet and we send a RST. */
1218
  if((BUF->flags & TCP_CTL) != TCP_SYN) {
1219
    goto reset;
1220
  }
1221
  
1222
  tmp16 = BUF->destport;
1223
  /* Next, check listening connections. */
1224
  for(c = 0; c < UIP_LISTENPORTS; ++c) {
1225
    if(tmp16 == uip_listenports[c])
1226
      goto found_listen;
1227
  }
1228
  
1229
  /* No matching connection found, so we send a RST packet. */
1230
  UIP_STAT(++uip_stat.tcp.synrst);
1231
 reset:
1232
1233
  /* We do not send resets in response to resets. */
1234
  if(BUF->flags & TCP_RST) {
1235
    goto drop;
1236
  }
1237
1238
  UIP_STAT(++uip_stat.tcp.rst);
1239
  
1240
  BUF->flags = TCP_RST | TCP_ACK;
1241
  uip_len = UIP_IPTCPH_LEN;
1242
  BUF->tcpoffset = 5 << 4;
1243
1244
  /* Flip the seqno and ackno fields in the TCP header. */
1245
  c = BUF->seqno[3];
1246
  BUF->seqno[3] = BUF->ackno[3];
1247
  BUF->ackno[3] = c;
1248
  
1249
  c = BUF->seqno[2];
1250
  BUF->seqno[2] = BUF->ackno[2];
1251
  BUF->ackno[2] = c;
1252
  
1253
  c = BUF->seqno[1];
1254
  BUF->seqno[1] = BUF->ackno[1];
1255
  BUF->ackno[1] = c;
1256
  
1257
  c = BUF->seqno[0];
1258
  BUF->seqno[0] = BUF->ackno[0];
1259
  BUF->ackno[0] = c;
1260
1261
  /* We also have to increase the sequence number we are
1262
     acknowledging. If the least significant byte overflowed, we need
1263
     to propagate the carry to the other bytes as well. */
1264
  if(++BUF->ackno[3] == 0) {
1265
    if(++BUF->ackno[2] == 0) {
1266
      if(++BUF->ackno[1] == 0) {
1267
        ++BUF->ackno[0];
1268
      }
1269
    }
1270
  }
1271
 
1272
  /* Swap port numbers. */
1273
  tmp16 = BUF->srcport;
1274
  BUF->srcport = BUF->destport;
1275
  BUF->destport = tmp16;
1276
  
1277
  /* Swap IP addresses. */
1278
  uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
1279
  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
1280
  
1281
  /* And send out the RST packet! */
1282
  goto tcp_send_noconn;
1283
1284
  /* This label will be jumped to if we matched the incoming packet
1285
     with a connection in LISTEN. In that case, we should create a new
1286
     connection and send a SYNACK in return. */
1287
 found_listen:
1288
  /* First we check if there are any connections avaliable. Unused
1289
     connections are kept in the same table as used connections, but
1290
     unused ones have the tcpstate set to CLOSED. Also, connections in
1291
     TIME_WAIT are kept track of and we'll use the oldest one if no
1292
     CLOSED connections are found. Thanks to Eddie C. Dost for a very
1293
     nice algorithm for the TIME_WAIT search. */
1294
  uip_connr = 0;
1295
  for(c = 0; c < UIP_CONNS; ++c) {
1296
    if(uip_conns[c].tcpstateflags == UIP_CLOSED) {
1297
      uip_connr = &uip_conns[c];
1298
      break;
1299
    }
1300
    if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {
1301
      if(uip_connr == 0 ||
1302
         uip_conns[c].timer > uip_connr->timer) {
1303
        uip_connr = &uip_conns[c];
1304
      }
1305
    }
1306
  }
1307
1308
  if(uip_connr == 0) {
1309
    /* All connections are used already, we drop packet and hope that
1310
       the remote end will retransmit the packet at a time when we
1311
       have more spare connections. */
1312
    UIP_STAT(++uip_stat.tcp.syndrop);
1313
    UIP_LOG("tcp: found no unused connections.");
1314
    goto drop;
1315
  }
1316
  uip_conn = uip_connr;
1317
  
1318
  /* Fill in the necessary fields for the new connection. */
1319
  uip_connr->rto = uip_connr->timer = UIP_RTO;
1320
  uip_connr->sa = 0;
1321
  uip_connr->sv = 4;
1322
  uip_connr->nrtx = 0;
1323
  uip_connr->lport = BUF->destport;
1324
  uip_connr->rport = BUF->srcport;
1325
  uip_ipaddr_copy(uip_connr->ripaddr, BUF->srcipaddr);
1326
  uip_connr->tcpstateflags = UIP_SYN_RCVD;
1327
1328
  uip_connr->snd_nxt[0] = iss[0];
1329
  uip_connr->snd_nxt[1] = iss[1];
1330
  uip_connr->snd_nxt[2] = iss[2];
1331
  uip_connr->snd_nxt[3] = iss[3];
1332
  uip_connr->len = 1;
1333
1334
  /* rcv_nxt should be the seqno from the incoming packet + 1. */
1335
  uip_connr->rcv_nxt[3] = BUF->seqno[3];
1336
  uip_connr->rcv_nxt[2] = BUF->seqno[2];
1337
  uip_connr->rcv_nxt[1] = BUF->seqno[1];
1338
  uip_connr->rcv_nxt[0] = BUF->seqno[0];
1339
  uip_add_rcv_nxt(1);
1340
1341
  /* Parse the TCP MSS option, if present. */
1342
  if((BUF->tcpoffset & 0xf0) > 0x50) {
1343
    for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
1344
      opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
1345
      if(opt == TCP_OPT_END) {
1346
        /* End of options. */
1347
        break;
1348
      } else if(opt == TCP_OPT_NOOP) {
1349
        ++c;
1350
        /* NOP option. */
1351
      } else if(opt == TCP_OPT_MSS &&
1352
                uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
1353
        /* An MSS option with the right option length. */
1354
        tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1355
          (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];
1356
        uip_connr->initialmss = uip_connr->mss =
1357
          tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1358
        
1359
        /* And we are done processing options. */
1360
        break;
1361
      } else {
1362
        /* All other options have a length field, so that we easily
1363
           can skip past them. */
1364
        if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1365
          /* If the length field is zero, the options are malformed
1366
             and we don't process them further. */
1367
          break;
1368
        }
1369
        c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1370
      }
1371
    }
1372
  }
1373
  
1374
  /* Our response will be a SYNACK. */
1375
#if UIP_ACTIVE_OPEN
1376
 tcp_send_synack:
1377
  BUF->flags = TCP_ACK;
1378
  
1379
 tcp_send_syn:
1380
  BUF->flags |= TCP_SYN;
1381
#else /* UIP_ACTIVE_OPEN */
1382
 tcp_send_synack:
1383
  BUF->flags = TCP_SYN | TCP_ACK;
1384
#endif /* UIP_ACTIVE_OPEN */
1385
  
1386
  /* We send out the TCP Maximum Segment Size option with our
1387
     SYNACK. */
1388
  BUF->optdata[0] = TCP_OPT_MSS;
1389
  BUF->optdata[1] = TCP_OPT_MSS_LEN;
1390
  BUF->optdata[2] = (UIP_TCP_MSS) / 256;
1391
  BUF->optdata[3] = (UIP_TCP_MSS) & 255;
1392
  uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;
1393
  BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;
1394
  goto tcp_send;
1395
1396
  /* This label will be jumped to if we found an active connection. */
1397
 found:
1398
  uip_conn = uip_connr;
1399
  uip_flags = 0;
1400
  /* We do a very naive form of TCP reset processing; we just accept
1401
     any RST and kill our connection. We should in fact check if the
1402
     sequence number of this reset is wihtin our advertised window
1403
     before we accept the reset. */
1404
  if(BUF->flags & TCP_RST) {
1405
#if 0
1406
    uip_connr->tcpstateflags = UIP_CLOSED;
1407
    UIP_LOG("tcp: got reset, aborting connection.");
1408
    uip_flags = UIP_ABORT;
1409
    UIP_APPCALL();
1410
#else
1411
    /* Sven-Ola: we simply ignore... */
1412
    UIP_LOG("tcp: got reset, try to continue.");
1413
#endif
1414
    goto drop;
1415
  }
1416
  /* Calculated the length of the data, if the application has sent
1417
     any data to us. */
1418
  c = (BUF->tcpoffset >> 4) << 2;
1419
  /* uip_len will contain the length of the actual TCP data. This is
1420
     calculated by subtracing the length of the TCP header (in
1421
     c) and the length of the IP header (20 bytes). */
1422
  uip_len = uip_len - c - UIP_IPH_LEN;
1423
1424
  /* First, check if the sequence number of the incoming packet is
1425
     what we're expecting next. If not, we send out an ACK with the
1426
     correct numbers in. */
1427
  if(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
1428
       ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) {
1429
    if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&
1430
       (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
1431
        BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
1432
        BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
1433
        BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
1434
      goto tcp_send_ack;
1435
    }
1436
  }
1437
1438
  /* Next, check if the incoming segment acknowledges any outstanding
1439
     data. If so, we update the sequence number, reset the length of
1440
     the outstanding data, calculate RTT estimations, and reset the
1441
     retransmission timer. */
1442
  if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
1443
    uip_add32(uip_connr->snd_nxt, uip_connr->len);
1444
1445
    if(BUF->ackno[0] == uip_acc32[0] &&
1446
       BUF->ackno[1] == uip_acc32[1] &&
1447
       BUF->ackno[2] == uip_acc32[2] &&
1448
       BUF->ackno[3] == uip_acc32[3]) {
1449
      /* Update sequence number. */
1450
      uip_connr->snd_nxt[0] = uip_acc32[0];
1451
      uip_connr->snd_nxt[1] = uip_acc32[1];
1452
      uip_connr->snd_nxt[2] = uip_acc32[2];
1453
      uip_connr->snd_nxt[3] = uip_acc32[3];
1454
        
1455
1456
      /* Do RTT estimation, unless we have done retransmissions. */
1457
      if(uip_connr->nrtx == 0) {
1458
        signed char m;
1459
        m = uip_connr->rto - uip_connr->timer;
1460
        /* This is taken directly from VJs original code in his paper */
1461
        m = m - (uip_connr->sa >> 3);
1462
        uip_connr->sa += m;
1463
        if(m < 0) {
1464
          m = -m;
1465
        }
1466
        m = m - (uip_connr->sv >> 2);
1467
        uip_connr->sv += m;
1468
        uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
1469
1470
      }
1471
      /* Set the acknowledged flag. */
1472
      uip_flags = UIP_ACKDATA;
1473
      /* Reset the retransmission timer. */
1474
      uip_connr->timer = uip_connr->rto;
1475
1476
      /* Reset length of outstanding data. */
1477
      uip_connr->len = 0;
1478
    }
1479
    
1480
  }
1481
1482
  /* Do different things depending on in what state the connection is. */
1483
  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
1484
    /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
1485
        implemented, since we force the application to close when the
1486
        peer sends a FIN (hence the application goes directly from
1487
        ESTABLISHED to LAST_ACK). */
1488
  case UIP_SYN_RCVD:
1489
    /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
1490
       we are waiting for an ACK that acknowledges the data we sent
1491
       out the last time. Therefore, we want to have the UIP_ACKDATA
1492
       flag set. If so, we enter the ESTABLISHED state. */
1493
    if(uip_flags & UIP_ACKDATA) {
1494
      uip_connr->tcpstateflags = UIP_ESTABLISHED;
1495
      uip_flags = UIP_CONNECTED;
1496
      uip_connr->len = 0;
1497
      if(uip_len > 0) {
1498
        uip_flags |= UIP_NEWDATA;
1499
        uip_add_rcv_nxt(uip_len);
1500
      }
1501
      uip_slen = 0;
1502
      UIP_APPCALL();
1503
      goto appsend;
1504
    }
1505
    goto drop;
1506
#if UIP_ACTIVE_OPEN
1507
  case UIP_SYN_SENT:
1508
    /* In SYN_SENT, we wait for a SYNACK that is sent in response to
1509
       our SYN. The rcv_nxt is set to sequence number in the SYNACK
1510
       plus one, and we send an ACK. We move into the ESTABLISHED
1511
       state. */
1512
    if((uip_flags & UIP_ACKDATA) &&
1513
       (BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) {
1514
1515
      /* Parse the TCP MSS option, if present. */
1516
      if((BUF->tcpoffset & 0xf0) > 0x50) {
1517
        for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
1518
          opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c];
1519
          if(opt == TCP_OPT_END) {
1520
            /* End of options. */
1521
            break;
1522
          } else if(opt == TCP_OPT_NOOP) {
1523
            ++c;
1524
            /* NOP option. */
1525
          } else if(opt == TCP_OPT_MSS &&
1526
                    uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
1527
            /* An MSS option with the right option length. */
1528
            tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1529
              uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
1530
            uip_connr->initialmss =
1531
              uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1532
1533
            /* And we are done processing options. */
1534
            break;
1535
          } else {
1536
            /* All other options have a length field, so that we easily
1537
               can skip past them. */
1538
            if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1539
              /* If the length field is zero, the options are malformed
1540
                 and we don't process them further. */
1541
              break;
1542
            }
1543
            c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1544
          }
1545
        }
1546
      }
1547
      uip_connr->tcpstateflags = UIP_ESTABLISHED;
1548
      uip_connr->rcv_nxt[0] = BUF->seqno[0];
1549
      uip_connr->rcv_nxt[1] = BUF->seqno[1];
1550
      uip_connr->rcv_nxt[2] = BUF->seqno[2];
1551
      uip_connr->rcv_nxt[3] = BUF->seqno[3];
1552
      uip_add_rcv_nxt(1);
1553
      uip_flags = UIP_CONNECTED | UIP_NEWDATA;
1554
      uip_connr->len = 0;
1555
      uip_len = 0;
1556
      uip_slen = 0;
1557
      UIP_APPCALL();
1558
      goto appsend;
1559
    }
1560
    /* Inform the application that the connection failed */
1561
    uip_flags = UIP_ABORT;
1562
    UIP_APPCALL();
1563
    /* The connection is closed after we send the RST */
1564
    uip_conn->tcpstateflags = UIP_CLOSED;
1565
    goto reset;
1566
#endif /* UIP_ACTIVE_OPEN */
1567
    
1568
  case UIP_ESTABLISHED:
1569
    /* In the ESTABLISHED state, we call upon the application to feed
1570
    data into the uip_buf. If the UIP_ACKDATA flag is set, the
1571
    application should put new data into the buffer, otherwise we are
1572
    retransmitting an old segment, and the application should put that
1573
    data into the buffer.
1574
1575
    If the incoming packet is a FIN, we should close the connection on
1576
    this side as well, and we send out a FIN and enter the LAST_ACK
1577
    state. We require that there is no outstanding data; otherwise the
1578
    sequence numbers will be screwed up. */
1579
1580
    if(BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
1581
      if(uip_outstanding(uip_connr)) {
1582
        goto drop;
1583
      }
1584
      uip_add_rcv_nxt(1 + uip_len);
1585
      uip_flags |= UIP_CLOSE;
1586
      if(uip_len > 0) {
1587
        uip_flags |= UIP_NEWDATA;
1588
      }
1589
      UIP_APPCALL();
1590
      uip_connr->len = 1;
1591
      uip_connr->tcpstateflags = UIP_LAST_ACK;
1592
      uip_connr->nrtx = 0;
1593
    tcp_send_finack:
1594
      BUF->flags = TCP_FIN | TCP_ACK;
1595
      goto tcp_send_nodata;
1596
    }
1597
1598
    /* Check the URG flag. If this is set, the segment carries urgent
1599
       data that we must pass to the application. */
1600
    if((BUF->flags & TCP_URG) != 0) {
1601
#if UIP_URGDATA > 0
1602
      uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
1603
      if(uip_urglen > uip_len) {
1604
        /* There is more urgent data in the next segment to come. */
1605
        uip_urglen = uip_len;
1606
      }
1607
      uip_add_rcv_nxt(uip_urglen);
1608
      uip_len -= uip_urglen;
1609
      uip_urgdata = uip_appdata;
1610
      uip_appdata += uip_urglen;
1611
    } else {
1612
      uip_urglen = 0;
1613
#else /* UIP_URGDATA > 0 */
1614
      uip_appdata = ((char *)uip_appdata) + ((BUF->urgp[0] << 8) | BUF->urgp[1]);
1615
      uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
1616
#endif /* UIP_URGDATA > 0 */
1617
    }
1618
1619
    /* If uip_len > 0 we have TCP data in the packet, and we flag this
1620
       by setting the UIP_NEWDATA flag and update the sequence number
1621
       we acknowledge. If the application has stopped the dataflow
1622
       using uip_stop(), we must not accept any data packets from the
1623
       remote host. */
1624
    if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
1625
      uip_flags |= UIP_NEWDATA;
1626
      uip_add_rcv_nxt(uip_len);
1627
    }
1628
1629
    /* Check if the available buffer space advertised by the other end
1630
       is smaller than the initial MSS for this connection. If so, we
1631
       set the current MSS to the window size to ensure that the
1632
       application does not send more data than the other end can
1633
       handle.
1634
1635
       If the remote host advertises a zero window, we set the MSS to
1636
       the initial MSS so that the application will send an entire MSS
1637
       of data. This data will not be acknowledged by the receiver,
1638
       and the application will retransmit it. This is called the
1639
       "persistent timer" and uses the retransmission mechanim.
1640
    */
1641
    tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
1642
    if(tmp16 > uip_connr->initialmss ||
1643
       tmp16 == 0) {
1644
      tmp16 = uip_connr->initialmss;
1645
    }
1646
    uip_connr->mss = tmp16;
1647
1648
    /* If this packet constitutes an ACK for outstanding data (flagged
1649
       by the UIP_ACKDATA flag, we should call the application since it
1650
       might want to send more data. If the incoming packet had data
1651
       from the peer (as flagged by the UIP_NEWDATA flag), the
1652
       application must also be notified.
1653
1654
       When the application is called, the global variable uip_len
1655
       contains the length of the incoming data. The application can
1656
       access the incoming data through the global pointer
1657
       uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN
1658
       bytes into the uip_buf array.
1659
1660
       If the application wishes to send any data, this data should be
1661
       put into the uip_appdata and the length of the data should be
1662
       put into uip_len. If the application don't have any data to
1663
       send, uip_len must be set to 0. */
1664
    if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
1665
      uip_slen = 0;
1666
      UIP_APPCALL();
1667
1668
    appsend:
1669
      
1670
      if(uip_flags & UIP_ABORT) {
1671
        uip_slen = 0;
1672
        uip_connr->tcpstateflags = UIP_CLOSED;
1673
        BUF->flags = TCP_RST | TCP_ACK;
1674
        goto tcp_send_nodata;
1675
      }
1676
1677
      if(uip_flags & UIP_CLOSE) {
1678
        uip_slen = 0;
1679
        uip_connr->len = 1;
1680
        uip_connr->tcpstateflags = UIP_FIN_WAIT_1;
1681
        uip_connr->nrtx = 0;
1682
        BUF->flags = TCP_FIN | TCP_ACK;
1683
        goto tcp_send_nodata;
1684
      }
1685
1686
      /* If uip_slen > 0, the application has data to be sent. */
1687
      if(uip_slen > 0) {
1688
1689
        /* If the connection has acknowledged data, the contents of
1690
           the ->len variable should be discarded. */
1691
        if((uip_flags & UIP_ACKDATA) != 0) {
1692
          uip_connr->len = 0;
1693
        }
1694
1695
        /* If the ->len variable is non-zero the connection has
1696
           already data in transit and cannot send anymore right
1697
           now. */
1698
        if(uip_connr->len == 0) {
1699
1700
          /* The application cannot send more than what is allowed by
1701
             the mss (the minumum of the MSS and the available
1702
             window). */
1703
          if(uip_slen > uip_connr->mss) {
1704
            uip_slen = uip_connr->mss;
1705
          }
1706
1707
          /* Remember how much data we send out now so that we know
1708
             when everything has been acknowledged. */
1709
          uip_connr->len = uip_slen;
1710
        } else {
1711
1712
          /* If the application already had unacknowledged data, we
1713
             make sure that the application does not send (i.e.,
1714
             retransmit) out more than it previously sent out. */
1715
          uip_slen = uip_connr->len;
1716
        }
1717
      }
1718
      uip_connr->nrtx = 0;
1719
    apprexmit:
1720
      uip_appdata = uip_sappdata;
1721
      
1722
      /* If the application has data to be sent, or if the incoming
1723
         packet had new data in it, we must send out a packet. */
1724
      if(uip_slen > 0 && uip_connr->len > 0) {
1725
        /* Add the length of the IP and TCP headers. */
1726
        uip_len = uip_connr->len + UIP_TCPIP_HLEN;
1727
        /* We always set the ACK flag in response packets. */
1728
        BUF->flags = TCP_ACK | TCP_PSH;
1729
        /* Send the packet. */
1730
        goto tcp_send_noopts;
1731
      }
1732
      /* If there is no data to send, just send out a pure ACK if
1733
         there is newdata. */
1734
      if(uip_flags & UIP_NEWDATA) {
1735
        uip_len = UIP_TCPIP_HLEN;
1736
        BUF->flags = TCP_ACK;
1737
        goto tcp_send_noopts;
1738
      }
1739
    }
1740
    goto drop;
1741
  case UIP_LAST_ACK:
1742
    /* We can close this connection if the peer has acknowledged our
1743
       FIN. This is indicated by the UIP_ACKDATA flag. */
1744
    if(uip_flags & UIP_ACKDATA) {
1745
      uip_connr->tcpstateflags = UIP_CLOSED;
1746
      uip_flags = UIP_CLOSE;
1747
      UIP_APPCALL();
1748
    }
1749
    break;
1750
    
1751
  case UIP_FIN_WAIT_1:
1752
    /* The application has closed the connection, but the remote host
1753
       hasn't closed its end yet. Thus we do nothing but wait for a
1754
       FIN from the other side. */
1755
    if(uip_len > 0) {
1756
      uip_add_rcv_nxt(uip_len);
1757
    }
1758
    if(BUF->flags & TCP_FIN) {
1759
      if(uip_flags & UIP_ACKDATA) {
1760
        uip_connr->tcpstateflags = UIP_TIME_WAIT;
1761
        uip_connr->timer = 0;
1762
        uip_connr->len = 0;
1763
      } else {
1764
        uip_connr->tcpstateflags = UIP_CLOSING;
1765
      }
1766
      uip_add_rcv_nxt(1);
1767
      uip_flags = UIP_CLOSE;
1768
      UIP_APPCALL();
1769
      goto tcp_send_ack;
1770
    } else if(uip_flags & UIP_ACKDATA) {
1771
      uip_connr->tcpstateflags = UIP_FIN_WAIT_2;
1772
      uip_connr->len = 0;
1773
      goto drop;
1774
    }
1775
    if(uip_len > 0) {
1776
      goto tcp_send_ack;
1777
    }
1778
    goto drop;
1779
      
1780
  case UIP_FIN_WAIT_2:
1781
    if(uip_len > 0) {
1782
      uip_add_rcv_nxt(uip_len);
1783
    }
1784
    if(BUF->flags & TCP_FIN) {
1785
      uip_connr->tcpstateflags = UIP_TIME_WAIT;
1786
      uip_connr->timer = 0;
1787
      uip_add_rcv_nxt(1);
1788
      uip_flags = UIP_CLOSE;
1789
      UIP_APPCALL();
1790
      goto tcp_send_ack;
1791
    }
1792
    if(uip_len > 0) {
1793
      goto tcp_send_ack;
1794
    }
1795
    goto drop;
1796
1797
  case UIP_TIME_WAIT:
1798
    goto tcp_send_ack;
1799
    
1800
  case UIP_CLOSING:
1801
    if(uip_flags & UIP_ACKDATA) {
1802
      uip_connr->tcpstateflags = UIP_TIME_WAIT;
1803
      uip_connr->timer = 0;
1804
    }
1805
  }
1806
  goto drop;
1807
  
1808
1809
  /* We jump here when we are ready to send the packet, and just want
1810
     to set the appropriate TCP sequence numbers in the TCP header. */
1811
 tcp_send_ack:
1812
  BUF->flags = TCP_ACK;
1813
 tcp_send_nodata:
1814
  uip_len = UIP_IPTCPH_LEN;
1815
 tcp_send_noopts:
1816
  BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;
1817
 tcp_send:
1818
  /* We're done with the input processing. We are now ready to send a
1819
     reply. Our job is to fill in all the fields of the TCP and IP
1820
     headers before calculating the checksum and finally send the
1821
     packet. */
1822
  BUF->ackno[0] = uip_connr->rcv_nxt[0];
1823
  BUF->ackno[1] = uip_connr->rcv_nxt[1];
1824
  BUF->ackno[2] = uip_connr->rcv_nxt[2];
1825
  BUF->ackno[3] = uip_connr->rcv_nxt[3];
1826
  
1827
  BUF->seqno[0] = uip_connr->snd_nxt[0];
1828
  BUF->seqno[1] = uip_connr->snd_nxt[1];
1829
  BUF->seqno[2] = uip_connr->snd_nxt[2];
1830
  BUF->seqno[3] = uip_connr->snd_nxt[3];
1831
1832
  BUF->proto = UIP_PROTO_TCP;
1833
  
1834
  BUF->srcport  = uip_connr->lport;
1835
  BUF->destport = uip_connr->rport;
1836
1837
  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
1838
  uip_ipaddr_copy(BUF->destipaddr, uip_connr->ripaddr);
1839
1840
  if(uip_connr->tcpstateflags & UIP_STOPPED) {
1841
    /* If the connection has issued uip_stop(), we advertise a zero
1842
       window so that the remote host will stop sending data. */
1843
    BUF->wnd[0] = BUF->wnd[1] = 0;
1844
  } else {
1845
    BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
1846
    BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
1847
  }
1848
1849
 tcp_send_noconn:
1850
  BUF->ttl = UIP_TTL;
1851
#if UIP_CONF_IPV6
1852
  /* For IPv6, the IP length field does not include the IPv6 IP header
1853
     length. */
1854
  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
1855
  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
1856
#else /* UIP_CONF_IPV6 */
1857
  BUF->len[0] = (uip_len >> 8);
1858
  BUF->len[1] = (uip_len & 0xff);
1859
#endif /* UIP_CONF_IPV6 */
1860
1861
  BUF->urgp[0] = BUF->urgp[1] = 0;
1862
  
1863
  /* Calculate TCP checksum. */
1864
  BUF->tcpchksum = 0;
1865
  BUF->tcpchksum = ~(uip_tcpchksum());
1866
  
1867
 ip_send_nolen:
1868
1869
#if UIP_CONF_IPV6
1870
  BUF->vtc = 0x60;
1871
  BUF->tcflow = 0x00;
1872
  BUF->flow = 0x00;
1873
#else /* UIP_CONF_IPV6 */
1874
  BUF->vhl = 0x45;
1875
  BUF->tos = 0;
1876
  BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
1877
  ++ipid;
1878
  BUF->ipid[0] = ipid >> 8;
1879
  BUF->ipid[1] = ipid & 0xff;
1880
  /* Calculate IP checksum. */
1881
  BUF->ipchksum = 0;
1882
  BUF->ipchksum = ~(uip_ipchksum());
1883
#ifndef WIN32
1884
  DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum());
1885
#endif
1886
#endif /* UIP_CONF_IPV6 */
1887
   
1888
  UIP_STAT(++uip_stat.tcp.sent);
1889
 send:
1890
#ifndef WIN32
1891
  DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len,
1892
               (BUF->len[0] << 8) | BUF->len[1]);
1893
#endif
1894
  
1895
  UIP_STAT(++uip_stat.ip.sent);
1896
  /* Return and let the caller do the actual transmission. */
1897
  uip_flags = 0;
1898
  return;
1899
 drop:
1900
  uip_len = 0;
1901
  uip_flags = 0;
1902
  return;
1903
}
1904
/*---------------------------------------------------------------------------*/
1905
u16_t
1906
myhtons(u16_t val)
1907
{
1908
  return UIP_HTONS(val);
1909
}
1910
/*---------------------------------------------------------------------------*/
1911
void
1912
uip_send(const void *data, int len)
1913
{
1914
  if(len > 0) {
1915
    uip_slen = len;
1916
    if(data != uip_sappdata) {
1917
      memcpy(uip_sappdata, (data), uip_slen);
1918
    }
1919
  }
1920
}
1921
/** @} */