Returning an IPv4 Address in an IPv6-Enabled Environment


Introduction The Internet is best described as a "network of networks,"and every device which is connected to the Internet is uniquely identified by its Internet Protocol (IP) address. When client browsers connect to websites, they pass along a collection of information which the website can use for various purposes - these are known as the Request Headers, and the IP address of the client machine is included in this information. Although a machine's IP address is not guaranteed to be 100% accurate, many web developers capture it for logging and other purposes.
インターネットを紹介する最良の説明は「ネットワークのネットワーク」であり、接続されたデバイスごとにインターネット規約アドレス(IPアドレス)という唯一の表示がある.クライアントブラウザがウェブサイトに接続されると、要求ヘッダ、クライアントのIPアドレスなど、さまざまな目的で情報が収集されます.IPアドレスの情報を100%正確に取得することは保証できないが,ユーザ登録イベントや他の目的を記録するための開発者が多い.
One challenge in capturing IP addresses is that there are differences in the IP address format between IP version 4 and IP version 6. When the IPv4 protocol is the only protocol enabled on the web server, accessing the IP address in the Request Headers will return the familiar IPv4 address. However, if IPv6 is also enabled, it will take precedence over IPv4. While earlier versions of Windows supported IPv6, Windows Vista is the first to have it enabled by default. Therefore, if you are using Windows Vista as the operating system for your development machine or web server, you may have noticed that the client IP addresses retrieved from the Request Headers are being returned in the IPv4 format.
IPv 4とIPv 6の違いは、IPアドレスを取得する上での課題です.WebサーバにIPv 4プロトコルのみがインストールされている場合は、IPv 4のアドレスしか取得できません.その後、IPv 6プロトコルがインストールされると、IPv 4よりも優先されます.初期のWindowsバージョンではIPv 6もサポートされていましたが、Windows Vistaは最初のデフォルトインストールです.そのため、Windows Vistaを開発機やWebサーバのオペレーティングシステムとして使用すると、クライアントから返されるデータがIPv 4形式であることがわかります(注:実際には、テスト中に返されるアドレスがIPv 6形式であることに気づきました.原文が間違っているのか、翻訳に問題があるのか分かりません).
In this article we will examine a short bit of code that will seamlessly convert the IPv6 address returned by Windows Vista (or other IPv6-enabled environments) into an IPv4 format. This is useful if your database or back-end logic is expecting an IPv4 address. Read on to learn more!
本稿では、Windwos Vista(または他のIPv 6環境)でIPv 6をシームレスにIPv 4に変換するために、小さなコードを使用することを試みます.データベースまたはバックグラウンド操作でIPv 4アドレスが期待される場合は、この文書が役立ちます.
An IP Overview In the early 1980s, version 4 (IPv4) of the Internet Protocol was introduced, and this version is still in use on the vast majority of networks around the world. IPv4 addresses are in the format xxx.xxx.xxx.xxx , e.g. 192.168.1.1. Each of the four elements of an IPv4 address is comprised of a single byte of data, which means that the largest possible IPv4 address is 255.255.255.255. This gives a maximum number of unique IPv4 addresses at just over 4 billion, though approximately 18 million of these are reserved for private networks.
IP概要は1980年代初期に第4版のインターネットプロトコル(IPv 4)が導入されて以来、現在も世界で広く、大量の応用が行われている.IPv 4アドレスのフォーマットはxxxです.xxx.xxx.xxx、例えば192.168.1.1.4つのセグメントの各セグメントは1バイトのデータであり、これは最大のIPv 4アドレスが255.255.255.255.255.255であることを意味する.これにより、40億以上の独立したIPv 4アドレスがありますが、約1,800万以上のアドレスがプライベートネットワークに残されています.
Although processes such as Network Address Translation (NAT) have helped expand the potential pool of IP addresses, with more computers, cell phones, and Internet-connect devices, the limits imposed by IPv4 are beginning to be reached. Therefore, version 6 of Internet Protocol was devised in the mid 1990s. Unlike the 32-bit IPv4 addresses, IPv6 addresses are 128-bit in the format: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx , where each x is a 4-bit hexidecimal digit. This expanded format gives a theoretical maximum number of unique IPv6 addresses of 2128 - this is thought highly unlikely ever to run out.
URL解析(NAT)は、コンピュータ、電話、その他のインターネットデバイスに接続するためにより多くのアドレスを拡張するのに役立つが、IPv 4の限界はすでに共通認識に達し始めているため、1990年代に第6版インターネットプロトコル(IPv 6)が設計された.32ビットのIPv 4アドレスとは異なり、IPv 6アドレスは128ビットであり、そのフォーマットはxxxx:xxxxxx:xxxxxx:xxxxxx:xxxxxx:xxxxxx:xxxxxxxxであり、各xは16進数を表す.このように,理論的には,2の128個の独立したIPv 6アドレスを提供することができ,これ以上使い切ることはほとんど不可能である.
A common method used by ASP.NET developers for retrieving a visitor's IP address from the Request Headers is to use the UserHostAddress property of the HttpRequest object like so:
ASP.NET開発者がアクセス者のIPアドレスを取得するには、HttpRequestオブジェクトのUserHostAddressプロパティを使用する一般的な方法があります. ' VB
Dim ClientIP As String = Request.UserHostAddress


