1212
1313int main ()
1414{
15- // Create a v1 PROXY protocol header
16- pp_info_t pp_info_in = {
15+ /* Create a v1 PROXY protocol header */
16+ pp_info_t pp_info_in_v1 = {
1717 .address_family = ADDR_FAMILY_INET ,
1818 .transport_protocol = TRANSPORT_PROTOCOL_STREAM ,
1919 .src_addr = "172.22.32.1" ,
@@ -23,14 +23,16 @@ int main()
2323 };
2424 uint16_t pp1_hdr_len ;
2525 uint32_t error ;
26- uint8_t * pp1_hdr = pp_create_hdr (1 , & pp_info_in , & pp1_hdr_len , & error );
26+ uint8_t * pp1_hdr = pp_create_hdr (1 , & pp_info_in_v1 , & pp1_hdr_len , & error );
27+ /* Clear the pp_info passed in pp_create_hdr(). Not really needed for v1 but good to do out of principle */
28+ pp_info_clear (& pp_info_in_v1 );
2729 if (error != ERR_NULL )
2830 {
2931 fprintf (stderr , "pp_create_hdr() failed: %s" , pp_strerror (error ));
3032 return EXIT_FAILURE ;
3133 }
3234
33- // Parse a v1 PROXY protocol header
35+ /* Parse a v1 PROXY protocol header */
3436 pp_info_t pp_info_out ;
3537 int32_t rc = pp_parse_hdr (pp1_hdr , pp1_hdr_len , & pp_info_out );
3638 free (pp1_hdr );
@@ -51,9 +53,10 @@ int main()
5153 pp_info_out .src_addr , pp_info_out .dst_addr ,
5254 pp_info_out .src_port , pp_info_out .dst_port );
5355 }
56+ /* ALWAYS clear the pp_info after a call to pp_parse_hdr() */
5457 pp_info_clear (& pp_info_out );
5558
56- // Create a v2 PROXY protocol header with some TLVs
59+ /* Create a v2 PROXY protocol header with some TLVs */
5760 pp_info_t pp_info_in_v2 = {
5861 .address_family = ADDR_FAMILY_INET ,
5962 .transport_protocol = TRANSPORT_PROTOCOL_STREAM ,
@@ -71,15 +74,20 @@ int main()
7174 }
7275 }
7376 };
77+ /* Add SSL TLVs */
7478 pp_info_add_ssl (& pp_info_in_v2 , "TLSv1.2" , "ECDHE-RSA-AES128-GCM-SHA256" , "SHA256" , "RSA2048" , "example.com" , 11 );
79+ /* Add Azure Link ID TLV */
7580 pp_info_add_azure_linkid (& pp_info_in_v2 , 1234 );
7681 uint8_t * pp2_hdr = pp_create_hdr (2 , & pp_info_in_v2 , & pp1_hdr_len , & error );
82+ /* IMPORTANT: Clear the pp_info passed in pp_create_hdr() because TLVs were created. Otherwise memory will be leaked */
83+ pp_info_clear (& pp_info_in_v2 );
7784 if (error != ERR_NULL )
7885 {
7986 fprintf (stderr , "pp_create_hdr() failed: %s" , pp_strerror (error ));
8087 return EXIT_FAILURE ;
8188 }
8289
90+ /* Parse a v2 PROXY protocol header */
8391 rc = pp_parse_hdr (pp2_hdr , pp1_hdr_len , & pp_info_out );
8492 free (pp2_hdr );
8593 if (!rc )
@@ -106,7 +114,7 @@ int main()
106114 "\tSSL cipher: %s\n"
107115 "\tSSL sig_alg: %s\n"
108116 "\tSSL key_alg: %s\n"
109- "\tSSL CN: %*s\n"
117+ "\tSSL CN: %. *s\n"
110118 "%s %s %hu %hu\n" ,
111119 rc , linkid ,
112120 pp_info_out .pp2_info .crc32c == 1 ? "verified" : "not present" ,
@@ -118,6 +126,7 @@ int main()
118126 pp_info_out .src_addr , pp_info_out .dst_addr ,
119127 pp_info_out .src_port , pp_info_out .dst_port );
120128 }
129+ /* ALWAYS clear the pp_info after a call to pp_parse_hdr() */
121130 pp_info_clear (& pp_info_out );
122131
123132 return EXIT_SUCCESS ;
0 commit comments