#include "ecdsa.h"
#include "graph.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "net.h"
#include "netutl.h"
#include "protocol.h"
#include "utils.h"
#include "xalloc.h"
-char *myport;
-
-char *proxyhost;
-char *proxyport;
-char *proxyuser;
-char *proxypass;
-proxytype_t proxytype;
-int autoconnect;
-bool disablebuggypeers;
+int autoconnect = 3;
bool node_read_ecdsa_public_key(node_t *n) {
if(ecdsa_active(n->ecdsa))
FILE *fp;
char *fname;
- xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
+ xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
fp = fopen(fname, "r");
free(fname);
return false;
}
- myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
+ mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
fclose(fp);
- if(!myself->connection->ecdsa)
+ if(!mesh->self->connection->ecdsa)
logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
- return myself->connection->ecdsa;
+ return mesh->self->connection->ecdsa;
}
static bool read_invitation_key(void) {
FILE *fp;
char *fname;
- if(invitation_key) {
- ecdsa_free(invitation_key);
- invitation_key = NULL;
+ if(mesh->invitation_key) {
+ ecdsa_free(mesh->invitation_key);
+ mesh->invitation_key = NULL;
}
- xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
+ xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
fp = fopen(fname, "r");
if(fp) {
- invitation_key = ecdsa_read_pem_private_key(fp);
+ mesh->invitation_key = ecdsa_read_pem_private_key(fp);
fclose(fp);
- if(!invitation_key)
+ if(!mesh->invitation_key)
logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
}
free(fname);
- return invitation_key;
-}
-
-static timeout_t keyexpire_timeout;
-
-static void keyexpire_handler(void *data) {
- regenerate_key();
- timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
-}
-
-void regenerate_key(void) {
- logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
- send_key_changed();
+ return mesh->invitation_key;
}
void load_all_nodes(void) {
struct dirent *ent;
char *dname;
- xasprintf(&dname, "%s" SLASH "hosts", confbase);
+ xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase);
dir = opendir(dname);
if(!dir) {
logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
char *get_name(void) {
char *name = NULL;
- get_config_string(lookup_config(config_tree, "Name"), &name);
+ get_config_string(lookup_config(mesh->config, "Name"), &name);
if(!name)
return NULL;
if(!check_id(name)) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
+ logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for mesh->self!");
free(name);
return NULL;
}
}
bool setup_myself_reloadable(void) {
- localdiscovery = true;
- macexpire = 600; // TODO: remove
+ mesh->localdiscovery = true;
keylifetime = 3600; // TODO: check if this can be removed as well
maxtimeout = 900;
autoconnect = 3;
- myself->options |= OPTION_PMTU_DISCOVERY;
+ mesh->self->options |= OPTION_PMTU_DISCOVERY;
read_invitation_key();
Add listening sockets.
*/
static bool add_listen_address(char *address, bool bindto) {
- char *port = myport;
+ char *port = mesh->myport;
if(address) {
char *space = strchr(address, ' ');
// Ignore duplicate addresses
bool found = false;
- for(int i = 0; i < listen_sockets; i++)
- if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
+ for(int i = 0; i < mesh->listen_sockets; i++)
+ if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
found = true;
break;
}
if(found)
continue;
- if(listen_sockets >= MAXSOCKETS) {
+ if(mesh->listen_sockets >= MAXSOCKETS) {
logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
return false;
}
continue;
}
- io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
- io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
+ io_add(&mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
+ io_add(&mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
if(debug_level >= DEBUG_CONNECTIONS) {
char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
free(hostname);
}
- listen_socket[listen_sockets].bindto = bindto;
- memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
- listen_sockets++;
+ mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
+ memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+ mesh->listen_sockets++;
}
freeaddrinfo(ai);
}
/*
- Configure node_t myself and set up the local sockets (listen only)
+ Configure node_t mesh->self and set up the local sockets (listen only)
*/
bool setup_myself(void) {
char *name, *hostname, *cipher, *digest, *type;
return false;
}
- myself = new_node();
- myself->connection = new_connection();
- myself->name = name;
- myself->connection->name = xstrdup(name);
- read_host_config(config_tree, name);
+ mesh->self = new_node();
+ mesh->self->connection = new_connection();
+ mesh->self->name = name;
+ mesh->self->connection->name = xstrdup(name);
+ read_host_config(mesh->config, name);
- if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
- myport = xstrdup("655");
+ if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
+ mesh->myport = xstrdup("655");
else
port_specified = true;
- myself->connection->options = 0;
- myself->connection->protocol_major = PROT_MAJOR;
- myself->connection->protocol_minor = PROT_MINOR;
+ mesh->self->connection->options = 0;
+ mesh->self->connection->protocol_major = PROT_MAJOR;
+ mesh->self->connection->protocol_minor = PROT_MINOR;
- myself->options |= PROT_MINOR << 24;
+ mesh->self->options |= PROT_MINOR << 24;
if(!read_ecdsa_private_key())
return false;
- /* Ensure myport is numeric */
+ /* Ensure mesh->myport is numeric */
- if(!atoi(myport)) {
- struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
+ if(!atoi(mesh->myport)) {
+ struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
sockaddr_t sa;
if(!ai || !ai->ai_addr)
return false;
- free(myport);
+ free(mesh->myport);
memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
- sockaddr2str(&sa, NULL, &myport);
+ sockaddr2str(&sa, NULL, &mesh->myport);
}
/* Check some options */
if(!setup_myself_reloadable())
return false;
- // TODO: check whether this is used at all
- timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
-
/* Compression */
// TODO: drop compression in the packet layer?
- myself->incompression = 0;
- myself->connection->outcompression = 0;
+ mesh->self->incompression = 0;
+ mesh->self->connection->outcompression = 0;
/* Done */
- myself->nexthop = myself;
- myself->via = myself;
- myself->status.reachable = true;
- myself->last_state_change = now.tv_sec;
- node_add(myself);
+ mesh->self->nexthop = mesh->self;
+ mesh->self->via = mesh->self;
+ mesh->self->status.reachable = true;
+ mesh->self->last_state_change = now.tv_sec;
+ node_add(mesh->self);
graph();
/* Open sockets */
- listen_sockets = 0;
+ mesh->listen_sockets = 0;
int cfgs = 0;
if(!add_listen_address(address, NULL))
return false;
- if(!listen_sockets) {
+ if(!mesh->listen_sockets) {
logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
return false;
}
// TODO: require Port to be set? Or use "0" and use getsockname()?
- if(!myport)
- myport = xstrdup("655");
+ if(!mesh->myport)
+ mesh->myport = xstrdup("655");
- xasprintf(&myself->hostname, "MYSELF port %s", myport);
- myself->connection->hostname = xstrdup(myself->hostname);
+ xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
+ mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
/* Done. */
- last_config_check = now.tv_sec;
+ mesh->last_config_check = now.tv_sec;
return true;
}
close all open network connections
*/
void close_network_connections(void) {
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
+ for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
next = node->next;
connection_t *c = node->data;
c->outgoing = NULL;
terminate_connection(c, false);
}
- if(outgoing_list)
- list_delete_list(outgoing_list);
+ if(mesh->outgoings)
+ list_delete_list(mesh->outgoings);
- if(myself && myself->connection) {
- terminate_connection(myself->connection, false);
- free_connection(myself->connection);
+ if(mesh->self && mesh->self->connection) {
+ terminate_connection(mesh->self->connection, false);
+ free_connection(mesh->self->connection);
}
- for(int i = 0; i < listen_sockets; i++) {
- io_del(&listen_socket[i].tcp);
- io_del(&listen_socket[i].udp);
- close(listen_socket[i].tcp.fd);
- close(listen_socket[i].udp.fd);
+ for(int i = 0; i < mesh->listen_sockets; i++) {
+ io_del(&mesh->listen_socket[i].tcp);
+ io_del(&mesh->listen_socket[i].udp);
+ close(mesh->listen_socket[i].tcp.fd);
+ close(mesh->listen_socket[i].udp.fd);
}
exit_requests();
exit_nodes();
exit_connections();
- if(myport) free(myport);
+ if(mesh->myport) free(mesh->myport);
return;
}