c语言兼容ipv4 ipv6的结构体,C++ IPv4与IPv6的兼容编码

本文介绍了如何在iOS和OSX中利用getaddrinfo函数将IPv4地址转换为IPv6地址,特别是在不支持IPv4但支持IPv6、NAT64和DNS64的网络环境下。通过示例代码展示了将IPv4地址转换为IPv6并尝试建立连接的过程,同时强调了检查和消除IPv4特定API的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

structaddrinfo 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

#include

#include

#include

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值