Skip to content

Commit

Permalink
fix networking library to allow multicast
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreRouma committed Jan 25, 2024
1 parent 97c1a13 commit 3aa1677
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
31 changes: 24 additions & 7 deletions core/src/utils/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,34 @@ 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);
}

void Address::setIP(IP_t ip) {
addr.sin_addr.s_addr = htonl(ip);
}

int Address::getPort() {
int Address::getPort() const {
return htons(addr.sin_port);
}

void Address::setPort(int port) {
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 @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 13 additions & 7 deletions core/src/utils/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ namespace net {
* Get the IP address.
* @return IP address in standard string format.
*/
std::string getIPStr();
std::string getIPStr() const;

/**
* Get the IP address.
* @return IP address in host byte order.
*/
IP_t getIP();
IP_t getIP() const;

/**
* Set the IP address.
Expand All @@ -85,14 +85,20 @@ namespace net {
* Get the TCP/UDP port.
* @return TCP/UDP port number.
*/
int getPort();
int getPort() const;

/**
* Set the TCP/UDP port.
* @param port TCP/UDP port number.
*/
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 @@ -246,15 +252,15 @@ namespace net {

/**
* Create UDP socket.
* @param raddr Remote address.
* @param raddr Remote address. Set to a multicast address to allow multicast.
* @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);

/**
* Create UDP socket.
* @param rhost Remote hostname or IP address.
* @param rhost Remote hostname or IP address. Set to a multicast address to allow multicast.
* @param rport Remote port.
* @param laddr Local address to bind the socket to.
* @return Socket instance on success, Throws runtime_error otherwise.
Expand All @@ -263,7 +269,7 @@ namespace net {

/**
* Create UDP socket.
* @param raddr Remote address.
* @param raddr Remote address. Set to a multicast 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.
Expand All @@ -272,7 +278,7 @@ namespace net {

/**
* Create UDP socket.
* @param rhost Remote hostname or IP address.
* @param rhost Remote hostname or IP address. Set to a multicast 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).
Expand Down

0 comments on commit 3aa1677

Please sign in to comment.