Update Files

This commit is contained in:
2025-01-22 17:22:38 +01:00
parent 89b9349629
commit 4c5e729485
5132 changed files with 1195369 additions and 0 deletions

View File

@ -0,0 +1,66 @@
#
# libwebsockets - small server side websockets and web server implementation
#
# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
#
# 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:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# 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.
#
# The strategy is to only export to PARENT_SCOPE
#
# - changes to LIB_LIST
# - changes to SOURCES
# - includes via include_directories
#
# and keep everything else private
include_directories(. esp32)
list(APPEND SOURCES
plat/freertos/freertos-fds.c
plat/freertos/freertos-init.c
plat/freertos/freertos-misc.c
plat/freertos/freertos-pipe.c
plat/freertos/freertos-service.c
plat/freertos/freertos-sockets.c
misc/romfs.c)
if (LWS_ESP_PLATFORM AND LWS_WITH_DRIVERS)
list(APPEND SOURCES plat/freertos/esp32/drivers/settings-esp32.c
plat/freertos/esp32/drivers/spi-esp32.c)
if (LWS_WITH_NETWORK)
list(APPEND SOURCES plat/freertos/esp32/drivers/netdev/wifi-esp32.c)
endif()
endif()
if (LWS_WITH_FILE_OPS)
list(APPEND SOURCES plat/freertos/freertos-file.c)
endif()
if (LWS_WITH_SYS_ASYNC_DNS OR LWS_WITH_SYS_NTPCLIENT)
list(APPEND SOURCES plat/freertos/freertos-resolv.c)
endif()
if (LWS_ESP_PLATFORM AND LWS_WITH_OTA)
list(APPEND SOURCES plat/freertos/esp32/esp32-lws_ota.c)
endif()
#
# Keep explicit parent scope exports at end
#
exports_to_parent_scope()

View File

@ -0,0 +1,96 @@
/*
* esp32 / esp-idf gpio
*
* Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include <libwebsockets.h>
static void
lws_gpio_esp32_mode(_lws_plat_gpio_t gpio, int flags)
{
int mode, pup = GPIO_FLOATING;
switch (flags & (LWSGGPIO_FL_READ | LWSGGPIO_FL_WRITE)) {
default:
lwsl_err("%s: neither read nor write\n", __func__);
return;
case LWSGGPIO_FL_READ:
mode = GPIO_MODE_INPUT;
break;
case LWSGGPIO_FL_WRITE:
mode = GPIO_MODE_OUTPUT;
break;
case LWSGGPIO_FL_READ | LWSGGPIO_FL_WRITE:
mode = GPIO_MODE_INPUT_OUTPUT;
break;
}
switch (flags & (LWSGGPIO_FL_PULLUP | LWSGGPIO_FL_PULLDOWN)) {
default:
break;
case LWSGGPIO_FL_PULLUP:
pup = GPIO_PULLUP_ONLY;
break;
case LWSGGPIO_FL_PULLDOWN:
pup = GPIO_PULLDOWN_ONLY;
break;
case LWSGGPIO_FL_PULLUP | LWSGGPIO_FL_PULLDOWN:
pup = GPIO_PULLUP_PULLDOWN;
break;
}
gpio_reset_pin(gpio);
gpio_set_direction(gpio, mode);
gpio_set_pull_mode(gpio, pup);
gpio_set_level(gpio, flags & LWSGGPIO_FL_START_LOW ? 0 : 1);
}
static int
lws_gpio_esp32_read(_lws_plat_gpio_t gpio)
{
return gpio_get_level(gpio);
}
static void
lws_gpio_esp32_set(_lws_plat_gpio_t gpio, int val)
{
gpio_set_level(gpio, val);
}
static int
lws_gpio_esp32_irq_mode(_lws_plat_gpio_t gpio, lws_gpio_irq_t irq_type,
lws_gpio_irq_cb_t cb, void *arg)
{
if (gpio_set_intr_type(gpio, irq_type))
return 1;
if (cb)
return gpio_isr_handler_add(gpio, cb, arg);
return gpio_isr_handler_remove(gpio);
}
const lws_gpio_ops_t lws_gpio_plat = {
.mode = lws_gpio_esp32_mode,
.read = lws_gpio_esp32_read,
.set = lws_gpio_esp32_set,
.irq_mode = lws_gpio_esp32_irq_mode,
};

View File

@ -0,0 +1,25 @@
/*
* lws generic gpio - esp32 platform wrapper
*
* Written in 2010-2020 by Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
extern const lws_gpio_ops_t lws_gpio_plat;

View File

@ -0,0 +1,498 @@
/*
* libwebsockets - esp32 wifi -> lws_netdev_wifi
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
*
* These are the esp platform wifi-specific netdev pieces. Nothing else should
* know any esp-specific apis.
*
* Operations happen via the generic lws_detdev instantiation for the platform
* wifi device, which point in here for operations. We also set up native OS
* event hooks per device for wifi and IP stack events, and post them as lws_smd
* NETWORK events on the if in the "platform private" namespace. We then
* service the events in the lws event loop thread context, which may again
* generate lws_smd NETWORK events in the public namespace depending on what
* happened.
*
* Scan requests go through a sul to make sure we don't get "piling on" from
* scheduled, timed scans. Scan results go through the lws_smd "washing" and
* are actually parsed in lws thread context, where they are converted to lws
* netdev scan results and processed by generic code.
*/
#include "private-lib-core.h"
#include "esp_system.h"
#include "spi_flash_mmap.h"
#include "esp_wifi.h"
#include <nvs_flash.h>
#include <esp_netif.h>
/*
* lws_netdev_instance_t:
* lws_netdev_instance_wifi_t:
* lws_netdev_instance_wifi_esp32_t
*/
typedef struct lws_netdev_instance_wifi_esp32 {
lws_netdev_instance_wifi_t wnd;
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
wifi_config_t sta_config;
} lws_netdev_instance_wifi_esp32_t;
/*
static wifi_config_t config = {
.ap = {
.channel = 6,
.authmode = WIFI_AUTH_OPEN,
.max_connection = 1,
} };
*/
/*
* Platform-specific connect / associate
*/
int
lws_netdev_wifi_connect_plat(lws_netdev_instance_t *nd, const char *ssid,
const char *passphrase, uint8_t *bssid)
{
lws_netdev_instance_wifi_esp32_t *wnde32 =
(lws_netdev_instance_wifi_esp32_t *)nd;
wnde32->wnd.inst.ops->up(&wnde32->wnd.inst);
wnde32->wnd.flags |= LNDIW_MODE_STA;
esp_wifi_set_mode(WIFI_MODE_STA);
#if 0
/* we will do our own dhcp */
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
#endif
lws_strncpy((char *)wnde32->sta_config.sta.ssid, ssid,
sizeof(wnde32->sta_config.sta.ssid));
lws_strncpy((char *)wnde32->sta_config.sta.password, passphrase,
sizeof(wnde32->sta_config.sta.password));
esp_wifi_set_config(WIFI_IF_STA, &wnde32->sta_config);
esp_wifi_connect();
return 0;
}
/*
* This is called from the SMD / lws thread context, after we heard there were
* scan results on this netdev
*/
static void
lws_esp32_scan_update(lws_netdev_instance_wifi_t *wnd)
{
// lws_netdevs_t *netdevs = lws_netdevs_from_ndi(&wnd->inst);
wifi_ap_record_t ap_records[LWS_WIFI_MAX_SCAN_TRACK], *ar;
uint32_t now = lws_now_secs();
uint16_t count_ap_records;
int n;
count_ap_records = LWS_ARRAY_SIZE(ap_records);
if (esp_wifi_scan_get_ap_records(&count_ap_records, ap_records)) {
lwsl_err("%s: failed\n", __func__);
return;
}
if (!count_ap_records)
return;
if (wnd->state != LWSNDVWIFI_STATE_SCAN)
return;
/*
* ... let's collect the OS-specific scan results, and convert then to
* lws_netdev sorted by rssi. If we already have it in the scan list,
* keep it and keep a little ringbuffer of its rssi along with an
* averaging. If it's new, add it into the linked-list sorted by rssi.
*/
ar = &ap_records[0];
for (n = 0; n < count_ap_records; n++) {
lws_wifi_sta_t *w;
int m;
m = strlen((const char *)ar->ssid);
if (!m)
goto next;
/*
* We know this guy from before?
*/
w = lws_netdev_wifi_scan_find(wnd, (const char *)ar->ssid,
ar->bssid);
if (!w) {
w = lws_zalloc(sizeof(*w) + m + 1, __func__);
if (!w)
goto next;
w->ssid = (char *)&w[1];
memcpy(w->ssid, ar->ssid, m + 1);
w->ssid_len = m;
memcpy(w->bssid, ar->bssid, 6);
lws_dll2_add_sorted(&w->list, &wnd->scan,
lws_netdev_wifi_rssi_sort_compare);
}
if (w->rssi_count == LWS_ARRAY_SIZE(w->rssi))
w->rssi_avg -= w->rssi[w->rssi_next];
else
w->rssi_count++;
w->rssi[w->rssi_next] = ar->rssi;
w->rssi_avg += w->rssi[w->rssi_next++];
w->rssi_next = w->rssi_next & (LWS_ARRAY_SIZE(w->rssi) - 1);
w->ch = ar->primary;
w->authmode = ar->authmode;
w->last_seen = now;
next:
ar++;
}
/*
* We can do the rest of it using the generic scan list and credentials
*/
lws_netdev_wifi_scan_select(wnd);
}
static wifi_scan_config_t scan_config = {
.ssid = 0,
.bssid = 0,
.channel = 0,
.show_hidden = true
};
void
lws_netdev_wifi_scan_plat(lws_netdev_instance_t *nd)
{
lws_netdev_instance_wifi_t *wnd = (lws_netdev_instance_wifi_t *)nd;
if (esp_wifi_scan_start(&scan_config, false))
lwsl_err("%s: %s scan failed\n", __func__, wnd->inst.name);
}
/*
* Platform-private interface events turn up here after going through SMD and
* passed down by matching network interface name via generic lws_netdev. All
* that messing around gets us from an OS-specific thread with an event to back
* here in lws event loop thread context, with the same event bound to a the
* netdev it belongs to.
*/
int
lws_netdev_wifi_event_plat(struct lws_netdev_instance *nd, lws_usec_t timestamp,
void *buf, size_t len)
{
lws_netdev_instance_wifi_t *wnd = (lws_netdev_instance_wifi_t *)nd;
struct lws_context *ctx = netdev_instance_to_ctx(&wnd->inst);
size_t al;
/*
* netdev-private sync messages?
*/
if (!lws_json_simple_strcmp(buf, len, "\"type\":", "priv")) {
const char *ev = lws_json_simple_find(buf, len, "\"ev\":", &al);
if (!ev)
return 0;
lwsl_notice("%s: smd priv ev %.*s\n", __func__, (int)al, ev);
switch (atoi(ev)) {
case WIFI_EVENT_STA_START:
wnd->state = LWSNDVWIFI_STATE_INITIAL;
if (!lws_netdev_wifi_redo_last(wnd))
break;
/*
* if the "try last successful" one fails, start the
* scan by falling through
*/
case WIFI_EVENT_STA_DISCONNECTED:
lws_smd_msg_printf(ctx, LWSSMDCL_NETWORK,
"{\"type\":\"linkdown\","
"\"if\":\"%s\"}", wnd->inst.name);
wnd->state = LWSNDVWIFI_STATE_SCAN;
/*
* We do it via the sul so we don't get timed scans
* on top of each other
*/
lws_sul_schedule(ctx, 0, &wnd->sul_scan,
lws_netdev_wifi_scan, 1);
break;
case WIFI_EVENT_STA_CONNECTED:
lws_smd_msg_printf(ctx, LWSSMDCL_NETWORK,
"{\"type\":\"linkup\","
"\"if\":\"%s\"}", wnd->inst.name);
break;
case WIFI_EVENT_SCAN_DONE:
lws_esp32_scan_update(wnd);
break;
default:
return 0;
}
return 0;
}
return 0;
}
/*
* This is coming from a thread context unrelated to lws... the first order is
* to turn these into lws_smd events synchronized on lws thread, since we want
* to change correspsonding lws netdev object states without locking.
*/
static void
_event_handler_wifi(void *arg, esp_event_base_t event_base, int32_t event_id,
void *event_data)
{
lws_netdev_instance_wifi_t *wnd = (lws_netdev_instance_wifi_t *)arg;
struct lws_context *ctx = netdev_instance_to_ctx(&wnd->inst);
switch (event_id) {
case WIFI_EVENT_STA_START:
case WIFI_EVENT_STA_DISCONNECTED:
case WIFI_EVENT_SCAN_DONE:
case WIFI_EVENT_STA_CONNECTED:
/*
* These are events in the platform's private namespace,
* interpreted only by the lws_smd handler above, ** in the lws
* event thread context **. The point of this is to requeue the
* event in the lws thread context like a bottom-half.
*
* To save on registrations, the context's NETWORK smd
* participant passes messages to lws_netdev, who passes ones
* that have if matching the netdev name to that netdev's
* (*event) handler.
*
* The other handler may emit generic network state SMD events
* for other things to consume.
*/
lws_smd_msg_printf(ctx, LWSSMDCL_NETWORK,
"{\"type\":\"priv\",\"if\":\"%s\",\"ev\":%d}",
wnd->inst.name, (int)event_id);
break;
default:
return;
}
}
#if 0
static int
espip_to_sa46(lws_sockaddr46 *sa46, esp_ip_addr_t *eip)
{
memset(sa46, 0, sizeof(sa46));
switch (eip->type) {
case ESP_IPADDR_TYPE_V4:
sa46->sa4.sin_family = AF_INET;
memcpy(sa46->sa4.sin_addr, &eip->u_addr.ip4.addr, );
return;
case ESP_IPADDR_TYPE_V6:
}
}
#endif
/*
* This is coming from a thread context unrelated to lws
*/
static void
_event_handler_ip(void *arg, esp_event_base_t event_base, int32_t event_id,
void *event_data)
{
lws_netdev_instance_wifi_t *wnd = (lws_netdev_instance_wifi_t *)arg;
lws_netdevs_t *netdevs = lws_netdevs_from_ndi(&wnd->inst);
struct lws_context *ctx = lws_context_from_netdevs(netdevs);
if (event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *e = (ip_event_got_ip_t *)event_data;
char ip[16];
#if 0
tcpip_adapter_dns_info_t e32ip;
/*
* Since atm we get this via DHCP, presumably we can get ahold
* of related info set by the router
*/
if (tcpip_adapter_get_dns_info(TCPIP_ADAPTER_IF_STA,
TCPIP_ADAPTER_DNS_MAIN,
/* also _BACKUP, _FALLBACK */
&e32ip)) {
lwsl_err("%s: there's no dns server set\n", __func__);
e32ip.ip.u_addr.ipv4 = 0x08080808;
e32ip.ip.type = ESP_IPADDR_TYPE_V4;
}
netdevs->sa46_dns_resolver.
#endif
lws_write_numeric_address((void *)&e->ip_info.ip, 4, ip,
sizeof(ip));
lws_smd_msg_printf(ctx, LWSSMDCL_NETWORK,
"{\"type\":\"ipacq\",\"if\":\"%s\","
"\"ipv4\":\"%s\"}", wnd->inst.name, ip);
}
}
/*
* This is the platform (esp-idf) init for any kind of networking to be
* available at all
*/
int
lws_netdev_plat_init(void)
{
nvs_flash_init();
esp_netif_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
return 0;
}
/*
* This is the platform (esp-idf) init for any wifi to be available at all
*/
int
lws_netdev_plat_wifi_init(void)
{
wifi_init_config_t wic = WIFI_INIT_CONFIG_DEFAULT();
int n;
esp_netif_create_default_wifi_sta();
n = esp_wifi_init(&wic);
if (n) {
lwsl_err("%s: wifi init fail: %d\n", __func__, n);
return 1;
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
return 0;
}
struct lws_netdev_instance *
lws_netdev_wifi_create_plat(struct lws_context *ctx,
const lws_netdev_ops_t *ops,
const char *name, void *platinfo)
{
lws_netdev_instance_wifi_esp32_t *wnde32 = lws_zalloc(
sizeof(*wnde32), __func__);
if (!wnde32)
return NULL;
wnde32->wnd.inst.type = LWSNDTYP_WIFI;
lws_netdev_instance_create(&wnde32->wnd.inst, ctx, ops, name, platinfo);
return &wnde32->wnd.inst;
}
int
lws_netdev_wifi_configure_plat(struct lws_netdev_instance *nd,
lws_netdev_config_t *config)
{
return 0;
}
int
lws_netdev_wifi_up_plat(struct lws_netdev_instance *nd)
{
lws_netdev_instance_wifi_esp32_t *wnde32 =
(lws_netdev_instance_wifi_esp32_t *)nd;
struct lws_context *ctx = netdev_instance_to_ctx(&wnde32->wnd.inst);
if (wnde32->wnd.flags & LNDIW_UP)
return 0;
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP, _event_handler_ip, nd,
&wnde32->instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID, _event_handler_wifi, nd,
&wnde32->instance_any_id));
esp_wifi_start();
wnde32->wnd.flags |= LNDIW_UP;
lws_smd_msg_printf(ctx, LWSSMDCL_NETWORK,
"{\"type\":\"up\",\"if\":\"%s\"}",
wnde32->wnd.inst.name);
return 0;
}
int
lws_netdev_wifi_down_plat(struct lws_netdev_instance *nd)
{
lws_netdev_instance_wifi_esp32_t *wnde32 =
(lws_netdev_instance_wifi_esp32_t *)nd;
struct lws_context *ctx = netdev_instance_to_ctx(&wnde32->wnd.inst);
if (!(wnde32->wnd.flags & LNDIW_UP))
return 0;
lws_smd_msg_printf(ctx, LWSSMDCL_NETWORK,
"{\"type\":\"down\",\"if\":\"%s\"}",
wnde32->wnd.inst.name);
esp_wifi_stop();
esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
&wnde32->instance_got_ip);
esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID,
&wnde32->instance_any_id);
wnde32->wnd.flags &= ~LNDIW_UP;
return 0;
}
void
lws_netdev_wifi_destroy_plat(struct lws_netdev_instance **pnd)
{
lws_free(*pnd);
*pnd = NULL;
}

View File

@ -0,0 +1,95 @@
/*
* esp32 / esp-idf pwm
*
* Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include "soc/ledc_reg.h"
#include "driver/ledc.h"
#define _LEDC_HIGH_SPEED_MODE 0
static const ledc_timer_config_t tc = {
#if defined(CONFIG_IDF_TARGET_ESP32S2)
.speed_mode = LEDC_LOW_SPEED_MODE,
#else
.speed_mode = _LEDC_HIGH_SPEED_MODE,
#endif
.duty_resolution = LEDC_TIMER_13_BIT,
.timer_num = LEDC_TIMER_0,
#if defined(CONFIG_IDF_TARGET_ESP32S2)
.freq_hz = 1000,
#else
.freq_hz = 5000,
#endif
.clk_cfg = LEDC_AUTO_CLK
};
int
lws_pwm_plat_init(const struct lws_pwm_ops *lo)
{
ledc_channel_config_t lc = {
#if defined(CONFIG_IDF_TARGET_ESP32S2)
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty = 8191,
#else
.speed_mode = _LEDC_HIGH_SPEED_MODE,
.duty = 8191,
#endif
.intr_type = LEDC_INTR_FADE_END,
.timer_sel = LEDC_TIMER_0,
};
size_t n;
ledc_timer_config(&tc);
for (n = 0; n < lo->count_pwm_map; n++) {
lc.channel = LEDC_CHANNEL_0 + lo->pwm_map[n].index;
lc.gpio_num = lo->pwm_map[n].gpio;
ledc_channel_config(&lc);
ledc_set_duty(_LEDC_HIGH_SPEED_MODE, lc.channel, 0);
ledc_update_duty(_LEDC_HIGH_SPEED_MODE, lc.channel);
}
return 0;
}
void
lws_pwm_plat_intensity(const struct lws_pwm_ops *lo, _lws_plat_gpio_t gpio,
lws_led_intensity_t inten)
{
size_t n;
for (n = 0; n < lo->count_pwm_map; n++) {
if (lo->pwm_map[n].gpio == gpio) {
if (!lo->pwm_map[n].active_level)
inten = 65535 - inten;
ledc_set_duty(_LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0 +
lo->pwm_map[n].index, inten >> 3);
ledc_update_duty(_LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0 +
lo->pwm_map[n].index);
return;
}
}
lwsl_err("%s: unknown gpio for pwm\n", __func__);
}

View File

@ -0,0 +1,75 @@
/*
* esp32 / esp-idf NV settings shim
*
* Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include <private-lib-core.h>
#include <nvs_flash.h>
int
lws_settings_plat_get(lws_settings_instance_t *si, const char *name,
uint8_t *dest, size_t *max_actual)
{
int n;
n = nvs_flash_init_partition((const char *)si->opaque_plat);
lwsl_notice("%s: init partition %d\n", __func__, n);
if (n == ESP_ERR_NOT_FOUND)
return 1;
if (nvs_open_from_partition((const char *)si->opaque_plat,
"_lws_settings", NVS_READONLY,
(nvs_handle_t *)&si->handle_plat))
return 1;
n = nvs_get_blob((nvs_handle_t)si->handle_plat,
name, dest, max_actual);
nvs_close((nvs_handle_t)si->handle_plat);
return !!n;
}
int
lws_settings_plat_set(lws_settings_instance_t *si, const char *name,
const uint8_t *src, size_t len)
{
int n = nvs_flash_init_partition((const char *)si->opaque_plat);
lwsl_notice("%s: init partition %d\n", __func__, n);
if (n == ESP_ERR_NOT_FOUND)
return 1;
if (nvs_open_from_partition((const char *)si->opaque_plat,
"_lws_settings", NVS_READWRITE,
(nvs_handle_t *)&si->handle_plat))
return 1;
n = nvs_set_blob((nvs_handle_t)si->handle_plat, name, src, len);
nvs_commit((nvs_handle_t)si->handle_plat);
nvs_close((nvs_handle_t)si->handle_plat);
return 0;
}

View File

@ -0,0 +1,297 @@
/*
* esp32 / esp-idf SPI
*
* Copyright (C) 2019 - 2022 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include <libwebsockets.h>
#include <driver/spi_master.h>
#include <esp_heap_caps.h>
typedef struct {
uint32_t *dma_stash;
size_t dma_stash_len;
spi_transaction_t esp_txn;
} lws_spi_async_txn_t;
static spi_device_handle_t sdh[4][4]; /* [unit][cs index] */
static volatile lws_spi_async_txn_t sat[7];
void *
lws_esp32_spi_alloc_dma(const struct lws_spi_ops *ctx, size_t size)
{
return heap_caps_malloc(size, MALLOC_CAP_32BIT | MALLOC_CAP_DMA);
}
void
lws_esp32_spi_free_dma(const struct lws_spi_ops *ctx, void **p)
{
if (*p) {
heap_caps_free(*p);
*p = NULL;
}
}
static void IRAM_ATTR
lcd_spi_pre_transfer_callback(spi_transaction_t *t)
{
int n = (int)(intptr_t)((volatile spi_transaction_t *)t)->user;
gpio_set_level((n >> 8) & 0xff, n & 1);
}
static void IRAM_ATTR
lcd_spi_post_transfer_callback(spi_transaction_t *t)
{
((volatile spi_transaction_t *)t)->user = NULL;
}
static lws_spi_async_txn_t *
find_idle_sat(void)
{
size_t n = 0;
for (n = 0; n < LWS_ARRAY_SIZE(sat); n++)
if (!sat[n].esp_txn.user) {
memset((void *)&sat[n].esp_txn, 0, sizeof(sat[0].esp_txn));
return (lws_spi_async_txn_t *)&sat[n];
}
return NULL;
}
int
lws_esp32_spi_in_flight(const struct lws_spi_ops *ctx)
{
size_t n = 0;
int inf = 0;
for (n = 0; n < LWS_ARRAY_SIZE(sat); n++)
if (sat[n].esp_txn.user)
inf++;
return inf;
}
int
lws_esp32_spi_init(const lws_spi_ops_t *spi_ops)
{
lws_bb_spi_t *bb = lws_container_of(spi_ops, lws_bb_spi_t, bb_ops);
spi_bus_config_t bc;
/* This inits the specified SPI BUS */
memset(&bc, 0, sizeof(bc));
bc.mosi_io_num = -1; // bb->mosi;
bc.miso_io_num = -1; // bb->miso;
bc.sclk_io_num = bb->clk;
bc.data0_io_num = bb->mosi;
bc.data1_io_num = -1;
bc.data2_io_num = -1;
bc.data3_io_num = -1;
bc.data4_io_num = -1;
bc.data5_io_num = -1;
bc.data6_io_num = -1;
bc.data7_io_num = -1;
bc.quadwp_io_num = -1;
bc.quadhd_io_num = -1;
bc.flags = SPICOMMON_BUSFLAG_MASTER;
if (spi_bus_initialize(bb->unit, &bc, SPI_DMA_CH_AUTO) != ESP_OK) {
lwsl_err("%s: SPI init failed\n", __func__);
return 1;
}
memset((void *)&sat, 0, sizeof(sat));
return 0;
}
int
lws_esp32_spi_queue(const lws_spi_ops_t *spi_ops, const lws_spi_desc_t *desc)
{
lws_bb_spi_t *bb = lws_container_of(spi_ops, lws_bb_spi_t, bb_ops);
spi_device_handle_t h = sdh[bb->unit][desc->channel];
uint8_t *d = (uint8_t *)desc->data;
size_t cw = desc->count_write;
// spi_transaction_t *ett;
esp_err_t e;
if (!h) {
spi_device_interface_config_t edic;
/* We need to create the device at these coordinates */
memset(&edic, 0, sizeof(edic));
edic.mode = spi_ops->bus_mode;
edic.clock_speed_hz = spi_ops->spi_clk_hz ?
spi_ops->spi_clk_hz : 16000000;
edic.input_delay_ns = 50;
edic.spics_io_num = bb->ncs[desc->channel];
edic.queue_size = 7;
edic.pre_cb = lcd_spi_pre_transfer_callback;
edic.post_cb = lcd_spi_post_transfer_callback;
edic.flags = SPI_DEVICE_NO_DUMMY;
/* we do these manually in callbacks */
bb->gpio->mode(bb->ncmd[desc->channel], LWSGGPIO_FL_WRITE);
bb->gpio->mode(bb->ncs[desc->channel], LWSGGPIO_FL_WRITE);
e = spi_bus_add_device(bb->unit, &edic, &h);
if (e != ESP_OK) {
lwsl_err("%s: failed to add device: 0x%x\n", __func__, e);
return 1;
}
sdh[bb->unit][desc->channel] = h;
}
if (desc->count_cmd) {
lws_spi_async_txn_t *at = NULL;
while (!at)
at = find_idle_sat();
if (at->dma_stash && at->dma_stash_len != 4) {
/* we lazily free these to avoid heap apis in IRQ ctx */
lws_esp32_spi_free_dma(NULL, (void **)&at->dma_stash);
at->dma_stash_len = 0;
}
if (at->dma_stash_len != 4) {
at->dma_stash = lws_esp32_spi_alloc_dma(NULL, 4);
if (!at->dma_stash) {
lwsl_err("%s: OOM getting DMA bounce\n", __func__);
return -1;
}
at->dma_stash_len = 4;
}
at->esp_txn.tx_buffer = at->dma_stash;
{
uint32_t u = 0;
size_t i;
for (i = 0; i < desc->count_cmd; i++) {
((uint8_t *)&u)[i & 3] = desc->src[i];
((uint32_t *)at->esp_txn.tx_buffer)[i >> 2] = u;
}
}
at->esp_txn.flags = 0;
at->esp_txn.length = desc->count_cmd * 8;
at->esp_txn.rx_buffer = NULL;
at->esp_txn.rxlength = 0;
at->esp_txn.user = (void *)((bb->ncs[desc->channel] << 16) |
(bb->ncmd[desc->channel] << 8) |
!!(desc->flags & LWS_SPI_FLAG_DC_CMD_IS_HIGH));
e = spi_device_queue_trans(h, &at->esp_txn, 50);
if (e != ESP_OK) {
lwsl_err("%s: failed to queue cmd trans: 0x%x\n",
__func__, e);
return 1;
}
#if 0
ett = &at->esp_txn;
e = spi_device_get_trans_result(h, &ett, 50);
if (e != ESP_OK) {
lwsl_err("%s: failed to get trans result\n", __func__);
return 1;
}
#endif
}
while (cw) {
size_t chunk = cw < 4000 ? cw : 4000;
lws_spi_async_txn_t *at = NULL;
while (!at)
at = find_idle_sat();
if (at->dma_stash && at->dma_stash_len != chunk) {
/* we lazily free these to avoid heap apis in IRQ ctx */
lws_esp32_spi_free_dma(NULL, (void **)&at->dma_stash);
at->dma_stash_len = 0;
}
if (at->dma_stash_len != chunk &&
!(desc->flags & LWS_SPI_FLAG_DMA_BOUNCE_NOT_NEEDED)) {
/* allocate a bounce buffer and fill it */
at->dma_stash = lws_esp32_spi_alloc_dma(NULL, chunk);
if (!at->dma_stash) {
lwsl_err("%s: OOM getting DMA bounce\n", __func__);
return -1;
}
at->dma_stash_len = chunk;
}
if (desc->flags & LWS_SPI_FLAG_DMA_BOUNCE_NOT_NEEDED) {
at->esp_txn.tx_buffer = d;
d += chunk;
} else {
uint32_t u = 0;
size_t i;
at->esp_txn.tx_buffer = at->dma_stash;
for (i = 0; i < chunk; i++) {
((uint8_t *)&u)[i & 3] = *d++;
((uint32_t *)at->esp_txn.tx_buffer)[i >> 2] = u;
}
}
at->esp_txn.rx_buffer = NULL;
at->esp_txn.rxlength = 0;
at->esp_txn.length = chunk * 8;
at->esp_txn.user = (void *)((bb->ncs[desc->channel] << 16) |
(bb->ncmd[desc->channel] << 8) |
!(desc->flags & LWS_SPI_FLAG_DC_CMD_IS_HIGH));
at->esp_txn.flags = 0;
e = spi_device_queue_trans(h, &at->esp_txn, 50);
if (e != ESP_OK) {
lwsl_err("%s: failed to queue data trans\n", __func__);
assert(0);
return 1;
}
#if 0
ett = &at->esp_txn;
e = spi_device_get_trans_result(h, &ett, 50);
if (e != ESP_OK) {
lwsl_err("%s: failed to get trans result\n", __func__);
return 1;
}
#endif
cw -= chunk;
}
return 0;
}

View File

@ -0,0 +1,201 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2022 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
* lws_ota platform implementation for esp-idf
*
* The whole platform OTA implementation runs in its own task context, which
* is created in ota_start() and taken down in ota_finalize(). Async
* completions are passed back to the main code by lws_cancel_service().
*/
#include "private-lib-core.h"
#include "esp_ota_ops.h"
extern lws_settings_instance_t *si;
/*
* Our platform-specific single OTA process object, it knows the esp-idf OTA
* handle too after ota_start succeeds.
*/
typedef struct {
lws_ota_t *g;
esp_ota_handle_t ota; /* opaque platform ota handle */
TaskHandle_t th;
SemaphoreHandle_t sem;
const esp_partition_t *ep;
} _lws_ota_process_t;
static _lws_ota_process_t pop;
static void
ota_task(void *_g)
{
lws_ota_t *g = (lws_ota_t *)_g;
esp_err_t e;
uint32_t no;
while (1) {
xTaskNotifyWaitIndexed(0, 0, ULONG_MAX, &no, portMAX_DELAY);
/* something to do */
g->async_r = LWSOTARET_ONGOING;
switch (no) {
case LWS_OTA_ASYNC_START:
pop.ep = esp_ota_get_next_update_partition(NULL);
g->async_r = LWSOTARET_NOSLOT;
if (pop.ep) {
e = esp_ota_begin(pop.ep, g->expected_size,
&pop.ota);
if (e == ESP_OK)
g->async_r = LWSOTARET_OK;
else
printf("esp_ota_begin: %d\n", (int)e);
} else
lwsl_err("%s: no next update part\n", __func__);
g->async_completed = 1;
lws_cancel_service(g->cx);
break;
case LWS_OTA_ASYNC_WRITE:
/*
* g->flow has compressed data we can use when we
* need it
*/
g->async_r = LWSOTARET_FAILED;
e = esp_ota_write(pop.ota, g->buf, g->buf_len);
if (e == ESP_OK)
g->async_r = LWSOTARET_OK;
else
lwsl_cx_err(g->cx, "esp_ota_write: %d", (int)e);
g->async_completed = 1;
lws_cancel_service(g->cx);
break;
case LWS_OTA_ASYNC_ABORT:
case LWS_OTA_ASYNC_FINALIZE:
g->async_r = LWSOTARET_FAILED;
if (no == LWS_OTA_ASYNC_ABORT)
e = esp_ota_abort(pop.ota);
else {
e = esp_ota_end(pop.ota);
if (e == ESP_OK) {
struct timeval tv;
/*
* Mark that we want to boot into the
* updated firmware that we just
* installed
*/
e = esp_ota_set_boot_partition(pop.ep);
/*
* Set the latest fw unixtime to the new
* guy. Set the time we updated.
*/
lws_settings_plat_printf(si,
"ota.fw_unixtime", "%llu",
(unsigned long long)g->unixtime);
if (!gettimeofday(&tv, NULL))
lws_settings_plat_printf(si,
"ota.upd_unixtime", "%llu",
(unsigned long long)tv.tv_sec);
}
}
if (e == ESP_OK)
g->async_r = LWSOTARET_OK;
else
lwsl_cx_err(g->cx, "esp_ota_end: %d", (int)e);
g->async_completed = 1;
lws_cancel_service(g->cx);
pop.th = NULL;
vTaskDelete(0);
return;
}
}
}
void
lws_plat_ota_queue(lws_ota_t *g, lws_ota_async_t a)
{
g->async_last = a;
xTaskNotify(pop.th, a, eSetValueWithOverwrite);
}
int
lws_plat_ota_start(lws_ota_t *g)
{
g->op = (lws_ota_process_t)&pop;
xTaskCreate(ota_task, "ota", 3072, g, tskIDLE_PRIORITY, &pop.th);
if (!pop.th)
return 1;
lws_plat_ota_queue(g, LWS_OTA_ASYNC_START);
return 0;
}
int
lws_plat_ota_report_current(lws_ota_t *g, int bad)
{
if (bad)
esp_ota_mark_app_invalid_rollback_and_reboot();
else
esp_ota_mark_app_valid_cancel_rollback();
return LWSOTARET_OK;
}
int
lws_plat_ota_get_last_fw_unixtime(uint64_t *fw_unixtime)
{
uint8_t buf[20];
size_t l = sizeof(buf);
if (lws_settings_plat_get(si, "ota.fw_unixtime", buf, &l)) {
lwsl_notice("%s: not in settings\n", __func__);
return 1;
}
*fw_unixtime = atoll((const char *)buf);
return 0;
}

View File

@ -0,0 +1,61 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
void
lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
pt->fds[pt->fds_count++].revents = 0;
}
void
lws_plat_delete_socket_from_fds(struct lws_context *context,
struct lws *wsi, int m)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
pt->fds_count--;
}
int
lws_plat_change_pollfd(struct lws_context *context,
struct lws *wsi, struct lws_pollfd *pfd)
{
return 0;
}
int
insert_wsi(const struct lws_context *context, struct lws *wsi)
{
assert(context->lws_lookup[wsi->desc.sockfd -
lws_plat_socket_offset()] == 0);
context->lws_lookup[wsi->desc.sockfd - \
lws_plat_socket_offset()] = wsi;
return 0;
}

View File

@ -0,0 +1,227 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
int lws_plat_apply_FD_CLOEXEC(int n)
{
return 0;
}
lws_fop_fd_t IRAM_ATTR
_lws_plat_file_open(const struct lws_plat_file_ops *fops_own,
const struct lws_plat_file_ops *fops, const char *filename,
const char *vpath, lws_fop_flags_t *flags)
{
struct stat stat_buf;
lws_fop_fd_t fop_fd;
int ret = open(filename, *flags, 0664);
if (ret < 0)
return NULL;
if (fstat(ret, &stat_buf) < 0)
goto bail;
fop_fd = lws_malloc(sizeof(*fop_fd), "fops open");
if (!fop_fd)
goto bail;
fop_fd->fops = fops;
fop_fd->fd = ret;
fop_fd->flags = *flags;
fop_fd->filesystem_priv = NULL; /* we don't use it */
fop_fd->pos = 0;
fop_fd->len = stat_buf.st_size;
return fop_fd;
bail:
close(ret);
return NULL;
}
int IRAM_ATTR
_lws_plat_file_close(lws_fop_fd_t *fops_fd)
{
int fd = (*fops_fd)->fd;
lws_free(*fops_fd);
*fops_fd = NULL;
return close(fd);
}
lws_fileofs_t IRAM_ATTR
_lws_plat_file_seek_cur(lws_fop_fd_t fops_fd, lws_fileofs_t offset)
{
return lseek(fops_fd->fd, offset, SEEK_CUR);
}
int IRAM_ATTR
_lws_plat_file_read(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
long n;
n = read(fops_fd->fd, buf, len);
if (n == -1) {
*amount = 0;
return -1;
}
fops_fd->pos += n;
*amount = n;
return 0;
}
int IRAM_ATTR
_lws_plat_file_write(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
long n;
n = write(fops_fd->fd, buf, len);
if (n == -1) {
*amount = 0;
return -1;
}
fops_fd->pos += n;
*amount = n;
return 0;
}
#if defined(LWS_AMAZON_RTOS)
int
lws_find_string_in_file(const char *filename, const char *string, int stringlen)
{
return 0;
}
#else
int
lws_find_string_in_file(const char *filename, const char *string, int stringlen)
{
nvs_handle nvh;
size_t s;
int n;
char buf[64], result[64];
const char *p = strchr(string, ':'), *q;
if (!p)
return 0;
q = string;
n = 0;
while ((size_t)n < sizeof(buf) - 1 && q != p)
buf[n++] = *q++;
buf[n] = '\0';
ESP_ERROR_CHECK(nvs_open(filename, NVS_READWRITE, &nvh));
s = sizeof(result) - 1;
n = nvs_get_str(nvh, buf, result, &s);
nvs_close(nvh);
if (n != ESP_OK)
return 0;
return !strcmp(p + 1, result);
}
#endif
#if !defined(LWS_AMAZON_RTOS)
int
lws_plat_write_file(const char *filename, void *buf, size_t len)
{
nvs_handle nvh;
int n;
if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
lwsl_notice("%s: failed to open nvs\n", __func__);
return -1;
}
n = nvs_set_blob(nvh, filename, buf, len);
if (n >= 0)
nvs_commit(nvh);
nvs_close(nvh);
lwsl_notice("%s: wrote %s (%d)\n", __func__, filename, n);
return n;
}
/* we write vhostname.cert.pem and vhostname.key.pem, 0 return means OK */
int
lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
size_t len)
{
const char *name = vhost->tls.alloc_cert_path;
if (is_key)
name = vhost->tls.key_path;
return lws_plat_write_file(name, buf, len) < 0;
}
int
lws_plat_read_file(const char *filename, void *buf, size_t len)
{
nvs_handle nvh;
size_t s = 0;
int n = 0;
if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
lwsl_notice("%s: failed to open nvs\n", __func__);
return 1;
}
ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
if (nvs_get_blob(nvh, filename, NULL, &s) != ESP_OK)
goto bail;
if (s > len)
goto bail;
n = nvs_get_blob(nvh, filename, buf, &s);
nvs_close(nvh);
lwsl_notice("%s: read %s (%d)\n", __func__, filename, (int)s);
if (n)
return -1;
return (int)s;
bail:
nvs_close(nvh);
return -1;
}
#endif /* LWS_AMAZON_RTOS */

View File

@ -0,0 +1,119 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
int
lws_plat_context_early_init(void)
{
return 0;
}
void
lws_plat_context_early_destroy(struct lws_context *context)
{
#if defined(LWS_AMAZON_RTOS) && defined(LWS_WITH_MBEDTLS)
mbedtls_ctr_drbg_free(&context->mcdc);
mbedtls_entropy_free(&context->mec);
#endif
}
void
lws_plat_context_late_destroy(struct lws_context *context)
{
#ifdef LWS_WITH_PLUGINS
if (context->plugin_list)
lws_plat_plugins_destroy(context);
#endif
if (context->lws_lookup)
lws_free(context->lws_lookup);
}
#if defined(LWS_WITH_HTTP2)
/*
* These are the default SETTINGS used on this platform. The user
* can selectively modify them for a vhost during vhost creation.
*/
const struct http2_settings lws_h2_defaults_esp32 = { {
1,
/* H2SET_HEADER_TABLE_SIZE */ 512,
/* H2SET_ENABLE_PUSH */ 0,
/* H2SET_MAX_CONCURRENT_STREAMS */ 16,
/* H2SET_INITIAL_WINDOW_SIZE */ 0,
/* H2SET_MAX_FRAME_SIZE */ 16384,
/* H2SET_MAX_HEADER_LIST_SIZE */ 512,
/* H2SET_RESERVED7 */ 0,
/* H2SET_ENABLE_CONNECT_PROTOCOL */ 1,
}};
#endif
int
lws_plat_init(struct lws_context *context,
const struct lws_context_creation_info *info)
{
#if defined(LWS_AMAZON_RTOS) && defined(LWS_WITH_MBEDTLS)
int n;
/* initialize platform random through mbedtls */
mbedtls_entropy_init(&context->mec);
mbedtls_ctr_drbg_init(&context->mcdc);
n = mbedtls_ctr_drbg_seed(&context->mcdc, mbedtls_entropy_func,
&context->mec, NULL, 0);
if (n) {
lwsl_err("%s: mbedtls_ctr_drbg_seed() returned 0x%x\n",
__func__, n);
return 1;
}
#endif
/* context has the global fd lookup array */
context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
context->max_fds, "esp32 lws_lookup");
if (context->lws_lookup == NULL) {
lwsl_err("OOM on lws_lookup array for %d connections\n",
context->max_fds);
return 1;
}
lwsl_notice(" mem: platform fd map: %5lu bytes\n",
(unsigned long)(sizeof(struct lws *) * context->max_fds));
#ifdef LWS_WITH_PLUGINS
if (info->plugin_dirs)
lws_plat_plugins_init(context, info->plugin_dirs);
#endif
#if defined(LWS_WITH_HTTP2)
/* override settings */
context->set = lws_h2_defaults_esp32;
#endif
#if defined(LWS_ESP_PLATFORM)
gpio_install_isr_service(0);
#endif
return 0;
}

View File

@ -0,0 +1,104 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
/*
* Normally you don't want this, use lws_sul instead inside the event loop.
* But sometimes for drivers it makes sense, so there's an internal-only
* crossplatform api for it.
*/
void
lws_msleep(unsigned int ms)
{
vTaskDelay(portTICK_PERIOD_MS > ms ? 1 : ms / portTICK_PERIOD_MS);
}
lws_usec_t
lws_now_usecs(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
}
size_t
lws_get_random(struct lws_context *context, void *buf, size_t len)
{
#if defined(LWS_WITH_ESP32)
uint8_t *pb = buf;
while (len) {
uint32_t r = esp_random();
uint8_t *p = (uint8_t *)&r;
int b = 4;
if (len < (size_t)b)
b = len;
len -= b;
while (b--)
*pb++ = p[b];
}
return pb - (uint8_t *)buf;
#else
#if defined(LWS_WITH_MBEDTLS)
int n;
n = mbedtls_ctr_drbg_random(&context->mcdc, buf, len);
if (!n)
return len;
/* failed */
lwsl_err("%s: mbedtls_ctr_drbg_random returned 0x%x\n", __func__, n);
#endif
return 0;
#endif
}
void lwsl_emit_syslog(int level, const char *line)
{
lwsl_emit_stderr(level, line);
}
int
lws_plat_drop_app_privileges(struct lws_context *context, int actually_init)
{
return 0;
}
int
lws_plat_recommended_rsa_bits(void)
{
/*
* 2048-bit key generation takes up to a minute on ESP32, 4096
* is like 15 minutes +
*/
return 2048;
}

View File

@ -0,0 +1,136 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include <lwip/sockets.h>
int
lws_plat_pipe_create(struct lws *wsi)
{
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
struct sockaddr_in *si = &wsi->a.context->frt_pipe_si;
lws_sockfd_type *fd = pt->dummy_pipe_fds;
socklen_t sl;
/*
* There's no pipe abstraction on lwip / freertos... use a UDP socket
* listening on 127.0.0.1:xxxx and send a byte to it from a second UDP
* socket to cancel the wait.
*
* Set the port to 0 at the bind, so lwip will choose a free one in the
* ephemeral range for us.
*/
fd[0] = lwip_socket(AF_INET, SOCK_DGRAM, 0);
if (fd[0] < 0)
goto bail;
fd[1] = lwip_socket(AF_INET, SOCK_DGRAM, 0);
if (fd[1] < 0)
goto bail;
/*
* No need for memset since it's in zalloc'd context... it's in the
* context so we can reuse the prepared sockaddr to send tp fd[0] whem
* we want to cancel the wait
*/
si->sin_family = AF_INET;
si->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
si->sin_port = 0;
if (lwip_bind(fd[0], (const struct sockaddr *)si, sizeof(*si)) < 0)
goto bail;
/*
* Query the socket to set context->frt_pipe_si to the full sockaddr it
* wants to be addressed by, including the port that lwip chose.
*
* Afterwards, we can use this prepared sockaddr stashed in the context
* to trigger the "pipe" without any other preliminaries.
*/
sl = sizeof(*si);
if (lwip_getsockname(fd[0], (struct sockaddr *)si, &sl))
goto bail;
lwsl_info("%s: cancel UDP skt port %d\n", __func__,
ntohs(si->sin_port));
return 0;
bail:
lwsl_err("%s: failed\n", __func__);
return 1;
}
int
lws_plat_pipe_signal(struct lws_context *ctx, int tsi)
{
struct lws_context_per_thread *pt = &ctx->pt[tsi];
struct sockaddr_in *si = &ctx->frt_pipe_si;
lws_sockfd_type *fd = pt->dummy_pipe_fds;
uint8_t u = 0;
int n;
/*
* Send a single UDP byte payload to the listening socket fd[0], forcing
* the event loop wait to wake. fd[1] and context->frt_pipe_si are
* set at context creation and are static, the UDP sendto is supposed to
* be threadsafe for lwip:
*
* https://lwip.fandom.com/wiki/LwIP_and_multithreading
*
* Sockets generally can't be used by more than one application thread
* (on udp/raw netconn, doing a sendto/recv is currently possible).
*/
n = lwip_sendto(fd[1], &u, 1, 0, (struct sockaddr *)si, sizeof(*si));
return n != 1;
}
void
lws_plat_pipe_close(struct lws *wsi)
{
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
lws_sockfd_type *fd = pt->dummy_pipe_fds;
if (fd[0] && fd[0] != -1)
close(fd[0]);
if (fd[1] && fd[1] != -1)
close(fd[1]);
fd[0] = fd[1] = -1;
}
int
lws_plat_pipe_is_fd_assocated(struct lws_context *cx, int tsi, lws_sockfd_type fd)
{
struct lws_context_per_thread *pt = &cx->pt[tsi];
return fd == pt->dummy_pipe_fds[0] || fd == pt->dummy_pipe_fds[1];
}

View File

@ -0,0 +1,62 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include "private-lib-async-dns.h"
#if defined(LWS_WITH_SYS_ASYNC_DNS)
lws_async_dns_server_check_t
lws_plat_asyncdns_init(struct lws_context *context, lws_async_dns_t *dns)
{
lws_sockaddr46 sa46t;
uint32_t ipv4;
lws_async_dns_server_check_t s = LADNS_CONF_SERVER_SAME;
lws_async_dns_server_t *dsrv;
FreeRTOS_GetAddressConfiguration(NULL, NULL, NULL, &ipv4);
memset(&sa46t, 0, sizeof(sa46t));
sa46t.sa4.sin_family = AF_INET;
sa46t.sa4.sin_addr.s_addr = ipv4;
dsrv = __lws_async_dns_server_find(dns, &sa46t);
if (!dsrv) {
__lws_async_dns_server_add(dns, &sa46t);
s = LADNS_CONF_SERVER_CHANGED;
}
return s;
}
#endif
int
lws_plat_ntpclient_config(struct lws_context *context)
{
lws_system_blob_heap_append(lws_system_get_blob(context,
LWS_SYSBLOB_TYPE_NTP_SERVER, 0),
(const uint8_t *)"pool.ntp.org", 13);
return 0;
}

View File

@ -0,0 +1,216 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
int
lws_plat_service(struct lws_context *context, int timeout_ms)
{
int n = _lws_plat_service_tsi(context, timeout_ms, 0);
#if !defined(LWS_AMAZON_RTOS) && defined(LWS_ESP_PLATFORM) && defined(CONFIG_ESP_INT_WDT)
esp_task_wdt_reset();
#endif
return n;
}
int
_lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
{
volatile struct lws_context_per_thread *vpt;
struct lws_context_per_thread *pt;
lws_usec_t timeout_us;
int n = -1, m, c, a = 0;
/* stay dead once we are dead */
if (!context)
return 1;
pt = &context->pt[tsi];
vpt = (volatile struct lws_context_per_thread *)pt;
{
unsigned long m = lws_now_secs();
if (m > context->time_last_state_dump) {
context->time_last_state_dump = m;
#if defined(LWS_ESP_PLATFORM)
n = esp_get_free_heap_size();
#else
n = xPortGetFreeHeapSize();
#endif
if ((unsigned int)n != context->last_free_heap) {
if ((unsigned int)n > context->last_free_heap)
lwsl_debug(" heap :%ld (+%ld)\n",
(unsigned long)n,
(unsigned long)(n -
context->last_free_heap));
else
lwsl_debug(" heap :%ld (-%ld)\n",
(unsigned long)n,
(unsigned long)(
context->last_free_heap -
n));
context->last_free_heap = n;
}
}
}
if (timeout_ms < 0)
timeout_ms = 0;
else
/* force a default timeout of 23 days */
timeout_ms = 2000000000;
timeout_us = ((lws_usec_t)timeout_ms) * LWS_US_PER_MS;
if (!pt->service_tid_detected && context->vhost_list) {
lws_fakewsi_def_plwsa(pt);
lws_fakewsi_prep_plwsa_ctx(context);
pt->service_tid = context->vhost_list->protocols[0].callback(
(struct lws *)plwsa, LWS_CALLBACK_GET_THREAD_ID,
NULL, NULL, 0);
pt->service_tid_detected = 1;
}
/*
* is there anybody with pending stuff that needs service forcing?
*/
#if !defined(LWS_AMAZON_RTOS)
again:
#endif
n = 0;
if (lws_service_adjust_timeout(context, 1, tsi)) {
#if defined(LWS_AMAZON_RTOS)
again:
#endif /* LWS_AMAZON_RTOS */
a = 0;
if (timeout_us) {
lws_usec_t us;
lws_pt_lock(pt, __func__);
/* don't stay in poll wait longer than next hr timeout */
us = __lws_sul_service_ripe(pt->pt_sul_owner,
LWS_COUNT_PT_SUL_OWNERS,
lws_now_usecs());
if (us && us < timeout_us)
timeout_us = us;
lws_pt_unlock(pt);
}
// n = poll(pt->fds, pt->fds_count, timeout_ms);
{
fd_set readfds, writefds, errfds;
struct timeval tv = { timeout_us / LWS_US_PER_SEC,
timeout_us % LWS_US_PER_SEC }, *ptv = &tv;
int max_fd = 0;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&errfds);
for (n = 0; n < (int)pt->fds_count; n++) {
pt->fds[n].revents = 0;
if (pt->fds[n].fd >= max_fd)
max_fd = pt->fds[n].fd;
if (pt->fds[n].events & LWS_POLLIN)
FD_SET(pt->fds[n].fd, &readfds);
if (pt->fds[n].events & LWS_POLLOUT)
FD_SET(pt->fds[n].fd, &writefds);
FD_SET(pt->fds[n].fd, &errfds);
}
vpt->inside_poll = 1;
lws_memory_barrier();
n = select(max_fd + 1, &readfds, &writefds, &errfds, ptv);
vpt->inside_poll = 0;
lws_memory_barrier();
n = 0;
for (m = 0; m < (int)pt->fds_count; m++) {
c = 0;
if (FD_ISSET(pt->fds[m].fd, &readfds)) {
pt->fds[m].revents |= LWS_POLLIN;
c = 1;
}
if (FD_ISSET(pt->fds[m].fd, &writefds)) {
pt->fds[m].revents |= LWS_POLLOUT;
c = 1;
}
if (FD_ISSET(pt->fds[m].fd, &errfds)) {
// lwsl_notice("errfds %d\n", pt->fds[m].fd);
pt->fds[m].revents |= LWS_POLLHUP;
c = 1;
}
if (c)
n++;
}
}
m = 0;
#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
m |= !!pt->ws.rx_draining_ext_list;
#endif
#if defined(LWS_WITH_TLS)
if (pt->context->tls_ops &&
pt->context->tls_ops->fake_POLLIN_for_buffered)
m |= pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
#endif
if (!m && !n)
return 0;
} else
a = 1;
m = lws_service_flag_pending(context, tsi);
c = m ? -1 : n;
/* any socket with events to service? */
for (n = 0; n < (int)pt->fds_count && c; n++) {
if (!pt->fds[n].revents)
continue;
c--;
m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
if (m < 0)
return -1;
/* if something closed, retry this slot */
if (m)
n--;
}
lws_service_do_ripe_rxflow(pt);
if (a)
goto again;
return 0;
}

View File

@ -0,0 +1,398 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include <errno.h>
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
int
lws_send_pipe_choked(struct lws *wsi)
{
struct lws *wsi_eff = wsi;
fd_set writefds;
struct timeval tv = { 0, 0 };
int n;
#if defined(LWS_WITH_HTTP2)
wsi_eff = lws_get_network_wsi(wsi);
#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 */
if (lws_has_buffered_out(wsi)
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|| wsi->http.comp_ctx.buflist_comp ||
wsi->http.comp_ctx.may_have_more
#endif
)
return 1;
FD_ZERO(&writefds);
FD_SET(wsi_eff->desc.sockfd, &writefds);
n = select(wsi_eff->desc.sockfd + 1, NULL, &writefds, NULL, &tv);
if (n < 0)
return 1; /* choked */
return !n; /* n = 0 = not writable = choked */
}
int
lws_poll_listen_fd(struct lws_pollfd *fd)
{
fd_set readfds;
struct timeval tv = { 0, 0 };
FD_ZERO(&readfds);
FD_SET(fd->fd, &readfds);
return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
}
int
lws_plat_set_nonblocking(lws_sockfd_type fd)
{
return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
}
int
lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
{
int optval = 1;
socklen_t optlen = sizeof(optval);
#if defined(__APPLE__) || \
defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
defined(__NetBSD__) || \
defined(__OpenBSD__)
struct protoent *tcp_proto;
#endif
if (vhost->ka_time) {
/* enable keepalive on this socket */
optval = 1;
if (lwip_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)
/*
* 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 */
optval = vhost->ka_time;
if (lwip_setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
(const void *)&optval, optlen) < 0)
return 1;
optval = vhost->ka_interval;
if (lwip_setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
(const void *)&optval, optlen) < 0)
return 1;
optval = vhost->ka_probes;
if (lwip_setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
(const void *)&optval, optlen) < 0)
return 1;
#endif
}
/* Disable Nagle */
optval = 1;
if (lwip_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, optlen) < 0)
return 1;
return lws_plat_set_nonblocking(fd);
}
static const int ip_opt_lws_flags[] = {
LCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT,
LCCSCF_IP_HIGH_RELIABILITY, LCCSCF_IP_LOW_COST
}, ip_opt_val[] = {
IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY, IPTOS_MINCOST
};
#if !defined(LWS_WITH_NO_LOGS)
static const char *ip_opt_names[] = {
"LOWDELAY", "THROUGHPUT", "RELIABILITY", "MINCOST"
};
#endif
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);
#if !defined(LWS_WITH_NO_LOGS)
int en;
#endif
#if defined(SO_PRIORITY)
if (pri) { /* 0 is the default already */
if (lwip_setsockopt(fd, SOL_SOCKET, SO_PRIORITY,
(const void *)&optval, optlen) < 0) {
#if !defined(LWS_WITH_NO_LOGS)
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
if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) {
optval = 1;
if (lwip_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval, optlen) < 0) {
#if !defined(LWS_WITH_NO_LOGS)
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__);
}
for (n = 0; n < 4; n++) {
if (!(lws_flags & ip_opt_lws_flags[n]))
continue;
optval = (int)ip_opt_val[n];
if (lwip_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]);
}
return ret;
}
/* cast a struct sockaddr_in6 * into addr for ipv6 */
int
lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
size_t addrlen)
{
#if 0
int rc = LWS_ITOSA_NOT_EXIST;
struct ifaddrs *ifr;
struct ifaddrs *ifc;
#ifdef LWS_WITH_IPV6
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
#endif
getifaddrs(&ifr);
for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
if (!ifc->ifa_addr)
continue;
lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
if (strcmp(ifc->ifa_name, ifname))
continue;
switch (ifc->ifa_addr->sa_family) {
case AF_INET:
#ifdef LWS_WITH_IPV6
if (ipv6) {
/* map IPv4 to IPv6 */
memset((char *)&addr6->sin6_addr, 0,
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));
} else
#endif
memcpy(addr,
(struct sockaddr_in *)ifc->ifa_addr,
sizeof(struct sockaddr_in));
break;
#ifdef LWS_WITH_IPV6
case AF_INET6:
memcpy(&addr6->sin6_addr,
&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
sizeof(struct in6_addr));
break;
#endif
default:
continue;
}
rc = LWS_ITOSA_USABLE;
}
freeifaddrs(ifr);
if (rc == LWS_ITOSA_NOT_EXIST) {
/* check if bind to IP address */
#ifdef LWS_WITH_IPV6
if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
rc = LWS_ITOSA_USABLE;
else
#endif
if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
rc = LWS_ITOSA_USABLE;
}
return rc;
#endif
return LWS_ITOSA_NOT_EXIST;
}
const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
return lwip_inet_ntop(af, src, dst, cnt);
}
int
lws_plat_inet_pton(int af, const char *src, void *dst)
{
return 1; // inet_pton(af, src, dst);
}
int
lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
size_t n, int fd, const char *iface)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_if_up(const char *ifname, int fd, int up)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
{
return 0;
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = write(fd, buf, len);
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)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
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 || !errno)
return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
#endif

View File

@ -0,0 +1,138 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
* Included from lib/private-lib-core.h if LWS_PLAT_FREERTOS
*/
#if !defined(LWS_ESP_PLATFORM)
#define SOMAXCONN 3
#endif
#if defined(LWS_AMAZON_RTOS)
int
open(const char *path, int oflag, ...);
#else
#include <fcntl.h>
#endif
#include <strings.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netdb.h>
#ifndef __cplusplus
#include <errno.h>
#endif
#include <signal.h>
#if !defined(LWS_AMAZON_RTOS)
#include <sys/socket.h>
#endif
#if defined(LWS_AMAZON_RTOS)
#include "FreeRTOS.h"
#if defined(LWS_WITH_SYS_ASYNC_DNS)
#include "FreeRTOS_IP.h"
#endif
#include "timers.h"
#if defined(LWS_ESP_PLATFORM)
#include <esp_attr.h>
#endif
#include <semphr.h>
#else
#include "freertos/timers.h"
#if defined(LWS_ESP_PLATFORM)
#include <esp_attr.h>
#if !defined(ETHER_ADDR_LEN)
#define ETHER_ADDR_LEN 6
#endif
#endif
#include <esp_system.h>
#include <esp_task_wdt.h>
#endif
#if defined(LWS_WITH_ESP32)
#include "lwip/apps/sntp.h"
#include <errno.h>
#endif
typedef SemaphoreHandle_t lws_mutex_t;
#define lws_mutex_init(x) x = xSemaphoreCreateMutex()
#define lws_mutex_destroy(x) vSemaphoreDelete(x)
#define lws_mutex_lock(x) (!xSemaphoreTake(x, portMAX_DELAY)) /*0 = OK */
#define lws_mutex_unlock(x) xSemaphoreGive(x)
#include <lwip/sockets.h>
#if defined(LWS_BUILTIN_GETIFADDRS)
#include "./misc/getifaddrs.h"
#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
#define LWS_ENOTCONN ENOTCONN
#define LWS_EWOULDBLOCK EWOULDBLOCK
#define LWS_EADDRINUSE EADDRINUSE
#define LWS_ECONNABORTED ECONNABORTED
#define lws_set_blocking_send(wsi)
#ifndef LWS_NO_FORK
#ifdef LWS_HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
#endif
#if !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0
#endif
#define compatible_close(x) close(x)
#define lws_plat_socket_offset() LWIP_SOCKET_OFFSET
#define wsi_from_fd(A,B) A->lws_lookup[B - lws_plat_socket_offset()]
struct lws_context;
struct lws;
int
insert_wsi(const struct lws_context *context, struct lws *wsi);
#define delete_from_fd(A,B) assert((int)A->max_fds > B - lws_plat_socket_offset()); \
A->lws_lookup[B - lws_plat_socket_offset()] = 0
#define LWS_PLAT_TIMER_TYPE TimerHandle_t
#define LWS_PLAT_TIMER_CB(name, var) void name(TimerHandle_t var)
#define LWS_PLAT_TIMER_CB_GET_OPAQUE(x) pvTimerGetTimerID(x)
#define LWS_PLAT_TIMER_CREATE(name, interval, repeat, opaque, cb) \
xTimerCreate(name, pdMS_TO_TICKS(interval) ? pdMS_TO_TICKS(interval) : 1, \
repeat ? pdTRUE : 0, opaque, cb)
#define LWS_PLAT_TIMER_DELETE(ptr) xTimerDelete(ptr, 0)
#define LWS_PLAT_TIMER_START(ptr) xTimerStart(ptr, 0)
#define LWS_PLAT_TIMER_STOP(ptr) xTimerStop(ptr, 0)

View File

@ -0,0 +1,49 @@
#
# libwebsockets - small server side websockets and web server implementation
#
# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
#
# 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:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# 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.
#
# The strategy is to only export to PARENT_SCOPE
#
# - changes to LIB_LIST
# - changes to SOURCES
# - includes via include_directories
#
# and keep everything else private
include_directories(.)
list(APPEND SOURCES
plat/optee/lws-plat-optee.c
)
if (LWS_WITH_NETWORK)
list(APPEND SOURCES
plat/optee/network.c
)
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot ../../../../lib/libutils/isoc/include -I../../../../lib/libutils/isoc/include -I../../../../lib/libutils/ext/include" )
#
# Keep explicit parent scope exports at end
#
exports_to_parent_scope()

View File

@ -0,0 +1,261 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#if !defined(LWS_WITH_NETWORK)
#include <crypto/crypto.h>
#endif
int errno;
#if !defined(LWS_WITH_NETWORK)
char *
strcpy(char *dest, const char *src)
{
char *desto = dest;
while (*src)
*(dest++) = *(src++);
*(dest++) = '\0';
return desto;
}
char *strncpy(char *dest, const char *src, size_t limit)
{
char *desto = dest;
while (*src && limit--)
*(dest++) = *(src++);
if (limit)
*(dest++) = '\0';
return desto;
}
#endif
int lws_plat_apply_FD_CLOEXEC(int n)
{
return 0;
}
void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen);
#if defined(LWS_WITH_NETWORK)
uint64_t
lws_now_usecs(void)
{
return ((unsigned long long)time(NULL)) * 1000000;
}
#endif
size_t
lws_get_random(struct lws_context *context, void *buf, size_t len)
{
#if defined(LWS_WITH_NETWORK)
TEE_GenerateRandom(buf, len);
#else
crypto_rng_read(buf, len);
#endif
return len;
}
static const char * const colours[] = {
"[31;1m", /* LLL_ERR */
"[36;1m", /* LLL_WARN */
"[35;1m", /* LLL_NOTICE */
"[32;1m", /* LLL_INFO */
"[34;1m", /* LLL_DEBUG */
"[33;1m", /* LLL_PARSER */
"[33;1m", /* LLL_HEADER */
"[33;1m", /* LLL_EXT */
"[33;1m", /* LLL_CLIENT */
"[33;1m", /* LLL_LATENCY */
"[30;1m", /* LLL_USER */
};
void lwsl_emit_optee(int level, const char *line)
{
char buf[50], linecp[512];
int n, m = LWS_ARRAY_SIZE(colours) - 1;
lwsl_timestamp(level, buf, sizeof(buf));
n = 1 << (LWS_ARRAY_SIZE(colours) - 1);
while (n) {
if (level & n)
break;
m--;
n >>= 1;
}
n = strlen(line);
if ((unsigned int)n > sizeof(linecp) - 1)
n = sizeof(linecp) - 1;
if (n) {
memcpy(linecp, line, n - 1);
linecp[n - 1] = '\0';
} else
linecp[0] = '\0';
EMSG("%c%s%s%s%c[0m", 27, colours[m], buf, linecp, 27);
}
int
lws_plat_set_nonblocking(lws_sockfd_type fd)
{
return 0;
}
int
lws_plat_drop_app_privileges(struct lws_context *context, int actually_init)
{
return 0;
}
int
lws_plat_context_early_init(void)
{
return 0;
}
void
lws_plat_context_early_destroy(struct lws_context *context)
{
}
void
lws_plat_context_late_destroy(struct lws_context *context)
{
#if defined(LWS_WITH_NETWORK)
if (context->lws_lookup)
lws_free(context->lws_lookup);
#endif
}
lws_fop_fd_t
_lws_plat_file_open(const struct lws_plat_file_ops *fops,
const char *filename, const char *vpath, lws_fop_flags_t *flags)
{
return NULL;
}
int
_lws_plat_file_close(lws_fop_fd_t *fop_fd)
{
return 0;
}
lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
{
return 0;
}
int
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
return 0;
}
int
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
return 0;
}
int
lws_plat_init(struct lws_context *context,
const struct lws_context_creation_info *info)
{
#if defined(LWS_WITH_NETWORK)
/* context has the global fd lookup array */
context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
context->max_fds, "lws_lookup");
if (context->lws_lookup == NULL) {
lwsl_err("OOM on lws_lookup array for %d connections\n",
context->max_fds);
return 1;
}
lwsl_notice(" mem: platform fd map: %5lu bytes\n",
(long)sizeof(struct lws *) * context->max_fds);
#endif
#ifdef LWS_WITH_PLUGINS
if (info->plugin_dirs)
lws_plat_plugins_init(context, info->plugin_dirs);
#endif
return 0;
}
int
lws_plat_write_file(const char *filename, void *buf, size_t len)
{
return 1;
}
int
lws_plat_read_file(const char *filename, void *buf, int len)
{
return -1;
}
int
lws_plat_recommended_rsa_bits(void)
{
return 4096;
}
int
lws_plat_ntpclient_config(struct lws_context *context)
{
#if 0
char *ntpsrv = getenv("LWS_NTP_SERVER");
if (ntpsrv && strlen(ntpsrv) < 64) {
lws_system_blob_heap_append(lws_system_get_blob(context,
LWS_SYSBLOB_TYPE_NTP_SERVER, 0),
(const uint8_t *)ntpsrv,
strlen(ntpsrv));
return 1;
}
#endif
return 0;
}
void
lws_msleep(unsigned int ms)
{
}

View File

@ -0,0 +1,328 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
int
lws_plat_pipe_create(struct lws *wsi)
{
return 1;
}
int
lws_plat_pipe_signal(struct lws *wsi)
{
return 1;
}
void
lws_plat_pipe_close(struct lws *wsi)
{
}
int
lws_plat_pipe_is_fd_assocated(struct lws_context *cx, int tsi, lws_sockfd_type fd)
{
return 0;
}
int
lws_send_pipe_choked(struct lws *wsi)
{
struct lws *wsi_eff;
#if defined(LWS_WITH_HTTP2)
wsi_eff = lws_get_network_wsi(wsi);
#else
wsi_eff = wsi;
#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 */
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
)
return 1;
/* okay to send another packet without blocking */
return 0;
}
int
lws_poll_listen_fd(struct lws_pollfd *fd)
{
// return poll(fd, 1, 0);
return 0;
}
int
_lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
{
lws_usec_t timeout_us = timeout_ms * LWS_US_PER_MS;
struct lws_context_per_thread *pt;
int n = -1, m, c, a = 0;
//char buf;
/* stay dead once we are dead */
if (!context)
return 1;
pt = &context->pt[tsi];
if (timeout_ms < 0)
timeout_ms = 0;
else
timeout_ms = 2000000000;
if (!pt->service_tid_detected && context->vhost_list) {
struct lws _lws;
memset(&_lws, 0, sizeof(_lws));
_lws.context = context;
pt->service_tid = context->vhost_list->protocols[0].callback(
&_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
pt->service_tid_detected = 1;
}
/*
* is there anybody with pending stuff that needs service forcing?
*/
if (lws_service_adjust_timeout(context, 1, tsi)) {
again:
a = 0;
if (timeout_us) {
lws_usec_t us;
lws_pt_lock(pt, __func__);
/* don't stay in poll wait longer than next hr timeout */
us = __lws_sul_service_ripe(pt->pt_sul_owner,
LWS_COUNT_PT_SUL_OWNERS,
lws_now_usecs());
if (us && us < timeout_us)
timeout_us = us;
lws_pt_unlock(pt);
}
n = poll(pt->fds, pt->fds_count, timeout_us / LWS_US_PER_MS);
m = 0;
if (pt->context->tls_ops &&
pt->context->tls_ops->fake_POLLIN_for_buffered)
m = pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
if (/*!pt->ws.rx_draining_ext_list && */!m && !n) /* nothing to do */
return 0;
} else
a = 1;
m = lws_service_flag_pending(context, tsi);
if (m)
c = -1; /* unknown limit */
else
if (n < 0) {
if (LWS_ERRNO != LWS_EINTR)
return -1;
return 0;
} else
c = n;
/* any socket with events to service? */
for (n = 0; n < (int)pt->fds_count && c; n++) {
if (!pt->fds[n].revents)
continue;
c--;
#if 0
if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
if (read(pt->fds[n].fd, &buf, 1) != 1)
lwsl_err("Cannot read from dummy pipe.");
continue;
}
#endif
m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
if (m < 0)
return -1;
/* if something closed, retry this slot */
if (m)
n--;
}
if (a)
goto again;
return 0;
}
int
lws_plat_service(struct lws_context *context, int timeout_ms)
{
return _lws_plat_service_tsi(context, timeout_ms, 0);
}
int
lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
{
return 0;
}
int
lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
size_t len)
{
return 1;
}
/* cast a struct sockaddr_in6 * into addr for ipv6 */
int
lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
size_t addrlen)
{
return -1;
}
void
lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
pt->fds[pt->fds_count++].revents = 0;
}
void
lws_plat_delete_socket_from_fds(struct lws_context *context,
struct lws *wsi, int m)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
pt->fds_count--;
}
int
lws_plat_change_pollfd(struct lws_context *context,
struct lws *wsi, struct lws_pollfd *pfd)
{
return 0;
}
const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
//return inet_ntop(af, src, dst, cnt);
return "lws_plat_inet_ntop";
}
int
lws_plat_inet_pton(int af, const char *src, void *dst)
{
//return inet_pton(af, src, dst);
return 1;
}
int
lws_plat_set_socket_options_ip(int fd, uint8_t pri, unsigned int lws_flags)
{
return 0;
}
int
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
{
return 0;
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = write(fd, buf, len);
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)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
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

View File

@ -0,0 +1,51 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
* Included from lib/private-lib-core.h if LWS_WITH_OPTEE
*/
#include <unistd.h>
#include <sys/types.h>
#define LWS_ERRNO errno
#define LWS_EAGAIN EAGAIN
#define LWS_EALREADY EALREADY
#define LWS_EINPROGRESS EINPROGRESS
#define LWS_EINTR EINTR
#define LWS_EISCONN EISCONN
#define LWS_ENOTCONN ENOTCONN
#define LWS_EWOULDBLOCK EWOULDBLOCK
#define LWS_EADDRINUSE EADDRINUSE
#define lws_set_blocking_send(wsi)
#define compatible_close(x) close(x)
#define lws_plat_socket_offset() (0)
#define wsi_from_fd(A,B) A->lws_lookup[B - lws_plat_socket_offset()]
#define insert_wsi(A,B) assert(A->lws_lookup[B->desc.sockfd - \
lws_plat_socket_offset()] == 0); \
A->lws_lookup[B->desc.sockfd - \
lws_plat_socket_offset()] = B
#define delete_from_fd(A,B) assert((int)A->max_fds > B - lws_plat_socket_offset()); \
A->lws_lookup[B - lws_plat_socket_offset()] = 0

View File

@ -0,0 +1,115 @@
#
# libwebsockets - small server side websockets and web server implementation
#
# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
#
# 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:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# 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.
#
# The strategy is to only export to PARENT_SCOPE
#
# - changes to LIB_LIST
# - changes to SOURCES
# - includes via include_directories
#
# and keep everything else private
include_directories(.)
execute_process( COMMAND grep -c illumos /lib/ld.so.1
OUTPUT_VARIABLE ILLUMOS ERROR_QUIET )
# Chomp the \n at end of output.
string(REGEX REPLACE "[\n]+" "" ILLUMOS "${ILLUMOS}")
if (NOT ${ILLUMOS} MATCHES "0")
set(ILLUMOS 1)
endif()
set(LWS_PLAT_UNIX 1)
list(APPEND SOURCES
plat/unix/unix-caps.c
plat/unix/unix-misc.c
plat/unix/unix-init.c
)
if (LWS_WITH_FILE_OPS)
list(APPEND SOURCES plat/unix/unix-file.c)
endif()
if (LWS_WITH_NETWORK)
list(APPEND SOURCES
plat/unix/unix-pipe.c
plat/unix/unix-service.c
plat/unix/unix-sockets.c
plat/unix/unix-fds.c
)
if (LWS_WITH_SYS_ASYNC_DNS)
if (LWS_PLAT_ANDROID)
list(APPEND SOURCES plat/unix/android/android-resolv.c)
else()
list(APPEND SOURCES plat/unix/unix-resolv.c)
endif()
endif()
if (LWS_HAVE_SYSTEMD_H)
list(APPEND SOURCES
plat/unix/unix-systemd.c
)
list(APPEND LIB_LIST_AT_END systemd)
endif()
endif()
if (LWS_WITH_PLUGINS_API)
list(APPEND SOURCES plat/unix/unix-plugins.c)
endif()
if (LWS_WITH_SPAWN)
list(APPEND SOURCES plat/unix/unix-spawn.c)
endif()
if (HAIKU)
set(CMAKE_REQUIRED_LIBRARIES network)
list(APPEND LIB_LIST_AT_END network)
endif()
IF (CMAKE_SYSTEM_NAME STREQUAL Linux AND NOT LWS_WITHOUT_EVENTFD)
CHECK_FUNCTION_EXISTS(eventfd_read LWS_HAVE_EVENTFD)
endif()
list(APPEND LIB_LIST_AT_END m)
if (ILLUMOS)
list(APPEND LIB_LIST_AT_END socket)
endif()
if (LWS_HAVE_LIBCAP)
list(APPEND LIB_LIST_AT_END cap)
endif()
if (${CMAKE_SYSTEM_NAME} MATCHES "QNX")
list(APPEND LIB_LIST_AT_END socket)
endif()
list(APPEND LIB_LIST_AT_END ${CMAKE_DL_LIBS})
#
# Keep explicit parent scope exports at end
#
exports_to_parent_scope()
set(LWS_PLAT_UNIX ${LWS_PLAT_UNIX} PARENT_SCOPE)
set(ILLUMOS ${ILLUMOS} PARENT_SCOPE)
set(LIB_LIST_AT_END ${LIB_LIST_AT_END} PARENT_SCOPE)
set(LWS_PLAT_UNIX ${LWS_PLAT_UNIX} PARENT_SCOPE)

View File

