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