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.
30 static int control_socket = -1;
31 static struct event control_event;
32 static splay_tree_t *control_socket_tree;
33 extern char *controlsocketname;
35 static void handle_control_data(int fd, short events, void *event) {
39 inlen = read(fd, buf, sizeof buf);
42 logger(LOG_DEBUG, _("Closing control socket"));
44 splay_delete(control_socket_tree, event);
49 static void handle_new_control_socket(int fd, short events, void *data) {
53 newfd = accept(fd, NULL, NULL);
56 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
57 event_del(&control_event);
61 ev = xmalloc(sizeof *ev);
62 event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev);
64 splay_insert(control_socket_tree, ev);
66 logger(LOG_DEBUG, _("Control socket connection accepted"));
69 static int control_compare(const struct event *a, const struct event *b) {
70 return a < b ? -1 : a > b ? 1 : 0;
75 struct sockaddr_un addr;
77 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
78 logger(LOG_ERR, _("Control socket filename too long!"));
82 memset(&addr, 0, sizeof addr);
83 addr.sun_family = AF_UNIX;
84 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
86 control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
88 if(control_socket < 0) {
89 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
93 //unlink(controlsocketname);
94 result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
96 if(result < 0 && errno == EADDRINUSE) {
97 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
99 logger(LOG_WARNING, _("Removing old control socket."));
100 unlink(controlsocketname);
101 result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
103 close(control_socket);
105 logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
107 logger(LOG_ERR, _("Another tincd is already running."));
113 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
114 close(control_socket);
118 if(listen(control_socket, 3) < 0) {
119 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
120 close(control_socket);
124 control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free);
126 event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
127 event_add(&control_event, NULL);
132 void exit_control() {
133 event_del(&control_event);
134 close(control_socket);
135 unlink(controlsocketname);