OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_Demo_Rowley_ARM7/] [lwip-1.1.0/] [src/] [core/] [tcp.c] - Blame information for rev 583

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/**
2
 * @file
3
 *
4
 * Transmission Control Protocol for IP
5
 *
6
 * This file contains common functions for the TCP implementation, such as functinos
7
 * for manipulating the data structures and the TCP timer functions. TCP functions
8
 * related to input and output is found in tcp_in.c and tcp_out.c respectively.
9
 *
10
 */
11
 
12
/*
13
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
14
 * All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without modification,
17
 * are permitted provided that the following conditions are met:
18
 *
19
 * 1. Redistributions of source code must retain the above copyright notice,
20
 *    this list of conditions and the following disclaimer.
21
 * 2. Redistributions in binary form must reproduce the above copyright notice,
22
 *    this list of conditions and the following disclaimer in the documentation
23
 *    and/or other materials provided with the distribution.
24
 * 3. The name of the author may not be used to endorse or promote products
25
 *    derived from this software without specific prior written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
30
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36
 * OF SUCH DAMAGE.
37
 *
38
 * This file is part of the lwIP TCP/IP stack.
39
 *
40
 * Author: Adam Dunkels <adam@sics.se>
41
 *
42
 */
43
 
44
#include <string.h>
45
 
46
#include "lwip/opt.h"
47
#include "lwip/def.h"
48
#include "lwip/mem.h"
49
#include "lwip/memp.h"
50
 
51
#include "lwip/tcp.h"
52
#if LWIP_TCP
53
 
54
/* Incremented every coarse grained timer shot
55
   (typically every 500 ms, determined by TCP_COARSE_TIMEOUT). */
56
u32_t tcp_ticks;
57
const u8_t tcp_backoff[13] =
58
    { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
59
 
60
/* The TCP PCB lists. */
61
 
62
/** List of all TCP PCBs in LISTEN state */
63
union tcp_listen_pcbs_t tcp_listen_pcbs;
64
/** List of all TCP PCBs that are in a state in which
65
 * they accept or send data. */
66
struct tcp_pcb *tcp_active_pcbs;
67
/** List of all TCP PCBs in TIME-WAIT state */
68
struct tcp_pcb *tcp_tw_pcbs;
69
 
70
struct tcp_pcb *tcp_tmp_pcb;
71
 
72
static u8_t tcp_timer;
73
static u16_t tcp_new_port(void);
74
 
75
/**
76
 * Initializes the TCP layer.
77
 */
78
void
79
tcp_init(void)
80
{
81
  /* Clear globals. */
82
  tcp_listen_pcbs.listen_pcbs = NULL;
83
  tcp_active_pcbs = NULL;
84
  tcp_tw_pcbs = NULL;
85
  tcp_tmp_pcb = NULL;
86
 
87
  /* initialize timer */
88
  tcp_ticks = 0;
89
  tcp_timer = 0;
90
 
91
}
92
 
93
/**
94
 * Called periodically to dispatch TCP timers.
95
 *
96
 */
97
void
98
tcp_tmr(void)
99
{
100
  /* Call tcp_fasttmr() every 250 ms */
101
  tcp_fasttmr();
102
 
103
  if (++tcp_timer & 1) {
104
    /* Call tcp_tmr() every 500 ms, i.e., every other timer
105
       tcp_tmr() is called. */
106
    tcp_slowtmr();
107
  }
108
}
109
 
110
/**
111
 * Closes the connection held by the PCB.
112
 *
113
 */
114
err_t
115
tcp_close(struct tcp_pcb *pcb)
116
{
117
  err_t err;
118
 
119
#if TCP_DEBUG
120
  LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in state "));
121
  tcp_debug_print_state(pcb->state);
122
  LWIP_DEBUGF(TCP_DEBUG, ("\n"));
123
#endif /* TCP_DEBUG */
124
  switch (pcb->state) {
125
  case CLOSED:
126
    /* Closing a pcb in the CLOSED state might seem erroneous,
127
     * however, it is in this state once allocated and as yet unused
128
     * and the user needs some way to free it should the need arise.
129
     * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
130
     * or for a pcb that has been used and then entered the CLOSED state
131
     * is erroneous, but this should never happen as the pcb has in those cases
132
     * been freed, and so any remaining handles are bogus. */
133
    err = ERR_OK;
134
    memp_free(MEMP_TCP_PCB, pcb);
135
    pcb = NULL;
136
    break;
137
  case LISTEN:
138
    err = ERR_OK;
139
    tcp_pcb_remove((struct tcp_pcb **)&tcp_listen_pcbs.pcbs, pcb);
140
    memp_free(MEMP_TCP_PCB_LISTEN, pcb);
141
    pcb = NULL;
142
    break;
143
  case SYN_SENT:
144
    err = ERR_OK;
145
    tcp_pcb_remove(&tcp_active_pcbs, pcb);
146
    memp_free(MEMP_TCP_PCB, pcb);
147
    pcb = NULL;
148
    break;
149
  case SYN_RCVD:
150
  case ESTABLISHED:
151
    err = tcp_send_ctrl(pcb, TCP_FIN);
152
    if (err == ERR_OK) {
153
      pcb->state = FIN_WAIT_1;
154
    }
155
    break;
156
  case CLOSE_WAIT:
157
    err = tcp_send_ctrl(pcb, TCP_FIN);
158
    if (err == ERR_OK) {
159
      pcb->state = LAST_ACK;
160
    }
161
    break;
162
  default:
163
    /* Has already been closed, do nothing. */
164
    err = ERR_OK;
165
    pcb = NULL;
166
    break;
167
  }
168
 
169
  if (pcb != NULL && err == ERR_OK) {
170
    err = tcp_output(pcb);
171
  }
172
  return err;
173
}
174
 
175
/**
176
 * Aborts a connection by sending a RST to the remote host and deletes
177
 * the local protocol control block. This is done when a connection is
178
 * killed because of shortage of memory.
179
 *
180
 */
181
void
182
tcp_abort(struct tcp_pcb *pcb)
183
{
184
  u32_t seqno, ackno;
185
  u16_t remote_port, local_port;
186
  struct ip_addr remote_ip, local_ip;
187
#if LWIP_CALLBACK_API  
188
  void (* errf)(void *arg, err_t err);
189
#endif /* LWIP_CALLBACK_API */
190
  void *errf_arg;
191
 
192
 
193
  /* Figure out on which TCP PCB list we are, and remove us. If we
194
     are in an active state, call the receive function associated with
195
     the PCB with a NULL argument, and send an RST to the remote end. */
196
  if (pcb->state == TIME_WAIT) {
197
    tcp_pcb_remove(&tcp_tw_pcbs, pcb);
198
    memp_free(MEMP_TCP_PCB, pcb);
199
  } else {
200
    seqno = pcb->snd_nxt;
201
    ackno = pcb->rcv_nxt;
202
    ip_addr_set(&local_ip, &(pcb->local_ip));
203
    ip_addr_set(&remote_ip, &(pcb->remote_ip));
204
    local_port = pcb->local_port;
205
    remote_port = pcb->remote_port;
206
#if LWIP_CALLBACK_API
207
    errf = pcb->errf;
208
#endif /* LWIP_CALLBACK_API */
209
    errf_arg = pcb->callback_arg;
210
    tcp_pcb_remove(&tcp_active_pcbs, pcb);
211
    if (pcb->unacked != NULL) {
212
      tcp_segs_free(pcb->unacked);
213
    }
214
    if (pcb->unsent != NULL) {
215
      tcp_segs_free(pcb->unsent);
216
    }
217
#if TCP_QUEUE_OOSEQ    
218
    if (pcb->ooseq != NULL) {
219
      tcp_segs_free(pcb->ooseq);
220
    }
221
#endif /* TCP_QUEUE_OOSEQ */
222
    memp_free(MEMP_TCP_PCB, pcb);
223
    TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
224
    LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abort: sending RST\n"));
225
    tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port);
226
  }
227
}
228
 
