[7a1c039] | 1 | from .value import Value |
---|
[d04a689] | 2 | import sys |
---|
| 3 | import struct |
---|
| 4 | import logging |
---|
[8979f90] | 5 | |
---|
| 6 | # initialize logger |
---|
[7a1c039] | 7 | logger = logging.getLogger(__name__) |
---|
[8979f90] | 8 | |
---|
[fc427ef] | 9 | class Attribute(object): |
---|
[5c5fe6d] | 10 | """In addition to what the RFC reports, an attribute has an |
---|
| 11 | 'attribute tag', which specifies what type of attribute it is. It |
---|
| 12 | is 1 bytes long, and comes before the list of values. |
---|
[8979f90] | 13 | |
---|
| 14 | From RFC 2565: |
---|
| 15 | |
---|
| 16 | Each attribute consists of: |
---|
| 17 | ----------------------------------------------- |
---|
| 18 | | value-tag | 1 byte |
---|
| 19 | ----------------------------------------------- |
---|
| 20 | | name-length (value is u) | 2 bytes |
---|
| 21 | ----------------------------------------------- |
---|
| 22 | | name | u bytes |
---|
| 23 | ----------------------------------------------- |
---|
| 24 | | value-length (value is v) | 2 bytes |
---|
| 25 | ----------------------------------------------- |
---|
| 26 | | value | v bytes |
---|
| 27 | ----------------------------------------------- |
---|
| 28 | |
---|
| 29 | An additional value consists of: |
---|
| 30 | ----------------------------------------------------------- |
---|
| 31 | | value-tag | 1 byte | |
---|
| 32 | ----------------------------------------------- | |
---|
| 33 | | name-length (value is 0x0000) | 2 bytes | |
---|
| 34 | ----------------------------------------------- |-0 or more |
---|
| 35 | | value-length (value is w) | 2 bytes | |
---|
| 36 | ----------------------------------------------- | |
---|
| 37 | | value | w bytes | |
---|
| 38 | ----------------------------------------------------------- |
---|
[5c5fe6d] | 39 | |
---|
[8979f90] | 40 | """ |
---|
| 41 | |
---|
[7a1c039] | 42 | def __init__(self, name=None, values=None): |
---|
[5c5fe6d] | 43 | """Initialize an Attribute. This function can be called in |
---|
| 44 | three different ways: |
---|
[c269bc7] | 45 | |
---|
| 46 | Attribute() -- creates an empty Attribute |
---|
| 47 | |
---|
| 48 | Attribute(name) -- creates an empty Attribute with a name |
---|
| 49 | |
---|
| 50 | Attribute(name, values) -- creates an Attribute |
---|
| 51 | initialized with a name and list of values |
---|
[8979f90] | 52 | |
---|
| 53 | Arguments: |
---|
| 54 | |
---|
| 55 | name -- the name of the attribute |
---|
| 56 | |
---|
[ebf327d] | 57 | values -- a list of Values. May not be empty. |
---|
[5c5fe6d] | 58 | |
---|
[8979f90] | 59 | """ |
---|
| 60 | |
---|
[c269bc7] | 61 | if name is not None: |
---|
| 62 | assert isinstance(name, str), \ |
---|
| 63 | "Attribute name must be a string!" |
---|
[7a1c039] | 64 | if values is None: |
---|
| 65 | values = [] |
---|
[c269bc7] | 66 | for value in values: |
---|
| 67 | assert isinstance(value, Value), \ |
---|
[f6c6897] | 68 | "Value %s must be of type Value" % (value,) |
---|
[c269bc7] | 69 | |
---|
[75c0cab] | 70 | self.name = name |
---|
| 71 | self.values = values |
---|
[c269bc7] | 72 | |
---|
[b828a96] | 73 | def __cmp__(self, other): |
---|
| 74 | eq = self.name == other.name |
---|
| 75 | for v1, v2 in zip(self.values, other.values): |
---|
| 76 | eq = eq and (v1 == v2) |
---|
| 77 | return 0 if eq else 1 |
---|
| 78 | |
---|
[fc427ef] | 79 | @property |
---|
| 80 | def packed_value(self): |
---|
[5c5fe6d] | 81 | """Packs the attribute data into binary data. |
---|
| 82 | |
---|
[8979f90] | 83 | """ |
---|
| 84 | |
---|
[c269bc7] | 85 | assert self.name is not None, \ |
---|
| 86 | "cannot pack unnamed attribute!" |
---|
| 87 | assert len(self.values) > 0, \ |
---|
| 88 | "cannot pack empty attribute!" |
---|
| 89 | |
---|
[8979f90] | 90 | # get the binary data for all the values |
---|
| 91 | values = [] |
---|
| 92 | for v, i in zip(self.values, xrange(len(self.values))): |
---|
| 93 | |
---|
| 94 | # get the name length (0 for everything but the first |
---|
| 95 | # value) |
---|
| 96 | if i == 0: |
---|
| 97 | name_length = len(self.name) |
---|
| 98 | else: |
---|
| 99 | name_length = 0 |
---|
| 100 | |
---|
[cad7502] | 101 | logger.debug("dumping name : %s" % self.name) |
---|
| 102 | logger.debug("dumping name_length : %i" % name_length) |
---|
| 103 | logger.debug("value tag : 0x%x" % v.tag) |
---|
| 104 | |
---|
| 105 | # get the binary value |
---|
[fc427ef] | 106 | value_bin = v.packed_value |
---|
[cad7502] | 107 | # get the value length |
---|
[fc427ef] | 108 | value_length = len(value_bin) |
---|
[8979f90] | 109 | |
---|
[cad7502] | 110 | logger.debug("dumping value : %s" % v.value) |
---|
[8979f90] | 111 | logger.debug("dumping value_length : %i" % value_length) |
---|
| 112 | |
---|
| 113 | # the value tag in binary |
---|
[f6c6897] | 114 | tag_bin = struct.pack('>b', v.tag) |
---|
[8979f90] | 115 | |
---|
| 116 | # the name length in binary |
---|
| 117 | name_length_bin = struct.pack('>h', name_length) |
---|
| 118 | |
---|
| 119 | # the name in binary |
---|
| 120 | name_bin = self.name |
---|
| 121 | |
---|
| 122 | # the value length in binary |
---|
| 123 | value_length_bin = struct.pack('>h', value_length) |
---|
| 124 | |
---|
| 125 | if i == 0: |
---|
[f6c6897] | 126 | values.append(''.join([tag_bin, |
---|
[8979f90] | 127 | name_length_bin, |
---|
| 128 | name_bin, |
---|
| 129 | value_length_bin, |
---|
| 130 | value_bin])) |
---|
| 131 | else: |
---|
[f6c6897] | 132 | values.append(''.join([tag_bin, |
---|
[8979f90] | 133 | name_length_bin, |
---|
| 134 | value_length_bin, |
---|
| 135 | value_bin])) |
---|
[c269bc7] | 136 | |
---|
| 137 | # concatenate everything together and return it along with the |
---|
| 138 | # total length of the attribute |
---|
[fc427ef] | 139 | return ''.join(values) |
---|
[c269bc7] | 140 | |
---|
[fc427ef] | 141 | @property |
---|
| 142 | def packed_value_size(self): |
---|
[5c5fe6d] | 143 | """Gets the total size of the attribute. |
---|
| 144 | |
---|
[c269bc7] | 145 | """ |
---|
[5c5fe6d] | 146 | |
---|
[75c0cab] | 147 | return len(self.packed_value) |
---|
[c269bc7] | 148 | |
---|
[fc427ef] | 149 | total_size = packed_value_size |
---|
[c269bc7] | 150 | |
---|
| 151 | def __str__(self): |
---|
| 152 | if len(self.values) > 0: |
---|
| 153 | values = [str(v) for v in self.values] |
---|
| 154 | else: |
---|
| 155 | values = "None" |
---|
| 156 | |
---|
| 157 | if self.name is None: |
---|
| 158 | name = "None" |
---|
| 159 | else: |
---|
| 160 | name = self.name |
---|
| 161 | |
---|
| 162 | return "%s: %s" % (name, str(values)) |
---|
[75c0cab] | 163 | |
---|
| 164 | def __repr__(self): |
---|
| 165 | return '<IPPAttribute (%r, %r)>' % (self.name, self.values) |
---|