0

Quick question

Is the good practice for getting a broadcast address to use system calls like ioctl() or getifaddrs(), or are they obsolete/useless because of the following two reasons?

  1. I can make a binary OR between the netmask and the IP
  2. The broadcast address is not always set anyway

Details

I am playing with some UDP broadcast code, both on my Arch Linux machine and on some Ubuntu Docker containers.

I see that some interfaces have the broadcast address set, for instance my wifi interface on Arch Linux (from $ ip addr):

inet 192.168.0.11/24 brd 192.168.0.255 scope global wlp4s0

And some don't, for instance the eth0 interface on an Ubuntu Docker container:

inet 172.17.0.5/16 scope global eth0

However, if I send packets to 172.17.255.255, they are actually broadcasted to other machines on the network.

On this question it is said that it is not necessary to set the broadcast address manually. And here, somebody seems to imply that Docker purposely doesn't set the broadcast address, for some reason I don't undertand.

So I get to wonder: what is the right way to detect the broadcast address for a network interface?

  • It seems like you have already answered your question. If the method you use to obtain the broadcast address isn't reliable, then it isn't really a solution at all. Every computer has an IP address and subnet mask. I'm sure there is a reason Linux allows people to configure a broadcast address and network address, but I have no clue what it is, and I'm positive it has nothing to do with networking. – Appleoddity Oct 31 '17 at 01:31
  • You mean that configuring the broadcast interface is not networking? What is it then? sysadmin? I don't undertand why those methods don't give me the right broadcast IP given that actually, a working broadcast IP does exist... So either the methods are buggy, or there is something I need to understand. I want to do things the right way, so I don't want to hack around without understanding what I am doing. – JonasVautherin Oct 31 '17 at 19:57
  • 1
    A broadcast address and network address are CALCULATED from the IP address and netmask. I have no idea why Linux allows to configure a broadcast and network address. Those values always come from a logical AND between IP address and subnet. – Appleoddity Oct 31 '17 at 20:36
  • I think you're right. I'd like to understand why Linux allows to do that then O_o. – JonasVautherin Nov 02 '17 at 12:22

1 Answers1

0

It seems like the broadcast address is somehow an "independent" field in the interface definition (at least on some linux systems I tried), but it should not be used for directed broadcasts, as it could be unset.

If unset, the broadcast address is wrongly read as "0.0.0.0" by commands such as ioctl() and we end up in this situation.

So I would again mention this question and support the idea that the broadcast address should be computed from the ip and the netmask. The broadcast address should merely be considered an indication for outputs of commands such as ip addr or ifconfig.