Small bugfixes and cosmetic changes

This commit is contained in:
Neale Pickett 2008-01-01 18:55:24 -07:00
parent f210967a1d
commit f52666a4c9
2 changed files with 18 additions and 10 deletions

View File

@ -4,7 +4,7 @@
import sys import sys
import struct import struct
printable = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}`~/=-\\?+|\',."<> ' printable = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}`~/=-\\?+|\',."<>: '
def unpack(fmt, buf): def unpack(fmt, buf):
"""Unpack buf based on fmt, return the rest as a string.""" """Unpack buf based on fmt, return the rest as a string."""

26
ip.py
View File

@ -78,8 +78,8 @@ class Frame:
self.cheksum, self.cheksum,
self.id, self.id,
self.seq, self.seq,
p) = unpackt('!BBHHH', p) p) = unpack('!BBHHH', p)
self.payload = p[:self.tot-len - 8] self.payload = p[:self.tot_len - 8]
else: else:
raise ValueError('Unknown protocol') raise ValueError('Unknown protocol')
@ -169,6 +169,12 @@ class Chunk:
return new return new
FIN = 1
SYN = 2
RST = 4
PSH = 8
ACK = 16
class TCP_Session: class TCP_Session:
"""TCP session resequencer. """TCP session resequencer.
@ -218,13 +224,13 @@ class TCP_Session:
if not self.first: if not self.first:
self.first = pkt self.first = pkt
if pkt.flags == 2: # SYN if pkt.flags == SYN:
self.cli, self.srv = pkt.src, pkt.dst self.cli, self.srv = pkt.src, pkt.dst
elif pkt.flags == 18: # SYNACK elif pkt.flags == (SYN | ACK):
assert (pkt.src == (self.srv or pkt.src)) assert (pkt.src == (self.srv or pkt.src))
self.cli, self.srv = pkt.dst, pkt.src self.cli, self.srv = pkt.dst, pkt.src
self.seq = [pkt.ack, pkt.seq + 1] self.seq = [pkt.ack, pkt.seq + 1]
elif pkt.flags == 16: # ACK elif pkt.flags == ACK:
assert (pkt.src == (self.cli or pkt.src)) assert (pkt.src == (self.cli or pkt.src))
self.cli, self.srv = pkt.src, pkt.dst self.cli, self.srv = pkt.src, pkt.dst
self.seq = [pkt.seq, pkt.ack] self.seq = [pkt.seq, pkt.ack]
@ -254,6 +260,8 @@ class TCP_Session:
continue continue
if key >= seq: if key >= seq:
ret.add(pending[key]) ret.add(pending[key])
else:
warnings.warn('Dropping %r from mid-stream session' % pending[key])
del pending[key] del pending[key]
self.seq[xdi] = pkt.ack self.seq[xdi] = pkt.ack
@ -262,12 +270,12 @@ class TCP_Session:
self.pending[idx][pkt.seq] = pkt self.pending[idx][pkt.seq] = pkt
# Is it a FIN or RST? # Is it a FIN or RST?
if pkt.flags & 5: if pkt.flags & (FIN | RST):
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('Unhandled packets') warnings.warn('Dropping unhandled frames after shutdown' % pkt)
self.handle = self.handle_drop self.handle = self.handle_drop
return ret return ret
@ -275,8 +283,8 @@ class TCP_Session:
def handle_drop(self, pkt): def handle_drop(self, pkt):
"""Warn about any unhandled packets""" """Warn about any unhandled packets"""
if not pkt.flags & 5: if pkt.payload:
warnings.warn('Extra packets at the end') warnings.warn('Spurious frame after shutdown: %r %d' % (pkt, pkt.flags))
class HTTP_side: class HTTP_side: