Skip to main content

build_proxy_protocol_v2_header

Function build_proxy_protocol_v2_header 

Source
pub fn build_proxy_protocol_v2_header(
    src: SocketAddr,
    dst: SocketAddr,
) -> Vec<u8> 
Expand description

Build a PROXY protocol v2 header describing a proxied TCP connection from src (the real client) to dst (the proxy’s listener address).

The header layout (see the HAProxy PROXY protocol v2 spec):

  • 12-byte signature 0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A
  • byte 13: version/command 0x21 (v2, PROXY command)
  • byte 14: address family + transport — 0x11 (AF_INET + STREAM) or 0x21 (AF_INET6 + STREAM)
  • bytes 15-16: big-endian length of the following address block (12 for IPv4, 36 for IPv6)
  • address block: src IP, dst IP, src port, dst port (all big-endian)

The address family is chosen from the client (src) address. When src and dst families differ, both addresses are coerced to the client’s family so the header stays internally consistent.