Use the event log under Windows.
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2001 Ivo Timmermans <ivo@o2w.nl>,
4                   2000,2001 Guus Sliepen <guus@sliepen.eu.org>
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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: protocol.c,v 1.28.4.146 2003/08/03 12:38:43 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "meta.h"
29 #include "protocol.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 /* Jumptable for the request handlers */
34
35 static bool (*request_handlers[])(connection_t *) = {
36                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
37                 status_h, error_h, termreq_h,
38                 ping_h, pong_h,
39                 add_subnet_h, del_subnet_h,
40                 add_edge_h, del_edge_h,
41                 key_changed_h, req_key_h, ans_key_h, tcppacket_h,
42 };
43
44 /* Request names */
45
46 static char (*request_name[]) = {
47                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
48                 "STATUS", "ERROR", "TERMREQ",
49                 "PING", "PONG",
50                 "ADD_SUBNET", "DEL_SUBNET",
51                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET",
52 };
53
54 static avl_tree_t *past_request_tree;
55
56 bool check_id(const char *id)
57 {
58         for(; *id; id++)
59                 if(!isalnum(*id) && *id != '_')
60                         return false;
61
62         return true;
63 }
64
65 /* Generic request routines - takes care of logging and error
66    detection as well */
67
68 bool send_request(connection_t *c, const char *format, ...)
69 {
70         va_list args;
71         char buffer[MAXBUFSIZE];
72         int len, request;
73
74         cp();
75
76         /* Use vsnprintf instead of vasprintf: 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(buffer, MAXBUFSIZE, format, args);
82         va_end(args);
83
84         if(len < 0 || len > MAXBUFSIZE - 1) {
85                 logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"),
86                            c->name, c->hostname);
87                 return false;
88         }
89
90         ifdebug(PROTOCOL) {
91                 sscanf(buffer, "%d", &request);
92                 ifdebug(META)
93                         logger(LOG_DEBUG, _("Sending %s to %s (%s): %s"),
94                                    request_name[request], c->name, c->hostname, buffer);
95                 else
96                         logger(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request],
97                                    c->name, c->hostname);
98         }
99
100         buffer[len++] = '\n';
101
102         if(c == broadcast) {
103                 broadcast_meta(NULL, buffer, len);
104                 return true;
105         } else
106                 return send_meta(c, buffer, len);
107 }
108
109 void forward_request(connection_t *from)
110 {
111         int request;
112
113         cp();
114
115         ifdebug(PROTOCOL) {
116                 sscanf(from->buffer, "%d", &request);
117                 ifdebug(META)
118                         logger(LOG_DEBUG, _("Forwarding %s from %s (%s): %s"),
119                                    request_name[request], from->name, from->hostname,
120                                    from->buffer);
121                 else
122                         logger(LOG_DEBUG, _("Forwarding %s from %s (%s)"),
123                                    request_name[request], from->name, from->hostname);
124         }
125
126         from->buffer[from->reqlen - 1] = '\n';
127
128         broadcast_meta(from, from->buffer, from->reqlen);
129 }
130
131 bool receive_request(connection_t *c)
132 {
133         int request;
134
135         cp();
136
137         if(sscanf(c->buffer, "%d", &request) == 1) {
138                 if((request < 0) || (request >= LAST) || !request_handlers[request]) {
139                         ifdebug(META)
140                                 logger(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
141                                            c->name, c->hostname, c->buffer);
142                         else
143                                 logger(LOG_ERR, _("Unknown request from %s (%s)"),
144                                            c->name, c->hostname);
145
146                         return false;
147                 } else {
148                         ifdebug(PROTOCOL) {
149                                 ifdebug(META)
150                                         logger(LOG_DEBUG, _("Got %s from %s (%s): %s"),
151                                                    request_name[request], c->name, c->hostname,
152                                                    c->buffer);
153                                 else
154                                         logger(LOG_DEBUG, _("Got %s from %s (%s)"),
155                                                    request_name[request], c->name, c->hostname);
156                         }
157                 }
158
159                 if((c->allow_request != ALL) && (c->allow_request != request)) {
160                         logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
161                                    c->hostname);
162                         return false;
163                 }
164
165                 if(!request_handlers[request](c)) {
166                         /* Something went wrong. Probably scriptkiddies. Terminate. */
167
168                         logger(LOG_ERR, _("Error while processing %s from %s (%s)"),
169                                    request_name[request], c->name, c->hostname);
170                         return false;
171                 }
172         } else {
173                 logger(LOG_ERR, _("Bogus data received from %s (%s)"),
174                            c->name, c->hostname);
175                 return false;
176         }
177
178         return true;
179 }
180
181 static int past_request_compare(const past_request_t *a, const past_request_t *b)
182 {
183         return strcmp(a->request, b->request);
184 }
185
186 static void free_past_request(past_request_t *r)
187 {
188         cp();
189
190         if(r->request)
191                 free(r->request);
192
193         free(r);
194 }
195
196 void init_requests(void)
197 {
198         cp();
199
200         past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
201 }
202
203 void exit_requests(void)
204 {
205         cp();
206
207         avl_delete_tree(past_request_tree);
208 }
209
210 bool seen_request(char *request)
211 {
212         past_request_t *new, p = {0};
213
214         cp();
215
216         p.request = request;
217
218         if(avl_search(past_request_tree, &p)) {
219                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request"));
220                 return true;
221         } else {
222                 new = (past_request_t *) xmalloc(sizeof(*new));
223                 new->request = xstrdup(request);
224                 new->firstseen = now;
225                 avl_insert(past_request_tree, new);
226                 return false;
227         }
228 }
229
230 void age_past_requests(void)
231 {
232         avl_node_t *node, *next;
233         past_request_t *p;
234         int left = 0, deleted = 0;
235
236         cp();
237
238         for(node = past_request_tree->head; node; node = next) {
239                 next = node->next;
240                 p = (past_request_t *) node->data;
241
242                 if(p->firstseen + pingtimeout < now)
243                         avl_delete_node(past_request_tree, node), deleted++;
244                 else
245                         left++;
246         }
247
248         if(left || deleted)
249                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Aging past requests: deleted %d, left %d"),
250                            deleted, left);
251 }