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) {
40 inlen = read(fd, buf, sizeof buf);
45 p = memchr(buf, '\n', sizeof buf);
46 if(!p || p - buf + 1 != inlen)
51 if(!strcasecmp(buf, "stop")) {
52 logger(LOG_NOTICE, _("Got stop command"));
58 logger(LOG_DEBUG, _("Malformed control command received"));
61 logger(LOG_DEBUG, _("Closing control socket"));
63 splay_delete(control_socket_tree, event);
67 static void handle_new_control_socket(int fd, short events, void *data) {
71 newfd = accept(fd, NULL, NULL);
74 logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
75 event_del(&control_event);
79 ev = xmalloc(sizeof *ev);
80 event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev);
82 splay_insert(control_socket_tree, ev);
84 logger(LOG_DEBUG, _("Control socket connection accepted"));
87 static int control_compare(const struct event *a, const struct event *b) {
88 return a < b ? -1 : a > b ? 1 : 0;
93 struct sockaddr_un addr;
95 if(strlen(controlsocketname) >= sizeof addr.sun_path) {
96 logger(LOG_ERR, _("Control socket filename too long!"));
100 memset(&addr, 0, sizeof addr);
101 addr.sun_family = AF_UNIX;
102 strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
104 control_socket = socket(PF_UNIX, SOCK_STREAM, 0);
106 if(control_socket < 0) {
107 logger(LOG_ERR, _("Creating UNIX socket failed: %s"), strerror(errno));
111 //unlink(controlsocketname);
112 result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
114 if(result < 0 && errno == EADDRINUSE) {
115 result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
117 logger(LOG_WARNING, _("Removing old control socket."));
118 unlink(controlsocketname);
119 result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
121 close(control_socket);
123 logger(LOG_ERR, _("Another tincd is already running for net `%s'."), netname);
125 logger(LOG_ERR, _("Another tincd is already running."));
131 logger(LOG_ERR, _("Can't bind to %s: %s\n"), controlsocketname, strerror(errno));
132 close(control_socket);
136 if(listen(control_socket, 3) < 0) {
137 logger(LOG_ERR, _("Can't listen on %s: %s\n"), controlsocketname, strerror(errno));
138 close(control_socket);
142 control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free);
144 event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL);
145 event_add(&control_event, NULL);
150 void exit_control() {
151 event_del(&control_event);
152 close(control_socket);
153 unlink(controlsocketname);