From 07e2e1ddfb3e7ccf5ef71a62f5c513f61f0f9183 Mon Sep 17 00:00:00 2001 From: dahall Date: Tue, 5 Jul 2022 20:18:52 -0600 Subject: [PATCH] Added SOCKADDR_IN6 to SOCKADDR conversion --- PInvoke/Ws2_32/WinSock2.cs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/PInvoke/Ws2_32/WinSock2.cs b/PInvoke/Ws2_32/WinSock2.cs index 2ff77ec1..96157ca8 100644 --- a/PInvoke/Ws2_32/WinSock2.cs +++ b/PInvoke/Ws2_32/WinSock2.cs @@ -1319,16 +1319,16 @@ namespace Vanara.PInvoke public byte[] S_un_b => BitConverter.GetBytes(S_addr); /// - public static readonly IN_ADDR INADDR_ANY = new IN_ADDR(0U); + public static readonly IN_ADDR INADDR_ANY = new(0U); /// - public static readonly IN_ADDR INADDR_LOOPBACK = new IN_ADDR(0x7f000001); + public static readonly IN_ADDR INADDR_LOOPBACK = new(0x7f000001); /// - public static readonly IN_ADDR INADDR_BROADCAST = new IN_ADDR(0xffffffff); + public static readonly IN_ADDR INADDR_BROADCAST = new(0xffffffff); /// - public static readonly IN_ADDR INADDR_NONE = new IN_ADDR(0xffffffff); + public static readonly IN_ADDR INADDR_NONE = new(0xffffffff); /// Implements the operator ==. /// The left. @@ -1360,17 +1360,17 @@ namespace Vanara.PInvoke /// Performs an implicit conversion from to . /// A UInt32 value. /// The result of the conversion. - public static implicit operator IN_ADDR(uint a) => new IN_ADDR(a); + public static implicit operator IN_ADDR(uint a) => new(a); /// Performs an implicit conversion from to . /// An Int64 value. /// The result of the conversion. - public static implicit operator IN_ADDR(long a) => new IN_ADDR((uint)a); + public static implicit operator IN_ADDR(long a) => new((uint)a); /// Performs an explicit conversion from to . /// The ipv4. /// The resulting instance from the conversion. - public static explicit operator IN6_ADDR(IN_ADDR ipv4) => new IN6_ADDR(ipv4); + public static explicit operator IN6_ADDR(IN_ADDR ipv4) => new(ipv4); /// Determines equality between this instance and . /// The other value to compare. @@ -1502,7 +1502,7 @@ namespace Vanara.PInvoke /// Performs an implicit conversion from [] to . /// The byte array. /// The resulting instance from the conversion. - public static implicit operator IN6_ADDR(byte[] a) => new IN6_ADDR(a); + public static implicit operator IN6_ADDR(byte[] a) => new(a); /// Performs an implicit conversion from to []. /// The instance. @@ -1782,7 +1782,7 @@ namespace Vanara.PInvoke public struct SOCKET : IHandle { /// The handle - private IntPtr handle; + private readonly IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. @@ -1790,11 +1790,11 @@ namespace Vanara.PInvoke /// Represents an invalid socket which is different than a null socket. /// The invalid socket. - public static SOCKET INVALID_SOCKET => new SOCKET(new IntPtr(-1)); + public static SOCKET INVALID_SOCKET => new(new IntPtr(-1)); /// Returns an invalid handle by instantiating a object with . /// Returns a value. - public static SOCKET NULL => new SOCKET(IntPtr.Zero); + public static SOCKET NULL => new(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. /// if this instance is null; otherwise, . @@ -1808,7 +1808,7 @@ namespace Vanara.PInvoke /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. - public static implicit operator SOCKET(IntPtr h) => new SOCKET(h); + public static implicit operator SOCKET(IntPtr h) => new(h); /// Implements the operator !=. /// The first handle. @@ -2569,11 +2569,11 @@ namespace Vanara.PInvoke /// Represents an invalid socket which is different than a null socket. /// The invalid socket. - public static SafeSOCKET INVALID_SOCKET => new SafeSOCKET(new IntPtr(-1), false); + public static SafeSOCKET INVALID_SOCKET => new(new IntPtr(-1), false); /// Returns an invalid handle by instantiating a object with . /// Returns a value. - public static SafeSOCKET NULL => new SafeSOCKET(IntPtr.Zero, false); + public static SafeSOCKET NULL => new(IntPtr.Zero, false); /// Performs an implicit conversion from to . /// The safe handle instance. @@ -2670,7 +2670,7 @@ namespace Vanara.PInvoke public SOCKADDR(IPEndPoint endPoint) : this(endPoint.Address.GetAddressBytes(), (ushort)endPoint.Port) { } /// Gets an instance that represents an empty address. - public static SOCKADDR Empty => new SOCKADDR(new byte[Marshal.SizeOf(typeof(IN6_ADDR))]); + public static SOCKADDR Empty => new(new byte[Marshal.SizeOf(typeof(IN6_ADDR))]); /// Gets the data behind this address as a byte array. /// The address data. @@ -2684,7 +2684,7 @@ namespace Vanara.PInvoke /// Native type /// The value. /// object to an native (unmanaged) memory block the size of T. - public static SOCKADDR CreateFromStructure(T value = default) => new SOCKADDR(InteropExtensions.MarshalToPtr(value, mm.AllocMem, out int s), true, s); + public static SOCKADDR CreateFromStructure(T value = default) => new(InteropExtensions.MarshalToPtr(value, mm.AllocMem, out int s), true, s); /// Performs an explicit conversion from to . /// The address. @@ -2712,12 +2712,17 @@ namespace Vanara.PInvoke /// Performs an implicit conversion from to . /// The address. /// The resulting instance from the conversion. - public static implicit operator SOCKADDR(SOCKADDR_IN addr) => new SOCKADDR(addr); + public static implicit operator SOCKADDR(SOCKADDR_IN addr) => new(addr); /// Performs an implicit conversion from to . /// The address. /// The resulting instance from the conversion. - public static implicit operator SOCKADDR(SOCKADDR_IN6 addr) => new SOCKADDR(addr); + public static implicit operator SOCKADDR(SOCKADDR_IN6 addr) => new(addr); + + /// Performs an implicit conversion from to . + /// The address. + /// The resulting instance from the conversion. + public static implicit operator SOCKADDR(SOCKADDR_INET addr) => CreateFromStructure(addr); /// Provides a copy of as an array of bytes. /// The array of bytes from this instance.