2 protocol.c -- handle the meta-protocol, basic functions
3 Copyright (C) 2014 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"
31 /* Jumptable for the request handlers */
33 static bool (*request_handlers[])(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,
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,
44 static char (*request_name[]) = {
45 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
46 "STATUS", "ERROR", "TERMREQ",
48 "ADD_SUBNET", "DEL_SUBNET",
49 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
52 static splay_tree_t *past_request_tree;
54 bool check_id(const char *id) {
59 if(!isalnum(*id) && *id != '_')
65 /* Generic request routines - takes care of logging and error
68 bool send_request(connection_t *c, const char *format, ...) {
70 char request[MAXBUFSIZE];
73 /* Use vsnprintf instead of vxasprintf: faster, no memory
74 fragmentation, cleanup is automatic, and there is a limit on the
75 input buffer anyway */
77 va_start(args, format);
78 len = vsnprintf(request, MAXBUFSIZE, format, args);
81 if(len < 0 || len > MAXBUFSIZE - 1) {
82 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
83 c->name, c->hostname);
87 logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[atoi(request)], c->name, c->hostname, request);
89 request[len++] = '\n';
91 if(c == mesh->everyone) {
92 broadcast_meta(NULL, request, len);
95 return send_meta(c, request, len);
98 void forward_request(connection_t *from, const char *request) {
99 logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
101 // Create a temporary newline-terminated copy of the request
102 int len = strlen(request);
104 memcpy(tmp, request, len);
106 broadcast_meta(from, tmp, sizeof tmp);
109 bool receive_request(connection_t *c, const char *request) {
110 if(c->outgoing && mesh->proxytype == PROXY_HTTP && c->allow_request == ID) {
111 if(!request[0] || request[0] == '\r')
113 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
114 if(!strncmp(request + 9, "200", 3)) {
115 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
118 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
124 int reqno = atoi(request);
126 if(reqno || *request == '0') {
127 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
128 logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
131 logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", request_name[reqno], c->name, c->hostname, request);
134 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
135 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
139 if(!request_handlers[reqno](c, request)) {
140 /* Something went wrong. Probably scriptkiddies. Terminate. */
142 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
146 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
153 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
154 return strcmp(a->request, b->request);
157 static void free_past_request(past_request_t *r) {
159 free((char *)r->request);
164 static timeout_t past_request_timeout;
166 static void age_past_requests(event_loop_t *loop, void *data) {
167 int left = 0, deleted = 0;
169 for splay_each(past_request_t, p, past_request_tree) {
170 if(p->firstseen + mesh->pinginterval <= now.tv_sec)
171 splay_delete_node(past_request_tree, node), deleted++;
177 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
180 timeout_set(&mesh->loop, &past_request_timeout, &(struct timeval){10, rand() % 100000});
183 bool seen_request(const char *request) {
184 past_request_t *new, p = {NULL};
188 if(splay_search(past_request_tree, &p)) {
189 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
192 new = xmalloc(sizeof *new);
193 new->request = xstrdup(request);
194 new->firstseen = now.tv_sec;
195 splay_insert(past_request_tree, new);
196 timeout_add(&mesh->loop, &past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000});
201 void init_requests(void) {
202 past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
205 void exit_requests(void) {
206 splay_delete_tree(past_request_tree);
208 timeout_del(&mesh->loop, &past_request_timeout);