From e7d9962486a7b6e7b9495a469d2747ed477812a6 Mon Sep 17 00:00:00 2001 From: qingyan01 <68208925+qingyan01@users.noreply.github.com> Date: Thu, 18 Apr 2024 16:42:07 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix=20=20ifa->ifa=5Faddr=20=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/network/NetworkUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index 43d67d892be..e615719b8d4 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -66,7 +66,7 @@ StatusOr>> NetworkUtils::listDev } for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { // Skip non-IPv4 devices - if (ifa->ifa_addr->sa_family != AF_INET) { + if (nullptr == ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) { continue; } auto* addr = reinterpret_cast(ifa->ifa_addr); From a641d3f829257269d8d8bd79eda37d0a4963466e Mon Sep 17 00:00:00 2001 From: qingyan01 <68208925+qingyan01@users.noreply.github.com> Date: Fri, 19 Apr 2024 16:46:25 +0800 Subject: [PATCH 2/4] move the following auto *addr above the if and check whether the addr is nullptr --- src/common/network/NetworkUtils.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index e615719b8d4..8ec6d6dc4f1 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -65,8 +65,9 @@ StatusOr>> NetworkUtils::listDev return Status::Error("%s", ::strerror(errno)); } for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { + auto* addr = ifa->ifa_addr; // Skip non-IPv4 devices - if (nullptr == ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) { + if (nullptr == addr || addr->sa_family != AF_INET) { continue; } auto* addr = reinterpret_cast(ifa->ifa_addr); From 2ed1cf9068975acd3b37a75adb29fa65948481e5 Mon Sep 17 00:00:00 2001 From: qingyan01 <68208925+qingyan01@users.noreply.github.com> Date: Tue, 7 May 2024 11:43:09 +0800 Subject: [PATCH 3/4] move addr --- src/common/network/NetworkUtils.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index 8ec6d6dc4f1..1e38f7cc036 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -65,12 +65,11 @@ StatusOr>> NetworkUtils::listDev return Status::Error("%s", ::strerror(errno)); } for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { - auto* addr = ifa->ifa_addr; + auto* addr = reinterpret_cast(ifa->ifa_addr); // Skip non-IPv4 devices if (nullptr == addr || addr->sa_family != AF_INET) { continue; } - auto* addr = reinterpret_cast(ifa->ifa_addr); // inet_ntoa is thread safe but not re-entrant, // we could use inet_ntop instead when we need support for IPv6 dev2ipv4s.emplace_back(ifa->ifa_name, ::inet_ntoa(addr->sin_addr)); From 8bb11b51421691d6b0184b0d6587a5f060a3ff95 Mon Sep 17 00:00:00 2001 From: qingyan01 <68208925+qingyan01@users.noreply.github.com> Date: Fri, 31 May 2024 10:51:45 +0800 Subject: [PATCH 4/4] Update NetworkUtils.cpp fix sa_family not exist --- src/common/network/NetworkUtils.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common/network/NetworkUtils.cpp b/src/common/network/NetworkUtils.cpp index 1e38f7cc036..d6351e3754f 100644 --- a/src/common/network/NetworkUtils.cpp +++ b/src/common/network/NetworkUtils.cpp @@ -65,11 +65,12 @@ StatusOr>> NetworkUtils::listDev return Status::Error("%s", ::strerror(errno)); } for (auto* ifa = iflist; ifa != nullptr; ifa = ifa->ifa_next) { - auto* addr = reinterpret_cast(ifa->ifa_addr); + auto* addr_ptr = ifa->ifa_addr; // Skip non-IPv4 devices - if (nullptr == addr || addr->sa_family != AF_INET) { + if (nullptr == addr_ptr || addr_ptr->sa_family != AF_INET) { continue; } + auto* addr = reinterpret_cast(addr_ptr); // inet_ntoa is thread safe but not re-entrant, // we could use inet_ntop instead when we need support for IPv6 dev2ipv4s.emplace_back(ifa->ifa_name, ::inet_ntoa(addr->sin_addr));