-
-bool send_id(connection_t *c) {
- gettimeofday(&c->start, NULL);
-
- return send_request(c, "%d %s %d", ID, myself->connection->name,
- myself->connection->protocol_version);
-}
-
-bool id_h(connection_t *c, char *request) {
- char name[MAX_STRING_SIZE];
-
- if(sscanf(request, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
- logger(LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
- c->hostname);
- return false;
- }
-
- /* Check if this is a control connection */
-
- if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
- c->status.control = true;
- c->allow_request = CONTROL;
- c->last_ping_time = time(NULL) + 3600;
- return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
+#include "ed25519/sha512.h"
+
+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, "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(DEBUG_ALWAYS, LOG_ERR, "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(DEBUG_ALWAYS, LOG_ERR, "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(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
+ return false;
+ case PROXY_EXEC:
+ return true;
+ default:
+ logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
+ return false;