Skip to content

Commit e3e7baf

Browse files
Easier API to get TLVs
1 parent 1c1d12c commit e3e7baf

3 files changed

Lines changed: 113 additions & 30 deletions

File tree

src/proxy_protocol.c

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,29 @@ typedef union
9090
} unix_addr;
9191
} proxy_addr_t;
9292

93+
/* Type-Length-Value (TLV vectors) */
94+
#define PP2_TYPE_ALPN 0x01
95+
#define PP2_TYPE_AUTHORITY 0x02
96+
#define PP2_TYPE_CRC32C 0x03
97+
#define PP2_TYPE_NOOP 0x04
98+
#define PP2_TYPE_UNIQUE_ID 0x05
99+
#define PP2_TYPE_SSL 0x20
100+
#define PP2_SUBTYPE_SSL_VERSION 0x21
101+
#define PP2_SUBTYPE_SSL_CN 0x22
102+
#define PP2_SUBTYPE_SSL_CIPHER 0x23
103+
#define PP2_SUBTYPE_SSL_SIG_ALG 0x24
104+
#define PP2_SUBTYPE_SSL_KEY_ALG 0x25
105+
#define PP2_TYPE_NETNS 0x30
106+
/* Custom TLVs */
107+
#define PP2_TYPE_AWS 0xEA
108+
#define PP2_TYPE_AZURE 0xEE
109+
110+
/* PP2_TYPE_AWS subtypes */
111+
#define PP2_SUBTYPE_AWS_VPCE_ID 0x01
112+
113+
/* PP2_TYPE_AZURE subtypes */
114+
#define PP2_SUBTYPE_AZURE_PRIVATEENDPOINT_LINKID 0x01
115+
93116
typedef struct
94117
{
95118
uint8_t type;
@@ -98,6 +121,11 @@ typedef struct
98121
uint8_t value[1];
99122
} pp2_tlv_t;
100123

124+
/* PP2_TYPE_SSL <client> bit field */
125+
#define PP2_CLIENT_SSL 0x01
126+
#define PP2_CLIENT_CERT_CONN 0x02
127+
#define PP2_CLIENT_CERT_SESS 0x04
128+
101129
typedef struct
102130
{
103131
uint8_t client;
@@ -263,7 +291,7 @@ static void tlv_array_clear(tlv_array_t *tlv_array)
263291
tlv_array->tlvs = NULL;
264292
}
265293

