BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file
3 * TCP API (to be used from TCPIP thread)\n
4 * See also @ref tcp_raw
5 */
6  
7 /*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 *
33 * This file is part of the lwIP TCP/IP stack.
34 *
35 * Author: Adam Dunkels <adam@sics.se>
36 *
37 */
38 #ifndef LWIP_HDR_TCP_H
39 #define LWIP_HDR_TCP_H
40  
41 #include "lwip/opt.h"
42  
43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
44  
45 #include "lwip/tcpbase.h"
46 #include "lwip/mem.h"
47 #include "lwip/pbuf.h"
48 #include "lwip/ip.h"
49 #include "lwip/icmp.h"
50 #include "lwip/err.h"
51 #include "lwip/ip6.h"
52 #include "lwip/ip6_addr.h"
53  
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57  
58 struct tcp_pcb;
59  
60 /** Function prototype for tcp accept callback functions. Called when a new
61 * connection can be accepted on a listening pcb.
62 *
63 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
64 * @param newpcb The new connection pcb
65 * @param err An error code if there has been an error accepting.
66 * Only return ERR_ABRT if you have called tcp_abort from within the
67 * callback function!
68 */
69 typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);
70  
71 /** Function prototype for tcp receive callback functions. Called when data has
72 * been received.
73 *
74 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
75 * @param tpcb The connection pcb which received data
76 * @param p The received data (or NULL when the connection has been closed!)
77 * @param err An error code if there has been an error receiving
78 * Only return ERR_ABRT if you have called tcp_abort from within the
79 * callback function!
80 */
81 typedef err_t (*tcp_recv_fn)(void *arg, struct tcp_pcb *tpcb,
82 struct pbuf *p, err_t err);
83  
84 /** Function prototype for tcp sent callback functions. Called when sent data has
85 * been acknowledged by the remote side. Use it to free corresponding resources.
86 * This also means that the pcb has now space available to send new data.
87 *
88 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
89 * @param tpcb The connection pcb for which data has been acknowledged
90 * @param len The amount of bytes acknowledged
91 * @return ERR_OK: try to send some data by calling tcp_output
92 * Only return ERR_ABRT if you have called tcp_abort from within the
93 * callback function!
94 */
95 typedef err_t (*tcp_sent_fn)(void *arg, struct tcp_pcb *tpcb,
96 u16_t len);
97  
98 /** Function prototype for tcp poll callback functions. Called periodically as
99 * specified by @see tcp_poll.
100 *
101 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
102 * @param tpcb tcp pcb
103 * @return ERR_OK: try to send some data by calling tcp_output
104 * Only return ERR_ABRT if you have called tcp_abort from within the
105 * callback function!
106 */
107 typedef err_t (*tcp_poll_fn)(void *arg, struct tcp_pcb *tpcb);
108  
109 /** Function prototype for tcp error callback functions. Called when the pcb
110 * receives a RST or is unexpectedly closed for any other reason.
111 *
112 * @note The corresponding pcb is already freed when this callback is called!
113 *
114 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
115 * @param err Error code to indicate why the pcb has been closed
116 * ERR_ABRT: aborted through tcp_abort or by a TCP timer
117 * ERR_RST: the connection was reset by the remote host
118 */
119 typedef void (*tcp_err_fn)(void *arg, err_t err);
120  
121 /** Function prototype for tcp connected callback functions. Called when a pcb
122 * is connected to the remote side after initiating a connection attempt by
123 * calling tcp_connect().
124 *
125 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
126 * @param tpcb The connection pcb which is connected
127 * @param err An unused error code, always ERR_OK currently ;-) @todo!
128 * Only return ERR_ABRT if you have called tcp_abort from within the
129 * callback function!
130 *
131 * @note When a connection attempt fails, the error callback is currently called!
132 */
133 typedef err_t (*tcp_connected_fn)(void *arg, struct tcp_pcb *tpcb, err_t err);
134  
135 #if LWIP_WND_SCALE
136 #define RCV_WND_SCALE(pcb, wnd) (((wnd) >> (pcb)->rcv_scale))
137 #define SND_WND_SCALE(pcb, wnd) (((wnd) << (pcb)->snd_scale))
138 #define TCPWND16(x) ((u16_t)LWIP_MIN((x), 0xFFFF))
139 #define TCP_WND_MAX(pcb) ((tcpwnd_size_t)(((pcb)->flags & TF_WND_SCALE) ? TCP_WND : TCPWND16(TCP_WND)))
140 #else
141 #define RCV_WND_SCALE(pcb, wnd) (wnd)
142 #define SND_WND_SCALE(pcb, wnd) (wnd)
143 #define TCPWND16(x) (x)
144 #define TCP_WND_MAX(pcb) TCP_WND
145 #endif
146 /* Increments a tcpwnd_size_t and holds at max value rather than rollover */
147 #define TCP_WND_INC(wnd, inc) do { \
148 if ((tcpwnd_size_t)(wnd + inc) >= wnd) { \
149 wnd = (tcpwnd_size_t)(wnd + inc); \
150 } else { \
151 wnd = (tcpwnd_size_t)-1; \
152 } \
153 } while(0)
154  
155 #if LWIP_TCP_SACK_OUT
156 /** SACK ranges to include in ACK packets.
157 * SACK entry is invalid if left==right. */
158 struct tcp_sack_range {
159 /** Left edge of the SACK: the first acknowledged sequence number. */
160 u32_t left;
161 /** Right edge of the SACK: the last acknowledged sequence number +1 (so first NOT acknowledged). */
162 u32_t right;
163 };
164 #endif /* LWIP_TCP_SACK_OUT */
165  
166 typedef u16_t tcpflags_t;
167  
168 /**
169 * members common to struct tcp_pcb and struct tcp_listen_pcb
170 */
171 #define TCP_PCB_COMMON(type) \
172 type *next; /* for the linked list */ \
173 void *callback_arg; \
174 enum tcp_state state; /* TCP state */ \
175 u8_t prio; \
176 /* ports are in host byte order */ \
177 u8_t bound_to_netif; \
178 char local_netif[3]; \
179 u16_t local_port
180  
181  
182 /** the TCP protocol control block for listening pcbs */
183 struct tcp_pcb_listen {
184 /** Common members of all PCB types */
185 IP_PCB;
186 /** Protocol specific PCB members */
187 TCP_PCB_COMMON(struct tcp_pcb_listen);
188  
189 #if LWIP_CALLBACK_API
190 /* Function to call when a listener has been connected. */
191 tcp_accept_fn accept;
192 #endif /* LWIP_CALLBACK_API */
193  
194 #if TCP_LISTEN_BACKLOG
195 u8_t backlog;
196 u8_t accepts_pending;
197 #endif /* TCP_LISTEN_BACKLOG */
198 };
199  
200  
201 /** the TCP protocol control block */
202 struct tcp_pcb {
203 /** common PCB members */
204 IP_PCB;
205 /** protocol specific PCB members */
206 TCP_PCB_COMMON(struct tcp_pcb);
207  
208 /* ports are in host byte order */
209 u16_t remote_port;
210  
211 tcpflags_t flags;
212 #define TF_ACK_DELAY 0x01U /* Delayed ACK. */
213 #define TF_ACK_NOW 0x02U /* Immediate ACK. */
214 #define TF_INFR 0x04U /* In fast recovery. */
215 #define TF_CLOSEPEND 0x08U /* If this is set, tcp_close failed to enqueue the FIN (retried in tcp_tmr) */
216 #define TF_RXCLOSED 0x10U /* rx closed by tcp_shutdown */
217 #define TF_FIN 0x20U /* Connection was closed locally (FIN segment enqueued). */
218 #define TF_NODELAY 0x40U /* Disable Nagle algorithm */
219 #define TF_NAGLEMEMERR 0x80U /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
220 #if LWIP_WND_SCALE
221 #define TF_WND_SCALE 0x0100U /* Window Scale option enabled */
222 #endif
223 #if TCP_LISTEN_BACKLOG
224 #define TF_BACKLOGPEND 0x0200U /* If this is set, a connection pcb has increased the backlog on its listener */
225 #endif
226 #if LWIP_TCP_TIMESTAMPS
227 #define TF_TIMESTAMP 0x0400U /* Timestamp option enabled */
228 #endif
229 #define TF_RTO 0x0800U /* RTO timer has fired, in-flight data moved to unsent and being retransmitted */
230 #if LWIP_TCP_SACK_OUT
231 #define TF_SACK 0x1000U /* Selective ACKs enabled */
232 #endif
233  
234 /* the rest of the fields are in host byte order
235 as we have to do some math with them */
236  
237 /* Timers */
238 u8_t polltmr, pollinterval;
239 u8_t last_timer;
240 u32_t tmr;
241  
242 /* receiver variables */
243 u32_t rcv_nxt; /* next seqno expected */
244 tcpwnd_size_t rcv_wnd; /* receiver window available */
245 tcpwnd_size_t rcv_ann_wnd; /* receiver window to announce */
246 u32_t rcv_ann_right_edge; /* announced right edge of window */
247  
248 #if LWIP_TCP_SACK_OUT
249 /* SACK ranges to include in ACK packets (entry is invalid if left==right) */
250 struct tcp_sack_range rcv_sacks[LWIP_TCP_MAX_SACK_NUM];
251 #define LWIP_TCP_SACK_VALID(pcb, idx) ((pcb)->rcv_sacks[idx].left != (pcb)->rcv_sacks[idx].right)
252 #endif /* LWIP_TCP_SACK_OUT */
253  
254 /* Retransmission timer. */
255 s16_t rtime;
256  
257 u16_t mss; /* maximum segment size */
258  
259 /* RTT (round trip time) estimation variables */
260 u32_t rttest; /* RTT estimate in 500ms ticks */
261 u32_t rtseq; /* sequence number being timed */
262 s16_t sa, sv; /* @see "Congestion Avoidance and Control" by Van Jacobson and Karels */
263  
264 s16_t rto; /* retransmission time-out (in ticks of TCP_SLOW_INTERVAL) */
265 u8_t nrtx; /* number of retransmissions */
266  
267 /* fast retransmit/recovery */
268 u8_t dupacks;
269 u32_t lastack; /* Highest acknowledged seqno. */
270  
271 /* congestion avoidance/control variables */
272 tcpwnd_size_t cwnd;
273 tcpwnd_size_t ssthresh;
274  
275 /* first byte following last rto byte */
276 u32_t rto_end;
277  
278 /* sender variables */
279 u32_t snd_nxt; /* next new seqno to be sent */
280 u32_t snd_wl1, snd_wl2; /* Sequence and acknowledgement numbers of last
281 window update. */
282 u32_t snd_lbb; /* Sequence number of next byte to be buffered. */
283 tcpwnd_size_t snd_wnd; /* sender window */
284 tcpwnd_size_t snd_wnd_max; /* the maximum sender window announced by the remote host */
285  
286 tcpwnd_size_t snd_buf; /* Available buffer space for sending (in bytes). */
287 #define TCP_SNDQUEUELEN_OVERFLOW (0xffffU-3)
288 u16_t snd_queuelen; /* Number of pbufs currently in the send buffer. */
289  
290 #if TCP_OVERSIZE
291 /* Extra bytes available at the end of the last pbuf in unsent. */
292 u16_t unsent_oversize;
293 #endif /* TCP_OVERSIZE */
294  
295 tcpwnd_size_t bytes_acked;
296  
297 /* These are ordered by sequence number: */
298 struct tcp_seg *unsent; /* Unsent (queued) segments. */
299 struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
300 #if TCP_QUEUE_OOSEQ
301 struct tcp_seg *ooseq; /* Received out of sequence segments. */
302 #endif /* TCP_QUEUE_OOSEQ */
303  
304 struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
305  
306 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
307 struct tcp_pcb_listen* listener;
308 #endif /* LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG */
309  
310 #if LWIP_CALLBACK_API
311 /* Function to be called when more send buffer space is available. */
312 tcp_sent_fn sent;
313 /* Function to be called when (in-sequence) data has arrived. */
314 tcp_recv_fn recv;
315 /* Function to be called when a connection has been set up. */
316 tcp_connected_fn connected;
317 /* Function which is called periodically. */
318 tcp_poll_fn poll;
319 /* Function to be called whenever a fatal error occurs. */
320 tcp_err_fn errf;
321 #endif /* LWIP_CALLBACK_API */
322  
323 #if LWIP_TCP_TIMESTAMPS
324 u32_t ts_lastacksent;
325 u32_t ts_recent;
326 #endif /* LWIP_TCP_TIMESTAMPS */
327  
328 /* idle time before KEEPALIVE is sent */
329 u32_t keep_idle;
330 #if LWIP_TCP_KEEPALIVE
331 u32_t keep_intvl;
332 u32_t keep_cnt;
333 #endif /* LWIP_TCP_KEEPALIVE */
334  
335 /* Persist timer counter */
336 u8_t persist_cnt;
337 /* Persist timer back-off */
338 u8_t persist_backoff;
339 /* Number of persist probes */
340 u8_t persist_probe;
341  
342 /* KEEPALIVE counter */
343 u8_t keep_cnt_sent;
344  
345 #if LWIP_WND_SCALE
346 u8_t snd_scale;
347 u8_t rcv_scale;
348 #endif
349 };
350  
351 #if LWIP_EVENT_API
352  
353 enum lwip_event {
354 LWIP_EVENT_ACCEPT,
355 LWIP_EVENT_SENT,
356 LWIP_EVENT_RECV,
357 LWIP_EVENT_CONNECTED,
358 LWIP_EVENT_POLL,
359 LWIP_EVENT_ERR
360 };
361  
362 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
363 enum lwip_event,
364 struct pbuf *p,
365 u16_t size,
366 err_t err);
367  
368 #endif /* LWIP_EVENT_API */
369  
370 /* Application program's interface: */
371 struct tcp_pcb * tcp_new (void);
372 struct tcp_pcb * tcp_new_ip_type (u8_t type);
373  
374 void tcp_arg (struct tcp_pcb *pcb, void *arg);
375 #if LWIP_CALLBACK_API
376 void tcp_recv (struct tcp_pcb *pcb, tcp_recv_fn recv);
377 void tcp_sent (struct tcp_pcb *pcb, tcp_sent_fn sent);
378 void tcp_err (struct tcp_pcb *pcb, tcp_err_fn err);
379 void tcp_accept (struct tcp_pcb *pcb, tcp_accept_fn accept);
380 #endif /* LWIP_CALLBACK_API */
381 void tcp_poll (struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval);
382  
383 #define tcp_set_flags(pcb, set_flags) do { (pcb)->flags = (tcpflags_t)((pcb)->flags | (set_flags)); } while(0)
384 #define tcp_clear_flags(pcb, clr_flags) do { (pcb)->flags = (tcpflags_t)((pcb)->flags & ~(clr_flags)); } while(0)
385 #define tcp_is_flag_set(pcb, flag) (((pcb)->flags & (flag)) != 0)
386  
387 #if LWIP_TCP_TIMESTAMPS
388 #define tcp_mss(pcb) (((pcb)->flags & TF_TIMESTAMP) ? ((pcb)->mss - 12) : (pcb)->mss)
389 #else /* LWIP_TCP_TIMESTAMPS */
390 /** @ingroup tcp_raw */
391 #define tcp_mss(pcb) ((pcb)->mss)
392 #endif /* LWIP_TCP_TIMESTAMPS */
393 /** @ingroup tcp_raw */
394 #define tcp_sndbuf(pcb) (TCPWND16((pcb)->snd_buf))
395 /** @ingroup tcp_raw */
396 #define tcp_sndqueuelen(pcb) ((pcb)->snd_queuelen)
397 /** @ingroup tcp_raw */
398 #define tcp_nagle_disable(pcb) tcp_set_flags(pcb, TF_NODELAY)
399 /** @ingroup tcp_raw */
400 #define tcp_nagle_enable(pcb) tcp_clear_flags(pcb, TF_NODELAY)
401 /** @ingroup tcp_raw */
402 #define tcp_nagle_disabled(pcb) tcp_is_flag_set(pcb, TF_NODELAY)
403  
404 #if TCP_LISTEN_BACKLOG
405 #define tcp_backlog_set(pcb, new_backlog) do { \
406 LWIP_ASSERT("pcb->state == LISTEN (called for wrong pcb?)", (pcb)->state == LISTEN); \
407 ((struct tcp_pcb_listen *)(pcb))->backlog = ((new_backlog) ? (new_backlog) : 1); } while(0)
408 void tcp_backlog_delayed(struct tcp_pcb* pcb);
409 void tcp_backlog_accepted(struct tcp_pcb* pcb);
410 #else /* TCP_LISTEN_BACKLOG */
411 #define tcp_backlog_set(pcb, new_backlog)
412 #define tcp_backlog_delayed(pcb)
413 #define tcp_backlog_accepted(pcb)
414 #endif /* TCP_LISTEN_BACKLOG */
415 #define tcp_accepted(pcb) do { LWIP_UNUSED_ARG(pcb); } while(0) /* compatibility define, not needed any more */
416  
417 void tcp_recved (struct tcp_pcb *pcb, u16_t len);
418 err_t tcp_bind (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
419 u16_t port);
420 void tcp_bind_netif(struct tcp_pcb *pcb, const struct netif *netif);
421 err_t tcp_bind_to_netif (struct tcp_pcb *pcb, const char ifname[3]);
422 err_t tcp_connect (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
423 u16_t port, tcp_connected_fn connected);
424  
425 struct tcp_pcb * tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err);
426 struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
427 /** @ingroup tcp_raw */
428 #define tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
429  
430 void tcp_abort (struct tcp_pcb *pcb);
431 err_t tcp_close (struct tcp_pcb *pcb);
432 err_t tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx);
433  
434 err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
435 u8_t apiflags);
436  
437 void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
438  
439 err_t tcp_output (struct tcp_pcb *pcb);
440  
441 err_t tcp_tcp_get_tcp_addrinfo(struct tcp_pcb *pcb, int local, ip_addr_t *addr, u16_t *port);
442  
443 #define tcp_dbg_get_tcp_state(pcb) ((pcb)->state)
444  
445 /* for compatibility with older implementation */
446 #define tcp_new_ip6() tcp_new_ip_type(IPADDR_TYPE_V6)
447  
448 #ifdef __cplusplus
449 }
450 #endif
451  
452 #endif /* LWIP_TCP */
453  
454 #endif /* LWIP_HDR_TCP_H */