From ff00a2cd0de966ee26a8367f3181c2691209a7e5 Mon Sep 17 00:00:00 2001 From: "[anp/hsw]" Date: Thu, 15 Aug 2024 23:23:18 +0700 Subject: [PATCH] fixing -Wstrict-aliasing (part 1) --- tpws/helpers.c | 11 ++++++----- tpws/helpers.h | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tpws/helpers.c b/tpws/helpers.c index f7798c7..08ec98f 100644 --- a/tpws/helpers.c +++ b/tpws/helpers.c @@ -10,6 +10,7 @@ #include #include #include +#include char *strncasestr(const char *s,const char *find, size_t slen) { @@ -132,7 +133,7 @@ void print_addrinfo(const struct addrinfo *ai) bool saismapped(const struct sockaddr_in6 *sa) { // ::ffff:1.2.3.4 - return !memcmp(sa->sin6_addr.s6_addr,"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff",12); + return IN6_IS_ADDR_V4MAPPED(sa->sin6_addr.s6_addr); } bool samappedcmp(const struct sockaddr_in *sa1,const struct sockaddr_in6 *sa2) { @@ -166,14 +167,14 @@ bool saconvmapped(struct sockaddr_storage *a) bool is_localnet(const struct sockaddr *a) { - // 0.0.0.0, ::ffff:0.0.0.0 = localhost in linux - return (a->sa_family==AF_INET && (*(char*)&((struct sockaddr_in *)a)->sin_addr.s_addr==127 || !((struct sockaddr_in *)a)->sin_addr.s_addr)) || - (a->sa_family==AF_INET6 && saismapped((struct sockaddr_in6 *)a) && (((struct sockaddr_in6 *)a)->sin6_addr.s6_addr[12]==127 || !*(uint32_t*)(((struct sockaddr_in6 *)a)->sin6_addr.s6_addr+12))); + // 127.0.0.0/8, ::1 = localhost + return (a->sa_family==AF_INET && IN_LOOPBACK(((struct sockaddr_in *)a)->sin_addr.s_addr)) || + (a->sa_family==AF_INET6 && IN6_IS_ADDR_LOOPBACK(((struct sockaddr_in6 *)a)->sin6_addr.s6_addr)); } bool is_linklocal(const struct sockaddr_in6 *a) { // fe80::/10 - return a->sin6_addr.s6_addr[0]==0xFE && (a->sin6_addr.s6_addr[1] & 0xC0)==0x80; + return IN6_IS_ADDR_LINKLOCAL(a->sin6_addr.s6_addr); } bool is_private6(const struct sockaddr_in6* a) { diff --git a/tpws/helpers.h b/tpws/helpers.h index 14cc220..1989a14 100644 --- a/tpws/helpers.h +++ b/tpws/helpers.h @@ -55,3 +55,7 @@ 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