socket.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. extern int __attribute__((weak)) espconn_mbedtls_parse_internal(int socket, sint8 error);
  58. extern int __attribute__((weak)) espconn_mbedtls_parse_thread(int socket, int event, int error);
  59. #if (!defined(lwIP_unlikely))
  60. #define lwIP_unlikely(Expression) !!(Expression)
  61. #endif
  62. #define lwIP_ASSERT(Expression) do{if (!(Expression)) ESP_LOG("%d\n", __LINE__);}while(0)
  63. #define lwIP_REQUIRE_ACTION(Expression,Label,Action) \
  64. do{\
  65. if (lwIP_unlikely(!(Expression))) \
  66. {\
  67. ESP_LOG("%d\n", __LINE__);\
  68. {Action;}\
  69. goto Label;\
  70. }\
  71. }while(0)
  72. #define lwIP_REQUIRE_NOERROR(Expression,Label) \
  73. do{\
  74. int LocalError;\
  75. LocalError = (int)Expression;\
  76. if (lwIP_unlikely(LocalError != 0)) \
  77. {\
  78. ESP_LOG("%d 0x%x\n", __LINE__, LocalError);\
  79. goto Label;\
  80. }\
  81. }while(0)
  82. #define lwIP_REQUIRE_NOERROR_ACTION(Expression,Label,Action) \
  83. do{\
  84. int LocalError;\
  85. LocalError = (int)Expression;\
  86. if (lwIP_unlikely(LocalError != 0)) \
  87. {\
  88. ESP_LOG("%d\n", __LINE__);\
  89. {Action;}\
  90. goto Label;\
  91. }\
  92. }while(0)
  93. typedef enum{
  94. NETCONN_EVENT_NONE = 0,
  95. NETCONN_EVENT_ESTABLISHED = 1,
  96. NETCONN_EVENT_RECV = 2,
  97. NETCONN_EVENT_SEND = 3,
  98. NETCONN_EVENT_ERROR = 4,
  99. NETCONN_EVENT_CLOSE = 5,
  100. NETCONN_EVENT_SUMNUM = 6
  101. }netconn_event;
  102. typedef enum _netconn_type {
  103. NETCONN_INVALID = 0,
  104. /* ESPCONN_TCP Group */
  105. NETCONN_TCP = 0x10,
  106. /* ESPCONN_UDP Group */
  107. NETCONN_UDP = 0x20,
  108. } netconn_type;
  109. /* members are in network byte order */
  110. struct sockaddr_in {
  111. u8_t sin_len;
  112. u8_t sin_family;
  113. u16_t sin_port;
  114. struct in_addr sin_addr;
  115. char sin_zero[8];
  116. };
  117. /** A netconn descriptor */
  118. typedef struct _lwIP_netconn{
  119. /** flags blocking or nonblocking defines */
  120. uint8 flags;
  121. /** type of the lwIP_netconn (TCP, UDP) */
  122. netconn_type type;
  123. /** current state of the lwIP_netconn */
  124. netconn_state state;
  125. /** the lwIP internal protocol control block */
  126. struct tcp_pcb *tcp;
  127. /**the new socket is unknown and sync*/
  128. void *acceptmbox;
  129. /**the lwIP internal buffer control block*/
  130. ringbuf *readbuf;
  131. /** only used for socket layer */
  132. int socket;
  133. }lwIP_netconn, *lwIPNetconn;
  134. /** Contains all internal pointers and states used for a socket */
  135. typedef struct _lwIP_sock{
  136. /** sockets currently are built on lwIP_netconn, each socket has one lwIP_netconn */
  137. lwIP_netconn *conn;
  138. /** data that was left from the previous read */
  139. u32_t recv_index;
  140. u32_t send_index;
  141. u32_t recv_data_len;
  142. void *send_buffer;
  143. }lwIP_sock;
  144. typedef uint8 sa_family_t;
  145. struct sockaddr {
  146. uint8 sa_len;
  147. sa_family_t sa_family;
  148. char sa_data[14];
  149. };
  150. typedef uint32 socklen_t;
  151. #define NETCONNTYPE_GROUP(t) ((t)&0xF0)
  152. #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
  153. #define AF_UNSPEC 0
  154. #define AF_INET 2
  155. /* Socket protocol types (TCP/UDP/RAW) */
  156. #define SOCK_STREAM 1
  157. #define SOCK_DGRAM 2
  158. #define SOCK_RAW 3
  159. #define lwIPThreadPrio 25
  160. #define lwIPThreadQueueLen 15
  161. /*
  162. * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
  163. */
  164. #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
  165. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  166. #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
  167. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  168. #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
  169. #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  170. #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
  171. #define SO_LINGER 0x0080 /* linger on close if data present */
  172. #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
  173. #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
  174. /*
  175. * Additional options, not kept in so_options.
  176. */
  177. #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
  178. #define SO_RCVBUF 0x1002 /* receive buffer size */
  179. #define SO_TYPE 0x1008 /* get socket type */
  180. #define IPPROTO_IP 0
  181. #define IPPROTO_TCP 6
  182. #define IPPROTO_UDP 17
  183. #define SOL_SOCKET 0xfff /* options for socket level */
  184. #if LWIP_TCP
  185. /*
  186. * Options for level IPPROTO_TCP
  187. */
  188. #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
  189. #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
  190. #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
  191. #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
  192. #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
  193. #endif /* LWIP_TCP */
  194. /* commands for fnctl */
  195. #ifndef F_GETFL
  196. #define F_GETFL 3
  197. #endif
  198. #ifndef F_SETFL
  199. #define F_SETFL 4
  200. #endif
  201. /* File status flags and file access modes for fnctl,
  202. these are bits in an int. */
  203. #ifndef O_NONBLOCK
  204. #define O_NONBLOCK 1 /* nonblocking I/O */
  205. #endif
  206. #ifndef O_NDELAY
  207. #define O_NDELAY 1 /* same as O_NONBLOCK, for compatibility */
  208. #endif
  209. struct timeval {
  210. long tv_sec; /* seconds */
  211. long tv_usec; /* and microseconds */
  212. };
  213. /* Flags for struct netconn.flags (u8_t) */
  214. /** TCP: when data passed to netconn_write doesn't fit into the send buffer,
  215. this temporarily stores whether to wake up the original application task
  216. if data couldn't be sent in the first try. */
  217. #define NETCONN_FLAG_WRITE_DELAYED 0x01
  218. /** Should this netconn avoid blocking? */
  219. #define NETCONN_FLAG_NON_BLOCKING 0x02
  220. /** Was the last connect action a non-blocking one? */
  221. #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
  222. /** If this is set, a TCP netconn must call netconn_recved() to update
  223. the TCP receive window (done automatically if not set). */
  224. #define NETCONN_FLAG_NO_AUTO_RECVED 0x08
  225. /** If a nonblocking write has been rejected before, poll_tcp needs to
  226. check if the netconn is writable again */
  227. #define NETCONN_FLAG_CHECK_WRITESPACE 0x10
  228. /** Set the blocking status of netconn calls (@todo: write/send is missing) */
  229. #define netconn_set_nonblocking(conn, val) do { if(val) { \
  230. (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
  231. } else { \
  232. (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
  233. /** Get the blocking status of netconn calls (@todo: write/send is missing) */
  234. #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
  235. /* FD_SET used for lwip_select */
  236. #ifndef FD_SET
  237. #undef FD_SETSIZE
  238. /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
  239. #define FD_SETSIZE NUM_SOCKETS
  240. #define FD_SET(n, p) ((p)->fd_bits[(n)/8] |= (1 << ((n) & 7)))
  241. #define FD_CLR(n, p) ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
  242. #define FD_ISSET(n,p) ((p)->fd_bits[(n)/8] & (1 << ((n) & 7)))
  243. #define FD_ZERO(p) os_memset((void*)(p),0,sizeof(*(p)))
  244. typedef struct fd_set {
  245. unsigned char fd_bits[(FD_SETSIZE + 7) / 8];
  246. } fd_set;
  247. #endif /* FD_SET */
  248. void lwip_socket_init(void);
  249. int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  250. int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
  251. int lwip_shutdown(int s, int how);
  252. int lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen);
  253. int lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen);
  254. int lwip_getsockopt(int s, int level, int optname, void *optval,socklen_t *optlen);
  255. int lwip_setsockopt(int s, int level, int optname, const void *optval,socklen_t optlen);
  256. int lwip_close(int s);
  257. int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
  258. int lwip_listen(int s, int backlog);
  259. int lwip_recv(int s, void *mem, size_t len, int flags);
  260. int lwip_read(int s, void *mem, size_t len);
  261. int lwip_recvfrom(int s, void *mem, size_t len, int flags,struct sockaddr *from, socklen_t *fromlen);
  262. int lwip_send(int s, const void *dataptr, size_t size, int flags);
  263. int lwip_sendto(int s, const void *dataptr, size_t size, int flags,const struct sockaddr *to, socklen_t tolen);
  264. int lwip_socket(int domain, int type, int protocol);
  265. int lwip_write(int s, const void *dataptr, size_t size);
  266. int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset,fd_set *exceptset, struct timeval *timeout);
  267. int lwip_ioctl(int s, long cmd, void *argp);
  268. int lwip_fcntl(int s, int cmd, int val);
  269. uint32_t lwip_getul(char *str);
  270. #define accept(a,b,c) lwip_accept(a,b,c)
  271. #define bind(a,b,c) lwip_bind(a,b,c)
  272. #define shutdown(a,b) lwip_shutdown(a,b)
  273. #define closesocket(s) lwip_close(s)
  274. #define connect(a,b,c) lwip_connect(a,b,c)
  275. #define getsockname(a,b,c) lwip_getsockname(a,b,c)
  276. #define getpeername(a,b,c) lwip_getpeername(a,b,c)
  277. #define setsockopt(a,b,c,d,e) lwip_setsockopt(a,b,c,d,e)
  278. #define getsockopt(a,b,c,d,e) lwip_getsockopt(a,b,c,d,e)
  279. #define listen(a,b) lwip_listen(a,b)
  280. #define recv(a,b,c,d) lwip_recv(a,b,c,d)
  281. #define recvfrom(a,b,c,d,e,f) lwip_recvfrom(a,b,c,d,e,f)
  282. #define send(a,b,c,d) lwip_send(a,b,c,d)
  283. #define sendto(a,b,c,d,e,f) lwip_sendto(a,b,c,d,e,f)
  284. #define socket(a,b,c) lwip_socket(a,b,c)
  285. #define select(a,b,c,d,e) lwip_select(a,b,c,d,e)
  286. #define ioctlsocket(a,b,c) lwip_ioctl(a,b,c)
  287. #define read(a,b,c) lwip_read(a,b,c)
  288. #define write(a,b,c) lwip_write(a,b,c)
  289. #define close(s) lwip_close(s)
  290. #define getul(s) lwip_getul(s)
  291. extern int system_overclock(void);
  292. extern int system_restoreclock(void);
  293. extern char *sys_itoa(int n);
  294. #endif /* ESPCONN_SOCKT_H_ */