Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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