A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcp.h Source File

tcp.h

00001 /*
00002  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  * 
00005  * Redistribution and use in source and binary forms, with or without modification, 
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission. 
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00025  * OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  * 
00029  * Author: Adam Dunkels <adam@sics.se>
00030  *
00031  */
00032 #ifndef __LWIP_TCP_H__
00033 #define __LWIP_TCP_H__
00034 
00035 #include "lwip/opt.h"
00036 
00037 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
00038 
00039 #include "lwip/sys.h"
00040 #include "lwip/mem.h"
00041 #include "lwip/pbuf.h"
00042 #include "lwip/ip.h"
00043 #include "lwip/icmp.h"
00044 #include "lwip/err.h"
00045 
00046 #ifdef __cplusplus
00047 // XXX: WTF
00048 //extern "C" {
00049 #endif
00050 
00051 struct tcp_pcb;
00052 
00053 /* Functions for interfacing with TCP: */
00054 
00055 /* Lower layer interface to TCP: */
00056 #define tcp_init() /* Compatibility define, not init needed. */
00057 void             tcp_tmr     (void);  /* Must be called every
00058                                          TCP_TMR_INTERVAL
00059                                          ms. (Typically 250 ms). */
00060 /* Application program's interface: */
00061 struct tcp_pcb * tcp_new     (void);
00062 struct tcp_pcb * tcp_alloc   (u8_t prio);
00063 
00064 void             tcp_arg     (struct tcp_pcb *pcb, void *arg);
00065 void             tcp_accept  (struct tcp_pcb *pcb,
00066                               err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
00067                  err_t err));
00068 void             tcp_recv    (struct tcp_pcb *pcb,
00069                               err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
00070                               struct pbuf *p, err_t err));
00071 void             tcp_sent    (struct tcp_pcb *pcb,
00072                               err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
00073                               u16_t len));
00074 void             tcp_poll    (struct tcp_pcb *pcb,
00075                               err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
00076                               u8_t interval);
00077 void             tcp_err     (struct tcp_pcb *pcb,
00078                               void (* err)(void *arg, err_t err));
00079 
00080 #define          tcp_mss(pcb)      ((pcb)->mss)
00081 #define          tcp_sndbuf(pcb)   ((pcb)->snd_buf)
00082 
00083 #if TCP_LISTEN_BACKLOG
00084 #define          tcp_accepted(pcb) (((struct tcp_pcb_listen *)(pcb))->accepts_pending--)
00085 #else  /* TCP_LISTEN_BACKLOG */
00086 #define          tcp_accepted(pcb)
00087 #endif /* TCP_LISTEN_BACKLOG */
00088 
00089 void             tcp_recved  (struct tcp_pcb *pcb, u16_t len);
00090 err_t            tcp_bind    (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
00091                               u16_t port);
00092 err_t            tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
00093                               u16_t port, err_t (* connected)(void *arg,
00094                               struct tcp_pcb *tpcb,
00095                               err_t err));
00096 
00097 struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
00098 #define          tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
00099 
00100 void             tcp_abort   (struct tcp_pcb *pcb);
00101 err_t            tcp_close   (struct tcp_pcb *pcb);
00102 
00103 /* Flags for "apiflags" parameter in tcp_write and tcp_enqueue */
00104 #define TCP_WRITE_FLAG_COPY 0x01
00105 #define TCP_WRITE_FLAG_MORE 0x02
00106 
00107 err_t            tcp_write   (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
00108                               u8_t apiflags);
00109 
00110 void             tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
00111 
00112 #define TCP_PRIO_MIN    1
00113 #define TCP_PRIO_NORMAL 64
00114 #define TCP_PRIO_MAX    127
00115 
00116 /* It is also possible to call these two functions at the right
00117    intervals (instead of calling tcp_tmr()). */
00118 void             tcp_slowtmr (void);
00119 void             tcp_fasttmr (void);
00120 
00121 
00122 /* Only used by IP to pass a TCP segment to TCP: */
00123 void             tcp_input   (struct pbuf *p, struct netif *inp);
00124 /* Used within the TCP code only: */
00125 err_t            tcp_output  (struct tcp_pcb *pcb);
00126 void             tcp_rexmit  (struct tcp_pcb *pcb);
00127 void             tcp_rexmit_rto  (struct tcp_pcb *pcb);
00128 
00129 /**
00130  * This is the Nagle algorithm: inhibit the sending of new TCP
00131  * segments when new outgoing data arrives from the user if any
00132  * previously transmitted data on the connection remains
00133  * unacknowledged.
00134  */
00135 #define tcp_do_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \
00136                             ((tpcb)->flags & TF_NODELAY) || \
00137                             (((tpcb)->unsent != NULL) && ((tpcb)->unsent->next != NULL))) ? \
00138                                 1 : 0)
00139 #define tcp_output_nagle(tpcb) (tcp_do_output_nagle(tpcb) ? tcp_output(tpcb) : ERR_OK)
00140 
00141 
00142 /** This returns a TCP header option for MSS in an u32_t */
00143 #define TCP_BUILD_MSS_OPTION()  htonl(((u32_t)2 << 24) | \
00144                                 ((u32_t)4 << 16) | \
00145                                 (((u32_t)TCP_MSS / 256) << 8) | \
00146                                 (TCP_MSS & 255))
00147 
00148 #define TCP_SEQ_LT(a,b)     ((s32_t)((a)-(b)) < 0)
00149 #define TCP_SEQ_LEQ(a,b)    ((s32_t)((a)-(b)) <= 0)
00150 #define TCP_SEQ_GT(a,b)     ((s32_t)((a)-(b)) > 0)
00151 #define TCP_SEQ_GEQ(a,b)    ((s32_t)((a)-(b)) >= 0)
00152 /* is b<=a<=c? */
00153 #if 0 /* see bug #10548 */
00154 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
00155 #endif
00156 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
00157 #define TCP_FIN 0x01U
00158 #define TCP_SYN 0x02U
00159 #define TCP_RST 0x04U
00160 #define TCP_PSH 0x08U
00161 #define TCP_ACK 0x10U
00162 #define TCP_URG 0x20U
00163 #define TCP_ECE 0x40U
00164 #define TCP_CWR 0x80U
00165 
00166 #define TCP_FLAGS 0x3fU
00167 
00168 /* Length of the TCP header, excluding options. */
00169 #define TCP_HLEN 20
00170 
00171 #ifndef TCP_TMR_INTERVAL
00172 #define TCP_TMR_INTERVAL       250  /* The TCP timer interval in milliseconds. */
00173 #endif /* TCP_TMR_INTERVAL */
00174 
00175 #ifndef TCP_FAST_INTERVAL
00176 #define TCP_FAST_INTERVAL      TCP_TMR_INTERVAL /* the fine grained timeout in milliseconds */
00177 #endif /* TCP_FAST_INTERVAL */
00178 
00179 #ifndef TCP_SLOW_INTERVAL
00180 #define TCP_SLOW_INTERVAL      (2*TCP_TMR_INTERVAL)  /* the coarse grained timeout in milliseconds */
00181 #endif /* TCP_SLOW_INTERVAL */
00182 
00183 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
00184 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
00185 
00186 #define TCP_OOSEQ_TIMEOUT        6U /* x RTO */
00187 
00188 #ifndef TCP_MSL
00189 #define TCP_MSL 60000U /* The maximum segment lifetime in milliseconds */
00190 #endif
00191 
00192 /* Keepalive values, compliant with RFC 1122. Don't change this unless you know what you're doing */
00193 #ifndef  TCP_KEEPIDLE_DEFAULT
00194 #define  TCP_KEEPIDLE_DEFAULT     7200000UL /* Default KEEPALIVE timer in milliseconds */
00195 #endif
00196 
00197 #ifndef  TCP_KEEPINTVL_DEFAULT
00198 #define  TCP_KEEPINTVL_DEFAULT    75000UL   /* Default Time between KEEPALIVE probes in milliseconds */
00199 #endif
00200 
00201 #ifndef  TCP_KEEPCNT_DEFAULT
00202 #define  TCP_KEEPCNT_DEFAULT      9U        /* Default Counter for KEEPALIVE probes */
00203 #endif
00204 
00205 #define  TCP_MAXIDLE              TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT  /* Maximum KEEPALIVE probe time */
00206 
00207 /* Fields are (of course) in network byte order.
00208  * Some fields are converted to host byte order in tcp_input().
00209  */
00210 #ifdef PACK_STRUCT_USE_INCLUDES
00211 #  include "arch/bpstruct.h"
00212 #endif
00213 PACK_STRUCT_BEGIN
00214 struct tcp_hdr {
00215   PACK_STRUCT_FIELD(u16_t src);
00216   PACK_STRUCT_FIELD(u16_t dest);
00217   PACK_STRUCT_FIELD(u32_t seqno);
00218   PACK_STRUCT_FIELD(u32_t ackno);
00219   PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
00220   PACK_STRUCT_FIELD(u16_t wnd);
00221   PACK_STRUCT_FIELD(u16_t chksum);
00222   PACK_STRUCT_FIELD(u16_t urgp);
00223 } PACK_STRUCT_STRUCT;
00224 PACK_STRUCT_END
00225 #ifdef PACK_STRUCT_USE_INCLUDES
00226 #  include "arch/epstruct.h"
00227 #endif
00228 
00229 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
00230 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
00231 #define TCPH_FLAGS(phdr)  (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
00232 
00233 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
00234 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
00235 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))
00236 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))
00237 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
00238 
00239 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
00240           TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
00241 
00242 enum tcp_state {
00243   CLOSED      = 0,
00244   LISTEN      = 1,
00245   SYN_SENT    = 2,
00246   SYN_RCVD    = 3,
00247   ESTABLISHED = 4,
00248   FIN_WAIT_1  = 5,
00249   FIN_WAIT_2  = 6,
00250   CLOSE_WAIT  = 7,
00251   CLOSING     = 8,
00252   LAST_ACK    = 9,
00253   TIME_WAIT   = 10
00254 };
00255 
00256 /** Flags used on input processing, not on pcb->flags
00257 */
00258 #define TF_RESET     (u8_t)0x08U   /* Connection was reset. */
00259 #define TF_CLOSED    (u8_t)0x10U   /* Connection was sucessfully closed. */
00260 #define TF_GOT_FIN   (u8_t)0x20U   /* Connection was closed by the remote end. */
00261 
00262 /**
00263  * members common to struct tcp_pcb and struct tcp_listen_pcb
00264  */
00265 #define TCP_PCB_COMMON(type) \
00266   type *next; /* for the linked list */ \
00267   enum tcp_state state; /* TCP state */ \
00268   u8_t prio; \
00269   void *callback_arg; \
00270   /* ports are in host byte order */ \
00271   u16_t local_port
00272 
00273 /* the TCP protocol control block */
00274 struct tcp_pcb {
00275 /** common PCB members */
00276   IP_PCB;
00277 /** protocol specific PCB members */
00278   TCP_PCB_COMMON(struct tcp_pcb);
00279 
00280   /* ports are in host byte order */
00281   u16_t remote_port;
00282   
00283   u8_t flags;
00284 #define TF_ACK_DELAY   (u8_t)0x01U   /* Delayed ACK. */
00285 #define TF_ACK_NOW     (u8_t)0x02U   /* Immediate ACK. */
00286 #define TF_INFR        (u8_t)0x04U   /* In fast recovery. */
00287 #define TF_FIN         (u8_t)0x20U   /* Connection was closed locally (FIN segment enqueued). */
00288 #define TF_NODELAY     (u8_t)0x40U   /* Disable Nagle algorithm */
00289 #define TF_NAGLEMEMERR (u8_t)0x80U /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
00290 
00291   /* the rest of the fields are in host byte order
00292      as we have to do some math with them */
00293   /* receiver variables */
00294   u32_t rcv_nxt;   /* next seqno expected */
00295   u16_t rcv_wnd;   /* receiver window */
00296   u16_t rcv_ann_wnd; /* announced receive window */
00297 
00298   /* Timers */
00299   u32_t tmr;
00300   u8_t polltmr, pollinterval;
00301   
00302   /* Retransmission timer. */
00303   s16_t rtime;
00304   
00305   u16_t mss;   /* maximum segment size */
00306   
00307   /* RTT (round trip time) estimation variables */
00308   u32_t rttest; /* RTT estimate in 500ms ticks */
00309   u32_t rtseq;  /* sequence number being timed */
00310   s16_t sa, sv; /* @todo document this */
00311 
00312   s16_t rto;    /* retransmission time-out */
00313   u8_t nrtx;    /* number of retransmissions */
00314 
00315   /* fast retransmit/recovery */
00316   u32_t lastack; /* Highest acknowledged seqno. */
00317   u8_t dupacks;
00318   
00319   /* congestion avoidance/control variables */
00320   u16_t cwnd;  
00321   u16_t ssthresh;
00322 
00323   /* sender variables */
00324   u32_t snd_nxt,   /* next seqno to be sent */
00325     snd_max;       /* Highest seqno sent. */
00326   u16_t snd_wnd;   /* sender window */
00327   u32_t snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last
00328                              window update. */
00329     snd_lbb;       /* Sequence number of next byte to be buffered. */
00330 
00331   u16_t acked;
00332   
00333   u16_t snd_buf;   /* Available buffer space for sending (in bytes). */
00334 #define TCP_SNDQUEUELEN_OVERFLOW (0xffff-3)
00335   u16_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
00336   
00337   
00338   /* These are ordered by sequence number: */
00339   struct tcp_seg *unsent;   /* Unsent (queued) segments. */
00340   struct tcp_seg *unacked;  /* Sent but unacknowledged segments. */
00341 #if TCP_QUEUE_OOSEQ  
00342   struct tcp_seg *ooseq;    /* Received out of sequence segments. */
00343 #endif /* TCP_QUEUE_OOSEQ */
00344 
00345   struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
00346 
00347 #if LWIP_CALLBACK_API
00348   /* Function to be called when more send buffer space is available.
00349    * @param arg user-supplied argument (tcp_pcb.callback_arg)
00350    * @param pcb the tcp_pcb which has send buffer space available
00351    * @param space the amount of bytes available
00352    * @return ERR_OK: try to send some data by calling tcp_output
00353    */
00354   err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
00355   
00356   /* Function to be called when (in-sequence) data has arrived.
00357    * @param arg user-supplied argument (tcp_pcb.callback_arg)
00358    * @param pcb the tcp_pcb for which data has arrived
00359    * @param p the packet buffer which arrived
00360    * @param err an error argument (TODO: that is current always ERR_OK?)
00361    * @return ERR_OK: try to send some data by calling tcp_output
00362    */
00363   err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
00364 
00365   /* Function to be called when a connection has been set up.
00366    * @param arg user-supplied argument (tcp_pcb.callback_arg)
00367    * @param pcb the tcp_pcb that now is connected
00368    * @param err an error argument (TODO: that is current always ERR_OK?)
00369    * @return value is currently ignored
00370    */
00371   err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
00372 
00373   /* Function to call when a listener has been connected.
00374    * @param arg user-supplied argument (tcp_pcb.callback_arg)
00375    * @param pcb a new tcp_pcb that now is connected
00376    * @param err an error argument (TODO: that is current always ERR_OK?)
00377    * @return ERR_OK: accept the new connection,
00378    *                 any other err_t abortsthe new connection
00379    */
00380   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
00381 
00382   /* Function which is called periodically.
00383    * The period can be adjusted in multiples of the TCP slow timer interval
00384    * by changing tcp_pcb.polltmr.
00385    * @param arg user-supplied argument (tcp_pcb.callback_arg)
00386    * @param pcb the tcp_pcb to poll for
00387    * @return ERR_OK: try to send some data by calling tcp_output
00388    */
00389   err_t (* poll)(void *arg, struct tcp_pcb *pcb);
00390 
00391   /* Function to be called whenever a fatal error occurs.
00392    * There is no pcb parameter since most of the times, the pcb is
00393    * already deallocated (or there is no pcb) when this function is called.
00394    * @param arg user-supplied argument (tcp_pcb.callback_arg)
00395    * @param err an indication why the error callback is called:
00396    *            ERR_ABRT: aborted through tcp_abort or by a TCP timer
00397    *            ERR_RST: the connection was reset by the remote host
00398    */
00399   void (* errf)(void *arg, err_t err);
00400 #endif /* LWIP_CALLBACK_API */
00401 
00402   /* idle time before KEEPALIVE is sent */
00403   u32_t keep_idle;
00404 #if LWIP_TCP_KEEPALIVE
00405   u32_t keep_intvl;
00406   u32_t keep_cnt;
00407 #endif /* LWIP_TCP_KEEPALIVE */
00408   
00409   /* Persist timer counter */
00410   u32_t persist_cnt;
00411   /* Persist timer back-off */
00412   u8_t persist_backoff;
00413 
00414   /* KEEPALIVE counter */
00415   u8_t keep_cnt_sent;
00416 };
00417 
00418 struct tcp_pcb_listen {  
00419 /* Common members of all PCB types */
00420   IP_PCB;
00421 /* Protocol specific PCB members */
00422   TCP_PCB_COMMON(struct tcp_pcb_listen);
00423 
00424 #if LWIP_CALLBACK_API
00425   /* Function to call when a listener has been connected.
00426    * @param arg user-supplied argument (tcp_pcb.callback_arg)
00427    * @param pcb a new tcp_pcb that now is connected
00428    * @param err an error argument (TODO: that is current always ERR_OK?)
00429    * @return ERR_OK: accept the new connection,
00430    *                 any other err_t abortsthe new connection
00431    */
00432   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
00433 #endif /* LWIP_CALLBACK_API */
00434 #if TCP_LISTEN_BACKLOG
00435   u8_t backlog;
00436   u8_t accepts_pending;
00437 #endif /* TCP_LISTEN_BACKLOG */
00438 };
00439 
00440 #if LWIP_EVENT_API
00441 
00442 enum lwip_event {
00443   LWIP_EVENT_ACCEPT,
00444   LWIP_EVENT_SENT,
00445   LWIP_EVENT_RECV,
00446   LWIP_EVENT_CONNECTED,
00447   LWIP_EVENT_POLL,
00448   LWIP_EVENT_ERR
00449 };
00450 
00451 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
00452          enum lwip_event,
00453          struct pbuf *p,
00454          u16_t size,
00455          err_t err);
00456 
00457 #define TCP_EVENT_ACCEPT(pcb,err,ret)    ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
00458                 LWIP_EVENT_ACCEPT, NULL, 0, err)
00459 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
00460                    LWIP_EVENT_SENT, NULL, space, ERR_OK)
00461 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
00462                 LWIP_EVENT_RECV, (p), 0, (err))
00463 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
00464                 LWIP_EVENT_CONNECTED, NULL, 0, (err))
00465 #define TCP_EVENT_POLL(pcb,ret)       ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
00466                 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
00467 #define TCP_EVENT_ERR(errf,arg,err)  lwip_tcp_event((arg), NULL, \
00468                 LWIP_EVENT_ERR, NULL, 0, (err))
00469 #else /* LWIP_EVENT_API */
00470 #define TCP_EVENT_ACCEPT(pcb,err,ret)     \
00471                         if((pcb)->accept != NULL) \
00472                         (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))
00473 #define TCP_EVENT_SENT(pcb,space,ret) \
00474                         if((pcb)->sent != NULL) \
00475                         (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))
00476 #define TCP_EVENT_RECV(pcb,p,err,ret) \
00477                         if((pcb)->recv != NULL) \
00478                         { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \
00479                           ret = ERR_OK; \
00480                           if (p) pbuf_free(p); }
00481 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
00482                         if((pcb)->connected != NULL) \
00483                         (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))
00484 #define TCP_EVENT_POLL(pcb,ret) \
00485                         if((pcb)->poll != NULL) \
00486                         (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))
00487 #define TCP_EVENT_ERR(errf,arg,err) \
00488                         if((errf) != NULL) \
00489                         (errf)((arg),(err))
00490 #endif /* LWIP_EVENT_API */
00491 
00492 /* This structure represents a TCP segment on the unsent and unacked queues */
00493 struct tcp_seg {
00494   struct tcp_seg *next;    /* used when putting segements on a queue */
00495   struct pbuf *p;          /* buffer containing data + TCP header */
00496   void *dataptr;           /* pointer to the TCP data in the pbuf */
00497   u16_t len;               /* the TCP length of this segment */
00498   struct tcp_hdr *tcphdr;  /* the TCP header */
00499 };
00500 
00501 /* Internal functions and global variables: */
00502 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
00503 void tcp_pcb_purge(struct tcp_pcb *pcb);
00504 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
00505 
00506 u8_t tcp_segs_free(struct tcp_seg *seg);
00507 u8_t tcp_seg_free(struct tcp_seg *seg);
00508 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
00509 
00510 #define tcp_ack(pcb)     if((pcb)->flags & TF_ACK_DELAY) { \
00511                             (pcb)->flags &= ~TF_ACK_DELAY; \
00512                             (pcb)->flags |= TF_ACK_NOW; \
00513                             tcp_output(pcb); \
00514                          } else { \
00515                             (pcb)->flags |= TF_ACK_DELAY; \
00516                          }
00517 
00518 #define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
00519                          tcp_output(pcb)
00520 
00521 err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
00522 err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
00523     u8_t flags, u8_t apiflags,
00524                 u8_t *optdata, u8_t optlen);
00525 
00526 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
00527 
00528 void tcp_rst(u32_t seqno, u32_t ackno,
00529        struct ip_addr *local_ip, struct ip_addr *remote_ip,
00530        u16_t local_port, u16_t remote_port);
00531 
00532 u32_t tcp_next_iss(void);
00533 
00534 void tcp_keepalive(struct tcp_pcb *pcb);
00535 void tcp_zero_window_probe(struct tcp_pcb *pcb);
00536 
00537 #if TCP_CALCULATE_EFF_SEND_MSS
00538 u16_t tcp_eff_send_mss(u16_t sendmss, struct ip_addr *addr);
00539 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
00540 
00541 extern struct tcp_pcb *tcp_input_pcb;
00542 extern u32_t tcp_ticks;
00543 
00544 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
00545 void tcp_debug_print(struct tcp_hdr *tcphdr);
00546 void tcp_debug_print_flags(u8_t flags);
00547 void tcp_debug_print_state(enum tcp_state s);
00548 void tcp_debug_print_pcbs(void);
00549 s16_t tcp_pcbs_sane(void);
00550 #else
00551 #  define tcp_debug_print(tcphdr)
00552 #  define tcp_debug_print_flags(flags)
00553 #  define tcp_debug_print_state(s)
00554 #  define tcp_debug_print_pcbs()
00555 #  define tcp_pcbs_sane() 1
00556 #endif /* TCP_DEBUG */
00557 
00558 #if NO_SYS
00559 #define tcp_timer_needed()
00560 #else
00561 void tcp_timer_needed(void);
00562 #endif
00563 
00564 /* The TCP PCB lists. */
00565 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
00566   struct tcp_pcb_listen *listen_pcbs; 
00567   struct tcp_pcb *pcbs;
00568 };
00569 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
00570 extern struct tcp_pcb *tcp_active_pcbs;  /* List of all TCP PCBs that are in a
00571               state in which they accept or send
00572               data. */
00573 extern struct tcp_pcb *tcp_tw_pcbs;      /* List of all TCP PCBs in TIME-WAIT. */
00574 
00575 extern struct tcp_pcb *tcp_tmp_pcb;      /* Only used for temporary storage. */
00576 
00577 /* Axioms about the above lists:   
00578    1) Every TCP PCB that is not CLOSED is in one of the lists.
00579    2) A PCB is only in one of the lists.
00580    3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
00581    4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
00582 */
00583 
00584 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
00585    with a PCB list or removes a PCB from a list, respectively. */
00586 #if 0
00587 #define TCP_REG(pcbs, npcb) do {\
00588                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
00589                             for(tcp_tmp_pcb = *pcbs; \
00590           tcp_tmp_pcb != NULL; \
00591         tcp_tmp_pcb = tcp_tmp_pcb->next) { \
00592                                 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
00593                             } \
00594                             LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
00595                             npcb->next = *pcbs; \
00596                             LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
00597                             *(pcbs) = npcb; \
00598                             LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
00599               tcp_timer_needed(); \
00600                             } while(0)
00601 #define TCP_RMV(pcbs, npcb) do { \
00602                             LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
00603                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
00604                             if(*pcbs == npcb) { \
00605                                *pcbs = (*pcbs)->next; \
00606                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
00607                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
00608                                   tcp_tmp_pcb->next = npcb->next; \
00609                                   break; \
00610                                } \
00611                             } \
00612                             npcb->next = NULL; \
00613                             LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
00614                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
00615                             } while(0)
00616 
00617 #else /* LWIP_DEBUG */
00618 #define TCP_REG(pcbs, npcb) do { \
00619                             npcb->next = *pcbs; \
00620                             *(pcbs) = npcb; \
00621               tcp_timer_needed(); \
00622                             } while(0)
00623 #define TCP_RMV(pcbs, npcb) do { \
00624                             if(*(pcbs) == npcb) { \
00625                                (*(pcbs)) = (*pcbs)->next; \
00626                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
00627                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
00628                                   tcp_tmp_pcb->next = npcb->next; \
00629                                   break; \
00630                                } \
00631                             } \
00632                             npcb->next = NULL; \
00633                             } while(0)
00634 #endif /* LWIP_DEBUG */
00635 
00636 #ifdef __cplusplus
00637 // XXX: WTF
00638 //}
00639 #endif
00640 
00641 #endif /* LWIP_TCP */
00642 
00643 #endif /* __LWIP_TCP_H__ */