return true;
}
+
meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
+ return meshlink_open_with_size(confbase, name, sizeof(meshlink_handle_t) );
+}
+
+
+meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, size_t size) {
// Validate arguments provided by the application
bool usingname = false;
} else { usingname = true;}
}
- meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
+ meshlink_handle_t *mesh = xzalloc(size);
mesh->confbase = xstrdup(confbase);
if (usingname) mesh->name = xstrdup(name);
- pthread_mutex_init ( &(mesh->outpacketqueue_mutex), NULL);
pthread_mutex_init ( &(mesh->nodes_mutex), NULL);
mesh->threadstarted = false;
event_loop_init(&mesh->loop);
try_outgoing_connections(mesh);
+ fprintf(stderr, "Starting main_loop...\n");
main_loop(mesh);
+ fprintf(stderr, "main_loop returned.\n");
return NULL;
}
return;
}
- // Shut down the listening sockets to signal the main thread to shut down
+ listen_socket_t *s = &mesh->listen_socket[0];
- for(int i = 0; i < mesh->listen_sockets; i++) {
- shutdown(mesh->listen_socket[i].tcp.fd, SHUT_RDWR);
- shutdown(mesh->listen_socket[i].udp.fd, SHUT_RDWR);
- }
+ // Shut down a listening socket to signal the main thread to shut down
+
+ shutdown(s->tcp.fd, SHUT_RDWR);
// Wait for the main thread to finish
pthread_join(mesh->thread, NULL);
+ mesh->threadstarted = false;
+
+ // Fix the socket
+
+ closesocket(s->tcp.fd);
+ io_del(&mesh->loop, &s->tcp);
+ s->tcp.fd = setup_listen_socket(&s->sa);
+ if(s->tcp.fd < 0)
+ logger(DEBUG_ALWAYS, LOG_ERR, "Could not repair listenen socket!");
+ else
+ io_add(&mesh->loop, &s->tcp, handle_new_meta_connection, s, s->tcp.fd, IO_READ);
}
void meshlink_close(meshlink_handle_t *mesh) {
return false;
}
- /* If there is no outgoing list yet, create one. */
-
- if(!mesh->outpacketqueue)
- mesh->outpacketqueue = list_alloc(NULL);
-
//add packet to the queue
outpacketqueue_t *packet_in_queue = xzalloc(sizeof *packet_in_queue);
packet_in_queue->destination=destination;
packet_in_queue->data=data;
packet_in_queue->len=len;
- pthread_mutex_lock(&(mesh->outpacketqueue_mutex));
- list_insert_head(mesh->outpacketqueue,packet_in_queue);
- pthread_mutex_unlock(&(mesh->outpacketqueue_mutex));
+ if(!meshlink_queue_push(&mesh->outpacketqueue, packet_in_queue)) {
+ free(packet_in_queue);
+ return false;
+ }
//notify event loop
signal_trigger(&(mesh->loop),&(mesh->datafromapp));
vpn_packet_t packet;
meshlink_packethdr_t *hdr = (meshlink_packethdr_t *)packet.data;
- outpacketqueue_t* p = list_get_tail(mesh->outpacketqueue);
- if (p)
- list_delete_tail(mesh->outpacketqueue);
- else return ;
+ outpacketqueue_t* p = meshlink_queue_pop(&mesh->outpacketqueue);
+ if(!p)
+ return;
if (sizeof(meshlink_packethdr_t) + p->len > MAXSIZE) {
//log something