2 protocol_subnet.c -- handle the meta-protocol, subnets
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
26 #include "connection.h"
36 bool send_add_subnet(connection_t *c, const subnet_t *subnet)
38 char netstr[MAXNETSTR];
42 if(!net2str(netstr, sizeof netstr, subnet))
45 return send_request(c, "%d %lx %s %s", ADD_SUBNET, random(), subnet->owner->name, netstr);
48 bool add_subnet_h(connection_t *c)
50 char subnetstr[MAX_STRING_SIZE];
51 char name[MAX_STRING_SIZE];
53 subnet_t s = {0}, *new;
57 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
58 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
63 /* Check if owner name is a valid */
66 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
67 c->hostname, _("invalid name"));
71 /* Check if subnet string is valid */
73 if(!str2net(&s, subnetstr)) {
74 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
75 c->hostname, _("invalid subnet string"));
79 if(seen_request(c->buffer))
82 /* Check if the owner of the new subnet is in the connection list */
84 owner = lookup_node(name);
88 owner->name = xstrdup(name);
92 if(tunnelserver && owner != myself && owner != c->node)
95 /* Check if we already know this subnet */
97 if(lookup_subnet(owner, &s))
100 /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
102 if(owner == myself) {
103 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
104 "ADD_SUBNET", c->name, c->hostname);
106 send_del_subnet(c, &s);
110 /* In tunnel server mode, check if the subnet matches one in the config file of this node */
116 for(cfg = lookup_config(c->config_tree, "Subnet"); cfg; cfg = lookup_config_next(c->config_tree, cfg)) {
117 if(!get_config_subnet(cfg, &allowed))
120 if(!subnet_compare(&s, allowed))
123 free_subnet(allowed);
129 free_subnet(allowed);
132 /* If everything is correct, add the subnet to the list of the owner */
134 *(new = new_subnet()) = s;
135 subnet_add(owner, new);
137 if(owner->status.reachable)
138 subnet_update(owner, new, true);
148 bool send_del_subnet(connection_t *c, const subnet_t *s)
150 char netstr[MAXNETSTR];
154 if(!net2str(netstr, sizeof netstr, s))
157 return send_request(c, "%d %lx %s %s", DEL_SUBNET, random(), s->owner->name, netstr);
160 bool del_subnet_h(connection_t *c)
162 char subnetstr[MAX_STRING_SIZE];
163 char name[MAX_STRING_SIZE];
165 subnet_t s = {0}, *find;
169 if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
170 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
175 /* Check if owner name is a valid */
177 if(!check_id(name)) {
178 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
179 c->hostname, _("invalid name"));
183 /* Check if the owner of the new subnet is in the connection list */
185 owner = lookup_node(name);
188 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
189 "DEL_SUBNET", c->name, c->hostname, name);
193 if(tunnelserver && owner != myself && owner != c->node)
196 /* Check if subnet string is valid */
198 if(!str2net(&s, subnetstr)) {
199 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
200 c->hostname, _("invalid subnet string"));
204 if(seen_request(c->buffer))
207 /* If everything is correct, delete the subnet from the list of the owner */
211 find = lookup_subnet(owner, &s);
214 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
215 "DEL_SUBNET", c->name, c->hostname, name);
219 /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
221 if(owner == myself) {
222 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
223 "DEL_SUBNET", c->name, c->hostname);
224 send_add_subnet(c, find);
233 /* Finally, delete it. */
235 if(owner->status.reachable)
236 subnet_update(owner, find, false);
238 subnet_del(owner, find);