af_inet_lwip.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-17 ChenYong First version
  9. */
  10. #include <rtthread.h>
  11. #include <lwip/sockets.h>
  12. #include <lwip/netdb.h>
  13. #include <lwip/api.h>
  14. #include <lwip/init.h>
  15. #include <lwip/netif.h>
  16. #ifdef SAL_USING_POSIX
  17. #include <poll.h>
  18. #endif
  19. #include <sal_low_lvl.h>
  20. #include <af_inet.h>
  21. #include <netdev.h>
  22. #if (LWIP_VERSION < 0x2000000) && NETDEV_IPV6
  23. #error "The lwIP version is not support IPV6, please disable netdev IPV6 configuration "
  24. #elif (LWIP_VERSION > 0x2000000) && (NETDEV_IPV6 != LWIP_IPV6)
  25. #error "IPV6 configuration error, Please check and synchronize netdev and lwip IPV6 configuration."
  26. #endif
  27. #if LWIP_VERSION < 0x2000000
  28. #define SELWAIT_T int
  29. #else
  30. #ifndef SELWAIT_T
  31. #define SELWAIT_T u8_t
  32. #endif
  33. #endif
  34. #ifdef SAL_USING_LWIP
  35. #ifdef SAL_USING_POSIX
  36. #if LWIP_VERSION >= 0x20100ff
  37. #include <lwip/priv/sockets_priv.h>
  38. #else /* LWIP_VERSION < 0x20100ff */
  39. /*
  40. * Re-define lwip socket
  41. *
  42. * NOTE: please make sure the definitions same in lwip::net_socket.c
  43. */
  44. struct lwip_sock {
  45. /** sockets currently are built on netconns, each socket has one netconn */
  46. struct netconn *conn;
  47. /** data that was left from the previous read */
  48. void *lastdata;
  49. /** offset in the data that was left from the previous read */
  50. u16_t lastoffset;
  51. /** number of times data was received, set by event_callback(),
  52. tested by the receive and select functions */
  53. s16_t rcvevent;
  54. /** number of times data was ACKed (free send buffer), set by event_callback(),
  55. tested by select */
  56. u16_t sendevent;
  57. /** error happened for this socket, set by event_callback(), tested by select */
  58. u16_t errevent;
  59. /** last error that occurred on this socket */
  60. #if LWIP_VERSION < 0x2000000
  61. int err;
  62. #else
  63. u8_t err;
  64. #endif
  65. /** counter of how many threads are waiting for this socket using select */
  66. SELWAIT_T select_waiting;
  67. rt_wqueue_t wait_head;
  68. };
  69. #endif /* LWIP_VERSION >= 0x20100ff */
  70. static RT_DEFINE_SPINLOCK(_spinlock);
  71. extern struct lwip_sock *lwip_tryget_socket(int s);
  72. static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
  73. {
  74. int s;
  75. struct lwip_sock *sock;
  76. uint32_t event = 0;
  77. SYS_ARCH_DECL_PROTECT(lev);
  78. LWIP_UNUSED_ARG(len);
  79. /* Get socket */
  80. if (conn)
  81. {
  82. s = conn->socket;
  83. if (s < 0)
  84. {
  85. /* Data comes in right away after an accept, even though
  86. * the server task might not have created a new socket yet.
  87. * Just count down (or up) if that's the case and we
  88. * will use the data later. Note that only receive events
  89. * can happen before the new socket is set up. */
  90. SYS_ARCH_PROTECT(lev);
  91. if (conn->socket < 0)
  92. {
  93. if (evt == NETCONN_EVT_RCVPLUS)
  94. {
  95. conn->socket--;
  96. }
  97. SYS_ARCH_UNPROTECT(lev);
  98. return;
  99. }
  100. s = conn->socket;
  101. SYS_ARCH_UNPROTECT(lev);
  102. }
  103. sock = lwip_tryget_socket(s);
  104. if (!sock)
  105. {
  106. return;
  107. }
  108. }
  109. else
  110. {
  111. return;
  112. }
  113. SYS_ARCH_PROTECT(lev);
  114. /* Set event as required */
  115. switch (evt)
  116. {
  117. case NETCONN_EVT_RCVPLUS:
  118. sock->rcvevent++;
  119. break;
  120. case NETCONN_EVT_RCVMINUS:
  121. sock->rcvevent--;
  122. break;
  123. case NETCONN_EVT_SENDPLUS:
  124. sock->sendevent = 1;
  125. break;
  126. case NETCONN_EVT_SENDMINUS:
  127. sock->sendevent = 0;
  128. break;
  129. case NETCONN_EVT_ERROR:
  130. sock->errevent = 1;
  131. break;
  132. default:
  133. LWIP_ASSERT("unknown event", 0);
  134. break;
  135. }
  136. #if LWIP_VERSION >= 0x20100ff
  137. if ((void*)(sock->lastdata.pbuf) || (sock->rcvevent > 0))
  138. #else
  139. if ((void*)(sock->lastdata) || (sock->rcvevent > 0))
  140. #endif
  141. event |= POLLIN;
  142. if (sock->sendevent)
  143. event |= POLLOUT;
  144. if (sock->errevent)
  145. event |= POLLERR;
  146. SYS_ARCH_UNPROTECT(lev);
  147. if (event)
  148. {
  149. rt_wqueue_wakeup(&sock->wait_head, (void*)(size_t)event);
  150. }
  151. }
  152. #endif /* SAL_USING_POSIX */
  153. static int inet_socket(int domain, int type, int protocol)
  154. {
  155. #ifdef SAL_USING_POSIX
  156. int socket;
  157. socket = lwip_socket(domain, type, protocol);
  158. if (socket >= 0)
  159. {
  160. struct lwip_sock *lwsock;
  161. lwsock = lwip_tryget_socket(socket);
  162. lwsock->conn->callback = event_callback;
  163. rt_wqueue_init(&lwsock->wait_head);
  164. }
  165. return socket;
  166. #else
  167. return lwip_socket(domain, type, protocol);
  168. #endif /* SAL_USING_POSIX */
  169. }
  170. static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  171. {
  172. #ifdef SAL_USING_POSIX
  173. int new_socket;
  174. new_socket = lwip_accept(socket, addr, addrlen);
  175. if (new_socket >= 0)
  176. {
  177. struct lwip_sock *lwsock;
  178. lwsock = lwip_tryget_socket(new_socket);
  179. rt_wqueue_init(&lwsock->wait_head);
  180. }
  181. return new_socket;
  182. #else
  183. return lwip_accept(socket, addr, addrlen);
  184. #endif /* SAL_USING_POSIX */
  185. }
  186. static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  187. {
  188. #if LWIP_VERSION_MAJOR < 2U
  189. rt_kprintf("ERROR: Your lwIP version is not supported. Please using lwIP 2.0.0+.\n");
  190. RT_ASSERT(LWIP_VERSION_MAJOR >= 2U);
  191. #endif
  192. return lwip_getsockname(socket, name, namelen);
  193. }
  194. int inet_ioctlsocket(int socket, long cmd, void *arg)
  195. {
  196. int flags;
  197. switch (cmd)
  198. {
  199. case F_GETFL:
  200. case F_SETFL:
  201. flags = (int)(size_t)arg;
  202. #ifdef O_LARGEFILE
  203. flags &= ~O_LARGEFILE;
  204. #endif
  205. return lwip_fcntl(socket, cmd, flags);
  206. default:
  207. return lwip_ioctl(socket, cmd, arg);
  208. }
  209. }
  210. #ifdef SAL_USING_POSIX
  211. static int inet_poll(struct dfs_file *file, struct rt_pollreq *req)
  212. {
  213. int mask = 0;
  214. struct lwip_sock *sock;
  215. struct sal_socket *sal_sock;
  216. sal_sock = sal_get_socket((int)(size_t)file->vnode->data);
  217. if(!sal_sock)
  218. {
  219. return -1;
  220. }
  221. sock = lwip_tryget_socket((int)(size_t)sal_sock->user_data);
  222. if (sock != NULL)
  223. {
  224. rt_base_t level;
  225. rt_poll_add(&sock->wait_head, req);
  226. level = rt_spin_lock_irqsave(&_spinlock);
  227. #if LWIP_VERSION >= 0x20100ff
  228. if ((void*)(sock->lastdata.pbuf) || sock->rcvevent)
  229. #else
  230. if ((void*)(sock->lastdata) || sock->rcvevent)
  231. #endif
  232. {
  233. mask |= POLLIN;
  234. }
  235. if (sock->sendevent)
  236. {
  237. mask |= POLLOUT;
  238. }
  239. if (sock->errevent)
  240. {
  241. mask |= POLLERR;
  242. /* clean error event */
  243. sock->errevent = 0;
  244. }
  245. rt_spin_unlock_irqrestore(&_spinlock, level);
  246. }
  247. return mask;
  248. }
  249. #endif
  250. static const struct sal_socket_ops lwip_socket_ops =
  251. {
  252. .socket = inet_socket,
  253. .closesocket = lwip_close,
  254. .bind = lwip_bind,
  255. .listen = lwip_listen,
  256. .connect = lwip_connect,
  257. .accept = inet_accept,
  258. .sendto = (int (*)(int, const void *, size_t, int, const struct sockaddr *, socklen_t))lwip_sendto,
  259. #if LWIP_VERSION >= 0x20102ff
  260. .sendmsg = (int (*)(int, const struct msghdr *, int))lwip_sendmsg,
  261. .recvmsg = (int (*)(int, struct msghdr *, int))lwip_recvmsg,
  262. #endif
  263. .recvfrom = (int (*)(int, void *, size_t, int, struct sockaddr *, socklen_t *))lwip_recvfrom,
  264. .getsockopt = lwip_getsockopt,
  265. //TODO fix on 1.4.1
  266. .setsockopt = lwip_setsockopt,
  267. .shutdown = lwip_shutdown,
  268. .getpeername = lwip_getpeername,
  269. .getsockname = inet_getsockname,
  270. .ioctlsocket = inet_ioctlsocket,
  271. .socketpair = RT_NULL,
  272. #ifdef SAL_USING_POSIX
  273. .poll = inet_poll,
  274. #endif
  275. };
  276. static const struct sal_netdb_ops lwip_netdb_ops =
  277. {
  278. .gethostbyname = lwip_gethostbyname,
  279. .gethostbyname_r = lwip_gethostbyname_r,
  280. .getaddrinfo = lwip_getaddrinfo,
  281. .freeaddrinfo = lwip_freeaddrinfo,
  282. };
  283. static const struct sal_proto_family lwip_inet_family =
  284. {
  285. .family = AF_INET,
  286. #if LWIP_VERSION > 0x2000000
  287. .sec_family = AF_INET6,
  288. #else
  289. .sec_family = AF_INET,
  290. #endif
  291. .skt_ops = &lwip_socket_ops,
  292. .netdb_ops = &lwip_netdb_ops,
  293. };
  294. /* Set lwIP network interface device protocol family information */
  295. int sal_lwip_netdev_set_pf_info(struct netdev *netdev)
  296. {
  297. RT_ASSERT(netdev);
  298. netdev->sal_user_data = (void *) &lwip_inet_family;
  299. return 0;
  300. }
  301. #endif /* SAL_USING_LWIP */