1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2020 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 #include "private-lib-core.h"
26
27 int
lws_plat_pipe_create(struct lws * wsi)28 lws_plat_pipe_create(struct lws *wsi)
29 {
30 struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
31 struct sockaddr_in *si = &wsi->a.context->frt_pipe_si;
32 lws_sockfd_type *fd = pt->dummy_pipe_fds;
33 socklen_t sl;
34
35 /*
36 * There's no pipe abstraction on lwip / freertos... use a UDP socket
37 * listening on 127.0.0.1:xxxx and send a byte to it from a second UDP
38 * socket to cancel the wait.
39 *
40 * Set the port to 0 at the bind, so lwip will choose a free one in the
41 * ephemeral range for us.
42 */
43
44 fd[0] = socket(AF_INET, SOCK_DGRAM, 0);
45 if (fd[0] < 0)
46 goto bail;
47
48 fd[1] = socket(AF_INET, SOCK_DGRAM, 0);
49 if (fd[1] < 0)
50 goto bail;
51
52 /*
53 * No need for memset since it's in zalloc'd context... it's in the
54 * context so we can reuse the prepared sockaddr to send tp fd[0] whem
55 * we want to cancel the wait
56 */
57
58 si->sin_family = AF_INET;
59 si->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
60 si->sin_port = 0;
61
62 if (bind(fd[0], (const struct sockaddr *)si, sizeof(*si)) < 0)
63 goto bail;
64
65 /*
66 * Query the socket to set context->frt_pipe_si to the full sockaddr it
67 * wants to be addressed by, including the port that lwip chose.
68 *
69 * Afterwards, we can use this prepared sockaddr stashed in the context
70 * to trigger the "pipe" without any other preliminaries.
71 */
72
73 sl = sizeof(*si);
74 if (getsockname(fd[0], (struct sockaddr *)si, &sl))
75 goto bail;
76
77 lwsl_info("%s: cancel UDP skt port %d\n", __func__,
78 ntohs(si->sin_port));
79
80 return 0;
81
82 bail:
83 lwsl_err("%s: failed\n", __func__);
84
85 return 1;
86 }
87
88 int
lws_plat_pipe_signal(struct lws_context * ctx,int tsi)89 lws_plat_pipe_signal(struct lws_context *ctx, int tsi)
90 {
91 struct lws_context_per_thread *pt = &ctx->pt[tsi];
92 struct sockaddr_in *si = &ctx->frt_pipe_si;
93 lws_sockfd_type *fd = pt->dummy_pipe_fds;
94 uint8_t u = 0;
95 int n;
96
97 /*
98 * Send a single UDP byte payload to the listening socket fd[0], forcing
99 * the event loop wait to wake. fd[1] and context->frt_pipe_si are
100 * set at context creation and are static, the UDP sendto is supposed to
101 * be threadsafe for lwip:
102 *
103 * https://lwip.fandom.com/wiki/LwIP_and_multithreading
104 *
105 * Sockets generally can't be used by more than one application thread
106 * (on udp/raw netconn, doing a sendto/recv is currently possible).
107 */
108
109 n = sendto(fd[1], &u, 1, 0, (struct sockaddr *)si, sizeof(*si));
110
111 return n != 1;
112 }
113
114 void
lws_plat_pipe_close(struct lws * wsi)115 lws_plat_pipe_close(struct lws *wsi)
116 {
117 struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
118 lws_sockfd_type *fd = pt->dummy_pipe_fds;
119
120 if (fd[0] && fd[0] != -1)
121 close(fd[0]);
122 if (fd[1] && fd[1] != -1)
123 close(fd[1]);
124
125 fd[0] = fd[1] = -1;
126 }
127