remote = options.peer_addr elif options.protocol == "udp": # connect to remote endpoint if options.peer_addr and options.peer_port: rsock = tunchannel.udp_establish(TERMINATE, hostaddr, options.port, options.peer_addr, options.peer_port) remote = os.fdopen(rsock.fileno(), 'r+b', 0) else: print >> sys.stderr, "Error: need a remote endpoint in UDP mode" raise AssertionError, "Error: need a remote endpoint in UDP mode" elif options.protocol == "tcp": # connect to remote endpoint if options.peer_addr and options.peer_port: rsock = tunchannel.tcp_establish(TERMINATE, hostaddr, options.port, options.peer_addr, options.peer_port) remote = os.fdopen(rsock.fileno(), 'r+b', 0) else: print >> sys.stderr, "Error: need a remote endpoint in TCP mode" raise AssertionError, "Error: need a remote endpoint in TCP mode" else: msg = "Error: Invalid protocol %s" % options.protocol print >> sys.stderr, msg raise AssertionError, msg if filter_init: filter_local, filter_remote = filter_init() def filter_loop(): global TERMINATE
while not TERM: time.sleep(1) remote = options.peer_addr elif options.protocol == "udp": # connect to remote endpoint if options.peer_addr and options.peer_port: rsock = tunchannel.udp_establish(TERMINATE, hostaddr, options.port, options.peer_addr, options.peer_port) remote = os.fdopen(rsock.fileno(), 'r+b', 0) else: print >>sys.stderr, "Error: need a remote endpoint in UDP mode" raise AssertionError, "Error: need a remote endpoint in UDP mode" elif options.protocol == "tcp": # connect to remote endpoint if options.peer_addr and options.peer_port: rsock = tunchannel.tcp_establish(TERMINATE, hostaddr, options.port, options.peer_addr, options.peer_port) remote = os.fdopen(rsock.fileno(), 'r+b', 0) else: print >>sys.stderr, "Error: need a remote endpoint in TCP mode" raise AssertionError, "Error: need a remote endpoint in TCP mode" else: msg = "Error: Invalid protocol %s" % options.protocol print >>sys.stderr, msg raise AssertionError, msg if filter_init: filter_local, filter_remote = filter_init() def filter_loop(): global TERMINATE TERM = TERMINATE
def __forwarder(weak_self): # grab strong reference self = weak_self() if not self: return peer_port = self.peer_port peer_addr = self.peer_addr peer_proto = self.peer_proto peer_cipher = self.peer_cipher local_port = self.tun_port local_addr = self.tun_addr local_proto = self.tun_proto local_cipher = self.tun_cipher stderr = self.stderr ether_mode = self.ethernet_mode with_pi = self.with_pi if local_proto != peer_proto: raise RuntimeError, "Peering protocol mismatch: %s != %s" % ( local_proto, peer_proto) if local_cipher != peer_cipher: raise RuntimeError, "Peering cipher mismatch: %s != %s" % ( local_cipher, peer_cipher) if not peer_port or not peer_addr: raise RuntimeError, "Misconfigured peer for: %s" % (self, ) if not local_port or not local_addr: raise RuntimeError, "Misconfigured TUN: %s" % (self, ) TERMINATE = self._terminate SUSPEND = self._suspend cipher_key = self.tun_key tun = self.tun_socket udp = local_proto == 'udp' if not tun: raise RuntimeError, "Unconnected TUN channel %s" % (self, ) if local_proto == 'udp': rsock = udp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port) remote = os.fdopen(rsock.fileno(), 'r+b', 0) elif local_proto == 'tcp': rsock = tcp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port) remote = os.fdopen(rsock.fileno(), 'r+b', 0) else: raise RuntimeError, "Bad protocol for %s: %r" % (self, local_proto) # notify that we're ready self._connected.set() # drop strong reference del self print >> sys.stderr, "Connected" tun_fwd(tun, remote, with_pi=with_pi, ether_mode=ether_mode, cipher_key=cipher_key, udp=udp, TERMINATE=TERMINATE, SUSPEND=SUSPEND, stderr=stderr, cipher=local_cipher) tun.close() remote.close()