提交 bac8b2ca 编写于 作者: N Natanael Copa 提交者: Laine Stump

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: NNatanael Copa <ncopa@alpinelinux.org>
Signed-off-by: NLaine Stump <laine@laine.org>
上级 1716e7a6
...@@ -628,9 +628,9 @@ int virNetworkIpDefNetmask(const virNetworkIpDefPtr def, ...@@ -628,9 +628,9 @@ int virNetworkIpDefNetmask(const virNetworkIpDefPtr def,
static int static int
virNetworkDHCPRangeDefParseXML(const char *networkName, virSocketAddrRangeParseXML(const char *networkName,
xmlNodePtr node, xmlNodePtr node,
virNetworkDHCPRangeDefPtr range) virSocketAddrRangePtr range)
{ {
...@@ -793,7 +793,7 @@ virNetworkDHCPDefParseXML(const char *networkName, ...@@ -793,7 +793,7 @@ virNetworkDHCPDefParseXML(const char *networkName,
virReportOOMError(); virReportOOMError();
return -1; return -1;
} }
if (virNetworkDHCPRangeDefParseXML(networkName, cur, if (virSocketAddrRangeParseXML(networkName, cur,
&def->ranges[def->nranges]) < 0) { &def->ranges[def->nranges]) < 0) {
return -1; return -1;
} }
...@@ -1376,14 +1376,14 @@ virNetworkForwardNatDefParseXML(const char *networkName, ...@@ -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, virReportError(VIR_ERR_XML_ERROR,
_("Bad ipv4 start address '%s' in <nat> in <forward> in " _("Bad ipv4 start address '%s' in <nat> in <forward> in "
"network '%s'"), addrStart, networkName); "network '%s'"), addrStart, networkName);
goto cleanup; 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, virReportError(VIR_ERR_XML_ERROR,
_("Bad ipv4 end address '%s' in <nat> in <forward> in " _("Bad ipv4 end address '%s' in <nat> in <forward> in "
"network '%s'"), addrEnd, networkName); "network '%s'"), addrEnd, networkName);
...@@ -1403,8 +1403,8 @@ virNetworkForwardNatDefParseXML(const char *networkName, ...@@ -1403,8 +1403,8 @@ virNetworkForwardNatDefParseXML(const char *networkName,
"<forward> in network %s"), networkName); "<forward> in network %s"), networkName);
goto cleanup; goto cleanup;
} else if (nNatPorts == 1) { } else if (nNatPorts == 1) {
if (virXPathUInt("string(./port[1]/@start)", ctxt, &def->portStart) < 0 if (virXPathUInt("string(./port[1]/@start)", ctxt, &def->port.start) < 0
|| def->portStart > 65535) { || def->port.start > 65535) {
virReportError(VIR_ERR_XML_DETAIL, virReportError(VIR_ERR_XML_DETAIL,
_("Missing or invalid 'start' attribute in <port> " _("Missing or invalid 'start' attribute in <port> "
...@@ -1412,8 +1412,8 @@ virNetworkForwardNatDefParseXML(const char *networkName, ...@@ -1412,8 +1412,8 @@ virNetworkForwardNatDefParseXML(const char *networkName,
networkName); networkName);
goto cleanup; goto cleanup;
} }
if (virXPathUInt("string(./port[1]/@end)", ctxt, &def->portEnd) < 0 if (virXPathUInt("string(./port[1]/@end)", ctxt, &def->port.end) < 0
|| def->portEnd > 65535 || def->portEnd < def->portStart) { || def->port.end > 65535 || def->port.end < def->port.start) {
virReportError(VIR_ERR_XML_DETAIL, virReportError(VIR_ERR_XML_DETAIL,
_("Missing or invalid 'end' attribute in <port> in " _("Missing or invalid 'end' attribute in <port> in "
"<nat> in <forward> in network %s"), networkName); "<nat> in <forward> in network %s"), networkName);
...@@ -2212,19 +2212,19 @@ virNetworkForwardNatDefFormat(virBufferPtr buf, ...@@ -2212,19 +2212,19 @@ virNetworkForwardNatDefFormat(virBufferPtr buf,
char *addrEnd = NULL; char *addrEnd = NULL;
int ret = -1; int ret = -1;
if (VIR_SOCKET_ADDR_VALID(&fwd->addrStart)) { if (VIR_SOCKET_ADDR_VALID(&fwd->addr.start)) {
addrStart = virSocketAddrFormat(&fwd->addrStart); addrStart = virSocketAddrFormat(&fwd->addr.start);
if (!addrStart) if (!addrStart)
goto cleanup; goto cleanup;
} }
if (VIR_SOCKET_ADDR_VALID(&fwd->addrEnd)) { if (VIR_SOCKET_ADDR_VALID(&fwd->addr.end)) {
addrEnd = virSocketAddrFormat(&fwd->addrEnd); addrEnd = virSocketAddrFormat(&fwd->addr.end);
if (!addrEnd) if (!addrEnd)
goto cleanup; goto cleanup;
} }
if (!addrEnd && !addrStart && !fwd->portStart && !fwd->portEnd) if (!addrEnd && !addrStart && !fwd->port.start && !fwd->port.end)
return 0; return 0;
virBufferAddLit(buf, "<nat>\n"); virBufferAddLit(buf, "<nat>\n");
...@@ -2237,10 +2237,10 @@ virNetworkForwardNatDefFormat(virBufferPtr buf, ...@@ -2237,10 +2237,10 @@ virNetworkForwardNatDefFormat(virBufferPtr buf,
virBufferAddLit(buf, "/>\n"); virBufferAddLit(buf, "/>\n");
} }
if (fwd->portStart || fwd->portEnd) { if (fwd->port.start || fwd->port.end) {
virBufferAsprintf(buf, "<port start='%d'", fwd->portStart); virBufferAsprintf(buf, "<port start='%d'", fwd->port.start);
if (fwd->portEnd) if (fwd->port.end)
virBufferAsprintf(buf, " end='%d'", fwd->portEnd); virBufferAsprintf(buf, " end='%d'", fwd->port.end);
virBufferAddLit(buf, "/>\n"); virBufferAddLit(buf, "/>\n");
} }
...@@ -2299,10 +2299,10 @@ virNetworkDefFormatInternal(virBufferPtr buf, ...@@ -2299,10 +2299,10 @@ virNetworkDefFormatInternal(virBufferPtr buf,
virBufferAddLit(buf, " managed='no'"); virBufferAddLit(buf, " managed='no'");
} }
shortforward = !(def->forward.nifs || def->forward.npfs shortforward = !(def->forward.nifs || def->forward.npfs
|| VIR_SOCKET_ADDR_VALID(&def->forward.addrStart) || VIR_SOCKET_ADDR_VALID(&def->forward.addr.start)
|| VIR_SOCKET_ADDR_VALID(&def->forward.addrEnd) || VIR_SOCKET_ADDR_VALID(&def->forward.addr.end)
|| def->forward.portStart || def->forward.port.start
|| def->forward.portEnd); || def->forward.port.end);
virBufferAsprintf(buf, "%s>\n", shortforward ? "/" : ""); virBufferAsprintf(buf, "%s>\n", shortforward ? "/" : "");
virBufferAdjustIndent(buf, 2); virBufferAdjustIndent(buf, 2);
...@@ -3016,7 +3016,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def, ...@@ -3016,7 +3016,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
{ {
int ii, ret = -1; int ii, ret = -1;
virNetworkIpDefPtr ipdef = virNetworkIpDefByIndex(def, parentIndex); virNetworkIpDefPtr ipdef = virNetworkIpDefByIndex(def, parentIndex);
virNetworkDHCPRangeDef range; virSocketAddrRange range;
memset(&range, 0, sizeof(range)); memset(&range, 0, sizeof(range));
...@@ -3027,7 +3027,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def, ...@@ -3027,7 +3027,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
if (!ipdef) if (!ipdef)
goto cleanup; goto cleanup;
/* parse the xml into a virNetworkDHCPRangeDef */ /* parse the xml into a virSocketAddrRange */
if (command == VIR_NETWORK_UPDATE_COMMAND_MODIFY) { if (command == VIR_NETWORK_UPDATE_COMMAND_MODIFY) {
virReportError(VIR_ERR_NO_SUPPORT, "%s", virReportError(VIR_ERR_NO_SUPPORT, "%s",
...@@ -3036,7 +3036,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def, ...@@ -3036,7 +3036,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
goto cleanup; goto cleanup;
} }
if (virNetworkDHCPRangeDefParseXML(def->name, ctxt->node, &range) < 0) if (virSocketAddrRangeParseXML(def->name, ctxt->node, &range) < 0)
goto cleanup; goto cleanup;
/* check if an entry with same name/address/ip already exists */ /* check if an entry with same name/address/ip already exists */
......
...@@ -62,13 +62,6 @@ enum virNetworkForwardHostdevDeviceType { ...@@ -62,13 +62,6 @@ enum virNetworkForwardHostdevDeviceType {
VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_LAST, VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_LAST,
}; };
typedef struct _virNetworkDHCPRangeDef virNetworkDHCPRangeDef;
typedef virNetworkDHCPRangeDef *virNetworkDHCPRangeDefPtr;
struct _virNetworkDHCPRangeDef {
virSocketAddr start;
virSocketAddr end;
};
typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef; typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef;
typedef virNetworkDHCPHostDef *virNetworkDHCPHostDefPtr; typedef virNetworkDHCPHostDef *virNetworkDHCPHostDefPtr;
struct _virNetworkDHCPHostDef { struct _virNetworkDHCPHostDef {
...@@ -131,7 +124,7 @@ struct _virNetworkIpDef { ...@@ -131,7 +124,7 @@ struct _virNetworkIpDef {
virSocketAddr netmask; /* ipv4 - either netmask or prefix specified */ virSocketAddr netmask; /* ipv4 - either netmask or prefix specified */
size_t nranges; /* Zero or more dhcp ranges */ size_t nranges; /* Zero or more dhcp ranges */
virNetworkDHCPRangeDefPtr ranges; virSocketAddrRangePtr ranges;
size_t nhosts; /* Zero or more dhcp hosts */ size_t nhosts; /* Zero or more dhcp hosts */
virNetworkDHCPHostDefPtr hosts; virNetworkDHCPHostDefPtr hosts;
...@@ -176,8 +169,8 @@ struct _virNetworkForwardDef { ...@@ -176,8 +169,8 @@ struct _virNetworkForwardDef {
virNetworkForwardIfDefPtr ifs; virNetworkForwardIfDefPtr ifs;
/* ranges for NAT */ /* ranges for NAT */
virSocketAddr addrStart, addrEnd; virSocketAddrRange addr;
unsigned int portStart, portEnd; virPortRange port;
}; };
typedef struct _virPortGroupDef virPortGroupDef; typedef struct _virPortGroupDef virPortGroupDef;
......
...@@ -1587,10 +1587,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver, ...@@ -1587,10 +1587,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
NULL) < 0) { NULL) < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR, virReportError(VIR_ERR_SYSTEM_ERROR,
forwardIf ? forwardIf ?
...@@ -1605,10 +1603,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver, ...@@ -1605,10 +1603,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
"udp") < 0) { "udp") < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR, virReportError(VIR_ERR_SYSTEM_ERROR,
forwardIf ? forwardIf ?
...@@ -1623,10 +1619,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver, ...@@ -1623,10 +1619,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
"tcp") < 0) { "tcp") < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR, virReportError(VIR_ERR_SYSTEM_ERROR,
forwardIf ? forwardIf ?
...@@ -1643,20 +1637,16 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver, ...@@ -1643,20 +1637,16 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
"udp"); "udp");
masqerr4: masqerr4:
iptablesRemoveForwardMasquerade(driver->iptables, iptablesRemoveForwardMasquerade(driver->iptables,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
NULL); NULL);
masqerr3: masqerr3:
iptablesRemoveForwardAllowRelatedIn(driver->iptables, iptablesRemoveForwardAllowRelatedIn(driver->iptables,
...@@ -1687,28 +1677,22 @@ networkRemoveMasqueradingIptablesRules(struct network_driver *driver, ...@@ -1687,28 +1677,22 @@ networkRemoveMasqueradingIptablesRules(struct network_driver *driver,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
"tcp"); "tcp");
iptablesRemoveForwardMasquerade(driver->iptables, iptablesRemoveForwardMasquerade(driver->iptables,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
"udp"); "udp");
iptablesRemoveForwardMasquerade(driver->iptables, iptablesRemoveForwardMasquerade(driver->iptables,
&ipdef->address, &ipdef->address,
prefix, prefix,
forwardIf, forwardIf,
&network->def->forward.addrStart, &network->def->forward.addr,
&network->def->forward.addrEnd, &network->def->forward.port,
network->def->forward.portStart,
network->def->forward.portEnd,
NULL); NULL);
iptablesRemoveForwardAllowRelatedIn(driver->iptables, iptablesRemoveForwardAllowRelatedIn(driver->iptables,
......
...@@ -805,10 +805,8 @@ iptablesForwardMasquerade(iptablesContext *ctx, ...@@ -805,10 +805,8 @@ iptablesForwardMasquerade(iptablesContext *ctx,
virSocketAddr *netaddr, virSocketAddr *netaddr,
unsigned int prefix, unsigned int prefix,
const char *physdev, const char *physdev,
virSocketAddr *addrStart, virSocketAddrRangePtr addr,
virSocketAddr *addrEnd, virPortRangePtr port,
unsigned int portStart,
unsigned int portEnd,
const char *protocol, const char *protocol,
int action) int action)
{ {
...@@ -831,11 +829,11 @@ iptablesForwardMasquerade(iptablesContext *ctx, ...@@ -831,11 +829,11 @@ iptablesForwardMasquerade(iptablesContext *ctx,
goto cleanup; goto cleanup;
} }
if (VIR_SOCKET_ADDR_IS_FAMILY(addrStart, AF_INET)) { if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
if (!(addrStartStr = virSocketAddrFormat(addrStart))) if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
goto cleanup; goto cleanup;
if (VIR_SOCKET_ADDR_IS_FAMILY(addrEnd, AF_INET)) { if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
if (!(addrEndStr = virSocketAddrFormat(addrEnd))) if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
goto cleanup; goto cleanup;
} }
} }
...@@ -852,20 +850,21 @@ iptablesForwardMasquerade(iptablesContext *ctx, ...@@ -852,20 +850,21 @@ iptablesForwardMasquerade(iptablesContext *ctx,
virCommandAddArgList(cmd, "--out-interface", physdev, NULL); virCommandAddArgList(cmd, "--out-interface", physdev, NULL);
if (protocol && protocol[0]) { if (protocol && protocol[0]) {
if (portStart == 0 && portEnd == 0) { if (port->start == 0 && port->end == 0) {
portStart = 1024; port->start = 1024;
portEnd = 65535; port->end = 65535;
} }
if (portStart < portEnd && portEnd < 65536) { if (port->start < port->end && port->end < 65536) {
if (virAsprintf(&portRangeStr, ":%u-%u", portStart, portEnd) < 0) { if (virAsprintf(&portRangeStr, ":%u-%u",
port->start, port->end) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
} else { } else {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid port range '%u-%u'."), _("Invalid port range '%u-%u'."),
portStart, portEnd); port->start, port->end);
} }
} }
...@@ -924,15 +923,11 @@ iptablesAddForwardMasquerade(iptablesContext *ctx, ...@@ -924,15 +923,11 @@ iptablesAddForwardMasquerade(iptablesContext *ctx,
virSocketAddr *netaddr, virSocketAddr *netaddr,
unsigned int prefix, unsigned int prefix,
const char *physdev, const char *physdev,
virSocketAddr *addrStart, virSocketAddrRangePtr addr,
virSocketAddr *addrEnd, virPortRangePtr port,
unsigned int portStart,
unsigned int portEnd,
const char *protocol) const char *protocol)
{ {
return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, addr, port,
addrStart, addrEnd,
portStart, portEnd,
protocol, ADD); protocol, ADD);
} }
...@@ -954,15 +949,11 @@ iptablesRemoveForwardMasquerade(iptablesContext *ctx, ...@@ -954,15 +949,11 @@ iptablesRemoveForwardMasquerade(iptablesContext *ctx,
virSocketAddr *netaddr, virSocketAddr *netaddr,
unsigned int prefix, unsigned int prefix,
const char *physdev, const char *physdev,
virSocketAddr *addrStart, virSocketAddrRangePtr addr,
virSocketAddr *addrEnd, virPortRangePtr port,
unsigned int portStart,
unsigned int portEnd,
const char *protocol) const char *protocol)
{ {
return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, addr, port,
addrStart, addrEnd,
portStart, portEnd,
protocol, REMOVE); protocol, REMOVE);
} }
......
...@@ -107,19 +107,15 @@ int iptablesAddForwardMasquerade (iptablesContext *ctx, ...@@ -107,19 +107,15 @@ int iptablesAddForwardMasquerade (iptablesContext *ctx,
virSocketAddr *netaddr, virSocketAddr *netaddr,
unsigned int prefix, unsigned int prefix,
const char *physdev, const char *physdev,
virSocketAddr *addrStart, virSocketAddrRangePtr addr,
virSocketAddr *addrEnd, virPortRangePtr port,
unsigned int portStart,
unsigned int portEnd,
const char *protocol); const char *protocol);
int iptablesRemoveForwardMasquerade (iptablesContext *ctx, int iptablesRemoveForwardMasquerade (iptablesContext *ctx,
virSocketAddr *netaddr, virSocketAddr *netaddr,
unsigned int prefix, unsigned int prefix,
const char *physdev, const char *physdev,
virSocketAddr *addrStart, virSocketAddrRangePtr addr,
virSocketAddr *addrEnd, virPortRangePtr port,
unsigned int portStart,
unsigned int portEnd,
const char *protocol); const char *protocol);
int iptablesAddOutputFixUdpChecksum (iptablesContext *ctx, int iptablesAddOutputFixUdpChecksum (iptablesContext *ctx,
const char *iface, const char *iface,
......
...@@ -56,6 +56,20 @@ typedef struct { ...@@ -56,6 +56,20 @@ typedef struct {
typedef virSocketAddr *virSocketAddrPtr; 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, int virSocketAddrParse(virSocketAddrPtr addr,
const char *val, const char *val,
int family); int family);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册