HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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