]> git.meshlink.io Git - meshlink/blob - src/protocol.c
Change my email address.
[meshlink] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2013 Guus Sliepen <guus@meshlink.io>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "connection.h"
25 #include "logger.h"
26 #include "meta.h"
27 #include "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 bool tunnelserver = false;
32 bool experimental = true;
33
34 /* Jumptable for the request handlers */
35
36 static bool (*request_handlers[])(connection_t *, const char *) = {
37                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
38                 status_h, error_h, termreq_h,
39                 ping_h, pong_h,
40                 NULL, NULL, //add_subnet_h, del_subnet_h,
41                 add_edge_h, del_edge_h,
42                 key_changed_h, req_key_h, ans_key_h, tcppacket_h, NULL, //control_h,
43 };
44
45 /* Request names */
46
47 static char (*request_name[]) = {
48                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
49                 "STATUS", "ERROR", "TERMREQ",
50                 "PING", "PONG",
51                 "ADD_SUBNET", "DEL_SUBNET",
52                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
53 };
54
55 static splay_tree_t *past_request_tree;
56
57 bool check_id(const char *id) {
58         if(!id || !*id)
59                 return false;
60
61         for(; *id; id++)
62                 if(!isalnum(*id) && *id != '_')
63                         return false;
64
65         return true;
66 }
67
68 /* Generic request routines - takes care of logging and error
69    detection as well */
70
71 bool send_request(connection_t *c, const char *format, ...) {
72         va_list args;
73         char request[MAXBUFSIZE];
74         int len;
75
76         /* Use vsnprintf instead of vxasprintf: faster, no memory
77            fragmentation, cleanup is automatic, and there is a limit on the
78            input buffer anyway */
79
80         va_start(args, format);
81         len = vsnprintf(request, MAXBUFSIZE, format, args);
82         va_end(args);
83
84         if(len < 0 || len > MAXBUFSIZE - 1) {
85                 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
86                            c->name, c->hostname);
87                 return false;
88         }
89
90         logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[atoi(request)], c->name, c->hostname, request);
91
92         request[len++] = '\n';
93
94         if(c == everyone) {
95                 broadcast_meta(NULL, request, len);
96                 return true;
97         } else
98                 return send_meta(c, request, len);
99 }
100
101 void forward_request(connection_t *from, const char *request) {
102         logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
103
104         // Create a temporary newline-terminated copy of the request
105         int len = strlen(request);
106         char tmp[len + 1];
107         memcpy(tmp, request, len);
108         tmp[len] = '\n';
109         broadcast_meta(from, tmp, sizeof tmp);
110 }
111
112 bool receive_request(connection_t *c, const char *request) {
113         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
114                 if(!request[0] || request[0] == '\r')
115                         return true;
116                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
117                         if(!strncmp(request + 9, "200", 3)) {
118                                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
119                                 return true;
120                         } else {
121                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
122                                 return false;
123                         }
124                 }
125         }
126
127         int reqno = atoi(request);
128
129         if(reqno || *request == '0') {
130                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
131                         logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
132                         return false;
133                 } else {
134                         logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", request_name[reqno], c->name, c->hostname, request);
135                 }
136
137                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
138                         logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
139                         return false;
140                 }
141
142                 if(!request_handlers[reqno](c, request)) {
143                         /* Something went wrong. Probably scriptkiddies. Terminate. */
144
145                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
146                         return false;
147                 }
148         } else {
149                 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
150                 return false;
151         }
152
153         return true;
154 }
155
156 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
157         return strcmp(a->request, b->request);
158 }
159
160 static void free_past_request(past_request_t *r) {
161         if(r->request)
162                 free((char *)r->request);
163
164         free(r);
165 }
166
167 static timeout_t past_request_timeout;
168
169 static void age_past_requests(void *data) {
170         int left = 0, deleted = 0;
171
172         for splay_each(past_request_t, p, past_request_tree) {
173                 if(p->firstseen + pinginterval <= now.tv_sec)
174                         splay_delete_node(past_request_tree, node), deleted++;
175                 else
176                         left++;
177         }
178
179         if(left || deleted)
180                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
181
182         if(left)
183                 timeout_set(&past_request_timeout, &(struct timeval){10, rand() % 100000});
184 }
185
186 bool seen_request(const char *request) {
187         past_request_t *new, p = {NULL};
188
189         p.request = request;
190
191         if(splay_search(past_request_tree, &p)) {
192                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
193                 return true;
194         } else {
195                 new = xmalloc(sizeof *new);
196                 new->request = xstrdup(request);
197                 new->firstseen = now.tv_sec;
198                 splay_insert(past_request_tree, new);
199                 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000});
200                 return false;
201         }
202 }
203
204 void init_requests(void) {
205         past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
206 }
207
208 void exit_requests(void) {
209         splay_delete_tree(past_request_tree);
210
211         timeout_del(&past_request_timeout);
212 }