szaszm commented on a change in pull request #1208: URL: https://github.com/apache/nifi-minifi-cpp/pull/1208#discussion_r777609050
########## File path: libminifi/src/utils/net/Socket.cpp ########## @@ -0,0 +1,72 @@ +/** + * 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/net/Socket.h" +#include "Exception.h" +#include <cstring> +#include <system_error> +#ifdef WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif /* WIN32_LEAN_AND_MEAN */ +#include <ws2tcpip.h> +#else +#include <arpa/inet.h> +#endif /* WIN32 */ + +namespace org::apache::nifi::minifi::utils::net { +std::error_code get_last_socket_error() { +#ifdef WIN32 + const auto error_code = WSAGetLastError(); +#else + const auto error_code = errno; +#endif /* WIN32 */ + return {error_code, std::system_category()}; +} + +nonstd::expected<OpenSocketResult, std::error_code> open_socket(const addrinfo* const getaddrinfo_result) { + for (const addrinfo* it = getaddrinfo_result; it; it = it->ai_next) { + const auto fd = socket(it->ai_family, it->ai_socktype, it->ai_protocol); + if (fd != utils::net::InvalidSocket) return OpenSocketResult{UniqueSocketHandle{fd}, gsl::make_not_null(it)}; + } + return nonstd::make_unexpected(get_last_socket_error()); +} + +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)); Review comment: Good point, removing. -- 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]
