This internal API checks, if passed address is ANYCAST address. --- src/libvirt_private.syms | 1 + src/util/virsocketaddr.c | 20 ++++++++++++++++++++ src/util/virsocketaddr.h | 1 + tests/sockettest.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b93629f..1ea7467 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1782,6 +1782,7 @@ virSocketAddrGetPort; virSocketAddrGetRange; virSocketAddrIsNetmask; virSocketAddrIsPrivate; +virSocketAddrIsWildcard; virSocketAddrMask; virSocketAddrMaskByPrefix; virSocketAddrParse; diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 1071b00..e84c58e 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -227,6 +227,26 @@ virSocketAddrIsPrivate(const virSocketAddrPtr addr) } /* + * virSocketAddrIsWildcard: + * @addr: address to check + * + * Check if passed address is a variant of ANYCAST address. + */ +bool +virSocketAddrIsWildcard(const virSocketAddrPtr addr) +{ + in_addr_t tmp = INADDR_ANY; + switch (addr->data.stor.ss_family) { + case AF_INET: + return memcmp(&addr->data.inet4.sin_addr.s_addr, &tmp, + sizeof(addr->data.inet4.sin_addr.s_addr)) == 0; + case AF_INET6: + return IN6_IS_ADDR_UNSPECIFIED(&addr->data.inet6.sin6_addr.s6_addr); + } + return false; +} + +/* * virSocketAddrFormat: * @addr: an initialized virSocketAddrPtr * diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h index 1c9e54a..b28fe6c 100644 --- a/src/util/virsocketaddr.h +++ b/src/util/virsocketaddr.h @@ -123,4 +123,5 @@ bool virSocketAddrEqual(const virSocketAddrPtr s1, const virSocketAddrPtr s2); bool virSocketAddrIsPrivate(const virSocketAddrPtr addr); +bool virSocketAddrIsWildcard(const virSocketAddrPtr addr); #endif /* __VIR_SOCKETADDR_H__ */ diff --git a/tests/sockettest.c b/tests/sockettest.c index 5b36a6c..092de88 100644 --- a/tests/sockettest.c +++ b/tests/sockettest.c @@ -157,6 +157,28 @@ static int testNetmaskHelper(const void *opaque) return testNetmask(data->addr1, data->addr2, data->netmask, data->pass); } +static int testWildcard(const char *addrstr, + bool pass) +{ + virSocketAddr addr; + + if (virSocketAddrParse(&addr, addrstr, AF_UNSPEC) < 0) + return -1; + + if (virSocketAddrIsWildcard(&addr)) + return pass ? 0 : -1; + return pass ? -1 : 0; +} + +struct testWildcardData { + const char *addr; + bool pass; +}; +static int testWildcardHelper(const void *opaque) +{ + const struct testWildcardData *data = opaque; + return testWildcard(data->addr, data->pass); +} static int mymain(void) @@ -223,6 +245,14 @@ mymain(void) ret = -1; \ } while (0) +#define DO_TEST_WILDCARD(addr, pass) \ + do { \ + struct testWildcardData data = { addr, pass}; \ + if (virtTestRun("Test wildcard " addr, 1, \ + testWildcardHelper, &data) < 0) \ + ret = -1; \ + } while (0) + DO_TEST_PARSE_AND_FORMAT("127.0.0.1", AF_UNSPEC, true); DO_TEST_PARSE_AND_FORMAT("127.0.0.1", AF_INET, true); @@ -276,6 +306,14 @@ mymain(void) DO_TEST_NETMASK("2000::1:1", "9000::1:1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0", false); + DO_TEST_WILDCARD("0.0.0.0", true); + DO_TEST_WILDCARD("::", true); + DO_TEST_WILDCARD("0", true); + DO_TEST_WILDCARD("0.0", true); + DO_TEST_WILDCARD("0.0.0", true); + DO_TEST_WILDCARD("1", false); + DO_TEST_WILDCARD("0.1", false); + return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 1.8.2.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list