Skip to content

Commit 422166e

Browse files
Merge pull request #10 from kosmas-valianos/docserrorfix
Docs and error reporting fixes
2 parents d57fa2b + f4138ec commit 422166e

4 files changed

Lines changed: 69 additions & 57 deletions

File tree

README.md

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,54 @@ The library should be compilable to any platform as it is written in ANSI C. It
1010

1111
## API
1212
### Parsing
13-
**`pp_parse()`**: parsing a PROXY protocol header. Parameters:
14-
* `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.
15-
* `uint32_t pktlen`: Data's length. Normally it will be the return value of a `recv(MSG_PEEK)`.
16-
* `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
17-
* `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.
13+
**`int32_t pp_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t *pp_info)`**:
1814

19-
You shall not pass your `pp_info_t` variable to `pp_parse()` again without first clearing it with `pp_info_clear()` (see below)
15+
Inpects the buffer for a PROXY protocol header and extracts all the information if any.
16+
* `buffer`: Pointer to a buffer with the data to parse. Normally it will be the buffer used to peek data from a socket.
17+
* `buffer_length`: Buffer's length. Typically the bytes read from the `recv(MSG_PEEK)` operation.
18+
* `pp_info`: Pointer to a `pp_info_t` structure which will get filled with all the extracted information.
19+
* `return`:
20+
* \> 0 Length of the PROXY protocol header
21+
* == 0 No PROXY protocol header found
22+
* < 0 Error occurred. `pp_strerror()` with that value can be used to get a descriptive message
2023

21-
**`pp_info_get_tlv_value()`**: extracting TLVs' values. Parameters:
22-
* `pp_info_t *pp_info`: The `pp_info_t` used in the `pp_parse()`
23-
* `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.
24-
* `uint8_t subtype`: The subtype of the TLV you are looking for (in case it is needed else 0 to get it ignored) as per the specification e.g. PP2_SUBTYPE_AWS_VPCE_ID, PP2_SUBTYPE_AZURE_PRIVATEENDPOINT_LINKID
25-
* `uint16_t *value_len_out`: The length of the value so that applications can copy and use the value properly
26-
* `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 then the 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+
You shall not pass your `pp_info_t` variable to `pp_parse()` again without first clearing it with `pp_info_clear()`
2725

28-
**`pp_info_clear()`**: clearing a `pp_info_t` structure. You **MUST** use it. Parameter:
29-
* `pp_info_t *pp_info`: A pointer to the `pp_info_t` used in `pp_parse()`
26+
**`void pp_info_clear(pp_info_t *pp_info)`**
3027

31-
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.
28+
Clears the `pp_info_t` structure and frees any allocated memory associated with it. Shall always be called after a call to `pp_parse()`
29+
* `pp_info`: Pointer to a filled `pp_info_t` structure which has been used to a previous call to `pp_parse()`
30+
31+
**`const uint8_t *pp_info_get_*(const pp_info_t *pp_info, uint16_t *length);`**
32+
33+
Searches for the specified TLV and returns its value
34+
* `pp_info` Pointer to a `pp_info_t` structure used in `pp_parse()`
35+
* `length` Pointer to a `uint16_t` where the TLV's value length will be set
36+
* `return` Pointer to a buffer holding the TLV's value if found else `NULL`. In case of US-ASCII value the buffer is `NULL` terminated
3237

3338
### Creating
34-
**`pp_create_hdr()`**: create a PROXY protocol header. Parameters:
35-
* `uint8_t version`: `1` or `2` depending on the PROXY protocol version you want to use.
36-
* `uint8_t fam`: Transport and address family. The values match exactly the specification:
37-
* v2
38-
* `'\x00'` : UNSPEC
39-
* `'\x11'` : TCP over IPv4
40-
* `'\x12'` : UDP over IPv4
41-
* `'\x21'` : TCP over IPv6
42-
* `'\x22'` : UDP over IPv6
43-
* `'\x31'` : UNIX stream
44-
* `'\x32'` : UNIX datagram
45-
* v1
46-
* `AF_INET`
47-
* `AF_INET6`
48-
* `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.
49-
* `uint32_t *pp_hdr_len`: Output parameter where the length of the the PROXY protocol header will be stored.
50-
* `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
51-
* `return value: uint8_t *`: Pointer to a dynamically allocated buffer where the PROXY protocol header exists. Shall be freed with `free()`
39+
**`uint8_t *pp_create_hdr(uint8_t version, const pp_info_t *pp_info, uint16_t *pp_hdr_len, int32_t *error)`**:
40+
41+
Creates a PROXY protocol header considering the information inside the pp_info.
42+
* `version`:
43+
* 0 Create a v1 PROXY protocol header.
44+
* 1 Create a v2 PROXY protocol header.
45+
* `pp_info`: Pointer to a filled `pp_info_t` structure whose information will be used for the creation of the PROXY protocol header.
46+
* `pp_hdr_len`: Pointer to a `uint16_t` where the length of the create PROXY protocol header will be set
47+
* `error`: Pointer to a `int32_t` where the error value will be set
48+
* `ERR_NULL` No error occurred
49+
* < 0 Error
50+
* `return`: Pointer to a heap allocated buffer containing the PROXY protocol header. Must be freed with free()
51+
52+
### Error reporting
53+
**`const char *pp_strerror(int32_t error)`**
54+
55+
Returns a descriptive error message
56+
* `error`: `int32_t` value from other API functions
57+
* `return`: Pointer to the descriptive message if the error value is recognized else `NULL`
5258

5359
## Example
5460
See `examples/client_server.c`
5561

5662
## In progress
57-
* Creating v2 PROXY protocol headers with TLVs is not yet supported. Will be added in the next release
63+
* Creating v2 PROXY protocol headers with TLVs is not yet supported. Will be added in the next major release

src/proxy_protocol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void pp_info_clear(pp_info_t *pp_info)
392392
memset(pp_info, 0, sizeof(*pp_info));
393393
}
394394

395-
uint8_t *pp2_create_hdr(const pp_info_t *pp_info, uint16_t *pp2_hdr_len, uint32_t *error)
395+
uint8_t *pp2_create_hdr(const pp_info_t *pp_info, uint16_t *pp2_hdr_len, int32_t *error)
396396
{
397397
proxy_hdr_v2_t proxy_hdr_v2 = {
398398
.sig = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A",
@@ -479,7 +479,7 @@ uint8_t *pp2_create_hdr(const pp_info_t *pp_info, uint16_t *pp2_hdr_len, uint32_
479479
return pp2_hdr;
480480
}
481481

482-
static uint8_t *pp1_create_hdr(const pp_info_t *pp_info, uint16_t *pp1_hdr_len, uint32_t *error)
482+
static uint8_t *pp1_create_hdr(const pp_info_t *pp_info, uint16_t *pp1_hdr_len, int32_t *error)
483483
{
484484
if (pp_info->transport_protocol != TRANSPORT_PROTOCOL_STREAM)
485485
{
@@ -530,7 +530,7 @@ static uint8_t *pp1_create_hdr(const pp_info_t *pp_info, uint16_t *pp1_hdr_len,
530530
return pp1_hdr;
531531
}
532532

533-
uint8_t *pp_create_hdr(uint8_t version, const pp_info_t *pp_info, uint16_t *pp_hdr_len, uint32_t *error)
533+
uint8_t *pp_create_hdr(uint8_t version, const pp_info_t *pp_info, uint16_t *pp_hdr_len, int32_t *error)
534534
{
535535
if (version == 1)
536536
{

src/proxy_protocol.h

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ typedef struct
9797
tlv_array_t *tlv_array;
9898
} pp_info_t;
9999

100-
/* Returns a descriptive error message.
100+
/* Returns a descriptive error message
101101
*
102-
* error int32_t value from other API functions.
103-
* return Pointer to the descriptive message if error value is recognized else NULL.
102+
* error int32_t value from other API functions
103+
* return Pointer to the descriptive message if the error value is recognized else NULL
104104
*/
105105
const char *pp_strerror(int32_t error);
106106

107-
/* Searches for the specified TLV and returns its value.
107+
/* Searches for the specified TLV and returns its value
108108
*
109-
* pp_info Pointer to a pp_info_t structure used in pp_parse().
110-
* length Pointer to a uint16_t where the TLV's value length will be set.
109+
* pp_info Pointer to a pp_info_t structure used in pp_parse()
110+
* length Pointer to a uint16_t where the TLV's value length will be set
111111
* return Pointer to a buffer holding the TLV's value if found else NULL.
112112
* In case of US-ASCII value the buffer is NULL terminated
113113
*/
@@ -123,28 +123,34 @@ const uint8_t *pp_info_get_ssl_key_alg(const pp_info_t *pp_info, uint16_t *lengt
123123
const uint8_t *pp_info_get_ssl_netns(const pp_info_t *pp_info, uint16_t *length);
124124
const uint8_t *pp_info_get_aws_vpce_id(const pp_info_t *pp_info, uint16_t *length);
125125
const uint8_t *pp_info_get_azure_linkid(const pp_info_t *pp_info, uint16_t *length);
126-
void pp_info_clear(pp_info_t *pp_info);
127126

128-
/* Inpects the buffer for a PROXY protocol header and extracts all the information if any.
127+
/* Clears the pp_info_t structure and frees any allocated memory associated with it. Shall always be called after a call to pp_parse_hdr()
128+
*
129+
* pp_info Pointer to a filled pp_info_t structure which has been used to a previous call to pp_parse_hdr()
130+
*/
131+
void pp_info_clear(pp_info_t *pp_info);
132+
133+
/* Creates a PROXY protocol header considering the information inside the pp_info.
129134
*
130135
* version: 0 Create a v1 PROXY protocol header
131136
* 1 Create a v2 PROXY protocol header
132-
* pp_info Pointer to a filled pp_info_t structure whose information will be used for the creation of the PROXY protocol header.
133-
* pp_hdr_len Pointer to a uint16_t where the length of the create PROXY protocol header will be set.
134-
* error Pointer to a uint32_t where the error value will be set.
137+
* pp_info Pointer to a filled pp_info_t structure whose information will be used for the creation of the PROXY protocol header
138+
* pp_hdr_len Pointer to a uint16_t where the length of the create PROXY protocol header will be set
139+
* error Pointer to a uint32_t where the error value will be set
135140
* ERR_NULL No error occurred
136141
* < 0 Error
142+
* return Pointer to a heap allocated buffer containing the PROXY protocol header. Must be freed with free()
137143
*/
138-
uint8_t *pp_create_hdr(uint8_t version, const pp_info_t *pp_info, uint16_t *pp_hdr_len, uint32_t *error);
144+
uint8_t *pp_create_hdr(uint8_t version, const pp_info_t *pp_info, uint16_t *pp_hdr_len, int32_t *error);
139145

140-
/* Inpects the buffer for a PROXY protocol header and extracts all the information if any.
146+
/* Inpects the buffer for a PROXY protocol header and extracts all the information if any
141147
*
142-
* buffer Buffer to be inspected and parsed. Typically the buffer given for a read operation
143-
* length Buffer's length. Typically the bytes read from the read operation
144-
* pp_info Pointer to a pp_info_t structure which will get filled with all the extracted information.
145-
* return > 0 Length of the PROXY protocol header.
146-
* == 0 No PROXY protocol header found.
147-
* < 0 Error occurred. pp_strerror() with that value can be used to get a descriptive message
148+
* buffer Buffer to be inspected and parsed. Typically the buffer given for a read operation
149+
* buffer_length Buffer's length. Typically the bytes read from the read operation
150+
* pp_info Pointer to a pp_info_t structure which will get filled with all the extracted information
151+
* return > 0 Length of the PROXY protocol header
152+
* == 0 No PROXY protocol header found
153+
* < 0 Error occurred. pp_strerror() with that value can be used to get a descriptive message
148154
*/
149155
int32_t pp_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t *pp_info);
150156

tests/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ int main()
406406
else
407407
{
408408
uint16_t pp_hdr_len;
409-
uint32_t error;
409+
int32_t error;
410410
uint8_t *pp_hdr = pp_create_hdr(tests[i].version, &tests[i].pp_info_in, &pp_hdr_len, &error);
411411
if (!pp_hdr || error != ERR_NULL)
412412
{

0 commit comments

Comments
 (0)