1 /* 2 * libwebsockets - small server side websockets and web server implementation 3 * 4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * IN THE SOFTWARE. 23 * 24 * Included from lib/private-lib-core.h if no explicit platform 25 */ 26 27 #include <fcntl.h> 28 #include <strings.h> 29 #include <unistd.h> 30 31 #include <netinet/in.h> 32 #include <netinet/tcp.h> 33 #include <arpa/inet.h> 34 #include <poll.h> 35 #include <netdb.h> 36 37 #ifndef __cplusplus 38 #include <errno.h> 39 #endif 40 #include <netdb.h> 41 #include <signal.h> 42 43 #include <sys/socket.h> 44 #include <sys/types.h> 45 #include <sys/stat.h> 46 #include <sys/time.h> 47 #include <sys/mman.h> 48 #include <sys/un.h> 49 #if defined(LWS_HAVE_EVENTFD) 50 #include <sys/eventfd.h> 51 #endif 52 53 #if defined(__APPLE__) 54 #include <machine/endian.h> 55 #endif 56 #if defined(__FreeBSD__) 57 #include <sys/endian.h> 58 #endif 59 #if defined(__linux__) 60 #include <endian.h> 61 #include <linux/if_packet.h> 62 #include <net/if.h> 63 #endif 64 #if defined(__QNX__) 65 #include <gulliver.h> 66 #if defined(__LITTLEENDIAN__) 67 #define BYTE_ORDER __LITTLEENDIAN__ 68 #define LITTLE_ENDIAN __LITTLEENDIAN__ 69 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */ 70 #endif 71 #if defined(__BIGENDIAN__) 72 #define BYTE_ORDER __BIGENDIAN__ 73 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */ 74 #define BIG_ENDIAN __BIGENDIAN__ 75 #endif 76 #endif 77 78 #if defined(__sun) && defined(__GNUC__) 79 80 #include <arpa/nameser_compat.h> 81 82 #if !defined (BYTE_ORDER) 83 #define BYTE_ORDER __BYTE_ORDER__ 84 #endif 85 86 #if !defined(LITTLE_ENDIAN) 87 #define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ 88 #endif 89 90 #if !defined(BIG_ENDIAN) 91 #define BIG_ENDIAN __ORDER_BIG_ENDIAN__ 92 #endif 93 94 #endif /* sun + GNUC */ 95 96 #if !defined(BYTE_ORDER) 97 #define BYTE_ORDER __BYTE_ORDER 98 #endif 99 #if !defined(LITTLE_ENDIAN) 100 #define LITTLE_ENDIAN __LITTLE_ENDIAN 101 #endif 102 #if !defined(BIG_ENDIAN) 103 #define BIG_ENDIAN __BIG_ENDIAN 104 #endif 105 106 #if defined(LWS_BUILTIN_GETIFADDRS) 107 #include "./misc/getifaddrs.h" 108 #else 109 110 #if defined(__HAIKU__) 111 #define _BSD_SOURCE 112 #endif 113 #include <ifaddrs.h> 114 115 #endif 116 117 #if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__) 118 #include <syslog.h> 119 120 #if defined(__ANDROID__) 121 #include <sys/resource.h> 122 #endif 123 124 #else 125 #include <sys/syslog.h> 126 #endif 127 128 #ifdef __QNX__ 129 # include "netinet/tcp_var.h" 130 # define TCP_KEEPINTVL TCPCTL_KEEPINTVL 131 # define TCP_KEEPIDLE TCPCTL_KEEPIDLE 132 # define TCP_KEEPCNT TCPCTL_KEEPCNT 133 #endif 134 135 #define LWS_ERRNO errno 136 #define LWS_EAGAIN EAGAIN 137 #define LWS_EALREADY EALREADY 138 #define LWS_EINPROGRESS EINPROGRESS 139 #define LWS_EINTR EINTR 140 #define LWS_EISCONN EISCONN 141 #define LWS_ENOTCONN ENOTCONN 142 #define LWS_EWOULDBLOCK EWOULDBLOCK 143 #define LWS_EADDRINUSE EADDRINUSE 144 #define lws_set_blocking_send(wsi) 145 #define LWS_SOCK_INVALID (-1) 146 147 struct lws_context; 148 149 struct lws * 150 wsi_from_fd(const struct lws_context *context, int fd); 151 152 int 153 insert_wsi(const struct lws_context *context, struct lws *wsi); 154 155 int 156 lws_plat_ifconfig_ip(const char *ifname, int fd, uint8_t *ip, uint8_t *mask_ip, 157 uint8_t *gateway_ip); 158 159 void 160 delete_from_fd(const struct lws_context *context, int fd); 161 162 #ifndef LWS_NO_FORK 163 #ifdef LWS_HAVE_SYS_PRCTL_H 164 #include <sys/prctl.h> 165 #endif 166 #endif 167 168 #define compatible_close(x) close(x) 169 #define lws_plat_socket_offset() (0) 170 171 /* 172 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag, 173 * but happily have something equivalent in the SO_NOSIGPIPE flag. 174 */ 175 #ifdef __APPLE__ 176 #define MSG_NOSIGNAL SO_NOSIGPIPE 177 #endif 178 179 /* 180 * Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in 181 * POSIX 2008. 182 */ 183 #if defined(__sun) && !defined(MSG_NOSIGNAL) 184 #define MSG_NOSIGNAL 0 185 #endif 186 187 int 188 lws_plat_BINDTODEVICE(int fd, const char *ifname); 189 190 int 191 lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, int canned_len, 192 int n, int fd, const char *iface); 193 194 int 195 lws_plat_if_up(const char *ifname, int fd, int up); 196