From f210967a1dd8e0d78848f83284bd36842f5541e1 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 1 Jan 2008 14:47:06 -0700 Subject: [PATCH] Pick up TCP mid-stream --- __init__.py | 4 ++++ ip.py | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/__init__.py b/__init__.py index 85a773a..5501f96 100755 --- a/__init__.py +++ b/__init__.py @@ -19,6 +19,10 @@ def to_printable(c): return c elif c == '\0': return '␀' + elif c == '\r': + return '␍' + elif c == '\n': + return '␤' else: return '·' diff --git a/ip.py b/ip.py index 28e125f..72f0516 100755 --- a/ip.py +++ b/ip.py @@ -160,7 +160,8 @@ class Chunk: return s def __add__(self, next): - new = Chunk(self.seq, self.drop) + seq = min(self.seq, next.seq) + new = Chunk(seq, self.drop) for frame in self.collection.itervalues(): new.add(frame) for frame in next.collection.itervalues(): @@ -229,24 +230,30 @@ class TCP_Session: self.seq = [pkt.seq, pkt.ack] self.handle = self.handle_packet else: - raise ValueError('Weird flags in handshake: %d' % pkt.flags) + # In the middle of a session, do the best we can + self.cli, self.srv = pkt.src, pkt.dst + self.seq = [pkt.seq, pkt.ack] + self.handle = self.handle_packet + self.handle(pkt) def handle_packet(self, pkt): ret = None self.frames += 1 - # Which way is this going? + # Which way is this going? 0 == from client idx = int(pkt.src == self.srv) xdi = 1 - idx # Does this ACK after the last output sequence number? - if pkt.ack > self.seq[xdi]: - ret = Chunk(self.seq[xdi]) + seq = self.seq[xdi] + if pkt.ack > seq: + ret = Chunk(seq) pending = self.pending[xdi] for key in pending.keys(): if key >= pkt.ack: continue - ret.add(pending[key]) + if key >= seq: + ret.add(pending[key]) del pending[key] self.seq[xdi] = pkt.ack