/*
 *
 *
 *    l4address.h
 *
 *    C++ header file for class 'l4address.'
 *
 *    =========================================================================
 *
 *    Copyright (C) 2001-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
 *
 *    =========================================================================
 *
 *
 */


#ifndef __L4ADDRESS_H
#define __L4ADDRESS_H

	#include <my++/socket.h>
	#include <my++/inaddr.h>
	#include <string>

	#ifndef in_port_t
	typedef uint16_t in_port_t;
	#endif

	namespace my {

		/*
		 *
		 *    address
		 *
		 */

		class l4address : public socket::address {
			private:
				my::inet_address sa_address;
				in_port_t sa_port;

			public: /* from socket::address */
				void to_sockaddr (sockaddr *sa) const;
				void from_sockaddr (const sockaddr *sa);

				my::socket::address & parse (const std::string &s);
				std::string & to_string (std::string &s) const;
				std::string   to_string (void) const;

			public:
				l4address (void) { };
				l4address (in_port_t p) : sa_port(p) { /* noop */ };
				l4address (const std::string &a, in_port_t p) : sa_address(a), sa_port(p) { /* noop */ };
				l4address (const inet_address &a, in_port_t p) : sa_address(a), sa_port(p) { /* noop */ };
				l4address (const l4address &a) : sa_address(a.sa_address), sa_port(a.sa_port) { /* noop */ };
				~l4address (void) { /* noop */ };

				int port (void) const { return static_cast<int>(sa_port); };
				void port (int p) { sa_port = static_cast<in_port_t>(p); };

				my::inet_address host (void) const { return sa_address; };
				void host (const my::inet_address &a) { sa_address = a; };

				const l4address &operator = (const sockaddr_in &sa) { from_sockaddr(&sa); return (*this); };

				// these complement virtual functions from base class
				void to_sockaddr (sockaddr_in *sa) const   { to_sockaddr  ((sockaddr*)(sa)); };
				void from_sockaddr (const sockaddr_in *sa) { from_sockaddr((const sockaddr*)(sa)); };
		};
	}

#endif /* __L4ADDRESS_H */

