]> git.meshlink.io Git - meshlink/blobdiff - src/protocol_edge.c
Merge branch 'mesh_topology_output' into roles
[meshlink] / src / protocol_edge.c
index 5c47f5837a544a1cc833905ef0f85e97a9083b07..89ed901d1979b0e04e5a865dc18deb78a2e2700f 100644 (file)
 #include "utils.h"
 #include "xalloc.h"
 
+extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
+
 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e) {
        bool x;
        char *address, *port;
 
        sockaddr2str(&e->address, &address, &port);
 
-       x = send_request(mesh, c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
-                                        e->from->name, e->to->name, address, port,
+       x = send_request(mesh, c, "%d %x %s %d %s %s %s %d %x %d", ADD_EDGE, rand(),
+                                        e->from->name, e->from->devclass, e->to->name, address, port, e->to->devclass,
                                         e->options, e->weight);
        free(address);
        free(port);
@@ -52,15 +54,17 @@ bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
        edge_t *e;
        node_t *from, *to;
        char from_name[MAX_STRING_SIZE];
+       int from_devclass;
        char to_name[MAX_STRING_SIZE];
        char to_address[MAX_STRING_SIZE];
        char to_port[MAX_STRING_SIZE];
+       int to_devclass;
        sockaddr_t address;
        uint32_t options;
        int weight;
 
-       if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d",
-                         from_name, to_name, to_address, to_port, &options, &weight) != 6) {
+       if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d",
+                         from_name, &from_devclass, to_name, to_address, to_port, &to_devclass, &options, &weight) != 8) {
                logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
                           c->hostname);
                return false;
@@ -74,6 +78,20 @@ bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                return false;
        }
 
+       // Check if devclasses are valid
+
+       if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) {
+               logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
+                          c->hostname, "from devclass invalid");
+               return false;
+       }
+
+       if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) {
+               logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
+                          c->hostname, "to devclass invalid");
+               return false;
+       }
+
        if(seen_request(mesh, request))
                return true;
 
@@ -88,12 +106,17 @@ bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                node_add(mesh, from);
        }
 
+       from->devclass = from_devclass;
+       node_write_devclass(mesh, from);
+
        if(!to) {
                to = new_node();
                to->name = xstrdup(to_name);
                node_add(mesh, to);
        }
 
+       to->devclass = to_devclass;
+       node_write_devclass(mesh, to);
 
        /* Convert addresses */