1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci */
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ci#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
26d4afb5ceSopenharmony_ci#define _WINSOCK_DEPRECATED_NO_WARNINGS
27d4afb5ceSopenharmony_ci#endif
28d4afb5ceSopenharmony_ci#include "private-lib-core.h"
29d4afb5ceSopenharmony_ci
30d4afb5ceSopenharmony_ci#include <stdlib.h>
31d4afb5ceSopenharmony_ci#include <stdio.h>
32d4afb5ceSopenharmony_ci#include <io.h>
33d4afb5ceSopenharmony_ci#include <fcntl.h>
34d4afb5ceSopenharmony_ci
35d4afb5ceSopenharmony_ciint
36d4afb5ceSopenharmony_cilws_plat_pipe_create(struct lws *wsi)
37d4afb5ceSopenharmony_ci{
38d4afb5ceSopenharmony_ci	struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
39d4afb5ceSopenharmony_ci	struct sockaddr_in *si = &pt->frt_pipe_si;
40d4afb5ceSopenharmony_ci	lws_sockfd_type *fd = pt->dummy_pipe_fds;
41d4afb5ceSopenharmony_ci	socklen_t sl;
42d4afb5ceSopenharmony_ci
43d4afb5ceSopenharmony_ci	/*
44d4afb5ceSopenharmony_ci	 * Non-WSA HANDLEs can't join the WSAPoll() wait... use a UDP socket
45d4afb5ceSopenharmony_ci	 * listening on 127.0.0.1:xxxx and send a byte to it from a second UDP
46d4afb5ceSopenharmony_ci	 * socket to cancel the wait.
47d4afb5ceSopenharmony_ci	 *
48d4afb5ceSopenharmony_ci	 * Set the port to 0 at the bind, so lwip will choose a free one in the
49d4afb5ceSopenharmony_ci	 * ephemeral range for us.
50d4afb5ceSopenharmony_ci	 */
51d4afb5ceSopenharmony_ci
52d4afb5ceSopenharmony_ci	fd[0] = socket(AF_INET, SOCK_DGRAM, 0);
53d4afb5ceSopenharmony_ci	if (fd[0] == INVALID_SOCKET)
54d4afb5ceSopenharmony_ci		goto bail;
55d4afb5ceSopenharmony_ci
56d4afb5ceSopenharmony_ci	fd[1] = socket(AF_INET, SOCK_DGRAM, 0);
57d4afb5ceSopenharmony_ci	if (fd[1] == INVALID_SOCKET)
58d4afb5ceSopenharmony_ci		goto bail;
59d4afb5ceSopenharmony_ci
60d4afb5ceSopenharmony_ci	/*
61d4afb5ceSopenharmony_ci	 * No need for memset since it's in zalloc'd context... it's in the
62d4afb5ceSopenharmony_ci	 * context so we can reuse the prepared sockaddr to send tp fd[0] whem
63d4afb5ceSopenharmony_ci	 * we want to cancel the wait
64d4afb5ceSopenharmony_ci	 */
65d4afb5ceSopenharmony_ci
66d4afb5ceSopenharmony_ci	si->sin_family = AF_INET;
67d4afb5ceSopenharmony_ci	si->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
68d4afb5ceSopenharmony_ci	si->sin_port = 0;
69d4afb5ceSopenharmony_ci
70d4afb5ceSopenharmony_ci	if (bind(fd[0], (const struct sockaddr *)si, sizeof(*si)) < 0)
71d4afb5ceSopenharmony_ci		goto bail;
72d4afb5ceSopenharmony_ci
73d4afb5ceSopenharmony_ci	/*
74d4afb5ceSopenharmony_ci	 * Query the socket to set pt->frt_pipe_si to the full sockaddr it
75d4afb5ceSopenharmony_ci	 * wants to be addressed by, including the port that the os chose.
76d4afb5ceSopenharmony_ci	 *
77d4afb5ceSopenharmony_ci	 * Afterwards, we can use this prepared sockaddr stashed in the context
78d4afb5ceSopenharmony_ci	 * to trigger the "pipe" without any other preliminaries.
79d4afb5ceSopenharmony_ci	 */
80d4afb5ceSopenharmony_ci
81d4afb5ceSopenharmony_ci	sl = sizeof(*si);
82d4afb5ceSopenharmony_ci	if (getsockname(fd[0], (struct sockaddr *)si, &sl))
83d4afb5ceSopenharmony_ci		goto bail;
84d4afb5ceSopenharmony_ci
85d4afb5ceSopenharmony_ci	lwsl_info("%s: cancel UDP skt port %d\n", __func__,
86d4afb5ceSopenharmony_ci		  ntohs(si->sin_port));
87d4afb5ceSopenharmony_ci
88d4afb5ceSopenharmony_ci	return 0;
89d4afb5ceSopenharmony_ci
90d4afb5ceSopenharmony_cibail:
91d4afb5ceSopenharmony_ci	lwsl_err("%s: failed\n", __func__);
92d4afb5ceSopenharmony_ci
93d4afb5ceSopenharmony_ci	return 1;
94d4afb5ceSopenharmony_ci}
95d4afb5ceSopenharmony_ci
96d4afb5ceSopenharmony_ciint
97d4afb5ceSopenharmony_cilws_plat_pipe_signal(struct lws_context *ctx, int tsi)
98d4afb5ceSopenharmony_ci{
99d4afb5ceSopenharmony_ci	struct lws_context_per_thread *pt = &ctx->pt[tsi];
100d4afb5ceSopenharmony_ci	struct sockaddr_in *si = &pt->frt_pipe_si;
101d4afb5ceSopenharmony_ci	lws_sockfd_type *fd = pt->dummy_pipe_fds;
102d4afb5ceSopenharmony_ci	char u = 0;
103d4afb5ceSopenharmony_ci	int n;
104d4afb5ceSopenharmony_ci
105d4afb5ceSopenharmony_ci	/*
106d4afb5ceSopenharmony_ci	 * Send a single UDP byte payload to the listening socket fd[0], forcing
107d4afb5ceSopenharmony_ci	 * the event loop wait to wake.  fd[1] and context->frt_pipe_si are
108d4afb5ceSopenharmony_ci	 * set at pt creation and are static.
109d4afb5ceSopenharmony_ci	 */
110d4afb5ceSopenharmony_ci
111d4afb5ceSopenharmony_ci	n = sendto(fd[1], &u, 1, 0, (struct sockaddr *)si, sizeof(*si));
112d4afb5ceSopenharmony_ci
113d4afb5ceSopenharmony_ci	return n != 1;
114d4afb5ceSopenharmony_ci}
115d4afb5ceSopenharmony_ci
116d4afb5ceSopenharmony_civoid
117d4afb5ceSopenharmony_cilws_plat_pipe_close(struct lws *wsi)
118d4afb5ceSopenharmony_ci{
119d4afb5ceSopenharmony_ci	struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
120d4afb5ceSopenharmony_ci
121d4afb5ceSopenharmony_ci	if (pt->dummy_pipe_fds[0] && pt->dummy_pipe_fds[0] != LWS_SOCK_INVALID)
122d4afb5ceSopenharmony_ci		closesocket(pt->dummy_pipe_fds[0]);
123d4afb5ceSopenharmony_ci	if (pt->dummy_pipe_fds[1] && pt->dummy_pipe_fds[1] != LWS_SOCK_INVALID)
124d4afb5ceSopenharmony_ci		closesocket(pt->dummy_pipe_fds[1]);
125d4afb5ceSopenharmony_ci
126d4afb5ceSopenharmony_ci	pt->dummy_pipe_fds[0] = pt->dummy_pipe_fds[1] = LWS_SOCK_INVALID;
127d4afb5ceSopenharmony_ci}
128