Skip to content

Commit 4ad9da6

Browse files
Make error enum maintainable
1 parent 6500c9f commit 4ad9da6

3 files changed

Lines changed: 70 additions & 70 deletions

File tree

src/proxy_protocol.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static const char *errors[] = {
190190

191191
const char *pp_strerror(int32_t error)
192192
{
193-
if (error < ERR_HEAP_ALLOC || error > ERR_NULL)
193+
if (error < -ERR_HEAP_ALLOC || error > ERR_NULL)
194194
{
195195
return NULL;
196196
}
@@ -752,7 +752,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
752752
uint8_t version = proxy_hdr_v2->ver_cmd >> 4;
753753
if (version != 0x2)
754754
{
755-
return ERR_PP2_VERSION;
755+
return -ERR_PP2_VERSION;
756756
}
757757
/* The lowest four bits represents the command
758758
* \x0 : LOCAL
@@ -769,7 +769,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
769769
}
770770
else
771771
{
772-
return ERR_PP2_CMD;
772+
return -ERR_PP2_CMD;
773773
}
774774

775775
/* The 14th byte contains the transport protocol and address family */
@@ -794,21 +794,21 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
794794
}
795795
else
796796
{
797-
return ERR_PP2_ADDR_FAMILY;
797+
return -ERR_PP2_ADDR_FAMILY;
798798
}
799799
/* the lowest 4 bits contain the protocol */
800800
pp_info->transport_protocol = proxy_hdr_v2->fam & 0x0f;
801801
if (pp_info->transport_protocol > TRANSPORT_PROTOCOL_DGRAM)
802802
{
803-
return ERR_PP2_TRANSPORT_PROTOCOL;
803+
return -ERR_PP2_TRANSPORT_PROTOCOL;
804804
}
805805

806806

807807
/* The 15th and 16th bytes is the address length in bytes in network byte order */
808808
uint16_t len = ntohs(proxy_hdr_v2->len);
809809
if (buffer_length < sizeof(proxy_hdr_v2_t) + len)
810810
{
811-
return ERR_PP2_LENGTH;
811+
return -ERR_PP2_LENGTH;
812812
}
813813

814814
/*
@@ -830,11 +830,11 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
830830
{
831831
if (!inet_ntop(fam, &addr->ipv4_addr.src_addr, pp_info->src_addr, sizeof(pp_info->src_addr)))
832832
{
833-
return ERR_PP2_IPV4_SRC_IP;
833+
return -ERR_PP2_IPV4_SRC_IP;
834834
}
835835
if (!inet_ntop(fam, &addr->ipv4_addr.dst_addr, pp_info->dst_addr, sizeof(pp_info->dst_addr)))
836836
{
837-
return ERR_PP2_IPV4_DST_IP;
837+
return -ERR_PP2_IPV4_DST_IP;
838838
}
839839

840840
pp_info->src_port = ntohs(addr->ipv4_addr.src_port);
@@ -847,11 +847,11 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
847847
{
848848
if (!inet_ntop(fam, &addr->ipv6_addr.src_addr, pp_info->src_addr, sizeof(pp_info->src_addr)))
849849
{
850-
return ERR_PP2_IPV6_SRC_IP;
850+
return -ERR_PP2_IPV6_SRC_IP;
851851
}
852852
if (!inet_ntop(fam, &addr->ipv6_addr.dst_addr, pp_info->dst_addr, sizeof(pp_info->dst_addr)))
853853
{
854-
return ERR_PP2_IPV6_DST_IP;
854+
return -ERR_PP2_IPV6_DST_IP;
855855
}
856856

857857
pp_info->src_port = ntohs(addr->ipv6_addr.src_port);
@@ -867,7 +867,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
867867
}
868868
else
869869
{
870-
return ERR_PP2_LENGTH;
870+
return -ERR_PP2_LENGTH;
871871
}
872872

873873
/* TLVs */
@@ -879,7 +879,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
879879
uint16_t pp2_tlv_offset = 3 + pp2_tlv_len;
880880
if (pp2_tlv_offset > tlv_vectors_len)
881881
{
882-
return ERR_PP2_TLV_LENGTH;
882+
return -ERR_PP2_TLV_LENGTH;
883883
}
884884

885885
switch (pp2_tlv->type)
@@ -888,14 +888,14 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
888888
case PP2_TYPE_AUTHORITY: /* UTF8 */
889889
if (!tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, pp2_tlv->type, pp2_tlv_len, pp2_tlv->value))
890890
{
891-
return ERR_HEAP_ALLOC;
891+
return -ERR_HEAP_ALLOC;
892892
}
893893
break;
894894
case PP2_TYPE_CRC32C: /* 32-bit number */
895895
{
896896
if (pp2_tlv_len != sizeof(uint32_t))
897897
{
898-
return ERR_PP2_TYPE_CRC32C;
898+
return -ERR_PP2_TYPE_CRC32C;
899899
}
900900

901901
/* Received CRC32c checksum */
@@ -909,12 +909,12 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
909909
/* Verify that the calculated CRC32c checksum is the same as the received CRC32c checksum*/
910910
if (memcmp(&crc32c_chksum, &crc32c_calculated, 4))
911911
{
912-
return ERR_PP2_TYPE_CRC32C;
912+
return -ERR_PP2_TYPE_CRC32C;
913913
}
914914

915915
if (!tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, pp2_tlv->type, pp2_tlv_len, &crc32c_chksum))
916916
{
917-
return ERR_HEAP_ALLOC;
917+
return -ERR_HEAP_ALLOC;
918918
}
919919
break;
920920
}
@@ -923,11 +923,11 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
923923
case PP2_TYPE_UNIQUE_ID: /* Byte sequence */
924924
if (pp2_tlv_len > 128)
925925
{
926-
return ERR_PP2_TYPE_UNIQUE_ID;
926+
return -ERR_PP2_TYPE_UNIQUE_ID;
927927
}
928928
if (!tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, pp2_tlv->type, pp2_tlv_len, pp2_tlv->value))
929929
{
930-
return ERR_HEAP_ALLOC;
930+
return -ERR_HEAP_ALLOC;
931931
}
932932
break;
933933
case PP2_TYPE_SSL:
@@ -956,38 +956,38 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
956956
case PP2_SUBTYPE_SSL_KEY_ALG: /* US-ASCII */
957957
if (!tlv_array_append_tlv_new_usascii(&pp_info->pp2_info.tlv_array, pp2_sub_tlv_ssl->type, pp2_sub_tlv_ssl_len, pp2_sub_tlv_ssl->value))
958958
{
959-
return ERR_HEAP_ALLOC;
959+
return -ERR_HEAP_ALLOC;
960960
}
961961
break;
962962
case PP2_SUBTYPE_SSL_CN: /* UTF8 */
963963
if (!tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, pp2_sub_tlv_ssl->type, pp2_sub_tlv_ssl_len, pp2_sub_tlv_ssl->value))
964964
{
965-
return ERR_HEAP_ALLOC;
965+
return -ERR_HEAP_ALLOC;
966966
}
967967
break;
968968
default:
969-
return ERR_PP2_TYPE_SSL;
969+
return -ERR_PP2_TYPE_SSL;
970970
}
971971

