Fix bug in gapstr, ARP addition cleanup

This commit is contained in:
Neale Pickett 2008-07-14 18:17:43 -06:00
parent 79daacd3ce
commit dc12a01a55
2 changed files with 13 additions and 7 deletions

View File

@ -73,7 +73,7 @@ class GapString:
start = min(self.length, start) start = min(self.length, start)
new = self.__class__(drop=self.drop) new = self.__class__(drop=self.drop)
new.length = end - start new.length = max(end - start, 0)
if new.length == 0: if new.length == 0:
new.contents = [] new.contents = []
return new return new

18
ip.py
View File

@ -17,24 +17,30 @@ def unpack_nybbles(byte):
return (byte >> 4, byte & 0x0F) return (byte >> 4, byte & 0x0F)
IP = 0x0800
ARP = 0x0806
ICMP = 1 ICMP = 1
TCP = 6 TCP = 6
UDP = 17 UDP = 17
def str_of_eth(d):
return ':'.join([('%02x' % ord(x)) for x in d])
class Frame: class Frame:
"""Turn an ethernet frame into relevant parts""" """Turn an ethernet frame into relevant parts"""
def __init__(self, pkt): def __init__(self, pkt):
((self.time, _, _), frame) = pkt ((self.time, self.time_usec, _), frame) = pkt
# Ethernet # Ethernet
(self.eth_dhost, (self.eth_dhost,
self.eth_shost, self.eth_shost,
self.eth_type, self.eth_type,
p) = unpack('!6s6sH', frame) p) = unpack('!6s6sH', frame)
if self.eth_type == 0x0806: if self.eth_type == ARP:
# ARP # ARP
self.name = 'ARP' self.name, self.protocol = ('ARP', ARP)
(self.ar_hrd, (self.ar_hrd,
self.ar_pro, self.ar_pro,
self.ar_hln, self.ar_hln,
@ -48,7 +54,7 @@ class Frame:
self.saddr = self.ar_sip self.saddr = self.ar_sip
self.daddr = self.ar_tip self.daddr = self.ar_tip
self.__repr__ = self.__arp_repr__ self.__repr__ = self.__arp_repr__
elif self.eth_type == 0x0800: elif self.eth_type == IP:
# IP # IP
(self.ihlvers, (self.ihlvers,
self.tos, self.tos,
@ -132,9 +138,9 @@ class Frame:
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,
self.ar_sha.encode('hex'), str_of_eth(self.ar_sha),
self.src_addr, self.src_addr,
self.ar_tha.encode('hex'), str_of_eth(self.ar_tha),
self.dst_addr) self.dst_addr)