266-
const uint8_t *pp_info_get_tlv_value(const pp_info_t *pp_info, uint8_t type, uint8_t subtype, uint16_t *value_len_out)
294+
static const uint8_t *pp_info_get_tlv_value(const pp_info_t *pp_info, uint8_t type, uint8_t subtype, uint16_t *value_len_out)
267295
{
268296
*value_len_out = 0;
269297
if (!pp_info->tlv_array->tlvs || !pp_info->tlv_array->len)
@@ -293,6 +321,66 @@ const uint8_t *pp_info_get_tlv_value(const pp_info_t *pp_info, uint8_t type, uin
293321
return NULL;
294322
}
295323

324+
const uint8_t *pp_info_get_alpn(const pp_info_t *pp_info, uint16_t *value_len_out)
325+
{
326+
return pp_info_get_tlv_value(pp_info, PP2_TYPE_ALPN, 0, value_len_out);
327+
}
328+
329+
const uint8_t *pp_info_get_authority(const pp_info_t *pp_info, uint16_t *value_len_out)
330+
{
331+
return pp_info_get_tlv_value(pp_info, PP2_TYPE_AUTHORITY, 0, value_len_out);
332+
}
333+
334+
const uint8_t *pp_info_get_crc32c(const pp_info_t *pp_info, uint16_t *value_len_out)
335+
{
336+
return pp_info_get_tlv_value(pp_info, PP2_TYPE_CRC32C, 0, value_len_out);
337+
}
338+
339+
const uint8_t *pp_info_get_unique_id(const pp_info_t *pp_info, uint16_t *value_len_out)
340+
{
341+
return pp_info_get_tlv_value(pp_info, PP2_TYPE_UNIQUE_ID, 0, value_len_out);
342+
}
343+
344+
const uint8_t *pp_info_get_ssl_version(const pp_info_t *pp_info, uint16_t *value_len_out)
345+
{
346+
return pp_info_get_tlv_value(pp_info, PP2_SUBTYPE_SSL_VERSION, 0, value_len_out);
347+
}
348+
349+
const uint8_t *pp_info_get_ssl_cn(const pp_info_t *pp_info, uint16_t *value_len_out)
350+
{
351+
return pp_info_get_tlv_value(pp_info, PP2_SUBTYPE_SSL_CN, 0, value_len_out);
352+
}
353+
354+
const uint8_t *pp_info_get_ssl_cipher(const pp_info_t *pp_info, uint16_t *value_len_out)
355+
{
356+
return pp_info_get_tlv_value(pp_info, PP2_SUBTYPE_SSL_CIPHER, 0, value_len_out);
357+
}
358+
359+
const uint8_t *pp_info_get_ssl_sig_alg(const pp_info_t *pp_info, uint16_t *value_len_out)
360+
{
361+
return pp_info_get_tlv_value(pp_info, PP2_SUBTYPE_SSL_SIG_ALG, 0, value_len_out);
362+
}
363+
364+
const uint8_t *pp_info_get_ssl_key_alg(const pp_info_t *pp_info, uint16_t *value_len_out)
365+
{
366+
return pp_info_get_tlv_value(pp_info, PP2_SUBTYPE_SSL_KEY_ALG, 0, value_len_out);
367+
}
368+
369+
const uint8_t *pp_info_get_ssl_netns(const pp_info_t *pp_info, uint16_t *value_len_out)
370+
{
371+
return pp_info_get_tlv_value(pp_info, PP2_TYPE_NETNS, 0, value_len_out);
372+
}
373+
374+
const uint8_t *pp_info_get_aws_vpce_id(const pp_info_t *pp_info, uint16_t *value_len_out)
375+
{
376+
return pp_info_get_tlv_value(pp_info, PP2_TYPE_AWS, PP2_SUBTYPE_AWS_VPCE_ID, value_len_out);
377+
}
378+
379+
const uint8_t* pp_info_get_azure_linkid(const pp_info_t *pp_info, uint16_t *value_len_out)
380+
{
381+
return pp_info_get_tlv_value(pp_info, PP2_TYPE_AZURE, PP2_SUBTYPE_AZURE_PRIVATEENDPOINT_LINKID, value_len_out);
382+
}
383+
296384
void pp_info_clear(pp_info_t *pp_info)
297385
{
298386
if (pp_info->tlv_array)

src/proxy_protocol.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,6 @@ enum
5353
ERR_HEAP_ALLOC = -27,
5454
};
5555

56-
/* Type-Length-Value (TLV vectors) */
57-
#define PP2_TYPE_ALPN 0x01
58-
#define PP2_TYPE_AUTHORITY 0x02
59-
#define PP2_TYPE_CRC32C 0x03
60-
#define PP2_TYPE_NOOP 0x04
61-
#define PP2_TYPE_UNIQUE_ID 0x05
62-
#define PP2_TYPE_SSL 0x20
63-
#define PP2_SUBTYPE_SSL_VERSION 0x21
64-
#define PP2_SUBTYPE_SSL_CN 0x22
65-
#define PP2_SUBTYPE_SSL_CIPHER 0x23
66-
#define PP2_SUBTYPE_SSL_SIG_ALG 0x24
67-
#define PP2_SUBTYPE_SSL_KEY_ALG 0x25
68-
#define PP2_TYPE_NETNS 0x30
69-
/* Custom TLVs */
70-
#define PP2_TYPE_AWS 0xEA
71-
#define PP2_TYPE_AZURE 0xEE
72-
73-
/* PP2_TYPE_SSL <client> bit field */
74-
#define PP2_CLIENT_SSL 0x01
75-
#define PP2_CLIENT_CERT_CONN 0x02
76-
#define PP2_CLIENT_CERT_SESS 0x04
77-
78-
/* PP2_TYPE_AWS subtypes */
79-
#define PP2_SUBTYPE_AWS_VPCE_ID 0x01
80-
81-
/* PP2_TYPE_AZURE subtypes */
82-
#define PP2_SUBTYPE_AZURE_PRIVATEENDPOINT_LINKID 0x01
83-
8456
typedef struct _tlv_array_t tlv_array_t;
8557

8658
typedef struct
@@ -108,7 +80,6 @@ typedef struct
10880
} pp_info_t;
10981

11082
const char *pp_strerror(int32_t error);
111-
const uint8_t *pp_info_get_tlv_value(const pp_info_t *pp_info, uint8_t type, uint8_t subtype, uint16_t *value_len_out);
11283
void pp_info_clear(pp_info_t *pp_info);
11384

11485
/*

tests/test.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@
3030

3131
#define NUM_ELEMS(array) (uint32_t)(sizeof(array) / sizeof(array[0]))
3232

33+
/* Type-Length-Value (TLV vectors) */
34+
/* They need to be defined, for tests purposes, as the API does not expose them */
35+
#define PP2_TYPE_ALPN 0x01
36+
#define PP2_TYPE_AUTHORITY 0x02
37+
#define PP2_TYPE_CRC32C 0x03
38+
#define PP2_TYPE_NOOP 0x04
39+
#define PP2_TYPE_UNIQUE_ID 0x05
40+
#define PP2_TYPE_SSL 0x20
41+
#define PP2_SUBTYPE_SSL_VERSION 0x21
42+
#define PP2_SUBTYPE_SSL_CN 0x22
43+
#define PP2_SUBTYPE_SSL_CIPHER 0x23
44+
#define PP2_SUBTYPE_SSL_SIG_ALG 0x24
45+
#define PP2_SUBTYPE_SSL_KEY_ALG 0x25
46+
#define PP2_TYPE_NETNS 0x30
47+
/* Custom TLVs */
48+
#define PP2_TYPE_AWS 0xEA
49+
#define PP2_TYPE_AZURE 0xEE
50+
51+
/* PP2_TYPE_AWS subtypes */
52+
#define PP2_SUBTYPE_AWS_VPCE_ID 0x01
53+
54+
/* PP2_TYPE_AZURE subtypes */
55+
#define PP2_SUBTYPE_AZURE_PRIVATEENDPOINT_LINKID 0x01
56+
3357
typedef struct
3458
{
3559
uint8_t type;

0 commit comments

Comments
 (0)