-static int control_socket = -1;
-static struct event control_event;
-static splay_tree_t *control_socket_tree;
-extern char *controlsocketname;
-
-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;
-
- 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(EVBUFFER_LENGTH(event->input) < req.length)
- return;
-
- if(req.length < sizeof(tinc_ctl_request_t))
- goto failure;
-
- 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(req.type == REQ_STOP) {
- logger(LOG_NOTICE, _("Got stop command"));
- event_loopexit(NULL);
- goto respond;
- }
-
- 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;