Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting IPv6 as an int in C# and storing it in SQL Server

Under IPv4 I have been parsing the string representation of IP addresses to Int32 and storing them as INT in the SQL Server.

Now, with IPv6 I'm trying to find out if there's a standard or accepted way to parse the string representation of IPv6 to two Int64 using C#?

Also how are people storing those values in the SQL Server - as two fields of BIGINT?

like image 685
Guy Avatar asked Apr 21 '09 20:04

Guy


4 Answers

Just as an IPv4 address is really a 32 bit number, an IPv6 address is really a 128 bit number. There are different string representations of the addresses, but the actual address is the number, not the string.

So, you don't convert an IP address to a number, you parse a string representation of the address into the actual address.

Not even a decimal can hold a 128 bit number, so that leaves three obvious alternatives:

  • store the numeric value split into two bigint fields
  • store a string representation of the address in a varchar field
  • store the numeric value in a 16 byte binary field

Neither is as convenient as storing an IPv4 address in an int, so you have to consider their limitations against what you need to do with the addresses.

like image 125
Guffa Avatar answered Oct 06 '22 11:10

Guffa


The simplest route is to get the framework to do this for you. Use IPAddress.Parse to parse the address, then IPAddress.GetAddressBytes to get the "number" as byte[].

Finally, divide the array into the first and second 8 bytes for conversion to two Int64s, e.g. by creating a MemoryStream over the byte array and then reading via a BinaryReader.

This avoids needing to understand all the available short cut representations for IPv6 addresses.

like image 12
Richard Avatar answered Oct 06 '22 11:10

Richard


If you are using SQL Server 2005, you can use the uniqueidentifier type. This type stores 16 bytes, which is perfect for an IPv6 ip address. You can convert between IPAddress and Guid by using the constructors and ToByteArray.

like image 4
anvilis Avatar answered Oct 06 '22 13:10

anvilis


I use the following method for converting an IP address to two UInt64s (C# 3.0).

/// <summary>
/// Converts an IP address to its UInt64[2] equivalent.
/// For an IPv4 address, the first element will be 0,
/// and the second will be a UInt32 representation of the four bytes.
/// For an IPv6 address, the first element will be a UInt64
/// representation of the first eight bytes, and the second will be the
/// last eight bytes.
/// </summary>
/// <param name="ipAddress">The IP address to convert.</param>
/// <returns></returns>
private static ulong[] ConvertIPAddressToUInt64Array(string ipAddress)
{
    byte[] addrBytes = System.Net.IPAddress.Parse(ipAddress).GetAddressBytes();
    if (System.BitConverter.IsLittleEndian)
    {
        //little-endian machines store multi-byte integers with the
        //least significant byte first. this is a problem, as integer
        //values are sent over the network in big-endian mode. reversing
        //the order of the bytes is a quick way to get the BitConverter
        //methods to convert the byte arrays in big-endian mode.
        System.Collections.Generic.List<byte> byteList = new System.Collections.Generic.List<byte>(addrBytes);
        byteList.Reverse();
        addrBytes = byteList.ToArray();
    }
    ulong[] addrWords = new ulong[2];
    if (addrBytes.Length > 8)
    {
        addrWords[0] = System.BitConverter.ToUInt64(addrBytes, 8);
        addrWords[1] = System.BitConverter.ToUInt64(addrBytes, 0);
    }
    else
    {
        addrWords[0] = 0;
        addrWords[1] = System.BitConverter.ToUInt32(addrBytes, 0);
    }
    return addrWords;
}

Make sure you cast your UInt64s to Int64s before you put them into the database, or you'll get an ArgumentException. When you get your values back out, you can cast them back to UInt64 to get the unsigned value.

I don't have a need to do the reverse (i.e. convert a UInt64[2] to an IP string) so I never built a method for it.

like image 3
Billy Jo Avatar answered Oct 06 '22 11:10

Billy Jo