@ -0,0 +1,59 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include "private-lib-async-dns.h"
#include <sys/system_properties.h>
lws_async_dns_server_check_t
lws_plat_asyncdns_init(struct lws_context *context, lws_async_dns_t *dns)
{
lws_async_dns_server_check_t s = LADNS_CONF_SERVER_SAME;
char prop[PROP_VALUE_MAX], netdns[9];
lws_async_dns_server_t *dsrv;
lws_sockaddr46 sa46t;
int n;
strcpy(netdns, "net.dns1");
for (n = 0; n < 4; n++) {
prop[0] = '\0';
if (__system_property_get(netdns, prop) <= 0)
continue;
netdns[7]++; /* net.dns2... etc */
memset(&sa46t, 0, sizeof(sa46t));
if (lws_sa46_parse_numeric_address(prop, &sa46t) < 0)
continue;
dsrv = __lws_async_dns_server_find(dns, &sa46t);
if (!dsrv) {
__lws_async_dns_server_add(dns, &sa46t);
s = LADNS_CONF_SERVER_CHANGED;
}
}
return s;
}

View File

@ -0,0 +1,204 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
* Included from lib/private-lib-core.h if no explicit platform
*/
#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>
#if defined(LWS_HAVE_EVENTFD)
#include <sys/eventfd.h>
#endif
#if defined(__APPLE__)
#include <machine/endian.h>
#endif
#if defined(__FreeBSD__)
#include <sys/endian.h>
#endif
#if defined(__linux__)
#include <endian.h>
#include <linux/if_packet.h>
#include <net/if.h>
#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(LWS_HAVE_PTHREAD_H)
#include <pthread.h>
typedef pthread_mutex_t lws_mutex_t;
#define lws_mutex_init(x) pthread_mutex_init(&(x), NULL)
#define lws_mutex_destroy(x) pthread_mutex_destroy(&(x))
#define lws_mutex_lock(x) pthread_mutex_lock(&(x))
#define lws_mutex_unlock(x) pthread_mutex_unlock(&(x))
#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
#if defined (__sun) || defined(__HAIKU__) || defined(__QNX__) || defined(__ANDROID__) || defined(__NuttX__)
#include <syslog.h>
#if defined(__ANDROID__)
#include <sys/resource.h>
#endif
#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
#define LWS_ENOTCONN ENOTCONN
#define LWS_EWOULDBLOCK EWOULDBLOCK
#define LWS_EADDRINUSE EADDRINUSE
#define lws_set_blocking_send(wsi)
#define LWS_SOCK_INVALID (-1)
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);
struct lws_dhcpc_ifstate;
int
lws_plat_ifconfig(int fd, struct lws_dhcpc_ifstate *is);
void
delete_from_fd(const struct lws_context *context, int fd);
#ifndef LWS_NO_FORK
#ifdef LWS_HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
#endif
#define compatible_close(x) close(x)
#define compatible_file_close(fd) close(fd)
#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__
/* iOS SDK 12+ seems to define it, undef it for compatibility both ways */
#undef MSG_NOSIGNAL
#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif
/*
* Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
* POSIX 2008.
*/
#if defined(__sun) && !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0
#endif
int
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
size_t n, int fd, const char *iface);
int
lws_plat_if_up(const char *ifname, int fd, int up);

View File

@ -0,0 +1,249 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
#include <pwd.h>
#include <grp.h>
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
static void
_lws_plat_apply_caps(unsigned int mode, const cap_value_t *cv, int count)
{
cap_t caps;
if (!count)
return;
caps = cap_get_proc();
cap_set_flag(caps, (cap_flag_t)mode, count, cv, CAP_SET);
cap_set_proc(caps);
prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
cap_free(caps);
}
#endif
int
lws_plat_user_colon_group_to_ids(const char *u_colon_g, uid_t *puid, gid_t *pgid)
{
char *colon = strchr(u_colon_g, ':'), u[33];
struct group *g;
struct passwd *p;
size_t ulen;
if (!colon)
return 1;
ulen = (size_t)(unsigned int)lws_ptr_diff(colon, u_colon_g);
if (ulen < 2 || ulen > sizeof(u) - 1)
return 1;
memcpy(u, u_colon_g, ulen);
u[ulen] = '\0';
colon++;
#if defined(LWS_HAVE_GETGRNAM_R)
{
struct group gr;
char strs[1024];
if (getgrnam_r(colon, &gr, strs, sizeof(strs), &g) || !g) {
#else
{
g = getgrnam(colon);
if (!g) {
#endif
lwsl_err("%s: unknown group '%s'\n", __func__, colon);
return 1;
}
*pgid = g->gr_gid;
}
#if defined(LWS_HAVE_GETPWNAM_R)
{
struct passwd pr;
char strs[1024];
if (getpwnam_r(u, &pr, strs, sizeof(strs), &p) || !p) {
#else
{
p = getpwnam(u);
if (!p) {
#endif
lwsl_err("%s: unknown user '%s'\n", __func__, u);
return 1;
}
*puid = p->pw_uid;
}
return 0;
}
int
lws_plat_drop_app_privileges(struct lws_context *context, int actually_drop)
{
struct passwd *p;
struct group *g;
/* if he gave us the groupname, align gid to match it */
if (context->groupname) {
#if defined(LWS_HAVE_GETGRNAM_R)
struct group gr;
char strs[1024];
if (!getgrnam_r(context->groupname, &gr, strs, sizeof(strs), &g) && g) {
#else
g = getgrnam(context->groupname);
if (g) {
#endif
lwsl_cx_info(context, "group %s -> gid %u",
context->groupname, g->gr_gid);
context->gid = g->gr_gid;
} else {
lwsl_cx_err(context, "unknown groupname '%s'",
context->groupname);
return 1;
}
}
/* if he gave us the username, align uid to match it */
if (context->username) {
#if defined(LWS_HAVE_GETPWNAM_R)
struct passwd pr;
char strs[1024];
if (!getpwnam_r(context->username, &pr, strs, sizeof(strs), &p) && p) {
#else
p = getpwnam(context->username);
if (p) {
#endif
context->uid = p->pw_uid;
lwsl_cx_info(context, "username %s -> uid %u",
context->username, (unsigned int)p->pw_uid);
} else {
lwsl_cx_err(context, "unknown username %s",
context->username);
return 1;
}
}
if (!actually_drop)
return 0;
/* if he gave us the gid or we have it from the groupname, set it */
if (context->gid && context->gid != (gid_t)-1l) {
#if defined(LWS_HAVE_GETGRGID_R)
struct group gr;
char strs[1024];
if (getgrgid_r(context->gid, &gr, strs, sizeof(strs), &g) || !g) {
#else
g = getgrgid(context->gid);
if (!g) {
#endif
lwsl_cx_err(context, "cannot find name for gid %d",
context->gid);
return 1;
}
if (setgid(context->gid)) {
lwsl_cx_err(context, "setgid: %s failed",
strerror(LWS_ERRNO));
return 1;
}
lwsl_cx_notice(context, "effective group '%s'", g->gr_name);
} else
lwsl_cx_info(context, "not changing group");
/* if he gave us the uid or we have it from the username, set it */
if (context->uid && context->uid != (uid_t)-1l) {
#if defined(LWS_HAVE_GETPWUID_R)
struct passwd pr;
char strs[1024];
if (getpwuid_r(context->uid, &pr, strs, sizeof(strs), &p) || !p) {
#else
p = getpwuid(context->uid);
if (!p) {
#endif
lwsl_cx_err(context, "getpwuid: unable to find uid %d",
context->uid);
return 1;
}
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
_lws_plat_apply_caps(CAP_PERMITTED, context->caps,
context->count_caps);
#endif
if (initgroups(p->pw_name,
#if defined(__APPLE__)
(int)
#endif
context->gid))
return 1;
if (setuid(context->uid)) {
lwsl_cx_err(context, "setuid: %s failed",
strerror(LWS_ERRNO));
return 1;
} else
lwsl_cx_notice(context, "effective user '%s'",
p->pw_name);
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
_lws_plat_apply_caps(CAP_EFFECTIVE, context->caps,
context->count_caps);
if (context->count_caps) {
int n;
for (n = 0; n < context->count_caps; n++)
lwsl_cx_notice(context, " RETAINING CAP %d",
(int)context->caps[n]);
}
#endif
} else
lwsl_cx_info(context, "not changing user");
return 0;
}

View File

@ -0,0 +1,266 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
struct lws *
wsi_from_fd(const struct lws_context *context, int fd)
{
struct lws **p, **done;
if (!context->max_fds_unrelated_to_ulimit)
return context->lws_lookup[fd - lws_plat_socket_offset()];
/* slow fds handling */
p = context->lws_lookup;
done = &p[context->max_fds];
while (p != done) {
if (*p && (*p)->desc.sockfd == fd)
return *p;
p++;
}
return NULL;
}
#if defined(_DEBUG)
int
sanity_assert_no_wsi_traces(const struct lws_context *context, struct lws *wsi)
{
struct lws **p, **done;
if (!context->max_fds_unrelated_to_ulimit)
/* can't tell */
return 0;
/* slow fds handling */
p = context->lws_lookup;
done = &p[context->max_fds];
/* confirm the wsi doesn't already exist */
while (p != done && *p != wsi)
p++;
if (p == done)
return 0;
assert(0); /* this wsi is still mentioned inside lws */
return 1;
}
int
sanity_assert_no_sockfd_traces(const struct lws_context *context,
lws_sockfd_type sfd)
{
#if LWS_MAX_SMP > 1
/*
* We can't really do this test... another thread can accept and
* reuse the closed fd
*/
return 0;
#else
struct lws **p, **done;
if (sfd == LWS_SOCK_INVALID || !context->lws_lookup)
return 0;
if (!context->max_fds_unrelated_to_ulimit &&
context->lws_lookup[sfd - lws_plat_socket_offset()]) {
assert(0); /* the fd is still in use */
return 1;
}
/* slow fds handling */
p = context->lws_lookup;
done = &p[context->max_fds];
/* confirm the sfd not already in use */
while (p != done && (!*p || (*p)->desc.sockfd != sfd))
p++;
if (p == done)
return 0;
assert(0); /* this fd is still in the tables */
return 1;
#endif
}
#endif
int
insert_wsi(const struct lws_context *context, struct lws *wsi)
{
struct lws **p, **done;
if (sanity_assert_no_wsi_traces(context, wsi))
return 0;
if (!context->max_fds_unrelated_to_ulimit) {
assert(context->lws_lookup[wsi->desc.sockfd -
lws_plat_socket_offset()] == 0);
context->lws_lookup[wsi->desc.sockfd - \
lws_plat_socket_offset()] = wsi;
return 0;
}
/* slow fds handling */
p = context->lws_lookup;
done = &p[context->max_fds];
/* confirm fd isn't already in use by a wsi */
if (sanity_assert_no_sockfd_traces(context, wsi->desc.sockfd))
return 0;
p = context->lws_lookup;
/* find an empty slot */
while (p != done && *p)
p++;
if (p == done) {
lwsl_err("%s: reached max fds\n", __func__);
return 1;
}
*p = wsi;
return 0;
}
void
delete_from_fd(const struct lws_context *context, int fd)
{
struct lws **p, **done;
if (!context->max_fds_unrelated_to_ulimit) {
if (context->lws_lookup) {
assert((int)context->max_fds > fd - lws_plat_socket_offset());
context->lws_lookup[fd - lws_plat_socket_offset()] = NULL;
}
return;
}
/* slow fds handling */
p = context->lws_lookup;
assert(p);
done = &p[context->max_fds];
/* find the match */
while (p != done && (!*p || (*p)->desc.sockfd != fd))
p++;
if (p != done)
*p = NULL;
#if defined(_DEBUG)
p = context->lws_lookup;
while (p != done && (!*p || (*p)->desc.sockfd != fd))
p++;
if (p != done) {
lwsl_err("%s: fd %d in lws_lookup again at %d\n", __func__,
fd, (int)(p - context->lws_lookup));
assert(0);
}
#endif
}
void
delete_from_fdwsi(const struct lws_context *context, struct lws *wsi)
{
struct lws **p, **done;
if (!context->max_fds_unrelated_to_ulimit)
return;
/* slow fds handling */
p = context->lws_lookup;
done = &p[context->max_fds];
/* find the match */
while (p != done && (!*p || (*p) != wsi))
p++;
if (p != done)
*p = NULL;
}
void
lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
if (context->event_loop_ops->io)
context->event_loop_ops->io(wsi, LWS_EV_START | LWS_EV_READ);
pt->fds[pt->fds_count++].revents = 0;
}
void
lws_plat_delete_socket_from_fds(struct lws_context *context,
struct lws *wsi, int m)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
if (context->event_loop_ops->io)
context->event_loop_ops->io(wsi,
LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
pt->fds_count--;
}
int
lws_plat_change_pollfd(struct lws_context *context,
struct lws *wsi, struct lws_pollfd *pfd)
{
return 0;
}

View File

@ -0,0 +1,185 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
#include <pwd.h>
#include <grp.h>
#ifdef LWS_WITH_PLUGINS
#include <dlfcn.h>
#endif
#include <dirent.h>
int lws_plat_apply_FD_CLOEXEC(int n)
{
if (n == -1)
return 0;
return fcntl(n, F_SETFD, FD_CLOEXEC);
}
int
lws_plat_write_file(const char *filename, void *buf, size_t len)
{
ssize_t m;
int fd;
fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd == -1)
return 1;
m = write(fd, buf, len);
close(fd);
if (m < 0)
return 1;
return (size_t)m != len;
}
int
lws_plat_read_file(const char *filename, void *buf, size_t len)
{
int fd = lws_open(filename, O_RDONLY);
ssize_t n;
if (fd == -1)
return -1;
n = read(fd, buf, len);
close(fd);
return (int)n;
}
lws_fop_fd_t
_lws_plat_file_open(const struct lws_plat_file_ops *fops_own,
const struct lws_plat_file_ops *fops, const char *filename,
const char *vpath, lws_fop_flags_t *flags)
{
struct stat stat_buf;
int ret = lws_open(filename, (int)((*flags) & LWS_FOP_FLAGS_MASK), 0664);
lws_fop_fd_t fop_fd;
if (ret < 0)
return NULL;
if (fstat(ret, &stat_buf) < 0)
goto bail;
fop_fd = malloc(sizeof(*fop_fd));
if (!fop_fd)
goto bail;
fop_fd->fops = fops;
fop_fd->flags = *flags;
fop_fd->fd = ret;
fop_fd->filesystem_priv = NULL; /* we don't use it */
fop_fd->len = (lws_filepos_t)stat_buf.st_size;
fop_fd->pos = 0;
return fop_fd;
bail:
close(ret);
return NULL;
}
int
_lws_plat_file_close(lws_fop_fd_t *fop_fd)
{
int fd = (*fop_fd)->fd;
free(*fop_fd);
*fop_fd = NULL;
return close(fd);
}
lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
{
lws_fileofs_t r;
if (offset > 0 &&
offset > (lws_fileofs_t)fop_fd->len - (lws_fileofs_t)fop_fd->pos)
offset = (lws_fileofs_t)(fop_fd->len - fop_fd->pos);
if ((lws_fileofs_t)fop_fd->pos + offset < 0)
offset = (lws_fileofs_t)(-fop_fd->pos);
r = lseek(fop_fd->fd, (off_t)offset, SEEK_CUR);
if (r >= 0)
fop_fd->pos = (lws_filepos_t)r;
else
lwsl_err("error seeking from cur %ld, offset %ld\n",
(long)fop_fd->pos, (long)offset);
return r;
}
int
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
ssize_t n;
n = read((int)fop_fd->fd, buf, (size_t)len);
if (n == -1l) {
*amount = 0;
return -1;
}
fop_fd->pos = (lws_filepos_t)(fop_fd->pos + (lws_filepos_t)n);
lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__,
(long)n, (long)len, (long)fop_fd->pos,
(long)fop_fd->len);
*amount = (lws_filepos_t)n;
return 0;
}
int
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
ssize_t n;
n = write((int)fop_fd->fd, buf, (size_t)len);
if (n == -1) {
*amount = 0;
return -1;
}
fop_fd->pos = (lws_filepos_t)(fop_fd->pos + (lws_filepos_t)n);
*amount = (lws_filepos_t)n;
return 0;
}

View File

@ -0,0 +1,270 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
#include <pwd.h>
#include <grp.h>
#ifdef LWS_WITH_PLUGINS
#include <dlfcn.h>
#endif
#include <dirent.h>
#if defined(LWS_WITH_NETWORK)
static void
lws_sul_plat_unix(lws_sorted_usec_list_t *sul)
{
struct lws_context_per_thread *pt =
lws_container_of(sul, struct lws_context_per_thread, sul_plat);
struct lws_context *context = pt->context;
int n = 0, m = 0;
#if !defined(LWS_NO_DAEMONIZE)
/* if our parent went down, don't linger around */
if (pt->context->started_with_parent &&
kill(pt->context->started_with_parent, 0) < 0)
kill(getpid(), SIGTERM);
#endif
for (n = 0; n < context->count_threads; n++)
m = m | (int)pt->fds_count;
if (context->deprecated && !m) {
lwsl_notice("%s: ending deprecated context\n", __func__);
kill(getpid(), SIGINT);
return;
}
#if defined(LWS_WITH_SERVER)
lws_context_lock(context, "periodic checks");
lws_start_foreach_llp(struct lws_vhost **, pv,
context->no_listener_vhost_list) {
struct lws_vhost *v = *pv;
lwsl_debug("deferred iface: checking if on vh %s\n", (*pv)->name);
if (_lws_vhost_init_server(NULL, *pv) == 0) {
/* became happy */
lwsl_notice("vh %s: became connected\n", v->name);
*pv = v->no_listener_vhost_list;
v->no_listener_vhost_list = NULL;
break;
}
} lws_end_foreach_llp(pv, no_listener_vhost_list);
lws_context_unlock(context);
#endif
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
&pt->sul_plat, 30 * LWS_US_PER_SEC);
}
#endif
#if defined(LWS_WITH_PLUGINS)
static int
protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
{
struct lws_context *context = (struct lws_context *)each_user;
const lws_plugin_protocol_t *plpr =
(const lws_plugin_protocol_t *)pin->hdr;
context->plugin_protocol_count = (short)(context->plugin_protocol_count +
plpr->count_protocols);
context->plugin_extension_count = (short)(context->plugin_extension_count +
plpr->count_extensions);
return 0;
}
#endif
int
lws_plat_init(struct lws_context *context,
const struct lws_context_creation_info *info)
{
int fd;
#if defined(LWS_WITH_NETWORK)
/*
* context has the process-global fd lookup array. This can be
* done two different ways now; one or the other is done depending on if
* info->fd_limit_per_thread was snonzero
*
* - default: allocate a worst-case lookup array sized for ulimit -n
* and use the fd directly as an index into it
*
* - slow: allocate context->max_fds entries only (which can be
* forced at context creation time to be
* info->fd_limit_per_thread * the number of threads)
* and search the array to lookup fds
*
* the default way is optimized for server, if you only use one or two
* client wsi the slow way may save a lot of memory.
*
* Both ways allocate an array of struct lws *... one allocates it for
* all possible fd indexes the process could produce and uses it as a
* map, the other allocates for an amount of wsi the lws context is
* expected to use and searches through it to manipulate it.
*/
context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
context->max_fds, "lws_lookup");
if (!context->lws_lookup) {
lwsl_cx_err(context, "OOM on alloc lws_lookup array for %d conn",
context->max_fds);
return 1;
}
#if defined(LWS_WITH_MBEDTLS)
{
int n;
/* initialize platform random through mbedtls */
mbedtls_entropy_init(&context->mec);
mbedtls_ctr_drbg_init(&context->mcdc);
n = mbedtls_ctr_drbg_seed(&context->mcdc, mbedtls_entropy_func,
&context->mec, NULL, 0);
if (n)
lwsl_err("%s: mbedtls_ctr_drbg_seed() returned 0x%x\n",
__func__, n);
#if 0
else {
uint8_t rtest[16];
lwsl_notice("%s: started drbg\n", __func__);
if (mbedtls_ctr_drbg_random(&context->mcdc, rtest,
sizeof(rtest)))
lwsl_err("%s: get random failed\n", __func__);
else
lwsl_hexdump_notice(rtest, sizeof(rtest));
}
#endif
}
#endif
lwsl_cx_info(context, " mem: platform fd map: %5lu B",
(unsigned long)(sizeof(struct lws *) * context->max_fds));
#endif
#if defined(LWS_WITH_FILE_OPS)
fd = lws_open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
#else
fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
#endif
context->fd_random = fd;
if (context->fd_random < 0) {
lwsl_err("Unable to open random device %s %d, errno %d\n",
SYSTEM_RANDOM_FILEPATH, context->fd_random, errno);
return 1;
}
#if defined(LWS_HAVE_SSL_CTX_set_keylog_callback) && !defined(__COVERITY__) && \
defined(LWS_WITH_TLS) && defined(LWS_WITH_CLIENT)
{
char *klf_env = getenv("SSLKEYLOGFILE");
size_t n = 0;
/* ... coverity taint with lws_strncpy()... */
while (klf_env && klf_env[n] &&
n < sizeof(context->keylog_file) - 1) {
context->keylog_file[n] = klf_env[n];
n++;
}
context->keylog_file[n] = '\0';
}
#endif
#if defined(LWS_WITH_PLUGINS) && !defined(LWS_WITH_PLUGINS_BUILTIN)
{
char *ld_env = getenv("LD_LIBRARY_PATH");
if (ld_env) {
const char *pp[2] = { ld_env, NULL };
lws_plugins_init(&context->plugin_list, pp,
"lws_protocol_plugin", NULL,
protocol_plugin_cb, context);
}
if (info->plugin_dirs)
lws_plugins_init(&context->plugin_list,
info->plugin_dirs,
"lws_protocol_plugin", NULL,
protocol_plugin_cb, context);
}
#endif
#if defined(LWS_BUILTIN_PLUGIN_NAMES) && defined(LWS_WITH_PLUGINS)
lws_plugins_handle_builtin(&context->plugin_list,
protocol_plugin_cb, context);
#endif
#if defined(LWS_WITH_NETWORK)
/* we only need to do this on pt[0] */
context->pt[0].sul_plat.cb = lws_sul_plat_unix;
__lws_sul_insert_us(&context->pt[0].pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
&context->pt[0].sul_plat, 30 * LWS_US_PER_SEC);
#endif
return 0;
}
int
lws_plat_context_early_init(void)
{
#if !defined(LWS_AVOID_SIGPIPE_IGN)
signal(SIGPIPE, SIG_IGN);
#endif
return 0;
}
void
lws_plat_context_early_destroy(struct lws_context *context)
{
}
void
lws_plat_context_late_destroy(struct lws_context *context)
{
#if defined(LWS_WITH_PLUGINS)
if (context->plugin_list)
lws_plugins_destroy(&context->plugin_list, NULL, NULL);
#endif
#if defined(LWS_WITH_NETWORK)
if (context->lws_lookup)
lws_free_set_NULL(context->lws_lookup);
#endif
if (!context->fd_random)
lwsl_err("ZERO RANDOM FD\n");
if (context->fd_random != LWS_INVALID_FILE)
close(context->fd_random);
#if defined(LWS_WITH_MBEDTLS)
mbedtls_entropy_free(&context->mec);
mbedtls_ctr_drbg_free(&context->mcdc);
#endif
}

View File

@ -0,0 +1,142 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
/*
* Normally you don't want this, use lws_sul instead inside the event loop.
* But sometimes for drivers it makes sense, so there's an internal-only
* crossplatform api for it.
*/
void
lws_msleep(unsigned int ms)
{
usleep((unsigned int)(ms * LWS_US_PER_MS));
}
lws_usec_t
lws_now_usecs(void)
{
#if defined(LWS_HAVE_CLOCK_GETTIME)
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
return 0;
return (((lws_usec_t)ts.tv_sec) * LWS_US_PER_SEC) +
((lws_usec_t)ts.tv_nsec / LWS_NS_PER_US);
#else
struct timeval now;
gettimeofday(&now, NULL);
return (((lws_usec_t)now.tv_sec) * LWS_US_PER_SEC) +
(lws_usec_t)now.tv_usec;
#endif
}
size_t
lws_get_random(struct lws_context *context, void *buf, size_t len)
{
#if defined(__COVERITY__)
memset(buf, 0, len);
return len;
#else
/* coverity[tainted_scalar] */
return (size_t)read(context->fd_random, (char *)buf, len);
#endif
}
void lwsl_emit_syslog(int level, const char *line)
{
int syslog_level = LOG_DEBUG;
switch (level) {
case LLL_ERR:
syslog_level = LOG_ERR;
break;
case LLL_WARN:
syslog_level = LOG_WARNING;
break;
case LLL_NOTICE:
syslog_level = LOG_NOTICE;
break;
case LLL_INFO:
syslog_level = LOG_INFO;
break;
}
syslog(syslog_level, "%s", line);
}
int
lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
size_t len)
{
ssize_t n;
n = write(fd, buf, len);
if (n < 0 || fsync(fd))
return 1;
if (lseek(fd, 0, SEEK_SET) < 0)
return 1;
return (size_t)n != len;
}
int
lws_plat_recommended_rsa_bits(void)
{
return 4096;
}
/*
* Platform-specific ntpclient server configuration
*/
int
lws_plat_ntpclient_config(struct lws_context *context)
{
#if defined(LWS_HAVE_GETENV)
char *ntpsrv = getenv("LWS_NTP_SERVER");
if (ntpsrv && strlen(ntpsrv) < 64) {
lws_system_blob_t *blob = lws_system_get_blob(context,
LWS_SYSBLOB_TYPE_NTP_SERVER, 0);
if (!blob)
return 0;
lws_system_blob_direct_set(blob, (const uint8_t *)ntpsrv,
strlen(ntpsrv));
return 1;
}
#endif
return 0;
}

