2007-05-18 16:52:34 +00:00
|
|
|
/*
|
|
|
|
control.c -- Control socket handling.
|
|
|
|
Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "control.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "xalloc.h"
|
|
|
|
|
|
|
|
static int control_socket = -1;
|
|
|
|
static struct event control_event;
|
|
|
|
static splay_tree_t *control_socket_tree;
|
2007-05-19 14:13:21 +00:00
|
|
|
extern char *controlsocketname;
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
static void handle_control_data(int fd, short events, void *event) {
|
|
|
|
char buf[MAXBUFSIZE];
|
|
|
|
size_t inlen;
|
|
|
|
|
|
|
|
inlen = read(fd, buf, sizeof buf);
|
|
|
|
|
|
|
|
if(inlen <= 0) {
|
|
|
|
logger(LOG_DEBUG, _("Closing control socket"));
|
|
|
|
event_del(event);
|
|
|
|
splay_delete(control_socket_tree, event);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_new_control_socket(int fd, short events, void *data) {
|
|
|
|
int newfd;
|
|
|
|
struct event *ev;
|
|
|
|
|
|
|
|
newfd = accept(fd, NULL, NULL);
|
|
|
|
|
|
|
|
if(newfd < 0) {
|
|
|
|
logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
|
|
|
|
event_del(&control_event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev = xmalloc(sizeof *ev);
|
|
|
|
event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev);
|
|
|
|
event_add(ev, NULL);
|
|
|
|
splay_insert(control_socket_tree, ev);
|
|
|
|
|
|
|
|
logger(LOG_DEBUG, _("Control socket connection accepted"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control_compare(const struct event *a, const struct event *b) {
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2007-05-19 14:13:21 +00:00
|
|
|
bool init_control() {
|
|
|
|
int result;
|
2007-05-18 16:52:34 +00:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
2007-05-19 14:13:21 +00:00
|
|
|
if(strlen(controlsocketname) >= sizeof addr.sun_path) {
|
2007-05-18 16:52:34 +00:00
|
|
|
logger(LOG_ERR, _("Control socket filename too long!"));
|
2007-05-19 14:13:21 +00:00
|
|
|
return false;
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof addr);
|
|
|
|
addr.sun_family = AF_UNIX;
|
2007-05-19 14:13:21 +00:00
|
|
|
strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if(control_socket < 0) {
|
|
|
|
logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
|
2007-05-19 14:13:21 +00:00
|
|
|
return false;
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 14:13:21 +00:00
|
|
|
//unlink(controlsocketname);
|
|
|
|
result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
|
|
|
|
|
|
|
|
if(result < 0 && errno == EADDRINUSE) {
|
|
|
|
result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
|
|
|
|
if(result < 0) {
|
|
|
|
logger(LOG_WARNING, _("Removing old control socket."));
|
|
|
|
unlink(controlsocketname);
|
|
|
|
result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
|
|
|
|
} else {
|
|
|
|
close(control_socket);
|
|
|
|
if(netname)
|
|
|
|
logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
|
|
|
|
else
|
|
|
|
logger(LOG_ERR, _("Another tincd is already running."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result < 0) {
|
|
|
|
logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
|
2007-05-18 16:52:34 +00:00
|
|
|
close(control_socket);
|
2007-05-19 14:13:21 +00:00
|
|
|
return false;
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(listen(control_socket, 3) < 0) {
|
2007-05-19 14:13:21 +00:00
|
|
|
logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
|
2007-05-18 16:52:34 +00:00
|
|
|
close(control_socket);
|
2007-05-19 14:13:21 +00:00
|
|
|
return false;
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 14:13:21 +00:00
|
|
|
control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free);
|
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
|
|
|
|
event_add(&control_event, NULL);
|
2007-05-19 14:13:21 +00:00
|
|
|
|
|
|
|
return true;
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit_control() {
|
2007-05-19 14:13:21 +00:00
|
|
|
event_del(&control_event);
|
|
|
|
close(control_socket);
|
|
|
|
unlink(controlsocketname);
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|