19ac37a4dd23857200ccd4ca84e91314086afc81
[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 "control_common.h"
28 #include "logger.h"
29 #include "xalloc.h"
30
31 static int control_socket = -1;
32 static struct event control_event;
33 static splay_tree_t *control_socket_tree;
34 extern char *controlsocketname;
35
36 static void handle_control_data(struct bufferevent *event, void *data) {
37         tinc_ctl_request_t req;
38         size_t size;
39         tinc_ctl_request_t res;
40         struct evbuffer *res_data = NULL;
41
42         if(EVBUFFER_LENGTH(event->input) < sizeof(tinc_ctl_request_t))
43                 return;
44
45         /* Copy the structure to ensure alignment */
46         memcpy(&req, EVBUFFER_DATA(event->input), sizeof(tinc_ctl_request_t));
47
48         if(EVBUFFER_LENGTH(event->input) < req.length)
49                 return;
50
51         if(req.length < sizeof(tinc_ctl_request_t))
52                 goto failure;
53
54         memset(&res, 0, sizeof res);
55         res.type = req.type;
56         res.id = req.id;
57
58         res_data = evbuffer_new();
59         if (res_data == NULL) {
60                 res.res_errno = ENOMEM;
61                 goto respond;
62         }
63
64         if(req.type == REQ_STOP) {
65                 logger(LOG_NOTICE, _("Got '%s' command"), "stop");
66                 event_loopexit(NULL);
67                 goto respond;
68         }
69
70         if(req.type == REQ_DUMP_NODES) {
71                 logger(LOG_NOTICE, _("Got '%s' command"), "dump nodes");
72                 res.res_errno = dump_nodes(res_data);
73                 goto respond;
74         }
75
76         if(req.type == REQ_DUMP_EDGES) {
77                 logger(LOG_NOTICE, _("Got '%s' command"), "dump edges");
78                 res.res_errno = dump_edges(res_data);
79                 goto respond;
80         }
81
82         if(req.type == REQ_DUMP_SUBNETS) {
83                 logger(LOG_NOTICE, _("Got '%s' command"), "dump subnets");
84                 res.res_errno = dump_subnets(res_data);
85                 goto respond;
86         }
87
88         if(req.type == REQ_DUMP_CONNECTIONS) {
89                 logger(LOG_NOTICE, _("Got '%s' command"), "dump connections");
90                 res.res_errno = dump_connections(res_data);
91                 goto respond;
92         }
93
94         if(req.type == REQ_DUMP_GRAPH) {
95                 logger(LOG_NOTICE, _("Got '%s' command"), "dump graph");
96                 res.res_errno = dump_graph(res_data);
97                 goto respond;
98         }
99
100         logger(LOG_DEBUG, _("Malformed control command received"));
101         res.res_errno = EINVAL;
102
103 respond:
104         res.length = (sizeof res)
105                                  + ((res_data == NULL) ? 0 : EVBUFFER_LENGTH(res_data));
106         evbuffer_drain(event->input, req.length);
107         if(bufferevent_write(event, &res, sizeof res) == -1)
108                 goto failure;
109         if(res_data != NULL) {
110                 if(bufferevent_write_buffer(event, res_data) == -1)
111                         goto failure;
112                 evbuffer_free(res_data);
113         }
114         return;
115
116 failure:
117         logger(LOG_INFO, _("Closing control socket on error"));
118         evbuffer_free(res_data);
119         close(event->ev_read.ev_fd);
120         splay_delete(control_socket_tree, event);
121 }
122
123 static void handle_control_error(struct bufferevent *event, short what, void *data) {
124         if(what & EVBUFFER_EOF)
125                 logger(LOG_DEBUG, _("Control socket connection closed by peer"));
126         else
127                 logger(LOG_DEBUG, _("Error while reading from control socket: %s"), strerror(errno));
128
129         close(event->ev_read.ev_fd);
130         splay_delete(control_socket_tree, event);
131 }
132
133 static void handle_new_control_socket(int fd, short events, void *data) {
134         int newfd;
135         struct bufferevent *ev;
136         tinc_ctl_greeting_t greeting;
137
138         newfd = accept(fd, NULL, NULL);
139
140         if(newfd < 0) {
141                 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
142                 event_del(&control_event);
143                 return;
144         }
145
146         ev = bufferevent_new(newfd, handle_control_data, NULL, handle_control_error, NULL);
147         if(!ev) {
148                 logger(LOG_ERR, _("Could not create bufferevent for new control connection: %s"), strerror(errno));
149                 close(newfd);
150                 return;
151         }
152
153         memset(&greeting, 0, sizeof greeting);
154         greeting.version = TINC_CTL_VERSION_CURRENT;
155         if(bufferevent_write(ev, &greeting, sizeof greeting) == -1) {
156                 logger(LOG_ERR,
157                            _("Cannot send greeting for new control connection: %s"),
158                            strerror(errno));
159                 bufferevent_free(ev);
160                 close(newfd);
161                 return;
162         }
163
164         bufferevent_enable(ev, EV_READ);
165         splay_insert(control_socket_tree, ev);
166
167         logger(LOG_DEBUG, _("Control socket connection accepted"));
168 }
169
170 static int control_compare(const struct event *a, const struct event *b) {
171         return a < b ? -1 : a > b ? 1 : 0;
172 }
173
174 bool init_control() {
175         int result;
176         struct sockaddr_un addr;
177
178         if(strlen(controlsocketname) >= sizeof addr.sun_path) {
179                 logger(LOG_ERR, _("Control socket filename too long!"));
180                 return false;
181         }
182
183         memset(&addr, 0, sizeof addr);
184         addr.sun_family = AF_UNIX;
185         strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
186
187         control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
188
189         if(control_socket < 0) {
190                 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
191                 return false;
192         }
193
194         //unlink(controlsocketname);
195         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
196         
197         if(result < 0 && errno == EADDRINUSE) {
198                 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
199                 if(result < 0) {
200                         logger(LOG_WARNING, _("Removing old control socket."));
201                         unlink(controlsocketname);
202                         result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
203                 } else {
204                         close(control_socket);
205                         if(netname)
206                                 logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
207                         else
208                                 logger(LOG_ERR, _("Another tincd is already running."));
209                         return false;
210                 }
211         }
212
213         if(result < 0) {
214                 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
215                 close(control_socket);
216                 return false;
217         }
218
219         if(listen(control_socket, 3) < 0) {
220                 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
221                 close(control_socket);
222                 return false;
223         }
224
225         control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)bufferevent_free);
226
227         event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
228         event_add(&control_event, NULL);
229
230         return true;
231 }
232
233 void exit_control() {
234         event_del(&control_event);
235         close(control_socket);
236         unlink(controlsocketname);
237 }