2 * Copyright (c) 2001-2003 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 #include "lwip/sockets.h"
42 #define NUM_SOCKETS MEMP_NUM_NETCONN
46 struct netbuf *lastdata;
56 struct lwip_select_cb *next;
64 static struct lwip_socket sockets[NUM_SOCKETS];
65 static struct lwip_select_cb *select_cb_list = 0;
67 static sys_sem_t socksem = 0;
68 static sys_sem_t selectsem = 0;
71 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
73 static int err_to_errno_table[11] = {
74 0, /* ERR_OK 0 No error, everything OK. */
75 ENOMEM, /* ERR_MEM -1 Out of memory error. */
76 ENOBUFS, /* ERR_BUF -2 Buffer error. */
77 ECONNABORTED, /* ERR_ABRT -3 Connection aborted. */
78 ECONNRESET, /* ERR_RST -4 Connection reset. */
79 ESHUTDOWN, /* ERR_CLSD -5 Connection closed. */
80 ENOTCONN, /* ERR_CONN -6 Not connected. */
81 EINVAL, /* ERR_VAL -7 Illegal value. */
82 EIO, /* ERR_ARG -8 Illegal argument. */
83 EHOSTUNREACH, /* ERR_RTE -9 Routing problem. */
84 EADDRINUSE /* ERR_USE -10 Address in use. */
87 #define err_to_errno(err) \
88 ((err) < (sizeof(err_to_errno_table)/sizeof(int))) ? \
89 err_to_errno_table[-(err)] : EIO
92 #define set_errno(err) errno = (err)
94 #define set_errno(err)
97 #define sock_set_errno(sk, e) do { \
103 static struct lwip_socket *
106 struct lwip_socket *sock;
108 if ((s < 0) || (s > NUM_SOCKETS)) {
109 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
117 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
126 alloc_socket(struct netconn *newconn)
131 socksem = sys_sem_new(1);
133 /* Protect socket array */
134 sys_sem_wait(socksem);
136 /* allocate a new socket identifier */
137 for(i = 0; i < NUM_SOCKETS; ++i) {
138 if (!sockets[i].conn) {
139 sockets[i].conn = newconn;
140 sockets[i].lastdata = NULL;
141 sockets[i].lastoffset = 0;
142 sockets[i].rcvevent = 0;
143 sockets[i].sendevent = 1; /* TCP send buf is empty */
144 sockets[i].flags = 0;
146 sys_sem_signal(socksem);
150 sys_sem_signal(socksem);
155 lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
157 struct lwip_socket *sock;
158 struct netconn *newconn;
159 struct ip_addr naddr;
162 struct sockaddr_in sin;
164 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
165 sock = get_socket(s);
171 newconn = netconn_accept(sock->conn);
173 /* get the IP address and port of the remote host */
174 netconn_peer(newconn, &naddr, &port);
176 memset(&sin, 0, sizeof(sin));
177 sin.sin_len = sizeof(sin);
178 sin.sin_family = AF_INET;
179 sin.sin_port = htons(port);
180 sin.sin_addr.s_addr = naddr.addr;
182 if (*addrlen > sizeof(sin))
183 *addrlen = sizeof(sin);
185 memcpy(addr, &sin, *addrlen);
187 newsock = alloc_socket(newconn);
189 netconn_delete(newconn);
190 sock_set_errno(sock, ENOBUFS);
193 newconn->callback = event_callback;
194 sock = get_socket(newsock);
196 sys_sem_wait(socksem);
197 sock->rcvevent += -1 - newconn->socket;
198 newconn->socket = newsock;
199 sys_sem_signal(socksem);
201 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
202 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
203 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", port));
205 sock_set_errno(sock, 0);
210 lwip_bind(int s, struct sockaddr *name, socklen_t namelen)
212 struct lwip_socket *sock;
213 struct ip_addr local_addr;
217 sock = get_socket(s);
223 local_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
224 local_port = ((struct sockaddr_in *)name)->sin_port;
226 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
227 ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
228 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(local_port)));
230 err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
233 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
234 sock_set_errno(sock, err_to_errno(err));
238 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
239 sock_set_errno(sock, 0);
246 struct lwip_socket *sock;
248 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
250 socksem = sys_sem_new(1);
252 /* We cannot allow multiple closes of the same socket. */
253 sys_sem_wait(socksem);
255 sock = get_socket(s);
257 sys_sem_signal(socksem);
262 netconn_delete(sock->conn);
263 if (sock->lastdata) {
264 netbuf_delete(sock->lastdata);
266 sock->lastdata = NULL;
267 sock->lastoffset = 0;
269 sys_sem_signal(socksem);
270 sock_set_errno(sock, 0);
275 lwip_connect(int s, struct sockaddr *name, socklen_t namelen)
277 struct lwip_socket *sock;
280 sock = get_socket(s);
286 if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) {
287 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
288 err = netconn_disconnect(sock->conn);
290 struct ip_addr remote_addr;
293 remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
294 remote_port = ((struct sockaddr_in *)name)->sin_port;
296 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
297 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
298 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port)));
300 err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
304 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
305 sock_set_errno(sock, err_to_errno(err));
309 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
310 sock_set_errno(sock, 0);
315 lwip_listen(int s, int backlog)
317 struct lwip_socket *sock;
320 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
321 sock = get_socket(s);
327 err = netconn_listen(sock->conn);
330 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
331 sock_set_errno(sock, err_to_errno(err));
335 sock_set_errno(sock, 0);
340 lwip_recvfrom(int s, void *mem, int len, unsigned int flags,
341 struct sockaddr *from, socklen_t *fromlen)
343 struct lwip_socket *sock;
345 u16_t buflen, copylen;
346 struct ip_addr *addr;
350 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %d, 0x%x, ..)\n", s, mem, len, flags));
351 sock = get_socket(s);
357 /* Check if there is data left from the last recv operation. */
358 if (sock->lastdata) {
359 buf = sock->lastdata;
361 /* If this is non-blocking call, then check first */
362 if (((flags & MSG_DONTWAIT) || (sock->flags & O_NONBLOCK))
365 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
366 sock_set_errno(sock, EWOULDBLOCK);
370 /* No data was left from the previous operation, so we try to get
371 some from the network. */
372 buf = netconn_recv(sock->conn);
375 /* We should really do some error checking here. */
376 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL!\n", s));
377 sock_set_errno(sock, 0);
382 buflen = netbuf_len(buf);
384 buflen -= sock->lastoffset;
392 /* copy the contents of the received buffer into
393 the supplied memory pointer mem */
394 netbuf_copy_partial(buf, mem, copylen, sock->lastoffset);
396 /* Check to see from where the data was. */
397 if (from && fromlen) {
398 struct sockaddr_in sin;
400 addr = netbuf_fromaddr(buf);
401 port = netbuf_fromport(buf);
403 memset(&sin, 0, sizeof(sin));
404 sin.sin_len = sizeof(sin);
405 sin.sin_family = AF_INET;
406 sin.sin_port = htons(port);
407 sin.sin_addr.s_addr = addr->addr;
409 if (*fromlen > sizeof(sin))
410 *fromlen = sizeof(sin);
412 memcpy(from, &sin, *fromlen);
414 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
415 ip_addr_debug_print(SOCKETS_DEBUG, addr);
416 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, copylen));
418 #if SOCKETS_DEBUG > 0
419 addr = netbuf_fromaddr(buf);
420 port = netbuf_fromport(buf);
422 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
423 ip_addr_debug_print(SOCKETS_DEBUG, addr);
424 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, copylen));
429 /* If this is a TCP socket, check if there is data left in the
430 buffer. If so, it should be saved in the sock structure for next
432 if (netconn_type(sock->conn) == NETCONN_TCP && buflen - copylen > 0) {
433 sock->lastdata = buf;
434 sock->lastoffset += copylen;
436 sock->lastdata = NULL;
437 sock->lastoffset = 0;
442 sock_set_errno(sock, 0);
447 lwip_read(int s, void *mem, int len)
449 return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
453 lwip_recv(int s, void *mem, int len, unsigned int flags)
455 return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
459 lwip_send(int s, void *data, int size, unsigned int flags)
461 struct lwip_socket *sock;
465 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%d, flags=0x%x)\n", s, data, size, flags));
467 sock = get_socket(s);
473 switch (netconn_type(sock->conn)) {
476 case NETCONN_UDPLITE:
477 case NETCONN_UDPNOCHKSUM:
478 /* create a buffer */
482 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ENOBUFS\n", s));
483 sock_set_errno(sock, ENOBUFS);
487 /* make the buffer point to the data that should
489 netbuf_ref(buf, data, size);
492 err = netconn_send(sock->conn, buf);
494 /* deallocated the buffer */
498 err = netconn_write(sock->conn, data, size, NETCONN_COPY);
505 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d\n", s, err));
506 sock_set_errno(sock, err_to_errno(err));
510 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ok size=%d\n", s, size));
511 sock_set_errno(sock, 0);
516 lwip_sendto(int s, void *data, int size, unsigned int flags,
517 struct sockaddr *to, socklen_t tolen)
519 struct lwip_socket *sock;
520 struct ip_addr remote_addr, addr;
521 u16_t remote_port, port;
524 sock = get_socket(s);
530 /* get the peer if currently connected */
531 connected = (netconn_peer(sock->conn, &addr, &port) == ERR_OK);
533 remote_addr.addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
534 remote_port = ((struct sockaddr_in *)to)->sin_port;
536 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, size=%d, flags=0x%x to=", s, data, size, flags));
537 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
538 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", ntohs(remote_port)));
540 netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
542 ret = lwip_send(s, data, size, flags);
544 /* reset the remote address and port number
547 netconn_connect(sock->conn, &addr, port);
549 netconn_disconnect(sock->conn);
554 lwip_socket(int domain, int type, int protocol)
556 struct netconn *conn;
559 /* create a netconn */
562 conn = netconn_new_with_proto_and_callback(NETCONN_RAW, protocol, event_callback);
563 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
566 conn = netconn_new_with_callback(NETCONN_UDP, event_callback);
567 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
570 conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
571 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
574 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n", domain, type, protocol));
580 LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
585 i = alloc_socket(conn);
588 netconn_delete(conn);
593 LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
599 lwip_write(int s, void *data, int size)
601 return lwip_send(s, data, size, 0);
606 lwip_selscan(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset)
609 fd_set lreadset, lwriteset, lexceptset;
610 struct lwip_socket *p_sock;
614 FD_ZERO(&lexceptset);
616 /* Go through each socket in each list to count number of sockets which
618 for(i = 0; i < maxfdp1; i++)
620 if (FD_ISSET(i, readset))
622 /* See if netconn of this socket is ready for read */
623 p_sock = get_socket(i);
624 if (p_sock && (p_sock->lastdata || p_sock->rcvevent))
626 FD_SET(i, &lreadset);
627 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
631 if (FD_ISSET(i, writeset))
633 /* See if netconn of this socket is ready for write */
634 p_sock = get_socket(i);
635 if (p_sock && p_sock->sendevent)
637 FD_SET(i, &lwriteset);
638 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
644 *writeset = lwriteset;
653 lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
654 struct timeval *timeout)
658 fd_set lreadset, lwriteset, lexceptset;
660 struct lwip_select_cb select_cb;
661 struct lwip_select_cb *p_selcb;
663 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));
666 select_cb.readset = readset;
667 select_cb.writeset = writeset;
668 select_cb.exceptset = exceptset;
669 select_cb.sem_signalled = 0;
671 /* Protect ourselves searching through the list */
673 selectsem = sys_sem_new(1);
674 sys_sem_wait(selectsem);
681 lwriteset = *writeset;
685 lexceptset = *exceptset;
687 FD_ZERO(&lexceptset);
689 /* Go through each socket in each list to count number of sockets which
691 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
693 /* If we don't have any current events, then suspend if we are supposed to */
696 if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0)
698 sys_sem_signal(selectsem);
706 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
712 /* add our semaphore to list */
713 /* We don't actually need any dynamic memory. Our entry on the
714 * list is only valid while we are in this function, so it's ok
715 * to use local variables */
717 select_cb.sem = sys_sem_new(0);
718 /* Note that we are still protected */
719 /* Put this select_cb on top of list */
720 select_cb.next = select_cb_list;
721 select_cb_list = &select_cb;
723 /* Now we can safely unprotect */
724 sys_sem_signal(selectsem);
726 /* Now just wait to be woken */
731 msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
733 i = sys_sem_wait_timeout(select_cb.sem, msectimeout);
735 /* Take us off the list */
736 sys_sem_wait(selectsem);
737 if (select_cb_list == &select_cb)
738 select_cb_list = select_cb.next;
740 for (p_selcb = select_cb_list; p_selcb; p_selcb = p_selcb->next)
741 if (p_selcb->next == &select_cb)
743 p_selcb->next = select_cb.next;
747 sys_sem_signal(selectsem);
749 sys_sem_free(select_cb.sem);
750 if (i == 0) /* Timeout */
759 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
770 lwriteset = *writeset;
774 lexceptset = *exceptset;
776 FD_ZERO(&lexceptset);
779 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
782 sys_sem_signal(selectsem);
787 *writeset = lwriteset;
789 *exceptset = lexceptset;
791 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
799 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
802 struct lwip_socket *sock;
803 struct lwip_select_cb *scb;
811 /* Data comes in right away after an accept, even though
812 * the server task might not have created a new socket yet.
813 * Just count down (or up) if that's the case and we
814 * will use the data later. Note that only receive events
815 * can happen before the new socket is set up. */
816 if (evt == NETCONN_EVT_RCVPLUS)
821 sock = get_socket(s);
829 selectsem = sys_sem_new(1);
831 sys_sem_wait(selectsem);
832 /* Set event as required */
835 case NETCONN_EVT_RCVPLUS:
838 case NETCONN_EVT_RCVMINUS:
841 case NETCONN_EVT_SENDPLUS:
844 case NETCONN_EVT_SENDMINUS:
848 sys_sem_signal(selectsem);
850 /* Now decide if anyone is waiting for this socket */
851 /* NOTE: This code is written this way to protect the select link list
852 but to avoid a deadlock situation by releasing socksem before
853 signalling for the select. This means we need to go through the list
854 multiple times ONLY IF a select was actually waiting. We go through
855 the list the number of waiting select calls + 1. This list is
856 expected to be small. */
859 sys_sem_wait(selectsem);
860 for (scb = select_cb_list; scb; scb = scb->next)
862 if (scb->sem_signalled == 0)
864 /* Test this select call for our socket */
865 if (scb->readset && FD_ISSET(s, scb->readset))
868 if (scb->writeset && FD_ISSET(s, scb->writeset))
875 scb->sem_signalled = 1;
876 sys_sem_signal(selectsem);
877 sys_sem_signal(scb->sem);
879 sys_sem_signal(selectsem);
889 int lwip_shutdown(int s, int how)
891 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
892 return lwip_close(s); /* XXX temporary hack until proper implementation */
895 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen)
897 struct lwip_socket *sock;
898 struct sockaddr_in sin;
899 struct ip_addr naddr;
901 sock = get_socket(s);
907 memset(&sin, 0, sizeof(sin));
908 sin.sin_len = sizeof(sin);
909 sin.sin_family = AF_INET;
911 /* get the IP address and port of the remote host */
912 netconn_peer(sock->conn, &naddr, &sin.sin_port);
914 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getpeername(%d, addr=", s));
915 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
916 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%d)\n", sin.sin_port));
918 sin.sin_port = htons(sin.sin_port);
919 sin.sin_addr.s_addr = naddr.addr;
921 if (*namelen > sizeof(sin))
922 *namelen = sizeof(sin);
924 memcpy(name, &sin, *namelen);
925 sock_set_errno(sock, 0);
929 int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen)
931 struct lwip_socket *sock;
932 struct sockaddr_in sin;
933 struct ip_addr *naddr;
935 sock = get_socket(s);
941 memset(&sin, 0, sizeof(sin));
942 sin.sin_len = sizeof(sin);
943 sin.sin_family = AF_INET;
945 /* get the IP address and port of the remote host */
946 netconn_addr(sock->conn, &naddr, &sin.sin_port);
948 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockname(%d, addr=", s));
949 ip_addr_debug_print(SOCKETS_DEBUG, naddr);
950 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%d)\n", sin.sin_port));
952 sin.sin_port = htons(sin.sin_port);
953 sin.sin_addr.s_addr = naddr->addr;
955 if (*namelen > sizeof(sin))
956 *namelen = sizeof(sin);
958 memcpy(name, &sin, *namelen);
959 sock_set_errno(sock, 0);
963 int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
966 struct lwip_socket *sock = get_socket(s);
973 if( NULL == optval || NULL == optlen ) {
974 sock_set_errno( sock, EFAULT );
978 /* Do length and type checks for the various options first, to keep it readable. */
981 /* Level: SOL_SOCKET */
987 /* UNIMPL case SO_DEBUG: */
988 /* UNIMPL case SO_DONTROUTE: */
991 /* UNIMPL case SO_OOBINLINE: */
992 /* UNIMPL case SO_RCVBUF: */
993 /* UNIMPL case SO_SNDBUF: */
994 /* UNIMPL case SO_RCVLOWAT: */
995 /* UNIMPL case SO_SNDLOWAT: */
999 #endif /* SO_REUSE */
1001 /* UNIMPL case SO_USELOOPBACK: */
1002 if( *optlen < sizeof(int) ) {
1008 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname));
1013 /* Level: IPPROTO_IP */
1016 /* UNIMPL case IP_HDRINCL: */
1017 /* UNIMPL case IP_RCVDSTADDR: */
1018 /* UNIMPL case IP_RCVIF: */
1021 if( *optlen < sizeof(int) ) {
1027 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1032 /* Level: IPPROTO_TCP */
1034 if( *optlen < sizeof(int) ) {
1039 /* If this is no TCP socket, ignore any options. */
1040 if ( sock->conn->type != NETCONN_TCP ) return 0;
1048 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1053 /* UNDEFINED LEVEL */
1055 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname));
1061 sock_set_errno(sock, err);
1067 /* Now do the actual option processing */
1071 /* Level: SOL_SOCKET */
1075 /* The option flags */
1078 /* UNIMPL case SO_DEBUG: */
1079 /* UNIMPL case SO_DONTROUTE: */
1081 /* UNIMPL case SO_OOBINCLUDE: */
1085 #endif /* SO_REUSE */
1086 /*case SO_USELOOPBACK: UNIMPL */
1087 *(int*)optval = sock->conn->pcb.tcp->so_options & optname;
1088 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n", s, optname, (*(int*)optval?"on":"off")));
1092 switch (sock->conn->type) {
1094 *(int*)optval = SOCK_RAW;
1097 *(int*)optval = SOCK_STREAM;
1100 case NETCONN_UDPLITE:
1101 case NETCONN_UDPNOCHKSUM:
1102 *(int*)optval = SOCK_DGRAM;
1104 default: /* unrecognized socket type */
1105 *(int*)optval = sock->conn->type;
1106 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n", s, *(int *)optval));
1108 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n", s, *(int *)optval));
1112 *(int *)optval = sock->err;
1114 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n", s, *(int *)optval));
1119 /* Level: IPPROTO_IP */
1123 *(int*)optval = sock->conn->pcb.tcp->ttl;
1124 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n", s, *(int *)optval));
1127 *(int*)optval = sock->conn->pcb.tcp->tos;
1128 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n", s, *(int *)optval));
1133 /* Level: IPPROTO_TCP */
1137 *(int*)optval = (sock->conn->pcb.tcp->flags & TF_NODELAY);
1138 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n", s, (*(int*)optval)?"on":"off") );
1141 *(int*)optval = sock->conn->pcb.tcp->keepalive;
1142 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPALIVE) = %d\n", s, *(int *)optval));
1149 sock_set_errno(sock, err);
1150 return err ? -1 : 0;
1153 int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
1155 struct lwip_socket *sock = get_socket(s);
1163 if( NULL == optval ) {
1164 sock_set_errno( sock, EFAULT );
1169 /* Do length and type checks for the various options first, to keep it readable. */
1172 /* Level: SOL_SOCKET */
1177 /* UNIMPL case SO_DEBUG: */
1178 /* UNIMPL case SO_DONTROUTE: */
1180 /* UNIMPL case SO_OOBINLINE: */
1181 /* UNIMPL case SO_RCVBUF: */
1182 /* UNIMPL case SO_SNDBUF: */
1183 /* UNIMPL case SO_RCVLOWAT: */
1184 /* UNIMPL case SO_SNDLOWAT: */
1188 #endif /* SO_REUSE */
1189 /* UNIMPL case SO_USELOOPBACK: */
1190 if( optlen < sizeof(int) ) {
1195 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname));
1200 /* Level: IPPROTO_IP */
1203 /* UNIMPL case IP_HDRINCL: */
1204 /* UNIMPL case IP_RCVDSTADDR: */
1205 /* UNIMPL case IP_RCVIF: */
1208 if( optlen < sizeof(int) ) {
1213 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1218 /* Level: IPPROTO_TCP */
1220 if( optlen < sizeof(int) ) {
1225 /* If this is no TCP socket, ignore any options. */
1226 if ( sock->conn->type != NETCONN_TCP ) return 0;
1234 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1239 /* UNDEFINED LEVEL */
1241 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname));
1247 sock_set_errno(sock, err);
1253 /* Now do the actual option processing */
1257 /* Level: SOL_SOCKET */
1261 /* The option flags */
1263 /* UNIMPL case SO_DEBUG: */
1264 /* UNIMPL case SO_DONTROUTE: */
1266 /* UNIMPL case SO_OOBINCLUDE: */
1270 #endif /* SO_REUSE */
1271 /* UNIMPL case SO_USELOOPBACK: */
1272 if ( *(int*)optval ) {
1273 sock->conn->pcb.tcp->so_options |= optname;
1275 sock->conn->pcb.tcp->so_options &= ~optname;
1277 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n", s, optname, (*(int*)optval?"on":"off")));
1282 /* Level: IPPROTO_IP */
1286 sock->conn->pcb.tcp->ttl = (u8_t)(*(int*)optval);
1287 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %u\n", s, sock->conn->pcb.tcp->ttl));
1290 sock->conn->pcb.tcp->tos = (u8_t)(*(int*)optval);
1291 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %u\n", s, sock->conn->pcb.tcp->tos));
1296 /* Level: IPPROTO_TCP */
1300 if ( *(int*)optval ) {
1301 sock->conn->pcb.tcp->flags |= TF_NODELAY;
1303 sock->conn->pcb.tcp->flags &= ~TF_NODELAY;
1305 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n", s, (*(int *)optval)?"on":"off") );
1308 sock->conn->pcb.tcp->keepalive = (u32_t)(*(int*)optval);
1309 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %u\n", s, sock->conn->pcb.tcp->keepalive));
1315 sock_set_errno(sock, err);
1316 return err ? -1 : 0;
1319 int lwip_ioctl(int s, long cmd, void *argp)
1321 struct lwip_socket *sock = get_socket(s);
1331 sock_set_errno(sock, EINVAL);
1335 *((u16_t*)argp) = sock->conn->recv_avail;
1337 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %u\n", s, argp, *((u16_t*)argp)));
1338 sock_set_errno(sock, 0);
1342 if (argp && *(u32_t*)argp)
1343 sock->flags |= O_NONBLOCK;
1345 sock->flags &= ~O_NONBLOCK;
1346 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, !!(sock->flags & O_NONBLOCK)));
1347 sock_set_errno(sock, 0);
1351 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
1352 sock_set_errno(sock, ENOSYS); /* not yet implemented */