Further improvements

This commit is contained in:
Neale Pickett 2008-07-25 17:38:30 -06:00
parent 3f20d4ab29
commit 5fd76ede00
1 changed files with 34 additions and 21 deletions

55
ip.py
View File

@ -136,10 +136,11 @@ class Frame:
dst_addr = property(get_dst_addr) dst_addr = property(get_dst_addr)
def __repr__(self): def __repr__(self):
return '<Frame %s %s:%r -> %s:%r length %d>' % (self.name, return ('<Frame %s %s:%r(%08x) -> %s:%r(%08x) length %d>' %
self.src_addr, self.sport, (self.name,
self.dst_addr, self.dport, self.src_addr, self.sport, self.seq,
len(self.payload)) self.dst_addr, self.dport, self.ack,
len(self.payload)))
def __arp_repr__(self): def __arp_repr__(self):
return '<Frame %s %s(%s) -> %s(%s)>' % (self.name, return '<Frame %s %s(%s) -> %s(%s)>' % (self.name,
@ -180,10 +181,9 @@ class TCP_Resequence:
def __init__(self): def __init__(self):
self.cli = None self.cli = None
self.srv = None self.srv = None
self.seq = [None, None] self.lastack = [None, None]
self.first = None self.first = None
self.pending = [{}, {}] self.pending = [{}, {}]
self.frames = 0
self.closed = 0 self.closed = 0
self.midstream = False self.midstream = False
self.hash = 0 self.hash = 0
@ -201,8 +201,6 @@ class TCP_Resequence:
pass pass
def handle_handshake(self, pkt): def handle_handshake(self, pkt):
self.frames += 1
if not self.first: if not self.first:
self.first = pkt self.first = pkt
self.hash = pkt.hash self.hash = pkt.hash
@ -231,7 +229,6 @@ class TCP_Resequence:
def handle_packet(self, pkt): def handle_packet(self, pkt):
ret = None ret = None
self.frames += 1
# Which way is this going? 0 == from client # Which way is this going? 0 == from client
idx = int(pkt.src == self.srv) idx = int(pkt.src == self.srv)
@ -259,7 +256,9 @@ class TCP_Resequence:
if key >= pkt.ack: if key >= pkt.ack:
break break
if key < seq: if key < seq:
warnings.warn('Dropping %r from mid-stream session' % pending[key]) # Hopefully just a retransmit...
del pending[key]
continue
elif key > seq: elif key > seq:
gs.append(key - seq) gs.append(key - seq)
seq = key seq = key
@ -277,12 +276,14 @@ class TCP_Resequence:
# Is it a FIN or RST? # Is it a FIN or RST?
if pkt.flags & (FIN | RST): if pkt.flags & (FIN | RST):
self.lastack[xdi] = pkt.seq + 1
self.closed += 1 self.closed += 1
if self.closed == 2: if self.closed == 2:
# Warn about any unhandled packets # Warn about any unhandled packets
if self.pending[0] or self.pending[1]: if self.pending[0] or self.pending[1]:
warnings.warn('Dropping unhandled frames after shutdown' % pkt) warnings.warn('Dropping unhandled frames after shutdown' % pkt)
self.handle = self.handle_drop self.handle = self.handle_drop
ret = None
return ret return ret
@ -470,7 +471,7 @@ class Packet(UserDict.DictMixin):
def opcode_unknown(self): def opcode_unknown(self):
"""Unknown opcode""" """Unknown opcode"""
raise AttributeError('Opcode %d unknown' % self.opcode) raise AttributeError('Opcode %s unknown' % self.opcode)
class Session: class Session:
@ -483,8 +484,15 @@ class Session:
self.firstframe = frame self.firstframe = frame
self.lastframe = [None, None] self.lastframe = [None, None]
self.basename = 'transfers/%s' % (frame.src_addr,) self.basename = 'transfers/%s' % (frame.src_addr,)
self.basename2 = 'transfers/%s' % (frame.dst_addr,)
self.pending = {} self.pending = {}
self.count = 0 self.count = 0
for d in (self.basename, self.basename2):
try:
os.makedirs(d)
except OSError:
pass
self.setup() self.setup()
def setup(self): def setup(self):
@ -544,16 +552,22 @@ class Session:
return return
def make_filename(self, fn): def open_out(self, fn):
frame = self.firstframe
fn = '%d-%s~%d-%s~%d---%s' % (frame.time,
frame.src_addr, frame.sport,
frame.dst_addr, frame.dport,
urllib.quote(fn, ''))
fullfn = os.path.join(self.basename, fn)
fullfn2 = os.path.join(self.basename2, fn)
print 'writing %s' % (fn,)
fd = file(fullfn, 'w')
try: try:
os.makedirs(self.basename) os.unlink(fullfn2)
except OSError: except OSError:
pass pass
frame = self.firstframe os.link(fullfn, fullfn2)
fn = '%s:%d-%s:%d---%s' % (frame.src_addr, frame.sport, return fd
frame.dst_addr, frame.dport,
urllib.quote(fn, '\:'))
return os.path.join(self.basename, fn)
def handle_packets(self, collection): def handle_packets(self, collection):
"""Handle a collection of packets""" """Handle a collection of packets"""
@ -566,8 +580,7 @@ class Session:
class HtmlSession(Session): class HtmlSession(Session):
def __init__(self, frame): def __init__(self, frame):
Session.__init__(self, frame) Session.__init__(self, frame)
self.sessfn = self.make_filename('session.html') self.sessfd = self.open_out('session.html')
self.sessfd = file(self.sessfn, 'w')
self.sessfd.write('''<?xml version="1.0" encoding="UTF-8"?> self.sessfd.write('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html <!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
@ -591,7 +604,7 @@ class HtmlSession(Session):
def log(self, frame, payload, escape=True): def log(self, frame, payload, escape=True):
if escape: if escape:
p = cgi.escape(payload) p = cgi.escape(str(payload))
else: else:
p = payload p = payload
if not self.srv: if not self.srv: