Skip to content

Commit

Permalink
fix bad network lib broadcast implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreRouma committed Jan 25, 2024
1 parent 3aa1677 commit 68bf2fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
31 changes: 13 additions & 18 deletions core/src/utils/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ namespace net {
addr.sin_port = htons(port);
}

bool Address::isMulticast() const {
IP_t ip = getIP();
return (ip >> 28) == 0b1110;
}

// === Socket functions ===

Socket::Socket(SockHandle_t sock, const Address* raddr) {
Expand Down Expand Up @@ -380,20 +375,20 @@ namespace net {
return connect(Address(host, port));
}

std::shared_ptr<Socket> openudp(const Address& raddr, const Address& laddr) {
std::shared_ptr<Socket> openudp(const Address& raddr, const Address& laddr, bool allowBroadcast) {
// Init library if needed
init();

// Create socket
SockHandle_t s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

// If the remote address is multicast, allow multicast connections
#ifdef _WIN32
const char enable = raddr.isMulticast();
#else
int enable = raddr.isMulticast();
#endif
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
#ifdef _WIN32
const char enable = allowBroadcast;
#else
int enable = allowBroadcast;
#endif
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(int)) < 0) {
closeSocket(s);
throw std::runtime_error("Could not configure socket");
return NULL;
Expand All @@ -410,15 +405,15 @@ namespace net {
return std::make_shared<Socket>(s, &raddr);
}

std::shared_ptr<Socket> openudp(std::string rhost, int rport, const Address& laddr) {
return openudp(Address(rhost, rport), laddr);
std::shared_ptr<Socket> openudp(std::string rhost, int rport, const Address& laddr, bool allowBroadcast) {
return openudp(Address(rhost, rport), laddr, allowBroadcast);
}

std::shared_ptr<Socket> openudp(const Address& raddr, std::string lhost, int lport) {
return openudp(raddr, Address(lhost, lport));
std::shared_ptr<Socket> openudp(const Address& raddr, std::string lhost, int lport, bool allowBroadcast) {
return openudp(raddr, Address(lhost, lport), allowBroadcast);
}

std::shared_ptr<Socket> openudp(std::string rhost, int rport, std::string lhost, int lport) {
return openudp(Address(rhost, rport), Address(lhost, lport));
std::shared_ptr<Socket> openudp(std::string rhost, int rport, std::string lhost, int lport, bool allowBroadcast) {
return openudp(Address(rhost, rport), Address(lhost, lport), allowBroadcast);
}
}
18 changes: 6 additions & 12 deletions core/src/utils/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ namespace net {
*/
void setPort(int port);

/**
* Check if the address is multicast.
* @return True if multicast, false if not.
*/
bool isMulticast() const;

struct sockaddr_in addr;
};

Expand Down Expand Up @@ -256,7 +250,7 @@ namespace net {
* @param laddr Local address to bind the socket to.
* @return Socket instance on success, Throws runtime_error otherwise.
*/
std::shared_ptr<Socket> openudp(const Address& raddr, const Address& laddr);
std::shared_ptr<Socket> openudp(const Address& raddr, const Address& laddr, bool allowBroadcast = false);

/**
* Create UDP socket.
Expand All @@ -265,24 +259,24 @@ namespace net {
* @param laddr Local address to bind the socket to.
* @return Socket instance on success, Throws runtime_error otherwise.
*/
std::shared_ptr<Socket> openudp(std::string rhost, int rport, const Address& laddr);
std::shared_ptr<Socket> openudp(std::string rhost, int rport, const Address& laddr, bool allowBroadcast = false);

/**
* Create UDP socket.
* @param raddr Remote address. Set to a multicast address to allow multicast.
* @param raddr Remote address. Set to a multicast or broadcast address to allow multicast.
* @param lhost Local hostname or IP used to bind the socket (optional, "0.0.0.0" for Any).
* @param lpost Local port used to bind the socket to (optional, 0 to allocate automatically).
* @return Socket instance on success, Throws runtime_error otherwise.
*/
std::shared_ptr<Socket> openudp(const Address& raddr, std::string lhost = "0.0.0.0", int lport = 0);
std::shared_ptr<Socket> openudp(const Address& raddr, std::string lhost = "0.0.0.0", int lport = 0, bool allowBroadcast = false);

/**
* Create UDP socket.
* @param rhost Remote hostname or IP address. Set to a multicast address to allow multicast.
* @param rhost Remote hostname or IP address. Set to a multicast or broadcast address to allow multicast.
* @param rport Remote port.
* @param lhost Local hostname or IP used to bind the socket (optional, "0.0.0.0" for Any).
* @param lpost Local port used to bind the socket to (optional, 0 to allocate automatically).
* @return Socket instance on success, Throws runtime_error otherwise.
*/
std::shared_ptr<Socket> openudp(std::string rhost, int rport, std::string lhost = "0.0.0.0", int lport = 0);
std::shared_ptr<Socket> openudp(std::string rhost, int rport, std::string lhost = "0.0.0.0", int lport = 0, bool allowBroadcast = false);
}

0 comments on commit 68bf2fc

Please sign in to comment.