/**
 * \file socktools.C
 * This file is part of LyX, the document processor.
 * Licence details can be found in the file COPYING.
 *
 * \author Joćo Luis M. Assirati
 *
 * Full author contact details are available in file CREDITS.
 */

#include <config.h>

#include "support/socktools.h"

#if !defined (HAVE_FCNTL)
// We provide stubs because we don't (yet?) support the native OS API.

namespace lyx {
namespace support {
namespace socktools {

int listen(std::string const &, int)
{
	return -1;
}


int accept(int)
{
	return -1;
}

} // namespace socktools
} // namespace support
} // namespace lyx

#else // defined (HAVE_FCNTL)

#include "support/lyxlib.h"

#include "debug.h"

#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <cerrno>

using std::endl;
using std::string;

#ifndef CXX_GLOBAL_CSTD
using std::strerror;
#endif

// This MACRO eppears to be defined only on Linux.
#if !defined(SUN_LEN)
#define SUN_LEN(su) \
	(sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
#endif


namespace lyx {
namespace support {
namespace socktools {

// Returns a local socket already in the "listen" state (or -1 in case
// of error). The first argument is the socket address, the second
// is the length of the queue for connections. If successful, a socket
// special file 'name' will be created in the filesystem.
int listen(string const & name, int queue)
{
	int fd; // File descriptor for the socket
	sockaddr_un addr; // Structure that hold the socket address

	// We use 'name' to fill 'addr'
	string::size_type len = name.size();
	// the field sun_path in sockaddr_un is a char[108]
	if (len > 107) {
		lyxerr << "lyx: Socket address '" << name << "' too long."
		       << endl;
		return -1;
	}
	// Synonims for AF_UNIX are AF_LOCAL and AF_FILE
	addr.sun_family = AF_UNIX;
	name.copy(addr.sun_path, 107);
	addr.sun_path[len] = '\0';

	// This creates a file descriptor for the socket
	// Synonims for PF_UNIX are PF_LOCAL and PF_FILE
	// For local sockets, the protocol is always 0
	// socket() returns -1 in case of error
	if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
		lyxerr << "lyx: Could not create socket descriptor: "
		       << strerror(errno) << endl;
		return -1;
	}

	// Set NONBLOCK mode for the file descriptor
	if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
		lyxerr << "lyx: Could not set NONBLOCK mode for socket descriptor: "
		     << strerror(errno) << endl;
		::close(fd);
		return -1;
	}

	// bind() gives the local address 'name' for 'fd', also creating
	// the socket special file in the filesystem. bind() returns -1
	// in case of error
	if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
		lyxerr << "lyx: Could not bind address '" << name
		       << "' to socket descriptor: " << strerror(errno) << endl;
		::close(fd);
		lyx::support::unlink(name);
		return -1;
	}

	// Puts the socket in listen state, that is, ready to accept
	// connections. The second parameter of listen() defines the
	// maximum length the queue of pending connections may grow to.
	// It is not a restriction on the number of connections the socket
	// can accept. Returns -1 in case of error
	if (::listen (fd, queue) == -1) {
		lyxerr << "lyx: Could not put socket in 'listen' state: "
		       << strerror(errno) << endl;
		::close(fd);
		lyx::support::unlink(name);
		return -1;
	}

	return fd;
}

// Returns a file descriptor for a new connection from the socket
// descriptor 'sd' (or -1 in case of error)
int accept(int sd)
{
	int fd;

	// Returns the new file descriptor or -1 in case of error
	// Using null pointers for the second and third arguments
	// dismiss all information about the connecting client
	if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
		lyxerr << "lyx: Could not accept connection: "
		       << strerror(errno) << endl;
		return -1;
	}

	// Sets NONBLOCK mode for the file descriptor
	if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
		lyxerr << "lyx: Could not set NONBLOCK mode for connection: "
		       << strerror(errno) << endl;
		::close(fd);
		return -1;
	}
	return fd;
}

} // namespace socktools
} // namespace support
} // namespace lyx

#endif // defined (HAVE_FCNTL)
