-/*
- * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
- * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
- * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
- * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- * OF SUCH DAMAGE.
- *
- * This file is part of the lwIP TCP/IP stack.
- *
- * Author: Adam Dunkels <adam@sics.se>
- *
- */
-#ifndef __LWIP_TCP_H__
-#define __LWIP_TCP_H__
-
-#include "lwip/sys.h"
-#include "lwip/mem.h"
-
-#include "lwip/pbuf.h"
-#include "lwip/opt.h"
-#include "lwip/ip.h"
-#include "lwip/icmp.h"
-
-#include "lwip/err.h"
-
-struct tcp_pcb;
-
-/* Functions for interfacing with TCP: */
-
-/* Lower layer interface to TCP: */
-void tcp_init (void); /* Must be called first to
- initialize TCP. */
-void tcp_tmr (void); /* Must be called every
- TCP_TMR_INTERVAL
- ms. (Typically 250 ms). */
-/* Application program's interface: */
-struct tcp_pcb * tcp_new (void);
-struct tcp_pcb * tcp_alloc (u8_t prio);
-
-void tcp_arg (struct tcp_pcb *pcb, void *arg);
-void tcp_accept (struct tcp_pcb *pcb,
- err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
- err_t err));
-void tcp_recv (struct tcp_pcb *pcb,
- err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
- struct pbuf *p, err_t err));
-void tcp_sent (struct tcp_pcb *pcb,
- err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
- u16_t len));
-void tcp_poll (struct tcp_pcb *pcb,
- err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
- u8_t interval);
-void tcp_err (struct tcp_pcb *pcb,
- void (* err)(void *arg, err_t err));
-
-#define tcp_mss(pcb) ((pcb)->mss)
-#define tcp_sndbuf(pcb) ((pcb)->snd_buf)
-
-void tcp_recved (struct tcp_pcb *pcb, u16_t len);
-err_t tcp_bind (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
- u16_t port);
-err_t tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
- u16_t port, err_t (* connected)(void *arg,
- struct tcp_pcb *tpcb,
- err_t err));
-struct tcp_pcb * tcp_listen (struct tcp_pcb *pcb);
-void tcp_abort (struct tcp_pcb *pcb);
-err_t tcp_close (struct tcp_pcb *pcb);
-err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
- u8_t copy);
-
-void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
-
-#define TCP_PRIO_MIN 1
-#define TCP_PRIO_NORMAL 64
-#define TCP_PRIO_MAX 127
-
-/* It is also possible to call these two functions at the right
- intervals (instead of calling tcp_tmr()). */
-void tcp_slowtmr (void);
-void tcp_fasttmr (void);
-
-
-/* Only used by IP to pass a TCP segment to TCP: */
-void tcp_input (struct pbuf *p, struct netif *inp);
-/* Used within the TCP code only: */
-err_t tcp_output (struct tcp_pcb *pcb);
-void tcp_rexmit (struct tcp_pcb *pcb);
-void tcp_rexmit_rto (struct tcp_pcb *pcb);
-
-
-
-#define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
-#define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
-#define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
-#define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
-/* is b<=a<=c? */
-#if 0 /* see bug #10548 */
-#define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
-#endif
-#define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
-#define TCP_FIN 0x01U
-#define TCP_SYN 0x02U
-#define TCP_RST 0x04U
-#define TCP_PSH 0x08U
-#define TCP_ACK 0x10U
-#define TCP_URG 0x20U
-#define TCP_ECE 0x40U
-#define TCP_CWR 0x80U
-
-#define TCP_FLAGS 0x3fU
-
-/* Length of the TCP header, excluding options. */
-#define TCP_HLEN 20
-
-#ifndef TCP_TMR_INTERVAL
-#define TCP_TMR_INTERVAL 250 /* The TCP timer interval in
- milliseconds. */
-#endif /* TCP_TMR_INTERVAL */
-
-#ifndef TCP_FAST_INTERVAL
-#define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in
- milliseconds */
-#endif /* TCP_FAST_INTERVAL */
-
-#ifndef TCP_SLOW_INTERVAL
-#define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in
- milliseconds */
-#endif /* TCP_SLOW_INTERVAL */
-
-#define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
-#define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
-
-#define TCP_OOSEQ_TIMEOUT 6 /* x RTO */
-
-#define TCP_MSL 60000 /* The maximum segment lifetime in microseconds */
-
-/*
- * User-settable options (used with setsockopt).
- */
-#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
-#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keepalive miliseconds */
-
-/* Keepalive values */
-#define TCP_KEEPDEFAULT 7200000 /* KEEPALIVE timer in miliseconds */
-#define TCP_KEEPINTVL 75000 /* Time between KEEPALIVE probes in miliseconds */
-#define TCP_KEEPCNT 9 /* Counter for KEEPALIVE probes */
-#define TCP_MAXIDLE TCP_KEEPCNT * TCP_KEEPINTVL /* Maximum KEEPALIVE probe time */
-
-
-#ifdef PACK_STRUCT_USE_INCLUDES
-# include "arch/bpstruct.h"
-#endif
-PACK_STRUCT_BEGIN
-struct tcp_hdr {
- PACK_STRUCT_FIELD(u16_t src);
- PACK_STRUCT_FIELD(u16_t dest);
- PACK_STRUCT_FIELD(u32_t seqno);
- PACK_STRUCT_FIELD(u32_t ackno);
- PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
- PACK_STRUCT_FIELD(u16_t wnd);
- PACK_STRUCT_FIELD(u16_t chksum);
- PACK_STRUCT_FIELD(u16_t urgp);
-} PACK_STRUCT_STRUCT;
-PACK_STRUCT_END
-#ifdef PACK_STRUCT_USE_INCLUDES
-# include "arch/epstruct.h"
-#endif
-
-#define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
-#define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
-#define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
-
-#define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
-#define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
-#define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))
-#define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))
-#define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
-
-#define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
- TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
-
-enum tcp_state {
- CLOSED = 0,
- LISTEN = 1,
- SYN_SENT = 2,
- SYN_RCVD = 3,
- ESTABLISHED = 4,
- FIN_WAIT_1 = 5,
- FIN_WAIT_2 = 6,
- CLOSE_WAIT = 7,
- CLOSING = 8,
- LAST_ACK = 9,
- TIME_WAIT = 10
-};
-
-/* the TCP protocol control block */
-struct tcp_pcb {
-/** common PCB members */
- IP_PCB;
-/** protocol specific PCB members */
- struct tcp_pcb *next; /* for the linked list */
- enum tcp_state state; /* TCP state */
- u8_t prio;
- void *callback_arg;
-
- u16_t local_port;
- u16_t remote_port;
-
- u8_t flags;
-#define TF_ACK_DELAY (u8_t)0x01U /* Delayed ACK. */
-#define TF_ACK_NOW (u8_t)0x02U /* Immediate ACK. */
-#define TF_INFR (u8_t)0x04U /* In fast recovery. */
-#define TF_RESET (u8_t)0x08U /* Connection was reset. */
-#define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
-#define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
-#define TF_NODELAY (u8_t)0x40U /* Disable Nagle algorithm */
-
- /* receiver variables */
- u32_t rcv_nxt; /* next seqno expected */
- u16_t rcv_wnd; /* receiver window */
-
- /* Timers */
- u32_t tmr;
- u8_t polltmr, pollinterval;
-
- /* Retransmission timer. */
- u16_t rtime;
-
- u16_t mss; /* maximum segment size */
-
- /* RTT (round trip time) estimation variables */
- u32_t rttest; /* RTT estimate in 500ms ticks */
- u32_t rtseq; /* sequence number being timed */
- s16_t sa, sv; /* @todo document this */
-
- u16_t rto; /* retransmission time-out */
- u8_t nrtx; /* number of retransmissions */
-
- /* fast retransmit/recovery */
- u32_t lastack; /* Highest acknowledged seqno. */
- u8_t dupacks;
-
- /* congestion avoidance/control variables */
- u16_t cwnd;
- u16_t ssthresh;
-
- /* sender variables */
- u32_t snd_nxt, /* next seqno to be sent */
- snd_max, /* Highest seqno sent. */
- snd_wnd, /* sender window */
- snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last
- window update. */
- snd_lbb; /* Sequence number of next byte to be buffered. */
-
- u16_t acked;
-
- u16_t snd_buf; /* Available buffer space for sending (in bytes). */
- u8_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
-
-
- /* These are ordered by sequence number: */
- struct tcp_seg *unsent; /* Unsent (queued) segments. */
- struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
-#if TCP_QUEUE_OOSEQ
- struct tcp_seg *ooseq; /* Received out of sequence segments. */
-#endif /* TCP_QUEUE_OOSEQ */
-
-#if LWIP_CALLBACK_API
- /* Function to be called when more send buffer space is available. */
- err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
-
- /* Function to be called when (in-sequence) data has arrived. */
- err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
-
- /* Function to be called when a connection has been set up. */
- err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
-
- /* Function to call when a listener has been connected. */
- err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
-
- /* Function which is called periodically. */
- err_t (* poll)(void *arg, struct tcp_pcb *pcb);
-
- /* Function to be called whenever a fatal error occurs. */
- void (* errf)(void *arg, err_t err);
-#endif /* LWIP_CALLBACK_API */
-
- /* idle time before KEEPALIVE is sent */
- u32_t keepalive;
-
- /* KEEPALIVE counter */
- u8_t keep_cnt;
-};
-
-struct tcp_pcb_listen {
-/* Common members of all PCB types */
- IP_PCB;
-
-/* Protocol specific PCB members */
- struct tcp_pcb_listen *next; /* for the linked list */
-
- /* Even if state is obviously LISTEN this is here for
- * field compatibility with tpc_pcb to which it is cast sometimes
- * Until a cleaner solution emerges this is here.FIXME
- */
- enum tcp_state state; /* TCP state */
-
- u8_t prio;
- void *callback_arg;
-
- u16_t local_port;
-
-#if LWIP_CALLBACK_API
- /* Function to call when a listener has been connected. */
- err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
-#endif /* LWIP_CALLBACK_API */
-};
-
-#if LWIP_EVENT_API
-
-enum lwip_event {
- LWIP_EVENT_ACCEPT,
- LWIP_EVENT_SENT,
- LWIP_EVENT_RECV,
- LWIP_EVENT_CONNECTED,
- LWIP_EVENT_POLL,
- LWIP_EVENT_ERR
-};
-
-err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
- enum lwip_event,
- struct pbuf *p,
- u16_t size,
- err_t err);
-
-#define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
- LWIP_EVENT_ACCEPT, NULL, 0, err)
-#define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
- LWIP_EVENT_SENT, NULL, space, ERR_OK)
-#define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
- LWIP_EVENT_RECV, (p), 0, (err))
-#define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
- LWIP_EVENT_CONNECTED, NULL, 0, (err))
-#define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
- LWIP_EVENT_POLL, NULL, 0, ERR_OK)
-#define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
- LWIP_EVENT_ERR, NULL, 0, (err))
-#else /* LWIP_EVENT_API */
-#define TCP_EVENT_ACCEPT(pcb,err,ret) \
- if((pcb)->accept != NULL) \
- (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))
-#define TCP_EVENT_SENT(pcb,space,ret) \
- if((pcb)->sent != NULL) \
- (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))
-#define TCP_EVENT_RECV(pcb,p,err,ret) \
- if((pcb)->recv != NULL) \
- { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \
- if (p) pbuf_free(p); }
-#define TCP_EVENT_CONNECTED(pcb,err,ret) \
- if((pcb)->connected != NULL) \
- (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))
-#define TCP_EVENT_POLL(pcb,ret) \
- if((pcb)->poll != NULL) \
- (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))
-#define TCP_EVENT_ERR(errf,arg,err) \
- if((errf) != NULL) \
- (errf)((arg),(err))
-#endif /* LWIP_EVENT_API */
-
-/* This structure represents a TCP segment on the unsent and unacked queues */
-struct tcp_seg {
- struct tcp_seg *next; /* used when putting segements on a queue */
- struct pbuf *p; /* buffer containing data + TCP header */
- void *dataptr; /* pointer to the TCP data in the pbuf */
- u16_t len; /* the TCP length of this segment */
- struct tcp_hdr *tcphdr; /* the TCP header */
-};
-
-/* Internal functions and global variables: */
-struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
-void tcp_pcb_purge(struct tcp_pcb *pcb);
-void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
-
-u8_t tcp_segs_free(struct tcp_seg *seg);
-u8_t tcp_seg_free(struct tcp_seg *seg);
-struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
-
-#define tcp_ack(pcb) if((pcb)->flags & TF_ACK_DELAY) { \
- (pcb)->flags &= ~TF_ACK_DELAY; \
- (pcb)->flags |= TF_ACK_NOW; \
- tcp_output(pcb); \
- } else { \
- (pcb)->flags |= TF_ACK_DELAY; \
- }
-
-#define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
- tcp_output(pcb)
-
-err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
-err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
- u8_t flags, u8_t copy,
- u8_t *optdata, u8_t optlen);
-
-void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
-
-void tcp_rst(u32_t seqno, u32_t ackno,
- struct ip_addr *local_ip, struct ip_addr *remote_ip,
- u16_t local_port, u16_t remote_port);
-
-u32_t tcp_next_iss(void);
-
-void tcp_keepalive(struct tcp_pcb *pcb);
-
-extern struct tcp_pcb *tcp_input_pcb;
-extern u32_t tcp_ticks;
-
-#if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
-void tcp_debug_print(struct tcp_hdr *tcphdr);
-void tcp_debug_print_flags(u8_t flags);
-void tcp_debug_print_state(enum tcp_state s);
-void tcp_debug_print_pcbs(void);
-int tcp_pcbs_sane(void);
-#else
-# define tcp_debug_print(tcphdr)
-# define tcp_debug_print_flags(flags)
-# define tcp_debug_print_state(s)
-# define tcp_debug_print_pcbs()
-# define tcp_pcbs_sane() 1
-#endif /* TCP_DEBUG */
-
-#if NO_SYS
-#define tcp_timer_needed()
-#else
-void tcp_timer_needed(void);
-#endif
-
-/* The TCP PCB lists. */
-union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
- struct tcp_pcb_listen *listen_pcbs;
- struct tcp_pcb *pcbs;
-};
-extern union tcp_listen_pcbs_t tcp_listen_pcbs;
-extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
- state in which they accept or send
- data. */
-extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
-
-extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
-
-/* Axioms about the above lists:
- 1) Every TCP PCB that is not CLOSED is in one of the lists.
- 2) A PCB is only in one of the lists.
- 3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
- 4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
-*/
-
-/* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
- with a PCB list or removes a PCB from a list, respectively. */
-#if 0
-#define TCP_REG(pcbs, npcb) do {\
- LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
- for(tcp_tmp_pcb = *pcbs; \
- tcp_tmp_pcb != NULL; \
- tcp_tmp_pcb = tcp_tmp_pcb->next) { \
- LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
- } \
- LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
- npcb->next = *pcbs; \
- LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
- *(pcbs) = npcb; \
- LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
- tcp_timer_needed(); \
- } while(0)
-#define TCP_RMV(pcbs, npcb) do { \
- LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
- LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
- if(*pcbs == npcb) { \
- *pcbs = (*pcbs)->next; \
- } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
- if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
- tcp_tmp_pcb->next = npcb->next; \
- break; \
- } \
- } \
- npcb->next = NULL; \
- LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
- LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
- } while(0)
-
-#else /* LWIP_DEBUG */
-#define TCP_REG(pcbs, npcb) do { \
- npcb->next = *pcbs; \
- *(pcbs) = npcb; \
- tcp_timer_needed(); \
- } while(0)
-#define TCP_RMV(pcbs, npcb) do { \
- if(*(pcbs) == npcb) { \
- (*(pcbs)) = (*pcbs)->next; \
- } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
- if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
- tcp_tmp_pcb->next = npcb->next; \
- break; \
- } \
- } \
- npcb->next = NULL; \
- } while(0)
-#endif /* LWIP_DEBUG */
-#endif /* __LWIP_TCP_H__ */
-
-
-
+/*\r
+ * Copyright (c) 2001-2004 Swedish Institute of Computer Science.\r
+ * All rights reserved. \r
+ * \r
+ * Redistribution and use in source and binary forms, with or without modification, \r
+ * are permitted provided that the following conditions are met:\r
+ *\r
+ * 1. Redistributions of source code must retain the above copyright notice,\r
+ * this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright notice,\r
+ * this list of conditions and the following disclaimer in the documentation\r
+ * and/or other materials provided with the distribution.\r
+ * 3. The name of the author may not be used to endorse or promote products\r
+ * derived from this software without specific prior written permission. \r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED \r
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF \r
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT \r
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, \r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT \r
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS \r
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN \r
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING \r
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY \r
+ * OF SUCH DAMAGE.\r
+ *\r
+ * This file is part of the lwIP TCP/IP stack.\r
+ * \r
+ * Author: Adam Dunkels <adam@sics.se>\r
+ *\r
+ */\r
+#ifndef __LWIP_TCP_H__\r
+#define __LWIP_TCP_H__\r
+\r
+#include "lwip/sys.h"\r
+#include "lwip/mem.h"\r
+\r
+#include "lwip/pbuf.h"\r
+#include "lwip/opt.h"\r
+#include "lwip/ip.h"\r
+#include "lwip/icmp.h"\r
+\r
+#include "lwip/err.h"\r
+\r
+struct tcp_pcb;\r
+\r
+/* Functions for interfacing with TCP: */\r
+\r
+/* Lower layer interface to TCP: */\r
+void tcp_init (void); /* Must be called first to\r
+ initialize TCP. */\r
+void tcp_tmr (void); /* Must be called every\r
+ TCP_TMR_INTERVAL\r
+ ms. (Typically 250 ms). */\r
+/* Application program's interface: */\r
+struct tcp_pcb * tcp_new (void);\r
+struct tcp_pcb * tcp_alloc (u8_t prio);\r
+\r
+void tcp_arg (struct tcp_pcb *pcb, void *arg);\r
+void tcp_accept (struct tcp_pcb *pcb,\r
+ err_t (* accept)(void *arg, struct tcp_pcb *newpcb,\r
+ err_t err));\r
+void tcp_recv (struct tcp_pcb *pcb,\r
+ err_t (* recv)(void *arg, struct tcp_pcb *tpcb,\r
+ struct pbuf *p, err_t err));\r
+void tcp_sent (struct tcp_pcb *pcb,\r
+ err_t (* sent)(void *arg, struct tcp_pcb *tpcb,\r
+ u16_t len));\r
+void tcp_poll (struct tcp_pcb *pcb,\r
+ err_t (* poll)(void *arg, struct tcp_pcb *tpcb),\r
+ u8_t interval);\r
+void tcp_err (struct tcp_pcb *pcb,\r
+ void (* err)(void *arg, err_t err));\r
+\r
+#define tcp_mss(pcb) ((pcb)->mss)\r
+#define tcp_sndbuf(pcb) ((pcb)->snd_buf)\r
+\r
+void tcp_recved (struct tcp_pcb *pcb, u16_t len);\r
+err_t tcp_bind (struct tcp_pcb *pcb, struct ip_addr *ipaddr,\r
+ u16_t port);\r
+err_t tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,\r
+ u16_t port, err_t (* connected)(void *arg,\r
+ struct tcp_pcb *tpcb,\r
+ err_t err));\r
+struct tcp_pcb * tcp_listen (struct tcp_pcb *pcb);\r
+void tcp_abort (struct tcp_pcb *pcb);\r
+err_t tcp_close (struct tcp_pcb *pcb);\r
+err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,\r
+ u8_t copy);\r
+\r
+void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);\r
+\r
+#define TCP_PRIO_MIN 1\r
+#define TCP_PRIO_NORMAL 64\r
+#define TCP_PRIO_MAX 127\r
+\r
+/* It is also possible to call these two functions at the right\r
+ intervals (instead of calling tcp_tmr()). */\r
+void tcp_slowtmr (void);\r
+void tcp_fasttmr (void);\r
+\r
+\r
+/* Only used by IP to pass a TCP segment to TCP: */\r
+void tcp_input (struct pbuf *p, struct netif *inp);\r
+/* Used within the TCP code only: */\r
+err_t tcp_output (struct tcp_pcb *pcb);\r
+void tcp_rexmit (struct tcp_pcb *pcb);\r
+void tcp_rexmit_rto (struct tcp_pcb *pcb);\r
+\r
+\r
+\r
+#define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)\r
+#define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)\r
+#define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)\r
+#define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)\r
+/* is b<=a<=c? */\r
+#if 0 /* see bug #10548 */\r
+#define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))\r
+#endif\r
+#define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))\r
+#define TCP_FIN 0x01U\r
+#define TCP_SYN 0x02U\r
+#define TCP_RST 0x04U\r
+#define TCP_PSH 0x08U\r
+#define TCP_ACK 0x10U\r
+#define TCP_URG 0x20U\r
+#define TCP_ECE 0x40U\r
+#define TCP_CWR 0x80U\r
+\r
+#define TCP_FLAGS 0x3fU\r
+\r
+/* Length of the TCP header, excluding options. */\r
+#define TCP_HLEN 20\r
+\r
+#ifndef TCP_TMR_INTERVAL\r
+#define TCP_TMR_INTERVAL 250 /* The TCP timer interval in\r
+ milliseconds. */\r
+#endif /* TCP_TMR_INTERVAL */\r
+\r
+#ifndef TCP_FAST_INTERVAL\r
+#define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in\r
+ milliseconds */\r
+#endif /* TCP_FAST_INTERVAL */\r
+\r
+#ifndef TCP_SLOW_INTERVAL\r
+#define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in\r
+ milliseconds */\r
+#endif /* TCP_SLOW_INTERVAL */\r
+\r
+#define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */\r
+#define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */\r
+\r
+#define TCP_OOSEQ_TIMEOUT 6 /* x RTO */\r
+\r
+#define TCP_MSL 60000 /* The maximum segment lifetime in microseconds */\r
+\r
+/*\r
+ * User-settable options (used with setsockopt).\r
+ */\r
+#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */\r
+#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keepalive miliseconds */\r
+\r
+/* Keepalive values */\r
+#define TCP_KEEPDEFAULT 7200000 /* KEEPALIVE timer in miliseconds */\r
+#define TCP_KEEPINTVL 75000 /* Time between KEEPALIVE probes in miliseconds */\r
+#define TCP_KEEPCNT 9 /* Counter for KEEPALIVE probes */\r
+#define TCP_MAXIDLE TCP_KEEPCNT * TCP_KEEPINTVL /* Maximum KEEPALIVE probe time */\r
+\r
+\r
+#ifdef PACK_STRUCT_USE_INCLUDES\r
+# include "arch/bpstruct.h"\r
+#endif\r
+PACK_STRUCT_BEGIN\r
+struct tcp_hdr {\r
+ PACK_STRUCT_FIELD(u16_t src);\r
+ PACK_STRUCT_FIELD(u16_t dest);\r
+ PACK_STRUCT_FIELD(u32_t seqno);\r
+ PACK_STRUCT_FIELD(u32_t ackno);\r
+ PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);\r
+ PACK_STRUCT_FIELD(u16_t wnd);\r
+ PACK_STRUCT_FIELD(u16_t chksum);\r
+ PACK_STRUCT_FIELD(u16_t urgp);\r
+} PACK_STRUCT_STRUCT;\r
+PACK_STRUCT_END\r
+#ifdef PACK_STRUCT_USE_INCLUDES\r
+# include "arch/epstruct.h"\r
+#endif\r
+\r
+#define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)\r
+#define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)\r
+#define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)\r
+\r
+#define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))\r
+#define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))\r
+#define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))\r
+#define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))\r
+#define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )\r
+\r
+#define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \\r
+ TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))\r
+\r
+enum tcp_state {\r
+ CLOSED = 0,\r
+ LISTEN = 1,\r
+ SYN_SENT = 2,\r
+ SYN_RCVD = 3,\r
+ ESTABLISHED = 4,\r
+ FIN_WAIT_1 = 5,\r
+ FIN_WAIT_2 = 6,\r
+ CLOSE_WAIT = 7,\r
+ CLOSING = 8,\r
+ LAST_ACK = 9,\r
+ TIME_WAIT = 10\r
+};\r
+\r
+/* the TCP protocol control block */\r
+struct tcp_pcb {\r
+/** common PCB members */\r
+ IP_PCB;\r
+/** protocol specific PCB members */\r
+ struct tcp_pcb *next; /* for the linked list */\r
+ enum tcp_state state; /* TCP state */\r
+ u8_t prio;\r
+ void *callback_arg;\r
+\r
+ u16_t local_port;\r
+ u16_t remote_port;\r
+ \r
+ u8_t flags;\r
+#define TF_ACK_DELAY (u8_t)0x01U /* Delayed ACK. */\r
+#define TF_ACK_NOW (u8_t)0x02U /* Immediate ACK. */\r
+#define TF_INFR (u8_t)0x04U /* In fast recovery. */\r
+#define TF_RESET (u8_t)0x08U /* Connection was reset. */\r
+#define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */\r
+#define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */\r
+#define TF_NODELAY (u8_t)0x40U /* Disable Nagle algorithm */\r
+\r
+ /* receiver variables */\r
+ u32_t rcv_nxt; /* next seqno expected */\r
+ u16_t rcv_wnd; /* receiver window */\r
+ \r
+ /* Timers */\r
+ u32_t tmr;\r
+ u8_t polltmr, pollinterval;\r
+ \r
+ /* Retransmission timer. */\r
+ u16_t rtime;\r
+ \r
+ u16_t mss; /* maximum segment size */\r
+ \r
+ /* RTT (round trip time) estimation variables */\r
+ u32_t rttest; /* RTT estimate in 500ms ticks */\r
+ u32_t rtseq; /* sequence number being timed */\r
+ s16_t sa, sv; /* @todo document this */\r
+\r
+ u16_t rto; /* retransmission time-out */\r
+ u8_t nrtx; /* number of retransmissions */\r
+\r
+ /* fast retransmit/recovery */\r
+ u32_t lastack; /* Highest acknowledged seqno. */\r
+ u8_t dupacks;\r
+ \r
+ /* congestion avoidance/control variables */\r
+ u16_t cwnd; \r
+ u16_t ssthresh;\r
+\r
+ /* sender variables */\r
+ u32_t snd_nxt, /* next seqno to be sent */\r
+ snd_max, /* Highest seqno sent. */\r
+ snd_wnd, /* sender window */\r
+ snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last\r
+ window update. */\r
+ snd_lbb; /* Sequence number of next byte to be buffered. */\r
+\r
+ u16_t acked;\r
+ \r
+ u16_t snd_buf; /* Available buffer space for sending (in bytes). */\r
+ u8_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */\r
+ \r
+ \r
+ /* These are ordered by sequence number: */\r
+ struct tcp_seg *unsent; /* Unsent (queued) segments. */\r
+ struct tcp_seg *unacked; /* Sent but unacknowledged segments. */\r
+#if TCP_QUEUE_OOSEQ \r
+ struct tcp_seg *ooseq; /* Received out of sequence segments. */\r
+#endif /* TCP_QUEUE_OOSEQ */\r
+\r
+#if LWIP_CALLBACK_API\r
+ /* Function to be called when more send buffer space is available. */\r
+ err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);\r
+ \r
+ /* Function to be called when (in-sequence) data has arrived. */\r
+ err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);\r
+\r
+ /* Function to be called when a connection has been set up. */\r
+ err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);\r
+\r
+ /* Function to call when a listener has been connected. */\r
+ err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);\r
+\r
+ /* Function which is called periodically. */\r
+ err_t (* poll)(void *arg, struct tcp_pcb *pcb);\r
+\r
+ /* Function to be called whenever a fatal error occurs. */\r
+ void (* errf)(void *arg, err_t err);\r
+#endif /* LWIP_CALLBACK_API */\r
+\r
+ /* idle time before KEEPALIVE is sent */\r
+ u32_t keepalive;\r
+ \r
+ /* KEEPALIVE counter */\r
+ u8_t keep_cnt;\r
+};\r
+\r
+struct tcp_pcb_listen { \r
+/* Common members of all PCB types */\r
+ IP_PCB;\r
+\r
+/* Protocol specific PCB members */\r
+ struct tcp_pcb_listen *next; /* for the linked list */\r
+ \r
+ /* Even if state is obviously LISTEN this is here for\r
+ * field compatibility with tpc_pcb to which it is cast sometimes\r
+ * Until a cleaner solution emerges this is here.FIXME\r
+ */ \r
+ enum tcp_state state; /* TCP state */\r
+\r
+ u8_t prio;\r
+ void *callback_arg;\r
+ \r
+ u16_t local_port; \r
+\r
+#if LWIP_CALLBACK_API\r
+ /* Function to call when a listener has been connected. */\r
+ err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);\r
+#endif /* LWIP_CALLBACK_API */\r
+};\r
+\r
+#if LWIP_EVENT_API\r
+\r
+enum lwip_event {\r
+ LWIP_EVENT_ACCEPT,\r
+ LWIP_EVENT_SENT,\r
+ LWIP_EVENT_RECV,\r
+ LWIP_EVENT_CONNECTED,\r
+ LWIP_EVENT_POLL,\r
+ LWIP_EVENT_ERR\r
+};\r
+\r
+err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,\r
+ enum lwip_event,\r
+ struct pbuf *p,\r
+ u16_t size,\r
+ err_t err);\r
+\r
+#define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
+ LWIP_EVENT_ACCEPT, NULL, 0, err)\r
+#define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
+ LWIP_EVENT_SENT, NULL, space, ERR_OK)\r
+#define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
+ LWIP_EVENT_RECV, (p), 0, (err))\r
+#define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
+ LWIP_EVENT_CONNECTED, NULL, 0, (err))\r
+#define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
+ LWIP_EVENT_POLL, NULL, 0, ERR_OK)\r
+#define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \\r
+ LWIP_EVENT_ERR, NULL, 0, (err))\r
+#else /* LWIP_EVENT_API */\r
+#define TCP_EVENT_ACCEPT(pcb,err,ret) \\r
+ if((pcb)->accept != NULL) \\r
+ (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))\r
+#define TCP_EVENT_SENT(pcb,space,ret) \\r
+ if((pcb)->sent != NULL) \\r
+ (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))\r
+#define TCP_EVENT_RECV(pcb,p,err,ret) \\r
+ if((pcb)->recv != NULL) \\r
+ { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \\r
+ if (p) pbuf_free(p); }\r
+#define TCP_EVENT_CONNECTED(pcb,err,ret) \\r
+ if((pcb)->connected != NULL) \\r
+ (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))\r
+#define TCP_EVENT_POLL(pcb,ret) \\r
+ if((pcb)->poll != NULL) \\r
+ (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))\r
+#define TCP_EVENT_ERR(errf,arg,err) \\r
+ if((errf) != NULL) \\r
+ (errf)((arg),(err))\r
+#endif /* LWIP_EVENT_API */\r
+\r
+/* This structure represents a TCP segment on the unsent and unacked queues */\r
+struct tcp_seg {\r
+ struct tcp_seg *next; /* used when putting segements on a queue */\r
+ struct pbuf *p; /* buffer containing data + TCP header */\r
+ void *dataptr; /* pointer to the TCP data in the pbuf */\r
+ u16_t len; /* the TCP length of this segment */\r
+ struct tcp_hdr *tcphdr; /* the TCP header */\r
+};\r
+\r
+/* Internal functions and global variables: */\r
+struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);\r
+void tcp_pcb_purge(struct tcp_pcb *pcb);\r
+void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);\r
+\r
+u8_t tcp_segs_free(struct tcp_seg *seg);\r
+u8_t tcp_seg_free(struct tcp_seg *seg);\r
+struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);\r
+\r
+#define tcp_ack(pcb) if((pcb)->flags & TF_ACK_DELAY) { \\r
+ (pcb)->flags &= ~TF_ACK_DELAY; \\r
+ (pcb)->flags |= TF_ACK_NOW; \\r
+ tcp_output(pcb); \\r
+ } else { \\r
+ (pcb)->flags |= TF_ACK_DELAY; \\r
+ }\r
+\r
+#define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \\r
+ tcp_output(pcb)\r
+\r
+err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);\r
+err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,\r
+ u8_t flags, u8_t copy,\r
+ u8_t *optdata, u8_t optlen);\r
+\r
+void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);\r
+\r
+void tcp_rst(u32_t seqno, u32_t ackno,\r
+ struct ip_addr *local_ip, struct ip_addr *remote_ip,\r
+ u16_t local_port, u16_t remote_port);\r
+\r
+u32_t tcp_next_iss(void);\r
+\r
+void tcp_keepalive(struct tcp_pcb *pcb);\r
+\r
+extern struct tcp_pcb *tcp_input_pcb;\r
+extern u32_t tcp_ticks;\r
+\r
+#if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG\r
+void tcp_debug_print(struct tcp_hdr *tcphdr);\r
+void tcp_debug_print_flags(u8_t flags);\r
+void tcp_debug_print_state(enum tcp_state s);\r
+void tcp_debug_print_pcbs(void);\r
+int tcp_pcbs_sane(void);\r
+#else\r
+# define tcp_debug_print(tcphdr)\r
+# define tcp_debug_print_flags(flags)\r
+# define tcp_debug_print_state(s)\r
+# define tcp_debug_print_pcbs()\r
+# define tcp_pcbs_sane() 1\r
+#endif /* TCP_DEBUG */\r
+\r
+#if NO_SYS\r
+#define tcp_timer_needed()\r
+#else\r
+void tcp_timer_needed(void);\r
+#endif\r
+\r
+/* The TCP PCB lists. */\r
+union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */\r
+ struct tcp_pcb_listen *listen_pcbs; \r
+ struct tcp_pcb *pcbs;\r
+};\r
+extern union tcp_listen_pcbs_t tcp_listen_pcbs;\r
+extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a\r
+ state in which they accept or send\r
+ data. */\r
+extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */\r
+\r
+extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */\r
+\r
+/* Axioms about the above lists: \r
+ 1) Every TCP PCB that is not CLOSED is in one of the lists.\r
+ 2) A PCB is only in one of the lists.\r
+ 3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.\r
+ 4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.\r
+*/\r
+\r
+/* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB\r
+ with a PCB list or removes a PCB from a list, respectively. */\r
+#if 0\r
+#define TCP_REG(pcbs, npcb) do {\\r
+ LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \\r
+ for(tcp_tmp_pcb = *pcbs; \\r
+ tcp_tmp_pcb != NULL; \\r
+ tcp_tmp_pcb = tcp_tmp_pcb->next) { \\r
+ LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \\r
+ } \\r
+ LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \\r
+ npcb->next = *pcbs; \\r
+ LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \\r
+ *(pcbs) = npcb; \\r
+ LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \\r
+ tcp_timer_needed(); \\r
+ } while(0)\r
+#define TCP_RMV(pcbs, npcb) do { \\r
+ LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \\r
+ LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \\r
+ if(*pcbs == npcb) { \\r
+ *pcbs = (*pcbs)->next; \\r
+ } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \\r
+ if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \\r
+ tcp_tmp_pcb->next = npcb->next; \\r
+ break; \\r
+ } \\r
+ } \\r
+ npcb->next = NULL; \\r
+ LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \\r
+ LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \\r
+ } while(0)\r
+\r
+#else /* LWIP_DEBUG */\r
+#define TCP_REG(pcbs, npcb) do { \\r
+ npcb->next = *pcbs; \\r
+ *(pcbs) = npcb; \\r
+ tcp_timer_needed(); \\r
+ } while(0)\r
+#define TCP_RMV(pcbs, npcb) do { \\r
+ if(*(pcbs) == npcb) { \\r
+ (*(pcbs)) = (*pcbs)->next; \\r
+ } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \\r
+ if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \\r
+ tcp_tmp_pcb->next = npcb->next; \\r
+ break; \\r
+ } \\r
+ } \\r
+ npcb->next = NULL; \\r
+ } while(0)\r
+#endif /* LWIP_DEBUG */\r
+#endif /* __LWIP_TCP_H__ */\r
+\r
+\r
+\r