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