tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

proto_haproxy.c (1199B)


      1 /* Copyright (c) 2019-2021, The Tor Project, Inc. */
      2 /* See LICENSE for licensing information */
      3 
      4 #define PROTO_HAPROXY_PRIVATE
      5 #include "lib/malloc/malloc.h"
      6 #include "lib/net/address.h"
      7 #include "lib/string/printf.h"
      8 #include "core/proto/proto_haproxy.h"
      9 
     10 /** Return a newly allocated PROXY header null-terminated string. Returns NULL
     11 * if addr_port->addr is incompatible with the proxy protocol.
     12 */
     13 char *
     14 haproxy_format_proxy_header_line(const tor_addr_port_t *addr_port)
     15 {
     16  tor_assert(addr_port);
     17 
     18  sa_family_t family = tor_addr_family(&addr_port->addr);
     19  const char *family_string = NULL;
     20  const char *src_addr_string = NULL;
     21 
     22  switch (family) {
     23    case AF_INET:
     24      family_string = "TCP4";
     25      src_addr_string = "0.0.0.0";
     26      break;
     27    case AF_INET6:
     28      family_string = "TCP6";
     29      src_addr_string = "::";
     30      break;
     31    default:
     32      /* Unknown family. */
     33      return NULL;
     34  }
     35 
     36  char *buf;
     37  char addrbuf[TOR_ADDR_BUF_LEN];
     38 
     39  tor_addr_to_str(addrbuf, &addr_port->addr, sizeof(addrbuf), 0);
     40 
     41  tor_asprintf(&buf, "PROXY %s %s %s 0 %d\r\n", family_string, src_addr_string,
     42                                                addrbuf, addr_port->port);
     43 
     44  return buf;
     45 }