[libvirt] [PATCH 2/3] util: new virSocketAddrIsPrivate function

Doug Goldstein cardoe at cardoe.com
Thu Nov 22 06:08:08 UTC 2012


On Nov 21, 2012, at 8:55 PM, Laine Stump <laine at laine.org> wrote:

> This new function returns true if the given address is in the range of
> any "private" or "local" networks as defined in RFC1918 (IPv4) or
> RFC4193 (IPv6), otherwise they return false.
> 
> These ranges are:
> 
>   192.168.0.0/16
>   172.16.0.0/16
>   10.0.0.0/24
>   FC00::/7
> ---
> src/libvirt_private.syms |  1 +
> src/util/virsocketaddr.c | 32 +++++++++++++++++++++++++++++++-
> src/util/virsocketaddr.h |  3 ++-
> 3 files changed, 34 insertions(+), 2 deletions(-)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 24d2033..85bff2c 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1776,6 +1776,7 @@ virSocketAddrFormatFull;
> virSocketAddrGetPort;
> virSocketAddrGetRange;
> virSocketAddrIsNetmask;
> +virSocketAddrIsPrivate;
> virSocketAddrMask;
> virSocketAddrMaskByPrefix;
> virSocketAddrParse;
> diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
> index b6cc16f..2d39458 100644
> --- a/src/util/virsocketaddr.c
> +++ b/src/util/virsocketaddr.c
> @@ -1,5 +1,5 @@
> /*
> - * Copyright (C) 2009-2011 Red Hat, Inc.
> + * Copyright (C) 2009-2012 Red Hat, Inc.
>  *
>  * This library is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU Lesser General Public
> @@ -194,6 +194,36 @@ virSocketAddrEqual(const virSocketAddrPtr s1, const virSocketAddrPtr s2)
> }
> 
> /*
> + * virSocketAddrIsPrivate:
> + * @s: the location of the IP address
> + *
> + * Return true if this address is in its family's defined
> + * "private/local" address space. For IPv4, private addresses are in
> + * the range of 192.168.0.0/16, 172.16.0.0/16, or 10.0.0.0/8.  For
> + * IPv6, local addresses are in the range of FC00::/7.
> + *
> + * See RFC1918 and RFC4193 for details.
> + */
> +bool
> +virSocketAddrIsPrivate(const virSocketAddrPtr addr)
> +{
> +    unsigned long val;
> +
> +    switch (addr->data.stor.ss_family) {
> +    case AF_INET:
> +       val = ntohl(addr->data.inet4.sin_addr.s_addr);
> +
> +       return ((val & 0xFFFF0000) == ((192L << 24) + (168 << 16)) ||
> +               (val & 0xFFFF0000) == ((172L << 24) + (16  << 16)) ||
> +               (val & 0xFF000000) == ((10L  << 24)));
> +
> +    case AF_INET6:
> +        return (addr->data.inet6.sin6_addr.s6_addr[0] & 0xFC) == 0xFC;
> +    }
> +    return false;
> +}
> +
> +/*
>  * virSocketAddrFormat:
>  * @addr: an initialized virSocketAddrPtr
>  *
> diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
> index 51ddd2d..66d4265 100644
> --- a/src/util/virsocketaddr.h
> +++ b/src/util/virsocketaddr.h
> @@ -1,5 +1,5 @@
> /*
> - * Copyright (C) 2009-2011 Red Hat, Inc.
> + * Copyright (C) 2009-2012 Red Hat, Inc.
>  *
>  * This library is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU Lesser General Public
> @@ -104,5 +104,6 @@ int virSocketAddrPrefixToNetmask(unsigned int prefix,
>                                  int family);
> bool virSocketAddrEqual(const virSocketAddrPtr s1,
>                         const virSocketAddrPtr s2);
> +bool virSocketAddrIsPrivate(const virSocketAddrPtr addr);
> 
> #endif /* __VIR_SOCKETADDR_H__ */
> -- 
> 1.7.11.7
> 
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

ACK. Looks good.

--
Doug




More information about the libvir-list mailing list