tcp.h 14 KB

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