When the setsockopt () function is used to set the socket options, an option in the so_broadcast line is displayed,
Its level is sol_socket, which allows the process to send broadcast messages
Note that the broadcast is only supported on the "network that supports broadcast message" (such as Ethernet, Token Ring) datainsocket.
Why do programs need to set this option before sending broadcast messages?
This is to prevent some programs from being designed to send broadcast messages because of incorrect input.
For example, a UDP program accepts a destination IP address as a command line parameter, but the user writes this address as a broadcast address.
At this time, the role of this option is shown. Instead, let the UDP program verify whether the user entered a broadcast address.
It is better to let the kernel detect it. If it is a broadcast address, but the so_broadcast option is not set,
An eacces error is returned.
The ip_output.c file in the 4.4bsd-lite version code is as follows:
If (in_broadcast (DST-> sin_addr, IFP )){
...
...
If (flags & ip_allowbroadcast) = 0)
Error = eaccess;
Goto bad;
}
...
...
}
Bad:
M_freem (M0 );
Goto done;