2019-01-13 06:58:21 +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>
|
2019-01-13 06:58:21 +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:
|
2019-01-13 06:58:21 +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.
|
2019-01-13 06:58:21 +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.
|
2019-01-13 06:58:21 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2020-05-14 21:28:48 +01:00
|
|
|
#include <errno.h>
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-08-18 10:35:43 +01:00
|
|
|
#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE)
|
2019-03-08 08:58:56 +08:00
|
|
|
static int
|
2019-01-13 06:58:21 +08:00
|
|
|
interface_to_sa(struct lws_vhost *vh, const char *ifname,
|
2019-03-08 08:58:56 +08:00
|
|
|
struct sockaddr_in *addr, size_t addrlen, int allow_ipv6)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
|
|
|
int ipv6 = 0;
|
|
|
|
#ifdef LWS_WITH_IPV6
|
2019-03-08 08:58:56 +08:00
|
|
|
if (allow_ipv6)
|
|
|
|
ipv6 = LWS_IPV6_ENABLED(vh);
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
|
|
|
(void)vh;
|
|
|
|
|
|
|
|
return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LWS_PLAT_OPTEE
|
|
|
|
static int
|
|
|
|
lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
|
|
|
|
int name_len, char *rip, int rip_len)
|
|
|
|
{
|
|
|
|
struct addrinfo ai, *res;
|
|
|
|
struct sockaddr_in addr4;
|
|
|
|
|
|
|
|
rip[0] = '\0';
|
|
|
|
name[0] = '\0';
|
|
|
|
addr4.sin_family = AF_UNSPEC;
|
|
|
|
|
|
|
|
#ifdef LWS_WITH_IPV6
|
|
|
|
if (LWS_IPV6_ENABLED(vh)) {
|
|
|
|
if (!lws_plat_inet_ntop(AF_INET6,
|
|
|
|
&((struct sockaddr_in6 *)ads)->sin6_addr,
|
2020-12-12 06:21:40 +00:00
|
|
|
rip, (socklen_t)rip_len)) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_vhost_err(vh, "inet_ntop: %s", strerror(LWS_ERRNO));
|
2019-01-13 06:58:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip off the IPv4 to IPv6 header if one exists
|
|
|
|
if (strncmp(rip, "::ffff:", 7) == 0)
|
|
|
|
memmove(rip, rip + 7, strlen(rip) - 6);
|
|
|
|
|
|
|
|
getnameinfo((struct sockaddr *)ads, sizeof(struct sockaddr_in6),
|
2021-06-20 09:13:48 +01:00
|
|
|
name,
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
(size_t)name_len,
|
|
|
|
#else
|
|
|
|
(socklen_t)name_len,
|
|
|
|
#endif
|
|
|
|
NULL, 0, 0);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct addrinfo *result;
|
|
|
|
|
|
|
|
memset(&ai, 0, sizeof ai);
|
|
|
|
ai.ai_family = PF_UNSPEC;
|
|
|
|
ai.ai_socktype = SOCK_STREAM;
|
2019-08-18 10:35:43 +01:00
|
|
|
#if !defined(LWS_PLAT_FREERTOS)
|
2019-01-13 06:58:21 +08:00
|
|
|
if (getnameinfo((struct sockaddr *)ads,
|
|
|
|
sizeof(struct sockaddr_in),
|
2021-06-20 09:13:48 +01:00
|
|
|
name,
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
(size_t)name_len,
|
|
|
|
#else
|
|
|
|
(socklen_t)name_len,
|
|
|
|
#endif
|
|
|
|
NULL, 0, 0))
|
2019-01-13 06:58:21 +08:00
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (getaddrinfo(name, NULL, &ai, &result))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
res = result;
|
|
|
|
while (addr4.sin_family == AF_UNSPEC && res) {
|
|
|
|
switch (res->ai_family) {
|
|
|
|
case AF_INET:
|
|
|
|
addr4.sin_addr =
|
|
|
|
((struct sockaddr_in *)res->ai_addr)->sin_addr;
|
|
|
|
addr4.sin_family = AF_INET;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = res->ai_next;
|
|
|
|
}
|
|
|
|
freeaddrinfo(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr4.sin_family == AF_UNSPEC)
|
|
|
|
return -1;
|
|
|
|
|
2021-02-01 11:48:04 +00:00
|
|
|
if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip,
|
|
|
|
(socklen_t)rip_len) == NULL)
|
2019-01-13 06:58:21 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-13 18:08:28 +01:00
|
|
|
const char *
|
2020-01-11 14:04:50 +00:00
|
|
|
lws_get_peer_simple_fd(lws_sockfd_type fd, char *name, size_t namelen)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
2019-10-13 18:08:28 +01:00
|
|
|
lws_sockaddr46 sa46;
|
|
|
|
socklen_t len = sizeof(sa46);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-10-13 18:08:28 +01:00
|
|
|
if (getpeername(fd, (struct sockaddr *)&sa46, &len) < 0) {
|
|
|
|
lws_snprintf(name, namelen, "getpeername: %s",
|
|
|
|
strerror(LWS_ERRNO));
|
|
|
|
return name;
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
2019-10-13 18:08:28 +01:00
|
|
|
lws_sa46_write_numeric_address(&sa46, name, namelen);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-10-13 18:08:28 +01:00
|
|
|
const char *
|
2020-01-11 14:04:50 +00:00
|
|
|
lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen)
|
2019-10-13 18:08:28 +01:00
|
|
|
{
|
|
|
|
wsi = lws_get_network_wsi(wsi);
|
|
|
|
return lws_get_peer_simple_fd(wsi->desc.sockfd, name, namelen);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
void
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
|
|
|
|
int name_len, char *rip, int rip_len)
|
|
|
|
{
|
|
|
|
#ifndef LWS_PLAT_OPTEE
|
|
|
|
socklen_t len;
|
|
|
|
#ifdef LWS_WITH_IPV6
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in sin4;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
rip[0] = '\0';
|
|
|
|
name[0] = '\0';
|
|
|
|
|
|
|
|
#ifdef LWS_WITH_IPV6
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (LWS_IPV6_ENABLED(wsi->a.vhost)) {
|
2019-01-13 06:58:21 +08:00
|
|
|
len = sizeof(sin6);
|
|
|
|
p = &sin6;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
len = sizeof(sin4);
|
|
|
|
p = &sin4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getpeername(fd, p, &len) < 0) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_warn(wsi, "getpeername: %s", strerror(LWS_ERRNO));
|
2019-01-13 06:58:21 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
lws_get_addresses(wsi->a.vhost, p, name, name_len, rip, rip_len);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
bail:
|
|
|
|
#endif
|
|
|
|
(void)wsi;
|
|
|
|
(void)fd;
|
|
|
|
(void)name;
|
|
|
|
(void)name_len;
|
|
|
|
(void)rip;
|
|
|
|
(void)rip_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* note: this returns a random port, or one of these <= 0 return codes:
|
|
|
|
*
|
|
|
|
* LWS_ITOSA_USABLE: the interface is usable, returned if so and sockfd invalid
|
|
|
|
* LWS_ITOSA_NOT_EXIST: the requested iface does not even exist
|
|
|
|
* LWS_ITOSA_NOT_USABLE: the requested iface exists but is not usable (eg, no IP)
|
|
|
|
* LWS_ITOSA_BUSY: the port at the requested iface + port is already in use
|
|
|
|
*/
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2021-02-05 13:08:41 +00:00
|
|
|
lws_socket_bind(struct lws_vhost *vhost, struct lws *wsi,
|
|
|
|
lws_sockfd_type sockfd, int port, const char *iface,
|
2021-06-21 10:36:36 +01:00
|
|
|
int af)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
|
|
|
#ifdef LWS_WITH_UNIX_SOCK
|
|
|
|
struct sockaddr_un serv_unix;
|
|
|
|
#endif
|
|
|
|
#ifdef LWS_WITH_IPV6
|
|
|
|
struct sockaddr_in6 serv_addr6;
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in serv_addr4;
|
|
|
|
#ifndef LWS_PLAT_OPTEE
|
|
|
|
socklen_t len = sizeof(struct sockaddr_storage);
|
|
|
|
#endif
|
2021-06-21 10:36:36 +01:00
|
|
|
int n = 0;
|
2019-08-18 10:35:43 +01:00
|
|
|
#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE)
|
2019-01-13 06:58:21 +08:00
|
|
|
int m;
|
|
|
|
#endif
|
2021-02-05 13:08:41 +00:00
|
|
|
struct sockaddr_storage sin, *psin = &sin;
|
2019-01-13 06:58:21 +08:00
|
|
|
struct sockaddr *v;
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
/* if there's a wsi, we want to mark it with our source ads:port */
|
|
|
|
if (wsi)
|
|
|
|
psin = (struct sockaddr_storage *)&wsi->sa46_local;
|
|
|
|
|
2021-06-21 10:36:36 +01:00
|
|
|
switch (af) {
|
2019-01-13 06:58:21 +08:00
|
|
|
#if defined(LWS_WITH_UNIX_SOCK)
|
2021-06-21 10:36:36 +01:00
|
|
|
case AF_UNIX:
|
2019-01-13 06:58:21 +08:00
|
|
|
v = (struct sockaddr *)&serv_unix;
|
2019-03-08 10:50:55 +08:00
|
|
|
memset(&serv_unix, 0, sizeof(serv_unix));
|
2019-01-13 06:58:21 +08:00
|
|
|
serv_unix.sun_family = AF_UNIX;
|
|
|
|
if (!iface)
|
|
|
|
return LWS_ITOSA_NOT_EXIST;
|
|
|
|
if (sizeof(serv_unix.sun_path) <= strlen(iface)) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_err(wsi, "\"%s\" too long for UNIX domain socket",
|
2019-01-13 06:58:21 +08:00
|
|
|
iface);
|
|
|
|
return LWS_ITOSA_NOT_EXIST;
|
|
|
|
}
|
2020-05-24 13:57:21 +01:00
|
|
|
n = (int)(sizeof(uint16_t) + strlen(iface));
|
2019-01-13 06:58:21 +08:00
|
|
|
strcpy(serv_unix.sun_path, iface);
|
|
|
|
if (serv_unix.sun_path[0] == '@')
|
|
|
|
serv_unix.sun_path[0] = '\0';
|
|
|
|
else
|
|
|
|
unlink(serv_unix.sun_path);
|
|
|
|
|
2020-05-15 18:21:56 +01:00
|
|
|
// lwsl_hexdump_notice(v, n);
|
2021-06-21 10:36:36 +01:00
|
|
|
break;
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_IPV6) && !defined(LWS_PLAT_FREERTOS)
|
2021-06-21 10:36:36 +01:00
|
|
|
case AF_INET6:
|
2019-01-13 06:58:21 +08:00
|
|
|
v = (struct sockaddr *)&serv_addr6;
|
|
|
|
n = sizeof(struct sockaddr_in6);
|
2021-06-21 10:36:36 +01:00
|
|
|
|
2019-03-08 10:50:55 +08:00
|
|
|
memset(&serv_addr6, 0, sizeof(serv_addr6));
|
2021-07-20 06:17:13 +01:00
|
|
|
serv_addr6.sin6_family = AF_INET6;
|
2019-01-13 06:58:21 +08:00
|
|
|
if (iface) {
|
|
|
|
m = interface_to_sa(vhost, iface,
|
2020-12-12 06:21:40 +00:00
|
|
|
(struct sockaddr_in *)v, (unsigned int)n, 1);
|
2019-01-13 06:58:21 +08:00
|
|
|
if (m == LWS_ITOSA_NOT_USABLE) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "netif %s: Not usable",
|
|
|
|
iface);
|
2019-01-13 06:58:21 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
if (m == LWS_ITOSA_NOT_EXIST) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "netif %s: Does not exist",
|
|
|
|
iface);
|
2019-01-13 06:58:21 +08:00
|
|
|
return m;
|
|
|
|
}
|
2021-07-20 06:17:13 +01:00
|
|
|
serv_addr6.sin6_scope_id = (unsigned int)htonl((uint32_t)
|
|
|
|
lws_get_addr_scope(wsi, iface));
|
|
|
|
}
|
|
|
|
|
|
|
|
serv_addr6.sin6_port = (uint16_t)htons((uint16_t)port);
|
2021-06-21 10:36:36 +01:00
|
|
|
break;
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
2021-06-21 10:36:36 +01:00
|
|
|
|
|
|
|
case AF_INET:
|
2019-01-13 06:58:21 +08:00
|
|
|
v = (struct sockaddr *)&serv_addr4;
|
|
|
|
n = sizeof(serv_addr4);
|
2019-03-08 10:50:55 +08:00
|
|
|
memset(&serv_addr4, 0, sizeof(serv_addr4));
|
2019-01-13 06:58:21 +08:00
|
|
|
serv_addr4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
serv_addr4.sin_family = AF_INET;
|
|
|
|
|
2019-08-18 10:35:43 +01:00
|
|
|
#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE)
|
2019-01-13 06:58:21 +08:00
|
|
|
if (iface) {
|
|
|
|
m = interface_to_sa(vhost, iface,
|
2020-12-12 06:21:40 +00:00
|
|
|
(struct sockaddr_in *)v, (unsigned int)n, 0);
|
2019-01-13 06:58:21 +08:00
|
|
|
if (m == LWS_ITOSA_NOT_USABLE) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "netif %s: Not usable",
|
|
|
|
iface);
|
2019-01-13 06:58:21 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
if (m == LWS_ITOSA_NOT_EXIST) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_info(wsi, "netif %s: Does not exist",
|
|
|
|
iface);
|
2019-01-13 06:58:21 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-12 06:21:40 +00:00
|
|
|
serv_addr4.sin_port = htons((uint16_t)(unsigned int)port);
|
2021-06-21 10:36:36 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
} /* switch */
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/* just checking for the interface extant */
|
|
|
|
if (sockfd == LWS_SOCK_INVALID)
|
|
|
|
return LWS_ITOSA_USABLE;
|
|
|
|
|
2021-02-01 11:48:04 +00:00
|
|
|
n = bind(sockfd, v, (socklen_t)n);
|
2019-01-13 06:58:21 +08:00
|
|
|
#ifdef LWS_WITH_UNIX_SOCK
|
2021-06-21 10:36:36 +01:00
|
|
|
if (n < 0 && af == AF_UNIX) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_err(wsi, "ERROR on binding fd %d to \"%s\" (%d %d)",
|
|
|
|
sockfd, iface, n, LWS_ERRNO);
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
return LWS_ITOSA_NOT_EXIST;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (n < 0) {
|
2019-12-12 18:01:36 +00:00
|
|
|
int _lws_errno = LWS_ERRNO;
|
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_err(wsi, "ERROR on binding fd %d to port %d (%d %d)",
|
|
|
|
sockfd, port, n, _lws_errno);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/* if something already listening, tell caller to fail permanently */
|
|
|
|
|
2019-12-12 18:01:36 +00:00
|
|
|
if (_lws_errno == LWS_EADDRINUSE)
|
2019-01-13 06:58:21 +08:00
|
|
|
return LWS_ITOSA_BUSY;
|
|
|
|
|
|
|
|
/* otherwise ask caller to retry later */
|
|
|
|
|
|
|
|
return LWS_ITOSA_NOT_EXIST;
|
|
|
|
}
|
|
|
|
|
2020-05-24 13:57:21 +01:00
|
|
|
#if defined(LWS_WITH_UNIX_SOCK) && !defined(WIN32)
|
2021-06-21 10:36:36 +01:00
|
|
|
if (af == AF_UNIX) {
|
2019-03-25 08:07:28 +08:00
|
|
|
uid_t uid = vhost->context->uid;
|
|
|
|
gid_t gid = vhost->context->gid;
|
|
|
|
|
|
|
|
if (vhost->unix_socket_perms) {
|
|
|
|
if (lws_plat_user_colon_group_to_ids(
|
|
|
|
vhost->unix_socket_perms, &uid, &gid)) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_err(wsi, "Failed to translate %s",
|
|
|
|
vhost->unix_socket_perms);
|
2019-03-25 08:07:28 +08:00
|
|
|
return LWS_ITOSA_NOT_EXIST;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 21:28:48 +01:00
|
|
|
if (iface && iface[0] != '@' && uid && gid) {
|
|
|
|
if (chown(iface, uid, gid)) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_err(wsi, "failed to set %s perms %u:%u",
|
|
|
|
iface, (unsigned int)uid,
|
|
|
|
(unsigned int)gid);
|
2019-03-25 08:07:28 +08:00
|
|
|
|
|
|
|
return LWS_ITOSA_NOT_EXIST;
|
|
|
|
}
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_notice(wsi, "vh %s unix skt %s perms %u:%u",
|
|
|
|
vhost->name, iface,
|
|
|
|
(unsigned int)uid,
|
|
|
|
(unsigned int)gid);
|
2019-03-25 08:07:28 +08:00
|
|
|
|
2020-05-14 21:28:48 +01:00
|
|
|
if (chmod(iface, 0660)) {
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_err(wsi, "0600 mode on %s fail", iface);
|
2019-03-25 08:07:28 +08:00
|
|
|
|
|
|
|
return LWS_ITOSA_NOT_EXIST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LWS_PLAT_OPTEE
|
2021-02-05 13:08:41 +00:00
|
|
|
if (getsockname(sockfd, (struct sockaddr *)psin, &len) == -1)
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_wsi_warn(wsi, "getsockname: %s", strerror(LWS_ERRNO));
|
2019-01-13 06:58:21 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
port = (sin.ss_family == AF_INET6) ?
|
2021-02-05 13:08:41 +00:00
|
|
|
ntohs(((struct sockaddr_in6 *)psin)->sin6_port) :
|
|
|
|
ntohs(((struct sockaddr_in *)psin)->sin_port);
|
2019-01-13 06:58:21 +08:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
struct sockaddr_in sain;
|
2021-02-05 13:08:41 +00:00
|
|
|
memcpy(&sain, psin, sizeof(sain));
|
2019-01-13 06:58:21 +08:00
|
|
|
port = ntohs(sain.sin_port);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-05 13:08:41 +00:00
|
|
|
{
|
|
|
|
char buf[72];
|
|
|
|
lws_sa46_write_numeric_address((lws_sockaddr46 *)psin,
|
|
|
|
buf, sizeof(buf));
|
|
|
|
|
2021-10-16 05:07:31 +01:00
|
|
|
lwsl_vhost_notice(vhost, "source ads %s", buf);
|
2021-02-05 13:08:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2020-09-17 12:43:31 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
|
2019-08-05 14:47:51 +01:00
|
|
|
unsigned int
|
|
|
|
lws_retry_get_delay_ms(struct lws_context *context,
|
2019-08-20 04:49:30 +01:00
|
|
|
const lws_retry_bo_t *retry, uint16_t *ctry,
|
|
|
|
char *conceal)
|
2019-08-05 14:47:51 +01:00
|
|
|
{
|
2019-08-20 04:49:30 +01:00
|
|
|
uint64_t ms = 3000, pc = 30; /* sane-ish defaults if no retry table */
|
2019-08-05 14:47:51 +01:00
|
|
|
uint16_t ra;
|
|
|
|
|
|
|
|
if (conceal)
|
|
|
|
*conceal = 0;
|
|
|
|
|
|
|
|
if (retry) {
|
2021-11-06 09:09:32 +00:00
|
|
|
if (retry->retry_ms_table_count) {
|
|
|
|
if (*ctry < retry->retry_ms_table_count)
|
|
|
|
ms = retry->retry_ms_table[*ctry];
|
|
|
|
else
|
|
|
|
ms = retry->retry_ms_table[
|
|
|
|
retry->retry_ms_table_count - 1];
|
|
|
|
}
|
2019-08-20 04:49:30 +01:00
|
|
|
|
|
|
|
/* if no percent given, use the default 30% */
|
|
|
|
if (retry->jitter_percent)
|
|
|
|
pc = retry->jitter_percent;
|
2019-08-05 14:47:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lws_get_random(context, &ra, sizeof(ra)) == sizeof(ra))
|
2019-08-20 04:49:30 +01:00
|
|
|
ms += ((ms * pc * ra) >> 16) / 100;
|
|
|
|
else
|
|
|
|
assert(0);
|
2019-08-05 14:47:51 +01:00
|
|
|
|
|
|
|
if (*ctry < 0xffff)
|
|
|
|
(*ctry)++;
|
|
|
|
|
|
|
|
if (retry && conceal)
|
|
|
|
*conceal = (int)*ctry <= retry->conceal_count;
|
|
|
|
|
2020-01-11 14:04:50 +00:00
|
|
|
return (unsigned int)ms;
|
2019-08-05 14:47:51 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 09:48:17 +01:00
|
|
|
int
|
|
|
|
lws_retry_sul_schedule(struct lws_context *context, int tid,
|
|
|
|
lws_sorted_usec_list_t *sul,
|
|
|
|
const lws_retry_bo_t *retry, sul_cb_t cb, uint16_t *ctry)
|
|
|
|
{
|
|
|
|
char conceal;
|
|
|
|
uint64_t ms = lws_retry_get_delay_ms(context, retry, ctry, &conceal);
|
|
|
|
|
|
|
|
if (!conceal)
|
|
|
|
return 1;
|
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
lwsl_cx_info(context, "sul %p: scheduling retry in %dms", sul, (int)ms);
|
2019-09-08 08:08:55 +01:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_sul_schedule(context, tid, sul, cb, (int64_t)(ms * 1000));
|
2019-09-19 09:48:17 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-08 08:08:55 +01:00
|
|
|
int
|
|
|
|
lws_retry_sul_schedule_retry_wsi(struct lws *wsi, lws_sorted_usec_list_t *sul,
|
|
|
|
sul_cb_t cb, uint16_t *ctry)
|
|
|
|
{
|
2020-09-17 12:43:31 +01:00
|
|
|
char conceal;
|
|
|
|
lws_usec_t us = lws_retry_get_delay_ms(wsi->a.context,
|
|
|
|
wsi->retry_policy, ctry,
|
|
|
|
&conceal) * LWS_US_PER_MS;
|
|
|
|
|
|
|
|
if (!conceal)
|
|
|
|
/* if our reties are up, they're up... */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
2020-11-18 06:34:16 +00:00
|
|
|
if (
|
|
|
|
#if defined(LWS_ROLE_H1)
|
|
|
|
wsi->role_ops == &role_ops_h1
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_ROLE_H1) && defined(LWS_ROLE_H2)
|
|
|
|
||
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_ROLE_H2)
|
|
|
|
wsi->role_ops == &role_ops_h2
|
|
|
|
#endif
|
|
|
|
)
|
2020-09-17 12:43:31 +01:00
|
|
|
/*
|
|
|
|
* Since we're doing it by wsi, we're in a position to check for
|
|
|
|
* http retry-after, it will increase us accordingly if found
|
|
|
|
*/
|
|
|
|
lws_http_check_retry_after(wsi, &us);
|
|
|
|
#endif
|
|
|
|
lws_sul_schedule(wsi->a.context, wsi->tsi, sul, cb, us);
|
|
|
|
|
|
|
|
return 0;
|
2019-09-08 08:08:55 +01:00
|
|
|
}
|
|
|
|
|
2020-09-17 12:43:31 +01:00
|
|
|
#endif
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
#if defined(LWS_WITH_IPV6)
|
2020-01-02 08:32:23 +00:00
|
|
|
unsigned long
|
2021-06-28 05:05:57 +01:00
|
|
|
lws_get_addr_scope(struct lws *wsi, const char *ifname_or_ipaddr)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
2020-06-08 05:27:11 +01:00
|
|
|
unsigned long scope;
|
2019-01-13 06:58:21 +08:00
|
|
|
char ip[NI_MAXHOST];
|
|
|
|
unsigned int i;
|
2020-06-08 05:27:11 +01:00
|
|
|
#if !defined(WIN32)
|
|
|
|
struct ifaddrs *addrs, *addr;
|
|
|
|
#else
|
|
|
|
PIP_ADAPTER_ADDRESSES adapter, addrs = NULL;
|
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS addr;
|
|
|
|
struct sockaddr_in6 *sockaddr;
|
|
|
|
ULONG size = 0;
|
|
|
|
int found = 0;
|
|
|
|
DWORD ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First see if we can look the string up as a network interface name...
|
|
|
|
* windows vista+ also has this
|
|
|
|
*/
|
|
|
|
|
|
|
|
scope = if_nametoindex(ifname_or_ipaddr);
|
|
|
|
if (scope > 0)
|
|
|
|
/* we found it from the interface name lookup */
|
|
|
|
return scope;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if not, try to look it up as an IP -> interface -> interface index
|
|
|
|
*/
|
|
|
|
|
|
|
|
scope = 0;
|
|
|
|
|
|
|
|
#if !defined(WIN32)
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
getifaddrs(&addrs);
|
|
|
|
for (addr = addrs; addr; addr = addr->ifa_next) {
|
|
|
|
if (!addr->ifa_addr ||
|
|
|
|
addr->ifa_addr->sa_family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
2020-06-08 05:27:11 +01:00
|
|
|
ip[0] = '\0';
|
2021-06-28 05:05:57 +01:00
|
|
|
getnameinfo(addr->ifa_addr, sizeof(struct sockaddr_in6),
|
|
|
|
ip, sizeof(ip), NULL, 0, NI_NUMERICHOST);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (ip[i])
|
|
|
|
if (ip[i++] == '%') {
|
|
|
|
ip[i - 1] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-08 05:27:11 +01:00
|
|
|
if (!strcmp(ip, ifname_or_ipaddr)) {
|
2019-01-13 06:58:21 +08:00
|
|
|
scope = if_nametoindex(addr->ifa_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeifaddrs(addrs);
|
|
|
|
#else
|
|
|
|
|
2021-06-28 05:05:57 +01:00
|
|
|
for (i = 0; i < 5; i++) {
|
2019-01-13 06:58:21 +08:00
|
|
|
ret = GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX,
|
|
|
|
NULL, addrs, &size);
|
2021-06-28 05:05:57 +01:00
|
|
|
if (ret == NO_ERROR || ret == ERROR_NO_DATA)
|
2019-01-13 06:58:21 +08:00
|
|
|
break;
|
2021-06-28 05:05:57 +01:00
|
|
|
|
|
|
|
if (addrs)
|
|
|
|
free(addrs);
|
|
|
|
|
|
|
|
if (ret != ERROR_BUFFER_OVERFLOW) {
|
|
|
|
addrs = NULL;
|
|
|
|
lwsl_wsi_err(wsi, "Get IPv6 ads table fail (%d)", ret);
|
2019-01-13 06:58:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-06-28 05:05:57 +01:00
|
|
|
|
|
|
|
addrs = (IP_ADAPTER_ADDRESSES *)malloc(size);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret == NO_ERROR) && (addrs)) {
|
|
|
|
adapter = addrs;
|
|
|
|
while (adapter && !found) {
|
|
|
|
addr = adapter->FirstUnicastAddress;
|
|
|
|
while (addr && !found) {
|
|
|
|
if (addr->Address.lpSockaddr->sa_family ==
|
|
|
|
AF_INET6) {
|
|
|
|
sockaddr = (struct sockaddr_in6 *)
|
|
|
|
(addr->Address.lpSockaddr);
|
|
|
|
|
|
|
|
lws_plat_inet_ntop(sockaddr->sin6_family,
|
|
|
|
&sockaddr->sin6_addr,
|
|
|
|
ip, sizeof(ip));
|
|
|
|
|
2020-06-08 05:27:11 +01:00
|
|
|
if (!strcmp(ip, ifname_or_ipaddr)) {
|
2019-01-13 06:58:21 +08:00
|
|
|
scope = sockaddr->sin6_scope_id;
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr = addr->Next;
|
|
|
|
}
|
|
|
|
adapter = adapter->Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (addrs)
|
|
|
|
free(addrs);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-15 11:04:16 +01:00
|
|
|
/*
|
|
|
|
* https://en.wikipedia.org/wiki/IPv6_address
|
|
|
|
*
|
|
|
|
* An IPv6 address is represented as eight groups of four hexadecimal digits,
|
|
|
|
* each group representing 16 bits (two octets, a group sometimes also called a
|
|
|
|
* hextet[6][7]). The groups are separated by colons (:). An example of an IPv6
|
|
|
|
* address is:
|
|
|
|
*
|
|
|
|
* 2001:0db8:85a3:0000:0000:8a2e:0370:7334
|
|
|
|
*
|
|
|
|
* The hexadecimal digits are case-insensitive, but IETF recommendations suggest
|
|
|
|
* the use of lower case letters. The full representation of eight 4-digit
|
|
|
|
* groups may be simplified by several techniques, eliminating parts of the
|
|
|
|
* representation.
|
|
|
|
*
|
|
|
|
* Leading zeroes in a group may be omitted, but each group must retain at least
|
|
|
|
* one hexadecimal digit.[1] Thus, the example address may be written as:
|
|
|
|
*
|
|
|
|
* 2001:db8:85a3:0:0:8a2e:370:7334
|
|
|
|
*
|
|
|
|
* One or more consecutive groups containing zeros only may be replaced with a
|
|
|
|
* single empty group, using two consecutive colons (::).[1] The substitution
|
|
|
|
* may only be applied once in the address, however, because multiple
|
|
|
|
* occurrences would create an ambiguous representation. Thus, the example
|
|
|
|
* address can be further simplified:
|
|
|
|
*
|
|
|
|
* 2001:db8:85a3::8a2e:370:7334
|
|
|
|
*
|
|
|
|
* The localhost (loopback) address, 0:0:0:0:0:0:0:1, and the IPv6 unspecified
|
|
|
|
* address, 0:0:0:0:0:0:0:0, are reduced to ::1 and ::, respectively.
|
|
|
|
*
|
|
|
|
* During the transition of the Internet from IPv4 to IPv6, it is typical to
|
|
|
|
* operate in a mixed addressing environment. For such use cases, a special
|
|
|
|
* notation has been introduced, which expresses IPv4-mapped and IPv4-compatible
|
|
|
|
* IPv6 addresses by writing the least-significant 32 bits of an address in the
|
|
|
|
* familiar IPv4 dot-decimal notation, whereas the other 96 (most significant)
|
|
|
|
* bits are written in IPv6 format. For example, the IPv4-mapped IPv6 address
|
|
|
|
* ::ffff:c000:0280 is written as ::ffff:192.0.2.128, thus expressing clearly
|
|
|
|
* the original IPv4 address that was mapped to IPv6.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_parse_numeric_address(const char *ads, uint8_t *result, size_t max_len)
|
|
|
|
{
|
|
|
|
struct lws_tokenize ts;
|
|
|
|
uint8_t *orig = result, temp[16];
|
2020-01-11 14:04:50 +00:00
|
|
|
int sects = 0, ipv6 = !!strchr(ads, ':'), skip_point = -1, dm = 0;
|
2019-09-15 11:04:16 +01:00
|
|
|
char t[5];
|
2020-01-11 14:04:50 +00:00
|
|
|
size_t n;
|
2019-09-15 11:04:16 +01:00
|
|
|
long u;
|
|
|
|
|
|
|
|
lws_tokenize_init(&ts, ads, LWS_TOKENIZE_F_NO_INTEGERS |
|
|
|
|
LWS_TOKENIZE_F_MINUS_NONTERM);
|
|
|
|
ts.len = strlen(ads);
|
|
|
|
if (!ipv6 && ts.len < 7)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ipv6 && ts.len < 2)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (!ipv6 && max_len < 4)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
if (ipv6 && max_len < 16)
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
if (ipv6)
|
|
|
|
memset(result, 0, max_len);
|
|
|
|
|
|
|
|
do {
|
2020-12-12 06:21:40 +00:00
|
|
|
ts.e = (int8_t)lws_tokenize(&ts);
|
2019-09-15 11:04:16 +01:00
|
|
|
switch (ts.e) {
|
|
|
|
case LWS_TOKZE_TOKEN:
|
|
|
|
dm = 0;
|
|
|
|
if (ipv6) {
|
|
|
|
if (ts.token_len > 4)
|
|
|
|
return -1;
|
|
|
|
memcpy(t, ts.token, ts.token_len);
|
|
|
|
t[ts.token_len] = '\0';
|
|
|
|
for (n = 0; n < ts.token_len; n++)
|
|
|
|
if (t[n] < '0' || t[n] > 'f' ||
|
|
|
|
(t[n] > '9' && t[n] < 'A') ||
|
|
|
|
(t[n] > 'F' && t[n] < 'a'))
|
|
|
|
return -1;
|
|
|
|
u = strtol(t, NULL, 16);
|
|
|
|
if (u > 0xffff)
|
|
|
|
return -5;
|
2020-01-11 14:04:50 +00:00
|
|
|
*result++ = (uint8_t)(u >> 8);
|
2019-09-15 11:04:16 +01:00
|
|
|
} else {
|
|
|
|
if (ts.token_len > 3)
|
|
|
|
return -1;
|
|
|
|
memcpy(t, ts.token, ts.token_len);
|
|
|
|
t[ts.token_len] = '\0';
|
|
|
|
for (n = 0; n < ts.token_len; n++)
|
|
|
|
if (t[n] < '0' || t[n] > '9')
|
|
|
|
return -1;
|
|
|
|
u = strtol(t, NULL, 10);
|
|
|
|
if (u > 0xff)
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
if (u < 0)
|
|
|
|
return -7;
|
|
|
|
*result++ = (uint8_t)u;
|
|
|
|
sects++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_TOKZE_DELIMITER:
|
|
|
|
if (dm++) {
|
|
|
|
if (dm > 2)
|
|
|
|
return -8;
|
|
|
|
if (*ts.token != ':')
|
|
|
|
return -9;
|
|
|
|
/* back to back : */
|
|
|
|
*result++ = 0;
|
|
|
|
*result++ = 0;
|
2020-01-11 14:04:50 +00:00
|
|
|
skip_point = lws_ptr_diff(result, orig);
|
2019-09-15 11:04:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ipv6 && orig[2] == 0xff && orig[3] == 0xff &&
|
|
|
|
skip_point == 2) {
|
|
|
|
/* ipv4 backwards compatible format */
|
|
|
|
ipv6 = 0;
|
|
|
|
memset(orig, 0, max_len);
|
|
|
|
orig[10] = 0xff;
|
|
|
|
orig[11] = 0xff;
|
|
|
|
skip_point = -1;
|
|
|
|
result = &orig[12];
|
|
|
|
sects = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ipv6 && *ts.token != ':')
|
|
|
|
return -10;
|
|
|
|
if (!ipv6 && *ts.token != '.')
|
|
|
|
return -11;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_TOKZE_ENDED:
|
|
|
|
if (!ipv6 && sects == 4)
|
2020-01-11 14:04:50 +00:00
|
|
|
return lws_ptr_diff(result, orig);
|
2019-09-15 11:04:16 +01:00
|
|
|
if (ipv6 && sects == 8)
|
2020-01-11 14:04:50 +00:00
|
|
|
return lws_ptr_diff(result, orig);
|
2019-09-15 11:04:16 +01:00
|
|
|
if (skip_point != -1) {
|
2020-01-11 14:04:50 +00:00
|
|
|
int ow = lws_ptr_diff(result, orig);
|
2019-09-15 11:04:16 +01:00
|
|
|
/*
|
|
|
|
* contains ...::...
|
|
|
|
*/
|
|
|
|
if (ow == 16)
|
|
|
|
return 16;
|
2020-12-12 06:21:40 +00:00
|
|
|
memcpy(temp, &orig[skip_point], (unsigned int)(ow - skip_point));
|
|
|
|
memset(&orig[skip_point], 0, (unsigned int)(16 - skip_point));
|
2019-09-15 11:04:16 +01:00
|
|
|
memcpy(&orig[16 - (ow - skip_point)], temp,
|
2020-12-12 06:21:40 +00:00
|
|
|
(unsigned int)(ow - skip_point));
|
2019-09-15 11:04:16 +01:00
|
|
|
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
return -12;
|
|
|
|
|
|
|
|
default: /* includes ENDED */
|
|
|
|
lwsl_err("%s: malformed ip address\n",
|
|
|
|
__func__);
|
|
|
|
|
|
|
|
return -13;
|
|
|
|
}
|
|
|
|
} while (ts.e > 0 && result - orig <= (int)max_len);
|
|
|
|
|
|
|
|
lwsl_err("%s: ended on e %d\n", __func__, ts.e);
|
|
|
|
|
|
|
|
return -14;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_sa46_parse_numeric_address(const char *ads, lws_sockaddr46 *sa46)
|
|
|
|
{
|
|
|
|
uint8_t a[16];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = lws_parse_numeric_address(ads, a, sizeof(a));
|
|
|
|
if (n < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
if (n == 16) {
|
|
|
|
sa46->sa6.sin6_family = AF_INET6;
|
|
|
|
memcpy(sa46->sa6.sin6_addr.s6_addr, a,
|
|
|
|
sizeof(sa46->sa6.sin6_addr.s6_addr));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (n != 4)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sa46->sa4.sin_family = AF_INET;
|
|
|
|
memcpy(&sa46->sa4.sin_addr.s_addr, a,
|
|
|
|
sizeof(sa46->sa4.sin_addr.s_addr));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-01-11 14:04:50 +00:00
|
|
|
lws_write_numeric_address(const uint8_t *ads, int size, char *buf, size_t len)
|
2019-09-15 11:04:16 +01:00
|
|
|
{
|
2020-12-12 06:21:40 +00:00
|
|
|
char c, elided = 0, soe = 0, zb = (char)-1, n, ipv4 = 0;
|
2019-09-15 11:04:16 +01:00
|
|
|
const char *e = buf + len;
|
|
|
|
char *obuf = buf;
|
|
|
|
int q = 0;
|
|
|
|
|
|
|
|
if (size == 4)
|
|
|
|
return lws_snprintf(buf, len, "%u.%u.%u.%u",
|
|
|
|
ads[0], ads[1], ads[2], ads[3]);
|
|
|
|
|
|
|
|
if (size != 16)
|
|
|
|
return -1;
|
|
|
|
|
2019-12-01 18:04:19 +00:00
|
|
|
for (c = 0; c < (char)size / 2; c++) {
|
2020-12-12 06:21:40 +00:00
|
|
|
uint16_t v = (uint16_t)((ads[q] << 8) | ads[q + 1]);
|
2019-09-15 11:04:16 +01:00
|
|
|
|
|
|
|
if (buf + 8 > e)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
q += 2;
|
|
|
|
if (soe) {
|
|
|
|
if (v)
|
|
|
|
*buf++ = ':';
|
|
|
|
/* fall thru to print hex value */
|
|
|
|
} else
|
|
|
|
if (!elided && !soe && !v) {
|
|
|
|
elided = soe = 1;
|
|
|
|
zb = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ipv4) {
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (char)lws_snprintf(buf, lws_ptr_diff_size_t(e, buf), "%u.%u",
|
2019-09-15 11:04:16 +01:00
|
|
|
ads[q - 2], ads[q - 1]);
|
|
|
|
buf += n;
|
|
|
|
if (c == 6)
|
|
|
|
*buf++ = '.';
|
|
|
|
} else {
|
|
|
|
if (soe && !v)
|
|
|
|
continue;
|
|
|
|
if (c)
|
|
|
|
*buf++ = ':';
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
buf += lws_snprintf(buf, lws_ptr_diff_size_t(e, buf), "%x", v);
|
2019-09-15 11:04:16 +01:00
|
|
|
|
|
|
|
if (soe && v) {
|
|
|
|
soe = 0;
|
|
|
|
if (c == 5 && v == 0xffff && !zb) {
|
|
|
|
ipv4 = 1;
|
|
|
|
*buf++ = ':';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (buf + 3 > e)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (soe) { /* as is the case for all zeros */
|
|
|
|
*buf++ = ':';
|
|
|
|
*buf++ = ':';
|
|
|
|
*buf = '\0';
|
|
|
|
}
|
|
|
|
|
2020-01-11 14:04:50 +00:00
|
|
|
return lws_ptr_diff(buf, obuf);
|
2019-09-15 11:04:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-01-11 14:04:50 +00:00
|
|
|
lws_sa46_write_numeric_address(lws_sockaddr46 *sa46, char *buf, size_t len)
|
2019-09-15 11:04:16 +01:00
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
if (sa46->sa4.sin_family == AF_INET6)
|
|
|
|
return lws_write_numeric_address(
|
|
|
|
(uint8_t *)&sa46->sa6.sin6_addr, 16, buf, len);
|
|
|
|
#endif
|
|
|
|
if (sa46->sa4.sin_family == AF_INET)
|
|
|
|
return lws_write_numeric_address(
|
|
|
|
(uint8_t *)&sa46->sa4.sin_addr, 4, buf, len);
|
|
|
|
|
2021-03-07 09:44:29 +00:00
|
|
|
#if defined(LWS_WITH_UNIX_SOCK)
|
|
|
|
if (sa46->sa4.sin_family == AF_UNIX)
|
|
|
|
return lws_snprintf(buf, len, "(unix skt)");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!sa46->sa4.sin_family)
|
|
|
|
return lws_snprintf(buf, len, "(unset)");
|
|
|
|
|
|
|
|
if (sa46->sa4.sin_family == AF_INET6)
|
2021-01-06 15:08:22 +00:00
|
|
|
return lws_snprintf(buf, len, "(ipv6 unsupp)");
|
2021-03-07 09:44:29 +00:00
|
|
|
|
|
|
|
lws_snprintf(buf, len, "(AF%d unsupp)", (int)sa46->sa4.sin_family);
|
2021-02-05 13:08:41 +00:00
|
|
|
|
2019-09-15 11:04:16 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-09-15 11:04:16 +01:00
|
|
|
int
|
|
|
|
lws_sa46_compare_ads(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46b)
|
|
|
|
{
|
|
|
|
if (sa46a->sa4.sin_family != sa46b->sa4.sin_family)
|
|
|
|
return 1;
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-09-15 11:04:16 +01:00
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
if (sa46a->sa4.sin_family == AF_INET6)
|
|
|
|
return memcmp(&sa46a->sa6.sin6_addr, &sa46b->sa6.sin6_addr, 16);
|
|
|
|
#endif
|
|
|
|
|
2020-10-05 08:21:28 +01:00
|
|
|
if (sa46a->sa4.sin_family == AF_INET)
|
|
|
|
return sa46a->sa4.sin_addr.s_addr != sa46b->sa4.sin_addr.s_addr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-19 19:36:38 +01:00
|
|
|
void
|
|
|
|
lws_4to6(uint8_t *v6addr, const uint8_t *v4addr)
|
|
|
|
{
|
|
|
|
v6addr[12] = v4addr[0];
|
|
|
|
v6addr[13] = v4addr[1];
|
|
|
|
v6addr[14] = v4addr[2];
|
|
|
|
v6addr[15] = v4addr[3];
|
2020-09-20 09:14:46 +01:00
|
|
|
|
|
|
|
memset(v6addr, 0, 10);
|
|
|
|
|
|
|
|
v6addr[10] = v6addr[11] = 0xff;
|
2020-09-19 19:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
void
|
|
|
|
lws_sa46_4to6(lws_sockaddr46 *sa46, const uint8_t *v4addr, uint16_t port)
|
|
|
|
{
|
|
|
|
sa46->sa4.sin_family = AF_INET6;
|
|
|
|
|
|
|
|
lws_4to6((uint8_t *)&sa46->sa6.sin6_addr.s6_addr[0], v4addr);
|
|
|
|
|
|
|
|
sa46->sa6.sin6_port = htons(port);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-05 08:21:28 +01:00
|
|
|
int
|
|
|
|
lws_sa46_on_net(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46_net,
|
|
|
|
int net_len)
|
|
|
|
{
|
|
|
|
uint8_t mask = 0xff, norm[16];
|
|
|
|
const uint8_t *p1, *p2;
|
|
|
|
|
|
|
|
if (sa46a->sa4.sin_family == AF_INET) {
|
|
|
|
p1 = (uint8_t *)&sa46a->sa4.sin_addr;
|
|
|
|
if (sa46_net->sa4.sin_family == AF_INET6) {
|
|
|
|
/* ip is v4, net is v6, promote ip to v6 */
|
2020-09-19 19:36:38 +01:00
|
|
|
|
|
|
|
lws_4to6(norm, p1);
|
2020-10-05 08:21:28 +01:00
|
|
|
p1 = norm;
|
|
|
|
}
|
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
} else
|
|
|
|
if (sa46a->sa4.sin_family == AF_INET6) {
|
|
|
|
p1 = (uint8_t *)&sa46a->sa6.sin6_addr;
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (sa46_net->sa4.sin_family == AF_INET) {
|
|
|
|
p2 = (uint8_t *)&sa46_net->sa4.sin_addr;
|
|
|
|
if (sa46a->sa4.sin_family == AF_INET6) {
|
|
|
|
/* ip is v6, net is v4, promote net to v6 */
|
2020-09-19 19:36:38 +01:00
|
|
|
|
|
|
|
lws_4to6(norm, p2);
|
2020-10-05 08:21:28 +01:00
|
|
|
p2 = norm;
|
|
|
|
/* because the mask length is for net v4 address */
|
|
|
|
net_len += 12 * 8;
|
|
|
|
}
|
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
} else
|
|
|
|
if (sa46a->sa4.sin_family == AF_INET6) {
|
|
|
|
p2 = (uint8_t *)&sa46_net->sa6.sin6_addr;
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
while (net_len > 0) {
|
|
|
|
if (net_len < 8)
|
2020-12-12 06:21:40 +00:00
|
|
|
mask = (uint8_t)(mask << (8 - net_len));
|
2020-10-05 08:21:28 +01:00
|
|
|
|
|
|
|
if (((*p1++) & mask) != ((*p2++) & mask))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
net_len -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_sa46_copy_address(lws_sockaddr46 *sa46a, const void *in, int af)
|
|
|
|
{
|
2020-12-12 06:21:40 +00:00
|
|
|
sa46a->sa4.sin_family = (sa_family_t)af;
|
2020-10-05 08:21:28 +01:00
|
|
|
|
|
|
|
if (af == AF_INET)
|
|
|
|
memcpy(&sa46a->sa4.sin_addr, in, 4);
|
|
|
|
#if defined(LWS_WITH_IPV6)
|
|
|
|
else if (af == AF_INET6)
|
|
|
|
memcpy(&sa46a->sa6.sin6_addr, in, sizeof(sa46a->sa6.sin6_addr));
|
|
|
|
#endif
|
2019-09-15 11:04:16 +01:00
|
|
|
}
|
2019-09-19 09:48:17 +01:00
|
|
|
|
2020-06-23 13:19:30 +01:00
|
|
|
#if defined(LWS_WITH_SYS_STATE)
|
2019-09-19 09:48:17 +01:00
|
|
|
lws_state_manager_t *
|
|
|
|
lws_system_get_state_manager(struct lws_context *context)
|
|
|
|
{
|
|
|
|
return &context->mgr_system;
|
|
|
|
}
|
2020-06-23 13:19:30 +01:00
|
|
|
#endif
|