972972
pp2_sub_tlv_offset += 3 + pp2_sub_tlv_ssl_len;
973973
}
974974
if (pp2_sub_tlv_offset > pp2_tlvs_ssl_len || (pp_info->pp2_info.pp2_ssl_info.ssl && !tlv_ssl_version_found))
975975
{
976-
return ERR_PP2_TYPE_SSL;
976+
return -ERR_PP2_TYPE_SSL;
977977
}
978978
break;
979979
}
980980
case PP2_TYPE_NETNS: /* US-ASCII */
981981
if (!tlv_array_append_tlv_new_usascii(&pp_info->pp2_info.tlv_array, pp2_tlv->type, pp2_tlv_len, pp2_tlv->value))
982982
{
983-
return ERR_HEAP_ALLOC;
983+
return -ERR_HEAP_ALLOC;
984984
}
985985
break;
986986
case PP2_TYPE_AWS:
987987
{
988988
if (pp2_tlv_len < sizeof(pp2_tlv_aws_t))
989989
{
990-
return ERR_PP2_TYPE_AWS;
990+
return -ERR_PP2_TYPE_AWS;
991991
}
992992
pp2_tlv_aws_t *pp2_tlv_aws = (pp2_tlv_aws_t *) pp2_tlv->value;
993993
/* Connection is done through Private Link/Interface VPC endpoint */
@@ -996,7 +996,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
996996
/* Example: \x1vpce-08d2bf15fac5001c9 */
997997
if (!tlv_array_append_tlv_new_usascii(&pp_info->pp2_info.tlv_array, pp2_tlv->type, pp2_tlv_len, pp2_tlv->value))
998998
{
999-
return ERR_HEAP_ALLOC;
999+
return -ERR_HEAP_ALLOC;
10001000
}
10011001
}
10021002
break;
@@ -1005,7 +1005,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
10051005
{
10061006
if (pp2_tlv_len < sizeof(pp2_tlv_azure_t))
10071007
{
1008-
return ERR_PP2_TYPE_AZURE;
1008+
return -ERR_PP2_TYPE_AZURE;
10091009
}
10101010
pp2_tlv_azure_t *pp2_tlv_azure = (pp2_tlv_azure_t *) pp2_tlv->value;
10111011
/* Connection is done through Private Link service */
@@ -1014,7 +1014,7 @@ static int32_t pp2_parse_hdr(uint8_t *buffer, uint32_t buffer_length, pp_info_t
10141014
pp2_tlv_t *tlv = tlv_new(pp2_tlv->type, pp2_tlv_len, pp2_tlv->value);
10151015
if (!tlv || !tlv_array_append_tlv(&pp_info->pp2_info.tlv_array, tlv))
10161016
{
1017-
return ERR_HEAP_ALLOC;
1017+
return -ERR_HEAP_ALLOC;
10181018
}
10191019
}
10201020
break;
@@ -1039,22 +1039,22 @@ static int32_t pp1_parse_hdr(const uint8_t *buffer, uint32_t buffer_length, pp_i
10391039
char *block_end = strstr(block, CRLF);
10401040
if (!block_end)
10411041
{
1042-
return ERR_PP1_CRLF;
1042+
return -ERR_PP1_CRLF;
10431043
}
10441044
block_end += strlen(CRLF);
10451045
pp1_hdr_len = block_end - block;
10461046

10471047
/* PROXY */
10481048
if (memcmp(block, "PROXY", 5))
10491049
{
1050-
return ERR_PP1_PROXY;
1050+
return -ERR_PP1_PROXY;
10511051
}
10521052
ptr += 5;
10531053

10541054
/* Exactly one space */
10551055
if (*ptr != '\x20')
10561056
{
1057-
return ERR_PP1_SPACE;
1057+
return -ERR_PP1_SPACE;
10581058
}
10591059
ptr++;
10601060

@@ -1069,7 +1069,7 @@ static int32_t pp1_parse_hdr(const uint8_t *buffer, uint32_t buffer_length, pp_i
10691069
pp_info->transport_protocol = TRANSPORT_PROTOCOL_UNSPEC;
10701070
return pp1_hdr_len;
10711071
}
1072-
return ERR_PP1_TRANSPORT_FAMILY;
1072+
return -ERR_PP1_TRANSPORT_FAMILY;
10731073
}
10741074
uint8_t sa_family = AF_UNSPEC;
10751075
if (!memcmp(ptr, "TCP4", 4))
@@ -1095,13 +1095,13 @@ static int32_t pp1_parse_hdr(const uint8_t *buffer, uint32_t buffer_length, pp_i
10951095
}
10961096
else
10971097
{
1098-
return ERR_PP1_TRANSPORT_FAMILY;;
1098+
return -ERR_PP1_TRANSPORT_FAMILY;;
10991099
}
11001100

11011101
/* Exactly one space */
11021102
if (*ptr != '\x20')
11031103
{
1104-
return ERR_PP1_SPACE;
1104+
return -ERR_PP1_SPACE;
11051105
}
11061106
ptr++;
11071107

@@ -1123,7 +1123,7 @@ static int32_t pp1_parse_hdr(const uint8_t *buffer, uint32_t buffer_length, pp_i
11231123
/* Exactly one space */
11241124
if (*ptr != '\x20')
11251125
{
1126-
return ERR_PP1_SPACE;
1126+
return -ERR_PP1_SPACE;
11271127
}
11281128
ptr++;
11291129

@@ -1145,51 +1145,51 @@ static int32_t pp1_parse_hdr(const uint8_t *buffer, uint32_t buffer_length, pp_i
11451145
/* Exactly one space */
11461146
if (*ptr != '\x20')
11471147
{
1148-
return ERR_PP1_SPACE;
1148+
return -ERR_PP1_SPACE;
11491149
}
11501150
ptr++;
11511151

11521152
/* TCP source port represented as a decimal integer in the range [0..65535] inclusive */
11531153
char *src_port_end = strchr(ptr, ' ');
11541154
if (!src_port_end)
11551155
{
1156-
return ERR_PP1_SRC_PORT;
1156+
return -ERR_PP1_SRC_PORT;
11571157
}
11581158
char src_port_str[6] = { 0 };
11591159
uint16_t src_port_length = src_port_end - ptr;
11601160
memcpy(src_port_str, ptr, src_port_length);
11611161
if (!parse_port(src_port_str, &pp_info->src_port))
11621162
{
1163-
return ERR_PP1_SRC_PORT;
1163+
return -ERR_PP1_SRC_PORT;
11641164
}
11651165
ptr += src_port_length;
11661166

11671167
/* Exactly one space */
11681168
if (*ptr != '\x20')
11691169
{
1170-
return ERR_PP1_SPACE;
1170+
return -ERR_PP1_SPACE;
11711171
}
11721172
ptr++;
11731173

11741174
/* TCP destination port represented as a decimal integer in the range [0..65535] inclusive */
11751175
char *dst_port_end = strchr(ptr, '\r');
11761176
if (!dst_port_end)
11771177
{
1178-
return ERR_PP1_DST_PORT;
1178+
return -ERR_PP1_DST_PORT;
11791179
}
11801180
char dst_port_str[6] = { 0 };
11811181
uint16_t dst_port_length = dst_port_end - ptr;
11821182
memcpy(dst_port_str, ptr, dst_port_length);
11831183
if (!parse_port(dst_port_str, &pp_info->dst_port))
11841184
{
1185-
return ERR_PP1_DST_PORT;
1185+
return -ERR_PP1_DST_PORT;
11861186
}
11871187
ptr += dst_port_length;
11881188

11891189
/* The CRLF sequence */
11901190
if (*ptr != '\r' || *(ptr+1) != '\n')
11911191
{
1192-
return ERR_PP1_CRLF;
1192+
return -ERR_PP1_CRLF;
11931193
}
11941194

11951195
return pp1_hdr_len;

src/proxy_protocol.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,34 @@
2424
enum
2525
{
2626
ERR_NULL,
27-
ERR_PP_VERSION = -1,
28-
ERR_PP2_SIG = -2,
29-
ERR_PP2_VERSION = -3,
30-
ERR_PP2_CMD = -4,
31-
ERR_PP2_ADDR_FAMILY = -5,
32-
ERR_PP2_TRANSPORT_PROTOCOL = -6,
33-
ERR_PP2_LENGTH = -7,
34-
ERR_PP2_IPV4_SRC_IP = -8,
35-
ERR_PP2_IPV4_DST_IP = -9,
36-
ERR_PP2_IPV6_SRC_IP = -10,
37-
ERR_PP2_IPV6_DST_IP = -11,
38-
ERR_PP2_TLV_LENGTH = -12,
39-
ERR_PP2_TYPE_CRC32C = -13,
40-
ERR_PP2_TYPE_SSL = -14,
41-
ERR_PP2_TYPE_UNIQUE_ID = -15,
42-
ERR_PP2_TYPE_AWS = -16,
43-
ERR_PP2_TYPE_AZURE = -17,
44-
ERR_PP1_CRLF = -18,
45-
ERR_PP1_PROXY = -19,
46-
ERR_PP1_SPACE = -20,
47-
ERR_PP1_TRANSPORT_FAMILY = -21,
48-
ERR_PP1_IPV4_SRC_IP = -22,
49-
ERR_PP1_IPV4_DST_IP = -23,
50-
ERR_PP1_IPV6_SRC_IP = -24,
51-
ERR_PP1_IPV6_DST_IP = -25,
52-
ERR_PP1_SRC_PORT = -26,
53-
ERR_PP1_DST_PORT = -27,
54-
ERR_HEAP_ALLOC = -28,
27+
ERR_PP_VERSION,
28+
ERR_PP2_SIG,
29+
ERR_PP2_VERSION,
30+
ERR_PP2_CMD,
31+
ERR_PP2_ADDR_FAMILY,
32+
ERR_PP2_TRANSPORT_PROTOCOL,
33+
ERR_PP2_LENGTH,
34+
ERR_PP2_IPV4_SRC_IP,
35+
ERR_PP2_IPV4_DST_IP,
36+
ERR_PP2_IPV6_SRC_IP,
37+
ERR_PP2_IPV6_DST_IP,
38+
ERR_PP2_TLV_LENGTH,
39+
ERR_PP2_TYPE_CRC32C,
40+
ERR_PP2_TYPE_SSL,
41+
ERR_PP2_TYPE_UNIQUE_ID,
42+
ERR_PP2_TYPE_AWS,
43+
ERR_PP2_TYPE_AZURE,
44+
ERR_PP1_CRLF,
45+
ERR_PP1_PROXY,
46+
ERR_PP1_SPACE,
47+
ERR_PP1_TRANSPORT_FAMILY,
48+
ERR_PP1_IPV4_SRC_IP,
49+
ERR_PP1_IPV4_DST_IP,
50+
ERR_PP1_IPV6_SRC_IP,
51+
ERR_PP1_IPV6_DST_IP,
52+
ERR_PP1_SRC_PORT,
53+
ERR_PP1_DST_PORT,
54+
ERR_HEAP_ALLOC
5555
};
5656

5757
/* Returns a descriptive error message

0 commit comments

Comments
 (0)