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