简体   繁体   中英

Boost.Asio IPv6 Why bind error?

I want to use IPv6 using boost asio in Linux (fedora).

NIC is

ifconfig -a
em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.16.109  netmask 255.255.255.0  broadcast 172.16.16.255
        inet6 fe80::215:17ff:fe62:d168  prefixlen 64  scopeid 0x20<link>
        ether 00:15:17:62:d1:68  txqueuelen 1000  (Ethernet)
        RX packets 59516986  bytes 7105720351 (6.6 GiB)
        RX errors 0  dropped 5015310  overruns 0  frame 0
        TX packets 8680244  bytes 1666346667 (1.5 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 18  memory 0xb8820000-b8840000

and IPv6 udp bind code is...

int main(int argc, char* argv[])
{
    try
    {
        boost::asio::io_service io_service;

        const char* ip_address_string = "fe80::215:17ff:fe62:d168";
        // const char* ip_address_string = "::1";  // It's OK
        boost::asio::ip::address my_address = boost::asio::ip::address::from_string(ip_address_string);
        udp::endpoint local_endpoint(my_address, 15060);

        udp my_protocol = udp::v6();
        udp::socket sock(io_service);
        sock.open(my_protocol);
        sock.bind(local_endpoint);

        std::cout << "ip:" << local_endpoint.address().to_string() << std::endl;
        // -*/

    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

Binding of v6 loopback address is ok, but specific ("fe80::215:17ff:fe62:d168") address is binding error.

exception error is "bind: Invalid argument".

Why binding error?

It looks like you might not have permission to access the external network adaptor.

Perhaps (parts of)

  • ipv6 have been disabled (even though the adaptor is obviously capable and configured)
  • /proc is not mounted (are you in a restricted environment, like a chroot jail?);
  • the ip address is actually different - this is a bit of a lame one, since you'll have checked this a bazillion times, but I felt I should at least mention it

Now, try in environments with fewer restrictions (eg outside virtualization containers, as root...).

If this doesn't get you the information you need consider using strace or ltrace to see which syscalls are failing.


Your code is ok, I've tested it to work on Linux and MSVC (substituting my NIC addresses)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM