packet lib: improve readability of json

this patch makes some json members of several packet libraries human-readable.

ex) ethernet.ethernet

before applying this patch:

    {'ethernet': {'ethertype': 2048, 'dst': 'ZmY6ZmY6ZmY6ZmY6ZmY6ZmY=', 'src': 'MDA6MDA6MDA6MDA6MDA6MDA='}}

after applying this patch:

    {'ethernet': {'ethertype': 2048, 'dst': u'ff:ff:ff:ff:ff:ff', 'src': u'00:00:00:00:00:00'}}

Signed-off-by: Yuichi Ito <ito.yuichi0@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Yuichi Ito 2013-12-16 17:34:30 +09:00 committed by FUJITA Tomonori
parent 3d26e755ad
commit 31b0f1625b
10 changed files with 87 additions and 0 deletions

View File

@ -54,6 +54,11 @@ class arp(packet_base.PacketBase):
_PACK_STR = '!HHBBH6s4s6s4s'
_MIN_LEN = struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'src_mac', 'src_ip', 'dst_mac', 'dst_ip'
]
}
def __init__(self, hwtype=ARP_HW_TYPE_ETHERNET, proto=ether.ETH_TYPE_IP,
hlen=6, plen=4, opcode=ARP_REQUEST,

View File

@ -151,6 +151,12 @@ class _BinAddrPrefix(_AddrPrefix):
class _IPAddrPrefix(_AddrPrefix):
_TYPE = {
'ascii': [
'addr'
]
}
@staticmethod
def _to_bin(addr):
return addrconv.ipv4.text_to_bin(addr)
@ -526,6 +532,11 @@ class BGPPathAttributeAs4Path(_BGPPathAttributeAsPathCommon):
class BGPPathAttributeNextHop(_PathAttribute):
_VALUE_PACK_STR = '!4s'
_ATTR_FLAGS = BGP_ATTR_FLAG_TRANSITIVE
_TYPE = {
'ascii': [
'value'
]
}
@classmethod
def parse_value(cls, buf):
@ -566,6 +577,11 @@ class BGPPathAttributeAtomicAggregate(_PathAttribute):
class _BGPPathAttributeAggregatorCommon(_PathAttribute):
_VALUE_PACK_STR = None
_ATTR_FLAGS = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANSITIVE
_TYPE = {
'ascii': [
'addr'
]
}
def __init__(self, as_number, addr, flags=0, type_=None, length=None):
super(_BGPPathAttributeAggregatorCommon, self).__init__(flags=flags,
@ -741,6 +757,11 @@ class BGPTwoOctetAsSpecificExtendedCommunity(_ExtendedCommunity):
class BGPIPv4AddressSpecificExtendedCommunity(_ExtendedCommunity):
_VALUE_PACK_STR = '!B4sH' # sub type, IPv4 address, local adm
_VALUE_FIELDS = ['subtype', 'ipv4_address', 'local_administrator']
_TYPE = {
'ascii': [
'ipv4_address'
]
}
def __init__(self, type_=_ExtendedCommunity.IPV4_ADDRESS_SPECIFIC,
**kwargs):
@ -990,6 +1011,11 @@ class BGPOpen(BGPMessage):
_PACK_STR = '!BHH4sB'
_MIN_LEN = BGPMessage._HDR_LEN + struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'bgp_identifier'
]
}
def __init__(self, my_as, bgp_identifier, type_=BGP_MSG_OPEN,
opt_param_len=0, opt_param=[],

View File

@ -247,6 +247,11 @@ class ConfigurationBPDUs(bpdu):
BPDU_TYPE = TYPE_CONFIG_BPDU
_PACK_STR = '!BQIQHHHHH'
PACK_LEN = struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'root_mac_address', "bridge_mac_address"
]
}
_BRIDGE_PRIORITY_STEP = 4096
_PORT_PRIORITY_STEP = 16

View File

@ -140,6 +140,11 @@ class dhcp(packet_base.PacketBase):
_DHCP_CHADDR_LEN = 16
_HARDWARE_TYPE_ETHERNET = 1
_class_prefixes = ['options']
_TYPE = {
'ascii': [
'ciaddr', 'yiaddr', 'siaddr', 'giaddr', 'chaddr', 'sname'
]
}
def __init__(self, op, chaddr, options, htype=_HARDWARE_TYPE_ETHERNET,
hlen=0, hops=0, xid=None, secs=0, flags=0,
@ -232,6 +237,11 @@ class options(stringify.StringifyMixin):
_MAGIC_COOKIE = '99.130.83.99'
_OPT_TAG_LEN_BYTE = 2
_class_prefixes = ['option']
_TYPE = {
'ascii': [
'magic_cookie'
]
}
def __init__(self, option_list=None, options_len=0,
magic_cookie=_MAGIC_COOKIE):

View File

@ -39,6 +39,11 @@ class ethernet(packet_base.PacketBase):
_PACK_STR = '!6s6sH'
_MIN_LEN = struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'src', 'dst'
]
}
def __init__(self, dst='ff:ff:ff:ff:ff:ff', src='00:00:00:00:00:00',
ethertype=ether.ETH_TYPE_IP):

View File

@ -172,6 +172,11 @@ class nd_neighbor(stringify.StringifyMixin):
_PACK_STR = '!I16s'
_MIN_LEN = struct.calcsize(_PACK_STR)
_ND_OPTION_TYPES = {}
_TYPE = {
'ascii': [
'dst'
]
}
@staticmethod
def register_nd_option_type(*args):
@ -404,6 +409,11 @@ class nd_option_la(nd_option):
_PACK_STR = '!BB6s'
_MIN_LEN = struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'hw_src'
]
}
@abc.abstractmethod
def __init__(self, length, hw_src, data):
@ -553,6 +563,11 @@ class nd_option_pi(nd_option):
_PACK_STR = '!BBBBIII16s'
_MIN_LEN = struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'prefix'
]
}
@classmethod
def option_type(cls):

View File

@ -89,6 +89,11 @@ class igmp(packet_base.PacketBase):
"""
_PACK_STR = '!BBH4s'
_MIN_LEN = struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'address'
]
}
def __init__(self, msgtype=IGMP_TYPE_QUERY, maxresp=0, csum=0,
address='0.0.0.0'):

View File

@ -70,6 +70,11 @@ class ipv4(packet_base.PacketBase):
_PACK_STR = '!BBHHHBBH4s4s'
_MIN_LEN = struct.calcsize(_PACK_STR)
_TYPE = {
'ascii': [
'src', 'dst'
]
}
def __init__(self, version=4, header_length=5, tos=0,
total_length=0, identification=0, flags=0,

View File

@ -60,6 +60,11 @@ class ipv6(packet_base.PacketBase):
_PACK_STR = '!IHBB16s16s'
_MIN_LEN = struct.calcsize(_PACK_STR)
_IPV6_EXT_HEADER_TYPE = {}
_TYPE = {
'ascii': [
'src', 'dst'
]
}
@staticmethod
def register_header_type(type_):

View File

@ -388,6 +388,12 @@ class lacp(packet_base.PacketBase):
_MIN_LEN = _ALL_PACK_LEN
_TYPE = {
'ascii': [
'actor_system', 'partner_system'
]
}
def __init__(self, version=LACP_VERSION_NUMBER,
actor_system_priority=0,
actor_system='00:00:00:00:00:00',