szaszm commented on a change in pull request #713: MINIFICPP-1119 MINIFICPP-1154 unify win/posix sockets + fix bugs URL: https://github.com/apache/nifi-minifi-cpp/pull/713#discussion_r393702174
########## File path: libminifi/src/io/ClientSocket.cpp ########## @@ -0,0 +1,625 @@ +/** + * + * 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 "io/ClientSocket.h" +#ifndef WIN32 +#include <netinet/tcp.h> +#include <sys/types.h> +#include <netinet/in.h> +#include <ifaddrs.h> +#include <unistd.h> +#else +#include <WS2tcpip.h> +#pragma comment(lib, "Ws2_32.lib") +#endif /* !WIN32 */ + +#include <memory> +#include <utility> +#include <vector> +#include <cerrno> +#include <string> +#include <system_error> +#include <cinttypes> +#include <Exception.h> +#include <utils/Deleters.h> +#include "io/validation.h" +#include "core/logging/LoggerConfiguration.h" +#include "utils/GeneralUtils.h" + +namespace util = org::apache::nifi::minifi::utils; + +namespace { + +std::string get_last_err_str() { +#ifdef WIN32 + const auto error_code = WSAGetLastError(); +#else + const auto error_code = errno; +#endif /* WIN32 */ + return std::system_category().message(error_code); +} + +std::string get_last_getaddrinfo_err_str(int getaddrinfo_result) { +#ifdef WIN32 + (void)getaddrinfo_result; // against unused warnings on windows + return get_last_err_str(); +#else + return gai_strerror(getaddrinfo_result); +#endif /* WIN32 */ +} + +bool valid_sock_fd(org::apache::nifi::minifi::io::SocketDescriptor fd) { +#ifdef WIN32 + return fd != INVALID_SOCKET && fd >= 0; +#else + return fd >= 0; +#endif /* WIN32 */ +} + +std::string sockaddr_ntop(const sockaddr* const sa) { + std::string result; + if (sa->sa_family == AF_INET) { + sockaddr_in sa_in{}; + std::memcpy(reinterpret_cast<void*>(&sa_in), sa, sizeof(sockaddr_in)); + result.resize(INET_ADDRSTRLEN); + if (inet_ntop(AF_INET, &sa_in.sin_addr, &result[0], INET_ADDRSTRLEN) == nullptr) { + throw minifi::Exception{ minifi::ExceptionType::GENERAL_EXCEPTION, get_last_err_str() }; + } + } else if (sa->sa_family == AF_INET6) { + sockaddr_in6 sa_in6{}; + std::memcpy(reinterpret_cast<void*>(&sa_in6), sa, sizeof(sockaddr_in6)); + result.resize(INET6_ADDRSTRLEN); + if (inet_ntop(AF_INET6, &sa_in6.sin6_addr, &result[0], INET6_ADDRSTRLEN) == nullptr) { + throw minifi::Exception{ minifi::ExceptionType::GENERAL_EXCEPTION, get_last_err_str() }; + } + } else { + throw minifi::Exception{ minifi::ExceptionType::GENERAL_EXCEPTION, "sockaddr_ntop: unknown address family" }; + } + result.resize(strlen(result.c_str())); // discard remaining null bytes at the end + return result; +} + +template<typename T, typename Pred, typename Adv> Review comment: No, almost nothing is "absolutely necessary". I wanted to name the logic there, so that instead of a for loop, we have something with a descriptive name. Looking up "abomination" gave me this: "something regarded with disgust or hatred". Do you not like the fact that I extracted the logic or the minimal concept emulation on the next line? Could you give more details? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services