~ubuntu-branches/ubuntu/trusty/virtualbox-lts-xenial/trusty-updates

« back to all changes in this revision

Viewing changes to src/VBox/Devices/Network/lwip-new/src/include/lwip/tcp.h

  • Committer: Package Import Robot
  • Author(s): Gianfranco Costamagna
  • Date: 2016-02-23 14:28:26 UTC
  • Revision ID: package-import@ubuntu.com-20160223142826-bdu69el2z6wa2a44
Tags: upstream-4.3.36-dfsg
ImportĀ upstreamĀ versionĀ 4.3.36-dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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/mem.h"
 
40
#include "lwip/pbuf.h"
 
41
#include "lwip/ip.h"
 
42
#include "lwip/icmp.h"
 
43
#include "lwip/err.h"
 
44
#include "lwip/ip6.h"
 
45
#include "lwip/ip6_addr.h"
 
46
 
 
47
#ifdef __cplusplus
 
48
extern "C" {
 
49
#endif
 
50
 
 
51
struct tcp_pcb;
 
52
 
 
53
/** Function prototype for tcp accept callback functions. Called when a new
 
54
 * connection can be accepted on a listening pcb.
 
55
 *
 
56
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 
57
 * @param newpcb The new connection pcb
 
58
 * @param err An error code if there has been an error accepting.
 
59
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 
60
 *            callback function!
 
61
 */
 
62
typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);
 
63
 
 
64
/** Function prototype for tcp receive callback functions. Called when data has
 
65
 * been received.
 
66
 *
 
67
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 
68
 * @param tpcb The connection pcb which received data
 
69
 * @param p The received data (or NULL when the connection has been closed!)
 
70
 * @param err An error code if there has been an error receiving
 
71
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 
72
 *            callback function!
 
73
 */
 
74
typedef err_t (*tcp_recv_fn)(void *arg, struct tcp_pcb *tpcb,
 
75
                             struct pbuf *p, err_t err);
 
76
 
 
77
/** Function prototype for tcp sent callback functions. Called when sent data has
 
78
 * been acknowledged by the remote side. Use it to free corresponding resources.
 
79
 * This also means that the pcb has now space available to send new data.
 
80
 *
 
81
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 
82
 * @param tpcb The connection pcb for which data has been acknowledged
 
83
 * @param len The amount of bytes acknowledged
 
84
 * @return ERR_OK: try to send some data by calling tcp_output
 
85
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 
86
 *            callback function!
 
87
 */
 
88
typedef err_t (*tcp_sent_fn)(void *arg, struct tcp_pcb *tpcb,
 
89
                              u16_t len);
 
90
 
 
91
/** Function prototype for tcp poll callback functions. Called periodically as
 
92
 * specified by @see tcp_poll.
 
93
 *
 
94
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 
95
 * @param tpcb tcp pcb
 
96
 * @return ERR_OK: try to send some data by calling tcp_output
 
97
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 
98
 *            callback function!
 
99
 */
 
100
typedef err_t (*tcp_poll_fn)(void *arg, struct tcp_pcb *tpcb);
 
101
 
 
102
/** Function prototype for tcp error callback functions. Called when the pcb
 
103
 * receives a RST or is unexpectedly closed for any other reason.
 
104
 *
 
105
 * @note The corresponding pcb is already freed when this callback is called!
 
106
 *
 
107
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 
108
 * @param err Error code to indicate why the pcb has been closed
 
109
 *            ERR_ABRT: aborted through tcp_abort or by a TCP timer
 
110
 *            ERR_RST: the connection was reset by the remote host
 
111
 */
 
112
typedef void  (*tcp_err_fn)(void *arg, err_t err);
 
113
 
 
114
/** Function prototype for tcp connected callback functions. Called when a pcb
 
115
 * is connected to the remote side after initiating a connection attempt by
 
116
 * calling tcp_connect().
 
117
 *
 
118
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 
119
 * @param tpcb The connection pcb which is connected
 
120
 * @param err An unused error code, always ERR_OK currently ;-) TODO!
 
121
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 
122
 *            callback function!
 
123
 *
 
124
 * @note When a connection attempt fails, the error callback is currently called!
 
125
 */
 
126
typedef err_t (*tcp_connected_fn)(void *arg, struct tcp_pcb *tpcb, err_t err);
 
127
 
 
128
enum tcp_state {
 
129
  CLOSED      = 0,
 
130
  LISTEN      = 1,
 
131
  SYN_SENT    = 2,
 
132
#if LWIP_CONNECTION_PROXY
 
133
  /* see tcp_proxy_listen_input */
 
134
  SYN_RCVD_0  = 3,
 
135
#endif
 
136
  SYN_RCVD    = 4,
 
137
  ESTABLISHED = 5,
 
138
  FIN_WAIT_1  = 6,
 
139
  FIN_WAIT_2  = 7,
 
140
  CLOSE_WAIT  = 8,
 
141
  CLOSING     = 9,
 
142
  LAST_ACK    = 10,
 
143
  TIME_WAIT   = 11
 
144
};
 