// C#
string ClientIP = Request.UserHostAddress;

When the IPv4 protocol is the only protocol enabled, this will return a familiar IPv4 address. However, if IPv6 is also enabled, it will take precedence over IPv4, and the above code will return an IPv6 address instead. Since the release of the Vista Update patch for Visual Studio.NET 2005 SP1, many ASP.NET developers have started to use Vista as their development platform. Although Microsoft shipped the IPv6 protocol with previous versions of Windows, Vista is the first version where IPv6 is enabled by default. This, of course, means that Request.UserHostAddress no longer works as expected because it is suddenly returning an IPv6 address.
IPv 4プロトコルのみの場合は、よく知られているIPv 4アドレスを取得しますが、IPv 6プロトコルもインストールされている場合は、IPv 4よりも優先され、IPv 6のアドレスが返されます.マイクロソフト社がVistaバージョンのVS 2005 SP 1の更新パッチを発表してから、ますます多くの開発者がVistaを開発プラットフォームとしています.以前のWindowsバージョンではIPv 6がバンドルされていましたが、Vistaは最初のデフォルトインストールであり、Requestを意味します.UserHostAddressが返すデータは、IPv 6のアドレスを返すため、私たちが望んでいるほどではありません.
Solution Although IPv6 can be disabled in Vista, there is thankfully no need to do this, as it is very easy to return an IPv4 address even when IPv6 is enabled. Firstly, add the following code to your ASP.NET project:
ソリューションは、VistaでIPv 6を無効にすることができますが、幸いなことに、IPv 4のアドレスを取得するためにIPv 6環境でより簡単な方法があります.まず、次のコードをプロジェクトに追加します.
Visual Basic Code: Imports System
Imports System.Net

Public Class IPNetworking
  Public Shared Function GetIP4Address() As String
    Dim IP4Address As String = String.Empty

    For Each IPA As IPAddress In Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress)
      If IPA.AddressFamily.ToString() = "InterNetwork" Then
        IP4Address = IPA.ToString()
        Exit For
      End If
    Next

    If IP4Address <> String.Empty Then
      Return IP4Address
    End If

    For Each IPA As IPAddress In Dns.GetHostAddresses(Dns.GetHostName())
      If IPA.AddressFamily.ToString() = "InterNetwork" Then
        IP4Address = IPA.ToString()
        Exit For
      End If
    Next

    Return IP4Address
  End Function
End Class

C# Code: using System;
using System.Net;

public class IPNetworking
{
  public static string GetIP4Address()
  {
    string IP4Address = String.Empty;

    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;
  }
}

Then use the following code every time you need an IPv4 address:
次に、IPv 4アドレスを取得する必要がある場所で次のコードを使用します. ' VB
Dim ClientIP As String = IP4.GetIP4Address


// C#
string ClientIP = IP4.GetIP4Address();

ConclusionIn this article we saw how to program ASP.NET to return an IPv4 address in an IPv6-enabled environment. As the IPv4 protocol is likely to co-exist with the IPv6 protocol for the foreseeable future, this gives web programmers the best of both worlds.
本稿では、ASPの使用方法についてまとめた.NETはIPv 6の環境でIPv 4アドレスを取得します.これはIPv 4とIPv 6が併存する将来の両方の方法です.
Happy Programming!
  • By Mark Rae Further Readings
  • Internet Protocol (IP)
  • IPv4
  • IPv6