标题:
兼容IPv6代码
[打印本页]
作者:
look_w
时间:
2019-2-18 20:31
标题:
兼容IPv6代码
升级AFNetworking2.X到最新版本
1、将 AFHTTPRequestOperationManager 替换为: AFHTTPSessionManager
2、将 AFHTTPRequestOperation 替换为:NSURLSessionTask
3、如果通过IP访问,或者允许https证书之外的域名访问,需要设置 validatesDomainName 为false:
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.securityPolicy = [AFSecurityPolicy defaultPolicy];
manager.securityPolicy.allowInvalidCertificates = YES; //忽略https证书
manager.securityPolicy.validatesDomainName = NO; //是否验证域名
六、系统兼容IPv6代码
1、下面的方法 只适用于IPv4,不能兼容IPv6:
inet_addr()
inet_aton()
inet_lnaof()
inet_makeaddr()
inet_netof()
inet_network()
inet_ntoa()
inet_ntoa_r()
bindresvport()
getipv4sourcefilter()
setipv4sourcefilter()
2、如果有以下代码处理IPv4类型,要确保IPv6的形式也被处理过:
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
3、使用系统API合成IPv6:
如果你的APP需要连接的服务器 只有IPv4地址,没有域名,可以用 getaddrinfo 来解决。下面的代码,将IPv4地址(如:192.0.2.1) 地址转换为IPv6地址(如:包含 64:ff9b::192.0.2.1 的struct sockaddr_in6 )
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<err.h>
uint8_t ipv4[4] = {192,0,2,1};
structaddrinfo hints, *res, *res0;
interror, s;
constchar*cause =NULL;
charipv4_str_buf[INET_ADDRSTRLEN] = {0};
constchar*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);
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0