IPv6 connectivity test in C++

2019-07-15 16:30发布

Is there any C++ function to test whether the computer on which my program is running supports IPv6 connectivity?

I need to know something like result of this page http://test-ipv6.com/, but in C++.

EDIT: I was trying to use getifaddrs(). But return value can be AF_INET6 found, because I really have IPv6 address set, but IPv6 connectivity does not work (probably because ISP). So is there some way how to check if IPv6 connectivity works indeed?

1条回答
叛逆
2楼-- · 2019-07-15 17:00

All modern operating systems support IPv6, so you don't have to worry about whether the protocol is available. (If it isn't, your program won't compile anyway.)

But the only way to determine if the computer actually has working IPv6 connectivity is to attempt an IPv6 connection.

This is actually what Microsoft does with their network status indicator in Windows. They attempt to load a URL from an IPv6-only web server and a URL from an IPv4-only web server. If both of these fail, you get the yellow triangle with exclamation point over the network icon in the system tray.

Since I needed a similar functionality, but I also needed the actual IPv6 addresses, I created my own public service which includes this functionality (and several other things). So I have an IPv6-only URL which returns the public IPv6 address, and an IPv4-only URL which returns the public IPv4 address.

Because one of the URLs is only accessible via IPv6, and the other is only accessible via IPv4, you can independently verify connectivity for each protocol.

Either way, if your attempt to fetch the URL fails, you can examine the returned error to determine what the problem might be.

查看更多
登录 后发表回答