The Pedigree Project  0.1
tcp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
26 /*
27  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without modification,
31  * are permitted provided that the following conditions are met:
32  *
33  * 1. Redistributions of source code must retain the above copyright notice,
34  * this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright notice,
36  * this list of conditions and the following disclaimer in the documentation
37  * and/or other materials provided with the distribution.
38  * 3. The name of the author may not be used to endorse or promote products
39  * derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
44  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
46  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
49  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
50  * OF SUCH DAMAGE.
51  *
52  * This file is part of the lwIP TCP/IP stack.
53  *
54  * Author: Adam Dunkels <adam@sics.se>
55  *
56  */
57 #ifndef LWIP_HDR_TCP_H
58 #define LWIP_HDR_TCP_H
59 
60 #include "lwip/opt.h"
61 
62 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
63 
64 #include "lwip/mem.h"
65 #include "lwip/pbuf.h"
66 #include "lwip/ip.h"
67 #include "lwip/icmp.h"
68 #include "lwip/err.h"
69 #include "lwip/ip6.h"
70 #include "lwip/ip6_addr.h"
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 struct tcp_pcb;
77 
87 typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);
88 
99 typedef err_t (*tcp_recv_fn)(void *arg, struct tcp_pcb *tpcb,
100  struct pbuf *p, err_t err);
101 
113 typedef err_t (*tcp_sent_fn)(void *arg, struct tcp_pcb *tpcb,
114  u16_t len);
115 
125 typedef err_t (*tcp_poll_fn)(void *arg, struct tcp_pcb *tpcb);
126 
137 typedef void (*tcp_err_fn)(void *arg, err_t err);
138 
151 typedef err_t (*tcp_connected_fn)(void *arg, struct tcp_pcb *tpcb, err_t err);
152 
153 #if LWIP_WND_SCALE
154 #define RCV_WND_SCALE(pcb, wnd) (((wnd) >> (pcb)->rcv_scale))
155 #define SND_WND_SCALE(pcb, wnd) (((wnd) << (pcb)->snd_scale))
156 #define TCPWND16(x) ((u16_t)LWIP_MIN((x), 0xFFFF))
157 #define TCP_WND_MAX(pcb) ((tcpwnd_size_t)(((pcb)->flags & TF_WND_SCALE) ? TCP_WND : TCPWND16(TCP_WND)))
158 typedef u32_t tcpwnd_size_t;
159 #else
160 #define RCV_WND_SCALE(pcb, wnd) (wnd)
161 #define SND_WND_SCALE(pcb, wnd) (wnd)
162 #define TCPWND16(x) (x)
163 #define TCP_WND_MAX(pcb) TCP_WND
164 typedef u16_t tcpwnd_size_t;
165 #endif
166 
167 #if LWIP_WND_SCALE || TCP_LISTEN_BACKLOG || LWIP_TCP_TIMESTAMPS
168 typedef u16_t tcpflags_t;
169 #else
170 typedef u8_t tcpflags_t;
171 #endif
172 
173 enum tcp_state {
174  CLOSED = 0,
175  LISTEN = 1,
176  SYN_SENT = 2,
177  SYN_RCVD = 3,
178  ESTABLISHED = 4,
179  FIN_WAIT_1 = 5,
180  FIN_WAIT_2 = 6,
181  CLOSE_WAIT = 7,
182  CLOSING = 8,
183  LAST_ACK = 9,
184  TIME_WAIT = 10
185 };
186 
190 #define TCP_PCB_COMMON(type) \
191  type *next; /* for the linked list */ \
192  void *callback_arg; \
193  enum tcp_state state; /* TCP state */ \
194  u8_t prio; \
195  /* ports are in host byte order */ \
196  u16_t local_port
197 
198 
200 struct tcp_pcb_listen {
202  IP_PCB;
204  TCP_PCB_COMMON(struct tcp_pcb_listen);
205 
206 #if LWIP_CALLBACK_API
207  /* Function to call when a listener has been connected. */
208  tcp_accept_fn accept;
209 #endif /* LWIP_CALLBACK_API */
210 
211 #if TCP_LISTEN_BACKLOG
212  u8_t backlog;
213  u8_t accepts_pending;
214 #endif /* TCP_LISTEN_BACKLOG */
215 };
216 
217 
219 struct tcp_pcb {
221  IP_PCB;
223  TCP_PCB_COMMON(struct tcp_pcb);
224 
225  /* ports are in host byte order */
226  u16_t remote_port;
227 
228  tcpflags_t flags;
229 #define TF_ACK_DELAY 0x01U /* Delayed ACK. */
230 #define TF_ACK_NOW 0x02U /* Immediate ACK. */
231 #define TF_INFR 0x04U /* In fast recovery. */
232 #define TF_CLOSEPEND 0x08U /* If this is set, tcp_close failed to enqueue the FIN (retried in tcp_tmr) */
233 #define TF_RXCLOSED 0x10U /* rx closed by tcp_shutdown */
234 #define TF_FIN 0x20U /* Connection was closed locally (FIN segment enqueued). */
235 #define TF_NODELAY 0x40U /* Disable Nagle algorithm */
236 #define TF_NAGLEMEMERR 0x80U /* nagle enabled, memerr, try to output to prevent delayed ACK to happen */
237 #if LWIP_WND_SCALE
238 #define TF_WND_SCALE 0x0100U /* Window Scale option enabled */
239 #endif
240 #if TCP_LISTEN_BACKLOG
241 #define TF_BACKLOGPEND 0x0200U /* If this is set, a connection pcb has increased the backlog on its listener */
242 #endif
243 #if LWIP_TCP_TIMESTAMPS
244 #define TF_TIMESTAMP 0x0400U /* Timestamp option enabled */
245 #endif
246 
247  /* the rest of the fields are in host byte order
248  as we have to do some math with them */
249 
250  /* Timers */
251  u8_t polltmr, pollinterval;
252  u8_t last_timer;
253  u32_t tmr;
254 
255  /* receiver variables */
256  u32_t rcv_nxt; /* next seqno expected */
257  tcpwnd_size_t rcv_wnd; /* receiver window available */
258  tcpwnd_size_t rcv_ann_wnd; /* receiver window to announce */
259  u32_t rcv_ann_right_edge; /* announced right edge of window */
260 
261  /* Retransmission timer. */
262  s16_t rtime;
263 
264  u16_t mss; /* maximum segment size */
265 
266  /* RTT (round trip time) estimation variables */
267  u32_t rttest; /* RTT estimate in 500ms ticks */
268  u32_t rtseq; /* sequence number being timed */
269  s16_t sa, sv; /* @todo document this */
270 
271  s16_t rto; /* retransmission time-out */
272  u8_t nrtx; /* number of retransmissions */
273 
274  /* fast retransmit/recovery */
275  u8_t dupacks;
276  u32_t lastack; /* Highest acknowledged seqno. */
277 
278  /* congestion avoidance/control variables */
279  tcpwnd_size_t cwnd;
280  tcpwnd_size_t ssthresh;
281 
282  /* sender variables */
283  u32_t snd_nxt; /* next new seqno to be sent */
284  u32_t snd_wl1, snd_wl2; /* Sequence and acknowledgement numbers of last
285  window update. */
286  u32_t snd_lbb; /* Sequence number of next byte to be buffered. */
287  tcpwnd_size_t snd_wnd; /* sender window */
288  tcpwnd_size_t snd_wnd_max; /* the maximum sender window announced by the remote host */
289 
290  tcpwnd_size_t snd_buf; /* Available buffer space for sending (in bytes). */
291 #define TCP_SNDQUEUELEN_OVERFLOW (0xffffU-3)
292  u16_t snd_queuelen; /* Number of pbufs currently in the send buffer. */
293 
294 #if TCP_OVERSIZE
295  /* Extra bytes available at the end of the last pbuf in unsent. */
296  u16_t unsent_oversize;
297 #endif /* TCP_OVERSIZE */
298 
299  /* These are ordered by sequence number: */
300  struct tcp_seg *unsent; /* Unsent (queued) segments. */
301  struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
302 #if TCP_QUEUE_OOSEQ
303  struct tcp_seg *ooseq; /* Received out of sequence segments. */
304 #endif /* TCP_QUEUE_OOSEQ */
305 
306  struct pbuf *refused_data; /* Data previously received but not yet taken by upper layer */
307 
308 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
309  struct tcp_pcb_listen* listener;
310 #endif /* LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG */
311 
312 #if LWIP_CALLBACK_API
313  /* Function to be called when more send buffer space is available. */
314  tcp_sent_fn sent;
315  /* Function to be called when (in-sequence) data has arrived. */
316  tcp_recv_fn recv;
317  /* Function to be called when a connection has been set up. */
318  tcp_connected_fn connected;
319  /* Function which is called periodically. */
320  tcp_poll_fn poll;
321  /* Function to be called whenever a fatal error occurs. */
322  tcp_err_fn errf;
323 #endif /* LWIP_CALLBACK_API */
324 
325 #if LWIP_TCP_TIMESTAMPS
326  u32_t ts_lastacksent;
327  u32_t ts_recent;
328 #endif /* LWIP_TCP_TIMESTAMPS */
329 
330  /* idle time before KEEPALIVE is sent */
331  u32_t keep_idle;
332 #if LWIP_TCP_KEEPALIVE
333  u32_t keep_intvl;
334  u32_t keep_cnt;
335 #endif /* LWIP_TCP_KEEPALIVE */
336 
337  /* Persist timer counter */
338  u8_t persist_cnt;
339  /* Persist timer back-off */
340  u8_t persist_backoff;
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 #if LWIP_TCP_TIMESTAMPS
384 #define tcp_mss(pcb) (((pcb)->flags & TF_TIMESTAMP) ? ((pcb)->mss - 12) : (pcb)->mss)
385 #else /* LWIP_TCP_TIMESTAMPS */
386 #define tcp_mss(pcb) ((pcb)->mss)
387 #endif /* LWIP_TCP_TIMESTAMPS */
388 #define tcp_sndbuf(pcb) (TCPWND16((pcb)->snd_buf))
389 #define tcp_sndqueuelen(pcb) ((pcb)->snd_queuelen)
390 
391 #define tcp_nagle_disable(pcb) ((pcb)->flags |= TF_NODELAY)
392 
393 #define tcp_nagle_enable(pcb) ((pcb)->flags = (tcpflags_t)((pcb)->flags & ~TF_NODELAY))
394 
395 #define tcp_nagle_disabled(pcb) (((pcb)->flags & TF_NODELAY) != 0)
396 
397 #if TCP_LISTEN_BACKLOG
398 #define tcp_backlog_set(pcb, new_backlog) do { \
399  LWIP_ASSERT("pcb->state == LISTEN (called for wrong pcb?)", (pcb)->state == LISTEN); \
400  ((struct tcp_pcb_listen *)(pcb))->backlog = ((new_backlog) ? (new_backlog) : 1); } while(0)
401 void tcp_backlog_delayed(struct tcp_pcb* pcb);
402 void tcp_backlog_accepted(struct tcp_pcb* pcb);
403 #else /* TCP_LISTEN_BACKLOG */
404 #define tcp_backlog_set(pcb, new_backlog)
405 #define tcp_backlog_delayed(pcb)
406 #define tcp_backlog_accepted(pcb)
407 #endif /* TCP_LISTEN_BACKLOG */
408 #define tcp_accepted(pcb) /* compatibility define, not needed any more */
409 
410 void tcp_recved (struct tcp_pcb *pcb, u16_t len);
411 err_t tcp_bind (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
412  u16_t port);
413 err_t tcp_connect (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
414  u16_t port, tcp_connected_fn connected);
415 
416 struct tcp_pcb * tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err);
417 struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
419 #define tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
420 
421 void tcp_abort (struct tcp_pcb *pcb);
422 err_t tcp_close (struct tcp_pcb *pcb);
423 err_t tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx);
424 
425 /* Flags for "apiflags" parameter in tcp_write */
426 #define TCP_WRITE_FLAG_COPY 0x01
427 #define TCP_WRITE_FLAG_MORE 0x02
428 
429 err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
430  u8_t apiflags);
431 
432 void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
433 
434 #define TCP_PRIO_MIN 1
435 #define TCP_PRIO_NORMAL 64
436 #define TCP_PRIO_MAX 127
437 
438 err_t tcp_output (struct tcp_pcb *pcb);
439 
440 
441 const char* tcp_debug_state_str(enum tcp_state s);
442 
443 /* for compatibility with older implementation */
444 #define tcp_new_ip6() tcp_new_ip_type(IPADDR_TYPE_V6)
445 
446 #ifdef __cplusplus
447 }
448 #endif
449 
450 #endif /* LWIP_TCP */
451 
452 #endif /* LWIP_HDR_TCP_H */
#define IP_PCB
Definition: ip.h:95
Definition: pbuf.h:161
s8_t err_t
Definition: err.h:76