1 |
606 |
jeremybenn |
/*
|
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/sys.h"
|
36 |
|
|
#include "lwip/mem.h"
|
37 |
|
|
|
38 |
|
|
#include "lwip/pbuf.h"
|
39 |
|
|
#include "lwip/opt.h"
|
40 |
|
|
#include "lwip/ip.h"
|
41 |
|
|
#include "lwip/icmp.h"
|
42 |
|
|
|
43 |
|
|
#include "lwip/err.h"
|
44 |
|
|
|
45 |
|
|
struct tcp_pcb;
|
46 |
|
|
|
47 |
|
|
/* Functions for interfacing with TCP: */
|
48 |
|
|
|
49 |
|
|
/* Lower layer interface to TCP: */
|
50 |
|
|
void tcp_init (void); /* Must be called first to
|
51 |
|
|
initialize TCP. */
|
52 |
|
|
void tcp_tmr (void); /* Must be called every
|
53 |
|
|
TCP_TMR_INTERVAL
|
54 |
|
|
ms. (Typically 250 ms). */
|
55 |
|
|
/* Application program's interface: */
|
56 |
|
|
struct tcp_pcb * tcp_new (void);
|
57 |
|
|
struct tcp_pcb * tcp_alloc (u8_t prio);
|
58 |
|
|
|
59 |
|
|
void tcp_arg (struct tcp_pcb *pcb, void *arg);
|
60 |
|
|
void tcp_accept (struct tcp_pcb *pcb,
|
61 |
|
|
err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
|
62 |
|
|
err_t err));
|
63 |
|
|
void tcp_recv (struct tcp_pcb *pcb,
|
64 |
|
|
err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
|
65 |
|
|
struct pbuf *p, err_t err));
|
66 |
|
|
void tcp_sent (struct tcp_pcb *pcb,
|
67 |
|
|
err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
|
68 |
|
|
u16_t len));
|
69 |
|
|
void tcp_poll (struct tcp_pcb *pcb,
|
70 |
|
|
err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
|
71 |
|
|
u8_t interval);
|
72 |
|
|
void tcp_err (struct tcp_pcb *pcb,
|
73 |
|
|
void (* err)(void *arg, err_t err));
|
74 |
|
|
|
75 |
|
|
#define tcp_mss(pcb) ((pcb)->mss)
|
76 |
|
|
#define tcp_sndbuf(pcb) ((pcb)->snd_buf)
|
77 |
|
|
|
78 |
|
|
void tcp_recved (struct tcp_pcb *pcb, u16_t len);
|
79 |
|
|
err_t tcp_bind (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
|
80 |
|
|
u16_t port);
|
81 |
|
|
err_t tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
|
82 |
|
|
u16_t port, err_t (* connected)(void *arg,
|
83 |
|
|
struct tcp_pcb *tpcb,
|
84 |
|
|
err_t err));
|
85 |
|
|
struct tcp_pcb * tcp_listen (struct tcp_pcb *pcb);
|
86 |
|
|
void tcp_abort (struct tcp_pcb *pcb);
|
87 |
|
|
err_t tcp_close (struct tcp_pcb *pcb);
|
88 |
|
|
err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
|
89 |
|
|
u8_t copy);
|
90 |
|
|
|
91 |
|
|
void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
|
92 |
|
|
|
93 |
|
|
#define TCP_PRIO_MIN 1
|
94 |
|
|
#define TCP_PRIO_NORMAL 64
|
95 |
|
|
#define TCP_PRIO_MAX 127
|
96 |
|
|
|
97 |
|
|
/* It is also possible to call these two functions at the right
|
98 |
|
|
intervals (instead of calling tcp_tmr()). */
|
99 |
|
|
void tcp_slowtmr (void);
|
100 |
|
|
void tcp_fasttmr (void);
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
/* Only used by IP to pass a TCP segment to TCP: */
|
104 |
|
|
void tcp_input (struct pbuf *p, struct netif *inp);
|
105 |
|
|
/* Used within the TCP code only: */
|
106 |
|
|
err_t tcp_output (struct tcp_pcb *pcb);
|
107 |
|
|
void tcp_rexmit (struct tcp_pcb *pcb);
|
108 |
|
|
void tcp_rexmit_rto (struct tcp_pcb *pcb);
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
#define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
|
113 |
|
|
#define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
|
114 |
|
|
#define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
|
115 |
|
|
#define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
|
116 |
|
|
/* is b<=a<=c? */
|
117 |
|
|
#if 0 /* see bug #10548 */
|
118 |
|
|
#define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
|
119 |
|
|
#endif
|
120 |
|
|
#define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
|
121 |
|
|
#define TCP_FIN 0x01U
|
122 |
|
|
#define TCP_SYN 0x02U
|
123 |
|
|
#define TCP_RST 0x04U
|
124 |
|
|
#define TCP_PSH 0x08U
|
125 |
|
|
#define TCP_ACK 0x10U
|
126 |
|
|
#define TCP_URG 0x20U
|
127 |
|
|
#define TCP_ECE 0x40U
|
128 |
|
|
#define TCP_CWR 0x80U
|
129 |
|
|
|
130 |
|
|
#define TCP_FLAGS 0x3fU
|
131 |
|
|
|
132 |
|
|
/* Length of the TCP header, excluding options. */
|
133 |
|
|
#define TCP_HLEN 20
|
134 |
|
|
|
135 |
|
|
#ifndef TCP_TMR_INTERVAL
|
136 |
|
|
#define TCP_TMR_INTERVAL 250 /* The TCP timer interval in
|
137 |
|
|
milliseconds. */
|
138 |
|
|
#endif /* TCP_TMR_INTERVAL */
|
139 |
|
|
|
140 |
|
|
#ifndef TCP_FAST_INTERVAL
|
141 |
|
|
#define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in
|
142 |
|
|
milliseconds */
|
143 |
|
|
#endif /* TCP_FAST_INTERVAL */
|
144 |
|
|
|
145 |
|
|
#ifndef TCP_SLOW_INTERVAL
|
146 |
|
|
#define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in
|
147 |
|
|
milliseconds */
|
148 |
|
|
#endif /* TCP_SLOW_INTERVAL */
|
149 |
|
|
|
150 |
|
|
#define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
|
151 |
|
|
#define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
|
152 |
|
|
|
153 |
|
|
#define TCP_OOSEQ_TIMEOUT 6U /* x RTO */
|
154 |
|
|
|
155 |
|
|
#define TCP_MSL 60000 /* The maximum segment lifetime in microseconds */
|
156 |
|
|
|
157 |
|
|
/*
|
158 |
|
|
* User-settable options (used with setsockopt).
|
159 |
|
|
*/
|
160 |
|
|
#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
|
161 |
|
|
#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keepalive miliseconds */
|
162 |
|
|
|
163 |
|
|
/* Keepalive values */
|
164 |
|
|
#define TCP_KEEPDEFAULT 7200000 /* KEEPALIVE timer in miliseconds */
|
165 |
|
|
#define TCP_KEEPINTVL 75000 /* Time between KEEPALIVE probes in miliseconds */
|
166 |
|
|
#define TCP_KEEPCNT 9 /* Counter for KEEPALIVE probes */
|
167 |
|
|
#define TCP_MAXIDLE TCP_KEEPCNT * TCP_KEEPINTVL /* Maximum KEEPALIVE probe time */
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
171 |
|
|
# include "arch/bpstruct.h"
|
172 |
|
|
#endif
|
173 |
|
|
PACK_STRUCT_BEGIN
|
174 |
|
|
struct tcp_hdr {
|
175 |
|
|
PACK_STRUCT_FIELD(u16_t src);
|
176 |
|
|
PACK_STRUCT_FIELD(u16_t dest);
|
177 |
|
|
PACK_STRUCT_FIELD(u32_t seqno);
|
178 |
|
|
PACK_STRUCT_FIELD(u32_t ackno);
|
179 |
|
|
PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
|
180 |
|
|
PACK_STRUCT_FIELD(u16_t wnd);
|
181 |
|
|
PACK_STRUCT_FIELD(u16_t chksum);
|
182 |
|
|
PACK_STRUCT_FIELD(u16_t urgp);
|
183 |
|
|
} PACK_STRUCT_STRUCT;
|
184 |
|
|
PACK_STRUCT_END
|
185 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
186 |
|
|
# include "arch/epstruct.h"
|
187 |
|
|
#endif
|
188 |
|
|
|
189 |
|
|
#define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
|
190 |
|
|
#define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
|
191 |
|
|
#define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
|
192 |
|
|
|
193 |
|
|
#define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
|
194 |
|
|
#define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
|
195 |
|
|
#define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))
|
196 |
|
|
#define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))
|
197 |
|
|
#define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
|
198 |
|
|
|
199 |
|
|
#define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
|
200 |
|
|
TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
|
201 |
|
|
|
202 |
|
|
enum tcp_state {
|
203 |
|
|
CLOSED = 0,
|
204 |
|
|
LISTEN = 1,
|
205 |
|
|
SYN_SENT = 2,
|
206 |
|
|
SYN_RCVD = 3,
|
207 |
|
|
ESTABLISHED = 4,
|
208 |
|
|
FIN_WAIT_1 = 5,
|
209 |
|
|
FIN_WAIT_2 = 6,
|
210 |
|
|
CLOSE_WAIT = 7,
|
211 |
|
|
CLOSING = 8,
|
212 |
|
|
LAST_ACK = 9,
|
213 |
|
|
TIME_WAIT = 10
|
214 |
|
|
};
|
215 |
|
|
|
216 |
|
|
/* the TCP protocol control block */
|
217 |
|
|
struct tcp_pcb {
|
218 |
|
|
/** common PCB members */
|
219 |
|
|
IP_PCB;
|
220 |
|
|
/** protocol specific PCB members */
|
221 |
|
|
struct tcp_pcb *next; /* for the linked list */
|
222 |
|
|
enum tcp_state state; /* TCP state */
|
223 |
|
|
u8_t prio;
|
224 |
|
|
void *callback_arg;
|
225 |
|
|
|
226 |
|
|
u16_t local_port;
|
227 |
|
|
u16_t remote_port;
|
228 |
|
|
|
229 |
|
|
u8_t flags;
|
230 |
|
|
#define TF_ACK_DELAY (u8_t)0x01U /* Delayed ACK. */
|
231 |
|
|
#define TF_ACK_NOW (u8_t)0x02U /* Immediate ACK. */
|
232 |
|
|
#define TF_INFR (u8_t)0x04U /* In fast recovery. */
|
233 |
|
|
#define TF_RESET (u8_t)0x08U /* Connection was reset. */
|
234 |
|
|
#define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
|
235 |
|
|
#define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
|
236 |
|
|
#define TF_NODELAY (u8_t)0x40U /* Disable Nagle algorithm */
|
237 |
|
|
|
238 |
|
|
/* receiver variables */
|
239 |
|
|
u32_t rcv_nxt; /* next seqno expected */
|
240 |
|
|
u16_t rcv_wnd; /* receiver window */
|
241 |
|
|
|
242 |
|
|
/* Timers */
|
243 |
|
|
u32_t tmr;
|
244 |
|
|
u8_t polltmr, pollinterval;
|
245 |
|
|
|
246 |
|
|
/* Retransmission timer. */
|
247 |
|
|
u16_t rtime;
|
248 |
|
|
|
249 |
|
|
u16_t mss; /* maximum segment size */
|
250 |
|
|
|
251 |
|
|
/* RTT (round trip time) estimation variables */
|
252 |
|
|
u32_t rttest; /* RTT estimate in 500ms ticks */
|
253 |
|
|
u32_t rtseq; /* sequence number being timed */
|
254 |
|
|
s16_t sa, sv; /* @todo document this */
|
255 |
|
|
|
256 |
|
|
u16_t rto; /* retransmission time-out */
|
257 |
|
|
u8_t nrtx; /* number of retransmissions */
|
258 |
|
|
|
259 |
|
|
/* fast retransmit/recovery */
|
260 |
|
|
u32_t lastack; /* Highest acknowledged seqno. */
|
261 |
|
|
u8_t dupacks;
|
262 |
|
|
|
263 |
|
|
/* congestion avoidance/control variables */
|
264 |
|
|
u16_t cwnd;
|
265 |
|
|
u16_t ssthresh;
|
266 |
|
|
|
267 |
|
|
/* sender variables */
|
268 |
|
|
u32_t snd_nxt, /* next seqno to be sent */
|
269 |
|
|
snd_max, /* Highest seqno sent. */
|
270 |
|
|
snd_wnd, /* sender window */
|
271 |
|
|
snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last
|
272 |
|
|
window update. */
|
273 |
|
|
snd_lbb; /* Sequence number of next byte to be buffered. */
|
274 |
|
|
|
275 |
|
|
u16_t acked;
|
276 |
|
|
|
277 |
|
|
u16_t snd_buf; /* Available buffer space for sending (in bytes). */
|
278 |
|
|
u8_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
/* These are ordered by sequence number: */
|
282 |
|
|
struct tcp_seg *unsent; /* Unsent (queued) segments. */
|
283 |
|
|
struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
|
284 |
|
|
#if TCP_QUEUE_OOSEQ
|
285 |
|
|
struct tcp_seg *ooseq; /* Received out of sequence segments. */
|
286 |
|
|
#endif /* TCP_QUEUE_OOSEQ */
|
287 |
|
|
|
288 |
|
|
#if LWIP_CALLBACK_API
|
289 |
|
|
/* Function to be called when more send buffer space is available. */
|
290 |
|
|
err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
|
291 |
|
|
|
292 |
|
|
/* Function to be called when (in-sequence) data has arrived. */
|
293 |
|
|
err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
|
294 |
|
|
|
295 |
|
|
/* Function to be called when a connection has been set up. */
|
296 |
|
|
err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
|
297 |
|
|
|
298 |
|
|
/* Function to call when a listener has been connected. */
|
299 |
|
|
err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
|
300 |
|
|
|
301 |
|
|
/* Function which is called periodically. */
|
302 |
|
|
err_t (* poll)(void *arg, struct tcp_pcb *pcb);
|
303 |
|
|
|
304 |
|
|
/* Function to be called whenever a fatal error occurs. */
|
305 |
|
|
void (* errf)(void *arg, err_t err);
|
306 |
|
|
#endif /* LWIP_CALLBACK_API */
|
307 |
|
|
|
308 |
|
|
/* idle time before KEEPALIVE is sent */
|
309 |
|
|
u32_t keepalive;
|
310 |
|
|
|
311 |
|
|
/* KEEPALIVE counter */
|
312 |
|
|
u8_t keep_cnt;
|
313 |
|
|
};
|
314 |
|
|
|
315 |
|
|
struct tcp_pcb_listen {
|
316 |
|
|
/* Common members of all PCB types */
|
317 |
|
|
IP_PCB;
|
318 |
|
|
|
319 |
|
|
/* Protocol specific PCB members */
|
320 |
|
|
struct tcp_pcb_listen *next; /* for the linked list */
|
321 |
|
|
|
322 |
|
|
/* Even if state is obviously LISTEN this is here for
|
323 |
|
|
* field compatibility with tpc_pcb to which it is cast sometimes
|
324 |
|
|
* Until a cleaner solution emerges this is here.FIXME
|
325 |
|
|
*/
|
326 |
|
|
enum tcp_state state; /* TCP state */
|
327 |
|
|
|
328 |
|
|
u8_t prio;
|
329 |
|
|
void *callback_arg;
|
330 |
|
|
|
331 |
|
|
u16_t local_port;
|
332 |
|
|
|
333 |
|
|
#if LWIP_CALLBACK_API
|
334 |
|
|
/* Function to call when a listener has been connected. */
|
335 |
|
|
err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
|
336 |
|
|
#endif /* LWIP_CALLBACK_API */
|
337 |
|
|
};
|
338 |
|
|
|
339 |
|
|
#if LWIP_EVENT_API
|
340 |
|
|
|
341 |
|
|
enum lwip_event {
|
342 |
|
|
LWIP_EVENT_ACCEPT,
|
343 |
|
|
LWIP_EVENT_SENT,
|
344 |
|
|
LWIP_EVENT_RECV,
|
345 |
|
|
LWIP_EVENT_CONNECTED,
|
346 |
|
|
LWIP_EVENT_POLL,
|
347 |
|
|
LWIP_EVENT_ERR
|
348 |
|
|
};
|
349 |
|
|
|
350 |
|
|
err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
|
351 |
|
|
enum lwip_event,
|
352 |
|
|
struct pbuf *p,
|
353 |
|
|
u16_t size,
|
354 |
|
|
err_t err);
|
355 |
|
|
|
356 |
|
|
#define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
357 |
|
|
LWIP_EVENT_ACCEPT, NULL, 0, err)
|
358 |
|
|
#define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
359 |
|
|
LWIP_EVENT_SENT, NULL, space, ERR_OK)
|
360 |
|
|
#define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
361 |
|
|
LWIP_EVENT_RECV, (p), 0, (err))
|
362 |
|
|
#define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
363 |
|
|
LWIP_EVENT_CONNECTED, NULL, 0, (err))
|
364 |
|
|
#define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
365 |
|
|
LWIP_EVENT_POLL, NULL, 0, ERR_OK)
|
366 |
|
|
#define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
|
367 |
|
|
LWIP_EVENT_ERR, NULL, 0, (err))
|
368 |
|
|
#else /* LWIP_EVENT_API */
|
369 |
|
|
#define TCP_EVENT_ACCEPT(pcb,err,ret) \
|
370 |
|
|
if((pcb)->accept != NULL) \
|
371 |
|
|
(ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))
|
372 |
|
|
#define TCP_EVENT_SENT(pcb,space,ret) \
|
373 |
|
|
if((pcb)->sent != NULL) \
|
374 |
|
|
(ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))
|
375 |
|
|
#define TCP_EVENT_RECV(pcb,p,err,ret) \
|
376 |
|
|
if((pcb)->recv != NULL) \
|
377 |
|
|
{ ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \
|
378 |
|
|
if (p) pbuf_free(p); }
|
379 |
|
|
#define TCP_EVENT_CONNECTED(pcb,err,ret) \
|
380 |
|
|
if((pcb)->connected != NULL) \
|
381 |
|
|
(ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))
|
382 |
|
|
#define TCP_EVENT_POLL(pcb,ret) \
|
383 |
|
|
if((pcb)->poll != NULL) \
|
384 |
|
|
(ret = (pcb)->poll((pcb)->callback_arg,(pcb)))
|
385 |
|
|
#define TCP_EVENT_ERR(errf,arg,err) \
|
386 |
|
|
if((errf) != NULL) \
|
387 |
|
|
(errf)((arg),(err))
|
388 |
|
|
#endif /* LWIP_EVENT_API */
|
389 |
|
|
|
390 |
|
|
/* This structure represents a TCP segment on the unsent and unacked queues */
|
391 |
|
|
struct tcp_seg {
|
392 |
|
|
struct tcp_seg *next; /* used when putting segements on a queue */
|
393 |
|
|
struct pbuf *p; /* buffer containing data + TCP header */
|
394 |
|
|
void *dataptr; /* pointer to the TCP data in the pbuf */
|
395 |
|
|
u16_t len; /* the TCP length of this segment */
|
396 |
|
|
struct tcp_hdr *tcphdr; /* the TCP header */
|
397 |
|
|
};
|
398 |
|
|
|
399 |
|
|
/* Internal functions and global variables: */
|
400 |
|
|
struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
|
401 |
|
|
void tcp_pcb_purge(struct tcp_pcb *pcb);
|
402 |
|
|
void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
|
403 |
|
|
|
404 |
|
|
u8_t tcp_segs_free(struct tcp_seg *seg);
|
405 |
|
|
u8_t tcp_seg_free(struct tcp_seg *seg);
|
406 |
|
|
struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
|
407 |
|
|
|
408 |
|
|
#define tcp_ack(pcb) if((pcb)->flags & TF_ACK_DELAY) { \
|
409 |
|
|
(pcb)->flags &= ~TF_ACK_DELAY; \
|
410 |
|
|
(pcb)->flags |= TF_ACK_NOW; \
|
411 |
|
|
tcp_output(pcb); \
|
412 |
|
|
} else { \
|
413 |
|
|
(pcb)->flags |= TF_ACK_DELAY; \
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
#define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
|
417 |
|
|
tcp_output(pcb)
|
418 |
|
|
|
419 |
|
|
err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
|
420 |
|
|
err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
|
421 |
|
|
u8_t flags, u8_t copy,
|
422 |
|
|
u8_t *optdata, u8_t optlen);
|
423 |
|
|
|
424 |
|
|
void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
|
425 |
|
|
|
426 |
|
|
void tcp_rst(u32_t seqno, u32_t ackno,
|
427 |
|
|
struct ip_addr *local_ip, struct ip_addr *remote_ip,
|
428 |
|
|
u16_t local_port, u16_t remote_port);
|
429 |
|
|
|
430 |
|
|
u32_t tcp_next_iss(void);
|
431 |
|
|
|
432 |
|
|
void tcp_keepalive(struct tcp_pcb *pcb);
|
433 |
|
|
|
434 |
|
|
extern struct tcp_pcb *tcp_input_pcb;
|
435 |
|
|
extern u32_t tcp_ticks;
|
436 |
|
|
|
437 |
|
|
#if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
|
438 |
|
|
void tcp_debug_print(struct tcp_hdr *tcphdr);
|
439 |
|
|
void tcp_debug_print_flags(u8_t flags);
|
440 |
|
|
void tcp_debug_print_state(enum tcp_state s);
|
441 |
|
|
void tcp_debug_print_pcbs(void);
|
442 |
|
|
s16_t tcp_pcbs_sane(void);
|
443 |
|
|
#else
|
444 |
|
|
# define tcp_debug_print(tcphdr)
|
445 |
|
|
# define tcp_debug_print_flags(flags)
|
446 |
|
|
# define tcp_debug_print_state(s)
|
447 |
|
|
# define tcp_debug_print_pcbs()
|
448 |
|
|
# define tcp_pcbs_sane() 1
|
449 |
|
|
#endif /* TCP_DEBUG */
|
450 |
|
|
|
451 |
|
|
#if NO_SYS
|
452 |
|
|
#define tcp_timer_needed()
|
453 |
|
|
#else
|
454 |
|
|
void tcp_timer_needed(void);
|
455 |
|
|
#endif
|
456 |
|
|
|
457 |
|
|
/* The TCP PCB lists. */
|
458 |
|
|
union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
|
459 |
|
|
struct tcp_pcb_listen *listen_pcbs;
|
460 |
|
|
struct tcp_pcb *pcbs;
|
461 |
|
|
};
|
462 |
|
|
extern union tcp_listen_pcbs_t tcp_listen_pcbs;
|
463 |
|
|
extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
|
464 |
|
|
state in which they accept or send
|
465 |
|
|
data. */
|
466 |
|
|
extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
|
467 |
|
|
|
468 |
|
|
extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
|
469 |
|
|
|
470 |
|
|
/* Axioms about the above lists:
|
471 |
|
|
1) Every TCP PCB that is not CLOSED is in one of the lists.
|
472 |
|
|
2) A PCB is only in one of the lists.
|
473 |
|
|
3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
|
474 |
|
|
4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
|
475 |
|
|
*/
|
476 |
|
|
|
477 |
|
|
/* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
|
478 |
|
|
with a PCB list or removes a PCB from a list, respectively. */
|
479 |
|
|
#if 0
|
480 |
|
|
#define TCP_REG(pcbs, npcb) do {\
|
481 |
|
|
LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
|
482 |
|
|
for(tcp_tmp_pcb = *pcbs; \
|
483 |
|
|
tcp_tmp_pcb != NULL; \
|
484 |
|
|
tcp_tmp_pcb = tcp_tmp_pcb->next) { \
|
485 |
|
|
LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
|
486 |
|
|
} \
|
487 |
|
|
LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
|
488 |
|
|
npcb->next = *pcbs; \
|
489 |
|
|
LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
|
490 |
|
|
*(pcbs) = npcb; \
|
491 |
|
|
LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
|
492 |
|
|
tcp_timer_needed(); \
|
493 |
|
|
} while(0)
|
494 |
|
|
#define TCP_RMV(pcbs, npcb) do { \
|
495 |
|
|
LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
|
496 |
|
|
LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
|
497 |
|
|
if(*pcbs == npcb) { \
|
498 |
|
|
*pcbs = (*pcbs)->next; \
|
499 |
|
|
} else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
|
500 |
|
|
if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
|
501 |
|
|
tcp_tmp_pcb->next = npcb->next; \
|
502 |
|
|
break; \
|
503 |
|
|
} \
|
504 |
|
|
} \
|
505 |
|
|
npcb->next = NULL; \
|
506 |
|
|
LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
|
507 |
|
|
LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
|
508 |
|
|
} while(0)
|
509 |
|
|
|
510 |
|
|
#else /* LWIP_DEBUG */
|
511 |
|
|
#define TCP_REG(pcbs, npcb) do { \
|
512 |
|
|
npcb->next = *pcbs; \
|
513 |
|
|
*(pcbs) = npcb; \
|
514 |
|
|
tcp_timer_needed(); \
|
515 |
|
|
} while(0)
|
516 |
|
|
#define TCP_RMV(pcbs, npcb) do { \
|
517 |
|
|
if(*(pcbs) == npcb) { \
|
518 |
|
|
(*(pcbs)) = (*pcbs)->next; \
|
519 |
|
|
} else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
|
520 |
|
|
if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
|
521 |
|
|
tcp_tmp_pcb->next = npcb->next; \
|
522 |
|
|
break; \
|
523 |
|
|
} \
|
524 |
|
|
} \
|
525 |
|
|
npcb->next = NULL; \
|
526 |
|
|
} while(0)
|
527 |
|
|
#endif /* LWIP_DEBUG */
|
528 |
|
|
#endif /* __LWIP_TCP_H__ */
|
529 |
|
|
|
530 |
|
|
|
531 |
|
|
|