]> git.meshlink.io Git - meshlink/blob - src/protocol.c
Remove everything GPL that is not copyright Guus Sliepen, update copyright statements.
[meshlink] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "connection.h"
24 #include "logger.h"
25 #include "meta.h"
26 #include "protocol.h"
27 #include "utils.h"
28 #include "xalloc.h"
29
30 bool experimental = true;
31
32 /* Jumptable for the request handlers */
33
34 static bool (*request_handlers[])(connection_t *, const char *) = {
35                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
36                 status_h, error_h, termreq_h,
37                 ping_h, pong_h,
38                 NULL, NULL, //add_subnet_h, del_subnet_h,
39                 add_edge_h, del_edge_h,
40                 key_changed_h, req_key_h, ans_key_h, tcppacket_h, NULL, //control_h,
41 };
42
43 /* Request names */
44
45 static char (*request_name[]) = {
46                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
47                 "STATUS", "ERROR", "TERMREQ",
48                 "PING", "PONG",
49                 "ADD_SUBNET", "DEL_SUBNET",
50                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
51 };
52
53 static splay_tree_t *past_request_tree;
54
55 bool check_id(const char *id) {
56         if(!id || !*id)
57                 return false;
58
59         for(; *id; id++)
60                 if(!isalnum(*id) && *id != '_')
61                         return false;
62
63         return true;
64 }
65
66 /* Generic request routines - takes care of logging and error
67    detection as well */
68
69 bool send_request(connection_t *c, const char *format, ...) {
70         va_list args;
71         char request[MAXBUFSIZE];
72         int len;
73
74         /* Use vsnprintf instead of vxasprintf: faster, no memory
75            fragmentation, cleanup is automatic, and there is a limit on the
76            input buffer anyway */
77
78         va_start(args, format);
79         len = vsnprintf(request, MAXBUFSIZE, format, args);
80         va_end(args);
81
82         if(len < 0 || len > MAXBUFSIZE - 1) {
83                 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
84                            c->name, c->hostname);
85                 return false;
86         }
87
88         logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[atoi(request)], c->name, c->hostname, request);
89
90         request[len++] = '\n';
91
92         if(c == everyone) {
93                 broadcast_meta(NULL, request, len);
94                 return true;
95         } else
96                 return send_meta(c, request, len);
97 }
98
99 void forward_request(connection_t *from, const char *request) {
100         logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
101
102         // Create a temporary newline-terminated copy of the request
103         int len = strlen(request);
104         char tmp[len + 1];
105         memcpy(tmp, request, len);
106         tmp[len] = '\n';
107         broadcast_meta(from, tmp, sizeof tmp);
108 }
109
110 bool receive_request(connection_t *c, const char *request) {
111         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
112                 if(!request[0] || request[0] == '\r')
113                         return true;
114                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
115                         if(!strncmp(request + 9, "200", 3)) {
116                                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
117                                 return true;
118                         } else {
119                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
120                                 return false;
121                         }
122                 }
123         }
124
125         int reqno = atoi(request);
126
127         if(reqno || *request == '0') {
128                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
129                         logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
130                         return false;
131                 } else {
132                         logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", request_name[reqno], c->name, c->hostname, request);
133                 }
134
135                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
136                         logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
137                         return false;
138                 }
139
140                 if(!request_handlers[reqno](c, request)) {
141                         /* Something went wrong. Probably scriptkiddies. Terminate. */
142
143                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
144                         return false;
145                 }
146         } else {
147                 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
148                 return false;
149         }
150
151         return true;
152 }
153
154 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
155         return strcmp(a->request, b->request);
156 }
157
158 static void free_past_request(past_request_t *r) {
159         if(r->request)
160                 free((char *)r->request);
161
162         free(r);
163 }
164
165 static timeout_t past_request_timeout;
166
167 static void age_past_requests(void *data) {
168         int left = 0, deleted = 0;
169
170         for splay_each(past_request_t, p, past_request_tree) {
171                 if(p->firstseen + pinginterval <= now.tv_sec)
172                         splay_delete_node(past_request_tree, node), deleted++;
173                 else
174                         left++;
175         }
176
177         if(left || deleted)
178                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
179
180         if(left)
181                 timeout_set(&past_request_timeout, &(struct timeval){10, rand() % 100000});
182 }
183
184 bool seen_request(const char *request) {
185         past_request_t *new, p = {NULL};
186
187         p.request = request;
188
189         if(splay_search(past_request_tree, &p)) {
190                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
191                 return true;
192         } else {
193                 new = xmalloc(sizeof *new);
194                 new->request = xstrdup(request);
195                 new->firstseen = now.tv_sec;
196                 splay_insert(past_request_tree, new);
197                 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000});
198                 return false;
199         }
200 }
201
202 void init_requests(void) {
203         past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
204 }
205
206 void exit_requests(void) {
207         splay_delete_tree(past_request_tree);
208
209         timeout_del(&past_request_timeout);
210 }