Official mbed lwIP library (version 1.4.0)

Dependents:   LwIPNetworking NetServicesMin EthernetInterface EthernetInterface_RSF ... more

Legacy Networking Libraries

This is an mbed 2 networking library. For mbed OS 5, lwip has been integrated with built-in networking interfaces. The networking libraries have been revised to better support additional network stacks and thread safety here.

This library is based on the code of lwIP v1.4.0

Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
All rights reserved. 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
Committer:
mbed_official
Date:
Mon Mar 14 16:15:36 2016 +0000
Revision:
20:08f08bfc3f3d
Parent:
0:51ac1d130fd4
Synchronized with git revision fec574a5ed6db26aca1b13992ff271bf527d4a0d

Full URL: https://github.com/mbedmicro/mbed/commit/fec574a5ed6db26aca1b13992ff271bf527d4a0d/

Increased allocated netbufs to handle DTLS handshakes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:51ac1d130fd4 1 /*
mbed_official 0:51ac1d130fd4 2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
mbed_official 0:51ac1d130fd4 3 * All rights reserved.
mbed_official 0:51ac1d130fd4 4 *
mbed_official 0:51ac1d130fd4 5 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 0:51ac1d130fd4 6 * are permitted provided that the following conditions are met:
mbed_official 0:51ac1d130fd4 7 *
mbed_official 0:51ac1d130fd4 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 0:51ac1d130fd4 9 * this list of conditions and the following disclaimer.
mbed_official 0:51ac1d130fd4 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 0:51ac1d130fd4 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 0:51ac1d130fd4 12 * and/or other materials provided with the distribution.
mbed_official 0:51ac1d130fd4 13 * 3. The name of the author may not be used to endorse or promote products
mbed_official 0:51ac1d130fd4 14 * derived from this software without specific prior written permission.
mbed_official 0:51ac1d130fd4 15 *
mbed_official 0:51ac1d130fd4 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
mbed_official 0:51ac1d130fd4 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 0:51ac1d130fd4 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
mbed_official 0:51ac1d130fd4 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mbed_official 0:51ac1d130fd4 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
mbed_official 0:51ac1d130fd4 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 0:51ac1d130fd4 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 0:51ac1d130fd4 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
mbed_official 0:51ac1d130fd4 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
mbed_official 0:51ac1d130fd4 25 * OF SUCH DAMAGE.
mbed_official 0:51ac1d130fd4 26 *
mbed_official 0:51ac1d130fd4 27 * This file is part of the lwIP TCP/IP stack.
mbed_official 0:51ac1d130fd4 28 *
mbed_official 0:51ac1d130fd4 29 * Author: Adam Dunkels <adam@sics.se>
mbed_official 0:51ac1d130fd4 30 *
mbed_official 0:51ac1d130fd4 31 */
mbed_official 0:51ac1d130fd4 32 #ifndef __LWIP_TCP_IMPL_H__
mbed_official 0:51ac1d130fd4 33 #define __LWIP_TCP_IMPL_H__
mbed_official 0:51ac1d130fd4 34
mbed_official 0:51ac1d130fd4 35 #include "lwip/opt.h"
mbed_official 0:51ac1d130fd4 36
mbed_official 0:51ac1d130fd4 37 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
mbed_official 0:51ac1d130fd4 38
mbed_official 0:51ac1d130fd4 39 #include "lwip/tcp.h"
mbed_official 0:51ac1d130fd4 40 #include "lwip/sys.h"
mbed_official 0:51ac1d130fd4 41 #include "lwip/mem.h"
mbed_official 0:51ac1d130fd4 42 #include "lwip/pbuf.h"
mbed_official 0:51ac1d130fd4 43 #include "lwip/ip.h"
mbed_official 0:51ac1d130fd4 44 #include "lwip/icmp.h"
mbed_official 0:51ac1d130fd4 45 #include "lwip/err.h"
mbed_official 0:51ac1d130fd4 46
mbed_official 0:51ac1d130fd4 47 #ifdef __cplusplus
mbed_official 0:51ac1d130fd4 48 extern "C" {
mbed_official 0:51ac1d130fd4 49 #endif
mbed_official 0:51ac1d130fd4 50
mbed_official 0:51ac1d130fd4 51 /* Functions for interfacing with TCP: */
mbed_official 0:51ac1d130fd4 52
mbed_official 0:51ac1d130fd4 53 /* Lower layer interface to TCP: */
mbed_official 0:51ac1d130fd4 54 #define tcp_init() /* Compatibility define, no init needed. */
mbed_official 0:51ac1d130fd4 55 void tcp_tmr (void); /* Must be called every
mbed_official 0:51ac1d130fd4 56 TCP_TMR_INTERVAL
mbed_official 0:51ac1d130fd4 57 ms. (Typically 250 ms). */
mbed_official 0:51ac1d130fd4 58 /* It is also possible to call these two functions at the right
mbed_official 0:51ac1d130fd4 59 intervals (instead of calling tcp_tmr()). */
mbed_official 0:51ac1d130fd4 60 void tcp_slowtmr (void);
mbed_official 0:51ac1d130fd4 61 void tcp_fasttmr (void);
mbed_official 0:51ac1d130fd4 62
mbed_official 0:51ac1d130fd4 63
mbed_official 0:51ac1d130fd4 64 /* Only used by IP to pass a TCP segment to TCP: */
mbed_official 0:51ac1d130fd4 65 void tcp_input (struct pbuf *p, struct netif *inp);
mbed_official 0:51ac1d130fd4 66 /* Used within the TCP code only: */
mbed_official 0:51ac1d130fd4 67 struct tcp_pcb * tcp_alloc (u8_t prio);
mbed_official 0:51ac1d130fd4 68 void tcp_abandon (struct tcp_pcb *pcb, int reset);
mbed_official 0:51ac1d130fd4 69 err_t tcp_send_empty_ack(struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 70 void tcp_rexmit (struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 71 void tcp_rexmit_rto (struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 72 void tcp_rexmit_fast (struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 73 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 74
mbed_official 0:51ac1d130fd4 75 /**
mbed_official 0:51ac1d130fd4 76 * This is the Nagle algorithm: try to combine user data to send as few TCP
mbed_official 0:51ac1d130fd4 77 * segments as possible. Only send if
mbed_official 0:51ac1d130fd4 78 * - no previously transmitted data on the connection remains unacknowledged or
mbed_official 0:51ac1d130fd4 79 * - the TF_NODELAY flag is set (nagle algorithm turned off for this pcb) or
mbed_official 0:51ac1d130fd4 80 * - the only unsent segment is at least pcb->mss bytes long (or there is more
mbed_official 0:51ac1d130fd4 81 * than one unsent segment - with lwIP, this can happen although unsent->len < mss)
mbed_official 0:51ac1d130fd4 82 * - or if we are in fast-retransmit (TF_INFR)
mbed_official 0:51ac1d130fd4 83 */
mbed_official 0:51ac1d130fd4 84 #define tcp_do_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \
mbed_official 0:51ac1d130fd4 85 ((tpcb)->flags & (TF_NODELAY | TF_INFR)) || \
mbed_official 0:51ac1d130fd4 86 (((tpcb)->unsent != NULL) && (((tpcb)->unsent->next != NULL) || \
mbed_official 0:51ac1d130fd4 87 ((tpcb)->unsent->len >= (tpcb)->mss))) \
mbed_official 0:51ac1d130fd4 88 ) ? 1 : 0)
mbed_official 0:51ac1d130fd4 89 #define tcp_output_nagle(tpcb) (tcp_do_output_nagle(tpcb) ? tcp_output(tpcb) : ERR_OK)
mbed_official 0:51ac1d130fd4 90
mbed_official 0:51ac1d130fd4 91
mbed_official 0:51ac1d130fd4 92 #define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
mbed_official 0:51ac1d130fd4 93 #define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
mbed_official 0:51ac1d130fd4 94 #define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
mbed_official 0:51ac1d130fd4 95 #define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
mbed_official 0:51ac1d130fd4 96 /* is b<=a<=c? */
mbed_official 0:51ac1d130fd4 97 #if 0 /* see bug #10548 */
mbed_official 0:51ac1d130fd4 98 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
mbed_official 0:51ac1d130fd4 99 #endif
mbed_official 0:51ac1d130fd4 100 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
mbed_official 0:51ac1d130fd4 101 #define TCP_FIN 0x01U
mbed_official 0:51ac1d130fd4 102 #define TCP_SYN 0x02U
mbed_official 0:51ac1d130fd4 103 #define TCP_RST 0x04U
mbed_official 0:51ac1d130fd4 104 #define TCP_PSH 0x08U
mbed_official 0:51ac1d130fd4 105 #define TCP_ACK 0x10U
mbed_official 0:51ac1d130fd4 106 #define TCP_URG 0x20U
mbed_official 0:51ac1d130fd4 107 #define TCP_ECE 0x40U
mbed_official 0:51ac1d130fd4 108 #define TCP_CWR 0x80U
mbed_official 0:51ac1d130fd4 109
mbed_official 0:51ac1d130fd4 110 #define TCP_FLAGS 0x3fU
mbed_official 0:51ac1d130fd4 111
mbed_official 0:51ac1d130fd4 112 /* Length of the TCP header, excluding options. */
mbed_official 0:51ac1d130fd4 113 #define TCP_HLEN 20
mbed_official 0:51ac1d130fd4 114
mbed_official 0:51ac1d130fd4 115 #ifndef TCP_TMR_INTERVAL
mbed_official 0:51ac1d130fd4 116 #define TCP_TMR_INTERVAL 250 /* The TCP timer interval in milliseconds. */
mbed_official 0:51ac1d130fd4 117 #endif /* TCP_TMR_INTERVAL */
mbed_official 0:51ac1d130fd4 118
mbed_official 0:51ac1d130fd4 119 #ifndef TCP_FAST_INTERVAL
mbed_official 0:51ac1d130fd4 120 #define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in milliseconds */
mbed_official 0:51ac1d130fd4 121 #endif /* TCP_FAST_INTERVAL */
mbed_official 0:51ac1d130fd4 122
mbed_official 0:51ac1d130fd4 123 #ifndef TCP_SLOW_INTERVAL
mbed_official 0:51ac1d130fd4 124 #define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in milliseconds */
mbed_official 0:51ac1d130fd4 125 #endif /* TCP_SLOW_INTERVAL */
mbed_official 0:51ac1d130fd4 126
mbed_official 0:51ac1d130fd4 127 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
mbed_official 0:51ac1d130fd4 128 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
mbed_official 0:51ac1d130fd4 129
mbed_official 0:51ac1d130fd4 130 #define TCP_OOSEQ_TIMEOUT 6U /* x RTO */
mbed_official 0:51ac1d130fd4 131
mbed_official 0:51ac1d130fd4 132 #ifndef TCP_MSL
mbed_official 0:51ac1d130fd4 133 #define TCP_MSL 60000UL /* The maximum segment lifetime in milliseconds */
mbed_official 0:51ac1d130fd4 134 #endif
mbed_official 0:51ac1d130fd4 135
mbed_official 0:51ac1d130fd4 136 /* Keepalive values, compliant with RFC 1122. Don't change this unless you know what you're doing */
mbed_official 0:51ac1d130fd4 137 #ifndef TCP_KEEPIDLE_DEFAULT
mbed_official 0:51ac1d130fd4 138 #define TCP_KEEPIDLE_DEFAULT 7200000UL /* Default KEEPALIVE timer in milliseconds */
mbed_official 0:51ac1d130fd4 139 #endif
mbed_official 0:51ac1d130fd4 140
mbed_official 0:51ac1d130fd4 141 #ifndef TCP_KEEPINTVL_DEFAULT
mbed_official 0:51ac1d130fd4 142 #define TCP_KEEPINTVL_DEFAULT 75000UL /* Default Time between KEEPALIVE probes in milliseconds */
mbed_official 0:51ac1d130fd4 143 #endif
mbed_official 0:51ac1d130fd4 144
mbed_official 0:51ac1d130fd4 145 #ifndef TCP_KEEPCNT_DEFAULT
mbed_official 0:51ac1d130fd4 146 #define TCP_KEEPCNT_DEFAULT 9U /* Default Counter for KEEPALIVE probes */
mbed_official 0:51ac1d130fd4 147 #endif
mbed_official 0:51ac1d130fd4 148
mbed_official 0:51ac1d130fd4 149 #define TCP_MAXIDLE TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT /* Maximum KEEPALIVE probe time */
mbed_official 0:51ac1d130fd4 150
mbed_official 0:51ac1d130fd4 151 /* Fields are (of course) in network byte order.
mbed_official 0:51ac1d130fd4 152 * Some fields are converted to host byte order in tcp_input().
mbed_official 0:51ac1d130fd4 153 */
mbed_official 0:51ac1d130fd4 154 #ifdef PACK_STRUCT_USE_INCLUDES
mbed_official 0:51ac1d130fd4 155 # include "arch/bpstruct.h"
mbed_official 0:51ac1d130fd4 156 #endif
mbed_official 0:51ac1d130fd4 157 PACK_STRUCT_BEGIN
mbed_official 0:51ac1d130fd4 158 struct tcp_hdr {
mbed_official 0:51ac1d130fd4 159 PACK_STRUCT_FIELD(u16_t src);
mbed_official 0:51ac1d130fd4 160 PACK_STRUCT_FIELD(u16_t dest);
mbed_official 0:51ac1d130fd4 161 PACK_STRUCT_FIELD(u32_t seqno);
mbed_official 0:51ac1d130fd4 162 PACK_STRUCT_FIELD(u32_t ackno);
mbed_official 0:51ac1d130fd4 163 PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
mbed_official 0:51ac1d130fd4 164 PACK_STRUCT_FIELD(u16_t wnd);
mbed_official 0:51ac1d130fd4 165 PACK_STRUCT_FIELD(u16_t chksum);
mbed_official 0:51ac1d130fd4 166 PACK_STRUCT_FIELD(u16_t urgp);
mbed_official 0:51ac1d130fd4 167 } PACK_STRUCT_STRUCT;
mbed_official 0:51ac1d130fd4 168 PACK_STRUCT_END
mbed_official 0:51ac1d130fd4 169 #ifdef PACK_STRUCT_USE_INCLUDES
mbed_official 0:51ac1d130fd4 170 # include "arch/epstruct.h"
mbed_official 0:51ac1d130fd4 171 #endif
mbed_official 0:51ac1d130fd4 172
mbed_official 0:51ac1d130fd4 173 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
mbed_official 0:51ac1d130fd4 174 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
mbed_official 0:51ac1d130fd4 175 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
mbed_official 0:51ac1d130fd4 176
mbed_official 0:51ac1d130fd4 177 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
mbed_official 0:51ac1d130fd4 178 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
mbed_official 0:51ac1d130fd4 179 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = (((phdr)->_hdrlen_rsvd_flags & PP_HTONS((u16_t)(~(u16_t)(TCP_FLAGS)))) | htons(flags))
mbed_official 0:51ac1d130fd4 180 #define TCPH_HDRLEN_FLAGS_SET(phdr, len, flags) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | (flags))
mbed_official 0:51ac1d130fd4 181
mbed_official 0:51ac1d130fd4 182 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = ((phdr)->_hdrlen_rsvd_flags | htons(flags))
mbed_official 0:51ac1d130fd4 183 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
mbed_official 0:51ac1d130fd4 184
mbed_official 0:51ac1d130fd4 185 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & (TCP_FIN | TCP_SYN)) != 0))
mbed_official 0:51ac1d130fd4 186
mbed_official 0:51ac1d130fd4 187 /** Flags used on input processing, not on pcb->flags
mbed_official 0:51ac1d130fd4 188 */
mbed_official 0:51ac1d130fd4 189 #define TF_RESET (u8_t)0x08U /* Connection was reset. */
mbed_official 0:51ac1d130fd4 190 #define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
mbed_official 0:51ac1d130fd4 191 #define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
mbed_official 0:51ac1d130fd4 192
mbed_official 0:51ac1d130fd4 193
mbed_official 0:51ac1d130fd4 194 #if LWIP_EVENT_API
mbed_official 0:51ac1d130fd4 195
mbed_official 0:51ac1d130fd4 196 #define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
mbed_official 0:51ac1d130fd4 197 LWIP_EVENT_ACCEPT, NULL, 0, err)
mbed_official 0:51ac1d130fd4 198 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
mbed_official 0:51ac1d130fd4 199 LWIP_EVENT_SENT, NULL, space, ERR_OK)
mbed_official 0:51ac1d130fd4 200 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
mbed_official 0:51ac1d130fd4 201 LWIP_EVENT_RECV, (p), 0, (err))
mbed_official 0:51ac1d130fd4 202 #define TCP_EVENT_CLOSED(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
mbed_official 0:51ac1d130fd4 203 LWIP_EVENT_RECV, NULL, 0, ERR_OK)
mbed_official 0:51ac1d130fd4 204 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
mbed_official 0:51ac1d130fd4 205 LWIP_EVENT_CONNECTED, NULL, 0, (err))
mbed_official 0:51ac1d130fd4 206 #define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
mbed_official 0:51ac1d130fd4 207 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
mbed_official 0:51ac1d130fd4 208 #define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
mbed_official 0:51ac1d130fd4 209 LWIP_EVENT_ERR, NULL, 0, (err))
mbed_official 0:51ac1d130fd4 210
mbed_official 0:51ac1d130fd4 211 #else /* LWIP_EVENT_API */
mbed_official 0:51ac1d130fd4 212
mbed_official 0:51ac1d130fd4 213 #define TCP_EVENT_ACCEPT(pcb,err,ret) \
mbed_official 0:51ac1d130fd4 214 do { \
mbed_official 0:51ac1d130fd4 215 if((pcb)->accept != NULL) \
mbed_official 0:51ac1d130fd4 216 (ret) = (pcb)->accept((pcb)->callback_arg,(pcb),(err)); \
mbed_official 0:51ac1d130fd4 217 else (ret) = ERR_ARG; \
mbed_official 0:51ac1d130fd4 218 } while (0)
mbed_official 0:51ac1d130fd4 219
mbed_official 0:51ac1d130fd4 220 #define TCP_EVENT_SENT(pcb,space,ret) \
mbed_official 0:51ac1d130fd4 221 do { \
mbed_official 0:51ac1d130fd4 222 if((pcb)->sent != NULL) \
mbed_official 0:51ac1d130fd4 223 (ret) = (pcb)->sent((pcb)->callback_arg,(pcb),(space)); \
mbed_official 0:51ac1d130fd4 224 else (ret) = ERR_OK; \
mbed_official 0:51ac1d130fd4 225 } while (0)
mbed_official 0:51ac1d130fd4 226
mbed_official 0:51ac1d130fd4 227 #define TCP_EVENT_RECV(pcb,p,err,ret) \
mbed_official 0:51ac1d130fd4 228 do { \
mbed_official 0:51ac1d130fd4 229 if((pcb)->recv != NULL) { \
mbed_official 0:51ac1d130fd4 230 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err));\
mbed_official 0:51ac1d130fd4 231 } else { \
mbed_official 0:51ac1d130fd4 232 (ret) = tcp_recv_null(NULL, (pcb), (p), (err)); \
mbed_official 0:51ac1d130fd4 233 } \
mbed_official 0:51ac1d130fd4 234 } while (0)
mbed_official 0:51ac1d130fd4 235
mbed_official 0:51ac1d130fd4 236 #define TCP_EVENT_CLOSED(pcb,ret) \
mbed_official 0:51ac1d130fd4 237 do { \
mbed_official 0:51ac1d130fd4 238 if(((pcb)->recv != NULL)) { \
mbed_official 0:51ac1d130fd4 239 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),NULL,ERR_OK);\
mbed_official 0:51ac1d130fd4 240 } else { \
mbed_official 0:51ac1d130fd4 241 (ret) = ERR_OK; \
mbed_official 0:51ac1d130fd4 242 } \
mbed_official 0:51ac1d130fd4 243 } while (0)
mbed_official 0:51ac1d130fd4 244
mbed_official 0:51ac1d130fd4 245 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
mbed_official 0:51ac1d130fd4 246 do { \
mbed_official 0:51ac1d130fd4 247 if((pcb)->connected != NULL) \
mbed_official 0:51ac1d130fd4 248 (ret) = (pcb)->connected((pcb)->callback_arg,(pcb),(err)); \
mbed_official 0:51ac1d130fd4 249 else (ret) = ERR_OK; \
mbed_official 0:51ac1d130fd4 250 } while (0)
mbed_official 0:51ac1d130fd4 251
mbed_official 0:51ac1d130fd4 252 #define TCP_EVENT_POLL(pcb,ret) \
mbed_official 0:51ac1d130fd4 253 do { \
mbed_official 0:51ac1d130fd4 254 if((pcb)->poll != NULL) \
mbed_official 0:51ac1d130fd4 255 (ret) = (pcb)->poll((pcb)->callback_arg,(pcb)); \
mbed_official 0:51ac1d130fd4 256 else (ret) = ERR_OK; \
mbed_official 0:51ac1d130fd4 257 } while (0)
mbed_official 0:51ac1d130fd4 258
mbed_official 0:51ac1d130fd4 259 #define TCP_EVENT_ERR(errf,arg,err) \
mbed_official 0:51ac1d130fd4 260 do { \
mbed_official 0:51ac1d130fd4 261 if((errf) != NULL) \
mbed_official 0:51ac1d130fd4 262 (errf)((arg),(err)); \
mbed_official 0:51ac1d130fd4 263 } while (0)
mbed_official 0:51ac1d130fd4 264
mbed_official 0:51ac1d130fd4 265 #endif /* LWIP_EVENT_API */
mbed_official 0:51ac1d130fd4 266
mbed_official 0:51ac1d130fd4 267 /** Enabled extra-check for TCP_OVERSIZE if LWIP_DEBUG is enabled */
mbed_official 0:51ac1d130fd4 268 #if TCP_OVERSIZE && defined(LWIP_DEBUG)
mbed_official 0:51ac1d130fd4 269 #define TCP_OVERSIZE_DBGCHECK 1
mbed_official 0:51ac1d130fd4 270 #else
mbed_official 0:51ac1d130fd4 271 #define TCP_OVERSIZE_DBGCHECK 0
mbed_official 0:51ac1d130fd4 272 #endif
mbed_official 0:51ac1d130fd4 273
mbed_official 0:51ac1d130fd4 274 /** Don't generate checksum on copy if CHECKSUM_GEN_TCP is disabled */
mbed_official 0:51ac1d130fd4 275 #define TCP_CHECKSUM_ON_COPY (LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_TCP)
mbed_official 0:51ac1d130fd4 276
mbed_official 0:51ac1d130fd4 277 /* This structure represents a TCP segment on the unsent, unacked and ooseq queues */
mbed_official 0:51ac1d130fd4 278 struct tcp_seg {
mbed_official 0:51ac1d130fd4 279 struct tcp_seg *next; /* used when putting segements on a queue */
mbed_official 0:51ac1d130fd4 280 struct pbuf *p; /* buffer containing data + TCP header */
mbed_official 0:51ac1d130fd4 281 u16_t len; /* the TCP length of this segment */
mbed_official 0:51ac1d130fd4 282 #if TCP_OVERSIZE_DBGCHECK
mbed_official 0:51ac1d130fd4 283 u16_t oversize_left; /* Extra bytes available at the end of the last
mbed_official 0:51ac1d130fd4 284 pbuf in unsent (used for asserting vs.
mbed_official 0:51ac1d130fd4 285 tcp_pcb.unsent_oversized only) */
mbed_official 0:51ac1d130fd4 286 #endif /* TCP_OVERSIZE_DBGCHECK */
mbed_official 0:51ac1d130fd4 287 #if TCP_CHECKSUM_ON_COPY
mbed_official 0:51ac1d130fd4 288 u16_t chksum;
mbed_official 0:51ac1d130fd4 289 u8_t chksum_swapped;
mbed_official 0:51ac1d130fd4 290 #endif /* TCP_CHECKSUM_ON_COPY */
mbed_official 0:51ac1d130fd4 291 u8_t flags;
mbed_official 0:51ac1d130fd4 292 #define TF_SEG_OPTS_MSS (u8_t)0x01U /* Include MSS option. */
mbed_official 0:51ac1d130fd4 293 #define TF_SEG_OPTS_TS (u8_t)0x02U /* Include timestamp option. */
mbed_official 0:51ac1d130fd4 294 #define TF_SEG_DATA_CHECKSUMMED (u8_t)0x04U /* ALL data (not the header) is
mbed_official 0:51ac1d130fd4 295 checksummed into 'chksum' */
mbed_official 0:51ac1d130fd4 296 struct tcp_hdr *tcphdr; /* the TCP header */
mbed_official 0:51ac1d130fd4 297 };
mbed_official 0:51ac1d130fd4 298
mbed_official 0:51ac1d130fd4 299 #define LWIP_TCP_OPT_LENGTH(flags) \
mbed_official 0:51ac1d130fd4 300 (flags & TF_SEG_OPTS_MSS ? 4 : 0) + \
mbed_official 0:51ac1d130fd4 301 (flags & TF_SEG_OPTS_TS ? 12 : 0)
mbed_official 0:51ac1d130fd4 302
mbed_official 0:51ac1d130fd4 303 /** This returns a TCP header option for MSS in an u32_t */
mbed_official 0:51ac1d130fd4 304 #define TCP_BUILD_MSS_OPTION(x) (x) = PP_HTONL(((u32_t)2 << 24) | \
mbed_official 0:51ac1d130fd4 305 ((u32_t)4 << 16) | \
mbed_official 0:51ac1d130fd4 306 (((u32_t)TCP_MSS / 256) << 8) | \
mbed_official 0:51ac1d130fd4 307 (TCP_MSS & 255))
mbed_official 0:51ac1d130fd4 308
mbed_official 0:51ac1d130fd4 309 /* Global variables: */
mbed_official 0:51ac1d130fd4 310 extern struct tcp_pcb *tcp_input_pcb;
mbed_official 0:51ac1d130fd4 311 extern u32_t tcp_ticks;
mbed_official 0:51ac1d130fd4 312
mbed_official 0:51ac1d130fd4 313 /* The TCP PCB lists. */
mbed_official 0:51ac1d130fd4 314 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
mbed_official 0:51ac1d130fd4 315 struct tcp_pcb_listen *listen_pcbs;
mbed_official 0:51ac1d130fd4 316 struct tcp_pcb *pcbs;
mbed_official 0:51ac1d130fd4 317 };
mbed_official 0:51ac1d130fd4 318 extern struct tcp_pcb *tcp_bound_pcbs;
mbed_official 0:51ac1d130fd4 319 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
mbed_official 0:51ac1d130fd4 320 extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
mbed_official 0:51ac1d130fd4 321 state in which they accept or send
mbed_official 0:51ac1d130fd4 322 data. */
mbed_official 0:51ac1d130fd4 323 extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
mbed_official 0:51ac1d130fd4 324
mbed_official 0:51ac1d130fd4 325 extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
mbed_official 0:51ac1d130fd4 326
mbed_official 0:51ac1d130fd4 327 /* Axioms about the above lists:
mbed_official 0:51ac1d130fd4 328 1) Every TCP PCB that is not CLOSED is in one of the lists.
mbed_official 0:51ac1d130fd4 329 2) A PCB is only in one of the lists.
mbed_official 0:51ac1d130fd4 330 3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
mbed_official 0:51ac1d130fd4 331 4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
mbed_official 0:51ac1d130fd4 332 */
mbed_official 0:51ac1d130fd4 333 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
mbed_official 0:51ac1d130fd4 334 with a PCB list or removes a PCB from a list, respectively. */
mbed_official 0:51ac1d130fd4 335 #ifndef TCP_DEBUG_PCB_LISTS
mbed_official 0:51ac1d130fd4 336 #define TCP_DEBUG_PCB_LISTS 0
mbed_official 0:51ac1d130fd4 337 #endif
mbed_official 0:51ac1d130fd4 338 #if TCP_DEBUG_PCB_LISTS
mbed_official 0:51ac1d130fd4 339 #define TCP_REG(pcbs, npcb) do {\
mbed_official 0:51ac1d130fd4 340 LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", (npcb), (npcb)->local_port)); \
mbed_official 0:51ac1d130fd4 341 for(tcp_tmp_pcb = *(pcbs); \
mbed_official 0:51ac1d130fd4 342 tcp_tmp_pcb != NULL; \
mbed_official 0:51ac1d130fd4 343 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
mbed_official 0:51ac1d130fd4 344 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != (npcb)); \
mbed_official 0:51ac1d130fd4 345 } \
mbed_official 0:51ac1d130fd4 346 LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", ((pcbs) == &tcp_bound_pcbs) || ((npcb)->state != CLOSED)); \
mbed_official 0:51ac1d130fd4 347 (npcb)->next = *(pcbs); \
mbed_official 0:51ac1d130fd4 348 LWIP_ASSERT("TCP_REG: npcb->next != npcb", (npcb)->next != (npcb)); \
mbed_official 0:51ac1d130fd4 349 *(pcbs) = (npcb); \
mbed_official 0:51ac1d130fd4 350 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
mbed_official 0:51ac1d130fd4 351 tcp_timer_needed(); \
mbed_official 0:51ac1d130fd4 352 } while(0)
mbed_official 0:51ac1d130fd4 353 #define TCP_RMV(pcbs, npcb) do { \
mbed_official 0:51ac1d130fd4 354 LWIP_ASSERT("TCP_RMV: pcbs != NULL", *(pcbs) != NULL); \
mbed_official 0:51ac1d130fd4 355 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", (npcb), *(pcbs))); \
mbed_official 0:51ac1d130fd4 356 if(*(pcbs) == (npcb)) { \
mbed_official 0:51ac1d130fd4 357 *(pcbs) = (*pcbs)->next; \
mbed_official 0:51ac1d130fd4 358 } else for(tcp_tmp_pcb = *(pcbs); tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
mbed_official 0:51ac1d130fd4 359 if(tcp_tmp_pcb->next == (npcb)) { \
mbed_official 0:51ac1d130fd4 360 tcp_tmp_pcb->next = (npcb)->next; \
mbed_official 0:51ac1d130fd4 361 break; \
mbed_official 0:51ac1d130fd4 362 } \
mbed_official 0:51ac1d130fd4 363 } \
mbed_official 0:51ac1d130fd4 364 (npcb)->next = NULL; \
mbed_official 0:51ac1d130fd4 365 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
mbed_official 0:51ac1d130fd4 366 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", (npcb), *(pcbs))); \
mbed_official 0:51ac1d130fd4 367 } while(0)
mbed_official 0:51ac1d130fd4 368
mbed_official 0:51ac1d130fd4 369 #else /* LWIP_DEBUG */
mbed_official 0:51ac1d130fd4 370
mbed_official 0:51ac1d130fd4 371 #define TCP_REG(pcbs, npcb) \
mbed_official 0:51ac1d130fd4 372 do { \
mbed_official 0:51ac1d130fd4 373 (npcb)->next = *pcbs; \
mbed_official 0:51ac1d130fd4 374 *(pcbs) = (npcb); \
mbed_official 0:51ac1d130fd4 375 tcp_timer_needed(); \
mbed_official 0:51ac1d130fd4 376 } while (0)
mbed_official 0:51ac1d130fd4 377
mbed_official 0:51ac1d130fd4 378 #define TCP_RMV(pcbs, npcb) \
mbed_official 0:51ac1d130fd4 379 do { \
mbed_official 0:51ac1d130fd4 380 if(*(pcbs) == (npcb)) { \
mbed_official 0:51ac1d130fd4 381 (*(pcbs)) = (*pcbs)->next; \
mbed_official 0:51ac1d130fd4 382 } \
mbed_official 0:51ac1d130fd4 383 else { \
mbed_official 0:51ac1d130fd4 384 for(tcp_tmp_pcb = *pcbs; \
mbed_official 0:51ac1d130fd4 385 tcp_tmp_pcb != NULL; \
mbed_official 0:51ac1d130fd4 386 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
mbed_official 0:51ac1d130fd4 387 if(tcp_tmp_pcb->next == (npcb)) { \
mbed_official 0:51ac1d130fd4 388 tcp_tmp_pcb->next = (npcb)->next; \
mbed_official 0:51ac1d130fd4 389 break; \
mbed_official 0:51ac1d130fd4 390 } \
mbed_official 0:51ac1d130fd4 391 } \
mbed_official 0:51ac1d130fd4 392 } \
mbed_official 0:51ac1d130fd4 393 (npcb)->next = NULL; \
mbed_official 0:51ac1d130fd4 394 } while(0)
mbed_official 0:51ac1d130fd4 395
mbed_official 0:51ac1d130fd4 396 #endif /* LWIP_DEBUG */
mbed_official 0:51ac1d130fd4 397
mbed_official 0:51ac1d130fd4 398
mbed_official 0:51ac1d130fd4 399 /* Internal functions: */
mbed_official 0:51ac1d130fd4 400 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 401 void tcp_pcb_purge(struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 402 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 403
mbed_official 0:51ac1d130fd4 404 void tcp_segs_free(struct tcp_seg *seg);
mbed_official 0:51ac1d130fd4 405 void tcp_seg_free(struct tcp_seg *seg);
mbed_official 0:51ac1d130fd4 406 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
mbed_official 0:51ac1d130fd4 407
mbed_official 0:51ac1d130fd4 408 #define tcp_ack(pcb) \
mbed_official 0:51ac1d130fd4 409 do { \
mbed_official 0:51ac1d130fd4 410 if((pcb)->flags & TF_ACK_DELAY) { \
mbed_official 0:51ac1d130fd4 411 (pcb)->flags &= ~TF_ACK_DELAY; \
mbed_official 0:51ac1d130fd4 412 (pcb)->flags |= TF_ACK_NOW; \
mbed_official 0:51ac1d130fd4 413 } \
mbed_official 0:51ac1d130fd4 414 else { \
mbed_official 0:51ac1d130fd4 415 (pcb)->flags |= TF_ACK_DELAY; \
mbed_official 0:51ac1d130fd4 416 } \
mbed_official 0:51ac1d130fd4 417 } while (0)
mbed_official 0:51ac1d130fd4 418
mbed_official 0:51ac1d130fd4 419 #define tcp_ack_now(pcb) \
mbed_official 0:51ac1d130fd4 420 do { \
mbed_official 0:51ac1d130fd4 421 (pcb)->flags |= TF_ACK_NOW; \
mbed_official 0:51ac1d130fd4 422 } while (0)
mbed_official 0:51ac1d130fd4 423
mbed_official 0:51ac1d130fd4 424 err_t tcp_send_fin(struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 425 err_t tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags);
mbed_official 0:51ac1d130fd4 426
mbed_official 0:51ac1d130fd4 427 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
mbed_official 0:51ac1d130fd4 428
mbed_official 0:51ac1d130fd4 429 void tcp_rst(u32_t seqno, u32_t ackno,
mbed_official 0:51ac1d130fd4 430 ip_addr_t *local_ip, ip_addr_t *remote_ip,
mbed_official 0:51ac1d130fd4 431 u16_t local_port, u16_t remote_port);
mbed_official 0:51ac1d130fd4 432
mbed_official 0:51ac1d130fd4 433 u32_t tcp_next_iss(void);
mbed_official 0:51ac1d130fd4 434
mbed_official 0:51ac1d130fd4 435 void tcp_keepalive(struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 436 void tcp_zero_window_probe(struct tcp_pcb *pcb);
mbed_official 0:51ac1d130fd4 437
mbed_official 0:51ac1d130fd4 438 #if TCP_CALCULATE_EFF_SEND_MSS
mbed_official 0:51ac1d130fd4 439 u16_t tcp_eff_send_mss(u16_t sendmss, ip_addr_t *addr);
mbed_official 0:51ac1d130fd4 440 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
mbed_official 0:51ac1d130fd4 441
mbed_official 0:51ac1d130fd4 442 #if LWIP_CALLBACK_API
mbed_official 0:51ac1d130fd4 443 err_t tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
mbed_official 0:51ac1d130fd4 444 #endif /* LWIP_CALLBACK_API */
mbed_official 0:51ac1d130fd4 445
mbed_official 0:51ac1d130fd4 446 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
mbed_official 0:51ac1d130fd4 447 void tcp_debug_print(struct tcp_hdr *tcphdr);
mbed_official 0:51ac1d130fd4 448 void tcp_debug_print_flags(u8_t flags);
mbed_official 0:51ac1d130fd4 449 void tcp_debug_print_state(enum tcp_state s);
mbed_official 0:51ac1d130fd4 450 void tcp_debug_print_pcbs(void);
mbed_official 0:51ac1d130fd4 451 s16_t tcp_pcbs_sane(void);
mbed_official 0:51ac1d130fd4 452 #else
mbed_official 0:51ac1d130fd4 453 # define tcp_debug_print(tcphdr)
mbed_official 0:51ac1d130fd4 454 # define tcp_debug_print_flags(flags)
mbed_official 0:51ac1d130fd4 455 # define tcp_debug_print_state(s)
mbed_official 0:51ac1d130fd4 456 # define tcp_debug_print_pcbs()
mbed_official 0:51ac1d130fd4 457 # define tcp_pcbs_sane() 1
mbed_official 0:51ac1d130fd4 458 #endif /* TCP_DEBUG */
mbed_official 0:51ac1d130fd4 459
mbed_official 0:51ac1d130fd4 460 /** External function (implemented in timers.c), called when TCP detects
mbed_official 0:51ac1d130fd4 461 * that a timer is needed (i.e. active- or time-wait-pcb found). */
mbed_official 0:51ac1d130fd4 462 void tcp_timer_needed(void);
mbed_official 0:51ac1d130fd4 463
mbed_official 0:51ac1d130fd4 464
mbed_official 0:51ac1d130fd4 465 #ifdef __cplusplus
mbed_official 0:51ac1d130fd4 466 }
mbed_official 0:51ac1d130fd4 467 #endif
mbed_official 0:51ac1d130fd4 468
mbed_official 0:51ac1d130fd4 469 #endif /* LWIP_TCP */
mbed_official 0:51ac1d130fd4 470
mbed_official 0:51ac1d130fd4 471 #endif /* __LWIP_TCP_H__ */