]> git.meshlink.io Git - meshlink/blob - src/protocol_edge.c
Add duplicate node detection callback.
[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 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
37
38 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
39         bool x;
40         char *address, *port;
41
42         sockaddr2str(&e->address, &address, &port);
43
44         x = send_request(mesh, c, "%d %x %s %d %s %s %s %d %x %d %d", ADD_EDGE, rand(),
45                          e->from->name, e->from->devclass, e->to->name, address, port, e->to->devclass,
46                          e->options, e->weight, contradictions);
47         free(address);
48         free(port);
49
50         return x;
51 }
52
53 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
54         edge_t *e;
55         node_t *from, *to;
56         char from_name[MAX_STRING_SIZE];
57         int from_devclass;
58         char to_name[MAX_STRING_SIZE];
59         char to_address[MAX_STRING_SIZE];
60         char to_port[MAX_STRING_SIZE];
61         int to_devclass;
62         sockaddr_t address;
63         uint32_t options;
64         int weight;
65         int contradictions = 0;
66
67         if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d %d",
68                         from_name, &from_devclass, to_name, to_address, to_port, &to_devclass, &options, &weight, &contradictions) < 8) {
69                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ADD_EDGE", c->name);
70                 return false;
71         }
72
73         /* Check if names are valid */
74
75         if(!check_id(from_name) || !check_id(to_name)) {
76                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid name");
77                 return false;
78         }
79
80         // Check if devclasses are valid
81
82         if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) {
83                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid");
84                 return false;
85         }
86
87         if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) {
88                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid");
89                 return false;
90         }
91
92         if(seen_request(mesh, request)) {
93                 return true;
94         }
95
96         /* Lookup nodes */
97
98         from = lookup_node(mesh, from_name);
99         to = lookup_node(mesh, to_name);
100
101         if(!from) {
102                 from = new_node();
103                 from->status.blacklisted = mesh->default_blacklist;
104                 from->name = xstrdup(from_name);
105                 node_add(mesh, from);
106         }
107
108         if(contradictions > 50) {
109                 handle_duplicate_node(mesh, from);
110         }
111
112         from->devclass = from_devclass;
113         node_write_devclass(mesh, from);
114
115         if(!to) {
116                 to = new_node();
117                 to->status.blacklisted = mesh->default_blacklist;
118                 to->name = xstrdup(to_name);
119                 node_add(mesh, to);
120         }
121
122         to->devclass = to_devclass;
123         node_write_devclass(mesh, to);
124
125         /* Convert addresses */
126
127         address = str2sockaddr(to_address, to_port);
128
129         /* Check if edge already exists */
130
131         e = lookup_edge(from, to);
132
133         if(e) {
134                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
135                         if(from == mesh->self) {
136                                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not match existing entry",
137                                        "ADD_EDGE", c->name);
138                                 send_add_edge(mesh, c, e, 0);
139                                 return true;
140                         } else {
141                                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not match existing entry",
142                                        "ADD_EDGE", c->name);
143                                 edge_del(mesh, e);
144                                 graph(mesh);
145                         }
146                 } else {
147                         return true;
148                 }
149         } else if(from == mesh->self) {
150                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist",
151                        "ADD_EDGE", c->name);
152                 mesh->contradicting_add_edge++;
153                 e = new_edge();
154                 e->from = from;
155                 e->to = to;
156                 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
157                 free_edge(e);
158                 return true;
159         }
160
161         e = new_edge();
162         e->from = from;
163         e->to = to;
164         e->address = address;
165         e->options = options;
166         e->weight = weight;
167         edge_add(mesh, e);
168
169         /* Tell the rest about the new edge */
170
171         forward_request(mesh, c, request);
172
173         /* Run MST before or after we tell the rest? */
174
175         graph(mesh);
176
177         return true;
178 }
179
180 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
181         return send_request(mesh, c, "%d %x %s %s %d", DEL_EDGE, rand(),
182                             e->from->name, e->to->name, contradictions);
183 }
184
185 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
186         edge_t *e;
187         char from_name[MAX_STRING_SIZE];
188         char to_name[MAX_STRING_SIZE];
189         node_t *from, *to;
190         int contradictions = 0;
191
192         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &contradictions) < 2) {
193                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
194                 return false;
195         }
196
197         /* Check if names are valid */
198
199         if(!check_id(from_name) || !check_id(to_name)) {
200                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "DEL_EDGE", c->name, "invalid name");
201                 return false;
202         }
203
204         if(seen_request(mesh, request)) {
205                 return true;
206         }
207
208         /* Lookup nodes */
209
210         from = lookup_node(mesh, from_name);
211         to = lookup_node(mesh, to_name);
212
213         if(!from) {
214                 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
215                        "DEL_EDGE", c->name);
216                 return true;
217         }
218
219         if(!to) {
220                 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
221                        "DEL_EDGE", c->name);
222                 return true;
223         }
224
225         if(contradictions > 50) {
226                 handle_duplicate_node(mesh, from);
227         }
228
229         /* Check if edge exists */
230
231         e = lookup_edge(from, to);
232
233         if(!e) {
234                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree",
235                        "DEL_EDGE", c->name);
236                 return true;
237         }
238
239         if(e->from == mesh->self) {
240                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself",
241                        "DEL_EDGE", c->name);
242                 mesh->contradicting_del_edge++;
243                 send_add_edge(mesh, c, e, mesh->contradicting_del_edge);    /* Send back a correction */
244                 return true;
245         }
246
247         /* Tell the rest about the deleted edge */
248
249         forward_request(mesh, c, request);
250
251         /* Delete the edge */
252
253         edge_del(mesh, e);
254
255         /* Run MST before or after we tell the rest? */
256
257         graph(mesh);
258
259         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
260
261         if(!to->status.reachable) {
262                 e = lookup_edge(to, mesh->self);
263
264                 if(e) {
265                         send_del_edge(mesh, mesh->everyone, e, 0);
266                         edge_del(mesh, e);
267                 }
268         }
269
270         return true;
271 }