/*
 *
 *
 *    unixaddress.cc
 *
 *    C++ source code for class my::unixaddress 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++/unixaddress.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>

// SERIOUS FIXME: figure out how to get this portably from the system headers
#define LOCAL_PATH_MAX (108)


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

	/* fill in the structure */
	((sockaddr_un*)(sa))->sun_family = PF_UNIX;
	strncpy(((sockaddr_un*)(sa))->sun_path, sa_address.c_str(), LOCAL_PATH_MAX);
}


void my::unixaddress::from_sockaddr (const sockaddr *sa) {
	sa_address = ((const sockaddr_un*)(sa))->sun_path;
}

std::string & my::unixaddress::to_string (std::string &s) const {
	s = sa_address;
	return s;
}

std::string my::unixaddress::to_string (void) const {
	return std::string(sa_address);
}

my::socket::address & my::unixaddress::parse (const std::string &s) {
	sa_address = s;
	return (*this);
}
