szaszm commented on a change in pull request #1116: URL: https://github.com/apache/nifi-minifi-cpp/pull/1116#discussion_r677164998
########## File path: extensions/standard-processors/tests/unit/AppendHostInfoTests.cpp ########## @@ -0,0 +1,73 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +#include "TestBase.h" +#include "AppendHostInfo.h" +#include "LogAttribute.h" + +using AppendHostInfo = org::apache::nifi::minifi::processors::AppendHostInfo; +using LogAttribute = org::apache::nifi::minifi::processors::LogAttribute; + +TEST_CASE("AppendHostInfoTest", "[appendhostinfotest]") { + TestController testController; + std::shared_ptr<TestPlan> plan = testController.createPlan(); + LogTestController::getInstance().setTrace<processors::AppendHostInfo>(); + LogTestController::getInstance().setTrace<processors::LogAttribute>(); + std::shared_ptr<core::Processor> generate_flow_file = plan->addProcessor("GenerateFlowFile", "generate_flow_file"); + std::shared_ptr<core::Processor> append_host_info = plan->addProcessor("AppendHostInfo", "append_host_info", core::Relationship("success", "description"), true); + std::shared_ptr<core::Processor> log_attribute = plan->addProcessor("LogAttribute", "log_attributes", core::Relationship("success", "description"), true); + + testController.runSession(plan); + + REQUIRE(LogTestController::getInstance().contains("source.hostname")); + REQUIRE(LogTestController::getInstance().contains("source.ipv4")); +} + +TEST_CASE("AppendHostInfoTestWithUnmatchableRegex", "[appendhostinfotestunmatchableregex]") { + TestController testController; + std::shared_ptr<TestPlan> plan = testController.createPlan(); + LogTestController::getInstance().setTrace<processors::AppendHostInfo>(); + LogTestController::getInstance().setTrace<processors::LogAttribute>(); + std::shared_ptr<core::Processor> generate_flow_file = plan->addProcessor("GenerateFlowFile", "generate_flow_file"); + std::shared_ptr<core::Processor> append_host_info = plan->addProcessor("AppendHostInfo", "append_host_info", core::Relationship("success", "description"), true); + std::shared_ptr<core::Processor> log_attribute = plan->addProcessor("LogAttribute", "log_attributes", core::Relationship("success", "description"), true); + + plan->setProperty(append_host_info, AppendHostInfo::InterfaceNameFilter.getName(), "\b"); + + testController.runSession(plan); + + REQUIRE(LogTestController::getInstance().contains("source.hostname", std::chrono::seconds(0), std::chrono::milliseconds(0))); + REQUIRE_FALSE(LogTestController::getInstance().contains("source.ipv4", std::chrono::seconds(0), std::chrono::milliseconds(0))); Review comment: Consider using standard literal suffixes. ```suggestion using namespace std::literals; // NOLINT: using-directives are not allowed by the style guide, but are necessary to use standard literals REQUIRE(LogTestController::getInstance().contains("source.hostname", 0s, 0ms)); REQUIRE_FALSE(LogTestController::getInstance().contains("source.ipv4", 0s, 0ms)); ``` After writing the above, I checked the google style guide: It bans user-defined literals, including the standard ones used here. https://google.github.io/styleguide/cppguide.html#Operator_Overloading I don't agree with this rule. User-defined literals have the potential to make our code more expressive. Not sure how to proceed, so I leave it up to you whether you follow this rule or not. ########## File path: extensions/standard-processors/processors/AppendHostInfo.h ########## @@ -37,38 +39,43 @@ namespace nifi { namespace minifi { namespace processors { -// AppendHostInfo Class class AppendHostInfo : public core::Processor { public: - // Constructor - /*! - * Create a new processor - */ - AppendHostInfo(const std::string& name, const utils::Identifier& uuid = {}) // NOLINT + static constexpr const char* REFRESH_POLICY_ON_TRIGGER = "On every trigger"; + static constexpr const char* REFRESH_POLICY_ON_SCHEDULE = "On schedule"; + + explicit AppendHostInfo(const std::string& name, const utils::Identifier& uuid = {}) : core::Processor(name, uuid), - logger_(logging::LoggerFactory<AppendHostInfo>::getLogger()) { + logger_(logging::LoggerFactory<AppendHostInfo>::getLogger()), + refresh_on_trigger_(false) { } - // Destructor virtual ~AppendHostInfo() = default; - // Processor Name static constexpr char const* ProcessorName = "AppendHostInfo"; - // Supported Properties - static core::Property InterfaceName; + + static core::Property InterfaceNameFilter; static core::Property HostAttribute; static core::Property IPAttribute; + static core::Property RefreshPolicy; - // Supported Relationships static core::Relationship Success; public: - // OnTrigger method, implemented by NiFi AppendHostInfo - virtual void onTrigger(core::ProcessContext *context, core::ProcessSession *session); - // Initialize, over write by NiFi AppendHostInfo - virtual void initialize(void); + void onSchedule(const std::shared_ptr<core::ProcessContext>& context, const std::shared_ptr<core::ProcessSessionFactory>& sessionFactory) override; + void onTrigger(core::ProcessContext* context, core::ProcessSession* session) override; + void initialize() override; + + protected: + virtual void refreshHostInfo(); private: - // Logger std::shared_ptr<logging::Logger> logger_; + std::string hostname_attribute_name_; + std::string ipaddress_attribute_name_; + std::optional<std::regex> interface_name_filter_; + + std::string hostname_; + std::optional<std::string> ipaddresses_; + bool refresh_on_trigger_; Review comment: All of these members may be accessed concurrently if the max number of concurrent tasks > 1, unless I'm missing some kind of synchronization. An easy solution would be to disable concurrency support by just holding a mutex at all times in onTrigger and onSchedule. Another alternative is to protect those accesses or just document that concurrency is not supported. I'm not aware of usages of processor concurrency in the field, although it's still a regression. (logger_ is thread-safe.) ########## File path: extensions/standard-processors/tests/unit/AppendHostInfoTests.cpp ########## @@ -0,0 +1,73 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +#include "TestBase.h" +#include "AppendHostInfo.h" +#include "LogAttribute.h" + +using AppendHostInfo = org::apache::nifi::minifi::processors::AppendHostInfo; +using LogAttribute = org::apache::nifi::minifi::processors::LogAttribute; + +TEST_CASE("AppendHostInfoTest", "[appendhostinfotest]") { + TestController testController; + std::shared_ptr<TestPlan> plan = testController.createPlan(); + LogTestController::getInstance().setTrace<processors::AppendHostInfo>(); + LogTestController::getInstance().setTrace<processors::LogAttribute>(); + std::shared_ptr<core::Processor> generate_flow_file = plan->addProcessor("GenerateFlowFile", "generate_flow_file"); + std::shared_ptr<core::Processor> append_host_info = plan->addProcessor("AppendHostInfo", "append_host_info", core::Relationship("success", "description"), true); + std::shared_ptr<core::Processor> log_attribute = plan->addProcessor("LogAttribute", "log_attributes", core::Relationship("success", "description"), true); + + testController.runSession(plan); + + REQUIRE(LogTestController::getInstance().contains("source.hostname")); + REQUIRE(LogTestController::getInstance().contains("source.ipv4")); +} + +TEST_CASE("AppendHostInfoTestWithUnmatchableRegex", "[appendhostinfotestunmatchableregex]") { + TestController testController; + std::shared_ptr<TestPlan> plan = testController.createPlan(); + LogTestController::getInstance().setTrace<processors::AppendHostInfo>(); + LogTestController::getInstance().setTrace<processors::LogAttribute>(); + std::shared_ptr<core::Processor> generate_flow_file = plan->addProcessor("GenerateFlowFile", "generate_flow_file"); + std::shared_ptr<core::Processor> append_host_info = plan->addProcessor("AppendHostInfo", "append_host_info", core::Relationship("success", "description"), true); + std::shared_ptr<core::Processor> log_attribute = plan->addProcessor("LogAttribute", "log_attributes", core::Relationship("success", "description"), true); + + plan->setProperty(append_host_info, AppendHostInfo::InterfaceNameFilter.getName(), "\b"); + + testController.runSession(plan); + + REQUIRE(LogTestController::getInstance().contains("source.hostname", std::chrono::seconds(0), std::chrono::milliseconds(0))); + REQUIRE_FALSE(LogTestController::getInstance().contains("source.ipv4", std::chrono::seconds(0), std::chrono::milliseconds(0))); +} + +TEST_CASE("AppendHostInfoTestCanFilterOutLoopbackInterfacesWithRegex", "[appendhostinfotestfilterloopback]") { + TestController testController; + std::shared_ptr<TestPlan> plan = testController.createPlan(); + LogTestController::getInstance().setTrace<processors::AppendHostInfo>(); + LogTestController::getInstance().setTrace<processors::LogAttribute>(); + std::shared_ptr<core::Processor> generate_flow_file = plan->addProcessor("GenerateFlowFile", "generate_flow_file"); + std::shared_ptr<core::Processor> append_host_info = plan->addProcessor("AppendHostInfo", "append_host_info", core::Relationship("success", "description"), true); + std::shared_ptr<core::Processor> log_attribute = plan->addProcessor("LogAttribute", "log_attributes", core::Relationship("success", "description"), true); + + plan->setProperty(append_host_info, AppendHostInfo::InterfaceNameFilter.getName(), "(?!Loopback|lo).*?"); // set up the regex to accept everything except interfaces starting with Loopback or lo + + testController.runSession(plan); + + REQUIRE(LogTestController::getInstance().contains("source.hostname", std::chrono::seconds(0), std::chrono::milliseconds(0))); + REQUIRE_FALSE(LogTestController::getInstance().contains("127.0.0.1", std::chrono::seconds(0), std::chrono::milliseconds(0))); Review comment: ```suggestion using namespace std::literals; // NOLINT: using-directives are not allowed by the style guide, but are necessary to use standard literals REQUIRE(LogTestController::getInstance().contains("source.hostname", 0s, 0ms)); REQUIRE_FALSE(LogTestController::getInstance().contains("127.0.0.1", 0s, 0ms)); ``` ########## File path: libminifi/src/utils/NetworkInterfaceInfo.cpp ########## @@ -0,0 +1,169 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +#include "utils/NetworkInterfaceInfo.h" +#include "utils/OsUtils.h" +#include "core/logging/LoggerConfiguration.h" +#ifdef WIN32 +#include <Windows.h> +#include <winsock2.h> +#include <iphlpapi.h> +#include <WS2tcpip.h> +#pragma comment(lib, "IPHLPAPI.lib") +#else +#include <unistd.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <net/if.h> +#include <arpa/inet.h> +#include <ifaddrs.h> +#endif + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +std::shared_ptr<core::logging::Logger> NetworkInterfaceInfo::logger_ = core::logging::LoggerFactory<NetworkInterfaceInfo>::getLogger(); + +#ifdef WIN32 +namespace { +std::string utf8_encode(const std::wstring& wstr) { + if (wstr.empty()) + return std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); + std::string result_string(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, result_string.data(), size_needed, nullptr, nullptr); + return result_string; +} +} + +NetworkInterfaceInfo::NetworkInterfaceInfo(const IP_ADAPTER_ADDRESSES* adapter) { + name_ = utf8_encode(adapter->FriendlyName); + for (auto unicast_address = adapter->FirstUnicastAddress; unicast_address != nullptr; unicast_address = unicast_address->Next) { + if (unicast_address->Address.lpSockaddr->sa_family == AF_INET) { + ip_v4_addresses_.push_back(OsUtils::sockaddr_ntop(unicast_address->Address.lpSockaddr)); + } else if (unicast_address->Address.lpSockaddr->sa_family == AF_INET6) { + ip_v6_addresses_.push_back(OsUtils::sockaddr_ntop(unicast_address->Address.lpSockaddr)); + } + } + running_ = adapter->OperStatus == IfOperStatusUp; + loopback_ = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK; +} +#else +NetworkInterfaceInfo::NetworkInterfaceInfo(const struct ifaddrs* ifa) { + name_ = ifa->ifa_name; + if (ifa->ifa_addr->sa_family == AF_INET) { + ip_v4_addresses_.push_back(OsUtils::sockaddr_ntop(ifa->ifa_addr)); + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + ip_v6_addresses_.push_back(OsUtils::sockaddr_ntop(ifa->ifa_addr)); + } + running_ = (ifa->ifa_flags & IFF_RUNNING); + loopback_ = (ifa->ifa_flags & IFF_LOOPBACK); +} +#endif + +namespace { +struct HasName { + explicit HasName(const std::string& name) : name_(name) {} + bool operator()(const NetworkInterfaceInfo& network_interface_info) { + return network_interface_info.getName() == name_; + } + const std::string& name_; +}; +} + +std::vector<NetworkInterfaceInfo> NetworkInterfaceInfo::getNetworkInterfaceInfos(std::function<bool(const NetworkInterfaceInfo&)> filter, + const std::optional<uint32_t> max_interfaces) { + std::vector<NetworkInterfaceInfo> network_adapters; +#ifdef WIN32 + ULONG buffer_length = sizeof(IP_ADAPTER_ADDRESSES); + auto get_adapters_err = GetAdaptersAddresses(0, 0, nullptr, nullptr, &buffer_length); + if (ERROR_BUFFER_OVERFLOW != get_adapters_err) { + logger_->log_error("GetAdaptersAddresses failed: %lu", get_adapters_err); + return network_adapters; + } + std::vector<char> bytes(buffer_length, 0); + IP_ADAPTER_ADDRESSES* adapter = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(bytes.data()); + get_adapters_err = GetAdaptersAddresses(0, 0, nullptr, adapter, &buffer_length); + if (NO_ERROR != get_adapters_err) { + logger_->log_error("GetAdaptersAddresses failed: %lu", get_adapters_err); + return network_adapters; + } + while (adapter != nullptr) { + NetworkInterfaceInfo interface_info(adapter); + if (filter(interface_info)) { + auto it = std::find_if(network_adapters.begin(), network_adapters.end(), HasName(interface_info.getName())); + if (it == network_adapters.end()) { + network_adapters.emplace_back(std::move(interface_info)); + } else { + interface_info.moveAddressesInto(*it); + } + } + if (max_interfaces.has_value() && network_adapters.size() >= max_interfaces.value()) + return network_adapters; + adapter = adapter->Next; + } +#else + struct ifaddrs* interface_addresses = nullptr; + auto cleanup = gsl::finally([interface_addresses] { freeifaddrs(interface_addresses); }); + if (getifaddrs(&interface_addresses) == -1) { + logger_->log_error("getifaddrs failed: %s", std::strerror(errno)); + return network_adapters; + } + + for (struct ifaddrs* ifa = interface_addresses; ifa != nullptr; ifa = ifa->ifa_next) { + if (!ifa->ifa_addr) + continue; + NetworkInterfaceInfo interface_info(ifa); + if (filter(interface_info)) { + auto it = std::find_if(network_adapters.begin(), network_adapters.end(), HasName(interface_info.getName())); + if (it == network_adapters.end()) { + network_adapters.emplace_back(std::move(interface_info)); + } else { + interface_info.moveAddressesInto(*it); + } + } + if (max_interfaces.has_value() && network_adapters.size() >= max_interfaces.value()) + return network_adapters; + } +#endif + return network_adapters; +} + +void move_append(std::vector<std::string>& source, std::vector<std::string>& destination) { Review comment: I would change this signature to take `source` by rvalue reference. This would make it clear that it's getting moved-from in the process. ```suggestion void move_append(std::vector<std::string>&& source, std::vector<std::string>& destination) { ``` ########## File path: libminifi/include/utils/NetworkInterfaceInfo.h ########## @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +#pragma once + +#include <string> +#include <vector> +#include <memory> +#include <unordered_map> +#include <optional> +#include "core/logging/Logger.h" + +#ifdef WIN32 +struct _IP_ADAPTER_ADDRESSES_LH; +typedef _IP_ADAPTER_ADDRESSES_LH IP_ADAPTER_ADDRESSES_LH; +typedef IP_ADAPTER_ADDRESSES_LH IP_ADAPTER_ADDRESSES; +#else +struct ifaddrs; +#endif + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { + +namespace utils { +class NetworkInterfaceInfo { + public: + NetworkInterfaceInfo(NetworkInterfaceInfo&& src) noexcept = default; +#ifdef WIN32 + explicit NetworkInterfaceInfo(const IP_ADAPTER_ADDRESSES* adapter); +#else + explicit NetworkInterfaceInfo(const struct ifaddrs* ifa); +#endif + NetworkInterfaceInfo& operator=(NetworkInterfaceInfo&& other) noexcept = default; + const std::string& getName() const noexcept { return name_; } + bool hasIpV4Address() const noexcept { return ip_v4_addresses_.size() > 0; } + bool hasIpV6Address() const noexcept { return ip_v6_addresses_.size() > 0; } + bool isRunning() const noexcept { return running_; } + bool isLoopback() const noexcept { return loopback_; } + const std::vector<std::string>& getIpV4Addresses() const noexcept { return ip_v4_addresses_; } + const std::vector<std::string>& getIpV6Addresses() const noexcept { return ip_v6_addresses_; } + + void moveAddressesInto(NetworkInterfaceInfo& destination); Review comment: This could be `private` or have internal linkage (static or anon. namespace) in the .cpp file. It's only used in `getNetworkInterfaceInfos` as far as I can see. ########## File path: libminifi/include/utils/NetworkInterfaceInfo.h ########## @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +#pragma once + +#include <string> +#include <vector> +#include <memory> +#include <unordered_map> +#include <optional> +#include "core/logging/Logger.h" + +#ifdef WIN32 +struct _IP_ADAPTER_ADDRESSES_LH; +typedef _IP_ADAPTER_ADDRESSES_LH IP_ADAPTER_ADDRESSES_LH; +typedef IP_ADAPTER_ADDRESSES_LH IP_ADAPTER_ADDRESSES; +#else +struct ifaddrs; +#endif + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { + +namespace utils { +class NetworkInterfaceInfo { + public: + NetworkInterfaceInfo(NetworkInterfaceInfo&& src) noexcept = default; +#ifdef WIN32 + explicit NetworkInterfaceInfo(const IP_ADAPTER_ADDRESSES* adapter); +#else + explicit NetworkInterfaceInfo(const struct ifaddrs* ifa); +#endif + NetworkInterfaceInfo& operator=(NetworkInterfaceInfo&& other) noexcept = default; + const std::string& getName() const noexcept { return name_; } + bool hasIpV4Address() const noexcept { return ip_v4_addresses_.size() > 0; } + bool hasIpV6Address() const noexcept { return ip_v6_addresses_.size() > 0; } Review comment: ```suggestion bool hasIpV4Address() const noexcept { return !ip_v4_addresses_.empty(); } bool hasIpV6Address() const noexcept { return !ip_v6_addresses_.empty(); } ``` ########## File path: libminifi/src/utils/NetworkInterfaceInfo.cpp ########## @@ -0,0 +1,169 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +#include "utils/NetworkInterfaceInfo.h" +#include "utils/OsUtils.h" +#include "core/logging/LoggerConfiguration.h" +#ifdef WIN32 +#include <Windows.h> +#include <winsock2.h> +#include <iphlpapi.h> +#include <WS2tcpip.h> +#pragma comment(lib, "IPHLPAPI.lib") +#else +#include <unistd.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <net/if.h> +#include <arpa/inet.h> +#include <ifaddrs.h> +#endif + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +std::shared_ptr<core::logging::Logger> NetworkInterfaceInfo::logger_ = core::logging::LoggerFactory<NetworkInterfaceInfo>::getLogger(); + +#ifdef WIN32 +namespace { +std::string utf8_encode(const std::wstring& wstr) { + if (wstr.empty()) + return std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); + std::string result_string(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, result_string.data(), size_needed, nullptr, nullptr); + return result_string; +} +} + +NetworkInterfaceInfo::NetworkInterfaceInfo(const IP_ADAPTER_ADDRESSES* adapter) { + name_ = utf8_encode(adapter->FriendlyName); + for (auto unicast_address = adapter->FirstUnicastAddress; unicast_address != nullptr; unicast_address = unicast_address->Next) { + if (unicast_address->Address.lpSockaddr->sa_family == AF_INET) { + ip_v4_addresses_.push_back(OsUtils::sockaddr_ntop(unicast_address->Address.lpSockaddr)); + } else if (unicast_address->Address.lpSockaddr->sa_family == AF_INET6) { + ip_v6_addresses_.push_back(OsUtils::sockaddr_ntop(unicast_address->Address.lpSockaddr)); + } + } + running_ = adapter->OperStatus == IfOperStatusUp; + loopback_ = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK; +} +#else +NetworkInterfaceInfo::NetworkInterfaceInfo(const struct ifaddrs* ifa) { + name_ = ifa->ifa_name; + if (ifa->ifa_addr->sa_family == AF_INET) { + ip_v4_addresses_.push_back(OsUtils::sockaddr_ntop(ifa->ifa_addr)); + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + ip_v6_addresses_.push_back(OsUtils::sockaddr_ntop(ifa->ifa_addr)); + } + running_ = (ifa->ifa_flags & IFF_RUNNING); + loopback_ = (ifa->ifa_flags & IFF_LOOPBACK); +} Review comment: What about `ifa->ifa_next`? This is also a linked list, similarly to the windows version. ########## File path: libminifi/src/utils/NetworkInterfaceInfo.cpp ########## @@ -0,0 +1,169 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +#include "utils/NetworkInterfaceInfo.h" +#include "utils/OsUtils.h" +#include "core/logging/LoggerConfiguration.h" +#ifdef WIN32 +#include <Windows.h> +#include <winsock2.h> +#include <iphlpapi.h> +#include <WS2tcpip.h> +#pragma comment(lib, "IPHLPAPI.lib") +#else +#include <unistd.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <net/if.h> +#include <arpa/inet.h> +#include <ifaddrs.h> +#endif + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +std::shared_ptr<core::logging::Logger> NetworkInterfaceInfo::logger_ = core::logging::LoggerFactory<NetworkInterfaceInfo>::getLogger(); + +#ifdef WIN32 +namespace { +std::string utf8_encode(const std::wstring& wstr) { + if (wstr.empty()) + return std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); + std::string result_string(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, result_string.data(), size_needed, nullptr, nullptr); + return result_string; +} +} + +NetworkInterfaceInfo::NetworkInterfaceInfo(const IP_ADAPTER_ADDRESSES* adapter) { + name_ = utf8_encode(adapter->FriendlyName); + for (auto unicast_address = adapter->FirstUnicastAddress; unicast_address != nullptr; unicast_address = unicast_address->Next) { + if (unicast_address->Address.lpSockaddr->sa_family == AF_INET) { + ip_v4_addresses_.push_back(OsUtils::sockaddr_ntop(unicast_address->Address.lpSockaddr)); + } else if (unicast_address->Address.lpSockaddr->sa_family == AF_INET6) { + ip_v6_addresses_.push_back(OsUtils::sockaddr_ntop(unicast_address->Address.lpSockaddr)); + } + } + running_ = adapter->OperStatus == IfOperStatusUp; + loopback_ = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK; +} +#else +NetworkInterfaceInfo::NetworkInterfaceInfo(const struct ifaddrs* ifa) { + name_ = ifa->ifa_name; + if (ifa->ifa_addr->sa_family == AF_INET) { + ip_v4_addresses_.push_back(OsUtils::sockaddr_ntop(ifa->ifa_addr)); + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + ip_v6_addresses_.push_back(OsUtils::sockaddr_ntop(ifa->ifa_addr)); + } + running_ = (ifa->ifa_flags & IFF_RUNNING); + loopback_ = (ifa->ifa_flags & IFF_LOOPBACK); +} +#endif + +namespace { +struct HasName { + explicit HasName(const std::string& name) : name_(name) {} + bool operator()(const NetworkInterfaceInfo& network_interface_info) { + return network_interface_info.getName() == name_; + } + const std::string& name_; +}; +} + +std::vector<NetworkInterfaceInfo> NetworkInterfaceInfo::getNetworkInterfaceInfos(std::function<bool(const NetworkInterfaceInfo&)> filter, + const std::optional<uint32_t> max_interfaces) { + std::vector<NetworkInterfaceInfo> network_adapters; +#ifdef WIN32 + ULONG buffer_length = sizeof(IP_ADAPTER_ADDRESSES); + auto get_adapters_err = GetAdaptersAddresses(0, 0, nullptr, nullptr, &buffer_length); + if (ERROR_BUFFER_OVERFLOW != get_adapters_err) { + logger_->log_error("GetAdaptersAddresses failed: %lu", get_adapters_err); + return network_adapters; + } + std::vector<char> bytes(buffer_length, 0); + IP_ADAPTER_ADDRESSES* adapter = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(bytes.data()); + get_adapters_err = GetAdaptersAddresses(0, 0, nullptr, adapter, &buffer_length); + if (NO_ERROR != get_adapters_err) { + logger_->log_error("GetAdaptersAddresses failed: %lu", get_adapters_err); + return network_adapters; + } + while (adapter != nullptr) { + NetworkInterfaceInfo interface_info(adapter); + if (filter(interface_info)) { + auto it = std::find_if(network_adapters.begin(), network_adapters.end(), HasName(interface_info.getName())); + if (it == network_adapters.end()) { + network_adapters.emplace_back(std::move(interface_info)); + } else { + interface_info.moveAddressesInto(*it); + } + } + if (max_interfaces.has_value() && network_adapters.size() >= max_interfaces.value()) + return network_adapters; + adapter = adapter->Next; + } +#else + struct ifaddrs* interface_addresses = nullptr; + auto cleanup = gsl::finally([interface_addresses] { freeifaddrs(interface_addresses); }); + if (getifaddrs(&interface_addresses) == -1) { + logger_->log_error("getifaddrs failed: %s", std::strerror(errno)); + return network_adapters; + } + + for (struct ifaddrs* ifa = interface_addresses; ifa != nullptr; ifa = ifa->ifa_next) { + if (!ifa->ifa_addr) + continue; + NetworkInterfaceInfo interface_info(ifa); + if (filter(interface_info)) { + auto it = std::find_if(network_adapters.begin(), network_adapters.end(), HasName(interface_info.getName())); + if (it == network_adapters.end()) { + network_adapters.emplace_back(std::move(interface_info)); + } else { + interface_info.moveAddressesInto(*it); + } + } + if (max_interfaces.has_value() && network_adapters.size() >= max_interfaces.value()) + return network_adapters; + } +#endif + return network_adapters; +} + +void move_append(std::vector<std::string>& source, std::vector<std::string>& destination) { + if (source.size() == 0) + return; + if (destination.empty()) { + destination = std::move(source); + } else { + destination.reserve(destination.size() + source.size()); + std::move(std::begin(source), std::end(source), std::back_inserter(destination)); + source.clear(); + } Review comment: The last block would cover the special cases, too. Let's simplify this code! ```suggestion destination.reserve(destination.size() + source.size()); std::move(std::begin(source), std::end(source), std::back_inserter(destination)); source.clear(); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
