diff options
author | Omar Polo <op@omarpolo.com> | 2024-06-17 20:47:24 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2024-06-17 20:47:24 +0000 |
commit | cc399bfabfdc793dbe848120df6a7ed5b8f013b4 (patch) | |
tree | bd25ca5ce6a6c7b4096695bf3cb03466df706aa7 /proxy-proto.c | |
parent | 94f143773948fe3bf811a9cd8c233198af58235b (diff) |
whitespace and minor style nits
Diffstat (limited to 'proxy-proto.c')
-rw-r--r-- | proxy-proto.c | 250 |
1 files changed, 128 insertions, 122 deletions
diff --git a/proxy-proto.c b/proxy-proto.c index 43b9cae..4ad13f3 100644 --- a/proxy-proto.c +++ b/proxy-proto.c @@ -21,107 +21,107 @@ #define MIN(a, b) (a) < (b) ? (a) : (b) -static int +static int check_prefix_v1(char **buf) { - static const char PROXY[6] = "PROXY "; + static const char PROXY[6] = "PROXY "; - if (0 != strncmp(*buf, PROXY, sizeof(PROXY))) - return PROXY_PROTO_PARSE_FAIL; + if (0 != strncmp(*buf, PROXY, sizeof(PROXY))) + return PROXY_PROTO_PARSE_FAIL; - *buf += sizeof(PROXY); + *buf += sizeof(PROXY); - return PROXY_PROTO_PARSE_SUCCESS; + return PROXY_PROTO_PARSE_SUCCESS; } static int check_proto_v1(char **buf) { - static const char TCP[3] = "TCP"; + static const char TCP[3] = "TCP"; - if (0 != strncmp(*buf, TCP, sizeof(TCP))) - return PROXY_PROTO_PARSE_FAIL; + if (0 != strncmp(*buf, TCP, sizeof(TCP))) + return PROXY_PROTO_PARSE_FAIL; - *buf += sizeof(TCP); + *buf += sizeof(TCP); - int type; - switch ((*buf)[0]) { - case '4': type = 4; break; - case '6': type = 6; break; - default: return PROXY_PROTO_PARSE_FAIL; - } + int type; + switch ((*buf)[0]) { + case '4': type = 4; break; + case '6': type = 6; break; + default: return PROXY_PROTO_PARSE_FAIL; + } - // '4' / '6' + ' ' - *buf += 2; + // '4' / '6' + ' ' + *buf += 2; - return type; + return type; } static int check_unknown_v1(char **buf) { - static const char UNKNOWN[7] = "UNKNOWN"; + static const char UNKNOWN[7] = "UNKNOWN"; - if (0 != strncmp(*buf, UNKNOWN, sizeof(UNKNOWN))) - return PROXY_PROTO_PARSE_FAIL; + if (0 != strncmp(*buf, UNKNOWN, sizeof(UNKNOWN))) + return PROXY_PROTO_PARSE_FAIL; - *buf += sizeof(UNKNOWN); + *buf += sizeof(UNKNOWN); - return PROXY_PROTO_PARSE_SUCCESS; + return PROXY_PROTO_PARSE_SUCCESS; } static int check_crlf_v1(char *const *buf, size_t buflen) { - static const char CRLF[2] = "\r\n"; + static const char CRLF[2] = "\r\n"; - if (buflen < sizeof(CRLF)) - return PROXY_PROTO_PARSE_FAIL; + if (buflen < sizeof(CRLF)) + return PROXY_PROTO_PARSE_FAIL; - if (!memmem(*buf, buflen, CRLF, sizeof(CRLF))) - return PROXY_PROTO_PARSE_FAIL; + if (!memmem(*buf, buflen, CRLF, sizeof(CRLF))) + return PROXY_PROTO_PARSE_FAIL; - return PROXY_PROTO_PARSE_SUCCESS; + return PROXY_PROTO_PARSE_SUCCESS; } static int check_ip_v1(int af, void *addr, char **buf) { - char *spc; + char *spc; - if (NULL == (spc = strchr(*buf, ' '))) - return PROXY_PROTO_PARSE_FAIL; + if (NULL == (spc = strchr(*buf, ' '))) + return PROXY_PROTO_PARSE_FAIL; - *spc++ = '\0'; + *spc++ = '\0'; - if (1 != inet_pton(af, *buf, addr)) - return PROXY_PROTO_PARSE_FAIL; + if (1 != inet_pton(af, *buf, addr)) + return PROXY_PROTO_PARSE_FAIL; - *buf = spc; + *buf = spc; - return PROXY_PROTO_PARSE_SUCCESS; + return PROXY_PROTO_PARSE_SUCCESS; } static int check_port_v1(uint16_t *port, char **buf, size_t *buflen) { - size_t wspc_idx = strcspn(*buf, " \r"); - char *wspc = *buf + wspc_idx; + size_t wspc_idx = strcspn(*buf, " \r"); + char *wspc = *buf + wspc_idx; - if (!(' ' == *wspc || '\r' == *wspc)) - return PROXY_PROTO_PARSE_FAIL; + if (!(' ' == *wspc || '\r' == *wspc)) + return PROXY_PROTO_PARSE_FAIL; - *wspc++ = '\0'; + *wspc++ = '\0'; - const char *errstr; - long long num = strtonum(*buf, 0, UINT16_MAX, &errstr); - if (errstr) - return PROXY_PROTO_PARSE_FAIL; + const char *errstr; + long long num = strtonum(*buf, 0, UINT16_MAX, &errstr); + if (errstr) + return PROXY_PROTO_PARSE_FAIL; - *buf = wspc; - *port = num; + *buf = wspc; + *port = num; - return PROXY_PROTO_PARSE_SUCCESS; + return PROXY_PROTO_PARSE_SUCCESS; } #define EXPECT_SUCCESS(call) do { \ @@ -130,82 +130,88 @@ check_port_v1(uint16_t *port, char **buf, size_t *buflen) return ret; \ } while (0) -int proxy_proto_v1_parse(struct proxy_protocol_v1 *s, char *buf, size_t buflen, size_t *consumed) +int +proxy_proto_v1_parse(struct proxy_protocol_v1 *s, char *buf, size_t buflen, + size_t *consumed) { - const char *begin = buf; - int ret; - - EXPECT_SUCCESS(check_crlf_v1(&buf, buflen)); - EXPECT_SUCCESS(check_prefix_v1(&buf)); - - ret = check_proto_v1(&buf); - switch (ret) - { - case 4: s->proto = PROTO_V4; break; - case 6: s->proto = PROTO_V6; break; - case PROXY_PROTO_PARSE_FAIL: { - EXPECT_SUCCESS(check_unknown_v1(&buf)); - - s->proto = PROTO_UNKNOWN; - return PROXY_PROTO_PARSE_SUCCESS; - } break; - default: return ret; - } - - switch (s->proto) - { - case PROTO_V4: { - EXPECT_SUCCESS(check_ip_v1(AF_INET, &s->srcaddr.v4, &buf)); - EXPECT_SUCCESS(check_ip_v1(AF_INET, &s->dstaddr.v4, &buf)); - } break; - - case PROTO_V6: { - EXPECT_SUCCESS(check_ip_v1(AF_INET6, &s->srcaddr.v6, &buf)); - EXPECT_SUCCESS(check_ip_v1(AF_INET6, &s->dstaddr.v6, &buf)); - } break; - - default: ASSERT_MSG(0, "unimplemented"); - } - - EXPECT_SUCCESS(check_port_v1(&s->srcport, &buf, &buflen)); - EXPECT_SUCCESS(check_port_v1(&s->dstport, &buf, &buflen)); - - assert('\n' == *buf); - buf += 1; - - *consumed = buf - begin; - return PROXY_PROTO_PARSE_SUCCESS; + const char *begin = buf; + int ret; + + EXPECT_SUCCESS(check_crlf_v1(&buf, buflen)); + EXPECT_SUCCESS(check_prefix_v1(&buf)); + + ret = check_proto_v1(&buf); + switch (ret) { + case 4: s->proto = PROTO_V4; break; + case 6: s->proto = PROTO_V6; break; + case PROXY_PROTO_PARSE_FAIL: + EXPECT_SUCCESS(check_unknown_v1(&buf)); + + s->proto = PROTO_UNKNOWN; + return PROXY_PROTO_PARSE_SUCCESS; + default: + return ret; + } + + switch (s->proto) { + case PROTO_V4: + EXPECT_SUCCESS(check_ip_v1(AF_INET, &s->srcaddr.v4, &buf)); + EXPECT_SUCCESS(check_ip_v1(AF_INET, &s->dstaddr.v4, &buf)); + break; + + case PROTO_V6: + EXPECT_SUCCESS(check_ip_v1(AF_INET6, &s->srcaddr.v6, &buf)); + EXPECT_SUCCESS(check_ip_v1(AF_INET6, &s->dstaddr.v6, &buf)); + break; + + default: + ASSERT_MSG(0, "unimplemented"); + } + + EXPECT_SUCCESS(check_port_v1(&s->srcport, &buf, &buflen)); + EXPECT_SUCCESS(check_port_v1(&s->dstport, &buf, &buflen)); + + assert('\n' == *buf); + buf += 1; + + *consumed = buf - begin; + return PROXY_PROTO_PARSE_SUCCESS; } -int -proxy_proto_v1_string(const struct proxy_protocol_v1 *s, char* buf, size_t buflen) +int +proxy_proto_v1_string(const struct proxy_protocol_v1 *s, char *buf, + size_t buflen) { - // "0000:0000:0000:0000:0000:0000:0000:0000\0" - char srcaddrbuf[40], dstaddrbuf[40]; - int ret; - switch (s->proto) - { - case PROTO_UNKNOWN: ret = snprintf(buf, buflen, "unknown"); goto fin; - case PROTO_V4: { - inet_ntop(AF_INET, &s->srcaddr.v4, srcaddrbuf, sizeof(srcaddrbuf)); - inet_ntop(AF_INET, &s->dstaddr.v4, dstaddrbuf, sizeof(dstaddrbuf)); - } break; - case PROTO_V6: { - inet_ntop(AF_INET6, &s->srcaddr.v6, srcaddrbuf, sizeof(srcaddrbuf)); - inet_ntop(AF_INET6, &s->dstaddr.v6, dstaddrbuf, sizeof(dstaddrbuf)); - } break; - } - - ret = snprintf( - buf, - buflen, - "from %s port %u via %s port %u", - srcaddrbuf, - s->srcport, - dstaddrbuf, - s->dstport - ); + // "0000:0000:0000:0000:0000:0000:0000:0000\0" + char srcaddrbuf[40], dstaddrbuf[40]; + int ret; + switch (s->proto) { + case PROTO_UNKNOWN: + ret = snprintf(buf, buflen, "unknown"); + goto fin; + case PROTO_V4: + inet_ntop(AF_INET, &s->srcaddr.v4, srcaddrbuf, + sizeof(srcaddrbuf)); + inet_ntop(AF_INET, &s->dstaddr.v4, dstaddrbuf, + sizeof(dstaddrbuf)); + break; + case PROTO_V6: + inet_ntop(AF_INET6, &s->srcaddr.v6, srcaddrbuf, + sizeof(srcaddrbuf)); + inet_ntop(AF_INET6, &s->dstaddr.v6, dstaddrbuf, + sizeof(dstaddrbuf)); + break; + } + + ret = snprintf( + buf, + buflen, + "from %s port %u via %s port %u", + srcaddrbuf, + s->srcport, + dstaddrbuf, + s->dstport); fin: - return ret; + return ret; } |