NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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