UDP socket setsockopt fix

This commit is contained in:
Peter Šurda 2017-08-05 10:14:15 +02:00
parent 7a4551e1e7
commit 8f14fb05a1
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -33,6 +33,7 @@ class UDPSocket(BMProto):
self.create_socket(socket.AF_INET6, socket.SOCK_DGRAM) self.create_socket(socket.AF_INET6, socket.SOCK_DGRAM)
else: else:
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM) self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
self.set_socket_reuse()
logger.info("Binding UDP socket to %s:%i", host, UDPSocket.port) logger.info("Binding UDP socket to %s:%i", host, UDPSocket.port)
self.socket.bind((host, UDPSocket.port)) self.socket.bind((host, UDPSocket.port))
#BINDTODEVICE is only available on linux and requires root #BINDTODEVICE is only available on linux and requires root
@ -42,12 +43,7 @@ class UDPSocket(BMProto):
#except AttributeError: #except AttributeError:
else: else:
self.socket = sock self.socket = sock
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) self.set_socket_reuse()
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
pass
self.listening = state.Peer(self.socket.getsockname()[0], self.socket.getsockname()[1]) self.listening = state.Peer(self.socket.getsockname()[0], self.socket.getsockname()[1])
self.destination = state.Peer(self.socket.getsockname()[0], self.socket.getsockname()[1]) self.destination = state.Peer(self.socket.getsockname()[0], self.socket.getsockname()[1])
ObjectTracker.__init__(self) ObjectTracker.__init__(self)
@ -55,6 +51,14 @@ class UDPSocket(BMProto):
self.connected = True self.connected = True
self.set_state("bm_header", expectBytes=protocol.Header.size) self.set_state("bm_header", expectBytes=protocol.Header.size)
def set_socket_reuse(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
pass
def state_bm_command(self): def state_bm_command(self):
BMProto.state_bm_command(self) BMProto.state_bm_command(self)