diff --git a/BUILD.gn b/BUILD.gn index d3309d557bac1dcb9a2faa27109ea7c4907785d5..590da7be1ba0fbb786c88e1c2ba2c488ff93230d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -15,11 +15,6 @@ import("//build/ohos.gni") platform = "${current_os}_${current_cpu}" -if (target_os == "ios") { - libwebsockets_path = rebase_path("//third_party/libwebsockets") - exec_script("for_ios.sh", [ "$libwebsockets_path" ]) -} - ohos_static_library("websockets") { sources = [ "//third_party/libwebsockets/lib/core-net/adopt.c", @@ -35,9 +30,11 @@ ohos_static_library("websockets") { "//third_party/libwebsockets/lib/core-net/network.c", "//third_party/libwebsockets/lib/core-net/output.c", "//third_party/libwebsockets/lib/core-net/pollfd.c", + "//third_party/libwebsockets/lib/core-net/route.c", "//third_party/libwebsockets/lib/core-net/service.c", "//third_party/libwebsockets/lib/core-net/sorted-usec-list.c", "//third_party/libwebsockets/lib/core-net/state.c", + "//third_party/libwebsockets/lib/core-net/vhost.c", "//third_party/libwebsockets/lib/core-net/wsi-timeout.c", "//third_party/libwebsockets/lib/core-net/wsi.c", "//third_party/libwebsockets/lib/core/alloc.c", @@ -81,6 +78,7 @@ ohos_static_library("websockets") { "//third_party/libwebsockets/lib/roles/http/server/lws-spa.c", "//third_party/libwebsockets/lib/roles/http/server/server.c", "//third_party/libwebsockets/lib/roles/listen/ops-listen.c", + "//third_party/libwebsockets/lib/roles/netlink/ops-netlink.c", "//third_party/libwebsockets/lib/roles/pipe/ops-pipe.c", "//third_party/libwebsockets/lib/roles/raw-file/ops-raw-file.c", "//third_party/libwebsockets/lib/roles/raw-skt/ops-raw-skt.c", @@ -106,13 +104,6 @@ ohos_static_library("websockets") { part_name = "libwebsockets" subsystem_name = "thirdparty" - if (target_os != "ios") { - sources += [ - "//third_party/libwebsockets/lib/core-net/route.c", - "//third_party/libwebsockets/lib/roles/netlink/ops-netlink.c", - ] - } - include_dirs = [ "//third_party/libwebsockets/include", "//third_party/libwebsockets/include/libwebsockets", @@ -217,20 +208,10 @@ ohos_static_library("websockets") { "//third_party/glib", ] - if (target_os == "ios") { - sources += [ "//third_party/libwebsockets/lib/core-net/vhost_ios.c" ] - } else { - sources += [ "//third_party/libwebsockets/lib/core-net/vhost.c" ] - } - defines = [ "OHOS_LIBWEBSOCKETS=1", "OPENSSL_SUPPRESS_DEPRECATED", ] - if (target_os == "ios") { - defines += [ "LWS_DETECTED_PLAT_IOS=1" ] - defines += [ "CROSS_PLATFORM_IOS_LIBWEBSOCKETS=1" ] - } cflags = [ "-fPIC", @@ -247,7 +228,6 @@ ohos_static_library("websockets") { deps = [ "//third_party/openssl:libcrypto_shared", "//third_party/openssl:libssl_shared", - "//third_party/zlib:libz", ] } @@ -305,7 +285,7 @@ config("websockets_public_config") { if (platform == "mingw_x86_64") { include_dirs += [ "//third_party/libwebsockets/lib/plat/windows" ] } else if (platform == "mac_arm64" || platform == "mac_x64" || - platform == "linux_x64" || platform == "linux_arm64") { + platform == "linux_x64") { include_dirs += [ "//third_party/libwebsockets/lib/plat/unix" ] } cflags = [ "-Wno-error=#warnings" ] @@ -378,7 +358,7 @@ ohos_static_library("websockets_static") { "//third_party/libwebsockets/lib/plat/windows/windows-sockets.c", ] } else if (platform == "mac_arm64" || platform == "mac_x64" || - platform == "linux_x64" || platform == "linux_arm64") { + platform == "linux_x64") { defines = [ "LWS_HAVE_SYS_RESOURCE_H" ] sources += [ "//third_party/libwebsockets/lib/plat/unix/unix-caps.c", diff --git a/bundle.json b/bundle.json index ec52483f89d2c783c898bd7102ba297e06fbe185..4df4850c0b30d2e3ce34480aa9e4e643ba9ed315 100644 --- a/bundle.json +++ b/bundle.json @@ -19,10 +19,9 @@ "rom": "319KB", "ram": "13865KB", "deps": { - "components": [], + "components": [ "zlib" ], "third_party": [ - "openssl", - "zlib" + "openssl" ] }, "build": { diff --git a/for_ios.patch b/for_ios.patch deleted file mode 100755 index c3c398aedf6b5d0c5719303f40d0246b4f393cbd..0000000000000000000000000000000000000000 --- a/for_ios.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/lib/core-net/vhost_ios.c b/lib/core-net/vhost_ios.c -index 298bf01e..e8dc468e 100644 ---- a/lib/core-net/vhost_ios.c -+++ b/lib/core-net/vhost_ios.c -@@ -48,6 +48,8 @@ const struct lws_role_ops *available_roles[] = { - #endif - #if defined(LWS_WITH_NETLINK) - &role_ops_netlink, -+#else -+ NULL, - #endif - NULL - }; diff --git a/for_ios.sh b/for_ios.sh deleted file mode 100755 index fbc3cdbe42c68dc797f63435153bc4caf4bba48c..0000000000000000000000000000000000000000 --- a/for_ios.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2023 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e - -cd $1 -rm -f lib/core-net/vhost_ios.c -cp lib/core-net/vhost.c lib/core-net/vhost_ios.c -git apply for_ios.patch diff --git a/include/lws_config.h b/include/lws_config.h index cf9bdd02e13ca1f0076e1cd38666c7da6e788453..55dcbe379944c3896a617321a586d2c54bb2a4ad 100644 --- a/include/lws_config.h +++ b/include/lws_config.h @@ -89,9 +89,7 @@ #define LWS_HAVE_HMAC_CTX_new #define LWS_HAVE_MALLOC_H #define LWS_HAVE_MALLOC_TRIM -#if !defined (CROSS_PLATFORM_IOS_LIBWEBSOCKETS) #define LWS_HAVE_MALLOC_USABLE_SIZE -#endif /* #undef LWS_HAVE_mbedtls_md_setup */ /* #undef LWS_HAVE_mbedtls_net_init */ /* #undef LWS_HAVE_mbedtls_rsa_complete */ @@ -109,10 +107,8 @@ /* #undef LWS_HAVE_NEW_UV_VERSION_H */ #define LWS_HAVE_OPENSSL_ECDH_H #define LWS_HAVE_OPENSSL_STACK -#if !defined (CROSS_PLATFORM_IOS_LIBWEBSOCKETS) #define LWS_HAVE_PIPE2 #define LWS_HAVE_EVENTFD -#endif #define LWS_HAVE_PTHREAD_H #define LWS_HAVE_RSA_SET0_KEY /* #undef LWS_HAVE_RSA_verify_pss_mgf1 */ @@ -212,9 +208,7 @@ #define LWS_LOGS_TIMESTAMP /* #undef LWS_WITH_MBEDTLS */ /* #undef LWS_WITH_MINIZ */ -#if !defined (CROSS_PLATFORM_IOS_LIBWEBSOCKETS) #define LWS_WITH_NETLINK -#endif #define LWS_WITH_NETWORK /* #undef LWS_WITH_NO_LOGS */ #define LWS_WITH_CACHE_NSCOOKIEJAR diff --git a/include/lws_config_private.h b/include/lws_config_private.h index 248979444815dcea356bd64b23f029d1ecb903e8..1005421b18859e3a429edb22540449a5fdf2715a 100644 --- a/include/lws_config_private.h +++ b/include/lws_config_private.h @@ -71,10 +71,8 @@ /* Define to 1 if you have the header file. */ #define LWS_HAVE_STRING_H -#if !defined (CROSS_PLATFORM_IOS_LIBWEBSOCKETS) /* Define to 1 if you have the header file. */ #define LWS_HAVE_SYS_PRCTL_H -#endif /* Define to 1 if you have the header file. */ #define LWS_HAVE_SYS_RESOURCE_H diff --git a/lib/core-net/client/connect3.c b/lib/core-net/client/connect3.c index 9d669e3b5a1c69a0884fe249475cd5870bce0fad..c0c826f9859b12adeb597e3e5e021e15bfa2fd0a 100644 --- a/lib/core-net/client/connect3.c +++ b/lib/core-net/client/connect3.c @@ -24,6 +24,108 @@ #include "private-lib-core.h" +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +size_t get_res_size(struct addrinfo *res) +{ + size_t size = 0; + for (struct addrinfo *p = res; p; p = p->ai_next) { + if (p->ai_family != AF_INET && p->ai_family != AF_INET6) { + continue; + } + ++size; + } + return size; +} + +struct addrinfo **get_dns_res(struct addrinfo *res, sa_family_t family, size_t totalLength, size_t *size) +{ + struct addrinfo **temp = (struct addrinfo **)(malloc(sizeof(struct addrinfo *) * totalLength)); + if (!temp) { + return NULL; + } + size_t index = 0; + for (struct addrinfo *p = res; p; p = p->ai_next) { + if (p->ai_family == family) { + temp[index] = p; + ++index; + } + } + *size = index; + return temp; +} + +sa_family_t change_family(sa_family_t nowFamily) +{ + if (nowFamily == AF_INET6) { + return AF_INET; + } + return AF_INET6; +} + +struct addrinfo *sort_dns(struct addrinfo *res) +{ + size_t totalLength = get_res_size(res); + if (totalLength == 0) { + return NULL; + } + + size_t ipv6Size = 0; + struct addrinfo **ipv6Dns = get_dns_res(res, AF_INET6, totalLength, &ipv6Size); + size_t ipv4Size = 0; + struct addrinfo **ipv4Dns = get_dns_res(res, AF_INET, totalLength, &ipv4Size); + if (ipv4Dns == NULL && ipv6Dns == NULL) { + return NULL; + } + + for (size_t i = 0; i < ipv6Size; ++i) { + ipv6Dns[i]->ai_next = NULL; + } + for (size_t i = 0; i < ipv4Size; ++i) { + ipv4Dns[i]->ai_next = NULL; + } + + size_t ipv6Index = 0; + size_t ipv4Index = 0; + sa_family_t now = AF_INET6; + + struct addrinfo *head = (struct addrinfo *)malloc(sizeof(struct addrinfo)); + memset(head, 0, sizeof(struct addrinfo)); + struct addrinfo *next = head; + + size_t minSize = MIN(ipv6Size, ipv4Size); + size_t index = 0; + while (index < 2 * minSize) { + if (now == AF_INET6) { + next->ai_next = ipv6Dns[ipv6Index++]; + } else { + next->ai_next = ipv4Dns[ipv4Index++]; + } + ++index; + now = change_family(now); + next = next->ai_next; + } + while (ipv6Index < ipv6Size) { + next->ai_next = ipv6Dns[ipv6Index++]; + ++index; + next = next->ai_next; + } + while (ipv4Index < ipv4Size) { + next->ai_next = ipv4Dns[ipv4Index++]; + ++index; + next = next->ai_next; + } + struct addrinfo *result = head->ai_next; + free(head); + if (ipv6Dns) { + free(ipv6Dns); + } + if (ipv4Dns) { + free(ipv4Dns); + } + return result; +} + void lws_client_conn_wait_timeout(lws_sorted_usec_list_t *sul) { @@ -183,6 +285,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads, lws_conmon_append_copy_new_dns_results(wsi, result); #endif + result = sort_dns((struct addrinfo *)result); lws_sort_dns(wsi, result); #if defined(LWS_WITH_SYS_ASYNC_DNS) lws_async_dns_freeaddrinfo(&result); @@ -244,7 +347,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads, * If the connection failed, the OS-level errno may be * something like EINPROGRESS rather than the actual problem * that prevented a connection. This value will represent the - * ¡°real¡± problem that we should report to the caller. + * "real" problem that we should report to the caller. */ int real_errno = 0;