229
/**
230
 * Binds the connection to a local portnumber and IP address. If the
231
 * IP address is not given (i.e., ipaddr == NULL), the IP address of
232
 * the outgoing network interface is used instead.
233
 *
234
 */
235
 
236
err_t
237
tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
238
{
239
  struct tcp_pcb *cpcb;
240
#if SO_REUSE
241
  int reuse_port_all_set = 1;
242
#endif /* SO_REUSE */
243
 
244
  if (port == 0) {
245
    port = tcp_new_port();
246
  }
247
#if SO_REUSE == 0
248
  /* Check if the address already is in use. */
249
  for(cpcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs;
250
      cpcb != NULL; cpcb = cpcb->next) {
251
    if (cpcb->local_port == port) {
252
      if (ip_addr_isany(&(cpcb->local_ip)) ||
253
        ip_addr_isany(ipaddr) ||
254
        ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
255
          return ERR_USE;
256
      }
257
    }
258
  }
259
  for(cpcb = tcp_active_pcbs;
260
      cpcb != NULL; cpcb = cpcb->next) {
261
    if (cpcb->local_port == port) {
262
      if (ip_addr_isany(&(cpcb->local_ip)) ||
263
   ip_addr_isany(ipaddr) ||
264
   ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
265
  return ERR_USE;
266
      }
267
    }
268
  }
269
#else /* SO_REUSE */
270
  /* Search through list of PCB's in LISTEN state.
271
 
272
  If there is a PCB bound to specified port and IP_ADDR_ANY another PCB can be bound to the interface IP
273
  or to the loopback address on the same port if SOF_REUSEADDR is set. Any combination of PCB's bound to
274
  the same local port, but to one address out of {IP_ADDR_ANY, 127.0.0.1, interface IP} at a time is valid.
275
  But no two PCB's bound to same local port and same local address is valid.
276
 
277
  If SOF_REUSEPORT is set several PCB's can be bound to same local port and same local address also. But then
278
  all PCB's must have the SOF_REUSEPORT option set.
279
 
280
  When the two options aren't set and specified port is already bound, ERR_USE is returned saying that
281
  address is already in use. */
282
  for(cpcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; cpcb != NULL; cpcb = cpcb->next) {
283
    if(cpcb->local_port == port) {
284
      if(ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
285
        if(pcb->so_options & SOF_REUSEPORT) {
286
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in listening PCB's: SO_REUSEPORT set and same address.\n"));
287
          reuse_port_all_set = (reuse_port_all_set && (cpcb->so_options & SOF_REUSEPORT));
288
        }
289
        else {
290
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in listening PCB's: SO_REUSEPORT not set and same address.\n"));
291
          return ERR_USE;
292
        }
293
      }
294
      else if((ip_addr_isany(ipaddr) && !ip_addr_isany(&(cpcb->local_ip))) ||
295
              (!ip_addr_isany(ipaddr) && ip_addr_isany(&(cpcb->local_ip)))) {
296
        if(!(pcb->so_options & SOF_REUSEADDR) && !(pcb->so_options & SOF_REUSEPORT)) {
297
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in listening PCB's SO_REUSEPORT or SO_REUSEADDR not set and not the same address.\n"));
298
          return ERR_USE;
299
        }
300
        else {
301
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in listening PCB's SO_REUSEPORT or SO_REUSEADDR set and not the same address.\n"));
302
        }
303
      }
304
    }
305
  }
306
 
307
  /* Search through list of PCB's in a state in which they can accept or send data. Same decription as for
308
     PCB's in state LISTEN applies to this PCB's regarding the options SOF_REUSEADDR and SOF_REUSEPORT. */
309
  for(cpcb = tcp_active_pcbs; cpcb != NULL; cpcb = cpcb->next) {
310
    if(cpcb->local_port == port) {
311
      if(ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
312
        if(pcb->so_options & SOF_REUSEPORT) {
313
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in active PCB's SO_REUSEPORT set and same address.\n"));
314
          reuse_port_all_set = (reuse_port_all_set && (cpcb->so_options & SOF_REUSEPORT));
315
        }
316
        else {
317
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in active PCB's SO_REUSEPORT not set and same address.\n"));
318
          return ERR_USE;
319
        }
320
      }
321
      else if((ip_addr_isany(ipaddr) && !ip_addr_isany(&(cpcb->local_ip))) ||
322
              (!ip_addr_isany(ipaddr) && ip_addr_isany(&(cpcb->local_ip)))) {
323
        if(!(pcb->so_options & SOF_REUSEADDR) && !(pcb->so_options & SOF_REUSEPORT)) {
324
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in active PCB's SO_REUSEPORT or SO_REUSEADDR not set and not the same address.\n"));
325
          return ERR_USE;
326
        }
327
        else {
328
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in active PCB's SO_REUSEPORT or SO_REUSEADDR set and not the same address.\n"));
329
        }
330
      }
331
    }
332
  }
333
 
334
  /* Search through list of PCB's in TIME_WAIT state. If SO_REUSEADDR is set a bound combination [IP, port}
335
     can be rebound. The same applies when SOF_REUSEPORT is set.
336
 
337
     If SOF_REUSEPORT is set several PCB's can be bound to same local port and same local address also. But then
338
     all PCB's must have the SOF_REUSEPORT option set.
339
 
340
     When the two options aren't set and specified port is already bound, ERR_USE is returned saying that
341
     address is already in use. */
342
  for(cpcb = tcp_tw_pcbs; cpcb != NULL; cpcb = cpcb->next) {
343
    if(cpcb->local_port == port) {
344
      if(ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
345
        if(!(pcb->so_options & SOF_REUSEADDR) && !(pcb->so_options & SOF_REUSEPORT)) {
346
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in TIME_WAIT PCB's SO_REUSEPORT or SO_REUSEADDR not set and same address.\n"));
347
          return ERR_USE;
348
        }
349
        else if(pcb->so_options & SOF_REUSEPORT) {
350
          LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: in TIME_WAIT PCB's SO_REUSEPORT set and same address.\n"));
351
          reuse_port_all_set = (reuse_port_all_set && (cpcb->so_options & SOF_REUSEPORT));
352
        }
353
      }
354
    }
355
  }
356
 
357
  /* If SOF_REUSEPORT isn't set in all PCB's bound to specified port and local address specified then
358
     {IP, port} can't be reused. */
359
  if(!reuse_port_all_set) {
360
    LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: not all sockets have SO_REUSEPORT set.\n"));
361
    return ERR_USE;
362
  }
363
#endif /* SO_REUSE */
364
 
365
  if (!ip_addr_isany(ipaddr)) {
366
    pcb->local_ip = *ipaddr;
367
  }
368
  pcb->local_port = port;
369
  LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %u\n", port));
370
  return ERR_OK;
371
}
372
#if LWIP_CALLBACK_API
373
static err_t
374
tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
375
{
376
  (void)arg;
377
  (void)pcb;
378
  (void)err;
379
 
380
  return ERR_ABRT;
381
}
382
#endif /* LWIP_CALLBACK_API */
383
 
