[libvirt] [PATCHv2 02/13] New virNetworkDef utility functions

Eric Blake eblake at redhat.com
Wed Dec 22 23:10:33 UTC 2010


On 12/22/2010 11:58 AM, Laine Stump wrote:
> Later patches will add the possibility to define a network's netmask
> as a prefix (0-32, or 0-128 in the case of IPv6). To make it easier to
> deal with definition of both kinds (prefix or netmask), add two new
> functions:
> 
> virNetworkDefNetmask: return a copy of the netmask into a
> virSocketAddr. If no netmask was specified in the XML, create a
> default netmask based on the network class of the virNetworkDef's IP
> address.
> 
> virNetworkDefPrefix: return the netmask as numeric prefix (or the
> default prefix for the network class of the virNetworkDef's IP
> address, if no netmask was specified in the XML)

> +int virNetworkDefPrefix(const virNetworkDefPtr def)
> +{
> +    if (VIR_SOCKET_HAS_ADDR(&def->netmask)) {
> +        return virSocketGetNumNetmaskBits(&def->netmask);
> +    } else if (VIR_SOCKET_IS_FAMILY(&def->ipAddress, AF_INET)) {
> +        /* Return the natural prefix for the network's ip address.
> +         * On Linux we could use the IN_CLASSx() macros, but those
> +         * aren't guaranteed on all platforms, so we just deal with
> +         * the bits ourselves.
> +         */
> +        const unsigned char *octets
> +            = (const unsigned char *)(&def->ipAddress.data.inet4.sin_addr.s_addr);

Is this type-punning guaranteed to work on both big- and little-endian
systems?  Or are you better off doing:

unsigned char octet
  = ntohl(def->ipAddress.data.inet4.sin_addr.s_addr) >> 24;

and use octet instead of octets[0]?

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20101222/00b740db/attachment-0001.sig>


More information about the libvir-list mailing list