forked from p15670423/monkey
find tunnel now searches on all host's interfaces
This commit is contained in:
parent
3990806d8c
commit
3dacde2f28
|
@ -20,11 +20,11 @@ DEFAULT_TIMEOUT = 10
|
||||||
QUIT_TIMEOUT = 60 * 10 # 10 minutes
|
QUIT_TIMEOUT = 60 * 10 # 10 minutes
|
||||||
|
|
||||||
|
|
||||||
def _set_multicast_socket(timeout=DEFAULT_TIMEOUT):
|
def _set_multicast_socket(timeout=DEFAULT_TIMEOUT, adapter=''):
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
sock.settimeout(timeout)
|
sock.settimeout(timeout)
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
sock.bind(('', MCAST_PORT))
|
sock.bind((adapter, MCAST_PORT))
|
||||||
sock.setsockopt(socket.IPPROTO_IP,
|
sock.setsockopt(socket.IPPROTO_IP,
|
||||||
socket.IP_ADD_MEMBERSHIP,
|
socket.IP_ADD_MEMBERSHIP,
|
||||||
struct.pack("4sl", socket.inet_aton(MCAST_GROUP), socket.INADDR_ANY))
|
struct.pack("4sl", socket.inet_aton(MCAST_GROUP), socket.INADDR_ANY))
|
||||||
|
@ -32,43 +32,44 @@ def _set_multicast_socket(timeout=DEFAULT_TIMEOUT):
|
||||||
|
|
||||||
|
|
||||||
def find_tunnel(default=None, attempts=3, timeout=DEFAULT_TIMEOUT):
|
def find_tunnel(default=None, attempts=3, timeout=DEFAULT_TIMEOUT):
|
||||||
sock = _set_multicast_socket(timeout)
|
|
||||||
|
|
||||||
l_ips = local_ips()
|
l_ips = local_ips()
|
||||||
|
|
||||||
for attempt in range(0, attempts):
|
for adapter in l_ips:
|
||||||
try:
|
for attempt in range(0, attempts):
|
||||||
sock.sendto("?", (MCAST_GROUP, MCAST_PORT))
|
try:
|
||||||
tunnels = []
|
LOG.info("Trying to find using adapter %s", adapter)
|
||||||
if default:
|
sock = _set_multicast_socket(timeout, adapter)
|
||||||
tunnels.append(default)
|
sock.sendto("?", (MCAST_GROUP, MCAST_PORT))
|
||||||
|
tunnels = []
|
||||||
while True:
|
if default:
|
||||||
try:
|
tunnels.append(default)
|
||||||
answer, address = sock.recvfrom(BUFFER_READ)
|
|
||||||
if answer not in ['?', '+', '-']:
|
|
||||||
tunnels.append(answer)
|
|
||||||
except socket.timeout:
|
|
||||||
break
|
|
||||||
|
|
||||||
for tunnel in tunnels:
|
while True:
|
||||||
if tunnel.find(':') != -1:
|
try:
|
||||||
address, port = tunnel.split(':', 1)
|
answer, address = sock.recvfrom(BUFFER_READ)
|
||||||
if address in l_ips:
|
if answer not in ['?', '+', '-']:
|
||||||
continue
|
tunnels.append(answer)
|
||||||
|
except socket.timeout:
|
||||||
|
break
|
||||||
|
|
||||||
LOG.debug("Checking tunnel %s:%s", address, port)
|
for tunnel in tunnels:
|
||||||
is_open, _ = check_port_tcp(address, int(port))
|
if tunnel.find(':') != -1:
|
||||||
if not is_open:
|
address, port = tunnel.split(':', 1)
|
||||||
LOG.debug("Could not connect to %s:%s", address, port)
|
if address in l_ips:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
sock.sendto("+", (address, MCAST_PORT))
|
LOG.debug("Checking tunnel %s:%s", address, port)
|
||||||
sock.close()
|
is_open, _ = check_port_tcp(address, int(port))
|
||||||
return address, port
|
if not is_open:
|
||||||
except Exception, exc:
|
LOG.debug("Could not connect to %s:%s", address, port)
|
||||||
LOG.debug("Caught exception in tunnel lookup: %s", exc)
|
continue
|
||||||
continue
|
|
||||||
|
sock.sendto("+", (address, MCAST_PORT))
|
||||||
|
sock.close()
|
||||||
|
return address, port
|
||||||
|
except Exception, exc:
|
||||||
|
LOG.debug("Caught exception in tunnel lookup: %s", exc)
|
||||||
|
continue
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue