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