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