2019-08-14 10:44:14 +01:00
|
|
|
/*
|
2018-06-16 09:37:07 +08:00
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
2019-08-15 10:49:52 +01:00
|
|
|
* Included from lib/private-lib-core.h if no explicit platform
|
2018-06-16 09:37:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#ifndef __cplusplus
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/un.h>
|
2020-02-09 05:13:18 +00:00
|
|
|
#if defined(LWS_HAVE_EVENTFD)
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <machine/endian.h>
|
|
|
|
#endif
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#endif
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include <endian.h>
|
2019-10-05 12:47:42 +01:00
|
|
|
#include <linux/if_packet.h>
|
|
|
|
#include <net/if.h>
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
|
|
|
#if defined(__QNX__)
|
|
|
|
#include <gulliver.h>
|
|
|
|
#if defined(__LITTLEENDIAN__)
|
|
|
|
#define BYTE_ORDER __LITTLEENDIAN__
|
|
|
|
#define LITTLE_ENDIAN __LITTLEENDIAN__
|
|
|
|
#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
|
|
|
|
#endif
|
|
|
|
#if defined(__BIGENDIAN__)
|
|
|
|
#define BYTE_ORDER __BIGENDIAN__
|
|
|
|
#define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
|
|
|
|
#define BIG_ENDIAN __BIGENDIAN__
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__sun) && defined(__GNUC__)
|
|
|
|
|
|
|
|
#include <arpa/nameser_compat.h>
|
|
|
|
|
|
|
|
#if !defined (BYTE_ORDER)
|
|
|
|
#define BYTE_ORDER __BYTE_ORDER__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(LITTLE_ENDIAN)
|
|
|
|
#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(BIG_ENDIAN)
|
|
|
|
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* sun + GNUC */
|
|
|
|
|
|
|
|
#if !defined(BYTE_ORDER)
|
|
|
|
#define BYTE_ORDER __BYTE_ORDER
|
|
|
|
#endif
|
|
|
|
#if !defined(LITTLE_ENDIAN)
|
|
|
|
#define LITTLE_ENDIAN __LITTLE_ENDIAN
|
|
|
|
#endif
|
|
|
|
#if !defined(BIG_ENDIAN)
|
|
|
|
#define BIG_ENDIAN __BIG_ENDIAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LWS_BUILTIN_GETIFADDRS)
|
|
|
|
#include "./misc/getifaddrs.h"
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(__HAIKU__)
|
|
|
|
#define _BSD_SOURCE
|
|
|
|
#endif
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-08-02 17:54:57 +08:00
|
|
|
#if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__)
|
2018-06-16 09:37:07 +08:00
|
|
|
#include <syslog.h>
|
2019-08-02 17:54:57 +08:00
|
|
|
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
#else
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __QNX__
|
|
|
|
# include "netinet/tcp_var.h"
|
|
|
|
# define TCP_KEEPINTVL TCPCTL_KEEPINTVL
|
|
|
|
# define TCP_KEEPIDLE TCPCTL_KEEPIDLE
|
|
|
|
# define TCP_KEEPCNT TCPCTL_KEEPCNT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LWS_ERRNO errno
|
|
|
|
#define LWS_EAGAIN EAGAIN
|
|
|
|
#define LWS_EALREADY EALREADY
|
|
|
|
#define LWS_EINPROGRESS EINPROGRESS
|
|
|
|
#define LWS_EINTR EINTR
|
|
|
|
#define LWS_EISCONN EISCONN
|
2018-09-05 14:45:10 +08:00
|
|
|
#define LWS_ENOTCONN ENOTCONN
|
2018-06-16 09:37:07 +08:00
|
|
|
#define LWS_EWOULDBLOCK EWOULDBLOCK
|
2018-11-22 17:58:30 +08:00
|
|
|
#define LWS_EADDRINUSE EADDRINUSE
|
2018-06-16 09:37:07 +08:00
|
|
|
#define lws_set_blocking_send(wsi)
|
|
|
|
#define LWS_SOCK_INVALID (-1)
|
|
|
|
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
struct lws_context;
|
|
|
|
|
|
|
|
struct lws *
|
|
|
|
wsi_from_fd(const struct lws_context *context, int fd);
|
|
|
|
|
|
|
|
int
|
|
|
|
insert_wsi(const struct lws_context *context, struct lws *wsi);
|
|
|
|
|
2019-10-05 12:47:42 +01:00
|
|
|
int
|
|
|
|
lws_plat_ifconfig_ip(const char *ifname, int fd, uint8_t *ip, uint8_t *mask_ip,
|
|
|
|
uint8_t *gateway_ip);
|
|
|
|
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
void
|
|
|
|
delete_from_fd(const struct lws_context *context, int fd);
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
#ifndef LWS_NO_FORK
|
|
|
|
#ifdef LWS_HAVE_SYS_PRCTL_H
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define compatible_close(x) close(x)
|
|
|
|
#define lws_plat_socket_offset() (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
|
|
|
|
* but happily have something equivalent in the SO_NOSIGPIPE flag.
|
|
|
|
*/
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define MSG_NOSIGNAL SO_NOSIGPIPE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
|
|
|
|
* POSIX 2008.
|
|
|
|
*/
|
2019-10-07 10:12:03 +01:00
|
|
|
#if defined(__sun) && !defined(MSG_NOSIGNAL)
|
2018-06-16 09:37:07 +08:00
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
2019-10-05 12:47:42 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_BINDTODEVICE(int fd, const char *ifname);
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, int canned_len,
|
|
|
|
int n, int fd, const char *iface);
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_if_up(const char *ifname, int fd, int up);
|