Compare commits

..

2 Commits

Author SHA1 Message Date
[anp/hsw]
1224206f40 fixing -Wstrict-aliasing (part 1) 2024-08-16 00:07:38 +07:00
[anp/hsw]
ff00a2cd0d fixing -Wstrict-aliasing (part 1) 2024-08-15 23:23:18 +07:00
2 changed files with 20 additions and 6 deletions

View File

@ -10,6 +10,7 @@
#include <ifaddrs.h> #include <ifaddrs.h>
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <netinet/in.h>
char *strncasestr(const char *s,const char *find, size_t slen) 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) bool saismapped(const struct sockaddr_in6 *sa)
{ {
// ::ffff:1.2.3.4 // ::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) bool samappedcmp(const struct sockaddr_in *sa1,const struct sockaddr_in6 *sa2)
{ {
@ -154,7 +155,7 @@ bool saconvmapped(struct sockaddr_storage *a)
{ {
if ((a->ss_family == AF_INET6) && saismapped((struct sockaddr_in6*)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; uint16_t port = ((struct sockaddr_in6*)a)->sin6_port;
a->ss_family = AF_INET; a->ss_family = AF_INET;
((struct sockaddr_in*)a)->sin_addr.s_addr = ip4; ((struct sockaddr_in*)a)->sin_addr.s_addr = ip4;
@ -166,14 +167,14 @@ bool saconvmapped(struct sockaddr_storage *a)
bool is_localnet(const struct sockaddr *a) bool is_localnet(const struct sockaddr *a)
{ {
// 0.0.0.0, ::ffff:0.0.0.0 = localhost in linux // 127.0.0.0/8, ::1 = localhost
return (a->sa_family==AF_INET && (*(char*)&((struct sockaddr_in *)a)->sin_addr.s_addr==127 || !((struct sockaddr_in *)a)->sin_addr.s_addr)) || return (a->sa_family==AF_INET && IN_LOOPBACK(((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))); (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) bool is_linklocal(const struct sockaddr_in6 *a)
{ {
// fe80::/10 // 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) bool is_private6(const struct sockaddr_in6* a)
{ {

View File

@ -55,3 +55,16 @@ typedef struct
} port_filter; } port_filter;
bool pf_in_range(uint16_t port, const port_filter *pf); bool pf_in_range(uint16_t port, const port_filter *pf);
bool pf_parse(const char *s, 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