145
 
 
146
#if LWIP_CALLBACK_API
 
147
  /* Function to call when a listener has been connected.
 
148
   * @param arg user-supplied argument (tcp_pcb.callback_arg)
 
149
   * @param pcb a new tcp_pcb that now is connected
 
150
   * @param err an error argument (TODO: that is current always ERR_OK?)
 
151
   * @return ERR_OK: accept the new connection,
 
152
   *                 any other err_t abortsthe new connection
 
153
   */
 
154
#define DEF_ACCEPT_CALLBACK  tcp_accept_fn accept;
 
155
#else /* LWIP_CALLBACK_API */
 
156
#define DEF_ACCEPT_CALLBACK
 
157
#endif /* LWIP_CALLBACK_API */
 
158
 
 
159
/**
 
160
 * members common to struct tcp_pcb and struct tcp_listen_pcb
 
161
 */
 
162
#define TCP_PCB_COMMON(type) \
 
163
  type *next; /* for the linked list */ \
 
164
  void *callback_arg; \
 
165
  /* the accept callback for listen- and normal pcbs, if LWIP_CALLBACK_API */ \
 
166
  DEF_ACCEPT_CALLBACK \
 
167
  enum tcp_state state; /* TCP state */ \
 
168
  u8_t prio; \
 
169
  /* ports are in host byte order */ \
 
170
  u16_t local_port
 
171
 
 
172
 
 
173
/* the TCP protocol control block */
 
174
struct tcp_pcb {
 
175
/** common PCB members */
 
176
  IP_PCB;
 
177
/** protocol specific PCB members */
 
178
  TCP_PCB_COMMON(struct tcp_pcb);
 
179
 
 
180
  /* ports are in host byte order */
 
181
  u16_t remote_port;
 
182
  
 
183
  u8_t flags;
 
184
#define TF_ACK_DELAY   ((u8_t)0x01U)   /* Delayed ACK. */
 
185
#define TF_ACK_NOW     ((u8_t)0x02U)   /* Immediate ACK. */
 
186
#define TF_INFR        ((u8_t)0x04U)   /* In fast recovery. */
 
187
#define TF_TIMESTAMP   ((u8_t)0x08U)   /* Timestamp option enabled */
 
188
#define TF_RXCLOSED    ((u8_t)0x10U)   /* rx closed by tcp_shutdown */
 
189
#define TF_FIN         ((u8_t)0x20U)   /* Connection was closed locally (FIN segment enqueued). */
 
190
#define TF_NODELAY     ((u8_t)0x40U)   /* Disable Nagle algorithm */
 
191
#define TF_NAGLEMEMERR ((u8_t)0x80U)   /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
 
192
 
 
193
  /* the rest of the fields are in host byte order
 
194
     as we have to do some math with them */
 
195
 
 
196
  /* Timers */
 
197
  u8_t polltmr, pollinterval;
 
198
  u8_t last_timer;
 
199
  u32_t tmr;
 
200
 
 
201
  /* receiver variables */
 
202
  u32_t rcv_nxt;   /* next seqno expected */
 
203
  u16_t rcv_wnd;   /* receiver window available */
 
204
  u16_t rcv_ann_wnd; /* receiver window to announce */
 
205
  u32_t rcv_ann_right_edge; /* announced right edge of window */
 
206
 
 
207
  /* Retransmission timer. */
 
208
  s16_t rtime;
 
209
 
 
210
  u16_t mss;   /* maximum segment size */
 
211
 
 
212
  /* RTT (round trip time) estimation variables */
 
213
  u32_t rttest; /* RTT estimate in 500ms ticks */
 
214
  u32_t rtseq;  /* sequence number being timed */
 
215
  s16_t sa, sv; /* @todo document this */
 
216
 
 
217
  s16_t rto;    /* retransmission time-out */
 
218
  u8_t nrtx;    /* number of retransmissions */
 
219
 
 
220
  /* fast retransmit/recovery */
 
221
  u8_t dupacks;
 
222
  u32_t lastack; /* Highest acknowledged seqno. */
 
223
 
 
224
  /* congestion avoidance/control variables */
 
225
  u16_t cwnd;
 
226
  u16_t ssthresh;
 
227
 
 
228
  /* sender variables */
 
229
  u32_t snd_nxt;   /* next new seqno to be sent */
 
230
  u32_t snd_wl1, snd_wl2; /* Sequence and acknowledgement numbers of last
 
231
                             window update. */
 
232
  u32_t snd_lbb;       /* Sequence number of next byte to be buffered. */
 
233
  u16_t snd_wnd;   /* sender window */
 
234
  u16_t snd_wnd_max; /* the maximum sender window announced by the remote host */
 
235
 
 
236
  u16_t acked;
 
237
 
 
238
  u16_t snd_buf;   /* Available buffer space for sending (in bytes). */
 
239
#define TCP_SNDQUEUELEN_OVERFLOW (0xffffU-3)
 
240
  u16_t snd_queuelen; /* Available buffer space for sending (in pbufs). */
 
241
 
 
242
#if TCP_OVERSIZE
 
243
  /* Extra bytes available at the end of the last pbuf in unsent. */
 
244
  u16_t unsent_oversize;
 
245
#endif /* TCP_OVERSIZE */ 
 
246
 
 
247
  /* These are ordered by sequence number: */
 
248
  struct tcp_seg *unsent;   /* Unsent (queued) segments. */
 
249
  struct tcp_seg *unacked;  /* Sent but unacknowledged segments. */
 
250
#if TCP_QUEUE_OOSEQ  
 
251
  struct tcp_seg *ooseq;    /* Received out of sequence segments. */
 
252
#endif /* TCP_QUEUE_OOSEQ */
 
253
 
 
254
  struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
 
255
 
 
256
#if LWIP_CALLBACK_API
 
257
  /* Function to be called when more send buffer space is available. */
 
258
  tcp_sent_fn sent;
 
259
  /* Function to be called when (in-sequence) data has arrived. */
 
260
  tcp_recv_fn recv;
 
261
  /* Function to be called when a connection has been set up. */
 
262
  tcp_connected_fn connected;
 
263
  /* Function which is called periodically. */
 
264
  tcp_poll_fn poll;
 
265
  /* Function to be called whenever a fatal error occurs. */
 
266
  tcp_err_fn errf;
 
267
#endif /* LWIP_CALLBACK_API */
 
268
 
 
269
#if LWIP_TCP_TIMESTAMPS
 
270
  u32_t ts_lastacksent;
 
271
  u32_t ts_recent;
 
272
#endif /* LWIP_TCP_TIMESTAMPS */
 
273
 
 
274
  /* idle time before KEEPALIVE is sent */
 
275
  u32_t keep_idle;
 
276
#if LWIP_TCP_KEEPALIVE
 
277
  u32_t keep_intvl;
 
278
  u32_t keep_cnt;
 
279
#endif /* LWIP_TCP_KEEPALIVE */
 
280
  
 
281
  /* Persist timer counter */
 
282
  u8_t persist_cnt;
 
283
  /* Persist timer back-off */
 
284
  u8_t persist_backoff;
 
285
 
 
286
  /* KEEPALIVE counter */
 
287
  u8_t keep_cnt_sent;
 
288
};
 
