Uniy iOS兼容ipv6

苹果的规定:2016年6月1日提交到App Store必须支持IPv6-only网
iOS 9.0、OS X 10.11 以上的系统 在IPv6的环境下是支持IP地址访问网络的。所以大家测试机如果是 iOS9.0以上的系统,可以直接通过IP访问。这是因为iOS 9.0之后 NSURLSession和CFNetwork能把IPv4的地址 合成IPv6的地址(在DNS64/NAT64网络环境中)。
iOS 9.0以下的系统 就会报错

objective-c接口

#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>
#include <string.h>

extern "C" const char* AppMiscGetIPv6(const char *mHost,const char *mPrt)
{
	if( nil == mHost )
		return NULL;
	const char *newChar = "No";
	const char *cause = NULL;
	struct addrinfo* res0;
	struct addrinfo hints;
	struct addrinfo* res;
	int n, s;
	
	memset(&hints, 0, sizeof(hints));
	
	hints.ai_flags = AI_DEFAULT;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	
	if((n=getaddrinfo(mHost, "http", &hints, &res0))!=0)
	{
		printf("getaddrinfo error: %s\n",gai_strerror(n));
		return NULL;
	}
    
	
	struct sockaddr_in6* addr6;
	struct sockaddr_in* addr;
	char ipbuf[128];
    bool isipv6 = false;
	s = -1;
	for(res = res0; res; res = res->ai_next)
	{
		if (res->ai_family == AF_INET6)
		{
			addr6 =( struct sockaddr_in6*)res->ai_addr;
			newChar = inet_ntop(AF_INET6, &addr6->sin6_addr, ipbuf, sizeof(ipbuf));
            isipv6 = true;
		}
		else
		{
			addr =( struct sockaddr_in*)res->ai_addr;
			newChar = inet_ntop(AF_INET, &addr->sin_addr, sizeof(ipbuf));
            isipv6 = false;
        }
		break;
	}
	
	freeaddrinfo(res0);
    
    int len = strlen(newChar);
    char* sbuff = (char*)malloc(len +3+1);
    sbuff[0] = 'v';
    sbuff[1] = isipv6 ? '6':'4';
    sbuff[2] = ';';
    int i = 0;
    len++;
    for(i = 0; i < len; i++)
    {
        sbuff[i+3] = newChar[i];
    }
	return sbuff;
}

c#接口

using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Net.Sockets;

public static class AppMiscIOS
{
	public static void IPv6GetIPType(string serverIp, string serverPorts, out string outServerIp, out AddressFamily outIPType)
	{
		outIPType = AddressFamily.InterNetwork;
		outServerIp = serverIp;
		try
		{
			string newAddr = GetIPv6(serverIp, serverPorts);
			if (!string.IsNullOrEmpty(newAddr) && newAddr.Length > 4)
			{
				if (newAddr[0] == 'v' && newAddr[2] == ';')
				{
					if (newAddr[1] =='6')
					{
						outServerIp = newAddr.Substring(3);
						outIPType = AddressFamily.InterNetworkV6;
					}else if (newAddr[1] == '4'){
						outServerIp = newAddr.Substring(3);
					}
				}
			}
		}
		catch (Exception e)
		{
			Debug.LogError("IPv6_GetIPType error:" + e);
		}
	}

	[DllImport("__Internal")]
	private static extern string AppMiscGetIPv6(string host, string port);

	private static string GetIPv6(string host, string port)
	{
		#if UNITY_IOS && !UNITY_EDITOR
			return  AppMiscGetIPv6(host, port);
		#else
			return "v4;"+host;
		#endif
	}
}

使用例子(注意下面例子只是示范,具体有些变量的声明和方法没有写明,仅供参考)

public bool Connect(string strHost, int port)
{
	try
	{
		CloseSocket();
	#if UNITY_IOS
		var newHost = strHost;
		var address_family = AddressFamily.InterNetwork;
		// 调用接口兼容ipv6
		AppMiscIOS.IPv6GetIPType(strHost, port.ToString(), out newHost, out address_family);
		m_socket = new Socket(address_family, SocketType.Stream, ProtocolType.Tcp);
		m_socket.SendTimeout = 500;

		m_iAsyncResult = m_socket.BeginConnect(newHost, port, null, m_socket);
		Debug.Log("connect: " + newHost + " port: " + port + " handle: " + m_socket.Handle + " ipv"+(address_family == AddressFamily.InterNetworkV6 ? "6" : "4"));
	#else
		m_socket = new Socket(AddressFamily.InterNetwork, ProtocolType.Tcp);
		m_socket.SendTimeout = 500;
		m_iAsyncResult = m_socket.BeginConnect(strHost, m_socket);
		Debug.Log("connect: " + strHost + " port: " + port + " handle: " + m_socket.Handle);
	#endif
		AddNetState(NetState.ConnectStart, null);
	}
	catch (Exception exception)
	{
		CloseSocket();
		m_bConnect = false;
		Debug.LogError("Connect Exception:" + exception.ToString());
		AddNetState(NetState.ConnectFail, null);
	}
	return m_bConnect;
}

相关链接
《IOS解决ipv6问题》:https://www.jianshu.com/p/10ef0d568251

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...