1# $Id: stp.py 23 2006-11-08 15:45:33Z dugsong $
2# -*- coding: utf-8 -*-
3"""Spanning Tree Protocol."""
4from __future__ import print_function
5from __future__ import absolute_import
6
7from . import dpkt
8
9
10class STP(dpkt.Packet):
11    """Spanning Tree Protocol.
12
13    TODO: Longer class information....
14
15    Attributes:
16        __hdr__: Header fields of STP.
17        TODO.
18    """
19
20    __hdr__ = (
21        ('proto_id', 'H', 0),
22        ('v', 'B', 0),
23        ('type', 'B', 0),
24        ('flags', 'B', 0),
25        ('root_id', '8s', b''),
26        ('root_path', 'I', 0),
27        ('bridge_id', '8s', b''),
28        ('port_id', 'H', 0),
29        ('_age', 'H', 0),
30        ('_max_age', 'H', 0),
31        ('_hello', 'H', 0),
32        ('_fd', 'H', 0)
33    )
34
35    @property
36    def age(self):
37        return self._age >> 8
38
39    @age.setter
40    def age(self, age):
41        self._age = age << 8
42
43    @property
44    def max_age(self):
45        return self._max_age >> 8
46
47    @max_age.setter
48    def max_age(self, max_age):
49        self._max_age = max_age << 8
50
51    @property
52    def hello(self):
53        return self._hello >> 8
54
55    @hello.setter
56    def hello(self, hello):
57        self._hello = hello << 8
58
59    @property
60    def fd(self):
61        return self._fd >> 8
62
63    @fd.setter
64    def fd(self, fd):
65        self._fd = fd << 8
66
67
68def test_stp():
69    buf = (b'\x00\x00\x02\x02\x3e\x80\x00\x08\x00\x27\xad\xa3\x41\x00\x00\x00\x00\x80\x00\x08\x00\x27'
70           b'\xad\xa3\x41\x80\x01\x00\x00\x14\x00\x02\x00\x0f\x00\x00\x00\x00\x00\x02\x00\x14\x00')
71    stp = STP(buf)
72
73    assert stp.proto_id == 0
74    assert stp.port_id == 0x8001
75    assert stp.age == 0
76    assert stp.max_age == 20
77    assert stp.hello == 2
78    assert stp.fd == 15
79
80    assert bytes(stp) == buf
81
82    stp.fd = 100
83    assert stp.pack_hdr()[-2:] == b'\x64\x00'  # 100 << 8
84
85
86def test_properties():
87    stp = STP()
88    stp.age = 10
89    assert stp.age == 10
90
91    stp.max_age = 20
92    assert stp.max_age == 20
93
94    stp.hello = 1234
95    assert stp.hello == 1234
96