Home | History | Annotate | Line # | Download | only in global
      1 /*	$NetBSD: haproxy_srvr.h,v 1.3 2026/05/09 18:49:16 christos Exp $	*/
      2 
      3 #ifndef _HAPROXY_SRVR_H_INCLUDED_
      4 #define _HAPROXY_SRVR_H_INCLUDED_
      5 
      6 /*++
      7 /* NAME
      8 /*	haproxy_srvr 3h
      9 /* SUMMARY
     10 /*	server-side haproxy protocol support
     11 /* SYNOPSIS
     12 /*	#include <haproxy_srvr.h>
     13 /* DESCRIPTION
     14 /* .nf
     15 
     16  /*
     17   * Utility library.
     18   */
     19 #include <myaddrinfo.h>
     20 
     21  /*
     22   * External interface.
     23   */
     24 extern const char *haproxy_srvr_parse_sa(const char *, ssize_t *, int *,
     25 			             MAI_HOSTADDR_STR *, MAI_SERVPORT_STR *,
     26 			             MAI_HOSTADDR_STR *, MAI_SERVPORT_STR *,
     27 				         struct sockaddr *, SOCKADDR_SIZE *,
     28 				        struct sockaddr *, SOCKADDR_SIZE *);
     29 
     30 #define haproxy_srvr_parse(str, len, non_proxy, client_addr, client_port, \
     31 				server_addr, server_port) \
     32 				haproxy_srvr_parse_sa((str), (len), (non_proxy), \
     33 				(client_addr), (client_port), \
     34 				(server_addr), (server_port), \
     35 				(struct sockaddr *) 0, (SOCKADDR_SIZE *) 0, \
     36 				(struct	sockaddr *) 0, (SOCKADDR_SIZE *) 0)
     37 extern int haproxy_srvr_receive_sa(int, int *,
     38 			             MAI_HOSTADDR_STR *, MAI_SERVPORT_STR *,
     39 			             MAI_HOSTADDR_STR *, MAI_SERVPORT_STR *,
     40 				         struct sockaddr *, SOCKADDR_SIZE *,
     41 				        struct sockaddr *, SOCKADDR_SIZE *);
     42 
     43 #define haproxy_srvr_receive(fd, non_proxy, client_addr, client_port, \
     44 				server_addr, server_port) \
     45 				haproxy_srvr_receive_sa((fd), (non_proxy), \
     46 				(client_addr), (client_port), \
     47 				(server_addr), (server_port), \
     48 				(struct sockaddr *) 0, (SOCKADDR_SIZE *) 0, \
     49 				(struct	sockaddr *) 0, (SOCKADDR_SIZE *) 0)
     50 
     51 #define HAPROXY_PROTO_NAME	"haproxy"
     52 
     53 #ifndef DO_GRIPE
     54 #define DO_GRIPE 	1
     55 #define DONT_GRIPE	0
     56 #endif
     57 
     58 #ifdef _HAPROXY_SRVR_INTERNAL_
     59 
     60  /*
     61   * The haproxy protocol assumes that a haproxy header will normally not
     62   * exceed the default IPv4 TCP MSS, i.e. 576-40=536 bytes (the IPv6 default
     63   * is larger: 1280-60=1220). With a proxy header that contains IPv6
     64   * addresses, that leaves room for 536-52=484 bytes of TLVs. The Postfix
     65   * implementation does not support headers with UNIX-domain addresses.
     66   */
     67 #define HAPROXY_HEADER_MAX_LEN		536
     68 
     69  /*
     70   * Begin protocol v2 definitions from haproxy/include/types/connection.h.
     71   */
     72 #define PP2_SIGNATURE		"\r\n\r\n\0\r\nQUIT\n"
     73 #define PP2_SIGNATURE_LEN	12
     74 #define PP2_HEADER_LEN		16
     75 
     76 /* ver_cmd byte */
     77 #define PP2_CMD_LOCAL		0x00
     78 #define PP2_CMD_PROXY		0x01
     79 #define PP2_CMD_MASK		0x0F
     80 
     81 #define PP2_VERSION		0x20
     82 #define PP2_VERSION_MASK	0xF0
     83 
     84 /* fam byte */
     85 #define PP2_TRANS_UNSPEC	0x00
     86 #define PP2_TRANS_STREAM	0x01
     87 #define PP2_TRANS_DGRAM		0x02
     88 #define PP2_TRANS_MASK		0x0F
     89 
     90 #define PP2_FAM_UNSPEC		0x00
     91 #define PP2_FAM_INET		0x10
     92 #define PP2_FAM_INET6		0x20
     93 #define PP2_FAM_UNIX		0x30
     94 #define PP2_FAM_MASK		0xF0
     95 
     96 /* len field (2 bytes) */
     97 #define PP2_ADDR_LEN_UNSPEC	(0)
     98 #define PP2_ADDR_LEN_INET	(4 + 4 + 2 + 2)
     99 #define PP2_ADDR_LEN_INET6	(16 + 16 + 2 + 2)
    100 #define PP2_ADDR_LEN_UNIX	(108 + 108)
    101 
    102 #define PP2_HDR_LEN_UNSPEC	(PP2_HEADER_LEN + PP2_ADDR_LEN_UNSPEC)
    103 #define PP2_HDR_LEN_INET	(PP2_HEADER_LEN + PP2_ADDR_LEN_INET)
    104 #define PP2_HDR_LEN_INET6	(PP2_HEADER_LEN + PP2_ADDR_LEN_INET6)
    105 #define PP2_HDR_LEN_UNIX	(PP2_HEADER_LEN + PP2_ADDR_LEN_UNIX)
    106 
    107 struct proxy_hdr_v2 {
    108     uint8_t sig[PP2_SIGNATURE_LEN];	/* PP2_SIGNATURE */
    109     uint8_t ver_cmd;			/* protocol version | command */
    110     uint8_t fam;			/* protocol family and transport */
    111     uint16_t len;			/* length of remainder */
    112     union {
    113 	struct {			/* for TCP/UDP over IPv4, len = 12 */
    114 	    uint32_t src_addr;
    115 	    uint32_t dst_addr;
    116 	    uint16_t src_port;
    117 	    uint16_t dst_port;
    118 	}       ip4;
    119 	struct {			/* for TCP/UDP over IPv6, len = 36 */
    120 	    uint8_t src_addr[16];
    121 	    uint8_t dst_addr[16];
    122 	    uint16_t src_port;
    123 	    uint16_t dst_port;
    124 	}       ip6;
    125 	struct {			/* for AF_UNIX sockets, len = 216 */
    126 	    uint8_t src_addr[108];
    127 	    uint8_t dst_addr[108];
    128 	}       unx;
    129     }       addr;
    130 };
    131 
    132  /*
    133   * End protocol v2 definitions from haproxy/include/types/connection.h.
    134   */
    135 
    136 #endif					/* _HAPROXY_SRVR_INTERNAL_ */
    137 
    138 /* LICENSE
    139 /* .ad
    140 /* .fi
    141 /*	The Secure Mailer license must be distributed with this software.
    142 /* AUTHOR(S)
    143 /*	Wietse Venema
    144 /*	IBM T.J. Watson Research
    145 /*	P.O. Box 704
    146 /*	Yorktown Heights, NY 10598, USA
    147 /*
    148 /*	Wietse Venema
    149 /*	Google, Inc.
    150 /*	111 8th Avenue
    151 /*	New York, NY 10011, USA
    152 /*--*/
    153 
    154 #endif
    155