3 * Transmission Control Protocol for IP
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.
12 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without modification,
16 * are permitted provided that the following conditions are met:
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.
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
37 * This file is part of the lwIP TCP/IP stack.
39 * Author: Adam Dunkels <adam@sics.se>
45 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
49 #include "lwip/memp.h"
50 #include "lwip/snmp.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/debug.h"
54 #include "lwip/stats.h"
58 const char * const tcp_state_str
[] = {
72 /* Incremented every coarse grained timer shot (typically every 500 ms). */
74 const u8_t tcp_backoff
[13] =
75 { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
76 /* Times per slowtmr hits */
77 const u8_t tcp_persist_backoff
[7] = { 3, 6, 12, 24, 48, 96, 120 };
79 /* The TCP PCB lists. */
81 /** List of all TCP PCBs bound but not yet (connected || listening) */
82 struct tcp_pcb
*tcp_bound_pcbs
;
83 /** List of all TCP PCBs in LISTEN state */
84 union tcp_listen_pcbs_t tcp_listen_pcbs
;
85 /** List of all TCP PCBs that are in a state in which
86 * they accept or send data. */
87 struct tcp_pcb
*tcp_active_pcbs
;
88 /** List of all TCP PCBs in TIME-WAIT state */
89 struct tcp_pcb
*tcp_tw_pcbs
;
91 #define NUM_TCP_PCB_LISTS 4
92 #define NUM_TCP_PCB_LISTS_NO_TIME_WAIT 3
93 /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */
94 struct tcp_pcb
**tcp_pcb_lists
[] = {&tcp_listen_pcbs
.pcbs
, &tcp_bound_pcbs
,
95 &tcp_active_pcbs
, &tcp_tw_pcbs
};
97 /** Only used for temporary storage. */
98 struct tcp_pcb
*tcp_tmp_pcb
;
100 /** Timer counter to handle calling slow-timer from tcp_tmr() */
101 static u8_t tcp_timer
;
102 static u16_t
tcp_new_port(void);
105 * Called periodically to dispatch TCP timers.
111 /* Call tcp_fasttmr() every 250 ms */
114 if (++tcp_timer
& 1) {
115 /* Call tcp_tmr() every 500 ms, i.e., every other timer
116 tcp_tmr() is called. */
122 * Closes the TX side of a connection held by the PCB.
123 * For tcp_close(), a RST is sent if the application didn't receive all data
124 * (tcp_recved() not called for all data passed to recv callback).
126 * Listening pcbs are freed and may not be referenced any more.
127 * Connection pcbs are freed if not yet connected and may not be referenced
128 * any more. If a connection is established (at least SYN received or in
129 * a closing state), the connection is closed, and put in a closing state.
130 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
131 * unsafe to reference it.
133 * @param pcb the tcp_pcb to close
134 * @return ERR_OK if connection has been closed
135 * another err_t if closing failed and pcb is not freed
138 tcp_close_shutdown(struct tcp_pcb
*pcb
, u8_t rst_on_unacked_data
)
142 if (rst_on_unacked_data
&& (pcb
->state
!= LISTEN
)) {
143 if ((pcb
->refused_data
!= NULL
) || (pcb
->rcv_wnd
!= TCP_WND
)) {
144 /* Not all data received by application, send RST to tell the remote
146 LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb
->flags
& TF_RXCLOSED
);
148 /* don't call tcp_abort here: we must not deallocate the pcb since
149 that might not be expected when calling tcp_close */
150 tcp_rst(pcb
->snd_nxt
, pcb
->rcv_nxt
, &pcb
->local_ip
, &pcb
->remote_ip
,
151 pcb
->local_port
, pcb
->remote_port
);
155 /* TODO: to which state do we move now? */
157 /* move to TIME_WAIT since we close actively */
158 TCP_RMV(&tcp_active_pcbs
, pcb
);
159 pcb
->state
= TIME_WAIT
;
160 TCP_REG(&tcp_tw_pcbs
, pcb
);
166 switch (pcb
->state
) {
168 /* Closing a pcb in the CLOSED state might seem erroneous,
169 * however, it is in this state once allocated and as yet unused
170 * and the user needs some way to free it should the need arise.
171 * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
172 * or for a pcb that has been used and then entered the CLOSED state
173 * is erroneous, but this should never happen as the pcb has in those cases
174 * been freed, and so any remaining handles are bogus. */
176 if(pcb
->local_port
!= 0)
177 TCP_RMV(&tcp_bound_pcbs
, pcb
);
178 memp_free(MEMP_TCP_PCB
, pcb
);
183 tcp_pcb_remove(&tcp_listen_pcbs
.pcbs
, pcb
);
184 memp_free(MEMP_TCP_PCB_LISTEN
, pcb
);
189 tcp_pcb_remove(&tcp_active_pcbs
, pcb
);
190 memp_free(MEMP_TCP_PCB
, pcb
);
192 snmp_inc_tcpattemptfails();
195 err
= tcp_send_fin(pcb
);
197 snmp_inc_tcpattemptfails();
198 pcb
->state
= FIN_WAIT_1
;
202 err
= tcp_send_fin(pcb
);
204 snmp_inc_tcpestabresets();
205 pcb
->state
= FIN_WAIT_1
;
209 err
= tcp_send_fin(pcb
);
211 snmp_inc_tcpestabresets();
212 pcb
->state
= LAST_ACK
;
216 /* Has already been closed, do nothing. */
222 if (pcb
!= NULL
&& err
== ERR_OK
) {
223 /* To ensure all data has been sent when tcp_close returns, we have
224 to make sure tcp_output doesn't fail.
225 Since we don't really have to ensure all data has been sent when tcp_close
226 returns (unsent data is sent from tcp timer functions, also), we don't care
227 for the return value of tcp_output for now. */
228 /* @todo: When implementing SO_LINGER, this must be changed somehow:
229 If SOF_LINGER is set, the data should be sent and acked before close returns.
230 This can only be valid for sequential APIs, not for the raw API. */
237 * Closes the connection held by the PCB.
239 * Listening pcbs are freed and may not be referenced any more.
240 * Connection pcbs are freed if not yet connected and may not be referenced
241 * any more. If a connection is established (at least SYN received or in
242 * a closing state), the connection is closed, and put in a closing state.
243 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
244 * unsafe to reference it (unless an error is returned).
246 * @param pcb the tcp_pcb to close
247 * @return ERR_OK if connection has been closed
248 * another err_t if closing failed and pcb is not freed
251 tcp_close(struct tcp_pcb
*pcb
)
254 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_close: closing in "));
255 tcp_debug_print_state(pcb
->state
);
256 #endif /* TCP_DEBUG */
258 if (pcb
->state
!= LISTEN
) {
259 /* Set a flag not to receive any more data... */
260 pcb
->flags
|= TF_RXCLOSED
;
263 return tcp_close_shutdown(pcb
, 1);
267 * Causes all or part of a full-duplex connection of this PCB to be shut down.
268 * This doesn't deallocate the PCB!
270 * @param pcb PCB to shutdown
271 * @param shut_rx shut down receive side if this is != 0
272 * @param shut_tx shut down send side if this is != 0
273 * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down)
274 * another err_t on error.
277 tcp_shutdown(struct tcp_pcb
*pcb
, int shut_rx
, int shut_tx
)
279 if (pcb
->state
== LISTEN
) {
283 /* shut down the receive side: free buffered data... */
284 if (pcb
->refused_data
!= NULL
) {
285 pbuf_free(pcb
->refused_data
);
286 pcb
->refused_data
= NULL
;
288 /* ... and set a flag not to receive any more data */
289 pcb
->flags
|= TF_RXCLOSED
;
292 /* This can't happen twice since if it succeeds, the pcb's state is changed.
293 Only close in these states as the others directly deallocate the PCB */
294 switch (pcb
->state
) {
298 return tcp_close_shutdown(pcb
, 0);
300 /* don't shut down other states */
304 /* @todo: return another err_t if not in correct state or already shut? */
309 * Abandons a connection and optionally sends a RST to the remote
310 * host. Deletes the local protocol control block. This is done when
311 * a connection is killed because of shortage of memory.
313 * @param pcb the tcp_pcb to abort
314 * @param reset boolean to indicate whether a reset should be sent
317 tcp_abandon(struct tcp_pcb
*pcb
, int reset
)
320 u16_t remote_port
, local_port
;
321 ip_addr_t remote_ip
, local_ip
;
322 #if LWIP_CALLBACK_API
324 #endif /* LWIP_CALLBACK_API */
327 /* pcb->state LISTEN not allowed here */
328 LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
329 pcb
->state
!= LISTEN
);
330 /* Figure out on which TCP PCB list we are, and remove us. If we
331 are in an active state, call the receive function associated with
332 the PCB with a NULL argument, and send an RST to the remote end. */
333 if (pcb
->state
== TIME_WAIT
) {
334 tcp_pcb_remove(&tcp_tw_pcbs
, pcb
);
335 memp_free(MEMP_TCP_PCB
, pcb
);
337 seqno
= pcb
->snd_nxt
;
338 ackno
= pcb
->rcv_nxt
;
339 ip_addr_copy(local_ip
, pcb
->local_ip
);
340 ip_addr_copy(remote_ip
, pcb
->remote_ip
);
341 local_port
= pcb
->local_port
;
342 remote_port
= pcb
->remote_port
;
343 #if LWIP_CALLBACK_API
345 #endif /* LWIP_CALLBACK_API */
346 errf_arg
= pcb
->callback_arg
;
347 tcp_pcb_remove(&tcp_active_pcbs
, pcb
);
348 if (pcb
->unacked
!= NULL
) {
349 tcp_segs_free(pcb
->unacked
);
351 if (pcb
->unsent
!= NULL
) {
352 tcp_segs_free(pcb
->unsent
);
355 if (pcb
->ooseq
!= NULL
) {
356 tcp_segs_free(pcb
->ooseq
);
358 #endif /* TCP_QUEUE_OOSEQ */
359 memp_free(MEMP_TCP_PCB
, pcb
);
360 TCP_EVENT_ERR(errf
, errf_arg
, ERR_ABRT
);
362 LWIP_DEBUGF(TCP_RST_DEBUG
, ("tcp_abandon: sending RST\n"));
363 tcp_rst(seqno
, ackno
, &local_ip
, &remote_ip
, local_port
, remote_port
);
369 * Aborts the connection by sending a RST (reset) segment to the remote
370 * host. The pcb is deallocated. This function never fails.
372 * ATTENTION: When calling this from one of the TCP callbacks, make
373 * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise
374 * or you will risk accessing deallocated memory or memory leaks!
376 * @param pcb the tcp pcb to abort
379 tcp_abort(struct tcp_pcb
*pcb
)
385 * Binds the connection to a local portnumber and IP address. If the
386 * IP address is not given (i.e., ipaddr == NULL), the IP address of
387 * the outgoing network interface is used instead.
389 * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
391 * @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind
392 * to any local address
393 * @param port the local port to bind to
394 * @return ERR_USE if the port is already in use
398 tcp_bind(struct tcp_pcb
*pcb
, ip_addr_t
*ipaddr
, u16_t port
)
401 int max_pcb_list
= NUM_TCP_PCB_LISTS
;
402 struct tcp_pcb
*cpcb
;
404 LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb
->state
== CLOSED
, return ERR_ISCONN
);
407 /* Unless the REUSEADDR flag is set,
408 we have to check the pcbs in TIME-WAIT state, also.
409 We do not dump TIME_WAIT pcb's; they can still be matched by incoming
410 packets using both local and remote IP addresses and ports to distinguish.
413 if ((pcb
->so_options
& SOF_REUSEADDR
) != 0) {
414 max_pcb_list
= NUM_TCP_PCB_LISTS_NO_TIME_WAIT
;
416 #endif /* SO_REUSE */
417 #endif /* SO_REUSE */
420 port
= tcp_new_port();
423 /* Check if the address already is in use (on all lists) */
424 for (i
= 0; i
< max_pcb_list
; i
++) {
425 for(cpcb
= *tcp_pcb_lists
[i
]; cpcb
!= NULL
; cpcb
= cpcb
->next
) {
426 if (cpcb
->local_port
== port
) {
428 /* Omit checking for the same port if both pcbs have REUSEADDR set.
429 For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
431 if (((pcb
->so_options
& SOF_REUSEADDR
) == 0) ||
432 ((cpcb
->so_options
& SOF_REUSEADDR
) == 0))
433 #endif /* SO_REUSE */
435 if (ip_addr_isany(&(cpcb
->local_ip
)) ||
436 ip_addr_isany(ipaddr
) ||
437 ip_addr_cmp(&(cpcb
->local_ip
), ipaddr
)) {
445 if (!ip_addr_isany(ipaddr
)) {
446 pcb
->local_ip
= *ipaddr
;
448 pcb
->local_port
= port
;
449 TCP_REG(&tcp_bound_pcbs
, pcb
);
450 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_bind: bind to port %"U16_F
"\n", port
));
453 #if LWIP_CALLBACK_API
455 * Default accept callback if no accept callback is specified by the user.
458 tcp_accept_null(void *arg
, struct tcp_pcb
*pcb
, err_t err
)
460 LWIP_UNUSED_ARG(arg
);
461 LWIP_UNUSED_ARG(pcb
);
462 LWIP_UNUSED_ARG(err
);
466 #endif /* LWIP_CALLBACK_API */
469 * Set the state of the connection to be LISTEN, which means that it
470 * is able to accept incoming connections. The protocol control block
471 * is reallocated in order to consume less memory. Setting the
472 * connection to LISTEN is an irreversible process.
474 * @param pcb the original tcp_pcb
475 * @param backlog the incoming connections queue limit
476 * @return tcp_pcb used for listening, consumes less memory.
478 * @note The original tcp_pcb is freed. This function therefore has to be
480 * tpcb = tcp_listen(tpcb);
483 tcp_listen_with_backlog(struct tcp_pcb
*pcb
, u8_t backlog
)
485 struct tcp_pcb_listen
*lpcb
;
487 LWIP_UNUSED_ARG(backlog
);
488 LWIP_ERROR("tcp_listen: pcb already connected", pcb
->state
== CLOSED
, return NULL
);
490 /* already listening? */
491 if (pcb
->state
== LISTEN
) {
495 if ((pcb
->so_options
& SOF_REUSEADDR
) != 0) {
496 /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
497 is declared (listen-/connection-pcb), we have to make sure now that
498 this port is only used once for every local IP. */
499 for(lpcb
= tcp_listen_pcbs
.listen_pcbs
; lpcb
!= NULL
; lpcb
= lpcb
->next
) {
500 if (lpcb
->local_port
== pcb
->local_port
) {
501 if (ip_addr_cmp(&lpcb
->local_ip
, &pcb
->local_ip
)) {
502 /* this address/port is already used */
508 #endif /* SO_REUSE */
509 lpcb
= (struct tcp_pcb_listen
*)memp_malloc(MEMP_TCP_PCB_LISTEN
);
513 lpcb
->callback_arg
= pcb
->callback_arg
;
514 lpcb
->local_port
= pcb
->local_port
;
515 lpcb
->state
= LISTEN
;
516 lpcb
->prio
= pcb
->prio
;
517 lpcb
->so_options
= pcb
->so_options
;
518 lpcb
->so_options
|= SOF_ACCEPTCONN
;
519 lpcb
->ttl
= pcb
->ttl
;
520 lpcb
->tos
= pcb
->tos
;
521 ip_addr_copy(lpcb
->local_ip
, pcb
->local_ip
);
522 TCP_RMV(&tcp_bound_pcbs
, pcb
);
523 memp_free(MEMP_TCP_PCB
, pcb
);
524 #if LWIP_CALLBACK_API
525 lpcb
->accept
= tcp_accept_null
;
526 #endif /* LWIP_CALLBACK_API */
527 #if TCP_LISTEN_BACKLOG
528 lpcb
->accepts_pending
= 0;
529 lpcb
->backlog
= (backlog
? backlog
: 1);
530 #endif /* TCP_LISTEN_BACKLOG */
531 TCP_REG(&tcp_listen_pcbs
.pcbs
, (struct tcp_pcb
*)lpcb
);
532 return (struct tcp_pcb
*)lpcb
;
536 * Update the state that tracks the available window space to advertise.
538 * Returns how much extra window would be advertised if we sent an
541 u32_t
tcp_update_rcv_ann_wnd(struct tcp_pcb
*pcb
)
543 u32_t new_right_edge
= pcb
->rcv_nxt
+ pcb
->rcv_wnd
;
545 if (TCP_SEQ_GEQ(new_right_edge
, pcb
->rcv_ann_right_edge
+ LWIP_MIN((TCP_WND
/ 2), pcb
->mss
))) {
546 /* we can advertise more window */
547 pcb
->rcv_ann_wnd
= pcb
->rcv_wnd
;
548 return new_right_edge
- pcb
->rcv_ann_right_edge
;
550 if (TCP_SEQ_GT(pcb
->rcv_nxt
, pcb
->rcv_ann_right_edge
)) {
551 /* Can happen due to other end sending out of advertised window,
552 * but within actual available (but not yet advertised) window */
553 pcb
->rcv_ann_wnd
= 0;
555 /* keep the right edge of window constant */
556 u32_t new_rcv_ann_wnd
= pcb
->rcv_ann_right_edge
- pcb
->rcv_nxt
;
557 LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd
<= 0xffff);
558 pcb
->rcv_ann_wnd
= (u16_t
)new_rcv_ann_wnd
;
565 * This function should be called by the application when it has
566 * processed the data. The purpose is to advertise a larger window
567 * when the data has been processed.
569 * @param pcb the tcp_pcb for which data is read
570 * @param len the amount of bytes that have been read by the application
573 tcp_recved(struct tcp_pcb
*pcb
, u16_t len
)
577 LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n",
578 len
<= 0xffff - pcb
->rcv_wnd
);
581 if (pcb
->rcv_wnd
> TCP_WND
) {
582 pcb
->rcv_wnd
= TCP_WND
;
585 wnd_inflation
= tcp_update_rcv_ann_wnd(pcb
);
587 /* If the change in the right edge of window is significant (default
588 * watermark is TCP_WND/4), then send an explicit update now.
589 * Otherwise wait for a packet to be sent in the normal course of
590 * events (or more window to be available later) */
591 if (wnd_inflation
>= TCP_WND_UPDATE_THRESHOLD
) {
596 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_recved: recveived %"U16_F
" bytes, wnd %"U16_F
" (%"U16_F
").\n",
597 len
, pcb
->rcv_wnd
, TCP_WND
- pcb
->rcv_wnd
));
601 * A nastly hack featuring 'goto' statements that allocates a
602 * new TCP local port.
604 * @return a new (free) local TCP port number
611 #ifndef TCP_LOCAL_PORT_RANGE_START
612 #define TCP_LOCAL_PORT_RANGE_START 4096
613 #define TCP_LOCAL_PORT_RANGE_END 0x7fff
615 static u16_t port
= TCP_LOCAL_PORT_RANGE_START
;
618 if (++port
> TCP_LOCAL_PORT_RANGE_END
) {
619 port
= TCP_LOCAL_PORT_RANGE_START
;
621 /* Check all PCB lists. */
622 for (i
= 1; i
< NUM_TCP_PCB_LISTS
; i
++) {
623 for(pcb
= *tcp_pcb_lists
[i
]; pcb
!= NULL
; pcb
= pcb
->next
) {
624 if (pcb
->local_port
== port
) {
633 * Connects to another host. The function given as the "connected"
634 * argument will be called when the connection has been established.
636 * @param pcb the tcp_pcb used to establish the connection
637 * @param ipaddr the remote ip address to connect to
638 * @param port the remote tcp port to connect to
639 * @param connected callback function to call when connected (or on error)
640 * @return ERR_VAL if invalid arguments are given
641 * ERR_OK if connect request has been sent
642 * other err_t values if connect request couldn't be sent
645 tcp_connect(struct tcp_pcb
*pcb
, ip_addr_t
*ipaddr
, u16_t port
,
646 tcp_connected_fn connected
)
651 LWIP_ERROR("tcp_connect: can only connected from state CLOSED", pcb
->state
== CLOSED
, return ERR_ISCONN
);
653 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_connect to port %"U16_F
"\n", port
));
654 if (ipaddr
!= NULL
) {
655 pcb
->remote_ip
= *ipaddr
;
659 pcb
->remote_port
= port
;
661 /* check if we have a route to the remote host */
662 if (ip_addr_isany(&(pcb
->local_ip
))) {
663 /* no local IP address set, yet. */
664 struct netif
*netif
= ip_route(&(pcb
->remote_ip
));
666 /* Don't even try to send a SYN packet if we have no route
667 since that will fail. */
670 /* Use the netif's IP address as local address. */
671 ip_addr_copy(pcb
->local_ip
, netif
->ip_addr
);
674 if (pcb
->local_port
== 0) {
675 pcb
->local_port
= tcp_new_port();
678 if ((pcb
->so_options
& SOF_REUSEADDR
) != 0) {
679 /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
680 now that the 5-tuple is unique. */
681 struct tcp_pcb
*cpcb
;
683 /* Don't check listen PCBs, check bound-, active- and TIME-WAIT PCBs. */
684 for (i
= 1; i
< NUM_TCP_PCB_LISTS
; i
++) {
685 for(cpcb
= *tcp_pcb_lists
[i
]; cpcb
!= NULL
; cpcb
= cpcb
->next
) {
686 if ((cpcb
->local_port
== pcb
->local_port
) &&
687 (cpcb
->remote_port
== port
) &&
688 ip_addr_cmp(&cpcb
->local_ip
, &pcb
->local_ip
) &&
689 ip_addr_cmp(&cpcb
->remote_ip
, ipaddr
)) {
690 /* linux returns EISCONN here, but ERR_USE should be OK for us */
696 #endif /* SO_REUSE */
697 iss
= tcp_next_iss();
700 pcb
->lastack
= iss
- 1;
701 pcb
->snd_lbb
= iss
- 1;
702 pcb
->rcv_wnd
= TCP_WND
;
703 pcb
->rcv_ann_wnd
= TCP_WND
;
704 pcb
->rcv_ann_right_edge
= pcb
->rcv_nxt
;
705 pcb
->snd_wnd
= TCP_WND
;
706 /* As initial send MSS, we use TCP_MSS but limit it to 536.
707 The send MSS is updated when an MSS option is received. */
708 pcb
->mss
= (TCP_MSS
> 536) ? 536 : TCP_MSS
;
709 #if TCP_CALCULATE_EFF_SEND_MSS
710 pcb
->mss
= tcp_eff_send_mss(pcb
->mss
, ipaddr
);
711 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
713 pcb
->ssthresh
= pcb
->mss
* 10;
714 #if LWIP_CALLBACK_API
715 pcb
->connected
= connected
;
716 #else /* LWIP_CALLBACK_API */
717 LWIP_UNUSED_ARG(connected
);
718 #endif /* LWIP_CALLBACK_API */
720 /* Send a SYN together with the MSS option. */
721 ret
= tcp_enqueue_flags(pcb
, TCP_SYN
);
723 /* SYN segment was enqueued, changed the pcbs state now */
724 pcb
->state
= SYN_SENT
;
725 TCP_RMV(&tcp_bound_pcbs
, pcb
);
726 TCP_REG(&tcp_active_pcbs
, pcb
);
727 snmp_inc_tcpactiveopens();
735 * Called every 500 ms and implements the retransmission timer and the timer that
736 * removes PCBs that have been in TIME-WAIT for enough time. It also increments
737 * various timers such as the inactivity timer in each PCB.
739 * Automatically called from tcp_tmr().
744 struct tcp_pcb
*pcb
, *pcb2
, *prev
;
746 u8_t pcb_remove
; /* flag if a PCB should be removed */
747 u8_t pcb_reset
; /* flag if a RST should be sent when removing */
754 /* Steps through all of the active PCBs. */
756 pcb
= tcp_active_pcbs
;
758 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: no active pcbs\n"));
760 while (pcb
!= NULL
) {
761 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: processing active pcb\n"));
762 LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb
->state
!= CLOSED
);
763 LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb
->state
!= LISTEN
);
764 LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb
->state
!= TIME_WAIT
);
769 if (pcb
->state
== SYN_SENT
&& pcb
->nrtx
== TCP_SYNMAXRTX
) {
771 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: max SYN retries reached\n"));
773 else if (pcb
->nrtx
== TCP_MAXRTX
) {
775 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: max DATA retries reached\n"));
777 if (pcb
->persist_backoff
> 0) {
778 /* If snd_wnd is zero, use persist timer to send 1 byte probes
779 * instead of using the standard retransmission mechanism. */
781 if (pcb
->persist_cnt
>= tcp_persist_backoff
[pcb
->persist_backoff
-1]) {
782 pcb
->persist_cnt
= 0;
783 if (pcb
->persist_backoff
< sizeof(tcp_persist_backoff
)) {
784 pcb
->persist_backoff
++;
786 tcp_zero_window_probe(pcb
);
789 /* Increase the retransmission timer if it is running */
793 if (pcb
->unacked
!= NULL
&& pcb
->rtime
>= pcb
->rto
) {
794 /* Time for a retransmission. */
795 LWIP_DEBUGF(TCP_RTO_DEBUG
, ("tcp_slowtmr: rtime %"S16_F
796 " pcb->rto %"S16_F
"\n",
797 pcb
->rtime
, pcb
->rto
));
799 /* Double retransmission time-out unless we are trying to
800 * connect to somebody (i.e., we are in SYN_SENT). */
801 if (pcb
->state
!= SYN_SENT
) {
802 pcb
->rto
= ((pcb
->sa
>> 3) + pcb
->sv
) << tcp_backoff
[pcb
->nrtx
];
805 /* Reset the retransmission timer. */
808 /* Reduce congestion window and ssthresh. */
809 eff_wnd
= LWIP_MIN(pcb
->cwnd
, pcb
->snd_wnd
);
810 pcb
->ssthresh
= eff_wnd
>> 1;
811 if (pcb
->ssthresh
< (pcb
->mss
<< 1)) {
812 pcb
->ssthresh
= (pcb
->mss
<< 1);
814 pcb
->cwnd
= pcb
->mss
;
815 LWIP_DEBUGF(TCP_CWND_DEBUG
, ("tcp_slowtmr: cwnd %"U16_F
816 " ssthresh %"U16_F
"\n",
817 pcb
->cwnd
, pcb
->ssthresh
));
819 /* The following needs to be called AFTER cwnd is set to one
825 /* Check if this PCB has stayed too long in FIN-WAIT-2 */
826 if (pcb
->state
== FIN_WAIT_2
) {
827 if ((u32_t
)(tcp_ticks
- pcb
->tmr
) >
828 TCP_FIN_WAIT_TIMEOUT
/ TCP_SLOW_INTERVAL
) {
830 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
834 /* Check if KEEPALIVE should be sent */
835 if((pcb
->so_options
& SOF_KEEPALIVE
) &&
836 ((pcb
->state
== ESTABLISHED
) ||
837 (pcb
->state
== CLOSE_WAIT
))) {
838 #if LWIP_TCP_KEEPALIVE
839 if((u32_t
)(tcp_ticks
- pcb
->tmr
) >
840 (pcb
->keep_idle
+ (pcb
->keep_cnt
*pcb
->keep_intvl
))
843 if((u32_t
)(tcp_ticks
- pcb
->tmr
) >
844 (pcb
->keep_idle
+ TCP_MAXIDLE
) / TCP_SLOW_INTERVAL
)
845 #endif /* LWIP_TCP_KEEPALIVE */
847 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
".\n",
848 ip4_addr1_16(&pcb
->remote_ip
), ip4_addr2_16(&pcb
->remote_ip
),
849 ip4_addr3_16(&pcb
->remote_ip
), ip4_addr4_16(&pcb
->remote_ip
)));
854 #if LWIP_TCP_KEEPALIVE
855 else if((u32_t
)(tcp_ticks
- pcb
->tmr
) >
856 (pcb
->keep_idle
+ pcb
->keep_cnt_sent
* pcb
->keep_intvl
)
859 else if((u32_t
)(tcp_ticks
- pcb
->tmr
) >
860 (pcb
->keep_idle
+ pcb
->keep_cnt_sent
* TCP_KEEPINTVL_DEFAULT
)
862 #endif /* LWIP_TCP_KEEPALIVE */
865 pcb
->keep_cnt_sent
++;
869 /* If this PCB has queued out of sequence data, but has been
870 inactive for too long, will drop the data (it will eventually
871 be retransmitted). */
873 if (pcb
->ooseq
!= NULL
&&
874 (u32_t
)tcp_ticks
- pcb
->tmr
>= pcb
->rto
* TCP_OOSEQ_TIMEOUT
) {
875 tcp_segs_free(pcb
->ooseq
);
877 LWIP_DEBUGF(TCP_CWND_DEBUG
, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
879 #endif /* TCP_QUEUE_OOSEQ */
881 /* Check if this PCB has stayed too long in SYN-RCVD */
882 if (pcb
->state
== SYN_RCVD
) {
883 if ((u32_t
)(tcp_ticks
- pcb
->tmr
) >
884 TCP_SYN_RCVD_TIMEOUT
/ TCP_SLOW_INTERVAL
) {
886 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
890 /* Check if this PCB has stayed too long in LAST-ACK */
891 if (pcb
->state
== LAST_ACK
) {
892 if ((u32_t
)(tcp_ticks
- pcb
->tmr
) > 2 * TCP_MSL
/ TCP_SLOW_INTERVAL
) {
894 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
898 /* If the PCB should be removed, do it. */
901 /* Remove PCB from tcp_active_pcbs list. */
903 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb
!= tcp_active_pcbs
);
904 prev
->next
= pcb
->next
;
906 /* This PCB was the first. */
907 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs
== pcb
);
908 tcp_active_pcbs
= pcb
->next
;
911 TCP_EVENT_ERR(pcb
->errf
, pcb
->callback_arg
, ERR_ABRT
);
913 tcp_rst(pcb
->snd_nxt
, pcb
->rcv_nxt
, &pcb
->local_ip
, &pcb
->remote_ip
,
914 pcb
->local_port
, pcb
->remote_port
);
918 memp_free(MEMP_TCP_PCB
, pcb
);
921 /* get the 'next' element now and work with 'prev' below (in case of abort) */
925 /* We check if we should poll the connection. */
927 if (prev
->polltmr
>= prev
->pollinterval
) {
929 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_slowtmr: polling application\n"));
930 TCP_EVENT_POLL(prev
, err
);
931 /* if err == ERR_ABRT, 'prev' is already deallocated */
940 /* Steps through all of the TIME-WAIT PCBs. */
943 while (pcb
!= NULL
) {
944 LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb
->state
== TIME_WAIT
);
947 /* Check if this PCB has stayed long enough in TIME-WAIT */
948 if ((u32_t
)(tcp_ticks
- pcb
->tmr
) > 2 * TCP_MSL
/ TCP_SLOW_INTERVAL
) {
954 /* If the PCB should be removed, do it. */
957 /* Remove PCB from tcp_tw_pcbs list. */
959 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb
!= tcp_tw_pcbs
);
960 prev
->next
= pcb
->next
;
962 /* This PCB was the first. */
963 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs
== pcb
);
964 tcp_tw_pcbs
= pcb
->next
;
967 memp_free(MEMP_TCP_PCB
, pcb
);
977 * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
978 * "refused" by upper layer (application) and sends delayed ACKs.
980 * Automatically called from tcp_tmr().
985 struct tcp_pcb
*pcb
= tcp_active_pcbs
;
988 struct tcp_pcb
*next
= pcb
->next
;
989 /* If there is data which was previously "refused" by upper layer */
990 if (pcb
->refused_data
!= NULL
) {
991 /* Notify again application with data previously received. */
993 LWIP_DEBUGF(TCP_INPUT_DEBUG
, ("tcp_fasttmr: notify kept packet\n"));
994 TCP_EVENT_RECV(pcb
, pcb
->refused_data
, ERR_OK
, err
);
996 pcb
->refused_data
= NULL
;
997 } else if (err
== ERR_ABRT
) {
998 /* if err == ERR_ABRT, 'pcb' is already deallocated */
1003 /* send delayed ACKs */
1004 if (pcb
&& (pcb
->flags
& TF_ACK_DELAY
)) {
1005 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_fasttmr: delayed ACK\n"));
1008 pcb
->flags
&= ~(TF_ACK_DELAY
| TF_ACK_NOW
);
1016 * Deallocates a list of TCP segments (tcp_seg structures).
1018 * @param seg tcp_seg list of TCP segments to free
1021 tcp_segs_free(struct tcp_seg
*seg
)
1023 while (seg
!= NULL
) {
1024 struct tcp_seg
*next
= seg
->next
;
1031 * Frees a TCP segment (tcp_seg structure).
1033 * @param seg single tcp_seg to free
1036 tcp_seg_free(struct tcp_seg
*seg
)
1039 if (seg
->p
!= NULL
) {
1043 #endif /* TCP_DEBUG */
1045 memp_free(MEMP_TCP_SEG
, seg
);
1050 * Sets the priority of a connection.
1052 * @param pcb the tcp_pcb to manipulate
1053 * @param prio new priority
1056 tcp_setprio(struct tcp_pcb
*pcb
, u8_t prio
)
1063 * Returns a copy of the given TCP segment.
1064 * The pbuf and data are not copied, only the pointers
1066 * @param seg the old tcp_seg
1067 * @return a copy of seg
1070 tcp_seg_copy(struct tcp_seg
*seg
)
1072 struct tcp_seg
*cseg
;
1074 cseg
= (struct tcp_seg
*)memp_malloc(MEMP_TCP_SEG
);
1078 SMEMCPY((u8_t
*)cseg
, (const u8_t
*)seg
, sizeof(struct tcp_seg
));
1082 #endif /* TCP_QUEUE_OOSEQ */
1084 #if LWIP_CALLBACK_API
1086 * Default receive callback that is called if the user didn't register
1087 * a recv callback for the pcb.
1090 tcp_recv_null(void *arg
, struct tcp_pcb
*pcb
, struct pbuf
*p
, err_t err
)
1092 LWIP_UNUSED_ARG(arg
);
1094 tcp_recved(pcb
, p
->tot_len
);
1096 } else if (err
== ERR_OK
) {
1097 return tcp_close(pcb
);
1101 #endif /* LWIP_CALLBACK_API */
1104 * Kills the oldest active connection that has lower priority than prio.
1106 * @param prio minimum priority
1109 tcp_kill_prio(u8_t prio
)
1111 struct tcp_pcb
*pcb
, *inactive
;
1116 mprio
= TCP_PRIO_MAX
;
1118 /* We kill the oldest active connection that has lower priority than prio. */
1121 for(pcb
= tcp_active_pcbs
; pcb
!= NULL
; pcb
= pcb
->next
) {
1122 if (pcb
->prio
<= prio
&&
1123 pcb
->prio
<= mprio
&&
1124 (u32_t
)(tcp_ticks
- pcb
->tmr
) >= inactivity
) {
1125 inactivity
= tcp_ticks
- pcb
->tmr
;
1130 if (inactive
!= NULL
) {
1131 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F
")\n",
1132 (void *)inactive
, inactivity
));
1133 tcp_abort(inactive
);
1138 * Kills the oldest connection that is in TIME_WAIT state.
1139 * Called from tcp_alloc() if no more connections are available.
1142 tcp_kill_timewait(void)
1144 struct tcp_pcb
*pcb
, *inactive
;
1149 /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
1150 for(pcb
= tcp_tw_pcbs
; pcb
!= NULL
; pcb
= pcb
->next
) {
1151 if ((u32_t
)(tcp_ticks
- pcb
->tmr
) >= inactivity
) {
1152 inactivity
= tcp_ticks
- pcb
->tmr
;
1156 if (inactive
!= NULL
) {
1157 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F
")\n",
1158 (void *)inactive
, inactivity
));
1159 tcp_abort(inactive
);
1164 * Allocate a new tcp_pcb structure.
1166 * @param prio priority for the new pcb
1167 * @return a new tcp_pcb that initially is in state CLOSED
1170 tcp_alloc(u8_t prio
)
1172 struct tcp_pcb
*pcb
;
1175 pcb
= (struct tcp_pcb
*)memp_malloc(MEMP_TCP_PCB
);
1177 /* Try killing oldest connection in TIME-WAIT. */
1178 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1179 tcp_kill_timewait();
1180 /* Try to allocate a tcp_pcb again. */
1181 pcb
= (struct tcp_pcb
*)memp_malloc(MEMP_TCP_PCB
);
1183 /* Try killing active connections with lower priority than the new one. */
1184 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_alloc: killing connection with prio lower than %d\n", prio
));
1185 tcp_kill_prio(prio
);
1186 /* Try to allocate a tcp_pcb again. */
1187 pcb
= (struct tcp_pcb
*)memp_malloc(MEMP_TCP_PCB
);
1189 /* adjust err stats: memp_malloc failed twice before */
1190 MEMP_STATS_DEC(err
, MEMP_TCP_PCB
);
1194 /* adjust err stats: timewait PCB was freed above */
1195 MEMP_STATS_DEC(err
, MEMP_TCP_PCB
);
1199 memset(pcb
, 0, sizeof(struct tcp_pcb
));
1201 pcb
->snd_buf
= TCP_SND_BUF
;
1202 pcb
->snd_queuelen
= 0;
1203 pcb
->rcv_wnd
= TCP_WND
;
1204 pcb
->rcv_ann_wnd
= TCP_WND
;
1207 /* As initial send MSS, we use TCP_MSS but limit it to 536.
1208 The send MSS is updated when an MSS option is received. */
1209 pcb
->mss
= (TCP_MSS
> 536) ? 536 : TCP_MSS
;
1210 pcb
->rto
= 3000 / TCP_SLOW_INTERVAL
;
1212 pcb
->sv
= 3000 / TCP_SLOW_INTERVAL
;
1215 iss
= tcp_next_iss();
1220 pcb
->tmr
= tcp_ticks
;
1224 #if LWIP_CALLBACK_API
1225 pcb
->recv
= tcp_recv_null
;
1226 #endif /* LWIP_CALLBACK_API */
1228 /* Init KEEPALIVE timer */
1229 pcb
->keep_idle
= TCP_KEEPIDLE_DEFAULT
;
1231 #if LWIP_TCP_KEEPALIVE
1232 pcb
->keep_intvl
= TCP_KEEPINTVL_DEFAULT
;
1233 pcb
->keep_cnt
= TCP_KEEPCNT_DEFAULT
;
1234 #endif /* LWIP_TCP_KEEPALIVE */
1236 pcb
->keep_cnt_sent
= 0;
1242 * Creates a new TCP protocol control block but doesn't place it on
1243 * any of the TCP PCB lists.
1244 * The pcb is not put on any list until binding using tcp_bind().
1246 * @internal: Maybe there should be a idle TCP PCB list where these
1247 * PCBs are put on. Port reservation using tcp_bind() is implemented but
1248 * allocated pcbs that are not bound can't be killed automatically if wanting
1249 * to allocate a pcb with higher prio (@see tcp_kill_prio())
1251 * @return a new tcp_pcb that initially is in state CLOSED
1256 return tcp_alloc(TCP_PRIO_NORMAL
);
1260 * Used to specify the argument that should be passed callback
1263 * @param pcb tcp_pcb to set the callback argument
1264 * @param arg void pointer argument to pass to callback functions
1267 tcp_arg(struct tcp_pcb
*pcb
, void *arg
)
1269 pcb
->callback_arg
= arg
;
1271 #if LWIP_CALLBACK_API
1274 * Used to specify the function that should be called when a TCP
1275 * connection receives data.
1277 * @param pcb tcp_pcb to set the recv callback
1278 * @param recv callback function to call for this pcb when data is received
1281 tcp_recv(struct tcp_pcb
*pcb
, tcp_recv_fn recv
)
1287 * Used to specify the function that should be called when TCP data
1288 * has been successfully delivered to the remote host.
1290 * @param pcb tcp_pcb to set the sent callback
1291 * @param sent callback function to call for this pcb when data is successfully sent
1294 tcp_sent(struct tcp_pcb
*pcb
, tcp_sent_fn sent
)
1300 * Used to specify the function that should be called when a fatal error
1301 * has occured on the connection.
1303 * @param pcb tcp_pcb to set the err callback
1304 * @param err callback function to call for this pcb when a fatal error
1305 * has occured on the connection
1308 tcp_err(struct tcp_pcb
*pcb
, tcp_err_fn err
)
1314 * Used for specifying the function that should be called when a
1315 * LISTENing connection has been connected to another host.
1317 * @param pcb tcp_pcb to set the accept callback
1318 * @param accept callback function to call for this pcb when LISTENing
1319 * connection has been connected to another host
1322 tcp_accept(struct tcp_pcb
*pcb
, tcp_accept_fn accept
)
1324 pcb
->accept
= accept
;
1326 #endif /* LWIP_CALLBACK_API */
1330 * Used to specify the function that should be called periodically
1331 * from TCP. The interval is specified in terms of the TCP coarse
1332 * timer interval, which is called twice a second.
1336 tcp_poll(struct tcp_pcb
*pcb
, tcp_poll_fn poll
, u8_t interval
)
1338 #if LWIP_CALLBACK_API
1340 #else /* LWIP_CALLBACK_API */
1341 LWIP_UNUSED_ARG(poll
);
1342 #endif /* LWIP_CALLBACK_API */
1343 pcb
->pollinterval
= interval
;
1347 * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
1348 * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
1350 * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
1353 tcp_pcb_purge(struct tcp_pcb
*pcb
)
1355 if (pcb
->state
!= CLOSED
&&
1356 pcb
->state
!= TIME_WAIT
&&
1357 pcb
->state
!= LISTEN
) {
1359 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_pcb_purge\n"));
1361 #if TCP_LISTEN_BACKLOG
1362 if (pcb
->state
== SYN_RCVD
) {
1363 /* Need to find the corresponding listen_pcb and decrease its accepts_pending */
1364 struct tcp_pcb_listen
*lpcb
;
1365 LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL",
1366 tcp_listen_pcbs
.listen_pcbs
!= NULL
);
1367 for (lpcb
= tcp_listen_pcbs
.listen_pcbs
; lpcb
!= NULL
; lpcb
= lpcb
->next
) {
1368 if ((lpcb
->local_port
== pcb
->local_port
) &&
1369 (ip_addr_isany(&lpcb
->local_ip
) ||
1370 ip_addr_cmp(&pcb
->local_ip
, &lpcb
->local_ip
))) {
1371 /* port and address of the listen pcb match the timed-out pcb */
1372 LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending",
1373 lpcb
->accepts_pending
> 0);
1374 lpcb
->accepts_pending
--;
1379 #endif /* TCP_LISTEN_BACKLOG */
1382 if (pcb
->refused_data
!= NULL
) {
1383 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_pcb_purge: data left on ->refused_data\n"));
1384 pbuf_free(pcb
->refused_data
);
1385 pcb
->refused_data
= NULL
;
1387 if (pcb
->unsent
!= NULL
) {
1388 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_pcb_purge: not all data sent\n"));
1390 if (pcb
->unacked
!= NULL
) {
1391 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_pcb_purge: data left on ->unacked\n"));
1394 if (pcb
->ooseq
!= NULL
) {
1395 LWIP_DEBUGF(TCP_DEBUG
, ("tcp_pcb_purge: data left on ->ooseq\n"));
1397 tcp_segs_free(pcb
->ooseq
);
1399 #endif /* TCP_QUEUE_OOSEQ */
1401 /* Stop the retransmission timer as it will expect data on unacked
1402 queue if it fires */
1405 tcp_segs_free(pcb
->unsent
);
1406 tcp_segs_free(pcb
->unacked
);
1407 pcb
->unacked
= pcb
->unsent
= NULL
;
1409 pcb
->unsent_oversize
= 0;
1410 #endif /* TCP_OVERSIZE */
1415 * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
1417 * @param pcblist PCB list to purge.
1418 * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
1421 tcp_pcb_remove(struct tcp_pcb
**pcblist
, struct tcp_pcb
*pcb
)
1423 TCP_RMV(pcblist
, pcb
);
1427 /* if there is an outstanding delayed ACKs, send it */
1428 if (pcb
->state
!= TIME_WAIT
&&
1429 pcb
->state
!= LISTEN
&&
1430 pcb
->flags
& TF_ACK_DELAY
) {
1431 pcb
->flags
|= TF_ACK_NOW
;
1435 if (pcb
->state
!= LISTEN
) {
1436 LWIP_ASSERT("unsent segments leaking", pcb
->unsent
== NULL
);
1437 LWIP_ASSERT("unacked segments leaking", pcb
->unacked
== NULL
);
1439 LWIP_ASSERT("ooseq segments leaking", pcb
->ooseq
== NULL
);
1440 #endif /* TCP_QUEUE_OOSEQ */
1443 pcb
->state
= CLOSED
;
1445 LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1449 * Calculates a new initial sequence number for new connections.
1451 * @return u32_t pseudo random sequence number
1456 static u32_t iss
= 6510;
1458 iss
+= tcp_ticks
; /* XXX */
1462 #if TCP_CALCULATE_EFF_SEND_MSS
1464 * Calcluates the effective send mss that can be used for a specific IP address
1465 * by using ip_route to determin the netif used to send to the address and
1466 * calculating the minimum of TCP_MSS and that netif's mtu (if set).
1469 tcp_eff_send_mss(u16_t sendmss
, ip_addr_t
*addr
)
1472 struct netif
*outif
;
1474 outif
= ip_route(addr
);
1475 if ((outif
!= NULL
) && (outif
->mtu
!= 0)) {
1476 mss_s
= outif
->mtu
- IP_HLEN
- TCP_HLEN
;
1477 /* RFC 1122, chap 4.2.2.6:
1478 * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1479 * We correct for TCP options in tcp_write(), and don't support IP options.
1481 sendmss
= LWIP_MIN(sendmss
, mss_s
);
1485 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1488 tcp_debug_state_str(enum tcp_state s
)
1490 return tcp_state_str
[s
];
1493 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
1495 * Print a tcp header for debugging purposes.
1497 * @param tcphdr pointer to a struct tcp_hdr
1500 tcp_debug_print(struct tcp_hdr
*tcphdr
)
1502 LWIP_DEBUGF(TCP_DEBUG
, ("TCP header:\n"));
1503 LWIP_DEBUGF(TCP_DEBUG
, ("+-------------------------------+\n"));
1504 LWIP_DEBUGF(TCP_DEBUG
, ("| %5"U16_F
" | %5"U16_F
" | (src port, dest port)\n",
1505 ntohs(tcphdr
->src
), ntohs(tcphdr
->dest
)));
1506 LWIP_DEBUGF(TCP_DEBUG
, ("+-------------------------------+\n"));
1507 LWIP_DEBUGF(TCP_DEBUG
, ("| %010"U32_F
" | (seq no)\n",
1508 ntohl(tcphdr
->seqno
)));
1509 LWIP_DEBUGF(TCP_DEBUG
, ("+-------------------------------+\n"));
1510 LWIP_DEBUGF(TCP_DEBUG
, ("| %010"U32_F
" | (ack no)\n",
1511 ntohl(tcphdr
->ackno
)));
1512 LWIP_DEBUGF(TCP_DEBUG
, ("+-------------------------------+\n"));
1513 LWIP_DEBUGF(TCP_DEBUG
, ("| %2"U16_F
" | |%"U16_F
"%"U16_F
"%"U16_F
"%"U16_F
"%"U16_F
"%"U16_F
"| %5"U16_F
" | (hdrlen, flags (",
1514 TCPH_HDRLEN(tcphdr
),
1515 TCPH_FLAGS(tcphdr
) >> 5 & 1,
1516 TCPH_FLAGS(tcphdr
) >> 4 & 1,
1517 TCPH_FLAGS(tcphdr
) >> 3 & 1,
1518 TCPH_FLAGS(tcphdr
) >> 2 & 1,
1519 TCPH_FLAGS(tcphdr
) >> 1 & 1,
1520 TCPH_FLAGS(tcphdr
) & 1,
1521 ntohs(tcphdr
->wnd
)));
1522 tcp_debug_print_flags(TCPH_FLAGS(tcphdr
));
1523 LWIP_DEBUGF(TCP_DEBUG
, ("), win)\n"));
1524 LWIP_DEBUGF(TCP_DEBUG
, ("+-------------------------------+\n"));
1525 LWIP_DEBUGF(TCP_DEBUG
, ("| 0x%04"X16_F
" | %5"U16_F
" | (chksum, urgp)\n",
1526 ntohs(tcphdr
->chksum
), ntohs(tcphdr
->urgp
)));
1527 LWIP_DEBUGF(TCP_DEBUG
, ("+-------------------------------+\n"));
1531 * Print a tcp state for debugging purposes.
1533 * @param s enum tcp_state to print
1536 tcp_debug_print_state(enum tcp_state s
)
1538 LWIP_DEBUGF(TCP_DEBUG
, ("State: %s\n", tcp_state_str
[s
]));
1542 * Print tcp flags for debugging purposes.
1544 * @param flags tcp flags, all active flags are printed
1547 tcp_debug_print_flags(u8_t flags
)
1549 if (flags
& TCP_FIN
) {
1550 LWIP_DEBUGF(TCP_DEBUG
, ("FIN "));
1552 if (flags
& TCP_SYN
) {
1553 LWIP_DEBUGF(TCP_DEBUG
, ("SYN "));
1555 if (flags
& TCP_RST
) {
1556 LWIP_DEBUGF(TCP_DEBUG
, ("RST "));
1558 if (flags
& TCP_PSH
) {
1559 LWIP_DEBUGF(TCP_DEBUG
, ("PSH "));
1561 if (flags
& TCP_ACK
) {
1562 LWIP_DEBUGF(TCP_DEBUG
, ("ACK "));
1564 if (flags
& TCP_URG
) {
1565 LWIP_DEBUGF(TCP_DEBUG
, ("URG "));
1567 if (flags
& TCP_ECE
) {
1568 LWIP_DEBUGF(TCP_DEBUG
, ("ECE "));
1570 if (flags
& TCP_CWR
) {
1571 LWIP_DEBUGF(TCP_DEBUG
, ("CWR "));
1573 LWIP_DEBUGF(TCP_DEBUG
, ("\n"));
1577 * Print all tcp_pcbs in every list for debugging purposes.
1580 tcp_debug_print_pcbs(void)
1582 struct tcp_pcb
*pcb
;
1583 LWIP_DEBUGF(TCP_DEBUG
, ("Active PCB states:\n"));
1584 for(pcb
= tcp_active_pcbs
; pcb
!= NULL
; pcb
= pcb
->next
) {
1585 LWIP_DEBUGF(TCP_DEBUG
, ("Local port %"U16_F
", foreign port %"U16_F
" snd_nxt %"U32_F
" rcv_nxt %"U32_F
" ",
1586 pcb
->local_port
, pcb
->remote_port
,
1587 pcb
->snd_nxt
, pcb
->rcv_nxt
));
1588 tcp_debug_print_state(pcb
->state
);
1590 LWIP_DEBUGF(TCP_DEBUG
, ("Listen PCB states:\n"));
1591 for(pcb
= (struct tcp_pcb
*)tcp_listen_pcbs
.pcbs
; pcb
!= NULL
; pcb
= pcb
->next
) {
1592 LWIP_DEBUGF(TCP_DEBUG
, ("Local port %"U16_F
", foreign port %"U16_F
" snd_nxt %"U32_F
" rcv_nxt %"U32_F
" ",
1593 pcb
->local_port
, pcb
->remote_port
,
1594 pcb
->snd_nxt
, pcb
->rcv_nxt
));
1595 tcp_debug_print_state(pcb
->state
);
1597 LWIP_DEBUGF(TCP_DEBUG
, ("TIME-WAIT PCB states:\n"));
1598 for(pcb
= tcp_tw_pcbs
; pcb
!= NULL
; pcb
= pcb
->next
) {
1599 LWIP_DEBUGF(TCP_DEBUG
, ("Local port %"U16_F
", foreign port %"U16_F
" snd_nxt %"U32_F
" rcv_nxt %"U32_F
" ",
1600 pcb
->local_port
, pcb
->remote_port
,
1601 pcb
->snd_nxt
, pcb
->rcv_nxt
));
1602 tcp_debug_print_state(pcb
->state
);
1607 * Check state consistency of the tcp_pcb lists.
1612 struct tcp_pcb
*pcb
;
1613 for(pcb
= tcp_active_pcbs
; pcb
!= NULL
; pcb
= pcb
->next
) {
1614 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb
->state
!= CLOSED
);
1615 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb
->state
!= LISTEN
);
1616 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb
->state
!= TIME_WAIT
);
1618 for(pcb
= tcp_tw_pcbs
; pcb
!= NULL
; pcb
= pcb
->next
) {
1619 LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb
->state
== TIME_WAIT
);
1623 #endif /* TCP_DEBUG */
1625 #endif /* LWIP_TCP */