2 control.c -- Control socket handling.
3 Copyright (C) 2012 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 along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "control_common.h"
32 #include "splay_tree.h"
36 char controlcookie[65];
37 extern char *pidfilename;
39 static bool control_return(connection_t *c, int type, int error) {
40 return send_request(c, "%d %d %d", CONTROL, type, error);
43 static bool control_ok(connection_t *c, int type) {
44 return control_return(c, type, 0);
47 bool control_h(connection_t *c, const char *request) {
50 if(!c->status.control || c->allow_request != CONTROL) {
51 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
55 if(sscanf(request, "%*d %d", &type) != 1) {
56 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
63 return control_ok(c, REQ_STOP);
71 case REQ_DUMP_SUBNETS:
72 return dump_subnets(c);
74 case REQ_DUMP_CONNECTIONS:
75 return dump_connections(c);
79 return control_ok(c, REQ_PURGE);
83 if(sscanf(request, "%*d %*d %d", &new_level) != 1)
85 send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
87 debug_level = new_level;
93 return control_ok(c, REQ_RETRY);
96 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got '%s' command", "reload");
97 int result = reload_configuration();
98 return control_return(c, REQ_RELOAD, result);
100 case REQ_DISCONNECT: {
101 char name[MAX_STRING_SIZE];
103 splay_node_t *node, *next;
106 if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
107 return control_return(c, REQ_DISCONNECT, -1);
109 for(node = connection_tree->head; node; node = next) {
112 if(strcmp(other->name, name))
114 terminate_connection(other, other->status.active);
118 return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
121 case REQ_DUMP_TRAFFIC:
122 return dump_traffic(c);
125 sscanf(request, "%*d %*d %d", &c->outmaclength);
126 c->status.pcap = true;
131 sscanf(request, "%*d %*d %d", &c->outcompression);
132 c->status.log = true;
137 return send_request(c, "%d %d", CONTROL, REQ_INVALID);
141 bool init_control(void) {
142 randomize(controlcookie, sizeof controlcookie / 2);
143 bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
145 FILE *f = fopen(pidfilename, "w");
147 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno));
152 fchmod(fileno(f), 0600);
154 chmod(pidfilename, 0600);
156 // Get the address and port of the first listening socket
158 char *localhost = NULL;
160 socklen_t len = sizeof sa;
162 // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1.
164 if(getsockname(listen_socket[0].tcp, (struct sockaddr *)&sa, &len)) {
165 xasprintf(&localhost, "127.0.0.1 port %d", myport);
167 if(sa.sa.sa_family == AF_INET) {
168 if(sa.in.sin_addr.s_addr == 0)
169 sa.in.sin_addr.s_addr = htonl(0x7f000001);
170 } else if(sa.sa.sa_family == AF_INET6) {
171 static const uint8_t zero[16] = {0};
172 if(!memcmp(sa.in6.sin6_addr.s6_addr, zero, sizeof zero))
173 sa.in6.sin6_addr.s6_addr[15] = 1;
176 localhost = sockaddr2hostname(&sa);
179 fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, localhost);
187 void exit_control(void) {