]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/tcp.c
ee19f389bf943cdf02c23a60cee7a6574c5d0ef4
[pes-rpp/rpp-lwip.git] / src / core / tcp.c
1 /**
2  * @file
3  * Transmission Control Protocol for IP
4  *
5  * This file contains common functions for the TCP implementation, such as functinos
6  * for manipulating the data structures and the TCP timer functions. TCP functions
7  * related to input and output is found in tcp_in.c and tcp_out.c respectively.
8  *
9  */
10
11 /*
12  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
13  * All rights reserved. 
14  * 
15  * Redistribution and use in source and binary forms, with or without modification, 
16  * are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  *    this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright notice,
21  *    this list of conditions and the following disclaimer in the documentation
22  *    and/or other materials provided with the distribution.
23  * 3. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission. 
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
29  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
31  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
35  * OF SUCH DAMAGE.
36  *
37  * This file is part of the lwIP TCP/IP stack.
38  * 
39  * Author: Adam Dunkels <adam@sics.se>
40  *
41  */
42
43 #include "lwip/opt.h"
44
45 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
46
47 #include "lwip/def.h"
48 #include "lwip/mem.h"
49 #include "lwip/memp.h"
50 #include "lwip/snmp.h"
51 #include "lwip/tcp.h"
52 #include "lwip/debug.h"
53 #include "lwip/stats.h"
54
55 #include <string.h>
56
57 const char *tcp_state_str[] = {
58   "CLOSED",      
59   "LISTEN",      
60   "SYN_SENT",    
61   "SYN_RCVD",    
62   "ESTABLISHED", 
63   "FIN_WAIT_1",  
64   "FIN_WAIT_2",  
65   "CLOSE_WAIT",  
66   "CLOSING",     
67   "LAST_ACK",    
68   "TIME_WAIT"   
69 };
70
71 /* Incremented every coarse grained timer shot (typically every 500 ms). */
72 u32_t tcp_ticks;
73 const u8_t tcp_backoff[13] =
74     { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
75  /* Times per slowtmr hits */
76 const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
77
78 /* The TCP PCB lists. */
79
80 /** List of all TCP PCBs bound but not yet (connected || listening) */
81 struct tcp_pcb *tcp_bound_pcbs;
82 /** List of all TCP PCBs in LISTEN state */
83 union tcp_listen_pcbs_t tcp_listen_pcbs;
84 /** List of all TCP PCBs that are in a state in which
85  * they accept or send data. */
86 struct tcp_pcb *tcp_active_pcbs;
87 /** List of all TCP PCBs in TIME-WAIT state */
88 struct tcp_pcb *tcp_tw_pcbs;
89
90 /** Only used for temporary storage. */
91 struct tcp_pcb *tcp_tmp_pcb;
92
93 /** Timer counter to handle calling slow-timer from tcp_tmr() */ 
94 static u8_t tcp_timer;
95 static u16_t tcp_new_port(void);
96
97 /**
98  * Called periodically to dispatch TCP timers.
99  *
100  */
101 void
102 tcp_tmr(void)
103 {
104   /* Call tcp_fasttmr() every 250 ms */
105   tcp_fasttmr();
106
107   if (++tcp_timer & 1) {
108     /* Call tcp_tmr() every 500 ms, i.e., every other timer
109        tcp_tmr() is called. */
110     tcp_slowtmr();
111   }
112 }
113
114 /**
115  * Closes the connection held by the PCB.
116  *
117  * Listening pcbs are freed and may not be referenced any more.
118  * Connection pcbs are freed if not yet connected and may not be referenced
119  * any more. If a connection is established (at least SYN received or in
120  * a closing state), the connection is closed, and put in a closing state.
121  * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
122  * unsafe to reference it.
123  *
124  * @param pcb the tcp_pcb to close
125  * @return ERR_OK if connection has been closed
126  *         another err_t if closing failed and pcb is not freed
127  */
128 err_t
129 tcp_close(struct tcp_pcb *pcb)
130 {
131   err_t err;
132
133 #if TCP_DEBUG
134   LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
135   tcp_debug_print_state(pcb->state);
136 #endif /* TCP_DEBUG */
137
138   switch (pcb->state) {
139   case CLOSED:
140     /* Closing a pcb in the CLOSED state might seem erroneous,
141      * however, it is in this state once allocated and as yet unused
142      * and the user needs some way to free it should the need arise.
143      * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
144      * or for a pcb that has been used and then entered the CLOSED state 
145      * is erroneous, but this should never happen as the pcb has in those cases
146      * been freed, and so any remaining handles are bogus. */
147     err = ERR_OK;
148     TCP_RMV(&tcp_bound_pcbs, pcb);
149     memp_free(MEMP_TCP_PCB, pcb);
150     pcb = NULL;
151     break;
152   case LISTEN:
153     err = ERR_OK;
154     tcp_pcb_remove((struct tcp_pcb **)&tcp_listen_pcbs.pcbs, pcb);
155     memp_free(MEMP_TCP_PCB_LISTEN, pcb);
156     pcb = NULL;
157     break;
158   case SYN_SENT:
159     err = ERR_OK;
160     tcp_pcb_remove(&tcp_active_pcbs, pcb);
161     memp_free(MEMP_TCP_PCB, pcb);
162     pcb = NULL;
163     snmp_inc_tcpattemptfails();
164     break;
165   case SYN_RCVD:
166     err = tcp_send_ctrl(pcb, TCP_FIN);
167     if (err == ERR_OK) {
168       snmp_inc_tcpattemptfails();
169       pcb->state = FIN_WAIT_1;
170     }
171     break;
172   case ESTABLISHED:
173     err = tcp_send_ctrl(pcb, TCP_FIN);
174     if (err == ERR_OK) {
175       snmp_inc_tcpestabresets();
176       pcb->state = FIN_WAIT_1;
177     }
178     break;
179   case CLOSE_WAIT:
180     err = tcp_send_ctrl(pcb, TCP_FIN);
181     if (err == ERR_OK) {
182       snmp_inc_tcpestabresets();
183       pcb->state = LAST_ACK;
184     }
185     break;
186   default:
187     /* Has already been closed, do nothing. */
188     err = ERR_OK;
189     pcb = NULL;
190     break;
191   }
192
193   if (pcb != NULL && err == ERR_OK) {
194     /* To ensure all data has been sent when tcp_close returns, we have
195        to make sure tcp_output doesn't fail.
196        Since we don't really have to ensure all data has been sent when tcp_close
197        returns (unsent data is sent from tcp timer functions, also), we don't care
198        for the return value of tcp_output for now. */
199     /* @todo: When implementing SO_LINGER, this must be changed somehow:
200        If SOF_LINGER is set, the data should be sent when tcp_close returns. */
201     tcp_output(pcb);
202   }
203   return err;
204 }
205
206 /**
207  * Abandons a connection and optionally sends a RST to the remote
208  * host.  Deletes the local protocol control block. This is done when
209  * a connection is killed because of shortage of memory.
210  *
211  * @param pcb the tcp_pcb to abort
212  * @param reset boolean to indicate whether a reset should be sent
213  */
214 void
215 tcp_abandon(struct tcp_pcb *pcb, int reset)
216 {
217   u32_t seqno, ackno;
218   u16_t remote_port, local_port;
219   struct ip_addr remote_ip, local_ip;
220 #if LWIP_CALLBACK_API  
221   tcp_err_fn errf;
222 #endif /* LWIP_CALLBACK_API */
223   void *errf_arg;
224
225   
226   /* Figure out on which TCP PCB list we are, and remove us. If we
227      are in an active state, call the receive function associated with
228      the PCB with a NULL argument, and send an RST to the remote end. */
229   if (pcb->state == TIME_WAIT) {
230     tcp_pcb_remove(&tcp_tw_pcbs, pcb);
231     memp_free(MEMP_TCP_PCB, pcb);
232   } else {
233     seqno = pcb->snd_nxt;
234     ackno = pcb->rcv_nxt;
235     ip_addr_set(&local_ip, &(pcb->local_ip));
236     ip_addr_set(&remote_ip, &(pcb->remote_ip));
237     local_port = pcb->local_port;
238     remote_port = pcb->remote_port;
239 #if LWIP_CALLBACK_API
240     errf = pcb->errf;
241 #endif /* LWIP_CALLBACK_API */
242     errf_arg = pcb->callback_arg;
243     tcp_pcb_remove(&tcp_active_pcbs, pcb);
244     if (pcb->unacked != NULL) {
245       tcp_segs_free(pcb->unacked);
246     }
247     if (pcb->unsent != NULL) {
248       tcp_segs_free(pcb->unsent);
249     }
250 #if TCP_QUEUE_OOSEQ    
251     if (pcb->ooseq != NULL) {
252       tcp_segs_free(pcb->ooseq);
253     }
254 #endif /* TCP_QUEUE_OOSEQ */
255     memp_free(MEMP_TCP_PCB, pcb);
256     TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
257     if (reset) {
258       LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
259       tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port);
260     }
261   }
262 }
263
264 /**
265  * Binds the connection to a local portnumber and IP address. If the
266  * IP address is not given (i.e., ipaddr == NULL), the IP address of
267  * the outgoing network interface is used instead.
268  *
269  * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
270  *        already bound!)
271  * @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind
272  *        to any local address
273  * @param port the local port to bind to
274  * @return ERR_USE if the port is already in use
275  *         ERR_OK if bound
276  */
277 err_t
278 tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
279 {
280   struct tcp_pcb *cpcb;
281
282   LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
283
284   if (port == 0) {
285     port = tcp_new_port();
286   }
287   /* Check if the address already is in use. */
288   /* Check the listen pcbs. */
289   for(cpcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs;
290       cpcb != NULL; cpcb = cpcb->next) {
291     if (cpcb->local_port == port) {
292       if (ip_addr_isany(&(cpcb->local_ip)) ||
293           ip_addr_isany(ipaddr) ||
294           ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
295         return ERR_USE;
296       }
297     }
298   }
299   /* Check the connected pcbs. */
300   for(cpcb = tcp_active_pcbs;
301       cpcb != NULL; cpcb = cpcb->next) {
302     if (cpcb->local_port == port) {
303       if (ip_addr_isany(&(cpcb->local_ip)) ||
304           ip_addr_isany(ipaddr) ||
305           ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
306         return ERR_USE;
307       }
308     }
309   }
310   /* Check the bound, not yet connected pcbs. */
311   for(cpcb = tcp_bound_pcbs; cpcb != NULL; cpcb = cpcb->next) {
312     if (cpcb->local_port == port) {
313       if (ip_addr_isany(&(cpcb->local_ip)) ||
314           ip_addr_isany(ipaddr) ||
315           ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
316         return ERR_USE;
317       }
318     }
319   }
320   /* @todo: until SO_REUSEADDR is implemented (see task #6995 on savannah),
321    * we have to check the pcbs in TIME-WAIT state, also: */
322   for(cpcb = tcp_tw_pcbs; cpcb != NULL; cpcb = cpcb->next) {
323     if (cpcb->local_port == port) {
324       if (ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
325         return ERR_USE;
326       }
327     }
328   }
329
330   if (!ip_addr_isany(ipaddr)) {
331     pcb->local_ip = *ipaddr;
332   }
333   pcb->local_port = port;
334   TCP_REG(&tcp_bound_pcbs, pcb);
335   LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
336   return ERR_OK;
337 }
338 #if LWIP_CALLBACK_API
339 /**
340  * Default accept callback if no accept callback is specified by the user.
341  */
342 static err_t
343 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
344 {
345   LWIP_UNUSED_ARG(arg);
346   LWIP_UNUSED_ARG(pcb);
347   LWIP_UNUSED_ARG(err);
348
349   return ERR_ABRT;
350 }
351 #endif /* LWIP_CALLBACK_API */
352
353 /**
354  * Set the state of the connection to be LISTEN, which means that it
355  * is able to accept incoming connections. The protocol control block
356  * is reallocated in order to consume less memory. Setting the
357  * connection to LISTEN is an irreversible process.
358  *
359  * @param pcb the original tcp_pcb
360  * @param backlog the incoming connections queue limit
361  * @return tcp_pcb used for listening, consumes less memory.
362  *
363  * @note The original tcp_pcb is freed. This function therefore has to be
364  *       called like this:
365  *             tpcb = tcp_listen(tpcb);
366  */
367 struct tcp_pcb *
368 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
369 {
370   struct tcp_pcb_listen *lpcb;
371
372   LWIP_UNUSED_ARG(backlog);
373   LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL);
374
375   /* already listening? */
376   if (pcb->state == LISTEN) {
377     return pcb;
378   }
379   lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
380   if (lpcb == NULL) {
381     return NULL;
382   }
383   lpcb->callback_arg = pcb->callback_arg;
384   lpcb->local_port = pcb->local_port;
385   lpcb->state = LISTEN;
386   lpcb->so_options = pcb->so_options;
387   lpcb->so_options |= SOF_ACCEPTCONN;
388   lpcb->ttl = pcb->ttl;
389   lpcb->tos = pcb->tos;
390   ip_addr_set(&lpcb->local_ip, &pcb->local_ip);
391   TCP_RMV(&tcp_bound_pcbs, pcb);
392   memp_free(MEMP_TCP_PCB, pcb);
393 #if LWIP_CALLBACK_API
394   lpcb->accept = tcp_accept_null;
395 #endif /* LWIP_CALLBACK_API */
396 #if TCP_LISTEN_BACKLOG
397   lpcb->accepts_pending = 0;
398   lpcb->backlog = (backlog ? backlog : 1);
399 #endif /* TCP_LISTEN_BACKLOG */
400   TCP_REG(&tcp_listen_pcbs.listen_pcbs, lpcb);
401   return (struct tcp_pcb *)lpcb;
402 }
403
404 /** 
405  * Update the state that tracks the available window space to advertise.
406  *
407  * Returns how much extra window would be advertised if we sent an
408  * update now.
409  */
410 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
411 {
412   u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
413
414   if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
415     /* we can advertise more window */
416     pcb->rcv_ann_wnd = pcb->rcv_wnd;
417     return new_right_edge - pcb->rcv_ann_right_edge;
418   } else {
419     if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
420       /* Can happen due to other end sending out of advertised window,
421        * but within actual available (but not yet advertised) window */
422       pcb->rcv_ann_wnd = 0;
423     } else {
424       /* keep the right edge of window constant */
425       u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
426       LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
427       pcb->rcv_ann_wnd = (u16_t)new_rcv_ann_wnd;
428     }
429     return 0;
430   }
431 }
432
433 /**
434  * This function should be called by the application when it has
435  * processed the data. The purpose is to advertise a larger window
436  * when the data has been processed.
437  *
438  * @param pcb the tcp_pcb for which data is read
439  * @param len the amount of bytes that have been read by the application
440  */
441 void
442 tcp_recved(struct tcp_pcb *pcb, u16_t len)
443 {
444   int wnd_inflation;
445
446   LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n",
447               len <= 0xffff - pcb->rcv_wnd );
448
449   pcb->rcv_wnd += len;
450   if (pcb->rcv_wnd > TCP_WND)
451     pcb->rcv_wnd = TCP_WND;
452
453   wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
454
455   /* If the change in the right edge of window is significant (default
456    * watermark is TCP_WND/2), then send an explicit update now.
457    * Otherwise wait for a packet to be sent in the normal course of
458    * events (or more window to be available later) */
459   if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
460     tcp_ack_now(pcb);
461     tcp_output(pcb);
462   }
463
464   LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n",
465          len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd));
466 }
467
468 /**
469  * A nastly hack featuring 'goto' statements that allocates a
470  * new TCP local port.
471  *
472  * @return a new (free) local TCP port number
473  */
474 static u16_t
475 tcp_new_port(void)
476 {
477   struct tcp_pcb *pcb;
478 #ifndef TCP_LOCAL_PORT_RANGE_START
479 #define TCP_LOCAL_PORT_RANGE_START 4096
480 #define TCP_LOCAL_PORT_RANGE_END   0x7fff
481 #endif
482   static u16_t port = TCP_LOCAL_PORT_RANGE_START;
483   
484  again:
485   if (++port > TCP_LOCAL_PORT_RANGE_END) {
486     port = TCP_LOCAL_PORT_RANGE_START;
487   }
488   
489   for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
490     if (pcb->local_port == port) {
491       goto again;
492     }
493   }
494   for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
495     if (pcb->local_port == port) {
496       goto again;
497     }
498   }
499   for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
500     if (pcb->local_port == port) {
501       goto again;
502     }
503   }
504   return port;
505 }
506
507 /**
508  * Connects to another host. The function given as the "connected"
509  * argument will be called when the connection has been established.
510  *
511  * @param pcb the tcp_pcb used to establish the connection
512  * @param ipaddr the remote ip address to connect to
513  * @param port the remote tcp port to connect to
514  * @param connected callback function to call when connected (or on error)
515  * @return ERR_VAL if invalid arguments are given
516  *         ERR_OK if connect request has been sent
517  *         other err_t values if connect request couldn't be sent
518  */
519 err_t
520 tcp_connect(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port,
521       tcp_connected_fn connected)
522 {
523   err_t ret;
524   u32_t iss;
525
526   LWIP_ERROR("tcp_connect: can only connected from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
527
528   LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
529   if (ipaddr != NULL) {
530     pcb->remote_ip = *ipaddr;
531   } else {
532     return ERR_VAL;
533   }
534   pcb->remote_port = port;
535   if (pcb->local_port == 0) {
536     pcb->local_port = tcp_new_port();
537   }
538   iss = tcp_next_iss();
539   pcb->rcv_nxt = 0;
540   pcb->snd_nxt = iss;
541   pcb->lastack = iss - 1;
542   pcb->snd_lbb = iss - 1;
543   pcb->rcv_wnd = TCP_WND;
544   pcb->rcv_ann_wnd = TCP_WND;
545   pcb->rcv_ann_right_edge = pcb->rcv_nxt;
546   pcb->snd_wnd = TCP_WND;
547   /* As initial send MSS, we use TCP_MSS but limit it to 536.
548      The send MSS is updated when an MSS option is received. */
549   pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
550 #if TCP_CALCULATE_EFF_SEND_MSS
551   pcb->mss = tcp_eff_send_mss(pcb->mss, ipaddr);
552 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
553   pcb->cwnd = 1;
554   pcb->ssthresh = pcb->mss * 10;
555 #if LWIP_CALLBACK_API
556   pcb->connected = connected;
557 #endif /* LWIP_CALLBACK_API */
558
559   ret = tcp_enqueue(pcb, NULL, 0, TCP_SYN, 0, TF_SEG_OPTS_MSS
560 #if LWIP_TCP_TIMESTAMPS
561                     | TF_SEG_OPTS_TS
562 #endif
563                     );
564   if (ret == ERR_OK) {
565     /* SYN segment was enqueued, changed the pcbs state now */
566     pcb->state = SYN_SENT;
567     TCP_RMV(&tcp_bound_pcbs, pcb);
568     TCP_REG(&tcp_active_pcbs, pcb);
569     snmp_inc_tcpactiveopens();
570
571     tcp_output(pcb);
572   }
573   return ret;
574 }
575
576 /**
577  * Called every 500 ms and implements the retransmission timer and the timer that
578  * removes PCBs that have been in TIME-WAIT for enough time. It also increments
579  * various timers such as the inactivity timer in each PCB.
580  *
581  * Automatically called from tcp_tmr().
582  */
583 void
584 tcp_slowtmr(void)
585 {
586   struct tcp_pcb *pcb, *pcb2, *prev;
587   u16_t eff_wnd;
588   u8_t pcb_remove;      /* flag if a PCB should be removed */
589   u8_t pcb_reset;       /* flag if a RST should be sent when removing */
590   err_t err;
591
592   err = ERR_OK;
593
594   ++tcp_ticks;
595
596   /* Steps through all of the active PCBs. */
597   prev = NULL;
598   pcb = tcp_active_pcbs;
599   if (pcb == NULL) {
600     LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
601   }
602   while (pcb != NULL) {
603     LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
604     LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
605     LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
606     LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
607
608     pcb_remove = 0;
609     pcb_reset = 0;
610
611     if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
612       ++pcb_remove;
613       LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
614     }
615     else if (pcb->nrtx == TCP_MAXRTX) {
616       ++pcb_remove;
617       LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
618     } else {
619       if (pcb->persist_backoff > 0) {
620         /* If snd_wnd is zero, use persist timer to send 1 byte probes
621          * instead of using the standard retransmission mechanism. */
622         pcb->persist_cnt++;
623         if (pcb->persist_cnt >= tcp_persist_backoff[pcb->persist_backoff-1]) {
624           pcb->persist_cnt = 0;
625           if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
626             pcb->persist_backoff++;
627           }
628           tcp_zero_window_probe(pcb);
629         }
630       } else {
631         /* Increase the retransmission timer if it is running */
632         if(pcb->rtime >= 0)
633           ++pcb->rtime;
634
635         if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
636           /* Time for a retransmission. */
637           LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
638                                       " pcb->rto %"S16_F"\n",
639                                       pcb->rtime, pcb->rto));
640
641           /* Double retransmission time-out unless we are trying to
642            * connect to somebody (i.e., we are in SYN_SENT). */
643           if (pcb->state != SYN_SENT) {
644             pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx];
645           }
646
647           /* Reset the retransmission timer. */
648           pcb->rtime = 0;
649
650           /* Reduce congestion window and ssthresh. */
651           eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
652           pcb->ssthresh = eff_wnd >> 1;
653           if (pcb->ssthresh < pcb->mss) {
654             pcb->ssthresh = pcb->mss * 2;
655           }
656           pcb->cwnd = pcb->mss;
657           LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F
658                                        " ssthresh %"U16_F"\n",
659                                        pcb->cwnd, pcb->ssthresh));
660  
661           /* The following needs to be called AFTER cwnd is set to one
662              mss - STJ */
663           tcp_rexmit_rto(pcb);
664         }
665       }
666     }
667     /* Check if this PCB has stayed too long in FIN-WAIT-2 */
668     if (pcb->state == FIN_WAIT_2) {
669       if ((u32_t)(tcp_ticks - pcb->tmr) >
670           TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
671         ++pcb_remove;
672         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
673       }
674     }
675
676     /* Check if KEEPALIVE should be sent */
677     if((pcb->so_options & SOF_KEEPALIVE) &&
678        ((pcb->state == ESTABLISHED) ||
679         (pcb->state == CLOSE_WAIT))) {
680 #if LWIP_TCP_KEEPALIVE
681       if((u32_t)(tcp_ticks - pcb->tmr) >
682          (pcb->keep_idle + (pcb->keep_cnt*pcb->keep_intvl))
683          / TCP_SLOW_INTERVAL)
684 #else      
685       if((u32_t)(tcp_ticks - pcb->tmr) >
686          (pcb->keep_idle + TCP_MAXIDLE) / TCP_SLOW_INTERVAL)
687 #endif /* LWIP_TCP_KEEPALIVE */
688       {
689         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F".%"U16_F".%"U16_F".%"U16_F".\n",
690                                 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
691                                 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
692         
693         ++pcb_remove;
694         ++pcb_reset;
695       }
696 #if LWIP_TCP_KEEPALIVE
697       else if((u32_t)(tcp_ticks - pcb->tmr) > 
698               (pcb->keep_idle + pcb->keep_cnt_sent * pcb->keep_intvl)
699               / TCP_SLOW_INTERVAL)
700 #else
701       else if((u32_t)(tcp_ticks - pcb->tmr) > 
702               (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEPINTVL_DEFAULT) 
703               / TCP_SLOW_INTERVAL)
704 #endif /* LWIP_TCP_KEEPALIVE */
705       {
706         tcp_keepalive(pcb);
707         pcb->keep_cnt_sent++;
708       }
709     }
710
711     /* If this PCB has queued out of sequence data, but has been
712        inactive for too long, will drop the data (it will eventually
713        be retransmitted). */
714 #if TCP_QUEUE_OOSEQ
715     if (pcb->ooseq != NULL &&
716         (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
717       tcp_segs_free(pcb->ooseq);
718       pcb->ooseq = NULL;
719       LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
720     }
721 #endif /* TCP_QUEUE_OOSEQ */
722
723     /* Check if this PCB has stayed too long in SYN-RCVD */
724     if (pcb->state == SYN_RCVD) {
725       if ((u32_t)(tcp_ticks - pcb->tmr) >
726           TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
727         ++pcb_remove;
728         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
729       }
730     }
731
732     /* Check if this PCB has stayed too long in LAST-ACK */
733     if (pcb->state == LAST_ACK) {
734       if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
735         ++pcb_remove;
736         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
737       }
738     }
739
740     /* If the PCB should be removed, do it. */
741     if (pcb_remove) {
742       tcp_pcb_purge(pcb);
743       /* Remove PCB from tcp_active_pcbs list. */
744       if (prev != NULL) {
745         LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
746         prev->next = pcb->next;
747       } else {
748         /* This PCB was the first. */
749         LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
750         tcp_active_pcbs = pcb->next;
751       }
752
753       TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT);
754       if (pcb_reset) {
755         tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
756           pcb->local_port, pcb->remote_port);
757       }
758
759       pcb2 = pcb->next;
760       memp_free(MEMP_TCP_PCB, pcb);
761       pcb = pcb2;
762     } else {
763
764       /* We check if we should poll the connection. */
765       ++pcb->polltmr;
766       if (pcb->polltmr >= pcb->pollinterval) {
767         pcb->polltmr = 0;
768         LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
769         TCP_EVENT_POLL(pcb, err);
770         if (err == ERR_OK) {
771           tcp_output(pcb);
772         }
773       }
774       
775       prev = pcb;
776       pcb = pcb->next;
777     }
778   }
779
780   
781   /* Steps through all of the TIME-WAIT PCBs. */
782   prev = NULL;    
783   pcb = tcp_tw_pcbs;
784   while (pcb != NULL) {
785     LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
786     pcb_remove = 0;
787
788     /* Check if this PCB has stayed long enough in TIME-WAIT */
789     if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
790       ++pcb_remove;
791     }
792     
793
794
795     /* If the PCB should be removed, do it. */
796     if (pcb_remove) {
797       tcp_pcb_purge(pcb);
798       /* Remove PCB from tcp_tw_pcbs list. */
799       if (prev != NULL) {
800         LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
801         prev->next = pcb->next;
802       } else {
803         /* This PCB was the first. */
804         LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
805         tcp_tw_pcbs = pcb->next;
806       }
807       pcb2 = pcb->next;
808       memp_free(MEMP_TCP_PCB, pcb);
809       pcb = pcb2;
810     } else {
811       prev = pcb;
812       pcb = pcb->next;
813     }
814   }
815 }
816
817 /**
818  * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
819  * "refused" by upper layer (application) and sends delayed ACKs.
820  *
821  * Automatically called from tcp_tmr().
822  */
823 void
824 tcp_fasttmr(void)
825 {
826   struct tcp_pcb *pcb;
827
828   for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
829     /* If there is data which was previously "refused" by upper layer */
830     if (pcb->refused_data != NULL) {
831       /* Notify again application with data previously received. */
832       err_t err;
833       LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_fasttmr: notify kept packet\n"));
834       TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err);
835       if (err == ERR_OK) {
836         pcb->refused_data = NULL;
837       }
838     }
839
840     /* send delayed ACKs */  
841     if (pcb->flags & TF_ACK_DELAY) {
842       LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
843       tcp_ack_now(pcb);
844       tcp_output(pcb);
845       pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
846     }
847   }
848 }
849
850 /**
851  * Deallocates a list of TCP segments (tcp_seg structures).
852  *
853  * @param seg tcp_seg list of TCP segments to free
854  * @return the number of pbufs that were deallocated
855  */
856 u8_t
857 tcp_segs_free(struct tcp_seg *seg)
858 {
859   u8_t count = 0;
860   struct tcp_seg *next;
861   while (seg != NULL) {
862     next = seg->next;
863     count += tcp_seg_free(seg);
864     seg = next;
865   }
866   return count;
867 }
868
869 /**
870  * Frees a TCP segment (tcp_seg structure).
871  *
872  * @param seg single tcp_seg to free
873  * @return the number of pbufs that were deallocated
874  */
875 u8_t
876 tcp_seg_free(struct tcp_seg *seg)
877 {
878   u8_t count = 0;
879   
880   if (seg != NULL) {
881     if (seg->p != NULL) {
882       count = pbuf_free(seg->p);
883 #if TCP_DEBUG
884       seg->p = NULL;
885 #endif /* TCP_DEBUG */
886     }
887     memp_free(MEMP_TCP_SEG, seg);
888   }
889   return count;
890 }
891
892 /**
893  * Sets the priority of a connection.
894  *
895  * @param pcb the tcp_pcb to manipulate
896  * @param prio new priority
897  */
898 void
899 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
900 {
901   pcb->prio = prio;
902 }
903 #if TCP_QUEUE_OOSEQ
904
905 /**
906  * Returns a copy of the given TCP segment.
907  * The pbuf and data are not copied, only the pointers
908  *
909  * @param seg the old tcp_seg
910  * @return a copy of seg
911  */ 
912 struct tcp_seg *
913 tcp_seg_copy(struct tcp_seg *seg)
914 {
915   struct tcp_seg *cseg;
916
917   cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
918   if (cseg == NULL) {
919     return NULL;
920   }
921   SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg)); 
922   pbuf_ref(cseg->p);
923   return cseg;
924 }
925 #endif
926
927 #if LWIP_CALLBACK_API
928 /**
929  * Default receive callback that is called if the user didn't register
930  * a recv callback for the pcb.
931  */
932 err_t
933 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
934 {
935   LWIP_UNUSED_ARG(arg);
936   if (p != NULL) {
937     tcp_recved(pcb, p->tot_len);
938     pbuf_free(p);
939   } else if (err == ERR_OK) {
940     return tcp_close(pcb);
941   }
942   return ERR_OK;
943 }
944 #endif /* LWIP_CALLBACK_API */
945
946 /**
947  * Kills the oldest active connection that has lower priority than prio.
948  *
949  * @param prio minimum priority
950  */
951 static void
952 tcp_kill_prio(u8_t prio)
953 {
954   struct tcp_pcb *pcb, *inactive;
955   u32_t inactivity;
956   u8_t mprio;
957
958
959   mprio = TCP_PRIO_MAX;
960   
961   /* We kill the oldest active connection that has lower priority than prio. */
962   inactivity = 0;
963   inactive = NULL;
964   for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
965     if (pcb->prio <= prio &&
966        pcb->prio <= mprio &&
967        (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
968       inactivity = tcp_ticks - pcb->tmr;
969       inactive = pcb;
970       mprio = pcb->prio;
971     }
972   }
973   if (inactive != NULL) {
974     LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
975            (void *)inactive, inactivity));
976     tcp_abort(inactive);
977   }      
978 }
979
980 /**
981  * Kills the oldest connection that is in TIME_WAIT state.
982  * Called from tcp_alloc() if no more connections are available.
983  */
984 static void
985 tcp_kill_timewait(void)
986 {
987   struct tcp_pcb *pcb, *inactive;
988   u32_t inactivity;
989
990   inactivity = 0;
991   inactive = NULL;
992   /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
993   for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
994     if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
995       inactivity = tcp_ticks - pcb->tmr;
996       inactive = pcb;
997     }
998   }
999   if (inactive != NULL) {
1000     LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
1001            (void *)inactive, inactivity));
1002     tcp_abort(inactive);
1003   }      
1004 }
1005
1006 /**
1007  * Allocate a new tcp_pcb structure.
1008  *
1009  * @param prio priority for the new pcb
1010  * @return a new tcp_pcb that initially is in state CLOSED
1011  */
1012 struct tcp_pcb *
1013 tcp_alloc(u8_t prio)
1014 {
1015   struct tcp_pcb *pcb;
1016   u32_t iss;
1017   
1018   pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1019   if (pcb == NULL) {
1020     /* Try killing oldest connection in TIME-WAIT. */
1021     LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1022     tcp_kill_timewait();
1023     /* Try to allocate a tcp_pcb again. */
1024     pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1025     if (pcb == NULL) {
1026       /* Try killing active connections with lower priority than the new one. */
1027       LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
1028       tcp_kill_prio(prio);
1029       /* Try to allocate a tcp_pcb again. */
1030       pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1031       if (pcb != NULL) {
1032         /* adjust err stats: memp_malloc failed twice before */
1033         MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1034       }
1035     }
1036     if (pcb != NULL) {
1037       /* adjust err stats: timewait PCB was freed above */
1038       MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1039     }
1040   }
1041   if (pcb != NULL) {
1042     memset(pcb, 0, sizeof(struct tcp_pcb));
1043     pcb->prio = TCP_PRIO_NORMAL;
1044     pcb->snd_buf = TCP_SND_BUF;
1045     pcb->snd_queuelen = 0;
1046     pcb->rcv_wnd = TCP_WND;
1047     pcb->rcv_ann_wnd = TCP_WND;
1048     pcb->tos = 0;
1049     pcb->ttl = TCP_TTL;
1050     /* As initial send MSS, we use TCP_MSS but limit it to 536.
1051        The send MSS is updated when an MSS option is received. */
1052     pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
1053     pcb->rto = 3000 / TCP_SLOW_INTERVAL;
1054     pcb->sa = 0;
1055     pcb->sv = 3000 / TCP_SLOW_INTERVAL;
1056     pcb->rtime = -1;
1057     pcb->cwnd = 1;
1058     iss = tcp_next_iss();
1059     pcb->snd_wl2 = iss;
1060     pcb->snd_nxt = iss;
1061     pcb->lastack = iss;
1062     pcb->snd_lbb = iss;   
1063     pcb->tmr = tcp_ticks;
1064
1065     pcb->polltmr = 0;
1066
1067 #if LWIP_CALLBACK_API
1068     pcb->recv = tcp_recv_null;
1069 #endif /* LWIP_CALLBACK_API */  
1070     
1071     /* Init KEEPALIVE timer */
1072     pcb->keep_idle  = TCP_KEEPIDLE_DEFAULT;
1073     
1074 #if LWIP_TCP_KEEPALIVE
1075     pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1076     pcb->keep_cnt   = TCP_KEEPCNT_DEFAULT;
1077 #endif /* LWIP_TCP_KEEPALIVE */
1078
1079     pcb->keep_cnt_sent = 0;
1080   }
1081   return pcb;
1082 }
1083
1084 /**
1085  * Creates a new TCP protocol control block but doesn't place it on
1086  * any of the TCP PCB lists.
1087  * The pcb is not put on any list until binding using tcp_bind().
1088  *
1089  * @internal: Maybe there should be a idle TCP PCB list where these
1090  * PCBs are put on. Port reservation using tcp_bind() is implemented but
1091  * allocated pcbs that are not bound can't be killed automatically if wanting
1092  * to allocate a pcb with higher prio (@see tcp_kill_prio())
1093  *
1094  * @return a new tcp_pcb that initially is in state CLOSED
1095  */
1096 struct tcp_pcb *
1097 tcp_new(void)
1098 {
1099   return tcp_alloc(TCP_PRIO_NORMAL);
1100 }
1101
1102 /**
1103  * Used to specify the argument that should be passed callback
1104  * functions.
1105  *
1106  * @param pcb tcp_pcb to set the callback argument
1107  * @param arg void pointer argument to pass to callback functions
1108  */ 
1109 void
1110 tcp_arg(struct tcp_pcb *pcb, void *arg)
1111 {  
1112   pcb->callback_arg = arg;
1113 }
1114 #if LWIP_CALLBACK_API
1115
1116 /**
1117  * Used to specify the function that should be called when a TCP
1118  * connection receives data.
1119  *
1120  * @param pcb tcp_pcb to set the recv callback
1121  * @param recv callback function to call for this pcb when data is received
1122  */ 
1123 void
1124 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
1125 {
1126   pcb->recv = recv;
1127 }
1128
1129 /**
1130  * Used to specify the function that should be called when TCP data
1131  * has been successfully delivered to the remote host.
1132  *
1133  * @param pcb tcp_pcb to set the sent callback
1134  * @param sent callback function to call for this pcb when data is successfully sent
1135  */ 
1136 void
1137 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
1138 {
1139   pcb->sent = sent;
1140 }
1141
1142 /**
1143  * Used to specify the function that should be called when a fatal error
1144  * has occured on the connection.
1145  *
1146  * @param pcb tcp_pcb to set the err callback
1147  * @param err callback function to call for this pcb when a fatal error
1148  *        has occured on the connection
1149  */ 
1150 void
1151 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
1152 {
1153   pcb->errf = err;
1154 }
1155
1156 /**
1157  * Used for specifying the function that should be called when a
1158  * LISTENing connection has been connected to another host.
1159  *
1160  * @param pcb tcp_pcb to set the accept callback
1161  * @param accept callback function to call for this pcb when LISTENing
1162  *        connection has been connected to another host
1163  */ 
1164 void
1165 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
1166 {
1167   pcb->accept = accept;
1168 }
1169 #endif /* LWIP_CALLBACK_API */
1170
1171
1172 /**
1173  * Used to specify the function that should be called periodically
1174  * from TCP. The interval is specified in terms of the TCP coarse
1175  * timer interval, which is called twice a second.
1176  *
1177  */ 
1178 void
1179 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1180 {
1181 #if LWIP_CALLBACK_API
1182   pcb->poll = poll;
1183 #endif /* LWIP_CALLBACK_API */  
1184   pcb->pollinterval = interval;
1185 }
1186
1187 /**
1188  * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
1189  * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
1190  *
1191  * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
1192  */
1193 void
1194 tcp_pcb_purge(struct tcp_pcb *pcb)
1195 {
1196   if (pcb->state != CLOSED &&
1197      pcb->state != TIME_WAIT &&
1198      pcb->state != LISTEN) {
1199
1200     LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1201
1202 #if TCP_LISTEN_BACKLOG
1203     if (pcb->state == SYN_RCVD) {
1204       /* Need to find the corresponding listen_pcb and decrease its accepts_pending */
1205       struct tcp_pcb_listen *lpcb;
1206       LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL",
1207         tcp_listen_pcbs.listen_pcbs != NULL);
1208       for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
1209         if ((lpcb->local_port == pcb->local_port) &&
1210             (ip_addr_isany(&lpcb->local_ip) ||
1211              ip_addr_cmp(&pcb->local_ip, &lpcb->local_ip))) {
1212             /* port and address of the listen pcb match the timed-out pcb */
1213             LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending",
1214               lpcb->accepts_pending > 0);
1215             lpcb->accepts_pending--;
1216             break;
1217           }
1218       }
1219     }
1220 #endif /* TCP_LISTEN_BACKLOG */
1221
1222
1223     if (pcb->refused_data != NULL) {
1224       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
1225       pbuf_free(pcb->refused_data);
1226       pcb->refused_data = NULL;
1227     }
1228     if (pcb->unsent != NULL) {
1229       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1230     }
1231     if (pcb->unacked != NULL) {
1232       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1233     }
1234 #if TCP_QUEUE_OOSEQ /* LW */
1235     if (pcb->ooseq != NULL) {
1236       LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1237     }
1238     tcp_segs_free(pcb->ooseq);
1239     pcb->ooseq = NULL;
1240 #endif /* TCP_QUEUE_OOSEQ */
1241
1242     /* Stop the retransmission timer as it will expect data on unacked
1243        queue if it fires */
1244     pcb->rtime = -1;
1245
1246     tcp_segs_free(pcb->unsent);
1247     tcp_segs_free(pcb->unacked);
1248     pcb->unacked = pcb->unsent = NULL;
1249   }
1250 }
1251
1252 /**
1253  * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
1254  *
1255  * @param pcblist PCB list to purge.
1256  * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
1257  */
1258 void
1259 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1260 {
1261   TCP_RMV(pcblist, pcb);
1262
1263   tcp_pcb_purge(pcb);
1264   
1265   /* if there is an outstanding delayed ACKs, send it */
1266   if (pcb->state != TIME_WAIT &&
1267      pcb->state != LISTEN &&
1268      pcb->flags & TF_ACK_DELAY) {
1269     pcb->flags |= TF_ACK_NOW;
1270     tcp_output(pcb);
1271   }
1272
1273   if (pcb->state != LISTEN) {
1274     LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
1275     LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
1276 #if TCP_QUEUE_OOSEQ
1277     LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
1278 #endif /* TCP_QUEUE_OOSEQ */
1279   }
1280
1281   pcb->state = CLOSED;
1282
1283   LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1284 }
1285
1286 /**
1287  * Calculates a new initial sequence number for new connections.
1288  *
1289  * @return u32_t pseudo random sequence number
1290  */
1291 u32_t
1292 tcp_next_iss(void)
1293 {
1294   static u32_t iss = 6510;
1295   
1296   iss += tcp_ticks;       /* XXX */
1297   return iss;
1298 }
1299
1300 #if TCP_CALCULATE_EFF_SEND_MSS
1301 /**
1302  * Calcluates the effective send mss that can be used for a specific IP address
1303  * by using ip_route to determin the netif used to send to the address and
1304  * calculating the minimum of TCP_MSS and that netif's mtu (if set).
1305  */
1306 u16_t
1307 tcp_eff_send_mss(u16_t sendmss, struct ip_addr *addr)
1308 {
1309   u16_t mss_s;
1310   struct netif *outif;
1311
1312   outif = ip_route(addr);
1313   if ((outif != NULL) && (outif->mtu != 0)) {
1314     mss_s = outif->mtu - IP_HLEN - TCP_HLEN;
1315     /* RFC 1122, chap 4.2.2.6:
1316      * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1317      * We correct for TCP options in tcp_enqueue(), and don't support
1318      * IP options
1319      */
1320     sendmss = LWIP_MIN(sendmss, mss_s);
1321   }
1322   return sendmss;
1323 }
1324 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1325
1326 const char*
1327 tcp_debug_state_str(enum tcp_state s)
1328 {
1329   return tcp_state_str[s];
1330 }
1331
1332 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
1333 /**
1334  * Print a tcp header for debugging purposes.
1335  *
1336  * @param tcphdr pointer to a struct tcp_hdr
1337  */
1338 void
1339 tcp_debug_print(struct tcp_hdr *tcphdr)
1340 {
1341   LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
1342   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1343   LWIP_DEBUGF(TCP_DEBUG, ("|    %5"U16_F"      |    %5"U16_F"      | (src port, dest port)\n",
1344          ntohs(tcphdr->src), ntohs(tcphdr->dest)));
1345   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1346   LWIP_DEBUGF(TCP_DEBUG, ("|           %010"U32_F"          | (seq no)\n",
1347           ntohl(tcphdr->seqno)));
1348   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1349   LWIP_DEBUGF(TCP_DEBUG, ("|           %010"U32_F"          | (ack no)\n",
1350          ntohl(tcphdr->ackno)));
1351   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1352   LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" |   |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"|     %5"U16_F"     | (hdrlen, flags (",
1353        TCPH_HDRLEN(tcphdr),
1354          TCPH_FLAGS(tcphdr) >> 5 & 1,
1355          TCPH_FLAGS(tcphdr) >> 4 & 1,
1356          TCPH_FLAGS(tcphdr) >> 3 & 1,
1357          TCPH_FLAGS(tcphdr) >> 2 & 1,
1358          TCPH_FLAGS(tcphdr) >> 1 & 1,
1359          TCPH_FLAGS(tcphdr) & 1,
1360          ntohs(tcphdr->wnd)));
1361   tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
1362   LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
1363   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1364   LWIP_DEBUGF(TCP_DEBUG, ("|    0x%04"X16_F"     |     %5"U16_F"     | (chksum, urgp)\n",
1365          ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
1366   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1367 }
1368
1369 /**
1370  * Print a tcp state for debugging purposes.
1371  *
1372  * @param s enum tcp_state to print
1373  */
1374 void
1375 tcp_debug_print_state(enum tcp_state s)
1376 {
1377   LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
1378 }
1379
1380 /**
1381  * Print tcp flags for debugging purposes.
1382  *
1383  * @param flags tcp flags, all active flags are printed
1384  */
1385 void
1386 tcp_debug_print_flags(u8_t flags)
1387 {
1388   if (flags & TCP_FIN) {
1389     LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
1390   }
1391   if (flags & TCP_SYN) {
1392     LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
1393   }
1394   if (flags & TCP_RST) {
1395     LWIP_DEBUGF(TCP_DEBUG, ("RST "));
1396   }
1397   if (flags & TCP_PSH) {
1398     LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
1399   }
1400   if (flags & TCP_ACK) {
1401     LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
1402   }
1403   if (flags & TCP_URG) {
1404     LWIP_DEBUGF(TCP_DEBUG, ("URG "));
1405   }
1406   if (flags & TCP_ECE) {
1407     LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
1408   }
1409   if (flags & TCP_CWR) {
1410     LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
1411   }
1412   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1413 }
1414
1415 /**
1416  * Print all tcp_pcbs in every list for debugging purposes.
1417  */
1418 void
1419 tcp_debug_print_pcbs(void)
1420 {
1421   struct tcp_pcb *pcb;
1422   LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
1423   for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1424     LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1425                        pcb->local_port, pcb->remote_port,
1426                        pcb->snd_nxt, pcb->rcv_nxt));
1427     tcp_debug_print_state(pcb->state);
1428   }    
1429   LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
1430   for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
1431     LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1432                        pcb->local_port, pcb->remote_port,
1433                        pcb->snd_nxt, pcb->rcv_nxt));
1434     tcp_debug_print_state(pcb->state);
1435   }    
1436   LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
1437   for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1438     LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1439                        pcb->local_port, pcb->remote_port,
1440                        pcb->snd_nxt, pcb->rcv_nxt));
1441     tcp_debug_print_state(pcb->state);
1442   }    
1443 }
1444
1445 /**
1446  * Check state consistency of the tcp_pcb lists.
1447  */
1448 s16_t
1449 tcp_pcbs_sane(void)
1450 {
1451   struct tcp_pcb *pcb;
1452   for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1453     LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
1454     LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
1455     LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
1456   }
1457   for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1458     LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1459   }
1460   return 1;
1461 }
1462 #endif /* TCP_DEBUG */
1463
1464 #endif /* LWIP_TCP */