Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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