UDP sockets broadcast is now disabled by default.

Add method `set_broadcast_enabled` to allow enabling broadcast via
GDScript.
This commit is contained in:
Fabio Alessandrelli 2019-11-25 17:36:09 +01:00
parent ab1bfb9007
commit 9269d6be32
4 changed files with 29 additions and 4 deletions

View file

@ -37,6 +37,12 @@ void PacketPeerUDP::set_blocking_mode(bool p_enable) {
blocking = p_enable;
}
void PacketPeerUDP::set_broadcast_enabled(bool p_enabled) {
broadcast = p_enabled;
if (_sock.is_valid() && _sock->is_open())
_sock->set_broadcasting_enabled(p_enabled);
}
Error PacketPeerUDP::join_multicast_group(IP_Address p_multi_address, String p_if_name) {
ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
@ -47,6 +53,7 @@ Error PacketPeerUDP::join_multicast_group(IP_Address p_multi_address, String p_i
Error err = _sock->open(NetSocket::TYPE_UDP, ip_type);
ERR_FAIL_COND_V(err != OK, err);
_sock->set_blocking_enabled(false);
_sock->set_broadcasting_enabled(broadcast);
}
return _sock->join_multicast_group(p_multi_address, p_if_name);
}
@ -122,6 +129,7 @@ Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
err = _sock->open(NetSocket::TYPE_UDP, ip_type);
ERR_FAIL_COND_V(err != OK, err);
_sock->set_blocking_enabled(false);
_sock->set_broadcasting_enabled(broadcast);
}
do {
@ -165,6 +173,7 @@ Error PacketPeerUDP::listen(int p_port, const IP_Address &p_bind_address, int p_
_sock->set_blocking_enabled(false);
_sock->set_reuse_address_enabled(true);
_sock->set_broadcasting_enabled(broadcast);
err = _sock->bind(p_bind_address, p_port);
if (err != OK) {
@ -258,6 +267,7 @@ void PacketPeerUDP::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_packet_ip"), &PacketPeerUDP::_get_packet_ip);
ClassDB::bind_method(D_METHOD("get_packet_port"), &PacketPeerUDP::get_packet_port);
ClassDB::bind_method(D_METHOD("set_dest_address", "host", "port"), &PacketPeerUDP::_set_dest_address);
ClassDB::bind_method(D_METHOD("set_broadcast_enabled", "enabled"), &PacketPeerUDP::set_broadcast_enabled);
ClassDB::bind_method(D_METHOD("join_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::join_multicast_group);
ClassDB::bind_method(D_METHOD("leave_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::leave_multicast_group);
}
@ -267,6 +277,7 @@ PacketPeerUDP::PacketPeerUDP() :
queue_count(0),
peer_port(0),
blocking(true),
broadcast(false),
_sock(Ref<NetSocket>(NetSocket::create())) {
rb.resize(16);
}

View file

@ -53,6 +53,7 @@ protected:
IP_Address peer_addr;
int peer_port;
bool blocking;
bool broadcast;
Ref<NetSocket> _sock;
static void _bind_methods();
@ -77,6 +78,7 @@ public:
Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
int get_available_packet_count() const;
int get_max_packet_size() const;
void set_broadcast_enabled(bool p_enabled);
Error join_multicast_group(IP_Address p_multi_address, String p_if_name);
Error leave_multicast_group(IP_Address p_multi_address, String p_if_name);

View file

@ -76,6 +76,15 @@
If [code]bind_address[/code] is set to any valid address (e.g. [code]"192.168.1.101"[/code], [code]"::1"[/code], etc), the peer will only listen on the interface with that addresses (or fail if no interface with the given address exists).
</description>
</method>
<method name="set_broadcast_enabled">
<return type="void">
</return>
<argument index="0" name="enabled" type="bool">
</argument>
<description>
Enable or disable sending of broadcast packets (e.g. [code]set_dest_address("255.255.255.255", 4343)[/code]. This option is disabled by default.
</description>
</method>
<method name="set_dest_address">
<return type="int" enum="Error">
</return>
@ -85,6 +94,7 @@
</argument>
<description>
Sets the destination address and port for sending packets and variables. A hostname will be resolved using DNS if needed.
Note: [method set_broadcast_enabled] must be enabled before sending packets to a broadcast address (e.g. [code]255.255.255.255[/code]).
</description>
</method>
<method name="wait">

View file

@ -333,9 +333,10 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
set_ipv6_only_enabled(ip_type != IP::TYPE_ANY);
}
if (protocol == IPPROTO_UDP && ip_type != IP::TYPE_IPV6) {
// Enable broadcasting for UDP sockets if it's not IPv6 only (IPv6 has no broadcast option).
set_broadcasting_enabled(true);
if (protocol == IPPROTO_UDP) {
// Make sure to disable broadcasting for UDP sockets.
// Depending on the OS, this option might or might not be enabled by default. Let's normalize it.
set_broadcasting_enabled(false);
}
_is_stream = p_sock_type == TYPE_TCP;
@ -606,7 +607,8 @@ Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP
Error NetSocketPosix::set_broadcasting_enabled(bool p_enabled) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
// IPv6 has no broadcast support.
ERR_FAIL_COND_V(_ip_type == IP::TYPE_IPV6, ERR_UNAVAILABLE);
if (_ip_type == IP::TYPE_IPV6)
return ERR_UNAVAILABLE;
int par = p_enabled ? 1 : 0;
if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, SOCK_CBUF(&par), sizeof(int)) != 0) {