开发者

IPv6 to IPv4 inter-conversion in C#

I have a list of IP's in IPv4 format that i have collected from previ开发者_StackOverflow中文版ous HttpRequest objects that my web-server received. I have a java applet which gives IP addresses in Ipv6 format(java applet returns a bunch of data along with the IP address). I need to check if the IP returned by the applet matches any item from list.

How do inter-convert IPv6 and IPv4 formats?

My web application is built on ASP.Net C#

EDIT

here is the problem that i am facing. The Java applet returns a bunch of IP's from the client which i assume depends on the number of network cards the client machine has.

Along with this i get a IP address from the ASP.Net HTTPRequest object. I need to find out which IP was actually used to connect to my website and carryout some tasks.

The problem is sometimes i get only one IP from the javaapplet in IPv6 format where as the IP i get from the HTTPRequest object is in IPv4 format so the comparison fails.


You are talking about IPv4-IPv6 mapping or IPv4 address embedding.

IPv4 address embedding is used to create a relationship between an IPv4 address and an IPv6 address to aid in the transition from IPv4 to IPv6. One type, the IPv4-compatible IPv6 address, is used for devices that are compatible with both IPv4 and IPv6; it begins with 96 zero bits. The other, the IPv4-mapped address, is used for mapping IPv4 devices that are not compatible with IPv6 into the IPv6 address space; it begins with 80 zeroes followed by 16 ones.

..

The difference between these two kinds of addresses is subtle, but important. The first 80 bits are always zero, so when this is seen you know it is an embedded IPv4 address of some sort. IPv4-compatible IPv6 addresses are only used for devices that are actually IPv6-aware; the IPv4-compatible address is in addition to its conventional IPv6 address. In contrast, if the "FFFF" is seen for the 16 bits after the initial 80, this designates a conventional IPv4 devices whose IPv4 address has been mapped into the IPv6 format. It is not an IPv6-capable device.

So the bottom line is unless they are mapped together, you won't be able to convert to/from. You'd need a translator.


0A0D already seemed to answer this, but I'll give it a shot:

If the first 10 bytes (80 bits) of your IPv6 address are zero, then you can compare the last 4 bytes with the 4 bytes of an IPv4 address.

bool AreEquivalent(IPAddress ip6addr, IPAddress ip4addr)
{
  byte[] ip6bytes = ip6addr.GetBytes();
  byte[] ip4bytes = ip4addr.GetBytes();
  for (int i = 0; i < 10; i++)
  {
     if (ip6bytes[i] != 0)
       return false;
  }
  for (int i = 0; i < 4; i++)
  {
     if (ip6bytes[i + 12] != ip4bytes[i])
        return false;
  }
  return true;
}

This assumes that you already know what is ipv6 and ipv4. Add extra logic as appropriate if you need to compare arbitrary addresses.


Assume you working with some kind of wireless platform or has the capability to do wireless. IPV6 has to do with improvement on mobile networking AKA "Wireless". Converting a IPV6 to a IPV4 is not what you are looking for. IPV6 is a real address. It is the new Advanced standard for IP Addresses these days. Take a look Here and decide for yourself. You can also do it manually here. IPV6 is supposed to mask your IPV4.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