socket.c
1.26 KB
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
47
48
49
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/types.h> /* SO_REUSEADDR */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <errno.h> /* for errno */
#include <stdarg.h>
#include "logger.h"
#include "cclass.h"
#include "socket.h"
INIT_CLASS(SOCK);
__construct(SOCK)
{
int reUse = 1; /* TODO: make this configurable */
this->logger = 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))) {
logger_log(this->logger, 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));
}
__destruct(SOCK)
{
if (0 != this->handle) {
shutdown(this->handle, SHUT_RDWR);
close(this->handle);
}
}
__jsonConst(SOCK) {}
__toJson(SOCK) {}
__clear(SOCK) {}
// vim: set ts=4 sw=4: