]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/include/lwip/tcp.h
Changed initialization: many init functions are not needed any more since we now...
[pes-rpp/rpp-lwip.git] / src / include / lwip / tcp.h
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved. 
4  * 
5  * Redistribution and use in source and binary forms, with or without modification, 
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission. 
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  * 
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 #ifndef __LWIP_TCP_H__
33 #define __LWIP_TCP_H__
34
35 #include "lwip/opt.h"
36
37 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
38
39 #include "lwip/sys.h"
40 #include "lwip/mem.h"
41 #include "lwip/pbuf.h"
42 #include "lwip/ip.h"
43 #include "lwip/icmp.h"
44 #include "lwip/err.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 struct tcp_pcb;
51
52 /* Functions for interfacing with TCP: */
53
54 /* Lower layer interface to TCP: */
55 #define tcp_init() /* Compatibility define, not init needed. */
56 void             tcp_tmr     (void);  /* Must be called every
57                                          TCP_TMR_INTERVAL
58                                          ms. (Typically 250 ms). */
59 /* Application program's interface: */
60 struct tcp_pcb * tcp_new     (void);
61 struct tcp_pcb * tcp_alloc   (u8_t prio);
62
63 void             tcp_arg     (struct tcp_pcb *pcb, void *arg);
64 void             tcp_accept  (struct tcp_pcb *pcb,
65                               err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
66                  err_t err));
67 void             tcp_recv    (struct tcp_pcb *pcb,
68                               err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
69                               struct pbuf *p, err_t err));
70 void             tcp_sent    (struct tcp_pcb *pcb,
71                               err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
72                               u16_t len));
73 void             tcp_poll    (struct tcp_pcb *pcb,
74                               err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
75                               u8_t interval);
76 void             tcp_err     (struct tcp_pcb *pcb,
77                               void (* err)(void *arg, err_t err));
78
79 #define          tcp_mss(pcb)      ((pcb)->mss)
80 #define          tcp_sndbuf(pcb)   ((pcb)->snd_buf)
81
82 void             tcp_recved  (struct tcp_pcb *pcb, u16_t len);
83 err_t            tcp_bind    (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
84                               u16_t port);
85 err_t            tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
86                               u16_t port, err_t (* connected)(void *arg,
87                               struct tcp_pcb *tpcb,
88                               err_t err));
89 struct tcp_pcb * tcp_listen  (struct tcp_pcb *pcb);
90 void             tcp_abort   (struct tcp_pcb *pcb);
91 err_t            tcp_close   (struct tcp_pcb *pcb);
92 err_t            tcp_write   (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
93                               u8_t copy);
94
95 void             tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
96
97 #define TCP_PRIO_MIN    1
98 #define TCP_PRIO_NORMAL 64
99 #define TCP_PRIO_MAX    127
100
101 /* It is also possible to call these two functions at the right
102    intervals (instead of calling tcp_tmr()). */
103 void             tcp_slowtmr (void);
104 void             tcp_fasttmr (void);
105
106
107 /* Only used by IP to pass a TCP segment to TCP: */
108 void             tcp_input   (struct pbuf *p, struct netif *inp);
109 /* Used within the TCP code only: */
110 err_t            tcp_output  (struct tcp_pcb *pcb);
111 void             tcp_rexmit  (struct tcp_pcb *pcb);
112 void             tcp_rexmit_rto  (struct tcp_pcb *pcb);
113
114 /**
115  * This is the Nagle algorithm: inhibit the sending of new TCP
116  * segments when new outgoing data arrives from the user if any
117  * previously transmitted data on the connection remains
118  * unacknowledged.
119  */
120 #define tcp_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \
121                             ((tpcb)->flags & TF_NODELAY) || \
122                             (((tpcb)->unsent != NULL) && ((tpcb)->unsent->next != NULL))) ? \
123                                 tcp_output(tpcb) : ERR_OK)
124
125
126
127 #define TCP_SEQ_LT(a,b)     ((s32_t)((a)-(b)) < 0)
128 #define TCP_SEQ_LEQ(a,b)    ((s32_t)((a)-(b)) <= 0)
129 #define TCP_SEQ_GT(a,b)     ((s32_t)((a)-(b)) > 0)
130 #define TCP_SEQ_GEQ(a,b)    ((s32_t)((a)-(b)) >= 0)
131 /* is b<=a<=c? */
132 #if 0 /* see bug #10548 */
133 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
134 #endif
135 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
136 #define TCP_FIN 0x01U
137 #define TCP_SYN 0x02U
138 #define TCP_RST 0x04U
139 #define TCP_PSH 0x08U
140 #define TCP_ACK 0x10U
141 #define TCP_URG 0x20U
142 #define TCP_ECE 0x40U
143 #define TCP_CWR 0x80U
144
145 #define TCP_FLAGS 0x3fU
146
147 /* Length of the TCP header, excluding options. */
148 #define TCP_HLEN 20
149
150 #ifndef TCP_TMR_INTERVAL
151 #define TCP_TMR_INTERVAL       250  /* The TCP timer interval in milliseconds. */
152 #endif /* TCP_TMR_INTERVAL */
153
154 #ifndef TCP_FAST_INTERVAL
155 #define TCP_FAST_INTERVAL      TCP_TMR_INTERVAL /* the fine grained timeout in milliseconds */
156 #endif /* TCP_FAST_INTERVAL */
157
158 #ifndef TCP_SLOW_INTERVAL
159 #define TCP_SLOW_INTERVAL      (2*TCP_TMR_INTERVAL)  /* the coarse grained timeout in milliseconds */
160 #endif /* TCP_SLOW_INTERVAL */
161
162 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
163 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
164
165 #define TCP_OOSEQ_TIMEOUT        6U /* x RTO */
166
167 #define TCP_MSL 60000U /* The maximum segment lifetime in microseconds */
168
169 /* Keepalive values, compliant with RFC 1122. Don't change this unless you know what you're doing */
170 #ifndef  TCP_KEEPIDLE_DEFAULT
171 #define  TCP_KEEPIDLE_DEFAULT     7200000UL /* Default KEEPALIVE timer in milliseconds */
172 #endif
173
174 #ifndef  TCP_KEEPINTVL_DEFAULT
175 #define  TCP_KEEPINTVL_DEFAULT    75000UL   /* Default Time between KEEPALIVE probes in milliseconds */
176 #endif
177
178 #ifndef  TCP_KEEPCNT_DEFAULT
179 #define  TCP_KEEPCNT_DEFAULT      9U        /* Default Counter for KEEPALIVE probes */
180 #endif
181
182 #define  TCP_MAXIDLE              TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT  /* Maximum KEEPALIVE probe time */
183
184 /* Fields are (of course) in network byte order.
185  * Some fields are converted to host byte order in tcp_input().
186  */
187 #ifdef PACK_STRUCT_USE_INCLUDES
188 #  include "arch/bpstruct.h"
189 #endif
190 PACK_STRUCT_BEGIN
191 struct tcp_hdr {
192   PACK_STRUCT_FIELD(u16_t src);
193   PACK_STRUCT_FIELD(u16_t dest);
194   PACK_STRUCT_FIELD(u32_t seqno);
195   PACK_STRUCT_FIELD(u32_t ackno);
196   PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
197   PACK_STRUCT_FIELD(u16_t wnd);
198   PACK_STRUCT_FIELD(u16_t chksum);
199   PACK_STRUCT_FIELD(u16_t urgp);
200 } PACK_STRUCT_STRUCT;
201 PACK_STRUCT_END
202 #ifdef PACK_STRUCT_USE_INCLUDES
203 #  include "arch/epstruct.h"
204 #endif
205
206 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
207 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
208 #define TCPH_FLAGS(phdr)  (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
209
210 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
211 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
212 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))
213 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))
214 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
215
216 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
217           TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
218
219 enum tcp_state {
220   CLOSED      = 0,
221   LISTEN      = 1,
222   SYN_SENT    = 2,
223   SYN_RCVD    = 3,
224   ESTABLISHED = 4,
225   FIN_WAIT_1  = 5,
226   FIN_WAIT_2  = 6,
227   CLOSE_WAIT  = 7,
228   CLOSING     = 8,
229   LAST_ACK    = 9,
230   TIME_WAIT   = 10
231 };
232
233 /**
234  * members common to struct tcp_pcb and struct tcp_listen_pcb
235  */
236 #define TCP_PCB_COMMON(type) \
237   type *next; /* for the linked list */ \
238   enum tcp_state state; /* TCP state */ \
239   u8_t prio; \
240   void *callback_arg; \
241   /* ports are in host byte order */ \
242   u16_t local_port
243
244 /* the TCP protocol control block */
245 struct tcp_pcb {
246 /** common PCB members */
247   IP_PCB;
248 /** protocol specific PCB members */
249   TCP_PCB_COMMON(struct tcp_pcb);
250
251   /* ports are in host byte order */
252   u16_t remote_port;
253   
254   u8_t flags;
255 #define TF_ACK_DELAY (u8_t)0x01U   /* Delayed ACK. */
256 #define TF_ACK_NOW   (u8_t)0x02U   /* Immediate ACK. */
257 #define TF_INFR      (u8_t)0x04U   /* In fast recovery. */
258 #define TF_RESET     (u8_t)0x08U   /* Connection was reset. */
259 #define TF_CLOSED    (u8_t)0x10U   /* Connection was sucessfully closed. */
260 #define TF_GOT_FIN   (u8_t)0x20U   /* Connection was closed by the remote end. */
261 #define TF_NODELAY   (u8_t)0x40U   /* Disable Nagle algorithm */
262
263   /* the rest of the fields are in host byte order
264      as we have to do some math with them */
265   /* receiver variables */
266   u32_t rcv_nxt;   /* next seqno expected */
267   u16_t rcv_wnd;   /* receiver window */
268   
269   /* Timers */
270   u32_t tmr;
271   u8_t polltmr, pollinterval;
272   
273   /* Retransmission timer. */
274   s16_t rtime;
275   
276   u16_t mss;   /* maximum segment size */
277   
278   /* RTT (round trip time) estimation variables */
279   u32_t rttest; /* RTT estimate in 500ms ticks */
280   u32_t rtseq;  /* sequence number being timed */
281   s16_t sa, sv; /* @todo document this */
282
283   s16_t rto;    /* retransmission time-out */
284   u8_t nrtx;    /* number of retransmissions */
285
286   /* fast retransmit/recovery */
287   u32_t lastack; /* Highest acknowledged seqno. */
288   u8_t dupacks;
289   
290   /* congestion avoidance/control variables */
291   u16_t cwnd;  
292   u16_t ssthresh;
293
294   /* sender variables */
295   u32_t snd_nxt,   /* next seqno to be sent */
296     snd_max;       /* Highest seqno sent. */
297   u16_t snd_wnd;   /* sender window */
298   u32_t snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last
299                              window update. */
300     snd_lbb;       /* Sequence number of next byte to be buffered. */
301
302   u16_t acked;
303   
304   u16_t snd_buf;   /* Available buffer space for sending (in bytes). */
305 #define TCP_SNDQUEUELEN_OVERFLOW (0xffff-3)
306   u16_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
307   
308   
309   /* These are ordered by sequence number: */
310   struct tcp_seg *unsent;   /* Unsent (queued) segments. */
311   struct tcp_seg *unacked;  /* Sent but unacknowledged segments. */
312 #if TCP_QUEUE_OOSEQ  
313   struct tcp_seg *ooseq;    /* Received out of sequence segments. */
314 #endif /* TCP_QUEUE_OOSEQ */
315
316 #if LWIP_CALLBACK_API
317   /* Function to be called when more send buffer space is available.
318    * @param arg user-supplied argument (tcp_pcb.callback_arg)
319    * @param pcb the tcp_pcb which has send buffer space available
320    * @param space the amount of bytes available
321    * @return ERR_OK: try to send some data by calling tcp_output
322    */
323   err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
324   
325   /* Function to be called when (in-sequence) data has arrived.
326    * @param arg user-supplied argument (tcp_pcb.callback_arg)
327    * @param pcb the tcp_pcb for which data has arrived
328    * @param p the packet buffer which arrived
329    * @param err an error argument (TODO: that is current always ERR_OK?)
330    * @return ERR_OK: try to send some data by calling tcp_output
331    */
332   err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
333
334   /* Function to be called when a connection has been set up.
335    * @param arg user-supplied argument (tcp_pcb.callback_arg)
336    * @param pcb the tcp_pcb that now is connected
337    * @param err an error argument (TODO: that is current always ERR_OK?)
338    * @return value is currently ignored
339    */
340   err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
341
342   /* Function to call when a listener has been connected.
343    * @param arg user-supplied argument (tcp_pcb.callback_arg)
344    * @param pcb a new tcp_pcb that now is connected
345    * @param err an error argument (TODO: that is current always ERR_OK?)
346    * @return ERR_OK: accept the new connection,
347    *                 any other err_t abortsthe new connection
348    */
349   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
350
351   /* Function which is called periodically.
352    * The period can be adjusted in multiples of the TCP slow timer interval
353    * by changing tcp_pcb.polltmr.
354    * @param arg user-supplied argument (tcp_pcb.callback_arg)
355    * @param pcb the tcp_pcb to poll for
356    * @return ERR_OK: try to send some data by calling tcp_output
357    */
358   err_t (* poll)(void *arg, struct tcp_pcb *pcb);
359
360   /* Function to be called whenever a fatal error occurs.
361    * There is no pcb parameter since most of the times, the pcb is
362    * already deallocated (or there is no pcb) when this function is called.
363    * @param arg user-supplied argument (tcp_pcb.callback_arg)
364    * @param err an indication why the error callback is called:
365    *            ERR_ABRT: aborted through tcp_abort or by a TCP timer
366    *            ERR_RST: the connection was reset by the remote host
367    */
368   void (* errf)(void *arg, err_t err);
369 #endif /* LWIP_CALLBACK_API */
370
371   /* idle time before KEEPALIVE is sent */
372   u32_t keep_idle;
373 #if LWIP_TCP_KEEPALIVE
374   u32_t keep_intvl;
375   u32_t keep_cnt;
376 #endif /* LWIP_TCP_KEEPALIVE */
377   
378   /* KEEPALIVE counter */
379   u8_t keep_cnt_sent;
380 };
381
382 struct tcp_pcb_listen {  
383 /* Common members of all PCB types */
384   IP_PCB;
385 /* Protocol specific PCB members */
386   TCP_PCB_COMMON(struct tcp_pcb_listen);
387
388 #if LWIP_CALLBACK_API
389   /* Function to call when a listener has been connected.
390    * @param arg user-supplied argument (tcp_pcb.callback_arg)
391    * @param pcb a new tcp_pcb that now is connected
392    * @param err an error argument (TODO: that is current always ERR_OK?)
393    * @return ERR_OK: accept the new connection,
394    *                 any other err_t abortsthe new connection
395    */
396   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
397 #endif /* LWIP_CALLBACK_API */
398 };
399
400 #if LWIP_EVENT_API
401
402 enum lwip_event {
403   LWIP_EVENT_ACCEPT,
404   LWIP_EVENT_SENT,
405   LWIP_EVENT_RECV,
406   LWIP_EVENT_CONNECTED,
407   LWIP_EVENT_POLL,
408   LWIP_EVENT_ERR
409 };
410
411 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
412          enum lwip_event,
413          struct pbuf *p,
414          u16_t size,
415          err_t err);
416
417 #define TCP_EVENT_ACCEPT(pcb,err,ret)    ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
418                 LWIP_EVENT_ACCEPT, NULL, 0, err)
419 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
420                    LWIP_EVENT_SENT, NULL, space, ERR_OK)
421 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
422                 LWIP_EVENT_RECV, (p), 0, (err))
423 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
424                 LWIP_EVENT_CONNECTED, NULL, 0, (err))
425 #define TCP_EVENT_POLL(pcb,ret)       ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
426                 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
427 #define TCP_EVENT_ERR(errf,arg,err)  lwip_tcp_event((arg), NULL, \
428                 LWIP_EVENT_ERR, NULL, 0, (err))
429 #else /* LWIP_EVENT_API */
430 #define TCP_EVENT_ACCEPT(pcb,err,ret)     \
431                         if((pcb)->accept != NULL) \
432                         (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))
433 #define TCP_EVENT_SENT(pcb,space,ret) \
434                         if((pcb)->sent != NULL) \
435                         (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))
436 #define TCP_EVENT_RECV(pcb,p,err,ret) \
437                         if((pcb)->recv != NULL) \
438                         { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \
439                           if (p) pbuf_free(p); }
440 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
441                         if((pcb)->connected != NULL) \
442                         (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))
443 #define TCP_EVENT_POLL(pcb,ret) \
444                         if((pcb)->poll != NULL) \
445                         (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))
446 #define TCP_EVENT_ERR(errf,arg,err) \
447                         if((errf) != NULL) \
448                         (errf)((arg),(err))
449 #endif /* LWIP_EVENT_API */
450
451 /* This structure represents a TCP segment on the unsent and unacked queues */
452 struct tcp_seg {
453   struct tcp_seg *next;    /* used when putting segements on a queue */
454   struct pbuf *p;          /* buffer containing data + TCP header */
455   void *dataptr;           /* pointer to the TCP data in the pbuf */
456   u16_t len;               /* the TCP length of this segment */
457   struct tcp_hdr *tcphdr;  /* the TCP header */
458 };
459
460 /* Internal functions and global variables: */
461 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
462 void tcp_pcb_purge(struct tcp_pcb *pcb);
463 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
464
465 u8_t tcp_segs_free(struct tcp_seg *seg);
466 u8_t tcp_seg_free(struct tcp_seg *seg);
467 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
468
469 #define tcp_ack(pcb)     if((pcb)->flags & TF_ACK_DELAY) { \
470                             (pcb)->flags &= ~TF_ACK_DELAY; \
471                             (pcb)->flags |= TF_ACK_NOW; \
472                             tcp_output(pcb); \
473                          } else { \
474                             (pcb)->flags |= TF_ACK_DELAY; \
475                          }
476
477 #define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
478                          tcp_output(pcb)
479
480 err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
481 err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
482     u8_t flags, u8_t copy,
483                 u8_t *optdata, u8_t optlen);
484
485 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
486
487 void tcp_rst(u32_t seqno, u32_t ackno,
488        struct ip_addr *local_ip, struct ip_addr *remote_ip,
489        u16_t local_port, u16_t remote_port);
490
491 u32_t tcp_next_iss(void);
492
493 void tcp_keepalive(struct tcp_pcb *pcb);
494
495 extern struct tcp_pcb *tcp_input_pcb;
496 extern u32_t tcp_ticks;
497
498 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
499 void tcp_debug_print(struct tcp_hdr *tcphdr);
500 void tcp_debug_print_flags(u8_t flags);
501 void tcp_debug_print_state(enum tcp_state s);
502 void tcp_debug_print_pcbs(void);
503 s16_t tcp_pcbs_sane(void);
504 #else
505 #  define tcp_debug_print(tcphdr)
506 #  define tcp_debug_print_flags(flags)
507 #  define tcp_debug_print_state(s)
508 #  define tcp_debug_print_pcbs()
509 #  define tcp_pcbs_sane() 1
510 #endif /* TCP_DEBUG */
511
512 #if NO_SYS
513 #define tcp_timer_needed()
514 #else
515 void tcp_timer_needed(void);
516 #endif
517
518 /* The TCP PCB lists. */
519 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
520   struct tcp_pcb_listen *listen_pcbs; 
521   struct tcp_pcb *pcbs;
522 };
523 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
524 extern struct tcp_pcb *tcp_active_pcbs;  /* List of all TCP PCBs that are in a
525               state in which they accept or send
526               data. */
527 extern struct tcp_pcb *tcp_tw_pcbs;      /* List of all TCP PCBs in TIME-WAIT. */
528
529 extern struct tcp_pcb *tcp_tmp_pcb;      /* Only used for temporary storage. */
530
531 /* Axioms about the above lists:   
532    1) Every TCP PCB that is not CLOSED is in one of the lists.
533    2) A PCB is only in one of the lists.
534    3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
535    4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
536 */
537
538 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
539    with a PCB list or removes a PCB from a list, respectively. */
540 #if 0
541 #define TCP_REG(pcbs, npcb) do {\
542                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
543                             for(tcp_tmp_pcb = *pcbs; \
544           tcp_tmp_pcb != NULL; \
545         tcp_tmp_pcb = tcp_tmp_pcb->next) { \
546                                 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
547                             } \
548                             LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
549                             npcb->next = *pcbs; \
550                             LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
551                             *(pcbs) = npcb; \
552                             LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
553               tcp_timer_needed(); \
554                             } while(0)
555 #define TCP_RMV(pcbs, npcb) do { \
556                             LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
557                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
558                             if(*pcbs == npcb) { \
559                                *pcbs = (*pcbs)->next; \
560                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
561                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
562                                   tcp_tmp_pcb->next = npcb->next; \
563                                   break; \
564                                } \
565                             } \
566                             npcb->next = NULL; \
567                             LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
568                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
569                             } while(0)
570
571 #else /* LWIP_DEBUG */
572 #define TCP_REG(pcbs, npcb) do { \
573                             npcb->next = *pcbs; \
574                             *(pcbs) = npcb; \
575               tcp_timer_needed(); \
576                             } while(0)
577 #define TCP_RMV(pcbs, npcb) do { \
578                             if(*(pcbs) == npcb) { \
579                                (*(pcbs)) = (*pcbs)->next; \
580                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
581                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
582                                   tcp_tmp_pcb->next = npcb->next; \
583                                   break; \
584                                } \
585                             } \
586                             npcb->next = NULL; \
587                             } while(0)
588 #endif /* LWIP_DEBUG */
589
590 #ifdef __cplusplus
591 }
592 #endif
593
594 #endif /* LWIP_TCP */
595
596 #endif /* __LWIP_TCP_H__ */