384
/**
385
 * Set the state of the connection to be LISTEN, which means that it
386
 * is able to accept incoming connections. The protocol control block
387
 * is reallocated in order to consume less memory. Setting the
388
 * connection to LISTEN is an irreversible process.
389
 *
390
 */
391
struct tcp_pcb *
392
tcp_listen(struct tcp_pcb *pcb)
393
{
394
  struct tcp_pcb_listen *lpcb;
395
 
396
  /* already listening? */
397
  if (pcb->state == LISTEN) {
398
    return pcb;
399
  }
400
  lpcb = memp_malloc(MEMP_TCP_PCB_LISTEN);
401
  if (lpcb == NULL) {
402
    return NULL;
403
  }
404
  lpcb->callback_arg = pcb->callback_arg;
405
  lpcb->local_port = pcb->local_port;
406
  lpcb->state = LISTEN;
407
  lpcb->so_options = pcb->so_options;
408
  lpcb->so_options |= SOF_ACCEPTCONN;
409
  lpcb->ttl = pcb->ttl;
410
  lpcb->tos = pcb->tos;
411
  ip_addr_set(&lpcb->local_ip, &pcb->local_ip);
412
  memp_free(MEMP_TCP_PCB, pcb);
413
#if LWIP_CALLBACK_API
414
  lpcb->accept = tcp_accept_null;
415
#endif /* LWIP_CALLBACK_API */
416
  TCP_REG(&tcp_listen_pcbs.listen_pcbs, lpcb);
417
  return (struct tcp_pcb *)lpcb;
418
}
419
 
420
/**
421
 * This function should be called by the application when it has
422
 * processed the data. The purpose is to advertise a larger window
423
 * when the data has been processed.
424
 *
425
 */
