2 control.c -- Control socket handling.
3 Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
27 #include "control_common.h"
31 static int control_socket = -1;
32 static struct event control_event;
33 static splay_tree_t *control_socket_tree;
34 extern char *controlsocketname;
36 static void handle_control_data(struct bufferevent *event, void *data) {
37 tinc_ctl_request_t req;
39 tinc_ctl_request_t res;
40 struct evbuffer *res_data = NULL;
42 if(EVBUFFER_LENGTH(event->input) < sizeof(tinc_ctl_request_t))
45 /* Copy the structure to ensure alignment */
46 memcpy(&req, EVBUFFER_DATA(event->input), sizeof(tinc_ctl_request_t));
48 if(EVBUFFER_LENGTH(event->input) < req.length)
51 if(req.length < sizeof(tinc_ctl_request_t))
54 memset(&res, 0, sizeof res);
58 res_data = evbuffer_new();
59 if (res_data == NULL) {
60 res.res_errno = ENOMEM;
64 if(req.type == REQ_STOP) {
65 logger(LOG_NOTICE, _("Got stop command"));
70 logger(LOG_DEBUG, _("Malformed control command received"));
71 res.res_errno = EINVAL;
74 res.length = (sizeof res)
75 + ((res_data == NULL) ? 0 : EVBUFFER_LENGTH(res_data));
76 evbuffer_drain(event->input, req.length);
77 if(bufferevent_write(event, &res, sizeof res) == -1)
79 if(res_data != NULL) {
80 if(bufferevent_write_buffer(event, res_data) == -1)
82 evbuffer_free(res_data);
87 logger(LOG_INFO, _("Closing control socket on error"));
88 evbuffer_free(res_data);
89 close(event->ev_read.ev_fd);
90 splay_delete(control_socket_tree, event);
93 static void handle_control_error(struct bufferevent *event, short what, void *data) {
94 if(what & EVBUFFER_EOF)
95 logger(LOG_DEBUG, _("Control socket connection closed by peer"));
97 logger(LOG_DEBUG, _("Error while reading from control socket: %s"), strerror(errno));
99 close(event->ev_read.ev_fd);
100 splay_delete(control_socket_tree, event);
103 static void handle_new_control_socket(int fd, short events, void *data) {
105 struct bufferevent *ev;
106 tinc_ctl_greeting_t greeting;
108 newfd = accept(fd, NULL, NULL);
111 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
112 event_del(&control_event);
116 ev = bufferevent_new(newfd, handle_control_data, NULL, handle_control_error, NULL);
118 logger(LOG_ERR, _("Could not create bufferevent for new control connection: %s"), strerror(errno));
123 memset(&greeting, 0, sizeof greeting);
124 greeting.version = TINC_CTL_VERSION_CURRENT;
125 if(bufferevent_write(ev, &greeting, sizeof greeting) == -1) {
127 _("Cannot send greeting for new control connection: %s"),
129 bufferevent_free(ev);
134 bufferevent_enable(ev, EV_READ);
135 splay_insert(control_socket_tree, ev);
137 logger(LOG_DEBUG, _("Control socket connection accepted"));
140 static int control_compare(const struct event *a, const struct event *b) {
141 return a < b ? -1 : a > b ? 1 : 0;
144 bool init_control() {
146 struct sockaddr_un addr;
148 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
149 logger(LOG_ERR, _("Control socket filename too long!"));
153 memset(&addr, 0, sizeof addr);
154 addr.sun_family = AF_UNIX;
155 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
157 control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
159 if(control_socket < 0) {
160 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
164 //unlink(controlsocketname);
165 result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
167 if(result < 0 && errno == EADDRINUSE) {
168 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
170 logger(LOG_WARNING, _("Removing old control socket."));
171 unlink(controlsocketname);
172 result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
174 close(control_socket);
176 logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
178 logger(LOG_ERR, _("Another tincd is already running."));
184 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
185 close(control_socket);
189 if(listen(control_socket, 3) < 0) {
190 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
191 close(control_socket);
195 control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)bufferevent_free);
197 event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
198 event_add(&control_event, NULL);
203 void exit_control() {
204 event_del(&control_event);
205 close(control_socket);
206 unlink(controlsocketname);