return true;
}
-static int check_port(meshlink_handle_t *mesh) {
+int check_port(meshlink_handle_t *mesh) {
for(int i = 0; i < 1000; i++) {
int port = 0x1000 + (rand() & 0x7fff);
FILE *f = fopen(filename, "a");
if(!f) {
- logger(mesh, MESHLINK_DEBUG, "Please change MeshLink's Port manually.\n");
+ meshlink_errno = MESHLINK_ESTORAGE;
+ logger(mesh, MESHLINK_DEBUG, "Could not store Port.\n");
return 0;
}
}
}
- logger(mesh, MESHLINK_DEBUG, "Please change MeshLink's Port manually.\n");
+ meshlink_errno = MESHLINK_ENETWORK;
+ logger(mesh, MESHLINK_DEBUG, "Could not find any available network port.\n");
return 0;
}
if(!ecdsa_keygen(mesh)) {
meshlink_errno = MESHLINK_EINTERNAL;
+ unlink(filename);
return false;
}
- check_port(mesh);
+ if (check_port(mesh) == 0) {
+ meshlink_errno = MESHLINK_ENETWORK;
+ unlink(filename);
+ return false;
+ }
return true;
}
extern void update_node_status(meshlink_handle_t *mesh, struct node_t *n);
extern meshlink_log_level_t global_log_level;
extern meshlink_log_cb_t global_log_cb;
+extern int check_port(meshlink_handle_t *mesh);
/// Device class traits
typedef struct {
read_host_config(mesh, mesh->config, name);
if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport)) {
- logger(mesh, MESHLINK_ERROR, "Port for MeshLink instance required!");
- return false;
+ int port = check_port(mesh);
+ if (port == 0)
+ return false;
+ xasprintf(&mesh->myport, "%d", port);
}
mesh->self->connection->options = 0;