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