Skip to content

Commit d87d500

Browse files
Update the example
1 parent 7f67724 commit d87d500

2 files changed

Lines changed: 58 additions & 35 deletions

File tree

examples/client_server.c

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <string.h>
34
#ifdef _WIN32
45
#include <ws2tcpip.h>
56
#pragma comment(lib, "ws2_32.lib")
@@ -13,26 +14,27 @@ int main()
1314
{
1415
// Create a v1 PROXY protocol header
1516
pp_info_t pp_info_in = {
17+
.address_family = ADDR_FAMILY_INET,
18+
.transport_protocol = TRANSPORT_PROTOCOL_STREAM,
1619
.src_addr = "172.22.32.1",
1720
.dst_addr = "172.22.33.1",
1821
.src_port = 4040,
1922
.dst_port = 443
2023
};
21-
uint32_t pp1_hdr_len;
24+
uint16_t pp1_hdr_len;
2225
uint32_t error;
23-
uint8_t *pp1_hdr = pp_create_hdr(1, AF_INET, &pp_info_in, &pp1_hdr_len, &error);
24-
if (!pp1_hdr)
26+
uint8_t *pp1_hdr = pp_create_hdr(1, &pp_info_in, &pp1_hdr_len, &error);
27+
if (error != ERR_NULL)
2528
{
2629
fprintf(stderr, "pp_create_hdr() failed: %s", pp_strerror(error));
27-
free(pp1_hdr);
2830
return EXIT_FAILURE;
2931
}
3032

31-
// Parse
33+
// Parse a v1 PROXY protocol header
3234
pp_info_t pp_info_out;
3335
int32_t rc = pp_parse_hdr(pp1_hdr, pp1_hdr_len, &pp_info_out);
3436
free(pp1_hdr);
35-
if (rc == 0)
37+
if (!rc)
3638
{
3739
printf("Not a PROXY protocol header\n");
3840
}
@@ -51,32 +53,36 @@ int main()
5153
}
5254
pp_info_clear(&pp_info_out);
5355

