Use bufferevents to handle control socket buffering.
[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 *controlsocketname;
34
35 static void handle_control_data(struct bufferevent *event, void *data) {
36         char *line = evbuffer_readline(event->input);
37         if(!line)
38                 return;
39         
40         if(!strcasecmp(line, "stop")) {
41                 logger(LOG_NOTICE, _("Got stop command"));
42                 event_loopexit(NULL);
43                 return;
44         }
45
46         logger(LOG_DEBUG, _("Malformed control command received"));
47         close(event->ev_read.ev_fd);
48         splay_delete(control_socket_tree, event);
49 }
50
51 static void handle_control_error(struct bufferevent *event, short what, void *data) {
52         if(what & EVBUFFER_EOF)
53                 logger(LOG_DEBUG, _("Control socket connection closed by peer"));
54         else
55                 logger(LOG_DEBUG, _("Error while reading from control socket: %s"), strerror(errno));
56
57         close(event->ev_read.ev_fd);
58         splay_delete(control_socket_tree, event);
59 }
60
61 static void handle_new_control_socket(int fd, short events, void *data) {
62         int newfd;
63         struct bufferevent *ev;
64
65         newfd = accept(fd, NULL, NULL);
66
67         if(newfd < 0) {
68                 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
69                 event_del(&control_event);
70                 return;
71         }
72
73         ev = bufferevent_new(newfd, handle_control_data, NULL, handle_control_error, NULL);
74         if(!ev) {
75                 logger(LOG_ERR, _("Could not create bufferevent for new control connection: %s"), strerror(errno));
76                 close(newfd);
77                 return;
78         }
79
80         bufferevent_enable(ev, EV_READ);
81         splay_insert(control_socket_tree, ev);
82
83         logger(LOG_DEBUG, _("Control socket connection accepted"));
84 }
85
86 static int control_compare(const struct event *a, const struct event *b) {
87         return a < b ? -1 : a > b ? 1 : 0;
88 }
89
90 bool init_control() {
91         int result;
92         struct sockaddr_un addr;
93
94         if(strlen(controlsocketname) >= sizeof addr.sun_path) {
95                 logger(LOG_ERR, _("Control socket filename too long!"));
96                 return false;
97         }
98
99         memset(&addr, 0, sizeof addr);
100         addr.sun_family = AF_UNIX;
101         strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
102
103         control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
104
105         if(control_socket < 0) {
106                 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
107                 return false;
108         }
109
110         //unlink(controlsocketname);
111         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
112         
113         if(result < 0 && errno == EADDRINUSE) {
114                 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
115                 if(result < 0) {
116                         logger(LOG_WARNING, _("Removing old control socket."));
117                         unlink(controlsocketname);
118                         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
119                 } else {
120                         close(control_socket);
121                         if(netname)
122                                 logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
123                         else
124                                 logger(LOG_ERR, _("Another tincd is already running."));
125                         return false;
126                 }
127         }
128
129         if(result < 0) {
130                 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
131                 close(control_socket);
132                 return false;
133         }
134
135         if(listen(control_socket, 3) < 0) {
136                 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
137                 close(control_socket);
138                 return false;
139         }
140
141         control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)bufferevent_free);
142
143         event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
144         event_add(&control_event, NULL);
145
146         return true;
147 }
148
149 void exit_control() {
150         event_del(&control_event);
151         close(control_socket);
152         unlink(controlsocketname);
153 }