-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiscovery.py
More file actions
61 lines (50 loc) · 1.52 KB
/
discovery.py
File metadata and controls
61 lines (50 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import socket
import ipaddress
from config import *
from util import *
from device_id import *
from serialize import *
def ip_to_string(b):
if len(b) == 0:
return ''
elif len(b) == 4:
return ipaddress.IPv4Address(b).exploded
elif len(b) == 16:
return ipaddress.IPv6Address(b).exploded
else:
assert(0)
def announcement(myID, serverID):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(40)
sock.bind(('', 21025))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
print('Local discovery...')
sock.sendto(pack_announce(myID), ('<broadcast>', 21025))
tries = 0
ret = None
while True:
if tries == 1:
print('Global discovery...')
sock.sendto(pack_query(serverID), (server_announce, 22026))
elif tries > 1:
break
message , address = sock.recvfrom(1024)
try:
a, _ = unpack_announce(message)
except socket.timeout:
tries +=1
except:
sock.close()
msg_address, _ = address
if a['magic'] == 0x9D79BC39 and \
a['device']['id'] == serverID:
m = a['device']['addresses']
if m:
ret = (ip_to_string(m[0]['ip']) or msg_address, \
m[0]['port'] or 22000)
else:
ret = (msg_address, 22000)
break
sock.close()
return ret