Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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