Skip to content

Commit f4138ec

Browse files
Update README
1 parent 9532796 commit f4138ec

2 files changed

Lines changed: 41 additions & 35 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.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ typedef struct
100100
/* Returns a descriptive error message
101101
*
102102
* error int32_t value from other API functions
103-
* return Pointer to the descriptive message if error value is recognized else NULL
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

0 commit comments

Comments
 (0)