Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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