Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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