1. Ivan Vučica
  2. XMPP TUN/TAP

Commits

Ivan Vučica  committed f90ddff

Work on buffering and packet size header.

  • Participants
  • Parent commits 4fe8367
  • Branches default

Comments (0)

Files changed (2)

File README.md

View file
  • Ignore whitespace
 -----
 To work with Linux, specify `net/tun` as the device on command line.
 
+Linux IPv4 forwarding
+---------------
+I'm primarily documenting this for my own use; run as root:
+
+    echo "1" > /proc/sys/net/ipv4/ip_forward
+    iptables -A FORWARD -i eth0 -o xmpptuntap0 -m state --state ESTABLISHED,RELATED -j ACCEPT
+    iptables -A FORWARD -i xmpptuntap0 -o eth0 -j ACCEPT
+    iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
+
+Save and restore iptables:
+
+    iptables-save > iptables.conf
+    iptables-restore < iptables.conf
+
+Show rules and flush them:
+
+    iptables -L
+    iptables -F
+
+
 Todo
 ----
 

File xmpptuntap.py

View file
  • Ignore whitespace
 
             conn.send(rep)
             raise xmpp.protocol.NodeProcessed
-	
+
+netBuffer = ""
+netBufferExpected = 0
+import struct
 def onMessage(client, message_in):
+
+	global netBuffer, netBufferExpected
 	global targetForNetTraffic
 	text = message_in.getBody()
 	user = message_in.getFrom()
 			if aNode.getName() == "data":
 
 				incomingData = base64.decodestring(aNode.getCDATA())
-			
-				try:
-					os.write(tuntapFD, incomingData)
-				except Exception, e:
-					print "problem"
-					print e
+				
+				netBuffer += incomingData
+				print " GOT " + str(len(incomingData)) + " DATA - new buffer size is " + str(len(netBuffer)) + ", expecting " + str(netBufferExpected)
+				print " (just arrived first 2 bytes: " + str(struct.unpack('!H', incomingData[0:2])) + ")"
+				while len(netBuffer) >= netBufferExpected:
+					if len(netBuffer) > 0:
+						print " GOT ALL DATA!"
+						try:
+							os.write(tuntapFD, netBuffer[:netBufferExpected])
+							netBufferExpected = 0
+						except Exception, e:
+							print "problem"
+							print e
+
+					if len(netBuffer) > 2:
+						netBufferExpected = struct.unpack('!H', netBuffer[0:2])
+						#netBufferExpected = socket.ntohs(netBufferExpected)
+						netBuffer = netBuffer[2:]
+						print " NOW EXPECTING " + str(netBufferExpected)
+
 				return	
 
 
 	TUNMODE |= IFF_NO_PI # do not prepend protocol information
 
 	from fcntl import ioctl
-	import struct
 
 	ifs = ioctl(tuntapFD, TUNSETIFF, struct.pack("16sH", "xmpptuntap%d", TUNMODE))
 	tuntapDevice = ifs[:16].strip("\x00")
 				#fcntl.ioctl(each, fcntl.FIONREAD...)
 				try:
 					output = os.read(tuntapFD, 8192)
+					output = struct.pack('!H', len(output)) + output # network byte order short is "tapper"'s header
 					dataNode.setData(base64.encodestring(output))
 
 					message = xmpp.Message(targetForNetTraffic, payload = [dataNode])