Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

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