2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
31 * Improved by Marc Boucher <marc@mbsi.ca> and David Haas <dhaas@alum.rpi.edu>
37 #include "lwip/arch.h"
40 #define LWIP_TIMEVAL_PRIVATE
41 #include "lwip/sockets.h"
43 #define NUM_SOCKETS MEMP_NUM_NETCONN
47 struct netbuf *lastdata;
57 struct lwip_select_cb *next;
65 static struct lwip_socket sockets[NUM_SOCKETS];
66 static struct lwip_select_cb *select_cb_list = 0;
68 static sys_sem_t socksem = 0;
69 static sys_sem_t selectsem = 0;
72 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
74 static int err_to_errno_table[11] = {
75 0, /* ERR_OK 0 No error, everything OK. */
76 ENOMEM, /* ERR_MEM -1 Out of memory error. */
77 ENOBUFS, /* ERR_BUF -2 Buffer error. */
78 ECONNABORTED, /* ERR_ABRT -3 Connection aborted. */
79 ECONNRESET, /* ERR_RST -4 Connection reset. */
80 ESHUTDOWN, /* ERR_CLSD -5 Connection closed. */
81 ENOTCONN, /* ERR_CONN -6 Not connected. */
82 EINVAL, /* ERR_VAL -7 Illegal value. */
83 EIO, /* ERR_ARG -8 Illegal argument. */
84 EHOSTUNREACH, /* ERR_RTE -9 Routing problem. */
85 EADDRINUSE /* ERR_USE -10 Address in use. */
88 #define err_to_errno(err) \
89 ((err) < (sizeof(err_to_errno_table)/sizeof(int))) ? \
90 err_to_errno_table[-(err)] : EIO
93 #define set_errno(err) errno = (err)
95 #define set_errno(err)
98 #define sock_set_errno(sk, e) do { \
100 set_errno(sk->err); \
104 static struct lwip_socket *
107 struct lwip_socket *sock;
109 if ((s < 0) || (s > NUM_SOCKETS)) {
110 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
118 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
127 alloc_socket(struct netconn *newconn)
132 socksem = sys_sem_new(1);
134 /* Protect socket array */
135 sys_sem_wait(socksem);
137 /* allocate a new socket identifier */
138 for(i = 0; i < NUM_SOCKETS; ++i) {
139 if (!sockets[i].conn) {
140 sockets[i].conn = newconn;
141 sockets[i].lastdata = NULL;
142 sockets[i].lastoffset = 0;
143 sockets[i].rcvevent = 0;
144 sockets[i].sendevent = 1; /* TCP send buf is empty */
145 sockets[i].flags = 0;
147 sys_sem_signal(socksem);
151 sys_sem_signal(socksem);
156 lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
158 struct lwip_socket *sock;
159 struct netconn *newconn;
160 struct ip_addr naddr;
163 struct sockaddr_in sin;
165 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
166 sock = get_socket(s);
172 newconn = netconn_accept(sock->conn);
174 /* get the IP address and port of the remote host */
175 netconn_peer(newconn, &naddr, &port);
177 memset(&sin, 0, sizeof(sin));
178 sin.sin_len = sizeof(sin);
179 sin.sin_family = AF_INET;
180 sin.sin_port = htons(port);
181 sin.sin_addr.s_addr = naddr.addr;
183 if (*addrlen > sizeof(sin))
184 *addrlen = sizeof(sin);
186 memcpy(addr, &sin, *addrlen);
188 newsock = alloc_socket(newconn);
190 netconn_delete(newconn);
191 sock_set_errno(sock, ENOBUFS);
194 newconn->callback = event_callback;
195 sock = get_socket(newsock);
197 sys_sem_wait(socksem);
198 sock->rcvevent += -1 - newconn->socket;
199 newconn->socket = newsock;
200 sys_sem_signal(socksem);
202 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
203 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
204 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", port));
206 sock_set_errno(sock, 0);
211 lwip_bind(int s, struct sockaddr *name, socklen_t namelen)
213 struct lwip_socket *sock;
214 struct ip_addr local_addr;
218 sock = get_socket(s);
224 local_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
225 local_port = ((struct sockaddr_in *)name)->sin_port;
227 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
228 ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
229 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(local_port)));
231 err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
234 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
235 sock_set_errno(sock, err_to_errno(err));
239 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
240 sock_set_errno(sock, 0);
247 struct lwip_socket *sock;
249 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
251 socksem = sys_sem_new(1);
253 /* We cannot allow multiple closes of the same socket. */
254 sys_sem_wait(socksem);
256 sock = get_socket(s);
258 sys_sem_signal(socksem);
263 netconn_delete(sock->conn);
264 if (sock->lastdata) {
265 netbuf_delete(sock->lastdata);
267 sock->lastdata = NULL;
268 sock->lastoffset = 0;
270 sys_sem_signal(socksem);
271 sock_set_errno(sock, 0);
276 lwip_connect(int s, struct sockaddr *name, socklen_t namelen)
278 struct lwip_socket *sock;
281 sock = get_socket(s);
287 if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) {
288 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
289 err = netconn_disconnect(sock->conn);
291 struct ip_addr remote_addr;
294 remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
295 remote_port = ((struct sockaddr_in *)name)->sin_port;
297 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
298 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
299 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port)));
301 err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
305 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
306 sock_set_errno(sock, err_to_errno(err));
310 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
311 sock_set_errno(sock, 0);
316 lwip_listen(int s, int backlog)
318 struct lwip_socket *sock;
321 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
322 sock = get_socket(s);
328 err = netconn_listen(sock->conn);
331 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
332 sock_set_errno(sock, err_to_errno(err));
336 sock_set_errno(sock, 0);
341 lwip_recvfrom(int s, void *mem, int len, unsigned int flags,
342 struct sockaddr *from, socklen_t *fromlen)
344 struct lwip_socket *sock;
346 u16_t buflen, copylen;
347 struct ip_addr *addr;
351 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %d, 0x%x, ..)\n", s, mem, len, flags));
352 sock = get_socket(s);
358 /* Check if there is data left from the last recv operation. */
359 if (sock->lastdata) {
360 buf = sock->lastdata;
362 /* If this is non-blocking call, then check first */
363 if (((flags & MSG_DONTWAIT) || (sock->flags & O_NONBLOCK))
366 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
367 sock_set_errno(sock, EWOULDBLOCK);
371 /* No data was left from the previous operation, so we try to get
372 some from the network. */
373 buf = netconn_recv(sock->conn);
376 /* We should really do some error checking here. */
377 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL!\n", s));
378 sock_set_errno(sock, 0);
383 buflen = netbuf_len(buf);
385 buflen -= sock->lastoffset;
393 /* copy the contents of the received buffer into
394 the supplied memory pointer mem */
395 netbuf_copy_partial(buf, mem, copylen, sock->lastoffset);
397 /* Check to see from where the data was. */
398 if (from && fromlen) {
399 struct sockaddr_in sin;
401 addr = netbuf_fromaddr(buf);
402 port = netbuf_fromport(buf);
404 memset(&sin, 0, sizeof(sin));
405 sin.sin_len = sizeof(sin);
406 sin.sin_family = AF_INET;
407 sin.sin_port = htons(port);
408 sin.sin_addr.s_addr = addr->addr;
410 if (*fromlen > sizeof(sin))
411 *fromlen = sizeof(sin);
413 memcpy(from, &sin, *fromlen);
415 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
416 ip_addr_debug_print(SOCKETS_DEBUG, addr);
417 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, copylen));
419 #if SOCKETS_DEBUG > 0
420 addr = netbuf_fromaddr(buf);
421 port = netbuf_fromport(buf);
423 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
424 ip_addr_debug_print(SOCKETS_DEBUG, addr);
425 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, copylen));
430 /* If this is a TCP socket, check if there is data left in the
431 buffer. If so, it should be saved in the sock structure for next
433 if (netconn_type(sock->conn) == NETCONN_TCP && buflen - copylen > 0) {
434 sock->lastdata = buf;
435 sock->lastoffset += copylen;
437 sock->lastdata = NULL;
438 sock->lastoffset = 0;
443 sock_set_errno(sock, 0);
448 lwip_read(int s, void *mem, int len)
450 return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
454 lwip_recv(int s, void *mem, int len, unsigned int flags)
456 return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
460 lwip_send(int s, void *data, int size, unsigned int flags)
462 struct lwip_socket *sock;
466 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%d, flags=0x%x)\n", s, data, size, flags));
468 sock = get_socket(s);
474 switch (netconn_type(sock->conn)) {
477 case NETCONN_UDPLITE:
478 case NETCONN_UDPNOCHKSUM:
479 /* create a buffer */
483 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ENOBUFS\n", s));
484 sock_set_errno(sock, ENOBUFS);
488 /* make the buffer point to the data that should
490 netbuf_ref(buf, data, size);
493 err = netconn_send(sock->conn, buf);
495 /* deallocated the buffer */
499 err = netconn_write(sock->conn, data, size, NETCONN_COPY);
506 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d\n", s, err));
507 sock_set_errno(sock, err_to_errno(err));
511 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ok size=%d\n", s, size));
512 sock_set_errno(sock, 0);
517 lwip_sendto(int s, void *data, int size, unsigned int flags,
518 struct sockaddr *to, socklen_t tolen)
520 struct lwip_socket *sock;
521 struct ip_addr remote_addr, addr;
522 u16_t remote_port, port;
525 sock = get_socket(s);
531 /* get the peer if currently connected */
532 connected = (netconn_peer(sock->conn, &addr, &port) == ERR_OK);
534 remote_addr.addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
535 remote_port = ((struct sockaddr_in *)to)->sin_port;
537 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, size=%d, flags=0x%x to=", s, data, size, flags));
538 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
539 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", ntohs(remote_port)));
541 netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
543 ret = lwip_send(s, data, size, flags);
545 /* reset the remote address and port number
548 netconn_connect(sock->conn, &addr, port);
550 netconn_disconnect(sock->conn);
555 lwip_socket(int domain, int type, int protocol)
557 struct netconn *conn;
560 /* create a netconn */
563 conn = netconn_new_with_proto_and_callback(NETCONN_RAW, protocol, event_callback);
564 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
567 conn = netconn_new_with_callback(NETCONN_UDP, event_callback);
568 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
571 conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
572 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
575 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n", domain, type, protocol));
581 LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
586 i = alloc_socket(conn);
589 netconn_delete(conn);
594 LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
600 lwip_write(int s, void *data, int size)
602 return lwip_send(s, data, size, 0);
607 lwip_selscan(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset)
610 fd_set lreadset, lwriteset, lexceptset;
611 struct lwip_socket *p_sock;
615 FD_ZERO(&lexceptset);
617 /* Go through each socket in each list to count number of sockets which
619 for(i = 0; i < maxfdp1; i++)
621 if (FD_ISSET(i, readset))
623 /* See if netconn of this socket is ready for read */
624 p_sock = get_socket(i);
625 if (p_sock && (p_sock->lastdata || p_sock->rcvevent))
627 FD_SET(i, &lreadset);
628 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
632 if (FD_ISSET(i, writeset))
634 /* See if netconn of this socket is ready for write */
635 p_sock = get_socket(i);
636 if (p_sock && p_sock->sendevent)
638 FD_SET(i, &lwriteset);
639 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
645 *writeset = lwriteset;
654 lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
655 struct timeval *timeout)
659 fd_set lreadset, lwriteset, lexceptset;
661 struct lwip_select_cb select_cb;
662 struct lwip_select_cb *p_selcb;
664 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));
667 select_cb.readset = readset;
668 select_cb.writeset = writeset;
669 select_cb.exceptset = exceptset;
670 select_cb.sem_signalled = 0;
672 /* Protect ourselves searching through the list */
674 selectsem = sys_sem_new(1);
675 sys_sem_wait(selectsem);
682 lwriteset = *writeset;
686 lexceptset = *exceptset;
688 FD_ZERO(&lexceptset);
690 /* Go through each socket in each list to count number of sockets which
692 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
694 /* If we don't have any current events, then suspend if we are supposed to */
697 if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0)
699 sys_sem_signal(selectsem);
707 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
713 /* add our semaphore to list */
714 /* We don't actually need any dynamic memory. Our entry on the
715 * list is only valid while we are in this function, so it's ok
716 * to use local variables */
718 select_cb.sem = sys_sem_new(0);
719 /* Note that we are still protected */
720 /* Put this select_cb on top of list */
721 select_cb.next = select_cb_list;
722 select_cb_list = &select_cb;
724 /* Now we can safely unprotect */
725 sys_sem_signal(selectsem);
727 /* Now just wait to be woken */
732 msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
734 i = sys_sem_wait_timeout(select_cb.sem, msectimeout);
736 /* Take us off the list */
737 sys_sem_wait(selectsem);
738 if (select_cb_list == &select_cb)
739 select_cb_list = select_cb.next;
741 for (p_selcb = select_cb_list; p_selcb; p_selcb = p_selcb->next)
742 if (p_selcb->next == &select_cb)
744 p_selcb->next = select_cb.next;
748 sys_sem_signal(selectsem);
750 sys_sem_free(select_cb.sem);
751 if (i == 0) /* Timeout */
760 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
771 lwriteset = *writeset;
775 lexceptset = *exceptset;
777 FD_ZERO(&lexceptset);
780 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
783 sys_sem_signal(selectsem);
788 *writeset = lwriteset;
790 *exceptset = lexceptset;
792 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
800 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
803 struct lwip_socket *sock;
804 struct lwip_select_cb *scb;
812 /* Data comes in right away after an accept, even though
813 * the server task might not have created a new socket yet.
814 * Just count down (or up) if that's the case and we
815 * will use the data later. Note that only receive events
816 * can happen before the new socket is set up. */
817 if (evt == NETCONN_EVT_RCVPLUS)
822 sock = get_socket(s);
830 selectsem = sys_sem_new(1);
832 sys_sem_wait(selectsem);
833 /* Set event as required */
836 case NETCONN_EVT_RCVPLUS:
839 case NETCONN_EVT_RCVMINUS:
842 case NETCONN_EVT_SENDPLUS:
845 case NETCONN_EVT_SENDMINUS:
849 sys_sem_signal(selectsem);
851 /* Now decide if anyone is waiting for this socket */
852 /* NOTE: This code is written this way to protect the select link list
853 but to avoid a deadlock situation by releasing socksem before
854 signalling for the select. This means we need to go through the list
855 multiple times ONLY IF a select was actually waiting. We go through
856 the list the number of waiting select calls + 1. This list is
857 expected to be small. */
860 sys_sem_wait(selectsem);
861 for (scb = select_cb_list; scb; scb = scb->next)
863 if (scb->sem_signalled == 0)
865 /* Test this select call for our socket */
866 if (scb->readset && FD_ISSET(s, scb->readset))
869 if (scb->writeset && FD_ISSET(s, scb->writeset))
876 scb->sem_signalled = 1;
877 sys_sem_signal(selectsem);
878 sys_sem_signal(scb->sem);
880 sys_sem_signal(selectsem);
890 int lwip_shutdown(int s, int how)
892 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
893 return lwip_close(s); /* XXX temporary hack until proper implementation */
896 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen)
898 struct lwip_socket *sock;
899 struct sockaddr_in sin;
900 struct ip_addr naddr;
902 sock = get_socket(s);
908 memset(&sin, 0, sizeof(sin));
909 sin.sin_len = sizeof(sin);
910 sin.sin_family = AF_INET;
912 /* get the IP address and port of the remote host */
913 netconn_peer(sock->conn, &naddr, &sin.sin_port);
915 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getpeername(%d, addr=", s));
916 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
917 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%d)\n", sin.sin_port));
919 sin.sin_port = htons(sin.sin_port);
920 sin.sin_addr.s_addr = naddr.addr;
922 if (*namelen > sizeof(sin))
923 *namelen = sizeof(sin);
925 memcpy(name, &sin, *namelen);
926 sock_set_errno(sock, 0);
930 int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen)
932 struct lwip_socket *sock;
933 struct sockaddr_in sin;
934 struct ip_addr *naddr;
936 sock = get_socket(s);
942 memset(&sin, 0, sizeof(sin));
943 sin.sin_len = sizeof(sin);
944 sin.sin_family = AF_INET;
946 /* get the IP address and port of the remote host */
947 netconn_addr(sock->conn, &naddr, &sin.sin_port);
949 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockname(%d, addr=", s));
950 ip_addr_debug_print(SOCKETS_DEBUG, naddr);
951 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%d)\n", sin.sin_port));
953 sin.sin_port = htons(sin.sin_port);
954 sin.sin_addr.s_addr = naddr->addr;
956 if (*namelen > sizeof(sin))
957 *namelen = sizeof(sin);
959 memcpy(name, &sin, *namelen);
960 sock_set_errno(sock, 0);
964 int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
967 struct lwip_socket *sock = get_socket(s);
974 if( NULL == optval || NULL == optlen ) {
975 sock_set_errno( sock, EFAULT );
979 /* Do length and type checks for the various options first, to keep it readable. */
982 /* Level: SOL_SOCKET */
988 /* UNIMPL case SO_DEBUG: */
989 /* UNIMPL case SO_DONTROUTE: */
992 /* UNIMPL case SO_OOBINLINE: */
993 /* UNIMPL case SO_RCVBUF: */
994 /* UNIMPL case SO_SNDBUF: */
995 /* UNIMPL case SO_RCVLOWAT: */
996 /* UNIMPL case SO_SNDLOWAT: */
1000 #endif /* SO_REUSE */
1002 /* UNIMPL case SO_USELOOPBACK: */
1003 if( *optlen < sizeof(int) ) {
1009 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname));
1014 /* Level: IPPROTO_IP */
1017 /* UNIMPL case IP_HDRINCL: */
1018 /* UNIMPL case IP_RCVDSTADDR: */
1019 /* UNIMPL case IP_RCVIF: */
1022 if( *optlen < sizeof(int) ) {
1028 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1033 /* Level: IPPROTO_TCP */
1035 if( *optlen < sizeof(int) ) {
1040 /* If this is no TCP socket, ignore any options. */
1041 if ( sock->conn->type != NETCONN_TCP ) return 0;
1049 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1054 /* UNDEFINED LEVEL */
1056 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname));
1062 sock_set_errno(sock, err);
1068 /* Now do the actual option processing */
1072 /* Level: SOL_SOCKET */
1076 /* The option flags */
1079 /* UNIMPL case SO_DEBUG: */
1080 /* UNIMPL case SO_DONTROUTE: */
1082 /* UNIMPL case SO_OOBINCLUDE: */
1086 #endif /* SO_REUSE */
1087 /*case SO_USELOOPBACK: UNIMPL */
1088 *(int*)optval = sock->conn->pcb.tcp->so_options & optname;
1089 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n", s, optname, (*(int*)optval?"on":"off")));
1093 switch (sock->conn->type) {
1095 *(int*)optval = SOCK_RAW;
1098 *(int*)optval = SOCK_STREAM;
1101 case NETCONN_UDPLITE:
1102 case NETCONN_UDPNOCHKSUM:
1103 *(int*)optval = SOCK_DGRAM;
1105 default: /* unrecognized socket type */
1106 *(int*)optval = sock->conn->type;
1107 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n", s, *(int *)optval));
1109 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n", s, *(int *)optval));
1113 *(int *)optval = sock->err;
1115 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n", s, *(int *)optval));
1120 /* Level: IPPROTO_IP */
1124 *(int*)optval = sock->conn->pcb.tcp->ttl;
1125 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n", s, *(int *)optval));
1128 *(int*)optval = sock->conn->pcb.tcp->tos;
1129 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n", s, *(int *)optval));
1134 /* Level: IPPROTO_TCP */
1138 *(int*)optval = (sock->conn->pcb.tcp->flags & TF_NODELAY);
1139 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n", s, (*(int*)optval)?"on":"off") );
1142 *(int*)optval = sock->conn->pcb.tcp->keepalive;
1143 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPALIVE) = %d\n", s, *(int *)optval));
1150 sock_set_errno(sock, err);
1151 return err ? -1 : 0;
1154 int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
1156 struct lwip_socket *sock = get_socket(s);
1164 if( NULL == optval ) {
1165 sock_set_errno( sock, EFAULT );
1170 /* Do length and type checks for the various options first, to keep it readable. */
1173 /* Level: SOL_SOCKET */
1178 /* UNIMPL case SO_DEBUG: */
1179 /* UNIMPL case SO_DONTROUTE: */
1181 /* UNIMPL case SO_OOBINLINE: */
1182 /* UNIMPL case SO_RCVBUF: */
1183 /* UNIMPL case SO_SNDBUF: */
1184 /* UNIMPL case SO_RCVLOWAT: */
1185 /* UNIMPL case SO_SNDLOWAT: */
1189 #endif /* SO_REUSE */
1190 /* UNIMPL case SO_USELOOPBACK: */
1191 if( optlen < sizeof(int) ) {
1196 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname));
1201 /* Level: IPPROTO_IP */
1204 /* UNIMPL case IP_HDRINCL: */
1205 /* UNIMPL case IP_RCVDSTADDR: */
1206 /* UNIMPL case IP_RCVIF: */
1209 if( optlen < sizeof(int) ) {
1214 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1219 /* Level: IPPROTO_TCP */
1221 if( optlen < sizeof(int) ) {
1226 /* If this is no TCP socket, ignore any options. */
1227 if ( sock->conn->type != NETCONN_TCP ) return 0;
1235 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1240 /* UNDEFINED LEVEL */
1242 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname));
1248 sock_set_errno(sock, err);
1254 /* Now do the actual option processing */
1258 /* Level: SOL_SOCKET */
1262 /* The option flags */
1264 /* UNIMPL case SO_DEBUG: */
1265 /* UNIMPL case SO_DONTROUTE: */
1267 /* UNIMPL case SO_OOBINCLUDE: */
1271 #endif /* SO_REUSE */
1272 /* UNIMPL case SO_USELOOPBACK: */
1273 if ( *(int*)optval ) {
1274 sock->conn->pcb.tcp->so_options |= optname;
1276 sock->conn->pcb.tcp->so_options &= ~optname;
1278 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n", s, optname, (*(int*)optval?"on":"off")));
1283 /* Level: IPPROTO_IP */
1287 sock->conn->pcb.tcp->ttl = (u8_t)(*(int*)optval);
1288 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %u\n", s, sock->conn->pcb.tcp->ttl));
1291 sock->conn->pcb.tcp->tos = (u8_t)(*(int*)optval);
1292 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %u\n", s, sock->conn->pcb.tcp->tos));
1297 /* Level: IPPROTO_TCP */
1301 if ( *(int*)optval ) {
1302 sock->conn->pcb.tcp->flags |= TF_NODELAY;
1304 sock->conn->pcb.tcp->flags &= ~TF_NODELAY;
1306 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n", s, (*(int *)optval)?"on":"off") );
1309 sock->conn->pcb.tcp->keepalive = (u32_t)(*(int*)optval);
1310 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %u\n", s, sock->conn->pcb.tcp->keepalive));
1316 sock_set_errno(sock, err);
1317 return err ? -1 : 0;
1320 int lwip_ioctl(int s, long cmd, void *argp)
1322 struct lwip_socket *sock = get_socket(s);
1332 sock_set_errno(sock, EINVAL);
1336 *((u16_t*)argp) = sock->conn->recv_avail;
1338 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %u\n", s, argp, *((u16_t*)argp)));
1339 sock_set_errno(sock, 0);
1343 if (argp && *(u32_t*)argp)
1344 sock->flags |= O_NONBLOCK;
1346 sock->flags &= ~O_NONBLOCK;
1347 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, !!(sock->flags & O_NONBLOCK)));
1348 sock_set_errno(sock, 0);
1352 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
1353 sock_set_errno(sock, ENOSYS); /* not yet implemented */