Skip to content

Commit

Permalink
Merge pull request #2 from westermo/handle-ipv4-mapped-on-ipv6
Browse files Browse the repository at this point in the history
Handle IPv4 addresses mapped on IPv6
  • Loading branch information
troglobit authored Sep 6, 2021
2 parents 90ac304 + 97c4d03 commit da3465d
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 da3465d

Please sign in to comment.