create virSocketAddrGetIpPrefix utility function

Create the utility function virSocketAddrGetIpPrefix() to
determine the prefix for this network.  The code in this
function was adapted from virNetworkIpDefPrefix().

Update virNetworkIpDefPrefix() in src/conf/network_conf.c
to use the new utility function.

Signed-off-by: Gene Czarcinski <gene@czarc.net>
This commit is contained in:
Gene Czarcinski 2013-04-20 15:45:46 -04:00 committed by Laine Stump
parent 1e05073fbb
commit bd7c7c1b3c
4 changed files with 56 additions and 27 deletions

View File

@ -582,33 +582,9 @@ virNetworkDefGetIpByIndex(const virNetworkDefPtr def,
*/
int virNetworkIpDefPrefix(const virNetworkIpDefPtr def)
{
if (def->prefix > 0) {
return def->prefix;
} else if (VIR_SOCKET_ADDR_VALID(&def->netmask)) {
return virSocketAddrGetNumNetmaskBits(&def->netmask);
} else if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, 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.
*/
unsigned char octet
= ntohl(def->address.data.inet4.sin_addr.s_addr) >> 24;
if ((octet & 0x80) == 0) {
/* Class A network */
return 8;
} else if ((octet & 0xC0) == 0x80) {
/* Class B network */
return 16;
} else if ((octet & 0xE0) == 0xC0) {
/* Class C network */
return 24;
}
return -1;
} else if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET6)) {
return 64;
}
return -1;
return virSocketAddrGetIpPrefix(&def->address,
&def->netmask,
def->prefix);
}
/* Fill in a virSocketAddr with the proper netmask for this

View File

@ -1700,6 +1700,7 @@ virSocketAddrCheckNetmask;
virSocketAddrEqual;
virSocketAddrFormat;
virSocketAddrFormatFull;
virSocketAddrGetIpPrefix;
virSocketAddrGetPort;
virSocketAddrGetRange;
virSocketAddrIsNetmask;

View File

@ -754,4 +754,53 @@ virSocketAddrPrefixToNetmask(unsigned int prefix,
error:
return result;
}
/**
* virSocketAddrGetIpPrefix:
* @address: network address
* @netmask: netmask for this network
* @prefix: prefix if specified instead of netmask
*
* Returns prefix value on success or -1 on error.
*/
int
virSocketAddrGetIpPrefix(const virSocketAddrPtr address,
const virSocketAddrPtr netmask,
int prefix)
{
if (prefix > 0) {
return prefix;
} else if (VIR_SOCKET_ADDR_VALID(netmask)) {
return virSocketAddrGetNumNetmaskBits(netmask);
} else if (VIR_SOCKET_ADDR_IS_FAMILY(address, 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.
*/
unsigned char octet
= ntohl(address->data.inet4.sin_addr.s_addr) >> 24;
if ((octet & 0x80) == 0) {
/* Class A network */
return 8;
} else if ((octet & 0xC0) == 0x80) {
/* Class B network */
return 16;
} else if ((octet & 0xE0) == 0xC0) {
/* Class C network */
return 24;
}
return -1;
} else if (VIR_SOCKET_ADDR_IS_FAMILY(address, AF_INET6)) {
return 64;
}
/* When none of the three (address/netmask/prefix) is given, 0 is
* returned rather than error, because this is a valid
* expectation, e.g. for the address/prefix used for a default
* route (the destination of a default route is 0.0.0.0/0).
*/
return 0;
}

View File

@ -116,6 +116,9 @@ int virSocketAddrGetNumNetmaskBits(const virSocketAddrPtr netmask);
int virSocketAddrPrefixToNetmask(unsigned int prefix,
virSocketAddrPtr netmask,
int family);
int virSocketAddrGetIpPrefix(const virSocketAddrPtr address,
const virSocketAddrPtr netmask,
int prefix);
bool virSocketAddrEqual(const virSocketAddrPtr s1,
const virSocketAddrPtr s2);
bool virSocketAddrIsPrivate(const virSocketAddrPtr addr);