SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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