View File

@ -0,0 +1,103 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
int
lws_plat_pipe_create(struct lws *wsi)
{
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
int n;
#if defined(LWS_HAVE_EVENTFD)
pt->dummy_pipe_fds[0] = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
pt->dummy_pipe_fds[1] = -1;
n = pt->dummy_pipe_fds[0] < 0 ? -1 : 0;
goto set;
#elif defined(LWS_HAVE_PIPE2)
n = pipe2(pt->dummy_pipe_fds, O_NONBLOCK);
#else
n = pipe(pt->dummy_pipe_fds);
#endif
#if defined(LWS_HAVE_EVENTFD)
set:
#endif
if (n >= 0) {
if (fcntl(pt->dummy_pipe_fds[0], F_SETFL, O_NONBLOCK) < 0)
n = -1;
else if (pt->dummy_pipe_fds[1] >= 0) {
if (fcntl(pt->dummy_pipe_fds[1], F_SETFL, O_NONBLOCK) < 0)
n = -1;
}
}
return n;
}
int
lws_plat_pipe_signal(struct lws_context *ctx, int tsi)
{
struct lws_context_per_thread *pt = &ctx->pt[tsi];
#if defined(LWS_HAVE_EVENTFD)
eventfd_t value = 1;
return eventfd_write(pt->dummy_pipe_fds[0], value);
#else
char buf = 0;
int n;
n = (int)write(pt->dummy_pipe_fds[1], &buf, 1);
return n != 1;
#endif
}
void
lws_plat_pipe_close(struct lws *wsi)
{
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
if (pt->dummy_pipe_fds[0] && pt->dummy_pipe_fds[0] != -1) {
close(pt->dummy_pipe_fds[0]);
pt->dummy_pipe_fds[0] = -1;
}
if (pt->dummy_pipe_fds[1] && pt->dummy_pipe_fds[1] != -1) {
close(pt->dummy_pipe_fds[1]);
pt->dummy_pipe_fds[1] = -1;
}
}
int
lws_plat_pipe_is_fd_assocated(struct lws_context *cx, int tsi, lws_sockfd_type fd)
{
struct lws_context_per_thread *pt = &cx->pt[tsi];
return fd == pt->dummy_pipe_fds[0] || fd == pt->dummy_pipe_fds[1];
}

View File

@ -0,0 +1,124 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
#include <pwd.h>
#include <grp.h>
#include <dlfcn.h>
const lws_plugin_header_t *
lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
const char *sofilename, const char *_class,
each_plugin_cb_t each, void *each_user)
{
const lws_plugin_header_t *hdr;
struct lws_plugin *pin;
char sym[96];
void *l;
int m;
if (strlen(sofilename) < 6)
/* [lib]...[.so] */
return NULL;
lwsl_info(" trying %s\n", libpath);
l = dlopen(libpath, RTLD_NOW);
if (!l) {
lwsl_info("%s: Error loading DSO: %s\n", __func__, dlerror());
return NULL;
}
/* we could open it... can we get his export struct? */
m = lws_snprintf(sym, sizeof(sym) - 1, "%s", sofilename);
if (m < 4)
goto bail;
if (!strcmp(&sym[m - 3], ".so"))
sym[m - 3] = '\0'; /* snip the .so */
hdr = (const lws_plugin_header_t *)dlsym(l, sym);
if (!hdr) {
lwsl_info("%s: Failed to get export '%s' from %s: %s\n",
__func__, sym, libpath, dlerror());
goto bail;
}
if (hdr->api_magic != LWS_PLUGIN_API_MAGIC) {
lwsl_info("%s: plugin %s has outdated api %d (vs %d)\n",
__func__, libpath, hdr->api_magic,
LWS_PLUGIN_API_MAGIC);
goto bail;
}
if (strcmp(hdr->lws_build_hash, LWS_BUILD_HASH))
goto bail;
if (strcmp(hdr->_class, _class))
goto bail;
/*
* We don't already have one of these, right?
*/
pin = *pplugin;
while (pin) {
if (!strcmp(pin->hdr->name, hdr->name))
goto bail;
pin = pin->list;
}
/*
* OK let's bring it in
*/
pin = lws_plugin_alloc(pplugin);
if (!pin)
goto bail;
pin->u.l = l;
pin->hdr = hdr;
if (each)
each(pin, each_user);
lwsl_notice(" %s\n", libpath);
return hdr;
bail:
dlclose(l);
return NULL;
}
int
lws_plat_destroy_dl(struct lws_plugin *p)
{
return dlclose(p->u.l);
}

View File

@ -0,0 +1,92 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include "private-lib-async-dns.h"
lws_async_dns_server_check_t
lws_plat_asyncdns_init(struct lws_context *context, lws_async_dns_t *dns)
{
lws_async_dns_server_check_t s = LADNS_CONF_SERVER_SAME;
lws_async_dns_server_t *dsrv;
lws_sockaddr46 sa46t;
lws_tokenize_t ts;
char ads[48], *r;
int fd, ns = 0;
ssize_t n;
r = (char *)context->pt[0].serv_buf;
/* grab the first chunk of /etc/resolv.conf */
fd = open("/etc/resolv.conf", LWS_O_RDONLY);
if (fd < 0)
return LADNS_CONF_SERVER_UNKNOWN;
n = read(fd, r, context->pt_serv_buf_size - 1);
close(fd);
if (n < 0)
return LADNS_CONF_SERVER_UNKNOWN;
r[n] = '\0';
lws_tokenize_init(&ts, r, LWS_TOKENIZE_F_DOT_NONTERM |
LWS_TOKENIZE_F_NO_FLOATS |
LWS_TOKENIZE_F_NO_INTEGERS |
LWS_TOKENIZE_F_MINUS_NONTERM |
LWS_TOKENIZE_F_HASH_COMMENT);
do {
ts.e = (int8_t)lws_tokenize(&ts);
if (ts.e != LWS_TOKZE_TOKEN) {
ns = 0;
continue;
}
if (!ns && !strncmp("nameserver", ts.token, ts.token_len)) {
ns = 1;
continue;
}
if (!ns)
continue;
/* we are a token just after the "nameserver" token */
ns = 0;
if (ts.token_len > (int)sizeof(ads) - 1)
continue;
memcpy(ads, ts.token, ts.token_len);
ads[ts.token_len] = '\0';
if (lws_sa46_parse_numeric_address(ads, &sa46t) < 0)
continue;
dsrv = __lws_async_dns_server_find(dns, &sa46t);
if (!dsrv) {
__lws_async_dns_server_add(dns, &sa46t);
s = LADNS_CONF_SERVER_CHANGED;
}
} while (ts.e > 0);
return s;
}

View File

@ -0,0 +1,236 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
int
lws_poll_listen_fd(struct lws_pollfd *fd)
{
return poll(fd, 1, 0);
}
int
_lws_plat_service_forced_tsi(struct lws_context *context, int tsi)
{
struct lws_context_per_thread *pt = &context->pt[tsi];
int m, n, r;
r = lws_service_flag_pending(context, tsi);
/* any socket with events to service? */
for (n = 0; n < (int)pt->fds_count; n++) {
lws_sockfd_type fd = pt->fds[n].fd;
if (!pt->fds[n].revents)
continue;
m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
if (m < 0) {
lwsl_err("%s: lws_service_fd_tsi returned %d\n",
__func__, m);
return -1;
}
/* if something closed, retry this slot since may have been
* swapped with end fd */
if (m && pt->fds[n].fd != fd)
n--;
}
lws_service_do_ripe_rxflow(pt);
return r;
}
#define LWS_POLL_WAIT_LIMIT 2000000000
int
_lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
{
volatile struct lws_foreign_thread_pollfd *ftp, *next;
volatile struct lws_context_per_thread *vpt;
struct lws_context_per_thread *pt;
lws_usec_t timeout_us, us;
#if defined(LWS_WITH_SYS_METRICS)
lws_usec_t a, b;
#endif
int n;
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
int m;
#endif
/* stay dead once we are dead */
if (!context)
return 1;
#if defined(LWS_WITH_SYS_METRICS)
b =
#endif
us = lws_now_usecs();
pt = &context->pt[tsi];
vpt = (volatile struct lws_context_per_thread *)pt;
if (timeout_ms < 0)
timeout_ms = 0;
else
/* force a default timeout of 23 days */
timeout_ms = LWS_POLL_WAIT_LIMIT;
timeout_us = ((lws_usec_t)timeout_ms) * LWS_US_PER_MS;
if (context->event_loop_ops->run_pt)
context->event_loop_ops->run_pt(context, tsi);
if (!pt->service_tid_detected && context->vhost_list) {
lws_fakewsi_def_plwsa(pt);
lws_fakewsi_prep_plwsa_ctx(context);
pt->service_tid = context->vhost_list->protocols[0].callback(
(struct lws *)plwsa,
LWS_CALLBACK_GET_THREAD_ID,
NULL, NULL, 0);
pt->service_tid_detected = 1;
}
lws_pt_lock(pt, __func__);
/*
* service ripe scheduled events, and limit wait to next expected one
*/
us = __lws_sul_service_ripe(pt->pt_sul_owner, LWS_COUNT_PT_SUL_OWNERS, us);
if (us && us < timeout_us)
/*
* If something wants zero wait, that's OK, but if the next sul
* coming ripe is an interval less than our wait resolution,
* bump it to be the wait resolution.
*/
timeout_us = us < context->us_wait_resolution ?
context->us_wait_resolution : us;
lws_pt_unlock(pt);
/*
* is there anybody with pending stuff that needs service forcing?
*/
if (!lws_service_adjust_timeout(context, 1, tsi))
timeout_us = 0;
/* ensure we don't wrap at 2^31 with poll()'s signed int ms */
timeout_us /= LWS_US_PER_MS; /* ms now */
#if defined(LWS_WITH_SYS_METRICS)
a = lws_now_usecs() - b;
#endif
vpt->inside_poll = 1;
lws_memory_barrier();
n = poll(pt->fds, pt->fds_count, (int)timeout_us /* ms now */ );
vpt->inside_poll = 0;
lws_memory_barrier();
#if defined(LWS_WITH_SYS_METRICS)
b = lws_now_usecs();
#endif
/* Collision will be rare and brief. Spin until it completes */
while (vpt->foreign_spinlock)
;
/*
* At this point we are not inside a foreign thread pollfd
* change, and we have marked ourselves as outside the poll()
* wait. So we are the only guys that can modify the
* lws_foreign_thread_pollfd list on the pt. Drain the list
* and apply the changes to the affected pollfds in the correct
* order.
*/
lws_pt_lock(pt, __func__);
ftp = vpt->foreign_pfd_list;
//lwsl_notice("cleared list %p\n", ftp);
while (ftp) {
struct lws *wsi;
struct lws_pollfd *pfd;
next = ftp->next;
pfd = &vpt->fds[ftp->fd_index];
if (lws_socket_is_valid(pfd->fd)) {
wsi = wsi_from_fd(context, pfd->fd);
if (wsi)
__lws_change_pollfd(wsi, ftp->_and,
ftp->_or);
}
lws_free((void *)ftp);
ftp = next;
}
vpt->foreign_pfd_list = NULL;
lws_memory_barrier();
lws_pt_unlock(pt);
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
m = 0;
#endif
#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
m |= !!pt->ws.rx_draining_ext_list;
#endif
#if defined(LWS_WITH_TLS)
if (pt->context->tls_ops &&
pt->context->tls_ops->fake_POLLIN_for_buffered)
m |= pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
#endif
if (
#if (defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)) || defined(LWS_WITH_TLS)
!m &&
#endif
!n) /* nothing to do */
lws_service_do_ripe_rxflow(pt);
else
if (_lws_plat_service_forced_tsi(context, tsi) < 0)
return -1;
#if defined(LWS_WITH_SYS_METRICS)
lws_metric_event(context->mt_service, METRES_GO,
(u_mt_t) (a + (lws_now_usecs() - b)));
#endif
if (pt->destroy_self) {
lws_context_destroy(pt->context);
return -1;
}
return 0;
}
int
lws_plat_service(struct lws_context *context, int timeout_ms)
{
return _lws_plat_service_tsi(context, timeout_ms, 0);
}

View File

@ -0,0 +1,685 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2023 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
#if defined(LWS_HAVE_LINUX_IPV6_H)
#include <linux/ipv6.h>
#endif
#include <sys/ioctl.h>
#if !defined(LWS_DETECTED_PLAT_IOS)
#include <net/route.h>
#endif
#include <net/if.h>
#include <pwd.h>
#include <grp.h>
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
#include <netinet/ip.h>
int
lws_send_pipe_choked(struct lws *wsi)
{
struct lws_pollfd fds;
struct lws *wsi_eff;
#if !defined(LWS_WITHOUT_EXTENSIONS)
if (wsi->ws && wsi->ws->tx_draining_ext)
return 1;
#endif
#if defined(LWS_WITH_HTTP2)
wsi_eff = lws_get_network_wsi(wsi);
#else
wsi_eff = wsi;
#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 */
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
)
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;
}
int
lws_plat_set_nonblocking(lws_sockfd_type fd)
{
return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
}
int
lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
{
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
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
if (!unix_skt && vhost->ka_time) {
/* 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)
if (!unix_skt && vhost->bind_iface && vhost->iface) {
lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
(socklen_t)strlen(vhost->iface)) < 0) {
lwsl_warn("Failed to bind to device %s\n", vhost->iface);
return 1;
}
}
#endif
/* Disable Nagle */
optval = 1;
#if defined (__sun) || defined(__QNX__) || defined(__NuttX__)
if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
return 1;
#elif !defined(__APPLE__) && \
!defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
!defined(__NetBSD__) && \
!defined(__OpenBSD__) && \
!defined(__HAIKU__)
if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
return 1;
#else
tcp_proto = getprotobyname("TCP");
if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
return 1;
#endif
return lws_plat_set_nonblocking(fd);
}
#if !defined(__NuttX__)
static const int ip_opt_lws_flags[] = {
LCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
, LCCSCF_IP_HIGH_RELIABILITY
, LCCSCF_IP_LOW_COST
#endif
}, ip_opt_val[] = {
IPTOS_LOWDELAY, IPTOS_THROUGHPUT
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
, IPTOS_RELIABILITY
, IPTOS_MINCOST
#endif
};
#if !defined(LWS_WITH_NO_LOGS)
static const char *ip_opt_names[] = {
"LOWDELAY", "THROUGHPUT"
#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
, "RELIABILITY"
, "MINCOST"
#endif
};
#endif
#endif
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);
#if (_LWS_ENABLED_LOGS & LLL_WARN)
int en;
#endif
#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
#if !defined(__APPLE__) && \
!defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
!defined(__NetBSD__) && \
!defined(__OpenBSD__) && \
!defined(__sun) && \
!defined(__HAIKU__) && \
!defined(__CYGWIN__) && \
!defined(__QNX__) && \
!defined(__NuttX__)
/* 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) {
#if (_LWS_ENABLED_LOGS & LLL_WARN)
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
if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) {
optval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval, optlen) < 0) {
#if (_LWS_ENABLED_LOGS & LLL_WARN)
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__);
}
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
}
#if !defined(__NuttX__)
/* array size differs by platform */
for (n = 0; n < (int)LWS_ARRAY_SIZE(ip_opt_lws_flags); n++) {
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]);
}
#endif
return ret;
}
/* cast a struct sockaddr_in6 * into addr for ipv6 */
enum {
IP_SCORE_NONE,
IP_SCORE_NONNATIVE,
IP_SCORE_IPV6_SCOPE_BASE,
/* ipv6 scopes */
IP_SCORE_GLOBAL_NATIVE = 18
};
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;
#if defined(LWS_WITH_IPV6)
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
unsigned long sco = IP_SCORE_NONE;
unsigned long ts;
const uint8_t *p;
#endif
if (getifaddrs(&ifr)) {
lwsl_err("%s: unable to getifaddrs: errno %d\n", __func__, errno);
return LWS_ITOSA_USABLE;
}
for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
if (!ifc->ifa_addr || !ifc->ifa_name)
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 */
if (rc == LWS_ITOSA_NOT_EXIST)
rc = LWS_ITOSA_NOT_USABLE;
continue;
#endif
case AF_INET:
#if defined(LWS_WITH_IPV6)
if (ipv6) {
/* any existing solution is better than this */
if (sco != IP_SCORE_NONE)
break;
sco = IP_SCORE_NONNATIVE;
rc = LWS_ITOSA_USABLE;
/* map IPv4 to IPv6 */
memset((char *)&addr6->sin6_addr, 0,
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));
lwsl_debug("%s: uplevelling ipv4 bind to ipv6\n", __func__);
break;
}
sco = IP_SCORE_GLOBAL_NATIVE;
#endif
rc = LWS_ITOSA_USABLE;
memcpy(addr, (struct sockaddr_in *)ifc->ifa_addr,
sizeof(struct sockaddr_in));
break;
#if defined(LWS_WITH_IPV6)
case AF_INET6:
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;
memcpy(&addr6->sin6_addr,
&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
sizeof(struct in6_addr));
break;
#endif
default:
break;
}
}
freeifaddrs(ifr);
if (rc &&
!lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))
rc = LWS_ITOSA_USABLE;
return rc;
}
const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
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);
}
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
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
size_t n, int fd, const char *iface)
{
#if defined(__linux__)
struct sockaddr_ll sll;
uint16_t *p16 = (uint16_t *)p;
uint32_t ucs = 0;
memcpy(p, canned, canned_len);
p[2] = (uint8_t)(n >> 8);
p[3] = (uint8_t)(n);
while (p16 < (uint16_t *)(p + 20))
ucs = ucs + (uint32_t)(ntohs((uint16_t)(*p16++)));
ucs += ucs >> 16;
ucs ^= 0xffff;
p[10] = (uint8_t)(ucs >> 8);
p[11] = (uint8_t)(ucs);
p[24] = (uint8_t)((n - 20) >> 8);
p[25] = (uint8_t)((n - 20));
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_protocol = (uint32_t)(htons((uint16_t)0x800));
sll.sll_halen = 6;
sll.sll_ifindex = (int)if_nametoindex(iface);
memset(sll.sll_addr, 0xff, 6);
return (int)sendto(fd, p, n, 0, (struct sockaddr *)&sll, sizeof(sll));
#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
lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
{
#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
lws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)
{
#if defined(__linux__)
struct rtentry route;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
memset(&route, 0, sizeof(route));
lws_strncpy(ifr.ifr_name, is->ifname, IFNAMSIZ);
lws_plat_if_up(is->ifname, fd, 0);
memcpy(&ifr.ifr_addr, &is->sa46[LWSDH_SA46_IP], sizeof(struct sockaddr));
if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) {
lwsl_err("%s: SIOCSIFADDR fail\n", __func__);
return 1;
}
if (is->sa46[LWSDH_SA46_IP].sa4.sin_family == AF_INET) {
struct sockaddr_in sin;
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;
}
lws_plat_if_up(is->ifname, fd, 1);
memcpy(&route.rt_gateway,
&is->sa46[LWSDH_SA46_IPV4_ROUTER].sa4,
sizeof(struct sockaddr));
sin.sin_addr.s_addr = 0;
memcpy(&route.rt_dst, &sin, sizeof(struct sockaddr));
memcpy(&route.rt_genmask, &sin, sizeof(struct sockaddr));
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);
return 0;
#else
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
#endif
}
int
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
{
return 0;
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = (int)write(fd, buf, len);
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)
{
int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
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

View File

@ -0,0 +1,615 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "private-lib-core.h"
#include <unistd.h>
#if defined(__OpenBSD__) || defined(__NetBSD__)
#include <sys/resource.h>
#include <sys/wait.h>
#endif
void
lws_spawn_timeout(struct lws_sorted_usec_list *sul)
{
struct lws_spawn_piped *lsp = lws_container_of(sul,
struct lws_spawn_piped, sul);
lwsl_warn("%s: spawn exceeded timeout, killing\n", __func__);
lws_spawn_piped_kill_child_process(lsp);
}
void
lws_spawn_sul_reap(struct lws_sorted_usec_list *sul)
{
struct lws_spawn_piped *lsp = lws_container_of(sul,
struct lws_spawn_piped, sul_reap);
lwsl_notice("%s: reaping spawn after last stdpipe, tries left %d\n",
__func__, lsp->reap_retry_budget);
if (!lws_spawn_reap(lsp) && !lsp->pipes_alive) {
if (--lsp->reap_retry_budget) {
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
&lsp->sul_reap, lws_spawn_sul_reap,
250 * LWS_US_PER_MS);
} else {
lwsl_err("%s: Unable to reap lsp %p, killing\n",
__func__, lsp);
lsp->reap_retry_budget = 20;
lws_spawn_piped_kill_child_process(lsp);
}
}
}
static struct lws *
lws_create_stdwsi(struct lws_context *context, int tsi,
const struct lws_role_ops *ops)
{
struct lws_context_per_thread *pt = &context->pt[tsi];
struct lws *new_wsi;
if (!context->vhost_list)
return NULL;
if ((unsigned int)pt->fds_count == context->fd_limit_per_thread - 1) {
lwsl_err("no space for new conn\n");
return NULL;
}
lws_context_lock(context, __func__);
new_wsi = __lws_wsi_create_with_role(context, tsi, ops, NULL);
lws_context_unlock(context);
if (new_wsi == NULL) {
lwsl_err("Out of memory for new connection\n");
return NULL;
}
new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
/* initialize the instance struct */
lws_role_transition(new_wsi, 0, LRS_ESTABLISHED, ops);
new_wsi->hdr_parsing_completed = 0;
/*
* these can only be set once the protocol is known
* we set an unestablished connection's protocol pointer
* to the start of the defauly vhost supported list, so it can look
* for matching ones during the handshake
*/
new_wsi->user_space = NULL;
return new_wsi;
}
void
lws_spawn_piped_destroy(struct lws_spawn_piped **_lsp)
{
struct lws_spawn_piped *lsp = *_lsp;
int n;
if (!lsp)
return;
lws_dll2_remove(&lsp->dll);
lws_sul_cancel(&lsp->sul);
lws_sul_cancel(&lsp->sul_reap);
for (n = 0; n < 3; n++) {
#if 0
if (lsp->pipe_fds[n][!!(n == 0)] == 0)
lwsl_err("ZERO FD IN CGI CLOSE");
if (lsp->pipe_fds[n][!!(n == 0)] >= 0) {
close(lsp->pipe_fds[n][!!(n == 0)]);
lsp->pipe_fds[n][!!(n == 0)] = LWS_SOCK_INVALID;
}
#endif
if (lsp->stdwsi[n]) {
lws_set_timeout(lsp->stdwsi[n], 1, LWS_TO_KILL_ASYNC);
lsp->stdwsi[n] = NULL;
}
}
lws_free_set_NULL((*_lsp));
}
int
lws_spawn_reap(struct lws_spawn_piped *lsp)
{
long hz = sysconf(_SC_CLK_TCK); /* accounting Hz */
void *opaque = lsp->info.opaque;
lsp_cb_t cb = lsp->info.reap_cb;
struct lws_spawn_piped temp;
struct tms tms;
#if defined(__OpenBSD__) || defined(__NetBSD__)
struct rusage rusa;
int status;
#endif
int n;
if (lsp->child_pid < 1)
return 0;
/* check if exited, do not reap yet */
memset(&lsp->si, 0, sizeof(lsp->si));
#if defined(__OpenBSD__) || defined(__NetBSD__)
n = wait4(lsp->child_pid, &status, WNOHANG, &rusa);
if (!n)
return 0;
lsp->si.si_code = WIFEXITED(status);
#else
n = waitid(P_PID, (id_t)lsp->child_pid, &lsp->si, WEXITED | WNOHANG | WNOWAIT);
#endif
if (n < 0) {
lwsl_info("%s: child %d still running\n", __func__, lsp->child_pid);
return 0;
}
if (!lsp->si.si_code)
return 0;
/* his process has exited... */
if (!lsp->reaped) {
/* mark the earliest time we knew he had gone */
lsp->reaped = lws_now_usecs();
/*
* Switch the timeout to restrict the amount of grace time
* to drain stdwsi
*/
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
&lsp->sul, lws_spawn_timeout,
5 * LWS_US_PER_SEC);
}
/*
* Stage finalizing our reaction to the process going down until the
* stdwsi flushed whatever is in flight and all noticed they were
* closed. For that reason, each stdwsi close must call lws_spawn_reap
* to check if that was the last one and we can proceed with the reap.
*/
if (!lsp->ungraceful && lsp->pipes_alive) {
lwsl_info("%s: %d stdwsi alive, not reaping\n", __func__,
lsp->pipes_alive);
return 0;
}
/* we reached the reap point, no need for timeout wait */
lws_sul_cancel(&lsp->sul);
/*
* All the stdwsi went down, nothing more is coming... it's over
* Collect the final information and then reap the dead process
*/
if (times(&tms) != (clock_t) -1) {
/*
* Cpu accounting in us
*/
lsp->accounting[0] = (lws_usec_t)((uint64_t)tms.tms_cstime * 1000000) / hz;
lsp->accounting[1] = (lws_usec_t)((uint64_t)tms.tms_cutime * 1000000) / hz;
lsp->accounting[2] = (lws_usec_t)((uint64_t)tms.tms_stime * 1000000) / hz;
lsp->accounting[3] = (lws_usec_t)((uint64_t)tms.tms_utime * 1000000) / hz;
}
temp = *lsp;
#if defined(__OpenBSD__) || defined(__NetBSD__)
n = wait4(lsp->child_pid, &status, WNOHANG, &rusa);
if (!n)
return 0;
lsp->si.si_code = WIFEXITED(status);
if (lsp->si.si_code == CLD_EXITED)
temp.si.si_code = CLD_EXITED;
temp.si.si_status = WEXITSTATUS(status);
#else
n = waitid(P_PID, (id_t)lsp->child_pid, &temp.si, WEXITED | WNOHANG);
#endif
temp.si.si_status &= 0xff; /* we use b8 + for flags */
lwsl_info("%s: waitd says %d, process exit %d\n",
__func__, n, temp.si.si_status);
lsp->child_pid = -1;
/* destroy the lsp itself first (it's freed and plsp set NULL */
if (lsp->info.plsp)
lws_spawn_piped_destroy(lsp->info.plsp);
/* then do the parent callback informing it's destroyed */
if (cb)
cb(opaque, temp.accounting, &temp.si,
temp.we_killed_him_timeout |
(temp.we_killed_him_spew << 1));
return 1; /* was reaped */
}
int
lws_spawn_piped_kill_child_process(struct lws_spawn_piped *lsp)
{
int status, n;
if (lsp->child_pid <= 0)
return 1;
lsp->ungraceful = 1; /* don't wait for flushing, just kill it */
if (lws_spawn_reap(lsp))
/* that may have invalidated lsp */
return 0;
/* kill the process group */
n = kill(-lsp->child_pid, SIGTERM);
lwsl_debug("%s: SIGTERM child PID %d says %d (errno %d)\n", __func__,
lsp->child_pid, n, errno);
if (n < 0) {
/*
* hum seen errno=3 when process is listed in ps,
* it seems we don't always retain process grouping
*
* Direct these fallback attempt to the exact child
*/
n = kill(lsp->child_pid, SIGTERM);
if (n < 0) {
n = kill(lsp->child_pid, SIGPIPE);
if (n < 0) {
n = kill(lsp->child_pid, SIGKILL);
if (n < 0)
lwsl_info("%s: SIGKILL PID %d "
"failed errno %d "
"(maybe zombie)\n", __func__,
lsp->child_pid, errno);
}
}
}
/* He could be unkillable because he's a zombie */
n = 1;
while (n > 0) {
n = waitpid(-lsp->child_pid, &status, WNOHANG);
if (n > 0)
lwsl_debug("%s: reaped PID %d\n", __func__, n);
if (n <= 0) {
n = waitpid(lsp->child_pid, &status, WNOHANG);
if (n > 0)
lwsl_debug("%s: reaped PID %d\n", __func__, n);
}
}
lws_spawn_reap(lsp);
/* that may have invalidated lsp */
return 0;
}
/*
* Deals with spawning a subprocess and executing it securely with stdin/out/err
* diverted into pipes
*/
struct lws_spawn_piped *
lws_spawn_piped(const struct lws_spawn_piped_info *i)
{
const struct lws_protocols *pcol = i->vh->context->vhost_list->protocols;
struct lws_context *context = i->vh->context;
struct lws_spawn_piped *lsp;
const char *wd;
int n, m;
if (i->protocol_name)
pcol = lws_vhost_name_to_protocol(i->vh, i->protocol_name);
if (!pcol) {
lwsl_err("%s: unknown protocol %s\n", __func__,
i->protocol_name ? i->protocol_name : "default");
return NULL;
}
lsp = lws_zalloc(sizeof(*lsp), __func__);
if (!lsp)
return NULL;
/* wholesale take a copy of info */
lsp->info = *i;
lsp->reap_retry_budget = 20;
/*
* Prepare the stdin / out / err pipes
*/
for (n = 0; n < 3; n++) {
lsp->pipe_fds[n][0] = -1;
lsp->pipe_fds[n][1] = -1;
}
/* create pipes for [stdin|stdout] and [stderr] */
for (n = 0; n < 3; n++) {
if (pipe(lsp->pipe_fds[n]) == -1)
goto bail1;
lws_plat_apply_FD_CLOEXEC(lsp->pipe_fds[n][n == 0]);
}
/*
* At this point, we have 6 pipe fds open on lws side and no wsis
* bound to them
*/
/* create wsis for each stdin/out/err fd */
for (n = 0; n < 3; n++) {
lsp->stdwsi[n] = lws_create_stdwsi(i->vh->context, i->tsi,
i->ops ? i->ops : &role_ops_raw_file);
if (!lsp->stdwsi[n]) {
lwsl_err("%s: unable to create lsp stdwsi\n", __func__);
goto bail2;
}
__lws_lc_tag(i->vh->context, &i->vh->context->lcg[LWSLCG_WSI],
&lsp->stdwsi[n]->lc, "nspawn-stdwsi-%d", n);
lsp->stdwsi[n]->lsp_channel = (uint8_t)n;
lws_vhost_bind_wsi(i->vh, lsp->stdwsi[n]);
lsp->stdwsi[n]->a.protocol = pcol;
lsp->stdwsi[n]->a.opaque_user_data = i->opaque;
lwsl_debug("%s: lsp stdwsi %p: pipe idx %d -> fd %d / %d\n", __func__,
lsp->stdwsi[n], n, lsp->pipe_fds[n][n == 0],
lsp->pipe_fds[n][n != 0]);
/* read side is 0, stdin we want the write side, others read */
lsp->stdwsi[n]->desc.sockfd = lsp->pipe_fds[n][n == 0];
if (fcntl(lsp->pipe_fds[n][n == 0], F_SETFL, O_NONBLOCK) < 0) {
lwsl_err("%s: setting NONBLOCK failed\n", __func__);
goto bail2;
}
/*
* We have bound 3 x pipe fds to wsis, wr side of stdin and rd
* side of stdout / stderr... those are marked CLOEXEC so they
* won't go through the fork
*
* rd side of stdin and wr side of stdout / stderr are open but
* not bound to anything on lws side.
*/
}
/*
* Stitch the wsi fd into the poll wait
*/
for (n = 0; n < 3; n++) {
if (context->event_loop_ops->sock_accept)
if (context->event_loop_ops->sock_accept(lsp->stdwsi[n]))
goto bail3;
if (__insert_wsi_socket_into_fds(context, lsp->stdwsi[n]))
goto bail3;
if (i->opt_parent) {
lsp->stdwsi[n]->parent = i->opt_parent;
lsp->stdwsi[n]->sibling_list = i->opt_parent->child_list;
i->opt_parent->child_list = lsp->stdwsi[n];
}
}
if (lws_change_pollfd(lsp->stdwsi[LWS_STDIN], LWS_POLLIN, LWS_POLLOUT))
goto bail3;
if (lws_change_pollfd(lsp->stdwsi[LWS_STDOUT], LWS_POLLOUT, LWS_POLLIN))
goto bail3;
if (lws_change_pollfd(lsp->stdwsi[LWS_STDERR], LWS_POLLOUT, LWS_POLLIN))
goto bail3;
lwsl_info("%s: fds in %d, out %d, err %d\n", __func__,
lsp->stdwsi[LWS_STDIN]->desc.sockfd,
lsp->stdwsi[LWS_STDOUT]->desc.sockfd,
lsp->stdwsi[LWS_STDERR]->desc.sockfd);
/* we are ready with the redirection pipes... do the (v)fork */
#if defined(__sun) || !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
lsp->child_pid = fork();
#else
lsp->child_pid = vfork();
#endif
if (lsp->child_pid < 0) {
lwsl_err("%s: fork failed, errno %d", __func__, errno);
goto bail3;
}
#if defined(__linux__)
if (!lsp->child_pid)
prctl(PR_SET_PDEATHSIG, SIGTERM);
#endif
if (lsp->info.disable_ctrlc)
/* stops non-daemonized main processess getting SIGINT
* from TTY */
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
setpgid(0, 0);
#else
setpgrp();
#endif
if (lsp->child_pid) {
/*
* We are the parent process. We can close our copy of the
* "other" side of the pipe fds, ie, rd for stdin and wr for
* stdout / stderr.
*/
for (n = 0; n < 3; n++)
/* these guys didn't have any wsi footprint */
close(lsp->pipe_fds[n][n != 0]);
lsp->pipes_alive = 3;
lsp->created = lws_now_usecs();
lwsl_info("%s: lsp %p spawned PID %d\n", __func__, lsp,
lsp->child_pid);
lws_sul_schedule(context, i->tsi, &lsp->sul, lws_spawn_timeout,
i->timeout_us ? i->timeout_us :
300 * LWS_US_PER_SEC);
if (i->owner)
lws_dll2_add_head(&lsp->dll, i->owner);
if (i->timeout_us)
lws_sul_schedule(context, i->tsi, &lsp->sul,
lws_spawn_timeout, i->timeout_us);
return lsp;
}
/*
* We are the forked process, redirect and kill inherited things.
*
* Because of vfork(), we cannot do anything that changes pages in
* the parent environment. Stuff that changes kernel state for the
* process is OK. Stuff that happens after the execvpe() is OK.
*/
if (i->chroot_path && chroot(i->chroot_path)) {
lwsl_err("%s: child chroot %s failed, errno %d\n",
__func__, i->chroot_path, errno);
exit(2);
}
/* cwd: somewhere we can at least read things and enter it */
wd = i->wd;
if (!wd)
wd = "/tmp";
if (chdir(wd))
lwsl_notice("%s: Failed to cd to %s\n", __func__, wd);
/*
* Bind the child's stdin / out / err to its side of our pipes
*/
for (m = 0; m < 3; m++) {
if (dup2(lsp->pipe_fds[m][m != 0], m) < 0) {
lwsl_err("%s: stdin dup2 failed\n", __func__);
goto bail3;
}
/*
* CLOEXEC on the lws-side of the pipe fds should have already
* dealt with closing those for the child perspective.
*
* Now it has done the dup, the child should close its original
* copies of its side of the pipes.
*/
close(lsp->pipe_fds[m][m != 0]);
}
#if defined(__sun) || !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
#if defined(__linux__) || defined(__APPLE__) || defined(__sun)
m = 0;
while (i->env_array[m]){
const char *p = strchr(i->env_array[m], '=');
int naml = lws_ptr_diff(p, i->env_array[m]);
char enam[32];
if (p) {
lws_strnncpy(enam, i->env_array[m], naml, sizeof(enam));
setenv(enam, p + 1, 1);
}
m++;
}
#endif
execvp(i->exec_array[0], (char * const *)&i->exec_array[0]);
#else
execvpe(i->exec_array[0], (char * const *)&i->exec_array[0],
(char **)&i->env_array[0]);
#endif
lwsl_err("%s: child exec of %s failed %d\n", __func__, i->exec_array[0],
LWS_ERRNO);
_exit(1);
bail3:
while (--n >= 0)
__remove_wsi_socket_from_fds(lsp->stdwsi[n]);
bail2:
for (n = 0; n < 3; n++)
if (lsp->stdwsi[n])
__lws_free_wsi(lsp->stdwsi[n]);
bail1:
for (n = 0; n < 3; n++) {
if (lsp->pipe_fds[n][0] >= 0)
close(lsp->pipe_fds[n][0]);
if (lsp->pipe_fds[n][1] >= 0)
close(lsp->pipe_fds[n][1]);
}
lws_free(lsp);
lwsl_err("%s: failed\n", __func__);
return NULL;
}
void
lws_spawn_stdwsi_closed(struct lws_spawn_piped *lsp, struct lws *wsi)
{
int n;
assert(lsp);
lsp->pipes_alive--;
lwsl_debug("%s: pipes alive %d\n", __func__, lsp->pipes_alive);
if (!lsp->pipes_alive)
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
&lsp->sul_reap, lws_spawn_sul_reap, 1);
for (n = 0; n < 3; n++)
if (lsp->stdwsi[n] == wsi)
lsp->stdwsi[n] = NULL;
}
int
lws_spawn_get_stdfd(struct lws *wsi)
{
return wsi->lsp_channel;
}

