Project homepage Mailing List  Warmcat.com  API Docs  Github Mirror 
{"schema":"libjg2-1", "vpath":"/git/", "avatar":"/git/avatar/", "alang":"", "gen_ut":1731493555, "reponame":"libwebsockets", "desc":"libwebsockets lightweight C networking library", "owner": { "name": "Andy Green", "email": "andy@warmcat.com", "md5": "c50933ca2aa61e0fe2c43d46bb6b59cb" },"url":"https://libwebsockets.org/repo/libwebsockets", "f":3, "items": [ {"schema":"libjg2-1", "cid":"a3de0d52c2e0045523cb9ced4814bb5b", "oid":{ "oid": "6328231f2aa628632344d96a58e25c3b194af506", "alias": [ "refs/heads/main"]},"blobname": "lib/plat/unix/unix-sockets.c", "blob": "/*\n * libwebsockets - small server side websockets and web server implementation\n *\n * Copyright (C) 2010 - 2023 Andy Green \u003candy@warmcat.com\u003e\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \u0022Software\u0022), to\n * deal in the Software without restriction, including without limitation the\n * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or\n * sell copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \u0022AS IS\u0022, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n * IN THE SOFTWARE.\n */\n\n#if !defined(_GNU_SOURCE)\n#define _GNU_SOURCE\n#endif\n#include \u0022private-lib-core.h\u0022\n\n#if defined(LWS_HAVE_LINUX_IPV6_H)\n#include \u003clinux/ipv6.h\u003e\n#endif\n\n#include \u003csys/ioctl.h\u003e\n\n#if !defined(LWS_DETECTED_PLAT_IOS)\n#include \u003cnet/route.h\u003e\n#endif\n\n#include \u003cnet/if.h\u003e\n\n#include \u003cpwd.h\u003e\n#include \u003cgrp.h\u003e\n\n#if defined(LWS_WITH_MBEDTLS)\n#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)\n#include \u0022mbedtls/net_sockets.h\u0022\n#else\n#include \u0022mbedtls/net.h\u0022\n#endif\n#endif\n\n#include \u003cnetinet/ip.h\u003e\n\nint\nlws_send_pipe_choked(struct lws *wsi)\n{\n\tstruct lws_pollfd fds;\n\tstruct lws *wsi_eff;\n\n#if !defined(LWS_WITHOUT_EXTENSIONS)\n\tif (wsi-\u003ews \u0026\u0026 wsi-\u003ews-\u003etx_draining_ext)\n\t\treturn 1;\n#endif\n\n#if defined(LWS_WITH_HTTP2)\n\twsi_eff \u003d lws_get_network_wsi(wsi);\n#else\n\twsi_eff \u003d wsi;\n#endif\n\n\t/* the fact we checked implies we avoided back-to-back writes */\n\twsi_eff-\u003ecould_have_pending \u003d 0;\n\n\t/* treat the fact we got a truncated send pending as if we're choked */\n\tif (lws_has_buffered_out(wsi_eff)\n#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)\n\t ||wsi-\u003ehttp.comp_ctx.buflist_comp ||\n\t wsi-\u003ehttp.comp_ctx.may_have_more\n#endif\n\t )\n\t\treturn 1;\n\n\tfds.fd \u003d wsi_eff-\u003edesc.sockfd;\n\tfds.events \u003d POLLOUT;\n\tfds.revents \u003d 0;\n\n\tif (poll(\u0026fds, 1, 0) !\u003d 1)\n\t\treturn 1;\n\n\tif ((fds.revents \u0026 POLLOUT) \u003d\u003d 0)\n\t\treturn 1;\n\n\t/* okay to send another packet without blocking */\n\n\treturn 0;\n}\n\nint\nlws_plat_set_nonblocking(lws_sockfd_type fd)\n{\n\treturn fcntl(fd, F_SETFL, O_NONBLOCK) \u003c 0;\n}\n\nint\nlws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)\n{\n\tint optval \u003d 1;\n\tsocklen_t optlen \u003d sizeof(optval);\n\n#if defined(__APPLE__) || \u005c\n defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \u005c\n defined(__NetBSD__) || \u005c\n defined(__OpenBSD__) || \u005c\n defined(__HAIKU__)\n\tstruct protoent *tcp_proto;\n#endif\n\n\t(void)fcntl(fd, F_SETFD, FD_CLOEXEC);\n\n\tif (!unix_skt \u0026\u0026 vhost-\u003eka_time) {\n\t\t/* enable keepalive on this socket */\n\t\toptval \u003d 1;\n\t\tif (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,\n\t\t\t (const void *)\u0026optval, optlen) \u003c 0)\n\t\t\treturn 1;\n\n#if defined(__APPLE__) || \u005c\n defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \u005c\n defined(__NetBSD__) || \u005c\n defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \u005c\n defined(__HAIKU__)\n\n\t\t/*\n\t\t * didn't find a way to set these per-socket, need to\n\t\t * tune kernel systemwide values\n\t\t */\n#else\n\t\t/* set the keepalive conditions we want on it too */\n\n#if defined(LWS_HAVE_TCP_USER_TIMEOUT)\n\t\toptval \u003d 1000 * (vhost-\u003eka_time +\n\t\t\t\t (vhost-\u003eka_interval * vhost-\u003eka_probes));\n\t\tif (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,\n\t\t\t (const void *)\u0026optval, optlen) \u003c 0)\n\t\t\treturn 1;\n#endif\n\t\toptval \u003d vhost-\u003eka_time;\n\t\tif (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,\n\t\t\t (const void *)\u0026optval, optlen) \u003c 0)\n\t\t\treturn 1;\n\n\t\toptval \u003d vhost-\u003eka_interval;\n\t\tif (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,\n\t\t\t (const void *)\u0026optval, optlen) \u003c 0)\n\t\t\treturn 1;\n\n\t\toptval \u003d vhost-\u003eka_probes;\n\t\tif (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,\n\t\t\t (const void *)\u0026optval, optlen) \u003c 0)\n\t\t\treturn 1;\n#endif\n\t}\n\n#if defined(SO_BINDTODEVICE)\n\tif (!unix_skt \u0026\u0026 vhost-\u003ebind_iface \u0026\u0026 vhost-\u003eiface) {\n\t\tlwsl_info(\u0022binding listen skt to %s using SO_BINDTODEVICE\u005cn\u0022, vhost-\u003eiface);\n\t\tif (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost-\u003eiface,\n\t\t\t\t(socklen_t)strlen(vhost-\u003eiface)) \u003c 0) {\n\t\t\tlwsl_warn(\u0022Failed to bind to device %s\u005cn\u0022, vhost-\u003eiface);\n\t\t\treturn 1;\n\t\t}\n\t}\n#endif\n\n\t/* Disable Nagle */\n\toptval \u003d 1;\n#if defined (__sun) || defined(__QNX__) || defined(__NuttX__)\n\tif (!unix_skt \u0026\u0026 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)\u0026optval, optlen) \u003c 0)\n\t\treturn 1;\n#elif !defined(__APPLE__) \u0026\u0026 \u005c\n !defined(__FreeBSD__) \u0026\u0026 !defined(__FreeBSD_kernel__) \u0026\u0026 \u005c\n !defined(__NetBSD__) \u0026\u0026 \u005c\n !defined(__OpenBSD__) \u0026\u0026 \u005c\n !defined(__HAIKU__)\n\tif (!unix_skt \u0026\u0026 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)\u0026optval, optlen) \u003c 0)\n\t\treturn 1;\n#else\n\ttcp_proto \u003d getprotobyname(\u0022TCP\u0022);\n\tif (!unix_skt \u0026\u0026 setsockopt(fd, tcp_proto-\u003ep_proto, TCP_NODELAY, \u0026optval, optlen) \u003c 0)\n\t\treturn 1;\n#endif\n\n\treturn lws_plat_set_nonblocking(fd);\n}\n\n#if !defined(__NuttX__)\nstatic const int ip_opt_lws_flags[] \u003d {\n\tLCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT\n#if !defined(__OpenBSD__) \u0026\u0026 !defined(__sun) \u0026\u0026 !defined(__QNX__)\n\t, LCCSCF_IP_HIGH_RELIABILITY\n\t, LCCSCF_IP_LOW_COST\n#endif\n}, ip_opt_val[] \u003d {\n\tIPTOS_LOWDELAY, IPTOS_THROUGHPUT\n#if !defined(__OpenBSD__) \u0026\u0026 !defined(__sun) \u0026\u0026 !defined(__QNX__)\n\t, IPTOS_RELIABILITY\n\t, IPTOS_MINCOST\n#endif\n};\n#if !defined(LWS_WITH_NO_LOGS)\nstatic const char *ip_opt_names[] \u003d {\n\t\u0022LOWDELAY\u0022, \u0022THROUGHPUT\u0022\n#if !defined(__OpenBSD__) \u0026\u0026 !defined(__sun) \u0026\u0026 !defined(__QNX__)\n\t, \u0022RELIABILITY\u0022\n\t, \u0022MINCOST\u0022\n#endif\n};\n#endif\n#endif\n\nint\nlws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)\n{\n\tint optval \u003d (int)pri, ret \u003d 0, n;\n\tsocklen_t optlen \u003d sizeof(optval);\n#if (_LWS_ENABLED_LOGS \u0026 LLL_WARN)\n\tint en;\n#endif\n\n#if 0\n#if defined(TCP_FASTOPEN_CONNECT)\n\toptval \u003d 1;\n\tif (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)\u0026optval,\n\t\t sizeof(optval)))\n\t\tlwsl_warn(\u0022%s: FASTOPEN_CONNECT failed\u005cn\u0022, __func__);\n\toptval \u003d (int)pri;\n#endif\n#endif\n\n#if !defined(__APPLE__) \u0026\u0026 \u005c\n !defined(__FreeBSD__) \u0026\u0026 !defined(__FreeBSD_kernel__) \u0026\u0026 \u005c\n !defined(__NetBSD__) \u0026\u0026 \u005c\n !defined(__OpenBSD__) \u0026\u0026 \u005c\n !defined(__sun) \u0026\u0026 \u005c\n !defined(__HAIKU__) \u0026\u0026 \u005c\n !defined(__CYGWIN__) \u0026\u0026 \u005c\n !defined(__QNX__) \u0026\u0026 \u005c\n !defined(__NuttX__)\n\n\t/* the BSDs don't have SO_PRIORITY */\n\n\tif (pri) { /* 0 is the default already */\n\t\tif (setsockopt(fd, SOL_SOCKET, SO_PRIORITY,\n\t\t\t\t(const void *)\u0026optval, optlen) \u003c 0) {\n#if (_LWS_ENABLED_LOGS \u0026 LLL_WARN)\n\t\t\ten \u003d errno;\n\t\t\tlwsl_warn(\u0022%s: unable to set socket pri %d: errno %d\u005cn\u0022,\n\t\t\t\t __func__, (int)pri, en);\n#endif\n\t\t\tret \u003d 1;\n\t\t} else\n\t\t\tlwsl_notice(\u0022%s: set pri %u\u005cn\u0022, __func__, pri);\n\t}\n#endif\n\n\tif (lws_flags \u0026 LCCSCF_ALLOW_REUSE_ADDR) {\n\t\toptval \u003d 1;\n\t\tif (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,\n\t\t\t\t\t(const void *)\u0026optval, optlen) \u003c 0) { \n#if (_LWS_ENABLED_LOGS \u0026 LLL_WARN)\n\t\t\ten \u003d errno;\n\t\t\tlwsl_warn(\u0022%s: unable to reuse local addresses: errno %d\u005cn\u0022,\n\t\t\t\t__func__, en);\n#endif\n\t\t\tret \u003d 1;\n\t\t} else\n\t\t\tlwsl_notice(\u0022%s: set reuse addresses\u005cn\u0022, __func__);\n\t}\n\n\n\tif (lws_flags \u0026 LCCSCF_IPV6_PREFER_PUBLIC_ADDR) {\n#if defined(LWS_WITH_IPV6) \u0026\u0026 defined(IPV6_PREFER_SRC_PUBLIC)\n\t\toptval \u003d IPV6_PREFER_SRC_PUBLIC;\n\n\t\tif (setsockopt(fd, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES,\n\t\t\t\t\t\t(const void *)\u0026optval, optlen) \u003c 0) {\n\t\t\t\t#if (_LWS_ENABLED_LOGS \u0026 LLL_WARN)\n\t\t\t\t\ten \u003d errno;\n\t\t\t\t\tlwsl_warn(\u0022%s: unable to set IPV6_PREFER_SRC_PUBLIC: errno %d\u005cn\u0022,\n\t\t\t\t\t\t__func__, en);\n\t\t\t\t#endif\n\t\t\t\tret \u003d 1;\n\t\t} else\n\t\t\tlwsl_notice(\u0022%s: set IPV6_PREFER_SRC_PUBLIC\u005cn\u0022, __func__);\n#else\n\t\tlwsl_err(\u0022%s: IPV6_PREFER_SRC_PUBLIC UNIMPLEMENTED on this platform\u005cn\u0022, __func__);\n#endif\n\t}\n\n\n#if !defined(__NuttX__)\n\t/* array size differs by platform */\n\tfor (n \u003d 0; n \u003c (int)LWS_ARRAY_SIZE(ip_opt_lws_flags); n++) {\n\t\tif (!(lws_flags \u0026 ip_opt_lws_flags[n]))\n\t\t\tcontinue;\n\n\t\toptval \u003d (int)ip_opt_val[n];\n\t\tif (setsockopt(fd, IPPROTO_IP, IP_TOS, (const void *)\u0026optval,\n\t\t\t optlen) \u003c 0) {\n#if !defined(LWS_WITH_NO_LOGS)\n\t\t\ten \u003d errno;\n\t\t\tlwsl_warn(\u0022%s: unable to set %s: errno %d\u005cn\u0022, __func__,\n\t\t\t\t ip_opt_names[n], en);\n#endif\n\t\t\tret \u003d 1;\n\t\t} else\n\t\t\tlwsl_notice(\u0022%s: set ip flag %s\u005cn\u0022, __func__,\n\t\t\t\t ip_opt_names[n]);\n\t}\n#endif\n\n\treturn ret;\n}\n\n/* cast a struct sockaddr_in6 * into addr for ipv6 */\n\nenum {\n\tIP_SCORE_NONE,\n\tIP_SCORE_NONNATIVE,\n\tIP_SCORE_IPV6_SCOPE_BASE,\n\t/* ipv6 scopes */\n\tIP_SCORE_GLOBAL_NATIVE \u003d 18\n};\n\nint\nlws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,\n\t\t size_t addrlen)\n{\n\tint rc \u003d LWS_ITOSA_NOT_EXIST;\n\n\tstruct ifaddrs *ifr;\n\tstruct ifaddrs *ifc;\n#if defined(LWS_WITH_IPV6)\n\tstruct sockaddr_in6 *addr6 \u003d (struct sockaddr_in6 *)addr;\n\tunsigned long sco \u003d IP_SCORE_NONE;\n\tunsigned long ts;\n\tconst uint8_t *p;\n#endif\n\n\tif (getifaddrs(\u0026ifr)) {\n\t\tlwsl_err(\u0022%s: unable to getifaddrs: errno %d\u005cn\u0022, __func__, errno);\n\n\t\treturn LWS_ITOSA_USABLE;\n\t}\n\tfor (ifc \u003d ifr; ifc !\u003d NULL; ifc \u003d ifc-\u003eifa_next) {\n\t\tif (!ifc-\u003eifa_addr || !ifc-\u003eifa_name)\n\t\t\tcontinue;\n\n\t\tlwsl_debug(\u0022 interface %s vs %s (fam %d) ipv6 %d\u005cn\u0022,\n\t\t\t ifc-\u003eifa_name, ifname,\n\t\t\t ifc-\u003eifa_addr-\u003esa_family, ipv6);\n\n\t\tif (strcmp(ifc-\u003eifa_name, ifname))\n\t\t\tcontinue;\n\n\t\tswitch (ifc-\u003eifa_addr-\u003esa_family) {\n#if defined(AF_PACKET)\n\t\tcase AF_PACKET:\n\t\t\t/* interface exists but is not usable */\n\t\t\tif (rc \u003d\u003d LWS_ITOSA_NOT_EXIST)\n\t\t\t\trc \u003d LWS_ITOSA_NOT_USABLE;\n\t\t\tcontinue;\n#endif\n\n\t\tcase AF_INET:\n#if defined(LWS_WITH_IPV6)\n\t\t\tif (ipv6) {\n\t\t\t\t/* any existing solution is better than this */\n\t\t\t\tif (sco !\u003d IP_SCORE_NONE)\n\t\t\t\t\tbreak;\n\t\t\t\tsco \u003d IP_SCORE_NONNATIVE;\n\t\t\t\trc \u003d LWS_ITOSA_USABLE;\n\t\t\t\t/* map IPv4 to IPv6 */\n\t\t\t\tmemset((char *)\u0026addr6-\u003esin6_addr, 0,\n\t\t\t\t\t\tsizeof(struct in6_addr));\n\t\t\t\taddr6-\u003esin6_addr.s6_addr[10] \u003d 0xff;\n\t\t\t\taddr6-\u003esin6_addr.s6_addr[11] \u003d 0xff;\n\t\t\t\tmemcpy(\u0026addr6-\u003esin6_addr.s6_addr[12],\n\t\t\t\t \u0026((struct sockaddr_in *)ifc-\u003eifa_addr)-\u003esin_addr,\n\t\t\t\t\t\t\tsizeof(struct in_addr));\n\t\t\t\tlwsl_debug(\u0022%s: uplevelling ipv4 bind to ipv6\u005cn\u0022, __func__);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tsco \u003d IP_SCORE_GLOBAL_NATIVE;\n#endif\n\t\t\trc \u003d LWS_ITOSA_USABLE;\n\t\t\tmemcpy(addr, (struct sockaddr_in *)ifc-\u003eifa_addr,\n\t\t\t\t\t\t sizeof(struct sockaddr_in));\n\t\t\tbreak;\n#if defined(LWS_WITH_IPV6)\n\t\tcase AF_INET6:\n\t\t\tp \u003d (const uint8_t *)\n\t\t\t\t\u0026((struct sockaddr_in6 *)ifc-\u003eifa_addr)-\u003esin6_addr;\n\t\t\tts \u003d IP_SCORE_IPV6_SCOPE_BASE;\n\t\t\tif (p[0] \u003d\u003d 0xff)\n\t\t\t\tts \u003d (unsigned long)(IP_SCORE_IPV6_SCOPE_BASE + (p[1] \u0026 0xf));\n\n\t\t\tif (sco \u003e\u003d ts)\n\t\t\t\tbreak;\n\n\t\t\tsco \u003d ts;\n\t\t\trc \u003d LWS_ITOSA_USABLE;\n\n\t\t\tmemcpy(\u0026addr6-\u003esin6_addr,\n\t\t\t \u0026((struct sockaddr_in6 *)ifc-\u003eifa_addr)-\u003esin6_addr,\n\t\t\t\t\t\t sizeof(struct in6_addr));\n\t\t\tbreak;\n#endif\n\t\tdefault:\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tfreeifaddrs(ifr);\n\n\tif (rc \u0026\u0026\n\t !lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))\n\t\trc \u003d LWS_ITOSA_USABLE;\n\n\treturn rc;\n}\n\n\nconst char *\nlws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)\n{\n\treturn inet_ntop(af, src, dst, cnt);\n}\n\nint\nlws_plat_inet_pton(int af, const char *src, void *dst)\n{\n\treturn inet_pton(af, src, dst);\n}\n\nint\nlws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)\n{\n#if defined(__linux__)\n\tstruct ifreq i;\n\n\tmemset(\u0026i, 0, sizeof(i));\n\tlws_strncpy(i.ifr_name, ifname, sizeof(i.ifr_name));\n\n\tif (ioctl(fd, SIOCGIFHWADDR, \u0026i) \u003c 0)\n\t\treturn -1;\n\n\tmemcpy(hwaddr, \u0026i.ifr_hwaddr.sa_data, 6);\n\n\treturn 6;\n#else\n\tlwsl_err(\u0022%s: UNIMPLEMENTED on this platform\u005cn\u0022, __func__);\n\n\treturn -1;\n#endif\n}\n\nint\nlws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,\n\t\t\t size_t n, int fd, const char *iface)\n{\n#if defined(__linux__)\n\tstruct sockaddr_ll sll;\n\tuint16_t *p16 \u003d (uint16_t *)p;\n\tuint32_t ucs \u003d 0;\n\n\tmemcpy(p, canned, canned_len);\n\n\tp[2] \u003d (uint8_t)(n \u003e\u003e 8);\n\tp[3] \u003d (uint8_t)(n);\n\n\twhile (p16 \u003c (uint16_t *)(p + 20))\n\t\tucs \u003d ucs + (uint32_t)(ntohs((uint16_t)(*p16++)));\n\n\tucs +\u003d ucs \u003e\u003e 16;\n\tucs ^\u003d 0xffff;\n\n\tp[10] \u003d (uint8_t)(ucs \u003e\u003e 8);\n\tp[11] \u003d (uint8_t)(ucs);\n\tp[24] \u003d (uint8_t)((n - 20) \u003e\u003e 8);\n\tp[25] \u003d (uint8_t)((n - 20));\n\n\tmemset(\u0026sll, 0, sizeof(sll));\n\tsll.sll_family \u003d AF_PACKET;\n\tsll.sll_protocol \u003d (uint32_t)(htons((uint16_t)0x800));\n\tsll.sll_halen \u003d 6;\n\tsll.sll_ifindex \u003d (int)if_nametoindex(iface);\n\tmemset(sll.sll_addr, 0xff, 6);\n\n\treturn (int)sendto(fd, p, n, 0, (struct sockaddr *)\u0026sll, sizeof(sll));\n#else\n\tlwsl_err(\u0022%s: UNIMPLEMENTED on this platform\u005cn\u0022, __func__);\n\n\treturn -1;\n#endif\n}\n\nint\nlws_plat_if_up(const char *ifname, int fd, int up)\n{\n#if defined(__linux__)\n\tstruct ifreq ifr;\n\n\tmemset(\u0026ifr, 0, sizeof(ifr));\n\tlws_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);\n\n\tif (ioctl(fd, SIOCGIFFLAGS, \u0026ifr) \u003c 0) {\n\t\tlwsl_err(\u0022%s: SIOCGIFFLAGS fail\u005cn\u0022, __func__);\n\t\treturn 1;\n\t}\n\n\tif (up)\n\t\tifr.ifr_flags |\u003d IFF_UP;\n\telse\n\t\tifr.ifr_flags \u0026\u003d ~IFF_UP;\n\n\tif (ioctl(fd, SIOCSIFFLAGS, \u0026ifr) \u003c 0) {\n\t\tlwsl_err(\u0022%s: SIOCSIFFLAGS fail\u005cn\u0022, __func__);\n\t\treturn 1;\n\t}\n\n\treturn 0;\n#else\n\tlwsl_err(\u0022%s: UNIMPLEMENTED on this platform\u005cn\u0022, __func__);\n\n\treturn -1;\n#endif\n}\n\nint\nlws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)\n{\n#if defined(__linux__)\n\tstruct ifreq i;\n\n\tmemset(\u0026i, 0, sizeof(i));\n\ti.ifr_addr.sa_family \u003d AF_INET;\n\tlws_strncpy(i.ifr_ifrn.ifrn_name, ifname,\n\t\t sizeof(i.ifr_ifrn.ifrn_name));\n\tif (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, \u0026i, sizeof(i)) \u003c 0) {\n\t\tlwsl_notice(\u0022%s: failed %d\u005cn\u0022, __func__, LWS_ERRNO);\n\t\treturn 1;\n\t}\n\n\treturn 0;\n#else\n\tlwsl_err(\u0022%s: UNIMPLEMENTED on this platform\u005cn\u0022, __func__);\n\n\treturn -1;\n#endif\n}\n\nint\nlws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)\n{\n#if defined(__linux__)\n\tstruct rtentry route;\n\tstruct ifreq ifr;\n\n\tmemset(\u0026ifr, 0, sizeof(ifr));\n\tmemset(\u0026route, 0, sizeof(route));\n\n\tlws_strncpy(ifr.ifr_name, is-\u003eifname, IFNAMSIZ);\n\n\tlws_plat_if_up(is-\u003eifname, fd, 0);\n\n\tmemcpy(\u0026ifr.ifr_addr, \u0026is-\u003esa46[LWSDH_SA46_IP], sizeof(struct sockaddr));\n\tif (ioctl(fd, SIOCSIFADDR, \u0026ifr) \u003c 0) {\n\t\tlwsl_err(\u0022%s: SIOCSIFADDR fail\u005cn\u0022, __func__);\n\t\treturn 1;\n\t}\n\n\tif (is-\u003esa46[LWSDH_SA46_IP].sa4.sin_family \u003d\u003d AF_INET) {\n\t\tstruct sockaddr_in sin;\n\n\t\tmemset(\u0026sin, 0, sizeof(sin));\n\t\tsin.sin_family \u003d AF_INET;\n\t\tsin.sin_addr.s_addr \u003d *(uint32_t *)\u0026is-\u003enums[LWSDH_IPV4_SUBNET_MASK];\n\t\tmemcpy(\u0026ifr.ifr_addr, \u0026sin, sizeof(struct sockaddr));\n\t\tif (ioctl(fd, SIOCSIFNETMASK, \u0026ifr) \u003c 0) {\n\t\t\tlwsl_err(\u0022%s: SIOCSIFNETMASK fail\u005cn\u0022, __func__);\n\t\t\treturn 1;\n\t\t}\n\n\t\tlws_plat_if_up(is-\u003eifname, fd, 1);\n\n\t\tmemcpy(\u0026route.rt_gateway,\n\t\t \u0026is-\u003esa46[LWSDH_SA46_IPV4_ROUTER].sa4,\n\t\t sizeof(struct sockaddr));\n\n\t\tsin.sin_addr.s_addr \u003d 0;\n\t\tmemcpy(\u0026route.rt_dst, \u0026sin, sizeof(struct sockaddr));\n\t\tmemcpy(\u0026route.rt_genmask, \u0026sin, sizeof(struct sockaddr));\n\n\t\troute.rt_flags \u003d RTF_UP | RTF_GATEWAY;\n\t\troute.rt_metric \u003d 100;\n\t\troute.rt_dev \u003d (char *)is-\u003eifname;\n\n\t\tif (ioctl(fd, SIOCADDRT, \u0026route) \u003c 0) {\n\t\t\tlwsl_err(\u0022%s: SIOCADDRT 0x%x fail: %d\u005cn\u0022, __func__,\n\t\t\t\t(unsigned int)htonl(*(uint32_t *)\u0026is-\u003e\n\t\t\t\t\tsa46[LWSDH_SA46_IPV4_ROUTER].\n\t\t\t\t\t\tsa4.sin_addr.s_addr), LWS_ERRNO);\n\t\t\treturn 1;\n\t\t}\n\t} else\n\t\tlws_plat_if_up(is-\u003eifname, fd, 1);\n\n\treturn 0;\n#else\n\tlwsl_err(\u0022%s: UNIMPLEMENTED on this platform\u005cn\u0022, __func__);\n\n\treturn -1;\n#endif\n}\n\nint\nlws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)\n{\n\treturn 0;\n}\n\n#if defined(LWS_WITH_MBEDTLS)\nint\nlws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)\n{\n\tint fd \u003d ((mbedtls_net_context *) ctx)-\u003eMBEDTLS_PRIVATE_V30_ONLY(fd);\n\tint ret;\n\n\tif (fd \u003c 0)\n\t\treturn MBEDTLS_ERR_NET_INVALID_CONTEXT;\n\n\tret \u003d (int)write(fd, buf, len);\n\tif (ret \u003e\u003d 0)\n\t\treturn ret;\n\n\tif (errno \u003d\u003d EAGAIN || errno \u003d\u003d EWOULDBLOCK)\n\t\treturn MBEDTLS_ERR_SSL_WANT_WRITE;\n\n\tif (errno \u003d\u003d EPIPE || errno \u003d\u003d ECONNRESET)\n\t\treturn MBEDTLS_ERR_NET_CONN_RESET;\n\n\tif( errno \u003d\u003d EINTR )\n\t\treturn MBEDTLS_ERR_SSL_WANT_WRITE;\n\n\treturn MBEDTLS_ERR_NET_SEND_FAILED;\n}\n\nint\nlws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)\n{\n\tint fd \u003d ((mbedtls_net_context *) ctx)-\u003eMBEDTLS_PRIVATE_V30_ONLY(fd);\n\tint ret;\n\n\tif (fd \u003c 0)\n\t\treturn MBEDTLS_ERR_NET_INVALID_CONTEXT;\n\n\tret \u003d (int)read(fd, buf, len);\n\tif (ret \u003e\u003d 0)\n\t\treturn ret;\n\n\tif (errno \u003d\u003d EAGAIN || errno \u003d\u003d EWOULDBLOCK)\n\t\treturn MBEDTLS_ERR_SSL_WANT_READ;\n\n\tif (errno \u003d\u003d EPIPE || errno \u003d\u003d ECONNRESET)\n\t\treturn MBEDTLS_ERR_NET_CONN_RESET;\n\n\tif (errno \u003d\u003d EINTR)\n\t\treturn MBEDTLS_ERR_SSL_WANT_READ;\n\n\treturn MBEDTLS_ERR_NET_RECV_FAILED;\n}\n#endif\n","s":{"c":1731461441,"u": 1442}} ],"g": 2441,"chitpc": 0,"ehitpc": 0,"indexed":0 , "ab": 0, "si": 0, "db":0, "di":0, "sat":0, "lfc": "7d0a"}