sources/network/ipaddress.cpp

Wed, 20 Jul 2016 16:01:10 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Wed, 20 Jul 2016 16:01:10 +0300
changeset 145
d0aedc9be448
parent 137
485cb6d6b98c
child 182
20ca0a6be175
permissions
-rw-r--r--

Renamed String methods, and reformatted mystring.h

/*
	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

// -----------------------------------------------------------------------------
//
IPAddress::IPAddress() :
	host (0),
	port (0) {}

// -----------------------------------------------------------------------------
//
IPAddress::IPAddress (unsigned long host, unsigned short port) :
	host (host),
	port (port) {}

// -----------------------------------------------------------------------------
//
IPAddress::IPAddress (const IPAddress& other) :
	host (other.host),
	port (other.port) {}

// -----------------------------------------------------------------------------
//
String IPAddress::to_string (WithPort withport) const
{
	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 (String input)
{
	unsigned int parts[4];
	int colonpos = input.find (":");
	String addressString = colonpos == -1 ? input : input.mid (0, colonpos);
	IPAddress value;

	// Try scanf the IPv4 host first
	if (sscanf (addressString, "%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)
		value.port = (unsigned short) input.mid (colonpos + 1, -1).toInt();

	return value;
}

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

	if (getaddrinfo (node, 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