2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
32 #ifndef __LWIP_TCP_H__
33 #define __LWIP_TCP_H__
38 #include "lwip/pbuf.h"
41 #include "lwip/icmp.h"
47 /* Functions for interfacing with TCP: */
49 /* Lower layer interface to TCP: */
50 void tcp_init (void); /* Must be called first to
52 void tcp_tmr (void); /* Must be called every
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);
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,
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,
69 void tcp_poll (struct tcp_pcb *pcb,
70 err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
72 void tcp_err (struct tcp_pcb *pcb,
73 void (* err)(void *arg, err_t err));
75 #define tcp_mss(pcb) ((pcb)->mss)
76 #define tcp_sndbuf(pcb) ((pcb)->snd_buf)
78 void tcp_recved (struct tcp_pcb *pcb, u16_t len);
79 err_t tcp_bind (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
81 err_t tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
82 u16_t port, err_t (* connected)(void *arg,
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,
91 void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
93 #define TCP_PRIO_MIN 1
94 #define TCP_PRIO_NORMAL 64
95 #define TCP_PRIO_MAX 127
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);
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);
111 #define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
112 #define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
113 #define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
114 #define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
116 #define TCP_FIN 0x01U
117 #define TCP_SYN 0x02U
118 #define TCP_RST 0x04U
119 #define TCP_PSH 0x08U
120 #define TCP_ACK 0x10U
121 #define TCP_URG 0x20U
122 #define TCP_ECE 0x40U
123 #define TCP_CWR 0x80U
125 #define TCP_FLAGS 0x3fU
127 /* Length of the TCP header, excluding options. */
130 #ifndef TCP_TMR_INTERVAL
131 #define TCP_TMR_INTERVAL 250 /* The TCP timer interval in
133 #endif /* TCP_TMR_INTERVAL */
135 #ifndef TCP_FAST_INTERVAL
136 #define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in
138 #endif /* TCP_FAST_INTERVAL */
140 #ifndef TCP_SLOW_INTERVAL
141 #define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in
143 #endif /* TCP_SLOW_INTERVAL */
145 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
146 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
148 #define TCP_OOSEQ_TIMEOUT 6 /* x RTO */
150 #define TCP_MSL 60000 /* The maximum segment lifetime in microseconds */
153 * User-settable options (used with setsockopt).
155 #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
156 #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keepalive miliseconds */
158 /* Keepalive values */
159 #define TCP_KEEPDEFAULT 7200000 /* KEEPALIVE timer in miliseconds */
160 #define TCP_KEEPINTVL 75000 /* Time between KEEPALIVE probes in miliseconds */
161 #define TCP_KEEPCNT 9 /* Counter for KEEPALIVE probes */
162 #define TCP_MAXIDLE TCP_KEEPCNT * TCP_KEEPINTVL /* Maximum KEEPALIVE probe time */
169 u16_t _hdrlen_rsvd_flags;
175 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
176 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
177 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
179 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
180 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
181 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))
182 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))
183 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
185 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
186 TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
203 /* the TCP protocol control block */
205 /* Common members of all PCB types */
208 /* Protocol specific PCB members */
210 struct tcp_pcb *next; /* for the linked list */
212 enum tcp_state state; /* TCP state */
221 #define TF_ACK_DELAY (u8_t)0x01U /* Delayed ACK. */
222 #define TF_ACK_NOW (u8_t)0x02U /* Immediate ACK. */
223 #define TF_INFR (u8_t)0x04U /* In fast recovery. */
224 #define TF_RESET (u8_t)0x08U /* Connection was reset. */
225 #define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
226 #define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
227 #define TF_NODELAY (u8_t)0x40U /* Disable Nagle algorithm */
229 /* receiver varables */
230 u32_t rcv_nxt; /* next seqno expected */
231 u16_t rcv_wnd; /* receiver window */
235 u8_t polltmr, pollinterval;
237 /* Retransmission timer. */
240 u16_t mss; /* maximum segment size */
242 /* RTT estimation variables. */
243 u16_t rttest; /* RTT estimate in 500ms ticks */
244 u32_t rtseq; /* sequence number being timed */
247 u16_t rto; /* retransmission time-out */
248 u8_t nrtx; /* number of retransmissions */
250 /* fast retransmit/recovery */
251 u32_t lastack; /* Highest acknowledged seqno. */
254 /* congestion avoidance/control variables */
258 /* sender variables */
259 u32_t snd_nxt, /* next seqno to be sent */
260 snd_max, /* Highest seqno sent. */
261 snd_wnd, /* sender window */
262 snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last
264 snd_lbb; /* Sequence number of next byte to be buffered. */
268 u16_t snd_buf; /* Available buffer space for sending (in bytes). */
269 u8_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
272 /* These are ordered by sequence number: */
273 struct tcp_seg *unsent; /* Unsent (queued) segments. */
274 struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
276 struct tcp_seg *ooseq; /* Received out of sequence segments. */
277 #endif /* TCP_QUEUE_OOSEQ */
279 #if LWIP_CALLBACK_API
280 /* Function to be called when more send buffer space is available. */
281 err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
283 /* Function to be called when (in-sequence) data has arrived. */
284 err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
286 /* Function to be called when a connection has been set up. */
287 err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
289 /* Function to call when a listener has been connected. */
290 err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
292 /* Function which is called periodically. */
293 err_t (* poll)(void *arg, struct tcp_pcb *pcb);
295 /* Function to be called whenever a fatal error occurs. */
296 void (* errf)(void *arg, err_t err);
297 #endif /* LWIP_CALLBACK_API */
299 /* idle time before KEEPALIVE is sent */
302 /* KEEPALIVE counter */
306 struct tcp_pcb_listen {
307 /* Common members of all PCB types */
310 /* Protocol specific PCB members */
311 struct tcp_pcb_listen *next; /* for the linked list */
313 /* Even if state is obviously LISTEN this is here for
314 * field compatibility with tpc_pcb to which it is cast sometimes
315 * Until a cleaner solution emerges this is here.FIXME
317 enum tcp_state state; /* TCP state */
324 #if LWIP_CALLBACK_API
325 /* Function to call when a listener has been connected. */
326 err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
327 #endif /* LWIP_CALLBACK_API */
336 LWIP_EVENT_CONNECTED,
341 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
347 #define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
348 LWIP_EVENT_ACCEPT, NULL, 0, err)
349 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
350 LWIP_EVENT_SENT, NULL, space, ERR_OK)
351 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
352 LWIP_EVENT_RECV, (p), 0, (err))
353 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
354 LWIP_EVENT_CONNECTED, NULL, 0, (err))
355 #define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
356 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
357 #define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
358 LWIP_EVENT_ERR, NULL, 0, (err))
359 #else /* LWIP_EVENT_API */
360 #define TCP_EVENT_ACCEPT(pcb,err,ret) \
361 if((pcb)->accept != NULL) \
362 (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))
363 #define TCP_EVENT_SENT(pcb,space,ret) \
364 if((pcb)->sent != NULL) \
365 (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))
366 #define TCP_EVENT_RECV(pcb,p,err,ret) \
367 if((pcb)->recv != NULL) \
368 { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \
370 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
371 if((pcb)->connected != NULL) \
372 (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))
373 #define TCP_EVENT_POLL(pcb,ret) \
374 if((pcb)->poll != NULL) \
375 (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))
376 #define TCP_EVENT_ERR(errf,arg,err) \
379 #endif /* LWIP_EVENT_API */
381 /* This structure is used to repressent TCP segments when queued. */
383 struct tcp_seg *next; /* used when putting segements on a queue */
384 struct pbuf *p; /* buffer containing data + TCP header */
385 void *dataptr; /* pointer to the TCP data in the pbuf */
386 u16_t len; /* the TCP length of this segment */
387 struct tcp_hdr *tcphdr; /* the TCP header */
390 /* Internal functions and global variables: */
391 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
392 void tcp_pcb_purge(struct tcp_pcb *pcb);
393 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
395 u8_t tcp_segs_free(struct tcp_seg *seg);
396 u8_t tcp_seg_free(struct tcp_seg *seg);
397 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
399 #define tcp_ack(pcb) if((pcb)->flags & TF_ACK_DELAY) { \
400 (pcb)->flags &= ~TF_ACK_DELAY; \
401 (pcb)->flags |= TF_ACK_NOW; \
404 (pcb)->flags |= TF_ACK_DELAY; \
407 #define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
410 err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
411 err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
412 u8_t flags, u8_t copy,
413 u8_t *optdata, u8_t optlen);
415 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
417 void tcp_rst(u32_t seqno, u32_t ackno,
418 struct ip_addr *local_ip, struct ip_addr *remote_ip,
419 u16_t local_port, u16_t remote_port);
421 u32_t tcp_next_iss(void);
423 void tcp_keepalive(struct tcp_pcb *pcb);
425 extern struct tcp_pcb *tcp_input_pcb;
426 extern u32_t tcp_ticks;
428 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
429 void tcp_debug_print(struct tcp_hdr *tcphdr);
430 void tcp_debug_print_flags(u8_t flags);
431 void tcp_debug_print_state(enum tcp_state s);
432 void tcp_debug_print_pcbs(void);
433 int tcp_pcbs_sane(void);
435 # define tcp_debug_print(tcphdr)
436 # define tcp_debug_print_flags(flags)
437 # define tcp_debug_print_state(s)
438 # define tcp_debug_print_pcbs()
439 # define tcp_pcbs_sane() 1
440 #endif /* TCP_DEBUG */
443 #define tcp_timer_needed()
445 void tcp_timer_needed(void);
448 /* The TCP PCB lists. */
449 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
450 struct tcp_pcb_listen *listen_pcbs;
451 struct tcp_pcb *pcbs;
453 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
454 extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
455 state in which they accept or send
457 extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
459 extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
461 /* Axioms about the above lists:
462 1) Every TCP PCB that is not CLOSED is in one of the lists.
463 2) A PCB is only in one of the lists.
464 3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
465 4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
468 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
469 with a PCB list or removes a PCB from a list, respectively. */
471 #define TCP_REG(pcbs, npcb) do {\
472 LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
473 for(tcp_tmp_pcb = *pcbs; \
474 tcp_tmp_pcb != NULL; \
475 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
476 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
478 LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
479 npcb->next = *pcbs; \
480 LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
482 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
483 tcp_timer_needed(); \
485 #define TCP_RMV(pcbs, npcb) do { \
486 LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
487 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
488 if(*pcbs == npcb) { \
489 *pcbs = (*pcbs)->next; \
490 } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
491 if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
492 tcp_tmp_pcb->next = npcb->next; \
497 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
498 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
501 #else /* LWIP_DEBUG */
502 #define TCP_REG(pcbs, npcb) do { \
503 npcb->next = *pcbs; \
505 tcp_timer_needed(); \
507 #define TCP_RMV(pcbs, npcb) do { \
508 if(*(pcbs) == npcb) { \
509 (*(pcbs)) = (*pcbs)->next; \
510 } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
511 if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
512 tcp_tmp_pcb->next = npcb->next; \
518 #endif /* LWIP_DEBUG */
519 #endif /* __LWIP_TCP_H__ */