NetCat ignoring (not sending) network data to broadcast addresses

If I run these NetCat below commands (no error messages at all) during a WireShark capture running (capture filter= udp dst port 4000) :

luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 4000
luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 4000
luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 4000
luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 4000

This is the captured data (note the two missing netcat orders) :

Missing supposedly sent data.

As I conclude, any data sent to a broadcast (x.x.x.255) network address is ignored (without errors 😛 ) by NetCat.

Why is this happening?

Additional data:

This is my full ifconfig :

luis@Zarzamoro:~$ sudo ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:ef:bb:aa
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::ba27:ebff:feef:bbaa/64 Scope:Link
          RX packets:9672 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8567 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:503027 (503.0 KB)  TX bytes:5993557 (5.9 MB)

lo        Link encap:Local Loopback
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:153 errors:0 dropped:0 overruns:0 frame:0
          TX packets:153 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:10926 (10.9 KB)  TX bytes:10926 (10.9 KB)

Running Ubuntu 14.04.

As a workaround for now, I am using socat:

echo "Hello" | socat - UDP-DATAGRAM:,broadcast

but I would anyway like to understand why NetCat is not capable of doing the same.

I assume you are using netcat-openbsd because you specify -4. It has -b to enable broadcast address, but it’s known that UDP broadcast is not supported by this version of netcat even with -b. Debian Bug#702204 suggests a patch to fix that.

You can install an alternative package netcat-traditional which seems to correctly support -b with UDP. Note that which netcat to be invoked by running /bin/nc is configurable using update-alternatives when both packages are installed in your system.

$ sudo update-alternatives --config nc
There are 2 choices for the alternative nc (providing /bin/nc).

  Selection    Path                 Priority   Status
* 0            /bin/nc.openbsd       50        auto mode
  1            /bin/nc.openbsd       50        manual mode
  2            /bin/nc.traditional   10        manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /bin/nc.traditional to provide /bin/nc (nc) in manual mode

Finally you might want to add -v to get more verbose diagnostics from nc.

Answered By: yaegashi
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.