Re: [PATCH 1/2] virsocket: Introduce virSocketAddrIsAny

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jun 06, 2013 at 16:10:29 +0200, Michal Privoznik wrote:
> This internal API checks, if passed address is ANYCAST address.
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virsocketaddr.c | 22 ++++++++++++++++++++++
>  src/util/virsocketaddr.h |  1 +
>  tests/sockettest.c       | 38 ++++++++++++++++++++++++++++++++++++++
>  4 files changed, 62 insertions(+)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index b93629f..0b2ce42 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1780,6 +1780,7 @@ virSocketAddrFormatFull;
>  virSocketAddrGetIpPrefix;
>  virSocketAddrGetPort;
>  virSocketAddrGetRange;
> +virSocketAddrIsAny;
>  virSocketAddrIsNetmask;
>  virSocketAddrIsPrivate;
>  virSocketAddrMask;
> diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
> index 1071b00..4bfc86d 100644
> --- a/src/util/virsocketaddr.c
> +++ b/src/util/virsocketaddr.c
> @@ -227,6 +227,28 @@ virSocketAddrIsPrivate(const virSocketAddrPtr addr)
>  }
>  
>  /*
> + * virSocketAddrIsAny:
> + * @addr: address to check
> + *
> + * Check if passed address is a variant of ANYCAST address.
> + */
> +bool
> +virSocketAddrIsAny(const virSocketAddrPtr addr)
> +{
> +    unsigned long tmp;
> +    switch (addr->data.stor.ss_family) {
> +    case AF_INET:
> +        tmp = INADDR_ANY;
> +        return memcmp(&addr->data.inet4.sin_addr.s_addr, &tmp,
> +                      sizeof(addr->data.inet4.sin_addr.s_addr)) == 0;
> +    case AF_INET6:
> +        return memcmp(addr->data.inet6.sin6_addr.s6_addr, &in6addr_any,
> +                      sizeof(addr->data.inet6.sin6_addr.s6_addr)) == 0;
> +    }
> +    return false;

Shouldn't we use in_addr_t (or uint32_t at least) for tmp rather than
unsigned long and

    IN6_IS_ADDR_UNSPECIFIED(&addr->data.inet6.sin6_addr.s6_addr)

instead of the second memcmp? However, I'm not sure how portable that
would be... in.h(0P) man page says both of them shall be defined in
netinet/in.h

> +}
> +
> +/*
>   * virSocketAddrFormat:
>   * @addr: an initialized virSocketAddrPtr
>   *
> diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
> index 1c9e54a..d4d7ccf 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 virSocketAddrIsAny(const virSocketAddrPtr addr);
>  #endif /* __VIR_SOCKETADDR_H__ */
> diff --git a/tests/sockettest.c b/tests/sockettest.c
> index 5b36a6c..e4a998b 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 testAnycast(const char *addrstr,
> +                       bool pass)

I'd probably use a different name since "anycast" has different meaning
in IPv6 world but I don't mind that much as I can't think of a better
name :-)

Jirka

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]