File: tns.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 (49 lines) | stat: -rw-r--r-- 1,247 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
# $Id: tns.py 23 2006-11-08 15:45:33Z dugsong $
# -*- coding: utf-8 -*-
"""Transparent Network Substrate."""
from __future__ import print_function
from __future__ import absolute_import

from . import dpkt


class TNS(dpkt.Packet):
    """Transparent Network Substrate.

    TODO: Longer class information....

    Attributes:
        __hdr__: Header fields of TNS.
        TODO.
    """

    __hdr__ = (
        ('length', 'H', 0),
        ('pktsum', 'H', 0),
        ('type', 'B', 0),
        ('rsvd', 'B', 0),
        ('hdrsum', 'H', 0),
        ('msg', '0s', b''),
    )

    def unpack(self, buf):
        dpkt.Packet.unpack(self, buf)
        n = self.length - self.__hdr_len__
        if n > len(self.data):
            raise dpkt.NeedData('short message (missing %d bytes)' %
                                (n - len(self.data)))
        self.msg = self.data[:n]
        self.data = self.data[n:]


def test_tns():
    s = (b'\x00\x23\x00\x00\x01\x00\x00\x00\x01\x34\x01\x2c\x00\x00\x08\x00\x7f'
         b'\xff\x4f\x98\x00\x00\x00\x01\x00\x01\x00\x22\x00\x00\x00\x00\x01\x01X')
    t = TNS(s)
    assert t.msg.startswith(b'\x01\x34')

    # test a truncated packet
    try:
        t = TNS(s[:-10])
    except dpkt.NeedData:
        pass