2 control.c -- Control socket handling.
3 Copyright (C) 2013 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"
36 char controlcookie[65];
38 static bool control_return(connection_t *c, int type, int error) {
39 return send_request(c, "%d %d %d", CONTROL, type, error);
42 static bool control_ok(connection_t *c, int type) {
43 return control_return(c, type, 0);
46 bool control_h(connection_t *c, const char *request) {
49 if(!c->status.control || c->allow_request != CONTROL) {
50 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized control request from %s (%s)", c->name, c->hostname);
54 if(sscanf(request, "%*d %d", &type) != 1) {
55 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CONTROL", c->name, c->hostname);
62 return control_ok(c, REQ_STOP);
70 case REQ_DUMP_CONNECTIONS:
71 return dump_connections(c);
75 return control_ok(c, REQ_PURGE);
79 if(sscanf(request, "%*d %*d %d", &new_level) != 1)
81 send_request(c, "%d %d %d", CONTROL, REQ_SET_DEBUG, debug_level);
83 debug_level = new_level;
89 return control_ok(c, REQ_RETRY);
92 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got '%s' command", "reload");
93 int result = reload_configuration();
94 return control_return(c, REQ_RELOAD, result);
96 case REQ_DISCONNECT: {
97 char name[MAX_STRING_SIZE];
100 if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
101 return control_return(c, REQ_DISCONNECT, -1);
103 for list_each(connection_t, other, connection_list) {
104 if(strcmp(other->name, name))
106 terminate_connection(other, other->status.active);
110 return control_return(c, REQ_DISCONNECT, found ? 0 : -2);
113 case REQ_DUMP_TRAFFIC:
114 return dump_traffic(c);
117 sscanf(request, "%*d %*d %d", &c->outmaclength);
118 c->status.pcap = true;
123 sscanf(request, "%*d %*d %d", &c->outcompression);
124 c->status.log = true;
129 return send_request(c, "%d %d", CONTROL, REQ_INVALID);
133 bool init_control(void) {
134 randomize(controlcookie, sizeof controlcookie / 2);
135 bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
137 mode_t mask = umask(0);
139 FILE *f = fopen(pidfilename, "w");
143 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno));
147 // Get the address and port of the first listening socket
149 char *localhost = NULL;
151 socklen_t len = sizeof sa;
153 // Make sure we have a valid address, and map 0.0.0.0 and :: to 127.0.0.1 and ::1.
155 if(getsockname(listen_socket[0].tcp.fd, (struct sockaddr *)&sa, &len)) {
156 xasprintf(&localhost, "127.0.0.1 port %s", myport);
158 if(sa.sa.sa_family == AF_INET) {
159 if(sa.in.sin_addr.s_addr == 0)
160 sa.in.sin_addr.s_addr = htonl(0x7f000001);
161 } else if(sa.sa.sa_family == AF_INET6) {
162 static const uint8_t zero[16] = {0};
163 if(!memcmp(sa.in6.sin6_addr.s6_addr, zero, sizeof zero))
164 sa.in6.sin6_addr.s6_addr[15] = 1;
167 localhost = sockaddr2hostname(&sa);
170 fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, localhost);
176 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
178 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno));
182 struct sockaddr_un sa_un;
183 sa_un.sun_family = AF_UNIX;
184 strncpy(sa_un.sun_path, unixsocketname, sizeof sa_un.sun_path);
186 if(connect(unix_fd, (struct sockaddr *)&sa_un, sizeof sa_un) >= 0) {
187 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
191 unlink(unixsocketname);
194 int result = bind(unix_fd, (struct sockaddr *)&sa_un, sizeof sa_un);
198 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
202 if(listen(unix_fd, 3) < 0) {
203 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno));
207 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
213 void exit_control(void) {
215 unlink(unixsocketname);
216 io_del(&unix_socket);
217 close(unix_socket.fd);