2 protocol.c -- handle the meta-protocol, basic functions
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2015 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "connection.h"
31 bool tunnelserver = false;
32 bool strictsubnets = false;
34 /* Jumptable for the request handlers */
36 static bool (*request_handlers[])(connection_t *) = {
37 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
38 status_h, error_h, termreq_h,
40 add_subnet_h, del_subnet_h,
41 add_edge_h, del_edge_h,
42 key_changed_h, req_key_h, ans_key_h, tcppacket_h,
47 static char (*request_name[]) = {
48 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
49 "STATUS", "ERROR", "TERMREQ",
51 "ADD_SUBNET", "DEL_SUBNET",
52 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET",
55 static avl_tree_t *past_request_tree;
57 bool check_id(const char *id) {
59 if(!isalnum(*id) && *id != '_')
65 /* Generic request routines - takes care of logging and error
68 bool send_request(connection_t *c, const char *format, ...) {
70 char buffer[MAXBUFSIZE];
73 /* Use vsnprintf instead of vxasprintf: faster, no memory
74 fragmentation, cleanup is automatic, and there is a limit on the
75 input buffer anyway */
77 va_start(args, format);
78 len = vsnprintf(buffer, MAXBUFSIZE, format, args);
81 if(len < 0 || len > MAXBUFSIZE - 1) {
82 logger(LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
83 c->name, c->hostname);
88 sscanf(buffer, "%d", &request);
90 logger(LOG_DEBUG, "Sending %s to %s (%s): %s",
91 request_name[request], c->name, c->hostname, buffer);
93 logger(LOG_DEBUG, "Sending %s to %s (%s)", request_name[request],
94 c->name, c->hostname);
100 broadcast_meta(NULL, buffer, len);
103 return send_meta(c, buffer, len);
106 void forward_request(connection_t *from) {
110 sscanf(from->buffer, "%d", &request);
112 logger(LOG_DEBUG, "Forwarding %s from %s (%s): %s",
113 request_name[request], from->name, from->hostname,
116 logger(LOG_DEBUG, "Forwarding %s from %s (%s)",
117 request_name[request], from->name, from->hostname);
120 from->buffer[from->reqlen - 1] = '\n';
122 broadcast_meta(from, from->buffer, from->reqlen);
125 bool receive_request(connection_t *c) {
128 if(sscanf(c->buffer, "%d", &request) == 1) {
129 if((request < 0) || (request >= LAST) || !request_handlers[request]) {
131 logger(LOG_DEBUG, "Unknown request from %s (%s): %s",
132 c->name, c->hostname, c->buffer);
134 logger(LOG_ERR, "Unknown request from %s (%s)",
135 c->name, c->hostname);
141 logger(LOG_DEBUG, "Got %s from %s (%s): %s",
142 request_name[request], c->name, c->hostname,
145 logger(LOG_DEBUG, "Got %s from %s (%s)",
146 request_name[request], c->name, c->hostname);
150 if((c->allow_request != ALL) && (c->allow_request != request)) {
151 logger(LOG_ERR, "Unauthorized request from %s (%s)", c->name,
156 if(!request_handlers[request](c)) {
157 /* Something went wrong. Probably scriptkiddies. Terminate. */
159 logger(LOG_ERR, "Error while processing %s from %s (%s)",
160 request_name[request], c->name, c->hostname);
164 logger(LOG_ERR, "Bogus data received from %s (%s)",
165 c->name, c->hostname);
172 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
173 return strcmp(a->request, b->request);
176 static void free_past_request(past_request_t *r) {
183 void init_requests(void) {
184 past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
187 void exit_requests(void) {
188 avl_delete_tree(past_request_tree);
191 bool seen_request(char *request) {
192 past_request_t *new, p = {NULL};
196 if(avl_search(past_request_tree, &p)) {
197 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Already seen request");
200 new = xmalloc(sizeof(*new));
201 new->request = xstrdup(request);
202 new->firstseen = now;
203 avl_insert(past_request_tree, new);
208 void age_past_requests(void) {
209 avl_node_t *node, *next;
211 int left = 0, deleted = 0;
213 for(node = past_request_tree->head; node; node = next) {
217 if(p->firstseen + pinginterval <= now)
218 avl_delete_node(past_request_tree, node), deleted++;
224 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Aging past requests: deleted %d, left %d",