Skip to content

Commit 440f12a

Browse files
committed
fix: explicitly use remote addr for unicast UDP queries
1 parent 085a9b6 commit 440f12a

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

opengsq/protocol_socket.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,26 @@ async def communicate(protocol: ProtocolBase, data: bytes, source_port: int = No
152152
if source_port:
153153
udpClient.bind_port(source_port)
154154
udpClient.settimeout(protocol._timeout)
155-
155+
156+
if protocol._allow_broadcast:
157+
local_addr = ('0.0.0.0', source_port if source_port else 0)
158+
remote_addr = None
159+
sendto_addr = (protocol._host, protocol._port)
160+
else:
161+
local_addr = ('0.0.0.0', source_port) if source_port else None
162+
remote_addr = (protocol._host, protocol._port)
163+
sendto_addr = None
164+
156165
loop = asyncio.get_running_loop()
157166
transport, protocol_instance = await loop.create_datagram_endpoint(
158167
lambda: Socket.Protocol(protocol._timeout), # Use public Protocol class
159-
local_addr=('0.0.0.0', source_port if source_port else 0),
168+
local_addr=local_addr,
169+
remote_addr=remote_addr,
160170
allow_broadcast=protocol._allow_broadcast
161171
)
162-
172+
163173
try:
164-
transport.sendto(data, (protocol._host, protocol._port))
174+
transport.sendto(data, sendto_addr)
165175
return await protocol_instance.recv()
166176
finally:
167177
transport.close()
@@ -194,4 +204,4 @@ async def test_socket_async():
194204

195205
loop = asyncio.get_event_loop()
196206
loop.run_until_complete(test_socket_async())
197-
loop.close()
207+
loop.close()

0 commit comments

Comments
 (0)