Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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