Philippe Bazot / Mbed 2 deprecated DU4SmartCities

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

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