ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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