fix bad network lib broadcast implementation

This commit is contained in:
AlexandreRouma 2024-01-25 21:34:13 +01:00
parent 3aa167701e
commit 68bf2fc16f
2 changed files with 19 additions and 30 deletions

View File

@ -109,11 +109,6 @@ namespace net {
addr.sin_port = htons(port); addr.sin_port = htons(port);
} }
bool Address::isMulticast() const {
IP_t ip = getIP();
return (ip >> 28) == 0b1110;
}
// === Socket functions === // === Socket functions ===
Socket::Socket(SockHandle_t sock, const Address* raddr) { Socket::Socket(SockHandle_t sock, const Address* raddr) {
@ -380,7 +375,7 @@ namespace net {
return connect(Address(host, port)); 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 library if needed
init(); init();
@ -388,12 +383,12 @@ namespace net {
SockHandle_t s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); SockHandle_t s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// If the remote address is multicast, allow multicast connections // If the remote address is multicast, allow multicast connections
#ifdef _WIN32 #ifdef _WIN32
const char enable = raddr.isMulticast(); const char enable = allowBroadcast;
#else #else
int enable = raddr.isMulticast(); int enable = allowBroadcast;
#endif #endif
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) { if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(int)) < 0) {
closeSocket(s); closeSocket(s);
throw std::runtime_error("Could not configure socket"); throw std::runtime_error("Could not configure socket");
return NULL; return NULL;
@ -410,15 +405,15 @@ namespace net {
return std::make_shared<Socket>(s, &raddr); return std::make_shared<Socket>(s, &raddr);
} }
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) {
return openudp(Address(rhost, rport), laddr); return openudp(Address(rhost, rport), laddr, allowBroadcast);
} }
std::shared_ptr<Socket> openudp(const Address& raddr, std::string lhost, int lport) { std::shared_ptr<Socket> openudp(const Address& raddr, std::string lhost, int lport, bool allowBroadcast) {
return openudp(raddr, Address(lhost, lport)); return openudp(raddr, Address(lhost, lport), allowBroadcast);
} }
std::shared_ptr<Socket> openudp(std::string rhost, int rport, std::string lhost, int 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)); return openudp(Address(rhost, rport), Address(lhost, lport), allowBroadcast);
} }
} }

View File

@ -93,12 +93,6 @@ namespace net {
*/ */
void setPort(int port); void setPort(int port);
/**
* Check if the address is multicast.
* @return True if multicast, false if not.
*/
bool isMulticast() const;
struct sockaddr_in addr; struct sockaddr_in addr;
}; };
@ -256,7 +250,7 @@ namespace net {
* @param laddr Local address to bind the socket to. * @param laddr Local address to bind the socket to.
* @return Socket instance on success, Throws runtime_error otherwise. * @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. * Create UDP socket.
@ -265,24 +259,24 @@ namespace net {
* @param laddr Local address to bind the socket to. * @param laddr Local address to bind the socket to.
* @return Socket instance on success, Throws runtime_error otherwise. * @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. * 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 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). * @param lpost Local port used to bind the socket to (optional, 0 to allocate automatically).
* @return Socket instance on success, Throws runtime_error otherwise. * @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. * 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 rport Remote port.
* @param lhost Local hostname or IP used to bind the socket (optional, "0.0.0.0" for Any). * @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). * @param lpost Local port used to bind the socket to (optional, 0 to allocate automatically).
* @return Socket instance on success, Throws runtime_error otherwise. * @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);
} }