api.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_API_H__
  33. #define __LWIP_API_H__
  34. #include "lwip/opt.h"
  35. #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
  36. #include <stddef.h> /* for size_t */
  37. #include "lwip/netbuf.h"
  38. #include "lwip/sys.h"
  39. #include "lwip/ip_addr.h"
  40. #include "lwip/err.h"
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /* Throughout this file, IP addresses and port numbers are expected to be in
  45. * the same byte order as in the corresponding pcb.
  46. */
  47. /* Flags for netconn_write (u8_t) */
  48. #define NETCONN_NOFLAG 0x00
  49. #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
  50. #define NETCONN_COPY 0x01
  51. #define NETCONN_MORE 0x02
  52. #define NETCONN_DONTBLOCK 0x04
  53. /* Flags for struct netconn.flags (u8_t) */
  54. /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
  55. this temporarily stores whether to wake up the original application task
  56. if data couldn't be sent in the first try. */
  57. #define NETCONN_FLAG_WRITE_DELAYED 0x01
  58. /** Should this netconn avoid blocking? */
  59. #define NETCONN_FLAG_NON_BLOCKING 0x02
  60. /** Was the last connect action a non-blocking one? */
  61. #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
  62. /** If this is set, a TCP netconn must call netconn_recved() to update
  63. the TCP receive window (done automatically if not set). */
  64. #define NETCONN_FLAG_NO_AUTO_RECVED 0x08
  65. /** If a nonblocking write has been rejected before, poll_tcp needs to
  66. check if the netconn is writable again */
  67. #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
  68. /* Helpers to process several netconn_types by the same code */
  69. #define NETCONNTYPE_GROUP(t) (t&0xF0)
  70. #define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
  71. /** Protocol family and type of the netconn */
  72. enum netconn_type {
  73. NETCONN_INVALID = 0,
  74. /* NETCONN_TCP Group */
  75. NETCONN_TCP = 0x10,
  76. /* NETCONN_UDP Group */
  77. NETCONN_UDP = 0x20,
  78. NETCONN_UDPLITE = 0x21,
  79. NETCONN_UDPNOCHKSUM= 0x22,
  80. /* NETCONN_RAW Group */
  81. NETCONN_RAW = 0x40
  82. };
  83. /** Current state of the netconn. Non-TCP netconns are always
  84. * in state NETCONN_NONE! */
  85. enum netconn_state {
  86. NETCONN_NONE,
  87. NETCONN_WRITE,
  88. NETCONN_LISTEN,
  89. NETCONN_CONNECT,
  90. NETCONN_CLOSE
  91. };
  92. /** Use to inform the callback function about changes */
  93. enum netconn_evt {
  94. NETCONN_EVT_RCVPLUS,
  95. NETCONN_EVT_RCVMINUS,
  96. NETCONN_EVT_SENDPLUS,
  97. NETCONN_EVT_SENDMINUS,
  98. NETCONN_EVT_ERROR
  99. };
  100. #if LWIP_IGMP
  101. /** Used for netconn_join_leave_group() */
  102. enum netconn_igmp {
  103. NETCONN_JOIN,
  104. NETCONN_LEAVE
  105. };
  106. #endif /* LWIP_IGMP */
  107. /* forward-declare some structs to avoid to include their headers */
  108. struct ip_pcb;
  109. struct tcp_pcb;
  110. struct udp_pcb;
  111. struct raw_pcb;
  112. struct netconn;
  113. struct api_msg_msg;
  114. /** A callback prototype to inform about events for a netconn */
  115. typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
  116. /** A netconn descriptor */
  117. struct netconn {
  118. /** type of the netconn (TCP, UDP or RAW) */
  119. enum netconn_type type;
  120. /** current state of the netconn */
  121. enum netconn_state state;
  122. /** the lwIP internal protocol control block */
  123. union {
  124. struct ip_pcb *ip;
  125. struct tcp_pcb *tcp;
  126. struct udp_pcb *udp;
  127. struct raw_pcb *raw;
  128. } pcb;
  129. /** the last error this netconn had */
  130. err_t last_err;
  131. /** sem that is used to synchroneously execute functions in the core context */
  132. sys_sem_t op_completed;
  133. /** mbox where received packets are stored until they are fetched
  134. by the netconn application thread (can grow quite big) */
  135. sys_mbox_t recvmbox;
  136. #if LWIP_TCP
  137. /** mbox where new connections are stored until processed
  138. by the application thread */
  139. sys_mbox_t acceptmbox;
  140. #endif /* LWIP_TCP */
  141. /** only used for socket layer */
  142. #if LWIP_SOCKET
  143. int socket;
  144. #endif /* LWIP_SOCKET */
  145. #if LWIP_SO_RCVTIMEO
  146. /** timeout to wait for new data to be received
  147. (or connections to arrive for listening netconns) */
  148. int recv_timeout;
  149. #endif /* LWIP_SO_RCVTIMEO */
  150. #if LWIP_SO_RCVBUF
  151. /** maximum amount of bytes queued in recvmbox
  152. not used for TCP: adjust TCP_WND instead! */
  153. int recv_bufsize;
  154. /** number of bytes currently in recvmbox to be received,
  155. tested against recv_bufsize to limit bytes on recvmbox
  156. for UDP and RAW, used for FIONREAD */
  157. s16_t recv_avail;
  158. #endif /* LWIP_SO_RCVBUF */
  159. /** flags holding more netconn-internal state, see NETCONN_FLAG_* defines */
  160. u8_t flags;
  161. #if LWIP_TCP
  162. /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
  163. this temporarily stores how much is already sent. */
  164. size_t write_offset;
  165. /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
  166. this temporarily stores the message.
  167. Also used during connect and close. */
  168. struct api_msg_msg *current_msg;
  169. #endif /* LWIP_TCP */
  170. /** A callback function that is informed about events for this netconn */
  171. netconn_callback callback;
  172. };
  173. /** Register an Network connection event */
  174. #define API_EVENT(c,e,l) if (c->callback) { \
  175. (*c->callback)(c, e, l); \
  176. }
  177. /** Set conn->last_err to err but don't overwrite fatal errors */
  178. #define NETCONN_SET_SAFE_ERR(conn, err) do { \
  179. SYS_ARCH_DECL_PROTECT(lev); \
  180. SYS_ARCH_PROTECT(lev); \
  181. if (!ERR_IS_FATAL((conn)->last_err)) { \
  182. (conn)->last_err = err; \
  183. } \
  184. SYS_ARCH_UNPROTECT(lev); \
  185. } while(0);
  186. /* Network connection functions: */
  187. #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
  188. #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
  189. struct
  190. netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
  191. netconn_callback callback);
  192. err_t netconn_delete(struct netconn *conn);
  193. /** Get the type of a netconn (as enum netconn_type). */
  194. #define netconn_type(conn) (conn->type)
  195. err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
  196. u16_t *port, u8_t local);
  197. #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
  198. #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
  199. err_t netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port);
  200. err_t netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port);
  201. err_t netconn_disconnect (struct netconn *conn);
  202. err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
  203. #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
  204. err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
  205. err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
  206. err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
  207. void netconn_recved(struct netconn *conn, u32_t length);
  208. err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
  209. ip_addr_t *addr, u16_t port);
  210. err_t netconn_send(struct netconn *conn, struct netbuf *buf);
  211. err_t netconn_write(struct netconn *conn, const void *dataptr, size_t size,
  212. u8_t apiflags);
  213. err_t netconn_close(struct netconn *conn);
  214. err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
  215. #if LWIP_IGMP
  216. err_t netconn_join_leave_group(struct netconn *conn, ip_addr_t *multiaddr,
  217. ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
  218. #endif /* LWIP_IGMP */
  219. #if LWIP_DNS
  220. err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
  221. #endif /* LWIP_DNS */
  222. #define netconn_err(conn) ((conn)->last_err)
  223. #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
  224. /** Set the blocking status of netconn calls (@todo: write/send is missing) */
  225. #define netconn_set_nonblocking(conn, val) do { if(val) { \
  226. (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
  227. } else { \
  228. (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
  229. /** Get the blocking status of netconn calls (@todo: write/send is missing) */
  230. #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
  231. /** TCP: Set the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) */
  232. #define netconn_set_noautorecved(conn, val) do { if(val) { \
  233. (conn)->flags |= NETCONN_FLAG_NO_AUTO_RECVED; \
  234. } else { \
  235. (conn)->flags &= ~ NETCONN_FLAG_NO_AUTO_RECVED; }} while(0)
  236. /** TCP: Get the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) */
  237. #define netconn_get_noautorecved(conn) (((conn)->flags & NETCONN_FLAG_NO_AUTO_RECVED) != 0)
  238. #if LWIP_SO_RCVTIMEO
  239. /** Set the receive timeout in milliseconds */
  240. #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
  241. /** Get the receive timeout in milliseconds */
  242. #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
  243. #endif /* LWIP_SO_RCVTIMEO */
  244. #if LWIP_SO_RCVBUF
  245. /** Set the receive buffer in bytes */
  246. #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
  247. /** Get the receive buffer in bytes */
  248. #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
  249. #endif /* LWIP_SO_RCVBUF*/
  250. #ifdef __cplusplus
  251. }
  252. #endif
  253. #endif /* LWIP_NETCONN */
  254. #endif /* __LWIP_API_H__ */