mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
virsocket: Introduce virSocketAddrIsWildcard
This internal API checks, if passed address is a wildcard address.
This commit is contained in:
parent
cbdb3c7326
commit
688994364f
@ -1782,6 +1782,7 @@ virSocketAddrGetPort;
|
||||
virSocketAddrGetRange;
|
||||
virSocketAddrIsNetmask;
|
||||
virSocketAddrIsPrivate;
|
||||
virSocketAddrIsWildcard;
|
||||
virSocketAddrMask;
|
||||
virSocketAddrMaskByPrefix;
|
||||
virSocketAddrParse;
|
||||
|
@ -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
|
||||
|
@ -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__ */
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user