I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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