Wed, 27 Jan 2021 19:32:55 +0200
various little touchups
/* Copyright 2014 - 2021 Teemu Piippo All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "ipaddress.h" #ifndef _WIN32 # include <netinet/in.h> # include <netdb.h> #else # include <winsock2.h> # include <ws2tcpip.h> #endif BEGIN_ZFC_NAMESPACE #ifdef _WIN32 typedef ADDRINFOA AddrInfo; #else typedef struct addrinfo AddrInfo; #endif net::octet_t net::ip_octet(const net::ip_address& address, unsigned char n) { assert(n < 4); return (address.host >> ((3 - n) * 8)) & 0xFF; } void net::ip_set_octet(net::ip_address* address, unsigned char n, net::octet_t octet) { // TODO: make a big-endian version assert(n < 4); address->host &= ~(0xFF << (3 - n) * 8); address->host |= octet << ((3 - n) * 8); } std::string net::ip_address_to_string(const net::ip_address& address) { const auto octet = [&](unsigned char n) { return ip_octet(address, n); }; std::string val = sprintf ("%u.%u.%u.%u:%u", octet(0), octet(1), octet(2), octet(3), address.port); return val; } int net::ip_compare(const net::ip_address &one, const net::ip_address &other) { if (one.host != other.host) { return one.host - other.host; } return one.port - other.port; } bool net::ip_address::operator<(const ip_address& other) const { return ip_compare(*this, other) < 0; } bool net::ip_address::operator==(const net::ip_address &other) const { return this->host == other.host and this->port == other.port; } // ----------------------------------------------------------------------------- // sockaddr_in net::ip_address_to_sockaddr_in(const net::ip_address& address) { sockaddr_in claddr; memset (&claddr, 0, sizeof claddr); claddr.sin_addr.s_addr = htonl(address.host); claddr.sin_port = htons(address.port); claddr.sin_family = AF_INET; return claddr; } std::optional<unsigned short> net::ip_parse_port(const char* port_string, std::ostream& errorStream) { std::optional<long> opt = to_int(port_string); if (opt.has_value()) { if (*opt >= 0 and *opt < 65536) { return static_cast<unsigned short>(opt.value()); } else { return {}; errorStream << "port is not in range"s; } } else { return {}; errorStream << "could not parse port"s; } } std::optional<net::ip_address> net::ip_resolve_hostname(const std::string& node, std::ostream& errorStream) { AddrInfo hints; AddrInfo* lookup; memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; std::optional<net::ip_address> result = {}; if (::getaddrinfo(node.data(), nullptr, &hints, &lookup) != 0) { errorStream << "unknown host "s + node; } else { assert (lookup != nullptr); sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(lookup[0].ai_addr); result = net::ip_address{ntohl(addr->sin_addr.s_addr), ntohs(addr->sin_port)}; ::freeaddrinfo(lookup); } return result; } std::optional<net::ip_address> net::ip_resolve(const std::string& input_string, std::ostream& errorStream) { int colonpos = input_string.find(":"); std::string addressString = colonpos == -1 ? input_string : mid(input_string, 0, colonpos); std::optional<net::ip_address> value; // Try scanf the IPv4 host first int parts[4]; if (std::sscanf(addressString.data(), "%d.%d.%d.%d", &parts[0], &parts[1], &parts[2], &parts[3])) { value = net::ip_address{}; for (unsigned char i = 0; i < 4; i += 1) { if (parts[i] >= 0 and parts[i] < 256) { ip_set_octet(&*value, i, parts[i]); } else { value.reset(); errorStream << "IP address value out of range"; break; } } } else { // Possibly a hostname, try resolve it value = ip_resolve_hostname(addressString, errorStream); } if (value.has_value() and colonpos != -1) { std::optional<unsigned short> port_opt = ip_parse_port(&input_string.data()[colonpos + 1], errorStream); if (port_opt.has_value()) { value->port = *port_opt; } else { value.reset(); } } return value; } END_ZFC_NAMESPACE