Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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