]> git.meshlink.io Git - meshlink/blob - src/protocol.c
Fix compiling with -Wall -W.
[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
31 /* Jumptable for the request handlers */
32
33 static bool (*request_handlers[])(meshlink_handle_t *, connection_t *, const char *) = {
34         id_h, NULL, NULL, NULL /* metakey_h, challenge_h, chal_reply_h */, ack_h,
35         status_h, error_h, termreq_h,
36         ping_h, pong_h,
37         NULL, NULL, //add_subnet_h, del_subnet_h,
38         add_edge_h, del_edge_h,
39         key_changed_h, req_key_h, ans_key_h, tcppacket_h, NULL, //control_h,
40 };
41
42 /* Request names */
43
44 static char (*request_name[]) = {
45         "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
46         "STATUS", "ERROR", "TERMREQ",
47         "PING", "PONG",
48         "ADD_SUBNET", "DEL_SUBNET",
49         "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
50 };
51
52 bool check_id(const char *id) {
53         if(!id || !*id)
54                 return false;
55
56         for(; *id; id++)
57                 if(!isalnum(*id) && *id != '_' && *id != '-')
58                         return false;
59
60         return true;
61 }
62
63 /* Generic request routines - takes care of logging and error
64    detection as well */
65
66 bool send_request(meshlink_handle_t *mesh, connection_t *c, const char *format, ...) {
67         va_list args;
68         char request[MAXBUFSIZE];
69         int len;
70
71         /* Use vsnprintf instead of vxasprintf: faster, no memory
72            fragmentation, cleanup is automatic, and there is a limit on the
73            input buffer anyway */
74
75         va_start(args, format);
76         len = vsnprintf(request, MAXBUFSIZE, format, args);
77         va_end(args);
78
79         if(len < 0 || len > MAXBUFSIZE - 1) {
80                 logger(mesh, MESHLINK_ERROR, "Output buffer overflow while sending request to %s", c->name);
81                 return false;
82         }
83
84         logger(mesh, MESHLINK_DEBUG, "Sending %s to %s: %s", request_name[atoi(request)], c->name, request);
85
86         request[len++] = '\n';
87
88         if(c == mesh->everyone) {
89                 broadcast_meta(mesh, NULL, request, len);
90                 return true;
91         } else
92                 return send_meta(mesh, c, request, len);
93 }
94
95 void forward_request(meshlink_handle_t *mesh, connection_t *from, const char *request) {
96         logger(mesh, MESHLINK_DEBUG, "Forwarding %s from %s: %s", request_name[atoi(request)], from->name, request);
97
98         // Create a temporary newline-terminated copy of the request
99         int len = strlen(request);
100         char tmp[len + 1];
101         memcpy(tmp, request, len);
102         tmp[len] = '\n';
103         broadcast_meta(mesh, from, tmp, sizeof(tmp));
104 }
105
106 bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) {
107         if(c->outgoing && mesh->proxytype == PROXY_HTTP && c->allow_request == ID) {
108                 if(!request[0] || request[0] == '\r')
109                         return true;
110                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
111                         if(!strncmp(request + 9, "200", 3)) {
112                                 logger(mesh, MESHLINK_DEBUG, "Proxy request granted");
113                                 return true;
114                         } else {
115                                 logger(mesh, MESHLINK_DEBUG, "Proxy request rejected: %s", request + 9);
116                                 return false;
117                         }
118                 }
119         }
120
121         int reqno = atoi(request);
122
123         if(reqno || *request == '0') {
124                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
125                         logger(mesh, MESHLINK_DEBUG, "Unknown request from %s: %s", c->name, request);
126                         return false;
127                 } else
128                         logger(mesh, MESHLINK_DEBUG, "Got %s from %s: %s", request_name[reqno], c->name, request);
129
130                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
131                         logger(mesh, MESHLINK_ERROR, "Unauthorized request from %s", c->name);
132                         return false;
133                 }
134
135                 if(!request_handlers[reqno](mesh, c, request)) {
136                         /* Something went wrong. Probably scriptkiddies. Terminate. */
137
138                         logger(mesh, MESHLINK_ERROR, "Error while processing %s from %s", request_name[reqno], c->name);
139                         return false;
140                 }
141         } else {
142                 logger(mesh, MESHLINK_ERROR, "Bogus data received from %s", c->name);
143                 return false;
144         }
145
146         return true;
147 }
148
149 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
150         return strcmp(a->request, b->request);
151 }
152
153 static void free_past_request(past_request_t *r) {
154         if(r->request)
155                 free((void *)r->request);
156
157         free(r);
158 }
159
160 static void age_past_requests(event_loop_t *loop, void *data) {
161         (void)data;
162         meshlink_handle_t *mesh = loop->data;
163         int left = 0, deleted = 0;
164
165         for splay_each(past_request_t, p, mesh->past_request_tree) {
166                 if(p->firstseen + mesh->pinginterval <= mesh->loop.now.tv_sec)
167                         splay_delete_node(mesh->past_request_tree, node), deleted++;
168                 else
169                         left++;
170         }
171
172         if(left || deleted)
173                 logger(mesh, MESHLINK_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
174
175         if(left)
176                 timeout_set(&mesh->loop, &mesh->past_request_timeout, &(struct timeval) {
177                 10, rand() % 100000
178         });
179 }
180
181 bool seen_request(meshlink_handle_t *mesh, const char *request) {
182         past_request_t *new, p = {};
183
184         p.request = request;
185
186         if(splay_search(mesh->past_request_tree, &p)) {
187                 logger(mesh, MESHLINK_DEBUG, "Already seen request");
188                 return true;
189         } else {
190                 new = xmalloc(sizeof(*new));
191                 new->request = xstrdup(request);
192                 new->firstseen = mesh->loop.now.tv_sec;
193                 splay_insert(mesh->past_request_tree, new);
194                 timeout_add(&mesh->loop, &mesh->past_request_timeout, age_past_requests, NULL, &(struct timeval) {
195                         10, rand() % 100000
196                 });
197                 return false;
198         }
199 }
200
201 void init_requests(meshlink_handle_t *mesh) {
202         mesh->past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
203 }
204
205 void exit_requests(meshlink_handle_t *mesh) {
206         if(mesh->past_request_tree)
207                 splay_delete_tree(mesh->past_request_tree);
208         mesh->past_request_tree = NULL;
209
210         timeout_del(&mesh->loop, &mesh->past_request_timeout);
211 }