From: Guus Sliepen Date: Mon, 21 Apr 2014 20:50:03 +0000 (+0200) Subject: Move proxy variables to mesh. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=4ea5d218c5c8e5739071bc6e57cf8404db5323ef;p=meshlink Move proxy variables to mesh. --- diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index c21608fe..3c2b0423 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -25,6 +25,15 @@ #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; @@ -53,6 +62,14 @@ struct meshlink_handle { 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. diff --git a/src/net.h b/src/net.h index c87bbf36..184dd934 100644 --- a/src/net.h +++ b/src/net.h @@ -91,23 +91,7 @@ extern int listen_sockets; 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" diff --git a/src/net_setup.c b/src/net_setup.c index d966f163..c48c0d5b 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -34,15 +34,7 @@ #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)) @@ -211,7 +203,7 @@ bool setup_myself_reloadable(void) { 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, ' '); @@ -305,8 +297,8 @@ bool setup_myself(void) { 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; @@ -319,16 +311,16 @@ bool setup_myself(void) { 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 */ @@ -373,10 +365,10 @@ bool setup_myself(void) { // 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. */ @@ -436,7 +428,7 @@ void close_network_connections(void) { exit_nodes(); exit_connections(); - if(myport) free(myport); + if(mesh->myport) free(mesh->myport); return; } diff --git a/src/net_socket.c b/src/net_socket.c index 80fe84ca..bc599e8a 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -405,18 +405,18 @@ begin: 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); } @@ -431,7 +431,7 @@ begin: 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) @@ -443,9 +443,9 @@ begin: /* 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); diff --git a/src/protocol.c b/src/protocol.c index eb406544..4f762825 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -22,6 +22,7 @@ #include "conf.h" #include "connection.h" #include "logger.h" +#include "meshlink_internal.h" #include "meta.h" #include "protocol.h" #include "utils.h" @@ -106,7 +107,7 @@ void forward_request(connection_t *from, const char *request) { } 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)) { diff --git a/src/protocol_auth.c b/src/protocol_auth.c index 8b419bad..a990caff 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -43,7 +43,7 @@ ecdsa_t *invitation_key = NULL; static bool send_proxyrequest(connection_t *c) { - switch(proxytype) { + switch(mesh->proxytype) { case PROXY_HTTP: { char *host; char *port; @@ -59,13 +59,13 @@ static bool send_proxyrequest(connection_t *c) { 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); @@ -73,21 +73,21 @@ static bool send_proxyrequest(connection_t *c) { 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; @@ -133,7 +133,7 @@ bool send_id(connection_t *c) { int minor = mesh->self->connection->protocol_minor; - if(proxytype && c->outgoing) + if(mesh->proxytype && c->outgoing) if(!send_proxyrequest(c)) return false; @@ -376,24 +376,12 @@ bool send_ack(connection_t *c) { 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);