289
 
 
290
struct tcp_pcb_listen {
 
291
/* Common members of all PCB types */
 
292
  IP_PCB;
 
293
/* Protocol specific PCB members */
 
294
  TCP_PCB_COMMON(struct tcp_pcb_listen);
 
295
 
 
296
#if TCP_LISTEN_BACKLOG
 
297
  u8_t backlog;
 
298
  u8_t accepts_pending;
 
299
#endif /* TCP_LISTEN_BACKLOG */
 
300
#if LWIP_IPV6
 
301
  u8_t accept_any_ip_version;
 
302
#endif /* LWIP_IPV6 */
 
303
};
 
304
 
 
305
#if LWIP_EVENT_API
 
306
 
 
307
enum lwip_event {
 
308
  LWIP_EVENT_ACCEPT,
 
309
  LWIP_EVENT_SENT,
 
310
  LWIP_EVENT_RECV,
 
311
  LWIP_EVENT_CONNECTED,
 
312
  LWIP_EVENT_POLL,
 
313
  LWIP_EVENT_ERR
 
314
};
 
315
 
 
316
err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
 
317
         enum lwip_event,
 
318
         struct pbuf *p,
 
319
         u16_t size,
 
320
         err_t err);
 
321
 
 
322
#endif /* LWIP_EVENT_API */
 
323
 
 
324
/* Application program's interface: */
 
325
struct tcp_pcb * tcp_new     (void);
 
326
 
 
327
void             tcp_arg     (struct tcp_pcb *pcb, void *arg);
 
328
void             tcp_accept  (struct tcp_pcb *pcb, tcp_accept_fn accept);
 
329
#if LWIP_CONNECTION_PROXY
 
330
/* when proxied connection is accepted there's no listening pcb */
 
331
void             tcp_proxy_arg(void *arg);
 
332
void             tcp_proxy_accept(tcp_accept_fn accept);
 
333
#endif
 
334
void             tcp_recv    (struct tcp_pcb *pcb, tcp_recv_fn recv);
 
