2 protocol.c -- handle the meta-protocol, basic functions
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include "connection.h"
25 #include "meshlink_internal.h"
32 /* Jumptable for the request handlers */
34 static bool (*request_handlers[NUM_REQUESTS])(meshlink_handle_t *, connection_t *, const char *) = {
39 [TERMREQ] = termreq_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,
51 static const char *request_name[NUM_REQUESTS] = {
56 [TERMREQ] = "TERMREQ",
59 [ADD_EDGE] = "ADD_EDGE",
60 [DEL_EDGE] = "DEL_EDGE",
61 [KEY_CHANGED] = "KEY_CHANGED",
62 [REQ_KEY] = "REQ_KEY",
63 [ANS_KEY] = "ANS_KEY",
66 bool check_id(const char *id) {
72 if(!isalnum(*id) && *id != '_' && *id != '-') {
79 /* Generic request routines - takes care of logging and error
82 bool send_request(meshlink_handle_t *mesh, connection_t *c, const submesh_t *s, const char *format, ...) {
87 logger(mesh, MESHLINK_ERROR, "Trying to send request to non-existing connection");
92 char request[MAXBUFSIZE];
95 /* Use vsnprintf instead of vxasprintf: faster, no memory
96 fragmentation, cleanup is automatic, and there is a limit on the
97 input buffer anyway */
99 va_start(args, format);
100 len = vsnprintf(request, MAXBUFSIZE, format, args);
103 if(len < 0 || len > MAXBUFSIZE - 1) {
104 logger(mesh, MESHLINK_ERROR, "Output buffer overflow while sending request to %s", c->name);
108 logger(mesh, MESHLINK_DEBUG, "Sending %s to %s: %s", request_name[atoi(request)], c->name, request);
110 request[len++] = '\n';
112 if(c == mesh->everyone) {
115 broadcast_submesh_meta(mesh, NULL, s, request, len);
117 broadcast_meta(mesh, NULL, request, len);
122 return send_meta(mesh, c, request, len);
126 void forward_request(meshlink_handle_t *mesh, connection_t *from, const submesh_t *s, const char *request) {
131 logger(mesh, MESHLINK_DEBUG, "Forwarding %s from %s: %s", request_name[atoi(request)], from->name, request);
133 // Create a temporary newline-terminated copy of the request
134 int len = strlen(request);
137 memcpy(tmp, request, len);
141 broadcast_submesh_meta(mesh, from, s, tmp, sizeof(tmp));
143 broadcast_meta(mesh, from, tmp, sizeof(tmp));
147 bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) {
150 int reqno = atoi(request);
152 if(reqno || *request == '0') {
153 if((reqno < 0) || (reqno >= NUM_REQUESTS) || !request_handlers[reqno]) {
154 logger(mesh, MESHLINK_DEBUG, "Unknown request from %s: %s", c->name, request);
157 logger(mesh, MESHLINK_DEBUG, "Got %s from %s: %s", request_name[reqno], c->name, request);
160 if((c->allow_request != ALL) && (c->allow_request != reqno) && (reqno != ERROR)) {
161 logger(mesh, MESHLINK_ERROR, "Unauthorized request from %s", c->name);
165 if(!request_handlers[reqno](mesh, c, request)) {
166 /* Something went wrong. Probably scriptkiddies. Terminate. */
168 logger(mesh, MESHLINK_ERROR, "Error while processing %s from %s", request_name[reqno], c->name);
172 logger(mesh, MESHLINK_ERROR, "Bogus data received from %s", c->name);
179 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
180 return strcmp(a->request, b->request);
183 static void free_past_request(past_request_t *r) {
185 free((void *)r->request);
191 static const int request_timeout = 60;
193 static void age_past_requests(event_loop_t *loop, void *data) {
195 meshlink_handle_t *mesh = loop->data;
196 int left = 0, deleted = 0;
198 for splay_each(past_request_t, p, mesh->past_request_tree) {
199 if(p->firstseen + request_timeout <= mesh->loop.now.tv_sec) {
200 splay_delete_node(mesh->past_request_tree, splay_node), deleted++;
206 if(left || deleted) {
207 logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
211 timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) {
212 10, prng(mesh, TIMER_FUDGE)
217 bool seen_request(meshlink_handle_t *mesh, const char *request) {
221 past_request_t *new, p = {.request = request};
223 if(splay_search(mesh->past_request_tree, &p)) {
224 logger(mesh, MESHLINK_DEBUG, "Already seen request");
227 new = xmalloc(sizeof(*new));
228 new->request = xstrdup(request);
229 new->firstseen = mesh->loop.now.tv_sec;
231 if(!mesh->past_request_tree->head && mesh->past_request_timeout.cb) {
232 timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) {
233 10, prng(mesh, TIMER_FUDGE)
237 splay_insert(mesh->past_request_tree, new);
242 void init_requests(meshlink_handle_t *mesh) {
243 assert(!mesh->past_request_tree);
245 mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
246 timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timespec) {
251 void exit_requests(meshlink_handle_t *mesh) {
252 if(mesh->past_request_tree) {
253 splay_delete_tree(mesh->past_request_tree);
256 mesh->past_request_tree = NULL;
258 timeout_del(&mesh->loop, &mesh->past_request_timeout);