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