Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

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