From: Niklas Hofmann Date: Sat, 9 Aug 2014 08:21:51 +0000 (+0200) Subject: introduction of dclass and use as source for weight of edges X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=2200060c4eaadae36b8150aa33b0db7c0d74722f;p=meshlink introduction of dclass and use as source for weight of edges --- diff --git a/examples/chat.c b/examples/chat.c index cd0144e0..3eaa5c7e 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -195,7 +195,7 @@ int main(int argc, char *argv[]) { meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message); - meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat"); + meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", STATIONARY); if(!mesh) { fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; diff --git a/examples/chatpp.cc b/examples/chatpp.cc index c94cf2bb..e500ad27 100644 --- a/examples/chatpp.cc +++ b/examples/chatpp.cc @@ -187,7 +187,7 @@ int main(int argc, char *argv[]) { if(argc > 2) nick = argv[2]; - ChatMesh* mesh = meshlink::open(confbase, nick, "chatpp"); + ChatMesh* mesh = meshlink::open(confbase, nick, "chatpp", STATIONARY); if(!mesh) { fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror()); diff --git a/examples/manynodes.c b/examples/manynodes.c index fa583f60..ecc8b458 100644 --- a/examples/manynodes.c +++ b/examples/manynodes.c @@ -229,7 +229,7 @@ int main(int argc, char *argv[]) { snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i); snprintf(filename, sizeof filename, "%s/%s", basebase, nodename); bool itsnew = access(filename, R_OK); - mesh[i] = meshlink_open(filename, nodename, "manynodes"); + mesh[i] = meshlink_open(filename, nodename, "manynodes", STATIONARY); if(itsnew) meshlink_add_address(mesh[i], "localhost"); if(!mesh[i]) { diff --git a/examples/meshlinkapp.c b/examples/meshlinkapp.c index 85cdf6fe..6c56165c 100644 --- a/examples/meshlinkapp.c +++ b/examples/meshlinkapp.c @@ -14,7 +14,7 @@ int main(int argc , char **argv){ meshlink_handle_t* myhandle; - myhandle = meshlink_open(confbase, name, "meshlinkapp"); + myhandle = meshlink_open(confbase, name, "meshlinkapp", STATIONARY); //Register callback function for incoming data meshlink_set_receive_cb(myhandle, (meshlink_receive_cb_t)handle_recv_data); diff --git a/src/connection.h b/src/connection.h index 5c112175..c3c219be 100644 --- a/src/connection.h +++ b/src/connection.h @@ -64,8 +64,6 @@ typedef struct connection_t { int socket; /* socket used for this connection */ uint32_t options; /* options for this connection */ connection_status_t status; /* status info */ - int estimated_weight; /* estimation for the weight of the edge for this connection */ - struct timeval start; /* time this connection was started, used for above estimation */ struct outgoing_t *outgoing; /* used to keep track of outgoing connections */ struct meshlink_handle *mesh; /* the mesh this connection belongs to */ diff --git a/src/meshlink++.h b/src/meshlink++.h index 9500852b..8271ab2f 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -382,12 +382,13 @@ namespace meshlink { * @param confbase The directory in which MeshLink will store its configuration files. * @param name The name which this instance of the application will use in the mesh. * @param appname The application name which will be used in the mesh. + * @param dclass The device class which will be used in the mesh. * * @return This function will return a pointer to a meshlink::mesh if MeshLink has succesfully set up its configuration files, NULL otherwise. */ template - static MESH* open(const char *confbase, const char *name, const char* appname) { - void* mp = (void *)meshlink_open_with_size(confbase, name, appname, sizeof(MESH)); + static MESH* open(const char *confbase, const char *name, const char* appname, dclass_t dclass) { + void* mp = (void *)meshlink_open_with_size(confbase, name, appname, dclass, sizeof(MESH)); return new (mp) MESH; } diff --git a/src/meshlink.c b/src/meshlink.c index b8fb5055..0ecab432 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -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); @@ -1721,3 +1723,10 @@ static void __attribute__((constructor)) meshlink_init(void) { static void __attribute__((destructor)) meshlink_exit(void) { crypto_exit(); } + +int weight_from_dclass(dclass_t dclass) +{ + if(dclass == PORTABLE) + return 3; + return 1; +} diff --git a/src/meshlink.h b/src/meshlink.h index 2ae5f5bb..30c118e7 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -65,6 +65,12 @@ typedef enum { MESHLINK_EPEER, ///< A peer caused an error } meshlink_errno_t; +// Device class +typedef enum { + STATIONARY = 1, + PORTABLE = 2 +} dclass_t; + /// A variable holding the last encountered error from MeshLink. /** This is a thread local variable that contains the error code of the most recent error * encountered by a MeshLink API function called in the current thread. @@ -123,14 +129,15 @@ extern const char *meshlink_strerror(meshlink_errno_t err); * After the function returns, the application is free to overwrite or free @a name @a. * @param appname The application name which will be used in the mesh. * After the function returns, the application is free to overwrite or free @a name @a. + * @param dclass The device class which will be used in the mesh. * * @return A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error. * The pointer is valid until meshlink_close() is called. */ -extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname); +extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dclass_t dclass); /// is used by the C++ wrapper to allocate more memory behind the handle -extern meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, size_t size); +extern meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, dclass_t dclass, size_t size); /// Start MeshLink. /** This function causes MeshLink to open network sockets, make outgoing connections, and diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 73c75b89..63a71dc0 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -63,12 +63,12 @@ typedef struct outpacketqueue { /// A handle for an instance of MeshLink. struct meshlink_handle { char *name; + char *appname; + dclass_t dclass; void *priv; char *confbase; - char *appname; - meshlink_receive_cb_t receive_cb; meshlink_node_status_cb_t node_status_cb; meshlink_log_cb_t log_cb; @@ -158,5 +158,6 @@ extern void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh); extern meshlink_log_level_t global_log_level; extern meshlink_log_cb_t global_log_cb; +extern int weight_from_dclass(dclass_t dclass); #endif // MESHLINK_INTERNAL_H diff --git a/src/protocol_auth.c b/src/protocol_auth.c index 85a0fbe1..b05b04b1 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -124,8 +124,7 @@ static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) { } bool send_id(meshlink_handle_t *mesh, connection_t *c) { - gettimeofday(&c->start, NULL); - + int minor = mesh->self->connection->protocol_minor; if(mesh->proxytype && c->outgoing) @@ -353,22 +352,13 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { } bool send_ack(meshlink_handle_t *mesh, connection_t *c) { - /* ACK message contains rest of the information the other end needs - to create node_t and edge_t structures. */ - - struct timeval now; - - /* Estimate weight */ - - gettimeofday(&now, NULL); - c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000; /* Check some options */ if(mesh->self->options & OPTION_PMTU_DISCOVERY) c->options |= OPTION_PMTU_DISCOVERY; - return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, c->estimated_weight, (c->options & 0xffffff) | (PROT_MINOR << 24)); + return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, mesh->dclass, (c->options & 0xffffff) | (PROT_MINOR << 24)); } static void send_everything(meshlink_handle_t *mesh, connection_t *c) { @@ -383,11 +373,11 @@ static void send_everything(meshlink_handle_t *mesh, connection_t *c) { bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { char hisport[MAX_STRING_SIZE]; char *hisaddress; - int weight; + int dclass; uint32_t options; node_t *n; - if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) { + if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &dclass, &options) != 3) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname); return false; @@ -449,7 +439,7 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { sockaddr2str(&c->address, &hisaddress, NULL); c->edge->address = str2sockaddr(hisaddress, hisport); free(hisaddress); - c->edge->weight = (weight + c->estimated_weight) / 2; + c->edge->weight = weight_from_dclass(dclass); c->edge->connection = c; c->edge->options = c->options;