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(LWS_HAVE_PTHREAD_H) 79#include <pthread.h> 80typedef pthread_mutex_t lws_mutex_t; 81#define lws_mutex_init(x) pthread_mutex_init(&(x), NULL) 82#define lws_mutex_destroy(x) pthread_mutex_destroy(&(x)) 83#define lws_mutex_lock(x) pthread_mutex_lock(&(x)) 84#define lws_mutex_unlock(x) pthread_mutex_unlock(&(x)) 85#endif 86 87#if defined(__sun) && defined(__GNUC__) 88 89#include <arpa/nameser_compat.h> 90 91#if !defined (BYTE_ORDER) 92#define BYTE_ORDER __BYTE_ORDER__ 93#endif 94 95#if !defined(LITTLE_ENDIAN) 96#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ 97#endif 98 99#if !defined(BIG_ENDIAN) 100#define BIG_ENDIAN __ORDER_BIG_ENDIAN__ 101#endif 102 103#endif /* sun + GNUC */ 104 105#if !defined(BYTE_ORDER) 106#define BYTE_ORDER __BYTE_ORDER 107#endif 108#if !defined(LITTLE_ENDIAN) 109#define LITTLE_ENDIAN __LITTLE_ENDIAN 110#endif 111#if !defined(BIG_ENDIAN) 112#define BIG_ENDIAN __BIG_ENDIAN 113#endif 114 115#if defined(LWS_BUILTIN_GETIFADDRS) 116#include "./misc/getifaddrs.h" 117#else 118 119#if defined(__HAIKU__) 120#define _BSD_SOURCE 121#endif 122#include <ifaddrs.h> 123 124#endif 125 126#if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__) 127#include <syslog.h> 128 129#if defined(__ANDROID__) 130#include <sys/resource.h> 131#endif 132 133#else 134#include <sys/syslog.h> 135#endif 136 137#ifdef __QNX__ 138# include "netinet/tcp_var.h" 139# define TCP_KEEPINTVL TCPCTL_KEEPINTVL 140# define TCP_KEEPIDLE TCPCTL_KEEPIDLE 141# define TCP_KEEPCNT TCPCTL_KEEPCNT 142#endif 143 144#define LWS_ERRNO errno 145#define LWS_EAGAIN EAGAIN 146#define LWS_EALREADY EALREADY 147#define LWS_EINPROGRESS EINPROGRESS 148#define LWS_EINTR EINTR 149#define LWS_EISCONN EISCONN 150#define LWS_ENOTCONN ENOTCONN 151#define LWS_EWOULDBLOCK EWOULDBLOCK 152#define LWS_EADDRINUSE EADDRINUSE 153#define lws_set_blocking_send(wsi) 154#define LWS_SOCK_INVALID (-1) 155 156struct lws_context; 157 158struct lws * 159wsi_from_fd(const struct lws_context *context, int fd); 160 161int 162insert_wsi(const struct lws_context *context, struct lws *wsi); 163 164struct lws_dhcpc_ifstate; 165int 166lws_plat_ifconfig(int fd, struct lws_dhcpc_ifstate *is); 167 168void 169delete_from_fd(const struct lws_context *context, int fd); 170 171#ifndef LWS_NO_FORK 172#ifdef LWS_HAVE_SYS_PRCTL_H 173#include <sys/prctl.h> 174#endif 175#endif 176 177#define compatible_close(x) close(x) 178#define compatible_file_close(fd) close(fd) 179#define lws_plat_socket_offset() (0) 180 181/* 182 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag, 183 * but happily have something equivalent in the SO_NOSIGPIPE flag. 184 */ 185#ifdef __APPLE__ 186/* iOS SDK 12+ seems to define it, undef it for compatibility both ways */ 187#undef MSG_NOSIGNAL 188#define MSG_NOSIGNAL SO_NOSIGPIPE 189#endif 190 191/* 192 * Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in 193 * POSIX 2008. 194 */ 195#if defined(__sun) && !defined(MSG_NOSIGNAL) 196 #define MSG_NOSIGNAL 0 197#endif 198 199int 200lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len, 201 size_t n, int fd, const char *iface); 202 203int 204lws_plat_if_up(const char *ifname, int fd, int up); 205