Directly build IPAddress from SocketAddress.

This commit is contained in:
Chris Ross 2014-02-17 11:57:21 -08:00
parent 742db6ad65
commit da1e6c3e7f
2 changed files with 36 additions and 5 deletions

View File

@ -8,6 +8,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Globalization; using System.Globalization;
using System.Net;
using System.Text; using System.Text;
namespace Microsoft.AspNet.Server.WebListener namespace Microsoft.AspNet.Server.WebListener
@ -170,6 +171,36 @@ namespace Microsoft.AspNet.Server.WebListener
return _hash; return _hash;
} }
internal IPAddress GetIPAddress()
{
if (Family == AddressFamily.InterNetworkV6)
{
return GetIpv6Address();
}
else if (Family == AddressFamily.InterNetwork)
{
return GetIPv4Address();
}
else
{
return null;
}
}
private IPAddress GetIpv6Address()
{
Contract.Assert(Size >= IPv6AddressSize);
byte[] bytes = new byte[NumberOfIPv6Labels * 2];
Array.Copy(_buffer, 8, bytes, 0, NumberOfIPv6Labels * 2);
return new IPAddress(bytes); // TODO: Does scope id matter?
}
private IPAddress GetIPv4Address()
{
Contract.Assert(Size >= IPv4AddressSize);
return new IPAddress(new byte[] { _buffer[4], _buffer[5], _buffer[6], _buffer[7] });
}
public override string ToString() public override string ToString()
{ {
StringBuilder bytes = new StringBuilder(); StringBuilder bytes = new StringBuilder();

View File

@ -356,7 +356,7 @@ namespace Microsoft.AspNet.Server.WebListener
{ {
if (!_isLocal.HasValue) if (!_isLocal.HasValue)
{ {
_isLocal = LocalEndPoint.GetIPAddressString().Equals(RemoteEndPoint.GetIPAddressString()); _isLocal = LocalEndPoint.GetIPAddress().Equals(RemoteEndPoint.GetIPAddress());
} }
return _isLocal.Value; return _isLocal.Value;
} }
@ -457,14 +457,14 @@ namespace Microsoft.AspNet.Server.WebListener
return _localEndPoint; return _localEndPoint;
} }
} }
#if NET45
public IPAddress RemoteIpAddress public IPAddress RemoteIpAddress
{ {
get get
{ {
if (_remoteIpAddress == null) if (_remoteIpAddress == null)
{ {
_remoteIpAddress = IPAddress.Parse(RemoteEndPoint.GetIPAddressString()); // TODO: Create directly from bytes _remoteIpAddress = RemoteEndPoint.GetIPAddress();
} }
return _remoteIpAddress; return _remoteIpAddress;
} }
@ -480,7 +480,7 @@ namespace Microsoft.AspNet.Server.WebListener
{ {
if (_localIpAddress == null) if (_localIpAddress == null)
{ {
_localIpAddress = IPAddress.Parse(LocalEndPoint.GetIPAddressString()); // TODO: Create directly from bytes _localIpAddress = LocalEndPoint.GetIPAddress();
} }
return _localIpAddress; return _localIpAddress;
} }
@ -489,7 +489,7 @@ namespace Microsoft.AspNet.Server.WebListener
_localIpAddress = value; _localIpAddress = value;
} }
} }
#endif
public int RemotePort public int RemotePort
{ {
get get