martinzink commented on a change in pull request #1116: URL: https://github.com/apache/nifi-minifi-cpp/pull/1116#discussion_r664301931
########## File path: libminifi/src/utils/NetworkInterfaceInfo.cpp ########## @@ -0,0 +1,155 @@ +/** + * 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" + +#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 { + +#ifdef WIN32 +std::string utf8_encode(const std::wstring& wstr) { + if (wstr.empty()) + return std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), nullptr, 0, nullptr, nullptr); + std::string result_string(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &result_string[0], size_needed, nullptr, nullptr); + return result_string; +} Review comment: moved it to anonymous namespace in https://github.com/apache/nifi-minifi-cpp/pull/1116/commits/d84894db14625f14cdef9fdf92754bed8ea4ac49 ########## File path: libminifi/src/utils/NetworkInterfaceInfo.cpp ########## @@ -0,0 +1,155 @@ +/** + * 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" + +#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 { + +#ifdef WIN32 +std::string utf8_encode(const std::wstring& wstr) { + if (wstr.empty()) + return std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), nullptr, 0, nullptr, nullptr); + std::string result_string(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &result_string[0], 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) { + char address_buffer[INET_ADDRSTRLEN]; + void* sin_address = &(reinterpret_cast<SOCKADDR_IN*>(unicast_address->Address.lpSockaddr)->sin_addr); + InetNtopA(AF_INET, sin_address, address_buffer, INET_ADDRSTRLEN); + ip_v4_addresses_.push_back(address_buffer); + } else if (unicast_address->Address.lpSockaddr->sa_family == AF_INET6) { + char address_buffer[INET6_ADDRSTRLEN]; + void* sin_address = &(reinterpret_cast<SOCKADDR_IN*>(unicast_address->Address.lpSockaddr)->sin_addr); + InetNtopA(AF_INET6, sin_address, address_buffer, INET6_ADDRSTRLEN); + ip_v6_addresses_.push_back(address_buffer); + } Review comment: good idea, I moved the ClientSocket's implementation to OSUtils and used that here and in clientsocket as well. https://github.com/apache/nifi-minifi-cpp/pull/1116/commits/d84894db14625f14cdef9fdf92754bed8ea4ac49 ########## File path: libminifi/src/utils/NetworkInterfaceInfo.cpp ########## @@ -0,0 +1,155 @@ +/** + * 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" + +#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 { + +#ifdef WIN32 +std::string utf8_encode(const std::wstring& wstr) { + if (wstr.empty()) + return std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), nullptr, 0, nullptr, nullptr); Review comment: changed it in https://github.com/apache/nifi-minifi-cpp/pull/1116/commits/d84894db14625f14cdef9fdf92754bed8ea4ac49 ########## File path: libminifi/src/utils/NetworkInterfaceInfo.cpp ########## @@ -0,0 +1,155 @@ +/** + * 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" + +#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 { + +#ifdef WIN32 +std::string utf8_encode(const std::wstring& wstr) { + if (wstr.empty()) + return std::string(); + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), nullptr, 0, nullptr, nullptr); + std::string result_string(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &result_string[0], 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) { + char address_buffer[INET_ADDRSTRLEN]; + void* sin_address = &(reinterpret_cast<SOCKADDR_IN*>(unicast_address->Address.lpSockaddr)->sin_addr); + InetNtopA(AF_INET, sin_address, address_buffer, INET_ADDRSTRLEN); + ip_v4_addresses_.push_back(address_buffer); + } else if (unicast_address->Address.lpSockaddr->sa_family == AF_INET6) { + char address_buffer[INET6_ADDRSTRLEN]; + void* sin_address = &(reinterpret_cast<SOCKADDR_IN*>(unicast_address->Address.lpSockaddr)->sin_addr); + InetNtopA(AF_INET6, sin_address, address_buffer, INET6_ADDRSTRLEN); + ip_v6_addresses_.push_back(address_buffer); + } + } + running_ = adapter->OperStatus == IfOperStatusUp; + loopback_ = adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK; +} +#else +NetworkInterfaceInfo::NetworkInterfaceInfo(const struct ifaddrs* ifa) { + name_ = ifa->ifa_name; + void* sin_address = &(reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr)->sin_addr); + if (ifa->ifa_addr->sa_family == AF_INET) { + char address_buffer[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, sin_address, address_buffer, INET_ADDRSTRLEN); + ip_v4_addresses_.push_back(address_buffer); + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + char address_buffer[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, sin_address, address_buffer, INET6_ADDRSTRLEN); Review comment: I moved the ClientSocket's implementation to OSUtils and used that here and in clientsocket as well. https://github.com/apache/nifi-minifi-cpp/pull/1116/commits/d84894db14625f14cdef9fdf92754bed8ea4ac49 -- 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]
