Browse Source

Added headerStripping to pytap, patched one tunnel

Sebastian Lohff 8 years ago
parent
commit
e78f20c22b
2 changed files with 13 additions and 6 deletions
  1. 8
    1
      pytap.py
  2. 5
    5
      tunnel/rfc1149/rfc1149.py

+ 8
- 1
pytap.py View File

@@ -14,13 +14,15 @@ class TapDevice:
14 14
 	DEVPATH   = "/dev/net/tun"
15 15
 	_ifreq = "16sh"
16 16
 
17
-	def __init__(self, name='', tap=True, conf=None):
17
+	def __init__(self, name='', tap=True, conf=None, stripHeader=True):
18 18
 		""" Constructor for the device.
19 19
 		
20 20
 		name - the device name, use a %d for a generated device numer
21 21
 		tap  - if this device should be a tap device
22 22
 		conf - conf to pass to the ifconfig function of this class
23 23
 		       (if None ifconfig() won't be called)
24
+		stripHeader - strips the first 4 bytes, they don't belong to
25
+		              the actual network traffic ("\x00\x00\x08\x00")
24 26
 		"""
25 27
 		self._mode = (tap and self.IFF_TAP) or self.IFF_TUN
26 28
 		self._fd = None
@@ -29,6 +31,7 @@ class TapDevice:
29 31
 		self._mac = None
30 32
 		self._mtu = 1500
31 33
 		self.conf = conf
34
+		self._stripHeader = stripHeader
32 35
 		
33 36
 		if name == '':
34 37
 			self._nametpl = (tap and "tap%d") or "tun%d"
@@ -98,10 +101,14 @@ class TapDevice:
98 101
 			# don't forget the ethernet frame (not included in MTU)
99 102
 			readSize += 18
100 103
 		data = os.read(self._fd, self._mtu)
104
+		if self._stripHeader:
105
+			data = data[4:]
101 106
 		return data
102 107
 	
103 108
 	def write(self, data):
104 109
 		""" Write a packet to the device """
110
+		if self._stripHeader:
111
+			data= "\x00\x00\x08\x00" + data
105 112
 		os.write(self._fd, data)
106 113
 	
107 114
 	def close(self):

+ 5
- 5
tunnel/rfc1149/rfc1149.py View File

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

Loading…
Cancel
Save