File: utils.py

package info (click to toggle)
python-dpkt 1.9.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,104 kB
  • sloc: python: 14,911; makefile: 23
file content (79 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Various Utility Functions"""
import socket
import warnings
from .compat import compat_ord
from .dns import DNS


def mac_to_str(address):
    r"""Convert a MAC address to a readable/printable string

       Args:
           address (str): a MAC address in hex form (e.g. '\x01\x02\x03\x04\x05\x06')
       Returns:
           str: Printable/readable MAC address
    """
    return ':'.join('%02x' % compat_ord(b) for b in address)


def inet_to_str(inet):
    """Convert inet object to a string

        Args:
            inet (inet struct): inet network address
        Returns:
            str: Printable/readable IP address
    """
    # First try ipv4 and then ipv6
    try:
        return socket.inet_ntop(socket.AF_INET, inet)
    except ValueError:
        return socket.inet_ntop(socket.AF_INET6, inet)


def make_dict(obj):
    """Create a dictionary out of a non-builtin object"""
    # Recursion base case
    if is_builtin(obj):
        return obj

    output_dict = {}
    for key in dir(obj):
        if not key.startswith('__') and not callable(getattr(obj, key)):
            attr = getattr(obj, key)
            if isinstance(attr, list):
                output_dict[key] = []
                for item in attr:
                    output_dict[key].append(make_dict(item))
            else:
                output_dict[key] = make_dict(attr)

    return output_dict


def is_builtin(obj):
    return obj.__class__.__module__ in ['__builtin__', 'builtins']


def deprecation_warning(*args):
    """print a deprecation warning"""
    warnings.warn(*args, stacklevel=2)


def test_utils():
    """Test the utility methods"""
    from binascii import unhexlify
    from pprint import pprint

    print(mac_to_str(b'\x01\x02\x03\x04\x05\x06'))
    assert mac_to_str(b'\x01\x02\x03\x04\x05\x06') == '01:02:03:04:05:06'
    print(inet_to_str(b'\x91\xfe\xa0\xed'))
    assert inet_to_str(b'\x91\xfe\xa0\xed') == '145.254.160.237'
    ipv6_inet = b' \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4'
    assert inet_to_str(ipv6_inet) == '2001:db8:85a3::8a2e:370:7334'

    # Test the make_dict method with a DNS response packet
    a_resp = unhexlify("059c8180000100010000000106676f6f676c6503636f6d0000010001c00c00010"
                       "0010000012b0004d83ace2e0000290200000000000000")
    my_dns = DNS(a_resp)
    pprint(make_dict(my_dns))