Added a bunch more functions to NetAddress

This commit is contained in:
Garry Newman 2020-02-26 15:28:25 +00:00
parent d900b6e217
commit 57775e5d10

View File

@ -23,26 +23,19 @@ namespace Steamworks.Data
internal byte ip3; internal byte ip3;
} }
/// <summary>
/// The Port. This is redundant documentation.
/// </summary>
public ushort Port => port;
/// <summary> /// <summary>
/// Any IP, specific port /// Any IP, specific port
/// </summary> /// </summary>
public static NetAddress AnyIp( ushort port ) public static NetAddress AnyIp( ushort port )
{ {
return new NetAddress var addr = Cleared;
{ addr.port = port;
ip = new IPV4 return addr;
{
m_8zeros = 0,
m_0000 = 0,
m_ffff = 0,
ip0 = 0,
ip1 = 0,
ip2 = 0,
ip3 = 0,
},
port = port
};
} }
/// <summary> /// <summary>
@ -50,21 +43,9 @@ namespace Steamworks.Data
/// </summary> /// </summary>
public static NetAddress LocalHost( ushort port ) public static NetAddress LocalHost( ushort port )
{ {
return new NetAddress var local = Cleared;
{ InternalSetIPv6LocalHost( ref local, port );
ip = new IPV4 return local;
{
m_8zeros = 0,
m_0000 = 0,
m_ffff = 0,
ip0 = 0,
ip1 = 0,
ip2 = 0,
ip3 = 1,
},
port = port
};
} }
/// <summary> /// <summary>
@ -82,26 +63,89 @@ namespace Steamworks.Data
{ {
var addr = address.GetAddressBytes(); var addr = address.GetAddressBytes();
if ( addr.Length == 4 ) if ( address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork )
{ {
return new NetAddress var local = Cleared;
{ InternalSetIPv4( ref local, Utility.IpToInt32( address ), port );
ip = new IPV4 return local;
{ }
m_8zeros = 0,
m_0000 = 0,
m_ffff = 0xffff,
ip0 = addr[0],
ip1 = addr[1],
ip2 = addr[2],
ip3 = addr[3],
},
port = port throw new System.NotImplementedException( "Oops - no IPV6 support yet?" );
}; }
/// <summary>
/// Set everything to zero
/// </summary>
public static NetAddress Cleared
{
get
{
NetAddress self = default;
InternalClear( ref self );
return self;
}
}
/// <summary>
/// Return true if the IP is ::0. (Doesn't check port.)
/// </summary>
public bool IsIPv6AllZeros
{
get
{
NetAddress self = this;
return InternalIsIPv6AllZeros( ref self );
}
}
/// <summary>
/// Return true if IP is mapped IPv4
/// </summary>
public bool IsIPv4
{
get
{
NetAddress self = this;
return InternalIsIPv4( ref self );
}
}
/// <summary>
/// Return true if this identity is localhost. (Either IPv6 ::1, or IPv4 127.0.0.1)
/// </summary>
public bool IsLocalHost
{
get
{
NetAddress self = this;
return InternalIsLocalHost( ref self );
}
}
/// <summary>
/// Get the Address section
/// </summary>
public IPAddress Address
{
get
{
if ( IsIPv4 )
{
NetAddress self = this;
var ip = InternalGetIPv4( ref self );
return Utility.Int32ToIp( ip );
} }
throw new System.NotImplementedException( "Oops - no IPV6 support yet?" ); throw new System.NotImplementedException( "Oops - no IPV6 support yet?" );
} }
} }
public override string ToString()
{
var ptr = Helpers.TakeMemory();
var self = this;
InternalToString( ref self, ptr, Helpers.MaxStringSize, true );
return Helpers.MemoryToString( ptr );
}
}
} }