/*
 *
 *
 *    l4address.cc
 *
 *    C++ source code for class my::l4address 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++/l4address.h>

void my::l4address::to_sockaddr (sockaddr *sa) const {
	/* zero out the structure */
	memset (((sockaddr_in*)(sa)), 0, sizeof(sockaddr_in));

	/* fill in the structure */
	((sockaddr_in*)(sa))->sin_family = PF_INET;
	sa_address.copy_to(((sockaddr_in*)(sa))->sin_addr);
	((sockaddr_in*)(sa))->sin_port = htons(sa_port);
}


void my::l4address::from_sockaddr (const sockaddr *sa) {
	sa_address = ((const sockaddr_in*)(sa))->sin_addr;
	sa_port = ntohs(((const sockaddr_in*)(sa))->sin_port);
}


std::string & my::l4address::to_string (std::string &s) const {
	char cbuf [16];
	snprintf (cbuf, sizeof(cbuf), "%u", (unsigned)(sa_port));
	sa_address.to_string(s);
	s += ":";
	s += cbuf;
	return s;
}

std::string my::l4address::to_string (void) const {
	char cbuf [16];
	std::string s;
	snprintf (cbuf, sizeof(cbuf), "%u", (unsigned)(sa_port));
	sa_address.to_string(s);
	s += ":";
	s += cbuf;
	return std::string(s);
}

my::socket::address & my::l4address::parse (const std::string &s) {
	/* implement me */
	return (*this);
}
