You do not "add" a broadcast address. You direct traffic to the broadcast address of a network.
The broadcast address is an entirely virtual concept, and should not be "added" or otherwise assigned to any host in normal practice.
The exception to the above rule is specifying the broadcast address (using ifconfig broadcast
) which is done in cases where the broadcast address may be nonstandard. Generally if you nave to use the broadcast
option to ifconfig you're doing something strange, and probably wrong...
If you want to send a broadcast to the 127.0.0.0/8 (local/loopback) network you would theoretically simply direct traffic to 127.255.255.255, but if you try doing that you'll find that it DOES NOT work because the loopback device does not support BROADCAST
traffic.
You can check this for yourself by running ifconfig lo0
on your mac, which will produce output like:
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=3<RXCSUM,TXCSUM>
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 127.0.0.1 netmask 0xff000000
nd6 options=1<PERFORMNUD>
Note the lack of BROADCAST
in the flags field.
Configuring the device with a broadcast option (address) specified results in the address being ignored. A BSD or Linux (Debian) box behaves similarly, and I'd expect other platforms do as well, though you may find an odd duck that allows you to set the BROADCAST
flag on the loopback device if you search hard enough.
The logic behind this is discussed in this FreeBSD mailing list post. An alternate reasoning for not allowing broadcast is also discusses in the comments on this Server Fault answer, which proposes the alternative of using MULTICAST
(which is typically supported by the loopback interface).
Specifically regarding your test case: What you are seeing is not expected behavior. I was able to reproduce your behavior, but only in a very narrow set of circumstances.
For a more robust test that shows that broadcasts on the loopback device in fact does not work on Linux try the following test case on a handy Linux machine (my test case: Ubuntu 13.04):
Configure a second address (third, fourth, fifth -- however many you want) on your loopback interface.
Start a netcat instance listening on each loopback address, e.g. --
nc -u -l 127.0.0.1 5555
nc -u -l 127.0.0.2 5555
nc -u -l 127.0.0.3 5555
...etc
Run your broadcast test command using socat
echo TEST | socat - udp-datagram:127.255.255.255:5555,broadcast
You will note that none of your netcat windows receive the message (Expected Behavior as the loopback interface does not advertise support for BROADCAST
in its flags field), however if you address them directly (e.g. echo TEST2 | socat - udp-datagram:127.0.0.2:5555
) they receive the message with no problem.
The "solution" you are asking for does not exist (or at least should not work) on any platform unless the loopback interface supports BROADCAST
(as can be determined by the flags
field in ifconfig) -- The platform(s) you are talking about do not advertise support for broadcasting on the loopback interface, therefore you cannot do what you're asking.