diff options
Diffstat (limited to 'daemon/proxyv2.c')
-rw-r--r-- | daemon/proxyv2.c | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/daemon/proxyv2.c b/daemon/proxyv2.c index f977ccb..aedbb91 100644 --- a/daemon/proxyv2.c +++ b/daemon/proxyv2.c @@ -2,6 +2,9 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ +#include "daemon/session.h" +#include "daemon/network.h" + #include "daemon/proxyv2.h" #include "lib/generic/trie.h" @@ -91,12 +94,12 @@ static inline enum proxy2_family proxy2_header_protocol(const struct proxy2_head static inline union proxy2_address *proxy2_get_address(const struct proxy2_header *h) { - return (union proxy2_address *) ((uint8_t *) h + sizeof(struct proxy2_header)); + return (union proxy2_address *)((uint8_t *)h + sizeof(struct proxy2_header)); } static inline struct proxy2_tlv *get_tlvs(const struct proxy2_header *h, size_t addr_len) { - return (struct proxy2_tlv *) ((uint8_t *) proxy2_get_address(h) + addr_len); + return (struct proxy2_tlv *)((uint8_t *)proxy2_get_address(h) + addr_len); } /** Gets the length of the TLV's `value` attribute. */ @@ -111,20 +114,20 @@ static inline bool has_tlv(const struct proxy2_header *h, uint64_t addr_length = ntohs(h->length); ptrdiff_t hdr_len = sizeof(struct proxy2_header) + addr_length; - uint8_t *tlv_hdr_end = (uint8_t *) tlv + sizeof(struct proxy2_tlv); - ptrdiff_t distance = tlv_hdr_end - (uint8_t *) h; + uint8_t *tlv_hdr_end = (uint8_t *)tlv + sizeof(struct proxy2_tlv); + ptrdiff_t distance = tlv_hdr_end - (uint8_t *)h; if (hdr_len < distance) return false; uint8_t *tlv_end = tlv_hdr_end + proxy2_tlv_length(tlv); - distance = tlv_end - (uint8_t *) h; + distance = tlv_end - (uint8_t *)h; return hdr_len >= distance; } static inline void next_tlv(struct proxy2_tlv **tlv) { - uint8_t *next = ((uint8_t *) *tlv + sizeof(struct proxy2_tlv) + proxy2_tlv_length(*tlv)); - *tlv = (struct proxy2_tlv *) next; + uint8_t *next = ((uint8_t *)*tlv + sizeof(struct proxy2_tlv) + proxy2_tlv_length(*tlv)); + *tlv = (struct proxy2_tlv *)next; } @@ -140,7 +143,7 @@ bool proxy_allowed(const struct network *net, const struct sockaddr *saddr) trie = net->proxy_addrs4; addr_size = sizeof(addr.ip4); - addr.ip4 = ((struct sockaddr_in *) saddr)->sin_addr; + addr.ip4 = ((struct sockaddr_in *)saddr)->sin_addr; break; case AF_INET6: if (net->proxy_all6) @@ -148,7 +151,7 @@ bool proxy_allowed(const struct network *net, const struct sockaddr *saddr) trie = net->proxy_addrs6; addr_size = sizeof(addr.ip6); - addr.ip6 = ((struct sockaddr_in6 *) saddr)->sin6_addr; + addr.ip6 = ((struct sockaddr_in6 *)saddr)->sin6_addr; break; default: kr_assert(false); // Only IPv4 and IPv6 proxy addresses supported @@ -156,14 +159,14 @@ bool proxy_allowed(const struct network *net, const struct sockaddr *saddr) } trie_val_t *val; - int ret = trie_get_leq(trie, (char *) &addr, addr_size, &val); + int ret = trie_get_leq(trie, (char *)&addr, addr_size, &val); if (ret != kr_ok() && ret != 1) return false; kr_assert(val); const struct net_proxy_data *found = *val; kr_assert(found); - return kr_bitcmp((char *) &addr, (char *) &found->addr, found->netmask) == 0; + return kr_bitcmp((char *)&addr, (char *)&found->addr, found->netmask) == 0; } ssize_t proxy_process_header(struct proxy_result *out, struct session *s, @@ -172,7 +175,7 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, if (!buf) return kr_error(EINVAL); - const struct proxy2_header *hdr = (struct proxy2_header *) buf; + const struct proxy2_header *hdr = (struct proxy2_header *)buf; uint64_t content_length = ntohs(hdr->length); ssize_t hdr_len = sizeof(struct proxy2_header) + content_length; @@ -191,7 +194,7 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, enum proxy2_command command = proxy2_header_command(hdr); if (command == PROXY2_CMD_LOCAL) { /* Addresses for LOCAL are to be discarded */ - *out = (struct proxy_result) { .command = PROXY2_CMD_LOCAL }; + *out = (struct proxy_result){ .command = PROXY2_CMD_LOCAL }; goto fill_wirebuf; } @@ -200,13 +203,14 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, return kr_error(KNOT_EMALF); } - *out = (struct proxy_result) { .command = PROXY2_CMD_PROXY }; + *out = (struct proxy_result){ .command = PROXY2_CMD_PROXY }; /* Parse flags */ enum proxy2_family family = proxy2_header_family(hdr); switch(family) { case PROXY2_AF_UNSPEC: - case PROXY2_AF_UNIX: /* UNIX is unsupported, fall back to UNSPEC */ + case PROXY2_AF_UNIX: + /* UNIX is unsupported, fall back to UNSPEC */ out->family = AF_UNSPEC; break; case PROXY2_AF_INET: @@ -215,7 +219,8 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, case PROXY2_AF_INET6: out->family = AF_INET6; break; - default: /* PROXYv2 prohibits other values */ + default: + /* PROXYv2 prohibits other values */ return kr_error(KNOT_EMALF); } @@ -227,7 +232,8 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, case PROXY2_PROTOCOL_STREAM: out->protocol = SOCK_STREAM; break; - default: /* PROXYv2 prohibits other values */ + default: + /* PROXYv2 prohibits other values */ return kr_error(KNOT_EMALF); } @@ -240,12 +246,12 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, if (content_length < addr_length) return kr_error(KNOT_EMALF); - out->src_addr.ip4 = (struct sockaddr_in) { + out->src_addr.ip4 = (struct sockaddr_in){ .sin_family = AF_INET, .sin_addr = { .s_addr = addr->ipv4_addr.src_addr }, .sin_port = addr->ipv4_addr.src_port, }; - out->dst_addr.ip4 = (struct sockaddr_in) { + out->dst_addr.ip4 = (struct sockaddr_in){ .sin_family = AF_INET, .sin_addr = { .s_addr = addr->ipv4_addr.dst_addr }, .sin_port = addr->ipv4_addr.dst_port, @@ -256,7 +262,7 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, if (content_length < addr_length) return kr_error(KNOT_EMALF); - out->src_addr.ip6 = (struct sockaddr_in6) { + out->src_addr.ip6 = (struct sockaddr_in6){ .sin6_family = AF_INET6, .sin6_port = addr->ipv6_addr.src_port }; @@ -264,7 +270,7 @@ ssize_t proxy_process_header(struct proxy_result *out, struct session *s, &out->src_addr.ip6.sin6_addr.s6_addr, &addr->ipv6_addr.src_addr, sizeof(out->src_addr.ip6.sin6_addr.s6_addr)); - out->dst_addr.ip6 = (struct sockaddr_in6) { + out->dst_addr.ip6 = (struct sockaddr_in6){ .sin6_family = AF_INET6, .sin6_port = addr->ipv6_addr.dst_port }; |