Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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