if len(buf) < 4:
return buf
else:
- return buf[4:]
+ return buffer(buf,4)
def piWrap(buf, ether_mode, etherProto=etherProto):
if ether_mode:
return False
def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, stderr=sys.stderr, reconnect=None, rwrite=None, rread=None, tunqueue=1000, tunkqueue=1000,
- len=len, max=max, OSError=OSError, cipher='AES'):
+ cipher='AES',
+ len=len, max=max, OSError=OSError, select=select.select, selecterror=select.error, piWrap=piWrap, piStrip=piStrip, os=os, socket=socket):
crypto_mode = False
try:
if cipher_key:
tunfd = tun.fileno()
os_read = os.read
os_write = os.write
+ encrypt_ = encrypt
+ decrypt_ = decrypt
while not TERMINATE:
wset = []
if packetReady(bkbuf):
rset.append(remote)
try:
- rdrdy, wrdy, errs = select.select(rset,wset,(tun,remote),1)
- except select.error, e:
+ rdrdy, wrdy, errs = select(rset,wset,(tun,remote),1)
+ except selecterror, e:
if e.args[0] == errno.EINTR:
# just retry
continue
if remote in wrdy:
try:
try:
- while True:
+ while 1:
packet = pullPacket(fwbuf)
if crypto_mode:
- enpacket = encrypt(packet, crypter)
+ enpacket = encrypt_(packet, crypter)
else:
enpacket = packet
rwrite(remote, enpacket)
#wr += 1
- if stderr is not None:
- print >>stderr, '>', formatPacket(packet, ether_mode)
-
if not rnonblock or not packetReady(fwbuf):
break
except OSError,e:
traceback.print_exc(file=sys.stderr)
if tun in wrdy:
try:
- while True:
+ while 1:
packet = pullPacket(bkbuf)
- if stderr is not None:
- formatted = formatPacket(packet, ether_mode)
if with_pi:
packet = piWrap(packet, ether_mode)
os_write(tunfd, packet)
#wt += 1
- if stderr is not None:
- print >>stderr, '<', formatted
# Do not inject packets into the TUN faster than they arrive, unless we're falling
# behind. TUN devices discard packets if their queue is full (tunkqueue), but they
# check incoming data packets
if tun in rdrdy:
try:
- while True:
+ while 1:
packet = os_read(tunfd,2000) # tun.read blocks until it gets 2k!
#rt += 1
if with_pi:
if remote in rdrdy:
try:
try:
- while True:
+ while 1:
# Try twice, sometimes it barks the first time,
# due to ICMP Port Unreachable packets from previous writes
try:
#rr += 1
if crypto_mode:
- packet = decrypt(packet, crypter)
+ packet = decrypt_(packet, crypter)
bkbuf.append(packet)
if not rnonblock or len(bkbuf) >= maxbkbuf: