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;
if(argc > 2)
nick = argv[2];
- ChatMesh* mesh = meshlink::open<ChatMesh>(confbase, nick, "chatpp");
+ ChatMesh* mesh = meshlink::open<ChatMesh>(confbase, nick, "chatpp", STATIONARY);
if(!mesh) {
fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
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]) {
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);
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 */
* @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<class MESH>
- 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;
}
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;
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;
ecdsa_free(mesh->invitation_key);
free(mesh->name);
+ free(mesh->appname);
free(mesh->confbase);
memset(mesh, 0, sizeof *mesh);
static void __attribute__((destructor)) meshlink_exit(void) {
crypto_exit();
}
+
+int weight_from_dclass(dclass_t dclass)
+{
+ if(dclass == PORTABLE)
+ return 3;
+ return 1;
+}
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.
* 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
/// 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;
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
}
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)
}
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) {
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;
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;