]> xenbits.xensource.com Git - libvirt.git/commitdiff
New virNetworkDef utility functions
authorLaine Stump <laine@laine.org>
Fri, 26 Nov 2010 22:20:37 +0000 (17:20 -0500)
committerLaine Stump <laine@laine.org>
Thu, 23 Dec 2010 20:52:20 +0000 (15:52 -0500)
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)

src/conf/network_conf.c
src/conf/network_conf.h
src/libvirt_private.syms

index b469269e10c0e6d5003bff6d2a4b62d2b118d883..b5c61dade5a78c357ca211503e5e1b8a303e487f 100644 (file)
@@ -207,6 +207,52 @@ void virNetworkRemoveInactive(virNetworkObjListPtr nets,
     }
 }
 
+/* return number of 1 bits in netmask for the network's ipAddress,
+ * or -1 on error
+ */
+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.
+         */
+        unsigned char octet
+            = ntohl(def->ipAddress.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;
+    }
+    return -1;
+}
+
+/* Fill in a virSocketAddr with the proper netmask for this
+ * definition, based on either the definition's netmask, or its
+ * prefix. Return -1 on error (and set the netmask family to AF_UNSPEC)
+ */
+int virNetworkDefNetmask(const virNetworkDefPtr def,
+                         virSocketAddrPtr netmask)
+{
+    if (VIR_SOCKET_IS_FAMILY(&def->netmask, AF_INET)) {
+        *netmask = def->netmask;
+        return 0;
+    }
+
+    return virSocketAddrPrefixToNetmask(virNetworkDefPrefix(def), netmask,
+                                        VIR_SOCKET_FAMILY(&def->ipAddress));
+}
+
 
 static int
 virNetworkDHCPRangeDefParseXML(virNetworkDefPtr def,
index 7d316933327508505ed51fd4949e57da57b02723..a922d286040af9640b5e8e97d0aa7cc45008bc89 100644 (file)
@@ -133,6 +133,9 @@ virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
 
 char *virNetworkDefFormat(const virNetworkDefPtr def);
 
+int virNetworkDefPrefix(const virNetworkDefPtr def);
+int virNetworkDefNetmask(const virNetworkDefPtr def,
+                         virSocketAddrPtr netmask);
 
 int virNetworkSaveXML(const char *configDir,
                       virNetworkDefPtr def,
index 19841ef96a7a972d8f68d25020f4f79f860f671d..d9ba7b19b5ff551c02f2edcac6188cabb4463580 100644 (file)
@@ -578,9 +578,11 @@ virNetworkAssignDef;
 virNetworkConfigFile;
 virNetworkDefFormat;
 virNetworkDefFree;
+virNetworkDefNetmask;
 virNetworkDefParseFile;
 virNetworkDefParseNode;
 virNetworkDefParseString;
+virNetworkDefPrefix;
 virNetworkDeleteConfig;
 virNetworkFindByName;
 virNetworkFindByUUID;