Skip to content

Commit 3dddc86

Browse files
Merge pull request #16 from kosmas-valianos/minorFixes
Minor fixes
2 parents 683485c + 31d1a06 commit 3dddc86

4 files changed

Lines changed: 28 additions & 16 deletions

File tree

.github/workflows/codeql-analysis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242

4343
# Initializes the CodeQL tools for scanning.
4444
- name: Initialize CodeQL
45-
uses: github/codeql-action/init@v1
45+
uses: github/codeql-action/init@v2
4646
with:
4747
languages: ${{ matrix.language }}
4848
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -53,7 +53,7 @@ jobs:
5353
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
5454
# If this step fails, then you should remove it and run the build manually (see below)
5555
- name: Autobuild
56-
uses: github/codeql-action/autobuild@v1
56+
uses: github/codeql-action/autobuild@v2
5757

5858
# ℹ️ Command-line programs to run using the OS shell.
5959
# 📚 https://git.io/JvXDl
@@ -67,4 +67,4 @@ jobs:
6767
# make release
6868

6969
- name: Perform CodeQL Analysis
70-
uses: github/codeql-action/analyze@v1
70+
uses: github/codeql-action/analyze@v2

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
CFLAGS := -Wall -Wextra -Wshadow -Wimplicit-fallthrough=0 -ansi -fshort-enums -fpic
2020

21-
all: build
21+
all: build tests example
2222

2323
build: libs_dir libs/libproxyprotocol.so
2424

@@ -37,6 +37,13 @@ tests: tests/test_libproxyprotocol
3737
tests/test_libproxyprotocol: tests/test.o libs/libproxyprotocol.so
3838
$(CC) -Llibs/ ${CFLAGS} -o $@ $< -lproxyprotocol
3939

40+
example: examples/client_server
41+
LD_LIBRARY_PATH=libs/ $<
42+
43+
examples/client_server: examples/client_server.o libs/libproxyprotocol.so
44+
$(CC) -Llibs/ ${CFLAGS} -o $@ $< -lproxyprotocol
45+
4046
clean:
4147
$(RM) src/*.o libs/libproxyprotocol.so
4248
$(RM) tests/*.o tests/test_libproxyprotocol
49+
$(RM) examples/*.o examples/client_server

examples/client_server.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ int main()
7474
}
7575
}
7676
};
77+
uint16_t pp2_hdr_len;
7778
/* Add SSL TLVs */
7879
/* IMPORTANT: Always clear the pp_info to be passed in pp_create_hdr() because TLVs are allocated in heap. Otherwise memory will be leaked */
79-
if (!pp_info_add_ssl(&pp_info_in_v2, "TLSv1.2", "ECDHE-RSA-AES128-GCM-SHA256", "SHA256", "RSA2048", "example.com", 11))
80+
if (!pp_info_add_ssl(&pp_info_in_v2, "TLSv1.2", "ECDHE-RSA-AES128-GCM-SHA256", "SHA256", "RSA2048", (uint8_t*) "example.com", 11))
8081
{
8182
fprintf(stderr, "pp_info_add_ssl() failed\n");
8283
pp_info_clear(&pp_info_in_v2);
@@ -89,7 +90,7 @@ int main()
8990
pp_info_clear(&pp_info_in_v2);
9091
return EXIT_FAILURE;
9192
}
92-
uint8_t *pp2_hdr = pp_create_hdr(2, &pp_info_in_v2, &pp1_hdr_len, &error);
93+
uint8_t *pp2_hdr = pp_create_hdr(2, &pp_info_in_v2, &pp2_hdr_len, &error);
9394
pp_info_clear(&pp_info_in_v2);
9495
if (error != ERR_NULL)
9596
{
@@ -98,7 +99,7 @@ int main()
9899
}
99100

