Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible

Dependents:   LwIP_raw_API_serverExample tiny-dtls

Committer:
RodColeman
Date:
Tue Sep 18 14:41:24 2012 +0000
Revision:
0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip

Who changed what in which revision?

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