426
void
427
tcp_recved(struct tcp_pcb *pcb, u16_t len)
428
{
429
  if ((u32_t)pcb->rcv_wnd + len > TCP_WND) {
430
    pcb->rcv_wnd = TCP_WND;
431
  } else {
432
    pcb->rcv_wnd += len;
433
  }
434
  if (!(pcb->flags & TF_ACK_DELAY) &&
435
     !(pcb->flags & TF_ACK_NOW)) {
436
    /*
437
     * We send an ACK here (if one is not already pending, hence
438
     * the above tests) as tcp_recved() implies that the application
439
     * has processed some data, and so we can open the receiver's
440
     * window to allow more to be transmitted.  This could result in
441
     * two ACKs being sent for each received packet in some limited cases
442
     * (where the application is only receiving data, and is slow to
443
     * process it) but it is necessary to guarantee that the sender can
444
     * continue to transmit.
445
     */
446
    tcp_ack(pcb);
447
  }
448
  else if (pcb->flags & TF_ACK_DELAY && pcb->rcv_wnd >= TCP_WND/2) {
449
    /* If we can send a window update such that there is a full
450
     * segment available in the window, do so now.  This is sort of
451
     * nagle-like in its goals, and tries to hit a compromise between
452
     * sending acks each time the window is updated, and only sending
453
     * window updates when a timer expires.  The "threshold" used
454
     * above (currently TCP_WND/2) can be tuned to be more or less
455
     * aggressive  */
456
    tcp_ack_now(pcb);
457
  }
458
 
459
  LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %u bytes, wnd %u (%u).\n",
460
         len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd));
461
}
462
 
463
/**
464
 * A nastly hack featuring 'goto' statements that allocates a
465
 * new TCP local port.
466
 */
467
static u16_t
468
tcp_new_port(void)
469
{
470
  struct tcp_pcb *pcb;
471
#ifndef TCP_LOCAL_PORT_RANGE_START
472
#define TCP_LOCAL_PORT_RANGE_START 4096
473
#define TCP_LOCAL_PORT_RANGE_END   0x7fff
474
#endif
475
  static u16_t port = TCP_LOCAL_PORT_RANGE_START;
476
 
477
 again:
478
  if (++port > TCP_LOCAL_PORT_RANGE_END) {
479
    port = TCP_LOCAL_PORT_RANGE_START;
480
  }
481
 
482
  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
483
    if (pcb->local_port == port) {
484
      goto again;
485
    }
486
  }
487
  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
488
    if (pcb->local_port == port) {
489
      goto again;
490
    }
491
  }
492
  for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
493
    if (pcb->local_port == port) {
494
      goto again;
495
    }
496
  }
497
  return port;
498
}
499
 
500
/**
501
 * Connects to another host. The function given as the "connected"
502
 * argument will be called when the connection has been established.
503
 *
504
 */
505
err_t
506
tcp_connect(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port,
507
      err_t (* connected)(void *arg, struct tcp_pcb *tpcb, err_t err))
508
{
509
  u32_t optdata;
510
  err_t ret;
511
  u32_t iss;
512
 
513
  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %u\n", port));
514
  if (ipaddr != NULL) {
515
    pcb->remote_ip = *ipaddr;
516
  } else {
517
    return ERR_VAL;
518
  }
519
  pcb->remote_port = port;
520
  if (pcb->local_port == 0) {
521
    pcb->local_port = tcp_new_port();
522
  }
523
  iss = tcp_next_iss();
524
  pcb->rcv_nxt = 0;
525
  pcb->snd_nxt = iss;
526
  pcb->lastack = iss - 1;
527
  pcb->snd_lbb = iss - 1;
528
  pcb->rcv_wnd = TCP_WND;
529
  pcb->snd_wnd = TCP_WND;
530
  pcb->mss = TCP_MSS;
531
  pcb->cwnd = 1;
532
  pcb->ssthresh = pcb->mss * 10;
533
  pcb->state = SYN_SENT;
534
#if LWIP_CALLBACK_API  
535
  pcb->connected = connected;
536
#endif /* LWIP_CALLBACK_API */  
537
  TCP_REG(&tcp_active_pcbs, pcb);
538
 
539
  /* Build an MSS option */
540
  optdata = htonl(((u32_t)2 << 24) |
541
      ((u32_t)4 << 16) |
542
      (((u32_t)pcb->mss / 256) << 8) |
543
      (pcb->mss & 255));
544
 
545
  ret = tcp_enqueue(pcb, NULL, 0, TCP_SYN, 0, (u8_t *)&optdata, 4);
546
  if (ret == ERR_OK) {
547
    tcp_output(pcb);
548
  }
549
  return ret;
550
}
551
 
552
/**
553
 * Called every 500 ms and implements the retransmission timer and the timer that
554
 * removes PCBs that have been in TIME-WAIT for enough time. It also increments
555
 * various timers such as the inactivity timer in each PCB.
556
 */
557
void
558
tcp_slowtmr(void)
559
{
560
  struct tcp_pcb *pcb, *pcb2, *prev;
561
  u32_t eff_wnd;
562
  u8_t pcb_remove;      /* flag if a PCB should be removed */
563
  err_t err;
564
 
565
  err = ERR_OK;
566
 
567
  ++tcp_ticks;
568
 
569
  /* Steps through all of the active PCBs. */
570
  prev = NULL;
571
  pcb = tcp_active_pcbs;
572
  if (pcb == NULL) {
573
    LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
574
  }
575
  while (pcb != NULL) {
576
    LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
577
    LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
578
    LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
579
    LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
580
 
581
    pcb_remove = 0;
582
 
583
    if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
584
      ++pcb_remove;
585
      LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
586
    }
587
    else if (pcb->nrtx == TCP_MAXRTX) {
588
      ++pcb_remove;
589
      LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
590
    } else {
591
      ++pcb->rtime;
592
      if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
593
 
594
        /* Time for a retransmission. */
595
        LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %u pcb->rto %u\n",
596
          pcb->rtime, pcb->rto));
