TwitterExample with newer library (2012Aug)

Dependencies:   EthernetNetIf HTTPClient mbed

Committer:
nxpfan
Date:
Wed Aug 29 03:50:19 2012 +0000
Revision:
0:075157567b0c
simple twitter example with newer library

Who changed what in which revision?

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