ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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