Skip to content

Commit 1ba6859

Browse files
Merge pull request #15 from kosmas-valianos/healthcheckHelper
Add helper function to quickly and easy create a v2 healthcheck header
2 parents 403e58a + 26eccd7 commit 1ba6859

3 files changed

Lines changed: 57 additions & 16 deletions

File tree

src/proxy_protocol.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,16 @@ uint8_t *pp2_create_hdr(const pp_info_t *pp_info, uint16_t *pp2_hdr_len, int32_t
721721
return pp2_hdr;
722722
}
723723

724+
uint8_t *pp2_create_healthcheck_hdr(uint16_t *pp2_hdr_len, int32_t *error)
725+
{
726+
pp_info_t pp_info = {
727+
.address_family = ADDR_FAMILY_UNSPEC,
728+
.transport_protocol = TRANSPORT_PROTOCOL_UNSPEC,
729+
.pp2_info.local = 1
730+
};
731+
return pp2_create_hdr(&pp_info, pp2_hdr_len, error);
732+
}
733+
724734
static uint8_t *pp1_create_hdr(const pp_info_t *pp_info, uint16_t *pp1_hdr_len, int32_t *error)
725735
{
726736
if (pp_info->transport_protocol != TRANSPORT_PROTOCOL_UNSPEC && pp_info->transport_protocol != TRANSPORT_PROTOCOL_STREAM)

src/proxy_protocol.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,26 @@ const uint8_t *pp_info_get_azure_linkid(const pp_info_t *pp_info, uint16_t *leng
176176
*/
177177
void pp_info_clear(pp_info_t *pp_info);
178178

179+
/* Helper to easily create a v2 healthcheck PROXY protocol header.
180+
*
181+
* Note: There is not an equivalent v1 function because specification 2.6 suggests that senders
182+
* SHOULD build a valid PROXY line i.e. usage of "UNKNOWN" style is discouraged
183+
*
184+
* pp_hdr_len Pointer to a uint16_t where the length of the create PROXY protocol header will be set
185+
* error Pointer to a int32_t where the error value will be set
186+
* ERR_NULL No error occurred
187+
* < 0 Error occurred. Optionally, pp_strerror() with that value can be used to get a descriptive message
188+
* return Pointer to a heap allocated buffer containing the PROXY protocol header. Must be freed with free()
189+
*/
190+
uint8_t *pp2_create_healthcheck_hdr(uint16_t *pp2_hdr_len, int32_t *error);
191+
179192
/* Creates a PROXY protocol header considering the information inside the pp_info.
180193
*
181194
* version: 0 Create a v1 PROXY protocol header
182195
* 1 Create a v2 PROXY protocol header
183196
* pp_info Pointer to a filled pp_info_t structure whose information will be used for the creation of the PROXY protocol header
184197
* pp_hdr_len Pointer to a uint16_t where the length of the create PROXY protocol header will be set
185-
* error Pointer to a uint32_t where the error value will be set
198+
* error Pointer to a int32_t where the error value will be set
186199
* ERR_NULL No error occurred
187200
* < 0 Error occurred. Optionally, pp_strerror() with that value can be used to get a descriptive message
188201
* return Pointer to a heap allocated buffer containing the PROXY protocol header. Must be freed with free()

tests/test.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ typedef struct
6666
{
6767
const char *name;
6868
uint8_t version;
69+
uint8_t create_healthcheck;
6970
pp_info_t pp_info_in;
7071
uint8_t *raw_bytes_in;
7172
uint32_t raw_bytes_in_length;
@@ -447,40 +448,40 @@ int main()
447448
},
448449
.add_tlvs = {
449450
{
450-
.type = PP2_SUBTYPE_SSL_VERSION,
451+
.type = PP2_SUBTYPE_SSL_VERSION,
451452
.value_len = 8,
452-
.value = (uint8_t*) "TLSv1.2"
453+
.value = (uint8_t*)"TLSv1.2"
453454
},
454455
{
455456
.type = PP2_SUBTYPE_SSL_CN,
456457
.value_len = 18,
457-
.value = (uint8_t*) "proxy-protocol.com"
458+
.value = (uint8_t*)"proxy-protocol.com"
458459
},
459460
{
460461
.type = PP2_SUBTYPE_SSL_CIPHER,
461462
.value_len = 28,
462-
.value = (uint8_t*) "ECDHE-RSA-AES128-GCM-SHA256"
463+
.value = (uint8_t*)"ECDHE-RSA-AES128-GCM-SHA256"
463464
},
464465
{
465466
.type = PP2_SUBTYPE_SSL_SIG_ALG,
466467
.value_len = 7,
467-
.value = (uint8_t*) "SHA256"
468+
.value = (uint8_t*)"SHA256"
468469
},
469470
{
470471
.type = PP2_SUBTYPE_SSL_KEY_ALG,
471472
.value_len = 8,
472-
.value = (uint8_t*) "RSA2048"
473+
.value = (uint8_t*)"RSA2048"
473474
},
474475
{
475476
.type = PP2_TYPE_AWS,
476477
.subtype = PP2_SUBTYPE_AWS_VPCE_ID,
477478
.value_len = 24,
478-
.value = (uint8_t*) "vpce-24d8ezjk38bchilm4m"
479+
.value = (uint8_t*)"vpce-24d8ezjk38bchilm4m"
479480
},
480481
{
481482
.type = PP2_TYPE_CRC32C,
482483
.value_len = 4,
483-
.value = (uint8_t*) "\x43\x84\x86\x4e"
484+
.value = (uint8_t*)"\x43\x84\x86\x4e"
484485
},
485486
},
486487
.pp_info_out_expected = tests[9].pp_info_in,
@@ -511,27 +512,27 @@ int main()
511512
{
512513
.type = PP2_SUBTYPE_SSL_VERSION,
513514
.value_len = 8,
514-
.value = (uint8_t*) "TLSv1.2"
515+
.value = (uint8_t*)"TLSv1.2"
515516
},
516517
{
517518
.type = PP2_SUBTYPE_SSL_CN,
518519
.value_len = 11,
519-
.value = (uint8_t*) "example.com"
520+
.value = (uint8_t*)"example.com"
520521
},
521522
{
522523
.type = PP2_SUBTYPE_SSL_CIPHER,
523524
.value_len = 28,
524-
.value = (uint8_t*) "ECDHE-RSA-AES128-GCM-SHA256"
525+
.value = (uint8_t*)"ECDHE-RSA-AES128-GCM-SHA256"
525526
},
526527
{
527528
.type = PP2_SUBTYPE_SSL_SIG_ALG,
528529
.value_len = 7,
529-
.value = (uint8_t*) "SHA256"
530+
.value = (uint8_t*)"SHA256"
530531
},
531532
{
532533
.type = PP2_SUBTYPE_SSL_KEY_ALG,
533534
.value_len = 8,
534-
.value = (uint8_t*) "RSA2048"
535+
.value = (uint8_t*)"RSA2048"
535536
},
536537
},
537538
},
@@ -544,6 +545,15 @@ int main()
544545
},
545546
.pp_info_out_expected = tests[11].pp_info_in,
546547
},
548+
{
549+
.name = "v2 PROXY protocol header: pp2_create_healthcheck_hdr() and parse - LOCAL, AF_UNSPEC",
550+
.create_healthcheck = 1,
551+
.pp_info_out_expected = {
552+
.address_family = ADDR_FAMILY_UNSPEC,
553+
.transport_protocol = TRANSPORT_PROTOCOL_UNSPEC,
554+
.pp2_info.local = 1
555+
},
556+
},
547557
};
548558

549559
/* Run tests */
@@ -574,8 +584,16 @@ int main()
574584
}
575585
}
576586

577-
uint8_t *pp_hdr = pp_create_hdr(tests[i].version, &tests[i].pp_info_in, &pp_hdr_len, &error);
578-
pp_info_clear(&tests[i].pp_info_in);
587+
uint8_t *pp_hdr = NULL;
588+
if (tests[i].create_healthcheck)
589+
{
590+
pp_hdr = pp2_create_healthcheck_hdr(&pp_hdr_len, &error);
591+
}
592+
else
593+
{
594+
pp_hdr = pp_create_hdr(tests[i].version, &tests[i].pp_info_in, &pp_hdr_len, &error);
595+
pp_info_clear(&tests[i].pp_info_in);
596+
}
579597
if (!pp_hdr
580598
|| error != ERR_NULL
581599
|| (alignment > 1 && pp_hdr_len % alignment))

0 commit comments

Comments
 (0)