Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
dcoban
Date:
Tue Apr 03 18:43:13 2012 +0000
Revision:
0:1a61c61d0845

        

Who changed what in which revision?

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