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[])(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,
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,
45 static const char *request_name[] = {
46 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
47 "STATUS", "ERROR", "TERMREQ",
49 "ADD_SUBNET", "DEL_SUBNET",
50 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
53 bool check_id(const char *id) {
59 if(!isalnum(*id) && *id != '_' && *id != '-') {
66 /* Generic request routines - takes care of logging and error
69 bool send_request(meshlink_handle_t *mesh, connection_t *c, const submesh_t *s, const char *format, ...) {
75 char request[MAXBUFSIZE];
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 */
82 va_start(args, format);
83 len = vsnprintf(request, MAXBUFSIZE, format, args);
86 if(len < 0 || len > MAXBUFSIZE - 1) {
87 logger(mesh, MESHLINK_ERROR, "Output buffer overflow while sending request to %s", c->name);
91 logger(mesh, MESHLINK_DEBUG, "Sending %s to %s: %s", request_name[atoi(request)], c->name, request);
93 request[len++] = '\n';
95 if(c == mesh->everyone) {
98 broadcast_submesh_meta(mesh, NULL, s, request, len);
100 broadcast_meta(mesh, NULL, request, len);
105 return send_meta(mesh, c, request, len);
109 void forward_request(meshlink_handle_t *mesh, connection_t *from, const submesh_t *s, const char *request) {
114 logger(mesh, MESHLINK_DEBUG, "Forwarding %s from %s: %s", request_name[atoi(request)], from->name, request);
116 // Create a temporary newline-terminated copy of the request
117 int len = strlen(request);
120 memcpy(tmp, request, len);
124 broadcast_submesh_meta(mesh, from, s, tmp, sizeof(tmp));
126 broadcast_meta(mesh, from, tmp, sizeof(tmp));
130 bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) {
133 int reqno = atoi(request);
135 if(reqno || *request == '0') {
136 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
137 logger(mesh, MESHLINK_DEBUG, "Unknown request from %s: %s", c->name, request);
140 logger(mesh, MESHLINK_DEBUG, "Got %s from %s: %s", request_name[reqno], c->name, request);
143 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
144 logger(mesh, MESHLINK_ERROR, "Unauthorized request from %s", c->name);
148 if(!request_handlers[reqno](mesh, c, request)) {
149 /* Something went wrong. Probably scriptkiddies. Terminate. */
151 logger(mesh, MESHLINK_ERROR, "Error while processing %s from %s", request_name[reqno], c->name);
155 logger(mesh, MESHLINK_ERROR, "Bogus data received from %s", c->name);
162 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
163 return strcmp(a->request, b->request);
166 static void free_past_request(past_request_t *r) {
168 free((void *)r->request);
174 static const int request_timeout = 60;
176 static void age_past_requests(event_loop_t *loop, void *data) {
178 meshlink_handle_t *mesh = loop->data;
179 int left = 0, deleted = 0;
181 for splay_each(past_request_t, p, mesh->past_request_tree) {
182 if(p->firstseen + request_timeout <= mesh->loop.now.tv_sec) {
183 splay_delete_node(mesh->past_request_tree, splay_node), deleted++;
189 if(left || deleted) {
190 logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
194 timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) {
195 10, prng(mesh, TIMER_FUDGE)
200 bool seen_request(meshlink_handle_t *mesh, const char *request) {
204 past_request_t *new, p = {.request = request};
206 if(splay_search(mesh->past_request_tree, &p)) {
207 logger(mesh, MESHLINK_DEBUG, "Already seen request");
210 new = xmalloc(sizeof(*new));
211 new->request = xstrdup(request);
212 new->firstseen = mesh->loop.now.tv_sec;
214 if(!mesh->past_request_tree->head) {
215 timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timespec) {
216 10, prng(mesh, TIMER_FUDGE)
220 splay_insert(mesh->past_request_tree, new);
225 void init_requests(meshlink_handle_t *mesh) {
226 assert(!mesh->past_request_tree);
228 mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
229 timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timespec) {
234 void exit_requests(meshlink_handle_t *mesh) {
235 if(mesh->past_request_tree) {
236 splay_delete_tree(mesh->past_request_tree);
239 mesh->past_request_tree = NULL;
241 timeout_del(&mesh->loop, &mesh->past_request_timeout);