提交 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,
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);
......@@ -2212,19 +2212,19 @@ virNetworkForwardNatDefFormat(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");
......@@ -2237,10 +2237,10 @@ virNetworkForwardNatDefFormat(virBufferPtr buf,
virBufferAddLit(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);
virBufferAddLit(buf, "/>\n");
}
......@@ -2299,10 +2299,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);
......@@ -3016,7 +3016,7 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
{
int ii, ret = -1;
virNetworkIpDefPtr ipdef = virNetworkIpDefByIndex(def, parentIndex);
virNetworkDHCPRangeDef range;
virSocketAddrRange range;
memset(&range, 0, sizeof(range));
......@@ -3027,7 +3027,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",
......@@ -3036,7 +3036,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 */
......
......@@ -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;
......
......@@ -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,
......
......@@ -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,11 +829,11 @@ iptablesForwardMasquerade(iptablesContext *ctx,
goto cleanup;
}
if (VIR_SOCKET_ADDR_IS_FAMILY(addrStart, AF_INET)) {
if (!(addrStartStr = virSocketAddrFormat(addrStart)))
if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->start, AF_INET)) {
if (!(addrStartStr = virSocketAddrFormat(&addr->start)))
goto cleanup;
if (VIR_SOCKET_ADDR_IS_FAMILY(addrEnd, AF_INET)) {
if (!(addrEndStr = virSocketAddrFormat(addrEnd)))
if (VIR_SOCKET_ADDR_IS_FAMILY(&addr->end, AF_INET)) {
if (!(addrEndStr = virSocketAddrFormat(&addr->end)))
goto cleanup;
}
}
......@@ -852,20 +850,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);
}
}
......@@ -924,15 +923,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);
}
......@@ -954,15 +949,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);
}
......
......@@ -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,
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册