Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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