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,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;
}