Skip to content

Commit

Permalink
Handle IPv4 addresses mapped on IPv6
Browse files Browse the repository at this point in the history
IPv4 addresses may be stored as the four least significant bytes of an IPv6
address.

Signed-off-by: Jacques de Laval <[email protected]>
  • Loading branch information
jackuess committed Sep 6, 2021
1 parent 90ac304 commit 97c4d03
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions accept-guard.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ static int identify_inbound(int sd, int ifindex, char *ifname, size_t len, int *
if (!ifa->ifa_addr)
continue;

#ifdef AF_INET6
/* Detect and handle IPv4-mapped-on-IPv6 */
if (ifa->ifa_addr->sa_family == AF_INET && ss.ss_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
size_t ina_len = sizeof(struct in_addr);
size_t ina_offset = sizeof(struct in6_addr) - sizeof(struct in_addr);
struct sockaddr_in *iin;

if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
continue;

iin = (struct sockaddr_in *)ifa->ifa_addr;
if (!memcmp((unsigned char *)&sin6->sin6_addr + ina_offset,
&iin->sin_addr, ina_len)) {
strlencpy(ifname, ifa->ifa_name, len);
*port = ntohs(sin6->sin6_port);
break;
}
}
#endif

if (ifa->ifa_addr->sa_family != ss.ss_family)
continue;

Expand Down

0 comments on commit 97c4d03

Please sign in to comment.