sources/network/ipaddress.cpp

Wed, 27 Jan 2021 14:06:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 14:06:45 +0200
changeset 184
afd63357a03d
parent 183
9b6a0daedfc0
child 185
e83ec58cc458
permissions
-rw-r--r--

deleted constructors from IPAddress

/*
	Copyright 2014 - 2016 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

// -----------------------------------------------------------------------------
//
std::string IPAddress::to_string (WithPort withport) const
{
	std::string val;

	if (withport == WITH_PORT)
		val = sprintf ("%u.%u.%u.%u:%u", octet (0), octet (1), octet (2), octet (3), port);
	else
		val = sprintf ("%u.%u.%u.%u", octet (0), octet (1), octet (2), octet (3));

	return val;
}

// -----------------------------------------------------------------------------
//
unsigned char IPAddress::octet (int n) const
{
	return (host >> ((3 - n) * 8)) & 0xFF;
}

// -----------------------------------------------------------------------------
//
void IPAddress::set_octet (int n, unsigned char oct)
{
	// TODO: make a big-endian version
	host &= ~(0xFF << (3 - n) * 8);
	host |= oct << ((3 - n) * 8);
}

// -----------------------------------------------------------------------------
//
bool IPAddress::compare (const IPAddress& other) const
{
	for (int i : range(4))
	{
		if (octet (i) != other.octet (i))
			return false;
	}

	if (port != 0
		and other.port != 0
		and port != other.port)
	{
		return false;
	}

	return true;
}

// -----------------------------------------------------------------------------
//
bool IPAddress::operator< (const IPAddress& other) const
{
	for (int i : range(4))
	{
		if (octet (i) != other.octet (i))
			return octet (i) < other.octet (i);
	}

	return port < other.port;
}

// -----------------------------------------------------------------------------
//
sockaddr_in IPAddress::to_sockaddr_in() const
{
	sockaddr_in claddr;
	memset (&claddr, 0, sizeof claddr);
	claddr.sin_addr.s_addr = htonl (host);
	claddr.sin_port = htons (port);
	claddr.sin_family = AF_INET;
	return claddr;
}

// -----------------------------------------------------------------------------
//
IPAddress IPAddress::from_string (std::string input)
{
	unsigned int parts[4];
	int colonpos = input.find (":");
	std::string addressString = colonpos == -1 ? input : mid(input, 0, colonpos);
	IPAddress value;

	// Try scanf the IPv4 host first
	if (sscanf(addressString.data(), "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3]))
	{
		for (int i : range(4))
			value.set_octet (i, parts[i]);
	}
	else
	{
		// Possibly a hostname, try resolve it
		value = IPAddress::resolve (addressString);
	}

	if (colonpos != -1)
	{
		std::optional<long> opt = to_int(mid(input, colonpos + 1, -1).data());
		if (opt.has_value())
		{
			value.port = opt.value();
		}
	}

	return value;
}

// -----------------------------------------------------------------------------
//
IPAddress IPAddress::resolve (std::string node)
{
	AddrInfo hints;
	AddrInfo* lookup;
	memset (&hints, 0, sizeof hints);
	hints.ai_family = AF_INET;

	if (getaddrinfo(node.data(), nullptr, &hints, &lookup) != 0)
		throw StringParseError ("unknown host " + node);

	IPAddress result;
	assert (lookup != nullptr);
	sockaddr_in* addr = reinterpret_cast<sockaddr_in*> (lookup[0].ai_addr);
	result.host = ntohl (addr->sin_addr.s_addr);
	result.port = ntohs (addr->sin_port);
	freeaddrinfo (lookup);
	return result;
}

END_ZFC_NAMESPACE

mercurial