Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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