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