Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libxbot-service/include/xbot-service/Io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Io {

static bool getEndpoint(char* ip, size_t ip_len, uint16_t* port);

static bool start();
static bool start(const char* bind_ip="0.0.0.0");
};
} // namespace xbot::service

Expand Down
3 changes: 2 additions & 1 deletion libxbot-service/include/xbot-service/portable/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ typedef XBOT_SOCKET_TYPEDEF* SocketPtr;
/**
* Creates a socket and returns the pointer to it
* @param bind_multicast: true to prepare the socket for listening
* @param bind_address: the IP address to bind the socket to (default 0.0.0.0)
* @return The created socket
*/
bool initialize(SocketPtr socket, bool bind_multicast);
bool initialize(SocketPtr socket, bool bind_multicast, const char* bind_address="0.0.0.0");

/**
* Frees all socket resources
Expand Down
4 changes: 2 additions & 2 deletions libxbot-service/src/portable/linux/Io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ bool Io::getEndpoint(char* ip, size_t ip_len, uint16_t* port) {
return sock::getEndpoint(&udp_socket_, ip, ip_len, port);
}

bool Io::start() {
if (!sock::initialize(&udp_socket_, false)) {
bool Io::start(const char* bind_ip) {
if (!sock::initialize(&udp_socket_, false, bind_ip)) {
return false;
}
return thread::initialize(&io_thread_, runIo, nullptr, nullptr, 0, IO_THD_NAME);
Expand Down
20 changes: 10 additions & 10 deletions libxbot-service/src/portable/linux/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bool get_ip(char* ip, size_t ip_len) {
return success;
}

bool xbot::service::sock::initialize(SocketPtr socket_ptr, bool bind_multicast) {
bool xbot::service::sock::initialize(SocketPtr socket_ptr, bool bind_multicast, const char* bind_ip) {
*socket_ptr = -1;
// Create a UDP socket

Expand All @@ -86,6 +86,11 @@ bool xbot::service::sock::initialize(SocketPtr socket_ptr, bool bind_multicast)
}
}


sockaddr_in saddr{};
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr(bind_ip);

if (bind_multicast) {
// Allow multiple binaries listening on the same socket.
{
Expand All @@ -107,17 +112,12 @@ bool xbot::service::sock::initialize(SocketPtr socket_ptr, bool bind_multicast)
return false;
}
}

sockaddr_in saddr{};
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inet_addr("0.0.0.0") == INADDR_ANY

saddr.sin_port = htons(config::multicast_port);
}

// Bind to any address (i.e. 0.0.0.0:port)
if (bind(fd, reinterpret_cast<sockaddr*>(&saddr), sizeof(saddr)) < 0) {
close(fd);
return false;
}
if (bind(fd, reinterpret_cast<sockaddr*>(&saddr), sizeof(saddr)) < 0) {
close(fd);
return false;
}

// Create a pointer to the fd and return it.
Expand Down