View File

@ -0,0 +1,85 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2024 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <sys/socket.h>
#include <systemd/sd-daemon.h>
#include "private-lib-core.h"
int
lws_systemd_inherited_fd(unsigned int index,
struct lws_context_creation_info *info)
{
unsigned int inherited = (unsigned int)sd_listen_fds(0);
if (index >= inherited)
return -1;
info->vh_listen_sockfd = (int)(SD_LISTEN_FDS_START + index);
if (sd_is_socket_unix(info->vh_listen_sockfd, 0, 0, NULL, 0)) {
info->options |= LWS_SERVER_OPTION_UNIX_SOCK;
info->port = 0;
}
if (sd_is_socket_inet(info->vh_listen_sockfd, AF_UNSPEC, 0, 1, 0)) {
struct sockaddr_storage addr;
socklen_t addrlen = sizeof(addr);
if (getsockname(info->vh_listen_sockfd,
(struct sockaddr *)&addr, &addrlen)) {
lwsl_err("%s: getsockname failed for fd %d\n",
__func__, info->vh_listen_sockfd);
return -1;
}
switch (((struct sockaddr *)&addr)->sa_family) {
case AF_INET:
info->port = ntohs(((struct sockaddr_in *)&addr)->sin_port);
lwsl_info("%s: inet socket %d\n", __func__, info->port);
break;
case AF_INET6:
info->port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
lwsl_info("%s: inet6 socket %d\n", __func__, info->port);
break;
}
if (sd_is_socket_inet(info->vh_listen_sockfd, AF_INET6, 0, 1, 0))
info->options |= LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY |
LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE;
if (sd_is_socket_inet(info->vh_listen_sockfd, AF_INET, 0, 1, 0)) {
info->options &= (uint64_t)~(LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY |
LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE);
info->options |= LWS_SERVER_OPTION_DISABLE_IPV6;
}
}
return 0;
}

View File

@ -0,0 +1,103 @@
#
# libwebsockets - small server side websockets and web server implementation
#
# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
#
# 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:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# 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.
#
# The strategy is to only export to PARENT_SCOPE
#
# - changes to LIB_LIST
# - changes to SOURCES
# - includes via include_directories
#
# and keep everything else private
include_directories(.)
list(APPEND SOURCES
plat/windows/windows-fds.c
plat/windows/windows-file.c
plat/windows/windows-init.c
plat/windows/windows-misc.c
plat/windows/windows-pipe.c
plat/windows/windows-plugins.c
plat/windows/windows-service.c
plat/windows/windows-sockets.c
)
if (LWS_WITH_SYS_ASYNC_DNS)
list(APPEND SOURCES plat/windows/windows-resolv.c)
endif()
if (LWS_WITH_SPAWN)
list(APPEND SOURCES plat/windows/windows-spawn.c)
endif()
if (LWS_WITH_ZLIB AND LWS_WITH_BUNDLED_ZLIB)
set(WIN32_ZLIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../win32port/zlib")
set(ZLIB_SRCS
${WIN32_ZLIB_PATH}/adler32.c
${WIN32_ZLIB_PATH}/compress.c
${WIN32_ZLIB_PATH}/crc32.c
${WIN32_ZLIB_PATH}/deflate.c
${WIN32_ZLIB_PATH}/gzlib.c
${WIN32_ZLIB_PATH}/gzread.c
${WIN32_ZLIB_PATH}/gzwrite.c
${WIN32_ZLIB_PATH}/infback.c
${WIN32_ZLIB_PATH}/inffast.c
${WIN32_ZLIB_PATH}/inflate.c
${WIN32_ZLIB_PATH}/inftrees.c
${WIN32_ZLIB_PATH}/trees.c
${WIN32_ZLIB_PATH}/uncompr.c
${WIN32_ZLIB_PATH}/zutil.c)
add_library(zlib_internal STATIC ${ZLIB_SRCS})
set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
set(ZLIB_LIBRARIES "")
set(ZLIB_FOUND 1)
# Make sure zlib_internal is compiled before the libs.
foreach (lib ${LWS_LIBRARIES})
add_dependencies(${lib} zlib_internal)
endforeach()
endif()
# Add helper files for Windows
# (from ./lib perspective)
set(WIN32_HELPERS_PATH ../win32port/win32helpers)
# from our perspective in ./lib/plat/windows
include_directories(../../${WIN32_HELPERS_PATH} ../../${WIN32_HELPERS_PATH}/zlib)
list(APPEND SOURCES
${WIN32_HELPERS_PATH}/gettimeofday.c
)
list(APPEND HDR_PRIVATE
${WIN32_HELPERS_PATH}/gettimeofday.h
)
#
# Keep explicit parent scope exports at end
#
exports_to_parent_scope()
set(WIN32_HELPERS_PATH ${WIN32_HELPERS_PATH} PARENT_SCOPE)
set(HDR_PRIVATE ${HDR_PRIVATE} PARENT_SCOPE)
set(ZLIB_FOUND ${ZLIB_FOUND} PARENT_SCOPE)
set(LIB_LIST_AT_END ${LIB_LIST_AT_END} PARENT_SCOPE)

View File

@ -0,0 +1,172 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
* Included from lib/private-lib-core.h if defined(WIN32) || defined(_WIN32)
*/
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#if defined(WINVER) && (WINVER < 0x0501)
#undef WINVER
#undef _WIN32_WINNT
#define WINVER 0x0501
#define _WIN32_WINNT WINVER
#endif
#define LWS_NO_DAEMONIZE
#define LWS_ERRNO WSAGetLastError()
#define LWS_EAGAIN WSAEWOULDBLOCK
#define LWS_EALREADY WSAEALREADY
#define LWS_EINPROGRESS WSAEINPROGRESS
#define LWS_EINTR WSAEINTR
#define LWS_EISCONN WSAEISCONN
#define LWS_ENOTCONN WSAENOTCONN
#define LWS_EWOULDBLOCK WSAEWOULDBLOCK
#define LWS_EADDRINUSE WSAEADDRINUSE
#define MSG_NOSIGNAL 0
#define SHUT_RDWR SD_BOTH
#define SOL_TCP IPPROTO_TCP
#define SHUT_WR SD_SEND
#define compatible_close(fd) closesocket(fd)
#define compatible_file_close(fd) CloseHandle(fd)
#define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <tchar.h>
#ifdef LWS_HAVE_IN6ADDR_H
#include <in6addr.h>
#endif
#include <mstcpip.h>
#include <io.h>
#if defined(LWS_WITH_UNIX_SOCK)
#include <afunix.h>
#endif
#if defined(LWS_WITH_TLS)
#include <wincrypt.h>
#endif
#if defined(LWS_HAVE_PTHREAD_H)
#define lws_mutex_t pthread_mutex_t
#define lws_mutex_init(x) pthread_mutex_init(&(x), NULL)
#define lws_mutex_destroy(x) pthread_mutex_destroy(&(x))
#define lws_mutex_lock(x) pthread_mutex_lock(&(x))
#define lws_mutex_unlock(x) pthread_mutex_unlock(&(x))
#endif
#if !defined(LWS_HAVE_ATOLL)
#if defined(LWS_HAVE__ATOI64)
#define atoll _atoi64
#else
#warning No atoll or _atoi64 available, using atoi
#define atoll atoi
#endif
#endif
#ifndef __func__
#define __func__ __FUNCTION__
#endif
#ifdef LWS_HAVE__VSNPRINTF
#define vsnprintf _vsnprintf
#endif
/* we don't have an implementation for this on windows... */
int kill(int pid, int sig);
int fork(void);
#ifndef SIGINT
#define SIGINT 2
#endif
#include <gettimeofday.h>
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
#endif
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN 1234
#endif
#ifndef BYTE_ORDER
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#undef __P
#ifndef __P
#if __STDC__
#define __P(protos) protos
#else
#define __P(protos) ()
#endif
#endif
#ifdef _WIN32
#ifndef FD_HASHTABLE_MODULUS
#define FD_HASHTABLE_MODULUS 32
#endif
#endif
#define lws_plat_socket_offset() (0)
struct lws;
struct lws_context;
#define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
struct lws_fd_hashtable {
struct lws **wsi;
int length;
};
#if !defined(LWS_EXTERN)
#ifdef LWS_DLL
#ifdef LWS_INTERNAL
#define LWS_EXTERN extern __declspec(dllexport)
#else
#define LWS_EXTERN extern __declspec(dllimport)
#endif
#else
#define LWS_EXTERN
#endif
#endif
typedef SOCKET lws_sockfd_type;
#if defined(__MINGW32__)
typedef int lws_filefd_type;
#else
typedef HANDLE lws_filefd_type;
#endif
#define LWS_WIN32_HANDLE_TYPES
LWS_EXTERN struct lws *
wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
LWS_EXTERN int
insert_wsi(struct lws_context *context, struct lws *wsi);
LWS_EXTERN int
delete_from_fd(struct lws_context *context, lws_sockfd_type fd);

View File

@ -0,0 +1,79 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include "private-lib-core.h"
struct lws *
wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
{
int h = LWS_FD_HASH(fd);
int n = 0;
for (n = 0; n < context->fd_hashtable[h].length; n++)
if (context->fd_hashtable[h].wsi[n]->desc.sockfd == fd)
return context->fd_hashtable[h].wsi[n];
return NULL;
}
int
insert_wsi(struct lws_context *context, struct lws *wsi)
{
int h = LWS_FD_HASH(wsi->desc.sockfd);
if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
lwsl_err("hash table overflow\n");
return 1;
}
context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
return 0;
}
int
delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
{
int h = LWS_FD_HASH(fd);
int n = 0;
for (n = 0; n < context->fd_hashtable[h].length; n++)
if (context->fd_hashtable[h].wsi[n]->desc.sockfd == fd) {
while (n < context->fd_hashtable[h].length) {
context->fd_hashtable[h].wsi[n] =
context->fd_hashtable[h].wsi[n + 1];
n++;
}
context->fd_hashtable[h].length--;
return 0;
}
lwsl_debug("Failed to find fd %d requested for "
"delete in hashtable\n", fd);
return 1;
}

View File

@ -0,0 +1,197 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include "private-lib-core.h"
int lws_plat_apply_FD_CLOEXEC(int n)
{
return 0;
}
lws_fop_fd_t
_lws_plat_file_open(const struct lws_plat_file_ops *fops_own,
const struct lws_plat_file_ops *fops, const char *filename,
const char *vpath, lws_fop_flags_t *flags)
{
HANDLE ret;
WCHAR buf[MAX_PATH];
lws_fop_fd_t fop_fd;
LARGE_INTEGER llFileSize = {0};
MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, LWS_ARRAY_SIZE(buf));
if (((*flags) & 7) == _O_RDONLY)
ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
else
ret = CreateFileW(buf, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (ret == INVALID_HANDLE_VALUE)
goto bail;
fop_fd = malloc(sizeof(*fop_fd));
if (!fop_fd)
goto bail;
fop_fd->fops = fops;
#if defined(__MINGW32__)
/* we use filesystem_priv */
fop_fd->fd = (int)(intptr_t)ret;
#else
fop_fd->fd = ret;
#endif
fop_fd->filesystem_priv = ret;
fop_fd->flags = *flags;
fop_fd->len = GetFileSize(ret, NULL);
if(GetFileSizeEx(ret, &llFileSize))
fop_fd->len = llFileSize.QuadPart;
fop_fd->pos = 0;
return fop_fd;
bail:
return NULL;
}
int
_lws_plat_file_close(lws_fop_fd_t *fop_fd)
{
HANDLE fd = (*fop_fd)->filesystem_priv;
free(*fop_fd);
*fop_fd = NULL;
CloseHandle((HANDLE)fd);
return 0;
}
lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
{
LARGE_INTEGER l;
l.QuadPart = offset;
if (!SetFilePointerEx((HANDLE)fop_fd->filesystem_priv, l, NULL, FILE_CURRENT))
{
lwsl_err("error seeking from cur %ld, offset %ld\n", (long)fop_fd->pos, (long)offset);
return -1;
}
LARGE_INTEGER zero;
zero.QuadPart = 0;
LARGE_INTEGER newPos;
if (!SetFilePointerEx((HANDLE)fop_fd->filesystem_priv, zero, &newPos, FILE_CURRENT))
{
lwsl_err("error seeking from cur %ld, offset %ld\n", (long)fop_fd->pos, (long)offset);
return -1;
}
fop_fd->pos = newPos.QuadPart;
return newPos.QuadPart;
}
int
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t *buf, lws_filepos_t len)
{
DWORD _amount;
if (!ReadFile((HANDLE)fop_fd->filesystem_priv, buf, (DWORD)len, &_amount, NULL)) {
*amount = 0;
return 1;
}
fop_fd->pos += _amount;
*amount = (unsigned long)_amount;
return 0;
}
int
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
uint8_t* buf, lws_filepos_t len)
{
DWORD _amount;
if (!WriteFile((HANDLE)fop_fd->filesystem_priv, buf, (DWORD)len, &_amount, NULL)) {
*amount = 0;
return 1;
}
fop_fd->pos += _amount;
*amount = (unsigned long)_amount;
return 0;
}
int
lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
size_t len)
{
int n;
n = (int)write(fd, buf, (unsigned int)len);
lseek(fd, 0, SEEK_SET);
return (size_t)n != len;
}
int
lws_plat_write_file(const char *filename, void *buf, size_t len)
{
int m, fd;
fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd == -1)
return -1;
m = (int)write(fd, buf, (unsigned int)len);
close(fd);
return (size_t)m != len;
}
int
lws_plat_read_file(const char *filename, void *buf, size_t len)
{
int n, fd = lws_open(filename, O_RDONLY);
if (fd == -1)
return -1;
n = (int)read(fd, buf, (unsigned int)len);
close(fd);
return n;
}

View File

