#include "event.h"
#include "meshlink.h"
+typedef enum proxytype_t {
+ PROXY_NONE = 0,
+ PROXY_SOCKS4,
+ PROXY_SOCKS4A,
+ PROXY_SOCKS5,
+ PROXY_HTTP,
+ PROXY_EXEC,
+} proxytype_t;
+
/// A handle for an instance of MeshLink.
struct meshlink_handle {
char *confbase;
time_t last_config_check;
timeout_t pingtimer;
timeout_t periodictimer;
+
+ char *myport;
+
+ char *proxyhost;
+ char *proxyport;
+ char *proxyuser;
+ char *proxypass;
+ proxytype_t proxytype;
};
/// A handle for a MeshLink node.
extern int keylifetime;
extern int max_connection_burst;
extern bool do_prune;
-extern char *myport;
extern int autoconnect;
-extern bool disablebuggypeers;
-
-extern char *proxyhost;
-extern char *proxyport;
-extern char *proxyuser;
-extern char *proxypass;
-typedef enum proxytype_t {
- PROXY_NONE = 0,
- PROXY_SOCKS4,
- PROXY_SOCKS4A,
- PROXY_SOCKS5,
- PROXY_HTTP,
- PROXY_EXEC,
-} proxytype_t;
-extern proxytype_t proxytype;
/* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */
#include "connection.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))
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, ' ');
mesh->self->connection->name = xstrdup(name);
read_host_config(mesh->config, name);
- if(!get_config_string(lookup_config(mesh->config, "Port"), &myport))
- myport = xstrdup("655");
+ if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
+ mesh->myport = xstrdup("655");
else
port_specified = true;
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 */
// 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(&mesh->self->hostname, "MYSELF port %s", myport);
+ xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
/* Done. */
exit_nodes();
exit_connections();
- if(myport) free(myport);
+ if(mesh->myport) free(mesh->myport);
return;
}
logger(DEBUG_CONNECTIONS, LOG_INFO, "Trying to connect to %s (%s)", outgoing->name, c->hostname);
- if(!proxytype) {
+ if(!mesh->proxytype) {
c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
configure_tcp(c);
- } else if(proxytype == PROXY_EXEC) {
- do_outgoing_pipe(c, proxyhost);
+ } else if(mesh->proxytype == PROXY_EXEC) {
+ do_outgoing_pipe(c, mesh->proxyhost);
} else {
- proxyai = str2addrinfo(proxyhost, proxyport, SOCK_STREAM);
+ proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
if(!proxyai) {
free_connection(c);
goto begin;
}
- logger(DEBUG_CONNECTIONS, LOG_INFO, "Using proxy at %s port %s", proxyhost, proxyport);
+ logger(DEBUG_CONNECTIONS, LOG_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
configure_tcp(c);
}
fcntl(c->socket, F_SETFD, FD_CLOEXEC);
#endif
- if(proxytype != PROXY_EXEC) {
+ if(mesh->proxytype != PROXY_EXEC) {
#if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
int option = 1;
if(c->address.sa.sa_family == AF_INET6)
/* Connect */
- if(!proxytype) {
+ if(!mesh->proxytype) {
result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
- } else if(proxytype == PROXY_EXEC) {
+ } else if(mesh->proxytype == PROXY_EXEC) {
result = 0;
} else {
result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
#include "conf.h"
#include "connection.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "meta.h"
#include "protocol.h"
#include "utils.h"
}
bool receive_request(connection_t *c, const char *request) {
- if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
+ if(c->outgoing && mesh->proxytype == PROXY_HTTP && c->allow_request == ID) {
if(!request[0] || request[0] == '\r')
return true;
if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
ecdsa_t *invitation_key = NULL;
static bool send_proxyrequest(connection_t *c) {
- switch(proxytype) {
+ switch(mesh->proxytype) {
case PROXY_HTTP: {
char *host;
char *port;
logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
return false;
}
- char s4req[9 + (proxyuser ? strlen(proxyuser) : 0)];
+ char s4req[9 + (mesh->proxyuser ? strlen(mesh->proxyuser) : 0)];
s4req[0] = 4;
s4req[1] = 1;
memcpy(s4req + 2, &c->address.in.sin_port, 2);
memcpy(s4req + 4, &c->address.in.sin_addr, 4);
- if(proxyuser)
- memcpy(s4req + 8, proxyuser, strlen(proxyuser));
+ if(mesh->proxyuser)
+ memcpy(s4req + 8, mesh->proxyuser, strlen(mesh->proxyuser));
s4req[sizeof s4req - 1] = 0;
c->tcplen = 8;
return send_meta(c, s4req, sizeof s4req);
case PROXY_SOCKS5: {
int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
c->tcplen = 2;
- if(proxypass)
- len += 3 + strlen(proxyuser) + strlen(proxypass);
+ if(mesh->proxypass)
+ len += 3 + strlen(mesh->proxyuser) + strlen(mesh->proxypass);
char s5req[len];
int i = 0;
s5req[i++] = 5;
s5req[i++] = 1;
- if(proxypass) {
+ if(mesh->proxypass) {
s5req[i++] = 2;
s5req[i++] = 1;
- s5req[i++] = strlen(proxyuser);
- memcpy(s5req + i, proxyuser, strlen(proxyuser));
- i += strlen(proxyuser);
- s5req[i++] = strlen(proxypass);
- memcpy(s5req + i, proxypass, strlen(proxypass));
- i += strlen(proxypass);
+ s5req[i++] = strlen(mesh->proxyuser);
+ memcpy(s5req + i, mesh->proxyuser, strlen(mesh->proxyuser));
+ i += strlen(mesh->proxyuser);
+ s5req[i++] = strlen(mesh->proxypass);
+ memcpy(s5req + i, mesh->proxypass, strlen(mesh->proxypass));
+ i += strlen(mesh->proxypass);
c->tcplen += 2;
} else {
s5req[i++] = 0;
int minor = mesh->self->connection->protocol_minor;
- if(proxytype && c->outgoing)
+ if(mesh->proxytype && c->outgoing)
if(!send_proxyrequest(c))
return false;
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));
+ return send_request(c, "%d %s %d %x", ACK, mesh->myport, c->estimated_weight, (c->options & 0xffffff) | (PROT_MINOR << 24));
}
static void send_everything(connection_t *c) {
/* Send all known subnets and edges */
- // TODO: remove this
- if(disablebuggypeers) {
- static struct {
- vpn_packet_t pkt;
- char pad[MAXBUFSIZE - MAXSIZE];
- } zeropkt;
-
- memset(&zeropkt, 0, sizeof zeropkt);
- zeropkt.pkt.len = MAXBUFSIZE;
- send_tcppacket(c, &zeropkt.pkt);
- }
-
for splay_each(node_t, n, mesh->nodes) {
for splay_each(edge_t, e, n->edge_tree)
send_add_edge(c, e);