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