From 4e27cff7cfe73401b47498d7288e1dbf4411897c Mon Sep 17 00:00:00 2001 From: Vadim Zhestikov Date: Tue, 12 May 2026 09:37:30 -0700 Subject: [PATCH] Core: named PP v2 address-family and TLV-type constants. Define NGX_PROXY_PROTOCOL_AF_* and NGX_PROXY_PROTOCOL_V2_TYPE_* / SUBTYPE_* constants in ngx_proxy_protocol.h, replacing the raw hex literals used in ngx_proxy_protocol_v2_write() for the family_transport byte. The local AF_INET/AF_INET6 defines in ngx_proxy_protocol.c are removed; the read path's existing NGX_PROXY_PROTOCOL_AF_INET/INET6 references now bind to the header definitions. --- src/core/ngx_proxy_protocol.c | 12 ++++-------- src/core/ngx_proxy_protocol.h | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/core/ngx_proxy_protocol.c b/src/core/ngx_proxy_protocol.c index 8df8343fa..36287103c 100644 --- a/src/core/ngx_proxy_protocol.c +++ b/src/core/ngx_proxy_protocol.c @@ -9,10 +9,6 @@ #include -#define NGX_PROXY_PROTOCOL_AF_INET 1 -#define NGX_PROXY_PROTOCOL_AF_INET6 2 - - #define ngx_proxy_protocol_parse_uint16(p) \ ( ((uint16_t) (p)[0] << 8) \ + ( (p)[1]) ) @@ -361,7 +357,7 @@ ngx_proxy_protocol_v2_write(ngx_connection_t *c, u_char *buf, u_char *last) case AF_INET: - header->family_transport = 0x10 | transport; /* AF_INET */ + header->family_transport = (NGX_PROXY_PROTOCOL_AF_INET << 4) | transport; header->len[0] = 0; header->len[1] = sizeof(ngx_proxy_protocol_inet_addrs_t); @@ -396,7 +392,7 @@ ngx_proxy_protocol_v2_write(ngx_connection_t *c, u_char *buf, u_char *last) if (IN6_IS_ADDR_V4MAPPED(src6)) { - header->family_transport = 0x10 | transport; /* AF_INET */ + header->family_transport = (NGX_PROXY_PROTOCOL_AF_INET << 4) | transport; header->len[0] = 0; header->len[1] = sizeof(ngx_proxy_protocol_inet_addrs_t); @@ -414,7 +410,7 @@ ngx_proxy_protocol_v2_write(ngx_connection_t *c, u_char *buf, u_char *last) } else { - header->family_transport = 0x20 | transport; /* AF_INET6 */ + header->family_transport = (NGX_PROXY_PROTOCOL_AF_INET6 << 4) | transport; header->len[0] = 0; header->len[1] = sizeof(ngx_proxy_protocol_inet6_addrs_t); @@ -437,7 +433,7 @@ ngx_proxy_protocol_v2_write(ngx_connection_t *c, u_char *buf, u_char *last) default: - header->family_transport = 0x00; /* UNSPEC */ + header->family_transport = NGX_PROXY_PROTOCOL_AF_UNSPEC << 4; header->len[0] = 0; header->len[1] = 0; diff --git a/src/core/ngx_proxy_protocol.h b/src/core/ngx_proxy_protocol.h index 2fe513c28..8c0ed84f3 100644 --- a/src/core/ngx_proxy_protocol.h +++ b/src/core/ngx_proxy_protocol.h @@ -17,6 +17,24 @@ #define NGX_PROXY_PROTOCOL_V2_MAX_HEADER 52 #define NGX_PROXY_PROTOCOL_MAX_HEADER 4096 +#define NGX_PROXY_PROTOCOL_AF_UNSPEC 0 +#define NGX_PROXY_PROTOCOL_AF_INET 1 +#define NGX_PROXY_PROTOCOL_AF_INET6 2 +#define NGX_PROXY_PROTOCOL_AF_UNIX 3 + +#define NGX_PROXY_PROTOCOL_V2_TYPE_ALPN 0x01 +#define NGX_PROXY_PROTOCOL_V2_TYPE_AUTHORITY 0x02 +#define NGX_PROXY_PROTOCOL_V2_TYPE_CRC32C 0x03 +#define NGX_PROXY_PROTOCOL_V2_TYPE_NOOP 0x04 +#define NGX_PROXY_PROTOCOL_V2_TYPE_UNIQUE_ID 0x05 +#define NGX_PROXY_PROTOCOL_V2_TYPE_SSL 0x20 +#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_VERSION 0x21 +#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_CN 0x22 +#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_CIPHER 0x23 +#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_SIG_ALG 0x24 +#define NGX_PROXY_PROTOCOL_V2_SUBTYPE_SSL_KEY_ALG 0x25 +#define NGX_PROXY_PROTOCOL_V2_TYPE_NETNS 0x30 + struct ngx_proxy_protocol_s { ngx_str_t src_addr;