A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

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