- int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
- c->tcplen = 2;
-
- if(proxypass) {
- len += 3 + strlen(proxyuser) + strlen(proxypass);
- }
-
- char s5req[len];
- int i = 0;
- s5req[i++] = 5;
- s5req[i++] = 1;
-
- if(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);
- 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 %x not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
- return false;
- }
-
- if(i > len) {
- abort();
- }
-
- return send_meta(c, s5req, sizeof(s5req));
+ size_t reqlen = socks_req_len(proxytype, &c->address);
+ uint8_t *req = alloca(reqlen);
+ c->tcplen = create_socks_req(proxytype, req, &c->address);
+ return c->tcplen ? send_meta(c, req, reqlen) : false;