597
 
598
        /* Double retransmission time-out unless we are trying to
599
         * connect to somebody (i.e., we are in SYN_SENT). */
600
        if (pcb->state != SYN_SENT) {
601
          pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx];
602
        }
603
        /* Reduce congestion window and ssthresh. */
604
        eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
605
        pcb->ssthresh = eff_wnd >> 1;
606
        if (pcb->ssthresh < pcb->mss) {
607
          pcb->ssthresh = pcb->mss * 2;
608
        }
609
        pcb->cwnd = pcb->mss;
610
        LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %u ssthresh %u\n",
611
                                pcb->cwnd, pcb->ssthresh));
612
 
613
        /* The following needs to be called AFTER cwnd is set to one mss - STJ */
614
        tcp_rexmit_rto(pcb);
615
     }
616
    }
617
    /* Check if this PCB has stayed too long in FIN-WAIT-2 */
618
    if (pcb->state == FIN_WAIT_2) {
619
      if ((u32_t)(tcp_ticks - pcb->tmr) >
620
        TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
621
        ++pcb_remove;
622
        LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
623
      }
624
    }
625
 
626
   /* Check if KEEPALIVE should be sent */
627
   if((pcb->so_options & SOF_KEEPALIVE) && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
628
      if((u32_t)(tcp_ticks - pcb->tmr) > (pcb->keepalive + TCP_MAXIDLE) / TCP_SLOW_INTERVAL)  {
629
         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %u.%u.%u.%u.\n",
630
                                 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
631
                                 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
632
 
633
         tcp_abort(pcb);
634
      }
635
      else if((u32_t)(tcp_ticks - pcb->tmr) > (pcb->keepalive + pcb->keep_cnt * TCP_KEEPINTVL) / TCP_SLOW_INTERVAL) {
636
         tcp_keepalive(pcb);
637
         pcb->keep_cnt++;
638
      }
639
   }
640
 
641
    /* If this PCB has queued out of sequence data, but has been
642
       inactive for too long, will drop the data (it will eventually
643
       be retransmitted). */
644
#if TCP_QUEUE_OOSEQ    
645
    if (pcb->ooseq != NULL &&
646
       (u32_t)tcp_ticks - pcb->tmr >=
647
       pcb->rto * (u32_t)TCP_OOSEQ_TIMEOUT) {
648
      tcp_segs_free(pcb->ooseq);
649
      pcb->ooseq = NULL;
650
      LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
651
    }
652
#endif /* TCP_QUEUE_OOSEQ */
653
 
654
    /* Check if this PCB has stayed too long in SYN-RCVD */
655
    if (pcb->state == SYN_RCVD) {
656
      if ((u32_t)(tcp_ticks - pcb->tmr) >
657
   TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
658
        ++pcb_remove;
659
        LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
660
      }
661
    }
662
 
663
 
664
    /* If the PCB should be removed, do it. */
665
    if (pcb_remove) {
666
      tcp_pcb_purge(pcb);
667
      /* Remove PCB from tcp_active_pcbs list. */
668
      if (prev != NULL) {
669
  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
670
        prev->next = pcb->next;
671
      } else {
672
        /* This PCB was the first. */
673
        LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
674
        tcp_active_pcbs = pcb->next;
675
      }
676
 
677
      TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT);
678
 
679
      pcb2 = pcb->next;
680
      memp_free(MEMP_TCP_PCB, pcb);
681
      pcb = pcb2;
682
    } else {
683
 
684
      /* We check if we should poll the connection. */
685
      ++pcb->polltmr;
686
      if (pcb->polltmr >= pcb->pollinterval) {
687
        pcb->polltmr = 0;
688
        LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
689
        TCP_EVENT_POLL(pcb, err);
690
        if (err == ERR_OK) {
691
          tcp_output(pcb);
692
        }
693
      }
694
 
695
      prev = pcb;
696
      pcb = pcb->next;
697
    }
698
  }
699
 
700
 
701
  /* Steps through all of the TIME-WAIT PCBs. */
702
  prev = NULL;
703
  pcb = tcp_tw_pcbs;
704
  while (pcb != NULL) {
705
    LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
706
    pcb_remove = 0;
707
 
708
    /* Check if this PCB has stayed long enough in TIME-WAIT */
709
    if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
710
      ++pcb_remove;
711
    }
712
 
713
 
714
 
715
    /* If the PCB should be removed, do it. */
716
    if (pcb_remove) {
717
      tcp_pcb_purge(pcb);
718
      /* Remove PCB from tcp_tw_pcbs list. */
719
      if (prev != NULL) {
720
  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
721
        prev->next = pcb->next;
722
      } else {
723
        /* This PCB was the first. */
724
        LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
725
        tcp_tw_pcbs = pcb->next;
726
      }
727
      pcb2 = pcb->next;
728
      memp_free(MEMP_TCP_PCB, pcb);
729
      pcb = pcb2;
730
    } else {
731
      prev = pcb;
732
      pcb = pcb->next;
733
    }
734
  }
735
}
736
 
737
/**
738
 * Is called every TCP_FAST_INTERVAL (250 ms) and sends delayed ACKs.
739
 */
740
void
741
tcp_fasttmr(void)
742
{
743
  struct tcp_pcb *pcb;
744
 
745
  /* send delayed ACKs */
746
  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
747
    if (pcb->flags & TF_ACK_DELAY) {
748
      LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
749
      tcp_ack_now(pcb);
750
      pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
751
    }
752
  }
753
}
754
 
755
/**
756
 * Deallocates a list of TCP segments (tcp_seg structures).
757
 *
758
 */
