简体   繁体   English

问题将ipv6转换为ipv4

[英]Problem Converting ipv6 to ipv4

I have some code in an asp.net app that needsto get the ipv4 address of the client computer (the users are all on our own network). 我在asp.net应用程序中有一些代码需要获取客户端计算机的ipv4地址(用户都在我们自己的网络上)。 Recently we upgraded the server the app runs on to windows 2008 server. 最近我们将应用程序运行的服务器升级到Windows 2008服务器。 Now the Request.UserHostAddress code returns the ipv4 when the client is on an older OS and ipv6 when they are on a newer OS (Vista and higher). 现在,当客户端在较旧的操作系统上时,Request.UserHostAddress代码返回ipv4,而当它们在较新的操作系统(Vista和更高版本)上时,返回ipv6。 So the feature that relys on this works for some clients and not others. 因此,依赖于此的功能适用于某些客户而非其他客户。

I added code that is supposed to convert from ipv6 to ipv4 to try to fix this problem. 我添加了应该从ipv6转换为ipv4的代码,以尝试解决此问题。 It's from this online tutorial: http://www.4guysfromrolla.com/articles/071807-1.aspx .I'm using dsn.GetHostAddress and then looping through the IPs returned looking for one that is "InterNetwork" 这是来自这个在线教程: http ://www.4guysfromrolla.com/articles/071807-1.aspx。我正在使用dsn.GetHostAddress然后循环返回的IP寻找一个“InterNetwork”

foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
{
    if (IPA.AddressFamily.ToString() == "InterNetwork")
    {
        IP4Address = IPA.ToString();
        break;
    }
}

if (IP4Address != String.Empty)
{
    return IP4Address;
}


foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
{
    if (IPA.AddressFamily.ToString() == "InterNetwork")
    {
        IP4Address = IPA.ToString();
        break;
    }
}

return IP4Address;

The problem is that this isn't working for me. 问题是这不适合我。 The clients connecting from ipv4 continue to return the correct ipv4 IP of the client computer, but the clients connecting from Vista and Windows 7 it is returning the ipv4 IP of the SERVER machine not the client computer. 从ipv4连接的客户端继续返回客户端计算机的正确ipv4 IP,但是从Vista和Windows 7连接的客户端返回SERVER计算机的ipv4 IP而不是客户端计算机。

Simple answer: Disable IPV6 on the server, or remove the IPV6 address of the server from the DNS entry. 简单回答:在服务器上禁用IPV6,或从DNS条目中删除服务器的IPV6地址。

There is not a magic IPV4<->IPV6 converter. 没有神奇的IPV4 < - > IPV6转换器。 They're completely different protocols, and addresses in one don't translate to the other. 它们是完全不同的协议,一个地址不转换为另一个。 If you want to reliably retrieve the IPV4 address of the client, you need to make sure that the client connects over IPV4. 如果要可靠地检索客户端的IPV4地址,则需要确保客户端通过IPV4连接。

I also had copied the example code and a colleague pointed out that it was obviously buggy. 我也复制了示例代码,一位同事指出它显然是错误的。 This line uses the host name of the server, hence the incorrect result: 此行使用服务器的主机名,因此结果不正确:

foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))

I have corrected the code in my project as follows: 我更正了项目中的代码如下:

/// <summary>
/// Returns the IPv4 address of the specified host name or IP address.
/// </summary>
/// <param name="sHostNameOrAddress">The host name or IP address to resolve.</param>
/// <returns>The first IPv4 address associated with the specified host name, or null.</returns>
public static string GetIPv4Address(string sHostNameOrAddress)
{
  try
  {
    // Get the list of IP addresses for the specified host
    IPAddress[] aIPHostAddresses = Dns.GetHostAddresses(sHostNameOrAddress);

    // First try to find a real IPV4 address in the list
    foreach (IPAddress ipHost in aIPHostAddresses)
      if (ipHost.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
        return ipHost.ToString();

    // If that didn't work, try to lookup the IPV4 addresses for IPV6 addresses in the list
   foreach (IPAddress ipHost in aIPHostAddresses)
     if (ipHost.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
      {
        IPHostEntry ihe = Dns.GetHostEntry(ipHost);
        foreach (IPAddress ipEntry in ihe.AddressList)
          if (ipEntry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            return ipEntry.ToString();
      }
  }
  catch (Exception ex)
  {
    System.Diagnostics.Trace.WriteLine(ex);
  }
  return null;
}

The code above works in ASP.Net 2.0 on Windows 7/Server 2008. Hope this helps. 上面的代码适用于Windows 7 / Server 2008上的ASP.Net 2.0。希望这会有所帮助。

if you are using .Net 4.5 Framework then there is a method provide to convert IP6 to IP4 如果你使用的是.Net 4.5 Framework,那么有一种方法可以将IP6转换为IP4

public IPAddress MapToIPv4()

You can find the details here 你可以在这里找到详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM