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 *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 TCP_RMV(&tcp_bound_pcbs, pcb);
177 memp_free(MEMP_TCP_PCB, pcb);
182 tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
183 memp_free(MEMP_TCP_PCB_LISTEN, pcb);
188 tcp_pcb_remove(&tcp_active_pcbs, pcb);
189 memp_free(MEMP_TCP_PCB, pcb);
191 snmp_inc_tcpattemptfails();
194 err = tcp_send_fin(pcb);
196 snmp_inc_tcpattemptfails();
197 pcb->state = FIN_WAIT_1;
201 err = tcp_send_fin(pcb);
203 snmp_inc_tcpestabresets();
204 pcb->state = FIN_WAIT_1;
208 err = tcp_send_fin(pcb);
210 snmp_inc_tcpestabresets();
211 pcb->state = LAST_ACK;
215 /* Has already been closed, do nothing. */
221 if (pcb != NULL && err == ERR_OK) {
222 /* To ensure all data has been sent when tcp_close returns, we have
223 to make sure tcp_output doesn't fail.
224 Since we don't really have to ensure all data has been sent when tcp_close
225 returns (unsent data is sent from tcp timer functions, also), we don't care
226 for the return value of tcp_output for now. */
227 /* @todo: When implementing SO_LINGER, this must be changed somehow:
228 If SOF_LINGER is set, the data should be sent and acked before close returns.
229 This can only be valid for sequential APIs, not for the raw API. */
236 * Closes the connection held by the PCB.
238 * Listening pcbs are freed and may not be referenced any more.
239 * Connection pcbs are freed if not yet connected and may not be referenced
240 * any more. If a connection is established (at least SYN received or in
241 * a closing state), the connection is closed, and put in a closing state.
242 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
243 * unsafe to reference it (unless an error is returned).
245 * @param pcb the tcp_pcb to close
246 * @return ERR_OK if connection has been closed
247 * another err_t if closing failed and pcb is not freed
250 tcp_close(struct tcp_pcb *pcb)
253 LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
254 tcp_debug_print_state(pcb->state);
255 #endif /* TCP_DEBUG */
257 if (pcb->state != LISTEN) {
258 /* Set a flag not to receive any more data... */
259 pcb->flags |= TF_RXCLOSED;
262 return tcp_close_shutdown(pcb, 1);
266 * Causes all or part of a full-duplex connection of this PCB to be shut down.
267 * This doesn't deallocate the PCB!
269 * @param pcb PCB to shutdown
270 * @param shut_rx shut down receive side if this is != 0
271 * @param shut_tx shut down send side if this is != 0
272 * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down)
273 * another err_t on error.
276 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
278 if (pcb->state == LISTEN) {
282 /* shut down the receive side: free buffered data... */
283 if (pcb->refused_data != NULL) {
284 pbuf_free(pcb->refused_data);
285 pcb->refused_data = NULL;
287 /* ... and set a flag not to receive any more data */
288 pcb->flags |= TF_RXCLOSED;
291 /* This can't happen twice since if it succeeds, the pcb's state is changed.
292 Only close in these states as the others directly deallocate the PCB */
293 switch (pcb->state) {
297 return tcp_close_shutdown(pcb, 0);
299 /* don't shut down other states */
303 /* @todo: return another err_t if not in correct state or already shut? */
308 * Abandons a connection and optionally sends a RST to the remote
309 * host. Deletes the local protocol control block. This is done when
310 * a connection is killed because of shortage of memory.
312 * @param pcb the tcp_pcb to abort
313 * @param reset boolean to indicate whether a reset should be sent
316 tcp_abandon(struct tcp_pcb *pcb, int reset)
319 u16_t remote_port, local_port;
320 ip_addr_t remote_ip, local_ip;
321 #if LWIP_CALLBACK_API
323 #endif /* LWIP_CALLBACK_API */
326 /* pcb->state LISTEN not allowed here */
327 LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
328 pcb->state != LISTEN);
329 /* Figure out on which TCP PCB list we are, and remove us. If we
330 are in an active state, call the receive function associated with
331 the PCB with a NULL argument, and send an RST to the remote end. */
332 if (pcb->state == TIME_WAIT) {
333 tcp_pcb_remove(&tcp_tw_pcbs, pcb);
334 memp_free(MEMP_TCP_PCB, pcb);
336 seqno = pcb->snd_nxt;
337 ackno = pcb->rcv_nxt;
338 ip_addr_copy(local_ip, pcb->local_ip);
339 ip_addr_copy(remote_ip, pcb->remote_ip);
340 local_port = pcb->local_port;
341 remote_port = pcb->remote_port;
342 #if LWIP_CALLBACK_API
344 #endif /* LWIP_CALLBACK_API */
345 errf_arg = pcb->callback_arg;
346 tcp_pcb_remove(&tcp_active_pcbs, pcb);
347 if (pcb->unacked != NULL) {
348 tcp_segs_free(pcb->unacked);
350 if (pcb->unsent != NULL) {
351 tcp_segs_free(pcb->unsent);
354 if (pcb->ooseq != NULL) {
355 tcp_segs_free(pcb->ooseq);
357 #endif /* TCP_QUEUE_OOSEQ */
358 memp_free(MEMP_TCP_PCB, pcb);
359 TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
361 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
362 tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port);
368 * Aborts the connection by sending a RST (reset) segment to the remote
369 * host. The pcb is deallocated. This function never fails.
371 * ATTENTION: When calling this from one of the TCP callbacks, make
372 * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise
373 * or you will risk accessing deallocated memory or memory leaks!
375 * @param pcb the tcp pcb to abort
378 tcp_abort(struct tcp_pcb *pcb)
384 * Binds the connection to a local portnumber and IP address. If the
385 * IP address is not given (i.e., ipaddr == NULL), the IP address of
386 * the outgoing network interface is used instead.
388 * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
390 * @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind
391 * to any local address
392 * @param port the local port to bind to
393 * @return ERR_USE if the port is already in use
397 tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
400 int max_pcb_list = NUM_TCP_PCB_LISTS;
401 struct tcp_pcb *cpcb;
403 LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
406 /* Unless the REUSEADDR flag is set,
407 we have to check the pcbs in TIME-WAIT state, also.
408 We do not dump TIME_WAIT pcb's; they can still be matched by incoming
409 packets using both local and remote IP addresses and ports to distinguish.
412 if ((pcb->so_options & SOF_REUSEADDR) != 0) {
413 max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
415 #endif /* SO_REUSE */
416 #endif /* SO_REUSE */
419 port = tcp_new_port();
422 /* Check if the address already is in use (on all lists) */
423 for (i = 0; i < max_pcb_list; i++) {
424 for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
425 if (cpcb->local_port == port) {
427 /* Omit checking for the same port if both pcbs have REUSEADDR set.
428 For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
430 if (((pcb->so_options & SOF_REUSEADDR) == 0) ||
431 ((cpcb->so_options & SOF_REUSEADDR) == 0))
432 #endif /* SO_REUSE */
434 if (ip_addr_isany(&(cpcb->local_ip)) ||
435 ip_addr_isany(ipaddr) ||
436 ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
444 if (!ip_addr_isany(ipaddr)) {
445 pcb->local_ip = *ipaddr;
447 pcb->local_port = port;
448 TCP_REG(&tcp_bound_pcbs, pcb);
449 LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
452 #if LWIP_CALLBACK_API
454 * Default accept callback if no accept callback is specified by the user.
457 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
459 LWIP_UNUSED_ARG(arg);
460 LWIP_UNUSED_ARG(pcb);
461 LWIP_UNUSED_ARG(err);
465 #endif /* LWIP_CALLBACK_API */
468 * Set the state of the connection to be LISTEN, which means that it
469 * is able to accept incoming connections. The protocol control block
470 * is reallocated in order to consume less memory. Setting the
471 * connection to LISTEN is an irreversible process.
473 * @param pcb the original tcp_pcb
474 * @param backlog the incoming connections queue limit
475 * @return tcp_pcb used for listening, consumes less memory.
477 * @note The original tcp_pcb is freed. This function therefore has to be
479 * tpcb = tcp_listen(tpcb);
482 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
484 struct tcp_pcb_listen *lpcb;
486 LWIP_UNUSED_ARG(backlog);
487 LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL);
489 /* already listening? */
490 if (pcb->state == LISTEN) {
494 if ((pcb->so_options & SOF_REUSEADDR) != 0) {
495 /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
496 is declared (listen-/connection-pcb), we have to make sure now that
497 this port is only used once for every local IP. */
498 for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
499 if (lpcb->local_port == pcb->local_port) {
500 if (ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
501 /* this address/port is already used */
507 #endif /* SO_REUSE */
508 lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
512 lpcb->callback_arg = pcb->callback_arg;
513 lpcb->local_port = pcb->local_port;
514 lpcb->state = LISTEN;
515 lpcb->prio = pcb->prio;
516 lpcb->so_options = pcb->so_options;
517 lpcb->so_options |= SOF_ACCEPTCONN;
518 lpcb->ttl = pcb->ttl;
519 lpcb->tos = pcb->tos;
520 ip_addr_copy(lpcb->local_ip, pcb->local_ip);
521 TCP_RMV(&tcp_bound_pcbs, pcb);
522 memp_free(MEMP_TCP_PCB, pcb);
523 #if LWIP_CALLBACK_API
524 lpcb->accept = tcp_accept_null;
525 #endif /* LWIP_CALLBACK_API */
526 #if TCP_LISTEN_BACKLOG
527 lpcb->accepts_pending = 0;
528 lpcb->backlog = (backlog ? backlog : 1);
529 #endif /* TCP_LISTEN_BACKLOG */
530 TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
531 return (struct tcp_pcb *)lpcb;
535 * Update the state that tracks the available window space to advertise.
537 * Returns how much extra window would be advertised if we sent an
540 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
542 u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
544 if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
545 /* we can advertise more window */
546 pcb->rcv_ann_wnd = pcb->rcv_wnd;
547 return new_right_edge - pcb->rcv_ann_right_edge;
549 if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
550 /* Can happen due to other end sending out of advertised window,
551 * but within actual available (but not yet advertised) window */
552 pcb->rcv_ann_wnd = 0;
554 /* keep the right edge of window constant */
555 u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
556 LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
557 pcb->rcv_ann_wnd = (u16_t)new_rcv_ann_wnd;
564 * This function should be called by the application when it has
565 * processed the data. The purpose is to advertise a larger window
566 * when the data has been processed.
568 * @param pcb the tcp_pcb for which data is read
569 * @param len the amount of bytes that have been read by the application
572 tcp_recved(struct tcp_pcb *pcb, u16_t len)
576 LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n",
577 len <= 0xffff - pcb->rcv_wnd );
580 if (pcb->rcv_wnd > TCP_WND) {
581 pcb->rcv_wnd = TCP_WND;
584 wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
586 /* If the change in the right edge of window is significant (default
587 * watermark is TCP_WND/4), then send an explicit update now.
588 * Otherwise wait for a packet to be sent in the normal course of
589 * events (or more window to be available later) */
590 if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
595 LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n",
596 len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd));
600 * A nastly hack featuring 'goto' statements that allocates a
601 * new TCP local port.
603 * @return a new (free) local TCP port number
610 #ifndef TCP_LOCAL_PORT_RANGE_START
611 #define TCP_LOCAL_PORT_RANGE_START 4096
612 #define TCP_LOCAL_PORT_RANGE_END 0x7fff
614 static u16_t port = TCP_LOCAL_PORT_RANGE_START;
617 if (++port > TCP_LOCAL_PORT_RANGE_END) {
618 port = TCP_LOCAL_PORT_RANGE_START;
620 /* Check all PCB lists. */
621 for (i = 1; i < NUM_TCP_PCB_LISTS; i++) {
622 for(pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
623 if (pcb->local_port == port) {
632 * Connects to another host. The function given as the "connected"
633 * argument will be called when the connection has been established.
635 * @param pcb the tcp_pcb used to establish the connection
636 * @param ipaddr the remote ip address to connect to
637 * @param port the remote tcp port to connect to
638 * @param connected callback function to call when connected (or on error)
639 * @return ERR_VAL if invalid arguments are given
640 * ERR_OK if connect request has been sent
641 * other err_t values if connect request couldn't be sent
644 tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port,
645 tcp_connected_fn connected)
650 LWIP_ERROR("tcp_connect: can only connected from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
652 LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
653 if (ipaddr != NULL) {
654 pcb->remote_ip = *ipaddr;
658 pcb->remote_port = port;
660 /* check if we have a route to the remote host */
661 if (ip_addr_isany(&(pcb->local_ip))) {
662 /* no local IP address set, yet. */
663 struct netif *netif = ip_route(&(pcb->remote_ip));
665 /* Don't even try to send a SYN packet if we have no route
666 since that will fail. */
669 /* Use the netif's IP address as local address. */
670 ip_addr_copy(pcb->local_ip, netif->ip_addr);
673 if (pcb->local_port == 0) {
674 pcb->local_port = tcp_new_port();
677 if ((pcb->so_options & SOF_REUSEADDR) != 0) {
678 /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
679 now that the 5-tuple is unique. */
680 struct tcp_pcb *cpcb;
682 /* Don't check listen PCBs, check bound-, active- and TIME-WAIT PCBs. */
683 for (i = 1; i < NUM_TCP_PCB_LISTS; i++) {
684 for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
685 if ((cpcb->local_port == pcb->local_port) &&
686 (cpcb->remote_port == port) &&
687 ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
688 ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
689 /* linux returns EISCONN here, but ERR_USE should be OK for us */
695 #endif /* SO_REUSE */
696 iss = tcp_next_iss();
699 pcb->lastack = iss - 1;
700 pcb->snd_lbb = iss - 1;
701 pcb->rcv_wnd = TCP_WND;
702 pcb->rcv_ann_wnd = TCP_WND;
703 pcb->rcv_ann_right_edge = pcb->rcv_nxt;
704 pcb->snd_wnd = TCP_WND;
705 /* As initial send MSS, we use TCP_MSS but limit it to 536.
706 The send MSS is updated when an MSS option is received. */
707 pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
708 #if TCP_CALCULATE_EFF_SEND_MSS
709 pcb->mss = tcp_eff_send_mss(pcb->mss, ipaddr);
710 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
712 pcb->ssthresh = pcb->mss * 10;
713 #if LWIP_CALLBACK_API
714 pcb->connected = connected;
715 #else /* LWIP_CALLBACK_API */
716 LWIP_UNUSED_ARG(connected);
717 #endif /* LWIP_CALLBACK_API */
719 /* Send a SYN together with the MSS option. */
720 ret = tcp_enqueue_flags(pcb, TCP_SYN);
722 /* SYN segment was enqueued, changed the pcbs state now */
723 pcb->state = SYN_SENT;
724 TCP_RMV(&tcp_bound_pcbs, pcb);
725 TCP_REG(&tcp_active_pcbs, pcb);
726 snmp_inc_tcpactiveopens();
734 * Called every 500 ms and implements the retransmission timer and the timer that
735 * removes PCBs that have been in TIME-WAIT for enough time. It also increments
736 * various timers such as the inactivity timer in each PCB.
738 * Automatically called from tcp_tmr().
743 struct tcp_pcb *pcb, *pcb2, *prev;
745 u8_t pcb_remove; /* flag if a PCB should be removed */
746 u8_t pcb_reset; /* flag if a RST should be sent when removing */
753 /* Steps through all of the active PCBs. */
755 pcb = tcp_active_pcbs;
757 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
759 while (pcb != NULL) {
760 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
761 LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
762 LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
763 LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
768 if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
770 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
772 else if (pcb->nrtx == TCP_MAXRTX) {
774 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
776 if (pcb->persist_backoff > 0) {
777 /* If snd_wnd is zero, use persist timer to send 1 byte probes
778 * instead of using the standard retransmission mechanism. */
780 if (pcb->persist_cnt >= tcp_persist_backoff[pcb->persist_backoff-1]) {
781 pcb->persist_cnt = 0;
782 if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
783 pcb->persist_backoff++;
785 tcp_zero_window_probe(pcb);
788 /* Increase the retransmission timer if it is running */
792 if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
793 /* Time for a retransmission. */
794 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
795 " pcb->rto %"S16_F"\n",
796 pcb->rtime, pcb->rto));
798 /* Double retransmission time-out unless we are trying to
799 * connect to somebody (i.e., we are in SYN_SENT). */
800 if (pcb->state != SYN_SENT) {
801 pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx];
804 /* Reset the retransmission timer. */
807 /* Reduce congestion window and ssthresh. */
808 eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
809 pcb->ssthresh = eff_wnd >> 1;
810 if (pcb->ssthresh < (pcb->mss << 1)) {
811 pcb->ssthresh = (pcb->mss << 1);
813 pcb->cwnd = pcb->mss;
814 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F
815 " ssthresh %"U16_F"\n",
816 pcb->cwnd, pcb->ssthresh));
818 /* The following needs to be called AFTER cwnd is set to one
824 /* Check if this PCB has stayed too long in FIN-WAIT-2 */
825 if (pcb->state == FIN_WAIT_2) {
826 if ((u32_t)(tcp_ticks - pcb->tmr) >
827 TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
829 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
833 /* Check if KEEPALIVE should be sent */
834 if((pcb->so_options & SOF_KEEPALIVE) &&
835 ((pcb->state == ESTABLISHED) ||
836 (pcb->state == CLOSE_WAIT))) {
837 #if LWIP_TCP_KEEPALIVE
838 if((u32_t)(tcp_ticks - pcb->tmr) >
839 (pcb->keep_idle + (pcb->keep_cnt*pcb->keep_intvl))
842 if((u32_t)(tcp_ticks - pcb->tmr) >
843 (pcb->keep_idle + TCP_MAXIDLE) / TCP_SLOW_INTERVAL)
844 #endif /* LWIP_TCP_KEEPALIVE */
846 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F".%"U16_F".%"U16_F".%"U16_F".\n",
847 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
848 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
853 #if LWIP_TCP_KEEPALIVE
854 else if((u32_t)(tcp_ticks - pcb->tmr) >
855 (pcb->keep_idle + pcb->keep_cnt_sent * pcb->keep_intvl)
858 else if((u32_t)(tcp_ticks - pcb->tmr) >
859 (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEPINTVL_DEFAULT)
861 #endif /* LWIP_TCP_KEEPALIVE */
864 pcb->keep_cnt_sent++;
868 /* If this PCB has queued out of sequence data, but has been
869 inactive for too long, will drop the data (it will eventually
870 be retransmitted). */
872 if (pcb->ooseq != NULL &&
873 (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
874 tcp_segs_free(pcb->ooseq);
876 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
878 #endif /* TCP_QUEUE_OOSEQ */
880 /* Check if this PCB has stayed too long in SYN-RCVD */
881 if (pcb->state == SYN_RCVD) {
882 if ((u32_t)(tcp_ticks - pcb->tmr) >
883 TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
885 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
889 /* Check if this PCB has stayed too long in LAST-ACK */
890 if (pcb->state == LAST_ACK) {
891 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
893 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
897 /* If the PCB should be removed, do it. */
900 /* Remove PCB from tcp_active_pcbs list. */
902 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
903 prev->next = pcb->next;
905 /* This PCB was the first. */
906 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
907 tcp_active_pcbs = pcb->next;
910 TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT);
912 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
913 pcb->local_port, pcb->remote_port);
917 memp_free(MEMP_TCP_PCB, pcb);
920 /* get the 'next' element now and work with 'prev' below (in case of abort) */
924 /* We check if we should poll the connection. */
926 if (prev->polltmr >= prev->pollinterval) {
928 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
929 TCP_EVENT_POLL(prev, err);
930 /* if err == ERR_ABRT, 'prev' is already deallocated */
939 /* Steps through all of the TIME-WAIT PCBs. */
942 while (pcb != NULL) {
943 LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
946 /* Check if this PCB has stayed long enough in TIME-WAIT */
947 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
953 /* If the PCB should be removed, do it. */
956 /* Remove PCB from tcp_tw_pcbs list. */
958 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
959 prev->next = pcb->next;
961 /* This PCB was the first. */
962 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
963 tcp_tw_pcbs = pcb->next;
966 memp_free(MEMP_TCP_PCB, pcb);
976 * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
977 * "refused" by upper layer (application) and sends delayed ACKs.
979 * Automatically called from tcp_tmr().
984 struct tcp_pcb *pcb = tcp_active_pcbs;
987 struct tcp_pcb *next = pcb->next;
988 /* If there is data which was previously "refused" by upper layer */
989 if (pcb->refused_data != NULL) {
990 /* Notify again application with data previously received. */
992 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_fasttmr: notify kept packet\n"));
993 TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err);
995 pcb->refused_data = NULL;
996 } else if (err == ERR_ABRT) {
997 /* if err == ERR_ABRT, 'pcb' is already deallocated */
1002 /* send delayed ACKs */
1003 if (pcb && (pcb->flags & TF_ACK_DELAY)) {
1004 LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
1007 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1015 * Deallocates a list of TCP segments (tcp_seg structures).
1017 * @param seg tcp_seg list of TCP segments to free
1020 tcp_segs_free(struct tcp_seg *seg)
1022 while (seg != NULL) {
1023 struct tcp_seg *next = seg->next;
1030 * Frees a TCP segment (tcp_seg structure).
1032 * @param seg single tcp_seg to free
1035 tcp_seg_free(struct tcp_seg *seg)
1038 if (seg->p != NULL) {
1042 #endif /* TCP_DEBUG */
1044 memp_free(MEMP_TCP_SEG, seg);
1049 * Sets the priority of a connection.
1051 * @param pcb the tcp_pcb to manipulate
1052 * @param prio new priority
1055 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
1062 * Returns a copy of the given TCP segment.
1063 * The pbuf and data are not copied, only the pointers
1065 * @param seg the old tcp_seg
1066 * @return a copy of seg
1069 tcp_seg_copy(struct tcp_seg *seg)
1071 struct tcp_seg *cseg;
1073 cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
1077 SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
1081 #endif /* TCP_QUEUE_OOSEQ */
1083 #if LWIP_CALLBACK_API
1085 * Default receive callback that is called if the user didn't register
1086 * a recv callback for the pcb.
1089 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1091 LWIP_UNUSED_ARG(arg);
1093 tcp_recved(pcb, p->tot_len);
1095 } else if (err == ERR_OK) {
1096 return tcp_close(pcb);
1100 #endif /* LWIP_CALLBACK_API */
1103 * Kills the oldest active connection that has lower priority than prio.
1105 * @param prio minimum priority
1108 tcp_kill_prio(u8_t prio)
1110 struct tcp_pcb *pcb, *inactive;
1115 mprio = TCP_PRIO_MAX;
1117 /* We kill the oldest active connection that has lower priority than prio. */
1120 for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1121 if (pcb->prio <= prio &&
1122 pcb->prio <= mprio &&
1123 (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1124 inactivity = tcp_ticks - pcb->tmr;
1129 if (inactive != NULL) {
1130 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
1131 (void *)inactive, inactivity));
1132 tcp_abort(inactive);
1137 * Kills the oldest connection that is in TIME_WAIT state.
1138 * Called from tcp_alloc() if no more connections are available.
1141 tcp_kill_timewait(void)
1143 struct tcp_pcb *pcb, *inactive;
1148 /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
1149 for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1150 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1151 inactivity = tcp_ticks - pcb->tmr;
1155 if (inactive != NULL) {
1156 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
1157 (void *)inactive, inactivity));
1158 tcp_abort(inactive);
1163 * Allocate a new tcp_pcb structure.
1165 * @param prio priority for the new pcb
1166 * @return a new tcp_pcb that initially is in state CLOSED
1169 tcp_alloc(u8_t prio)
1171 struct tcp_pcb *pcb;
1174 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1176 /* Try killing oldest connection in TIME-WAIT. */
1177 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1178 tcp_kill_timewait();
1179 /* Try to allocate a tcp_pcb again. */
1180 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1182 /* Try killing active connections with lower priority than the new one. */
1183 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
1184 tcp_kill_prio(prio);
1185 /* Try to allocate a tcp_pcb again. */
1186 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1188 /* adjust err stats: memp_malloc failed twice before */
1189 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1193 /* adjust err stats: timewait PCB was freed above */
1194 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1198 memset(pcb, 0, sizeof(struct tcp_pcb));
1200 pcb->snd_buf = TCP_SND_BUF;
1201 pcb->snd_queuelen = 0;
1202 pcb->rcv_wnd = TCP_WND;
1203 pcb->rcv_ann_wnd = TCP_WND;
1206 /* As initial send MSS, we use TCP_MSS but limit it to 536.
1207 The send MSS is updated when an MSS option is received. */
1208 pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
1209 pcb->rto = 3000 / TCP_SLOW_INTERVAL;
1211 pcb->sv = 3000 / TCP_SLOW_INTERVAL;
1214 iss = tcp_next_iss();
1219 pcb->tmr = tcp_ticks;
1223 #if LWIP_CALLBACK_API
1224 pcb->recv = tcp_recv_null;
1225 #endif /* LWIP_CALLBACK_API */
1227 /* Init KEEPALIVE timer */
1228 pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
1230 #if LWIP_TCP_KEEPALIVE
1231 pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1232 pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
1233 #endif /* LWIP_TCP_KEEPALIVE */
1235 pcb->keep_cnt_sent = 0;
1241 * Creates a new TCP protocol control block but doesn't place it on
1242 * any of the TCP PCB lists.
1243 * The pcb is not put on any list until binding using tcp_bind().
1245 * @internal: Maybe there should be a idle TCP PCB list where these
1246 * PCBs are put on. Port reservation using tcp_bind() is implemented but
1247 * allocated pcbs that are not bound can't be killed automatically if wanting
1248 * to allocate a pcb with higher prio (@see tcp_kill_prio())
1250 * @return a new tcp_pcb that initially is in state CLOSED
1255 return tcp_alloc(TCP_PRIO_NORMAL);
1259 * Used to specify the argument that should be passed callback
1262 * @param pcb tcp_pcb to set the callback argument
1263 * @param arg void pointer argument to pass to callback functions
1266 tcp_arg(struct tcp_pcb *pcb, void *arg)
1268 pcb->callback_arg = arg;
1270 #if LWIP_CALLBACK_API
1273 * Used to specify the function that should be called when a TCP
1274 * connection receives data.
1276 * @param pcb tcp_pcb to set the recv callback
1277 * @param recv callback function to call for this pcb when data is received
1280 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
1286 * Used to specify the function that should be called when TCP data
1287 * has been successfully delivered to the remote host.
1289 * @param pcb tcp_pcb to set the sent callback
1290 * @param sent callback function to call for this pcb when data is successfully sent
1293 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
1299 * Used to specify the function that should be called when a fatal error
1300 * has occured on the connection.
1302 * @param pcb tcp_pcb to set the err callback
1303 * @param err callback function to call for this pcb when a fatal error
1304 * has occured on the connection
1307 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
1313 * Used for specifying the function that should be called when a
1314 * LISTENing connection has been connected to another host.
1316 * @param pcb tcp_pcb to set the accept callback
1317 * @param accept callback function to call for this pcb when LISTENing
1318 * connection has been connected to another host
1321 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
1323 pcb->accept = accept;
1325 #endif /* LWIP_CALLBACK_API */
1329 * Used to specify the function that should be called periodically
1330 * from TCP. The interval is specified in terms of the TCP coarse
1331 * timer interval, which is called twice a second.
1335 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1337 #if LWIP_CALLBACK_API
1339 #else /* LWIP_CALLBACK_API */
1340 LWIP_UNUSED_ARG(poll);
1341 #endif /* LWIP_CALLBACK_API */
1342 pcb->pollinterval = interval;
1346 * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
1347 * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
1349 * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
1352 tcp_pcb_purge(struct tcp_pcb *pcb)
1354 if (pcb->state != CLOSED &&
1355 pcb->state != TIME_WAIT &&
1356 pcb->state != LISTEN) {
1358 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1360 #if TCP_LISTEN_BACKLOG
1361 if (pcb->state == SYN_RCVD) {
1362 /* Need to find the corresponding listen_pcb and decrease its accepts_pending */
1363 struct tcp_pcb_listen *lpcb;
1364 LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL",
1365 tcp_listen_pcbs.listen_pcbs != NULL);
1366 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
1367 if ((lpcb->local_port == pcb->local_port) &&
1368 (ip_addr_isany(&lpcb->local_ip) ||
1369 ip_addr_cmp(&pcb->local_ip, &lpcb->local_ip))) {
1370 /* port and address of the listen pcb match the timed-out pcb */
1371 LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending",
1372 lpcb->accepts_pending > 0);
1373 lpcb->accepts_pending--;
1378 #endif /* TCP_LISTEN_BACKLOG */
1381 if (pcb->refused_data != NULL) {
1382 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
1383 pbuf_free(pcb->refused_data);
1384 pcb->refused_data = NULL;
1386 if (pcb->unsent != NULL) {
1387 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1389 if (pcb->unacked != NULL) {
1390 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1393 if (pcb->ooseq != NULL) {
1394 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1396 tcp_segs_free(pcb->ooseq);
1398 #endif /* TCP_QUEUE_OOSEQ */
1400 /* Stop the retransmission timer as it will expect data on unacked
1401 queue if it fires */
1404 tcp_segs_free(pcb->unsent);
1405 tcp_segs_free(pcb->unacked);
1406 pcb->unacked = pcb->unsent = NULL;
1408 pcb->unsent_oversize = 0;
1409 #endif /* TCP_OVERSIZE */
1414 * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
1416 * @param pcblist PCB list to purge.
1417 * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
1420 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1422 TCP_RMV(pcblist, pcb);
1426 /* if there is an outstanding delayed ACKs, send it */
1427 if (pcb->state != TIME_WAIT &&
1428 pcb->state != LISTEN &&
1429 pcb->flags & TF_ACK_DELAY) {
1430 pcb->flags |= TF_ACK_NOW;
1434 if (pcb->state != LISTEN) {
1435 LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
1436 LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
1438 LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
1439 #endif /* TCP_QUEUE_OOSEQ */
1442 pcb->state = CLOSED;
1444 LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1448 * Calculates a new initial sequence number for new connections.
1450 * @return u32_t pseudo random sequence number
1455 static u32_t iss = 6510;
1457 iss += tcp_ticks; /* XXX */
1461 #if TCP_CALCULATE_EFF_SEND_MSS
1463 * Calcluates the effective send mss that can be used for a specific IP address
1464 * by using ip_route to determin the netif used to send to the address and
1465 * calculating the minimum of TCP_MSS and that netif's mtu (if set).
1468 tcp_eff_send_mss(u16_t sendmss, ip_addr_t *addr)
1471 struct netif *outif;
1473 outif = ip_route(addr);
1474 if ((outif != NULL) && (outif->mtu != 0)) {
1475 mss_s = outif->mtu - IP_HLEN - TCP_HLEN;
1476 /* RFC 1122, chap 4.2.2.6:
1477 * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1478 * We correct for TCP options in tcp_write(), and don't support IP options.
1480 sendmss = LWIP_MIN(sendmss, mss_s);
1484 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1487 tcp_debug_state_str(enum tcp_state s)
1489 return tcp_state_str[s];
1492 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
1494 * Print a tcp header for debugging purposes.
1496 * @param tcphdr pointer to a struct tcp_hdr
1499 tcp_debug_print(struct tcp_hdr *tcphdr)
1501 LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
1502 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1503 LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1504 ntohs(tcphdr->src), ntohs(tcphdr->dest)));
1505 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1506 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
1507 ntohl(tcphdr->seqno)));
1508 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1509 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
1510 ntohl(tcphdr->ackno)));
1511 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1512 LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
1513 TCPH_HDRLEN(tcphdr),
1514 TCPH_FLAGS(tcphdr) >> 5 & 1,
1515 TCPH_FLAGS(tcphdr) >> 4 & 1,
1516 TCPH_FLAGS(tcphdr) >> 3 & 1,
1517 TCPH_FLAGS(tcphdr) >> 2 & 1,
1518 TCPH_FLAGS(tcphdr) >> 1 & 1,
1519 TCPH_FLAGS(tcphdr) & 1,
1520 ntohs(tcphdr->wnd)));
1521 tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
1522 LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
1523 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1524 LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
1525 ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
1526 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1530 * Print a tcp state for debugging purposes.
1532 * @param s enum tcp_state to print
1535 tcp_debug_print_state(enum tcp_state s)
1537 LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
1541 * Print tcp flags for debugging purposes.
1543 * @param flags tcp flags, all active flags are printed
1546 tcp_debug_print_flags(u8_t flags)
1548 if (flags & TCP_FIN) {
1549 LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
1551 if (flags & TCP_SYN) {
1552 LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
1554 if (flags & TCP_RST) {
1555 LWIP_DEBUGF(TCP_DEBUG, ("RST "));
1557 if (flags & TCP_PSH) {
1558 LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
1560 if (flags & TCP_ACK) {
1561 LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
1563 if (flags & TCP_URG) {
1564 LWIP_DEBUGF(TCP_DEBUG, ("URG "));
1566 if (flags & TCP_ECE) {
1567 LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
1569 if (flags & TCP_CWR) {
1570 LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
1572 LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1576 * Print all tcp_pcbs in every list for debugging purposes.
1579 tcp_debug_print_pcbs(void)
1581 struct tcp_pcb *pcb;
1582 LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
1583 for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1584 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1585 pcb->local_port, pcb->remote_port,
1586 pcb->snd_nxt, pcb->rcv_nxt));
1587 tcp_debug_print_state(pcb->state);
1589 LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
1590 for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
1591 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1592 pcb->local_port, pcb->remote_port,
1593 pcb->snd_nxt, pcb->rcv_nxt));
1594 tcp_debug_print_state(pcb->state);
1596 LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
1597 for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1598 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1599 pcb->local_port, pcb->remote_port,
1600 pcb->snd_nxt, pcb->rcv_nxt));
1601 tcp_debug_print_state(pcb->state);
1606 * Check state consistency of the tcp_pcb lists.
1611 struct tcp_pcb *pcb;
1612 for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1613 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
1614 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
1615 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
1617 for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1618 LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1622 #endif /* TCP_DEBUG */
1624 #endif /* LWIP_TCP */