Dependencies:   mbed

Dependents:   TCP

Committer:
slowness
Date:
Tue Sep 06 18:05:46 2011 +0000
Revision:
0:58c3d014a4e7

        

Who changed what in which revision?

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