Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Sat Feb 25 03:28:05 2012 +0000
Revision:
1:c7958aa34fa1
Parent:
0:616601bde9fb
Use published libraries where possible.

Who changed what in which revision?

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