From 2595a2ae2e62ca29569aeb18f7f7857659cd78b5 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Tue, 22 Apr 2014 17:44:22 +0900 Subject: Improve iproute attribute decoding documentation. This reverts commit a54a9b1046a738d645c2aa1add5c26a348a517b8. Change-Id: I9d6566b4ed1f3723523745692bac0ed61e2775aa --- tests/net_test/iproute.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/net_test/iproute.py b/tests/net_test/iproute.py index ca4c9b7f..f3560c6e 100644 --- a/tests/net_test/iproute.py +++ b/tests/net_test/iproute.py @@ -126,6 +126,25 @@ def CommandSubject(command): def Decode(command, family, nla_type, nla_data): + """Decodes netlink attributes to Python types. + + Values for which the code knows the type (e.g., the fwmark ID in a + RTM_NEWRULE command) are decoded to Python integers, strings, etc. Values + of unknown type are returned as raw byte strings. + + Args: + command: An integer, the rtnetlink command being carried out. This is used + to interpret the attributes. For example, for an RTM_NEWROUTE command, + attribute type 3 is the incoming interface and is an integer, but for a + RTM_NEWRULE command, attribute type 3 is the incoming interface name + and is a string. + family: The address family. Used to convert IP addresses into strings. + nla_type: An integer, then netlink attribute type. + nla_data: A byte string, the netlink attribute data. + + Returns: + A Python object. (Currently an integer, a string, or a raw byte string.) + """ if CommandSubject(command) == "RULE": if nla_type in [FRA_PRIORITY, FRA_FWMARK, FRA_TABLE, EXPERIMENTAL_FRA_UID_START, EXPERIMENTAL_FRA_UID_END]: @@ -172,20 +191,15 @@ class IPRoute(object): return self._NlAttr(nla_type, socket.inet_pton(family, address)) def _ParseAttributes(self, command, family, data): - """Parses netlink attributes. + """Parses and decodes netlink attributes. - Values for which the code knows the type (e.g., the fwmark ID in a - RTM_NEWRULE command) are decoded to Python integers, strings, etc. Values - of unknown type are returned as raw byte strings. + Takes a block of NLAttr data structures, decodes them using Decode, and + returns the result in a dict keyed by attribute number. Args: - command: An integer, the rtnetlink command being carried out. This is used - to interpret the attributes. For example, for an RTM_NEWROUTE command, - attribute type 3 is the incoming interface and is an integer, but for a - RTM_NEWRULE command, attribute type 3 is the incoming interface name - and is a string. - family: The address family. Used to convert IP addresses into strings. - data: A byte string containing a sequence of NLAttrs data structures. + command: An integer, the rtnetlink command being carried out. + family: The address family. + data: A byte string containing a sequence of NLAttr data structures. Returns: A dictionary mapping attribute types (integers) to decoded values. -- cgit v1.2.3