X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fcontrol.c;h=a7958437db94394b102e4ebc91df8634316be7c5;hb=b1f8c65a2cfa307d9b8ed8cc3c8d4819f605e4f6;hp=2fa326a07741460a6f82d9fc90ddb7ccda5bc1bd;hpb=8c6131deda546452386f3703af968ee664cadfbd;p=tinc diff --git a/src/control.c b/src/control.c index 2fa326a0..a7958437 100644 --- a/src/control.c +++ b/src/control.c @@ -24,6 +24,7 @@ #include "system.h" #include "conf.h" #include "control.h" +#include "control_common.h" #include "logger.h" #include "xalloc.h" @@ -32,41 +33,146 @@ static struct event control_event; static splay_tree_t *control_socket_tree; extern char *controlsocketname; -static void handle_control_data(int fd, short events, void *event) { - char buf[MAXBUFSIZE]; - size_t inlen; - char *p; +static void handle_control_data(struct bufferevent *event, void *data) { + tinc_ctl_request_t req; + size_t size; + tinc_ctl_request_t res; + struct evbuffer *res_data = NULL; + void *req_data; - inlen = read(fd, buf, sizeof buf); + if(EVBUFFER_LENGTH(event->input) < sizeof(tinc_ctl_request_t)) + return; + + /* Copy the structure to ensure alignment */ + memcpy(&req, EVBUFFER_DATA(event->input), sizeof(tinc_ctl_request_t)); - if(inlen <= 0) - goto close; + if(EVBUFFER_LENGTH(event->input) < req.length) + return; + req_data = EVBUFFER_DATA(event->input) + sizeof(tinc_ctl_request_t); - p = memchr(buf, '\n', sizeof buf); - if(!p || p - buf + 1 != inlen) - goto malformed; + if(req.length < sizeof(tinc_ctl_request_t)) + goto failure; - *p = 0; + memset(&res, 0, sizeof res); + res.type = req.type; + res.id = req.id; + + res_data = evbuffer_new(); + if(res_data == NULL) { + res.res_errno = ENOMEM; + goto respond; + } - if(!strcasecmp(buf, "stop")) { - logger(LOG_NOTICE, _("Got stop command")); + if(req.type == REQ_STOP) { + logger(LOG_NOTICE, _("Got '%s' command"), "stop"); event_loopexit(NULL); - return; + goto respond; + } + + if(req.type == REQ_DUMP_NODES) { + logger(LOG_NOTICE, _("Got '%s' command"), "dump nodes"); + res.res_errno = dump_nodes(res_data); + goto respond; + } + + if(req.type == REQ_DUMP_EDGES) { + logger(LOG_NOTICE, _("Got '%s' command"), "dump edges"); + res.res_errno = dump_edges(res_data); + goto respond; + } + + if(req.type == REQ_DUMP_SUBNETS) { + logger(LOG_NOTICE, _("Got '%s' command"), "dump subnets"); + res.res_errno = dump_subnets(res_data); + goto respond; + } + + if(req.type == REQ_DUMP_CONNECTIONS) { + logger(LOG_NOTICE, _("Got '%s' command"), "dump connections"); + res.res_errno = dump_connections(res_data); + goto respond; + } + + if(req.type == REQ_DUMP_GRAPH) { + logger(LOG_NOTICE, _("Got '%s' command"), "dump graph"); + res.res_errno = dump_graph(res_data); + goto respond; + } + + if(req.type == REQ_PURGE) { + logger(LOG_NOTICE, _("Got '%s' command"), "purge"); + purge(); + goto respond; + } + + if(req.type == REQ_SET_DEBUG) { + debug_t new_debug_level; + + logger(LOG_NOTICE, _("Got '%s' command"), "debug"); + if(req.length != sizeof(req) + sizeof debug_level) + res.res_errno = EINVAL; + else { + memcpy(&new_debug_level, req_data, sizeof(debug_t)); + logger(LOG_NOTICE, _("Changing debug level from %d to %d"), + debug_level, new_debug_level); + if(evbuffer_add_printf(res_data, + _("Changing debug level from %d to %d\n"), + debug_level, new_debug_level) == -1) + res.res_errno = errno; + debug_level = new_debug_level; + } + goto respond; + } + + if(req.type == REQ_RETRY) { + logger(LOG_NOTICE, _("Got '%s' command"), "retry"); + retry(); + goto respond; + } + + if(req.type == REQ_RELOAD) { + logger(LOG_NOTICE, _("Got '%s' command"), "reload"); + res.res_errno = reload_configuration(); + goto respond; } -malformed: logger(LOG_DEBUG, _("Malformed control command received")); + res.res_errno = EINVAL; + +respond: + res.length = (sizeof res) + + ((res_data == NULL) ? 0 : EVBUFFER_LENGTH(res_data)); + evbuffer_drain(event->input, req.length); + if(bufferevent_write(event, &res, sizeof res) == -1) + goto failure; + if(res_data != NULL) { + if(bufferevent_write_buffer(event, res_data) == -1) + goto failure; + evbuffer_free(res_data); + } + return; + +failure: + logger(LOG_INFO, _("Closing control socket on error")); + evbuffer_free(res_data); + close(event->ev_read.ev_fd); + splay_delete(control_socket_tree, event); +} + +static void handle_control_error(struct bufferevent *event, short what, void *data) { + if(what & EVBUFFER_EOF) + logger(LOG_DEBUG, _("Control socket connection closed by peer")); + else + logger(LOG_DEBUG, _("Error while reading from control socket: %s"), strerror(errno)); -close: - logger(LOG_DEBUG, _("Closing control socket")); - event_del(event); + close(event->ev_read.ev_fd); 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; + struct bufferevent *ev; + tinc_ctl_greeting_t greeting; newfd = accept(fd, NULL, NULL); @@ -76,9 +182,25 @@ static void handle_new_control_socket(int fd, short events, void *data) { return; } - ev = xmalloc(sizeof *ev); - event_set(ev, newfd, EV_READ | EV_PERSIST, handle_control_data, ev); - event_add(ev, NULL); + ev = bufferevent_new(newfd, handle_control_data, NULL, handle_control_error, NULL); + if(!ev) { + logger(LOG_ERR, _("Could not create bufferevent for new control connection: %s"), strerror(errno)); + close(newfd); + return; + } + + memset(&greeting, 0, sizeof greeting); + greeting.version = TINC_CTL_VERSION_CURRENT; + if(bufferevent_write(ev, &greeting, sizeof greeting) == -1) { + logger(LOG_ERR, + _("Cannot send greeting for new control connection: %s"), + strerror(errno)); + bufferevent_free(ev); + close(newfd); + return; + } + + bufferevent_enable(ev, EV_READ); splay_insert(control_socket_tree, ev); logger(LOG_DEBUG, _("Control socket connection accepted")); @@ -139,7 +261,7 @@ bool init_control() { return false; } - control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)free); + control_socket_tree = splay_alloc_tree((splay_compare_t)control_compare, (splay_action_t)bufferevent_free); event_set(&control_event, control_socket, EV_READ | EV_PERSIST, handle_new_control_socket, NULL); event_add(&control_event, NULL);