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