From 9af54b09dd0a81506c67edfd3f8dd7a8bc15641e Mon Sep 17 00:00:00 2001 From: "[anp/hsw]" Date: Fri, 16 Aug 2024 00:07:38 +0700 Subject: [PATCH] fixing -Wstrict-aliasing (part 1) --- tpws/helpers.c | 2 +- tpws/helpers.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tpws/helpers.c b/tpws/helpers.c index 670aa61..a5363db 100644 --- a/tpws/helpers.c +++ b/tpws/helpers.c @@ -154,7 +154,7 @@ bool saconvmapped(struct sockaddr_storage *a) { if ((a->ss_family == AF_INET6) && saismapped((struct sockaddr_in6*)a)) { - uint32_t ip4 = *(uint32_t*)(((struct sockaddr_in6*)a)->sin6_addr.s6_addr+12); + uint32_t ip4 = IN6_EXTRACT_MAP4(((struct sockaddr_in6*)a)->sin6_addr.s6_addr); uint16_t port = ((struct sockaddr_in6*)a)->sin6_port; a->ss_family = AF_INET; ((struct sockaddr_in*)a)->sin_addr.s_addr = ip4; diff --git a/tpws/helpers.h b/tpws/helpers.h index 14cc220..f1383cb 100644 --- a/tpws/helpers.h +++ b/tpws/helpers.h @@ -55,3 +55,16 @@ typedef struct } port_filter; bool pf_in_range(uint16_t port, const port_filter *pf); bool pf_parse(const char *s, port_filter *pf); + +#ifndef IN_LOOPBACK +#define IN_LOOPBACK(a) ((((uint32_t) (a)) & 0xff000000) == 0x7f000000) +#endif + +#ifdef __GNUC__ +#define IN6_EXTRACT_MAP4(a) \ + (__extension__ \ + ({ const struct in6_addr *__a = (const struct in6_addr *) (a); \ + (((const uint32_t *) (__a))[3]); })) +#else +#define IN6_EXTRACT_MAP4(a) (((const uint32_t *) (a))[3]) +#endif