提交 ec3d03db 编写于 作者: S Stefan Berger

nwfilter: Extend XML parser and generator w/ comment attribute

The patch below extends the XML parser and generator so that every protocol
now can have a comment node. Comments are limited to 256 characters.
上级 f8db6c90
...@@ -246,7 +246,11 @@ virNWFilterRuleDefFree(virNWFilterRuleDefPtr def) { ...@@ -246,7 +246,11 @@ virNWFilterRuleDefFree(virNWFilterRuleDefPtr def) {
for (i = 0; i < def->nvars; i++) for (i = 0; i < def->nvars; i++)
VIR_FREE(def->vars[i]); VIR_FREE(def->vars[i]);
for (i = 0; i < def->nstrings; i++)
VIR_FREE(def->strings[i]);
VIR_FREE(def->vars); VIR_FREE(def->vars);
VIR_FREE(def->strings);
VIR_FREE(def); VIR_FREE(def);
} }
...@@ -350,6 +354,29 @@ virNWFilterRuleDefAddVar(virNWFilterRuleDefPtr nwf, ...@@ -350,6 +354,29 @@ virNWFilterRuleDefAddVar(virNWFilterRuleDefPtr nwf,
} }
static char *
virNWFilterRuleDefAddString(virNWFilterRuleDefPtr nwf,
const char *string,
size_t maxstrlen)
{
if (VIR_REALLOC_N(nwf->strings, nwf->nstrings+1) < 0) {
virReportOOMError();
return NULL;
}
nwf->strings[nwf->nstrings] = strndup(string, maxstrlen);
if (!nwf->strings[nwf->nstrings]) {
virReportOOMError();
return NULL;
}
nwf->nstrings++;
return nwf->strings[nwf->nstrings-1];
}
void void
virNWFilterPoolObjRemove(virNWFilterPoolObjListPtr pools, virNWFilterPoolObjRemove(virNWFilterPoolObjListPtr pools,
virNWFilterPoolObjPtr pool) virNWFilterPoolObjPtr pool)
...@@ -399,6 +426,7 @@ struct _virXMLAttr2Struct ...@@ -399,6 +426,7 @@ struct _virXMLAttr2Struct
int dataIdx; // offset of the hasXYZ boolean int dataIdx; // offset of the hasXYZ boolean
valueValidator validator; // beyond-standard checkers valueValidator validator; // beyond-standard checkers
valueFormatter formatter; // beyond-standard formatter valueFormatter formatter; // beyond-standard formatter
size_t maxstrlen;
}; };
...@@ -655,6 +683,18 @@ dscpValidator(enum attrDatatype datatype, union data *val, ...@@ -655,6 +683,18 @@ dscpValidator(enum attrDatatype datatype, union data *val,
} }
#define COMMENT_PROP(STRUCT) \
{\
.name = "comment",\
.datatype = DATATYPE_STRINGCOPY,\
.dataIdx = offsetof(virNWFilterRuleDef, p.STRUCT.dataComment),\
.maxstrlen = MAX_COMMENT_LENGTH,\
}
#define COMMENT_PROP_IPHDR(STRUCT) \
COMMENT_PROP(STRUCT.ipHdr)
static const virXMLAttr2Struct macAttributes[] = { static const virXMLAttr2Struct macAttributes[] = {
COMMON_MAC_PROPS(ethHdrFilter), COMMON_MAC_PROPS(ethHdrFilter),
{ {
...@@ -664,6 +704,7 @@ static const virXMLAttr2Struct macAttributes[] = { ...@@ -664,6 +704,7 @@ static const virXMLAttr2Struct macAttributes[] = {
.validator= checkMacProtocolID, .validator= checkMacProtocolID,
.formatter= macProtocolIDFormatter, .formatter= macProtocolIDFormatter,
}, },
COMMENT_PROP(ethHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -702,6 +743,7 @@ static const virXMLAttr2Struct arpAttributes[] = { ...@@ -702,6 +743,7 @@ static const virXMLAttr2Struct arpAttributes[] = {
.datatype = DATATYPE_IPADDR, .datatype = DATATYPE_IPADDR,
.dataIdx = offsetof(virNWFilterRuleDef, p.arpHdrFilter.dataARPDstIPAddr), .dataIdx = offsetof(virNWFilterRuleDef, p.arpHdrFilter.dataARPDstIPAddr),
}, },
COMMENT_PROP(arpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -762,6 +804,7 @@ static const virXMLAttr2Struct ipAttributes[] = { ...@@ -762,6 +804,7 @@ static const virXMLAttr2Struct ipAttributes[] = {
.dataIdx = offsetof(virNWFilterRuleDef, p.ipHdrFilter.ipHdr.dataDSCP), .dataIdx = offsetof(virNWFilterRuleDef, p.ipHdrFilter.ipHdr.dataDSCP),
.validator = dscpValidator, .validator = dscpValidator,
}, },
COMMENT_PROP_IPHDR(ipHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -817,6 +860,7 @@ static const virXMLAttr2Struct ipv6Attributes[] = { ...@@ -817,6 +860,7 @@ static const virXMLAttr2Struct ipv6Attributes[] = {
.datatype = DATATYPE_UINT16 | DATATYPE_UINT16_HEX, .datatype = DATATYPE_UINT16 | DATATYPE_UINT16_HEX,
.dataIdx = offsetof(virNWFilterRuleDef, p.ipv6HdrFilter.portData.dataDstPortEnd), .dataIdx = offsetof(virNWFilterRuleDef, p.ipv6HdrFilter.portData.dataDstPortEnd),
}, },
COMMENT_PROP_IPHDR(ipv6HdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -914,6 +958,7 @@ static const virXMLAttr2Struct tcpAttributes[] = { ...@@ -914,6 +958,7 @@ static const virXMLAttr2Struct tcpAttributes[] = {
.datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX, .datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX,
.dataIdx = offsetof(virNWFilterRuleDef, p.tcpHdrFilter.dataTCPOption), .dataIdx = offsetof(virNWFilterRuleDef, p.tcpHdrFilter.dataTCPOption),
}, },
COMMENT_PROP_IPHDR(tcpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -922,6 +967,7 @@ static const virXMLAttr2Struct tcpAttributes[] = { ...@@ -922,6 +967,7 @@ static const virXMLAttr2Struct tcpAttributes[] = {
static const virXMLAttr2Struct udpAttributes[] = { static const virXMLAttr2Struct udpAttributes[] = {
COMMON_IP_PROPS(udpHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK), COMMON_IP_PROPS(udpHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK),
COMMON_PORT_PROPS(udpHdrFilter), COMMON_PORT_PROPS(udpHdrFilter),
COMMENT_PROP_IPHDR(udpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -929,6 +975,7 @@ static const virXMLAttr2Struct udpAttributes[] = { ...@@ -929,6 +975,7 @@ static const virXMLAttr2Struct udpAttributes[] = {
static const virXMLAttr2Struct udpliteAttributes[] = { static const virXMLAttr2Struct udpliteAttributes[] = {
COMMON_IP_PROPS(udpliteHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK), COMMON_IP_PROPS(udpliteHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK),
COMMENT_PROP_IPHDR(udpliteHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -936,6 +983,7 @@ static const virXMLAttr2Struct udpliteAttributes[] = { ...@@ -936,6 +983,7 @@ static const virXMLAttr2Struct udpliteAttributes[] = {
static const virXMLAttr2Struct espAttributes[] = { static const virXMLAttr2Struct espAttributes[] = {
COMMON_IP_PROPS(espHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK), COMMON_IP_PROPS(espHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK),
COMMENT_PROP_IPHDR(espHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -943,6 +991,7 @@ static const virXMLAttr2Struct espAttributes[] = { ...@@ -943,6 +991,7 @@ static const virXMLAttr2Struct espAttributes[] = {
static const virXMLAttr2Struct ahAttributes[] = { static const virXMLAttr2Struct ahAttributes[] = {
COMMON_IP_PROPS(ahHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK), COMMON_IP_PROPS(ahHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK),
COMMENT_PROP_IPHDR(ahHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -951,6 +1000,7 @@ static const virXMLAttr2Struct ahAttributes[] = { ...@@ -951,6 +1000,7 @@ static const virXMLAttr2Struct ahAttributes[] = {
static const virXMLAttr2Struct sctpAttributes[] = { static const virXMLAttr2Struct sctpAttributes[] = {
COMMON_IP_PROPS(sctpHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK), COMMON_IP_PROPS(sctpHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK),
COMMON_PORT_PROPS(sctpHdrFilter), COMMON_PORT_PROPS(sctpHdrFilter),
COMMENT_PROP_IPHDR(sctpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -969,6 +1019,7 @@ static const virXMLAttr2Struct icmpAttributes[] = { ...@@ -969,6 +1019,7 @@ static const virXMLAttr2Struct icmpAttributes[] = {
.datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX, .datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX,
.dataIdx = offsetof(virNWFilterRuleDef, p.icmpHdrFilter.dataICMPCode), .dataIdx = offsetof(virNWFilterRuleDef, p.icmpHdrFilter.dataICMPCode),
}, },
COMMENT_PROP_IPHDR(icmpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -977,6 +1028,7 @@ static const virXMLAttr2Struct icmpAttributes[] = { ...@@ -977,6 +1028,7 @@ static const virXMLAttr2Struct icmpAttributes[] = {
static const virXMLAttr2Struct allAttributes[] = { static const virXMLAttr2Struct allAttributes[] = {
COMMON_IP_PROPS(allHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK), COMMON_IP_PROPS(allHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK),
COMMENT_PROP_IPHDR(allHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -985,6 +1037,7 @@ static const virXMLAttr2Struct allAttributes[] = { ...@@ -985,6 +1037,7 @@ static const virXMLAttr2Struct allAttributes[] = {
static const virXMLAttr2Struct igmpAttributes[] = { static const virXMLAttr2Struct igmpAttributes[] = {
COMMON_IP_PROPS(igmpHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK), COMMON_IP_PROPS(igmpHdrFilter, DATATYPE_IPADDR, DATATYPE_IPMASK),
COMMENT_PROP_IPHDR(igmpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -999,6 +1052,7 @@ static const virXMLAttr2Struct tcpipv6Attributes[] = { ...@@ -999,6 +1052,7 @@ static const virXMLAttr2Struct tcpipv6Attributes[] = {
.datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX, .datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX,
.dataIdx = offsetof(virNWFilterRuleDef, p.tcpHdrFilter.dataTCPOption), .dataIdx = offsetof(virNWFilterRuleDef, p.tcpHdrFilter.dataTCPOption),
}, },
COMMENT_PROP_IPHDR(tcpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1007,6 +1061,7 @@ static const virXMLAttr2Struct tcpipv6Attributes[] = { ...@@ -1007,6 +1061,7 @@ static const virXMLAttr2Struct tcpipv6Attributes[] = {
static const virXMLAttr2Struct udpipv6Attributes[] = { static const virXMLAttr2Struct udpipv6Attributes[] = {
COMMON_IP_PROPS(udpHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK), COMMON_IP_PROPS(udpHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK),
COMMON_PORT_PROPS(udpHdrFilter), COMMON_PORT_PROPS(udpHdrFilter),
COMMENT_PROP_IPHDR(udpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1015,6 +1070,7 @@ static const virXMLAttr2Struct udpipv6Attributes[] = { ...@@ -1015,6 +1070,7 @@ static const virXMLAttr2Struct udpipv6Attributes[] = {
static const virXMLAttr2Struct udpliteipv6Attributes[] = { static const virXMLAttr2Struct udpliteipv6Attributes[] = {
COMMON_IP_PROPS(udpliteHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK), COMMON_IP_PROPS(udpliteHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK),
COMMENT_PROP_IPHDR(udpliteHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1023,6 +1079,7 @@ static const virXMLAttr2Struct udpliteipv6Attributes[] = { ...@@ -1023,6 +1079,7 @@ static const virXMLAttr2Struct udpliteipv6Attributes[] = {
static const virXMLAttr2Struct espipv6Attributes[] = { static const virXMLAttr2Struct espipv6Attributes[] = {
COMMON_IP_PROPS(espHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK), COMMON_IP_PROPS(espHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK),
COMMENT_PROP_IPHDR(espHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1031,6 +1088,7 @@ static const virXMLAttr2Struct espipv6Attributes[] = { ...@@ -1031,6 +1088,7 @@ static const virXMLAttr2Struct espipv6Attributes[] = {
static const virXMLAttr2Struct ahipv6Attributes[] = { static const virXMLAttr2Struct ahipv6Attributes[] = {
COMMON_IP_PROPS(ahHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK), COMMON_IP_PROPS(ahHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK),
COMMENT_PROP_IPHDR(ahHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1040,6 +1098,7 @@ static const virXMLAttr2Struct ahipv6Attributes[] = { ...@@ -1040,6 +1098,7 @@ static const virXMLAttr2Struct ahipv6Attributes[] = {
static const virXMLAttr2Struct sctpipv6Attributes[] = { static const virXMLAttr2Struct sctpipv6Attributes[] = {
COMMON_IP_PROPS(sctpHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK), COMMON_IP_PROPS(sctpHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK),
COMMON_PORT_PROPS(sctpHdrFilter), COMMON_PORT_PROPS(sctpHdrFilter),
COMMENT_PROP_IPHDR(sctpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1058,6 +1117,7 @@ static const virXMLAttr2Struct icmpv6Attributes[] = { ...@@ -1058,6 +1117,7 @@ static const virXMLAttr2Struct icmpv6Attributes[] = {
.datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX, .datatype = DATATYPE_UINT8 | DATATYPE_UINT8_HEX,
.dataIdx = offsetof(virNWFilterRuleDef, p.icmpHdrFilter.dataICMPCode), .dataIdx = offsetof(virNWFilterRuleDef, p.icmpHdrFilter.dataICMPCode),
}, },
COMMENT_PROP_IPHDR(icmpHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1066,6 +1126,7 @@ static const virXMLAttr2Struct icmpv6Attributes[] = { ...@@ -1066,6 +1126,7 @@ static const virXMLAttr2Struct icmpv6Attributes[] = {
static const virXMLAttr2Struct allipv6Attributes[] = { static const virXMLAttr2Struct allipv6Attributes[] = {
COMMON_IP_PROPS(allHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK), COMMON_IP_PROPS(allHdrFilter, DATATYPE_IPV6ADDR, DATATYPE_IPV6MASK),
COMMENT_PROP_IPHDR(allHdrFilter),
{ {
.name = NULL, .name = NULL,
} }
...@@ -1332,6 +1393,17 @@ virNWFilterRuleDetailsParse(xmlNodePtr node, ...@@ -1332,6 +1393,17 @@ virNWFilterRuleDetailsParse(xmlNodePtr node,
found = 1; found = 1;
break; break;
case DATATYPE_STRINGCOPY:
if (!(item->u.string =
virNWFilterRuleDefAddString(nwf, prop,
att[idx].maxstrlen))) {
rc = -1;
break;
}
data.c = item->u.string;
found = 1;
break;
case DATATYPE_LAST: case DATATYPE_LAST:
default: default:
break; break;
...@@ -2510,6 +2582,10 @@ virNWFilterRuleDefDetailsFormat(virBufferPtr buf, ...@@ -2510,6 +2582,10 @@ virNWFilterRuleDefDetailsFormat(virBufferPtr buf,
(j < 5) ? ":" : ""); (j < 5) ? ":" : "");
break; break;
case DATATYPE_STRINGCOPY:
virBufferEscapeString(buf, "%s", item->u.string);
break;
case DATATYPE_STRING: case DATATYPE_STRING:
default: default:
virBufferVSprintf(buf, virBufferVSprintf(buf,
......
...@@ -73,6 +73,8 @@ enum virNWFilterEntryItemFlags { ...@@ -73,6 +73,8 @@ enum virNWFilterEntryItemFlags {
}; };
# define MAX_COMMENT_LENGTH 256
# define HAS_ENTRY_ITEM(data) \ # define HAS_ENTRY_ITEM(data) \
(((data)->flags) & NWFILTER_ENTRY_ITEM_FLAG_EXISTS) (((data)->flags) & NWFILTER_ENTRY_ITEM_FLAG_EXISTS)
...@@ -92,8 +94,9 @@ enum attrDatatype { ...@@ -92,8 +94,9 @@ enum attrDatatype {
DATATYPE_STRING = (1 << 8), DATATYPE_STRING = (1 << 8),
DATATYPE_IPV6ADDR = (1 << 9), DATATYPE_IPV6ADDR = (1 << 9),
DATATYPE_IPV6MASK = (1 << 10), DATATYPE_IPV6MASK = (1 << 10),
DATATYPE_STRINGCOPY = (1 << 11),
DATATYPE_LAST = (1 << 11), DATATYPE_LAST = (1 << 12),
}; };
...@@ -123,6 +126,7 @@ struct _nwItemDesc { ...@@ -123,6 +126,7 @@ struct _nwItemDesc {
uint8_t u8; uint8_t u8;
uint16_t u16; uint16_t u16;
char protocolID[10]; char protocolID[10];
char *string;
} u; } u;
}; };
...@@ -142,6 +146,7 @@ typedef ethHdrFilterDef *ethHdrFilterDefPtr; ...@@ -142,6 +146,7 @@ typedef ethHdrFilterDef *ethHdrFilterDefPtr;
struct _ethHdrFilterDef { struct _ethHdrFilterDef {
ethHdrDataDef ethHdr; ethHdrDataDef ethHdr;
nwItemDesc dataProtocolID; nwItemDesc dataProtocolID;
nwItemDesc dataComment;
}; };
...@@ -156,6 +161,7 @@ struct _arpHdrFilterDef { ...@@ -156,6 +161,7 @@ struct _arpHdrFilterDef {
nwItemDesc dataARPSrcIPAddr; nwItemDesc dataARPSrcIPAddr;
nwItemDesc dataARPDstMACAddr; nwItemDesc dataARPDstMACAddr;
nwItemDesc dataARPDstIPAddr; nwItemDesc dataARPDstIPAddr;
nwItemDesc dataComment;
}; };
...@@ -174,6 +180,7 @@ struct _ipHdrDataDef { ...@@ -174,6 +180,7 @@ struct _ipHdrDataDef {
nwItemDesc dataDstIPTo; nwItemDesc dataDstIPTo;
nwItemDesc dataDSCP; nwItemDesc dataDSCP;
nwItemDesc dataConnlimitAbove; nwItemDesc dataConnlimitAbove;
nwItemDesc dataComment;
}; };
...@@ -376,6 +383,9 @@ struct _virNWFilterRuleDef { ...@@ -376,6 +383,9 @@ struct _virNWFilterRuleDef {
int nvars; int nvars;
char **vars; char **vars;
int nstrings;
char **strings;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册