Package dpkt :: Module llc
[hide private]
[frames] | no frames]

Source Code for Module dpkt.llc

  1  # -*- coding: utf-8 -*- 
  2   
  3  from __future__ import print_function 
  4  from __future__ import absolute_import 
  5   
  6  import struct 
  7   
  8  from . import dpkt 
  9  from . import stp 
10 11 12 -class LLC(dpkt.Packet):
13 """802.2 Logical Link Control (LLC) data communication protocol. 14 15 Attributes: 16 __hdr__ = ( 17 ('dsap', 'B', 0xaa), # Destination Service Access Point 18 ('ssap', 'B', 0xaa), # Source Service Access Point 19 ('ctl', 'B', 3) # Control Byte 20 ) 21 """ 22 23 __hdr__ = ( 24 ('dsap', 'B', 0xaa), # Destination Service Access Point 25 ('ssap', 'B', 0xaa), # Source Service Access Point 26 ('ctl', 'B', 3) # Control Byte 27 ) 28 29 @property
30 - def is_snap(self):
31 return self.dsap == self.ssap == 0xaa
32
33 - def unpack(self, buf):
34 from .ethernet import Ethernet, ETH_TYPE_IP, ETH_TYPE_IPX 35 36 dpkt.Packet.unpack(self, buf) 37 if self.is_snap: 38 self.oui, self.type = struct.unpack('>IH', b'\x00' + self.data[:5]) 39 self.data = self.data[5:] 40 try: 41 self.data = Ethernet.get_type(self.type)(self.data) 42 setattr(self, self.data.__class__.__name__.lower(), self.data) 43 except (KeyError, dpkt.UnpackError): 44 pass 45 else: 46 # non-SNAP 47 if self.dsap == 0x06: # SAP_IP 48 self.data = self.ip = Ethernet.get_type(ETH_TYPE_IP)(self.data) 49 elif self.dsap == 0x10 or self.dsap == 0xe0: # SAP_NETWARE{1,2} 50 self.data = self.ipx = Ethernet.get_type(ETH_TYPE_IPX)(self.data) 51 elif self.dsap == 0x42: # SAP_STP 52 self.data = self.stp = stp.STP(self.data)
53
54 - def pack_hdr(self):
55 buf = dpkt.Packet.pack_hdr(self) 56 if self.is_snap: # add SNAP sublayer 57 oui = getattr(self, 'oui', 0) 58 _type = getattr(self, 'type', 0) 59 if not _type and isinstance(self.data, dpkt.Packet): 60 from .ethernet import Ethernet 61 try: 62 _type = Ethernet.get_type_rev(self.data.__class__) 63 except KeyError: 64 pass 65 buf += struct.pack('>IH', oui, _type)[1:] 66 return buf
67
68 - def __len__(self): # add 5 bytes of SNAP header if needed
69 return self.__hdr_len__ + 5 * int(self.is_snap) + len(self.data)
70
71 72 -def test_llc():
73 from . import ip 74 from . import ethernet 75 s = (b'\xaa\xaa\x03\x00\x00\x00\x08\x00\x45\x00\x00\x28\x07\x27\x40\x00\x80\x06\x1d' 76 b'\x39\x8d\xd4\x37\x3d\x3f\xf5\xd1\x69\xc0\x5f\x01\xbb\xb2\xd6\xef\x23\x38\x2b' 77 b'\x4f\x08\x50\x10\x42\x04\xac\x17\x00\x00') 78 llc_pkt = LLC(s) 79 ip_pkt = llc_pkt.data 80 assert isinstance(ip_pkt, ip.IP) 81 assert llc_pkt.type == ethernet.ETH_TYPE_IP 82 assert ip_pkt.dst == b'\x3f\xf5\xd1\x69' 83 assert str(llc_pkt) == str(s) 84 assert len(llc_pkt) == len(s) 85 86 # construction with SNAP header 87 llc_pkt = LLC(ssap=0xaa, dsap=0xaa, data=ip.IP(s[8:])) 88 assert str(llc_pkt) == str(s) 89 90 # no SNAP 91 llc_pkt = LLC(ssap=6, dsap=6, data=ip.IP(s[8:])) 92 assert isinstance(llc_pkt.data, ip.IP) 93 assert str(llc_pkt) == str(b'\x06\x06\x03' + s[8:])
94 95 96 if __name__ == '__main__': 97 test_llc() 98 print('Tests Successful...') 99