socat does not honor sourceport option

I am trying to send a udp packet from a specific port:

$ echo hello | socat - UDP-DATAGRAM:192.168.1.255:11111,broadcast,sourceport=22222

But a random port is used instead:

# tcpdump -vvvv -ttttt -nienp0s31f6 udp
tcpdump: listening on enp0s31f6, link-type EN10MB (Ethernet), snapshot length 262144 bytes
 00:00:00.000000 IP (tos 0x0, ttl 64, id 40649, offset 0, flags [DF], proto UDP (17), length 34)
    192.168.1.17.35829 > 192.168.1.255.11111: [udp sum ok] UDP, length 6

How can I send a (broadcast) udp packet from a specific port?

N.B. I also tried sending a unicast udp packet with socat, but sourceport wasn’t honored there either.

Use the bind option instead of sourceport:

$ echo hello | socat - UDP-DATAGRAM:192.168.1.255:11111,broadcast,bind=:22222

I’m not sure why sourceport doesn’t work in this case, since it clearly does for e.g. UDP-CONNECT…but it’s also not clear why there’s a separate sourceport option when bind already does the same thing.

Answered By: larsks
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.