Branch data Line data Source code
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 : 9 : socketCtor(void * _this, va_list * params)
34 : : {
35 : 9 : Sock this = _this;
36 : 9 : int reUse = 1; //! \todo make this configurable
37 : : int port;
38 : :
39 : 9 : this->log = va_arg(* params, Logger);
40 : 9 : port = va_arg(* params, int);
41 : :
42 : : //! if port is -1 do not initialize the socket. (Used with accept)
43 : 9 : if (-1 == port) {
44 : 3 : return 0;
45 : : } else {
46 : 6 : this->port = port;
47 : : }
48 : :
49 : : //! Create socket for incoming connections
50 : 6 : if (-1 == (this->handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))) {
51 : 0 : loggerLog(this->log, LOGGER_CRIT,
52 : : "error opening socket: %s - service terminated",
53 : 0 : strerror(errno));
54 : 0 : return -1;
55 : : }
56 : :
57 : : //! Make the socket REUSE a TIME_WAIT socket
58 : 6 : setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof(reUse));
59 : :
60 : 6 : return 0;
61 : : }
62 : :
63 : : static
64 : : void
65 : 12 : socketDtor(void * _this)
66 : : {
67 : 12 : Sock this = _this;
68 : :
69 : 12 : if (STDERR_FILENO < this->handle) {
70 : 11 : shutdown(this->handle, SHUT_RDWR);
71 : 11 : close(this->handle);
72 : : }
73 : 12 : }
74 : :
75 : : INIT_IFACE(Class, socketCtor, socketDtor, NULL);
76 : 2 : CREATE_CLASS(Sock, NULL, IFACE(Class));
77 : :
78 : : // vim: set ts=4 sw=4:
|