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