From 688994364fd06036b41cad0c0eec59ff436cdcdb Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Thu, 6 Jun 2013 15:50:01 +0200 Subject: [PATCH] virsocket: Introduce virSocketAddrIsWildcard This internal API checks, if passed address is a wildcard 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 b93629feb8..1ea7467497 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 1071b0056e..e84c58e81e 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -226,6 +226,26 @@ virSocketAddrIsPrivate(const virSocketAddrPtr addr) return false; } +/* + * 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 1c9e54a441..b28fe6c7d5 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 5b36a6c0bf..092de88ba6 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; }