Skip to content

Commit b204c68

Browse files
v1/v2 Create/Parse PROXY protocol headers
0 parents  commit b204c68

9 files changed

Lines changed: 2338 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
libs/libproxyprotocol.so
3+
tests/test_libproxyprotocol

COPYING

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

COPYING.LESSER

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
GNU LESSER GENERAL PUBLIC LICENSE
2+
Version 3, 29 June 2007
3+
4+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5+
Everyone is permitted to copy and distribute verbatim copies
6+
of this license document, but changing it is not allowed.
7+
8+
9+
This version of the GNU Lesser General Public License incorporates
10+
the terms and conditions of version 3 of the GNU General Public
11+
License, supplemented by the additional permissions listed below.
12+
13+
0. Additional Definitions.
14+
15+
As used herein, "this License" refers to version 3 of the GNU Lesser
16+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
17+
General Public License.
18+
19+
"The Library" refers to a covered work governed by this License,
20+
other than an Application or a Combined Work as defined below.
21+
22+
An "Application" is any work that makes use of an interface provided
23+
by the Library, but which is not otherwise based on the Library.
24+
Defining a subclass of a class defined by the Library is deemed a mode
25+
of using an interface provided by the Library.
26+
27+
A "Combined Work" is a work produced by combining or linking an
28+
Application with the Library. The particular version of the Library
29+
with which the Combined Work was made is also called the "Linked
30+
Version".
31+
32+
The "Minimal Corresponding Source" for a Combined Work means the
33+
Corresponding Source for the Combined Work, excluding any source code
34+
for portions of the Combined Work that, considered in isolation, are
35+
based on the Application, and not on the Linked Version.
36+
37+
The "Corresponding Application Code" for a Combined Work means the
38+
object code and/or source code for the Application, including any data
39+
and utility programs needed for reproducing the Combined Work from the
40+
Application, but excluding the System Libraries of the Combined Work.
41+
42+
1. Exception to Section 3 of the GNU GPL.
43+
44+
You may convey a covered work under sections 3 and 4 of this License
45+
without being bound by section 3 of the GNU GPL.
46+
47+
2. Conveying Modified Versions.
48+
49+
If you modify a copy of the Library, and, in your modifications, a
50+
facility refers to a function or data to be supplied by an Application
51+
that uses the facility (other than as an argument passed when the
52+
facility is invoked), then you may convey a copy of the modified
53+
version:
54+
55+
a) under this License, provided that you make a good faith effort to
56+
ensure that, in the event an Application does not supply the
57+
function or data, the facility still operates, and performs
58+
whatever part of its purpose remains meaningful, or
59+
60+
b) under the GNU GPL, with none of the additional permissions of
61+
this License applicable to that copy.
62+
63+
3. Object Code Incorporating Material from Library Header Files.
64+
65+
The object code form of an Application may incorporate material from
66+
a header file that is part of the Library. You may convey such object
67+
code under terms of your choice, provided that, if the incorporated
68+
material is not limited to numerical parameters, data structure
69+
layouts and accessors, or small macros, inline functions and templates
70+
(ten or fewer lines in length), you do both of the following:
71+
72+
a) Give prominent notice with each copy of the object code that the
73+
Library is used in it and that the Library and its use are
74+
covered by this License.
75+
76+
b) Accompany the object code with a copy of the GNU GPL and this license
77+
document.
78+
79+
4. Combined Works.
80+
81+
You may convey a Combined Work under terms of your choice that,
82+
taken together, effectively do not restrict modification of the
83+
portions of the Library contained in the Combined Work and reverse
84+
engineering for debugging such modifications, if you also do each of
85+
the following:
86+
87+
a) Give prominent notice with each copy of the Combined Work that
88+
the Library is used in it and that the Library and its use are
89+
covered by this License.
90+
91+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
92+
document.
93+
94+
c) For a Combined Work that displays copyright notices during
95+
execution, include the copyright notice for the Library among
96+
these notices, as well as a reference directing the user to the
97+
copies of the GNU GPL and this license document.
98+
99+
d) Do one of the following:
100+
101+
0) Convey the Minimal Corresponding Source under the terms of this
102+
License, and the Corresponding Application Code in a form
103+
suitable for, and under terms that permit, the user to
104+
recombine or relink the Application with a modified version of
105+
the Linked Version to produce a modified Combined Work, in the
106+
manner specified by section 6 of the GNU GPL for conveying
107+
Corresponding Source.
108+
109+
1) Use a suitable shared library mechanism for linking with the
110+
Library. A suitable mechanism is one that (a) uses at run time
111+
a copy of the Library already present on the user's computer
112+
system, and (b) will operate properly with a modified version
113+
of the Library that is interface-compatible with the Linked
114+
Version.
115+
116+
e) Provide Installation Information, but only if you would otherwise
117+
be required to provide such information under section 6 of the
118+
GNU GPL, and only to the extent that such information is
119+
necessary to install and execute a modified version of the
120+
Combined Work produced by recombining or relinking the
121+
Application with a modified version of the Linked Version. (If
122+
you use option 4d0, the Installation Information must accompany
123+
the Minimal Corresponding Source and Corresponding Application
124+
Code. If you use option 4d1, you must provide the Installation
125+
Information in the manner specified by section 6 of the GNU GPL
126+
for conveying Corresponding Source.)
127+
128+
5. Combined Libraries.
129+
130+
You may place library facilities that are a work based on the
131+
Library side by side in a single library together with other library
132+
facilities that are not Applications and are not covered by this
133+
License, and convey such a combined library under terms of your
134+
choice, if you do both of the following:
135+
136+
a) Accompany the combined library with a copy of the same work based
137+
on the Library, uncombined with any other library facilities,
138+
conveyed under the terms of this License.
139+
140+
b) Give prominent notice with the combined library that part of it
141+
is a work based on the Library, and explaining where to find the
142+
accompanying uncombined form of the same work.
143+
144+
6. Revised Versions of the GNU Lesser General Public License.
145+
146+
The Free Software Foundation may publish revised and/or new versions
147+
of the GNU Lesser General Public License from time to time. Such new
148+
versions will be similar in spirit to the present version, but may
149+
differ in detail to address new problems or concerns.
150+
151+
Each version is given a distinguishing version number. If the
152+
Library as you received it specifies that a certain numbered version
153+
of the GNU Lesser General Public License "or any later version"
154+
applies to it, you have the option of following the terms and
155+
conditions either of that published version or of any later version
156+
published by the Free Software Foundation. If the Library as you
157+
received it does not specify a version number of the GNU Lesser
158+
General Public License, you may choose any version of the GNU Lesser
159+
General Public License ever published by the Free Software Foundation.
160+
161+
If the Library as you received it specifies that a proxy can decide
162+
whether future versions of the GNU Lesser General Public License shall
163+
apply, that proxy's public statement of acceptance of any version is
164+
permanent authorization for you to choose that version for the
165+
Library.

Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# libproxyprotocol is an ANSI C library to parse and create PROXY protocol v1 and v2 headers
3+
# Copyright (C) 2022 Kosmas Valianos (kosmas.valianos@gmail.com)
4+
#
5+
# The libproxyprotocol library is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Lesser General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# The libproxyprotocol library is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public License
16+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
#
18+
19+
CFLAGS := -Wall -Wextra -Wshadow -ansi -fshort-enums -fpic
20+
21+
all: build
22+
23+
build: libs_dir libs/libproxyprotocol.so
24+
25+
libs_dir:
26+
mkdir -p libs
27+
28+
libs/libproxyprotocol.so: src/proxy_protocol.o
29+
$(CC) -shared -o $@ $+
30+
31+
src/%.o: %.c src/proxy_protocol.h
32+
$(CC) ${CFLAGS} -c -o $@ $<
33+
34+
tests: tests/test_libproxyprotocol
35+
LD_LIBRARY_PATH=libs/ $<
36+
37+
tests/test_libproxyprotocol: tests/test.o libs/libproxyprotocol.so
38+
$(CC) -Llibs/ ${CFLAGS} -o $@ $< -lproxyprotocol
39+
40+
clean:
41+
$(RM) src/*.o libs/libproxyprotocol.so
42+
$(RM) tests/*.o tests/test_libproxyprotocol

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# libproxyprotocol
2+
An ANSI C library to parse and create [PROXY protocol](https://www.haproxy.org/download/2.6/doc/proxy-protocol.txt) v1 and v2 headers
3+
>The PROXY protocol provides a convenient way to safely transport connection information such as a client's address across multiple layers of NAT or TCP proxies. It is designed to require little changes to existing components and to limit the performance impact caused by the processing of the transported information.
4+
5+
## Installation
6+
The library should be compilable to any platform as it is written in ANSI C. It comes with a Makefile which can create the shared library `libproxyprotocol.so` which can then be linked to your application. Special care has been taken to make it work with Windows as well. In that case you have to compile it to a DLL yourself. In case of windows remember that you have to link with the `ws2_32.lib`. An example of this is shown in tests.
7+
8+
## API
9+
### Parsing
10+
To parse a PROXY protocol header you need to use `pp_parse()` with the following parameters:
11+
* `uint8_t *pkt`: Pointer to a buffer with the data to parse. Normally it will be the buffer used to peek data from a socket.
12+
* `uint32_t pktlen`: Data's length. Normally it will be the return value of a `recv(MSG_PEEK)`.
13+
* `pp_info_t *proxy_info`: Pointer to a `pp_info_t` variable which will be used to save all the extracted information of the PROXY protocol header including the TLVs
14+
* `return value: int32_t`: The length of the PROXY protocol header in case of success or a negative integer in case of error. You can use `pp_strerror()` to get a descriptive error message. In case the data dont't match any of the v1/v2 signatures `0` is returned.
15+
16+
You shall not pass your `pp_info_t` variable to `pp_parse()` again without first clearing it with `pp_info_clear()` (see below)
17+
18+
To extract TLVs' values it is strongly suggested that you use `pp_info_get_tlv_value()` with the following parameters:
19+
* `pp_info_t *pp_info`: The `pp_info_t` used in the `pp_parse()`
20+
* `uint8_t type`: The type of the TLV you are looking for as per the specification e.g. PP2_TYPE_AWS, PP2_TYPE_AZURE etc.
21+
* `uint8_t subtype`: The subtype of the TLV you are looking for (in case it is needed or just a value <= 0 to be ignored) as per the specification e.g. PP2_SUBTYPE_AWS_VPCE_ID, PP2_SUBTYPE_AZURE_PRIVATEENDPOINT_LINKID
22+
* `uint16_t *value_len_out`: The length of the value so that applications can copy and use the value properly
23+
* `return value: uint8_t *`: Pointer to the value. In case the value is a string e.g. PP2_TYPE_AWS-PP2_SUBTYPE_AWS_VPCE_ID thenthe buffer is NULL terminated so that it can be used directly for string operations like `strcmp()` etc. **Do not manipulate these data in any way, rather make copies of them if you need to modify them.**
24+
25+
To clear a `pp_info_t` structure you **MUST** use `pp_info_clear()` with the following parameter:
26+
* `pp_info_t *pp_info`: A pointer to the `pp_info_t` used in `pp_parse()`
27+
28+
It basically clears the saved TLVs structure. For v1 it is not really needed as there are not any TLVs but to be safe always use it! A PROXY protocol sender might change from v1 to v2 so better to have your application prepared.
29+
30+
### Creating
31+
To create a PROXY protocol header you need to use `pp_create_hdr()` with the following parameters:
32+
* `uint8_t version`: `1` or `2` depending on the PROXY protocol version you want to use.
33+
* `uint8_t fam`: Transport and address family. The values match exactly the specification:
34+
* v2
35+
* `'\x00'` : UNSPEC
36+
* `'\x11'` : TCP over IPv4
37+
* `'\x12'` : UDP over IPv4
38+
* `'\x21'` : TCP over IPv6
39+
* `'\x22'` : UDP over IPv6
40+
* `'\x31'` : UNIX stream
41+
* `'\x32'` : UNIX datagram
42+
* v1
43+
* `AF_INET`
44+
* `AF_INET6`
45+
* `pp_info_t *pp_info` : Pointer to a filled `pp_info_t` structure. Note that at the moment tlvs from the `tlv_array_t tlv_array` inside it will not be included in the header. This functionality will be added with the next release.
46+
* `uint32_t *pp_hdr_len`: Output parameter where the length of the the PROXY protocol header will be stored.
47+
* `uint32_t *error`: Outpur parameter where its value will be set to a negative integer in case of error or `ERR_NULL` in case of success. You can use `pp_strerror()` to get a descriptive error message
48+
* `return value: uint8_t *`: Pointer to a dynamically allocated buffer where the PROXY protocol header exists. Shall be freed with `free()`
49+
50+
## Example
51+
See `examples/client_server.c`
52+
53+
## Limitations
54+
* Creating v2 PROXY protocol headers with TLVs is not yet supported. Will be added in the next release 1.0.0
55+
* Parsing `PP2_TYPE_SSL` TLV of the v2 PROXY protocol header is not yet supported. TLVs of this type will not be saved in the `pp_info_t` structure. Will be added in the next release 1.0.0
56+
* The library is not heavily tested. Much more test coverage will be added in the next release 1.0.0

examples/client_server.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <arpa/inet.h>
5+
6+
#include "../src/proxy_protocol.h"
7+
8+
int main()
9+
{
10+
// Create a v1 PROXY protocol header
11+
pp_info_t pp_info_in = {
12+
.src_addr = "172.22.32.1",
13+
.dst_addr = "172.22.33.1",
14+
.src_port = 4040,
15+
.dst_port = 443
16+
};
17+
uint32_t pp1_hdr_len;
18+
uint32_t error;
19+
uint8_t *pp1_hdr = pp_create_hdr(1, AF_INET, &pp_info_in, &pp1_hdr_len, &error);
20+
if (!pp1_hdr)
21+
{
22+
fprintf(stderr, "pp_create_hdr() failed: %s", pp_strerror(error));
23+
free(pp1_hdr);
24+
return EXIT_FAILURE;
25+
}
26+
27+
// Parse
28+
pp_info_t pp_info_out;
29+
int32_t rc = pp_parse_hdr(pp1_hdr, pp1_hdr_len, &pp_info_out);
30+
free(pp1_hdr);
31+
if (rc == 0)
32+
{
33+
printf("Not a PROXY protocol header\n");
34+
}
35+
else if (rc < 0)
36+
{
37+
fprintf(stderr, "pp_parse_hdr() failed: %s", pp_strerror(rc));
38+
pp_info_clear(&pp_info_out);
39+
return EXIT_FAILURE;
40+
}
41+
else
42+
{
43+
printf("%d bytes PROXY protocol header: %s %s %hu %hu\n",
44+
rc,
45+
pp_info_out.src_addr, pp_info_out.dst_addr,
46+
pp_info_out.src_port, pp_info_out.dst_port);
47+
}
48+
pp_info_clear(&pp_info_out);
49+
50+
// Parse
51+
uint8_t pp2_hdr_vpce[] = {
52+
0x0d, 0x0a, 0x0d, 0x0a, /* Start of v2 signature */
53+
0x00, 0x0d, 0x0a, 0x51,
54+
0x55, 0x49, 0x54, 0x0a, /* End of v2 signature */
55+
0x21, 0x11, 0x00, 0x40, /* ver_cmd, fam and len */
56+
0xc0, 0xa8, 0x0a, 0x64, /* Source IP */
57+
0xc0, 0xa8, 0x0b, 0x5a, /* Destination IP */
58+
0xa5, 0x5c, 0x1f, 0x90, /* Source port, Destination port */
59+
0x03, 0x00, 0x04, 0xe5, /* CRC32C TLV start */
60+
0x18, 0x86, 0xf8, 0xea, /* CRC32C TLV end, AWS VPCE ID TLV start */
61+
0x00, 0x17, 0x01, 0x76,
62+
0x70, 0x63, 0x65, 0x2d,
63+
0x32, 0x33, 0x64, 0x38,
64+
0x65, 0x7a, 0x6a, 0x6b,
65+
0x33, 0x38, 0x62, 0x63,
66+
0x68, 0x69, 0x6c, 0x6d,
67+
0x34, 0x04, 0x00, 0x10, /* AWS VPCE ID TLV end, NOOP TLV start */
68+
0x00, 0x00, 0x00, 0x00,
69+
0x00, 0x00, 0x00, 0x00,
70+
0x00, 0x00, 0x00, 0x00,
71+
0x00, 0x00, 0x00, 0x00, /* NOOP TLV end */
72+
};
73+
74+
rc = pp_parse_hdr(pp2_hdr_vpce, sizeof(pp2_hdr_vpce), &pp_info_out);
75+
if (rc == 0)
76+
{
77+
printf("Not a PROXY protocol header\n");
78+
}
79+
else if (rc < 0)
80+
{
81+
fprintf(stderr, "pp_parse_hdr() failed: %s", pp_strerror(rc));
82+
pp_info_clear(&pp_info_out);
83+
return EXIT_FAILURE;
84+
}
85+
else
86+
{
87+
uint16_t tlv_value_len;
88+
char *vpc_id = pp_info_get_tlv_value(&pp_info_out, PP2_TYPE_AWS, PP2_SUBTYPE_AWS_VPCE_ID, &tlv_value_len);
89+
printf("%d bytes PROXY protocol header: AWS VPC ID: %s. %s %s %hu %hu\n",
90+
rc, vpc_id,
91+
pp_info_out.src_addr, pp_info_out.dst_addr,
92+
pp_info_out.src_port, pp_info_out.dst_port);
93+
}
94+
pp_info_clear(&pp_info_out);
95+
96+
return EXIT_SUCCESS;
97+
}

0 commit comments

Comments
 (0)