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

[libvirt] [PATCH 09/11] Remove all use of inet_pton and inet_ntop



The  inet_pton and inet_ntop functions are obsolete, replaced
by getaddrinfo+getnameinfo with the AI_NUMERICHOST flag set.
These can be accessed via the virSocket APIs.

The bridge.c code had methods for fetching the IP address of
a bridge which used inet_ntop. Aside from the use of inet_ntop
these methods are broken, because a NIC can have multiple
addresses and this only returns one address. Since the methods
are never used, just remove them.

* src/conf/network_conf.c, src/nwfilter/nwfilter_learnipaddr.c:
  Replace inet_pton and inet_ntop with virSocket APIs
* src/util/bridge.c, src/util/bridge.h: Remove unused methods
  which called inet_ntop.
---
 src/conf/network_conf.c             |   36 +++++++-------
 src/nwfilter/nwfilter_learnipaddr.c |   36 +++++++------
 src/util/bridge.c                   |   95 ++---------------------------------
 src/util/bridge.h                   |    8 ---
 4 files changed, 43 insertions(+), 132 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index fe52f95..0663d52 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -278,7 +278,7 @@ virNetworkDHCPRangeDefParseXML(virNetworkDefPtr def,
             xmlStrEqual(cur->name, BAD_CAST "host")) {
             xmlChar *mac, *name, *ip;
             unsigned char addr[6];
-            struct in_addr inaddress;
+            virSocketAddr inaddr;
 
             mac = xmlGetProp(cur, BAD_CAST "mac");
             if ((mac != NULL) &&
@@ -305,10 +305,7 @@ virNetworkDHCPRangeDefParseXML(virNetworkDefPtr def,
                 continue;
             }
             ip = xmlGetProp(cur, BAD_CAST "ip");
-            if (inet_pton(AF_INET, (const char *) ip, &inaddress) <= 0) {
-                virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
-                                      _("cannot parse IP address '%s'"),
-                                      ip);
+            if (virSocketParseAddr((const char *)ip, &inaddr, AF_UNSPEC) < 0) {
                 VIR_FREE(ip);
                 VIR_FREE(mac);
                 VIR_FREE(name);
@@ -428,31 +425,34 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
     def->netmask = virXPathString("string(./ip[1]/@netmask)", ctxt);
     if (def->ipAddress &&
         def->netmask) {
-        /* XXX someday we want IPv6 too, so inet_aton won't work there */
-        struct in_addr inaddress, innetmask;
+        virSocketAddr inaddress, innetmask;
         char *netaddr;
         xmlNodePtr ip;
 
-        if (inet_pton(AF_INET, def->ipAddress, &inaddress) <= 0) {
-            virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
-                                  _("cannot parse IP address '%s'"),
-                                  def->ipAddress);
+        if (virSocketParseAddr(def->ipAddress, &inaddress, AF_UNSPEC) < 0)
             goto error;
-        }
-        if (inet_pton(AF_INET, def->netmask, &innetmask) <= 0) {
-            virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
-                                  _("cannot parse netmask '%s'"),
-                                  def->netmask);
+        if (virSocketParseAddr(def->netmask, &innetmask, AF_UNSPEC) < 0)
+            goto error;
+
+        /* XXX someday we want IPv6, so will need to relax this */
+        if (inaddress.data.sa.sa_family != AF_INET ||
+            innetmask.data.sa.sa_family != AF_INET) {
+            virNetworkReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                                  "%s", _("Only IPv4 addresses are supported"));
             goto error;
         }
 
-        inaddress.s_addr &= innetmask.s_addr;
-        netaddr = inet_ntoa(inaddress);
+        inaddress.data.inet4.sin_addr.s_addr &=
+            innetmask.data.inet4.sin_addr.s_addr;
+        if (!(netaddr = virSocketFormatAddr(&inaddress)))
+            goto error;
 
         if (virAsprintf(&def->network, "%s/%s", netaddr, def->netmask) < 0) {
+            VIR_FREE(netaddr);
             virReportOOMError();
             goto error;
         }
+        VIR_FREE(netaddr);
 
         if ((ip = virXPathNode("./ip[1]", ctxt)) &&
             virNetworkIPParseXML(def, ip) < 0)
diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c
index 7c94fc2..813a205 100644
--- a/src/nwfilter/nwfilter_learnipaddr.c
+++ b/src/nwfilter/nwfilter_learnipaddr.c
@@ -627,22 +627,26 @@ learnIPAddressThread(void *arg)
 
     if (req->status == 0) {
         int ret;
-        char inetaddr[INET_ADDRSTRLEN];
-        inet_ntop(AF_INET, &vmaddr, inetaddr, sizeof(inetaddr));
-
-        virNWFilterAddIpAddrForIfname(req->ifname, strdup(inetaddr));
-
-        ret = virNWFilterInstantiateFilterLate(NULL,
-                                               req->ifname,
-                                               req->ifindex,
-                                               req->linkdev,
-                                               req->nettype,
-                                               req->macaddr,
-                                               req->filtername,
-                                               req->filterparams,
-                                               req->driver);
-        VIR_DEBUG("Result from applying firewall rules on "
-                  "%s with IP addr %s : %d\n", req->ifname, inetaddr, ret);
+        virSocketAddr sa;
+        sa.len = sizeof(sa.data.inet4);
+        sa.data.inet4.sin_addr.s_addr = vmaddr;
+        char *inetaddr;
+
+        if ((inetaddr = virSocketFormatAddr(&sa))!= NULL) {
+            virNWFilterAddIpAddrForIfname(req->ifname, inetaddr);
+
+            ret = virNWFilterInstantiateFilterLate(NULL,
+                                                   req->ifname,
+                                                   req->ifindex,
+                                                   req->linkdev,
+                                                   req->nettype,
+                                                   req->macaddr,
+                                                   req->filtername,
+                                                   req->filterparams,
+                                                   req->driver);
+            VIR_DEBUG("Result from applying firewall rules on "
+                      "%s with IP addr %s : %d\n", req->ifname, inetaddr, ret);
+        }
     } else {
         if (showError)
             virReportSystemError(req->status,
diff --git a/src/util/bridge.c b/src/util/bridge.c
index da62c5e..b4a7e26 100644
--- a/src/util/bridge.c
+++ b/src/util/bridge.c
@@ -48,6 +48,7 @@
 # include "memory.h"
 # include "util.h"
 # include "logging.h"
+# include "network.h"
 
 # define JIFFIES_TO_MS(j) (((j)*1000)/HZ)
 # define MS_TO_JIFFIES(ms) (((ms)*HZ)/1000)
@@ -660,13 +661,8 @@ brSetInetAddr(brControl *ctl,
               int cmd,
               const char *addr)
 {
-    union {
-        struct sockaddr sa;
-        struct sockaddr_in sa_in;
-    } s;
+    virSocketAddr sa;
     struct ifreq ifr;
-    struct in_addr inaddr;
-    int ret;
 
     if (!ctl || !ctl->fd || !ifname || !addr)
         return EINVAL;
@@ -676,51 +672,17 @@ brSetInetAddr(brControl *ctl,
     if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL)
         return EINVAL;
 
-    if ((ret = inet_pton(AF_INET, addr, &inaddr)) < 0)
-        return errno;
-    else if (ret == 0)
+    if (virSocketParseAddr(addr, &sa, AF_UNSPEC) < 0)
         return EINVAL;
 
-    s.sa_in.sin_family = AF_INET;
-    s.sa_in.sin_addr   = inaddr;
-
-    ifr.ifr_addr = s.sa;
-
-    if (ioctl(ctl->fd, cmd, &ifr) < 0)
-        return errno;
-
-    return 0;
-}
-
-static int
-brGetInetAddr(brControl *ctl,
-              const char *ifname,
-              int cmd,
-              char *addr,
-              int maxlen)
-{
-    struct ifreq ifr;
-    struct in_addr *inaddr;
-
-    if (!ctl || !ctl->fd || !ifname || !addr)
+    if (sa.data.sa.sa_family != AF_INET)
         return EINVAL;
 
-    memset(&ifr, 0, sizeof(struct ifreq));
-
-    if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL)
-        return EINVAL;
+    ifr.ifr_addr = sa.data.sa;
 
     if (ioctl(ctl->fd, cmd, &ifr) < 0)
         return errno;
 
-    if (maxlen < BR_INET_ADDR_MAXLEN || ifr.ifr_addr.sa_family != AF_INET)
-        return EFAULT;
-
-    inaddr = &((struct sockaddr_in *)&ifr.ifr_data)->sin_addr;
-
-    if (!inet_ntop(AF_INET, inaddr, addr, maxlen))
-        return errno;
-
     return 0;
 }
 
@@ -746,29 +708,6 @@ brSetInetAddress(brControl *ctl,
 }
 
 /**
- * brGetInetAddress:
- * @ctl: bridge control pointer
- * @ifname: the interface name
- * @addr: the array for the string representation of the IP address
- * @maxlen: size of @addr in bytes
- *
- * Function to get the IP address of an interface, it should handle
- * IPV4 and IPv6. The returned string for addr would be of the form
- * "ddd.ddd.ddd.ddd" assuming the common IPv4 format.
- *
- * Returns 0 in case of success or an errno code in case of failure.
- */
-
-int
-brGetInetAddress(brControl *ctl,
-                 const char *ifname,
-                 char *addr,
-                 int maxlen)
-{
-    return brGetInetAddr(ctl, ifname, SIOCGIFADDR, addr, maxlen);
-}
-
-/**
  * brSetInetNetmask:
  * @ctl: bridge control pointer
  * @ifname: the interface name
@@ -790,30 +729,6 @@ brSetInetNetmask(brControl *ctl,
 }
 
 /**
- * brGetInetNetmask:
- * @ctl: bridge control pointer
- * @ifname: the interface name
- * @addr: the array for the string representation of the netmask
- * @maxlen: size of @addr in bytes
- *
- * Function to get the netmask of an interface, it should handle
- * IPV4 and IPv6. The returned string for addr would be of the form
- * "ddd.ddd.ddd.ddd" assuming the common IPv4 format.
- *
- * Returns 0 in case of success or an errno code in case of failure.
- */
-
-int
-brGetInetNetmask(brControl *ctl,
-                 const char *ifname,
-                 char *addr,
-                 int maxlen)
-{
-    return brGetInetAddr(ctl, ifname, SIOCGIFNETMASK, addr, maxlen);
-}
-
-
-/**
  * brSetForwardDelay:
  * @ctl: bridge control pointer
  * @bridge: the bridge name
diff --git a/src/util/bridge.h b/src/util/bridge.h
index 96696ac..abcd1b5 100644
--- a/src/util/bridge.h
+++ b/src/util/bridge.h
@@ -85,17 +85,9 @@ int     brGetInterfaceUp        (brControl *ctl,
 int     brSetInetAddress        (brControl *ctl,
                                  const char *ifname,
                                  const char *addr);
-int     brGetInetAddress        (brControl *ctl,
-                                 const char *ifname,
-                                 char *addr,
-                                 int maxlen);
 int     brSetInetNetmask        (brControl *ctl,
                                  const char *ifname,
                                  const char *netmask);
-int     brGetInetNetmask        (brControl *ctl,
-                                 const char *ifname,
-                                 char *netmask,
-                                 int maxlen);
 
 int     brSetForwardDelay       (brControl *ctl,
                                  const char *bridge,
-- 
1.7.2.3


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