@ -0,0 +1,172 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include "private-lib-core.h"
int
lws_plat_drop_app_privileges(struct lws_context *context, int actually_set)
{
return 0;
}
int
lws_plat_context_early_init(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (!err)
return 0;
/*
* Tell the user that we could not find a usable
* Winsock DLL
*/
lwsl_err("WSAStartup failed with error: %d\n", err);
return 1;
}
#if defined(LWS_WITH_PLUGINS)
static int
protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
{
struct lws_context *context = (struct lws_context *)each_user;
const lws_plugin_protocol_t *plpr =
(const lws_plugin_protocol_t *)pin->hdr;
context->plugin_protocol_count += plpr->count_protocols;
context->plugin_extension_count += plpr->count_extensions;
return 0;
}
#endif
int
lws_plat_init(struct lws_context *context,
const struct lws_context_creation_info *info)
{
struct lws_context_per_thread *pt = &context->pt[0];
int i, n = context->count_threads;
#if defined(LWS_WITH_MBEDTLS)
{
int n;
/* initialize platform random through mbedtls */
mbedtls_entropy_init(&context->mec);
mbedtls_ctr_drbg_init(&context->mcdc);
n = mbedtls_ctr_drbg_seed(&context->mcdc, mbedtls_entropy_func,
&context->mec, NULL, 0);
if (n)
lwsl_err("%s: mbedtls_ctr_drbg_seed() returned 0x%x\n",
__func__, n);
#if 0
else {
uint8_t rtest[16];
lwsl_notice("%s: started drbg\n", __func__);
if (mbedtls_ctr_drbg_random(&context->mcdc, rtest,
sizeof(rtest)))
lwsl_err("%s: get random failed\n", __func__);
else
lwsl_hexdump_notice(rtest, sizeof(rtest));
}
#endif
}
#endif
#if defined(LWS_HAVE_SSL_CTX_set_keylog_callback) && \
defined(LWS_WITH_TLS) && defined(LWS_WITH_CLIENT)
{
char *klf_env = getenv("SSLKEYLOGFILE");
if (klf_env)
lws_strncpy(context->keylog_file, klf_env,
sizeof(context->keylog_file));
}
#endif
for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
context->fd_hashtable[i].wsi =
lws_zalloc(sizeof(struct lws*) * context->max_fds,
"win hashtable");
if (!context->fd_hashtable[i].wsi)
return -1;
}
while (n--) {
pt->fds_count = 0;
pt++;
}
context->fd_random = 0;
#if defined(LWS_WITH_PLUGINS) && !defined(LWS_WITH_PLUGINS_BUILTIN)
if (info->plugin_dirs)
lws_plat_plugins_init(&context->plugin_list, info->plugin_dirs,
"lws_protocol_plugin",
protocol_plugin_cb, context);
#endif
#if defined(LWS_BUILTIN_PLUGIN_NAMES)
lws_plugins_handle_builtin(&context->plugin_list,
protocol_plugin_cb, context);
#endif
return 0;
}
void
lws_plat_context_early_destroy(struct lws_context *context)
{
}
void
lws_plat_context_late_destroy(struct lws_context *context)
{
int n;
#ifdef LWS_WITH_PLUGINS
if (context->plugin_list)
lws_plugins_destroy(&context->plugin_list, NULL, NULL);
#endif
for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
if (context->fd_hashtable[n].wsi)
lws_free(context->fd_hashtable[n].wsi);
}
WSACleanup();
}

View File

@ -0,0 +1,122 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include "private-lib-core.h"
/*
* Normally you don't want this, use lws_sul instead inside the event loop.
* But sometimes for drivers it makes sense, so there's an internal-only
* crossplatform api for it.
*/
void
lws_msleep(unsigned int ms)
{
Sleep(ms);
}
lws_usec_t
lws_now_usecs(void)
{
#ifndef DELTA_EPOCH_IN_MICROSECS
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
FILETIME filetime;
ULARGE_INTEGER datetime;
#ifdef _WIN32_WCE
GetCurrentFT(&filetime);
#else
GetSystemTimeAsFileTime(&filetime);
#endif
/*
* As per Windows documentation for FILETIME, copy the resulting
* FILETIME structure to a ULARGE_INTEGER structure using memcpy
* (using memcpy instead of direct assignment can prevent alignment
* faults on 64-bit Windows).
*/
memcpy(&datetime, &filetime, sizeof(datetime));
/* Windows file times are in 100s of nanoseconds. */
return (datetime.QuadPart / 10) - DELTA_EPOCH_IN_MICROSECS;
}
#ifdef _WIN32_WCE
time_t time(time_t *t)
{
time_t ret = lws_now_usecs() / 1000000;
if(t != NULL)
*t = ret;
return ret;
}
#endif
size_t
lws_get_random(struct lws_context *context, void *buf, size_t len)
{
size_t n;
char *p = (char *)buf;
for (n = 0; n < len; n++)
p[n] = (unsigned char)rand();
return n;
}
void
lwsl_emit_syslog(int level, const char *line)
{
lwsl_emit_stderr(level, line);
}
int kill(int pid, int sig)
{
lwsl_err("Sorry Windows doesn't support kill().");
exit(0);
}
int fork(void)
{
lwsl_err("Sorry Windows doesn't support fork().");
exit(0);
}
int
lws_plat_recommended_rsa_bits(void)
{
return 4096;
}

View File

@ -0,0 +1,135 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include "private-lib-core.h"
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
int
lws_plat_pipe_create(struct lws *wsi)
{
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
struct sockaddr_in *si = &pt->frt_pipe_si;
lws_sockfd_type *fd = pt->dummy_pipe_fds;
socklen_t sl;
/*
* Non-WSA HANDLEs can't join the WSAPoll() wait... use a UDP socket
* listening on 127.0.0.1:xxxx and send a byte to it from a second UDP
* socket to cancel the wait.
*
* Set the port to 0 at the bind, so lwip will choose a free one in the
* ephemeral range for us.
*/
fd[0] = socket(AF_INET, SOCK_DGRAM, 0);
if (fd[0] == INVALID_SOCKET)
goto bail;
fd[1] = socket(AF_INET, SOCK_DGRAM, 0);
if (fd[1] == INVALID_SOCKET)
goto bail;
/*
* No need for memset since it's in zalloc'd context... it's in the
* context so we can reuse the prepared sockaddr to send tp fd[0] whem
* we want to cancel the wait
*/
si->sin_family = AF_INET;
si->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
si->sin_port = 0;
if (bind(fd[0], (const struct sockaddr *)si, sizeof(*si)) < 0)
goto bail;
/*
* Query the socket to set pt->frt_pipe_si to the full sockaddr it
* wants to be addressed by, including the port that the os chose.
*
* Afterwards, we can use this prepared sockaddr stashed in the context
* to trigger the "pipe" without any other preliminaries.
*/
sl = sizeof(*si);
if (getsockname(fd[0], (struct sockaddr *)si, &sl))
goto bail;
lwsl_info("%s: cancel UDP skt port %d\n", __func__,
ntohs(si->sin_port));
return 0;
bail:
lwsl_err("%s: failed\n", __func__);
return 1;
}
int
lws_plat_pipe_signal(struct lws_context *ctx, int tsi)
{
struct lws_context_per_thread *pt = &ctx->pt[tsi];
struct sockaddr_in *si = &pt->frt_pipe_si;
lws_sockfd_type *fd = pt->dummy_pipe_fds;
char u = 0;
int n;
/*
* Send a single UDP byte payload to the listening socket fd[0], forcing
* the event loop wait to wake. fd[1] and context->frt_pipe_si are
* set at pt creation and are static.
*/
n = sendto(fd[1], &u, 1, 0, (struct sockaddr *)si, sizeof(*si));
return n != 1;
}
void
lws_plat_pipe_close(struct lws *wsi)
{
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
if (pt->dummy_pipe_fds[0] && pt->dummy_pipe_fds[0] != LWS_SOCK_INVALID)
closesocket(pt->dummy_pipe_fds[0]);
if (pt->dummy_pipe_fds[1] && pt->dummy_pipe_fds[1] != LWS_SOCK_INVALID)
closesocket(pt->dummy_pipe_fds[1]);
pt->dummy_pipe_fds[0] = pt->dummy_pipe_fds[1] = LWS_SOCK_INVALID;
}
int
lws_plat_pipe_is_fd_assocated(struct lws_context *cx, int tsi, lws_sockfd_type fd)
{
struct lws_context_per_thread *pt = &cx->pt[tsi];
return fd == pt->dummy_pipe_fds[0] || fd == pt->dummy_pipe_fds[1];
}

View File

@ -0,0 +1,181 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include "private-lib-core.h"
/*
* ie, if the plugins api needed at all
*/
#if defined(LWS_WITH_PLUGINS_API) && (UV_VERSION_MAJOR > 0)
const lws_plugin_header_t *
lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
const char *sofilename, const char *_class,
each_plugin_cb_t each, void *each_user)
{
const lws_plugin_header_t *hdr;
struct lws_plugin *pin;
char sym[96], *dot;
uv_lib_t lib;
void *v;
int m;
lib.errmsg = NULL;
lib.handle = NULL;
if (uv_dlopen(libpath, &lib)) {
uv_dlerror(&lib);
lwsl_err("Error loading DSO: %s\n", lib.errmsg);
uv_dlclose(&lib);
return NULL;
}
/* we could open it... can we get his export struct? */
m = lws_snprintf(sym, sizeof(sym) - 1, "%s", sofilename);
if (m < 4)
goto bail;
dot = strchr(sym, '.');
if (dot)
*dot = '\0'; /* snip the .so or .lib or what-have-you*/
if (uv_dlsym(&lib, sym, &v)) {
uv_dlerror(&lib);
lwsl_err("%s: Failed to get '%s' on %s: %s\n",
__func__, path, libpath, lib.errmsg);
goto bail;
}
hdr = (const lws_plugin_header_t *)v;
if (hdr->api_magic != LWS_PLUGIN_API_MAGIC) {
lwsl_info("%s: plugin %s has outdated api %d (vs %d)\n",
__func__, libpath, hdr->api_magic,
LWS_PLUGIN_API_MAGIC);
goto bail;
}
if (strcmp(hdr->lws_build_hash, LWS_BUILD_HASH))
goto bail;
if (strcmp(hdr->_class, _class))
goto bail;
/*
* We don't already have one of these, right?
*/
pin = *pplugin;
while (pin) {
if (!strcmp(pin->hdr->name, hdr->name))
goto bail;
pin = pin->list;
}
/*
* OK let's bring it in
*/
pin = lws_malloc(sizeof(*pin), __func__);
if (!pin)
goto bail;
pin->list = *pplugin;
*pplugin = pin;
pin->u.lib = lib;
pin->hdr = hdr;
if (each)
each(pin, each_user);
return hdr;
bail:
uv_dlclose(&lib);
return NULL;
}
int
lws_plat_destroy_dl(struct lws_plugin *p)
{
uv_dlclose(&p->u.lib);
return 0;
}
#endif
/*
* Specifically for protocol plugins support
*/
#if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
static int
protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
{
struct lws_context *context = (struct lws_context *)each_user;
const lws_plugin_protocol_t *plpr =
(const lws_plugin_protocol_t *)pin->hdr;
context->plugin_protocol_count += plpr->count_protocols;
context->plugin_extension_count += plpr->count_extensions;
return 0;
}
#endif
int
lws_plat_plugins_init(struct lws_context *context, const char * const *d)
{
#if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
if (info->plugin_dirs) {
uv_loop_init(&context->uv.loop);
lws_plugins_init(&context->plugin_list, info->plugin_dirs,
"lws_protocol_plugin", NULL,
protocol_plugin_cb, context);
}
#endif
return 0;
}
int
lws_plat_plugins_destroy(struct lws_context * context)
{
#if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV) &&
context->plugin_list) {
lws_plugins_destroy(&context->plugin_list, NULL, NULL);
while (uv_loop_close(&context->uv.loop))
;
}
#endif
return 0;
}

View File

@ -0,0 +1,104 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include "private-lib-async-dns.h"
#include <iphlpapi.h>
lws_async_dns_server_check_t
lws_plat_asyncdns_init(struct lws_context *context, lws_async_dns_t *dns)
{
lws_async_dns_server_check_t s = LADNS_CONF_SERVER_SAME;
lws_async_dns_server_t *dsrv;
lws_sockaddr46 sa46t;
unsigned long ul;
FIXED_INFO *fi;
int n = 0;
DWORD dw;
ul = sizeof(fi);
do {
fi = (FIXED_INFO *)lws_malloc(ul, __func__);
if (!fi)
goto oom;
dw = GetNetworkParams(fi, &ul);
if (dw == NO_ERROR)
break;
if (dw != ERROR_BUFFER_OVERFLOW) {
lwsl_err("%s: GetNetworkParams says 0x%x\n", __func__,
(unsigned int)dw);
return LADNS_CONF_SERVER_UNKNOWN;
}
lws_free(fi);
if (n++)
/* not twice or more */
goto oom;
} while (1);
/* if we got here, then we have it */
lwsl_info("%s: trying %s\n", __func__,
fi->DnsServerList.IpAddress.String);
n = lws_sa46_parse_numeric_address(
fi->DnsServerList.IpAddress.String, &sa46t);
lws_free(fi);
if (!n) {
dsrv = __lws_async_dns_server_find(dns, &sa46t);
if (!dsrv) {
__lws_async_dns_server_add(dns, &sa46t);
s = LADNS_CONF_SERVER_CHANGED;
}
}
return s;
oom:
lwsl_err("%s: OOM\n", __func__);
return LADNS_CONF_SERVER_UNKNOWN;
}
int
lws_plat_ntpclient_config(struct lws_context *context)
{
#if defined(LWS_HAVE_GETENV)
char *ntpsrv = getenv("LWS_NTP_SERVER");
if (ntpsrv && strlen(ntpsrv) < 64) {
lws_system_blob_heap_append(lws_system_get_blob(context,
LWS_SYSBLOB_TYPE_NTP_SERVER, 0),
(const uint8_t *)ntpsrv,
strlen(ntpsrv));
return 1;
}
#endif
return 0;
}

View File

@ -0,0 +1,193 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include "private-lib-core.h"
int
_lws_plat_service_forced_tsi(struct lws_context *context, int tsi)
{
struct lws_context_per_thread *pt = &context->pt[tsi];
int m, n, r;
r = lws_service_flag_pending(context, tsi);
/* any socket with events to service? */
for (n = 0; n < (int)pt->fds_count; n++) {
if (!pt->fds[n].revents)
continue;
unsigned int fds_count = pt->fds_count;
m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
if (m < 0)
return -1;
/* if something closed, fds_count will change, retry this slot */
if (pt->fds_count != fds_count)
n--;
}
lws_service_do_ripe_rxflow(pt);
return r;
}
extern void lws_client_conn_wait_timeout(lws_sorted_usec_list_t *sul);
int
_lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
{
struct lws_context_per_thread *pt;
struct lws_pollfd *pfd;
lws_usec_t timeout_us;
int64_t timeout_ms64 = (int64_t)timeout_ms;
struct lws *wsi;
unsigned int i;
int n;
/* stay dead once we are dead */
if (context == NULL)
return 1;
pt = &context->pt[tsi];
if (!pt->service_tid_detected && context->vhost_list) {
lws_fakewsi_def_plwsa(pt);
lws_fakewsi_prep_plwsa_ctx(context);
pt->service_tid = context->vhost_list->
protocols[0].callback((struct lws *)plwsa,
LWS_CALLBACK_GET_THREAD_ID,
NULL, NULL, 0);
pt->service_tid_detected = 1;
}
if (timeout_ms64 < 0)
timeout_ms64 = 0;
else
/* force a default timeout of 23 days */
timeout_ms64 = 2000000000;
timeout_us = ((lws_usec_t)timeout_ms64) * LWS_US_PER_MS;
if (context->event_loop_ops->run_pt)
context->event_loop_ops->run_pt(context, tsi);
for (i = 0; i < pt->fds_count; ++i) {
pfd = &pt->fds[i];
if (!(pfd->events & LWS_POLLOUT))
continue;
wsi = wsi_from_fd(context, pfd->fd);
if (!wsi || wsi->listener)
continue;
if (wsi->sock_send_blocking)
continue;
pfd->revents = LWS_POLLOUT;
n = lws_service_fd(context, pfd);
if (n < 0)
return -1;
/*
* Force WSAWaitForMultipleEvents() to check events
* and then return immediately.
*/
timeout_us = 0;
/* if something closed, retry this slot */
if (n)
i--;
}
/*
* service pending callbacks and get maximum wait time
*/
{
lws_usec_t us;
lws_pt_lock(pt, __func__);
/* don't stay in poll wait longer than next hr timeout */
us = __lws_sul_service_ripe(pt->pt_sul_owner,
LWS_COUNT_PT_SUL_OWNERS,
lws_now_usecs());
if (us && us < timeout_us)
/*
* If something wants zero wait, that's OK, but if the next sul
* coming ripe is an interval less than our wait resolution,
* bump it to be the wait resolution.
*/
timeout_us = us < context->us_wait_resolution ?
context->us_wait_resolution : us;
lws_pt_unlock(pt);
}
if (_lws_plat_service_forced_tsi(context, tsi))
timeout_us = 0;
/*
* is there anybody with pending stuff that needs service forcing?
*/
if (!lws_service_adjust_timeout(context, 1, tsi))
timeout_us = 0;
// lwsl_notice("%s: in %dms, count %d\n", __func__, (int)(timeout_us / 1000), pt->fds_count);
// for (n = 0; n < (int)pt->fds_count; n++)
// lwsl_notice("%s: fd %d ev 0x%x POLLIN %d, POLLOUT %d\n", __func__, (int)pt->fds[n].fd, (int)pt->fds[n].events, POLLIN, POLLOUT);
int d = WSAPoll((WSAPOLLFD *)&pt->fds[0], pt->fds_count, (int)(timeout_us / LWS_US_PER_MS));
if (d < 0) {
lwsl_err("%s: WSAPoll failed: count %d, err %d: %d\n", __func__, pt->fds_count, d, WSAGetLastError());
return 0;
}
// lwsl_notice("%s: out\n", __func__);
#if defined(LWS_WITH_TLS)
if (pt->context->tls_ops &&
pt->context->tls_ops->fake_POLLIN_for_buffered)
pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
#endif
for (n = 0; n < (int)pt->fds_count; n++)
if (pt->fds[n].fd != LWS_SOCK_INVALID && pt->fds[n].revents) {
// lwsl_notice("%s: idx %d, revents 0x%x\n", __func__, n, pt->fds[n].revents);
lws_service_fd_tsi(context, &pt->fds[n], tsi);
}
if (pt->destroy_self) {
lws_context_destroy(pt->context);
return -1;
}
return 0;
}
int
lws_plat_service(struct lws_context *context, int timeout_ms)
{
return _lws_plat_service_tsi(context, timeout_ms, 0);
}

View File

