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

Source Code for Module dpkt.ppp

 1  # $Id: ppp.py 363 2006-05-08 19:32:45Z dugsong $ 
 2   
 3  """Point-to-Point Protocol.""" 
 4   
 5  import struct 
 6  import dpkt 
 7   
 8  # XXX - finish later 
 9   
10  # http://www.iana.org/assignments/ppp-numbers 
11  PPP_IP  = 0x21          # Internet Protocol 
12  PPP_IP6 = 0x57          # Internet Protocol v6 
13   
14  # Protocol field compression 
15  PFC_BIT = 0x01 
16   
17 -class PPP(dpkt.Packet):
18 __hdr__ = ( 19 ('p', 'B', PPP_IP), 20 ) 21 _protosw = {} 22
23 - def set_p(cls, p, pktclass):
24 cls._protosw[p] = pktclass
25 set_p = classmethod(set_p) 26
27 - def get_p(cls, p):
28 return cls._protosw[p]
29 get_p = classmethod(get_p) 30
31 - def unpack(self, buf):
32 dpkt.Packet.unpack(self, buf) 33 if self.p & PFC_BIT == 0: 34 self.p = struct.unpack('>H', buf[:2])[0] 35 self.data = self.data[1:] 36 try: 37 self.data = self._protosw[self.p](self.data) 38 setattr(self, self.data.__class__.__name__.lower(), self.data) 39 except (KeyError, struct.error, dpkt.UnpackError): 40 pass
41
42 - def pack_hdr(self):
43 try: 44 if self.p > 0xff: 45 return struct.pack('>H', self.p) 46 return dpkt.Packet.pack_hdr(self) 47 except struct.error, e: 48 raise dpkt.PackError(str(e))
49
50 -def __load_protos():
51 import os 52 d = dict.fromkeys([ x[:-3] for x in os.listdir(os.path.dirname(__file__) or '.') if x.endswith('.py') ]) 53 g = globals() 54 for k, v in g.iteritems(): 55 if k.startswith('PPP_'): 56 name = k[4:] 57 modname = name.lower() 58 if modname in d: 59 mod = __import__(modname, g) 60 PPP.set_p(v, getattr(mod, name))
61 62 if not PPP._protosw: 63 __load_protos() 64