2018-06-16 09:37:07 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2023-10-14 16:52:25 -04:00
|
|
|
* Copyright (C) 2010 - 2023 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-12-22 03:38:05 +00:00
|
|
|
#if !defined(_GNU_SOURCE)
|
2018-06-16 09:37:07 +08:00
|
|
|
#define _GNU_SOURCE
|
2019-12-22 03:38:05 +00:00
|
|
|
#endif
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2023-10-14 16:52:25 -04:00
|
|
|
#if defined(LWS_HAVE_LINUX_IPV6_H)
|
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#endif
|
|
|
|
|
2019-10-05 12:47:42 +01:00
|
|
|
#include <sys/ioctl.h>
|
2020-04-23 06:17:22 +01:00
|
|
|
|
|
|
|
#if !defined(LWS_DETECTED_PLAT_IOS)
|
2019-10-05 12:47:42 +01:00
|
|
|
#include <net/route.h>
|
2020-04-23 06:17:22 +01:00
|
|
|
#endif
|
|
|
|
|
2019-10-05 12:47:42 +01:00
|
|
|
#include <net/if.h>
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
2020-03-27 13:24:44 +00:00
|
|
|
#if defined(LWS_WITH_MBEDTLS)
|
|
|
|
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
|
|
|
|
#include "mbedtls/net_sockets.h"
|
|
|
|
#else
|
|
|
|
#include "mbedtls/net.h"
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2021-02-04 09:08:17 +00:00
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
int
|
|
|
|
lws_send_pipe_choked(struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws_pollfd fds;
|
2018-10-10 13:54:43 +08:00
|
|
|
struct lws *wsi_eff;
|
2018-06-16 09:37:07 +08:00
|
|
|
|
2019-05-04 13:19:12 +01:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
|
|
|
if (wsi->ws && wsi->ws->tx_draining_ext)
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
#if defined(LWS_WITH_HTTP2)
|
|
|
|
wsi_eff = lws_get_network_wsi(wsi);
|
2018-10-10 13:54:43 +08:00
|
|
|
#else
|
|
|
|
wsi_eff = wsi;
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* the fact we checked implies we avoided back-to-back writes */
|
|
|
|
wsi_eff->could_have_pending = 0;
|
|
|
|
|
|
|
|
/* treat the fact we got a truncated send pending as if we're choked */
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
if (lws_has_buffered_out(wsi_eff)
|
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
||wsi->http.comp_ctx.buflist_comp ||
|
|
|
|
wsi->http.comp_ctx.may_have_more
|
|
|
|
#endif
|
|
|
|
)
|
2018-06-16 09:37:07 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
fds.fd = wsi_eff->desc.sockfd;
|
|
|
|
fds.events = POLLOUT;
|
|
|
|
fds.revents = 0;
|
|
|
|
|
|
|
|
if (poll(&fds, 1, 0) != 1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if ((fds.revents & POLLOUT) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* okay to send another packet without blocking */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-07 09:49:52 +08:00
|
|
|
int
|
2019-10-21 23:56:32 +00:00
|
|
|
lws_plat_set_nonblocking(lws_sockfd_type fd)
|
2019-03-07 09:49:52 +08:00
|
|
|
{
|
|
|
|
return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
|
|
|
|
}
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
int
|
2018-09-05 14:45:10 +08:00
|
|
|
lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
|
2018-06-16 09:37:07 +08:00
|
|
|
{
|
|
|
|
int optval = 1;
|
|
|
|
socklen_t optlen = sizeof(optval);
|
|
|
|
|
|
|
|
#if defined(__APPLE__) || \
|
|
|
|
defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
|
|
|
|
defined(__NetBSD__) || \
|
|
|
|
defined(__OpenBSD__) || \
|
|
|
|
defined(__HAIKU__)
|
|
|
|
struct protoent *tcp_proto;
|
|
|
|
#endif
|
|
|
|
|
2018-08-01 06:52:03 +08:00
|
|
|
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
|
2018-06-23 05:44:36 +08:00
|
|
|
|
2018-09-05 14:45:10 +08:00
|
|
|
if (!unix_skt && vhost->ka_time) {
|
2018-06-16 09:37:07 +08:00
|
|
|
/* enable keepalive on this socket */
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
|
|
|
|
(const void *)&optval, optlen) < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
#if defined(__APPLE__) || \
|
|
|
|
defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
|
|
|
|
defined(__NetBSD__) || \
|
|
|
|
defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \
|
|
|
|
defined(__HAIKU__)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* didn't find a way to set these per-socket, need to
|
|
|
|
* tune kernel systemwide values
|
|
|
|
*/
|
|
|
|
#else
|
|
|
|
/* set the keepalive conditions we want on it too */
|
|
|
|
|
|
|
|
#if defined(LWS_HAVE_TCP_USER_TIMEOUT)
|
|
|
|
optval = 1000 * (vhost->ka_time +
|
|
|
|
(vhost->ka_interval * vhost->ka_probes));
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
|
|
|
|
(const void *)&optval, optlen) < 0)
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
optval = vhost->ka_time;
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
|
|
|
|
(const void *)&optval, optlen) < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
optval = vhost->ka_interval;
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
|
|
|
|
(const void *)&optval, optlen) < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
optval = vhost->ka_probes;
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
|
|
|
|
(const void *)&optval, optlen) < 0)
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(SO_BINDTODEVICE)
|
2018-09-05 14:45:10 +08:00
|
|
|
if (!unix_skt && vhost->bind_iface && vhost->iface) {
|
2018-06-16 09:37:07 +08:00
|
|
|
lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
|
2020-12-12 06:21:40 +00:00
|
|
|
(socklen_t)strlen(vhost->iface)) < 0) {
|
2018-06-16 09:37:07 +08:00
|
|
|
lwsl_warn("Failed to bind to device %s\n", vhost->iface);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Disable Nagle */
|
|
|
|
optval = 1;
|
2023-09-19 15:58:38 +08:00
|
|
|
#if defined (__sun) || defined(__QNX__) || defined(__NuttX__)
|
2018-09-05 14:45:10 +08:00
|
|
|
if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
|
2018-06-16 09:37:07 +08:00
|
|
|
return 1;
|
|
|
|
#elif !defined(__APPLE__) && \
|
|
|
|
!defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
|
|
|
|
!defined(__NetBSD__) && \
|
|
|
|
!defined(__OpenBSD__) && \
|
|
|
|
!defined(__HAIKU__)
|
2018-09-05 14:45:10 +08:00
|
|
|
if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
|
2018-06-16 09:37:07 +08:00
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
tcp_proto = getprotobyname("TCP");
|
2018-09-05 14:45:10 +08:00
|
|
|
if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
|
2018-06-16 09:37:07 +08:00
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
|
2019-03-07 09:49:52 +08:00
|
|
|
return lws_plat_set_nonblocking(fd);
|
2018-06-16 09:37:07 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 15:58:38 +08:00
|
|
|
#if !defined(__NuttX__)
|
2021-02-04 09:08:17 +00:00
|
|
|
static const int ip_opt_lws_flags[] = {
|
2024-09-25 06:22:45 +01:00
|
|
|
LCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT
|
|
|
|
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
|
|
|
|
, LCCSCF_IP_HIGH_RELIABILITY
|
2021-02-14 16:04:40 +00:00
|
|
|
, LCCSCF_IP_LOW_COST
|
|
|
|
#endif
|
2021-02-04 09:08:17 +00:00
|
|
|
}, ip_opt_val[] = {
|
2024-09-25 06:22:45 +01:00
|
|
|
IPTOS_LOWDELAY, IPTOS_THROUGHPUT
|
2022-06-30 12:37:37 +03:00
|
|
|
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
|
2024-09-25 06:22:45 +01:00
|
|
|
, IPTOS_RELIABILITY
|
2021-02-14 16:04:40 +00:00
|
|
|
, IPTOS_MINCOST
|
|
|
|
#endif
|
2021-02-04 09:08:17 +00:00
|
|
|
};
|
|
|
|
#if !defined(LWS_WITH_NO_LOGS)
|
|
|
|
static const char *ip_opt_names[] = {
|
2024-09-25 06:22:45 +01:00
|
|
|
"LOWDELAY", "THROUGHPUT"
|
|
|
|
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
|
|
|
|
, "RELIABILITY"
|
2021-02-14 16:04:40 +00:00
|
|
|
, "MINCOST"
|
|
|
|
#endif
|
2021-02-04 09:08:17 +00:00
|
|
|
};
|
|
|
|
#endif
|
2023-09-19 15:58:38 +08:00
|
|
|
#endif
|
2021-02-04 09:08:17 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
|
|
|
|
{
|
|
|
|
int optval = (int)pri, ret = 0, n;
|
|
|
|
socklen_t optlen = sizeof(optval);
|
2022-04-03 09:03:10 +01:00
|
|
|
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
2021-02-04 09:08:17 +00:00
|
|
|
int en;
|
|
|
|
#endif
|
|
|
|
|
2021-05-25 06:09:01 +01:00
|
|
|
#if 0
|
|
|
|
#if defined(TCP_FASTOPEN_CONNECT)
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval,
|
|
|
|
sizeof(optval)))
|
|
|
|
lwsl_warn("%s: FASTOPEN_CONNECT failed\n", __func__);
|
|
|
|
optval = (int)pri;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-02-04 09:08:17 +00:00
|
|
|
#if !defined(__APPLE__) && \
|
|
|
|
!defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
|
|
|
|
!defined(__NetBSD__) && \
|
|
|
|
!defined(__OpenBSD__) && \
|
2021-03-08 10:33:48 +00:00
|
|
|
!defined(__sun) && \
|
2021-06-06 18:45:25 +01:00
|
|
|
!defined(__HAIKU__) && \
|
2021-12-15 04:52:33 +00:00
|
|
|
!defined(__CYGWIN__) && \
|
2023-09-19 15:58:38 +08:00
|
|
|
!defined(__QNX__) && \
|
|
|
|
!defined(__NuttX__)
|
2021-02-04 09:08:17 +00:00
|
|
|
|
|
|
|
/* the BSDs don't have SO_PRIORITY */
|
|
|
|
|
|
|
|
if (pri) { /* 0 is the default already */
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY,
|
|
|
|
(const void *)&optval, optlen) < 0) {
|
2022-04-03 09:03:10 +01:00
|
|
|
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
2021-02-04 09:08:17 +00:00
|
|
|
en = errno;
|
|
|
|
lwsl_warn("%s: unable to set socket pri %d: errno %d\n",
|
|
|
|
__func__, (int)pri, en);
|
|
|
|
#endif
|
|
|
|
ret = 1;
|
|
|
|
} else
|
|
|
|
lwsl_notice("%s: set pri %u\n", __func__, pri);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-14 10:28:33 +00:00
|
|
|
if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) {
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(const void *)&optval, optlen) < 0) {
|
2022-04-03 09:03:10 +01:00
|
|
|
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
2022-01-14 10:28:33 +00:00
|
|
|
en = errno;
|
|
|
|
lwsl_warn("%s: unable to reuse local addresses: errno %d\n",
|
|
|
|
__func__, en);
|
|
|
|
#endif
|
|
|
|
ret = 1;
|
|
|
|
} else
|
|
|
|
lwsl_notice("%s: set reuse addresses\n", __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-14 16:52:25 -04:00
|
|
|
if (lws_flags & LCCSCF_IPV6_PREFER_PUBLIC_ADDR) {
|
|
|
|
#if defined(LWS_WITH_IPV6) && defined(IPV6_PREFER_SRC_PUBLIC)
|
|
|
|
optval = IPV6_PREFER_SRC_PUBLIC;
|
|
|
|
|
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES,
|
|
|
|
(const void *)&optval, optlen) < 0) {
|
|
|
|
#if (_LWS_ENABLED_LOGS & LLL_WARN)
|
|
|
|
en = errno;
|
|
|
|
lwsl_warn("%s: unable to set IPV6_PREFER_SRC_PUBLIC: errno %d\n",
|
|
|
|
__func__, en);
|
|
|
|
#endif
|
|
|
|
ret = 1;
|
|
|
|
} else
|
|
|
|
lwsl_notice("%s: set IPV6_PREFER_SRC_PUBLIC\n", __func__);
|
|
|
|
#else
|
|
|
|
lwsl_err("%s: IPV6_PREFER_SRC_PUBLIC UNIMPLEMENTED on this platform\n", __func__);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-19 15:58:38 +08:00
|
|
|
#if !defined(__NuttX__)
|
2024-09-25 06:22:45 +01:00
|
|
|
/* array size differs by platform */
|
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(ip_opt_lws_flags); n++) {
|
2021-02-04 09:08:17 +00:00
|
|
|
if (!(lws_flags & ip_opt_lws_flags[n]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
optval = (int)ip_opt_val[n];
|
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_TOS, (const void *)&optval,
|
|
|
|
optlen) < 0) {
|
|
|
|
#if !defined(LWS_WITH_NO_LOGS)
|
|
|
|
en = errno;
|
|
|
|
lwsl_warn("%s: unable to set %s: errno %d\n", __func__,
|
|
|
|
ip_opt_names[n], en);
|
|
|
|
#endif
|
|
|
|
ret = 1;
|
|
|
|
} else
|
|
|
|
lwsl_notice("%s: set ip flag %s\n", __func__,
|
|
|
|
ip_opt_names[n]);
|
|
|
|
}
|
2023-09-19 15:58:38 +08:00
|
|
|
#endif
|
2021-02-04 09:08:17 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
/* cast a struct sockaddr_in6 * into addr for ipv6 */
|
|
|
|
|
2021-07-20 06:17:13 +01:00
|
|
|
enum {
|
|
|
|
IP_SCORE_NONE,
|
|
|
|
IP_SCORE_NONNATIVE,
|
|
|
|
IP_SCORE_IPV6_SCOPE_BASE,
|
|
|
|
/* ipv6 scopes */
|
|
|
|
IP_SCORE_GLOBAL_NATIVE = 18
|
|
|
|
};
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
int
|
|
|
|
lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
|
|
|
|
size_t addrlen)
|
|
|
|
{
|
|
|
|
int rc = LWS_ITOSA_NOT_EXIST;
|
|
|
|
|
|
|
|
struct ifaddrs *ifr;
|
|
|
|
struct ifaddrs *ifc;
|
2021-07-20 06:17:13 +01:00
|
|
|
#if defined(LWS_WITH_IPV6)
|
2018-06-16 09:37:07 +08:00
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
|
2021-07-20 06:17:13 +01:00
|
|
|
unsigned long sco = IP_SCORE_NONE;
|
|
|
|
unsigned long ts;
|
|
|
|
const uint8_t *p;
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
|
|
|
|
2020-07-29 11:14:37 +01:00
|
|
|
if (getifaddrs(&ifr)) {
|
|
|
|
lwsl_err("%s: unable to getifaddrs: errno %d\n", __func__, errno);
|
|
|
|
|
|
|
|
return LWS_ITOSA_USABLE;
|
|
|
|
}
|
2021-07-20 06:17:13 +01:00
|
|
|
for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
|
2020-07-29 11:14:37 +01:00
|
|
|
if (!ifc->ifa_addr || !ifc->ifa_name)
|
2018-06-16 09:37:07 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
|
|
|
|
ifc->ifa_name, ifname,
|
|
|
|
ifc->ifa_addr->sa_family, ipv6);
|
|
|
|
|
|
|
|
if (strcmp(ifc->ifa_name, ifname))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (ifc->ifa_addr->sa_family) {
|
|
|
|
#if defined(AF_PACKET)
|
|
|
|
case AF_PACKET:
|
|
|
|
/* interface exists but is not usable */
|
2021-07-20 06:17:13 +01:00
|
|
|
if (rc == LWS_ITOSA_NOT_EXIST)
|
|
|
|
rc = LWS_ITOSA_NOT_USABLE;
|
2018-06-16 09:37:07 +08:00
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case AF_INET:
|
2021-07-20 06:17:13 +01:00
|
|
|
#if defined(LWS_WITH_IPV6)
|
2018-06-16 09:37:07 +08:00
|
|
|
if (ipv6) {
|
2021-07-20 06:17:13 +01:00
|
|
|
/* any existing solution is better than this */
|
|
|
|
if (sco != IP_SCORE_NONE)
|
|
|
|
break;
|
|
|
|
sco = IP_SCORE_NONNATIVE;
|
|
|
|
rc = LWS_ITOSA_USABLE;
|
2018-06-16 09:37:07 +08:00
|
|
|
/* map IPv4 to IPv6 */
|
2019-03-08 10:50:55 +08:00
|
|
|
memset((char *)&addr6->sin6_addr, 0,
|
2018-06-16 09:37:07 +08:00
|
|
|
sizeof(struct in6_addr));
|
|
|
|
addr6->sin6_addr.s6_addr[10] = 0xff;
|
|
|
|
addr6->sin6_addr.s6_addr[11] = 0xff;
|
|
|
|
memcpy(&addr6->sin6_addr.s6_addr[12],
|
|
|
|
&((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
|
|
|
|
sizeof(struct in_addr));
|
2019-03-08 08:58:56 +08:00
|
|
|
lwsl_debug("%s: uplevelling ipv4 bind to ipv6\n", __func__);
|
2021-07-20 06:17:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sco = IP_SCORE_GLOBAL_NATIVE;
|
2018-06-16 09:37:07 +08:00
|
|
|
#endif
|
2021-07-20 06:17:13 +01:00
|
|
|
rc = LWS_ITOSA_USABLE;
|
|
|
|
memcpy(addr, (struct sockaddr_in *)ifc->ifa_addr,
|
2018-06-16 09:37:07 +08:00
|
|
|
sizeof(struct sockaddr_in));
|
|
|
|
break;
|
2021-07-20 06:17:13 +01:00
|
|
|
#if defined(LWS_WITH_IPV6)
|
2018-06-16 09:37:07 +08:00
|
|
|
case AF_INET6:
|
2021-07-20 06:17:13 +01:00
|
|
|
p = (const uint8_t *)
|
|
|
|
&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr;
|
|
|
|
ts = IP_SCORE_IPV6_SCOPE_BASE;
|
|
|
|
if (p[0] == 0xff)
|
|
|
|
ts = (unsigned long)(IP_SCORE_IPV6_SCOPE_BASE + (p[1] & 0xf));
|
|
|
|
|
|
|
|
if (sco >= ts)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sco = ts;
|
|
|
|
rc = LWS_ITOSA_USABLE;
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
memcpy(&addr6->sin6_addr,
|
2021-07-20 06:17:13 +01:00
|
|
|
&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
|
2018-06-16 09:37:07 +08:00
|
|
|
sizeof(struct in6_addr));
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2021-11-08 09:51:19 +00:00
|
|
|
break;
|
2018-06-16 09:37:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifr);
|
|
|
|
|
2021-05-06 14:33:05 +01:00
|
|
|
if (rc &&
|
|
|
|
!lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))
|
|
|
|
rc = LWS_ITOSA_USABLE;
|
2018-06-16 09:37:07 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
|
2018-06-16 09:37:07 +08:00
|
|
|
{
|
|
|
|
return inet_ntop(af, src, dst, cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_inet_pton(int af, const char *src, void *dst)
|
|
|
|
{
|
|
|
|
return inet_pton(af, src, dst);
|
|
|
|
}
|
2019-10-05 12:47:42 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
|
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct ifreq i;
|
|
|
|
|
|
|
|
memset(&i, 0, sizeof(i));
|
|
|
|
lws_strncpy(i.ifr_name, ifname, sizeof(i.ifr_name));
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCGIFHWADDR, &i) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(hwaddr, &i.ifr_hwaddr.sa_data, 6);
|
|
|
|
|
|
|
|
return 6;
|
|
|
|
#else
|
|
|
|
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
|
|
|
|
size_t n, int fd, const char *iface)
|
2019-10-05 12:47:42 +01:00
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct sockaddr_ll sll;
|
|
|
|
uint16_t *p16 = (uint16_t *)p;
|
|
|
|
uint32_t ucs = 0;
|
|
|
|
|
|
|
|
memcpy(p, canned, canned_len);
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
p[2] = (uint8_t)(n >> 8);
|
|
|
|
p[3] = (uint8_t)(n);
|
2019-10-05 12:47:42 +01:00
|
|
|
|
|
|
|
while (p16 < (uint16_t *)(p + 20))
|
2024-09-25 06:37:41 +01:00
|
|
|
ucs = ucs + (uint32_t)(ntohs((uint16_t)(*p16++)));
|
2019-10-05 12:47:42 +01:00
|
|
|
|
|
|
|
ucs += ucs >> 16;
|
|
|
|
ucs ^= 0xffff;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
p[10] = (uint8_t)(ucs >> 8);
|
|
|
|
p[11] = (uint8_t)(ucs);
|
|
|
|
p[24] = (uint8_t)((n - 20) >> 8);
|
|
|
|
p[25] = (uint8_t)((n - 20));
|
2019-10-05 12:47:42 +01:00
|
|
|
|
|
|
|
memset(&sll, 0, sizeof(sll));
|
|
|
|
sll.sll_family = AF_PACKET;
|
2025-01-22 10:08:55 +00:00
|
|
|
sll.sll_protocol = (uint16_t)(htons((uint16_t)0x800));
|
2019-10-05 12:47:42 +01:00
|
|
|
sll.sll_halen = 6;
|
2020-12-12 06:21:40 +00:00
|
|
|
sll.sll_ifindex = (int)if_nametoindex(iface);
|
2019-10-05 12:47:42 +01:00
|
|
|
memset(sll.sll_addr, 0xff, 6);
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
return (int)sendto(fd, p, n, 0, (struct sockaddr *)&sll, sizeof(sll));
|
2019-10-05 12:47:42 +01:00
|
|
|
#else
|
|
|
|
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_if_up(const char *ifname, int fd, int up)
|
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
lws_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
|
|
|
|
lwsl_err("%s: SIOCGIFFLAGS fail\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up)
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
|
|
|
else
|
|
|
|
ifr.ifr_flags &= ~IFF_UP;
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
|
|
|
|
lwsl_err("%s: SIOCSIFFLAGS fail\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-01-11 14:04:50 +00:00
|
|
|
lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
|
2019-10-05 12:47:42 +01:00
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct ifreq i;
|
|
|
|
|
|
|
|
memset(&i, 0, sizeof(i));
|
|
|
|
i.ifr_addr.sa_family = AF_INET;
|
|
|
|
lws_strncpy(i.ifr_ifrn.ifrn_name, ifname,
|
|
|
|
sizeof(i.ifr_ifrn.ifrn_name));
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &i, sizeof(i)) < 0) {
|
|
|
|
lwsl_notice("%s: failed %d\n", __func__, LWS_ERRNO);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-02-09 22:40:58 +00:00
|
|
|
lws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)
|
2019-10-05 12:47:42 +01:00
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct rtentry route;
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
memset(&route, 0, sizeof(route));
|
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
lws_strncpy(ifr.ifr_name, is->ifname, IFNAMSIZ);
|
2019-10-05 12:47:42 +01:00
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
lws_plat_if_up(is->ifname, fd, 0);
|
2019-10-05 12:47:42 +01:00
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
memcpy(&ifr.ifr_addr, &is->sa46[LWSDH_SA46_IP], sizeof(struct sockaddr));
|
2019-10-05 12:47:42 +01:00
|
|
|
if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) {
|
|
|
|
lwsl_err("%s: SIOCSIFADDR fail\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
if (is->sa46[LWSDH_SA46_IP].sa4.sin_family == AF_INET) {
|
|
|
|
struct sockaddr_in sin;
|
2019-10-05 12:47:42 +01:00
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = *(uint32_t *)&is->nums[LWSDH_IPV4_SUBNET_MASK];
|
|
|
|
memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
|
|
|
|
if (ioctl(fd, SIOCSIFNETMASK, &ifr) < 0) {
|
|
|
|
lwsl_err("%s: SIOCSIFNETMASK fail\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-10-05 12:47:42 +01:00
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
lws_plat_if_up(is->ifname, fd, 1);
|
2019-10-05 12:47:42 +01:00
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
memcpy(&route.rt_gateway,
|
|
|
|
&is->sa46[LWSDH_SA46_IPV4_ROUTER].sa4,
|
|
|
|
sizeof(struct sockaddr));
|
2019-10-05 12:47:42 +01:00
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
sin.sin_addr.s_addr = 0;
|
|
|
|
memcpy(&route.rt_dst, &sin, sizeof(struct sockaddr));
|
|
|
|
memcpy(&route.rt_genmask, &sin, sizeof(struct sockaddr));
|
2019-10-05 12:47:42 +01:00
|
|
|
|
2021-02-09 22:40:58 +00:00
|
|
|
route.rt_flags = RTF_UP | RTF_GATEWAY;
|
|
|
|
route.rt_metric = 100;
|
|
|
|
route.rt_dev = (char *)is->ifname;
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCADDRT, &route) < 0) {
|
|
|
|
lwsl_err("%s: SIOCADDRT 0x%x fail: %d\n", __func__,
|
|
|
|
(unsigned int)htonl(*(uint32_t *)&is->
|
|
|
|
sa46[LWSDH_SA46_IPV4_ROUTER].
|
|
|
|
sa4.sin_addr.s_addr), LWS_ERRNO);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
lws_plat_if_up(is->ifname, fd, 1);
|
2019-10-05 12:47:42 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
2020-03-27 13:24:44 +00:00
|
|
|
|
2021-03-12 11:21:30 +05:30
|
|
|
int
|
|
|
|
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:24:44 +00:00
|
|
|
#if defined(LWS_WITH_MBEDTLS)
|
|
|
|
int
|
|
|
|
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
|
|
|
|
{
|
2022-02-22 17:35:02 +00:00
|
|
|
int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
|
2020-03-27 13:24:44 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
ret = (int)write(fd, buf, len);
|
2020-03-27 13:24:44 +00:00
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
|
|
|
|
if (errno == EPIPE || errno == ECONNRESET)
|
|
|
|
return MBEDTLS_ERR_NET_CONN_RESET;
|
|
|
|
|
|
|
|
if( errno == EINTR )
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
|
|
|
|
return MBEDTLS_ERR_NET_SEND_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
|
|
|
|
{
|
2022-02-22 17:35:02 +00:00
|
|
|
int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
|
2020-03-27 13:24:44 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
|
|
|
|
|
|
|
|
ret = (int)read(fd, buf, len);
|
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
|
|
|
|
if (errno == EPIPE || errno == ECONNRESET)
|
|
|
|
return MBEDTLS_ERR_NET_CONN_RESET;
|
|
|
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
|
|
|
|
return MBEDTLS_ERR_NET_RECV_FAILED;
|
|
|
|
}
|
|
|
|
#endif
|