This patch modifies the NWFilter parameter parser to support multiple elements with the same name and to internally build a list of items. An example of the XML looks like this: <parameter name='TEST' value='10.1.2.3'/> <parameter name='TEST' value='10.2.3.4'/> <parameter name='TEST' value='10.1.1.1'/> The list of values is then stored in the newly introduced data type virNWFilterVarValue. The XML formatter is also adapted to print out all items in alphabetical order sorted by 'name'. This patch also fixes a bug in the XML schema on the way. v5: - Addressing Eric Blake's comments v4: - schema: removed the <optional> node in favor of the <zeroOrMore> node v3: - Follow Daniel Berrange's suggestion of parsing a list as shown above - Rewrote XML formatter to print out parameters in alphabetical order v2: - check that each item in the list only contains allowed characters Signed-off-by: Stefan Berger <stefanb@xxxxxxxxxxxxxxxxxx> --- docs/schemas/nwfilter.rng | 4 +- src/conf/nwfilter_params.c | 75 ++++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 23 deletions(-) Index: libvirt-acl/src/conf/nwfilter_params.c =================================================================== --- libvirt-acl.orig/src/conf/nwfilter_params.c +++ libvirt-acl/src/conf/nwfilter_params.c @@ -604,7 +604,7 @@ isValidVarName(const char *var) static bool isValidVarValue(const char *value) { - return value[strspn(value, VALID_VARVALUE)] == 0; + return (value[strspn(value, VALID_VARVALUE)] == 0) && (strlen(value) != 0); } static virNWFilterVarValuePtr @@ -636,15 +636,22 @@ virNWFilterParseParamAttributes(xmlNodeP if (nam != NULL && val != NULL) { if (!isValidVarName(nam)) goto skip_entry; - value = virNWFilterParseVarValue(val); - if (!value) + if (!isValidVarValue(val)) goto skip_entry; - if (virNWFilterHashTablePut(table, nam, value, 1)) { - VIR_FREE(nam); - VIR_FREE(val); - virNWFilterVarValueFree(value); - virNWFilterHashTableFree(table); - return NULL; + value = virHashLookup(table->hashTable, nam); + if (value) { + /* add value to existing value -> list */ + if (!virNWFilterVarValueAddValue(value, val)) { + value = NULL; + goto err_exit; + } + val = NULL; + } else { + value = virNWFilterParseVarValue(val); + if (!value) + goto skip_entry; + if (virNWFilterHashTablePut(table, nam, value, 1)) + goto err_exit; } value = NULL; } @@ -657,39 +664,65 @@ skip_entry: cur = cur->next; } return table; + +err_exit: + VIR_FREE(nam); + VIR_FREE(val); + virNWFilterVarValueFree(value); + virNWFilterHashTableFree(table); + return NULL; } -static void -_formatParameterAttrs(void *payload, const void *name, void *data) +static int +virNWFilterFormatParameterNameSorter(const virHashKeyValuePairPtr a, + const virHashKeyValuePairPtr b) { - virBufferPtr buf = data; - - virBufferAsprintf(buf, " <parameter name='%s' value='%s'/>\n", - (const char *)name, - (char *)payload); + return strcmp((const char *)a->key, (const char *)b->key); } - int virNWFilterFormatParamAttributes(virBufferPtr buf, virNWFilterHashTablePtr table, const char *filterref) { - int count = virHashSize(table->hashTable); + char **keys; + int i, j, card, numKeys; + virNWFilterVarValuePtr value; + + numKeys = virHashSize(table->hashTable); - if (count < 0) { + if (numKeys < 0) { virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing filter parameter table")); return -1; } + + keys = (char **)virHashGetKeys(table->hashTable, + virNWFilterFormatParameterNameSorter); + if (!keys) + return -1; + virBufferAsprintf(buf, "<filterref filter='%s'", filterref); - if (count) { + if (numKeys) { virBufferAddLit(buf, ">\n"); - virHashForEach(table->hashTable, _formatParameterAttrs, buf); + for (i = 0; i < numKeys; i++) { + value = virHashLookup(table->hashTable, keys[i]); + card = virNWFilterVarValueGetCardinality(value); + + for (j = 0; j < card; j++) + virBufferAsprintf(buf, + " <parameter name='%s' value='%s'/>\n", + keys[i], + virNWFilterVarValueGetNthValue(value, j)); + + } virBufferAddLit(buf, "</filterref>\n"); } else { virBufferAddLit(buf, "/>\n"); } + + virHashFreeKeys(table->hashTable, (void **)keys); + return 0; } Index: libvirt-acl/docs/schemas/nwfilter.rng =================================================================== --- libvirt-acl.orig/docs/schemas/nwfilter.rng +++ libvirt-acl/docs/schemas/nwfilter.rng @@ -312,7 +312,7 @@ <attribute name="filter"> <data type="NCName"/> </attribute> - <optional> + <zeroOrMore> <element name="parameter"> <attribute name="name"> <ref name="filter-param-name"/> @@ -321,7 +321,7 @@ <ref name="filter-param-value"/> </attribute> </element> - </optional> + </zeroOrMore> </define> <define name="rule-node-attributes"> -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list