Skip to content

Commit 6500c9f

Browse files
Fix v1 UNKNOWN creation
1 parent a857f36 commit 6500c9f

3 files changed

Lines changed: 42 additions & 23 deletions

File tree

src/proxy_protocol.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ for string processing.
5353
*/
5454

5555
#define PP1_MAX_LENGHT 108
56-
static const char *crlf = "\r\n";
56+
#define PP1_SIG "PROXY"
57+
#define CRLF "\r\n"
5758

5859
/****************************************************************/
5960

@@ -145,6 +146,8 @@ typedef struct
145146
uint32_t linkid;
146147
} pp2_tlv_azure_t;
147148

149+
#define PP2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
150+
148151
/* ANSI C makes us suffer as we cannot have value[0] */
149152
#define sizeof_pp2_tlv_t (sizeof(pp2_tlv_t) - 1)
150153
#define sizeof_pp2_tlv_aws_t (sizeof(pp2_tlv_aws_t) - 1)
@@ -472,11 +475,7 @@ void pp_info_clear(pp_info_t *pp_info)
472475

473476
uint8_t *pp2_create_hdr(const pp_info_t *pp_info, uint16_t *pp2_hdr_len, int32_t *error)
474477
{
475-
proxy_hdr_v2_t proxy_hdr_v2 = {
476-
.sig = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A",
477-
.ver_cmd = '\x21',
478-
};
479-
478+
proxy_hdr_v2_t proxy_hdr_v2 = { .sig = PP2_SIG, .ver_cmd = '\x21' };
480479
uint16_t proxy_addr_len;
481480
proxy_addr_t proxy_addr;
482481
if (pp_info->address_family == ADDR_FAMILY_UNSPEC)
@@ -577,17 +576,18 @@ uint8_t *pp2_create_hdr(const pp_info_t *pp_info, uint16_t *pp2_hdr_len, int32_t
577576

578577
static uint8_t *pp1_create_hdr(const pp_info_t *pp_info, uint16_t *pp1_hdr_len, int32_t *error)
579578
{
580-
if (pp_info->transport_protocol != TRANSPORT_PROTOCOL_STREAM)
579+
if (pp_info->transport_protocol != TRANSPORT_PROTOCOL_UNSPEC && pp_info->transport_protocol != TRANSPORT_PROTOCOL_STREAM)
581580
{
582581
*error = ERR_PP1_TRANSPORT_FAMILY;
583582
return NULL;
584583
}
585584

586-
/* sprintf() as snprintf does not exist in ANSI C */
587585
char block[PP1_MAX_LENGHT];
588586
if (pp_info->address_family == ADDR_FAMILY_UNSPEC)
589587
{
590-
*pp1_hdr_len = sprintf(block, "PROXY UNKNOWN%s", crlf);
588+
static const char str[] = "PROXY UNKNOWN"CRLF;
589+
*pp1_hdr_len = sizeof(str) - 1;
590+
memcpy(block, str, *pp1_hdr_len);
591591
}
592592
else if (pp_info->address_family == ADDR_FAMILY_INET || pp_info->address_family == ADDR_FAMILY_INET6)
593593
{
@@ -606,7 +606,8 @@ static uint8_t *pp1_create_hdr(const pp_info_t *pp_info, uint16_t *pp1_hdr_len,
606606
char dst_addr[39+1];
607607
memcpy(src_addr, pp_info->src_addr, sizeof(src_addr));
608608
memcpy(dst_addr, pp_info->dst_addr, sizeof(dst_addr));
609-
*pp1_hdr_len = sprintf(block, "PROXY %s %s %s %hu %hu%s", fam, src_addr, dst_addr, pp_info->src_port, pp_info->dst_port, crlf);
609+
/* sprintf() as snprintf does not exist in ANSI C */
610+
*pp1_hdr_len = sprintf(block, "PROXY %s %s %s %hu %hu"CRLF, fam, src_addr, dst_addr, pp_info->src_port, pp_info->dst_port);
610611
}
611612
else
612613
{
@@ -1035,12 +1036,12 @@ static int32_t pp1_parse_hdr(const uint8_t *buffer, uint32_t buffer_length, pp_i
10351036
int32_t pp1_hdr_len = 0;
10361037
memcpy(block, buffer, buffer_length < PP1_MAX_LENGHT ? buffer_length : PP1_MAX_LENGHT);
10371038

1038-
char *block_end = strstr(block, crlf);
1039+
char *block_end = strstr(block, CRLF);
10391040
if (!block_end)
10401041
{
10411042
return ERR_PP1_CRLF;
10421043
}
1043-
block_end += strlen(crlf);
1044+
block_end += strlen(CRLF);
10441045
pp1_hdr_len = block_end - block;
10451046

10461047
/* PROXY */
@@ -1197,11 +1198,11 @@ static int32_t pp1_parse_hdr(const uint8_t *buffer, uint32_t buffer_length, pp_i
11971198
int32_t pp_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t *pp_info)
11981199
{
11991200
memset(pp_info, 0, sizeof(*pp_info));
1200-
if (buffer_length >= 16 && !memcmp(buffer, "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12))
1201+
if (buffer_length >= 16 && !memcmp(buffer, PP2_SIG, 12))
12011202
{
12021203
return pp2_parse_hdr(buffer, buffer_length, pp_info);
12031204
}
1204-
else if (buffer_length >= 8 && !memcmp(buffer, "\x50\x52\x4F\x58\x59", 5))
1205+
else if (buffer_length >= 8 && !memcmp(buffer, PP1_SIG, 5))
12051206
{
12061207
return pp1_parse_hdr(buffer, buffer_length, pp_info);;
12071208
}

src/proxy_protocol.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ typedef struct
111111
pp2_info_t pp2_info;
112112
} pp_info_t;
113113

114+
115+
/* Adds the specified TLV in the given pp_info
116+
*
117+
* pp_info Pointer to a pp_info_t structure to be used in pp_create_hdr()
118+
* length The length of the TLV's value in case it is not a US-ASCII value
119+
* $value_param(s) The value(s) of the specified TLV
120+
* return 1: success 0: failure
121+
*/
114122
uint8_t pp_info_add_alpn(pp_info_t *pp_info, uint16_t length, const void *alpn);
115123
uint8_t pp_info_add_authority(pp_info_t *pp_info, uint16_t length, const void *host_name);
116124
uint8_t pp_info_add_unique_id(pp_info_t *pp_info, uint16_t length, const void *unique_id);
@@ -153,7 +161,7 @@ void pp_info_clear(pp_info_t *pp_info);
153161
* pp_hdr_len Pointer to a uint16_t where the length of the create PROXY protocol header will be set
154162
* error Pointer to a uint32_t where the error value will be set
155163
* ERR_NULL No error occurred
156-
* < 0 Error
164+
* < 0 Error occurred. pp_strerror() with that value can be used to get a descriptive message
157165
* return Pointer to a heap allocated buffer containing the PROXY protocol header. Must be freed with free()
158166
*/
159167
uint8_t *pp_create_hdr(uint8_t version, const pp_info_t *pp_info, uint16_t *pp_hdr_len, int32_t *error);

tests/test.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ int main()
345345
},
346346
},
347347
{
348-
.name = "v2 PROXY protocol header: PROXY, TCP over IPv4 create and parse",
348+
.name = "v2 PROXY protocol header: create and parse - PROXY, TCP over IPv4",
349349
.version = 2,
350350
.pp_info_in = {
351351
.address_family = ADDR_FAMILY_INET,
@@ -358,7 +358,7 @@ int main()
358358
.pp_info_out_expected = tests[3].pp_info_in,
359359
},
360360
{
361-
.name = "v1 PROXY protocol header: TCP4 create and parse",
361+
.name = "v1 PROXY protocol header: create and parse- TCP4",
362362
.version = 1,
363363
.pp_info_in = {
364364
.address_family = ADDR_FAMILY_INET,
@@ -371,7 +371,7 @@ int main()
371371
.pp_info_out_expected = tests[4].pp_info_in,
372372
},
373373
{
374-
.name = "v2 PROXY protocol header: PROXY, UNIX stream create and parse",
374+
.name = "v2 PROXY protocol header: create and parse - PROXY, UNIX stream",
375375
.version = 2,
376376
.pp_info_in = {
377377
.address_family = ADDR_FAMILY_UNIX,
@@ -382,7 +382,7 @@ int main()
382382
.pp_info_out_expected = tests[5].pp_info_in,
383383
},
384384
{
385-
.name = "v2 PROXY protocol header: LOCAL, AF_UNSPEC create and parse",
385+
.name = "v2 PROXY protocol header: create and parse - LOCAL, AF_UNSPEC",
386386
.version = 2,
387387
.pp_info_in = {
388388
.address_family = ADDR_FAMILY_UNSPEC,
@@ -392,7 +392,7 @@ int main()
392392
.pp_info_out_expected = tests[6].pp_info_in,
393393
},
394394
{
395-
.name = "v2 PROXY protocol header: PROXY, TCP over IPv6 create and parse",
395+
.name = "v2 PROXY protocol header: create and parse - PROXY, TCP over IPv6",
396396
.version = 2,
397397
.pp_info_in = {
398398
.address_family = ADDR_FAMILY_INET6,
@@ -405,7 +405,7 @@ int main()
405405
.pp_info_out_expected = tests[7].pp_info_in,
406406
},
407407
{
408-
.name = "v1 PROXY protocol header: TCP6 create and parse",
408+
.name = "v1 PROXY protocol header: create and parse - TCP6",
409409
.version = 1,
410410
.pp_info_in = {
411411
.address_family = ADDR_FAMILY_INET6,
@@ -418,7 +418,8 @@ int main()
418418
.pp_info_out_expected = tests[8].pp_info_in,
419419
},
420420
{
421-
.name = "v2 PROXY protocol header: create and parse: PROXY, TCP over IPv4.",
421+
.name = "v2 PROXY protocol header: create and parse - PROXY, TCP over IPv4. TLVs: "
422+
"PP2_TYPE_SSL, PP2_SUBTYPE_SSL_VERSION, PP2_SUBTYPE_SSL_CN, PP2_SUBTYPE_SSL_CIPHER, PP2_SUBTYPE_SSL_SIG_ALG, PP2_SUBTYPE_SSL_KEY_ALG, PP2_TYPE_AWS(PP2_SUBTYPE_AWS_VPCE_ID)",
422423
.version = 2,
423424
.pp_info_in = {
424425
.address_family = ADDR_FAMILY_INET,
@@ -474,7 +475,7 @@ int main()
474475
},
475476
{
476477
.name = "v2 PROXY protocol header: PROXY, TCP over IPv4. TLVs: "
477-
"PP2_TYPE_SSL, PP2_SUBTYPE_SSL_VERSION, PP2_SUBTYPE_SSL_CN, PP2_SUBTYPE_SSL_CIPHER, PP2_SUBTYPE_SSL_SIG_ALG, PP2_SUBTYPE_SSL_KEY_ALG ",
478+
"PP2_TYPE_SSL, PP2_SUBTYPE_SSL_VERSION, PP2_SUBTYPE_SSL_CN, PP2_SUBTYPE_SSL_CIPHER, PP2_SUBTYPE_SSL_SIG_ALG, PP2_SUBTYPE_SSL_KEY_ALG",
478479
.raw_bytes_in = pp2_hdr_ssl,
479480
.raw_bytes_in_length = sizeof(pp2_hdr_ssl),
480481
.rc_expected = sizeof(pp2_hdr_ssl),
@@ -523,6 +524,15 @@ int main()
523524
},
524525
},
525526
},
527+
{
528+
.name = "v1 PROXY protocol header: create and parse - AF_UNSPEC",
529+
.version = 1,
530+
.pp_info_in = {
531+
.address_family = ADDR_FAMILY_UNSPEC,
532+
.transport_protocol = TRANSPORT_PROTOCOL_UNSPEC,
533+
},
534+
.pp_info_out_expected = tests[11].pp_info_in,
535+
},
526536
};
527537

528538
/* Run tests */

0 commit comments

Comments
 (0)