Reduce KEY_CHANGED traffic.
[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.131 2002/09/04 08:02:33 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <errno.h>
33
34 #include <utils.h>
35 #include <xalloc.h>
36
37 #include "conf.h"
38 #include "protocol.h"
39 #include "meta.h"
40 #include "connection.h"
41
42 #include "system.h"
43
44 avl_tree_t *past_request_tree;
45
46 int check_id(char *id)
47 {
48   int i;
49
50   for (i = 0; i < strlen(id); i++)
51     if(!isalnum(id[i]) && id[i] != '_')
52       return -1;
53   
54   return 0;
55 }
56
57 /* Generic request routines - takes care of logging and error
58    detection as well */
59
60 int send_request(connection_t *c, const char *format, ...)
61 {
62   va_list args;
63   char buffer[MAXBUFSIZE];
64   int len, request;
65   char *name = "everyone";
66   char *hostname = "broadcast";
67 cp
68   if(c)
69     {
70       name = c->name;
71       hostname = c->hostname;
72     }
73
74   /* Use vsnprintf instead of vasprintf: faster, no memory
75      fragmentation, cleanup is automatic, and there is a limit on the
76      input buffer anyway */
77
78   va_start(args, format);
79   len = vsnprintf(buffer, MAXBUFSIZE, format, args);
80   va_end(args);
81
82   if(len < 0 || len > MAXBUFSIZE-1)
83     {
84       syslog(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"), name, hostname);
85       return -1;
86     }
87
88   if(debug_lvl >= DEBUG_PROTOCOL)
89     {
90       sscanf(buffer, "%d", &request);
91       if(debug_lvl >= DEBUG_META)
92         syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], name, hostname, buffer);
93       else
94         syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], name, hostname);
95     }
96
97   buffer[len++] = '\n';
98 cp
99   if(c)
100     return send_meta(c, buffer, len);
101   else
102     return broadcast_meta(NULL, buffer, len);
103 }
104
105 int receive_request(connection_t *c)
106 {
107   int request;
108 cp
109   if(sscanf(c->buffer, "%d", &request) == 1)
110     {
111       if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
112         {
113           if(debug_lvl >= DEBUG_META)
114             syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
115                    c->name, c->hostname, c->buffer);
116           else
117             syslog(LOG_ERR, _("Unknown request from %s (%s)"),
118                    c->name, c->hostname);
119                    
120           return -1;
121         }
122       else
123         {
124           if(debug_lvl >= DEBUG_PROTOCOL)
125             {
126               if(debug_lvl >= DEBUG_META)
127                 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
128                        request_name[request], c->name, c->hostname, c->buffer);
129               else
130                 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
131                        request_name[request], c->name, c->hostname);
132             }
133         }
134
135       if((c->allow_request != ALL) && (c->allow_request != request))
136         {
137           syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
138           return -1;
139         }
140
141       if(request_handlers[request](c))
142         /* Something went wrong. Probably scriptkiddies. Terminate. */
143         {
144           syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
145                  request_name[request], c->name, c->hostname);
146           return -1;
147         }
148     }
149   else
150     {
151       syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
152              c->name, c->hostname);
153       return -1;
154     }
155 cp
156   return 0;
157 }
158
159 int past_request_compare(past_request_t *a, past_request_t *b)
160 {
161 cp
162   return strcmp(a->request, b->request);
163 }
164
165 void free_past_request(past_request_t *r)
166 {
167 cp
168   if(r->request)
169     free(r->request);
170   free(r);
171 cp
172 }
173
174 void init_requests(void)
175 {
176 cp
177   past_request_tree = avl_alloc_tree((avl_compare_t)past_request_compare, (avl_action_t)free_past_request);
178 cp
179 }
180
181 void exit_requests(void)
182 {
183 cp
184   avl_delete_tree(past_request_tree);
185 cp
186 }
187
188 int seen_request(char *request)
189 {
190   past_request_t p, *new;
191 cp
192   p.request = request;
193
194   if(avl_search(past_request_tree, &p))
195     {
196       if(debug_lvl >= DEBUG_SCARY_THINGS)
197         syslog(LOG_DEBUG, _("Already seen request"));
198       return 1;
199     }
200   else
201     {
202       new = (past_request_t *)xmalloc(sizeof(*new));
203       new->request = xstrdup(request);
204       new->firstseen = now;
205       avl_insert(past_request_tree, new);
206       return 0;
207     }
208 cp  
209 }
210
211 void age_past_requests(void)
212 {
213   avl_node_t *node, *next;
214   past_request_t *p;
215   int left = 0, deleted = 0;
216 cp 
217   for(node = past_request_tree->head; node; node = next)
218     {
219       next = node->next;
220       p = (past_request_t *)node->data;
221       if(p->firstseen + pingtimeout < now)
222         avl_delete_node(past_request_tree, node), deleted++;
223       else
224         left++;
225     }
226
227   if(debug_lvl >= DEBUG_SCARY_THINGS && left + deleted)
228     syslog(LOG_DEBUG, _("Aging past requests: deleted %d, left %d\n"), deleted, left);
229 cp
230 }
231
232 /* Jumptable for the request handlers */
233
234 int (*request_handlers[])(connection_t*) = {
235   id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
236   status_h, error_h, termreq_h,
237   ping_h, pong_h,
238   add_subnet_h, del_subnet_h,
239   add_node_h, del_node_h,
240   key_changed_h, req_key_h, ans_key_h,
241   tcppacket_h,
242 };
243
244 /* Request names */
245
246 char (*request_name[]) = {
247   "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
248   "STATUS", "ERROR", "TERMREQ",
249   "PING", "PONG",
250   "ADD_SUBNET", "DEL_SUBNET",
251   "ADD_NODE", "DEL_NODE",
252   "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
253   "PACKET",
254 };