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>
40 #include "lwip/arch.h"
43 #include "lwip/sockets.h"
45 #define NUM_SOCKETS MEMP_NUM_NETCONN
49 struct netbuf *lastdata;
59 struct lwip_select_cb *next;
67 static struct lwip_socket sockets[NUM_SOCKETS];
68 static struct lwip_select_cb *select_cb_list = 0;
70 static sys_sem_t socksem = 0;
71 static sys_sem_t selectsem = 0;
74 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
76 static int err_to_errno_table[11] = {
77 0, /* ERR_OK 0 No error, everything OK. */
78 ENOMEM, /* ERR_MEM -1 Out of memory error. */
79 ENOBUFS, /* ERR_BUF -2 Buffer error. */
80 ECONNABORTED, /* ERR_ABRT -3 Connection aborted. */
81 ECONNRESET, /* ERR_RST -4 Connection reset. */
82 ESHUTDOWN, /* ERR_CLSD -5 Connection closed. */
83 ENOTCONN, /* ERR_CONN -6 Not connected. */
84 EINVAL, /* ERR_VAL -7 Illegal value. */
85 EIO, /* ERR_ARG -8 Illegal argument. */
86 EHOSTUNREACH, /* ERR_RTE -9 Routing problem. */
87 EADDRINUSE /* ERR_USE -10 Address in use. */
90 #define ERR_TO_ERRNO_TABLE_SIZE \
91 (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0]))
93 #define err_to_errno(err) \
94 (-(err) >= 0 && -(err) < ERR_TO_ERRNO_TABLE_SIZE ? \
95 err_to_errno_table[-(err)] : EIO)
98 #define set_errno(err) errno = (err)
100 #define set_errno(err)
103 #define sock_set_errno(sk, e) do { \
105 set_errno(sk->err); \
109 static struct lwip_socket *
112 struct lwip_socket *sock;
114 if ((s < 0) || (s > NUM_SOCKETS)) {
115 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
123 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
132 alloc_socket(struct netconn *newconn)
137 socksem = sys_sem_new(1);
139 /* Protect socket array */
140 sys_sem_wait(socksem);
142 /* allocate a new socket identifier */
143 for(i = 0; i < NUM_SOCKETS; ++i) {
144 if (!sockets[i].conn) {
145 sockets[i].conn = newconn;
146 sockets[i].lastdata = NULL;
147 sockets[i].lastoffset = 0;
148 sockets[i].rcvevent = 0;
149 sockets[i].sendevent = 1; /* TCP send buf is empty */
150 sockets[i].flags = 0;
152 sys_sem_signal(socksem);
156 sys_sem_signal(socksem);
161 lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
163 struct lwip_socket *sock;
164 struct netconn *newconn;
165 struct ip_addr naddr;
168 struct sockaddr_in sin;
170 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
171 sock = get_socket(s);
177 newconn = netconn_accept(sock->conn);
179 /* get the IP address and port of the remote host */
180 netconn_peer(newconn, &naddr, &port);
182 memset(&sin, 0, sizeof(sin));
183 sin.sin_len = sizeof(sin);
184 sin.sin_family = AF_INET;
185 sin.sin_port = htons(port);
186 sin.sin_addr.s_addr = naddr.addr;
188 if (*addrlen > sizeof(sin))
189 *addrlen = sizeof(sin);
191 memcpy(addr, &sin, *addrlen);
193 newsock = alloc_socket(newconn);
195 netconn_delete(newconn);
196 sock_set_errno(sock, ENOBUFS);
199 newconn->callback = event_callback;
200 sock = get_socket(newsock);
202 sys_sem_wait(socksem);
203 sock->rcvevent += -1 - newconn->socket;
204 newconn->socket = newsock;
205 sys_sem_signal(socksem);
207 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
208 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
209 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", port));
211 sock_set_errno(sock, 0);
216 lwip_bind(int s, struct sockaddr *name, socklen_t namelen)
218 struct lwip_socket *sock;
219 struct ip_addr local_addr;
223 sock = get_socket(s);
229 local_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
230 local_port = ((struct sockaddr_in *)name)->sin_port;
232 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
233 ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
234 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(local_port)));
236 err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
239 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
240 sock_set_errno(sock, err_to_errno(err));
244 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
245 sock_set_errno(sock, 0);
252 struct lwip_socket *sock;
254 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
256 socksem = sys_sem_new(1);
258 /* We cannot allow multiple closes of the same socket. */
259 sys_sem_wait(socksem);
261 sock = get_socket(s);
263 sys_sem_signal(socksem);
268 netconn_delete(sock->conn);
269 if (sock->lastdata) {
270 netbuf_delete(sock->lastdata);
272 sock->lastdata = NULL;
273 sock->lastoffset = 0;
275 sys_sem_signal(socksem);
276 sock_set_errno(sock, 0);
281 lwip_connect(int s, struct sockaddr *name, socklen_t namelen)
283 struct lwip_socket *sock;
286 sock = get_socket(s);
292 if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) {
293 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
294 err = netconn_disconnect(sock->conn);
296 struct ip_addr remote_addr;
299 remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
300 remote_port = ((struct sockaddr_in *)name)->sin_port;
302 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
303 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
304 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port)));
306 err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
310 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
311 sock_set_errno(sock, err_to_errno(err));
315 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
316 sock_set_errno(sock, 0);
321 lwip_listen(int s, int backlog)
323 struct lwip_socket *sock;
326 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
327 sock = get_socket(s);
333 err = netconn_listen(sock->conn);
336 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
337 sock_set_errno(sock, err_to_errno(err));
341 sock_set_errno(sock, 0);
346 lwip_recvfrom(int s, void *mem, int len, unsigned int flags,
347 struct sockaddr *from, socklen_t *fromlen)
349 struct lwip_socket *sock;
351 u16_t buflen, copylen;
352 struct ip_addr *addr;
356 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %d, 0x%x, ..)\n", s, mem, len, flags));
357 sock = get_socket(s);
363 /* Check if there is data left from the last recv operation. */
364 if (sock->lastdata) {
365 buf = sock->lastdata;
367 /* If this is non-blocking call, then check first */
368 if (((flags & MSG_DONTWAIT) || (sock->flags & O_NONBLOCK))
371 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
372 sock_set_errno(sock, EWOULDBLOCK);
376 /* No data was left from the previous operation, so we try to get
377 some from the network. */
378 buf = netconn_recv(sock->conn);
381 /* We should really do some error checking here. */
382 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL!\n", s));
383 sock_set_errno(sock, 0);
388 buflen = netbuf_len(buf);
390 buflen -= sock->lastoffset;
398 /* copy the contents of the received buffer into
399 the supplied memory pointer mem */
400 netbuf_copy_partial(buf, mem, copylen, sock->lastoffset);
402 /* Check to see from where the data was. */
403 if (from && fromlen) {
404 struct sockaddr_in sin;
406 addr = netbuf_fromaddr(buf);
407 port = netbuf_fromport(buf);
409 memset(&sin, 0, sizeof(sin));
410 sin.sin_len = sizeof(sin);
411 sin.sin_family = AF_INET;
412 sin.sin_port = htons(port);
413 sin.sin_addr.s_addr = addr->addr;
415 if (*fromlen > sizeof(sin))
416 *fromlen = sizeof(sin);
418 memcpy(from, &sin, *fromlen);
420 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
421 ip_addr_debug_print(SOCKETS_DEBUG, addr);
422 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, copylen));
425 addr = netbuf_fromaddr(buf);
426 port = netbuf_fromport(buf);
428 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
429 ip_addr_debug_print(SOCKETS_DEBUG, addr);
430 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u len=%u\n", port, copylen));
435 /* If this is a TCP socket, check if there is data left in the
436 buffer. If so, it should be saved in the sock structure for next
438 if (netconn_type(sock->conn) == NETCONN_TCP && buflen - copylen > 0) {
439 sock->lastdata = buf;
440 sock->lastoffset += copylen;
442 sock->lastdata = NULL;
443 sock->lastoffset = 0;
448 sock_set_errno(sock, 0);
453 lwip_read(int s, void *mem, int len)
455 return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
459 lwip_recv(int s, void *mem, int len, unsigned int flags)
461 return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
465 lwip_send(int s, void *data, int size, unsigned int flags)
467 struct lwip_socket *sock;
471 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%d, flags=0x%x)\n", s, data, size, flags));
473 sock = get_socket(s);
479 switch (netconn_type(sock->conn)) {
482 case NETCONN_UDPLITE:
483 case NETCONN_UDPNOCHKSUM:
484 /* create a buffer */
488 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ENOBUFS\n", s));
489 sock_set_errno(sock, ENOBUFS);
493 /* make the buffer point to the data that should
495 netbuf_ref(buf, data, size);
498 err = netconn_send(sock->conn, buf);
500 /* deallocated the buffer */
504 err = netconn_write(sock->conn, data, size, NETCONN_COPY);
511 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d\n", s, err));
512 sock_set_errno(sock, err_to_errno(err));
516 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ok size=%d\n", s, size));
517 sock_set_errno(sock, 0);
522 lwip_sendto(int s, void *data, int size, unsigned int flags,
523 struct sockaddr *to, socklen_t tolen)
525 struct lwip_socket *sock;
526 struct ip_addr remote_addr, addr;
527 u16_t remote_port, port;
530 sock = get_socket(s);
536 /* get the peer if currently connected */
537 connected = (netconn_peer(sock->conn, &addr, &port) == ERR_OK);
539 remote_addr.addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
540 remote_port = ((struct sockaddr_in *)to)->sin_port;
542 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, size=%d, flags=0x%x to=", s, data, size, flags));
543 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
544 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u\n", ntohs(remote_port)));
546 netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
548 ret = lwip_send(s, data, size, flags);
550 /* reset the remote address and port number
553 netconn_connect(sock->conn, &addr, port);
555 netconn_disconnect(sock->conn);
560 lwip_socket(int domain, int type, int protocol)
562 struct netconn *conn;
565 /* create a netconn */
568 conn = netconn_new_with_proto_and_callback(NETCONN_RAW, protocol, event_callback);
569 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
572 conn = netconn_new_with_callback(NETCONN_UDP, event_callback);
573 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
576 conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
577 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
580 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n", domain, type, protocol));
586 LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
591 i = alloc_socket(conn);
594 netconn_delete(conn);
599 LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
605 lwip_write(int s, void *data, int size)
607 return lwip_send(s, data, size, 0);
612 lwip_selscan(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset)
615 fd_set lreadset, lwriteset, lexceptset;
616 struct lwip_socket *p_sock;
620 FD_ZERO(&lexceptset);
622 /* Go through each socket in each list to count number of sockets which
624 for(i = 0; i < maxfdp1; i++)
626 if (FD_ISSET(i, readset))
628 /* See if netconn of this socket is ready for read */
629 p_sock = get_socket(i);
630 if (p_sock && (p_sock->lastdata || p_sock->rcvevent))
632 FD_SET(i, &lreadset);
633 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
637 if (FD_ISSET(i, writeset))
639 /* See if netconn of this socket is ready for write */
640 p_sock = get_socket(i);
641 if (p_sock && p_sock->sendevent)
643 FD_SET(i, &lwriteset);
644 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
650 *writeset = lwriteset;
659 lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
660 struct timeval *timeout)
664 fd_set lreadset, lwriteset, lexceptset;
666 struct lwip_select_cb select_cb;
667 struct lwip_select_cb *p_selcb;
669 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));
672 select_cb.readset = readset;
673 select_cb.writeset = writeset;
674 select_cb.exceptset = exceptset;
675 select_cb.sem_signalled = 0;
677 /* Protect ourselves searching through the list */
679 selectsem = sys_sem_new(1);
680 sys_sem_wait(selectsem);
687 lwriteset = *writeset;
691 lexceptset = *exceptset;
693 FD_ZERO(&lexceptset);
695 /* Go through each socket in each list to count number of sockets which
697 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
699 /* If we don't have any current events, then suspend if we are supposed to */
702 if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0)
704 sys_sem_signal(selectsem);
712 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
718 /* add our semaphore to list */
719 /* We don't actually need any dynamic memory. Our entry on the
720 * list is only valid while we are in this function, so it's ok
721 * to use local variables */
723 select_cb.sem = sys_sem_new(0);
724 /* Note that we are still protected */
725 /* Put this select_cb on top of list */
726 select_cb.next = select_cb_list;
727 select_cb_list = &select_cb;
729 /* Now we can safely unprotect */
730 sys_sem_signal(selectsem);
732 /* Now just wait to be woken */
737 msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
739 i = sys_sem_wait_timeout(select_cb.sem, msectimeout);
741 /* Take us off the list */
742 sys_sem_wait(selectsem);
743 if (select_cb_list == &select_cb)
744 select_cb_list = select_cb.next;
746 for (p_selcb = select_cb_list; p_selcb; p_selcb = p_selcb->next)
747 if (p_selcb->next == &select_cb)
749 p_selcb->next = select_cb.next;
753 sys_sem_signal(selectsem);
755 sys_sem_free(select_cb.sem);
756 if (i == 0) /* Timeout */
765 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
776 lwriteset = *writeset;
780 lexceptset = *exceptset;
782 FD_ZERO(&lexceptset);
785 nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
788 sys_sem_signal(selectsem);
793 *writeset = lwriteset;
795 *exceptset = lexceptset;
797 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
805 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
808 struct lwip_socket *sock;
809 struct lwip_select_cb *scb;
817 /* Data comes in right away after an accept, even though
818 * the server task might not have created a new socket yet.
819 * Just count down (or up) if that's the case and we
820 * will use the data later. Note that only receive events
821 * can happen before the new socket is set up. */
822 if (evt == NETCONN_EVT_RCVPLUS)
827 sock = get_socket(s);
835 selectsem = sys_sem_new(1);
837 sys_sem_wait(selectsem);
838 /* Set event as required */
841 case NETCONN_EVT_RCVPLUS:
844 case NETCONN_EVT_RCVMINUS:
847 case NETCONN_EVT_SENDPLUS:
850 case NETCONN_EVT_SENDMINUS:
854 sys_sem_signal(selectsem);
856 /* Now decide if anyone is waiting for this socket */
857 /* NOTE: This code is written this way to protect the select link list
858 but to avoid a deadlock situation by releasing socksem before
859 signalling for the select. This means we need to go through the list
860 multiple times ONLY IF a select was actually waiting. We go through
861 the list the number of waiting select calls + 1. This list is
862 expected to be small. */
865 sys_sem_wait(selectsem);
866 for (scb = select_cb_list; scb; scb = scb->next)
868 if (scb->sem_signalled == 0)
870 /* Test this select call for our socket */
871 if (scb->readset && FD_ISSET(s, scb->readset))
874 if (scb->writeset && FD_ISSET(s, scb->writeset))
881 scb->sem_signalled = 1;
882 sys_sem_signal(selectsem);
883 sys_sem_signal(scb->sem);
885 sys_sem_signal(selectsem);
895 int lwip_shutdown(int s, int how)
897 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
898 return lwip_close(s); /* XXX temporary hack until proper implementation */
901 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen)
903 struct lwip_socket *sock;
904 struct sockaddr_in sin;
905 struct ip_addr naddr;
907 sock = get_socket(s);
913 memset(&sin, 0, sizeof(sin));
914 sin.sin_len = sizeof(sin);
915 sin.sin_family = AF_INET;
917 /* get the IP address and port of the remote host */
918 netconn_peer(sock->conn, &naddr, &sin.sin_port);
920 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getpeername(%d, addr=", s));
921 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
922 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%d)\n", sin.sin_port));
924 sin.sin_port = htons(sin.sin_port);
925 sin.sin_addr.s_addr = naddr.addr;
927 if (*namelen > sizeof(sin))
928 *namelen = sizeof(sin);
930 memcpy(name, &sin, *namelen);
931 sock_set_errno(sock, 0);
935 int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen)
937 struct lwip_socket *sock;
938 struct sockaddr_in sin;
939 struct ip_addr *naddr;
941 sock = get_socket(s);
947 memset(&sin, 0, sizeof(sin));
948 sin.sin_len = sizeof(sin);
949 sin.sin_family = AF_INET;
951 /* get the IP address and port of the remote host */
952 netconn_addr(sock->conn, &naddr, &sin.sin_port);
954 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockname(%d, addr=", s));
955 ip_addr_debug_print(SOCKETS_DEBUG, naddr);
956 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%d)\n", sin.sin_port));
958 sin.sin_port = htons(sin.sin_port);
959 sin.sin_addr.s_addr = naddr->addr;
961 if (*namelen > sizeof(sin))
962 *namelen = sizeof(sin);
964 memcpy(name, &sin, *namelen);
965 sock_set_errno(sock, 0);
969 int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
972 struct lwip_socket *sock = get_socket(s);
979 if( NULL == optval || NULL == optlen ) {
980 sock_set_errno( sock, EFAULT );
984 /* Do length and type checks for the various options first, to keep it readable. */
987 /* Level: SOL_SOCKET */
993 /* UNIMPL case SO_DEBUG: */
994 /* UNIMPL case SO_DONTROUTE: */
997 /* UNIMPL case SO_OOBINLINE: */
998 /* UNIMPL case SO_RCVBUF: */
999 /* UNIMPL case SO_SNDBUF: */
1000 /* UNIMPL case SO_RCVLOWAT: */
1001 /* UNIMPL case SO_SNDLOWAT: */
1005 #endif /* SO_REUSE */
1007 /* UNIMPL case SO_USELOOPBACK: */
1008 if( *optlen < sizeof(int) ) {
1014 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname));
1019 /* Level: IPPROTO_IP */
1022 /* UNIMPL case IP_HDRINCL: */
1023 /* UNIMPL case IP_RCVDSTADDR: */
1024 /* UNIMPL case IP_RCVIF: */
1027 if( *optlen < sizeof(int) ) {
1033 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1038 /* Level: IPPROTO_TCP */
1040 if( *optlen < sizeof(int) ) {
1045 /* If this is no TCP socket, ignore any options. */
1046 if ( sock->conn->type != NETCONN_TCP ) return 0;
1054 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1059 /* UNDEFINED LEVEL */
1061 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname));
1067 sock_set_errno(sock, err);
1073 /* Now do the actual option processing */
1077 /* Level: SOL_SOCKET */
1081 /* The option flags */
1084 /* UNIMPL case SO_DEBUG: */
1085 /* UNIMPL case SO_DONTROUTE: */
1087 /* UNIMPL case SO_OOBINCLUDE: */
1091 #endif /* SO_REUSE */
1092 /*case SO_USELOOPBACK: UNIMPL */
1093 *(int*)optval = sock->conn->pcb.tcp->so_options & optname;
1094 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n", s, optname, (*(int*)optval?"on":"off")));
1098 switch (sock->conn->type) {
1100 *(int*)optval = SOCK_RAW;
1103 *(int*)optval = SOCK_STREAM;
1106 case NETCONN_UDPLITE:
1107 case NETCONN_UDPNOCHKSUM:
1108 *(int*)optval = SOCK_DGRAM;
1110 default: /* unrecognized socket type */
1111 *(int*)optval = sock->conn->type;
1112 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n", s, *(int *)optval));
1114 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n", s, *(int *)optval));
1118 *(int *)optval = sock->err;
1120 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n", s, *(int *)optval));
1125 /* Level: IPPROTO_IP */
1129 *(int*)optval = sock->conn->pcb.tcp->ttl;
1130 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n", s, *(int *)optval));
1133 *(int*)optval = sock->conn->pcb.tcp->tos;
1134 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n", s, *(int *)optval));
1139 /* Level: IPPROTO_TCP */
1143 *(int*)optval = (sock->conn->pcb.tcp->flags & TF_NODELAY);
1144 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n", s, (*(int*)optval)?"on":"off") );
1147 *(int*)optval = (int)sock->conn->pcb.tcp->keepalive;
1148 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPALIVE) = %d\n", s, *(int *)optval));
1155 sock_set_errno(sock, err);
1156 return err ? -1 : 0;
1159 int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
1161 struct lwip_socket *sock = get_socket(s);
1169 if( NULL == optval ) {
1170 sock_set_errno( sock, EFAULT );
1175 /* Do length and type checks for the various options first, to keep it readable. */
1178 /* Level: SOL_SOCKET */
1183 /* UNIMPL case SO_DEBUG: */
1184 /* UNIMPL case SO_DONTROUTE: */
1186 /* UNIMPL case SO_OOBINLINE: */
1187 /* UNIMPL case SO_RCVBUF: */
1188 /* UNIMPL case SO_SNDBUF: */
1189 /* UNIMPL case SO_RCVLOWAT: */
1190 /* UNIMPL case SO_SNDLOWAT: */
1194 #endif /* SO_REUSE */
1195 /* UNIMPL case SO_USELOOPBACK: */
1196 if( optlen < sizeof(int) ) {
1201 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname));
1206 /* Level: IPPROTO_IP */
1209 /* UNIMPL case IP_HDRINCL: */
1210 /* UNIMPL case IP_RCVDSTADDR: */
1211 /* UNIMPL case IP_RCVIF: */
1214 if( optlen < sizeof(int) ) {
1219 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1224 /* Level: IPPROTO_TCP */
1226 if( optlen < sizeof(int) ) {
1231 /* If this is no TCP socket, ignore any options. */
1232 if ( sock->conn->type != NETCONN_TCP ) return 0;
1240 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname));
1245 /* UNDEFINED LEVEL */
1247 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname));
1253 sock_set_errno(sock, err);
1259 /* Now do the actual option processing */
1263 /* Level: SOL_SOCKET */
1267 /* The option flags */
1269 /* UNIMPL case SO_DEBUG: */
1270 /* UNIMPL case SO_DONTROUTE: */
1272 /* UNIMPL case SO_OOBINCLUDE: */
1276 #endif /* SO_REUSE */
1277 /* UNIMPL case SO_USELOOPBACK: */
1278 if ( *(int*)optval ) {
1279 sock->conn->pcb.tcp->so_options |= optname;
1281 sock->conn->pcb.tcp->so_options &= ~optname;
1283 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n", s, optname, (*(int*)optval?"on":"off")));
1288 /* Level: IPPROTO_IP */
1292 sock->conn->pcb.tcp->ttl = (u8_t)(*(int*)optval);
1293 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %u\n", s, sock->conn->pcb.tcp->ttl));
1296 sock->conn->pcb.tcp->tos = (u8_t)(*(int*)optval);
1297 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %u\n", s, sock->conn->pcb.tcp->tos));
1302 /* Level: IPPROTO_TCP */
1306 if ( *(int*)optval ) {
1307 sock->conn->pcb.tcp->flags |= TF_NODELAY;
1309 sock->conn->pcb.tcp->flags &= ~TF_NODELAY;
1311 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n", s, (*(int *)optval)?"on":"off") );
1314 sock->conn->pcb.tcp->keepalive = (u32_t)(*(int*)optval);
1315 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %lu\n", s, sock->conn->pcb.tcp->keepalive));
1321 sock_set_errno(sock, err);
1322 return err ? -1 : 0;
1325 int lwip_ioctl(int s, long cmd, void *argp)
1327 struct lwip_socket *sock = get_socket(s);
1337 sock_set_errno(sock, EINVAL);
1341 *((u16_t*)argp) = sock->conn->recv_avail;
1343 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %u\n", s, argp, *((u16_t*)argp)));
1344 sock_set_errno(sock, 0);
1348 if (argp && *(u32_t*)argp)
1349 sock->flags |= O_NONBLOCK;
1351 sock->flags &= ~O_NONBLOCK;
1352 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, !!(sock->flags & O_NONBLOCK)));
1353 sock_set_errno(sock, 0);
1357 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
1358 sock_set_errno(sock, ENOSYS); /* not yet implemented */