From 8dd169473d596a6cc1d8b452d40107ee24ddd419 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 12:21:49 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E4=B8=80=E8=87=B4=E6=80=A7=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cy --- BUILD.gn | 1 + bundle.json | 1 + for_ios.patch | 1 + for_ios.sh | 3 ++- include/lws_config.h | 3 ++- include/lws_config_private.h | 3 ++- lib/core-net/client/connect3.c | 5 +++-- 7 files changed, 12 insertions(+), 5 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index d3309d55..b74447f6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -23,6 +23,7 @@ if (target_os == "ios") { ohos_static_library("websockets") { sources = [ "//third_party/libwebsockets/lib/core-net/adopt.c", + "//third_party/libwebsockets/lib/core-net/client/client.c", "//third_party/libwebsockets/lib/core-net/client/conmon.c", "//third_party/libwebsockets/lib/core-net/client/connect.c", diff --git a/bundle.json b/bundle.json index ec52483f..0eee099f 100644 --- a/bundle.json +++ b/bundle.json @@ -26,6 +26,7 @@ ] }, "build": { + "sub_component": [], "inner_kits": [], "test": [] diff --git a/for_ios.patch b/for_ios.patch index c3c398ae..d752e454 100755 --- a/for_ios.patch +++ b/for_ios.patch @@ -11,3 +11,4 @@ index 298bf01e..e8dc468e 100644 #endif NULL }; + diff --git a/for_ios.sh b/for_ios.sh index fbc3cdbe..fb52785d 100755 --- a/for_ios.sh +++ b/for_ios.sh @@ -17,5 +17,6 @@ set -e cd $1 rm -f lib/core-net/vhost_ios.c -cp lib/core-net/vhost.c 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 cf9bdd02..f32bfb99 100644 --- a/include/lws_config.h +++ b/include/lws_config.h @@ -10,7 +10,8 @@ * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in + * The above copyr + * ight notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR diff --git a/include/lws_config_private.h b/include/lws_config_private.h index 24897944..0175e972 100644 --- a/include/lws_config_private.h +++ b/include/lws_config_private.h @@ -8,7 +8,8 @@ * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * furnishe + * d to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. diff --git a/lib/core-net/client/connect3.c b/lib/core-net/client/connect3.c index 9d669e3b..31517166 100644 --- a/lib/core-net/client/connect3.c +++ b/lib/core-net/client/connect3.c @@ -6,7 +6,8 @@ * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * @@ -244,7 +245,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; -- Gitee From 42a8523f7297102aba058f1933da474223317ee6 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 06:53:22 +0000 Subject: [PATCH 02/12] update lib/core-net/client/connect3.c. Signed-off-by: cy --- lib/core-net/client/connect3.c | 108 ++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 3 deletions(-) diff --git a/lib/core-net/client/connect3.c b/lib/core-net/client/connect3.c index 31517166..cf6c988e 100644 --- a/lib/core-net/client/connect3.c +++ b/lib/core-net/client/connect3.c @@ -6,8 +6,7 @@ * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the - * rights to use, copy, - * modify, merge, publish, distribute, sublicense, and/or + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * @@ -25,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) { @@ -184,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); @@ -245,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; -- Gitee From 7cc2162636b2876d63a2885ba3b39d3b1c3a0da2 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 06:54:24 +0000 Subject: [PATCH 03/12] update BUILD.gn. Signed-off-by: cy --- BUILD.gn | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index b74447f6..aa37d671 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -22,8 +22,7 @@ if (target_os == "ios") { ohos_static_library("websockets") { sources = [ - "//third_party/libwebsockets/lib/core-net/adopt.c", - + "//third_party/libwebsockets/lib/core-net/adopt.c", "//third_party/libwebsockets/lib/core-net/client/client.c", "//third_party/libwebsockets/lib/core-net/client/conmon.c", "//third_party/libwebsockets/lib/core-net/client/connect.c", @@ -248,8 +247,8 @@ ohos_static_library("websockets") { deps = [ "//third_party/openssl:libcrypto_shared", "//third_party/openssl:libssl_shared", - "//third_party/zlib:libz", ] + external_deps = [ "zlib:libz" ] } config("websocket_config") { -- Gitee From ae39e5fd5c83e3d7675ee2e5cc68c481d3c6c959 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 06:55:44 +0000 Subject: [PATCH 04/12] update bundle.json. Signed-off-by: cy --- bundle.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bundle.json b/bundle.json index 0eee099f..1af270b5 100644 --- a/bundle.json +++ b/bundle.json @@ -19,14 +19,12 @@ "rom": "319KB", "ram": "13865KB", "deps": { - "components": [], + "components": [ "zlib" ], "third_party": [ - "openssl", - "zlib" + "openssl" ] }, - "build": { - + "build": { "sub_component": [], "inner_kits": [], "test": [] -- Gitee From 4a53a7d48558b6b847d7413bbef579aba8e956d4 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 06:57:06 +0000 Subject: [PATCH 05/12] update for_ios.patch. Signed-off-by: cy --- for_ios.patch | 1 - 1 file changed, 1 deletion(-) diff --git a/for_ios.patch b/for_ios.patch index d752e454..c3c398ae 100755 --- a/for_ios.patch +++ b/for_ios.patch @@ -11,4 +11,3 @@ index 298bf01e..e8dc468e 100644 #endif NULL }; - -- Gitee From be0e2751f580f831e1f5712002d8d9734664a046 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 06:57:36 +0000 Subject: [PATCH 06/12] update for_ios.sh. Signed-off-by: cy --- for_ios.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/for_ios.sh b/for_ios.sh index fb52785d..fbc3cdbe 100755 --- a/for_ios.sh +++ b/for_ios.sh @@ -17,6 +17,5 @@ set -e cd $1 rm -f lib/core-net/vhost_ios.c -cp lib/core-net/vhost.c lib/core-net/vhost_ios -.c +cp lib/core-net/vhost.c lib/core-net/vhost_ios.c git apply for_ios.patch -- Gitee From 47870335929fa20939cc26b3251718cb26221f5b Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 06:58:16 +0000 Subject: [PATCH 07/12] update include/lws_config_private.h. Signed-off-by: cy --- include/lws_config_private.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/lws_config_private.h b/include/lws_config_private.h index 0175e972..24897944 100644 --- a/include/lws_config_private.h +++ b/include/lws_config_private.h @@ -8,8 +8,7 @@ * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is - * furnishe - * d to do so, subject to the following conditions: + * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. -- Gitee From 7b1a944d496fc33fab23fd96b8697003b2ce0340 Mon Sep 17 00:00:00 2001 From: cy Date: Sat, 28 Dec 2024 06:58:40 +0000 Subject: [PATCH 08/12] update include/lws_config.h. Signed-off-by: cy --- include/lws_config.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/lws_config.h b/include/lws_config.h index f32bfb99..cf9bdd02 100644 --- a/include/lws_config.h +++ b/include/lws_config.h @@ -10,8 +10,7 @@ * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyr - * ight notice and this permission notice shall be included in + * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -- Gitee From 23b2e493261a09a4dc2f78bae23ad008d8e12615 Mon Sep 17 00:00:00 2001 From: cy Date: Mon, 30 Dec 2024 06:32:50 +0000 Subject: [PATCH 09/12] update BUILD.gn. Signed-off-by: cy --- BUILD.gn | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index aa37d671..62c0477f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -15,14 +15,9 @@ 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", + "//third_party/libwebsockets/lib/core-net/adopt.c", "//third_party/libwebsockets/lib/core-net/client/client.c", "//third_party/libwebsockets/lib/core-net/client/conmon.c", "//third_party/libwebsockets/lib/core-net/client/connect.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", @@ -305,7 +286,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 +359,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", -- Gitee From 7cb537f2e881e8fd2d75413557abed1ee8730fa8 Mon Sep 17 00:00:00 2001 From: cy Date: Mon, 30 Dec 2024 06:36:33 +0000 Subject: [PATCH 10/12] update lib/core-net/client/connect3.c. Signed-off-by: cy --- lib/core-net/client/connect3.c | 105 +-------------------------------- 1 file changed, 1 insertion(+), 104 deletions(-) diff --git a/lib/core-net/client/connect3.c b/lib/core-net/client/connect3.c index cf6c988e..a9d2e9e0 100644 --- a/lib/core-net/client/connect3.c +++ b/lib/core-net/client/connect3.c @@ -24,108 +24,6 @@ #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) { @@ -285,7 +183,6 @@ 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); @@ -347,7 +244,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; -- Gitee From bc21a147f28f5703b4d0aeae6b4545181d032bf0 Mon Sep 17 00:00:00 2001 From: cy Date: Mon, 30 Dec 2024 06:37:17 +0000 Subject: [PATCH 11/12] update lib/core-net/client/connect3.c. Signed-off-by: cy --- lib/core-net/client/connect3.c | 105 ++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/lib/core-net/client/connect3.c b/lib/core-net/client/connect3.c index a9d2e9e0..2a9fa6b8 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; -- Gitee From abfdd44fe50ba4a178847516d3678b4d56ee2e40 Mon Sep 17 00:00:00 2001 From: cy Date: Mon, 30 Dec 2024 06:38:23 +0000 Subject: [PATCH 12/12] update bundle.json. Signed-off-by: cy --- bundle.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundle.json b/bundle.json index 1af270b5..4df4850c 100644 --- a/bundle.json +++ b/bundle.json @@ -24,7 +24,7 @@ "openssl" ] }, - "build": { + "build": { "sub_component": [], "inner_kits": [], "test": [] -- Gitee