Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apatel336 0:1496281373a5 1 /**
apatel336 0:1496281373a5 2 * @file
apatel336 0:1496281373a5 3 * Sockets BSD-Like API module
apatel336 0:1496281373a5 4 *
apatel336 0:1496281373a5 5 */
apatel336 0:1496281373a5 6
apatel336 0:1496281373a5 7 /*
apatel336 0:1496281373a5 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
apatel336 0:1496281373a5 9 * All rights reserved.
apatel336 0:1496281373a5 10 *
apatel336 0:1496281373a5 11 * Redistribution and use in source and binary forms, with or without modification,
apatel336 0:1496281373a5 12 * are permitted provided that the following conditions are met:
apatel336 0:1496281373a5 13 *
apatel336 0:1496281373a5 14 * 1. Redistributions of source code must retain the above copyright notice,
apatel336 0:1496281373a5 15 * this list of conditions and the following disclaimer.
apatel336 0:1496281373a5 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
apatel336 0:1496281373a5 17 * this list of conditions and the following disclaimer in the documentation
apatel336 0:1496281373a5 18 * and/or other materials provided with the distribution.
apatel336 0:1496281373a5 19 * 3. The name of the author may not be used to endorse or promote products
apatel336 0:1496281373a5 20 * derived from this software without specific prior written permission.
apatel336 0:1496281373a5 21 *
apatel336 0:1496281373a5 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
apatel336 0:1496281373a5 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
apatel336 0:1496281373a5 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
apatel336 0:1496281373a5 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
apatel336 0:1496281373a5 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
apatel336 0:1496281373a5 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
apatel336 0:1496281373a5 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
apatel336 0:1496281373a5 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
apatel336 0:1496281373a5 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
apatel336 0:1496281373a5 31 * OF SUCH DAMAGE.
apatel336 0:1496281373a5 32 *
apatel336 0:1496281373a5 33 * This file is part of the lwIP TCP/IP stack.
apatel336 0:1496281373a5 34 *
apatel336 0:1496281373a5 35 * Author: Adam Dunkels <adam@sics.se>
apatel336 0:1496281373a5 36 *
apatel336 0:1496281373a5 37 * Improved by Marc Boucher <marc@mbsi.ca> and David Haas <dhaas@alum.rpi.edu>
apatel336 0:1496281373a5 38 *
apatel336 0:1496281373a5 39 */
apatel336 0:1496281373a5 40
apatel336 0:1496281373a5 41 #include "lwip/opt.h"
apatel336 0:1496281373a5 42
apatel336 0:1496281373a5 43 #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
apatel336 0:1496281373a5 44
apatel336 0:1496281373a5 45 #include "lwip/sockets.h"
apatel336 0:1496281373a5 46 #include "lwip/api.h"
apatel336 0:1496281373a5 47 #include "lwip/sys.h"
apatel336 0:1496281373a5 48 #include "lwip/igmp.h"
apatel336 0:1496281373a5 49 #include "lwip/inet.h"
apatel336 0:1496281373a5 50 #include "lwip/tcp.h"
apatel336 0:1496281373a5 51 #include "lwip/raw.h"
apatel336 0:1496281373a5 52 #include "lwip/udp.h"
apatel336 0:1496281373a5 53 #include "lwip/tcpip.h"
apatel336 0:1496281373a5 54 #include "lwip/pbuf.h"
apatel336 0:1496281373a5 55 #if LWIP_CHECKSUM_ON_COPY
apatel336 0:1496281373a5 56 #include "lwip/inet_chksum.h"
apatel336 0:1496281373a5 57 #endif
apatel336 0:1496281373a5 58
apatel336 0:1496281373a5 59 #include <string.h>
apatel336 0:1496281373a5 60
apatel336 0:1496281373a5 61 #define NUM_SOCKETS MEMP_NUM_NETCONN
apatel336 0:1496281373a5 62
apatel336 0:1496281373a5 63 /** Contains all internal pointers and states used for a socket */
apatel336 0:1496281373a5 64 struct lwip_sock {
apatel336 0:1496281373a5 65 /** sockets currently are built on netconns, each socket has one netconn */
apatel336 0:1496281373a5 66 struct netconn *conn;
apatel336 0:1496281373a5 67 /** data that was left from the previous read */
apatel336 0:1496281373a5 68 void *lastdata;
apatel336 0:1496281373a5 69 /** offset in the data that was left from the previous read */
apatel336 0:1496281373a5 70 u16_t lastoffset;
apatel336 0:1496281373a5 71 /** number of times data was received, set by event_callback(),
apatel336 0:1496281373a5 72 tested by the receive and select functions */
apatel336 0:1496281373a5 73 s16_t rcvevent;
apatel336 0:1496281373a5 74 /** number of times data was ACKed (free send buffer), set by event_callback(),
apatel336 0:1496281373a5 75 tested by select */
apatel336 0:1496281373a5 76 u16_t sendevent;
apatel336 0:1496281373a5 77 /** error happened for this socket, set by event_callback(), tested by select */
apatel336 0:1496281373a5 78 u16_t errevent;
apatel336 0:1496281373a5 79 /** last error that occurred on this socket */
apatel336 0:1496281373a5 80 int err;
apatel336 0:1496281373a5 81 /** counter of how many threads are waiting for this socket using select */
apatel336 0:1496281373a5 82 int select_waiting;
apatel336 0:1496281373a5 83 };
apatel336 0:1496281373a5 84
apatel336 0:1496281373a5 85 /** Description for a task waiting in select */
apatel336 0:1496281373a5 86 struct lwip_select_cb {
apatel336 0:1496281373a5 87 /** Pointer to the next waiting task */
apatel336 0:1496281373a5 88 struct lwip_select_cb *next;
apatel336 0:1496281373a5 89 /** Pointer to the previous waiting task */
apatel336 0:1496281373a5 90 struct lwip_select_cb *prev;
apatel336 0:1496281373a5 91 /** readset passed to select */
apatel336 0:1496281373a5 92 fd_set *readset;
apatel336 0:1496281373a5 93 /** writeset passed to select */
apatel336 0:1496281373a5 94 fd_set *writeset;
apatel336 0:1496281373a5 95 /** unimplemented: exceptset passed to select */
apatel336 0:1496281373a5 96 fd_set *exceptset;
apatel336 0:1496281373a5 97 /** don't signal the same semaphore twice: set to 1 when signalled */
apatel336 0:1496281373a5 98 int sem_signalled;
apatel336 0:1496281373a5 99 /** semaphore to wake up a task waiting for select */
apatel336 0:1496281373a5 100 sys_sem_t sem;
apatel336 0:1496281373a5 101 };
apatel336 0:1496281373a5 102
apatel336 0:1496281373a5 103 /** This struct is used to pass data to the set/getsockopt_internal
apatel336 0:1496281373a5 104 * functions running in tcpip_thread context (only a void* is allowed) */
apatel336 0:1496281373a5 105 struct lwip_setgetsockopt_data {
apatel336 0:1496281373a5 106 /** socket struct for which to change options */
apatel336 0:1496281373a5 107 struct lwip_sock *sock;
apatel336 0:1496281373a5 108 #ifdef LWIP_DEBUG
apatel336 0:1496281373a5 109 /** socket index for which to change options */
apatel336 0:1496281373a5 110 int s;
apatel336 0:1496281373a5 111 #endif /* LWIP_DEBUG */
apatel336 0:1496281373a5 112 /** level of the option to process */
apatel336 0:1496281373a5 113 int level;
apatel336 0:1496281373a5 114 /** name of the option to process */
apatel336 0:1496281373a5 115 int optname;
apatel336 0:1496281373a5 116 /** set: value to set the option to
apatel336 0:1496281373a5 117 * get: value of the option is stored here */
apatel336 0:1496281373a5 118 void *optval;
apatel336 0:1496281373a5 119 /** size of *optval */
apatel336 0:1496281373a5 120 socklen_t *optlen;
apatel336 0:1496281373a5 121 /** if an error occures, it is temporarily stored here */
apatel336 0:1496281373a5 122 err_t err;
apatel336 0:1496281373a5 123 };
apatel336 0:1496281373a5 124
apatel336 0:1496281373a5 125 /** The global array of available sockets */
apatel336 0:1496281373a5 126 static struct lwip_sock sockets[NUM_SOCKETS];
apatel336 0:1496281373a5 127 /** The global list of tasks waiting for select */
apatel336 0:1496281373a5 128 static struct lwip_select_cb *select_cb_list;
apatel336 0:1496281373a5 129 /** This counter is increased from lwip_select when the list is chagned
apatel336 0:1496281373a5 130 and checked in event_callback to see if it has changed. */
apatel336 0:1496281373a5 131 static volatile int select_cb_ctr;
apatel336 0:1496281373a5 132
apatel336 0:1496281373a5 133 /** Table to quickly map an lwIP error (err_t) to a socket error
apatel336 0:1496281373a5 134 * by using -err as an index */
apatel336 0:1496281373a5 135 static const int err_to_errno_table[] = {
apatel336 0:1496281373a5 136 0, /* ERR_OK 0 No error, everything OK. */
apatel336 0:1496281373a5 137 ENOMEM, /* ERR_MEM -1 Out of memory error. */
apatel336 0:1496281373a5 138 ENOBUFS, /* ERR_BUF -2 Buffer error. */
apatel336 0:1496281373a5 139 EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */
apatel336 0:1496281373a5 140 EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */
apatel336 0:1496281373a5 141 EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */
apatel336 0:1496281373a5 142 EINVAL, /* ERR_VAL -6 Illegal value. */
apatel336 0:1496281373a5 143 EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block. */
apatel336 0:1496281373a5 144 EADDRINUSE, /* ERR_USE -8 Address in use. */
apatel336 0:1496281373a5 145 EALREADY, /* ERR_ISCONN -9 Already connected. */
apatel336 0:1496281373a5 146 ECONNABORTED, /* ERR_ABRT -10 Connection aborted. */
apatel336 0:1496281373a5 147 ECONNRESET, /* ERR_RST -11 Connection reset. */
apatel336 0:1496281373a5 148 ENOTCONN, /* ERR_CLSD -12 Connection closed. */
apatel336 0:1496281373a5 149 ENOTCONN, /* ERR_CONN -13 Not connected. */
apatel336 0:1496281373a5 150 EIO, /* ERR_ARG -14 Illegal argument. */
apatel336 0:1496281373a5 151 -1, /* ERR_IF -15 Low-level netif error */
apatel336 0:1496281373a5 152 };
apatel336 0:1496281373a5 153
apatel336 0:1496281373a5 154 #define ERR_TO_ERRNO_TABLE_SIZE \
apatel336 0:1496281373a5 155 (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0]))
apatel336 0:1496281373a5 156
apatel336 0:1496281373a5 157 #define err_to_errno(err) \
apatel336 0:1496281373a5 158 ((unsigned)(-(err)) < ERR_TO_ERRNO_TABLE_SIZE ? \
apatel336 0:1496281373a5 159 err_to_errno_table[-(err)] : EIO)
apatel336 0:1496281373a5 160
apatel336 0:1496281373a5 161 #ifdef ERRNO
apatel336 0:1496281373a5 162 #ifndef set_errno
apatel336 0:1496281373a5 163 #define set_errno(err) errno = (err)
apatel336 0:1496281373a5 164 #endif
apatel336 0:1496281373a5 165 #else /* ERRNO */
apatel336 0:1496281373a5 166 #define set_errno(err)
apatel336 0:1496281373a5 167 #endif /* ERRNO */
apatel336 0:1496281373a5 168
apatel336 0:1496281373a5 169 #define sock_set_errno(sk, e) do { \
apatel336 0:1496281373a5 170 sk->err = (e); \
apatel336 0:1496281373a5 171 set_errno(sk->err); \
apatel336 0:1496281373a5 172 } while (0)
apatel336 0:1496281373a5 173
apatel336 0:1496281373a5 174 /* Forward delcaration of some functions */
apatel336 0:1496281373a5 175 static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
apatel336 0:1496281373a5 176 static void lwip_getsockopt_internal(void *arg);
apatel336 0:1496281373a5 177 static void lwip_setsockopt_internal(void *arg);
apatel336 0:1496281373a5 178
apatel336 0:1496281373a5 179 /**
apatel336 0:1496281373a5 180 * Initialize this module. This function has to be called before any other
apatel336 0:1496281373a5 181 * functions in this module!
apatel336 0:1496281373a5 182 */
apatel336 0:1496281373a5 183 void
apatel336 0:1496281373a5 184 lwip_socket_init(void)
apatel336 0:1496281373a5 185 {
apatel336 0:1496281373a5 186 }
apatel336 0:1496281373a5 187
apatel336 0:1496281373a5 188 /**
apatel336 0:1496281373a5 189 * Map a externally used socket index to the internal socket representation.
apatel336 0:1496281373a5 190 *
apatel336 0:1496281373a5 191 * @param s externally used socket index
apatel336 0:1496281373a5 192 * @return struct lwip_sock for the socket or NULL if not found
apatel336 0:1496281373a5 193 */
apatel336 0:1496281373a5 194 static struct lwip_sock *
apatel336 0:1496281373a5 195 get_socket(int s)
apatel336 0:1496281373a5 196 {
apatel336 0:1496281373a5 197 struct lwip_sock *sock;
apatel336 0:1496281373a5 198
apatel336 0:1496281373a5 199 if ((s < 0) || (s >= NUM_SOCKETS)) {
apatel336 0:1496281373a5 200 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
apatel336 0:1496281373a5 201 set_errno(EBADF);
apatel336 0:1496281373a5 202 return NULL;
apatel336 0:1496281373a5 203 }
apatel336 0:1496281373a5 204
apatel336 0:1496281373a5 205 sock = &sockets[s];
apatel336 0:1496281373a5 206
apatel336 0:1496281373a5 207 if (!sock->conn) {
apatel336 0:1496281373a5 208 LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
apatel336 0:1496281373a5 209 set_errno(EBADF);
apatel336 0:1496281373a5 210 return NULL;
apatel336 0:1496281373a5 211 }
apatel336 0:1496281373a5 212
apatel336 0:1496281373a5 213 return sock;
apatel336 0:1496281373a5 214 }
apatel336 0:1496281373a5 215
apatel336 0:1496281373a5 216 /**
apatel336 0:1496281373a5 217 * Same as get_socket but doesn't set errno
apatel336 0:1496281373a5 218 *
apatel336 0:1496281373a5 219 * @param s externally used socket index
apatel336 0:1496281373a5 220 * @return struct lwip_sock for the socket or NULL if not found
apatel336 0:1496281373a5 221 */
apatel336 0:1496281373a5 222 static struct lwip_sock *
apatel336 0:1496281373a5 223 tryget_socket(int s)
apatel336 0:1496281373a5 224 {
apatel336 0:1496281373a5 225 if ((s < 0) || (s >= NUM_SOCKETS)) {
apatel336 0:1496281373a5 226 return NULL;
apatel336 0:1496281373a5 227 }
apatel336 0:1496281373a5 228 if (!sockets[s].conn) {
apatel336 0:1496281373a5 229 return NULL;
apatel336 0:1496281373a5 230 }
apatel336 0:1496281373a5 231 return &sockets[s];
apatel336 0:1496281373a5 232 }
apatel336 0:1496281373a5 233
apatel336 0:1496281373a5 234 /**
apatel336 0:1496281373a5 235 * Allocate a new socket for a given netconn.
apatel336 0:1496281373a5 236 *
apatel336 0:1496281373a5 237 * @param newconn the netconn for which to allocate a socket
apatel336 0:1496281373a5 238 * @param accepted 1 if socket has been created by accept(),
apatel336 0:1496281373a5 239 * 0 if socket has been created by socket()
apatel336 0:1496281373a5 240 * @return the index of the new socket; -1 on error
apatel336 0:1496281373a5 241 */
apatel336 0:1496281373a5 242 static int
apatel336 0:1496281373a5 243 alloc_socket(struct netconn *newconn, int accepted)
apatel336 0:1496281373a5 244 {
apatel336 0:1496281373a5 245 int i;
apatel336 0:1496281373a5 246 SYS_ARCH_DECL_PROTECT(lev);
apatel336 0:1496281373a5 247
apatel336 0:1496281373a5 248 /* allocate a new socket identifier */
apatel336 0:1496281373a5 249 for (i = 0; i < NUM_SOCKETS; ++i) {
apatel336 0:1496281373a5 250 /* Protect socket array */
apatel336 0:1496281373a5 251 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 252 if (!sockets[i].conn) {
apatel336 0:1496281373a5 253 sockets[i].conn = newconn;
apatel336 0:1496281373a5 254 /* The socket is not yet known to anyone, so no need to protect
apatel336 0:1496281373a5 255 after having marked it as used. */
apatel336 0:1496281373a5 256 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 257 sockets[i].lastdata = NULL;
apatel336 0:1496281373a5 258 sockets[i].lastoffset = 0;
apatel336 0:1496281373a5 259 sockets[i].rcvevent = 0;
apatel336 0:1496281373a5 260 /* TCP sendbuf is empty, but the socket is not yet writable until connected
apatel336 0:1496281373a5 261 * (unless it has been created by accept()). */
apatel336 0:1496281373a5 262 sockets[i].sendevent = (newconn->type == NETCONN_TCP ? (accepted != 0) : 1);
apatel336 0:1496281373a5 263 sockets[i].errevent = 0;
apatel336 0:1496281373a5 264 sockets[i].err = 0;
apatel336 0:1496281373a5 265 sockets[i].select_waiting = 0;
apatel336 0:1496281373a5 266 return i;
apatel336 0:1496281373a5 267 }
apatel336 0:1496281373a5 268 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 269 }
apatel336 0:1496281373a5 270 return -1;
apatel336 0:1496281373a5 271 }
apatel336 0:1496281373a5 272
apatel336 0:1496281373a5 273 /** Free a socket. The socket's netconn must have been
apatel336 0:1496281373a5 274 * delete before!
apatel336 0:1496281373a5 275 *
apatel336 0:1496281373a5 276 * @param sock the socket to free
apatel336 0:1496281373a5 277 * @param is_tcp != 0 for TCP sockets, used to free lastdata
apatel336 0:1496281373a5 278 */
apatel336 0:1496281373a5 279 static void
apatel336 0:1496281373a5 280 free_socket(struct lwip_sock *sock, int is_tcp)
apatel336 0:1496281373a5 281 {
apatel336 0:1496281373a5 282 void *lastdata;
apatel336 0:1496281373a5 283 SYS_ARCH_DECL_PROTECT(lev);
apatel336 0:1496281373a5 284
apatel336 0:1496281373a5 285 lastdata = sock->lastdata;
apatel336 0:1496281373a5 286 sock->lastdata = NULL;
apatel336 0:1496281373a5 287 sock->lastoffset = 0;
apatel336 0:1496281373a5 288 sock->err = 0;
apatel336 0:1496281373a5 289
apatel336 0:1496281373a5 290 /* Protect socket array */
apatel336 0:1496281373a5 291 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 292 sock->conn = NULL;
apatel336 0:1496281373a5 293 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 294 /* don't use 'sock' after this line, as another task might have allocated it */
apatel336 0:1496281373a5 295
apatel336 0:1496281373a5 296 if (lastdata != NULL) {
apatel336 0:1496281373a5 297 if (is_tcp) {
apatel336 0:1496281373a5 298 pbuf_free((struct pbuf *)lastdata);
apatel336 0:1496281373a5 299 } else {
apatel336 0:1496281373a5 300 netbuf_delete((struct netbuf *)lastdata);
apatel336 0:1496281373a5 301 }
apatel336 0:1496281373a5 302 }
apatel336 0:1496281373a5 303 }
apatel336 0:1496281373a5 304
apatel336 0:1496281373a5 305 /* Below this, the well-known socket functions are implemented.
apatel336 0:1496281373a5 306 * Use google.com or opengroup.org to get a good description :-)
apatel336 0:1496281373a5 307 *
apatel336 0:1496281373a5 308 * Exceptions are documented!
apatel336 0:1496281373a5 309 */
apatel336 0:1496281373a5 310
apatel336 0:1496281373a5 311 int
apatel336 0:1496281373a5 312 lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
apatel336 0:1496281373a5 313 {
apatel336 0:1496281373a5 314 struct lwip_sock *sock, *nsock;
apatel336 0:1496281373a5 315 struct netconn *newconn;
apatel336 0:1496281373a5 316 ip_addr_t naddr;
apatel336 0:1496281373a5 317 u16_t port;
apatel336 0:1496281373a5 318 int newsock;
apatel336 0:1496281373a5 319 struct sockaddr_in sin;
apatel336 0:1496281373a5 320 err_t err;
apatel336 0:1496281373a5 321 SYS_ARCH_DECL_PROTECT(lev);
apatel336 0:1496281373a5 322
apatel336 0:1496281373a5 323 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
apatel336 0:1496281373a5 324 sock = get_socket(s);
apatel336 0:1496281373a5 325 if (!sock) {
apatel336 0:1496281373a5 326 return -1;
apatel336 0:1496281373a5 327 }
apatel336 0:1496281373a5 328
apatel336 0:1496281373a5 329 if (netconn_is_nonblocking(sock->conn) && (sock->rcvevent <= 0)) {
apatel336 0:1496281373a5 330 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): returning EWOULDBLOCK\n", s));
apatel336 0:1496281373a5 331 sock_set_errno(sock, EWOULDBLOCK);
apatel336 0:1496281373a5 332 return -1;
apatel336 0:1496281373a5 333 }
apatel336 0:1496281373a5 334
apatel336 0:1496281373a5 335 /* wait for a new connection */
apatel336 0:1496281373a5 336 err = netconn_accept(sock->conn, &newconn);
apatel336 0:1496281373a5 337 if (err != ERR_OK) {
apatel336 0:1496281373a5 338 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_acept failed, err=%d\n", s, err));
apatel336 0:1496281373a5 339 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 340 return -1;
apatel336 0:1496281373a5 341 }
apatel336 0:1496281373a5 342 LWIP_ASSERT("newconn != NULL", newconn != NULL);
apatel336 0:1496281373a5 343 /* Prevent automatic window updates, we do this on our own! */
apatel336 0:1496281373a5 344 netconn_set_noautorecved(newconn, 1);
apatel336 0:1496281373a5 345
apatel336 0:1496281373a5 346 /* get the IP address and port of the remote host */
apatel336 0:1496281373a5 347 err = netconn_peer(newconn, &naddr, &port);
apatel336 0:1496281373a5 348 if (err != ERR_OK) {
apatel336 0:1496281373a5 349 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_peer failed, err=%d\n", s, err));
apatel336 0:1496281373a5 350 netconn_delete(newconn);
apatel336 0:1496281373a5 351 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 352 return -1;
apatel336 0:1496281373a5 353 }
apatel336 0:1496281373a5 354
apatel336 0:1496281373a5 355 /* Note that POSIX only requires us to check addr is non-NULL. addrlen must
apatel336 0:1496281373a5 356 * not be NULL if addr is valid.
apatel336 0:1496281373a5 357 */
apatel336 0:1496281373a5 358 if (NULL != addr) {
apatel336 0:1496281373a5 359 LWIP_ASSERT("addr valid but addrlen NULL", addrlen != NULL);
apatel336 0:1496281373a5 360 memset(&sin, 0, sizeof(sin));
apatel336 0:1496281373a5 361 sin.sin_len = sizeof(sin);
apatel336 0:1496281373a5 362 sin.sin_family = AF_INET;
apatel336 0:1496281373a5 363 sin.sin_port = htons(port);
apatel336 0:1496281373a5 364 inet_addr_from_ipaddr(&sin.sin_addr, &naddr);
apatel336 0:1496281373a5 365
apatel336 0:1496281373a5 366 if (*addrlen > sizeof(sin))
apatel336 0:1496281373a5 367 *addrlen = sizeof(sin);
apatel336 0:1496281373a5 368
apatel336 0:1496281373a5 369 MEMCPY(addr, &sin, *addrlen);
apatel336 0:1496281373a5 370 }
apatel336 0:1496281373a5 371
apatel336 0:1496281373a5 372 newsock = alloc_socket(newconn, 1);
apatel336 0:1496281373a5 373 if (newsock == -1) {
apatel336 0:1496281373a5 374 netconn_delete(newconn);
apatel336 0:1496281373a5 375 sock_set_errno(sock, ENFILE);
apatel336 0:1496281373a5 376 return -1;
apatel336 0:1496281373a5 377 }
apatel336 0:1496281373a5 378 LWIP_ASSERT("invalid socket index", (newsock >= 0) && (newsock < NUM_SOCKETS));
apatel336 0:1496281373a5 379 LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback);
apatel336 0:1496281373a5 380 nsock = &sockets[newsock];
apatel336 0:1496281373a5 381
apatel336 0:1496281373a5 382 /* See event_callback: If data comes in right away after an accept, even
apatel336 0:1496281373a5 383 * though the server task might not have created a new socket yet.
apatel336 0:1496281373a5 384 * In that case, newconn->socket is counted down (newconn->socket--),
apatel336 0:1496281373a5 385 * so nsock->rcvevent is >= 1 here!
apatel336 0:1496281373a5 386 */
apatel336 0:1496281373a5 387 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 388 nsock->rcvevent += (s16_t)(-1 - newconn->socket);
apatel336 0:1496281373a5 389 newconn->socket = newsock;
apatel336 0:1496281373a5 390 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 391
apatel336 0:1496281373a5 392 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
apatel336 0:1496281373a5 393 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
apatel336 0:1496281373a5 394 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", port));
apatel336 0:1496281373a5 395
apatel336 0:1496281373a5 396 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 397 return newsock;
apatel336 0:1496281373a5 398 }
apatel336 0:1496281373a5 399
apatel336 0:1496281373a5 400 int
apatel336 0:1496281373a5 401 lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
apatel336 0:1496281373a5 402 {
apatel336 0:1496281373a5 403 struct lwip_sock *sock;
apatel336 0:1496281373a5 404 ip_addr_t local_addr;
apatel336 0:1496281373a5 405 u16_t local_port;
apatel336 0:1496281373a5 406 err_t err;
apatel336 0:1496281373a5 407 const struct sockaddr_in *name_in;
apatel336 0:1496281373a5 408
apatel336 0:1496281373a5 409 sock = get_socket(s);
apatel336 0:1496281373a5 410 if (!sock) {
apatel336 0:1496281373a5 411 return -1;
apatel336 0:1496281373a5 412 }
apatel336 0:1496281373a5 413
apatel336 0:1496281373a5 414 /* check size, familiy and alignment of 'name' */
apatel336 0:1496281373a5 415 LWIP_ERROR("lwip_bind: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
apatel336 0:1496281373a5 416 ((name->sa_family) == AF_INET) && ((((mem_ptr_t)name) % 4) == 0)),
apatel336 0:1496281373a5 417 sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
apatel336 0:1496281373a5 418 name_in = (const struct sockaddr_in *)(void*)name;
apatel336 0:1496281373a5 419
apatel336 0:1496281373a5 420 inet_addr_to_ipaddr(&local_addr, &name_in->sin_addr);
apatel336 0:1496281373a5 421 local_port = name_in->sin_port;
apatel336 0:1496281373a5 422
apatel336 0:1496281373a5 423 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
apatel336 0:1496281373a5 424 ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
apatel336 0:1496281373a5 425 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(local_port)));
apatel336 0:1496281373a5 426
apatel336 0:1496281373a5 427 err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
apatel336 0:1496281373a5 428
apatel336 0:1496281373a5 429 if (err != ERR_OK) {
apatel336 0:1496281373a5 430 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
apatel336 0:1496281373a5 431 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 432 return -1;
apatel336 0:1496281373a5 433 }
apatel336 0:1496281373a5 434
apatel336 0:1496281373a5 435 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
apatel336 0:1496281373a5 436 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 437 return 0;
apatel336 0:1496281373a5 438 }
apatel336 0:1496281373a5 439
apatel336 0:1496281373a5 440 int
apatel336 0:1496281373a5 441 lwip_close(int s)
apatel336 0:1496281373a5 442 {
apatel336 0:1496281373a5 443 struct lwip_sock *sock;
apatel336 0:1496281373a5 444 int is_tcp = 0;
apatel336 0:1496281373a5 445
apatel336 0:1496281373a5 446 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
apatel336 0:1496281373a5 447
apatel336 0:1496281373a5 448 sock = get_socket(s);
apatel336 0:1496281373a5 449 if (!sock) {
apatel336 0:1496281373a5 450 return -1;
apatel336 0:1496281373a5 451 }
apatel336 0:1496281373a5 452
apatel336 0:1496281373a5 453 if(sock->conn != NULL) {
apatel336 0:1496281373a5 454 is_tcp = netconn_type(sock->conn) == NETCONN_TCP;
apatel336 0:1496281373a5 455 } else {
apatel336 0:1496281373a5 456 LWIP_ASSERT("sock->lastdata == NULL", sock->lastdata == NULL);
apatel336 0:1496281373a5 457 }
apatel336 0:1496281373a5 458
apatel336 0:1496281373a5 459 netconn_delete(sock->conn);
apatel336 0:1496281373a5 460
apatel336 0:1496281373a5 461 free_socket(sock, is_tcp);
apatel336 0:1496281373a5 462 set_errno(0);
apatel336 0:1496281373a5 463 return 0;
apatel336 0:1496281373a5 464 }
apatel336 0:1496281373a5 465
apatel336 0:1496281373a5 466 int
apatel336 0:1496281373a5 467 lwip_connect(int s, const struct sockaddr *name, socklen_t namelen)
apatel336 0:1496281373a5 468 {
apatel336 0:1496281373a5 469 struct lwip_sock *sock;
apatel336 0:1496281373a5 470 err_t err;
apatel336 0:1496281373a5 471 const struct sockaddr_in *name_in;
apatel336 0:1496281373a5 472
apatel336 0:1496281373a5 473 sock = get_socket(s);
apatel336 0:1496281373a5 474 if (!sock) {
apatel336 0:1496281373a5 475 return -1;
apatel336 0:1496281373a5 476 }
apatel336 0:1496281373a5 477
apatel336 0:1496281373a5 478 /* check size, familiy and alignment of 'name' */
apatel336 0:1496281373a5 479 LWIP_ERROR("lwip_connect: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
apatel336 0:1496281373a5 480 ((name->sa_family) == AF_INET) && ((((mem_ptr_t)name) % 4) == 0)),
apatel336 0:1496281373a5 481 sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
apatel336 0:1496281373a5 482 name_in = (const struct sockaddr_in *)(void*)name;
apatel336 0:1496281373a5 483
apatel336 0:1496281373a5 484 if (name_in->sin_family == AF_UNSPEC) {
apatel336 0:1496281373a5 485 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
apatel336 0:1496281373a5 486 err = netconn_disconnect(sock->conn);
apatel336 0:1496281373a5 487 } else {
apatel336 0:1496281373a5 488 ip_addr_t remote_addr;
apatel336 0:1496281373a5 489 u16_t remote_port;
apatel336 0:1496281373a5 490
apatel336 0:1496281373a5 491 inet_addr_to_ipaddr(&remote_addr, &name_in->sin_addr);
apatel336 0:1496281373a5 492 remote_port = name_in->sin_port;
apatel336 0:1496281373a5 493
apatel336 0:1496281373a5 494 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
apatel336 0:1496281373a5 495 ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
apatel336 0:1496281373a5 496 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(remote_port)));
apatel336 0:1496281373a5 497
apatel336 0:1496281373a5 498 err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
apatel336 0:1496281373a5 499 }
apatel336 0:1496281373a5 500
apatel336 0:1496281373a5 501 if (err != ERR_OK) {
apatel336 0:1496281373a5 502 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
apatel336 0:1496281373a5 503 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 504 return -1;
apatel336 0:1496281373a5 505 }
apatel336 0:1496281373a5 506
apatel336 0:1496281373a5 507 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
apatel336 0:1496281373a5 508 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 509 return 0;
apatel336 0:1496281373a5 510 }
apatel336 0:1496281373a5 511
apatel336 0:1496281373a5 512 /**
apatel336 0:1496281373a5 513 * Set a socket into listen mode.
apatel336 0:1496281373a5 514 * The socket may not have been used for another connection previously.
apatel336 0:1496281373a5 515 *
apatel336 0:1496281373a5 516 * @param s the socket to set to listening mode
apatel336 0:1496281373a5 517 * @param backlog (ATTENTION: needs TCP_LISTEN_BACKLOG=1)
apatel336 0:1496281373a5 518 * @return 0 on success, non-zero on failure
apatel336 0:1496281373a5 519 */
apatel336 0:1496281373a5 520 int
apatel336 0:1496281373a5 521 lwip_listen(int s, int backlog)
apatel336 0:1496281373a5 522 {
apatel336 0:1496281373a5 523 struct lwip_sock *sock;
apatel336 0:1496281373a5 524 err_t err;
apatel336 0:1496281373a5 525
apatel336 0:1496281373a5 526 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
apatel336 0:1496281373a5 527
apatel336 0:1496281373a5 528 sock = get_socket(s);
apatel336 0:1496281373a5 529 if (!sock) {
apatel336 0:1496281373a5 530 return -1;
apatel336 0:1496281373a5 531 }
apatel336 0:1496281373a5 532
apatel336 0:1496281373a5 533 /* limit the "backlog" parameter to fit in an u8_t */
apatel336 0:1496281373a5 534 backlog = LWIP_MIN(LWIP_MAX(backlog, 0), 0xff);
apatel336 0:1496281373a5 535
apatel336 0:1496281373a5 536 err = netconn_listen_with_backlog(sock->conn, (u8_t)backlog);
apatel336 0:1496281373a5 537
apatel336 0:1496281373a5 538 if (err != ERR_OK) {
apatel336 0:1496281373a5 539 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
apatel336 0:1496281373a5 540 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 541 return -1;
apatel336 0:1496281373a5 542 }
apatel336 0:1496281373a5 543
apatel336 0:1496281373a5 544 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 545 return 0;
apatel336 0:1496281373a5 546 }
apatel336 0:1496281373a5 547
apatel336 0:1496281373a5 548 int
apatel336 0:1496281373a5 549 lwip_recvfrom(int s, void *mem, size_t len, int flags,
apatel336 0:1496281373a5 550 struct sockaddr *from, socklen_t *fromlen)
apatel336 0:1496281373a5 551 {
apatel336 0:1496281373a5 552 struct lwip_sock *sock;
apatel336 0:1496281373a5 553 void *buf = NULL;
apatel336 0:1496281373a5 554 struct pbuf *p;
apatel336 0:1496281373a5 555 u16_t buflen, copylen;
apatel336 0:1496281373a5 556 int off = 0;
apatel336 0:1496281373a5 557 ip_addr_t *addr;
apatel336 0:1496281373a5 558 u16_t port;
apatel336 0:1496281373a5 559 u8_t done = 0;
apatel336 0:1496281373a5 560 err_t err;
apatel336 0:1496281373a5 561
apatel336 0:1496281373a5 562 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %"SZT_F", 0x%x, ..)\n", s, mem, len, flags));
apatel336 0:1496281373a5 563 sock = get_socket(s);
apatel336 0:1496281373a5 564 if (!sock) {
apatel336 0:1496281373a5 565 return -1;
apatel336 0:1496281373a5 566 }
apatel336 0:1496281373a5 567
apatel336 0:1496281373a5 568 do {
apatel336 0:1496281373a5 569 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: top while sock->lastdata=%p\n", sock->lastdata));
apatel336 0:1496281373a5 570 /* Check if there is data left from the last recv operation. */
apatel336 0:1496281373a5 571 if (sock->lastdata) {
apatel336 0:1496281373a5 572 buf = sock->lastdata;
apatel336 0:1496281373a5 573 } else {
apatel336 0:1496281373a5 574 /* If this is non-blocking call, then check first */
apatel336 0:1496281373a5 575 if (((flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn)) &&
apatel336 0:1496281373a5 576 (sock->rcvevent <= 0)) {
apatel336 0:1496281373a5 577 if (off > 0) {
apatel336 0:1496281373a5 578 /* update receive window */
apatel336 0:1496281373a5 579 netconn_recved(sock->conn, (u32_t)off);
apatel336 0:1496281373a5 580 /* already received data, return that */
apatel336 0:1496281373a5 581 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 582 return off;
apatel336 0:1496281373a5 583 }
apatel336 0:1496281373a5 584 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
apatel336 0:1496281373a5 585 sock_set_errno(sock, EWOULDBLOCK);
apatel336 0:1496281373a5 586 return -1;
apatel336 0:1496281373a5 587 }
apatel336 0:1496281373a5 588
apatel336 0:1496281373a5 589 /* No data was left from the previous operation, so we try to get
apatel336 0:1496281373a5 590 some from the network. */
apatel336 0:1496281373a5 591 if (netconn_type(sock->conn) == NETCONN_TCP) {
apatel336 0:1496281373a5 592 err = netconn_recv_tcp_pbuf(sock->conn, (struct pbuf **)&buf);
apatel336 0:1496281373a5 593 } else {
apatel336 0:1496281373a5 594 err = netconn_recv(sock->conn, (struct netbuf **)&buf);
apatel336 0:1496281373a5 595 }
apatel336 0:1496281373a5 596 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: netconn_recv err=%d, netbuf=%p\n",
apatel336 0:1496281373a5 597 err, buf));
apatel336 0:1496281373a5 598
apatel336 0:1496281373a5 599 if (err != ERR_OK) {
apatel336 0:1496281373a5 600 if (off > 0) {
apatel336 0:1496281373a5 601 /* update receive window */
apatel336 0:1496281373a5 602 netconn_recved(sock->conn, (u32_t)off);
apatel336 0:1496281373a5 603 /* already received data, return that */
apatel336 0:1496281373a5 604 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 605 return off;
apatel336 0:1496281373a5 606 }
apatel336 0:1496281373a5 607 /* We should really do some error checking here. */
apatel336 0:1496281373a5 608 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL, error is \"%s\"!\n",
apatel336 0:1496281373a5 609 s, lwip_strerr(err)));
apatel336 0:1496281373a5 610 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 611 if (err == ERR_CLSD) {
apatel336 0:1496281373a5 612 return 0;
apatel336 0:1496281373a5 613 } else {
apatel336 0:1496281373a5 614 return -1;
apatel336 0:1496281373a5 615 }
apatel336 0:1496281373a5 616 }
apatel336 0:1496281373a5 617 LWIP_ASSERT("buf != NULL", buf != NULL);
apatel336 0:1496281373a5 618 sock->lastdata = buf;
apatel336 0:1496281373a5 619 }
apatel336 0:1496281373a5 620
apatel336 0:1496281373a5 621 if (netconn_type(sock->conn) == NETCONN_TCP) {
apatel336 0:1496281373a5 622 p = (struct pbuf *)buf;
apatel336 0:1496281373a5 623 } else {
apatel336 0:1496281373a5 624 p = ((struct netbuf *)buf)->p;
apatel336 0:1496281373a5 625 }
apatel336 0:1496281373a5 626 buflen = p->tot_len;
apatel336 0:1496281373a5 627 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: buflen=%"U16_F" len=%"SZT_F" off=%d sock->lastoffset=%"U16_F"\n",
apatel336 0:1496281373a5 628 buflen, len, off, sock->lastoffset));
apatel336 0:1496281373a5 629
apatel336 0:1496281373a5 630 buflen -= sock->lastoffset;
apatel336 0:1496281373a5 631
apatel336 0:1496281373a5 632 if (len > buflen) {
apatel336 0:1496281373a5 633 copylen = buflen;
apatel336 0:1496281373a5 634 } else {
apatel336 0:1496281373a5 635 copylen = (u16_t)len;
apatel336 0:1496281373a5 636 }
apatel336 0:1496281373a5 637
apatel336 0:1496281373a5 638 /* copy the contents of the received buffer into
apatel336 0:1496281373a5 639 the supplied memory pointer mem */
apatel336 0:1496281373a5 640 pbuf_copy_partial(p, (u8_t*)mem + off, copylen, sock->lastoffset);
apatel336 0:1496281373a5 641
apatel336 0:1496281373a5 642 off += copylen;
apatel336 0:1496281373a5 643
apatel336 0:1496281373a5 644 if (netconn_type(sock->conn) == NETCONN_TCP) {
apatel336 0:1496281373a5 645 LWIP_ASSERT("invalid copylen, len would underflow", len >= copylen);
apatel336 0:1496281373a5 646 len -= copylen;
apatel336 0:1496281373a5 647 if ( (len <= 0) ||
apatel336 0:1496281373a5 648 (p->flags & PBUF_FLAG_PUSH) ||
apatel336 0:1496281373a5 649 (sock->rcvevent <= 0) ||
apatel336 0:1496281373a5 650 ((flags & MSG_PEEK)!=0)) {
apatel336 0:1496281373a5 651 done = 1;
apatel336 0:1496281373a5 652 }
apatel336 0:1496281373a5 653 } else {
apatel336 0:1496281373a5 654 done = 1;
apatel336 0:1496281373a5 655 }
apatel336 0:1496281373a5 656
apatel336 0:1496281373a5 657 /* Check to see from where the data was.*/
apatel336 0:1496281373a5 658 if (done) {
apatel336 0:1496281373a5 659 ip_addr_t fromaddr;
apatel336 0:1496281373a5 660 if (from && fromlen) {
apatel336 0:1496281373a5 661 struct sockaddr_in sin;
apatel336 0:1496281373a5 662
apatel336 0:1496281373a5 663 if (netconn_type(sock->conn) == NETCONN_TCP) {
apatel336 0:1496281373a5 664 addr = &fromaddr;
apatel336 0:1496281373a5 665 netconn_getaddr(sock->conn, addr, &port, 0);
apatel336 0:1496281373a5 666 } else {
apatel336 0:1496281373a5 667 addr = netbuf_fromaddr((struct netbuf *)buf);
apatel336 0:1496281373a5 668 port = netbuf_fromport((struct netbuf *)buf);
apatel336 0:1496281373a5 669 }
apatel336 0:1496281373a5 670
apatel336 0:1496281373a5 671 memset(&sin, 0, sizeof(sin));
apatel336 0:1496281373a5 672 sin.sin_len = sizeof(sin);
apatel336 0:1496281373a5 673 sin.sin_family = AF_INET;
apatel336 0:1496281373a5 674 sin.sin_port = htons(port);
apatel336 0:1496281373a5 675 inet_addr_from_ipaddr(&sin.sin_addr, addr);
apatel336 0:1496281373a5 676
apatel336 0:1496281373a5 677 if (*fromlen > sizeof(sin)) {
apatel336 0:1496281373a5 678 *fromlen = sizeof(sin);
apatel336 0:1496281373a5 679 }
apatel336 0:1496281373a5 680
apatel336 0:1496281373a5 681 MEMCPY(from, &sin, *fromlen);
apatel336 0:1496281373a5 682
apatel336 0:1496281373a5 683 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
apatel336 0:1496281373a5 684 ip_addr_debug_print(SOCKETS_DEBUG, addr);
apatel336 0:1496281373a5 685 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, off));
apatel336 0:1496281373a5 686 } else {
apatel336 0:1496281373a5 687 #if SOCKETS_DEBUG
apatel336 0:1496281373a5 688 if (netconn_type(sock->conn) == NETCONN_TCP) {
apatel336 0:1496281373a5 689 addr = &fromaddr;
apatel336 0:1496281373a5 690 netconn_getaddr(sock->conn, addr, &port, 0);
apatel336 0:1496281373a5 691 } else {
apatel336 0:1496281373a5 692 addr = netbuf_fromaddr((struct netbuf *)buf);
apatel336 0:1496281373a5 693 port = netbuf_fromport((struct netbuf *)buf);
apatel336 0:1496281373a5 694 }
apatel336 0:1496281373a5 695
apatel336 0:1496281373a5 696 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
apatel336 0:1496281373a5 697 ip_addr_debug_print(SOCKETS_DEBUG, addr);
apatel336 0:1496281373a5 698 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, off));
apatel336 0:1496281373a5 699 #endif /* SOCKETS_DEBUG */
apatel336 0:1496281373a5 700 }
apatel336 0:1496281373a5 701 }
apatel336 0:1496281373a5 702
apatel336 0:1496281373a5 703 /* If we don't peek the incoming message... */
apatel336 0:1496281373a5 704 if ((flags & MSG_PEEK) == 0) {
apatel336 0:1496281373a5 705 /* If this is a TCP socket, check if there is data left in the
apatel336 0:1496281373a5 706 buffer. If so, it should be saved in the sock structure for next
apatel336 0:1496281373a5 707 time around. */
apatel336 0:1496281373a5 708 if ((netconn_type(sock->conn) == NETCONN_TCP) && (buflen - copylen > 0)) {
apatel336 0:1496281373a5 709 sock->lastdata = buf;
apatel336 0:1496281373a5 710 sock->lastoffset += copylen;
apatel336 0:1496281373a5 711 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: lastdata now netbuf=%p\n", buf));
apatel336 0:1496281373a5 712 } else {
apatel336 0:1496281373a5 713 sock->lastdata = NULL;
apatel336 0:1496281373a5 714 sock->lastoffset = 0;
apatel336 0:1496281373a5 715 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: deleting netbuf=%p\n", buf));
apatel336 0:1496281373a5 716 if (netconn_type(sock->conn) == NETCONN_TCP) {
apatel336 0:1496281373a5 717 pbuf_free((struct pbuf *)buf);
apatel336 0:1496281373a5 718 } else {
apatel336 0:1496281373a5 719 netbuf_delete((struct netbuf *)buf);
apatel336 0:1496281373a5 720 }
apatel336 0:1496281373a5 721 }
apatel336 0:1496281373a5 722 }
apatel336 0:1496281373a5 723 } while (!done);
apatel336 0:1496281373a5 724
apatel336 0:1496281373a5 725 if (off > 0) {
apatel336 0:1496281373a5 726 /* update receive window */
apatel336 0:1496281373a5 727 netconn_recved(sock->conn, (u32_t)off);
apatel336 0:1496281373a5 728 }
apatel336 0:1496281373a5 729 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 730 return off;
apatel336 0:1496281373a5 731 }
apatel336 0:1496281373a5 732
apatel336 0:1496281373a5 733 int
apatel336 0:1496281373a5 734 lwip_read(int s, void *mem, size_t len)
apatel336 0:1496281373a5 735 {
apatel336 0:1496281373a5 736 return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
apatel336 0:1496281373a5 737 }
apatel336 0:1496281373a5 738
apatel336 0:1496281373a5 739 int
apatel336 0:1496281373a5 740 lwip_recv(int s, void *mem, size_t len, int flags)
apatel336 0:1496281373a5 741 {
apatel336 0:1496281373a5 742 return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
apatel336 0:1496281373a5 743 }
apatel336 0:1496281373a5 744
apatel336 0:1496281373a5 745 int
apatel336 0:1496281373a5 746 lwip_send(int s, const void *data, size_t size, int flags)
apatel336 0:1496281373a5 747 {
apatel336 0:1496281373a5 748 struct lwip_sock *sock;
apatel336 0:1496281373a5 749 err_t err;
apatel336 0:1496281373a5 750 u8_t write_flags;
apatel336 0:1496281373a5 751
apatel336 0:1496281373a5 752 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%"SZT_F", flags=0x%x)\n",
apatel336 0:1496281373a5 753 s, data, size, flags));
apatel336 0:1496281373a5 754
apatel336 0:1496281373a5 755 sock = get_socket(s);
apatel336 0:1496281373a5 756 if (!sock) {
apatel336 0:1496281373a5 757 return -1;
apatel336 0:1496281373a5 758 }
apatel336 0:1496281373a5 759
apatel336 0:1496281373a5 760 if (sock->conn->type != NETCONN_TCP) {
apatel336 0:1496281373a5 761 #if (LWIP_UDP || LWIP_RAW)
apatel336 0:1496281373a5 762 return lwip_sendto(s, data, size, flags, NULL, 0);
apatel336 0:1496281373a5 763 #else /* (LWIP_UDP || LWIP_RAW) */
apatel336 0:1496281373a5 764 sock_set_errno(sock, err_to_errno(ERR_ARG));
apatel336 0:1496281373a5 765 return -1;
apatel336 0:1496281373a5 766 #endif /* (LWIP_UDP || LWIP_RAW) */
apatel336 0:1496281373a5 767 }
apatel336 0:1496281373a5 768
apatel336 0:1496281373a5 769 if ((flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn)) {
apatel336 0:1496281373a5 770 if ((size > TCP_SND_BUF) || ((size / TCP_MSS) > TCP_SND_QUEUELEN)) {
apatel336 0:1496281373a5 771 /* too much data to ever send nonblocking! */
apatel336 0:1496281373a5 772 sock_set_errno(sock, EMSGSIZE);
apatel336 0:1496281373a5 773 return -1;
apatel336 0:1496281373a5 774 }
apatel336 0:1496281373a5 775 }
apatel336 0:1496281373a5 776
apatel336 0:1496281373a5 777 write_flags = NETCONN_COPY |
apatel336 0:1496281373a5 778 ((flags & MSG_MORE) ? NETCONN_MORE : 0) |
apatel336 0:1496281373a5 779 ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0);
apatel336 0:1496281373a5 780 err = netconn_write(sock->conn, data, size, write_flags);
apatel336 0:1496281373a5 781
apatel336 0:1496281373a5 782 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d size=%"SZT_F"\n", s, err, size));
apatel336 0:1496281373a5 783 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 784 return (err == ERR_OK ? (int)size : -1);
apatel336 0:1496281373a5 785 }
apatel336 0:1496281373a5 786
apatel336 0:1496281373a5 787 int
apatel336 0:1496281373a5 788 lwip_sendto(int s, const void *data, size_t size, int flags,
apatel336 0:1496281373a5 789 const struct sockaddr *to, socklen_t tolen)
apatel336 0:1496281373a5 790 {
apatel336 0:1496281373a5 791 struct lwip_sock *sock;
apatel336 0:1496281373a5 792 err_t err;
apatel336 0:1496281373a5 793 u16_t short_size;
apatel336 0:1496281373a5 794 const struct sockaddr_in *to_in;
apatel336 0:1496281373a5 795 u16_t remote_port;
apatel336 0:1496281373a5 796 #if !LWIP_TCPIP_CORE_LOCKING
apatel336 0:1496281373a5 797 struct netbuf buf;
apatel336 0:1496281373a5 798 #endif
apatel336 0:1496281373a5 799
apatel336 0:1496281373a5 800 sock = get_socket(s);
apatel336 0:1496281373a5 801 if (!sock) {
apatel336 0:1496281373a5 802 return -1;
apatel336 0:1496281373a5 803 }
apatel336 0:1496281373a5 804
apatel336 0:1496281373a5 805 if (sock->conn->type == NETCONN_TCP) {
apatel336 0:1496281373a5 806 #if LWIP_TCP
apatel336 0:1496281373a5 807 return lwip_send(s, data, size, flags);
apatel336 0:1496281373a5 808 #else /* LWIP_TCP */
apatel336 0:1496281373a5 809 LWIP_UNUSED_ARG(flags);
apatel336 0:1496281373a5 810 sock_set_errno(sock, err_to_errno(ERR_ARG));
apatel336 0:1496281373a5 811 return -1;
apatel336 0:1496281373a5 812 #endif /* LWIP_TCP */
apatel336 0:1496281373a5 813 }
apatel336 0:1496281373a5 814
apatel336 0:1496281373a5 815 /* @todo: split into multiple sendto's? */
apatel336 0:1496281373a5 816 LWIP_ASSERT("lwip_sendto: size must fit in u16_t", size <= 0xffff);
apatel336 0:1496281373a5 817 short_size = (u16_t)size;
apatel336 0:1496281373a5 818 LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) ||
apatel336 0:1496281373a5 819 ((tolen == sizeof(struct sockaddr_in)) &&
apatel336 0:1496281373a5 820 ((to->sa_family) == AF_INET) && ((((mem_ptr_t)to) % 4) == 0))),
apatel336 0:1496281373a5 821 sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
apatel336 0:1496281373a5 822 to_in = (const struct sockaddr_in *)(void*)to;
apatel336 0:1496281373a5 823
apatel336 0:1496281373a5 824 #if LWIP_TCPIP_CORE_LOCKING
apatel336 0:1496281373a5 825 /* Should only be consider like a sample or a simple way to experiment this option (no check of "to" field...) */
apatel336 0:1496281373a5 826 {
apatel336 0:1496281373a5 827 struct pbuf* p;
apatel336 0:1496281373a5 828 ip_addr_t *remote_addr;
apatel336 0:1496281373a5 829
apatel336 0:1496281373a5 830 #if LWIP_NETIF_TX_SINGLE_PBUF
apatel336 0:1496281373a5 831 p = pbuf_alloc(PBUF_TRANSPORT, short_size, PBUF_RAM);
apatel336 0:1496281373a5 832 if (p != NULL) {
apatel336 0:1496281373a5 833 #if LWIP_CHECKSUM_ON_COPY
apatel336 0:1496281373a5 834 u16_t chksum = 0;
apatel336 0:1496281373a5 835 if (sock->conn->type != NETCONN_RAW) {
apatel336 0:1496281373a5 836 chksum = LWIP_CHKSUM_COPY(p->payload, data, short_size);
apatel336 0:1496281373a5 837 } else
apatel336 0:1496281373a5 838 #endif /* LWIP_CHECKSUM_ON_COPY */
apatel336 0:1496281373a5 839 MEMCPY(p->payload, data, size);
apatel336 0:1496281373a5 840 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
apatel336 0:1496281373a5 841 p = pbuf_alloc(PBUF_TRANSPORT, short_size, PBUF_REF);
apatel336 0:1496281373a5 842 if (p != NULL) {
apatel336 0:1496281373a5 843 p->payload = (void*)data;
apatel336 0:1496281373a5 844 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
apatel336 0:1496281373a5 845
apatel336 0:1496281373a5 846 if (to_in != NULL) {
apatel336 0:1496281373a5 847 inet_addr_to_ipaddr_p(remote_addr, &to_in->sin_addr);
apatel336 0:1496281373a5 848 remote_port = ntohs(to_in->sin_port);
apatel336 0:1496281373a5 849 } else {
apatel336 0:1496281373a5 850 remote_addr = &sock->conn->pcb.raw->remote_ip;
apatel336 0:1496281373a5 851 if (sock->conn->type == NETCONN_RAW) {
apatel336 0:1496281373a5 852 remote_port = 0;
apatel336 0:1496281373a5 853 } else {
apatel336 0:1496281373a5 854 remote_port = sock->conn->pcb.udp->remote_port;
apatel336 0:1496281373a5 855 }
apatel336 0:1496281373a5 856 }
apatel336 0:1496281373a5 857
apatel336 0:1496281373a5 858 LOCK_TCPIP_CORE();
apatel336 0:1496281373a5 859 if (sock->conn->type == NETCONN_RAW) {
apatel336 0:1496281373a5 860 err = sock->conn->last_err = raw_sendto(sock->conn->pcb.raw, p, remote_addr);
apatel336 0:1496281373a5 861 } else {
apatel336 0:1496281373a5 862 #if LWIP_UDP
apatel336 0:1496281373a5 863 #if LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF
apatel336 0:1496281373a5 864 err = sock->conn->last_err = udp_sendto_chksum(sock->conn->pcb.udp, p,
apatel336 0:1496281373a5 865 remote_addr, remote_port, 1, chksum);
apatel336 0:1496281373a5 866 #else /* LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF */
apatel336 0:1496281373a5 867 err = sock->conn->last_err = udp_sendto(sock->conn->pcb.udp, p,
apatel336 0:1496281373a5 868 remote_addr, remote_port);
apatel336 0:1496281373a5 869 #endif /* LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF */
apatel336 0:1496281373a5 870 #else /* LWIP_UDP */
apatel336 0:1496281373a5 871 err = ERR_ARG;
apatel336 0:1496281373a5 872 #endif /* LWIP_UDP */
apatel336 0:1496281373a5 873 }
apatel336 0:1496281373a5 874 UNLOCK_TCPIP_CORE();
apatel336 0:1496281373a5 875
apatel336 0:1496281373a5 876 pbuf_free(p);
apatel336 0:1496281373a5 877 } else {
apatel336 0:1496281373a5 878 err = ERR_MEM;
apatel336 0:1496281373a5 879 }
apatel336 0:1496281373a5 880 }
apatel336 0:1496281373a5 881 #else /* LWIP_TCPIP_CORE_LOCKING */
apatel336 0:1496281373a5 882 /* initialize a buffer */
apatel336 0:1496281373a5 883 buf.p = buf.ptr = NULL;
apatel336 0:1496281373a5 884 #if LWIP_CHECKSUM_ON_COPY
apatel336 0:1496281373a5 885 buf.flags = 0;
apatel336 0:1496281373a5 886 #endif /* LWIP_CHECKSUM_ON_COPY */
apatel336 0:1496281373a5 887 if (to) {
apatel336 0:1496281373a5 888 inet_addr_to_ipaddr(&buf.addr, &to_in->sin_addr);
apatel336 0:1496281373a5 889 remote_port = ntohs(to_in->sin_port);
apatel336 0:1496281373a5 890 netbuf_fromport(&buf) = remote_port;
apatel336 0:1496281373a5 891 } else {
apatel336 0:1496281373a5 892 remote_port = 0;
apatel336 0:1496281373a5 893 ip_addr_set_any(&buf.addr);
apatel336 0:1496281373a5 894 netbuf_fromport(&buf) = 0;
apatel336 0:1496281373a5 895 }
apatel336 0:1496281373a5 896
apatel336 0:1496281373a5 897 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%"U16_F", flags=0x%x to=",
apatel336 0:1496281373a5 898 s, data, short_size, flags));
apatel336 0:1496281373a5 899 ip_addr_debug_print(SOCKETS_DEBUG, &buf.addr);
apatel336 0:1496281373a5 900 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port));
apatel336 0:1496281373a5 901
apatel336 0:1496281373a5 902 /* make the buffer point to the data that should be sent */
apatel336 0:1496281373a5 903 #if LWIP_NETIF_TX_SINGLE_PBUF
apatel336 0:1496281373a5 904 /* Allocate a new netbuf and copy the data into it. */
apatel336 0:1496281373a5 905 if (netbuf_alloc(&buf, short_size) == NULL) {
apatel336 0:1496281373a5 906 err = ERR_MEM;
apatel336 0:1496281373a5 907 } else {
apatel336 0:1496281373a5 908 #if LWIP_CHECKSUM_ON_COPY
apatel336 0:1496281373a5 909 if (sock->conn->type != NETCONN_RAW) {
apatel336 0:1496281373a5 910 u16_t chksum = LWIP_CHKSUM_COPY(buf.p->payload, data, short_size);
apatel336 0:1496281373a5 911 netbuf_set_chksum(&buf, chksum);
apatel336 0:1496281373a5 912 err = ERR_OK;
apatel336 0:1496281373a5 913 } else
apatel336 0:1496281373a5 914 #endif /* LWIP_CHECKSUM_ON_COPY */
apatel336 0:1496281373a5 915 {
apatel336 0:1496281373a5 916 err = netbuf_take(&buf, data, short_size);
apatel336 0:1496281373a5 917 }
apatel336 0:1496281373a5 918 }
apatel336 0:1496281373a5 919 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
apatel336 0:1496281373a5 920 err = netbuf_ref(&buf, data, short_size);
apatel336 0:1496281373a5 921 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
apatel336 0:1496281373a5 922 if (err == ERR_OK) {
apatel336 0:1496281373a5 923 /* send the data */
apatel336 0:1496281373a5 924 err = netconn_send(sock->conn, &buf);
apatel336 0:1496281373a5 925 }
apatel336 0:1496281373a5 926
apatel336 0:1496281373a5 927 /* deallocated the buffer */
apatel336 0:1496281373a5 928 netbuf_free(&buf);
apatel336 0:1496281373a5 929 #endif /* LWIP_TCPIP_CORE_LOCKING */
apatel336 0:1496281373a5 930 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 931 return (err == ERR_OK ? short_size : -1);
apatel336 0:1496281373a5 932 }
apatel336 0:1496281373a5 933
apatel336 0:1496281373a5 934 int
apatel336 0:1496281373a5 935 lwip_socket(int domain, int type, int protocol)
apatel336 0:1496281373a5 936 {
apatel336 0:1496281373a5 937 struct netconn *conn;
apatel336 0:1496281373a5 938 int i;
apatel336 0:1496281373a5 939
apatel336 0:1496281373a5 940 LWIP_UNUSED_ARG(domain);
apatel336 0:1496281373a5 941
apatel336 0:1496281373a5 942 /* create a netconn */
apatel336 0:1496281373a5 943 switch (type) {
apatel336 0:1496281373a5 944 case SOCK_RAW:
apatel336 0:1496281373a5 945 conn = netconn_new_with_proto_and_callback(NETCONN_RAW, (u8_t)protocol, event_callback);
apatel336 0:1496281373a5 946 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ",
apatel336 0:1496281373a5 947 domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
apatel336 0:1496281373a5 948 break;
apatel336 0:1496281373a5 949 case SOCK_DGRAM:
apatel336 0:1496281373a5 950 conn = netconn_new_with_callback( (protocol == IPPROTO_UDPLITE) ?
apatel336 0:1496281373a5 951 NETCONN_UDPLITE : NETCONN_UDP, event_callback);
apatel336 0:1496281373a5 952 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ",
apatel336 0:1496281373a5 953 domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
apatel336 0:1496281373a5 954 break;
apatel336 0:1496281373a5 955 case SOCK_STREAM:
apatel336 0:1496281373a5 956 conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
apatel336 0:1496281373a5 957 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ",
apatel336 0:1496281373a5 958 domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
apatel336 0:1496281373a5 959 if (conn != NULL) {
apatel336 0:1496281373a5 960 /* Prevent automatic window updates, we do this on our own! */
apatel336 0:1496281373a5 961 netconn_set_noautorecved(conn, 1);
apatel336 0:1496281373a5 962 }
apatel336 0:1496281373a5 963 break;
apatel336 0:1496281373a5 964 default:
apatel336 0:1496281373a5 965 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n",
apatel336 0:1496281373a5 966 domain, type, protocol));
apatel336 0:1496281373a5 967 set_errno(EINVAL);
apatel336 0:1496281373a5 968 return -1;
apatel336 0:1496281373a5 969 }
apatel336 0:1496281373a5 970
apatel336 0:1496281373a5 971 if (!conn) {
apatel336 0:1496281373a5 972 LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
apatel336 0:1496281373a5 973 set_errno(ENOBUFS);
apatel336 0:1496281373a5 974 return -1;
apatel336 0:1496281373a5 975 }
apatel336 0:1496281373a5 976
apatel336 0:1496281373a5 977 i = alloc_socket(conn, 0);
apatel336 0:1496281373a5 978
apatel336 0:1496281373a5 979 if (i == -1) {
apatel336 0:1496281373a5 980 netconn_delete(conn);
apatel336 0:1496281373a5 981 set_errno(ENFILE);
apatel336 0:1496281373a5 982 return -1;
apatel336 0:1496281373a5 983 }
apatel336 0:1496281373a5 984 conn->socket = i;
apatel336 0:1496281373a5 985 LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
apatel336 0:1496281373a5 986 set_errno(0);
apatel336 0:1496281373a5 987 return i;
apatel336 0:1496281373a5 988 }
apatel336 0:1496281373a5 989
apatel336 0:1496281373a5 990 int
apatel336 0:1496281373a5 991 lwip_write(int s, const void *data, size_t size)
apatel336 0:1496281373a5 992 {
apatel336 0:1496281373a5 993 return lwip_send(s, data, size, 0);
apatel336 0:1496281373a5 994 }
apatel336 0:1496281373a5 995
apatel336 0:1496281373a5 996 /**
apatel336 0:1496281373a5 997 * Go through the readset and writeset lists and see which socket of the sockets
apatel336 0:1496281373a5 998 * set in the sets has events. On return, readset, writeset and exceptset have
apatel336 0:1496281373a5 999 * the sockets enabled that had events.
apatel336 0:1496281373a5 1000 *
apatel336 0:1496281373a5 1001 * exceptset is not used for now!!!
apatel336 0:1496281373a5 1002 *
apatel336 0:1496281373a5 1003 * @param maxfdp1 the highest socket index in the sets
apatel336 0:1496281373a5 1004 * @param readset_in: set of sockets to check for read events
apatel336 0:1496281373a5 1005 * @param writeset_in: set of sockets to check for write events
apatel336 0:1496281373a5 1006 * @param exceptset_in: set of sockets to check for error events
apatel336 0:1496281373a5 1007 * @param readset_out: set of sockets that had read events
apatel336 0:1496281373a5 1008 * @param writeset_out: set of sockets that had write events
apatel336 0:1496281373a5 1009 * @param exceptset_out: set os sockets that had error events
apatel336 0:1496281373a5 1010 * @return number of sockets that had events (read/write/exception) (>= 0)
apatel336 0:1496281373a5 1011 */
apatel336 0:1496281373a5 1012 static int
apatel336 0:1496281373a5 1013 lwip_selscan(int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in,
apatel336 0:1496281373a5 1014 fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out)
apatel336 0:1496281373a5 1015 {
apatel336 0:1496281373a5 1016 int i, nready = 0;
apatel336 0:1496281373a5 1017 fd_set lreadset, lwriteset, lexceptset;
apatel336 0:1496281373a5 1018 struct lwip_sock *sock;
apatel336 0:1496281373a5 1019 SYS_ARCH_DECL_PROTECT(lev);
apatel336 0:1496281373a5 1020
apatel336 0:1496281373a5 1021 FD_ZERO(&lreadset);
apatel336 0:1496281373a5 1022 FD_ZERO(&lwriteset);
apatel336 0:1496281373a5 1023 FD_ZERO(&lexceptset);
apatel336 0:1496281373a5 1024
apatel336 0:1496281373a5 1025 /* Go through each socket in each list to count number of sockets which
apatel336 0:1496281373a5 1026 currently match */
apatel336 0:1496281373a5 1027 for(i = 0; i < maxfdp1; i++) {
apatel336 0:1496281373a5 1028 void* lastdata = NULL;
apatel336 0:1496281373a5 1029 s16_t rcvevent = 0;
apatel336 0:1496281373a5 1030 u16_t sendevent = 0;
apatel336 0:1496281373a5 1031 u16_t errevent = 0;
apatel336 0:1496281373a5 1032 /* First get the socket's status (protected)... */
apatel336 0:1496281373a5 1033 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1034 sock = tryget_socket(i);
apatel336 0:1496281373a5 1035 if (sock != NULL) {
apatel336 0:1496281373a5 1036 lastdata = sock->lastdata;
apatel336 0:1496281373a5 1037 rcvevent = sock->rcvevent;
apatel336 0:1496281373a5 1038 sendevent = sock->sendevent;
apatel336 0:1496281373a5 1039 errevent = sock->errevent;
apatel336 0:1496281373a5 1040 }
apatel336 0:1496281373a5 1041 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1042 /* ... then examine it: */
apatel336 0:1496281373a5 1043 /* See if netconn of this socket is ready for read */
apatel336 0:1496281373a5 1044 if (readset_in && FD_ISSET(i, readset_in) && ((lastdata != NULL) || (rcvevent > 0))) {
apatel336 0:1496281373a5 1045 FD_SET(i, &lreadset);
apatel336 0:1496281373a5 1046 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
apatel336 0:1496281373a5 1047 nready++;
apatel336 0:1496281373a5 1048 }
apatel336 0:1496281373a5 1049 /* See if netconn of this socket is ready for write */
apatel336 0:1496281373a5 1050 if (writeset_in && FD_ISSET(i, writeset_in) && (sendevent != 0)) {
apatel336 0:1496281373a5 1051 FD_SET(i, &lwriteset);
apatel336 0:1496281373a5 1052 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
apatel336 0:1496281373a5 1053 nready++;
apatel336 0:1496281373a5 1054 }
apatel336 0:1496281373a5 1055 /* See if netconn of this socket had an error */
apatel336 0:1496281373a5 1056 if (exceptset_in && FD_ISSET(i, exceptset_in) && (errevent != 0)) {
apatel336 0:1496281373a5 1057 FD_SET(i, &lexceptset);
apatel336 0:1496281373a5 1058 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for exception\n", i));
apatel336 0:1496281373a5 1059 nready++;
apatel336 0:1496281373a5 1060 }
apatel336 0:1496281373a5 1061 }
apatel336 0:1496281373a5 1062 /* copy local sets to the ones provided as arguments */
apatel336 0:1496281373a5 1063 *readset_out = lreadset;
apatel336 0:1496281373a5 1064 *writeset_out = lwriteset;
apatel336 0:1496281373a5 1065 *exceptset_out = lexceptset;
apatel336 0:1496281373a5 1066
apatel336 0:1496281373a5 1067 LWIP_ASSERT("nready >= 0", nready >= 0);
apatel336 0:1496281373a5 1068 return nready;
apatel336 0:1496281373a5 1069 }
apatel336 0:1496281373a5 1070
apatel336 0:1496281373a5 1071 /**
apatel336 0:1496281373a5 1072 * Processing exceptset is not yet implemented.
apatel336 0:1496281373a5 1073 */
apatel336 0:1496281373a5 1074 int
apatel336 0:1496281373a5 1075 lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
apatel336 0:1496281373a5 1076 struct timeval *timeout)
apatel336 0:1496281373a5 1077 {
apatel336 0:1496281373a5 1078 u32_t waitres = 0;
apatel336 0:1496281373a5 1079 int nready;
apatel336 0:1496281373a5 1080 fd_set lreadset, lwriteset, lexceptset;
apatel336 0:1496281373a5 1081 u32_t msectimeout;
apatel336 0:1496281373a5 1082 struct lwip_select_cb select_cb;
apatel336 0:1496281373a5 1083 err_t err;
apatel336 0:1496281373a5 1084 int i;
apatel336 0:1496281373a5 1085 SYS_ARCH_DECL_PROTECT(lev);
apatel336 0:1496281373a5 1086
apatel336 0:1496281373a5 1087 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%"S32_F" tvusec=%"S32_F")\n",
apatel336 0:1496281373a5 1088 maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset,
apatel336 0:1496281373a5 1089 timeout ? (s32_t)timeout->tv_sec : (s32_t)-1,
apatel336 0:1496281373a5 1090 timeout ? (s32_t)timeout->tv_usec : (s32_t)-1));
apatel336 0:1496281373a5 1091
apatel336 0:1496281373a5 1092 /* Go through each socket in each list to count number of sockets which
apatel336 0:1496281373a5 1093 currently match */
apatel336 0:1496281373a5 1094 nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
apatel336 0:1496281373a5 1095
apatel336 0:1496281373a5 1096 /* If we don't have any current events, then suspend if we are supposed to */
apatel336 0:1496281373a5 1097 if (!nready) {
apatel336 0:1496281373a5 1098 if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
apatel336 0:1496281373a5 1099 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
apatel336 0:1496281373a5 1100 /* This is OK as the local fdsets are empty and nready is zero,
apatel336 0:1496281373a5 1101 or we would have returned earlier. */
apatel336 0:1496281373a5 1102 goto return_copy_fdsets;
apatel336 0:1496281373a5 1103 }
apatel336 0:1496281373a5 1104
apatel336 0:1496281373a5 1105 /* None ready: add our semaphore to list:
apatel336 0:1496281373a5 1106 We don't actually need any dynamic memory. Our entry on the
apatel336 0:1496281373a5 1107 list is only valid while we are in this function, so it's ok
apatel336 0:1496281373a5 1108 to use local variables. */
apatel336 0:1496281373a5 1109
apatel336 0:1496281373a5 1110 select_cb.next = NULL;
apatel336 0:1496281373a5 1111 select_cb.prev = NULL;
apatel336 0:1496281373a5 1112 select_cb.readset = readset;
apatel336 0:1496281373a5 1113 select_cb.writeset = writeset;
apatel336 0:1496281373a5 1114 select_cb.exceptset = exceptset;
apatel336 0:1496281373a5 1115 select_cb.sem_signalled = 0;
apatel336 0:1496281373a5 1116 err = sys_sem_new(&select_cb.sem, 0);
apatel336 0:1496281373a5 1117 if (err != ERR_OK) {
apatel336 0:1496281373a5 1118 /* failed to create semaphore */
apatel336 0:1496281373a5 1119 set_errno(ENOMEM);
apatel336 0:1496281373a5 1120 return -1;
apatel336 0:1496281373a5 1121 }
apatel336 0:1496281373a5 1122
apatel336 0:1496281373a5 1123 /* Protect the select_cb_list */
apatel336 0:1496281373a5 1124 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1125
apatel336 0:1496281373a5 1126 /* Put this select_cb on top of list */
apatel336 0:1496281373a5 1127 select_cb.next = select_cb_list;
apatel336 0:1496281373a5 1128 if (select_cb_list != NULL) {
apatel336 0:1496281373a5 1129 select_cb_list->prev = &select_cb;
apatel336 0:1496281373a5 1130 }
apatel336 0:1496281373a5 1131 select_cb_list = &select_cb;
apatel336 0:1496281373a5 1132 /* Increasing this counter tells even_callback that the list has changed. */
apatel336 0:1496281373a5 1133 select_cb_ctr++;
apatel336 0:1496281373a5 1134
apatel336 0:1496281373a5 1135 /* Now we can safely unprotect */
apatel336 0:1496281373a5 1136 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1137
apatel336 0:1496281373a5 1138 /* Increase select_waiting for each socket we are interested in */
apatel336 0:1496281373a5 1139 for(i = 0; i < maxfdp1; i++) {
apatel336 0:1496281373a5 1140 if ((readset && FD_ISSET(i, readset)) ||
apatel336 0:1496281373a5 1141 (writeset && FD_ISSET(i, writeset)) ||
apatel336 0:1496281373a5 1142 (exceptset && FD_ISSET(i, exceptset))) {
apatel336 0:1496281373a5 1143 struct lwip_sock *sock = tryget_socket(i);
apatel336 0:1496281373a5 1144 LWIP_ASSERT("sock != NULL", sock != NULL);
apatel336 0:1496281373a5 1145 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1146 sock->select_waiting++;
apatel336 0:1496281373a5 1147 LWIP_ASSERT("sock->select_waiting > 0", sock->select_waiting > 0);
apatel336 0:1496281373a5 1148 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1149 }
apatel336 0:1496281373a5 1150 }
apatel336 0:1496281373a5 1151
apatel336 0:1496281373a5 1152 /* Call lwip_selscan again: there could have been events between
apatel336 0:1496281373a5 1153 the last scan (whithout us on the list) and putting us on the list! */
apatel336 0:1496281373a5 1154 nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
apatel336 0:1496281373a5 1155 if (!nready) {
apatel336 0:1496281373a5 1156 /* Still none ready, just wait to be woken */
apatel336 0:1496281373a5 1157 if (timeout == 0) {
apatel336 0:1496281373a5 1158 /* Wait forever */
apatel336 0:1496281373a5 1159 msectimeout = 0;
apatel336 0:1496281373a5 1160 } else {
apatel336 0:1496281373a5 1161 msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
apatel336 0:1496281373a5 1162 if (msectimeout == 0) {
apatel336 0:1496281373a5 1163 /* Wait 1ms at least (0 means wait forever) */
apatel336 0:1496281373a5 1164 msectimeout = 1;
apatel336 0:1496281373a5 1165 }
apatel336 0:1496281373a5 1166 }
apatel336 0:1496281373a5 1167
apatel336 0:1496281373a5 1168 waitres = sys_arch_sem_wait(&select_cb.sem, msectimeout);
apatel336 0:1496281373a5 1169 }
apatel336 0:1496281373a5 1170 /* Increase select_waiting for each socket we are interested in */
apatel336 0:1496281373a5 1171 for(i = 0; i < maxfdp1; i++) {
apatel336 0:1496281373a5 1172 if ((readset && FD_ISSET(i, readset)) ||
apatel336 0:1496281373a5 1173 (writeset && FD_ISSET(i, writeset)) ||
apatel336 0:1496281373a5 1174 (exceptset && FD_ISSET(i, exceptset))) {
apatel336 0:1496281373a5 1175 struct lwip_sock *sock = tryget_socket(i);
apatel336 0:1496281373a5 1176 LWIP_ASSERT("sock != NULL", sock != NULL);
apatel336 0:1496281373a5 1177 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1178 sock->select_waiting--;
apatel336 0:1496281373a5 1179 LWIP_ASSERT("sock->select_waiting >= 0", sock->select_waiting >= 0);
apatel336 0:1496281373a5 1180 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1181 }
apatel336 0:1496281373a5 1182 }
apatel336 0:1496281373a5 1183 /* Take us off the list */
apatel336 0:1496281373a5 1184 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1185 if (select_cb.next != NULL) {
apatel336 0:1496281373a5 1186 select_cb.next->prev = select_cb.prev;
apatel336 0:1496281373a5 1187 }
apatel336 0:1496281373a5 1188 if (select_cb_list == &select_cb) {
apatel336 0:1496281373a5 1189 LWIP_ASSERT("select_cb.prev == NULL", select_cb.prev == NULL);
apatel336 0:1496281373a5 1190 select_cb_list = select_cb.next;
apatel336 0:1496281373a5 1191 } else {
apatel336 0:1496281373a5 1192 LWIP_ASSERT("select_cb.prev != NULL", select_cb.prev != NULL);
apatel336 0:1496281373a5 1193 select_cb.prev->next = select_cb.next;
apatel336 0:1496281373a5 1194 }
apatel336 0:1496281373a5 1195 /* Increasing this counter tells even_callback that the list has changed. */
apatel336 0:1496281373a5 1196 select_cb_ctr++;
apatel336 0:1496281373a5 1197 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1198
apatel336 0:1496281373a5 1199 sys_sem_free(&select_cb.sem);
apatel336 0:1496281373a5 1200 if (waitres == SYS_ARCH_TIMEOUT) {
apatel336 0:1496281373a5 1201 /* Timeout */
apatel336 0:1496281373a5 1202 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
apatel336 0:1496281373a5 1203 /* This is OK as the local fdsets are empty and nready is zero,
apatel336 0:1496281373a5 1204 or we would have returned earlier. */
apatel336 0:1496281373a5 1205 goto return_copy_fdsets;
apatel336 0:1496281373a5 1206 }
apatel336 0:1496281373a5 1207
apatel336 0:1496281373a5 1208 /* See what's set */
apatel336 0:1496281373a5 1209 nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
apatel336 0:1496281373a5 1210 }
apatel336 0:1496281373a5 1211
apatel336 0:1496281373a5 1212 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
apatel336 0:1496281373a5 1213 return_copy_fdsets:
apatel336 0:1496281373a5 1214 set_errno(0);
apatel336 0:1496281373a5 1215 if (readset) {
apatel336 0:1496281373a5 1216 *readset = lreadset;
apatel336 0:1496281373a5 1217 }
apatel336 0:1496281373a5 1218 if (writeset) {
apatel336 0:1496281373a5 1219 *writeset = lwriteset;
apatel336 0:1496281373a5 1220 }
apatel336 0:1496281373a5 1221 if (exceptset) {
apatel336 0:1496281373a5 1222 *exceptset = lexceptset;
apatel336 0:1496281373a5 1223 }
apatel336 0:1496281373a5 1224
apatel336 0:1496281373a5 1225
apatel336 0:1496281373a5 1226 return nready;
apatel336 0:1496281373a5 1227 }
apatel336 0:1496281373a5 1228
apatel336 0:1496281373a5 1229 /**
apatel336 0:1496281373a5 1230 * Callback registered in the netconn layer for each socket-netconn.
apatel336 0:1496281373a5 1231 * Processes recvevent (data available) and wakes up tasks waiting for select.
apatel336 0:1496281373a5 1232 */
apatel336 0:1496281373a5 1233 static void
apatel336 0:1496281373a5 1234 event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
apatel336 0:1496281373a5 1235 {
apatel336 0:1496281373a5 1236 int s;
apatel336 0:1496281373a5 1237 struct lwip_sock *sock;
apatel336 0:1496281373a5 1238 struct lwip_select_cb *scb;
apatel336 0:1496281373a5 1239 int last_select_cb_ctr;
apatel336 0:1496281373a5 1240 SYS_ARCH_DECL_PROTECT(lev);
apatel336 0:1496281373a5 1241
apatel336 0:1496281373a5 1242 LWIP_UNUSED_ARG(len);
apatel336 0:1496281373a5 1243
apatel336 0:1496281373a5 1244 /* Get socket */
apatel336 0:1496281373a5 1245 if (conn) {
apatel336 0:1496281373a5 1246 s = conn->socket;
apatel336 0:1496281373a5 1247 if (s < 0) {
apatel336 0:1496281373a5 1248 /* Data comes in right away after an accept, even though
apatel336 0:1496281373a5 1249 * the server task might not have created a new socket yet.
apatel336 0:1496281373a5 1250 * Just count down (or up) if that's the case and we
apatel336 0:1496281373a5 1251 * will use the data later. Note that only receive events
apatel336 0:1496281373a5 1252 * can happen before the new socket is set up. */
apatel336 0:1496281373a5 1253 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1254 if (conn->socket < 0) {
apatel336 0:1496281373a5 1255 if (evt == NETCONN_EVT_RCVPLUS) {
apatel336 0:1496281373a5 1256 conn->socket--;
apatel336 0:1496281373a5 1257 }
apatel336 0:1496281373a5 1258 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1259 return;
apatel336 0:1496281373a5 1260 }
apatel336 0:1496281373a5 1261 s = conn->socket;
apatel336 0:1496281373a5 1262 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1263 }
apatel336 0:1496281373a5 1264
apatel336 0:1496281373a5 1265 sock = get_socket(s);
apatel336 0:1496281373a5 1266 if (!sock) {
apatel336 0:1496281373a5 1267 return;
apatel336 0:1496281373a5 1268 }
apatel336 0:1496281373a5 1269 } else {
apatel336 0:1496281373a5 1270 return;
apatel336 0:1496281373a5 1271 }
apatel336 0:1496281373a5 1272
apatel336 0:1496281373a5 1273 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1274 /* Set event as required */
apatel336 0:1496281373a5 1275 switch (evt) {
apatel336 0:1496281373a5 1276 case NETCONN_EVT_RCVPLUS:
apatel336 0:1496281373a5 1277 sock->rcvevent++;
apatel336 0:1496281373a5 1278 break;
apatel336 0:1496281373a5 1279 case NETCONN_EVT_RCVMINUS:
apatel336 0:1496281373a5 1280 sock->rcvevent--;
apatel336 0:1496281373a5 1281 break;
apatel336 0:1496281373a5 1282 case NETCONN_EVT_SENDPLUS:
apatel336 0:1496281373a5 1283 sock->sendevent = 1;
apatel336 0:1496281373a5 1284 break;
apatel336 0:1496281373a5 1285 case NETCONN_EVT_SENDMINUS:
apatel336 0:1496281373a5 1286 sock->sendevent = 0;
apatel336 0:1496281373a5 1287 break;
apatel336 0:1496281373a5 1288 case NETCONN_EVT_ERROR:
apatel336 0:1496281373a5 1289 sock->errevent = 1;
apatel336 0:1496281373a5 1290 break;
apatel336 0:1496281373a5 1291 default:
apatel336 0:1496281373a5 1292 LWIP_ASSERT("unknown event", 0);
apatel336 0:1496281373a5 1293 break;
apatel336 0:1496281373a5 1294 }
apatel336 0:1496281373a5 1295
apatel336 0:1496281373a5 1296 if (sock->select_waiting == 0) {
apatel336 0:1496281373a5 1297 /* noone is waiting for this socket, no need to check select_cb_list */
apatel336 0:1496281373a5 1298 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1299 return;
apatel336 0:1496281373a5 1300 }
apatel336 0:1496281373a5 1301
apatel336 0:1496281373a5 1302 /* Now decide if anyone is waiting for this socket */
apatel336 0:1496281373a5 1303 /* NOTE: This code goes through the select_cb_list list multiple times
apatel336 0:1496281373a5 1304 ONLY IF a select was actually waiting. We go through the list the number
apatel336 0:1496281373a5 1305 of waiting select calls + 1. This list is expected to be small. */
apatel336 0:1496281373a5 1306
apatel336 0:1496281373a5 1307 /* At this point, SYS_ARCH is still protected! */
apatel336 0:1496281373a5 1308 again:
apatel336 0:1496281373a5 1309 for (scb = select_cb_list; scb != NULL; scb = scb->next) {
apatel336 0:1496281373a5 1310 if (scb->sem_signalled == 0) {
apatel336 0:1496281373a5 1311 /* semaphore not signalled yet */
apatel336 0:1496281373a5 1312 int do_signal = 0;
apatel336 0:1496281373a5 1313 /* Test this select call for our socket */
apatel336 0:1496281373a5 1314 if (sock->rcvevent > 0) {
apatel336 0:1496281373a5 1315 if (scb->readset && FD_ISSET(s, scb->readset)) {
apatel336 0:1496281373a5 1316 do_signal = 1;
apatel336 0:1496281373a5 1317 }
apatel336 0:1496281373a5 1318 }
apatel336 0:1496281373a5 1319 if (sock->sendevent != 0) {
apatel336 0:1496281373a5 1320 if (!do_signal && scb->writeset && FD_ISSET(s, scb->writeset)) {
apatel336 0:1496281373a5 1321 do_signal = 1;
apatel336 0:1496281373a5 1322 }
apatel336 0:1496281373a5 1323 }
apatel336 0:1496281373a5 1324 if (sock->errevent != 0) {
apatel336 0:1496281373a5 1325 if (!do_signal && scb->exceptset && FD_ISSET(s, scb->exceptset)) {
apatel336 0:1496281373a5 1326 do_signal = 1;
apatel336 0:1496281373a5 1327 }
apatel336 0:1496281373a5 1328 }
apatel336 0:1496281373a5 1329 if (do_signal) {
apatel336 0:1496281373a5 1330 scb->sem_signalled = 1;
apatel336 0:1496281373a5 1331 /* Don't call SYS_ARCH_UNPROTECT() before signaling the semaphore, as this might
apatel336 0:1496281373a5 1332 lead to the select thread taking itself off the list, invalidagin the semaphore. */
apatel336 0:1496281373a5 1333 sys_sem_signal(&scb->sem);
apatel336 0:1496281373a5 1334 }
apatel336 0:1496281373a5 1335 }
apatel336 0:1496281373a5 1336 /* unlock interrupts with each step */
apatel336 0:1496281373a5 1337 last_select_cb_ctr = select_cb_ctr;
apatel336 0:1496281373a5 1338 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1339 /* this makes sure interrupt protection time is short */
apatel336 0:1496281373a5 1340 SYS_ARCH_PROTECT(lev);
apatel336 0:1496281373a5 1341 if (last_select_cb_ctr != select_cb_ctr) {
apatel336 0:1496281373a5 1342 /* someone has changed select_cb_list, restart at the beginning */
apatel336 0:1496281373a5 1343 goto again;
apatel336 0:1496281373a5 1344 }
apatel336 0:1496281373a5 1345 }
apatel336 0:1496281373a5 1346 SYS_ARCH_UNPROTECT(lev);
apatel336 0:1496281373a5 1347 }
apatel336 0:1496281373a5 1348
apatel336 0:1496281373a5 1349 /**
apatel336 0:1496281373a5 1350 * Unimplemented: Close one end of a full-duplex connection.
apatel336 0:1496281373a5 1351 * Currently, the full connection is closed.
apatel336 0:1496281373a5 1352 */
apatel336 0:1496281373a5 1353 int
apatel336 0:1496281373a5 1354 lwip_shutdown(int s, int how)
apatel336 0:1496281373a5 1355 {
apatel336 0:1496281373a5 1356 struct lwip_sock *sock;
apatel336 0:1496281373a5 1357 err_t err;
apatel336 0:1496281373a5 1358 u8_t shut_rx = 0, shut_tx = 0;
apatel336 0:1496281373a5 1359
apatel336 0:1496281373a5 1360 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
apatel336 0:1496281373a5 1361
apatel336 0:1496281373a5 1362 sock = get_socket(s);
apatel336 0:1496281373a5 1363 if (!sock) {
apatel336 0:1496281373a5 1364 return -1;
apatel336 0:1496281373a5 1365 }
apatel336 0:1496281373a5 1366
apatel336 0:1496281373a5 1367 if (sock->conn != NULL) {
apatel336 0:1496281373a5 1368 if (netconn_type(sock->conn) != NETCONN_TCP) {
apatel336 0:1496281373a5 1369 sock_set_errno(sock, EOPNOTSUPP);
apatel336 0:1496281373a5 1370 return EOPNOTSUPP;
apatel336 0:1496281373a5 1371 }
apatel336 0:1496281373a5 1372 } else {
apatel336 0:1496281373a5 1373 sock_set_errno(sock, ENOTCONN);
apatel336 0:1496281373a5 1374 return ENOTCONN;
apatel336 0:1496281373a5 1375 }
apatel336 0:1496281373a5 1376
apatel336 0:1496281373a5 1377 if (how == SHUT_RD) {
apatel336 0:1496281373a5 1378 shut_rx = 1;
apatel336 0:1496281373a5 1379 } else if (how == SHUT_WR) {
apatel336 0:1496281373a5 1380 shut_tx = 1;
apatel336 0:1496281373a5 1381 } else if(how == SHUT_RDWR) {
apatel336 0:1496281373a5 1382 shut_rx = 1;
apatel336 0:1496281373a5 1383 shut_tx = 1;
apatel336 0:1496281373a5 1384 } else {
apatel336 0:1496281373a5 1385 sock_set_errno(sock, EINVAL);
apatel336 0:1496281373a5 1386 return EINVAL;
apatel336 0:1496281373a5 1387 }
apatel336 0:1496281373a5 1388 err = netconn_shutdown(sock->conn, shut_rx, shut_tx);
apatel336 0:1496281373a5 1389
apatel336 0:1496281373a5 1390 sock_set_errno(sock, err_to_errno(err));
apatel336 0:1496281373a5 1391 return (err == ERR_OK ? 0 : -1);
apatel336 0:1496281373a5 1392 }
apatel336 0:1496281373a5 1393
apatel336 0:1496281373a5 1394 static int
apatel336 0:1496281373a5 1395 lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local)
apatel336 0:1496281373a5 1396 {
apatel336 0:1496281373a5 1397 struct lwip_sock *sock;
apatel336 0:1496281373a5 1398 struct sockaddr_in sin;
apatel336 0:1496281373a5 1399 ip_addr_t naddr;
apatel336 0:1496281373a5 1400
apatel336 0:1496281373a5 1401 sock = get_socket(s);
apatel336 0:1496281373a5 1402 if (!sock) {
apatel336 0:1496281373a5 1403 return -1;
apatel336 0:1496281373a5 1404 }
apatel336 0:1496281373a5 1405
apatel336 0:1496281373a5 1406 memset(&sin, 0, sizeof(sin));
apatel336 0:1496281373a5 1407 sin.sin_len = sizeof(sin);
apatel336 0:1496281373a5 1408 sin.sin_family = AF_INET;
apatel336 0:1496281373a5 1409
apatel336 0:1496281373a5 1410 /* get the IP address and port */
apatel336 0:1496281373a5 1411 netconn_getaddr(sock->conn, &naddr, &sin.sin_port, local);
apatel336 0:1496281373a5 1412
apatel336 0:1496281373a5 1413 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s));
apatel336 0:1496281373a5 1414 ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
apatel336 0:1496281373a5 1415 LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", sin.sin_port));
apatel336 0:1496281373a5 1416
apatel336 0:1496281373a5 1417 sin.sin_port = htons(sin.sin_port);
apatel336 0:1496281373a5 1418 inet_addr_from_ipaddr(&sin.sin_addr, &naddr);
apatel336 0:1496281373a5 1419
apatel336 0:1496281373a5 1420 if (*namelen > sizeof(sin)) {
apatel336 0:1496281373a5 1421 *namelen = sizeof(sin);
apatel336 0:1496281373a5 1422 }
apatel336 0:1496281373a5 1423
apatel336 0:1496281373a5 1424 MEMCPY(name, &sin, *namelen);
apatel336 0:1496281373a5 1425 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 1426 return 0;
apatel336 0:1496281373a5 1427 }
apatel336 0:1496281373a5 1428
apatel336 0:1496281373a5 1429 int
apatel336 0:1496281373a5 1430 lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
apatel336 0:1496281373a5 1431 {
apatel336 0:1496281373a5 1432 return lwip_getaddrname(s, name, namelen, 0);
apatel336 0:1496281373a5 1433 }
apatel336 0:1496281373a5 1434
apatel336 0:1496281373a5 1435 int
apatel336 0:1496281373a5 1436 lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
apatel336 0:1496281373a5 1437 {
apatel336 0:1496281373a5 1438 return lwip_getaddrname(s, name, namelen, 1);
apatel336 0:1496281373a5 1439 }
apatel336 0:1496281373a5 1440
apatel336 0:1496281373a5 1441 int
apatel336 0:1496281373a5 1442 lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
apatel336 0:1496281373a5 1443 {
apatel336 0:1496281373a5 1444 err_t err = ERR_OK;
apatel336 0:1496281373a5 1445 struct lwip_sock *sock = get_socket(s);
apatel336 0:1496281373a5 1446 struct lwip_setgetsockopt_data data;
apatel336 0:1496281373a5 1447
apatel336 0:1496281373a5 1448 if (!sock) {
apatel336 0:1496281373a5 1449 return -1;
apatel336 0:1496281373a5 1450 }
apatel336 0:1496281373a5 1451
apatel336 0:1496281373a5 1452 if ((NULL == optval) || (NULL == optlen)) {
apatel336 0:1496281373a5 1453 sock_set_errno(sock, EFAULT);
apatel336 0:1496281373a5 1454 return -1;
apatel336 0:1496281373a5 1455 }
apatel336 0:1496281373a5 1456
apatel336 0:1496281373a5 1457 /* Do length and type checks for the various options first, to keep it readable. */
apatel336 0:1496281373a5 1458 switch (level) {
apatel336 0:1496281373a5 1459
apatel336 0:1496281373a5 1460 /* Level: SOL_SOCKET */
apatel336 0:1496281373a5 1461 case SOL_SOCKET:
apatel336 0:1496281373a5 1462 switch (optname) {
apatel336 0:1496281373a5 1463
apatel336 0:1496281373a5 1464 case SO_ACCEPTCONN:
apatel336 0:1496281373a5 1465 case SO_BROADCAST:
apatel336 0:1496281373a5 1466 /* UNIMPL case SO_DEBUG: */
apatel336 0:1496281373a5 1467 /* UNIMPL case SO_DONTROUTE: */
apatel336 0:1496281373a5 1468 case SO_ERROR:
apatel336 0:1496281373a5 1469 case SO_KEEPALIVE:
apatel336 0:1496281373a5 1470 /* UNIMPL case SO_CONTIMEO: */
apatel336 0:1496281373a5 1471 /* UNIMPL case SO_SNDTIMEO: */
apatel336 0:1496281373a5 1472 #if LWIP_SO_RCVTIMEO
apatel336 0:1496281373a5 1473 case SO_RCVTIMEO:
apatel336 0:1496281373a5 1474 #endif /* LWIP_SO_RCVTIMEO */
apatel336 0:1496281373a5 1475 #if LWIP_SO_RCVBUF
apatel336 0:1496281373a5 1476 case SO_RCVBUF:
apatel336 0:1496281373a5 1477 #endif /* LWIP_SO_RCVBUF */
apatel336 0:1496281373a5 1478 /* UNIMPL case SO_OOBINLINE: */
apatel336 0:1496281373a5 1479 /* UNIMPL case SO_SNDBUF: */
apatel336 0:1496281373a5 1480 /* UNIMPL case SO_RCVLOWAT: */
apatel336 0:1496281373a5 1481 /* UNIMPL case SO_SNDLOWAT: */
apatel336 0:1496281373a5 1482 #if SO_REUSE
apatel336 0:1496281373a5 1483 case SO_REUSEADDR:
apatel336 0:1496281373a5 1484 case SO_REUSEPORT:
apatel336 0:1496281373a5 1485 #endif /* SO_REUSE */
apatel336 0:1496281373a5 1486 case SO_TYPE:
apatel336 0:1496281373a5 1487 /* UNIMPL case SO_USELOOPBACK: */
apatel336 0:1496281373a5 1488 if (*optlen < sizeof(int)) {
apatel336 0:1496281373a5 1489 err = EINVAL;
apatel336 0:1496281373a5 1490 }
apatel336 0:1496281373a5 1491 break;
apatel336 0:1496281373a5 1492
apatel336 0:1496281373a5 1493 case SO_NO_CHECK:
apatel336 0:1496281373a5 1494 if (*optlen < sizeof(int)) {
apatel336 0:1496281373a5 1495 err = EINVAL;
apatel336 0:1496281373a5 1496 }
apatel336 0:1496281373a5 1497 #if LWIP_UDP
apatel336 0:1496281373a5 1498 if ((sock->conn->type != NETCONN_UDP) ||
apatel336 0:1496281373a5 1499 ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
apatel336 0:1496281373a5 1500 /* this flag is only available for UDP, not for UDP lite */
apatel336 0:1496281373a5 1501 err = EAFNOSUPPORT;
apatel336 0:1496281373a5 1502 }
apatel336 0:1496281373a5 1503 #endif /* LWIP_UDP */
apatel336 0:1496281373a5 1504 break;
apatel336 0:1496281373a5 1505
apatel336 0:1496281373a5 1506 default:
apatel336 0:1496281373a5 1507 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1508 s, optname));
apatel336 0:1496281373a5 1509 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1510 } /* switch (optname) */
apatel336 0:1496281373a5 1511 break;
apatel336 0:1496281373a5 1512
apatel336 0:1496281373a5 1513 /* Level: IPPROTO_IP */
apatel336 0:1496281373a5 1514 case IPPROTO_IP:
apatel336 0:1496281373a5 1515 switch (optname) {
apatel336 0:1496281373a5 1516 /* UNIMPL case IP_HDRINCL: */
apatel336 0:1496281373a5 1517 /* UNIMPL case IP_RCVDSTADDR: */
apatel336 0:1496281373a5 1518 /* UNIMPL case IP_RCVIF: */
apatel336 0:1496281373a5 1519 case IP_TTL:
apatel336 0:1496281373a5 1520 case IP_TOS:
apatel336 0:1496281373a5 1521 if (*optlen < sizeof(int)) {
apatel336 0:1496281373a5 1522 err = EINVAL;
apatel336 0:1496281373a5 1523 }
apatel336 0:1496281373a5 1524 break;
apatel336 0:1496281373a5 1525 #if LWIP_IGMP
apatel336 0:1496281373a5 1526 case IP_MULTICAST_TTL:
apatel336 0:1496281373a5 1527 if (*optlen < sizeof(u8_t)) {
apatel336 0:1496281373a5 1528 err = EINVAL;
apatel336 0:1496281373a5 1529 }
apatel336 0:1496281373a5 1530 break;
apatel336 0:1496281373a5 1531 case IP_MULTICAST_IF:
apatel336 0:1496281373a5 1532 if (*optlen < sizeof(struct in_addr)) {
apatel336 0:1496281373a5 1533 err = EINVAL;
apatel336 0:1496281373a5 1534 }
apatel336 0:1496281373a5 1535 break;
apatel336 0:1496281373a5 1536 case IP_MULTICAST_LOOP:
apatel336 0:1496281373a5 1537 if (*optlen < sizeof(u8_t)) {
apatel336 0:1496281373a5 1538 err = EINVAL;
apatel336 0:1496281373a5 1539 }
apatel336 0:1496281373a5 1540 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
apatel336 0:1496281373a5 1541 err = EAFNOSUPPORT;
apatel336 0:1496281373a5 1542 }
apatel336 0:1496281373a5 1543 break;
apatel336 0:1496281373a5 1544 #endif /* LWIP_IGMP */
apatel336 0:1496281373a5 1545
apatel336 0:1496281373a5 1546 default:
apatel336 0:1496281373a5 1547 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1548 s, optname));
apatel336 0:1496281373a5 1549 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1550 } /* switch (optname) */
apatel336 0:1496281373a5 1551 break;
apatel336 0:1496281373a5 1552
apatel336 0:1496281373a5 1553 #if LWIP_TCP
apatel336 0:1496281373a5 1554 /* Level: IPPROTO_TCP */
apatel336 0:1496281373a5 1555 case IPPROTO_TCP:
apatel336 0:1496281373a5 1556 if (*optlen < sizeof(int)) {
apatel336 0:1496281373a5 1557 err = EINVAL;
apatel336 0:1496281373a5 1558 break;
apatel336 0:1496281373a5 1559 }
apatel336 0:1496281373a5 1560
apatel336 0:1496281373a5 1561 /* If this is no TCP socket, ignore any options. */
apatel336 0:1496281373a5 1562 if (sock->conn->type != NETCONN_TCP)
apatel336 0:1496281373a5 1563 return 0;
apatel336 0:1496281373a5 1564
apatel336 0:1496281373a5 1565 switch (optname) {
apatel336 0:1496281373a5 1566 case TCP_NODELAY:
apatel336 0:1496281373a5 1567 case TCP_KEEPALIVE:
apatel336 0:1496281373a5 1568 #if LWIP_TCP_KEEPALIVE
apatel336 0:1496281373a5 1569 case TCP_KEEPIDLE:
apatel336 0:1496281373a5 1570 case TCP_KEEPINTVL:
apatel336 0:1496281373a5 1571 case TCP_KEEPCNT:
apatel336 0:1496281373a5 1572 #endif /* LWIP_TCP_KEEPALIVE */
apatel336 0:1496281373a5 1573 break;
apatel336 0:1496281373a5 1574
apatel336 0:1496281373a5 1575 default:
apatel336 0:1496281373a5 1576 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1577 s, optname));
apatel336 0:1496281373a5 1578 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1579 } /* switch (optname) */
apatel336 0:1496281373a5 1580 break;
apatel336 0:1496281373a5 1581 #endif /* LWIP_TCP */
apatel336 0:1496281373a5 1582 #if LWIP_UDP && LWIP_UDPLITE
apatel336 0:1496281373a5 1583 /* Level: IPPROTO_UDPLITE */
apatel336 0:1496281373a5 1584 case IPPROTO_UDPLITE:
apatel336 0:1496281373a5 1585 if (*optlen < sizeof(int)) {
apatel336 0:1496281373a5 1586 err = EINVAL;
apatel336 0:1496281373a5 1587 break;
apatel336 0:1496281373a5 1588 }
apatel336 0:1496281373a5 1589
apatel336 0:1496281373a5 1590 /* If this is no UDP lite socket, ignore any options. */
apatel336 0:1496281373a5 1591 if (sock->conn->type != NETCONN_UDPLITE) {
apatel336 0:1496281373a5 1592 return 0;
apatel336 0:1496281373a5 1593 }
apatel336 0:1496281373a5 1594
apatel336 0:1496281373a5 1595 switch (optname) {
apatel336 0:1496281373a5 1596 case UDPLITE_SEND_CSCOV:
apatel336 0:1496281373a5 1597 case UDPLITE_RECV_CSCOV:
apatel336 0:1496281373a5 1598 break;
apatel336 0:1496281373a5 1599
apatel336 0:1496281373a5 1600 default:
apatel336 0:1496281373a5 1601 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1602 s, optname));
apatel336 0:1496281373a5 1603 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1604 } /* switch (optname) */
apatel336 0:1496281373a5 1605 break;
apatel336 0:1496281373a5 1606 #endif /* LWIP_UDP && LWIP_UDPLITE*/
apatel336 0:1496281373a5 1607 /* UNDEFINED LEVEL */
apatel336 0:1496281373a5 1608 default:
apatel336 0:1496281373a5 1609 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1610 s, level, optname));
apatel336 0:1496281373a5 1611 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1612 } /* switch */
apatel336 0:1496281373a5 1613
apatel336 0:1496281373a5 1614
apatel336 0:1496281373a5 1615 if (err != ERR_OK) {
apatel336 0:1496281373a5 1616 sock_set_errno(sock, err);
apatel336 0:1496281373a5 1617 return -1;
apatel336 0:1496281373a5 1618 }
apatel336 0:1496281373a5 1619
apatel336 0:1496281373a5 1620 /* Now do the actual option processing */
apatel336 0:1496281373a5 1621 data.sock = sock;
apatel336 0:1496281373a5 1622 #ifdef LWIP_DEBUG
apatel336 0:1496281373a5 1623 data.s = s;
apatel336 0:1496281373a5 1624 #endif /* LWIP_DEBUG */
apatel336 0:1496281373a5 1625 data.level = level;
apatel336 0:1496281373a5 1626 data.optname = optname;
apatel336 0:1496281373a5 1627 data.optval = optval;
apatel336 0:1496281373a5 1628 data.optlen = optlen;
apatel336 0:1496281373a5 1629 data.err = err;
apatel336 0:1496281373a5 1630 tcpip_callback(lwip_getsockopt_internal, &data);
apatel336 0:1496281373a5 1631 sys_arch_sem_wait(&sock->conn->op_completed, 0);
apatel336 0:1496281373a5 1632 /* maybe lwip_getsockopt_internal has changed err */
apatel336 0:1496281373a5 1633 err = data.err;
apatel336 0:1496281373a5 1634
apatel336 0:1496281373a5 1635 sock_set_errno(sock, err);
apatel336 0:1496281373a5 1636 return err ? -1 : 0;
apatel336 0:1496281373a5 1637 }
apatel336 0:1496281373a5 1638
apatel336 0:1496281373a5 1639 static void
apatel336 0:1496281373a5 1640 lwip_getsockopt_internal(void *arg)
apatel336 0:1496281373a5 1641 {
apatel336 0:1496281373a5 1642 struct lwip_sock *sock;
apatel336 0:1496281373a5 1643 #ifdef LWIP_DEBUG
apatel336 0:1496281373a5 1644 int s;
apatel336 0:1496281373a5 1645 #endif /* LWIP_DEBUG */
apatel336 0:1496281373a5 1646 int level, optname;
apatel336 0:1496281373a5 1647 void *optval;
apatel336 0:1496281373a5 1648 struct lwip_setgetsockopt_data *data;
apatel336 0:1496281373a5 1649
apatel336 0:1496281373a5 1650 LWIP_ASSERT("arg != NULL", arg != NULL);
apatel336 0:1496281373a5 1651
apatel336 0:1496281373a5 1652 data = (struct lwip_setgetsockopt_data*)arg;
apatel336 0:1496281373a5 1653 sock = data->sock;
apatel336 0:1496281373a5 1654 #ifdef LWIP_DEBUG
apatel336 0:1496281373a5 1655 s = data->s;
apatel336 0:1496281373a5 1656 #endif /* LWIP_DEBUG */
apatel336 0:1496281373a5 1657 level = data->level;
apatel336 0:1496281373a5 1658 optname = data->optname;
apatel336 0:1496281373a5 1659 optval = data->optval;
apatel336 0:1496281373a5 1660
apatel336 0:1496281373a5 1661 switch (level) {
apatel336 0:1496281373a5 1662
apatel336 0:1496281373a5 1663 /* Level: SOL_SOCKET */
apatel336 0:1496281373a5 1664 case SOL_SOCKET:
apatel336 0:1496281373a5 1665 switch (optname) {
apatel336 0:1496281373a5 1666
apatel336 0:1496281373a5 1667 /* The option flags */
apatel336 0:1496281373a5 1668 case SO_ACCEPTCONN:
apatel336 0:1496281373a5 1669 case SO_BROADCAST:
apatel336 0:1496281373a5 1670 /* UNIMPL case SO_DEBUG: */
apatel336 0:1496281373a5 1671 /* UNIMPL case SO_DONTROUTE: */
apatel336 0:1496281373a5 1672 case SO_KEEPALIVE:
apatel336 0:1496281373a5 1673 /* UNIMPL case SO_OOBINCLUDE: */
apatel336 0:1496281373a5 1674 #if SO_REUSE
apatel336 0:1496281373a5 1675 case SO_REUSEADDR:
apatel336 0:1496281373a5 1676 case SO_REUSEPORT:
apatel336 0:1496281373a5 1677 #endif /* SO_REUSE */
apatel336 0:1496281373a5 1678 /*case SO_USELOOPBACK: UNIMPL */
apatel336 0:1496281373a5 1679 *(int*)optval = sock->conn->pcb.ip->so_options & optname;
apatel336 0:1496281373a5 1680 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n",
apatel336 0:1496281373a5 1681 s, optname, (*(int*)optval?"on":"off")));
apatel336 0:1496281373a5 1682 break;
apatel336 0:1496281373a5 1683
apatel336 0:1496281373a5 1684 case SO_TYPE:
apatel336 0:1496281373a5 1685 switch (NETCONNTYPE_GROUP(sock->conn->type)) {
apatel336 0:1496281373a5 1686 case NETCONN_RAW:
apatel336 0:1496281373a5 1687 *(int*)optval = SOCK_RAW;
apatel336 0:1496281373a5 1688 break;
apatel336 0:1496281373a5 1689 case NETCONN_TCP:
apatel336 0:1496281373a5 1690 *(int*)optval = SOCK_STREAM;
apatel336 0:1496281373a5 1691 break;
apatel336 0:1496281373a5 1692 case NETCONN_UDP:
apatel336 0:1496281373a5 1693 *(int*)optval = SOCK_DGRAM;
apatel336 0:1496281373a5 1694 break;
apatel336 0:1496281373a5 1695 default: /* unrecognized socket type */
apatel336 0:1496281373a5 1696 *(int*)optval = sock->conn->type;
apatel336 0:1496281373a5 1697 LWIP_DEBUGF(SOCKETS_DEBUG,
apatel336 0:1496281373a5 1698 ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n",
apatel336 0:1496281373a5 1699 s, *(int *)optval));
apatel336 0:1496281373a5 1700 } /* switch (sock->conn->type) */
apatel336 0:1496281373a5 1701 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n",
apatel336 0:1496281373a5 1702 s, *(int *)optval));
apatel336 0:1496281373a5 1703 break;
apatel336 0:1496281373a5 1704
apatel336 0:1496281373a5 1705 case SO_ERROR:
apatel336 0:1496281373a5 1706 /* only overwrite ERR_OK or tempoary errors */
apatel336 0:1496281373a5 1707 if ((sock->err == 0) || (sock->err == EINPROGRESS)) {
apatel336 0:1496281373a5 1708 sock_set_errno(sock, err_to_errno(sock->conn->last_err));
apatel336 0:1496281373a5 1709 }
apatel336 0:1496281373a5 1710 *(int *)optval = sock->err;
apatel336 0:1496281373a5 1711 sock->err = 0;
apatel336 0:1496281373a5 1712 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n",
apatel336 0:1496281373a5 1713 s, *(int *)optval));
apatel336 0:1496281373a5 1714 break;
apatel336 0:1496281373a5 1715
apatel336 0:1496281373a5 1716 #if LWIP_SO_RCVTIMEO
apatel336 0:1496281373a5 1717 case SO_RCVTIMEO:
apatel336 0:1496281373a5 1718 *(int *)optval = netconn_get_recvtimeout(sock->conn);
apatel336 0:1496281373a5 1719 break;
apatel336 0:1496281373a5 1720 #endif /* LWIP_SO_RCVTIMEO */
apatel336 0:1496281373a5 1721 #if LWIP_SO_RCVBUF
apatel336 0:1496281373a5 1722 case SO_RCVBUF:
apatel336 0:1496281373a5 1723 *(int *)optval = netconn_get_recvbufsize(sock->conn);
apatel336 0:1496281373a5 1724 break;
apatel336 0:1496281373a5 1725 #endif /* LWIP_SO_RCVBUF */
apatel336 0:1496281373a5 1726 #if LWIP_UDP
apatel336 0:1496281373a5 1727 case SO_NO_CHECK:
apatel336 0:1496281373a5 1728 *(int*)optval = (udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_NOCHKSUM) ? 1 : 0;
apatel336 0:1496281373a5 1729 break;
apatel336 0:1496281373a5 1730 #endif /* LWIP_UDP*/
apatel336 0:1496281373a5 1731 default:
apatel336 0:1496281373a5 1732 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 1733 break;
apatel336 0:1496281373a5 1734 } /* switch (optname) */
apatel336 0:1496281373a5 1735 break;
apatel336 0:1496281373a5 1736
apatel336 0:1496281373a5 1737 /* Level: IPPROTO_IP */
apatel336 0:1496281373a5 1738 case IPPROTO_IP:
apatel336 0:1496281373a5 1739 switch (optname) {
apatel336 0:1496281373a5 1740 case IP_TTL:
apatel336 0:1496281373a5 1741 *(int*)optval = sock->conn->pcb.ip->ttl;
apatel336 0:1496281373a5 1742 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n",
apatel336 0:1496281373a5 1743 s, *(int *)optval));
apatel336 0:1496281373a5 1744 break;
apatel336 0:1496281373a5 1745 case IP_TOS:
apatel336 0:1496281373a5 1746 *(int*)optval = sock->conn->pcb.ip->tos;
apatel336 0:1496281373a5 1747 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n",
apatel336 0:1496281373a5 1748 s, *(int *)optval));
apatel336 0:1496281373a5 1749 break;
apatel336 0:1496281373a5 1750 #if LWIP_IGMP
apatel336 0:1496281373a5 1751 case IP_MULTICAST_TTL:
apatel336 0:1496281373a5 1752 *(u8_t*)optval = sock->conn->pcb.ip->ttl;
apatel336 0:1496281373a5 1753 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n",
apatel336 0:1496281373a5 1754 s, *(int *)optval));
apatel336 0:1496281373a5 1755 break;
apatel336 0:1496281373a5 1756 case IP_MULTICAST_IF:
apatel336 0:1496281373a5 1757 inet_addr_from_ipaddr((struct in_addr*)optval, &sock->conn->pcb.udp->multicast_ip);
apatel336 0:1496281373a5 1758 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%"X32_F"\n",
apatel336 0:1496281373a5 1759 s, *(u32_t *)optval));
apatel336 0:1496281373a5 1760 break;
apatel336 0:1496281373a5 1761 case IP_MULTICAST_LOOP:
apatel336 0:1496281373a5 1762 if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
apatel336 0:1496281373a5 1763 *(u8_t*)optval = 1;
apatel336 0:1496281373a5 1764 } else {
apatel336 0:1496281373a5 1765 *(u8_t*)optval = 0;
apatel336 0:1496281373a5 1766 }
apatel336 0:1496281373a5 1767 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_LOOP) = %d\n",
apatel336 0:1496281373a5 1768 s, *(int *)optval));
apatel336 0:1496281373a5 1769 break;
apatel336 0:1496281373a5 1770 #endif /* LWIP_IGMP */
apatel336 0:1496281373a5 1771 default:
apatel336 0:1496281373a5 1772 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 1773 break;
apatel336 0:1496281373a5 1774 } /* switch (optname) */
apatel336 0:1496281373a5 1775 break;
apatel336 0:1496281373a5 1776
apatel336 0:1496281373a5 1777 #if LWIP_TCP
apatel336 0:1496281373a5 1778 /* Level: IPPROTO_TCP */
apatel336 0:1496281373a5 1779 case IPPROTO_TCP:
apatel336 0:1496281373a5 1780 switch (optname) {
apatel336 0:1496281373a5 1781 case TCP_NODELAY:
apatel336 0:1496281373a5 1782 *(int*)optval = tcp_nagle_disabled(sock->conn->pcb.tcp);
apatel336 0:1496281373a5 1783 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n",
apatel336 0:1496281373a5 1784 s, (*(int*)optval)?"on":"off") );
apatel336 0:1496281373a5 1785 break;
apatel336 0:1496281373a5 1786 case TCP_KEEPALIVE:
apatel336 0:1496281373a5 1787 *(int*)optval = (int)sock->conn->pcb.tcp->keep_idle;
apatel336 0:1496281373a5 1788 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPALIVE) = %d\n",
apatel336 0:1496281373a5 1789 s, *(int *)optval));
apatel336 0:1496281373a5 1790 break;
apatel336 0:1496281373a5 1791
apatel336 0:1496281373a5 1792 #if LWIP_TCP_KEEPALIVE
apatel336 0:1496281373a5 1793 case TCP_KEEPIDLE:
apatel336 0:1496281373a5 1794 *(int*)optval = (int)(sock->conn->pcb.tcp->keep_idle/1000);
apatel336 0:1496281373a5 1795 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPIDLE) = %d\n",
apatel336 0:1496281373a5 1796 s, *(int *)optval));
apatel336 0:1496281373a5 1797 break;
apatel336 0:1496281373a5 1798 case TCP_KEEPINTVL:
apatel336 0:1496281373a5 1799 *(int*)optval = (int)(sock->conn->pcb.tcp->keep_intvl/1000);
apatel336 0:1496281373a5 1800 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPINTVL) = %d\n",
apatel336 0:1496281373a5 1801 s, *(int *)optval));
apatel336 0:1496281373a5 1802 break;
apatel336 0:1496281373a5 1803 case TCP_KEEPCNT:
apatel336 0:1496281373a5 1804 *(int*)optval = (int)sock->conn->pcb.tcp->keep_cnt;
apatel336 0:1496281373a5 1805 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPCNT) = %d\n",
apatel336 0:1496281373a5 1806 s, *(int *)optval));
apatel336 0:1496281373a5 1807 break;
apatel336 0:1496281373a5 1808 #endif /* LWIP_TCP_KEEPALIVE */
apatel336 0:1496281373a5 1809 default:
apatel336 0:1496281373a5 1810 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 1811 break;
apatel336 0:1496281373a5 1812 } /* switch (optname) */
apatel336 0:1496281373a5 1813 break;
apatel336 0:1496281373a5 1814 #endif /* LWIP_TCP */
apatel336 0:1496281373a5 1815 #if LWIP_UDP && LWIP_UDPLITE
apatel336 0:1496281373a5 1816 /* Level: IPPROTO_UDPLITE */
apatel336 0:1496281373a5 1817 case IPPROTO_UDPLITE:
apatel336 0:1496281373a5 1818 switch (optname) {
apatel336 0:1496281373a5 1819 case UDPLITE_SEND_CSCOV:
apatel336 0:1496281373a5 1820 *(int*)optval = sock->conn->pcb.udp->chksum_len_tx;
apatel336 0:1496281373a5 1821 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
apatel336 0:1496281373a5 1822 s, (*(int*)optval)) );
apatel336 0:1496281373a5 1823 break;
apatel336 0:1496281373a5 1824 case UDPLITE_RECV_CSCOV:
apatel336 0:1496281373a5 1825 *(int*)optval = sock->conn->pcb.udp->chksum_len_rx;
apatel336 0:1496281373a5 1826 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
apatel336 0:1496281373a5 1827 s, (*(int*)optval)) );
apatel336 0:1496281373a5 1828 break;
apatel336 0:1496281373a5 1829 default:
apatel336 0:1496281373a5 1830 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 1831 break;
apatel336 0:1496281373a5 1832 } /* switch (optname) */
apatel336 0:1496281373a5 1833 break;
apatel336 0:1496281373a5 1834 #endif /* LWIP_UDP */
apatel336 0:1496281373a5 1835 default:
apatel336 0:1496281373a5 1836 LWIP_ASSERT("unhandled level", 0);
apatel336 0:1496281373a5 1837 break;
apatel336 0:1496281373a5 1838 } /* switch (level) */
apatel336 0:1496281373a5 1839 sys_sem_signal(&sock->conn->op_completed);
apatel336 0:1496281373a5 1840 }
apatel336 0:1496281373a5 1841
apatel336 0:1496281373a5 1842 int
apatel336 0:1496281373a5 1843 lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
apatel336 0:1496281373a5 1844 {
apatel336 0:1496281373a5 1845 struct lwip_sock *sock = get_socket(s);
apatel336 0:1496281373a5 1846 err_t err = ERR_OK;
apatel336 0:1496281373a5 1847 struct lwip_setgetsockopt_data data;
apatel336 0:1496281373a5 1848
apatel336 0:1496281373a5 1849 if (!sock) {
apatel336 0:1496281373a5 1850 return -1;
apatel336 0:1496281373a5 1851 }
apatel336 0:1496281373a5 1852
apatel336 0:1496281373a5 1853 if (NULL == optval) {
apatel336 0:1496281373a5 1854 sock_set_errno(sock, EFAULT);
apatel336 0:1496281373a5 1855 return -1;
apatel336 0:1496281373a5 1856 }
apatel336 0:1496281373a5 1857
apatel336 0:1496281373a5 1858 /* Do length and type checks for the various options first, to keep it readable. */
apatel336 0:1496281373a5 1859 switch (level) {
apatel336 0:1496281373a5 1860
apatel336 0:1496281373a5 1861 /* Level: SOL_SOCKET */
apatel336 0:1496281373a5 1862 case SOL_SOCKET:
apatel336 0:1496281373a5 1863 switch (optname) {
apatel336 0:1496281373a5 1864
apatel336 0:1496281373a5 1865 case SO_BROADCAST:
apatel336 0:1496281373a5 1866 /* UNIMPL case SO_DEBUG: */
apatel336 0:1496281373a5 1867 /* UNIMPL case SO_DONTROUTE: */
apatel336 0:1496281373a5 1868 case SO_KEEPALIVE:
apatel336 0:1496281373a5 1869 /* UNIMPL case case SO_CONTIMEO: */
apatel336 0:1496281373a5 1870 /* UNIMPL case case SO_SNDTIMEO: */
apatel336 0:1496281373a5 1871 #if LWIP_SO_RCVTIMEO
apatel336 0:1496281373a5 1872 case SO_RCVTIMEO:
apatel336 0:1496281373a5 1873 #endif /* LWIP_SO_RCVTIMEO */
apatel336 0:1496281373a5 1874 #if LWIP_SO_RCVBUF
apatel336 0:1496281373a5 1875 case SO_RCVBUF:
apatel336 0:1496281373a5 1876 #endif /* LWIP_SO_RCVBUF */
apatel336 0:1496281373a5 1877 /* UNIMPL case SO_OOBINLINE: */
apatel336 0:1496281373a5 1878 /* UNIMPL case SO_SNDBUF: */
apatel336 0:1496281373a5 1879 /* UNIMPL case SO_RCVLOWAT: */
apatel336 0:1496281373a5 1880 /* UNIMPL case SO_SNDLOWAT: */
apatel336 0:1496281373a5 1881 #if SO_REUSE
apatel336 0:1496281373a5 1882 case SO_REUSEADDR:
apatel336 0:1496281373a5 1883 case SO_REUSEPORT:
apatel336 0:1496281373a5 1884 #endif /* SO_REUSE */
apatel336 0:1496281373a5 1885 /* UNIMPL case SO_USELOOPBACK: */
apatel336 0:1496281373a5 1886 if (optlen < sizeof(int)) {
apatel336 0:1496281373a5 1887 err = EINVAL;
apatel336 0:1496281373a5 1888 }
apatel336 0:1496281373a5 1889 break;
apatel336 0:1496281373a5 1890 case SO_NO_CHECK:
apatel336 0:1496281373a5 1891 if (optlen < sizeof(int)) {
apatel336 0:1496281373a5 1892 err = EINVAL;
apatel336 0:1496281373a5 1893 }
apatel336 0:1496281373a5 1894 #if LWIP_UDP
apatel336 0:1496281373a5 1895 if ((sock->conn->type != NETCONN_UDP) ||
apatel336 0:1496281373a5 1896 ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
apatel336 0:1496281373a5 1897 /* this flag is only available for UDP, not for UDP lite */
apatel336 0:1496281373a5 1898 err = EAFNOSUPPORT;
apatel336 0:1496281373a5 1899 }
apatel336 0:1496281373a5 1900 #endif /* LWIP_UDP */
apatel336 0:1496281373a5 1901 break;
apatel336 0:1496281373a5 1902 default:
apatel336 0:1496281373a5 1903 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1904 s, optname));
apatel336 0:1496281373a5 1905 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1906 } /* switch (optname) */
apatel336 0:1496281373a5 1907 break;
apatel336 0:1496281373a5 1908
apatel336 0:1496281373a5 1909 /* Level: IPPROTO_IP */
apatel336 0:1496281373a5 1910 case IPPROTO_IP:
apatel336 0:1496281373a5 1911 switch (optname) {
apatel336 0:1496281373a5 1912 /* UNIMPL case IP_HDRINCL: */
apatel336 0:1496281373a5 1913 /* UNIMPL case IP_RCVDSTADDR: */
apatel336 0:1496281373a5 1914 /* UNIMPL case IP_RCVIF: */
apatel336 0:1496281373a5 1915 case IP_TTL:
apatel336 0:1496281373a5 1916 case IP_TOS:
apatel336 0:1496281373a5 1917 if (optlen < sizeof(int)) {
apatel336 0:1496281373a5 1918 err = EINVAL;
apatel336 0:1496281373a5 1919 }
apatel336 0:1496281373a5 1920 break;
apatel336 0:1496281373a5 1921 #if LWIP_IGMP
apatel336 0:1496281373a5 1922 case IP_MULTICAST_TTL:
apatel336 0:1496281373a5 1923 if (optlen < sizeof(u8_t)) {
apatel336 0:1496281373a5 1924 err = EINVAL;
apatel336 0:1496281373a5 1925 }
apatel336 0:1496281373a5 1926 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
apatel336 0:1496281373a5 1927 err = EAFNOSUPPORT;
apatel336 0:1496281373a5 1928 }
apatel336 0:1496281373a5 1929 break;
apatel336 0:1496281373a5 1930 case IP_MULTICAST_IF:
apatel336 0:1496281373a5 1931 if (optlen < sizeof(struct in_addr)) {
apatel336 0:1496281373a5 1932 err = EINVAL;
apatel336 0:1496281373a5 1933 }
apatel336 0:1496281373a5 1934 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
apatel336 0:1496281373a5 1935 err = EAFNOSUPPORT;
apatel336 0:1496281373a5 1936 }
apatel336 0:1496281373a5 1937 break;
apatel336 0:1496281373a5 1938 case IP_MULTICAST_LOOP:
apatel336 0:1496281373a5 1939 if (optlen < sizeof(u8_t)) {
apatel336 0:1496281373a5 1940 err = EINVAL;
apatel336 0:1496281373a5 1941 }
apatel336 0:1496281373a5 1942 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
apatel336 0:1496281373a5 1943 err = EAFNOSUPPORT;
apatel336 0:1496281373a5 1944 }
apatel336 0:1496281373a5 1945 break;
apatel336 0:1496281373a5 1946 case IP_ADD_MEMBERSHIP:
apatel336 0:1496281373a5 1947 case IP_DROP_MEMBERSHIP:
apatel336 0:1496281373a5 1948 if (optlen < sizeof(struct ip_mreq)) {
apatel336 0:1496281373a5 1949 err = EINVAL;
apatel336 0:1496281373a5 1950 }
apatel336 0:1496281373a5 1951 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
apatel336 0:1496281373a5 1952 err = EAFNOSUPPORT;
apatel336 0:1496281373a5 1953 }
apatel336 0:1496281373a5 1954 break;
apatel336 0:1496281373a5 1955 #endif /* LWIP_IGMP */
apatel336 0:1496281373a5 1956 default:
apatel336 0:1496281373a5 1957 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1958 s, optname));
apatel336 0:1496281373a5 1959 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1960 } /* switch (optname) */
apatel336 0:1496281373a5 1961 break;
apatel336 0:1496281373a5 1962
apatel336 0:1496281373a5 1963 #if LWIP_TCP
apatel336 0:1496281373a5 1964 /* Level: IPPROTO_TCP */
apatel336 0:1496281373a5 1965 case IPPROTO_TCP:
apatel336 0:1496281373a5 1966 if (optlen < sizeof(int)) {
apatel336 0:1496281373a5 1967 err = EINVAL;
apatel336 0:1496281373a5 1968 break;
apatel336 0:1496281373a5 1969 }
apatel336 0:1496281373a5 1970
apatel336 0:1496281373a5 1971 /* If this is no TCP socket, ignore any options. */
apatel336 0:1496281373a5 1972 if (sock->conn->type != NETCONN_TCP)
apatel336 0:1496281373a5 1973 return 0;
apatel336 0:1496281373a5 1974
apatel336 0:1496281373a5 1975 switch (optname) {
apatel336 0:1496281373a5 1976 case TCP_NODELAY:
apatel336 0:1496281373a5 1977 case TCP_KEEPALIVE:
apatel336 0:1496281373a5 1978 #if LWIP_TCP_KEEPALIVE
apatel336 0:1496281373a5 1979 case TCP_KEEPIDLE:
apatel336 0:1496281373a5 1980 case TCP_KEEPINTVL:
apatel336 0:1496281373a5 1981 case TCP_KEEPCNT:
apatel336 0:1496281373a5 1982 #endif /* LWIP_TCP_KEEPALIVE */
apatel336 0:1496281373a5 1983 break;
apatel336 0:1496281373a5 1984
apatel336 0:1496281373a5 1985 default:
apatel336 0:1496281373a5 1986 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 1987 s, optname));
apatel336 0:1496281373a5 1988 err = ENOPROTOOPT;
apatel336 0:1496281373a5 1989 } /* switch (optname) */
apatel336 0:1496281373a5 1990 break;
apatel336 0:1496281373a5 1991 #endif /* LWIP_TCP */
apatel336 0:1496281373a5 1992 #if LWIP_UDP && LWIP_UDPLITE
apatel336 0:1496281373a5 1993 /* Level: IPPROTO_UDPLITE */
apatel336 0:1496281373a5 1994 case IPPROTO_UDPLITE:
apatel336 0:1496281373a5 1995 if (optlen < sizeof(int)) {
apatel336 0:1496281373a5 1996 err = EINVAL;
apatel336 0:1496281373a5 1997 break;
apatel336 0:1496281373a5 1998 }
apatel336 0:1496281373a5 1999
apatel336 0:1496281373a5 2000 /* If this is no UDP lite socket, ignore any options. */
apatel336 0:1496281373a5 2001 if (sock->conn->type != NETCONN_UDPLITE)
apatel336 0:1496281373a5 2002 return 0;
apatel336 0:1496281373a5 2003
apatel336 0:1496281373a5 2004 switch (optname) {
apatel336 0:1496281373a5 2005 case UDPLITE_SEND_CSCOV:
apatel336 0:1496281373a5 2006 case UDPLITE_RECV_CSCOV:
apatel336 0:1496281373a5 2007 break;
apatel336 0:1496281373a5 2008
apatel336 0:1496281373a5 2009 default:
apatel336 0:1496281373a5 2010 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 2011 s, optname));
apatel336 0:1496281373a5 2012 err = ENOPROTOOPT;
apatel336 0:1496281373a5 2013 } /* switch (optname) */
apatel336 0:1496281373a5 2014 break;
apatel336 0:1496281373a5 2015 #endif /* LWIP_UDP && LWIP_UDPLITE */
apatel336 0:1496281373a5 2016 /* UNDEFINED LEVEL */
apatel336 0:1496281373a5 2017 default:
apatel336 0:1496281373a5 2018 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
apatel336 0:1496281373a5 2019 s, level, optname));
apatel336 0:1496281373a5 2020 err = ENOPROTOOPT;
apatel336 0:1496281373a5 2021 } /* switch (level) */
apatel336 0:1496281373a5 2022
apatel336 0:1496281373a5 2023
apatel336 0:1496281373a5 2024 if (err != ERR_OK) {
apatel336 0:1496281373a5 2025 sock_set_errno(sock, err);
apatel336 0:1496281373a5 2026 return -1;
apatel336 0:1496281373a5 2027 }
apatel336 0:1496281373a5 2028
apatel336 0:1496281373a5 2029
apatel336 0:1496281373a5 2030 /* Now do the actual option processing */
apatel336 0:1496281373a5 2031 data.sock = sock;
apatel336 0:1496281373a5 2032 #ifdef LWIP_DEBUG
apatel336 0:1496281373a5 2033 data.s = s;
apatel336 0:1496281373a5 2034 #endif /* LWIP_DEBUG */
apatel336 0:1496281373a5 2035 data.level = level;
apatel336 0:1496281373a5 2036 data.optname = optname;
apatel336 0:1496281373a5 2037 data.optval = (void*)optval;
apatel336 0:1496281373a5 2038 data.optlen = &optlen;
apatel336 0:1496281373a5 2039 data.err = err;
apatel336 0:1496281373a5 2040 tcpip_callback(lwip_setsockopt_internal, &data);
apatel336 0:1496281373a5 2041 sys_arch_sem_wait(&sock->conn->op_completed, 0);
apatel336 0:1496281373a5 2042 /* maybe lwip_setsockopt_internal has changed err */
apatel336 0:1496281373a5 2043 err = data.err;
apatel336 0:1496281373a5 2044
apatel336 0:1496281373a5 2045 sock_set_errno(sock, err);
apatel336 0:1496281373a5 2046 return err ? -1 : 0;
apatel336 0:1496281373a5 2047 }
apatel336 0:1496281373a5 2048
apatel336 0:1496281373a5 2049 static void
apatel336 0:1496281373a5 2050 lwip_setsockopt_internal(void *arg)
apatel336 0:1496281373a5 2051 {
apatel336 0:1496281373a5 2052 struct lwip_sock *sock;
apatel336 0:1496281373a5 2053 #ifdef LWIP_DEBUG
apatel336 0:1496281373a5 2054 int s;
apatel336 0:1496281373a5 2055 #endif /* LWIP_DEBUG */
apatel336 0:1496281373a5 2056 int level, optname;
apatel336 0:1496281373a5 2057 const void *optval;
apatel336 0:1496281373a5 2058 struct lwip_setgetsockopt_data *data;
apatel336 0:1496281373a5 2059
apatel336 0:1496281373a5 2060 LWIP_ASSERT("arg != NULL", arg != NULL);
apatel336 0:1496281373a5 2061
apatel336 0:1496281373a5 2062 data = (struct lwip_setgetsockopt_data*)arg;
apatel336 0:1496281373a5 2063 sock = data->sock;
apatel336 0:1496281373a5 2064 #ifdef LWIP_DEBUG
apatel336 0:1496281373a5 2065 s = data->s;
apatel336 0:1496281373a5 2066 #endif /* LWIP_DEBUG */
apatel336 0:1496281373a5 2067 level = data->level;
apatel336 0:1496281373a5 2068 optname = data->optname;
apatel336 0:1496281373a5 2069 optval = data->optval;
apatel336 0:1496281373a5 2070
apatel336 0:1496281373a5 2071 switch (level) {
apatel336 0:1496281373a5 2072
apatel336 0:1496281373a5 2073 /* Level: SOL_SOCKET */
apatel336 0:1496281373a5 2074 case SOL_SOCKET:
apatel336 0:1496281373a5 2075 switch (optname) {
apatel336 0:1496281373a5 2076
apatel336 0:1496281373a5 2077 /* The option flags */
apatel336 0:1496281373a5 2078 case SO_BROADCAST:
apatel336 0:1496281373a5 2079 /* UNIMPL case SO_DEBUG: */
apatel336 0:1496281373a5 2080 /* UNIMPL case SO_DONTROUTE: */
apatel336 0:1496281373a5 2081 case SO_KEEPALIVE:
apatel336 0:1496281373a5 2082 /* UNIMPL case SO_OOBINCLUDE: */
apatel336 0:1496281373a5 2083 #if SO_REUSE
apatel336 0:1496281373a5 2084 case SO_REUSEADDR:
apatel336 0:1496281373a5 2085 case SO_REUSEPORT:
apatel336 0:1496281373a5 2086 #endif /* SO_REUSE */
apatel336 0:1496281373a5 2087 /* UNIMPL case SO_USELOOPBACK: */
apatel336 0:1496281373a5 2088 if (*(int*)optval) {
apatel336 0:1496281373a5 2089 sock->conn->pcb.ip->so_options |= optname;
apatel336 0:1496281373a5 2090 } else {
apatel336 0:1496281373a5 2091 sock->conn->pcb.ip->so_options &= ~optname;
apatel336 0:1496281373a5 2092 }
apatel336 0:1496281373a5 2093 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n",
apatel336 0:1496281373a5 2094 s, optname, (*(int*)optval?"on":"off")));
apatel336 0:1496281373a5 2095 break;
apatel336 0:1496281373a5 2096 #if LWIP_SO_RCVTIMEO
apatel336 0:1496281373a5 2097 case SO_RCVTIMEO:
apatel336 0:1496281373a5 2098 netconn_set_recvtimeout(sock->conn, *(int*)optval);
apatel336 0:1496281373a5 2099 break;
apatel336 0:1496281373a5 2100 #endif /* LWIP_SO_RCVTIMEO */
apatel336 0:1496281373a5 2101 #if LWIP_SO_RCVBUF
apatel336 0:1496281373a5 2102 case SO_RCVBUF:
apatel336 0:1496281373a5 2103 netconn_set_recvbufsize(sock->conn, *(int*)optval);
apatel336 0:1496281373a5 2104 break;
apatel336 0:1496281373a5 2105 #endif /* LWIP_SO_RCVBUF */
apatel336 0:1496281373a5 2106 #if LWIP_UDP
apatel336 0:1496281373a5 2107 case SO_NO_CHECK:
apatel336 0:1496281373a5 2108 if (*(int*)optval) {
apatel336 0:1496281373a5 2109 udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_NOCHKSUM);
apatel336 0:1496281373a5 2110 } else {
apatel336 0:1496281373a5 2111 udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_NOCHKSUM);
apatel336 0:1496281373a5 2112 }
apatel336 0:1496281373a5 2113 break;
apatel336 0:1496281373a5 2114 #endif /* LWIP_UDP */
apatel336 0:1496281373a5 2115 default:
apatel336 0:1496281373a5 2116 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 2117 break;
apatel336 0:1496281373a5 2118 } /* switch (optname) */
apatel336 0:1496281373a5 2119 break;
apatel336 0:1496281373a5 2120
apatel336 0:1496281373a5 2121 /* Level: IPPROTO_IP */
apatel336 0:1496281373a5 2122 case IPPROTO_IP:
apatel336 0:1496281373a5 2123 switch (optname) {
apatel336 0:1496281373a5 2124 case IP_TTL:
apatel336 0:1496281373a5 2125 sock->conn->pcb.ip->ttl = (u8_t)(*(int*)optval);
apatel336 0:1496281373a5 2126 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n",
apatel336 0:1496281373a5 2127 s, sock->conn->pcb.ip->ttl));
apatel336 0:1496281373a5 2128 break;
apatel336 0:1496281373a5 2129 case IP_TOS:
apatel336 0:1496281373a5 2130 sock->conn->pcb.ip->tos = (u8_t)(*(int*)optval);
apatel336 0:1496281373a5 2131 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n",
apatel336 0:1496281373a5 2132 s, sock->conn->pcb.ip->tos));
apatel336 0:1496281373a5 2133 break;
apatel336 0:1496281373a5 2134 #if LWIP_IGMP
apatel336 0:1496281373a5 2135 case IP_MULTICAST_TTL:
apatel336 0:1496281373a5 2136 sock->conn->pcb.udp->ttl = (u8_t)(*(u8_t*)optval);
apatel336 0:1496281373a5 2137 break;
apatel336 0:1496281373a5 2138 case IP_MULTICAST_IF:
apatel336 0:1496281373a5 2139 inet_addr_to_ipaddr(&sock->conn->pcb.udp->multicast_ip, (struct in_addr*)optval);
apatel336 0:1496281373a5 2140 break;
apatel336 0:1496281373a5 2141 case IP_MULTICAST_LOOP:
apatel336 0:1496281373a5 2142 if (*(u8_t*)optval) {
apatel336 0:1496281373a5 2143 udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_MULTICAST_LOOP);
apatel336 0:1496281373a5 2144 } else {
apatel336 0:1496281373a5 2145 udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_MULTICAST_LOOP);
apatel336 0:1496281373a5 2146 }
apatel336 0:1496281373a5 2147 break;
apatel336 0:1496281373a5 2148 case IP_ADD_MEMBERSHIP:
apatel336 0:1496281373a5 2149 case IP_DROP_MEMBERSHIP:
apatel336 0:1496281373a5 2150 {
apatel336 0:1496281373a5 2151 /* If this is a TCP or a RAW socket, ignore these options. */
apatel336 0:1496281373a5 2152 struct ip_mreq *imr = (struct ip_mreq *)optval;
apatel336 0:1496281373a5 2153 ip_addr_t if_addr;
apatel336 0:1496281373a5 2154 ip_addr_t multi_addr;
apatel336 0:1496281373a5 2155 inet_addr_to_ipaddr(&if_addr, &imr->imr_interface);
apatel336 0:1496281373a5 2156 inet_addr_to_ipaddr(&multi_addr, &imr->imr_multiaddr);
apatel336 0:1496281373a5 2157 if(optname == IP_ADD_MEMBERSHIP){
apatel336 0:1496281373a5 2158 data->err = igmp_joingroup(&if_addr, &multi_addr);
apatel336 0:1496281373a5 2159 } else {
apatel336 0:1496281373a5 2160 data->err = igmp_leavegroup(&if_addr, &multi_addr);
apatel336 0:1496281373a5 2161 }
apatel336 0:1496281373a5 2162 if(data->err != ERR_OK) {
apatel336 0:1496281373a5 2163 data->err = EADDRNOTAVAIL;
apatel336 0:1496281373a5 2164 }
apatel336 0:1496281373a5 2165 }
apatel336 0:1496281373a5 2166 break;
apatel336 0:1496281373a5 2167 #endif /* LWIP_IGMP */
apatel336 0:1496281373a5 2168 default:
apatel336 0:1496281373a5 2169 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 2170 break;
apatel336 0:1496281373a5 2171 } /* switch (optname) */
apatel336 0:1496281373a5 2172 break;
apatel336 0:1496281373a5 2173
apatel336 0:1496281373a5 2174 #if LWIP_TCP
apatel336 0:1496281373a5 2175 /* Level: IPPROTO_TCP */
apatel336 0:1496281373a5 2176 case IPPROTO_TCP:
apatel336 0:1496281373a5 2177 switch (optname) {
apatel336 0:1496281373a5 2178 case TCP_NODELAY:
apatel336 0:1496281373a5 2179 if (*(int*)optval) {
apatel336 0:1496281373a5 2180 tcp_nagle_disable(sock->conn->pcb.tcp);
apatel336 0:1496281373a5 2181 } else {
apatel336 0:1496281373a5 2182 tcp_nagle_enable(sock->conn->pcb.tcp);
apatel336 0:1496281373a5 2183 }
apatel336 0:1496281373a5 2184 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n",
apatel336 0:1496281373a5 2185 s, (*(int *)optval)?"on":"off") );
apatel336 0:1496281373a5 2186 break;
apatel336 0:1496281373a5 2187 case TCP_KEEPALIVE:
apatel336 0:1496281373a5 2188 sock->conn->pcb.tcp->keep_idle = (u32_t)(*(int*)optval);
apatel336 0:1496281373a5 2189 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %"U32_F"\n",
apatel336 0:1496281373a5 2190 s, sock->conn->pcb.tcp->keep_idle));
apatel336 0:1496281373a5 2191 break;
apatel336 0:1496281373a5 2192
apatel336 0:1496281373a5 2193 #if LWIP_TCP_KEEPALIVE
apatel336 0:1496281373a5 2194 case TCP_KEEPIDLE:
apatel336 0:1496281373a5 2195 sock->conn->pcb.tcp->keep_idle = 1000*(u32_t)(*(int*)optval);
apatel336 0:1496281373a5 2196 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %"U32_F"\n",
apatel336 0:1496281373a5 2197 s, sock->conn->pcb.tcp->keep_idle));
apatel336 0:1496281373a5 2198 break;
apatel336 0:1496281373a5 2199 case TCP_KEEPINTVL:
apatel336 0:1496281373a5 2200 sock->conn->pcb.tcp->keep_intvl = 1000*(u32_t)(*(int*)optval);
apatel336 0:1496281373a5 2201 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %"U32_F"\n",
apatel336 0:1496281373a5 2202 s, sock->conn->pcb.tcp->keep_intvl));
apatel336 0:1496281373a5 2203 break;
apatel336 0:1496281373a5 2204 case TCP_KEEPCNT:
apatel336 0:1496281373a5 2205 sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(int*)optval);
apatel336 0:1496281373a5 2206 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %"U32_F"\n",
apatel336 0:1496281373a5 2207 s, sock->conn->pcb.tcp->keep_cnt));
apatel336 0:1496281373a5 2208 break;
apatel336 0:1496281373a5 2209 #endif /* LWIP_TCP_KEEPALIVE */
apatel336 0:1496281373a5 2210 default:
apatel336 0:1496281373a5 2211 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 2212 break;
apatel336 0:1496281373a5 2213 } /* switch (optname) */
apatel336 0:1496281373a5 2214 break;
apatel336 0:1496281373a5 2215 #endif /* LWIP_TCP*/
apatel336 0:1496281373a5 2216 #if LWIP_UDP && LWIP_UDPLITE
apatel336 0:1496281373a5 2217 /* Level: IPPROTO_UDPLITE */
apatel336 0:1496281373a5 2218 case IPPROTO_UDPLITE:
apatel336 0:1496281373a5 2219 switch (optname) {
apatel336 0:1496281373a5 2220 case UDPLITE_SEND_CSCOV:
apatel336 0:1496281373a5 2221 if ((*(int*)optval != 0) && ((*(int*)optval < 8)) || (*(int*)optval > 0xffff)) {
apatel336 0:1496281373a5 2222 /* don't allow illegal values! */
apatel336 0:1496281373a5 2223 sock->conn->pcb.udp->chksum_len_tx = 8;
apatel336 0:1496281373a5 2224 } else {
apatel336 0:1496281373a5 2225 sock->conn->pcb.udp->chksum_len_tx = (u16_t)*(int*)optval;
apatel336 0:1496281373a5 2226 }
apatel336 0:1496281373a5 2227 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n",
apatel336 0:1496281373a5 2228 s, (*(int*)optval)) );
apatel336 0:1496281373a5 2229 break;
apatel336 0:1496281373a5 2230 case UDPLITE_RECV_CSCOV:
apatel336 0:1496281373a5 2231 if ((*(int*)optval != 0) && ((*(int*)optval < 8)) || (*(int*)optval > 0xffff)) {
apatel336 0:1496281373a5 2232 /* don't allow illegal values! */
apatel336 0:1496281373a5 2233 sock->conn->pcb.udp->chksum_len_rx = 8;
apatel336 0:1496281373a5 2234 } else {
apatel336 0:1496281373a5 2235 sock->conn->pcb.udp->chksum_len_rx = (u16_t)*(int*)optval;
apatel336 0:1496281373a5 2236 }
apatel336 0:1496281373a5 2237 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n",
apatel336 0:1496281373a5 2238 s, (*(int*)optval)) );
apatel336 0:1496281373a5 2239 break;
apatel336 0:1496281373a5 2240 default:
apatel336 0:1496281373a5 2241 LWIP_ASSERT("unhandled optname", 0);
apatel336 0:1496281373a5 2242 break;
apatel336 0:1496281373a5 2243 } /* switch (optname) */
apatel336 0:1496281373a5 2244 break;
apatel336 0:1496281373a5 2245 #endif /* LWIP_UDP */
apatel336 0:1496281373a5 2246 default:
apatel336 0:1496281373a5 2247 LWIP_ASSERT("unhandled level", 0);
apatel336 0:1496281373a5 2248 break;
apatel336 0:1496281373a5 2249 } /* switch (level) */
apatel336 0:1496281373a5 2250 sys_sem_signal(&sock->conn->op_completed);
apatel336 0:1496281373a5 2251 }
apatel336 0:1496281373a5 2252
apatel336 0:1496281373a5 2253 int
apatel336 0:1496281373a5 2254 lwip_ioctl(int s, long cmd, void *argp)
apatel336 0:1496281373a5 2255 {
apatel336 0:1496281373a5 2256 struct lwip_sock *sock = get_socket(s);
apatel336 0:1496281373a5 2257 u8_t val;
apatel336 0:1496281373a5 2258 #if LWIP_SO_RCVBUF
apatel336 0:1496281373a5 2259 u16_t buflen = 0;
apatel336 0:1496281373a5 2260 s16_t recv_avail;
apatel336 0:1496281373a5 2261 #endif /* LWIP_SO_RCVBUF */
apatel336 0:1496281373a5 2262
apatel336 0:1496281373a5 2263 if (!sock) {
apatel336 0:1496281373a5 2264 return -1;
apatel336 0:1496281373a5 2265 }
apatel336 0:1496281373a5 2266
apatel336 0:1496281373a5 2267 switch (cmd) {
apatel336 0:1496281373a5 2268 #if LWIP_SO_RCVBUF
apatel336 0:1496281373a5 2269 case FIONREAD:
apatel336 0:1496281373a5 2270 if (!argp) {
apatel336 0:1496281373a5 2271 sock_set_errno(sock, EINVAL);
apatel336 0:1496281373a5 2272 return -1;
apatel336 0:1496281373a5 2273 }
apatel336 0:1496281373a5 2274
apatel336 0:1496281373a5 2275 SYS_ARCH_GET(sock->conn->recv_avail, recv_avail);
apatel336 0:1496281373a5 2276 if (recv_avail < 0) {
apatel336 0:1496281373a5 2277 recv_avail = 0;
apatel336 0:1496281373a5 2278 }
apatel336 0:1496281373a5 2279 *((u16_t*)argp) = (u16_t)recv_avail;
apatel336 0:1496281373a5 2280
apatel336 0:1496281373a5 2281 /* Check if there is data left from the last recv operation. /maq 041215 */
apatel336 0:1496281373a5 2282 if (sock->lastdata) {
apatel336 0:1496281373a5 2283 struct pbuf *p = (struct pbuf *)sock->lastdata;
apatel336 0:1496281373a5 2284 if (netconn_type(sock->conn) != NETCONN_TCP) {
apatel336 0:1496281373a5 2285 p = ((struct netbuf *)p)->p;
apatel336 0:1496281373a5 2286 }
apatel336 0:1496281373a5 2287 buflen = p->tot_len;
apatel336 0:1496281373a5 2288 buflen -= sock->lastoffset;
apatel336 0:1496281373a5 2289
apatel336 0:1496281373a5 2290 *((u16_t*)argp) += buflen;
apatel336 0:1496281373a5 2291 }
apatel336 0:1496281373a5 2292
apatel336 0:1496281373a5 2293 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %"U16_F"\n", s, argp, *((u16_t*)argp)));
apatel336 0:1496281373a5 2294 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 2295 return 0;
apatel336 0:1496281373a5 2296 #endif /* LWIP_SO_RCVBUF */
apatel336 0:1496281373a5 2297
apatel336 0:1496281373a5 2298 case FIONBIO:
apatel336 0:1496281373a5 2299 val = 0;
apatel336 0:1496281373a5 2300 if (argp && *(u32_t*)argp) {
apatel336 0:1496281373a5 2301 val = 1;
apatel336 0:1496281373a5 2302 }
apatel336 0:1496281373a5 2303 netconn_set_nonblocking(sock->conn, val);
apatel336 0:1496281373a5 2304 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, val));
apatel336 0:1496281373a5 2305 sock_set_errno(sock, 0);
apatel336 0:1496281373a5 2306 return 0;
apatel336 0:1496281373a5 2307
apatel336 0:1496281373a5 2308 default:
apatel336 0:1496281373a5 2309 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
apatel336 0:1496281373a5 2310 sock_set_errno(sock, ENOSYS); /* not yet implemented */
apatel336 0:1496281373a5 2311 return -1;
apatel336 0:1496281373a5 2312 } /* switch (cmd) */
apatel336 0:1496281373a5 2313 }
apatel336 0:1496281373a5 2314
apatel336 0:1496281373a5 2315 /** A minimal implementation of fcntl.
apatel336 0:1496281373a5 2316 * Currently only the commands F_GETFL and F_SETFL are implemented.
apatel336 0:1496281373a5 2317 * Only the flag O_NONBLOCK is implemented.
apatel336 0:1496281373a5 2318 */
apatel336 0:1496281373a5 2319 int
apatel336 0:1496281373a5 2320 lwip_fcntl(int s, int cmd, int val)
apatel336 0:1496281373a5 2321 {
apatel336 0:1496281373a5 2322 struct lwip_sock *sock = get_socket(s);
apatel336 0:1496281373a5 2323 int ret = -1;
apatel336 0:1496281373a5 2324
apatel336 0:1496281373a5 2325 if (!sock || !sock->conn) {
apatel336 0:1496281373a5 2326 return -1;
apatel336 0:1496281373a5 2327 }
apatel336 0:1496281373a5 2328
apatel336 0:1496281373a5 2329 switch (cmd) {
apatel336 0:1496281373a5 2330 case F_GETFL:
apatel336 0:1496281373a5 2331 ret = netconn_is_nonblocking(sock->conn) ? O_NONBLOCK : 0;
apatel336 0:1496281373a5 2332 break;
apatel336 0:1496281373a5 2333 case F_SETFL:
apatel336 0:1496281373a5 2334 if ((val & ~O_NONBLOCK) == 0) {
apatel336 0:1496281373a5 2335 /* only O_NONBLOCK, all other bits are zero */
apatel336 0:1496281373a5 2336 netconn_set_nonblocking(sock->conn, val & O_NONBLOCK);
apatel336 0:1496281373a5 2337 ret = 0;
apatel336 0:1496281373a5 2338 }
apatel336 0:1496281373a5 2339 break;
apatel336 0:1496281373a5 2340 default:
apatel336 0:1496281373a5 2341 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_fcntl(%d, UNIMPL: %d, %d)\n", s, cmd, val));
apatel336 0:1496281373a5 2342 break;
apatel336 0:1496281373a5 2343 }
apatel336 0:1496281373a5 2344 return ret;
apatel336 0:1496281373a5 2345 }
apatel336 0:1496281373a5 2346
apatel336 0:1496281373a5 2347 #endif /* LWIP_SOCKET */