put socket timeout and deadline around mumble queries

This commit is contained in:
jesopo 2019-09-02 14:18:27 +01:00
parent 3c7b276188
commit c80aab88f3

View file

@ -20,13 +20,21 @@ class Module(ModuleManager.BaseModule):
timestamp = datetime.datetime.utcnow().microsecond
ping_packet = struct.pack(">iQ", 0, timestamp)
s = socket.socket(type=socket.SOCK_DGRAM)
s.settimeout(5)
try:
s.sendto(ping_packet, (server, port))
except socket.gaierror as e:
raise utils.EventError(str(e))
with utils.deadline():
try:
s.sendto(ping_packet, (server, port))
except socket.gaierror as e:
raise utils.EventError(str(e))
try:
pong_packet = s.recv(24)
except socket.timeout:
raise utils.EventError(
"Timed out waiting for response from %s:%d"
% (server, port))
pong_packet = s.recv(24)
pong = struct.unpack(">bbbbQiii", pong_packet)
version = ".".join(str(v) for v in pong[1:4])