diff --git a/toxcore/network.c b/toxcore/network.c index 83081a49..44448f38 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -480,11 +480,24 @@ int ip_equal(IP *a, IP *b) return 0; #ifdef TOX_ENABLE_IPV6 - if (a->family == AF_INET) - return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); + /* same family */ + if (a->family == b->family) { + if (a->family == AF_INET) + return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); + else if (a->family == AF_INET6) + return IN6_ARE_ADDR_EQUAL(&a->ip6, &b->ip6); + else + return 0; + } - if (a->family == AF_INET6) - return IN6_ARE_ADDR_EQUAL(&a->ip6, &b->ip6); + /* different family: check on the IPv6 one if it is the IPv4 one embedded */ + if ((a->family == AF_INET) && (b->family == AF_INET6)) { + if (IN6_IS_ADDR_V4COMPAT(&b->ip6)) + return (a->ip4.in_addr.s_addr == b->ip6.s6_addr32[3]); + } else if ((a->family == AF_INET6) && (b->family == AF_INET)) { + if (IN6_IS_ADDR_V4COMPAT(&a->ip6)) + return (a->ip6.s6_addr32[3] == b->ip4.in_addr.s_addr); + } return 0; #else