Commit 0c22ff0d authored by Florent Fourcot's avatar Florent Fourcot

ipset: support header command

Header command help to get a fast response to test if the given ipset
exists, and to read basic attribute of the given ipset (like the
ATTR_REVISION, to check if a feature is supported on it)
parent a69afaff
......@@ -29,6 +29,7 @@ from pyroute2.netlink.nfnetlink.ipset import IPSET_CMD_FLUSH
from pyroute2.netlink.nfnetlink.ipset import IPSET_CMD_RENAME
from pyroute2.netlink.nfnetlink.ipset import IPSET_CMD_TEST
from pyroute2.netlink.nfnetlink.ipset import IPSET_CMD_TYPE
from pyroute2.netlink.nfnetlink.ipset import IPSET_CMD_HEADER
from pyroute2.netlink.nfnetlink.ipset import ipset_msg
from pyroute2.netlink.nfnetlink.ipset import IPSET_FLAG_WITH_COUNTERS
from pyroute2.netlink.nfnetlink.ipset import IPSET_FLAG_WITH_COMMENT
......@@ -49,7 +50,8 @@ class IPSet(NetlinkSocket):
policy = {IPSET_CMD_PROTOCOL: ipset_msg,
IPSET_CMD_LIST: ipset_msg,
IPSET_CMD_TYPE: ipset_msg}
IPSET_CMD_TYPE: ipset_msg,
IPSET_CMD_HEADER: ipset_msg}
def __init__(self, version=6, attr_revision=None, nfgen_family=2):
super(IPSet, self).__init__(family=NETLINK_NETFILTER)
......@@ -71,7 +73,13 @@ class IPSet(NetlinkSocket):
except NetlinkError as err:
raise IPSetError(err.code)
def list(self, name=None):
def headers(self, name):
'''
Get headers of the named ipset.
'''
return self._list_or_headers(IPSET_CMD_HEADER, name=name)
def list(self, **kwargs):
'''
List installed ipsets. If `name` is provided, list
the named ipset or return an empty list.
......@@ -79,11 +87,14 @@ class IPSet(NetlinkSocket):
It looks like nfnetlink doesn't return an error,
when requested ipset doesn't exist.
'''
return self._list_or_headers(IPSET_CMD_LIST, **kwargs)
def _list_or_headers(self, cmd, name=None):
msg = ipset_msg()
msg['attrs'] = [['IPSET_ATTR_PROTOCOL', self._proto_version]]
if name is not None:
msg['attrs'].append(['IPSET_ATTR_SETNAME', name])
return self.request(msg, IPSET_CMD_LIST)
return self.request(msg, cmd)
def destroy(self, name):
'''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment