]> git.meshlink.io Git - meshlink/blob - src/protocol_edge.c
Merge branch 'master' into saverio
[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 "meta.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "node.h"
31 #include "protocol.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 bool send_add_edge(connection_t *c, const edge_t *e) {
36         bool x;
37         char *address, *port;
38
39         sockaddr2str(&e->address, &address, &port);
40
41         x = send_request(c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
42                                          e->from->name, e->to->name, address, port,
43                                          e->options, e->weight);
44         free(address);
45         free(port);
46
47         return x;
48 }
49
50 bool add_edge_h(connection_t *c, const char *request) {
51         edge_t *e;
52         node_t *from, *to;
53         char from_name[MAX_STRING_SIZE];
54         char to_name[MAX_STRING_SIZE];
55         char to_address[MAX_STRING_SIZE];
56         char to_port[MAX_STRING_SIZE];
57         sockaddr_t address;
58         uint32_t options;
59         int weight;
60
61         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d",
62                           from_name, to_name, to_address, to_port, &options, &weight) != 6) {
63                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
64                            c->hostname);
65                 return false;
66         }
67
68         /* Check if names are valid */
69
70         if(!check_id(from_name) || !check_id(to_name)) {
71                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
72                            c->hostname, "invalid name");
73                 return false;
74         }
75
76         if(seen_request(request))
77                 return true;
78
79         /* Lookup nodes */
80
81         from = lookup_node(from_name);
82         to = lookup_node(to_name);
83
84         if(!from) {
85                 from = new_node();
86                 from->name = xstrdup(from_name);
87                 node_add(from);
88         }
89
90         if(!to) {
91                 to = new_node();
92                 to->name = xstrdup(to_name);
93                 node_add(to);
94         }
95
96
97         /* Convert addresses */
98
99         address = str2sockaddr(to_address, to_port);
100
101         /* Check if edge already exists */
102
103         e = lookup_edge(from, to);
104
105         if(e) {
106                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
107                         if(from == myself) {
108                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
109                                                    "ADD_EDGE", c->name, c->hostname);
110                                 send_add_edge(c, e);
111                                 return true;
112                         } else {
113                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
114                                                    "ADD_EDGE", c->name, c->hostname);
115                                 edge_del(e);
116                                 graph();
117                         }
118                 } else
119                         return true;
120         } else if(from == myself) {
121                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
122                                    "ADD_EDGE", c->name, c->hostname);
123                 contradicting_add_edge++;
124                 e = new_edge();
125                 e->from = from;
126                 e->to = to;
127                 send_del_edge(c, e);
128                 free_edge(e);
129                 return true;
130         }
131
132         e = new_edge();
133         e->from = from;
134         e->to = to;
135         e->address = address;
136         e->options = options;
137         e->weight = weight;
138         edge_add(e);
139
140         /* Tell the rest about the new edge */
141
142         forward_request(c, request);
143
144         /* Run MST before or after we tell the rest? */
145
146         graph();
147
148         return true;
149 }
150
151 bool send_del_edge(connection_t *c, const edge_t *e) {
152         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
153                                                 e->from->name, e->to->name);
154 }
155
156 bool del_edge_h(connection_t *c, const char *request) {
157         edge_t *e;
158         char from_name[MAX_STRING_SIZE];
159         char to_name[MAX_STRING_SIZE];
160         node_t *from, *to;
161
162         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
163                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
164                            c->hostname);
165                 return false;
166         }
167
168         /* Check if names are valid */
169
170         if(!check_id(from_name) || !check_id(to_name)) {
171                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
172                            c->hostname, "invalid name");
173                 return false;
174         }
175
176         if(seen_request(request))
177                 return true;
178
179         /* Lookup nodes */
180
181         from = lookup_node(from_name);
182         to = lookup_node(to_name);
183
184         if(!from) {
185                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
186                                    "DEL_EDGE", c->name, c->hostname);
187                 return true;
188         }
189
190         if(!to) {
191                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
192                                    "DEL_EDGE", c->name, c->hostname);
193                 return true;
194         }
195
196         /* Check if edge exists */
197
198         e = lookup_edge(from, to);
199
200         if(!e) {
201                 logger(DEBUG_PROTOCOL, LOG_WARNING, "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         if(e->from == myself) {
207                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
208                                    "DEL_EDGE", c->name, c->hostname);
209                 contradicting_del_edge++;
210                 send_add_edge(c, e);    /* Send back a correction */
211                 return true;
212         }
213
214         /* Tell the rest about the deleted edge */
215
216         forward_request(c, request);
217
218         /* Delete the edge */
219
220         edge_del(e);
221
222         /* Run MST before or after we tell the rest? */
223
224         graph();
225
226         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
227
228         if(!to->status.reachable) {
229                 e = lookup_edge(to, myself);
230                 if(e) {
231                         send_del_edge(everyone, e);
232                         edge_del(e);
233                 }
234         }
235
236         return true;
237 }