asio icon indicating copy to clipboard operation
asio copied to clipboard

Missing outbound_interface creating via address_v6

Open marek22k opened this issue 1 month ago • 3 comments

The code

    const boost::asio::ip::address_v4 addr = endpoint.address().to_v4();
    const boost::asio::ip::multicast::outbound_interface oif(addr);
    this->_socket.set_option(oif);

compiles, but not

    const boost::asio::ip::address_v6 addr = endpoint.address().to_v6();
    const boost::asio::ip::multicast::outbound_interface oif(addr);
    this->_socket.set_option(oif);

It would be nice to have full IPv6-Support.

marek22k avatar Dec 03 '25 23:12 marek22k

Image If you initialize the socket using an IPv4 socket (e.g., udp::v4()), then passing an IPv4 address is correct and will work properly.

However, there is an issue: outbound_interfacesupports IPv6, and my compiler indicates that a boost::asio::ip::address_v6object cannot be directly passed to its constructor. You should initialize it using an interface index (of type unsigned int). Additionally, my Boost library version is 1.8.9. I hope this information is helpful to you.

NotfriendAtAll avatar Dec 11 '25 13:12 NotfriendAtAll

Since my code is primarily intended for Linux, I am now using POSIX's if_nametoindex:

unsigned int PosixWrapper::if_nametoindex(const std::string& ifname)
{
    const unsigned int result = ::if_nametoindex(ifname.c_str());
    if (result == 0)
    {
        throw std::system_error(
            errno, std::generic_category(), "Failed to get interface index.");
    }
    return result;
}

https://codeberg.org/mark22k/mping-sender/src/commit/58a791a9dcfe7cec23fdccb5fba2e3d26d928bfd/src/posix_wrapper.cpp

marek22k avatar Dec 11 '25 14:12 marek22k

Looks good!

NotfriendAtAll avatar Dec 12 '25 05:12 NotfriendAtAll