enumerating ipv4 and ipv6 address of my cards using boost asio

南楼画角 提交于 2019-12-04 17:36:43

问题


I am trying to enumerate ipv4 and ipv6 addresses of all the network cards(I have 2 cards) my pc.

I am using the following code to do that.

using boost::asio::ip::tcp;
boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(boost::asio::ip::host_name(),"");
    tcp::resolver::iterator it=resolver.resolve(query);

    while(it!=tcp::resolver::iterator())
    {
        boost::asio::ip::address addr=(it++)->endpoint().address();
        if(addr.is_v6())
        {
            std::cout<<"ipv6 address: ";
        }
        else
            std::cout<<"ipv4 address: ";

        std::cout<<addr.to_string()<<std::endl;

    }

The code displays correct ipv4 addresses but not ipv6. Here is the output

ipv6 address: ::1
ipv4 address: 192.168.10.200
ipv4 address: 192.168.10.236

I have very minimum knowledge of ipv6. When I list the information about network interface using ipconfig/all I see that the actual ipv6 addresses are

fe80::226:5aff:fe14:5687%5 
fe80::225:64ff:feb2:4f61%4

Can someone please guide me how to list the ipv6 addresses. Thanks.


回答1:


If the platform is Windows 7 SP1 the link-local interfaces are being skipped as they are tagged "SkipAsSource" by Windows which means that getaddrinfo will not return them and hence neither will Boost.

You can try to inspect the flag with the following command:

netsh int ipv6 show addresses level=verbose

Address fe80::e0:0:0:0%14 Parameters
---------------------------------------------------------
Interface Luid     : Teredo Tunneling Pseudo-Interface
Scope Id           : 0.14
Valid Lifetime     : infinite
Preferred Lifetime : infinite
DAD State          : Deprecated
Address Type       : Other
Skip as Source     : **true**


来源:https://stackoverflow.com/questions/6327210/enumerating-ipv4-and-ipv6-address-of-my-cards-using-boost-asio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!