3 * Sockets BSD-Like API module
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
37 * Improved by Marc Boucher <marc@mbsi.ca> and David Haas <dhaas@alum.rpi.edu>
43 #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
45 #include "lwip/sockets.h"
48 #include "lwip/igmp.h"
49 #include "lwip/inet.h"
53 #include "lwip/tcpip.h"
55 #include "lwip/arch.h"
59 #define NUM_SOCKETS MEMP_NUM_NETCONN
61 /** Contains all internal pointers and states used for a socket */
63 /** sockets currently are built on netconns, each socket has one netconn */
65 /** data that was left from the previous read */
66 struct netbuf *lastdata;
67 /** offset in the data that was left from the previous read */
69 /** number of times data was received, set by event_callback(),
70 tested by the receive and select functions */
72 /** number of times data was received, set by event_callback(),
75 /** socket flags (currently, only used for O_NONBLOCK) */
77 /** last error that occurred on this socket */
81 /** Description for a task waiting in select */
82 struct lwip_select_cb {
83 /** Pointer to the next waiting task */
84 struct lwip_select_cb *next;
85 /** readset passed to select */
87 /** writeset passed to select */
89 /** unimplemented: exceptset passed to select */
91 /** don't signal the same semaphore twice: set to 1 when signalled */
93 /** semaphore to wake up a task waiting for select */
97 /** This struct is used to pass data to the set/getsockopt_internal
98 * functions running in tcpip_thread context (only a void* is allowed) */
99 struct lwip_setgetsockopt_data {
100 /** socket struct for which to change options */
101 struct lwip_socket *sock;
102 /** socket index for which to change options */
104 /** level of the option to process */
106 /** name of the option to process */
108 /** set: value to set the option to
109 * get: value of the option is stored here */
111 /** size of *optval */
113 /** if an error occures, it is temporarily stored here */
117 /** The global array of available sockets */
118 static struct lwip_socket sockets[NUM_SOCKETS];
119 /** The global list of tasks waiting for select */
120 static struct lwip_select_cb *select_cb_list;
122 /** Semaphore protecting the sockets array */
123 static sys_sem_t socksem;
124 /** Semaphore protecting select_cb_list */
125 static sys_sem_t selectsem;
127 /** Table to quickly map an lwIP error (err_t) to a socket error
128 * by using -err as an index */
129 static const int err_to_errno_table[] = {
130 0, /* ERR_OK 0 No error, everything OK. */
131 ENOMEM, /* ERR_MEM -1 Out of memory error. */
132 ENOBUFS, /* ERR_BUF -2 Buffer error. */
133 EHOSTUNREACH, /* ERR_RTE -3 Routing problem. */
134 ECONNABORTED, /* ERR_ABRT -4 Connection aborted. */
135 ECONNRESET, /* ERR_RST -5 Connection reset. */
136 ESHUTDOWN, /* ERR_CLSD -6 Connection closed. */
137 ENOTCONN, /* ERR_CONN -7 Not connected. */
138 EINVAL, /* ERR_VAL -8 Illegal value. */
139 EIO, /* ERR_ARG -9 Illegal argument. */
140 EADDRINUSE, /* ERR_USE -10 Address in use. */
141 -1, /* ERR_IF -11 Low-level netif error */
142 -1, /* ERR_ISCONN -12 Already connected. */
143 ETIMEDOUT, /* ERR_TIMEOUT -13 Timeout */
144 EINPROGRESS /* ERR_INPROGRESS -14 Operation in progress */
147 #define ERR_TO_ERRNO_TABLE_SIZE \
148 (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0]))
150 #define err_to_errno(err) \
151 ((unsigned)(-(err)) < ERR_TO_ERRNO_TABLE_SIZE ? \
152 err_to_errno_table[-(err)] : EIO)
155 #define set_errno(err) errno = (err)
157 #define set_errno(err)
160 #define sock_set_errno(sk, e) do { \
162 set_errno(sk->err); \
165 /* Forward delcaration of some functions */
166 static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
167 static void lwip_getsockopt_internal(void *arg);
168 static void lwip_setsockopt_internal(void *arg);
171 * Initialize this module. This function has to be called before any other
172 * functions in this module!
175 lwip_socket_init(void)
177 socksem = sys_sem_new(1);
178 selectsem = sys_sem_new(1);
182 * Map a externally used socket index to the internal socket representation.
184 * @param s externally used socket index
185 * @return struct lwip_socket for the socket or NULL if not found
187 static struct lwip_socket *
190 struct lwip_socket *sock;
192 if ((s < 0) || (s >= NUM_SOCKETS)) {
193 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
201 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
210 * Allocate a new socket for a given netconn.
212 * @param newconn the netconn for which to allocate a socket
213 * @return the index of the new socket; -1 on error
216 alloc_socket(struct netconn *newconn)
220 /* Protect socket array */
221 sys_sem_wait(socksem);
223 /* allocate a new socket identifier */
224 for (i = 0; i < NUM_SOCKETS; ++i) {
225 if (!sockets[i].conn) {
226 sockets[i].conn = newconn;
227 sockets[i].lastdata = NULL;
228 sockets[i].lastoffset = 0;
229 sockets[i].rcvevent = 0;
230 sockets[i].sendevent = 1; /* TCP send buf is empty */
231 sockets[i].flags = 0;
233 sys_sem_signal(socksem);
237 sys_sem_signal(socksem);
241 /* Below this, the well-known socket functions are implemented.
242 * Use google.com or opengroup.org to get a good description :-)
244 * Exceptions are documented!
248 lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
250 struct lwip_socket *sock, *nsock;
251 struct netconn *newconn;
252 struct ip_addr naddr;
255 struct sockaddr_in sin;
258 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
259 sock = get_socket(s);
263 newconn = netconn_accept(sock->conn);
265 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) failed, err=%d\n", s, sock->conn->err));
266 sock_set_errno(sock, err_to_errno(sock->conn->err));
270 /* get the IP address and port of the remote host */
271 err = netconn_peer(newconn, &naddr, &port);
273 netconn_delete(newconn);
274 sock_set_errno(sock, err_to_errno(err));
278 memset(&sin, 0, sizeof(sin));
279 sin.sin_len = sizeof(sin);
280 sin.sin_family = AF_INET;
281 sin.sin_port = htons(port);
282 sin.sin_addr.s_addr = naddr.addr;
284 if (*addrlen > sizeof(sin))
285 *addrlen = sizeof(sin);
287 SMEMCPY(addr, &sin, *addrlen);
289 newsock = alloc_socket(newconn);
291 netconn_delete(newconn);
292 sock_set_errno(sock, ENFILE);
295 LWIP_ASSERT("invalid socket index", (newsock >= 0) && (newsock < NUM_SOCKETS));
296 newconn->callback = event_callback;
297 nsock = &sockets[newsock];
298 LWIP_ASSERT("invalid socket pointer", nsock != NULL);
300 sys_sem_wait(socksem);
301 /* See event_callback: If data comes in right away after an accept, even
302 * though the server task might not have created a new socket yet.
303 * In that case, newconn->socket is counted down (newconn->socket--),
304 * so nsock->rcvevent is >= 1 here!
306 nsock->rcvevent += -1 - newconn->socket;
307 newconn->socket = newsock;
308 sys_sem_signal(socksem);
310 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
311 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
312 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", port));
314 sock_set_errno(sock, 0);
319 lwip_bind(int s, struct sockaddr *name, socklen_t namelen)
321 struct lwip_socket *sock;
322 struct ip_addr local_addr;
326 sock = get_socket(s);
330 LWIP_ERROR("lwip_bind: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
331 ((((struct sockaddr_in *)name)->sin_family) == AF_INET)),
332 sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
334 local_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
335 local_port = ((struct sockaddr_in *)name)->sin_port;
337 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
338 ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
339 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(local_port)));
341 err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
344 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
345 sock_set_errno(sock, err_to_errno(err));
349 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
350 sock_set_errno(sock, 0);
357 struct lwip_socket *sock;
359 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
361 sock = get_socket(s);
366 netconn_delete(sock->conn);
368 sys_sem_wait(socksem);
369 if (sock->lastdata) {
370 netbuf_delete(sock->lastdata);
372 sock->lastdata = NULL;
373 sock->lastoffset = 0;
375 sock_set_errno(sock, 0);
376 sys_sem_signal(socksem);
381 lwip_connect(int s, const struct sockaddr *name, socklen_t namelen)
383 struct lwip_socket *sock;
386 sock = get_socket(s);
390 LWIP_ERROR("lwip_connect: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
391 ((((struct sockaddr_in *)name)->sin_family) == AF_INET)),
392 sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
394 if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) {
395 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
396 err = netconn_disconnect(sock->conn);
398 struct ip_addr remote_addr;
401 remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
402 remote_port = ((struct sockaddr_in *)name)->sin_port;
404 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
405 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
406 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port)));
408 err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
412 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
413 sock_set_errno(sock, err_to_errno(err));
417 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
418 sock_set_errno(sock, 0);
423 * Set a socket into listen mode.
424 * The socket may not have been used for another connection previously.
426 * @param s the socket to set to listening mode
427 * @param backlog (ATTENTION: need TCP_LISTEN_BACKLOG=1)
428 * @return 0 on success, non-zero on failure
431 lwip_listen(int s, int backlog)
433 struct lwip_socket *sock;
436 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
438 sock = get_socket(s);
442 /* limit the "backlog" parameter to fit in an u8_t */
446 if (backlog > 0xff) {
450 err = netconn_listen_with_backlog(sock->conn, backlog);
453 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
454 sock_set_errno(sock, err_to_errno(err));
458 sock_set_errno(sock, 0);
463 lwip_recvfrom(int s, void *mem, int len, unsigned int flags,
464 struct sockaddr *from, socklen_t *fromlen)
466 struct lwip_socket *sock;
468 u16_t buflen, copylen, off = 0;
469 struct ip_addr *addr;
473 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %d, 0x%x, ..)\n", s, mem, len, flags));
474 sock = get_socket(s);
479 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: top while sock->lastdata=%p\n", (void*)sock->lastdata));
480 /* Check if there is data left from the last recv operation. */
481 if (sock->lastdata) {
482 buf = sock->lastdata;
484 /* If this is non-blocking call, then check first */
485 if (((flags & MSG_DONTWAIT) || (sock->flags & O_NONBLOCK)) && !sock->rcvevent) {
486 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
487 sock_set_errno(sock, EWOULDBLOCK);
491 /* No data was left from the previous operation, so we try to get
492 some from the network. */
493 sock->lastdata = buf = netconn_recv(sock->conn);
494 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: netconn_recv netbuf=%p\n", (void*)buf));
497 /* We should really do some error checking here. */
498 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL!\n", s));
499 sock_set_errno(sock, (((sock->conn->pcb.ip!=NULL) && (sock->conn->err==ERR_OK))?ETIMEDOUT:err_to_errno(sock->conn->err)));
504 buflen = netbuf_len(buf);
505 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: buflen=%d len=%d off=%d sock->lastoffset=%d\n", buflen, len, off, sock->lastoffset));
507 buflen -= sock->lastoffset;
515 /* copy the contents of the received buffer into
516 the supplied memory pointer mem */
517 netbuf_copy_partial(buf, (u8_t*)mem + off, copylen, sock->lastoffset);
521 if (netconn_type(sock->conn) == NETCONN_TCP) {
523 if ( (len <= 0) || (buf->p->flags & PBUF_FLAG_PUSH) || !sock->rcvevent) {
530 /* If we don't peek the incoming message... */
531 if ((flags & MSG_PEEK)==0) {
532 /* If this is a TCP socket, check if there is data left in the
533 buffer. If so, it should be saved in the sock structure for next
535 if ((sock->conn->type == NETCONN_TCP) && (buflen - copylen > 0)) {
536 sock->lastdata = buf;
537 sock->lastoffset += copylen;
538 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: lastdata now netbuf=%p\n", (void*)buf));
540 sock->lastdata = NULL;
541 sock->lastoffset = 0;
542 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: deleting netbuf=%p\n", (void*)buf));
550 /* Check to see from where the data was.*/
551 if (from && fromlen) {
552 struct sockaddr_in sin;
554 if (netconn_type(sock->conn) == NETCONN_TCP) {
555 addr = (struct ip_addr*)&(sin.sin_addr.s_addr);
556 netconn_getaddr(sock->conn, addr, &port, 0);
558 addr = netbuf_fromaddr(buf);
559 port = netbuf_fromport(buf);
562 memset(&sin, 0, sizeof(sin));
563 sin.sin_len = sizeof(sin);
564 sin.sin_family = AF_INET;
565 sin.sin_port = htons(port);
566 sin.sin_addr.s_addr = addr->addr;
568 if (*fromlen > sizeof(sin))
569 *fromlen = sizeof(sin);
571 SMEMCPY(from, &sin, *fromlen);
573 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
574 ip_addr_debug_print(SOCKETS_DEBUG, addr);
575 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, off));
578 struct sockaddr_in sin;
580 if (netconn_type(sock->conn) == NETCONN_TCP) {
581 addr = (struct ip_addr*)&(sin.sin_addr.s_addr);
582 netconn_getaddr(sock->conn, addr, &port, 0);
584 addr = netbuf_fromaddr(buf);
585 port = netbuf_fromport(buf);
588 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
589 ip_addr_debug_print(SOCKETS_DEBUG, addr);
590 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, off));
591 #endif /* SOCKETS_DEBUG */
594 sock_set_errno(sock, 0);
599 lwip_read(int s, void *mem, int len)
601 return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
605 lwip_recv(int s, void *mem, int len, unsigned int flags)
607 return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
611 lwip_send(int s, const void *data, int size, unsigned int flags)
613 struct lwip_socket *sock;
616 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%d, flags=0x%x)\n",
617 s, data, size, flags));
619 sock = get_socket(s);
623 if (sock->conn->type!=NETCONN_TCP) {
624 #if (LWIP_UDP || LWIP_RAW)
625 return lwip_sendto(s, data, size, flags, NULL, 0);
627 sock_set_errno(sock, err_to_errno(ERR_ARG));
629 #endif /* (LWIP_UDP || LWIP_RAW) */
632 err = netconn_write(sock->conn, data, size, NETCONN_COPY | ((flags & MSG_MORE)?NETCONN_MORE:0));
634 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d size=%d\n", s, err, size));
635 sock_set_errno(sock, err_to_errno(err));
636 return (err==ERR_OK?size:-1);
640 lwip_sendto(int s, const void *data, int size, unsigned int flags,
641 struct sockaddr *to, socklen_t tolen)
643 struct lwip_socket *sock;
644 struct ip_addr remote_addr;
646 #if !LWIP_TCPIP_CORE_LOCKING
651 sock = get_socket(s);
655 if (sock->conn->type==NETCONN_TCP) {
657 return lwip_send(s, data, size, flags);
659 sock_set_errno(sock, err_to_errno(ERR_ARG));
661 #endif /* LWIP_TCP */
664 LWIP_ASSERT("lwip_sendto: size must fit in u16_t",
665 ((size >= 0) && (size <= 0xffff)));
666 LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) ||
667 ((tolen == sizeof(struct sockaddr_in)) &&
668 ((((struct sockaddr_in *)to)->sin_family) == AF_INET))),
669 sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
671 #if LWIP_TCPIP_CORE_LOCKING
672 /* Should only be consider like a sample or a simple way to experiment this option (no check of "to" field...) */
675 p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
679 p->payload = (void*)data;
680 p->len = p->tot_len = size;
682 remote_addr.addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
685 if (sock->conn->type==NETCONN_RAW) {
686 err = sock->conn->err = raw_sendto(sock->conn->pcb.raw, p, &remote_addr);
688 err = sock->conn->err = udp_sendto(sock->conn->pcb.udp, p, &remote_addr, ntohs(((struct sockaddr_in *)to)->sin_port));
696 /* initialize a buffer */
697 buf.p = buf.ptr = NULL;
699 remote_addr.addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
700 remote_port = ntohs(((struct sockaddr_in *)to)->sin_port);
701 buf.addr = &remote_addr;
702 buf.port = remote_port;
704 remote_addr.addr = 0;
710 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, size=%d, flags=0x%x to=",
711 s, data, size, flags));
712 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
713 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", remote_port));
715 /* make the buffer point to the data that should be sent */
716 if ((err = netbuf_ref(&buf, data, size)) == ERR_OK) {
718 err = netconn_send(sock->conn, &buf);
721 /* deallocated the buffer */
725 #endif /* LWIP_TCPIP_CORE_LOCKING */
726 sock_set_errno(sock, err_to_errno(err));
727 return (err==ERR_OK?size:-1);
731 lwip_socket(int domain, int type, int protocol)
733 struct netconn *conn;
736 LWIP_UNUSED_ARG(domain);
738 /* create a netconn */
741 conn = netconn_new_with_proto_and_callback(NETCONN_RAW, (u8_t)protocol, event_callback);
742 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ",
743 domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
746 conn = netconn_new_with_callback( (protocol == IPPROTO_UDPLITE) ?
747 NETCONN_UDPLITE : NETCONN_UDP, event_callback);
748 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ",
749 domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
752 conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
753 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ",
754 domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
757 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n",
758 domain, type, protocol));
764 LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
769 i = alloc_socket(conn);
772 netconn_delete(conn);
777 LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
783 lwip_write(int s, const void *data, int size)
785 return lwip_send(s, data, size, 0);
789 * Go through the readset and writeset lists and see which socket of the sockets
790 * set in the sets has events. On return, readset, writeset and exceptset have
791 * the sockets enabled that had events.
793 * exceptset is not used for now!!!
795 * @param maxfdp1 the highest socket index in the sets
796 * @param readset in: set of sockets to check for read events;
797 * out: set of sockets that had read events
798 * @param writeset in: set of sockets to check for write events;
799 * out: set of sockets that had write events
800 * @param exceptset not yet implemented
801 * @return number of sockets that had events (read+write)
804 lwip_selscan(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset)
807 fd_set lreadset, lwriteset, lexceptset;
808 struct lwip_socket *p_sock;
812 FD_ZERO(&lexceptset);
814 /* Go through each socket in each list to count number of sockets which
816 for(i = 0; i < maxfdp1; i++) {
817 if (FD_ISSET(i, readset)) {
818 /* See if netconn of this socket is ready for read */
819 p_sock = get_socket(i);
820 if (p_sock && (p_sock->lastdata || p_sock->rcvevent)) {
821 FD_SET(i, &lreadset);
822 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
826 if (FD_ISSET(i, writeset)) {
827 /* See if netconn of this socket is ready for write */
828 p_sock = get_socket(i);
829 if (p_sock && p_sock->sendevent) {
830 FD_SET(i, &lwriteset);
831 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
837 *writeset = lwriteset;
845 * Processing exceptset is not yet implemented.
848 lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
849 struct timeval *timeout)
853 fd_set lreadset, lwriteset, lexceptset;
855 struct lwip_select_cb select_cb;
856 struct lwip_select_cb *p_selcb;
858 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%ld tvusec=%ld)\n", maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset, timeout?timeout->tv_sec:-1L, timeout?timeout->tv_usec:-1L));
861 select_cb.readset = readset;
862 select_cb.writeset = writeset;
863 select_cb.exceptset = exceptset;
864 select_cb.sem_signalled = 0;
866 /* Protect ourselves searching through the list */
867 sys_sem_wait(selectsem);
874 lwriteset = *writeset;
878 lexceptset = *exceptset;
880 FD_ZERO(&lexceptset);
882 /* Go through each socket in each list to count number of sockets which
884 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
886 /* If we don't have any current events, then suspend if we are supposed to */
888 if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
889 sys_sem_signal(selectsem);
897 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
903 /* add our semaphore to list */
904 /* We don't actually need any dynamic memory. Our entry on the
905 * list is only valid while we are in this function, so it's ok
906 * to use local variables */
908 select_cb.sem = sys_sem_new(0);
909 /* Note that we are still protected */
910 /* Put this select_cb on top of list */
911 select_cb.next = select_cb_list;
912 select_cb_list = &select_cb;
914 /* Now we can safely unprotect */
915 sys_sem_signal(selectsem);
917 /* Now just wait to be woken */
922 msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
927 i = sys_sem_wait_timeout(select_cb.sem, msectimeout);
929 /* Take us off the list */
930 sys_sem_wait(selectsem);
931 if (select_cb_list == &select_cb)
932 select_cb_list = select_cb.next;
934 for (p_selcb = select_cb_list; p_selcb; p_selcb = p_selcb->next) {
935 if (p_selcb->next == &select_cb) {
936 p_selcb->next = select_cb.next;
941 sys_sem_signal(selectsem);
943 sys_sem_free(select_cb.sem);
953 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
964 lwriteset = *writeset;
968 lexceptset = *exceptset;
970 FD_ZERO(&lexceptset);
973 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
975 sys_sem_signal(selectsem);
980 *writeset = lwriteset;
982 *exceptset = lexceptset;
984 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
991 * Callback registered in the netconn layer for each socket-netconn.
992 * Processes recvevent (data available) and wakes up tasks waiting for select.
995 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
998 struct lwip_socket *sock;
999 struct lwip_select_cb *scb;
1001 LWIP_UNUSED_ARG(len);
1007 /* Data comes in right away after an accept, even though
1008 * the server task might not have created a new socket yet.
1009 * Just count down (or up) if that's the case and we
1010 * will use the data later. Note that only receive events
1011 * can happen before the new socket is set up. */
1012 sys_sem_wait(socksem);
1013 if (conn->socket < 0) {
1014 if (evt == NETCONN_EVT_RCVPLUS) {
1017 sys_sem_signal(socksem);
1020 sys_sem_signal(socksem);
1023 sock = get_socket(s);
1031 sys_sem_wait(selectsem);
1032 /* Set event as required */
1034 case NETCONN_EVT_RCVPLUS:
1037 case NETCONN_EVT_RCVMINUS:
1040 case NETCONN_EVT_SENDPLUS:
1041 sock->sendevent = 1;
1043 case NETCONN_EVT_SENDMINUS:
1044 sock->sendevent = 0;
1047 LWIP_ASSERT("unknown event", 0);
1050 sys_sem_signal(selectsem);
1052 /* Now decide if anyone is waiting for this socket */
1053 /* NOTE: This code is written this way to protect the select link list
1054 but to avoid a deadlock situation by releasing socksem before
1055 signalling for the select. This means we need to go through the list
1056 multiple times ONLY IF a select was actually waiting. We go through
1057 the list the number of waiting select calls + 1. This list is
1058 expected to be small. */
1060 sys_sem_wait(selectsem);
1061 for (scb = select_cb_list; scb; scb = scb->next) {
1062 if (scb->sem_signalled == 0) {
1063 /* Test this select call for our socket */
1064 if (scb->readset && FD_ISSET(s, scb->readset))
1067 if (scb->writeset && FD_ISSET(s, scb->writeset))
1068 if (sock->sendevent)
1073 scb->sem_signalled = 1;
1074 sys_sem_signal(selectsem);
1075 sys_sem_signal(scb->sem);
1077 sys_sem_signal(selectsem);
1084 * Unimplemented: Close one end of a full-duplex connection.
1085 * Currently, the full connection is closed.
1088 lwip_shutdown(int s, int how)
1090 LWIP_UNUSED_ARG(how);
1091 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
1092 return lwip_close(s); /* XXX temporary hack until proper implementation */
1096 lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local)
1098 struct lwip_socket *sock;
1099 struct sockaddr_in sin;
1100 struct ip_addr naddr;
1102 sock = get_socket(s);
1106 memset(&sin, 0, sizeof(sin));
1107 sin.sin_len = sizeof(sin);
1108 sin.sin_family = AF_INET;
1110 /* get the IP address and port */
1111 netconn_getaddr(sock->conn, &naddr, &sin.sin_port, local);
1113 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s));
1114 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
1115 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%d)\n", sin.sin_port));
1117 sin.sin_port = htons(sin.sin_port);
1118 sin.sin_addr.s_addr = naddr.addr;
1120 if (*namelen > sizeof(sin))
1121 *namelen = sizeof(sin);
1123 SMEMCPY(name, &sin, *namelen);
1124 sock_set_errno(sock, 0);
1129 lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
1131 return lwip_getaddrname(s, name, namelen, 0);
1135 lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
1137 return lwip_getaddrname(s, name, namelen, 1);
1141 lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
1144 struct lwip_socket *sock = get_socket(s);
1145 struct lwip_setgetsockopt_data data;
1150 if ((NULL == optval) || (NULL == optlen)) {
1151 sock_set_errno(sock, EFAULT);
1155 /* Do length and type checks for the various options first, to keep it readable. */
1158 /* Level: SOL_SOCKET */
1164 /* UNIMPL case SO_DEBUG: */
1165 /* UNIMPL case SO_DONTROUTE: */
1168 /* UNIMPL case SO_CONTIMEO: */
1169 /* UNIMPL case SO_SNDTIMEO: */
1170 #if LWIP_SO_RCVTIMEO
1172 #endif /* LWIP_SO_RCVTIMEO */
1175 #endif /* LWIP_SO_RCVBUF */
1176 /* UNIMPL case SO_OOBINLINE: */
1177 /* UNIMPL case SO_SNDBUF: */
1178 /* UNIMPL case SO_RCVLOWAT: */
1179 /* UNIMPL case SO_SNDLOWAT: */
1183 #endif /* SO_REUSE */
1185 /* UNIMPL case SO_USELOOPBACK: */
1186 if (*optlen < sizeof(int)) {
1192 if (*optlen < sizeof(int)) {
1196 if ((sock->conn->type != NETCONN_UDP) ||
1197 ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
1198 /* this flag is only available for UDP, not for UDP lite */
1201 #endif /* LWIP_UDP */
1205 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
1208 } /* switch (optname) */
1211 /* Level: IPPROTO_IP */
1214 /* UNIMPL case IP_HDRINCL: */
1215 /* UNIMPL case IP_RCVDSTADDR: */
1216 /* UNIMPL case IP_RCVIF: */
1219 if (*optlen < sizeof(int)) {
1224 case IP_MULTICAST_TTL:
1225 if (*optlen < sizeof(u8_t)) {
1229 case IP_MULTICAST_IF:
1230 if (*optlen < sizeof(struct in_addr)) {
1234 #endif /* LWIP_IGMP */
1237 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
1240 } /* switch (optname) */
1244 /* Level: IPPROTO_TCP */
1246 if (*optlen < sizeof(int)) {
1251 /* If this is no TCP socket, ignore any options. */
1252 if (sock->conn->type != NETCONN_TCP)
1258 #if LWIP_TCP_KEEPALIVE
1262 #endif /* LWIP_TCP_KEEPALIVE */
1266 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
1269 } /* switch (optname) */
1271 #endif /* LWIP_TCP */
1272 #if LWIP_UDP && LWIP_UDPLITE
1273 /* Level: IPPROTO_UDPLITE */
1274 case IPPROTO_UDPLITE:
1275 if (*optlen < sizeof(int)) {
1280 /* If this is no UDP lite socket, ignore any options. */
1281 if (sock->conn->type != NETCONN_UDPLITE)
1285 case UDPLITE_SEND_CSCOV:
1286 case UDPLITE_RECV_CSCOV:
1290 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
1293 } /* switch (optname) */
1295 #endif /* LWIP_UDP && LWIP_UDPLITE*/
1296 /* UNDEFINED LEVEL */
1298 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
1299 s, level, optname));
1304 if (err != ERR_OK) {
1305 sock_set_errno(sock, err);
1309 /* Now do the actual option processing */
1312 data.optname = optname;
1313 data.optval = optval;
1314 data.optlen = optlen;
1316 tcpip_callback(lwip_getsockopt_internal, &data);
1317 sys_arch_sem_wait(sock->conn->op_completed, 0);
1318 /* maybe lwip_getsockopt_internal has changed err */
1321 sock_set_errno(sock, err);
1322 return err ? -1 : 0;
1326 lwip_getsockopt_internal(void *arg)
1328 struct lwip_socket *sock;
1331 #endif /* LWIP_DEBUG */
1334 struct lwip_setgetsockopt_data *data;
1336 LWIP_ASSERT("arg != NULL", arg != NULL);
1338 data = (struct lwip_setgetsockopt_data*)arg;
1342 #endif /* LWIP_DEBUG */
1343 level = data->level;
1344 optname = data->optname;
1345 optval = data->optval;
1349 /* Level: SOL_SOCKET */
1353 /* The option flags */
1356 /* UNIMPL case SO_DEBUG: */
1357 /* UNIMPL case SO_DONTROUTE: */
1359 /* UNIMPL case SO_OOBINCLUDE: */
1363 #endif /* SO_REUSE */
1364 /*case SO_USELOOPBACK: UNIMPL */
1365 *(int*)optval = sock->conn->pcb.ip->so_options & optname;
1366 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n",
1367 s, optname, (*(int*)optval?"on":"off")));
1371 switch (NETCONNTYPE_GROUP(sock->conn->type)) {
1373 *(int*)optval = SOCK_RAW;
1376 *(int*)optval = SOCK_STREAM;
1379 *(int*)optval = SOCK_DGRAM;
1381 default: /* unrecognized socket type */
1382 *(int*)optval = sock->conn->type;
1383 LWIP_DEBUGF(SOCKETS_DEBUG,
1384 ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n",
1385 s, *(int *)optval));
1386 } /* switch (sock->conn->type) */
1387 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n",
1388 s, *(int *)optval));
1392 if (sock->err == 0) {
1393 sock_set_errno(sock, err_to_errno(sock->conn->err));
1395 *(int *)optval = sock->err;
1397 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n",
1398 s, *(int *)optval));
1401 #if LWIP_SO_RCVTIMEO
1403 *(int *)optval = sock->conn->recv_timeout;
1405 #endif /* LWIP_SO_RCVTIMEO */
1408 *(int *)optval = sock->conn->recv_bufsize;
1410 #endif /* LWIP_SO_RCVBUF */
1413 *(int*)optval = (udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_NOCHKSUM) ? 1 : 0;
1415 #endif /* LWIP_UDP*/
1416 } /* switch (optname) */
1419 /* Level: IPPROTO_IP */
1423 *(int*)optval = sock->conn->pcb.ip->ttl;
1424 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n",
1425 s, *(int *)optval));
1428 *(int*)optval = sock->conn->pcb.ip->tos;
1429 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n",
1430 s, *(int *)optval));
1433 case IP_MULTICAST_TTL:
1434 *(u8_t*)optval = sock->conn->pcb.ip->ttl;
1435 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n",
1436 s, *(int *)optval));
1438 case IP_MULTICAST_IF:
1439 ((struct in_addr*) optval)->s_addr = sock->conn->pcb.udp->multicast_ip.addr;
1440 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%x\n",
1441 s, *(u32_t *)optval));
1443 #endif /* LWIP_IGMP */
1444 } /* switch (optname) */
1448 /* Level: IPPROTO_TCP */
1452 *(int*)optval = (sock->conn->pcb.tcp->flags & TF_NODELAY);
1453 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n",
1454 s, (*(int*)optval)?"on":"off") );
1457 *(int*)optval = (int)sock->conn->pcb.tcp->keep_idle;
1458 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPALIVE) = %d\n",
1459 s, *(int *)optval));
1462 #if LWIP_TCP_KEEPALIVE
1464 *(int*)optval = (int)(sock->conn->pcb.tcp->keep_idle/1000);
1465 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPIDLE) = %d\n",
1466 s, *(int *)optval));
1469 *(int*)optval = (int)(sock->conn->pcb.tcp->keep_intvl/1000);
1470 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPINTVL) = %d\n",
1471 s, *(int *)optval));
1474 *(int*)optval = (int)sock->conn->pcb.tcp->keep_cnt;
1475 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPCNT) = %d\n",
1476 s, *(int *)optval));
1478 #endif /* LWIP_TCP_KEEPALIVE */
1480 } /* switch (optname) */
1482 #endif /* LWIP_TCP */
1483 #if LWIP_UDP && LWIP_UDPLITE
1484 /* Level: IPPROTO_UDPLITE */
1485 case IPPROTO_UDPLITE:
1487 case UDPLITE_SEND_CSCOV:
1488 *(int*)optval = sock->conn->pcb.udp->chksum_len_tx;
1489 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
1490 s, (*(int*)optval)) );
1492 case UDPLITE_RECV_CSCOV:
1493 *(int*)optval = sock->conn->pcb.udp->chksum_len_rx;
1494 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
1495 s, (*(int*)optval)) );
1497 } /* switch (optname) */
1499 #endif /* LWIP_UDP */
1500 } /* switch (level) */
1501 sys_sem_signal(sock->conn->op_completed);
1505 lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
1507 struct lwip_socket *sock = get_socket(s);
1509 struct lwip_setgetsockopt_data data;
1514 if (NULL == optval) {
1515 sock_set_errno(sock, EFAULT);
1519 /* Do length and type checks for the various options first, to keep it readable. */
1522 /* Level: SOL_SOCKET */
1527 /* UNIMPL case SO_DEBUG: */
1528 /* UNIMPL case SO_DONTROUTE: */
1530 /* UNIMPL case case SO_CONTIMEO: */
1531 /* UNIMPL case case SO_SNDTIMEO: */
1532 #if LWIP_SO_RCVTIMEO
1534 #endif /* LWIP_SO_RCVTIMEO */
1537 #endif /* LWIP_SO_RCVBUF */
1538 /* UNIMPL case SO_OOBINLINE: */
1539 /* UNIMPL case SO_SNDBUF: */
1540 /* UNIMPL case SO_RCVLOWAT: */
1541 /* UNIMPL case SO_SNDLOWAT: */
1545 #endif /* SO_REUSE */
1546 /* UNIMPL case SO_USELOOPBACK: */
1547 if (optlen < sizeof(int)) {
1552 if (optlen < sizeof(int)) {
1556 if ((sock->conn->type != NETCONN_UDP) ||
1557 ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
1558 /* this flag is only available for UDP, not for UDP lite */
1561 #endif /* LWIP_UDP */
1564 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
1567 } /* switch (optname) */
1570 /* Level: IPPROTO_IP */
1573 /* UNIMPL case IP_HDRINCL: */
1574 /* UNIMPL case IP_RCVDSTADDR: */
1575 /* UNIMPL case IP_RCVIF: */
1578 if (optlen < sizeof(int)) {
1583 case IP_MULTICAST_TTL:
1584 if (optlen < sizeof(u8_t)) {
1587 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
1591 case IP_MULTICAST_IF:
1592 if (optlen < sizeof(struct in_addr)) {
1595 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
1599 case IP_ADD_MEMBERSHIP:
1600 case IP_DROP_MEMBERSHIP:
1601 if (optlen < sizeof(struct ip_mreq)) {
1604 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
1608 #endif /* LWIP_IGMP */
1610 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
1613 } /* switch (optname) */
1617 /* Level: IPPROTO_TCP */
1619 if (optlen < sizeof(int)) {
1624 /* If this is no TCP socket, ignore any options. */
1625 if (sock->conn->type != NETCONN_TCP)
1631 #if LWIP_TCP_KEEPALIVE
1635 #endif /* LWIP_TCP_KEEPALIVE */
1639 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
1642 } /* switch (optname) */
1644 #endif /* LWIP_TCP */
1645 #if LWIP_UDP && LWIP_UDPLITE
1646 /* Level: IPPROTO_UDPLITE */
1647 case IPPROTO_UDPLITE:
1648 if (optlen < sizeof(int)) {
1653 /* If this is no UDP lite socket, ignore any options. */
1654 if (sock->conn->type != NETCONN_UDPLITE)
1658 case UDPLITE_SEND_CSCOV:
1659 case UDPLITE_RECV_CSCOV:
1663 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
1666 } /* switch (optname) */
1668 #endif /* LWIP_UDP && LWIP_UDPLITE */
1669 /* UNDEFINED LEVEL */
1671 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
1672 s, level, optname));
1674 } /* switch (level) */
1677 if (err != ERR_OK) {
1678 sock_set_errno(sock, err);
1683 /* Now do the actual option processing */
1686 data.optname = optname;
1687 data.optval = (void*)optval;
1688 data.optlen = &optlen;
1690 tcpip_callback(lwip_setsockopt_internal, &data);
1691 sys_arch_sem_wait(sock->conn->op_completed, 0);
1692 /* maybe lwip_setsockopt_internal has changed err */
1695 sock_set_errno(sock, err);
1696 return err ? -1 : 0;
1700 lwip_setsockopt_internal(void *arg)
1702 struct lwip_socket *sock;
1705 #endif /* LWIP_DEBUG */
1708 struct lwip_setgetsockopt_data *data;
1710 LWIP_ASSERT("arg != NULL", arg != NULL);
1712 data = (struct lwip_setgetsockopt_data*)arg;
1716 #endif /* LWIP_DEBUG */
1717 level = data->level;
1718 optname = data->optname;
1719 optval = data->optval;
1723 /* Level: SOL_SOCKET */
1727 /* The option flags */
1729 /* UNIMPL case SO_DEBUG: */
1730 /* UNIMPL case SO_DONTROUTE: */
1732 /* UNIMPL case SO_OOBINCLUDE: */
1736 #endif /* SO_REUSE */
1737 /* UNIMPL case SO_USELOOPBACK: */
1738 if (*(int*)optval) {
1739 sock->conn->pcb.ip->so_options |= optname;
1741 sock->conn->pcb.ip->so_options &= ~optname;
1743 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n",
1744 s, optname, (*(int*)optval?"on":"off")));
1746 #if LWIP_SO_RCVTIMEO
1748 sock->conn->recv_timeout = ( *(int*)optval );
1750 #endif /* LWIP_SO_RCVTIMEO */
1753 sock->conn->recv_bufsize = ( *(int*)optval );
1755 #endif /* LWIP_SO_RCVBUF */
1758 if (*(int*)optval) {
1759 udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_NOCHKSUM);
1761 udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_NOCHKSUM);
1764 #endif /* LWIP_UDP */
1765 } /* switch (optname) */
1768 /* Level: IPPROTO_IP */
1772 sock->conn->pcb.ip->ttl = (u8_t)(*(int*)optval);
1773 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %u\n",
1774 s, sock->conn->pcb.ip->ttl));
1777 sock->conn->pcb.ip->tos = (u8_t)(*(int*)optval);
1778 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %u\n",
1779 s, sock->conn->pcb.ip->tos));
1782 case IP_MULTICAST_TTL:
1783 sock->conn->pcb.udp->ttl = (u8_t)(*(u8_t*)optval);
1785 case IP_MULTICAST_IF:
1786 sock->conn->pcb.udp->multicast_ip.addr = ((struct in_addr*) optval)->s_addr;
1788 case IP_ADD_MEMBERSHIP:
1789 case IP_DROP_MEMBERSHIP:
1791 /* If this is a TCP or a RAW socket, ignore these options. */
1792 struct ip_mreq *imr = (struct ip_mreq *)optval;
1793 if(optname == IP_ADD_MEMBERSHIP){
1794 data->err = igmp_joingroup((struct ip_addr*)&(imr->imr_interface.s_addr), (struct ip_addr*)&(imr->imr_multiaddr.s_addr));
1796 data->err = igmp_leavegroup((struct ip_addr*)&(imr->imr_interface.s_addr), (struct ip_addr*)&(imr->imr_multiaddr.s_addr));
1798 if(data->err != ERR_OK) {
1799 data->err = EADDRNOTAVAIL;
1803 #endif /* LWIP_IGMP */
1804 } /* switch (optname) */
1808 /* Level: IPPROTO_TCP */
1812 if (*(int*)optval) {
1813 sock->conn->pcb.tcp->flags |= TF_NODELAY;
1815 sock->conn->pcb.tcp->flags &= ~TF_NODELAY;
1817 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n",
1818 s, (*(int *)optval)?"on":"off") );
1821 sock->conn->pcb.tcp->keep_idle = (u32_t)(*(int*)optval);
1822 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %lu\n",
1823 s, sock->conn->pcb.tcp->keep_idle));
1826 #if LWIP_TCP_KEEPALIVE
1828 sock->conn->pcb.tcp->keep_idle = 1000*(u32_t)(*(int*)optval);
1829 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %lu\n",
1830 s, sock->conn->pcb.tcp->keep_idle));
1833 sock->conn->pcb.tcp->keep_intvl = 1000*(u32_t)(*(int*)optval);
1834 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %lu\n",
1835 s, sock->conn->pcb.tcp->keep_intvl));
1838 sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(int*)optval);
1839 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %lu\n",
1840 s, sock->conn->pcb.tcp->keep_cnt));
1842 #endif /* LWIP_TCP_KEEPALIVE */
1844 } /* switch (optname) */
1846 #endif /* LWIP_TCP*/
1847 #if LWIP_UDP && LWIP_UDPLITE
1848 /* Level: IPPROTO_UDPLITE */
1849 case IPPROTO_UDPLITE:
1851 case UDPLITE_SEND_CSCOV:
1852 if ((*(int*)optval != 0) && (*(int*)optval < 8)) {
1853 /* don't allow illegal values! */
1854 sock->conn->pcb.udp->chksum_len_tx = 8;
1856 sock->conn->pcb.udp->chksum_len_tx = *(int*)optval;
1858 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n",
1859 s, (*(int*)optval)) );
1861 case UDPLITE_RECV_CSCOV:
1862 if ((*(int*)optval != 0) && (*(int*)optval < 8)) {
1863 /* don't allow illegal values! */
1864 sock->conn->pcb.udp->chksum_len_rx = 8;
1866 sock->conn->pcb.udp->chksum_len_rx = *(int*)optval;
1868 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n",
1869 s, (*(int*)optval)) );
1871 } /* switch (optname) */
1873 #endif /* LWIP_UDP */
1874 } /* switch (level) */
1875 sys_sem_signal(sock->conn->op_completed);
1879 lwip_ioctl(int s, long cmd, void *argp)
1881 struct lwip_socket *sock = get_socket(s);
1890 sock_set_errno(sock, EINVAL);
1894 SYS_ARCH_GET(sock->conn->recv_avail, *((u16_t*)argp));
1896 /* Check if there is data left from the last recv operation. /maq 041215 */
1897 if (sock->lastdata) {
1898 buflen = netbuf_len(sock->lastdata);
1899 buflen -= sock->lastoffset;
1901 *((u16_t*)argp) += buflen;
1904 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %u\n", s, argp, *((u16_t*)argp)));
1905 sock_set_errno(sock, 0);
1909 if (argp && *(u32_t*)argp)
1910 sock->flags |= O_NONBLOCK;
1912 sock->flags &= ~O_NONBLOCK;
1913 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, !!(sock->flags & O_NONBLOCK)));
1914 sock_set_errno(sock, 0);
1918 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
1919 sock_set_errno(sock, ENOSYS); /* not yet implemented */
1921 } /* switch (cmd) */
1924 #endif /* LWIP_SOCKET */