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

Source Code for Module dpkt.tftp

 1  # $Id: tftp.py 271 2006-01-11 16:03:33Z dugsong $ 
 2   
 3  """Trivial File Transfer Protocol.""" 
 4   
 5  import struct 
 6  import dpkt 
 7   
 8  # Opcodes 
 9  OP_RRQ     = 1    # read request 
10  OP_WRQ     = 2    # write request 
11  OP_DATA    = 3    # data packet 
12  OP_ACK     = 4    # acknowledgment 
13  OP_ERR     = 5    # error code 
14   
15  # Error codes 
16  EUNDEF     = 0    # not defined 
17  ENOTFOUND  = 1    # file not found 
18  EACCESS    = 2    # access violation 
19  ENOSPACE   = 3    # disk full or allocation exceeded 
20  EBADOP     = 4    # illegal TFTP operation 
21  EBADID     = 5    # unknown transfer ID 
22  EEXISTS    = 6    # file already exists 
23  ENOUSER    = 7    # no such user 
24   
25 -class TFTP(dpkt.Packet):
26 __hdr__ = (('opcode', 'H', 1), ) 27
28 - def unpack(self, buf):
29 dpkt.Packet.unpack(self, buf) 30 if self.opcode in (OP_RRQ, OP_WRQ): 31 l = self.data.split('\x00') 32 self.filename = l[0] 33 self.mode = l[1] 34 self.data = '' 35 elif self.opcode in (OP_DATA, OP_ACK): 36 self.block = struct.unpack('>H', self.data[:2]) 37 self.data = self.data[2:] 38 elif self.opcode == OP_ERR: 39 self.errcode = struct.unpack('>H', self.data[:2]) 40 self.errmsg = self.data[2:].split('\x00')[0] 41 self.data = ''
42
43 - def __len__(self):
44 return len(str(self))
45
46 - def __str__(self):
47 if self.opcode in (OP_RRQ, OP_WRQ): 48 s = '%s\x00%s\x00' % (self.filename, self.mode) 49 elif self.opcode in (OP_DATA, OP_ACK): 50 s = struct.pack('>H', self.block) 51 elif self.opcode == OP_ERR: 52 s = struct.pack('>H', self.errcode) + ('%s\x00' % self.errmsg) 53 else: 54 s = '' 55 return self.pack_hdr() + s + self.data
56