Update copyrights to 2021, using "make update-copyright"
[tor.git] / src / core / proto / proto_haproxy.c
blobb082ef99e0d72e326c1012d9a9de45c52e2e9570
1 /* Copyright (c) 2019-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
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"
10 /** Return a newly allocated PROXY header null-terminated string. Returns NULL
11 * if addr_port->addr is incompatible with the proxy protocol.
13 char *
14 haproxy_format_proxy_header_line(const tor_addr_port_t *addr_port)
16 tor_assert(addr_port);
18 sa_family_t family = tor_addr_family(&addr_port->addr);
19 const char *family_string = NULL;
20 const char *src_addr_string = NULL;
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;
36 char *buf;
37 char addrbuf[TOR_ADDR_BUF_LEN];
39 tor_addr_to_str(addrbuf, &addr_port->addr, sizeof(addrbuf), 0);
41 tor_asprintf(&buf, "PROXY %s %s %s 0 %d\r\n", family_string, src_addr_string,
42 addrbuf, addr_port->port);
44 return buf;