759
u8_t
760
tcp_segs_free(struct tcp_seg *seg)
761
{
762
  u8_t count = 0;
763
  struct tcp_seg *next;
764
  while (seg != NULL) {
765
    next = seg->next;
766
    count += tcp_seg_free(seg);
767
    seg = next;
768
  }
769
  return count;
770
}
771
 
772
/**
773
 * Frees a TCP segment.
774
 *
775
 */
776
u8_t
777
tcp_seg_free(struct tcp_seg *seg)
778
{
779
  u8_t count = 0;
780
 
781
  if (seg != NULL) {
782
    if (seg->p != NULL) {
783
      count = pbuf_free(seg->p);
784
#if TCP_DEBUG
785
      seg->p = NULL;
786
#endif /* TCP_DEBUG */
787
    }
788
    memp_free(MEMP_TCP_SEG, seg);
789
  }
790
  return count;
791
}
792
 
793
/**
794
 * Sets the priority of a connection.
795
 *
796
 */
797
void
798
tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
799
{
800
  pcb->prio = prio;
801
}
802
#if TCP_QUEUE_OOSEQ
803
 
804
/**
805
 * Returns a copy of the given TCP segment.
806
 *
807
 */
808
struct tcp_seg *
809
tcp_seg_copy(struct tcp_seg *seg)
810
{
811
  struct tcp_seg *cseg;
812
 
813
  cseg = memp_malloc(MEMP_TCP_SEG);
814
  if (cseg == NULL) {
815
    return NULL;
816
  }
817
  memcpy((char *)cseg, (const char *)seg, sizeof(struct tcp_seg));
818
  pbuf_ref(cseg->p);
819
  return cseg;
820
}
821
#endif
822
 
823
#if LWIP_CALLBACK_API
824
static err_t
825
tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
826
{
827
  arg = arg;
828
  if (p != NULL) {
829
    pbuf_free(p);
830
  } else if (err == ERR_OK) {
831
    return tcp_close(pcb);
832
  }
833
  return ERR_OK;
834
}
835
#endif /* LWIP_CALLBACK_API */
836
 
837
static void
838
tcp_kill_prio(u8_t prio)
839
{
840
  struct tcp_pcb *pcb, *inactive;
841
  u32_t inactivity;
842
  u8_t mprio;
843
 
844
 
845
  mprio = TCP_PRIO_MAX;
846
 
847
  /* We kill the oldest active connection that has lower priority than
848
     prio. */
849
  inactivity = 0;
850
  inactive = NULL;
851
  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
852
    if (pcb->prio <= prio &&
853
       pcb->prio <= mprio &&
854
       (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
855
      inactivity = tcp_ticks - pcb->tmr;
856
      inactive = pcb;
857
      mprio = pcb->prio;
858
    }
859
  }
860
  if (inactive != NULL) {
861
    LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%ld)\n",
862
           (void *)inactive, inactivity));
863
    tcp_abort(inactive);
864
  }
865
}
866
 
867
 
868
static void
869
tcp_kill_timewait(void)
870
{
871
  struct tcp_pcb *pcb, *inactive;
872
  u32_t inactivity;
873
 
874
  inactivity = 0;
875
  inactive = NULL;
876
  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
877
    if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
878
      inactivity = tcp_ticks - pcb->tmr;
879
      inactive = pcb;
880
    }
881
  }
882
  if (inactive != NULL) {
883
    LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%ld)\n",
884
           (void *)inactive, inactivity));
885
    tcp_abort(inactive);
886
  }
887
}
888
 
889
 
890
 
891
struct tcp_pcb *
892
tcp_alloc(u8_t prio)
893
{
894
  struct tcp_pcb *pcb;
895
  u32_t iss;
896
 
897
  pcb = memp_malloc(MEMP_TCP_PCB);
898
  if (pcb == NULL) {
899
    /* Try killing oldest connection in TIME-WAIT. */
900
    LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
901
    tcp_kill_timewait();
902
    pcb = memp_malloc(MEMP_TCP_PCB);
903
    if (pcb == NULL) {
904
      tcp_kill_prio(prio);
905
      pcb = memp_malloc(MEMP_TCP_PCB);
906
    }
907
  }
908
  if (pcb != NULL) {
909
    memset(pcb, 0, sizeof(struct tcp_pcb));
910
    pcb->prio = TCP_PRIO_NORMAL;
911
    pcb->snd_buf = TCP_SND_BUF;
912
    pcb->snd_queuelen = 0;
913
    pcb->rcv_wnd = TCP_WND;
914
    pcb->tos = 0;
915
    pcb->ttl = TCP_TTL;
916
    pcb->mss = TCP_MSS;
917
    pcb->rto = 3000 / TCP_SLOW_INTERVAL;
918
    pcb->sa = 0;
919
    pcb->sv = 3000 / TCP_SLOW_INTERVAL;
920
    pcb->rtime = 0;
921
    pcb->cwnd = 1;
922
    iss = tcp_next_iss();
923
    pcb->snd_wl2 = iss;
924
    pcb->snd_nxt = iss;
925
    pcb->snd_max = iss;
926
    pcb->lastack = iss;
927
    pcb->snd_lbb = iss;
928
    pcb->tmr = tcp_ticks;
929
 
930
    pcb->polltmr = 0;
931
 
932
#if LWIP_CALLBACK_API
933
    pcb->recv = tcp_recv_null;
934
#endif /* LWIP_CALLBACK_API */  
935
 
936
    /* Init KEEPALIVE timer */
937
    pcb->keepalive = TCP_KEEPDEFAULT;
938
    pcb->keep_cnt = 0;
939
  }
940
  return pcb;
941
}
942
 
