]> git.meshlink.io Git - meshlink/blob - src/protocol.c
Make ping intervals and timeouts configurable for each device class.
[meshlink] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 2014-2017 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 "meshlink_internal.h"
26 #include "meta.h"
27 #include "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30 #include "submesh.h"
31
32 /* Jumptable for the request handlers */
33
34 static bool (*request_handlers[])(meshlink_handle_t *, connection_t *, const char *) = {
35         id_h, NULL, NULL, NULL /* 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 bool check_id(const char *id) {
54         if(!id || !*id) {
55                 return false;
56         }
57
58         for(; *id; id++)
59                 if(!isalnum(*id) && *id != '_' && *id != '-') {
60                         return false;
61                 }
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(meshlink_handle_t *mesh, connection_t *c, submesh_t *s, const char *format, ...) {
70         va_list args;
71         char request[MAXBUFSIZE];
72         int len;
73
74         if(!c) {
75                 return false;
76         }
77
78         /* Use vsnprintf instead of vxasprintf: faster, no memory
79            fragmentation, cleanup is automatic, and there is a limit on the
80            input buffer anyway */
81
82         va_start(args, format);
83         len = vsnprintf(request, MAXBUFSIZE, format, args);
84         va_end(args);
85
86         if(len < 0 || len > MAXBUFSIZE - 1) {
87                 logger(mesh, MESHLINK_ERROR, "Output buffer overflow while sending request to %s", c->name);
88                 return false;
89         }
90
91         logger(mesh, MESHLINK_DEBUG, "Sending %s to %s: %s", request_name[atoi(request)], c->name, request);
92
93         request[len++] = '\n';
94
95         if(c == mesh->everyone) {
96
97                 if(s) {
98                         broadcast_submesh_meta(mesh, NULL, s, request, len);
99                 } else {
100                         broadcast_meta(mesh, NULL, request, len);
101                 }
102
103                 return true;
104         } else {
105                 return send_meta(mesh, c, request, len);
106         }
107 }
108
109 void forward_request(meshlink_handle_t *mesh, connection_t *from, submesh_t *s, const char *request) {
110         logger(mesh, MESHLINK_DEBUG, "Forwarding %s from %s: %s", request_name[atoi(request)], from->name, request);
111
112         // Create a temporary newline-terminated copy of the request
113         int len = strlen(request);
114         char tmp[len + 1];
115
116         memcpy(tmp, request, len);
117         tmp[len] = '\n';
118
119         if(s) {
120                 broadcast_submesh_meta(mesh, from, s, tmp, sizeof(tmp));
121         } else {
122                 broadcast_meta(mesh, from, tmp, sizeof(tmp));
123         }
124 }
125
126 bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) {
127         if(c->outgoing && mesh->proxytype == PROXY_HTTP && c->allow_request == ID) {
128                 if(!request[0] || request[0] == '\r') {
129                         return true;
130                 }
131
132                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
133                         if(!strncmp(request + 9, "200", 3)) {
134                                 logger(mesh, MESHLINK_DEBUG, "Proxy request granted");
135                                 return true;
136                         } else {
137                                 logger(mesh, MESHLINK_DEBUG, "Proxy request rejected: %s", request + 9);
138                                 return false;
139                         }
140                 }
141         }
142
143         int reqno = atoi(request);
144
145         if(reqno || *request == '0') {
146                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
147                         logger(mesh, MESHLINK_DEBUG, "Unknown request from %s: %s", c->name, request);
148                         return false;
149                 } else {
150                         logger(mesh, MESHLINK_DEBUG, "Got %s from %s: %s", request_name[reqno], c->name, request);
151                 }
152
153                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
154                         logger(mesh, MESHLINK_ERROR, "Unauthorized request from %s", c->name);
155                         return false;
156                 }
157
158                 if(!request_handlers[reqno](mesh, c, request)) {
159                         /* Something went wrong. Probably scriptkiddies. Terminate. */
160
161                         logger(mesh, MESHLINK_ERROR, "Error while processing %s from %s", request_name[reqno], c->name);
162                         return false;
163                 }
164         } else {
165                 logger(mesh, MESHLINK_ERROR, "Bogus data received from %s", c->name);
166                 return false;
167         }
168
169         return true;
170 }
171
172 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
173         return strcmp(a->request, b->request);
174 }
175
176 static void free_past_request(past_request_t *r) {
177         if(r->request) {
178                 free((void *)r->request);
179         }
180
181         free(r);
182 }
183
184 static const int request_timeout = 60;
185
186 static void age_past_requests(event_loop_t *loop, void *data) {
187         (void)data;
188         meshlink_handle_t *mesh = loop->data;
189         int left = 0, deleted = 0;
190
191         for splay_each(past_request_t, p, mesh->past_request_tree) {
192                 if(p->firstseen + request_timeout <= mesh->loop.now.tv_sec) {
193                         splay_delete_node(mesh->past_request_tree, node), deleted++;
194                 } else {
195                         left++;
196                 }
197         }
198
199         if(left || deleted) {
200                 logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
201         }
202
203         if(left)
204                 timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval) {
205                 10, rand() % 100000
206         });
207 }
208
209 bool seen_request(meshlink_handle_t *mesh, const char *request) {
210         past_request_t *new, p = {.request = request};
211
212         if(splay_search(mesh->past_request_tree, &p)) {
213                 logger(mesh, MESHLINK_DEBUG, "Already seen request");
214                 return true;
215         } else {
216                 new = xmalloc(sizeof(*new));
217                 new->request = xstrdup(request);
218                 new->firstseen = mesh->loop.now.tv_sec;
219                 splay_insert(mesh->past_request_tree, new);
220                 timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timeval) {
221                         10, rand() % 100000
222                 });
223                 return false;
224         }
225 }
226
227 void init_requests(meshlink_handle_t *mesh) {
228         mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
229 }
230
231 void exit_requests(meshlink_handle_t *mesh) {
232         if(mesh->past_request_tree) {
233                 splay_delete_tree(mesh->past_request_tree);
234         }
235
236         mesh->past_request_tree = NULL;
237
238         timeout_del(&mesh->loop, &mesh->past_request_timeout);
239 }