54-
// Parse
55-
uint8_t pp2_hdr_vpce[] = {
56-
0x0d, 0x0a, 0x0d, 0x0a, /* Start of v2 signature */
57-
0x00, 0x0d, 0x0a, 0x51,
58-
0x55, 0x49, 0x54, 0x0a, /* End of v2 signature */
59-
0x21, 0x11, 0x00, 0x40, /* ver_cmd, fam and len */
60-
0xc0, 0xa8, 0x0a, 0x64, /* Source IP */
61-
0xc0, 0xa8, 0x0b, 0x5a, /* Destination IP */
62-
0xa5, 0x5c, 0x1f, 0x90, /* Source port, Destination port */
63-
0x03, 0x00, 0x04, 0xe5, /* CRC32C TLV start */
64-
0x18, 0x86, 0xf8, 0xea, /* CRC32C TLV end, AWS VPCE ID TLV start */
65-
0x00, 0x17, 0x01, 0x76,
66-
0x70, 0x63, 0x65, 0x2d,
67-
0x32, 0x33, 0x64, 0x38,
68-
0x65, 0x7a, 0x6a, 0x6b,
69-
0x33, 0x38, 0x62, 0x63,
70-
0x68, 0x69, 0x6c, 0x6d,
71-
0x34, 0x04, 0x00, 0x10, /* AWS VPCE ID TLV end, NOOP TLV start */
72-
0x00, 0x00, 0x00, 0x00,
73-
0x00, 0x00, 0x00, 0x00,
74-
0x00, 0x00, 0x00, 0x00,
75-
0x00, 0x00, 0x00, 0x00, /* NOOP TLV end */
56+
// Create a v2 PROXY protocol header with some TLVs
57+
pp_info_t pp_info_in_v2 = {
58+
.address_family = ADDR_FAMILY_INET,
59+
.transport_protocol = TRANSPORT_PROTOCOL_STREAM,
60+
.src_addr = "192.168.10.100",
61+
.dst_addr = "192.168.11.90",
62+
.src_port = 42332,
63+
.dst_port = 8080,
64+
.pp2_info = {
65+
.crc32c = 1, /* Add crc32c checksum */
66+
.pp2_ssl_info = { /* Add SSL information */
67+
.ssl = 1,
68+
.cert_in_connection = 1,
69+
.cert_in_session = 1,
70+
.cert_verified = 1,
71+
}
72+
}
7673
};
74+
pp_info_add_ssl(&pp_info_in_v2, "TLSv1.2", "ECDHE-RSA-AES128-GCM-SHA256", "SHA256", "RSA2048", "example.com", 11);
75+
pp_info_add_azure_linkid(&pp_info_in_v2, 1234);
76+
uint8_t *pp2_hdr = pp_create_hdr(2, &pp_info_in_v2, &pp1_hdr_len, &error);
77+
if (error != ERR_NULL)
78+
{
79+
fprintf(stderr, "pp_create_hdr() failed: %s", pp_strerror(error));
80+
return EXIT_FAILURE;
81+
}
7782

78-
rc = pp_parse_hdr(pp2_hdr_vpce, sizeof(pp2_hdr_vpce), &pp_info_out);
79-
if (rc == 0)
83+
rc = pp_parse_hdr(pp2_hdr, pp1_hdr_len, &pp_info_out);
84+
free(pp2_hdr);
85+
if (!rc)
8086
{
8187
printf("Not a PROXY protocol header\n");
8288
}
@@ -88,10 +94,27 @@ int main()
8894
}
8995
else
9096
{
91-
uint16_t tlv_value_len;
92-
char *vpc_id = pp_info_get_tlv_value(&pp_info_out, PP2_TYPE_AWS, PP2_SUBTYPE_AWS_VPCE_ID, &tlv_value_len);
93-
printf("%d bytes PROXY protocol header: AWS VPC ID: %s. %s %s %hu %hu\n",
94-
rc, vpc_id,
97+
uint16_t length, cn_length;
98+
const uint8_t *azure_linkid = pp_info_get_azure_linkid(&pp_info_out, &length);
99+
uint32_t linkid;
100+
memcpy(&linkid, azure_linkid, length);
101+
const uint8_t *cn = pp_info_get_ssl_cn(&pp_info_out, &cn_length);
102+
printf("%d bytes PROXY protocol header:\n"
103+
"\tAzure Link ID: %u\n"
104+
"\tCRC32C checksum: %s\n"
105+
"\tSSL version: %s\n"
106+
"\tSSL cipher: %s\n"
107+
"\tSSL sig_alg: %s\n"
108+
"\tSSL key_alg: %s\n"
109+
"\tSSL CN: %*s\n"
110+
"%s %s %hu %hu\n",
111+
rc, linkid,
112+
pp_info_out.pp2_info.crc32c == 1 ? "verified" : "not present",
113+
pp_info_get_ssl_version(&pp_info_out, &length),
114+
pp_info_get_ssl_cipher(&pp_info_out, &length),
115+
pp_info_get_ssl_sig_alg(&pp_info_out, &length),
116+
pp_info_get_ssl_key_alg(&pp_info_out, &length),
117+
cn_length, cn,
95118
pp_info_out.src_addr, pp_info_out.dst_addr,
96119
pp_info_out.src_port, pp_info_out.dst_port);
97120
}

src/proxy_protocol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ static const uint8_t *pp_info_get_tlv_value(const pp_info_t *pp_info, uint8_t ty
388388
uint32_t i;
389389
for (i = 0; i < pp_info->pp2_info.tlv_array.len; i++)
390390
{
391-
pp2_tlv_t * tlv = pp_info->pp2_info.tlv_array.tlvs[i];
391+
pp2_tlv_t *tlv = pp_info->pp2_info.tlv_array.tlvs[i];
392392
if (tlv->type == type)
393393
{
394394
*length = tlv->length_hi << 8 | tlv->length_lo;

0 commit comments

Comments
 (0)