问题
I'm writing an app that will store the IP Addresses of where requests are coming from. Those IP Addresses will be stored as a varbinary(16)
in my database. Its become apparent that a varbinary(16)
is not the proper size. Currently, I'm using the following code to get the IP Address of a request into a byte[];
HttpRequestMessage request = GetRequestMessage();
string ipAddress = string.Empty;
if (request.Properties.ContainsKey("MS_HttpContext"))
{
ipAddress = ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty property = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
ipAddress = property.Address;
}
byte[] bytes = new byte[ipAddress.Length * sizeof(char)];
System.Buffer.BlockCopy(ipAddress.ToCharArray(), 0, bytes, 0, bytes.Length);
// What is the maximum size of bytes?
I've noticed that bytes has a length of 26. My question is, what is the largest size that bytes can be if I need to support IPv6 addresses? I need to know to change the size of my varbinary(16)
to the appropriate length.
Thank you!
回答1:
byte[] bytes = new byte[ipAddress.Length * sizeof(char)];
This looks like something written by a C programmer, you don't need to do any of this.
All you need is ipAddress.GetAddressBytes()
and shove that in a binary(16)
As a side note, you can also use a uniqueidentifier
to store IPv6 addresses since they are the same length. These are much faster to search with than a varbinary
回答2:
The IPv6 address space is 128 bits. Therefore, any IPv6 address can be represented using 16 bytes. Therefore, varbinary(16) is correct.
However, the way you are extracting the bytes is, with respect, odd.
Please see the documentation for IPAddress.GetAddressBytes()
(Looks like you might also need IPAddress.Parse())
来源:https://stackoverflow.com/questions/15115463/byte-array-size-for-a-ipv6-ip-address