Radius 协议中的ipv6 处理算法思路
2010-09-30 21:00:37 来源:WEB开发网我们姑且不管如何到byte[]数组,毕竟他的存储范围是有限的,暂且在这篇文章中使用string数组进行存储。分割好的IPv6地址。说到IPV6的地址格式,对于不是很清楚的童鞋们,我简单的进行阐述。
算法输出结果预估,先写出来大家对算法目的看的更清楚
Input : n:n:n:n:n:n:n:n。
OutPut:new string[8] {"n",....."n"} 直接输出
Input:3FFE:FFFF::8:800:20C4:0 ::1
OutPut:{"3FFE","FFFF","0","0",...."20C4","0"} 补零输出 {"0","0",......"1"}
Input:n:n:n:n:n:n:d.d.d.d
OutPut: {"n","n"....."d.d->16位16进制","d.d->16位16进制"}
Input: n:n::d.d.d.d
Output: {"n","n","0","0",...."d.d->16位16进制"}
算法部分:对于此种的ipv6地址,就算是当他最标准的吧,从头到尾都是:分割,那么,直接使用split函数以:分割即可,得到一个长度为8的数组。
对于有(::)在地址里面的,我们首先使用::将IP地址分成前后两个部分,之后计算前后两部分的长度,填充中间的省略的0的个数,返回冒号十六进制的形式IPV6标准数组。
对于混合式的IPv6地址比较复杂,因为如果单纯的是前96位:表示的IPv6地址加上后面的32位IPv4地址,也好处理,将后面的部分以'.' 分割之后,变换成连个16位的ipv6即可。但是考虑到前面会有省略的一部分0的,也就是存在::形式的IPv6地址,那么就要参考压缩式的处理方式先对::进行分割后,在对存在'.'的ipv4混合地址进行处理。
附上代码,没来得及优化,测试几个还行。比较乱,有几个罗嗦的地方还需要改进。
public static string[] IPAddressToArray(string address)
{
string[] result = new string[8];
//address = string.Empty;
string[] ip = null;
string[] iptemp = null;
string[] iptempv4 = null;
string[] ipresult = new string[8];
//address = "5::2:10:20";
string[] a = { "::" };
if (address.Contains(':')) // ipv6 must contain ':'
{
if (address.Contains("::")) // for the ipv6 omit some zero (0) in the address
{
ip = address.Split(a, StringSplitOptions.None);
iptemp = ip[0].Split(':');
iptempv4 = ip[1].Split(':');
int iptempLength = 0;
if (iptemp[0] != string.Empty)
{
iptempLength = iptemp.Length;
}
int iptempv4Length = iptempv4.Length;
int j = 0;
int n = 0;
int k = 8 - iptempLength - iptempv4Length;
bool containsPoint = false;
foreach (string ipa in iptempv4)
{
if (ipa.Contains('.'))
{
containsPoint = true;
}
}
if (!containsPoint) // ipv6 type not contain extended ipv4 (e.g. fec0:0:0:1::1234)
{
for (int i = 0; i < 8; i++)
{
while (iptempLength != 0 && j < iptempLength)
{
ipresult[i] = iptemp[j];
i++;
j++;
}
while (k != 0)
{
ipresult[i] = "0";
i++;
k--;
}
int m = 0;
while ((8 - i) != 0)
{
ipresult[i] = iptempv4[m];
i++;
m++;
}
}
}
else // incluede extended ipv4 in the address (e.g. 0:0:0:0:0:0:10.1.2.3 ===== ::10.1.2.3)
{
int i =0;
while(i<8)
{
while (iptempLength != 0 && j < iptempLength)
{
ipresult[i] = iptemp[j];
i++;
j++;
}
while (k - 1 != 0)
{
ipresult[i] = "0";
i++;
k--;
}
while (iptempv4Length != 0 && !iptempv4[n].Contains ('.'))
{
iptempv4Length--;
ipresult[i] = iptempv4[n];
n++;
}
ipresult[i] = MergeIpv4ToV6Type(iptempv4[iptempv4.Length - 1])[0];
ipresult[++i] = MergeIpv4ToV6Type(iptempv4[iptemp.Length - 1])[1];
i++;
}
result = ipresult;
}
}
else
{
//no contains ipv6 means it do not ignore some zero in address (x:x:x:x:x:x:x:x or x:x:x:x:x:x:1.2.3.4)
iptemp = address.Split(':');
int iptempLength = iptemp.Length;
int m = 0;
for (int i = 0; i < iptemp.Length; i++)
{
while (iptempLength != 0 && !iptemp[i].Contains('.'))
{
ipresult[m] = iptemp[i];
m++;
i++;
iptempLength--;
}
if (iptemp[i].Contains('.') && iptempLength !=0)
{
ipresult[6] = MergeIpv4ToV6Type(iptemp[iptemp.Length - 1])[0];
ipresult[7] = MergeIpv4ToV6Type(iptemp[iptemp.Length - 1])[1];
}
}
result = ipresult;
}
}
else // ipv4 split it directly
{
result = address.Split('.');
}
return result;
}
public static string[] MergeIpv4ToV6Type(string ipv4)
{
string[] ipresult = new string[2];
string[] ipv4Array = ipv4.Split('.');
ipresult[0] = Convert.ToString((Convert.ToInt32(ipv4Array[0]) + Convert.ToInt32(ipv4Array[1])), 16);
ipresult[1] = Convert.ToString((Convert.ToInt32(ipv4Array[2] )+ Convert.ToInt32(ipv4Array[3])), 16);
return ipresult;
}
更多精彩
赞助商链接