Define logger(), cleans up source code and allows us to write log entries
[tinc] / src / protocol_subnet.c
1 /*
2     protocol_subnet.c -- handle the meta-protocol, subnets
3     Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2002 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_subnet.c,v 1.1.4.10 2003/07/06 22:11:33 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <errno.h>
30
31 #include <utils.h>
32 #include <xalloc.h>
33 #include <avl_tree.h>
34
35 #include "conf.h"
36 #include "net.h"
37 #include "netutl.h"
38 #include "protocol.h"
39 #include "meta.h"
40 #include "connection.h"
41 #include "node.h"
42 #include "logger.h"
43
44 #include "system.h"
45
46 int send_add_subnet(connection_t *c, subnet_t *subnet)
47 {
48         int x;
49         char *netstr;
50
51         cp();
52
53         x = send_request(c, "%d %lx %s %s", ADD_SUBNET, random(),
54                                          subnet->owner->name, netstr = net2str(subnet));
55
56         free(netstr);
57
58         return x;
59 }
60
61 int add_subnet_h(connection_t *c)
62 {
63         char subnetstr[MAX_STRING_SIZE];
64         char name[MAX_STRING_SIZE];
65         node_t *owner;
66         subnet_t *s;
67
68         cp();
69
70         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
71                 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
72                            c->hostname);
73                 return -1;
74         }
75
76         /* Check if owner name is a valid */
77
78         if(check_id(name)) {
79                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
80                            c->hostname, _("invalid name"));
81                 return -1;
82         }
83
84         /* Check if subnet string is valid */
85
86         s = str2net(subnetstr);
87
88         if(!s) {
89                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
90                            c->hostname, _("invalid subnet string"));
91                 return -1;
92         }
93
94         if(seen_request(c->buffer))
95                 return 0;
96
97         /* Check if the owner of the new subnet is in the connection list */
98
99         owner = lookup_node(name);
100
101         if(!owner) {
102                 owner = new_node();
103                 owner->name = xstrdup(name);
104                 node_add(owner);
105         }
106
107         /* Check if we already know this subnet */
108
109         if(lookup_subnet(owner, s)) {
110                 free_subnet(s);
111                 return 0;
112         }
113
114         /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
115
116         if(owner == myself) {
117                 logger(DEBUG_PROTOCOL, LOG_WARNING, _("Got %s from %s (%s) for ourself"),
118                                    "ADD_SUBNET", c->name, c->hostname);
119                 s->owner = myself;
120                 send_del_subnet(c, s);
121                 return 0;
122         }
123
124         /* If everything is correct, add the subnet to the list of the owner */
125
126         subnet_add(owner, s);
127
128         /* Tell the rest */
129
130         forward_request(c);
131
132         return 0;
133 }
134
135 int send_del_subnet(connection_t *c, subnet_t *s)
136 {
137         int x;
138         char *netstr;
139
140         cp();
141
142         netstr = net2str(s);
143
144         x = send_request(c, "%d %lx %s %s", DEL_SUBNET, random(), s->owner->name, netstr);
145
146         free(netstr);
147
148         return x;
149 }
150
151 int del_subnet_h(connection_t *c)
152 {
153         char subnetstr[MAX_STRING_SIZE];
154         char name[MAX_STRING_SIZE];
155         node_t *owner;
156         subnet_t *s, *find;
157
158         cp();
159
160         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
161                 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
162                            c->hostname);
163                 return -1;
164         }
165
166         /* Check if owner name is a valid */
167
168         if(check_id(name)) {
169                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
170                            c->hostname, _("invalid name"));
171                 return -1;
172         }
173
174         /* Check if the owner of the new subnet is in the connection list */
175
176         owner = lookup_node(name);
177
178         if(!owner) {
179                 logger(DEBUG_PROTOCOL, LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
180                                    "DEL_SUBNET", c->name, c->hostname, name);
181                 return 0;
182         }
183
184         /* Check if subnet string is valid */
185
186         s = str2net(subnetstr);
187
188         if(!s) {
189                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
190                            c->hostname, _("invalid subnet string"));
191                 return -1;
192         }
193
194         if(seen_request(c->buffer))
195                 return 0;
196
197         /* If everything is correct, delete the subnet from the list of the owner */
198
199         s->owner = owner;
200
201         find = lookup_subnet(owner, s);
202
203         free_subnet(s);
204
205         if(!find) {
206                 logger(DEBUG_PROTOCOL, LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
207                                    "DEL_SUBNET", c->name, c->hostname, name);
208                 return 0;
209         }
210
211         /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
212
213         if(owner == myself) {
214                 logger(DEBUG_PROTOCOL, LOG_WARNING, _("Got %s from %s (%s) for ourself"),
215                                    "DEL_SUBNET", c->name, c->hostname);
216                 send_add_subnet(c, find);
217                 return 0;
218         }
219
220         /* Tell the rest */
221
222         forward_request(c);
223
224         /* Finally, delete it. */
225
226         subnet_del(owner, find);
227
228         return 0;
229 }