taskrambler  0.1.8
Web server and task management solution.
socket.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 
27 #include "socket.h"
28 #include "logger.h"
29 #include "class.h"
30 
31 static
32 int
33 socketCtor(void * _this, va_list * params)
34 {
35  Sock this = _this;
36  int reUse = 1; //! \todo make this configurable
37  int port;
38 
39  this->log = va_arg(* params, Logger);
40  port = va_arg(* params, int);
41 
42  //! if port is -1 do not initialize the socket. (Used with accept)
43  if (-1 == port) {
44  return 0;
45  } else {
46  this->port = port;
47  }
48 
49  //! Create socket for incoming connections
50  if (-1 == (this->handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))) {
51  loggerLog(this->log, LOGGER_CRIT,
52  "error opening socket: %s - service terminated",
53  strerror(errno));
54  return -1;
55  }
56 
57  //! Make the socket REUSE a TIME_WAIT socket
58  setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof(reUse));
59 
60  return 0;
61 }
62 
63 static
64 void
65 socketDtor(void * _this)
66 {
67  Sock this = _this;
68 
69  if (STDERR_FILENO < this->handle) {
70  shutdown(this->handle, SHUT_RDWR);
71  close(this->handle);
72  }
73 }
74 
75 INIT_IFACE(Class, socketCtor, socketDtor, NULL);
76 CREATE_CLASS(Sock, NULL, IFACE(Class));
77 
78 // vim: set ts=4 sw=4:
static int socketCtor(void *_this, va_list *params)
Definition: socket.c:33
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
#define IFACE(name)
Definition: interface.h:34
CREATE_CLASS(Sock, NULL, IFACE(Class))
INIT_IFACE(Class, socketCtor, socketDtor, NULL)
static void socketDtor(void *_this)
Definition: socket.c:65