socket.c 978 Bytes
#include <errno.h>
#include <stdlib.h>

#include "socket.h"
#include "logger.h"
#include "interface/class.h"
#include "interface/logger.h"

static
void
ctor(void * _this, va_list * params)
{
	Sock this = _this;
	int reUse   = 1;     /* TODO: make this configurable */

	this->log  = va_arg(* params, Logger);
	this->port = va_arg(* params, int);

	/* Create socket for incoming connections */
	if (-1 == (this->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) {
		loggerLog(this->log, LOGGER_CRIT,
				"error opening socket: %s - service terminated",
				strerror(errno));
		exit(EXIT_FAILURE);
	}

	/* Make the socket REUSE a TIME_WAIT socket */
	setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse));
}

static
void
dtor(void * _this)
{
	Sock this = _this;

	if (0 != this->handle) {
		shutdown(this->handle, SHUT_RDWR);
		close(this->handle);
	}
}

INIT_IFACE(Class, ctor, dtor, NULL);
CREATE_CLASS(Sock, NULL, IFACE(Class));

// vim: set ts=4 sw=4: