Premature optimization

This commit is contained in:
Neale Pickett 2007-12-12 12:22:20 -07:00
parent 5282669973
commit 9308b408eb
1 changed files with 15 additions and 4 deletions

View File

@ -16,6 +16,7 @@ def unpack_nybbles(byte):
class Frame: class Frame:
def __init__(self, frame): def __init__(self, frame):
# Ethernet
(self.eth_dhost, (self.eth_dhost,
self.eth_shost, self.eth_shost,
self.eth_type, self.eth_type,
@ -23,6 +24,7 @@ class Frame:
if self.eth_type != 0x0800: if self.eth_type != 0x0800:
raise ValueError('Not IP %04x' % self.eth_type) raise ValueError('Not IP %04x' % self.eth_type)
# IP
(self.ihlvers, (self.ihlvers,
self.tos, self.tos,
self.tot_len, self.tot_len,
@ -37,6 +39,7 @@ class Frame:
if self.protocol != 6: if self.protocol != 6:
raise ValueError('Not TCP') raise ValueError('Not TCP')
# TCP
(self.th_sport, (self.th_sport,
self.th_dport, self.th_dport,
self.th_seq, self.th_seq,
@ -53,18 +56,26 @@ class Frame:
self.th_options, p = p[:opt_length - 20], p[opt_length - 20:] self.th_options, p = p[:opt_length - 20], p[opt_length - 20:]
payload = p[:self.tot_len - opt_length - 20] payload = p[:self.tot_len - opt_length - 20]
# Nice formatting
self.src = (self.saddr, self.th_sport) self.src = (self.saddr, self.th_sport)
self.dst = (self.daddr, self.th_dport) self.dst = (self.daddr, self.th_dport)
self.seq = self.th_seq self.seq = self.th_seq
self.ack = self.th_ack self.ack = self.th_ack
self.payload = payload self.payload = payload
self.saddr = socket.inet_ntoa(self.saddr) def get_src_addr(self):
self.daddr = socket.inet_ntoa(self.daddr) self.src_addr = socket.inet_ntoa(self.saddr)
return self.src_addr
src_addr = property(get_src_addr)
def get_dst_addr(self):
self.dst_addr = socket.inet_ntoa(self.daddr)
return self.dst_addr
dst_addr = property(get_dst_addr)
def __repr__(self): def __repr__(self):
return '<Frame %s:%d -> %s:%d len %d>' % (self.saddr, self.th_sport, return '<Frame %s:%d -> %s:%d len %d>' % (self.src_addr, self.th_sport,
self.daddr, self.th_dport, self.drc_addr, self.th_dport,
len(self.payload)) len(self.payload))