I'm trying to implement a simple proprietary discovery protocol using socat
. The discovery is done by sending a UDP broadcast to a well-defined port with a small payload, then listening to "replies" from these devices on my network.
This works if I use bidirectional socat
and "responses" go to stdout:
echo -ne "\x00\x01\x00\xF6" | socat -t5 - udp-datagram:255.255.255.255:30718,broadcast |xxd -p
Example responses from a few devices on my local network (each line is a response from a different device):
000000f70020300258366d112c15000062a71b21ff0000000080a3d2ded9
000000f70020300258366d112c15000062a71b21ff0000000080a3a40670
000000f70020300258366d112c15000062a71b21ff0000000080a3b94ca0
000000f70020300258366d112c15000062a71b21ff0000000080a3a4046b
The payloads are what I expect, however what I'm missing is the sender metadata, specifically the sender's IP that I would get if I used ip-pktinfo,fork SYSTEM:
. So what I want is to send the initial broadcast from stdin but use SYSTEM
to handle the resulting packets coming back.
I've tried a few variations of -u
unidirectional mode but I don't seem to receive data on my listener:
# Listener:
socat -u udp-recvfrom:30222,reuseaddr,ip-pktinfo,fork SYSTEM:./test.sh &
# Broadcast:
echo -ne "\x00\x01\x00\xF6" | socat -u - udp-datagram:255.255.255.255:30222,sourceport=30222,broadcast,reuseaddr
Note from "this end" the source port may be random so I'm explicitly choosing 30222
so my broadcast source port will match my response listener. e.g. if the broadcast comes from port 9987 clients will send their unicast response back to port 9987. If I run the unidirectional broadcast this way, I will occasionally get a response; I have a feeling it depends on how quickly the broadcast process quits.
(I intend to check packet captures next; I'm testing on a remote machine and I have to refresh my memory on tcpdump
first.)
References:
reuseport
option; I think I expectedreuseaddr
to have the same effect which obv it did not. Thanks so much for your thorough explanation! – thom_nic Dec 10 '20 at 19:36