Line data Source code
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 4 : haproxy_format_proxy_header_line(const tor_addr_port_t *addr_port) 15 : { 16 4 : tor_assert(addr_port); 17 : 18 4 : sa_family_t family = tor_addr_family(&addr_port->addr); 19 4 : const char *family_string = NULL; 20 4 : const char *src_addr_string = NULL; 21 : 22 4 : switch (family) { 23 : case AF_INET: 24 : family_string = "TCP4"; 25 : src_addr_string = "0.0.0.0"; 26 : break; 27 1 : case AF_INET6: 28 1 : family_string = "TCP6"; 29 1 : src_addr_string = "::"; 30 1 : break; 31 : default: 32 : /* Unknown family. */ 33 : return NULL; 34 : } 35 : 36 3 : char *buf; 37 3 : char addrbuf[TOR_ADDR_BUF_LEN]; 38 : 39 3 : tor_addr_to_str(addrbuf, &addr_port->addr, sizeof(addrbuf), 0); 40 : 41 3 : tor_asprintf(&buf, "PROXY %s %s %s 0 %d\r\n", family_string, src_addr_string, 42 3 : addrbuf, addr_port->port); 43 : 44 3 : return buf; 45 : }