When initializing a new MeshLink instance, fail if there is no port
available for listening. When starting an already configured instance,
if no Port statement is found in the local node's host config file,
select and add a new one.
-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);
for(int i = 0; i < 1000; i++) {
int port = 0x1000 + (rand() & 0x7fff);
FILE *f = fopen(filename, "a");
if(!f) {
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");
- 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");
if(!ecdsa_keygen(mesh)) {
meshlink_errno = MESHLINK_EINTERNAL;
if(!ecdsa_keygen(mesh)) {
meshlink_errno = MESHLINK_EINTERNAL;
+ if (check_port(mesh) == 0) {
+ meshlink_errno = MESHLINK_ENETWORK;
+ unlink(filename);
+ return false;
+ }
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 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 {
/// Device class traits
typedef struct {
read_host_config(mesh, mesh->config, name);
if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport)) {
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;
}
mesh->self->connection->options = 0;