Added headerStripping to pytap, patched one tunnel

This commit is contained in:
Sebastian Lohff 2012-01-12 00:41:07 +01:00
parent dc84f0265f
commit e78f20c22b
2 changed files with 13 additions and 6 deletions

View File

@ -14,13 +14,15 @@ class TapDevice:
DEVPATH = "/dev/net/tun"
_ifreq = "16sh"
def __init__(self, name='', tap=True, conf=None):
def __init__(self, name='', tap=True, conf=None, stripHeader=True):
""" Constructor for the device.
name - the device name, use a %d for a generated device numer
tap - if this device should be a tap device
conf - conf to pass to the ifconfig function of this class
(if None ifconfig() won't be called)
stripHeader - strips the first 4 bytes, they don't belong to
the actual network traffic ("\x00\x00\x08\x00")
"""
self._mode = (tap and self.IFF_TAP) or self.IFF_TUN
self._fd = None
@ -29,6 +31,7 @@ class TapDevice:
self._mac = None
self._mtu = 1500
self.conf = conf
self._stripHeader = stripHeader
if name == '':
self._nametpl = (tap and "tap%d") or "tun%d"
@ -98,10 +101,14 @@ class TapDevice:
# don't forget the ethernet frame (not included in MTU)
readSize += 18
data = os.read(self._fd, self._mtu)
if self._stripHeader:
data = data[4:]
return data
def write(self, data):
""" Write a packet to the device """
if self._stripHeader:
data= "\x00\x00\x08\x00" + data
os.write(self._fd, data)
def close(self):

View File

@ -74,8 +74,8 @@ class UdevReader(threading.Thread):
calcedChecksum = crc32(packet) & 0xffffffff
if checksum != calcedChecksum:
print " !! Checksum failed (was 0x%08x, should be 0x%08x)" % (checksum, calcedChecksum)
return Falsea
self.dev.write("\x00\x00\x08\x00" + packet)
return False
self.dev.write(packet)
return True
def run(self):
@ -122,9 +122,9 @@ class RFC1149(Ether2Any):
print packet
def sendToNet(self, packet):
print "packet", repr(packet[4:])
scp = IP(packet[4:])
checksum = crc32(packet[4:]) & 0xffffffff
print "packet", repr(packet)
scp = IP(packet)
checksum = crc32(packet) & 0xffffffff
checksumStr = "%08x" % checksum
hexRepr = " ".join(map(lambda x: "%02x" % ord(x), packet[4:])) + " " + " ".join(map(lambda x: checksumStr[x:x+2], range(0, len(checksumStr), 2)))
print " ?>", scp.summary(), " -- hex len", len(hexRepr), "checksum 0x%08x" % checksum