Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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