Start of control socket implementation.
[tinc] / src / control.c
1 /*
2     control.c -- Control socket handling.
3     Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include <sys/un.h>
23
24 #include "system.h"
25 #include "conf.h"
26 #include "control.h"
27 #include "logger.h"
28 #include "xalloc.h"
29
30 static int control_socket = -1;
31 static struct event control_event;
32 static splay_tree_t *control_socket_tree;
33 extern char *controlfilename;
34
35 static void handle_control_data(int fd, short events, void *event) {
36         char buf[MAXBUFSIZE];
37         size_t inlen;
38
39         inlen = read(fd, buf, sizeof buf);
40
41         if(inlen <= 0) {
42                 logger(LOG_DEBUG, _("Closing control socket"));
43                 event_del(event);
44                 splay_delete(control_socket_tree, event);
45                 close(fd);
46         }
47 }
48
49 static void handle_new_control_socket(int fd, short events, void *data) {
50         int newfd;
51         struct event *ev;
52
53         newfd = accept(fd, NULL, NULL);
54
55         if(newfd < 0) {
56                 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
57                 event_del(&control_event);
58                 return;
59         }
60
61         ev = xmalloc(sizeof *ev);
62         event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev);
63         event_add(ev, NULL);
64         splay_insert(control_socket_tree, ev);
65
66         logger(LOG_DEBUG, _("Control socket connection accepted"));
67 }
68
69 static int control_compare(const struct event *a, const struct event *b) {
70         return a < b ? -1 : a > b ? 1 : 0;
71 }
72
73 void init_control() {
74         struct sockaddr_un addr;
75
76         control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free);
77
78         if(strlen(controlfilename) >= sizeof addr.sun_path) {
79                 logger(LOG_ERR, _("Control socket filename too long!"));
80                 return;
81         }
82
83         memset(&addr, 0, sizeof addr);
84         addr.sun_family = AF_UNIX;
85         strncpy(addr.sun_path, controlfilename, sizeof addr.sun_path - 1);
86
87         control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
88
89         if(control_socket < 0) {
90                 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
91                 return;
92         }
93
94         unlink(controlfilename);
95         if(bind(control_socket, (struct sockaddr *)&addr, sizeof addr) < 0) {
96                 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlfilename, strerror(errno));
97                 close(control_socket);
98                 return;
99         }
100
101         if(listen(control_socket, 3) < 0) {
102                 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlfilename, strerror(errno));
103                 close(control_socket);
104                 return;
105         }
106
107         event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
108         event_add(&control_event, NULL);
109 }
110
111 void exit_control() {
112         if(control_socket >= 0) {
113                 event_del(&control_event);
114                 close(control_socket);
115         }
116 }