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