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

[libvirt] [PATCH v5 3/3] net: use structs for address and port ranges



We pass over the address/port start/end values many times so we put
them in structs.

Signed-off-by: Natanael Copa <ncopa alpinelinux org>
---

Skip this patch if there are doubts.

 src/conf/network_conf.c     | 50 ++++++++++++++++++++++-----------------------
 src/conf/network_conf.h     | 13 +++---------
 src/network/bridge_driver.c | 48 +++++++++++++++----------------------------
 src/util/viriptables.c      | 47 +++++++++++++++++-------------------------
 src/util/viriptables.h      | 12 ++++-------
 src/util/virsocketaddr.h    | 14 +++++++++++++
 6 files changed, 81 insertions(+), 103 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index bfdbeef..d384e38 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -628,9 +628,9 @@ int virNetworkIpDefNetmask(const virNetworkIpDefPtr def,
 
 
 static int
-virNetworkDHCPRangeDefParseXML(const char *networkName,
+virSocketAddrRangeParseXML(const char *networkName,
                                xmlNodePtr node,
-                               virNetworkDHCPRangeDefPtr range)
+                               virSocketAddrRangePtr range)
 {
 
 
@@ -793,7 +793,7 @@ virNetworkDHCPDefParseXML(const char *networkName,
                 virReportOOMError();
                 return -1;
             }
-            if (virNetworkDHCPRangeDefParseXML(networkName, cur,
+            if (virSocketAddrRangeParseXML(networkName, cur,
                                                &def->ranges[def->nranges]) < 0) {
                 return -1;
             }
@@ -1376,14 +1376,14 @@ virNetworkForwardNatDefParseXML(const char *networkName,
         }
     }
 
-    if (addrStart && virSocketAddrParse(&def->addrStart, addrStart, AF_INET) < 0) {
+    if (addrStart && virSocketAddrParse(&def->addr.start, addrStart, AF_INET) < 0) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Bad ipv4 start address '%s' in <nat> in <forward> in "
                          "network '%s'"), addrStart, networkName);
         goto cleanup;
     }
 
-    if (addrEnd && virSocketAddrParse(&def->addrEnd, addrEnd, AF_INET) < 0) {
+    if (addrEnd && virSocketAddrParse(&def->addr.end, addrEnd, AF_INET) < 0) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Bad ipv4 end address '%s' in <nat> in <forward> in "
                          "network '%s'"), addrEnd, networkName);
