Tor  0.4.7.0-alpha-dev
proto_haproxy.c
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 }
const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
Definition: address.c:328
Headers for address.h.
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
#define TOR_ADDR_BUF_LEN
Definition: address.h:224
uint16_t sa_family_t
Definition: inaddr_st.h:77
Headers for util_malloc.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
Header for printf.c.
#define tor_assert(expr)
Definition: util_bug.h:102