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