@@ -1403,8 +1403,8 @@ virNetworkForwardNatDefParseXML(const char *networkName,
                          "<forward> in network %s"), networkName);
         goto cleanup;
     } else if (nNatPorts == 1) {
-        if (virXPathUInt("string(./port[1]/@start)", ctxt, &def->portStart) < 0
-            || def->portStart > 65535) {
+        if (virXPathUInt("string(./port[1]/@start)", ctxt, &def->port.start) < 0
+            || def->port.start > 65535) {
 
             virReportError(VIR_ERR_XML_DETAIL,
                            _("Missing or invalid 'start' attribute in <port> "
@@ -1412,8 +1412,8 @@ virNetworkForwardNatDefParseXML(const char *networkName,
                              networkName);
             goto cleanup;
         }
-        if (virXPathUInt("string(./port[1]/@end)", ctxt, &def->portEnd) < 0
-            || def->portEnd > 65535 || def->portEnd < def->portStart) {
+        if (virXPathUInt("string(./port[1]/@end)", ctxt, &def->port.end) < 0
+            || def->port.end > 65535 || def->port.end < def->port.start) {
             virReportError(VIR_ERR_XML_DETAIL,
                            _("Missing or invalid 'end' attribute in <port> in "
                              "<nat> in <forward> in network %s"), networkName);
@@ -2211,19 +2211,19 @@ virNatDefFormat(virBufferPtr buf,
     char *addrEnd = NULL;
     int ret = -1;
 
-    if (VIR_SOCKET_ADDR_VALID(&fwd->addrStart)) {
-        addrStart = virSocketAddrFormat(&fwd->addrStart);
+    if (VIR_SOCKET_ADDR_VALID(&fwd->addr.start)) {
+        addrStart = virSocketAddrFormat(&fwd->addr.start);
         if (!addrStart)
             goto cleanup;
     }
 
-    if (VIR_SOCKET_ADDR_VALID(&fwd->addrEnd)) {
-        addrEnd = virSocketAddrFormat(&fwd->addrEnd);
+    if (VIR_SOCKET_ADDR_VALID(&fwd->addr.end)) {
+        addrEnd = virSocketAddrFormat(&fwd->addr.end);
         if (!addrEnd)
             goto cleanup;
     }
 
-    if (!addrEnd && !addrStart && !fwd->portStart && !fwd->portEnd)
+    if (!addrEnd && !addrStart && !fwd->port.start && !fwd->port.end)
         return 0;
 
     virBufferAddLit(buf, "<nat>\n");
@@ -2236,10 +2236,10 @@ virNatDefFormat(virBufferPtr buf,
         virBufferAsprintf(buf, "/>\n");
     }
 
-    if (fwd->portStart || fwd->portEnd) {
-        virBufferAsprintf(buf, "<port start='%d'", fwd->portStart);
-        if (fwd->portEnd)
-            virBufferAsprintf(buf, " end='%d'", fwd->portEnd);
+    if (fwd->port.start || fwd->port.end) {
+        virBufferAsprintf(buf, "<port start='%d'", fwd->port.start);
+        if (fwd->port.end)
+            virBufferAsprintf(buf, " end='%d'", fwd->port.end);
         virBufferAsprintf(buf, "/>\n");
     }
 
@@ -2298,10 +2298,10 @@ virNetworkDefFormatInternal(virBufferPtr buf,
                 virBufferAddLit(buf, " managed='no'");
         }
         shortforward = !(def->forward.nifs || def->forward.npfs
-                         || VIR_SOCKET_ADDR_VALID(&def->forward.addrStart)
-                         || VIR_SOCKET_ADDR_VALID(&def->forward.addrEnd)
-                         || def->forward.portStart
-                         || def->forward.portEnd);
+                         || VIR_SOCKET_ADDR_VALID(&def->forward.addr.start)
+                         || VIR_SOCKET_ADDR_VALID(&def->forward.addr.end)
+                         || def->forward.port.start
+                         || def->forward.port.end);
         virBufferAsprintf(buf, "%s>\n", shortforward ? "/" : "");
         virBufferAdjustIndent(buf, 2);
 
@@ -3015,7 +3015,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
 {
     int ii, ret = -1;
     virNetworkIpDefPtr ipdef = virNetworkIpDefByIndex(def, parentIndex);
-    virNetworkDHCPRangeDef range;
+    virSocketAddrRange range;
 
     memset(&range, 0, sizeof(range));
 
@@ -3026,7 +3026,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
     if (!ipdef)
         goto cleanup;
 
-    /* parse the xml into a virNetworkDHCPRangeDef */
+    /* parse the xml into a virSocketAddrRange */
     if (command == VIR_NETWORK_UPDATE_COMMAND_MODIFY) {
 
         virReportError(VIR_ERR_NO_SUPPORT, "%s",
@@ -3035,7 +3035,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
         goto cleanup;
     }
 
-    if (virNetworkDHCPRangeDefParseXML(def->name, ctxt->node, &range) < 0)
+    if (virSocketAddrRangeParseXML(def->name, ctxt->node, &range) < 0)
         goto cleanup;
 
     /* check if an entry with same name/address/ip already exists */
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 515115b..d5de7d1 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -62,13 +62,6 @@ enum virNetworkForwardHostdevDeviceType {
     VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_LAST,
 };
 
-typedef struct _virNetworkDHCPRangeDef virNetworkDHCPRangeDef;
-typedef virNetworkDHCPRangeDef *virNetworkDHCPRangeDefPtr;
-struct _virNetworkDHCPRangeDef {
-    virSocketAddr start;
-    virSocketAddr end;
-};
-
 typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef;
 typedef virNetworkDHCPHostDef *virNetworkDHCPHostDefPtr;
 struct _virNetworkDHCPHostDef {
@@ -131,7 +124,7 @@ struct _virNetworkIpDef {
     virSocketAddr netmask;      /* ipv4 - either netmask or prefix specified */
 
     size_t nranges;             /* Zero or more dhcp ranges */
-    virNetworkDHCPRangeDefPtr ranges;
+    virSocketAddrRangePtr ranges;
 
     size_t nhosts;              /* Zero or more dhcp hosts */
     virNetworkDHCPHostDefPtr hosts;
@@ -176,8 +169,8 @@ struct _virNetworkForwardDef {
     virNetworkForwardIfDefPtr ifs;
 
     /* ranges for NAT */
-    virSocketAddr addrStart, addrEnd;
-    unsigned int portStart, portEnd;
+    virSocketAddrRange addr;
+    virPortRange port;
 };
 
 typedef struct _virPortGroupDef virPortGroupDef;
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index cf47ec4..d7f6df9 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -1587,10 +1587,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                      &ipdef->address,
                                      prefix,
                                      forwardIf,
-                                     &network->def->forward.addrStart,
-                                     &network->def->forward.addrEnd,
-                                     network->def->forward.portStart,
-                                     network->def->forward.portEnd,
+                                     &network->def->forward.addr,
+                                     &network->def->forward.port,
                                      NULL) < 0) {
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        forwardIf ?
@@ -1605,10 +1603,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                      &ipdef->address,
                                      prefix,
                                      forwardIf,
-                                     &network->def->forward.addrStart,
-                                     &network->def->forward.addrEnd,
-                                     network->def->forward.portStart,
-                                     network->def->forward.portEnd,
+                                     &network->def->forward.addr,
+                                     &network->def->forward.port,
                                      "udp") < 0) {
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        forwardIf ?
@@ -1623,10 +1619,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                      &ipdef->address,
                                      prefix,
                                      forwardIf,
-                                     &network->def->forward.addrStart,
-                                     &network->def->forward.addrEnd,
-                                     network->def->forward.portStart,
-                                     network->def->forward.portEnd,
+                                     &network->def->forward.addr,
+                                     &network->def->forward.port,
                                      "tcp") < 0) {
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        forwardIf ?
@@ -1643,20 +1637,16 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                     &ipdef->address,
                                     prefix,
                                     forwardIf,
-                                    &network->def->forward.addrStart,
-                                    &network->def->forward.addrEnd,
-                                    network->def->forward.portStart,
-                                    network->def->forward.portEnd,
+                                    &network->def->forward.addr,
+                                    &network->def->forward.port,
                                     "udp");
  masqerr4:
     iptablesRemoveForwardMasquerade(driver->iptables,
                                     &ipdef->address,
                                     prefix,
                                     forwardIf,
-                                    &network->def->forward.addrStart,
-                                    &network->def->forward.addrEnd,
-                                    network->def->forward.portStart,
-                                    network->def->forward.portEnd,
+                                    &network->def->forward.addr,
+                                    &network->def->forward.port,
                                     NULL);
  masqerr3:
     iptablesRemoveForwardAllowRelatedIn(driver->iptables,
@@ -1687,28 +1677,22 @@ networkRemoveMasqueradingIptablesRules(struct network_driver *driver,
                                         &ipdef->address,
                                         prefix,
                                         forwardIf,
-                                        &network->def->forward.addrStart,
-                                        &network->def->forward.addrEnd,
-                                        network->def->forward.portStart,
-                                        network->def->forward.portEnd,
+                                        &network->def->forward.addr,
+                                        &network->def->forward.port,
                                         "tcp");
         iptablesRemoveForwardMasquerade(driver->iptables,
                                         &ipdef->address,
                                         prefix,
                                         forwardIf,
-                                        &network->def->forward.addrStart,
-                                        &network->def->forward.addrEnd,
-                                        network->def->forward.portStart,
-                                        network->def->forward.portEnd,
+                                        &network->def->forward.addr,
+                                        &network->def->forward.port,
                                         "udp");
         iptablesRemoveForwardMasquerade(driver->iptables,
                                         &ipdef->address,
                                         prefix,
                                         forwardIf,
-                                        &network->def->forward.addrStart,
-                                        &network->def->forward.addrEnd,
-                                        network->def->forward.portStart,
-                                        network->def->forward.portEnd,
+                                        &network->def->forward.addr,
+                                        &network->def->forward.port,
                                         NULL);
 
         iptablesRemoveForwardAllowRelatedIn(driver->iptables,
diff --git a/src/util/viriptables.c b/src/util/viriptables.c
index f9f28b4..d142454 100644
--- a/src/util/viriptables.c
+++ b/src/util/viriptables.c
@@ -805,10 +805,8 @@ iptablesForwardMasquerade(iptablesContext *ctx,
                           virSocketAddr *netaddr,
                           unsigned int prefix,
                           const char *physdev,
-                          virSocketAddr *addrStart,
-                          virSocketAddr *addrEnd,
-                          unsigned int portStart,
-                          unsigned int portEnd,
+                          virSocketAddrRangePtr addr,
+                          virPortRangePtr port,
                           const char *protocol,
                           int action)
 {
@@ -831,12 +829,12 @@ iptablesForwardMasquerade(iptablesContext *ctx,
         goto cleanup;
     }
 
-    if (VIR_SOCKET_ADDR_IS_FAMILY(addrStart, AF_INET)) {
-        addrStartStr = virSocketAddrFormat(addrStart);
+    if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
+        addrStartStr = virSocketAddrFormat(&addr->start);
         if (!addrStartStr)
             goto cleanup;
-        if (VIR_SOCKET_ADDR_IS_FAMILY(addrEnd, AF_INET)) {
-            addrEndStr = virSocketAddrFormat(addrEnd);
+        if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
+            addrEndStr = virSocketAddrFormat(&addr->end);
                 if (!addrEndStr)
                 goto cleanup;
         }
@@ -854,20 +852,21 @@ iptablesForwardMasquerade(iptablesContext *ctx,
         virCommandAddArgList(cmd, "--out-interface", physdev, NULL);
 
     if (protocol && protocol[0]) {
-        if (portStart == 0 && portEnd == 0) {
-            portStart = 1024;
-            portEnd = 65535;
+        if (port->start == 0 && port->end == 0) {
+            port->start = 1024;
+            port->end = 65535;
         }
 
-        if (portStart < portEnd && portEnd < 65536) {
-            if (virAsprintf(&portRangeStr, ":%u-%u", portStart, portEnd) < 0) {
+        if (port->start < port->end && port->end < 65536) {
+            if (virAsprintf(&portRangeStr, ":%u-%u",
+                            port->start, port->end) < 0) {
                 virReportOOMError();
                 goto cleanup;
             }
         } else {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Invalid port range '%u-%u'."),
-                           portStart, portEnd);
+                           port->start, port->end);
         }
     }
 
@@ -923,15 +922,11 @@ iptablesAddForwardMasquerade(iptablesContext *ctx,
                              virSocketAddr *netaddr,
                              unsigned int prefix,
                              const char *physdev,
-                             virSocketAddr *addrStart,
-                             virSocketAddr *addrEnd,
-                             unsigned int portStart,
-                             unsigned int portEnd,
+                             virSocketAddrRangePtr addr,
+                             virPortRangePtr port,
                              const char *protocol)
 {
-    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev,
-                                     addrStart, addrEnd,
-                                     portStart, portEnd,
+    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, addr, port,
                                      protocol, ADD);
 }
 
@@ -953,15 +948,11 @@ iptablesRemoveForwardMasquerade(iptablesContext *ctx,
                                 virSocketAddr *netaddr,
                                 unsigned int prefix,
                                 const char *physdev,
-                                virSocketAddr *addrStart,
-                                virSocketAddr *addrEnd,
-                                unsigned int portStart,
-                                unsigned int portEnd,
+                                virSocketAddrRangePtr addr,
+                                virPortRangePtr port,
                                 const char *protocol)
 {
-    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev,
-                                     addrStart, addrEnd,
-                                     portStart, portEnd,
+    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, addr, port,
                                      protocol, REMOVE);
 }
 
diff --git a/src/util/viriptables.h b/src/util/viriptables.h
index ca6adcc..b7ce59b 100644
--- a/src/util/viriptables.h
+++ b/src/util/viriptables.h
@@ -107,19 +107,15 @@ int              iptablesAddForwardMasquerade    (iptablesContext *ctx,
                                                   virSocketAddr *netaddr,
                                                   unsigned int prefix,
                                                   const char *physdev,
-                                                  virSocketAddr *addrStart,
-                                                  virSocketAddr *addrEnd,
-                                                  unsigned int portStart,
-                                                  unsigned int portEnd,
+                                                  virSocketAddrRangePtr addr,
+                                                  virPortRangePtr port,
                                                   const char *protocol);
 int              iptablesRemoveForwardMasquerade (iptablesContext *ctx,
                                                   virSocketAddr *netaddr,
                                                   unsigned int prefix,
                                                   const char *physdev,
-                                                  virSocketAddr *addrStart,
-                                                  virSocketAddr *addrEnd,
-                                                  unsigned int portStart,
-                                                  unsigned int portEnd,
+                                                  virSocketAddrRangePtr addr,
+                                                  virPortRangePtr port,
                                                   const char *protocol);
 int              iptablesAddOutputFixUdpChecksum (iptablesContext *ctx,
                                                   const char *iface,
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 66d4265..8993f7b 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -56,6 +56,20 @@ typedef struct {
 
 typedef virSocketAddr *virSocketAddrPtr;
 
+typedef struct _virSocketAddrRange virSocketAddrRange;
+typedef virSocketAddrRange *virSocketAddrRangePtr;
+struct _virSocketAddrRange {
+    virSocketAddr start;
+    virSocketAddr end;
+};
+
+typedef struct _virPortRange virPortRange;
+typedef virPortRange *virPortRangePtr;
+struct _virPortRange {
+    unsigned int start;
+    unsigned int end;
+};
+
 int virSocketAddrParse(virSocketAddrPtr addr,
                        const char *val,
                        int family);
-- 
1.8.1.3


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