]> git.meshlink.io Git - meshlink/blob - src/protocol_edge.c
Move node_udp_cache, everyone and invitation_key to mesh.
[meshlink] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "connection.h"
24 #include "edge.h"
25 #include "graph.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "meta.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "node.h"
32 #include "protocol.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 bool send_add_edge(connection_t *c, const edge_t *e) {
37         bool x;
38         char *address, *port;
39
40         sockaddr2str(&e->address, &address, &port);
41
42         x = send_request(c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
43                                          e->from->name, e->to->name, address, port,
44                                          e->options, e->weight);
45         free(address);
46         free(port);
47
48         return x;
49 }
50
51 bool add_edge_h(connection_t *c, const char *request) {
52         edge_t *e;
53         node_t *from, *to;
54         char from_name[MAX_STRING_SIZE];
55         char to_name[MAX_STRING_SIZE];
56         char to_address[MAX_STRING_SIZE];
57         char to_port[MAX_STRING_SIZE];
58         sockaddr_t address;
59         uint32_t options;
60         int weight;
61
62         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d",
63                           from_name, to_name, to_address, to_port, &options, &weight) != 6) {
64                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
65                            c->hostname);
66                 return false;
67         }
68
69         /* Check if names are valid */
70
71         if(!check_id(from_name) || !check_id(to_name)) {
72                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
73                            c->hostname, "invalid name");
74                 return false;
75         }
76
77         if(seen_request(request))
78                 return true;
79
80         /* Lookup nodes */
81
82         from = lookup_node(from_name);
83         to = lookup_node(to_name);
84
85         if(!from) {
86                 from = new_node();
87                 from->name = xstrdup(from_name);
88                 node_add(from);
89         }
90
91         if(!to) {
92                 to = new_node();
93                 to->name = xstrdup(to_name);
94                 node_add(to);
95         }
96
97
98         /* Convert addresses */
99
100         address = str2sockaddr(to_address, to_port);
101
102         /* Check if edge already exists */
103
104         e = lookup_edge(from, to);
105
106         if(e) {
107                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
108                         if(from == mesh->self) {
109                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
110                                                    "ADD_EDGE", c->name, c->hostname);
111                                 send_add_edge(c, e);
112                                 return true;
113                         } else {
114                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
115                                                    "ADD_EDGE", c->name, c->hostname);
116                                 edge_del(e);
117                                 graph();
118                         }
119                 } else
120                         return true;
121         } else if(from == mesh->self) {
122                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
123                                    "ADD_EDGE", c->name, c->hostname);
124                 mesh->contradicting_add_edge++;
125                 e = new_edge();
126                 e->from = from;
127                 e->to = to;
128                 send_del_edge(c, e);
129                 free_edge(e);
130                 return true;
131         }
132
133         e = new_edge();
134         e->from = from;
135         e->to = to;
136         e->address = address;
137         e->options = options;
138         e->weight = weight;
139         edge_add(e);
140
141         /* Tell the rest about the new edge */
142
143         forward_request(c, request);
144
145         /* Run MST before or after we tell the rest? */
146
147         graph();
148
149         return true;
150 }
151
152 bool send_del_edge(connection_t *c, const edge_t *e) {
153         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
154                                                 e->from->name, e->to->name);
155 }
156
157 bool del_edge_h(connection_t *c, const char *request) {
158         edge_t *e;
159         char from_name[MAX_STRING_SIZE];
160         char to_name[MAX_STRING_SIZE];
161         node_t *from, *to;
162
163         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
164                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
165                            c->hostname);
166                 return false;
167         }
168
169         /* Check if names are valid */
170
171         if(!check_id(from_name) || !check_id(to_name)) {
172                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
173                            c->hostname, "invalid name");
174                 return false;
175         }
176
177         if(seen_request(request))
178                 return true;
179
180         /* Lookup nodes */
181
182         from = lookup_node(from_name);
183         to = lookup_node(to_name);
184
185         if(!from) {
186                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
187                                    "DEL_EDGE", c->name, c->hostname);
188                 return true;
189         }
190
191         if(!to) {
192                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
193                                    "DEL_EDGE", c->name, c->hostname);
194                 return true;
195         }
196
197         /* Check if edge exists */
198
199         e = lookup_edge(from, to);
200
201         if(!e) {
202                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
203                                    "DEL_EDGE", c->name, c->hostname);
204                 return true;
205         }
206
207         if(e->from == mesh->self) {
208                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
209                                    "DEL_EDGE", c->name, c->hostname);
210                 mesh->contradicting_del_edge++;
211                 send_add_edge(c, e);    /* Send back a correction */
212                 return true;
213         }
214
215         /* Tell the rest about the deleted edge */
216
217         forward_request(c, request);
218
219         /* Delete the edge */
220
221         edge_del(e);
222
223         /* Run MST before or after we tell the rest? */
224
225         graph();
226
227         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
228
229         if(!to->status.reachable) {
230                 e = lookup_edge(to, mesh->self);
231                 if(e) {
232                         send_del_edge(mesh->everyone, e);
233                         edge_del(e);
234                 }
235         }
236
237         return true;
238 }