/*
 *
 *
 *    hostent.cc
 *
 *    C++ source code for class my::hostent member functions and/or static
 *    member data.
 *
 *    =========================================================================
 *
 *    Copyright (C) 1999-2003 by Matt Roberts, KK5JY.
 *
 *    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
 *
 *    =========================================================================
 *
 *
 */


#include <my++/hostent.h>



/*

   my::host_entry::host_entry (host_entry &)

 */

my::host_entry::host_entry (const host_entry &he) {
	copy (he.the_alias.begin(),    // source begin
			he.the_alias.end(),      // source end
			the_alias.begin());      // destination begin
	copy (he.the_address.begin(),
			he.the_address.end(),
			the_address.begin());
	the_name = he.the_name;
	valid = he.valid;
}


/*

   my::host_entry::operator = (...)

 */

my::host_entry &my::host_entry::operator = (const std::string &s) {
	lookup(s);
	return (*this);
}

my::host_entry &my::host_entry::operator = (const in_addr &s) {
	lookup(s);
	return (*this);
}

my::host_entry &my::host_entry::operator = (const my::host_entry &he) {
	copy (he.the_alias.begin(),    // source begin
			he.the_alias.end(),      // source end
			the_alias.begin());      // destination begin
	copy (he.the_address.begin(),
			he.the_address.end(),
			the_address.begin());
	the_name = he.the_name;
	valid = he.valid;
	return (*this);
}


/*

   my::host_entry::getHostname (std::string &)

 */

	bool my::host_entry::getHostname (std::string &a) {
		if (!valid)
			return false;
		a = the_name;
		return true;
	}


/*

   my::host_entry::getAddress (const int, in_addr &)

 */

	bool my::host_entry::getAddress (const size_t i, in_addr &a) {
		if (!valid)
			return false;
		if (i >= the_address.size())
			return false;

		a = the_address [i];
		return true;
	}


/*

   my::host_entry::getAlias (const int, std::string &)

 */

	bool my::host_entry::getAlias (size_t i, std::string &a) {
		if (!valid)
			return false;
		if (i >= the_alias.size())
			return false;

		a = the_alias [i];
		return true;
	}


/*

   my::host_entry::getAliases (std::vector<std::string> &)

 */

	bool my::host_entry::getAliases (std::vector<std::string> &l) {
		if (!valid)
			return false;

		copy (the_alias.begin(), the_alias.end(), l.begin());
		return true;
	}


/*

   my::host_entry::getAddresses (std::vector<in_addr> &)

 */

	bool my::host_entry::getAliases (std::vector<in_addr> &l) {
		if (!valid)
			return false;
		copy (the_address.begin(), the_address.end(), l.begin());
		return true;
	}


/*

   my::host_entry::lookup (...)

 */

bool my::host_entry::lookup (const std::string &s) {
	hostent *he_ptr;

	valid = false;
	he_ptr = gethostbyname (s.c_str());
	return process (he_ptr);
}

bool my::host_entry::lookup (const in_addr &in) {
	hostent *he_ptr;
	in_addr i = in;

	valid = false;
	he_ptr = gethostbyaddr ((char*)(&i), sizeof (in_addr), AF_INET);
	return process (he_ptr);
}

bool my::host_entry::lookup (const in_addr *in) {
	hostent *he_ptr;
	in_addr i = *in;

	valid = false;
	he_ptr = gethostbyaddr ((char*)(&i), sizeof (in_addr), AF_INET);
	return process (he_ptr);
}


/*

   my::host_entry::process (hostent *)  [private]

 */

bool my::host_entry::process (hostent *he_ptr) {
	hostent          he_obj;
	char **cpp;
	in_addr **iap;

	if (he_ptr)
		he_obj = *he_ptr;
	else
		return false;

	the_name = he_obj.h_name;
	cpp = he_obj.h_aliases;
	iap = (in_addr **)(he_obj.h_addr_list);

	the_alias.clear();
	the_address.clear();

	if (cpp) {
		while (*cpp != 0) {
			the_alias.push_back (*cpp);
			cpp++;
		}
	}

	if (iap) {
		while (*iap != 0) {
			the_address.push_back (**iap);
			iap++;
		}
	}

	valid = true;
	return true;
}
