/*  $Id: ip_address_getsock.cpp,v 1.21 2003/11/18 10:17:46 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#ifdef USE_GETADDR

// msvc got a strange bug that makes #ifndef USE_GETADDR sometimes not work!!
// thats why we check for USE_GETADDR and not the opposite.
// -- mbn 20 dec 2002

#else

#include "API/Core/System/clanstring.h"
#include "API/Core/System/error.h"
#include "ip_address_getsock.h"

#include <cstdlib>

#ifndef WIN32
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <arpa/inet.h>
	#include <netdb.h>
#else
	#include <windows.h>
#endif

/////////////////////////////////////////////////////////////////////////////
// CL_IPAddress construction:

CL_IPAddress_GetSock::CL_IPAddress_GetSock()
: ip(INADDR_ANY), port(0)
{
}

CL_IPAddress_GetSock::CL_IPAddress_GetSock(const std::string &new_port)
: ip(INADDR_ANY)
{
	port = CL_String::to_int(new_port);
}

CL_IPAddress_GetSock::CL_IPAddress_GetSock(const std::string &hostname, const std::string &new_port)
{
	port = CL_String::to_int(new_port);

	unsigned int addr = 0;

	addr = inet_addr(hostname.c_str());
	if (addr == INADDR_NONE)
	{
		hostent *host = gethostbyname(hostname.c_str());
		if (host == NULL) throw CL_Error("Could not lookup DNS name");

		addr = *((unsigned int*) host->h_addr_list[0]);
	}

	ip = addr;
}

CL_IPAddress_GetSock::CL_IPAddress_GetSock(const CL_IPAddress &copy)
{
	set_address(copy.get_address(),copy.get_port());
}

/////////////////////////////////////////////////////////////////////////////
// CL_IPAddress attributes:

void CL_IPAddress_GetSock::get_addrinfo(int type, sockaddr &addr, int &len, int domain) const
{
	// Domain ignored since it will always be ipv4
	sockaddr_in ret;
	ret.sin_family = AF_INET;
	ret.sin_port = htons(port);
	ret.sin_addr.s_addr = ip;
	len = sizeof(sockaddr_in);
	memcpy(&addr, &ret, len);
}

std::string CL_IPAddress_GetSock::get_address() const
{
	struct in_addr foo;
	foo.s_addr = ip;
	char *addr = inet_ntoa(foo);
	return std::string(addr);	
}

std::string CL_IPAddress_GetSock::get_port() const
{
	return CL_String::from_int(port);
}

bool CL_IPAddress_GetSock::operator == (const CL_IPAddress &other) const
{
	return (get_address() == other.get_address()) && (get_port() == other.get_port());
}

bool CL_IPAddress_GetSock::operator < (const CL_IPAddress &other) const
{
	if (get_address() == other.get_address())
		return atoi(get_port().c_str()) < atoi(other.get_port().c_str());
	return get_address() < other.get_address();
}

bool CL_IPAddress_GetSock::operator > (const CL_IPAddress &other) const
{
	if (get_address() == other.get_address())
		return atoi(get_port().c_str()) > atoi(other.get_port().c_str());
	return get_address() > other.get_address();
}

/////////////////////////////////////////////////////////////////////////////
// CL_IPAddress operations:


void CL_IPAddress_GetSock::set_address(const std::string &hostname)
{
	unsigned int addr = 0;

	addr = inet_addr(hostname.c_str());
	if (addr == INADDR_NONE)
	{
		hostent *host = gethostbyname(hostname.c_str());
		if (host == NULL) throw CL_Error("Could not lookup DNS name");

		addr = *((unsigned int*) host->h_addr_list[0]);
	}

	ip = addr;
}


void CL_IPAddress_GetSock::set_address(const std::string &hostname, const std::string &port)
{
	set_port(port);
	set_address(hostname);
}

void CL_IPAddress_GetSock::set_port(const std::string &new_port)
{
	port = atoi(new_port.c_str());
	ip = INADDR_ANY;
}

std::string CL_IPAddress_GetSock::dns_lookup() const
{
	hostent *host = gethostbyaddr((const char *) &ip, sizeof(ip), AF_INET);
	if (host == NULL) throw CL_Error("Unable to lookup IP address");
	return std::string(host->h_name);
}

bool CL_IPAddress_GetSock::is_ipv4() const
{
	return true;
}

bool CL_IPAddress_GetSock::is_ipv6() const
{
	return false;
}

#endif
