Added headerStripping to pytap, patched one tunnel

master
Sebastian Lohff 12 years ago
parent dc84f0265f
commit e78f20c22b

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

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

Loading…
Cancel
Save