]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
class based connect behaviour
[meshlink] / src / meshlink.c
index b8fb5055a41ed82a9ae740492f72cf0ab7e2b6b1..95ab41c7ab03f072fa1547b42be6eee92b20cadf 100644 (file)
@@ -742,11 +742,11 @@ static bool meshlink_setup(meshlink_handle_t *mesh) {
        return true;
 }
 
-meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname) {
-       return meshlink_open_with_size(confbase, name, appname, sizeof(meshlink_handle_t));
+meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dclass_t dclass) {
+       return meshlink_open_with_size(confbase, name, appname, dclass, sizeof(meshlink_handle_t));
 }
 
-meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, size_t size) {
+meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, dclass_t dclass, size_t size) {
 
        // Validate arguments provided by the application
        bool usingname = false;
@@ -779,6 +779,7 @@ meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *nam
        meshlink_handle_t *mesh = xzalloc(size);
        mesh->confbase = xstrdup(confbase);
        mesh->appname = xstrdup(appname);
+       mesh->dclass = dclass;
        if (usingname) mesh->name = xstrdup(name);
        pthread_mutex_init ( &(mesh->nodes_mutex), NULL);
        mesh->threadstarted = false;
@@ -934,6 +935,7 @@ void meshlink_close(meshlink_handle_t *mesh) {
        ecdsa_free(mesh->invitation_key);
 
        free(mesh->name);
+       free(mesh->appname);
        free(mesh->confbase);
 
        memset(mesh, 0, sizeof *mesh);
@@ -1677,39 +1679,21 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
 /* Hint that a hostname may be found at an address
  * See header file for detailed comment.
  */
-extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, struct sockaddr *addr) {
+extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr) {
        if(!mesh || !node || !addr)
                return;
        
-       char *addr_str = malloc(MAX_ADDRESS_LENGTH*sizeof(char));
-       memset(addr_str, 0, MAX_ADDRESS_LENGTH*sizeof(char));
+       char *host = NULL, *port = NULL, *str = NULL;
+       sockaddr2str((const sockaddr_t *)addr, &host, &port);
 
-       char *port_str = malloc(MAX_PORT_LENGTH*sizeof(char));
-       memset(port_str, 0, MAX_PORT_LENGTH*sizeof(char));
-       
-       // extra byte for a space, and one to make sure string is null-terminated
-       int full_addr_len = MAX_ADDRESS_LENGTH + MAX_PORT_LENGTH + 2;
-
-       char *full_addr_str = malloc(full_addr_len*sizeof(char));
-       memset(full_addr_str, 0, full_addr_len*sizeof(char));
-       
-       // get address and port number
-       if(!get_ip_str(addr, addr_str, MAX_ADDRESS_LENGTH))
-               goto fail;
-       if(!get_port_str(addr, port_str, MAX_ADDRESS_LENGTH))
-               goto fail;
-
-       // append_config_file expects an address, a space, and then a port number
-       strcat(full_addr_str, addr_str);
-       strcat(full_addr_str, " ");
-       strcat(full_addr_str, port_str);
-       
-       append_config_file(mesh, node->name, "Address", full_addr_str);
+       if(host && port) {
+               xasprintf(&str, "%s %s", host, port);
+               append_config_file(mesh, node->name, "Address", str);
+       }
 
-fail:
-       free(addr_str);
-       free(port_str);
-       free(full_addr_str);
+       free(str);
+       free(host);
+       free(port);
 
        // @TODO do we want to fire off a connection attempt right away?
 }
@@ -1721,3 +1705,101 @@ static void __attribute__((constructor)) meshlink_init(void) {
 static void __attribute__((destructor)) meshlink_exit(void) {
        crypto_exit();
 }
+
+int cweight_from_dclass(dclass_t dclass)
+{
+       switch(dclass)
+       {
+       case BACKBONE:
+               return 1;
+
+       case STATIONARY:
+               return 3;
+
+       case PORTABLE:
+               return 6;
+       }
+
+       return 9;
+}
+
+int max_ccount_from_dclass(dclass_t dclass)
+{
+       switch(dclass)
+       {
+       case BACKBONE:
+               return 10000;
+
+       case STATIONARY:
+               return 100;
+
+       case PORTABLE:
+               return 3;
+       }
+
+       return 3;
+}
+
+bool dclass_ccounts_satisfied(dclass_t dclass, splay_tree_t* counts, int total_count)
+{
+       // lookup keys
+       dclass_ccount_t key_portable;
+       key_portable.dclass = PORTABLE;
+
+       dclass_ccount_t key_stationary;
+       key_stationary.dclass = STATIONARY;
+
+       dclass_ccount_t key_backbone;
+       key_backbone.dclass = BACKBONE;
+
+       // check num of portable devices
+       dclass_ccount_t* portable = splay_search(counts, &key_portable);
+
+       if(portable)
+       {
+               if(portable->ccount > 9)
+                       return true;
+       }
+
+       // check num of stationary devices
+       dclass_ccount_t* stationary = splay_search(counts, &key_stationary);
+
+       if(stationary)
+       {
+               if(stationary->ccount > 6)
+                       return true;
+       }
+
+       // check num of backbone devices
+       dclass_ccount_t* backbone = splay_search(counts, &key_backbone);
+
+       if(backbone)
+       {
+               if(backbone->ccount > 3)
+                       return true;
+       }
+
+       // fallback
+       if(total_count >= max_ccount_from_dclass(dclass))
+               return true;
+
+       return false;
+}
+
+int dclass_ccount_compare(const void *a, const void *b)
+{
+       const dclass_ccount_t* da = a;
+       const dclass_ccount_t* db = b;
+
+       return da->dclass - db->dclass;
+}
+
+dclass_ccount_t* dclass_ccount_alloc()
+{
+       return xzalloc(sizeof(dclass_ccount_t));
+}
+
+void dclass_ccount_delete(void *c)
+{
+       free(c);
+}