socket.c
978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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: