sockaddr_t broadcast_sa;
} listen_socket_t;
-typedef enum proxytype_t {
- PROXY_NONE = 0,
- PROXY_SOCKS4,
- PROXY_SOCKS4A,
- PROXY_SOCKS5,
- PROXY_HTTP,
-} proxytype_t;
-
struct meshlink_open_params {
char *confbase;
char *appname;
meshlink_queue_t adns_queue;
meshlink_queue_t adns_done_queue;
signal_t adns_signal;
-
- // Proxy configuration, currently not exposed.
- char *proxyhost;
- char *proxyport;
- char *proxyuser;
- char *proxypass;
- proxytype_t proxytype;
};
/// A handle for a MeshLink node.
if(outgoing) {
do_outgoing_connection(mesh, outgoing);
}
-
-#ifndef HAVE_MINGW
- /* Clean up dead proxy processes */
-
- while(waitpid(-1, NULL, WNOHANG) > 0);
-
-#endif
}
/*
}
void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
- struct addrinfo *proxyai = NULL;
- int result;
-
begin:
if(!get_next_outgoing_address(mesh, outgoing)) {
memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
- char *hostname = sockaddr2hostname(&c->address);
-
- logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
+ if(mesh->log_level <= MESHLINK_INFO) {
+ char *hostname = sockaddr2hostname(&c->address);
+ logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
+ free(hostname);
+ }
- if(!mesh->proxytype) {
- c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
- configure_tcp(c);
- } else {
- proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
+ c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
- if(!proxyai) {
- free_connection(c);
+ if(c->socket == -1) {
+ if(mesh->log_level <= MESHLINK_ERROR) {
+ char *hostname = sockaddr2hostname(&c->address);
+ logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
free(hostname);
- goto begin;
}
- logger(mesh, MESHLINK_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
- c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
- configure_tcp(c);
- }
-
- if(c->socket == -1) {
- logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
free_connection(c);
- free(hostname);
goto begin;
}
- free(hostname);
+ configure_tcp(c);
#ifdef FD_CLOEXEC
fcntl(c->socket, F_SETFD, FD_CLOEXEC);
/* Connect */
- if(!mesh->proxytype) {
- result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
- } else {
- result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
- freeaddrinfo(proxyai);
- }
+ int result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
if(result == -1 && !sockinprogress(sockerrno)) {
- logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
- free_connection(c);
+ if(mesh->log_level <= MESHLINK_ERROR) {
+ char *hostname = sockaddr2hostname(&c->address);
+ logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
+ free(hostname);
+ }
+ free_connection(c);
goto begin;
}
bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) {
assert(request);
- 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)) {
- if(!strncmp(request + 9, "200", 3)) {
- logger(mesh, MESHLINK_DEBUG, "Proxy request granted");
- return true;
- } else {
- logger(mesh, MESHLINK_DEBUG, "Proxy request rejected: %s", request + 9);
- return false;
- }
- }
- }
-
int reqno = atoi(request);
if(reqno || *request == '0') {
extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
-static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) {
- switch(mesh->proxytype) {
- case PROXY_HTTP: {
- char *host;
- char *port;
-
- sockaddr2str(&c->address, &host, &port);
- send_request(mesh, c, NULL, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
- free(host);
- free(port);
- return true;
- }
-
- case PROXY_SOCKS4: {
- if(c->address.sa.sa_family != AF_INET) {
- logger(mesh, MESHLINK_ERROR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
- return false;
- }
-
- 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(mesh->proxyuser) {
- memcpy(s4req + 8, mesh->proxyuser, strlen(mesh->proxyuser));
- }
-
- s4req[sizeof(s4req) - 1] = 0;
- c->tcplen = 8;
- return send_meta(mesh, c, s4req, sizeof(s4req));
- }
-
- case PROXY_SOCKS5: {
- int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
- c->tcplen = 2;
-
- if(mesh->proxypass) {
- len += 3 + strlen(mesh->proxyuser) + strlen(mesh->proxypass);
- }
-
- char s5req[len];
- int i = 0;
- s5req[i++] = 5;
- s5req[i++] = 1;
-
- if(mesh->proxypass) {
- s5req[i++] = 2;
- s5req[i++] = 1;
- 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;
- }
-
- s5req[i++] = 5;
- s5req[i++] = 1;
- s5req[i++] = 0;
-
- if(c->address.sa.sa_family == AF_INET) {
- s5req[i++] = 1;
- memcpy(s5req + i, &c->address.in.sin_addr, 4);
- i += 4;
- memcpy(s5req + i, &c->address.in.sin_port, 2);
- i += 2;
- c->tcplen += 10;
- } else if(c->address.sa.sa_family == AF_INET6) {
- s5req[i++] = 3;
- memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
- i += 16;
- memcpy(s5req + i, &c->address.in6.sin6_port, 2);
- i += 2;
- c->tcplen += 22;
- } else {
- logger(mesh, MESHLINK_ERROR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
- return false;
- }
-
- if(i > len) {
- abort();
- }
-
- return send_meta(mesh, c, s5req, sizeof(s5req));
- }
-
- case PROXY_SOCKS4A:
- logger(mesh, MESHLINK_ERROR, "Proxy type not implemented yet");
- return false;
-
- default:
- logger(mesh, MESHLINK_ERROR, "Unknown proxy type");
- return false;
- }
-}
-
bool send_id(meshlink_handle_t *mesh, connection_t *c) {
- if(mesh->proxytype && c->outgoing)
- if(!send_proxyrequest(mesh, c)) {
- return false;
- }
-
return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
}