943
/**
944
 * Creates a new TCP protocol control block but doesn't place it on
945
 * any of the TCP PCB lists.
946
 *
947
 * @internal: Maybe there should be a idle TCP PCB list where these
948
 * PCBs are put on. We can then implement port reservation using
949
 * tcp_bind(). Currently, we lack this (BSD socket type of) feature.
950
 */
951
 
952
struct tcp_pcb *
953
tcp_new(void)
954
{
955
  return tcp_alloc(TCP_PRIO_NORMAL);
956
}
957
 
958
/*
959
 * tcp_arg():
960
 *
961
 * Used to specify the argument that should be passed callback
962
 * functions.
963
 *
964
 */
965
 
966
void
967
tcp_arg(struct tcp_pcb *pcb, void *arg)
968
{
969
  pcb->callback_arg = arg;
970
}
971
#if LWIP_CALLBACK_API
972
 
973
/**
974
 * Used to specify the function that should be called when a TCP
975
 * connection receives data.
976
 *
977
 */
978
void
979
tcp_recv(struct tcp_pcb *pcb,
980
   err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err))
981
{
982
  pcb->recv = recv;
983
}
984
 
985
/**
986
 * Used to specify the function that should be called when TCP data
987
 * has been successfully delivered to the remote host.
988
 *
989
 */
990
 
991
void
992
tcp_sent(struct tcp_pcb *pcb,
993
   err_t (* sent)(void *arg, struct tcp_pcb *tpcb, u16_t len))
994
{
995
  pcb->sent = sent;
996
}
997
 
998
/**
999
 * Used to specify the function that should be called when a fatal error
1000
 * has occured on the connection.
1001
 *
1002
 */
1003
void
1004
tcp_err(struct tcp_pcb *pcb,
1005
   void (* errf)(void *arg, err_t err))
1006
{
1007
  pcb->errf = errf;
1008
}
1009
 
1010
/**
1011
 * Used for specifying the function that should be called when a
1012
 * LISTENing connection has been connected to another host.
1013
 *
1014
 */
1015
void
1016
tcp_accept(struct tcp_pcb *pcb,
1017
     err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err))
1018
{
1019
  ((struct tcp_pcb_listen *)pcb)->accept = accept;
1020
}
1021
#endif /* LWIP_CALLBACK_API */
1022
 
1023
 
1024
/**
1025
 * Used to specify the function that should be called periodically
1026
 * from TCP. The interval is specified in terms of the TCP coarse
1027
 * timer interval, which is called twice a second.
1028
 *
1029
 */
1030
void
1031
tcp_poll(struct tcp_pcb *pcb,
1032
   err_t (* poll)(void *arg, struct tcp_pcb *tpcb), u8_t interval)
1033
{
1034
#if LWIP_CALLBACK_API
1035
  pcb->poll = poll;
1036
#endif /* LWIP_CALLBACK_API */  
1037
  pcb->pollinterval = interval;
1038
}
1039
 
1040
/**
1041
 * Purges a TCP PCB. Removes any buffered data and frees the buffer memory.
1042
 *
1043
 */
1044
void
1045
tcp_pcb_purge(struct tcp_pcb *pcb)
1046
{
1047
  if (pcb->state != CLOSED &&
1048
     pcb->state != TIME_WAIT &&
1049
     pcb->state != LISTEN) {
1050
 
1051
    LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1052
 
1053
    if (pcb->unsent != NULL) {
1054
      LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1055
    }
1056
    if (pcb->unacked != NULL) {
1057
      LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1058
    }
1059
#if TCP_QUEUE_OOSEQ /* LW */
1060
    if (pcb->ooseq != NULL) {
1061
      LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1062
    }
1063
 
1064
    tcp_segs_free(pcb->ooseq);
1065
    pcb->ooseq = NULL;
1066
#endif /* TCP_QUEUE_OOSEQ */
1067
    tcp_segs_free(pcb->unsent);
1068
    tcp_segs_free(pcb->unacked);
1069
    pcb->unacked = pcb->unsent = NULL;
1070
  }
1071
}
1072
 
1073
/**
1074
 * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
1075
 *
1076
 */
1077
void
1078
tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1079
{
1080
  TCP_RMV(pcblist, pcb);
1081
 
1082
  tcp_pcb_purge(pcb);
1083
 
1084
  /* if there is an outstanding delayed ACKs, send it */
1085
  if (pcb->state != TIME_WAIT &&
1086
     pcb->state != LISTEN &&
1087
     pcb->flags & TF_ACK_DELAY) {
1088
    pcb->flags |= TF_ACK_NOW;
1089
    tcp_output(pcb);
1090
  }
1091
  pcb->state = CLOSED;
1092
 
1093
  LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1094
}
1095
 
1096
/**
1097
 * Calculates a new initial sequence number for new connections.
1098
 *
1099
 */
1100
u32_t
1101
tcp_next_iss(void)
1102
{
1103
  static u32_t iss = 6510;
1104
 
1105
  iss += tcp_ticks;       /* XXX */
1106
  return iss;
1107
}
1108
 
