Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

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