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