Committer:
mbed714
Date:
Sat Sep 18 23:05:49 2010 +0000
Revision:
0:d616ece2d859

        

Who changed what in which revision?

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