335
void             tcp_sent    (struct tcp_pcb *pcb, tcp_sent_fn sent);
 
336
void             tcp_poll    (struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval);
 
337
void             tcp_err     (struct tcp_pcb *pcb, tcp_err_fn err);
 
338
 
 
339
#define          tcp_mss(pcb)             (((pcb)->flags & TF_TIMESTAMP) ? ((pcb)->mss - 12)  : (pcb)->mss)
 
340
#define          tcp_sndbuf(pcb)          ((pcb)->snd_buf)
 
341
#define          tcp_sndqueuelen(pcb)     ((pcb)->snd_queuelen)
 
342
#define          tcp_nagle_disable(pcb)   ((pcb)->flags |= TF_NODELAY)
 
343
#define          tcp_nagle_enable(pcb)    ((pcb)->flags &= ~TF_NODELAY)
 
344
#define          tcp_nagle_disabled(pcb)  (((pcb)->flags & TF_NODELAY) != 0)
 
345
 
 
346
#if TCP_LISTEN_BACKLOG
 
347
#define          tcp_accepted(pcb) do { \
 
348
  LWIP_ASSERT("pcb->state == LISTEN (called for wrong pcb?)", pcb->state == LISTEN); \
 
349
  (((struct tcp_pcb_listen *)(pcb))->accepts_pending--); } while(0)
 
350
#else  /* TCP_LISTEN_BACKLOG */
 
351
#define          tcp_accepted(pcb) do { \
 
352
  LWIP_ASSERT("pcb->state == LISTEN (called for wrong pcb?)", pcb->state == LISTEN); \
 
353
  LWIP_UNUSED_ARG(pcb); } while(0)
 
354
#endif /* TCP_LISTEN_BACKLOG */
 
355
 
 
356
void             tcp_recved  (struct tcp_pcb *pcb, u16_t len);
 
357
err_t            tcp_bind    (struct tcp_pcb *pcb, ip_addr_t *ipaddr,
 
358
                              u16_t port);
 
359
#if LWIP_CONNECTION_PROXY
 
360
err_t            tcp_proxy_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr,
 
361
                                u16_t port);
 
362
#endif /* LWIP_CONNECTION_PROXY */
 
363
err_t            tcp_connect (struct tcp_pcb *pcb, ip_addr_t *ipaddr,
 
364
                              u16_t port, tcp_connected_fn connected);
 
365
 
 
366
struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
 
367
#define          tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
 
368
 
 
369
#if LWIP_CONNECTION_PROXY
 
370
err_t            tcp_proxy_accept_confirm(struct tcp_pcb *npcb);
 
371
#endif
 
372
 
 
373
void             tcp_abort (struct tcp_pcb *pcb);
 
374
err_t            tcp_close   (struct tcp_pcb *pcb);
 
375
err_t            tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx);
 
376
 
 
377
/* Flags for "apiflags" parameter in tcp_write */
 
378
#define TCP_WRITE_FLAG_COPY 0x01
 
379
#define TCP_WRITE_FLAG_MORE 0x02
 
380
 
 
381
err_t            tcp_write   (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
 
382
                              u8_t apiflags);
 
383
 
 
384
void             tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
 
385
 
 
386
#define TCP_PRIO_MIN    1
 
387
#define TCP_PRIO_NORMAL 64
 
388
#define TCP_PRIO_MAX    127
 
389
 
 
390
err_t            tcp_output  (struct tcp_pcb *pcb);
 
391
 
 
392
 
 
393
const char* tcp_debug_state_str(enum tcp_state s);
 
394
 
 
395
#if LWIP_IPV6
 
396
struct tcp_pcb * tcp_new_ip6 (void);
 
397
#define          tcp_bind_ip6(pcb, ip6addr, port) \
 
398
                   tcp_bind(pcb, ip6_2_ip(ip6addr), port)
 
399
#define          tcp_connect_ip6(pcb, ip6addr, port, connected) \
 
400
                   tcp_connect(pcb, ip6_2_ip(ip6addr), port, connected)
 
401
struct tcp_pcb * tcp_listen_dual_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
 
402
#define          tcp_listen_dual(pcb) tcp_listen_dual_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
 
403
#else /* LWIP_IPV6 */
 
404
#define          tcp_listen_dual_with_backlog(pcb, backlog) tcp_listen_with_backlog(pcb, backlog)
 
405
#define          tcp_listen_dual(pcb) tcp_listen(pcb)
 
406
#endif /* LWIP_IPV6 */
 
407
 
 
408
 
 
409
#ifdef __cplusplus
 
410
}
 
411
#endif
 
412
 
 
413
#endif /* LWIP_TCP */
 
414
 
 
415
#endif /* __LWIP_TCP_H__ */