100101
/* Parse a v2 PROXY protocol header */
101-
rc = pp_parse_hdr(pp2_hdr, pp1_hdr_len, &pp_info_out);
102+
rc = pp_parse_hdr(pp2_hdr, pp2_hdr_len, &pp_info_out);
102103
free(pp2_hdr);
103104
if (!rc)
104105
{
@@ -127,6 +128,7 @@ int main()
127128
"\tSSL CN: %.*s\n"
128129
"\t%s %s %hu %hu\n",
129130
rc, linkid,
131+
/* In case CRC32c is wrong then rc < 0 => pp_strerror(rc) at previous block will print the error */
130132
pp_info_out.pp2_info.crc32c == 1 ? "verified" : "not present",
131133
pp_info_get_ssl_version(&pp_info_out, &length),
132134
pp_info_get_ssl_cipher(&pp_info_out, &length),

src/proxy_protocol.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
#include <string.h>
2222
#ifdef _WIN32
2323
#include <ws2tcpip.h>
24+
/* Caution: To be used only with fixed length arrays */
25+
#define _sprintf(buffer, format, ...) sprintf_s(buffer, sizeof(buffer), format, __VA_ARGS__)
2426
#else
2527
#include <arpa/inet.h>
28+
/* sprintf() as snprintf does not exist in ANSI C */
29+
#define _sprintf(buffer, format, ...) sprintf(buffer, format, __VA_ARGS__)
2630
#endif
2731

2832
#include "proxy_protocol.h"
@@ -149,8 +153,8 @@ typedef struct
149153
#define PP2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
150154

151155
/* ANSI C makes us suffer as we cannot have value[0] */
152-
#define sizeof_pp2_tlv_t (sizeof(pp2_tlv_t) - 1)
153-
#define sizeof_pp2_tlv_aws_t (sizeof(pp2_tlv_aws_t) - 1)
156+
#define sizeof_pp2_tlv_t ((uint16_t) sizeof(pp2_tlv_t) - 1)
157+
#define sizeof_pp2_tlv_aws_t ((uint16_t) sizeof(pp2_tlv_aws_t) - 1)
154158

155159
/****************************************************************/
156160

@@ -347,7 +351,7 @@ uint8_t pp_info_add_netns(pp_info_t *pp_info, const char *netns)
347351

348352
uint8_t pp_info_add_aws_vpce_id(pp_info_t *pp_info, const char *vpce_id)
349353
{
350-
uint16_t length = (uint16_t) (sizeof_pp2_tlv_aws_t + strlen(vpce_id));
354+
uint16_t length = sizeof_pp2_tlv_aws_t + (uint16_t) strlen(vpce_id);
351355
pp2_tlv_aws_t *pp2_tlv_aws = malloc(length);
352356
pp2_tlv_aws->type = PP2_SUBTYPE_AWS_VPCE_ID;
353357
memcpy(pp2_tlv_aws->value, vpce_id, strlen(vpce_id));
@@ -663,7 +667,7 @@ uint8_t *pp2_create_hdr(const pp_info_t *pp_info, uint16_t *pp2_hdr_len, int32_t
663667
{
664668
uint16_t pp2_hdr_len_padded = (*pp2_hdr_len / alignment + 1) * alignment;
665669
/* The NOOP TLV needs to be at least 3 bytes because a TLV can not be smaller than that */
666-
if (pp2_hdr_len_padded - *pp2_hdr_len < 3)
670+
if (pp2_hdr_len_padded - *pp2_hdr_len < sizeof_pp2_tlv_t)
667671
{
668672
pp2_hdr_len_padded += alignment;
669673
}
@@ -763,8 +767,7 @@ static uint8_t *pp1_create_hdr(const pp_info_t *pp_info, uint16_t *pp1_hdr_len,
763767
char dst_addr[39+1];
764768
memcpy(src_addr, pp_info->src_addr, sizeof(src_addr));
765769
memcpy(dst_addr, pp_info->dst_addr, sizeof(dst_addr));
766-
/* sprintf() as snprintf does not exist in ANSI C */
767-
*pp1_hdr_len = sprintf(block, "PROXY %s %s %s %hu %hu"CRLF, fam, src_addr, dst_addr, pp_info->src_port, pp_info->dst_port);
770+
*pp1_hdr_len = _sprintf(block, "PROXY %s %s %s %hu %hu"CRLF, fam, src_addr, dst_addr, pp_info->src_port, pp_info->dst_port);
768771
}
769772
else
770773
{
@@ -932,11 +935,11 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
932935

933936
/* TLVs */
934937
/* Any TLV vector must be at least 3 bytes */
935-
while (tlv_vectors_len > 3)
938+
while (tlv_vectors_len > sizeof_pp2_tlv_t)
936939
{
937940
pp2_tlv_t *pp2_tlv = (pp2_tlv_t*) buffer;
938941
uint16_t pp2_tlv_len = pp2_tlv->length_hi << 8 | pp2_tlv->length_lo;
939-
uint16_t pp2_tlv_offset = 3 + pp2_tlv_len;
942+
uint16_t pp2_tlv_offset = sizeof_pp2_tlv_t + pp2_tlv_len;
940943
if (pp2_tlv_offset > tlv_vectors_len)
941944
{
942945
return -ERR_PP2_TLV_LENGTH;
@@ -1030,7 +1033,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
10301033
return -ERR_PP2_TYPE_SSL;
10311034
}
10321035

1033-
pp2_sub_tlv_offset += 3 + pp2_sub_tlv_ssl_len;
1036+
pp2_sub_tlv_offset += sizeof_pp2_tlv_t + pp2_sub_tlv_ssl_len;
10341037
}
10351038
if (pp2_sub_tlv_offset > pp2_tlvs_ssl_len || (pp_info->pp2_info.pp2_ssl_info.ssl && !tlv_ssl_version_found))
10361039
{

0 commit comments

Comments
 (0)