@ -0,0 +1,662 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
#include "private-lib-core.h"
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
int
lws_send_pipe_choked(struct lws *wsi)
{ struct lws *wsi_eff;
#if defined(LWS_WITH_HTTP2)
wsi_eff = lws_get_network_wsi(wsi);
#else
wsi_eff = wsi;
#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 */
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
)
return 1;
return (int)wsi_eff->sock_send_blocking;
}
int
lws_poll_listen_fd(struct lws_pollfd *fd)
{
fd_set readfds;
struct timeval tv = { 0, 0 };
assert((fd->events & LWS_POLLIN) == LWS_POLLIN);
FD_ZERO(&readfds);
FD_SET(fd->fd, &readfds);
return select(((int)fd->fd) + 1, &readfds, NULL, NULL, &tv);
}
int
lws_plat_set_nonblocking(lws_sockfd_type fd)
{
u_long optl = 1;
int result = !!ioctlsocket(fd, FIONBIO, &optl);
#if (_LWS_ENABLED_LOGS & LLL_ERR)
if (result)
lwsl_err("ioctlsocket FIONBIO 1 failed with error %d\n", LWS_ERRNO);
#endif
return result;
}
int
lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd,
int unix_skt)
{
int optval = 1;
int optlen = sizeof(optval);
DWORD dwBytesRet;
struct tcp_keepalive alive;
int protonbr;
#ifndef _WIN32_WCE
struct protoent *tcp_proto;
#endif
if (vhost->ka_time) {
/* enable keepalive on this socket */
optval = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(const char *)&optval, optlen) < 0) {
#if (_LWS_ENABLED_LOGS & LLL_ERR)
lwsl_err("setsockopt SO_KEEPALIVE 1 failed with error %d\n", LWS_ERRNO);
#endif
return 1;
}
alive.onoff = TRUE;
alive.keepalivetime = vhost->ka_time * 1000;
alive.keepaliveinterval = vhost->ka_interval * 1000;
if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
NULL, 0, &dwBytesRet, NULL, NULL)) {
#if (_LWS_ENABLED_LOGS & LLL_ERR)
lwsl_err("WSAIoctl SIO_KEEPALIVE_VALS 1 %lu %lu failed with error %d\n", alive.keepalivetime, alive.keepaliveinterval, LWS_ERRNO);
#endif
return 1;
}
}
/* Disable Nagle */
optval = 1;
#ifndef _WIN32_WCE
tcp_proto = getprotobyname("TCP");
if (!tcp_proto) {
#if (_LWS_ENABLED_LOGS & LLL_WARN)
lwsl_warn("getprotobyname(\"TCP\") failed with error, falling back to 6 %d\n", LWS_ERRNO);
#endif
protonbr = 6; /* IPPROTO_TCP */
} else
protonbr = tcp_proto->p_proto;
#else
protonbr = 6;
#endif
if (setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen) ) {
#if (_LWS_ENABLED_LOGS & LLL_WARN)
lwsl_warn("setsockopt TCP_NODELAY 1 failed with error %d\n", LWS_ERRNO);
#endif
}
return lws_plat_set_nonblocking(fd);
}
int
lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
{
int optval = 1, ret = 0;
socklen_t optlen = sizeof(optval);
#if (_LWS_ENABLED_LOGS & LLL_WARN)
int en;
#endif
/*
* Seems to require "differeniated services" but no docs
*
* https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options
* https://docs.microsoft.com/en-us/previous-versions/windows/desktop/qos/differentiated-services
*/
lwsl_warn("%s: priority and ip sockets options not implemented on windows platform\n", __func__);
/*
* only accept that we are the only listener on the port
* https://msdn.microsoft.com/zh-tw/library/
* windows/desktop/ms740621(v=vs.85).aspx
*
* for lws, to match Linux, we default to exclusive listen
*/
if (lws_flags & LCCSCF_ALLOW_REUSE_ADDR) {
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval, optlen) < 0) {
#if (_LWS_ENABLED_LOGS & LLL_WARN)
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__);
} else {
if (setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
(const void *)&optval, optlen) < 0) {
#if (_LWS_ENABLED_LOGS & LLL_WARN)
en = errno;
lwsl_warn("%s: unable to use exclusive addresses: errno %d\n",
__func__, en);
#endif
ret = 1;
} else
lwsl_notice("%s: set use exclusive addresses\n", __func__);
}
#if defined(LWS_WITH_IPV6)
/* I do not believe Microsoft supports RFC5014
* Instead, you must set lws_client_connect_info::iface */
if (lws_flags & LCCSCF_IPV6_PREFER_PUBLIC_ADDR) {
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
}
#endif
return ret;
}
int
lws_interface_to_sa(int ipv6,
const char *ifname, struct sockaddr_in *addr, size_t addrlen)
{
long long address;
#ifdef LWS_WITH_IPV6
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
if (ipv6) {
if (lws_plat_inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1) {
return LWS_ITOSA_USABLE;
}
}
#endif
address = inet_addr(ifname);
if (address == INADDR_NONE) {
struct hostent *entry = gethostbyname(ifname);
if (entry)
address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
}
if (address == INADDR_NONE)
return LWS_ITOSA_NOT_EXIST;
addr->sin_addr.s_addr = (unsigned long)(lws_intptr_t)address;
return LWS_ITOSA_USABLE;
}
void
lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
#if defined(LWS_WITH_UDP)
if (wsi->udp) {
lwsl_info("%s: UDP\n", __func__);
pt->fds[pt->fds_count].events |= LWS_POLLIN;
}
#endif
if (context->event_loop_ops->io)
context->event_loop_ops->io(wsi, LWS_EV_START | LWS_EV_READ);
pt->fds[pt->fds_count++].revents = 0;
lws_plat_change_pollfd(context, wsi, &pt->fds[pt->fds_count - 1]);
}
void
lws_plat_delete_socket_from_fds(struct lws_context *context,
struct lws *wsi, int m)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
pt->fds_count--;
}
int
lws_plat_check_connection_error(struct lws *wsi)
{
int optVal;
int optLen = sizeof(int);
if (getsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_ERROR,
(char*)&optVal, &optLen) != SOCKET_ERROR && optVal &&
optVal != LWS_EALREADY && optVal != LWS_EINPROGRESS &&
optVal != LWS_EWOULDBLOCK && optVal != WSAEINVAL) {
lwsl_debug("Connect failed SO_ERROR=%d\n", optVal);
return 1;
}
return 0;
}
int
lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
struct lws_pollfd *pfd)
{
//struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
return 0;
}
#if defined(LWS_WITH_TLS)
int
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
{
#if !defined(LWS_WITH_MBEDTLS) && defined(LWS_SSL_CLIENT_USE_OS_CA_CERTS)
PCCERT_CONTEXT pcc = NULL;
CERT_ENHKEY_USAGE* ceu = NULL;
DWORD ceu_alloc = 0;
X509_STORE* store;
HCERTSTORE hStore;
int imps = 0;
if (lws_check_opt(vhost->options,
LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS))
return 0;
/*
* Windows Trust Store code adapted from curl (MIT) openssl.c
* https://github.com/warmcat/libwebsockets/pull/2233
*/
store = SSL_CTX_get_cert_store(vhost->tls.ssl_client_ctx);
hStore = CertOpenSystemStore((HCRYPTPROV_LEGACY)NULL, TEXT("ROOT"));
if (!hStore) {
lwsl_notice("%s: no store\n", __func__);
return 1;
}
do {
const unsigned char* ecert;
char cert_name[256];
DWORD req_size = 0;
BYTE key_usage[2];
FILETIME ft;
X509* x509;
pcc = CertEnumCertificatesInStore(hStore, pcc);
if (!pcc)
break;
if (!CertGetNameStringA(pcc, CERT_NAME_SIMPLE_DISPLAY_TYPE,
0, NULL, cert_name, sizeof(cert_name)))
strcpy(cert_name, "Unknown");
lwsl_debug("%s: Checking cert \"%s\"\n", __func__, cert_name);
ecert = (const unsigned char*)pcc->pbCertEncoded;
if (!ecert)
continue;
GetSystemTimeAsFileTime(&ft);
if (CompareFileTime(&pcc->pCertInfo->NotBefore, &ft) > 0 ||
CompareFileTime(&ft, &pcc->pCertInfo->NotAfter) > 0)
continue;
/* If key usage exists check for signing attribute */
if (CertGetIntendedKeyUsage(pcc->dwCertEncodingType,
pcc->pCertInfo,
key_usage, sizeof(key_usage))) {
if (!(key_usage[0] & CERT_KEY_CERT_SIGN_KEY_USAGE))
continue;
} else
if (GetLastError())
continue;
/*
* If enhanced key usage exists check for server auth attribute.
*
* Note "In a Microsoft environment, a certificate might also
* have EKU extended properties that specify valid uses for the
* certificate."
* The call below checks both, and behavior varies depending on
* what is found. For more details see CertGetEnhancedKeyUsage
* doc.
*/
if (!CertGetEnhancedKeyUsage(pcc, 0, NULL, &req_size))
continue;
if (req_size && req_size > ceu_alloc) {
void* tmp = lws_realloc(ceu, req_size, __func__);
if (!tmp) {
lwsl_err("%s: OOM", __func__);
break;
}
ceu = (CERT_ENHKEY_USAGE*)tmp;
ceu_alloc = req_size;
}
if (!CertGetEnhancedKeyUsage(pcc, 0, ceu, &req_size))
continue;
if (!ceu || (ceu && !ceu->cUsageIdentifier)) {
/*
* "If GetLastError returns CRYPT_E_NOT_FOUND, the
* certificate is good for all uses. If it returns
* zero, the certificate has no valid uses."
*/
if ((HRESULT)GetLastError() != CRYPT_E_NOT_FOUND)
continue;
/* ... allow it... */
} else
if (ceu) {
BOOL found = FALSE;
DWORD i;
/*
* If there is a CEU, check that it specifies
* we can use the cert for server validation
*/
for (i = 0; i < ceu->cUsageIdentifier; i++) {
if (strcmp("1.3.6.1.5.5.7.3.1"
/* OID server auth */,
ceu->rgpszUsageIdentifier[i]))
continue;
found = TRUE;
break;
}
if (!found)
/* Don't use cert if no usage match */
continue;
}
x509 = d2i_X509(NULL, &ecert, pcc->cbCertEncoded);
if (!x509)
/* We can't parse it as am X.509, skip it */
continue;
if (X509_STORE_add_cert(store, x509) == 1) {
lwsl_debug("%s: Imported cert \"%s\"\n", __func__,
cert_name);
imps++;
}
/*
* Treat failure as nonfatal, eg, may be dupe
*/
X509_free(x509);
} while (1);
lws_free(ceu);
CertFreeCertificateContext(pcc);
CertCloseStore(hStore, 0);
lwsl_notice("%s: Imported %d certs from plat store\n", __func__, imps);
#endif
return 0;
}
#endif
const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
{
WCHAR *buffer;
size_t bufferlen = (size_t)cnt;
BOOL ok = FALSE;
buffer = lws_malloc(bufferlen * 2, "inet_ntop");
if (!buffer) {
lwsl_err("Out of memory\n");
return NULL;
}
if (af == AF_INET) {
struct sockaddr_in srcaddr;
memset(&srcaddr, 0, sizeof(srcaddr));
srcaddr.sin_family = AF_INET;
memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
if (!WSAAddressToStringW((struct sockaddr*)&srcaddr,
sizeof(srcaddr), 0, buffer,
(LPDWORD)&bufferlen))
ok = TRUE;
#ifdef LWS_WITH_IPV6
} else if (af == AF_INET6) {
struct sockaddr_in6 srcaddr;
memset(&srcaddr, 0, sizeof(srcaddr));
srcaddr.sin6_family = AF_INET6;
memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
if (!WSAAddressToStringW((struct sockaddr*)&srcaddr,
sizeof(srcaddr), 0, buffer,
(LPDWORD)&bufferlen))
ok = TRUE;
#endif
} else
lwsl_err("Unsupported type\n");
if (!ok) {
int rv = WSAGetLastError();
lwsl_err("WSAAddressToString() : %d\n", rv);
} else {
if (WideCharToMultiByte(CP_ACP, 0, buffer, (int)bufferlen, dst,
cnt, 0, NULL) <= 0)
ok = FALSE;
}
lws_free(buffer);
return ok ? dst : NULL;
}
int
lws_plat_inet_pton(int af, const char *src, void *dst)
{
WCHAR *buffer;
size_t bufferlen = strlen(src) + 1;
BOOL ok = FALSE;
buffer = lws_malloc(bufferlen * 2, "inet_pton");
if (!buffer) {
lwsl_err("Out of memory\n");
return -1;
}
if (MultiByteToWideChar(CP_ACP, 0, src, (int)bufferlen, buffer,
(int)bufferlen) <= 0) {
lwsl_err("Failed to convert multi byte to wide char\n");
lws_free(buffer);
return -1;
}
if (af == AF_INET) {
struct sockaddr_in dstaddr;
int dstaddrlen = sizeof(dstaddr);
memset(&dstaddr, 0, sizeof(dstaddr));
dstaddr.sin_family = AF_INET;
if (!WSAStringToAddressW(buffer, af, 0, (struct sockaddr *) &dstaddr, &dstaddrlen)) {
ok = TRUE;
memcpy(dst, &dstaddr.sin_addr, sizeof(dstaddr.sin_addr));
}
#ifdef LWS_WITH_IPV6
} else if (af == AF_INET6) {
struct sockaddr_in6 dstaddr;
int dstaddrlen = sizeof(dstaddr);
memset(&dstaddr, 0, sizeof(dstaddr));
dstaddr.sin6_family = AF_INET6;
if (!WSAStringToAddressW(buffer, af, 0, (struct sockaddr *) &dstaddr, &dstaddrlen)) {
ok = TRUE;
memcpy(dst, &dstaddr.sin6_addr, sizeof(dstaddr.sin6_addr));
}
#endif
} else
lwsl_err("Unsupported type\n");
if (!ok) {
int rv = WSAGetLastError();
lwsl_err("WSAAddressToString() : %d\n", rv);
}
lws_free(buffer);
return ok ? 1 : -1;
}
int
lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
size_t n, int fd, const char *iface)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_if_up(const char *ifname, int fd, int up)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
int
lws_plat_ifconfig(int fd, uint8_t *ip, lws_dhcpc_ifstate_t *is)
{
lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
return -1;
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret, en;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = send(fd, (const char *)buf, (unsigned int)len, 0);
if (ret >= 0)
return ret;
en = LWS_ERRNO;
if (en == EAGAIN || en == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_WRITE;
ret = WSAGetLastError();
lwsl_notice("%s: errno %d, GLE %d\n", __func__, en, ret);
if (ret == WSAECONNRESET )
return( MBEDTLS_ERR_NET_CONN_RESET );
return MBEDTLS_ERR_NET_SEND_FAILED;
}
int
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret, en;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = (int)recv(fd, (char *)buf, (unsigned int)len, 0);
if (ret >= 0)
return ret;
en = LWS_ERRNO;
if (en == EAGAIN || en == EWOULDBLOCK || en == WSAEWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_READ;
ret = WSAGetLastError();
lwsl_notice("%s: errno %d, GLE %d\n", __func__, en, ret);
if (ret == WSAECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
#endif

View File

@ -0,0 +1,575 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include "private-lib-core.h"
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
void
lws_spawn_timeout(struct lws_sorted_usec_list *sul)
{
struct lws_spawn_piped *lsp = lws_container_of(sul,
struct lws_spawn_piped, sul);
lwsl_warn("%s: spawn exceeded timeout, killing\n", __func__);
lws_spawn_piped_kill_child_process(lsp);
}
void
lws_spawn_sul_reap(struct lws_sorted_usec_list *sul)
{
struct lws_spawn_piped *lsp = lws_container_of(sul,
struct lws_spawn_piped, sul_reap);
lwsl_notice("%s: reaping spawn after last stdpipe, tries left %d\n",
__func__, lsp->reap_retry_budget);
if (!lws_spawn_reap(lsp) && !lsp->pipes_alive) {
if (--lsp->reap_retry_budget) {
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
&lsp->sul_reap, lws_spawn_sul_reap,
250 * LWS_US_PER_MS);
} else {
lwsl_err("%s: Unable to reap lsp %p, killing\n",
__func__, lsp);
lsp->reap_retry_budget = 20;
lws_spawn_piped_kill_child_process(lsp);
}
}
}
static struct lws *
lws_create_basic_wsi(struct lws_context *context, int tsi,
const struct lws_role_ops *ops)
{
struct lws_context_per_thread *pt = &context->pt[tsi];
struct lws *new_wsi;
if (!context->vhost_list)
return NULL;
if ((unsigned int)context->pt[tsi].fds_count ==
context->fd_limit_per_thread - 1) {
lwsl_err("no space for new conn\n");
return NULL;
}
lws_context_lock(context, __func__);
new_wsi = __lws_wsi_create_with_role(context, tsi, ops, NULL);
lws_context_unlock(context);
if (new_wsi == NULL) {
lwsl_err("Out of memory for new connection\n");
return NULL;
}
new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
/* initialize the instance struct */
lws_role_transition(new_wsi, 0, LRS_ESTABLISHED, ops);
new_wsi->hdr_parsing_completed = 0;
new_wsi->position_in_fds_table = LWS_NO_FDS_POS;
/*
* these can only be set once the protocol is known
* we set an unestablished connection's protocol pointer
* to the start of the defauly vhost supported list, so it can look
* for matching ones during the handshake
*/
new_wsi->user_space = NULL;
new_wsi->desc.sockfd = LWS_SOCK_INVALID;
return new_wsi;
}
void
lws_spawn_piped_destroy(struct lws_spawn_piped **_lsp)
{
struct lws_spawn_piped *lsp = *_lsp;
struct lws *wsi;
int n;
if (!lsp)
return;
for (n = 0; n < 3; n++) {
if (lsp->pipe_fds[n][!!(n == 0)]) {
CloseHandle(lsp->pipe_fds[n][n == 0]);
lsp->pipe_fds[n][n == 0] = NULL;
}
for (n = 0; n < 3; n++) {
if (lsp->stdwsi[n]) {
lwsl_notice("%s: closing stdwsi %d\n", __func__, n);
wsi = lsp->stdwsi[n];
lsp->stdwsi[n]->desc.filefd = NULL;
lsp->stdwsi[n] = NULL;
lws_set_timeout(wsi, 1, LWS_TO_KILL_SYNC);
}
}
}
lws_dll2_remove(&lsp->dll);
lws_sul_cancel(&lsp->sul);
lws_sul_cancel(&lsp->sul_reap);
lws_sul_cancel(&lsp->sul_poll);
lwsl_warn("%s: deleting lsp\n", __func__);
lws_free_set_NULL((*_lsp));
}
int
lws_spawn_reap(struct lws_spawn_piped *lsp)
{
void *opaque = lsp->info.opaque;
lsp_cb_t cb = lsp->info.reap_cb;
struct _lws_siginfo_t lsi;
lws_usec_t acct[4];
DWORD ex;
if (!lsp->child_pid)
return 0;
if (!GetExitCodeProcess(lsp->child_pid, &ex)) {
lwsl_notice("%s: GetExitCodeProcess failed\n", __func__);
return 0;
}
/* nonzero = success */
if (ex == STILL_ACTIVE) {
lwsl_notice("%s: still active\n", __func__);
return 0;
}
/* mark the earliest time we knew he had gone */
if (!lsp->reaped) {
lsp->reaped = lws_now_usecs();
/*
* Switch the timeout to restrict the amount of grace time
* to drain stdwsi
*/
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
&lsp->sul, lws_spawn_timeout,
5 * LWS_US_PER_SEC);
}
/*
* Stage finalizing our reaction to the process going down until the
* stdwsi flushed whatever is in flight and all noticed they were
* closed. For that reason, each stdwsi close must call lws_spawn_reap
* to check if that was the last one and we can proceed with the reap.
*/
if (!lsp->ungraceful && lsp->pipes_alive) {
lwsl_notice("%s: stdwsi alive, not reaping\n", __func__);
return 0;
}
/* we reached the reap point, no need for timeout wait */
lws_sul_cancel(&lsp->sul);
/*
* All the stdwsi went down, nothing more is coming... it's over
* Collect the final information and then reap the dead process
*/
lsi.retcode = 0x10000 | (int)ex;
lwsl_notice("%s: process exit 0x%x\n", __func__, lsi.retcode);
lsp->child_pid = NULL;
/* destroy the lsp itself first (it's freed and plsp set NULL */
if (lsp->info.plsp)
lws_spawn_piped_destroy(lsp->info.plsp);
/* then do the parent callback informing it's destroyed */
memset(acct, 0, sizeof(acct));
if (cb)
cb(opaque, acct, &lsi, 0);
lwsl_notice("%s: completed reap\n", __func__);
return 1; /* was reaped */
}
int
lws_spawn_piped_kill_child_process(struct lws_spawn_piped *lsp)
{
if (!lsp->child_pid)
return 1;
lsp->ungraceful = 1; /* don't wait for flushing, just kill it */
if (lws_spawn_reap(lsp))
/* that may have invalidated lsp */
return 0;
lwsl_warn("%s: calling TerminateProcess on child pid\n", __func__);
TerminateProcess(lsp->child_pid, 252);
lws_spawn_reap(lsp);
/* that may have invalidated lsp */
return 0;
}
static void
windows_pipe_poll_hack(lws_sorted_usec_list_t *sul)
{
struct lws_spawn_piped *lsp = lws_container_of(sul,
struct lws_spawn_piped, sul_poll);
struct lws *wsi, *wsi1;
DWORD br;
char c;
/*
* Do it first, we know lsp exists and if it's destroyed inbetweentimes,
* it will already have cancelled this
*/
lws_sul_schedule(lsp->context, 0, &lsp->sul_poll,
windows_pipe_poll_hack, 50 * LWS_US_PER_MS);
wsi = lsp->stdwsi[LWS_STDOUT];
wsi1 = lsp->stdwsi[LWS_STDERR];
if (wsi && lsp->pipe_fds[LWS_STDOUT][0] != NULL) {
if (!PeekNamedPipe(lsp->pipe_fds[LWS_STDOUT][0], &c, 1, &br,
NULL, NULL)) {
lwsl_notice("%s: stdout pipe errored\n", __func__);
CloseHandle(lsp->stdwsi[LWS_STDOUT]->desc.filefd);
lsp->pipe_fds[LWS_STDOUT][0] = NULL;
lsp->stdwsi[LWS_STDOUT]->desc.filefd = NULL;
lsp->stdwsi[LWS_STDOUT] = NULL;
lws_set_timeout(wsi, 1, LWS_TO_KILL_SYNC);
if (lsp->stdwsi[LWS_STDIN]) {
lwsl_notice("%s: closing stdin from stdout close\n",
__func__);
CloseHandle(lsp->stdwsi[LWS_STDIN]->desc.filefd);
wsi = lsp->stdwsi[LWS_STDIN];
lsp->stdwsi[LWS_STDIN]->desc.filefd = NULL;
lsp->stdwsi[LWS_STDIN] = NULL;
lsp->pipe_fds[LWS_STDIN][1] = NULL;
lws_set_timeout(wsi, 1, LWS_TO_KILL_SYNC);
}
/*
* lsp may be destroyed by here... if we wanted to
* handle a still-extant stderr we'll get it next time
*/
return;
} else
if (br)
wsi->a.protocol->callback(wsi,
LWS_CALLBACK_RAW_RX_FILE,
NULL, NULL, 0);
}
/*
* lsp may have been destroyed above
*/
if (wsi1 && lsp->pipe_fds[LWS_STDERR][0]) {
if (!PeekNamedPipe(lsp->pipe_fds[LWS_STDERR][0], &c, 1, &br,
NULL, NULL)) {
lwsl_notice("%s: stderr pipe errored\n", __func__);
CloseHandle(wsi1->desc.filefd);
/*
* Assume is stderr still extant on entry, lsp can't
* have been destroyed by stdout/stdin processing
*/
lsp->stdwsi[LWS_STDERR]->desc.filefd = NULL;
lsp->stdwsi[LWS_STDERR] = NULL;
lsp->pipe_fds[LWS_STDERR][0] = NULL;
lws_set_timeout(wsi1, 1, LWS_TO_KILL_SYNC);
/*
* lsp may have been destroyed above
*/
} else
if (br)
wsi1->a.protocol->callback(wsi1,
LWS_CALLBACK_RAW_RX_FILE,
NULL, NULL, 0);
}
}
/*
* Deals with spawning a subprocess and executing it securely with stdin/out/err
* diverted into pipes
*/
struct lws_spawn_piped *
lws_spawn_piped(const struct lws_spawn_piped_info *i)
{
const struct lws_protocols *pcol = i->vh->context->vhost_list->protocols;
struct lws_context *context = i->vh->context;
struct lws_spawn_piped *lsp;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
char cli[300], *p;
STARTUPINFO si;
int n;
if (i->protocol_name)
pcol = lws_vhost_name_to_protocol(i->vh, i->protocol_name);
if (!pcol) {
lwsl_err("%s: unknown protocol %s\n", __func__,
i->protocol_name ? i->protocol_name : "default");
return NULL;
}
lsp = lws_zalloc(sizeof(*lsp), __func__);
if (!lsp) {
lwsl_err("%s: OOM\n", __func__);
return NULL;
}
/* wholesale take a copy of info */
lsp->info = *i;
lsp->context = context;
lsp->reap_retry_budget = 20;
/*
* Prepare the stdin / out / err pipes
*/
for (n = 0; n < 3; n++) {
lsp->pipe_fds[n][0] = NULL;
lsp->pipe_fds[n][1] = NULL;
}
/* create pipes for [stdin|stdout] and [stderr] */
memset(&sa, 0, sizeof(sa));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE; /* inherit the pipes */
sa.lpSecurityDescriptor = NULL;
for (n = 0; n < 3; n++) {
DWORD waitmode = PIPE_NOWAIT;
if (!CreatePipe(&lsp->pipe_fds[n][0], &lsp->pipe_fds[n][1],
&sa, 0)) {
lwsl_err("%s: CreatePipe() failed\n", __func__);
goto bail1;
}
SetNamedPipeHandleState(lsp->pipe_fds[1][0], &waitmode, NULL, NULL);
SetNamedPipeHandleState(lsp->pipe_fds[2][0], &waitmode, NULL, NULL);
/* don't inherit the pipe side that belongs to the parent */
if (!SetHandleInformation(&lsp->pipe_fds[n][!n],
HANDLE_FLAG_INHERIT, 0)) {
lwsl_err("%s: SetHandleInformation() failed\n", __func__);
//goto bail1;
}
}
/* create wsis for each stdin/out/err fd */
for (n = 0; n < 3; n++) {
lsp->stdwsi[n] = lws_create_basic_wsi(i->vh->context, i->tsi,
i->ops ? i->ops : &role_ops_raw_file);
if (!lsp->stdwsi[n]) {
lwsl_err("%s: unable to create lsp stdwsi\n", __func__);
goto bail2;
}
__lws_lc_tag(i->vh->context, &i->vh->context->lcg[LWSLCG_WSI],
&lsp->stdwsi[n]->lc, "nspawn-stdwsi-%d", n);
lsp->stdwsi[n]->lsp_channel = n;
lws_vhost_bind_wsi(i->vh, lsp->stdwsi[n]);
lsp->stdwsi[n]->a.protocol = pcol;
lsp->stdwsi[n]->a.opaque_user_data = i->opaque;
lsp->stdwsi[n]->desc.filefd = lsp->pipe_fds[n][!n];
lsp->stdwsi[n]->file_desc = 1;
lwsl_debug("%s: lsp stdwsi %p: pipe idx %d -> fd %d / %d\n",
__func__, lsp->stdwsi[n], n,
lsp->pipe_fds[n][!!(n == 0)],
lsp->pipe_fds[n][!(n == 0)]);
#if 0
/* read side is 0, stdin we want the write side, others read */
lsp->stdwsi[n]->desc.filefd = lsp->pipe_fds[n][!!(n == 0)];
if (fcntl(lsp->pipe_fds[n][!!(n == 0)], F_SETFL, O_NONBLOCK) < 0) {
lwsl_err("%s: setting NONBLOCK failed\n", __func__);
goto bail2;
}
#endif
}
for (n = 0; n < 3; n++)
if (i->opt_parent) {
lsp->stdwsi[n]->parent = i->opt_parent;
lsp->stdwsi[n]->sibling_list = i->opt_parent->child_list;
i->opt_parent->child_list = lsp->stdwsi[n];
}
lwsl_notice("%s: pipe handles in %p, out %p, err %p\n", __func__,
lsp->stdwsi[LWS_STDIN]->desc.sockfd,
lsp->stdwsi[LWS_STDOUT]->desc.sockfd,
lsp->stdwsi[LWS_STDERR]->desc.sockfd);
/*
* Windows nonblocking pipe handling is a mess that is unable
* to interoperate with WSA-based wait as far as I can tell.
*
* Let's set up a sul to poll the pipes and synthesize the
* protocol callbacks if anything coming.
*/
lws_sul_schedule(context, 0, &lsp->sul_poll, windows_pipe_poll_hack,
50 * LWS_US_PER_MS);
/*
* Windows wants a single string commandline
*/
p = cli;
n = 0;
while (i->exec_array[n]) {
lws_strncpy(p, i->exec_array[n],
sizeof(cli) - lws_ptr_diff(p, cli));
if (sizeof(cli) - lws_ptr_diff(p, cli) < 4)
break;
p += strlen(p);
*p++ = ' ';
*p = '\0';
n++;
}
puts(cli);
memset(&pi, 0, sizeof(pi));
memset(&si, 0, sizeof(si));
si.cb = sizeof(STARTUPINFO);
si.hStdInput = lsp->pipe_fds[LWS_STDIN][0];
si.hStdOutput = lsp->pipe_fds[LWS_STDOUT][1];
si.hStdError = lsp->pipe_fds[LWS_STDERR][1];
si.dwFlags = STARTF_USESTDHANDLES | CREATE_NO_WINDOW;
si.wShowWindow = TRUE;
if (!CreateProcess(NULL, cli, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
lwsl_err("%s: CreateProcess failed 0x%x\n", __func__,
(unsigned long)GetLastError());
goto bail3;
}
lsp->child_pid = pi.hProcess;
lwsl_notice("%s: lsp %p spawned PID %d\n", __func__, lsp, lsp->child_pid);
lws_sul_schedule(context, i->tsi, &lsp->sul, lws_spawn_timeout,
i->timeout_us ? i->timeout_us : 300 * LWS_US_PER_SEC);
/*
* close: stdin:r, stdout:w, stderr:w
*/
for (n = 0; n < 3; n++)
CloseHandle(lsp->pipe_fds[n][n != 0]);
lsp->pipes_alive = 3;
lsp->created = lws_now_usecs();
if (i->owner)
lws_dll2_add_head(&lsp->dll, i->owner);
if (i->timeout_us)
lws_sul_schedule(context, i->tsi, &lsp->sul,
lws_spawn_timeout, i->timeout_us);
return lsp;
bail3:
lws_sul_cancel(&lsp->sul_poll);
while (--n >= 0)
__remove_wsi_socket_from_fds(lsp->stdwsi[n]);
bail2:
for (n = 0; n < 3; n++)
if (lsp->stdwsi[n])
__lws_free_wsi(lsp->stdwsi[n]);
bail1:
for (n = 0; n < 3; n++) {
if (lsp->pipe_fds[n][0] >= 0)
CloseHandle(lsp->pipe_fds[n][0]);
if (lsp->pipe_fds[n][1] >= 0)
CloseHandle(lsp->pipe_fds[n][1]);
}
lws_free(lsp);
lwsl_err("%s: failed\n", __func__);
return NULL;
}
void
lws_spawn_stdwsi_closed(struct lws_spawn_piped *lsp, struct lws *wsi)
{
int n;
assert(lsp);
lsp->pipes_alive--;
lwsl_debug("%s: pipes alive %d\n", __func__, lsp->pipes_alive);
if (!lsp->pipes_alive)
lws_sul_schedule(lsp->info.vh->context, lsp->info.tsi,
&lsp->sul_reap, lws_spawn_sul_reap, 1);
for (n = 0; n < 3; n++)
if (lsp->stdwsi[n] == wsi)
lsp->stdwsi[n] = NULL;
}
int
lws_spawn_get_stdfd(struct lws *wsi)
{
return wsi->lsp_channel;
}