PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

lib/liblwip/api/sockets.c

http://www.minix3.org/
C | 2328 lines | 1768 code | 239 blank | 321 comment | 386 complexity | 7443916d13ab2996b58234bf6447072c MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD

Large files files are truncated, but you can click here to view the full file

  1. /**
  2. * @file
  3. * Sockets BSD-Like API module
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. * Improved by Marc Boucher <marc@mbsi.ca> and David Haas <dhaas@alum.rpi.edu>
  37. *
  38. */
  39. #include "lwip/opt.h"
  40. #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
  41. #include "lwip/sockets.h"
  42. #include "lwip/api.h"
  43. #include "lwip/sys.h"
  44. #include "lwip/igmp.h"
  45. #include "lwip/inet.h"
  46. #include "lwip/tcp.h"
  47. #include "lwip/raw.h"
  48. #include "lwip/udp.h"
  49. #include "lwip/tcpip.h"
  50. #include "lwip/pbuf.h"
  51. #if LWIP_CHECKSUM_ON_COPY
  52. #include "lwip/inet_chksum.h"
  53. #endif
  54. #include <string.h>
  55. #define NUM_SOCKETS MEMP_NUM_NETCONN
  56. /** Contains all internal pointers and states used for a socket */
  57. struct lwip_sock {
  58. /** sockets currently are built on netconns, each socket has one netconn */
  59. struct netconn *conn;
  60. /** data that was left from the previous read */
  61. void *lastdata;
  62. /** offset in the data that was left from the previous read */
  63. u16_t lastoffset;
  64. /** number of times data was received, set by event_callback(),
  65. tested by the receive and select functions */
  66. s16_t rcvevent;
  67. /** number of times data was ACKed (free send buffer), set by event_callback(),
  68. tested by select */
  69. u16_t sendevent;
  70. /** error happened for this socket, set by event_callback(), tested by select */
  71. u16_t errevent;
  72. /** last error that occurred on this socket */
  73. int err;
  74. /** counter of how many threads are waiting for this socket using select */
  75. int select_waiting;
  76. };
  77. /** Description for a task waiting in select */
  78. struct lwip_select_cb {
  79. /** Pointer to the next waiting task */
  80. struct lwip_select_cb *next;
  81. /** Pointer to the previous waiting task */
  82. struct lwip_select_cb *prev;
  83. /** readset passed to select */
  84. fd_set *readset;
  85. /** writeset passed to select */
  86. fd_set *writeset;
  87. /** unimplemented: exceptset passed to select */
  88. fd_set *exceptset;
  89. /** don't signal the same semaphore twice: set to 1 when signalled */
  90. int sem_signalled;
  91. /** semaphore to wake up a task waiting for select */
  92. sys_sem_t sem;
  93. };
  94. /** This struct is used to pass data to the set/getsockopt_internal
  95. * functions running in tcpip_thread context (only a void* is allowed) */
  96. struct lwip_setgetsockopt_data {
  97. /** socket struct for which to change options */
  98. struct lwip_sock *sock;
  99. /** socket index for which to change options */
  100. int s;
  101. /** level of the option to process */
  102. int level;
  103. /** name of the option to process */
  104. int optname;
  105. /** set: value to set the option to
  106. * get: value of the option is stored here */
  107. void *optval;
  108. /** size of *optval */
  109. socklen_t *optlen;
  110. /** if an error occures, it is temporarily stored here */
  111. err_t err;
  112. };
  113. /** The global array of available sockets */
  114. static struct lwip_sock sockets[NUM_SOCKETS];
  115. /** The global list of tasks waiting for select */
  116. static struct lwip_select_cb *select_cb_list;
  117. /** This counter is increased from lwip_select when the list is chagned
  118. and checked in event_callback to see if it has changed. */
  119. static volatile int select_cb_ctr;
  120. /** Table to quickly map an lwIP error (err_t) to a socket error
  121. * by using -err as an index */
  122. static const int err_to_errno_table[] = {
  123. 0, /* ERR_OK 0 No error, everything OK. */
  124. ENOMEM, /* ERR_MEM -1 Out of memory error. */
  125. ENOBUFS, /* ERR_BUF -2 Buffer error. */
  126. EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */
  127. EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */
  128. EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */
  129. EINVAL, /* ERR_VAL -6 Illegal value. */
  130. EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block. */
  131. ECONNABORTED, /* ERR_ABRT -8 Connection aborted. */
  132. ECONNRESET, /* ERR_RST -9 Connection reset. */
  133. ESHUTDOWN, /* ERR_CLSD -10 Connection closed. */
  134. ENOTCONN, /* ERR_CONN -11 Not connected. */
  135. EIO, /* ERR_ARG -12 Illegal argument. */
  136. EADDRINUSE, /* ERR_USE -13 Address in use. */
  137. -1, /* ERR_IF -14 Low-level netif error */
  138. -1, /* ERR_ISCONN -15 Already connected. */
  139. };
  140. #define ERR_TO_ERRNO_TABLE_SIZE \
  141. (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0]))
  142. #define err_to_errno(err) \
  143. ((unsigned)(-(err)) < ERR_TO_ERRNO_TABLE_SIZE ? \
  144. err_to_errno_table[-(err)] : EIO)
  145. #ifdef ERRNO
  146. #ifndef set_errno
  147. #define set_errno(err) errno = (err)
  148. #endif
  149. #else /* ERRNO */
  150. #define set_errno(err)
  151. #endif /* ERRNO */
  152. #define sock_set_errno(sk, e) do { \
  153. sk->err = (e); \
  154. set_errno(sk->err); \
  155. } while (0)
  156. /* Forward delcaration of some functions */
  157. static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
  158. static void lwip_getsockopt_internal(void *arg);
  159. static void lwip_setsockopt_internal(void *arg);
  160. /**
  161. * Initialize this module. This function has to be called before any other
  162. * functions in this module!
  163. */
  164. void
  165. lwip_socket_init(void)
  166. {
  167. }
  168. /**
  169. * Map a externally used socket index to the internal socket representation.
  170. *
  171. * @param s externally used socket index
  172. * @return struct lwip_sock for the socket or NULL if not found
  173. */
  174. static struct lwip_sock *
  175. get_socket(int s)
  176. {
  177. struct lwip_sock *sock;
  178. if ((s < 0) || (s >= NUM_SOCKETS)) {
  179. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
  180. set_errno(EBADF);
  181. return NULL;
  182. }
  183. sock = &sockets[s];
  184. if (!sock->conn) {
  185. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
  186. set_errno(EBADF);
  187. return NULL;
  188. }
  189. return sock;
  190. }
  191. /**
  192. * Same as get_socket but doesn't set errno
  193. *
  194. * @param s externally used socket index
  195. * @return struct lwip_sock for the socket or NULL if not found
  196. */
  197. static struct lwip_sock *
  198. tryget_socket(int s)
  199. {
  200. if ((s < 0) || (s >= NUM_SOCKETS)) {
  201. return NULL;
  202. }
  203. if (!sockets[s].conn) {
  204. return NULL;
  205. }
  206. return &sockets[s];
  207. }
  208. /**
  209. * Allocate a new socket for a given netconn.
  210. *
  211. * @param newconn the netconn for which to allocate a socket
  212. * @param accepted 1 if socket has been created by accept(),
  213. * 0 if socket has been created by socket()
  214. * @return the index of the new socket; -1 on error
  215. */
  216. static int
  217. alloc_socket(struct netconn *newconn, int accepted)
  218. {
  219. int i;
  220. SYS_ARCH_DECL_PROTECT(lev);
  221. /* allocate a new socket identifier */
  222. for (i = 0; i < NUM_SOCKETS; ++i) {
  223. /* Protect socket array */
  224. SYS_ARCH_PROTECT(lev);
  225. if (!sockets[i].conn) {
  226. sockets[i].conn = newconn;
  227. /* The socket is not yet known to anyone, so no need to protect
  228. after having marked it as used. */
  229. SYS_ARCH_UNPROTECT(lev);
  230. sockets[i].lastdata = NULL;
  231. sockets[i].lastoffset = 0;
  232. sockets[i].rcvevent = 0;
  233. /* TCP sendbuf is empty, but the socket is not yet writable until connected
  234. * (unless it has been created by accept()). */
  235. sockets[i].sendevent = (newconn->type == NETCONN_TCP ? (accepted != 0) : 1);
  236. sockets[i].errevent = 0;
  237. sockets[i].err = 0;
  238. sockets[i].select_waiting = 0;
  239. return i;
  240. }
  241. SYS_ARCH_UNPROTECT(lev);
  242. }
  243. return -1;
  244. }
  245. /** Free a socket. The socket's netconn must have been
  246. * delete before!
  247. *
  248. * @param sock the socket to free
  249. * @param is_tcp != 0 for TCP sockets, used to free lastdata
  250. */
  251. static void
  252. free_socket(struct lwip_sock *sock, int is_tcp)
  253. {
  254. void *lastdata;
  255. SYS_ARCH_DECL_PROTECT(lev);
  256. lastdata = sock->lastdata;
  257. sock->lastdata = NULL;
  258. sock->lastoffset = 0;
  259. sock->err = 0;
  260. /* Protect socket array */
  261. SYS_ARCH_PROTECT(lev);
  262. sock->conn = NULL;
  263. SYS_ARCH_UNPROTECT(lev);
  264. /* don't use 'sock' after this line, as another task might have allocated it */
  265. if (lastdata != NULL) {
  266. if (is_tcp) {
  267. pbuf_free((struct pbuf *)lastdata);
  268. } else {
  269. netbuf_delete((struct netbuf *)lastdata);
  270. }
  271. }
  272. }
  273. /* Below this, the well-known socket functions are implemented.
  274. * Use google.com or opengroup.org to get a good description :-)
  275. *
  276. * Exceptions are documented!
  277. */
  278. int
  279. lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  280. {
  281. struct lwip_sock *sock, *nsock;
  282. struct netconn *newconn;
  283. ip_addr_t naddr;
  284. u16_t port;
  285. int newsock;
  286. struct sockaddr_in sin;
  287. err_t err;
  288. SYS_ARCH_DECL_PROTECT(lev);
  289. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
  290. sock = get_socket(s);
  291. if (!sock) {
  292. return -1;
  293. }
  294. if (netconn_is_nonblocking(sock->conn) && (sock->rcvevent <= 0)) {
  295. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): returning EWOULDBLOCK\n", s));
  296. sock_set_errno(sock, EWOULDBLOCK);
  297. return -1;
  298. }
  299. /* wait for a new connection */
  300. err = netconn_accept(sock->conn, &newconn);
  301. if (err != ERR_OK) {
  302. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_acept failed, err=%d\n", s, err));
  303. sock_set_errno(sock, err_to_errno(err));
  304. return -1;
  305. }
  306. LWIP_ASSERT("newconn != NULL", newconn != NULL);
  307. /* Prevent automatic window updates, we do this on our own! */
  308. netconn_set_noautorecved(newconn, 1);
  309. /* get the IP address and port of the remote host */
  310. err = netconn_peer(newconn, &naddr, &port);
  311. if (err != ERR_OK) {
  312. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_peer failed, err=%d\n", s, err));
  313. netconn_delete(newconn);
  314. sock_set_errno(sock, err_to_errno(err));
  315. return -1;
  316. }
  317. /* Note that POSIX only requires us to check addr is non-NULL. addrlen must
  318. * not be NULL if addr is valid.
  319. */
  320. if (NULL != addr) {
  321. LWIP_ASSERT("addr valid but addrlen NULL", addrlen != NULL);
  322. memset(&sin, 0, sizeof(sin));
  323. sin.sin_len = sizeof(sin);
  324. sin.sin_family = AF_INET;
  325. sin.sin_port = htons(port);
  326. inet_addr_from_ipaddr(&sin.sin_addr, &naddr);
  327. if (*addrlen > sizeof(sin))
  328. *addrlen = sizeof(sin);
  329. MEMCPY(addr, &sin, *addrlen);
  330. }
  331. newsock = alloc_socket(newconn, 1);
  332. if (newsock == -1) {
  333. netconn_delete(newconn);
  334. sock_set_errno(sock, ENFILE);
  335. return -1;
  336. }
  337. LWIP_ASSERT("invalid socket index", (newsock >= 0) && (newsock < NUM_SOCKETS));
  338. LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback);
  339. nsock = &sockets[newsock];
  340. /* See event_callback: If data comes in right away after an accept, even
  341. * though the server task might not have created a new socket yet.
  342. * In that case, newconn->socket is counted down (newconn->socket--),
  343. * so nsock->rcvevent is >= 1 here!
  344. */
  345. SYS_ARCH_PROTECT(lev);
  346. nsock->rcvevent += (s16_t)(-1 - newconn->socket);
  347. newconn->socket = newsock;
  348. SYS_ARCH_UNPROTECT(lev);
  349. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
  350. ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
  351. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", port));
  352. sock_set_errno(sock, 0);
  353. return newsock;
  354. }
  355. int
  356. lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
  357. {
  358. struct lwip_sock *sock;
  359. ip_addr_t local_addr;
  360. u16_t local_port;
  361. err_t err;
  362. const struct sockaddr_in *name_in;
  363. sock = get_socket(s);
  364. if (!sock) {
  365. return -1;
  366. }
  367. /* check size, familiy and alignment of 'name' */
  368. LWIP_ERROR("lwip_bind: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
  369. ((name->sa_family) == AF_INET) && ((((mem_ptr_t)name) % 4) == 0)),
  370. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  371. name_in = (const struct sockaddr_in *)(void*)name;
  372. inet_addr_to_ipaddr(&local_addr, &name_in->sin_addr);
  373. local_port = name_in->sin_port;
  374. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
  375. ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
  376. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(local_port)));
  377. err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
  378. if (err != ERR_OK) {
  379. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
  380. sock_set_errno(sock, err_to_errno(err));
  381. return -1;
  382. }
  383. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
  384. sock_set_errno(sock, 0);
  385. return 0;
  386. }
  387. int
  388. lwip_close(int s)
  389. {
  390. struct lwip_sock *sock;
  391. int is_tcp = 0;
  392. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
  393. sock = get_socket(s);
  394. if (!sock) {
  395. return -1;
  396. }
  397. if(sock->conn != NULL) {
  398. is_tcp = netconn_type(sock->conn) == NETCONN_TCP;
  399. } else {
  400. LWIP_ASSERT("sock->lastdata == NULL", sock->lastdata == NULL);
  401. }
  402. netconn_delete(sock->conn);
  403. free_socket(sock, is_tcp);
  404. set_errno(0);
  405. return 0;
  406. }
  407. int
  408. lwip_connect(int s, const struct sockaddr *name, socklen_t namelen)
  409. {
  410. struct lwip_sock *sock;
  411. err_t err;
  412. const struct sockaddr_in *name_in;
  413. sock = get_socket(s);
  414. if (!sock) {
  415. return -1;
  416. }
  417. /* check size, familiy and alignment of 'name' */
  418. LWIP_ERROR("lwip_connect: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
  419. ((name->sa_family) == AF_INET) && ((((mem_ptr_t)name) % 4) == 0)),
  420. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  421. name_in = (const struct sockaddr_in *)(void*)name;
  422. if (name_in->sin_family == AF_UNSPEC) {
  423. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
  424. err = netconn_disconnect(sock->conn);
  425. } else {
  426. ip_addr_t remote_addr;
  427. u16_t remote_port;
  428. inet_addr_to_ipaddr(&remote_addr, &name_in->sin_addr);
  429. remote_port = name_in->sin_port;
  430. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
  431. ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
  432. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(remote_port)));
  433. err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
  434. }
  435. if (err != ERR_OK) {
  436. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
  437. sock_set_errno(sock, err_to_errno(err));
  438. return -1;
  439. }
  440. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
  441. sock_set_errno(sock, 0);
  442. return 0;
  443. }
  444. /**
  445. * Set a socket into listen mode.
  446. * The socket may not have been used for another connection previously.
  447. *
  448. * @param s the socket to set to listening mode
  449. * @param backlog (ATTENTION: needs TCP_LISTEN_BACKLOG=1)
  450. * @return 0 on success, non-zero on failure
  451. */
  452. int
  453. lwip_listen(int s, int backlog)
  454. {
  455. struct lwip_sock *sock;
  456. err_t err;
  457. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
  458. sock = get_socket(s);
  459. if (!sock) {
  460. return -1;
  461. }
  462. /* limit the "backlog" parameter to fit in an u8_t */
  463. backlog = LWIP_MIN(LWIP_MAX(backlog, 0), 0xff);
  464. err = netconn_listen_with_backlog(sock->conn, (u8_t)backlog);
  465. if (err != ERR_OK) {
  466. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
  467. sock_set_errno(sock, err_to_errno(err));
  468. return -1;
  469. }
  470. sock_set_errno(sock, 0);
  471. return 0;
  472. }
  473. int
  474. lwip_recvfrom(int s, void *mem, size_t len, int flags,
  475. struct sockaddr *from, socklen_t *fromlen)
  476. {
  477. struct lwip_sock *sock;
  478. void *buf = NULL;
  479. struct pbuf *p;
  480. u16_t buflen, copylen;
  481. int off = 0;
  482. ip_addr_t *addr;
  483. u16_t port;
  484. u8_t done = 0;
  485. err_t err;
  486. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %"SZT_F", 0x%x, ..)\n", s, mem, len, flags));
  487. sock = get_socket(s);
  488. if (!sock) {
  489. return -1;
  490. }
  491. do {
  492. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: top while sock->lastdata=%p\n", sock->lastdata));
  493. /* Check if there is data left from the last recv operation. */
  494. if (sock->lastdata) {
  495. buf = sock->lastdata;
  496. } else {
  497. /* If this is non-blocking call, then check first */
  498. if (((flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn)) &&
  499. (sock->rcvevent <= 0)) {
  500. if (off > 0) {
  501. /* update receive window */
  502. netconn_recved(sock->conn, (u32_t)off);
  503. /* already received data, return that */
  504. sock_set_errno(sock, 0);
  505. return off;
  506. }
  507. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
  508. sock_set_errno(sock, EWOULDBLOCK);
  509. return -1;
  510. }
  511. /* No data was left from the previous operation, so we try to get
  512. some from the network. */
  513. if (netconn_type(sock->conn) == NETCONN_TCP) {
  514. err = netconn_recv_tcp_pbuf(sock->conn, (struct pbuf **)&buf);
  515. } else {
  516. err = netconn_recv(sock->conn, (struct netbuf **)&buf);
  517. }
  518. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: netconn_recv err=%d, netbuf=%p\n",
  519. err, buf));
  520. if (err != ERR_OK) {
  521. if (off > 0) {
  522. /* update receive window */
  523. netconn_recved(sock->conn, (u32_t)off);
  524. /* already received data, return that */
  525. sock_set_errno(sock, 0);
  526. return off;
  527. }
  528. /* We should really do some error checking here. */
  529. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL, error is \"%s\"!\n",
  530. s, lwip_strerr(err)));
  531. sock_set_errno(sock, err_to_errno(err));
  532. if (err == ERR_CLSD) {
  533. return 0;
  534. } else {
  535. return -1;
  536. }
  537. }
  538. LWIP_ASSERT("buf != NULL", buf != NULL);
  539. sock->lastdata = buf;
  540. }
  541. if (netconn_type(sock->conn) == NETCONN_TCP) {
  542. p = (struct pbuf *)buf;
  543. } else {
  544. p = ((struct netbuf *)buf)->p;
  545. }
  546. buflen = p->tot_len;
  547. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: buflen=%"U16_F" len=%"SZT_F" off=%d sock->lastoffset=%"U16_F"\n",
  548. buflen, len, off, sock->lastoffset));
  549. buflen -= sock->lastoffset;
  550. if (len > buflen) {
  551. copylen = buflen;
  552. } else {
  553. copylen = (u16_t)len;
  554. }
  555. /* copy the contents of the received buffer into
  556. the supplied memory pointer mem */
  557. pbuf_copy_partial(p, (u8_t*)mem + off, copylen, sock->lastoffset);
  558. off += copylen;
  559. if (netconn_type(sock->conn) == NETCONN_TCP) {
  560. LWIP_ASSERT("invalid copylen, len would underflow", len >= copylen);
  561. len -= copylen;
  562. if ( (len <= 0) ||
  563. (p->flags & PBUF_FLAG_PUSH) ||
  564. (sock->rcvevent <= 0) ||
  565. ((flags & MSG_PEEK)!=0)) {
  566. done = 1;
  567. }
  568. } else {
  569. done = 1;
  570. }
  571. /* Check to see from where the data was.*/
  572. if (done) {
  573. ip_addr_t fromaddr;
  574. if (from && fromlen) {
  575. struct sockaddr_in sin;
  576. if (netconn_type(sock->conn) == NETCONN_TCP) {
  577. addr = &fromaddr;
  578. netconn_getaddr(sock->conn, addr, &port, 0);
  579. } else {
  580. addr = netbuf_fromaddr((struct netbuf *)buf);
  581. port = netbuf_fromport((struct netbuf *)buf);
  582. }
  583. memset(&sin, 0, sizeof(sin));
  584. sin.sin_len = sizeof(sin);
  585. sin.sin_family = AF_INET;
  586. sin.sin_port = htons(port);
  587. inet_addr_from_ipaddr(&sin.sin_addr, addr);
  588. if (*fromlen > sizeof(sin)) {
  589. *fromlen = sizeof(sin);
  590. }
  591. MEMCPY(from, &sin, *fromlen);
  592. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
  593. ip_addr_debug_print(SOCKETS_DEBUG, addr);
  594. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, off));
  595. } else {
  596. #if SOCKETS_DEBUG
  597. if (netconn_type(sock->conn) == NETCONN_TCP) {
  598. addr = &fromaddr;
  599. netconn_getaddr(sock->conn, addr, &port, 0);
  600. } else {
  601. addr = netbuf_fromaddr((struct netbuf *)buf);
  602. port = netbuf_fromport((struct netbuf *)buf);
  603. }
  604. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
  605. ip_addr_debug_print(SOCKETS_DEBUG, addr);
  606. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, off));
  607. #endif /* SOCKETS_DEBUG */
  608. }
  609. }
  610. /* If we don't peek the incoming message... */
  611. if ((flags & MSG_PEEK) == 0) {
  612. /* If this is a TCP socket, check if there is data left in the
  613. buffer. If so, it should be saved in the sock structure for next
  614. time around. */
  615. if ((netconn_type(sock->conn) == NETCONN_TCP) && (buflen - copylen > 0)) {
  616. sock->lastdata = buf;
  617. sock->lastoffset += copylen;
  618. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: lastdata now netbuf=%p\n", buf));
  619. } else {
  620. sock->lastdata = NULL;
  621. sock->lastoffset = 0;
  622. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: deleting netbuf=%p\n", buf));
  623. if (netconn_type(sock->conn) == NETCONN_TCP) {
  624. pbuf_free((struct pbuf *)buf);
  625. } else {
  626. netbuf_delete((struct netbuf *)buf);
  627. }
  628. }
  629. }
  630. } while (!done);
  631. if (off > 0) {
  632. /* update receive window */
  633. netconn_recved(sock->conn, (u32_t)off);
  634. }
  635. sock_set_errno(sock, 0);
  636. return off;
  637. }
  638. int
  639. lwip_read(int s, void *mem, size_t len)
  640. {
  641. return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
  642. }
  643. int
  644. lwip_recv(int s, void *mem, size_t len, int flags)
  645. {
  646. return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
  647. }
  648. int
  649. lwip_send(int s, const void *data, size_t size, int flags)
  650. {
  651. struct lwip_sock *sock;
  652. err_t err;
  653. u8_t write_flags;
  654. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%"SZT_F", flags=0x%x)\n",
  655. s, data, size, flags));
  656. sock = get_socket(s);
  657. if (!sock) {
  658. return -1;
  659. }
  660. if (sock->conn->type != NETCONN_TCP) {
  661. #if (LWIP_UDP || LWIP_RAW)
  662. return lwip_sendto(s, data, size, flags, NULL, 0);
  663. #else /* (LWIP_UDP || LWIP_RAW) */
  664. sock_set_errno(sock, err_to_errno(ERR_ARG));
  665. return -1;
  666. #endif /* (LWIP_UDP || LWIP_RAW) */
  667. }
  668. if ((flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn)) {
  669. if ((size > TCP_SND_BUF) || ((size / TCP_MSS) > TCP_SND_QUEUELEN)) {
  670. /* too much data to ever send nonblocking! */
  671. sock_set_errno(sock, EMSGSIZE);
  672. return -1;
  673. }
  674. }
  675. write_flags = NETCONN_COPY |
  676. ((flags & MSG_MORE) ? NETCONN_MORE : 0) |
  677. ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0);
  678. err = netconn_write(sock->conn, data, size, write_flags);
  679. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d size=%"SZT_F"\n", s, err, size));
  680. sock_set_errno(sock, err_to_errno(err));
  681. return (err == ERR_OK ? (int)size : -1);
  682. }
  683. int
  684. lwip_sendto(int s, const void *data, size_t size, int flags,
  685. const struct sockaddr *to, socklen_t tolen)
  686. {
  687. struct lwip_sock *sock;
  688. err_t err;
  689. u16_t short_size;
  690. const struct sockaddr_in *to_in;
  691. u16_t remote_port;
  692. #if !LWIP_TCPIP_CORE_LOCKING
  693. struct netbuf buf;
  694. #endif
  695. sock = get_socket(s);
  696. if (!sock) {
  697. return -1;
  698. }
  699. if (sock->conn->type == NETCONN_TCP) {
  700. #if LWIP_TCP
  701. return lwip_send(s, data, size, flags);
  702. #else /* LWIP_TCP */
  703. sock_set_errno(sock, err_to_errno(ERR_ARG));
  704. return -1;
  705. #endif /* LWIP_TCP */
  706. }
  707. /* @todo: split into multiple sendto's? */
  708. LWIP_ASSERT("lwip_sendto: size must fit in u16_t", size <= 0xffff);
  709. short_size = (u16_t)size;
  710. LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) ||
  711. ((tolen == sizeof(struct sockaddr_in)) &&
  712. ((to->sa_family) == AF_INET) && ((((mem_ptr_t)to) % 4) == 0))),
  713. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  714. to_in = (const struct sockaddr_in *)(void*)to;
  715. #if LWIP_TCPIP_CORE_LOCKING
  716. /* Should only be consider like a sample or a simple way to experiment this option (no check of "to" field...) */
  717. {
  718. struct pbuf* p;
  719. ip_addr_t *remote_addr;
  720. #if LWIP_NETIF_TX_SINGLE_PBUF
  721. p = pbuf_alloc(PBUF_TRANSPORT, short_size, PBUF_RAM);
  722. if (p != NULL) {
  723. #if LWIP_CHECKSUM_ON_COPY
  724. u16_t chksum = 0;
  725. if (sock->conn->type != NETCONN_RAW) {
  726. chksum = LWIP_CHKSUM_COPY(p->payload, data, short_size);
  727. } else
  728. #endif /* LWIP_CHECKSUM_ON_COPY */
  729. MEMCPY(p->payload, data, size);
  730. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  731. p = pbuf_alloc(PBUF_TRANSPORT, short_size, PBUF_REF);
  732. if (p != NULL) {
  733. p->payload = (void*)data;
  734. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  735. if (to_in != NULL) {
  736. inet_addr_to_ipaddr_p(remote_addr, &to_in->sin_addr);
  737. remote_port = ntohs(to_in->sin_port);
  738. } else {
  739. remote_addr = IP_ADDR_ANY;
  740. remote_port = 0;
  741. }
  742. LOCK_TCPIP_CORE();
  743. if (sock->conn->type == NETCONN_RAW) {
  744. err = sock->conn->last_err = raw_sendto(sock->conn->pcb.raw, p, remote_addr);
  745. } else {
  746. #if LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF
  747. err = sock->conn->last_err = udp_sendto_chksum(sock->conn->pcb.udp, p,
  748. remote_addr, remote_port, 1, chksum);
  749. #else /* LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF */
  750. err = sock->conn->last_err = udp_sendto(sock->conn->pcb.udp, p,
  751. remote_addr, remote_port);
  752. #endif /* LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF */
  753. }
  754. UNLOCK_TCPIP_CORE();
  755. pbuf_free(p);
  756. } else {
  757. err = ERR_MEM;
  758. }
  759. }
  760. #else /* LWIP_TCPIP_CORE_LOCKING */
  761. /* initialize a buffer */
  762. buf.p = buf.ptr = NULL;
  763. #if LWIP_CHECKSUM_ON_COPY
  764. buf.flags = 0;
  765. #endif /* LWIP_CHECKSUM_ON_COPY */
  766. if (to) {
  767. inet_addr_to_ipaddr(&buf.addr, &to_in->sin_addr);
  768. remote_port = ntohs(to_in->sin_port);
  769. netbuf_fromport(&buf) = remote_port;
  770. } else {
  771. remote_port = 0;
  772. ip_addr_set_any(&buf.addr);
  773. netbuf_fromport(&buf) = 0;
  774. }
  775. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%d"U16_F", flags=0x%x to=",
  776. s, data, short_size, flags));
  777. ip_addr_debug_print(SOCKETS_DEBUG, &buf.addr);
  778. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port));
  779. /* make the buffer point to the data that should be sent */
  780. #if LWIP_NETIF_TX_SINGLE_PBUF
  781. /* Allocate a new netbuf and copy the data into it. */
  782. if (netbuf_alloc(&buf, short_size) == NULL) {
  783. err = ERR_MEM;
  784. } else {
  785. #if LWIP_CHECKSUM_ON_COPY
  786. if (sock->conn->type != NETCONN_RAW) {
  787. u16_t chksum = LWIP_CHKSUM_COPY(buf.p->payload, data, short_size);
  788. netbuf_set_chksum(&buf, chksum);
  789. err = ERR_OK;
  790. } else
  791. #endif /* LWIP_CHECKSUM_ON_COPY */
  792. {
  793. err = netbuf_take(&buf, data, short_size);
  794. }
  795. }
  796. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  797. err = netbuf_ref(&buf, data, short_size);
  798. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  799. if (err == ERR_OK) {
  800. /* send the data */
  801. err = netconn_send(sock->conn, &buf);
  802. }
  803. /* deallocated the buffer */
  804. netbuf_free(&buf);
  805. #endif /* LWIP_TCPIP_CORE_LOCKING */
  806. sock_set_errno(sock, err_to_errno(err));
  807. return (err == ERR_OK ? short_size : -1);
  808. }
  809. int
  810. lwip_socket(int domain, int type, int protocol)
  811. {
  812. struct netconn *conn;
  813. int i;
  814. LWIP_UNUSED_ARG(domain);
  815. /* create a netconn */
  816. switch (type) {
  817. case SOCK_RAW:
  818. conn = netconn_new_with_proto_and_callback(NETCONN_RAW, (u8_t)protocol, event_callback);
  819. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ",
  820. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  821. break;
  822. case SOCK_DGRAM:
  823. conn = netconn_new_with_callback( (protocol == IPPROTO_UDPLITE) ?
  824. NETCONN_UDPLITE : NETCONN_UDP, event_callback);
  825. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ",
  826. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  827. break;
  828. case SOCK_STREAM:
  829. conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
  830. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ",
  831. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  832. if (conn != NULL) {
  833. /* Prevent automatic window updates, we do this on our own! */
  834. netconn_set_noautorecved(conn, 1);
  835. }
  836. break;
  837. default:
  838. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n",
  839. domain, type, protocol));
  840. set_errno(EINVAL);
  841. return -1;
  842. }
  843. if (!conn) {
  844. LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
  845. set_errno(ENOBUFS);
  846. return -1;
  847. }
  848. i = alloc_socket(conn, 0);
  849. if (i == -1) {
  850. netconn_delete(conn);
  851. set_errno(ENFILE);
  852. return -1;
  853. }
  854. conn->socket = i;
  855. LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
  856. set_errno(0);
  857. return i;
  858. }
  859. int
  860. lwip_write(int s, const void *data, size_t size)
  861. {
  862. return lwip_send(s, data, size, 0);
  863. }
  864. /**
  865. * Go through the readset and writeset lists and see which socket of the sockets
  866. * set in the sets has events. On return, readset, writeset and exceptset have
  867. * the sockets enabled that had events.
  868. *
  869. * exceptset is not used for now!!!
  870. *
  871. * @param maxfdp1 the highest socket index in the sets
  872. * @param readset_in: set of sockets to check for read events
  873. * @param writeset_in: set of sockets to check for write events
  874. * @param exceptset_in: set of sockets to check for error events
  875. * @param readset_out: set of sockets that had read events
  876. * @param writeset_out: set of sockets that had write events
  877. * @param exceptset_out: set os sockets that had error events
  878. * @return number of sockets that had events (read/write/exception) (>= 0)
  879. */
  880. static int
  881. lwip_selscan(int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in,
  882. fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out)
  883. {
  884. int i, nready = 0;
  885. fd_set lreadset, lwriteset, lexceptset;
  886. struct lwip_sock *sock;
  887. SYS_ARCH_DECL_PROTECT(lev);
  888. FD_ZERO(&lreadset);
  889. FD_ZERO(&lwriteset);
  890. FD_ZERO(&lexceptset);
  891. /* Go through each socket in each list to count number of sockets which
  892. currently match */
  893. for(i = 0; i < maxfdp1; i++) {
  894. void* lastdata = NULL;
  895. s16_t rcvevent = 0;
  896. u16_t sendevent = 0;
  897. u16_t errevent = 0;
  898. /* First get the socket's status (protected)... */
  899. SYS_ARCH_PROTECT(lev);
  900. sock = tryget_socket(i);
  901. if (sock != NULL) {
  902. lastdata = sock->lastdata;
  903. rcvevent = sock->rcvevent;
  904. sendevent = sock->sendevent;
  905. errevent = sock->errevent;
  906. }
  907. SYS_ARCH_UNPROTECT(lev);
  908. /* ... then examine it: */
  909. /* See if netconn of this socket is ready for read */
  910. if (readset_in && FD_ISSET(i, readset_in) && ((lastdata != NULL) || (rcvevent > 0))) {
  911. FD_SET(i, &lreadset);
  912. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
  913. nready++;
  914. }
  915. /* See if netconn of this socket is ready for write */
  916. if (writeset_in && FD_ISSET(i, writeset_in) && (sendevent != 0)) {
  917. FD_SET(i, &lwriteset);
  918. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
  919. nready++;
  920. }
  921. /* See if netconn of this socket had an error */
  922. if (exceptset_in && FD_ISSET(i, exceptset_in) && (errevent != 0)) {
  923. FD_SET(i, &lexceptset);
  924. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for exception\n", i));
  925. nready++;
  926. }
  927. }
  928. /* copy local sets to the ones provided as arguments */
  929. *readset_out = lreadset;
  930. *writeset_out = lwriteset;
  931. *exceptset_out = lexceptset;
  932. LWIP_ASSERT("nready >= 0", nready >= 0);
  933. return nready;
  934. }
  935. /**
  936. * Processing exceptset is not yet implemented.
  937. */
  938. int
  939. lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  940. struct timeval *timeout)
  941. {
  942. u32_t waitres = 0;
  943. int nready;
  944. fd_set lreadset, lwriteset, lexceptset;
  945. u32_t msectimeout;
  946. struct lwip_select_cb select_cb;
  947. err_t err;
  948. int i;
  949. SYS_ARCH_DECL_PROTECT(lev);
  950. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%"S32_F" tvusec=%"S32_F")\n",
  951. maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset,
  952. timeout ? (s32_t)timeout->tv_sec : (s32_t)-1,
  953. timeout ? (s32_t)timeout->tv_usec : (s32_t)-1));
  954. /* Go through each socket in each list to count number of sockets which
  955. currently match */
  956. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  957. /* If we don't have any current events, then suspend if we are supposed to */
  958. if (!nready) {
  959. if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
  960. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
  961. /* This is OK as the local fdsets are empty and nready is zero,
  962. or we would have returned earlier. */
  963. goto return_copy_fdsets;
  964. }
  965. /* None ready: add our semaphore to list:
  966. We don't actually need any dynamic memory. Our entry on the
  967. list is only valid while we are in this function, so it's ok
  968. to use local variables. */
  969. select_cb.next = NULL;
  970. select_cb.prev = NULL;
  971. select_cb.readset = readset;
  972. select_cb.writeset = writeset;
  973. select_cb.exceptset = exceptset;
  974. select_cb.sem_signalled = 0;
  975. err = sys_sem_new(&select_cb.sem, 0);
  976. if (err != ERR_OK) {
  977. /* failed to create semaphore */
  978. set_errno(ENOMEM);
  979. return -1;
  980. }
  981. /* Protect the select_cb_list */
  982. SYS_ARCH_PROTECT(lev);
  983. /* Put this select_cb on top of list */
  984. select_cb.next = select_cb_list;
  985. if (select_cb_list != NULL) {
  986. select_cb_list->prev = &select_cb;
  987. }
  988. select_cb_list = &select_cb;
  989. /* Increasing this counter tells even_callback that the list has changed. */
  990. select_cb_ctr++;
  991. /* Now we can safely unprotect */
  992. SYS_ARCH_UNPROTECT(lev);
  993. /* Increase select_waiting for each socket we are interested in */
  994. for(i = 0; i < maxfdp1; i++) {
  995. if ((readset && FD_ISSET(i, readset)) ||
  996. (writeset && FD_ISSET(i, writeset)) ||
  997. (exceptset && FD_ISSET(i, exceptset))) {
  998. struct lwip_sock *sock = tryget_socket(i);
  999. LWIP_ASSERT("sock != NULL", sock != NULL);
  1000. SYS_ARCH_PROTECT(lev);
  1001. sock->select_waiting++;
  1002. LWIP_ASSERT("sock->select_waiting > 0", sock->select_waiting > 0);
  1003. SYS_ARCH_UNPROTECT(lev);
  1004. }
  1005. }
  1006. /* Call lwip_selscan again: there could have been events between
  1007. the last scan (whithout us on the list) and putting us on the list! */
  1008. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  1009. if (!nready) {
  1010. /* Still none ready, just wait to be woken */
  1011. if (timeout == 0) {
  1012. /* Wait forever */
  1013. msectimeout = 0;
  1014. } else {
  1015. msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
  1016. if (msectimeout == 0) {
  1017. /* Wait 1ms at least (0 means wait forever) */
  1018. msectimeout = 1;
  1019. }
  1020. }
  1021. waitres = sys_arch_sem_wait(&select_cb.sem, msectimeout);
  1022. }
  1023. /* Increase select_waiting for each socket we are interested in */
  1024. for(i = 0; i < maxfdp1; i++) {
  1025. if ((readset && FD_ISSET(i, readset)) ||
  1026. (writeset && FD_ISSET(i, writeset)) ||
  1027. (exceptset && FD_ISSET(i, exceptset))) {
  1028. struct lwip_sock *sock = tryget_socket(i);
  1029. LWIP_ASSERT("sock != NULL", sock != NULL);
  1030. SYS_ARCH_PROTECT(lev);
  1031. sock->select_waiting--;
  1032. LWIP_ASSERT("sock->select_waiting >= 0", sock->select_waiting >= 0);
  1033. SYS_ARCH_UNPROTECT(lev);
  1034. }
  1035. }
  1036. /* Take us off the list */
  1037. SYS_ARCH_PROTECT(lev);
  1038. if (select_cb.next != NULL) {
  1039. select_cb.next->prev = select_cb.prev;
  1040. }
  1041. if (select_cb_list == &select_cb) {
  1042. LWIP_ASSERT("select_cb.prev == NULL", select_cb.prev == NULL);
  1043. select_cb_list = select_cb.next;
  1044. } else {
  1045. LWIP_ASSERT("select_cb.prev != NULL", select_cb.prev != NULL);
  1046. select_cb.prev->next = select_cb.next;
  1047. }
  1048. SYS_ARCH_UNPROTECT(lev);
  1049. sys_sem_free(&select_cb.sem);
  1050. if (waitres == SYS_ARCH_TIMEOUT) {
  1051. /* Timeout */
  1052. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
  1053. /* This is OK as the local fdsets are empty and nready is zero,
  1054. or we would have returned earlier. */
  1055. goto return_copy_fdsets;
  1056. }
  1057. /* See what's set */
  1058. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  1059. }
  1060. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
  1061. return_copy_fdsets:
  1062. set_errno(0);
  1063. if (readset) {
  1064. *readset = lreadset;
  1065. }
  1066. if (writeset) {
  1067. *writeset = lwriteset;
  1068. }
  1069. if (exceptset) {
  1070. *exceptset = lexceptset;
  1071. }
  1072. return nready;
  1073. }
  1074. /**
  1075. * Callback registered in the netconn layer for each socket-netconn.
  1076. * Processes recvevent (data available) and wakes up tasks waiting for select.
  1077. */
  1078. static void
  1079. event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
  1080. {
  1081. int s;
  1082. struct lwip_sock *sock;
  1083. struct lwip_select_cb *scb;
  1084. SYS_ARCH_DECL_PROTECT(lev);
  1085. LWIP_UNUSED_ARG(len);
  1086. /* Get socket */
  1087. if (conn) {
  1088. s = conn->socket;
  1089. if (s < 0) {
  1090. /* Data comes in right away after an accept, even though
  1091. * the server task might not have created a new socket yet.
  1092. * Just count down (or up) if that's the case and we
  1093. * will use the data later. Note that only receive events
  1094. * can happen before the new socket is set up. */
  1095. SYS_ARCH_PROTECT(lev);
  1096. if (conn->socket < 0) {
  1097. if (evt == NETCONN_EVT_RCVPLUS) {
  1098. conn->socket--;
  1099. }
  1100. SYS_ARCH_UNPROTECT(lev);
  1101. return;
  1102. }
  1103. s = conn->socket;
  1104. SYS_ARCH_UNPROTECT(lev);
  1105. }
  1106. sock = get_socket(s);
  1107. if (!sock) {
  1108. return;
  1109. }
  1110. } else {
  1111. return;
  1112. }
  1113. SYS_ARCH_PROTECT(lev);
  1114. /* Set event as required */
  1115. switch (evt) {
  1116. case NETCONN_EVT_RCVPLUS:
  1117. sock->rcvevent++;
  1118. break;
  1119. case NETCONN_EVT_RCVMINUS:
  1120. sock->rcvevent--;
  1121. break;
  1122. case NETCONN_EVT_SENDPLUS:
  1123. sock->sendevent = 1;
  1124. break;
  1125. case NETCONN_EVT_SENDMINUS:
  1126. sock->sendevent = 0;
  1127. break;
  1128. case NETCONN_EVT_ERROR:
  1129. sock->errevent = 1;
  1130. break;
  1131. default:
  1132. LWIP_ASSERT("unknown event", 0);
  1133. break;
  1134. }
  1135. if (sock->select_waiting == 0) {
  1136. /* noone is waiting for this socket, no need to check select_cb_list */
  1137. SYS_ARCH_UNPROTECT(lev);
  1138. return;
  1139. }
  1140. SYS_ARCH_UNPROTECT(lev);
  1141. /* Now decide if anyone is waiting for this socket */
  1142. /* NOTE: This code is written this way to protect the select link list
  1143. but to avoid a deadlock situation by releasing select_lock before
  1144. signalling for the select. This means we need to go through the list
  1145. multiple times ONLY IF a select was actually waiting. We go through
  1146. the list the number of waiting select calls + 1. This list is
  1147. expected to be small. */
  1148. while (1) {
  1149. int last_select_cb_ctr;
  1150. SYS_ARCH_PROTECT(lev);
  1151. for (scb = select_cb_list; scb; scb = scb->next) {
  1152. /* @todo: unprotect with each loop and check for changes? */
  1153. if (scb->sem_signalled == 0) {
  1154. /* Test this select call for our socket */
  1155. if (scb->readset && FD_ISSET(s, scb->readset)) {
  1156. if (sock->rcvevent > 0) {
  1157. break;
  1158. }
  1159. }
  1160. if (scb->writeset && FD_ISSET(s, scb->writeset)) {
  1161. if (sock->sendevent != 0) {
  1162. break;
  1163. }
  1164. }
  1165. if (scb->exceptset && FD_ISSET(s, scb->exceptset)) {
  1166. if (sock->errevent != 0) {
  1167. break;
  1168. }
  1169. }
  1170. }
  1171. /* unlock interrupts with each step */
  1172. last_select_cb_ctr = select_cb_ctr;
  1173. SYS_ARCH_UNPROTECT(lev);
  1174. SYS_ARCH_PROTECT(lev);
  1175. if (last_select_cb_ctr != select_cb_ctr) {
  1176. /* someone has changed select_cb_list, restart at the beginning */
  1177. scb = select_cb_list;
  1178. }
  1179. }
  1180. if (scb) {
  1181. scb->sem_signalled = 1;
  1182. sys_sem_signal(&scb->sem);
  1183. SYS_ARCH_UNPROTECT(lev);
  1184. } else {
  1185. SYS_ARCH_UNPROTECT(lev);
  1186. break;
  1187. }
  1188. }
  1189. }
  1190. /**
  1191. * Unimplemented: Close one end of a full-duplex connection.
  1192. * Currently, the full connection is closed.
  1193. */
  1194. int
  1195. lwip_shutdown(int s, int how)
  1196. {
  1197. struct lwip_sock *sock;
  1198. err_t err;
  1199. u8_t shut_rx = 0, shut_tx = 0;
  1200. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
  1201. sock = get_socket(s);
  1202. if (!sock) {
  1203. return -1;
  1204. }
  1205. if (sock->conn != NULL) {
  1206. if (netconn_type(sock->conn) != NETCONN_TCP) {
  1207. sock_set_errno(sock, EOPNOTSUPP);
  1208. return EOPNOTSUPP;
  1209. }
  1210. } else {
  1211. sock_set_errno(sock, ENOTCONN);
  1212. return ENOTCONN;
  1213. }
  1214. if (how == SHUT_RD) {
  1215. shut_rx = 1;
  1216. } else if (how == SHUT_WR) {
  1217. shut_tx = 1;
  1218. } else if(how == SHUT_RDWR) {
  1219. shut_rx = 1;
  1220. shut_tx = 1;
  1221. } else {
  1222. sock_set_errno(sock, EINVAL);
  1223. return EINVAL;
  1224. }
  1225. err = netconn_shutdown(sock->conn, shut_rx, shut_tx);
  1226. sock_set_errno(sock, err_to_errno(err));
  1227. return (err == ERR_OK ? 0 : -1);
  1228. }
  1229. static int
  1230. lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local)
  1231. {
  1232. struct lwip_sock *sock;
  1233. struct sockaddr_in sin;
  1234. ip_addr_t naddr;
  1235. sock = get_socket(s);
  1236. if (!sock) {
  1237. return -1;
  1238. }
  1239. memset(&sin, 0, sizeof(sin));
  1240. sin.sin_len = sizeof(sin);
  1241. sin.sin_family = AF_INET;
  1242. /* get the IP address and port */
  1243. netconn_getaddr(sock->conn, &naddr, &sin.sin_port, local);
  1244. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s));
  1245. ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
  1246. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", sin.sin_port));
  1247. sin.sin_port = htons(sin.sin_port);
  1248. inet_addr_from_ipaddr(&sin.sin_addr, &naddr);
  1249. if (*namelen > sizeof(sin)) {
  1250. *namelen = sizeof(sin);
  1251. }
  1252. MEMCPY(name, &sin, *namelen);
  1253. sock_set_errno(sock, 0);
  1254. return 0;
  1255. }
  1256. int
  1257. lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
  1258. {
  1259. return lwip_getaddrname(s, name, namelen, 0);
  1260. }
  1261. int
  1262. lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
  1263. {
  1264. return lwip_getaddrname(s, name, namelen, 1);
  1265. }
  1266. int
  1267. lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
  1268. {
  1269. err_t err = ERR_OK;
  1270. struct lwip_sock *sock = get_socket(s);
  1271. struct lwip_setgetsockopt_data data;
  1272. if (!sock) {
  1273. return -1;
  1274. }
  1275. if ((NULL == optval) || (NULL == optlen)) {
  1276. sock_set_errno(sock, EFAULT);
  1277. return -1;
  1278. }
  1279. /* Do length and type checks for the various options first, to keep it readable. */
  1280. switch (level) {
  1281. /* Level: SOL_SOCKET */
  1282. case SOL_SOCKET:
  1283. switch (optname) {
  1284. case SO_ACCEPTCONN:
  1285. case SO_BROADCAST:
  1286. /* UNIMPL case SO_DEBUG: */
  1287. /* UNIMPL case SO_DONTROUTE: */
  1288. case SO_ERROR:
  1289. case SO_KEEPALIVE:
  1290. /* UNIMPL case SO_CONTIMEO: */
  1291. /* UNIMPL case SO_SNDTIMEO: */
  1292. #if LWIP_SO_RCVTIMEO
  1293. case SO_RCVTIMEO:
  1294. #endif /* LWIP_SO_RCVTIMEO */
  1295. #if LWIP_SO_RCVBUF
  1296. case SO_RCVBUF:
  1297. #endif /* LWIP_SO_RCVBUF */
  1298. /* UNIMPL case SO_OOBINLINE: */
  1299. /* UNIMPL case SO_SNDBUF: */
  1300. /* UNIMPL case SO_RCVLOWAT: */
  1301. /* UNIMPL case SO_SNDLOWAT: */
  1302. #if SO_REUSE
  1303. case SO_REUSEADDR:
  1304. case SO_REUSEPORT:
  1305. #endif /* SO_REUSE */
  1306. case SO_TYPE:
  1307. /* UNIMPL case SO_USELOOPBACK: */
  1308. if (*optlen < sizeof(int)) {
  1309. err = EINVAL;
  1310. }
  1311. break;
  1312. case SO_NO_CHECK:
  1313. if (*optlen < sizeof(int)) {
  1314. err = EINVAL;
  1315. }
  1316. #if LWIP_UDP
  1317. if ((sock->conn->type != NETCONN_UDP) ||
  1318. ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
  1319. /* this flag is only available for UDP, not for UDP lite */
  1320. err = EAFNOSUPPORT;
  1321. }
  1322. #endif /* LWIP_UDP */
  1323. break;
  1324. default:
  1325. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
  1326. s, optname));
  1327. err = ENOPROTOOPT;
  1328. } /* switch (optname) */
  1329. break;
  1330. /* Level: IPPROTO_IP */
  1331. case IPPROTO_IP:
  1332. switch (optname) {
  1333. /* UNIMPL case IP_HDRINCL: */
  1334. /* UNIMPL case IP_RCVDSTADDR: */
  1335. /* UNIMPL case IP_RCVIF: */
  1336. case IP_TTL:
  1337. case IP_TOS:
  1338. if (*optlen < sizeof(int)) {
  1339. err = EINVAL;
  1340. }
  1341. break;
  1342. #if LWIP_IGMP
  1343. case IP_MULTICAST_TTL:
  1344. if (*optlen < sizeof(u8_t)) {
  1345. err = EINVAL;
  1346. }
  1347. break;
  1348. case IP_MULTICAST_IF:
  1349. if (*optlen < sizeof(struct in_addr)) {
  1350. err = EINVAL;
  1351. }
  1352. break;
  1353. case IP_MULTICAST_LOOP:
  1354. if (*optlen < sizeof(u8_t)) {
  1355. err = EINVAL;
  1356. }
  1357. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1358. err = EAFNOSUPPORT;
  1359. }
  1360. break;
  1361. #endif /* LWIP_IGMP */
  1362. default:
  1363. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
  1364. s, optname));
  1365. err = ENOPROTOOPT;
  1366. } /* switch (optname) */
  1367. break;
  1368. #if LWIP_TCP
  1369. /* Level: IPPROTO_TCP */
  1370. case IPPROTO_TCP:
  1371. if (*optlen < sizeof(int)) {
  1372. err = EINVAL;
  1373. break;
  1374. }
  1375. /* If this is no TCP socket, ignore any options. */
  1376. if (sock->conn->type != NETCONN_TCP)
  1377. return 0;
  1378. switch (optname) {
  1379. case TCP_NODELAY:
  1380. case TCP_KEEPALIVE:
  1381. #if LWIP_TCP_KEEPALIVE
  1382. case TCP_KEEPIDLE:
  1383. case TCP_KEEPINTVL:
  1384. case TCP_KEEPCNT:
  1385. #endif /* LWIP_TCP_KEEPALIVE */
  1386. break;
  1387. default:
  1388. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
  1389. s, optname));
  1390. err = ENOPROTOOPT;
  1391. } /* switch (optname) */
  1392. break;
  1393. #endif /* LWIP_TCP */
  1394. #if LWIP_UDP && LWIP_UDPLITE
  1395. /* Level: IPPROTO_UDPLITE */
  1396. case IPPROTO_UDPLITE:
  1397. if (*optlen < sizeof(int)) {
  1398. err = EINVAL;
  1399. break;
  1400. }
  1401. /* If this is no UDP lite socket, ignore any options. */
  1402. if (sock->conn->type != NETCONN_UDPLITE) {
  1403. return 0;
  1404. }
  1405. switch (optname) {
  1406. case UDPLITE_SEND_CSCOV:
  1407. case UDPLITE_RECV_CSCOV:
  1408. break;
  1409. default:
  1410. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1411. s, optname));
  1412. err = ENOPROTOOPT;
  1413. } /* switch (optname) */
  1414. break;
  1415. #endif /* LWIP_UDP && LWIP_UDPLITE*/
  1416. /* UNDEFINED LEVEL */
  1417. default:
  1418. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
  1419. s, level, optname));
  1420. err = ENOPROTOOPT;
  1421. } /* switch */
  1422. if (err != ERR_OK) {
  1423. sock_set_errno(sock, err);
  1424. return -1;
  1425. }
  1426. /* Now do the actual option processing */
  1427. data.sock = sock;
  1428. data.level = level;
  1429. data.optname = optname;
  1430. data.optval = optval;
  1431. data.optlen = optlen;
  1432. data.err = err;
  1433. tcpip_callback(lwip_getsockopt_internal, &data);
  1434. sys_arch_sem_wait(&sock->conn->op_completed, 0);
  1435. /* maybe lwip_getsockopt_internal has changed err */
  1436. err = data.err;
  1437. sock_set_errno(sock, err);
  1438. return err ? -1 : 0;
  1439. }
  1440. static void
  1441. lwip_getsockopt_internal(void *arg)
  1442. {
  1443. struct lwip_sock *sock;
  1444. #ifdef LWIP_DEBUG
  1445. int s;
  1446. #endif /* LWIP_DEBUG */
  1447. int level, optname;
  1448. void *optval;
  1449. struct lwip_setgetsockopt_data *data;
  1450. LWIP_ASSERT("arg != NULL", arg != NULL);
  1451. data = (struct lwip_setgetsockopt_data*)arg;
  1452. sock = data->sock;
  1453. #ifdef LWIP_DEBUG
  1454. s = data->s;
  1455. #endif /* LWIP_DEBUG */
  1456. level = data->level;
  1457. optname = data->optname;
  1458. optval = data->optval;
  1459. switch (level) {
  1460. /* Level: SOL_SOCKET */
  1461. case SOL_SOCKET:
  1462. switch (optname) {
  1463. /* The option flags */
  1464. case SO_ACCEPTCONN:
  1465. case SO_BROADCAST:
  1466. /* UNIMPL case SO_DEBUG: */
  1467. /* UNIMPL case SO_DONTROUTE: */
  1468. case SO_KEEPALIVE:
  1469. /* UNIMPL case SO_OOBINCLUDE: */
  1470. #if SO_REUSE
  1471. case SO_REUSEADDR:
  1472. case SO_REUSEPORT:
  1473. #endif /* SO_REUSE */
  1474. /*case SO_USELOOPBACK: UNIMPL */
  1475. *(int*)optval = sock->conn->pcb.ip->so_options & optname;
  1476. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n",
  1477. s, optname, (*(int*)optval?"on":"off")));
  1478. break;
  1479. case SO_TYPE:
  1480. switch (NETCONNTYPE_GROUP(sock->conn->type)) {
  1481. case NETCONN_RAW:
  1482. *(int*)optval = SOCK_RAW;
  1483. break;
  1484. case NETCONN_TCP:
  1485. *(int*)optval = SOCK_STREAM;
  1486. break;
  1487. case NETCONN_UDP:
  1488. *(int*)optval = SOCK_DGRAM;
  1489. break;
  1490. default: /* unrecognized socket type */
  1491. *(int*)optval = sock->conn->type;
  1492. LWIP_DEBUGF(SOCKETS_DEBUG,
  1493. ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n",
  1494. s, *(int *)optval));
  1495. } /* switch (sock->conn->type) */
  1496. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n",
  1497. s, *(int *)opt

Large files files are truncated, but you can click here to view the full file