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