sources/network/ipaddress.cpp

Tue, 26 May 2015 11:41:58 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 26 May 2015 11:41:58 +0300
changeset 81
a18aaf460648
parent 73
07dda51a7a8e
child 88
08ccaf26cffd
permissions
-rw-r--r--

Allow compilation on Windows/MinGW

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifdef _WIN32
using AddrInfo = ADDRINFOA;
#else
using AddrInfo = struct 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) {}

// -----------------------------------------------------------------------------
//
METHOD
IPAddress::to_string (WithPort withport) const -> String
{
	String val;

	if (withport == IP_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;
}

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

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

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

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

	return true;
}

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

	return port < other.port;
}

// -----------------------------------------------------------------------------
//
METHOD
IPAddress::to_sockaddr_in() const -> sockaddr_in
{
	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;
}

// -----------------------------------------------------------------------------
//
STATIC METHOD
IPAddress::from_string (String input) -> IPAddress
{
	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 (short i = 0; i < 4; ++i)
			value.set_octet (i, parts[i]);
	}
	else
	{
		// Possibly a hostname, try resolve it
		value = IPAddress::resolve (addressString);
	}

	if (colonpos != -1)
		value.port = input.mid (colonpos + 1, -1).to_int();

	return value;
}

// -----------------------------------------------------------------------------
//
STATIC METHOD
IPAddress::resolve (String node) -> IPAddress
{
	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;
}

mercurial