C++ IPv4与IPv6的兼容编码

原创 2016年05月30日 18:47:24

 struct addrinfo hints, *res, *ressave;      

bzero(&hints, sizeof(hints));    

hints.ai_family = AF_UNSPEC;  

 hints.ai_socktype = SOCK_STREAM;  

hints.ai_protocol = IPPROTO_IP;  



官网:

https://developer.apple.com/library/mac/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-SW1



Check Source Code for IPv6 DNS64/NAT64 Incompatibilities

Check for and eliminate IPv4-specific APIs, such as:

  • inet_addr()

  • inet_aton()

  • inet_lnaof()

  • inet_makeaddr()

  • inet_netof()

  • inet_network()

  • inet_ntoa()

  • inet_ntoa_r()

  • bindresvport()

  • getipv4sourcefilter()

  • setipv4sourcefilter()

If your code handles IPv4 types, make sure the IPv6 equivalents are handled too.

IPv4

IPv6

AF_INET

AF_INET6

PF_INET

PF_INET6

struct in_addr

struct in_addr6

struct sockaddr_in

struct sockaddr_in6

kDNSServiceProtocol_IPv4

kDNSServiceProtocol_IPv6

Use System APIs to Synthesize IPv6 Addresses

If your app needs to connect to an IPv4-only server without a DNS hostname, use getaddrinfo to resolve the IPv4 address literal. If the current network interface doesn’t support IPv4, but supports IPv6, NAT64, and DNS64, performing this task will result in a synthesized IPv6 address.

Listing 10-1 shows how to resolve an IPv4 literal using getaddrinfo. Assuming you have an IPv4 address stored in memory as four bytes (such as {192, 0, 2, 1}), this example code converts it to a string (such as "192.0.2.1"), uses getaddrinfo to synthesize an IPv6 address (such as a struct sockaddr_in6 containing the IPv6 address "64:ff9b::192.0.2.1") and tries to connect to that IPv6 address.

Listing 10-1  Using getaddrinfo to resolve an IPv4 address literal

#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>
 
    uint8_t ipv4[4] = {192, 0, 2, 1};
    struct addrinfo hints, *res, *res0;
    int error, s;
    const char *cause = NULL;
 
    char ipv4_str_buf[INET_ADDRSTRLEN] = { 0 };
    const char *ipv4_str = inet_ntop(AF_INET, &ipv4, ipv4_str_buf, sizeof(ipv4_str_buf));
 
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_DEFAULT;
    error = getaddrinfo(ipv4_str, "http", &hints, &res0);
    if (error) {
        errx(1, "%s", gai_strerror(error));
        /*NOTREACHED*/
    }
    s = -1;
    for (res = res0; res; res = res->ai_next) {
        s = socket(res->ai_family, res->ai_socktype,
                   res->ai_protocol);
        if (s < 0) {
            cause = "socket";
            continue;
        }
 
        if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
            cause = "connect";
            close(s);
            s = -1;
            continue;
        }
 
        break;  /* okay we got one */
    }
    if (s < 0) {
        err(1, "%s", cause);
        /*NOTREACHED*/
    }
    freeaddrinfo(res0);

Note: The ability to synthesize IPv6 addresses was added to getaddrinfo in iOS 9.2 and OS X 10.11.2. However, leveraging it does not break compatibility with older system versions. See getaddrinfo(3) Mac OS X Developer Tools Manual Page.


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bravegogo/article/details/51540235

C++基础

-
  • 1970年01月01日 08:00

C++中的IPv6网络程序

IPv4 最初是由美国国防部开发的用于网际互联(IP)协议,后来它不仅发展了TCP,而且还进一步发展了IPv4(IP 协议4.0版)。IPv4现在已经广泛应用于Internet网络中,同时也应用于大多...
  • Shakespearer
  • Shakespearer
  • 2017-07-14 11:00:58
  • 569

socket兼容IPV4、IPV6网络底层connect写法

由于IOS苹果审核需要支持ipv6,我公司手游用的socket底层调用的是C标准库,故在此记录下需要调整修改的地方 这里贴出主要的Connect方法,Connect能连接上了,后面send,recv都...
  • XunMengdQingMao
  • XunMengdQingMao
  • 2016-06-07 15:02:41
  • 6463

适配IPv6遇到的坑,终极解决方案

6.1AppStore发布之后的应用要兼容ipv6。 然而我上个星期发布的版本并没有因为没兼容而被拒,这次提交却遇到ipv6兼容问题。。。  话不多说,上代码: +(NSString *) get...
  • u010069091
  • u010069091
  • 2016-07-15 11:36:48
  • 26279

IOS支持IPv6 DNS64/NAT64网络

从2016年6月1日起,iOS应用必须支持IPv6,否则审核将被拒。详见Supporting IPv6 DNS64/NAT64 Networks。本文是翻译稿。从本文中可以学到有关IPv6过度时期的...
  • a997930294
  • a997930294
  • 2016-07-04 17:35:49
  • 3901

NAT64与DNS64

转自:http://blog.sina.com.cn/s/blog_626e838c0101hdyn.html 技术背景: 1.NAT64与DNS64背景 在IPv6网络的发展过程中...
  • zhenganzhong_csdn
  • zhenganzhong_csdn
  • 2016-08-25 17:34:47
  • 1537

c/c++ ipv4 to ipv6 support

(1)struct sockaddr_in 替换为addrinfo addrinfo.ai_family 设置为AF_UNSPEC addrinfo.ai_socktype 设置为SOCK_STREA...
  • jackboos
  • jackboos
  • 2015-03-10 17:04:39
  • 517

编写C++函数:识别一段string字符串是IPv4还是IPv6

今天做到Calix(南京凯易迅)的笔试题,其中有一题大致意思是:vector中存有string字符串,识别每一个字符串是否是ip地址,三种可能:IPv4、IPv6、Neither,将每个字符串的识别结...
  • Mind_V
  • Mind_V
  • 2017-05-15 16:06:08
  • 1188

c++ 封装ipv6类

一. socket编程函数 1.1   基本函数     socket、bind、listen、connect、read/send、write/recv、close函数。 1.2 信息获取函数  ...
  • u012654882
  • u012654882
  • 2016-07-01 18:30:46
  • 1086

IPV4与IPV6兼容的socket编程

  • 2013年03月26日 14:35
  • 467KB
  • 下载
收藏助手
不良信息举报
您举报文章:C++ IPv4与IPv6的兼容编码
举报原因:
原因补充:

(最多只允许输入30个字)