#include "graph.h"
#include "list.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "netutl.h"
#include "node.h"
#include "protocol.h"
n->distance = -1;
}
- /* Begin with myself */
+ /* Begin with mesh->self */
- myself->status.visited = true;
- myself->status.indirect = false;
- myself->nexthop = myself;
- myself->prevedge = NULL;
- myself->via = myself;
- myself->distance = 0;
- list_insert_head(todo_list, myself);
+ mesh->self->status.visited = true;
+ mesh->self->status.indirect = false;
+ mesh->self->nexthop = mesh->self;
+ mesh->self->prevedge = NULL;
+ mesh->self->via = mesh->self;
+ mesh->self->distance = 0;
+ list_insert_head(todo_list, mesh->self);
/* Loop while todo_list is filled */
e->to->status.visited = true;
e->to->status.indirect = indirect;
- e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
+ e->to->nexthop = (n->nexthop == mesh->self) ? e->to : n->nexthop;
e->to->prevedge = e;
e->to->via = indirect ? n->via : e->to;
e->to->options = e->options;
packet.probe = false;
memset(hdr, 0, sizeof *hdr);
memcpy(hdr->destination,receiver->name,sizeof(hdr->destination));
- memcpy(hdr->source,myself->name,sizeof(hdr->source));
+ memcpy(hdr->source,mesh->self->name,sizeof(hdr->source));
packet.len = sizeof(tincpackethdr) + len;
memcpy(packet.data+sizeof(tincpackethdr),buf,len);
- myself->in_packets++;
- myself->in_bytes += packet.len;
- route(myself, &packet);
+ mesh->self->in_packets++;
+ mesh->self->in_bytes += packet.len;
+ route(mesh->self, &packet);
return true;
}
#include "connection.h"
#include "graph.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "meta.h"
#include "net.h"
#include "netutl.h"
if(report && !c->node->status.reachable) {
edge_t *e;
- e = lookup_edge(c->node, myself);
+ e = lookup_edge(c->node, mesh->self);
if(e) {
send_del_edge(everyone, e);
edge_del(e);
return EINVAL;
}
- xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
+ xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, mesh->self->name);
read_config_file(config_tree, fname);
free(fname);
#include "digest.h"
#include "graph.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "net.h"
#include "netutl.h"
#include "protocol.h"
/* Send it via TCP if it is a handshake packet, TCPOnly is in use, or this packet is larger than the MTU. */
- if(type >= SPTPS_HANDSHAKE || ((myself->options | to->options) & OPTION_TCPONLY) || (type != PKT_PROBE && len > to->minmtu)) {
+ if(type >= SPTPS_HANDSHAKE || ((mesh->self->options | to->options) & OPTION_TCPONLY) || (type != PKT_PROBE && len > to->minmtu)) {
char buf[len * 4 / 3 + 5];
b64encode(data, buf, len);
/* If no valid key is known yet, send the packets using ANS_KEY requests,
to ensure we get to learn the reflexive UDP address. */
if(!to->status.validkey) {
- to->incompression = myself->incompression;
- return send_request(to->nexthop->connection, "%d %s %s %s -1 -1 -1 %d", ANS_KEY, myself->name, to->name, buf, to->incompression);
+ to->incompression = mesh->self->incompression;
+ return send_request(to->nexthop->connection, "%d %s %s %s -1 -1 -1 %d", ANS_KEY, mesh->self->name, to->name, buf, to->incompression);
} else {
- return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, to->name, REQ_SPTPS, buf);
+ return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_SPTPS, buf);
}
}
void send_packet(node_t *n, vpn_packet_t *packet) {
node_t *via;
- if(n == myself) {
+ if(n == mesh->self) {
n->out_packets++;
n->out_bytes += packet->len;
// TODO: send to application
void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
// Always give ourself a copy of the packet.
- if(from != myself)
- send_packet(myself, packet);
+ if(from != mesh->self)
+ send_packet(mesh->self, packet);
logger(DEBUG_TRAFFIC, LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
packet->len, from->name, from->hostname);
static time_t last_hard_try = 0;
for splay_each(edge_t, e, edge_weight_tree) {
- if(!e->to->status.reachable || e->to == myself)
+ if(!e->to->status.reachable || e->to == mesh->self)
continue;
if(sockaddrcmp_noport(from, &e->address)) {
#include "ecdsa.h"
#include "graph.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "net.h"
#include "netutl.h"
#include "protocol.h"
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) {
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;
}
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();
}
/*
- 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);
+ mesh->self = new_node();
+ mesh->self->connection = new_connection();
+ mesh->self->name = name;
+ mesh->self->connection->name = xstrdup(name);
read_host_config(config_tree, name);
if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
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;
/* 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();
if(!myport)
myport = xstrdup("655");
- xasprintf(&myself->hostname, "MYSELF port %s", myport);
- myself->connection->hostname = xstrdup(myself->hostname);
+ xasprintf(&mesh->self->hostname, "MYSELF port %s", myport);
+ mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
/* Done. */
if(outgoing_list)
list_delete_list(outgoing_list);
- 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++) {
#include "connection.h"
#include "list.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "meta.h"
#include "net.h"
#include "netutl.h"
#endif
#if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
- if(myself->options & OPTION_PMTU_DISCOVERY) {
+ if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = IP_PMTUDISC_DO;
setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
}
#elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT)
- if(myself->options & OPTION_PMTU_DISCOVERY) {
+ if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = 1;
setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
}
#endif
#if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
- if(myself->options & OPTION_PMTU_DISCOVERY) {
+ if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = IPV6_PMTUDISC_DO;
setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
}
#elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG)
- if(myself->options & OPTION_PMTU_DISCOVERY) {
+ if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
option = 1;
setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
}
setenv("REMOTEADDRESS", host, true);
setenv("REMOTEPORT", port, true);
setenv("NODE", c->name, true);
- setenv("NAME", myself->name, true);
+ setenv("NAME", mesh->self->name, true);
int result = system(command);
if(result < 0)
c->status.connecting = true;
c->name = xstrdup(outgoing->name);
- c->outcompression = myself->connection->outcompression;
+ c->outcompression = mesh->self->connection->outcompression;
c->last_ping_time = now.tv_sec;
connection_add(c);
c = new_connection();
c->name = xstrdup("<unknown>");
- c->outcompression = myself->connection->outcompression;
+ c->outcompression = mesh->self->connection->outcompression;
c->address = sa;
c->hostname = sockaddr2hostname(&sa);
#include "hash.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "net.h"
#include "netutl.h"
#include "node.h"
splay_tree_t *node_tree;
static hash_t *node_udp_cache;
-node_t *myself;
-
static int node_compare(const node_t *a, const node_t *b) {
return strcmp(a->name, b->name);
}
}
void update_node_udp(node_t *n, const sockaddr_t *sa) {
- if(n == myself) {
- logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of myself!");
+ if(n == mesh->self) {
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of mesh->self!");
return;
}
uint64_t out_bytes;
} node_t;
-extern struct node_t *myself;
extern struct splay_tree_t *node_tree;
extern void init_nodes(void);
#include "edge.h"
#include "graph.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "meta.h"
#include "net.h"
#include "netutl.h"
bool send_id(connection_t *c) {
gettimeofday(&c->start, NULL);
- int minor = myself->connection->protocol_minor;
+ int minor = mesh->self->connection->protocol_minor;
if(proxytype && c->outgoing)
if(!send_proxyrequest(c))
return false;
- return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
+ return send_request(c, "%d %s %d.%d", ID, mesh->self->connection->name, mesh->self->connection->protocol_major, minor);
}
static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
/* Check if version matches */
- if(c->protocol_major != myself->connection->protocol_major) {
+ if(c->protocol_major != mesh->self->connection->protocol_major) {
logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
c->name, c->hostname, c->protocol_major, c->protocol_minor);
return false;
}
c->allow_request = ACK;
- char label[25 + strlen(myself->name) + strlen(c->name)];
+ char label[25 + strlen(mesh->self->name) + strlen(c->name)];
if(c->outgoing)
- snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
+ snprintf(label, sizeof label, "tinc TCP key expansion %s %s", mesh->self->name, c->name);
else
- snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
+ snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, mesh->self->name);
- return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
+ return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
}
bool send_ack(connection_t *c) {
/* Check some options */
- if(myself->options & OPTION_PMTU_DISCOVERY)
+ if(mesh->self->options & OPTION_PMTU_DISCOVERY)
c->options |= OPTION_PMTU_DISCOVERY;
return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (PROT_MINOR << 24));
/* Create an edge_t for this connection */
c->edge = new_edge();
- c->edge->from = myself;
+ c->edge->from = mesh->self;
c->edge->to = n;
sockaddr2str(&c->address, &hisaddress, NULL);
c->edge->address = str2sockaddr(hisaddress, hisport);
#include "edge.h"
#include "graph.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "meta.h"
#include "net.h"
#include "netutl.h"
if(e) {
if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
- if(from == myself) {
+ if(from == mesh->self) {
logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
"ADD_EDGE", c->name, c->hostname);
send_add_edge(c, e);
}
} else
return true;
- } else if(from == myself) {
+ } else if(from == mesh->self) {
logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
"ADD_EDGE", c->name, c->hostname);
contradicting_add_edge++;
return true;
}
- if(e->from == myself) {
+ if(e->from == mesh->self) {
logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
"DEL_EDGE", c->name, c->hostname);
contradicting_del_edge++;
/* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
if(!to->status.reachable) {
- e = lookup_edge(to, myself);
+ e = lookup_edge(to, mesh->self);
if(e) {
send_del_edge(everyone, e);
edge_del(e);
#include "connection.h"
#include "crypto.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "net.h"
#include "netutl.h"
#include "node.h"
static bool mykeyused = false;
void send_key_changed(void) {
- send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), myself->name);
+ send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), mesh->self->name);
/* Force key exchange for connections using SPTPS */
to->sptps.send_data = send_sptps_data;
char buf[len * 4 / 3 + 5];
b64encode(data, buf, len);
- return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, to->name, REQ_KEY, buf);
+ return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
}
bool send_req_key(node_t *to) {
if(!node_read_ecdsa_public_key(to)) {
logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", to->name, to->hostname);
- send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, to->name, REQ_PUBKEY);
+ send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY);
return true;
}
if(to->sptps.label)
logger(DEBUG_ALWAYS, LOG_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
- char label[25 + strlen(myself->name) + strlen(to->name)];
- snprintf(label, sizeof label, "tinc UDP key expansion %s %s", myself->name, to->name);
+ char label[25 + strlen(mesh->self->name) + strlen(to->name)];
+ snprintf(label, sizeof label, "tinc UDP key expansion %s %s", mesh->self->name, to->name);
sptps_stop(&to->sptps);
to->status.validkey = false;
to->status.waitingforkey = true;
to->last_req_key = now.tv_sec;
- to->incompression = myself->incompression;
- return sptps_start(&to->sptps, to, true, true, myself->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
+ to->incompression = mesh->self->incompression;
+ return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
}
/* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, int reqno) {
switch(reqno) {
case REQ_PUBKEY: {
- char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
- send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, from->name, ANS_PUBKEY, pubkey);
+ char *pubkey = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa);
+ send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
free(pubkey);
return true;
}
case REQ_KEY: {
if(!node_read_ecdsa_public_key(from)) {
logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", from->name, from->hostname);
- send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, from->name, REQ_PUBKEY);
+ send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
return true;
}
return true;
}
- char label[25 + strlen(from->name) + strlen(myself->name)];
- snprintf(label, sizeof label, "tinc UDP key expansion %s %s", from->name, myself->name);
+ char label[25 + strlen(from->name) + strlen(mesh->self->name)];
+ snprintf(label, sizeof label, "tinc UDP key expansion %s %s", from->name, mesh->self->name);
sptps_stop(&from->sptps);
from->status.validkey = false;
from->status.waitingforkey = true;
from->last_req_key = now.tv_sec;
- sptps_start(&from->sptps, from, false, true, myself->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
+ sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
sptps_receive_data(&from->sptps, buf, len);
return true;
}
/* Check if this key request is for us */
- if(to == myself) { /* Yes */
+ if(to == mesh->self) { /* Yes */
/* Is this an extended REQ_KEY message? */
if(reqno)
return req_key_ext_h(c, request, from, reqno);
/* Forward it if necessary */
- if(to != myself) {
+ if(to != mesh->self) {
if(!to->status.reachable) {
logger(DEBUG_ALWAYS, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
"ANS_KEY", c->name, c->hostname, to_name);
#include "system.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "net.h"
#include "route.h"
#include "utils.h"
return;
}
- if (owner == myself ) {
+ if (owner == mesh->self ) {
//TODO: implement sending received data from meshlink library to the application
logger(DEBUG_TRAFFIC, LOG_WARNING, "I received a packet for me with payload: %s \n", packet->data + sizeof *hdr);
(recv_callback)(packet->data + sizeof *hdr);
return;
}
- via = (owner->via == myself) ? owner->nexthop : owner->via;
+ via = (owner->via == mesh->self) ? owner->nexthop : owner->via;
if(via == source) {
logger(DEBUG_TRAFFIC, LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
return;