Commits

Ivan Vučica committed cac5743

Buffering fixes.

Comments (0)

Files changed (1)

 				
 				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])) + ")"
+				if len(incomingData) >= 2:
+					print " (just arrived first 2 bytes: " + str(struct.unpack('!H', incomingData[0:2])[0]) + ")"
+
 				while len(netBuffer) >= netBufferExpected:
-					if len(netBuffer) > 0:
+					if len(netBuffer) > 0 and netBufferExpected > 0:
 						print " GOT ALL DATA!"
 						try:
-							os.write(tuntapFD, netBuffer[:netBufferExpected])
+							dataToWrite = netBuffer[:netBufferExpected]
+							print "  (writing " + str(len(dataToWrite)) + " bytes)"
+							os.write(tuntapFD, dataToWrite)
+							netBuffer = netBuffer[netBufferExpected:]
 							netBufferExpected = 0
+							print "  (data remaining in buffer: " + str(len(netBuffer)) + ")"
 						except Exception, e:
 							print "problem"
 							print e
 
 					if len(netBuffer) > 2:
-						netBufferExpected = struct.unpack('!H', netBuffer[0:2])
+						netBufferExpected = struct.unpack('!H', netBuffer[0:2])[0]
 						#netBufferExpected = socket.ntohs(netBufferExpected)
 						netBuffer = netBuffer[2:]
 						print " NOW EXPECTING " + str(netBufferExpected)
+						if netBufferExpected > 1500 * 2: # more than 2x MTU? something is wrong
+							print "  (which is more than 2x MTU, so giving up)"
+							netBuffer = ""
+							netBufferExpected = 0
 
+					if len(netBuffer) == 0 and netBufferExpected == 0:
+						break
 				return