]> git.meshlink.io Git - meshlink/blob - src/protocol.c
Never automatically try to bind to ports >= 32768.
[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[NUM_REQUESTS])(meshlink_handle_t *, connection_t *, const char *) = {
35         [ID] = id_h,
36         [ACK] = ack_h,
37         [STATUS] = status_h,
38         [ERROR] = error_h,
39         [TERMREQ] = termreq_h,
40         [PING] = ping_h,
41         [PONG] = pong_h,
42         [ADD_EDGE] = add_edge_h,
43         [DEL_EDGE] = del_edge_h,
44         [KEY_CHANGED] = key_changed_h,
45         [REQ_KEY] = req_key_h,
46         [ANS_KEY] = ans_key_h,
47         [PACKET] = raw_packet_h,
48 };
49
50 /* Request names */
51
52 static const char *request_name[NUM_REQUESTS] = {
53         [ID] = "ID",
54         [ACK] = "ACK",
55         [STATUS] = "STATUS",
56         [ERROR] = "ERROR",
57         [TERMREQ] = "TERMREQ",
58         [PING] = "PING",
59         [PONG] = "PONG",
60         [ADD_EDGE] = "ADD_EDGE",
61         [DEL_EDGE] = "DEL_EDGE",
62         [KEY_CHANGED] = "KEY_CHANGED",
63         [REQ_KEY] = "REQ_KEY",
64         [ANS_KEY] = "ANS_KEY",
65         [PACKET] = "PACKET",
66 };
67
68 bool check_id(const char *id) {
69         if(!id || !*id) {
70                 return false;
71         }
72
73         for(; *id; id++)
74                 if(!isalnum(*id) && *id != '_' && *id != '-') {
75                         return false;
76                 }
77
78         return true;
79 }
80
81 /* Generic request routines - takes care of logging and error
82    detection as well */
83
84 bool send_request(meshlink_handle_t *mesh, connection_t *c, const submesh_t *s, const char *format, ...) {
85         assert(format);
86         assert(*format);
87
88         if(!c) {
89                 logger(mesh, MESHLINK_ERROR, "Trying to send request to non-existing connection");
90                 return false;
91         }
92
93         va_list args;
94         char request[MAXBUFSIZE];
95         int len;
96
97         /* Use vsnprintf instead of vxasprintf: faster, no memory
98            fragmentation, cleanup is automatic, and there is a limit on the
99            input buffer anyway */
100
101         va_start(args, format);
102         len = vsnprintf(request, MAXBUFSIZE, format, args);
103         va_end(args);
104
105         if(len < 0 || len > MAXBUFSIZE - 1) {
106                 logger(mesh, MESHLINK_ERROR, "Output buffer overflow while sending request to %s", c->name);
107                 return false;
108         }
109
110         logger(mesh, MESHLINK_DEBUG, "Sending %s to %s: %s", request_name[atoi(request)], c->name, request);
111
112         request[len++] = '\n';
113
114         if(c == mesh->everyone) {
115
116                 if(s) {
117                         broadcast_submesh_meta(mesh, NULL, s, request, len);
118                 } else {
119                         broadcast_meta(mesh, NULL, request, len);
120                 }
121
122                 return true;
123         } else {
124                 return send_meta(mesh, c, request, len);
125         }
126 }
127
128 void forward_request(meshlink_handle_t *mesh, connection_t *from, const submesh_t *s, const char *request) {
129         assert(from);
130         assert(request);
131         assert(*request);
132
133         logger(mesh, MESHLINK_DEBUG, "Forwarding %s from %s: %s", request_name[atoi(request)], from->name, request);
134
135         // Create a temporary newline-terminated copy of the request
136         int len = strlen(request);
137         char tmp[len + 1];
138
139         memcpy(tmp, request, len);
140         tmp[len] = '\n';
141
142         if(s) {
143                 broadcast_submesh_meta(mesh, from, s, tmp, sizeof(tmp));
144         } else {
145                 broadcast_meta(mesh, from, tmp, sizeof(tmp));
146         }
147 }
148
149 bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) {
150         assert(request);
151
152         int reqno = atoi(request);
153
154         if(reqno || *request == '0') {
155                 if((reqno < 0) || (reqno >= NUM_REQUESTS) || !request_handlers[reqno]) {
156                         logger(mesh, MESHLINK_DEBUG, "Unknown request from %s: %s", c->name, request);
157                         return false;
158                 } else {
159                         logger(mesh, MESHLINK_DEBUG, "Got %s from %s: %s", request_name[reqno], c->name, request);
160                 }
161
162                 if((c->allow_request != ALL) && (c->allow_request != reqno) && (reqno != ERROR)) {
163                         logger(mesh, MESHLINK_ERROR, "Unauthorized request from %s", c->name);
164                         return false;
165                 }
166
167                 if(!request_handlers[reqno](mesh, c, request)) {
168                         /* Something went wrong. Probably scriptkiddies. Terminate. */
169
170                         logger(mesh, MESHLINK_ERROR, "Error while processing %s from %s", request_name[reqno], c->name);
171                         return false;
172                 }
173         } else {
174                 logger(mesh, MESHLINK_ERROR, "Bogus data received from %s", c->name);
175                 return false;
176         }
177
178         return true;
179 }
180
181 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
182         return strcmp(a->request, b->request);
183 }
184
185 static void free_past_request(past_request_t *r) {
186         if(r->request) {
187                 free((void *)r->request);
188         }
189
190         free(r);
191 }
192
193 static const int request_timeout = 60;
194
195 static void age_past_requests(event_loop_t *loop, void *data) {
196         (void)data;
197         meshlink_handle_t *mesh = loop->data;
198         int left = 0, deleted = 0;
199
200         for splay_each(past_request_t, p, mesh->past_request_tree) {
201                 if(p->firstseen + request_timeout <= mesh->loop.now.tv_sec) {
202                         splay_delete_node(mesh->past_request_tree, splay_node), deleted++;
203                 } else {
204                         left++;
205                 }
206         }
207
208         if(left || deleted) {
209                 logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
210         }
211
212         if(left) {
213                 timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) {
214                         10, prng(mesh, TIMER_FUDGE)
215                 });
216         }
217 }
218
219 bool seen_request(meshlink_handle_t *mesh, const char *request) {
220         assert(request);
221         assert(*request);
222
223         past_request_t *new, p = {.request = request};
224
225         if(splay_search(mesh->past_request_tree, &p)) {
226                 logger(mesh, MESHLINK_DEBUG, "Already seen request");
227                 return true;
228         } else {
229                 new = xmalloc(sizeof(*new));
230                 new->request = xstrdup(request);
231                 new->firstseen = mesh->loop.now.tv_sec;
232
233                 if(!mesh->past_request_tree->head && mesh->past_request_timeout.cb) {
234                         timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) {
235                                 10, prng(mesh, TIMER_FUDGE)
236                         });
237                 }
238
239                 splay_insert(mesh->past_request_tree, new);
240                 return false;
241         }
242 }
243
244 void init_requests(meshlink_handle_t *mesh) {
245         assert(!mesh->past_request_tree);
246
247         mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
248         timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timespec) {
249                 0, 0
250         });
251 }
252
253 void exit_requests(meshlink_handle_t *mesh) {
254         if(mesh->past_request_tree) {
255                 splay_delete_tree(mesh->past_request_tree);
256         }
257
258         mesh->past_request_tree = NULL;
259
260         timeout_del(&mesh->loop, &mesh->past_request_timeout);
261 }