1109
#if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
1110
void
1111
tcp_debug_print(struct tcp_hdr *tcphdr)
1112
{
1113
  LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
1114
  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1115
  LWIP_DEBUGF(TCP_DEBUG, ("|    %5u      |    %5u      | (src port, dest port)\n",
1116
         ntohs(tcphdr->src), ntohs(tcphdr->dest)));
1117
  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1118
  LWIP_DEBUGF(TCP_DEBUG, ("|           %010lu          | (seq no)\n",
1119
          ntohl(tcphdr->seqno)));
1120
  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1121
  LWIP_DEBUGF(TCP_DEBUG, ("|           %010lu          | (ack no)\n",
1122
         ntohl(tcphdr->ackno)));
1123
  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1124
  LWIP_DEBUGF(TCP_DEBUG, ("| %2u |   |%u%u%u%u%u%u|     %5u     | (hdrlen, flags (",
1125
       TCPH_HDRLEN(tcphdr),
1126
         TCPH_FLAGS(tcphdr) >> 5 & 1,
1127
         TCPH_FLAGS(tcphdr) >> 4 & 1,
1128
         TCPH_FLAGS(tcphdr) >> 3 & 1,
1129
         TCPH_FLAGS(tcphdr) >> 2 & 1,
1130
         TCPH_FLAGS(tcphdr) >> 1 & 1,
1131
         TCPH_FLAGS(tcphdr) & 1,
1132
         ntohs(tcphdr->wnd)));
1133
  tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
1134
  LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
1135
  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1136
  LWIP_DEBUGF(TCP_DEBUG, ("|    0x%04x     |     %5u     | (chksum, urgp)\n",
1137
         ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
1138
  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1139
}
1140
 
1141
void
1142
tcp_debug_print_state(enum tcp_state s)
1143
{
1144
  LWIP_DEBUGF(TCP_DEBUG, ("State: "));
1145
  switch (s) {
1146
  case CLOSED:
1147
    LWIP_DEBUGF(TCP_DEBUG, ("CLOSED\n"));
1148
    break;
1149
 case LISTEN:
1150
   LWIP_DEBUGF(TCP_DEBUG, ("LISTEN\n"));
1151
   break;
1152
  case SYN_SENT:
1153
    LWIP_DEBUGF(TCP_DEBUG, ("SYN_SENT\n"));
1154
    break;
1155
  case SYN_RCVD:
1156
    LWIP_DEBUGF(TCP_DEBUG, ("SYN_RCVD\n"));
1157
    break;
1158
  case ESTABLISHED:
1159
    LWIP_DEBUGF(TCP_DEBUG, ("ESTABLISHED\n"));
1160
    break;
1161
  case FIN_WAIT_1:
1162
    LWIP_DEBUGF(TCP_DEBUG, ("FIN_WAIT_1\n"));
1163
    break;
1164
  case FIN_WAIT_2:
1165
    LWIP_DEBUGF(TCP_DEBUG, ("FIN_WAIT_2\n"));
1166
    break;
1167
  case CLOSE_WAIT:
1168
    LWIP_DEBUGF(TCP_DEBUG, ("CLOSE_WAIT\n"));
1169
    break;
1170
  case CLOSING:
1171
    LWIP_DEBUGF(TCP_DEBUG, ("CLOSING\n"));
1172
    break;
1173
  case LAST_ACK:
1174
    LWIP_DEBUGF(TCP_DEBUG, ("LAST_ACK\n"));
1175
    break;
1176
  case TIME_WAIT:
1177
    LWIP_DEBUGF(TCP_DEBUG, ("TIME_WAIT\n"));
1178
   break;
1179
  }
1180
}
1181
 
1182
void
1183
tcp_debug_print_flags(u8_t flags)
1184
{
1185
  if (flags & TCP_FIN) {
1186
    LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
1187
  }
1188
  if (flags & TCP_SYN) {
1189
    LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
1190
  }
1191
  if (flags & TCP_RST) {
1192
    LWIP_DEBUGF(TCP_DEBUG, ("RST "));
1193
  }
1194
  if (flags & TCP_PSH) {
1195
    LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
1196
  }
1197
  if (flags & TCP_ACK) {
1198
    LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
1199
  }
1200
  if (flags & TCP_URG) {
1201
    LWIP_DEBUGF(TCP_DEBUG, ("URG "));
1202
  }
1203
  if (flags & TCP_ECE) {
1204
    LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
1205
  }
1206
  if (flags & TCP_CWR) {
1207
    LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
1208
  }
1209
}
1210
 
1211
void
1212
tcp_debug_print_pcbs(void)
1213
{
1214
  struct tcp_pcb *pcb;
1215
  LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
1216
  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1217
    LWIP_DEBUGF(TCP_DEBUG, ("Local port %u, foreign port %u snd_nxt %lu rcv_nxt %lu ",
1218
                       pcb->local_port, pcb->remote_port,
1219
                       pcb->snd_nxt, pcb->rcv_nxt));
1220
    tcp_debug_print_state(pcb->state);
1221
  }
1222
  LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
1223
  for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
1224
    LWIP_DEBUGF(TCP_DEBUG, ("Local port %u, foreign port %u snd_nxt %lu rcv_nxt %lu ",
1225
                       pcb->local_port, pcb->remote_port,
1226
                       pcb->snd_nxt, pcb->rcv_nxt));
1227
    tcp_debug_print_state(pcb->state);
1228
  }
1229
  LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
1230
  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1231
    LWIP_DEBUGF(TCP_DEBUG, ("Local port %u, foreign port %u snd_nxt %lu rcv_nxt %lu ",
1232
                       pcb->local_port, pcb->remote_port,
1233
                       pcb->snd_nxt, pcb->rcv_nxt));
1234
    tcp_debug_print_state(pcb->state);
1235
  }
1236
}
1237
 
1238
int
1239
tcp_pcbs_sane(void)
1240
{
1241
  struct tcp_pcb *pcb;
1242
  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1243
    LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
1244
    LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
1245
    LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
1246
  }
1247
  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1248
    LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1249
  }
1250
  return 1;
1251
}
1252
#endif /* TCP_DEBUG */
1253
#endif /* LWIP_TCP */
1254
 
1255
 
1256
 
1257
 
1258
 
1259
 
1260
 
1261
 
1262
 
1263
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.