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/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 |
|
|
#define tcp_nagle_disable(pcb) ((pcb)->flags |= TF_NODELAY)
|
82 |
|
|
#define tcp_nagle_enable(pcb) ((pcb)->flags &= ~TF_NODELAY)
|
83 |
|
|
#define tcp_nagle_disabled(pcb) (((pcb)->flags & TF_NODELAY) != 0)
|
84 |
|
|
|
85 |
|
|
#if TCP_LISTEN_BACKLOG
|
86 |
|
|
#define tcp_accepted(pcb) (((struct tcp_pcb_listen *)(pcb))->accepts_pending--)
|
87 |
|
|
#else /* TCP_LISTEN_BACKLOG */
|
88 |
|
|
#define tcp_accepted(pcb)
|
89 |
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
90 |
|
|
|
91 |
|
|
void tcp_recved (struct tcp_pcb *pcb, u16_t len);
|
92 |
|
|
err_t tcp_bind (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
|
93 |
|
|
u16_t port);
|
94 |
|
|
err_t tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
|
95 |
|
|
u16_t port, err_t (* connected)(void *arg,
|
96 |
|
|
struct tcp_pcb *tpcb,
|
97 |
|
|
err_t err));
|
98 |
|
|
|
99 |
|
|
struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
|
100 |
|
|
#define tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
|
101 |
|
|
|
102 |
|
|
void tcp_abandon (struct tcp_pcb *pcb, int reset);
|
103 |
|
|
#define tcp_abort(pcb) tcp_abandon((pcb), 1)
|
104 |
|
|
err_t tcp_close (struct tcp_pcb *pcb);
|
105 |
|
|
|
106 |
|
|
/* Flags for "apiflags" parameter in tcp_write and tcp_enqueue */
|
107 |
|
|
#define TCP_WRITE_FLAG_COPY 0x01
|
108 |
|
|
#define TCP_WRITE_FLAG_MORE 0x02
|
109 |
|
|
|
110 |
|
|
err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
|
111 |
|
|
u8_t apiflags);
|
112 |
|
|
|
113 |
|
|
void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
|
114 |
|
|
|
115 |
|
|
#define TCP_PRIO_MIN 1
|
116 |
|
|
#define TCP_PRIO_NORMAL 64
|
117 |
|
|
#define TCP_PRIO_MAX 127
|
118 |
|
|
|
119 |
|
|
/* It is also possible to call these two functions at the right
|
120 |
|
|
intervals (instead of calling tcp_tmr()). */
|
121 |
|
|
void tcp_slowtmr (void);
|
122 |
|
|
void tcp_fasttmr (void);
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
/* Only used by IP to pass a TCP segment to TCP: */
|
126 |
|
|
void tcp_input (struct pbuf *p, struct netif *inp);
|
127 |
|
|
/* Used within the TCP code only: */
|
128 |
|
|
err_t tcp_send_empty_ack(struct tcp_pcb *pcb);
|
129 |
|
|
err_t tcp_output (struct tcp_pcb *pcb);
|
130 |
|
|
void tcp_rexmit (struct tcp_pcb *pcb);
|
131 |
|
|
void tcp_rexmit_rto (struct tcp_pcb *pcb);
|
132 |
|
|
void tcp_rexmit_fast (struct tcp_pcb *pcb);
|
133 |
|
|
u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb);
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* This is the Nagle algorithm: try to combine user data to send as few TCP
|
137 |
|
|
* segments as possible. Only send if
|
138 |
|
|
* - no previously transmitted data on the connection remains unacknowledged or
|
139 |
|
|
* - the TF_NODELAY flag is set (nagle algorithm turned off for this pcb) or
|
140 |
|
|
* - the only unsent segment is at least pcb->mss bytes long (or there is more
|
141 |
|
|
* than one unsent segment - with lwIP, this can happen although unsent->len < mss)
|
142 |
|
|
* - or if we are in fast-retransmit (TF_INFR)
|
143 |
|
|
*/
|
144 |
|
|
#define tcp_do_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \
|
145 |
|
|
((tpcb)->flags & (TF_NODELAY | TF_INFR)) || \
|
146 |
|
|
(((tpcb)->unsent != NULL) && (((tpcb)->unsent->next != NULL) || \
|
147 |
|
|
((tpcb)->unsent->len >= (tpcb)->mss))) \
|
148 |
|
|
) ? 1 : 0)
|
149 |
|
|
#define tcp_output_nagle(tpcb) (tcp_do_output_nagle(tpcb) ? tcp_output(tpcb) : ERR_OK)
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
#define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
|
153 |
|
|
#define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
|
154 |
|
|
#define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
|
155 |
|
|
#define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
|
156 |
|
|
/* is b<=a<=c? */
|
157 |
|
|
#if 0 /* see bug #10548 */
|
158 |
|
|
#define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
|
159 |
|
|
#endif
|
160 |
|
|
#define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
|
161 |
|
|
#define TCP_FIN 0x01U
|
162 |
|
|
#define TCP_SYN 0x02U
|
163 |
|
|
#define TCP_RST 0x04U
|
164 |
|
|
#define TCP_PSH 0x08U
|
165 |
|
|
#define TCP_ACK 0x10U
|
166 |
|
|
#define TCP_URG 0x20U
|
167 |
|
|
#define TCP_ECE 0x40U
|
168 |
|
|
#define TCP_CWR 0x80U
|
169 |
|
|
|
170 |
|
|
#define TCP_FLAGS 0x3fU
|
171 |
|
|
|
172 |
|
|
/* Length of the TCP header, excluding options. */
|
173 |
|
|
#define TCP_HLEN 20
|
174 |
|
|
|
175 |
|
|
#ifndef TCP_TMR_INTERVAL
|
176 |
|
|
#define TCP_TMR_INTERVAL 250 /* The TCP timer interval in milliseconds. */
|
177 |
|
|
#endif /* TCP_TMR_INTERVAL */
|
178 |
|
|
|
179 |
|
|
#ifndef TCP_FAST_INTERVAL
|
180 |
|
|
#define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in milliseconds */
|
181 |
|
|
#endif /* TCP_FAST_INTERVAL */
|
182 |
|
|
|
183 |
|
|
#ifndef TCP_SLOW_INTERVAL
|
184 |
|
|
#define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in milliseconds */
|
185 |
|
|
#endif /* TCP_SLOW_INTERVAL */
|
186 |
|
|
|
187 |
|
|
#define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
|
188 |
|
|
#define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
|
189 |
|
|
|
190 |
|
|
#define TCP_OOSEQ_TIMEOUT 6U /* x RTO */
|
191 |
|
|
|
192 |
|
|
#ifndef TCP_MSL
|
193 |
|
|
#define TCP_MSL 60000UL /* The maximum segment lifetime in milliseconds */
|
194 |
|
|
#endif
|
195 |
|
|
|
196 |
|
|
/* Keepalive values, compliant with RFC 1122. Don't change this unless you know what you're doing */
|
197 |
|
|
#ifndef TCP_KEEPIDLE_DEFAULT
|
198 |
|
|
#define TCP_KEEPIDLE_DEFAULT 7200000UL /* Default KEEPALIVE timer in milliseconds */
|
199 |
|
|
#endif
|
200 |
|
|
|
201 |
|
|
#ifndef TCP_KEEPINTVL_DEFAULT
|
202 |
|
|
#define TCP_KEEPINTVL_DEFAULT 75000UL /* Default Time between KEEPALIVE probes in milliseconds */
|
203 |
|
|
#endif
|
204 |
|
|
|
205 |
|
|
#ifndef TCP_KEEPCNT_DEFAULT
|
206 |
|
|
#define TCP_KEEPCNT_DEFAULT 9U /* Default Counter for KEEPALIVE probes */
|
207 |
|
|
#endif
|
208 |
|
|
|
209 |
|
|
#define TCP_MAXIDLE TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT /* Maximum KEEPALIVE probe time */
|
210 |
|
|
|
211 |
|
|
/* Fields are (of course) in network byte order.
|
212 |
|
|
* Some fields are converted to host byte order in tcp_input().
|
213 |
|
|
*/
|
214 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
215 |
|
|
# include "arch/bpstruct.h"
|
216 |
|
|
#endif
|
217 |
|
|
PACK_STRUCT_BEGIN
|
218 |
|
|
struct tcp_hdr {
|
219 |
|
|
PACK_STRUCT_FIELD(u16_t src);
|
220 |
|
|
PACK_STRUCT_FIELD(u16_t dest);
|
221 |
|
|
PACK_STRUCT_FIELD(u32_t seqno);
|
222 |
|
|
PACK_STRUCT_FIELD(u32_t ackno);
|
223 |
|
|
PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
|
224 |
|
|
PACK_STRUCT_FIELD(u16_t wnd);
|
225 |
|
|
PACK_STRUCT_FIELD(u16_t chksum);
|
226 |
|
|
PACK_STRUCT_FIELD(u16_t urgp);
|
227 |
|
|
} PACK_STRUCT_STRUCT;
|
228 |
|
|
PACK_STRUCT_END
|
229 |
|
|
#ifdef PACK_STRUCT_USE_INCLUDES
|
230 |
|
|
# include "arch/epstruct.h"
|
231 |
|
|
#endif
|
232 |
|
|
|
233 |
|
|
#define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
|
234 |
|
|
#define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
|
235 |
|
|
#define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
|
236 |
|
|
|
237 |
|
|
#define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
|
238 |
|
|
#define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
|
239 |
|
|
#define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = (((phdr)->_hdrlen_rsvd_flags & htons((u16_t)(~(u16_t)(TCP_FLAGS)))) | htons(flags))
|
240 |
|
|
#define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = ((phdr)->_hdrlen_rsvd_flags | htons(flags))
|
241 |
|
|
#define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
|
242 |
|
|
|
243 |
|
|
#define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & (TCP_FIN | TCP_SYN)) != 0))
|
244 |
|
|
|
245 |
|
|
enum tcp_state {
|
246 |
|
|
CLOSED = 0,
|
247 |
|
|
LISTEN = 1,
|
248 |
|
|
SYN_SENT = 2,
|
249 |
|
|
SYN_RCVD = 3,
|
250 |
|
|
ESTABLISHED = 4,
|
251 |
|
|
FIN_WAIT_1 = 5,
|
252 |
|
|
FIN_WAIT_2 = 6,
|
253 |
|
|
CLOSE_WAIT = 7,
|
254 |
|
|
CLOSING = 8,
|
255 |
|
|
LAST_ACK = 9,
|
256 |
|
|
TIME_WAIT = 10
|
257 |
|
|
};
|
258 |
|
|
|
259 |
|
|
/** Flags used on input processing, not on pcb->flags
|
260 |
|
|
*/
|
261 |
|
|
#define TF_RESET (u8_t)0x08U /* Connection was reset. */
|
262 |
|
|
#define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
|
263 |
|
|
#define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
#if LWIP_CALLBACK_API
|
267 |
|
|
/* Function to call when a listener has been connected.
|
268 |
|
|
* @param arg user-supplied argument (tcp_pcb.callback_arg)
|
269 |
|
|
* @param pcb a new tcp_pcb that now is connected
|
270 |
|
|
* @param err an error argument (TODO: that is current always ERR_OK?)
|
271 |
|
|
* @return ERR_OK: accept the new connection,
|
272 |
|
|
* any other err_t abortsthe new connection
|
273 |
|
|
*/
|
274 |
|
|
#define DEF_ACCEPT_CALLBACK err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err)
|
275 |
|
|
#else /* LWIP_CALLBACK_API */
|
276 |
|
|
#define DEF_ACCEPT_CALLBACK
|
277 |
|
|
#endif /* LWIP_CALLBACK_API */
|
278 |
|
|
|
279 |
|
|
/**
|
280 |
|
|
* members common to struct tcp_pcb and struct tcp_listen_pcb
|
281 |
|
|
*/
|
282 |
|
|
#define TCP_PCB_COMMON(type) \
|
283 |
|
|
type *next; /* for the linked list */ \
|
284 |
|
|
enum tcp_state state; /* TCP state */ \
|
285 |
|
|
u8_t prio; \
|
286 |
|
|
void *callback_arg; \
|
287 |
|
|
/* ports are in host byte order */ \
|
288 |
|
|
u16_t local_port; \
|
289 |
|
|
/* the accept callback for listen- and normal pcbs, if LWIP_CALLBACK_API */ \
|
290 |
|
|
DEF_ACCEPT_CALLBACK
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
/* the TCP protocol control block */
|
294 |
|
|
struct tcp_pcb {
|
295 |
|
|
/** common PCB members */
|
296 |
|
|
IP_PCB;
|
297 |
|
|
/** protocol specific PCB members */
|
298 |
|
|
TCP_PCB_COMMON(struct tcp_pcb);
|
299 |
|
|
|
300 |
|
|
/* ports are in host byte order */
|
301 |
|
|
u16_t remote_port;
|
302 |
|
|
|
303 |
|
|
u8_t flags;
|
304 |
|
|
#define TF_ACK_DELAY ((u8_t)0x01U) /* Delayed ACK. */
|
305 |
|
|
#define TF_ACK_NOW ((u8_t)0x02U) /* Immediate ACK. */
|
306 |
|
|
#define TF_INFR ((u8_t)0x04U) /* In fast recovery. */
|
307 |
|
|
#define TF_TIMESTAMP ((u8_t)0x08U) /* Timestamp option enabled */
|
308 |
|
|
#define TF_FIN ((u8_t)0x20U) /* Connection was closed locally (FIN segment enqueued). */
|
309 |
|
|
#define TF_NODELAY ((u8_t)0x40U) /* Disable Nagle algorithm */
|
310 |
|
|
#define TF_NAGLEMEMERR ((u8_t)0x80U) /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
|
311 |
|
|
|
312 |
|
|
/* the rest of the fields are in host byte order
|
313 |
|
|
as we have to do some math with them */
|
314 |
|
|
/* receiver variables */
|
315 |
|
|
u32_t rcv_nxt; /* next seqno expected */
|
316 |
|
|
u16_t rcv_wnd; /* receiver window available */
|
317 |
|
|
u16_t rcv_ann_wnd; /* receiver window to announce */
|
318 |
|
|
u32_t rcv_ann_right_edge; /* announced right edge of window */
|
319 |
|
|
|
320 |
|
|
/* Timers */
|
321 |
|
|
u32_t tmr;
|
322 |
|
|
u8_t polltmr, pollinterval;
|
323 |
|
|
|
324 |
|
|
/* Retransmission timer. */
|
325 |
|
|
s16_t rtime;
|
326 |
|
|
|
327 |
|
|
u16_t mss; /* maximum segment size */
|
328 |
|
|
|
329 |
|
|
/* RTT (round trip time) estimation variables */
|
330 |
|
|
u32_t rttest; /* RTT estimate in 500ms ticks */
|
331 |
|
|
u32_t rtseq; /* sequence number being timed */
|
332 |
|
|
s16_t sa, sv; /* @todo document this */
|
333 |
|
|
|
334 |
|
|
s16_t rto; /* retransmission time-out */
|
335 |
|
|
u8_t nrtx; /* number of retransmissions */
|
336 |
|
|
|
337 |
|
|
/* fast retransmit/recovery */
|
338 |
|
|
u32_t lastack; /* Highest acknowledged seqno. */
|
339 |
|
|
u8_t dupacks;
|
340 |
|
|
|
341 |
|
|
/* congestion avoidance/control variables */
|
342 |
|
|
u16_t cwnd;
|
343 |
|
|
u16_t ssthresh;
|
344 |
|
|
|
345 |
|
|
/* sender variables */
|
346 |
|
|
u32_t snd_nxt; /* next new seqno to be sent */
|
347 |
|
|
u16_t snd_wnd; /* sender window */
|
348 |
|
|
u32_t snd_wl1, snd_wl2; /* Sequence and acknowledgement numbers of last
|
349 |
|
|
window update. */
|
350 |
|
|
u32_t snd_lbb; /* Sequence number of next byte to be buffered. */
|
351 |
|
|
|
352 |
|
|
u16_t acked;
|
353 |
|
|
|
354 |
|
|
u16_t snd_buf; /* Available buffer space for sending (in bytes). */
|
355 |
|
|
#define TCP_SNDQUEUELEN_OVERFLOW (0xffff-3)
|
356 |
|
|
u16_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
/* These are ordered by sequence number: */
|
360 |
|
|
struct tcp_seg *unsent; /* Unsent (queued) segments. */
|
361 |
|
|
struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
|
362 |
|
|
#if TCP_QUEUE_OOSEQ
|
363 |
|
|
struct tcp_seg *ooseq; /* Received out of sequence segments. */
|
364 |
|
|
#endif /* TCP_QUEUE_OOSEQ */
|
365 |
|
|
|
366 |
|
|
struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
|
367 |
|
|
|
368 |
|
|
#if LWIP_CALLBACK_API
|
369 |
|
|
/* Function to be called when more send buffer space is available.
|
370 |
|
|
* @param arg user-supplied argument (tcp_pcb.callback_arg)
|
371 |
|
|
* @param pcb the tcp_pcb which has send buffer space available
|
372 |
|
|
* @param space the amount of bytes available
|
373 |
|
|
* @return ERR_OK: try to send some data by calling tcp_output
|
374 |
|
|
*/
|
375 |
|
|
err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
|
376 |
|
|
|
377 |
|
|
/* Function to be called when (in-sequence) data has arrived.
|
378 |
|
|
* @param arg user-supplied argument (tcp_pcb.callback_arg)
|
379 |
|
|
* @param pcb the tcp_pcb for which data has arrived
|
380 |
|
|
* @param p the packet buffer which arrived
|
381 |
|
|
* @param err an error argument (TODO: that is current always ERR_OK?)
|
382 |
|
|
* @return ERR_OK: try to send some data by calling tcp_output
|
383 |
|
|
*/
|
384 |
|
|
err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
|
385 |
|
|
|
386 |
|
|
/* Function to be called when a connection has been set up.
|
387 |
|
|
* @param arg user-supplied argument (tcp_pcb.callback_arg)
|
388 |
|
|
* @param pcb the tcp_pcb that now is connected
|
389 |
|
|
* @param err an error argument (TODO: that is current always ERR_OK?)
|
390 |
|
|
* @return value is currently ignored
|
391 |
|
|
*/
|
392 |
|
|
err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
|
393 |
|
|
|
394 |
|
|
/* Function which is called periodically.
|
395 |
|
|
* The period can be adjusted in multiples of the TCP slow timer interval
|
396 |
|
|
* by changing tcp_pcb.polltmr.
|
397 |
|
|
* @param arg user-supplied argument (tcp_pcb.callback_arg)
|
398 |
|
|
* @param pcb the tcp_pcb to poll for
|
399 |
|
|
* @return ERR_OK: try to send some data by calling tcp_output
|
400 |
|
|
*/
|
401 |
|
|
err_t (* poll)(void *arg, struct tcp_pcb *pcb);
|
402 |
|
|
|
403 |
|
|
/* Function to be called whenever a fatal error occurs.
|
404 |
|
|
* There is no pcb parameter since most of the times, the pcb is
|
405 |
|
|
* already deallocated (or there is no pcb) when this function is called.
|
406 |
|
|
* @param arg user-supplied argument (tcp_pcb.callback_arg)
|
407 |
|
|
* @param err an indication why the error callback is called:
|
408 |
|
|
* ERR_ABRT: aborted through tcp_abort or by a TCP timer
|
409 |
|
|
* ERR_RST: the connection was reset by the remote host
|
410 |
|
|
*/
|
411 |
|
|
void (* errf)(void *arg, err_t err);
|
412 |
|
|
#endif /* LWIP_CALLBACK_API */
|
413 |
|
|
|
414 |
|
|
#if LWIP_TCP_TIMESTAMPS
|
415 |
|
|
u32_t ts_lastacksent;
|
416 |
|
|
u32_t ts_recent;
|
417 |
|
|
#endif /* LWIP_TCP_TIMESTAMPS */
|
418 |
|
|
|
419 |
|
|
/* idle time before KEEPALIVE is sent */
|
420 |
|
|
u32_t keep_idle;
|
421 |
|
|
#if LWIP_TCP_KEEPALIVE
|
422 |
|
|
u32_t keep_intvl;
|
423 |
|
|
u32_t keep_cnt;
|
424 |
|
|
#endif /* LWIP_TCP_KEEPALIVE */
|
425 |
|
|
|
426 |
|
|
/* Persist timer counter */
|
427 |
|
|
u32_t persist_cnt;
|
428 |
|
|
/* Persist timer back-off */
|
429 |
|
|
u8_t persist_backoff;
|
430 |
|
|
|
431 |
|
|
/* KEEPALIVE counter */
|
432 |
|
|
u8_t keep_cnt_sent;
|
433 |
|
|
};
|
434 |
|
|
|
435 |
|
|
struct tcp_pcb_listen {
|
436 |
|
|
/* Common members of all PCB types */
|
437 |
|
|
IP_PCB;
|
438 |
|
|
/* Protocol specific PCB members */
|
439 |
|
|
TCP_PCB_COMMON(struct tcp_pcb_listen);
|
440 |
|
|
|
441 |
|
|
#if TCP_LISTEN_BACKLOG
|
442 |
|
|
u8_t backlog;
|
443 |
|
|
u8_t accepts_pending;
|
444 |
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
445 |
|
|
};
|
446 |
|
|
|
447 |
|
|
#if LWIP_EVENT_API
|
448 |
|
|
|
449 |
|
|
enum lwip_event {
|
450 |
|
|
LWIP_EVENT_ACCEPT,
|
451 |
|
|
LWIP_EVENT_SENT,
|
452 |
|
|
LWIP_EVENT_RECV,
|
453 |
|
|
LWIP_EVENT_CONNECTED,
|
454 |
|
|
LWIP_EVENT_POLL,
|
455 |
|
|
LWIP_EVENT_ERR
|
456 |
|
|
};
|
457 |
|
|
|
458 |
|
|
err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
|
459 |
|
|
enum lwip_event,
|
460 |
|
|
struct pbuf *p,
|
461 |
|
|
u16_t size,
|
462 |
|
|
err_t err);
|
463 |
|
|
|
464 |
|
|
#define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
465 |
|
|
LWIP_EVENT_ACCEPT, NULL, 0, err)
|
466 |
|
|
#define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
467 |
|
|
LWIP_EVENT_SENT, NULL, space, ERR_OK)
|
468 |
|
|
#define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
469 |
|
|
LWIP_EVENT_RECV, (p), 0, (err))
|
470 |
|
|
#define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
471 |
|
|
LWIP_EVENT_CONNECTED, NULL, 0, (err))
|
472 |
|
|
#define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
|
473 |
|
|
LWIP_EVENT_POLL, NULL, 0, ERR_OK)
|
474 |
|
|
#define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
|
475 |
|
|
LWIP_EVENT_ERR, NULL, 0, (err))
|
476 |
|
|
#else /* LWIP_EVENT_API */
|
477 |
|
|
|
478 |
|
|
#define TCP_EVENT_ACCEPT(pcb,err,ret) \
|
479 |
|
|
do { \
|
480 |
|
|
if((pcb)->accept != NULL) \
|
481 |
|
|
(ret) = (pcb)->accept((pcb)->callback_arg,(pcb),(err)); \
|
482 |
|
|
else (ret) = ERR_OK; \
|
483 |
|
|
} while (0)
|
484 |
|
|
|
485 |
|
|
#define TCP_EVENT_SENT(pcb,space,ret) \
|
486 |
|
|
do { \
|
487 |
|
|
if((pcb)->sent != NULL) \
|
488 |
|
|
(ret) = (pcb)->sent((pcb)->callback_arg,(pcb),(space)); \
|
489 |
|
|
else (ret) = ERR_OK; \
|
490 |
|
|
} while (0)
|
491 |
|
|
|
492 |
|
|
#define TCP_EVENT_RECV(pcb,p,err,ret) \
|
493 |
|
|
do { \
|
494 |
|
|
if((pcb)->recv != NULL) { \
|
495 |
|
|
(ret) = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); \
|
496 |
|
|
} else { \
|
497 |
|
|
(ret) = tcp_recv_null(NULL, (pcb), (p), (err)); \
|
498 |
|
|
} \
|
499 |
|
|
} while (0)
|
500 |
|
|
|
501 |
|
|
#define TCP_EVENT_CONNECTED(pcb,err,ret) \
|
502 |
|
|
do { \
|
503 |
|
|
if((pcb)->connected != NULL) \
|
504 |
|
|
(ret) = (pcb)->connected((pcb)->callback_arg,(pcb),(err)); \
|
505 |
|
|
else (ret) = ERR_OK; \
|
506 |
|
|
} while (0)
|
507 |
|
|
|
508 |
|
|
#define TCP_EVENT_POLL(pcb,ret) \
|
509 |
|
|
do { \
|
510 |
|
|
if((pcb)->poll != NULL) \
|
511 |
|
|
(ret) = (pcb)->poll((pcb)->callback_arg,(pcb)); \
|
512 |
|
|
else (ret) = ERR_OK; \
|
513 |
|
|
} while (0)
|
514 |
|
|
|
515 |
|
|
#define TCP_EVENT_ERR(errf,arg,err) \
|
516 |
|
|
do { \
|
517 |
|
|
if((errf) != NULL) \
|
518 |
|
|
(errf)((arg),(err)); \
|
519 |
|
|
} while (0)
|
520 |
|
|
|
521 |
|
|
#endif /* LWIP_EVENT_API */
|
522 |
|
|
|
523 |
|
|
/* This structure represents a TCP segment on the unsent and unacked queues */
|
524 |
|
|
struct tcp_seg {
|
525 |
|
|
struct tcp_seg *next; /* used when putting segements on a queue */
|
526 |
|
|
struct pbuf *p; /* buffer containing data + TCP header */
|
527 |
|
|
void *dataptr; /* pointer to the TCP data in the pbuf */
|
528 |
|
|
u16_t len; /* the TCP length of this segment */
|
529 |
|
|
u8_t flags;
|
530 |
|
|
#define TF_SEG_OPTS_MSS (u8_t)0x01U /* Include MSS option. */
|
531 |
|
|
#define TF_SEG_OPTS_TS (u8_t)0x02U /* Include timestamp option. */
|
532 |
|
|
struct tcp_hdr *tcphdr; /* the TCP header */
|
533 |
|
|
};
|
534 |
|
|
|
535 |
|
|
#define LWIP_TCP_OPT_LENGTH(flags) \
|
536 |
|
|
(flags & TF_SEG_OPTS_MSS ? 4 : 0) + \
|
537 |
|
|
(flags & TF_SEG_OPTS_TS ? 12 : 0)
|
538 |
|
|
|
539 |
|
|
/** This returns a TCP header option for MSS in an u32_t */
|
540 |
|
|
#define TCP_BUILD_MSS_OPTION(x) (x) = htonl(((u32_t)2 << 24) | \
|
541 |
|
|
((u32_t)4 << 16) | \
|
542 |
|
|
(((u32_t)TCP_MSS / 256) << 8) | \
|
543 |
|
|
(TCP_MSS & 255))
|
544 |
|
|
|
545 |
|
|
/* Internal functions and global variables: */
|
546 |
|
|
struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
|
547 |
|
|
void tcp_pcb_purge(struct tcp_pcb *pcb);
|
548 |
|
|
void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
|
549 |
|
|
|
550 |
|
|
u8_t tcp_segs_free(struct tcp_seg *seg);
|
551 |
|
|
u8_t tcp_seg_free(struct tcp_seg *seg);
|
552 |
|
|
struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
|
553 |
|
|
|
554 |
|
|
#define tcp_ack(pcb) \
|
555 |
|
|
do { \
|
556 |
|
|
if((pcb)->flags & TF_ACK_DELAY) { \
|
557 |
|
|
(pcb)->flags &= ~TF_ACK_DELAY; \
|
558 |
|
|
(pcb)->flags |= TF_ACK_NOW; \
|
559 |
|
|
tcp_output(pcb); \
|
560 |
|
|
} \
|
561 |
|
|
else { \
|
562 |
|
|
(pcb)->flags |= TF_ACK_DELAY; \
|
563 |
|
|
} \
|
564 |
|
|
} while (0)
|
565 |
|
|
|
566 |
|
|
#define tcp_ack_now(pcb) \
|
567 |
|
|
do { \
|
568 |
|
|
(pcb)->flags |= TF_ACK_NOW; \
|
569 |
|
|
tcp_output(pcb); \
|
570 |
|
|
} while (0)
|
571 |
|
|
|
572 |
|
|
err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
|
573 |
|
|
err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
|
574 |
|
|
u8_t flags, u8_t apiflags, u8_t optflags);
|
575 |
|
|
|
576 |
|
|
void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
|
577 |
|
|
|
578 |
|
|
void tcp_rst(u32_t seqno, u32_t ackno,
|
579 |
|
|
struct ip_addr *local_ip, struct ip_addr *remote_ip,
|
580 |
|
|
u16_t local_port, u16_t remote_port);
|
581 |
|
|
|
582 |
|
|
u32_t tcp_next_iss(void);
|
583 |
|
|
|
584 |
|
|
void tcp_keepalive(struct tcp_pcb *pcb);
|
585 |
|
|
void tcp_zero_window_probe(struct tcp_pcb *pcb);
|
586 |
|
|
|
587 |
|
|
#if TCP_CALCULATE_EFF_SEND_MSS
|
588 |
|
|
u16_t tcp_eff_send_mss(u16_t sendmss, struct ip_addr *addr);
|
589 |
|
|
#endif /* TCP_CALCULATE_EFF_SEND_MSS */
|
590 |
|
|
|
591 |
|
|
#if LWIP_CALLBACK_API
|
592 |
|
|
err_t tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
|
593 |
|
|
#endif /* LWIP_CALLBACK_API */
|
594 |
|
|
|
595 |
|
|
extern struct tcp_pcb *tcp_input_pcb;
|
596 |
|
|
extern u32_t tcp_ticks;
|
597 |
|
|
|
598 |
|
|
const char* tcp_debug_state_str(enum tcp_state s);
|
599 |
|
|
#if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
|
600 |
|
|
void tcp_debug_print(struct tcp_hdr *tcphdr);
|
601 |
|
|
void tcp_debug_print_flags(u8_t flags);
|
602 |
|
|
void tcp_debug_print_state(enum tcp_state s);
|
603 |
|
|
void tcp_debug_print_pcbs(void);
|
604 |
|
|
s16_t tcp_pcbs_sane(void);
|
605 |
|
|
#else
|
606 |
|
|
# define tcp_debug_print(tcphdr)
|
607 |
|
|
# define tcp_debug_print_flags(flags)
|
608 |
|
|
# define tcp_debug_print_state(s)
|
609 |
|
|
# define tcp_debug_print_pcbs()
|
610 |
|
|
# define tcp_pcbs_sane() 1
|
611 |
|
|
#endif /* TCP_DEBUG */
|
612 |
|
|
|
613 |
|
|
#if NO_SYS
|
614 |
|
|
#define tcp_timer_needed()
|
615 |
|
|
#else
|
616 |
|
|
void tcp_timer_needed(void);
|
617 |
|
|
#endif
|
618 |
|
|
|
619 |
|
|
/* The TCP PCB lists. */
|
620 |
|
|
union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
|
621 |
|
|
struct tcp_pcb_listen *listen_pcbs;
|
622 |
|
|
struct tcp_pcb *pcbs;
|
623 |
|
|
};
|
624 |
|
|
extern union tcp_listen_pcbs_t tcp_listen_pcbs;
|
625 |
|
|
extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
|
626 |
|
|
state in which they accept or send
|
627 |
|
|
data. */
|
628 |
|
|
extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
|
629 |
|
|
|
630 |
|
|
extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
|
631 |
|
|
|
632 |
|
|
/* Axioms about the above lists:
|
633 |
|
|
1) Every TCP PCB that is not CLOSED is in one of the lists.
|
634 |
|
|
2) A PCB is only in one of the lists.
|
635 |
|
|
3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
|
636 |
|
|
4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
|
637 |
|
|
*/
|
638 |
|
|
|
639 |
|
|
/* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
|
640 |
|
|
with a PCB list or removes a PCB from a list, respectively. */
|
641 |
|
|
#if 0
|
642 |
|
|
#define TCP_REG(pcbs, npcb) do {\
|
643 |
|
|
LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
|
644 |
|
|
for(tcp_tmp_pcb = *pcbs; \
|
645 |
|
|
tcp_tmp_pcb != NULL; \
|
646 |
|
|
tcp_tmp_pcb = tcp_tmp_pcb->next) { \
|
647 |
|
|
LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
|
648 |
|
|
} \
|
649 |
|
|
LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
|
650 |
|
|
npcb->next = *pcbs; \
|
651 |
|
|
LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
|
652 |
|
|
*(pcbs) = npcb; \
|
653 |
|
|
LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
|
654 |
|
|
tcp_timer_needed(); \
|
655 |
|
|
} while(0)
|
656 |
|
|
#define TCP_RMV(pcbs, npcb) do { \
|
657 |
|
|
LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
|
658 |
|
|
LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
|
659 |
|
|
if(*pcbs == npcb) { \
|
660 |
|
|
*pcbs = (*pcbs)->next; \
|
661 |
|
|
} else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
|
662 |
|
|
if(tcp_tmp_pcb->next == npcb) { \
|
663 |
|
|
tcp_tmp_pcb->next = npcb->next; \
|
664 |
|
|
break; \
|
665 |
|
|
} \
|
666 |
|
|
} \
|
667 |
|
|
npcb->next = NULL; \
|
668 |
|
|
LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
|
669 |
|
|
LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
|
670 |
|
|
} while(0)
|
671 |
|
|
|
672 |
|
|
#else /* LWIP_DEBUG */
|
673 |
|
|
|
674 |
|
|
#define TCP_REG(pcbs, npcb) \
|
675 |
|
|
do { \
|
676 |
|
|
npcb->next = *pcbs; \
|
677 |
|
|
*(pcbs) = npcb; \
|
678 |
|
|
tcp_timer_needed(); \
|
679 |
|
|
} while (0)
|
680 |
|
|
|
681 |
|
|
#define TCP_RMV(pcbs, npcb) \
|
682 |
|
|
do { \
|
683 |
|
|
if(*(pcbs) == npcb) { \
|
684 |
|
|
(*(pcbs)) = (*pcbs)->next; \
|
685 |
|
|
} \
|
686 |
|
|
else { \
|
687 |
|
|
for(tcp_tmp_pcb = *pcbs; \
|
688 |
|
|
tcp_tmp_pcb != NULL; \
|
689 |
|
|
tcp_tmp_pcb = tcp_tmp_pcb->next) { \
|
690 |
|
|
if(tcp_tmp_pcb->next == npcb) { \
|
691 |
|
|
tcp_tmp_pcb->next = npcb->next; \
|
692 |
|
|
break; \
|
693 |
|
|
} \
|
694 |
|
|
} \
|
695 |
|
|
} \
|
696 |
|
|
npcb->next = NULL; \
|
697 |
|
|
} while(0)
|
698 |
|
|
|
699 |
|
|
#endif /* LWIP_DEBUG */
|
700 |
|
|
|
701 |
|
|
#ifdef __cplusplus
|
702 |
|
|
}
|
703 |
|
|
#endif
|
704 |
|
|
|
705 |
|
|
#endif /* LWIP_TCP */
|
706 |
|
|
|
707 |
|
|
#endif /* __LWIP_TCP_H__ */
|