]> git.sur5r.net Git - freertos/blob - Demo/Common/ethernet/lwIP_130/src/include/lwip/tcp.h
efef3df3921c94c865359f14bcdbf4e5cfbb3fc4
[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 struct tcp_hdr {\r
214   PACK_STRUCT_FIELD(u16_t src);\r
215   PACK_STRUCT_FIELD(u16_t dest);\r
216   PACK_STRUCT_FIELD(u32_t seqno);\r
217   PACK_STRUCT_FIELD(u32_t ackno);\r
218   PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);\r
219   PACK_STRUCT_FIELD(u16_t wnd);\r
220   PACK_STRUCT_FIELD(u16_t chksum);\r
221   PACK_STRUCT_FIELD(u16_t urgp);\r
222 } PACK_STRUCT_STRUCT;\r
223 PACK_STRUCT_END\r
224 #ifdef PACK_STRUCT_USE_INCLUDES\r
225 #  include "arch/epstruct.h"\r
226 #endif\r
227 \r
228 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)\r
229 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)\r
230 #define TCPH_FLAGS(phdr)  (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)\r
231 \r
232 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))\r
233 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))\r
234 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))\r
235 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))\r
236 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )\r
237 \r
238 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \\r
239           TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))\r
240 \r
241 enum tcp_state {\r
242   CLOSED      = 0,\r
243   LISTEN      = 1,\r
244   SYN_SENT    = 2,\r
245   SYN_RCVD    = 3,\r
246   ESTABLISHED = 4,\r
247   FIN_WAIT_1  = 5,\r
248   FIN_WAIT_2  = 6,\r
249   CLOSE_WAIT  = 7,\r
250   CLOSING     = 8,\r
251   LAST_ACK    = 9,\r
252   TIME_WAIT   = 10\r
253 };\r
254 \r
255 /** Flags used on input processing, not on pcb->flags\r
256 */\r
257 #define TF_RESET     (u8_t)0x08U   /* Connection was reset. */\r
258 #define TF_CLOSED    (u8_t)0x10U   /* Connection was sucessfully closed. */\r
259 #define TF_GOT_FIN   (u8_t)0x20U   /* Connection was closed by the remote end. */\r
260 \r
261 /**\r
262  * members common to struct tcp_pcb and struct tcp_listen_pcb\r
263  */\r
264 #define TCP_PCB_COMMON(type) \\r
265   type *next; /* for the linked list */ \\r
266   enum tcp_state state; /* TCP state */ \\r
267   u8_t prio; \\r
268   void *callback_arg; \\r
269   /* ports are in host byte order */ \\r
270   u16_t local_port\r
271 \r
272 /* the TCP protocol control block */\r
273 struct tcp_pcb {\r
274 /** common PCB members */\r
275   IP_PCB;\r
276 /** protocol specific PCB members */\r
277   TCP_PCB_COMMON(struct tcp_pcb);\r
278 \r
279   /* ports are in host byte order */\r
280   u16_t remote_port;\r
281 \r
282   u8_t flags;\r
283 #define TF_ACK_DELAY   (u8_t)0x01U   /* Delayed ACK. */\r
284 #define TF_ACK_NOW     (u8_t)0x02U   /* Immediate ACK. */\r
285 #define TF_INFR        (u8_t)0x04U   /* In fast recovery. */\r
286 #define TF_FIN         (u8_t)0x20U   /* Connection was closed locally (FIN segment enqueued). */\r
287 #define TF_NODELAY     (u8_t)0x40U   /* Disable Nagle algorithm */\r
288 #define TF_NAGLEMEMERR (u8_t)0x80U /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */\r
289 \r
290   /* the rest of the fields are in host byte order\r
291      as we have to do some math with them */\r
292   /* receiver variables */\r
293   u32_t rcv_nxt;   /* next seqno expected */\r
294   u16_t rcv_wnd;   /* receiver window */\r
295   u16_t rcv_ann_wnd; /* announced receive window */\r
296 \r
297   /* Timers */\r
298   u32_t tmr;\r
299   u8_t polltmr, pollinterval;\r
300 \r
301   /* Retransmission timer. */\r
302   s16_t rtime;\r
303 \r
304   u16_t mss;   /* maximum segment size */\r
305 \r
306   /* RTT (round trip time) estimation variables */\r
307   u32_t rttest; /* RTT estimate in 500ms ticks */\r
308   u32_t rtseq;  /* sequence number being timed */\r
309   s16_t sa, sv; /* @todo document this */\r
310 \r
311   s16_t rto;    /* retransmission time-out */\r
312   u8_t nrtx;    /* number of retransmissions */\r
313 \r
314   /* fast retransmit/recovery */\r
315   u32_t lastack; /* Highest acknowledged seqno. */\r
316   u8_t dupacks;\r
317 \r
318   /* congestion avoidance/control variables */\r
319   u16_t cwnd;\r
320   u16_t ssthresh;\r
321 \r
322   /* sender variables */\r
323   u32_t snd_nxt,   /* next seqno to be sent */\r
324     snd_max;       /* Highest seqno sent. */\r
325   u16_t snd_wnd;   /* sender window */\r
326   u32_t snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last\r
327                              window update. */\r
328     snd_lbb;       /* Sequence number of next byte to be buffered. */\r
329 \r
330   u16_t acked;\r
331 \r
332   u16_t snd_buf;   /* Available buffer space for sending (in bytes). */\r
333 #define TCP_SNDQUEUELEN_OVERFLOW (0xffff-3)\r
334   u16_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */\r
335 \r
336 \r
337   /* These are ordered by sequence number: */\r
338   struct tcp_seg *unsent;   /* Unsent (queued) segments. */\r
339   struct tcp_seg *unacked;  /* Sent but unacknowledged segments. */\r
340 #if TCP_QUEUE_OOSEQ\r
341   struct tcp_seg *ooseq;    /* Received out of sequence segments. */\r
342 #endif /* TCP_QUEUE_OOSEQ */\r
343 \r
344   struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */\r
345 \r
346 #if LWIP_CALLBACK_API\r
347   /* Function to be called when more send buffer space is available.\r
348    * @param arg user-supplied argument (tcp_pcb.callback_arg)\r
349    * @param pcb the tcp_pcb which has send buffer space available\r
350    * @param space the amount of bytes available\r
351    * @return ERR_OK: try to send some data by calling tcp_output\r
352    */\r
353   err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);\r
354 \r
355   /* Function to be called when (in-sequence) data has arrived.\r
356    * @param arg user-supplied argument (tcp_pcb.callback_arg)\r
357    * @param pcb the tcp_pcb for which data has arrived\r
358    * @param p the packet buffer which arrived\r
359    * @param err an error argument (TODO: that is current always ERR_OK?)\r
360    * @return ERR_OK: try to send some data by calling tcp_output\r
361    */\r
362   err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);\r
363 \r
364   /* Function to be called when a connection has been set up.\r
365    * @param arg user-supplied argument (tcp_pcb.callback_arg)\r
366    * @param pcb the tcp_pcb that now is connected\r
367    * @param err an error argument (TODO: that is current always ERR_OK?)\r
368    * @return value is currently ignored\r
369    */\r
370   err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);\r
371 \r
372   /* Function to call when a listener has been connected.\r
373    * @param arg user-supplied argument (tcp_pcb.callback_arg)\r
374    * @param pcb a new tcp_pcb that now is connected\r
375    * @param err an error argument (TODO: that is current always ERR_OK?)\r
376    * @return ERR_OK: accept the new connection,\r
377    *                 any other err_t abortsthe new connection\r
378    */\r
379   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);\r
380 \r
381   /* Function which is called periodically.\r
382    * The period can be adjusted in multiples of the TCP slow timer interval\r
383    * by changing tcp_pcb.polltmr.\r
384    * @param arg user-supplied argument (tcp_pcb.callback_arg)\r
385    * @param pcb the tcp_pcb to poll for\r
386    * @return ERR_OK: try to send some data by calling tcp_output\r
387    */\r
388   err_t (* poll)(void *arg, struct tcp_pcb *pcb);\r
389 \r
390   /* Function to be called whenever a fatal error occurs.\r
391    * There is no pcb parameter since most of the times, the pcb is\r
392    * already deallocated (or there is no pcb) when this function is called.\r
393    * @param arg user-supplied argument (tcp_pcb.callback_arg)\r
394    * @param err an indication why the error callback is called:\r
395    *            ERR_ABRT: aborted through tcp_abort or by a TCP timer\r
396    *            ERR_RST: the connection was reset by the remote host\r
397    */\r
398   void (* errf)(void *arg, err_t err);\r
399 #endif /* LWIP_CALLBACK_API */\r
400 \r
401   /* idle time before KEEPALIVE is sent */\r
402   u32_t keep_idle;\r
403 #if LWIP_TCP_KEEPALIVE\r
404   u32_t keep_intvl;\r
405   u32_t keep_cnt;\r
406 #endif /* LWIP_TCP_KEEPALIVE */\r
407 \r
408   /* Persist timer counter */\r
409   u32_t persist_cnt;\r
410   /* Persist timer back-off */\r
411   u8_t persist_backoff;\r
412 \r
413   /* KEEPALIVE counter */\r
414   u8_t keep_cnt_sent;\r
415 };\r
416 \r
417 struct tcp_pcb_listen {\r
418 /* Common members of all PCB types */\r
419   IP_PCB;\r
420 /* Protocol specific PCB members */\r
421   TCP_PCB_COMMON(struct tcp_pcb_listen);\r
422 \r
423 #if LWIP_CALLBACK_API\r
424   /* Function to call when a listener has been connected.\r
425    * @param arg user-supplied argument (tcp_pcb.callback_arg)\r
426    * @param pcb a new tcp_pcb that now is connected\r
427    * @param err an error argument (TODO: that is current always ERR_OK?)\r
428    * @return ERR_OK: accept the new connection,\r
429    *                 any other err_t abortsthe new connection\r
430    */\r
431   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);\r
432 #endif /* LWIP_CALLBACK_API */\r
433 #if TCP_LISTEN_BACKLOG\r
434   u8_t backlog;\r
435   u8_t accepts_pending;\r
436 #endif /* TCP_LISTEN_BACKLOG */\r
437 };\r
438 \r
439 #if LWIP_EVENT_API\r
440 \r
441 enum lwip_event {\r
442   LWIP_EVENT_ACCEPT,\r
443   LWIP_EVENT_SENT,\r
444   LWIP_EVENT_RECV,\r
445   LWIP_EVENT_CONNECTED,\r
446   LWIP_EVENT_POLL,\r
447   LWIP_EVENT_ERR\r
448 };\r
449 \r
450 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,\r
451          enum lwip_event,\r
452          struct pbuf *p,\r
453          u16_t size,\r
454          err_t err);\r
455 \r
456 #define TCP_EVENT_ACCEPT(pcb,err,ret)    ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
457                 LWIP_EVENT_ACCEPT, NULL, 0, err)\r
458 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
459                    LWIP_EVENT_SENT, NULL, space, ERR_OK)\r
460 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
461                 LWIP_EVENT_RECV, (p), 0, (err))\r
462 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
463                 LWIP_EVENT_CONNECTED, NULL, 0, (err))\r
464 #define TCP_EVENT_POLL(pcb,ret)       ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\\r
465                 LWIP_EVENT_POLL, NULL, 0, ERR_OK)\r
466 #define TCP_EVENT_ERR(errf,arg,err)  lwip_tcp_event((arg), NULL, \\r
467                 LWIP_EVENT_ERR, NULL, 0, (err))\r
468 #else /* LWIP_EVENT_API */\r
469 #define TCP_EVENT_ACCEPT(pcb,err,ret)     \\r
470                         if((pcb)->accept != NULL) \\r
471                         (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))\r
472 #define TCP_EVENT_SENT(pcb,space,ret) \\r
473                         if((pcb)->sent != NULL) \\r
474                         (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))\r
475 #define TCP_EVENT_RECV(pcb,p,err,ret) \\r
476                         if((pcb)->recv != NULL) \\r
477                         { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \\r
478                           ret = ERR_OK; \\r
479                           if (p) pbuf_free(p); }\r
480 #define TCP_EVENT_CONNECTED(pcb,err,ret) \\r
481                         if((pcb)->connected != NULL) \\r
482                         (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))\r
483 #define TCP_EVENT_POLL(pcb,ret) \\r
484                         if((pcb)->poll != NULL) \\r
485                         (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))\r
486 #define TCP_EVENT_ERR(errf,arg,err) \\r
487                         if((errf) != NULL) \\r
488                         (errf)((arg),(err))\r
489 #endif /* LWIP_EVENT_API */\r
490 \r
491 /* This structure represents a TCP segment on the unsent and unacked queues */\r
492 struct tcp_seg {\r
493   struct tcp_seg *next;    /* used when putting segements on a queue */\r
494   struct pbuf *p;          /* buffer containing data + TCP header */\r
495   void *dataptr;           /* pointer to the TCP data in the pbuf */\r
496   u16_t len;               /* the TCP length of this segment */\r
497   struct tcp_hdr *tcphdr;  /* the TCP header */\r
498 };\r
499 \r
500 /* Internal functions and global variables: */\r
501 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);\r
502 void tcp_pcb_purge(struct tcp_pcb *pcb);\r
503 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);\r
504 \r
505 u8_t tcp_segs_free(struct tcp_seg *seg);\r
506 u8_t tcp_seg_free(struct tcp_seg *seg);\r
507 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);\r
508 \r
509 #define tcp_ack(pcb)     if((pcb)->flags & TF_ACK_DELAY) { \\r
510                             (pcb)->flags &= ~TF_ACK_DELAY; \\r
511                             (pcb)->flags |= TF_ACK_NOW; \\r
512                             tcp_output(pcb); \\r
513                          } else { \\r
514                             (pcb)->flags |= TF_ACK_DELAY; \\r
515                          }\r
516 \r
517 #define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \\r
518                          tcp_output(pcb)\r
519 \r
520 err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);\r
521 err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,\r
522     u8_t flags, u8_t apiflags,\r
523                 u8_t *optdata, u8_t optlen);\r
524 \r
525 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);\r
526 \r
527 void tcp_rst(u32_t seqno, u32_t ackno,\r
528        struct ip_addr *local_ip, struct ip_addr *remote_ip,\r
529        u16_t local_port, u16_t remote_port);\r
530 \r
531 u32_t tcp_next_iss(void);\r
532 \r
533 void tcp_keepalive(struct tcp_pcb *pcb);\r
534 void tcp_zero_window_probe(struct tcp_pcb *pcb);\r
535 \r
536 #if TCP_CALCULATE_EFF_SEND_MSS\r
537 u16_t tcp_eff_send_mss(u16_t sendmss, struct ip_addr *addr);\r
538 #endif /* TCP_CALCULATE_EFF_SEND_MSS */\r
539 \r
540 extern struct tcp_pcb *tcp_input_pcb;\r
541 extern u32_t tcp_ticks;\r
542 \r
543 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG\r
544 void tcp_debug_print(struct tcp_hdr *tcphdr);\r
545 void tcp_debug_print_flags(u8_t flags);\r
546 void tcp_debug_print_state(enum tcp_state s);\r
547 void tcp_debug_print_pcbs(void);\r
548 s16_t tcp_pcbs_sane(void);\r
549 #else\r
550 #  define tcp_debug_print(tcphdr)\r
551 #  define tcp_debug_print_flags(flags)\r
552 #  define tcp_debug_print_state(s)\r
553 #  define tcp_debug_print_pcbs()\r
554 #  define tcp_pcbs_sane() 1\r
555 #endif /* TCP_DEBUG */\r
556 \r
557 #if NO_SYS\r
558 #define tcp_timer_needed()\r
559 #else\r
560 void tcp_timer_needed(void);\r
561 #endif\r
562 \r
563 /* The TCP PCB lists. */\r
564 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */\r
565   struct tcp_pcb_listen *listen_pcbs;\r
566   struct tcp_pcb *pcbs;\r
567 };\r
568 extern union tcp_listen_pcbs_t tcp_listen_pcbs;\r
569 extern struct tcp_pcb *tcp_active_pcbs;  /* List of all TCP PCBs that are in a\r
570               state in which they accept or send\r
571               data. */\r
572 extern struct tcp_pcb *tcp_tw_pcbs;      /* List of all TCP PCBs in TIME-WAIT. */\r
573 \r
574 extern struct tcp_pcb *tcp_tmp_pcb;      /* Only used for temporary storage. */\r
575 \r
576 /* Axioms about the above lists:\r
577    1) Every TCP PCB that is not CLOSED is in one of the lists.\r
578    2) A PCB is only in one of the lists.\r
579    3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.\r
580    4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.\r
581 */\r
582 \r
583 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB\r
584    with a PCB list or removes a PCB from a list, respectively. */\r
585 #if 0\r
586 #define TCP_REG(pcbs, npcb) do {\\r
587                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \\r
588                             for(tcp_tmp_pcb = *pcbs; \\r
589           tcp_tmp_pcb != NULL; \\r
590         tcp_tmp_pcb = tcp_tmp_pcb->next) { \\r
591                                 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \\r
592                             } \\r
593                             LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \\r
594                             npcb->next = *pcbs; \\r
595                             LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \\r
596                             *(pcbs) = npcb; \\r
597                             LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \\r
598               tcp_timer_needed(); \\r
599                             } while(0)\r
600 #define TCP_RMV(pcbs, npcb) do { \\r
601                             LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \\r
602                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \\r
603                             if(*pcbs == npcb) { \\r
604                                *pcbs = (*pcbs)->next; \\r
605                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \\r
606                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \\r
607                                   tcp_tmp_pcb->next = npcb->next; \\r
608                                   break; \\r
609                                } \\r
610                             } \\r
611                             npcb->next = NULL; \\r
612                             LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \\r
613                             LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \\r
614                             } while(0)\r
615 \r
616 #else /* LWIP_DEBUG */\r
617 #define TCP_REG(pcbs, npcb) do { \\r
618                             npcb->next = *pcbs; \\r
619                             *(pcbs) = npcb; \\r
620               tcp_timer_needed(); \\r
621                             } while(0)\r
622 #define TCP_RMV(pcbs, npcb) do { \\r
623                             if(*(pcbs) == npcb) { \\r
624                                (*(pcbs)) = (*pcbs)->next; \\r
625                             } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \\r
626                                if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \\r
627                                   tcp_tmp_pcb->next = npcb->next; \\r
628                                   break; \\r
629                                } \\r
630                             } \\r
631                             npcb->next = NULL; \\r
632                             } while(0)\r
633 #endif /* LWIP_DEBUG */\r
634 \r
635 #ifdef __cplusplus\r
636 }\r
637 #endif\r
638 \r
639 #endif /* LWIP_TCP */\r
640 \r
641 #endif /* __LWIP_TCP_H__ */\r