socket.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * ESPRSSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef ESP_SOCKET_H_
  25. #define ESP_SOCKET_H_
  26. #include "lwip/inet.h"
  27. #include "lwip/opt.h"
  28. #include "sys/ringbuf.h"
  29. #if 0
  30. #define ESP_LOG os_printf
  31. #else
  32. #define ESP_LOG(...)
  33. #endif
  34. #define NUM_SOCKETS 3
  35. #define ESP_OK 0 /* No error, everything OK. */
  36. #define ESP_MEM -1 /* Out of memory error. */
  37. #define ESP_TIMEOUT -3 /* Timeout. */
  38. #define ESP_RTE -4 /* Routing problem. */
  39. #define ESP_INPROGRESS -5 /* Operation in progress */
  40. #define ESP_MAXNUM -7 /* Total number exceeds the set maximum*/
  41. #define ESP_ABRT -8 /* Connection aborted. */
  42. #define ESP_RST -9 /* Connection reset. */
  43. #define ESP_CLSD -10 /* Connection closed. */
  44. #define ESP_CONN -11 /* Not connected. */
  45. #define ESP_ARG -12 /* Illegal argument. */
  46. #define ESP_IF -14 /* Low_level error */
  47. #define ESP_ISCONN -15 /* Already connected. */
  48. typedef enum{
  49. NETCONN_STATE_NONE = 0,
  50. NETCONN_STATE_ESTABLISHED,
  51. NETCONN_STATE_WRITE,
  52. NETCONN_STATE_READ,
  53. NETCONN_STATE_CLOSED,
  54. NETCONN_STATE_ERROR,
  55. NETCONN_STATE_SUMNUM
  56. }netconn_state;
  57. #if (!defined(lwIP_unlikely))
  58. #define lwIP_unlikely(Expression) !!(Expression)
  59. #endif
  60. #define lwIP_ASSERT(Expression) do{if (!(Expression)) ESP_LOG("%d\n", __LINE__);}while(0)
  61. #define lwIP_REQUIRE_ACTION(Expression,Label,Action) \
  62. do{\
  63. if (lwIP_unlikely(!(Expression))) \
  64. {\
  65. ESP_LOG("%d\n", __LINE__);\
  66. {Action;}\
  67. goto Label;\
  68. }\
  69. }while(0)
  70. #define lwIP_REQUIRE_NOERROR(Expression,Label) \
  71. do{\
  72. int LocalError;\
  73. LocalError = (int)Expression;\
  74. if (lwIP_unlikely(LocalError != 0)) \
  75. {\
  76. ESP_LOG("%d 0x%x\n", __LINE__, LocalError);\
  77. goto Label;\
  78. }\
  79. }while(0)
  80. #define lwIP_REQUIRE_NOERROR_ACTION(Expression,Label,Action) \
  81. do{\
  82. int LocalError;\
  83. LocalError = (int)Expression;\
  84. if (lwIP_unlikely(LocalError != 0)) \
  85. {\
  86. ESP_LOG("%d\n", __LINE__);\
  87. {Action;}\
  88. goto Label;\
  89. }\
  90. }while(0)
  91. #define lwIP_EVENT_PARSE(s, error) \
  92. do { \
  93. mbedtls_parse_internal(s, error); \
  94. } while (0)
  95. #define lwIP_EVENT_THREAD(s, event, error) \
  96. do { \
  97. mbedtls_parse_thread(s, event, error); \
  98. }while(0)
  99. typedef enum{
  100. ENTCONN_EVENT_NONE = 0,
  101. NETCONN_EVENT_ESTABLISHED = 1,
  102. ENTCONN_EVENT_RECV = 2,
  103. NETCONN_EVENT_SEND = 3,
  104. NETCONN_EVENT_ERROR = 4,
  105. NETCONN_EVENT_CLOSE = 5,
  106. NETCONN_EVENT_SUMNUM = 6
  107. }netconn_event;
  108. typedef enum _netconn_type {
  109. NETCONN_INVALID = 0,
  110. /* ESPCONN_TCP Group */
  111. NETCONN_TCP = 0x10,
  112. /* ESPCONN_UDP Group */
  113. NETCONN_UDP = 0x20,
  114. } netconn_type;
  115. /* members are in network byte order */
  116. struct sockaddr_in {
  117. u8_t sin_len;
  118. u8_t sin_family;
  119. u16_t sin_port;
  120. struct in_addr sin_addr;
  121. char sin_zero[8];
  122. };
  123. /** A netconn descriptor */
  124. typedef struct _lwIP_netconn{
  125. /** flags blocking or nonblocking defines */
  126. uint8 flags;
  127. /** type of the lwIP_netconn (TCP, UDP) */
  128. netconn_type type;
  129. /** current state of the lwIP_netconn */
  130. netconn_state state;
  131. /** the lwIP internal protocol control block */
  132. struct tcp_pcb *tcp;
  133. /**the new socket is unknown and sync*/
  134. void *acceptmbox;
  135. /**the lwIP internal buffer control block*/
  136. ringbuf *readbuf;
  137. /** only used for socket layer */
  138. int socket;
  139. }lwIP_netconn, *lwIPNetconn;
  140. /** Contains all internal pointers and states used for a socket */
  141. typedef struct _lwIP_sock{
  142. /** sockets currently are built on lwIP_netconn, each socket has one lwIP_netconn */
  143. lwIP_netconn *conn;
  144. /** data that was left from the previous read */
  145. u32_t recv_index;
  146. u32_t send_index;
  147. u32_t recv_data_len;
  148. void *send_buffer;
  149. }lwIP_sock;
  150. typedef uint8 sa_family_t;
  151. struct sockaddr {
  152. uint8 sa_len;
  153. sa_family_t sa_family;
  154. char sa_data[14];
  155. };
  156. typedef uint32 socklen_t;
  157. #define NETCONNTYPE_GROUP(t) ((t)&0xF0)
  158. #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
  159. #define AF_UNSPEC 0
  160. #define AF_INET 2
  161. /* Socket protocol types (TCP/UDP/RAW) */
  162. #define SOCK_STREAM 1
  163. #define SOCK_DGRAM 2
  164. #define SOCK_RAW 3
  165. #define lwIPThreadPrio 25
  166. #define lwIPThreadQueueLen 15
  167. /*
  168. * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
  169. */
  170. #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
  171. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  172. #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
  173. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  174. #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
  175. #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  176. #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
  177. #define SO_LINGER 0x0080 /* linger on close if data present */
  178. #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
  179. #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
  180. /*
  181. * Additional options, not kept in so_options.
  182. */
  183. #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
  184. #define SO_RCVBUF 0x1002 /* receive buffer size */
  185. #define SO_TYPE 0x1008 /* get socket type */
  186. #define IPPROTO_IP 0
  187. #define IPPROTO_TCP 6
  188. #define IPPROTO_UDP 17
  189. #define SOL_SOCKET 0xfff /* options for socket level */
  190. #if LWIP_TCP
  191. /*
  192. * Options for level IPPROTO_TCP
  193. */
  194. #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
  195. #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
  196. #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
  197. #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
  198. #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
  199. #endif /* LWIP_TCP */
  200. /* commands for fnctl */
  201. #ifndef F_GETFL
  202. #define F_GETFL 3
  203. #endif
  204. #ifndef F_SETFL
  205. #define F_SETFL 4
  206. #endif
  207. /* File status flags and file access modes for fnctl,
  208. these are bits in an int. */
  209. #ifndef O_NONBLOCK
  210. #define O_NONBLOCK 1 /* nonblocking I/O */
  211. #endif
  212. #ifndef O_NDELAY
  213. #define O_NDELAY 1 /* same as O_NONBLOCK, for compatibility */
  214. #endif
  215. struct timeval {
  216. long tv_sec; /* seconds */
  217. long tv_usec; /* and microseconds */
  218. };
  219. /* Flags for struct netconn.flags (u8_t) */
  220. /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
  221. this temporarily stores whether to wake up the original application task
  222. if data couldn't be sent in the first try. */
  223. #define NETCONN_FLAG_WRITE_DELAYED 0x01
  224. /** Should this netconn avoid blocking? */
  225. #define NETCONN_FLAG_NON_BLOCKING 0x02
  226. /** Was the last connect action a non-blocking one? */
  227. #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
  228. /** If this is set, a TCP netconn must call netconn_recved() to update
  229. the TCP receive window (done automatically if not set). */
  230. #define NETCONN_FLAG_NO_AUTO_RECVED 0x08
  231. /** If a nonblocking write has been rejected before, poll_tcp needs to
  232. check if the netconn is writable again */
  233. #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
  234. /** Set the blocking status of netconn calls (@todo: write/send is missing) */
  235. #define netconn_set_nonblocking(conn, val) do { if(val) { \
  236. (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
  237. } else { \
  238. (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
  239. /** Get the blocking status of netconn calls (@todo: write/send is missing) */
  240. #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
  241. /* FD_SET used for lwip_select */
  242. #ifndef FD_SET
  243. #undef FD_SETSIZE
  244. /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
  245. #define FD_SETSIZE NUM_SOCKETS
  246. #define FD_SET(n, p) ((p)->fd_bits[(n)/8] |= (1 << ((n) & 7)))
  247. #define FD_CLR(n, p) ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
  248. #define FD_ISSET(n,p) ((p)->fd_bits[(n)/8] & (1 << ((n) & 7)))
  249. #define FD_ZERO(p) os_memset((void*)(p),0,sizeof(*(p)))
  250. typedef struct fd_set {
  251. unsigned char fd_bits[(FD_SETSIZE + 7) / 8];
  252. } fd_set;
  253. #endif /* FD_SET */
  254. void lwip_socket_init(void);
  255. int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  256. int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
  257. int lwip_shutdown(int s, int how);
  258. int lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen);
  259. int lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen);
  260. int lwip_getsockopt(int s, int level, int optname, void *optval,socklen_t *optlen);
  261. int lwip_setsockopt(int s, int level, int optname, const void *optval,socklen_t optlen);
  262. int lwip_close(int s);
  263. int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
  264. int lwip_listen(int s, int backlog);
  265. int lwip_recv(int s, void *mem, size_t len, int flags);
  266. int lwip_read(int s, void *mem, size_t len);
  267. int lwip_recvfrom(int s, void *mem, size_t len, int flags,struct sockaddr *from, socklen_t *fromlen);
  268. int lwip_send(int s, const void *dataptr, size_t size, int flags);
  269. int lwip_sendto(int s, const void *dataptr, size_t size, int flags,const struct sockaddr *to, socklen_t tolen);
  270. int lwip_socket(int domain, int type, int protocol);
  271. int lwip_write(int s, const void *dataptr, size_t size);
  272. int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset,fd_set *exceptset, struct timeval *timeout);
  273. int lwip_ioctl(int s, long cmd, void *argp);
  274. int lwip_fcntl(int s, int cmd, int val);
  275. uint32_t lwip_getul(char *str);
  276. #define accept(a,b,c) lwip_accept(a,b,c)
  277. #define bind(a,b,c) lwip_bind(a,b,c)
  278. #define shutdown(a,b) lwip_shutdown(a,b)
  279. #define closesocket(s) lwip_close(s)
  280. #define connect(a,b,c) lwip_connect(a,b,c)
  281. #define getsockname(a,b,c) lwip_getsockname(a,b,c)
  282. #define getpeername(a,b,c) lwip_getpeername(a,b,c)
  283. #define setsockopt(a,b,c,d,e) lwip_setsockopt(a,b,c,d,e)
  284. #define getsockopt(a,b,c,d,e) lwip_getsockopt(a,b,c,d,e)
  285. #define listen(a,b) lwip_listen(a,b)
  286. #define recv(a,b,c,d) lwip_recv(a,b,c,d)
  287. #define recvfrom(a,b,c,d,e,f) lwip_recvfrom(a,b,c,d,e,f)
  288. #define send(a,b,c,d) lwip_send(a,b,c,d)
  289. #define sendto(a,b,c,d,e,f) lwip_sendto(a,b,c,d,e,f)
  290. #define socket(a,b,c) lwip_socket(a,b,c)
  291. #define select(a,b,c,d,e) lwip_select(a,b,c,d,e)
  292. #define ioctlsocket(a,b,c) lwip_ioctl(a,b,c)
  293. #define read(a,b,c) lwip_read(a,b,c)
  294. #define write(a,b,c) lwip_write(a,b,c)
  295. #define close(s) lwip_close(s)
  296. #define getul(s) lwip_getul(s)
  297. #endif /* ESPCONN_SOCKT_H_ */