mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-07-09 18:45:22 +02:00
fix networking library to allow multicast
This commit is contained in:
@ -86,14 +86,14 @@ namespace net {
|
||||
addr.sin_port = htons(port);
|
||||
}
|
||||
|
||||
std::string Address::getIPStr() {
|
||||
std::string Address::getIPStr() const {
|
||||
char buf[128];
|
||||
IP_t ip = getIP();
|
||||
sprintf(buf, "%d.%d.%d.%d", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF);
|
||||
return buf;
|
||||
}
|
||||
|
||||
IP_t Address::getIP() {
|
||||
IP_t Address::getIP() const {
|
||||
return htonl(addr.sin_addr.s_addr);
|
||||
}
|
||||
|
||||
@ -101,7 +101,7 @@ namespace net {
|
||||
addr.sin_addr.s_addr = htonl(ip);
|
||||
}
|
||||
|
||||
int Address::getPort() {
|
||||
int Address::getPort() const {
|
||||
return htons(addr.sin_port);
|
||||
}
|
||||
|
||||
@ -109,6 +109,11 @@ 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) {
|
||||
@ -160,8 +165,8 @@ namespace net {
|
||||
|
||||
// Set timeout
|
||||
timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = timeout * 1000;
|
||||
tv.tv_sec = timeout / 1000;
|
||||
tv.tv_usec = (timeout - tv.tv_sec*1000) * 1000;
|
||||
|
||||
// Wait for data
|
||||
int err = select(sock+1, &set, NULL, &set, (timeout > 0) ? &tv : NULL);
|
||||
@ -225,8 +230,8 @@ namespace net {
|
||||
|
||||
// Define timeout
|
||||
timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = timeout * 1000;
|
||||
tv.tv_sec = timeout / 1000;
|
||||
tv.tv_usec = (timeout - tv.tv_sec*1000) * 1000;
|
||||
|
||||
// Wait for data or error
|
||||
if (timeout != NONBLOCKING) {
|
||||
@ -382,6 +387,18 @@ namespace net {
|
||||
// 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) {
|
||||
closeSocket(s);
|
||||
throw std::runtime_error("Could not configure socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Bind socket to local port
|
||||
if (bind(s, (sockaddr*)&laddr.addr, sizeof(sockaddr_in))) {
|
||||
closeSocket(s);
|
||||
|
Reference in New Issue
Block a user