/// <summary> /// Converts the given object to the type of this converter. /// </summary> /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param> /// <param name="culture">A <see cref="System.Globalization.CultureInfo"/>. If null is passed, the current culture is presumed.</param> /// <param name="value">The value to convert.</param> /// <param name="destinationType">The destination type.</param> /// <returns>The converted object.</returns> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { var addr = (IPAddress)value; if (destinationType == typeof(NativeMethods.RASIPADDR) && (addr == null || (addr != null && addr.AddressFamily == AddressFamily.InterNetwork))) { var ipv4 = new NativeMethods.RASIPADDR(); ipv4.addr = value == null?IPAddress.Any.GetAddressBytes() : addr.GetAddressBytes(); return(ipv4); } #if (WIN2K8 || WIN7 || WIN8) else if (destinationType == typeof(NativeMethods.RASIPV6ADDR) && (addr == null || (addr != null && addr.AddressFamily == AddressFamily.InterNetworkV6))) { var ipv6 = new NativeMethods.RASIPV6ADDR { addr = addr == null?IPAddress.IPv6Any.GetAddressBytes() : addr.GetAddressBytes() }; return(ipv6); } #endif #if (WIN7 || WIN8) else if (destinationType == typeof(NativeMethods.RASTUNNELENDPOINT)) { var endpoint = new NativeMethods.RASTUNNELENDPOINT(); if (addr != null) { var bytes = new byte[16]; var actual = addr.GetAddressBytes(); // Transfer the bytes to the Array.Copy(actual, bytes, actual.Length); switch (addr.AddressFamily) { case AddressFamily.InterNetwork: endpoint.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv4; break; case AddressFamily.InterNetworkV6: endpoint.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv6; break; default: endpoint.type = NativeMethods.RASTUNNELENDPOINTTYPE.Unknown; break; } endpoint.addr = bytes; } return(endpoint); } #endif return(base.ConvertTo(context, culture, value, destinationType)); }
public void ConvertToRASIPADDRFromNullTest() { byte[] expected = IPAddress.Any.GetAddressBytes(); IPAddressConverter target = new IPAddressConverter(); NativeMethods.RASIPADDR result = (NativeMethods.RASIPADDR)target.ConvertTo(null, typeof(NativeMethods.RASIPADDR)); byte[] actual = result.addr; CollectionAssert.AreEqual(expected, actual); }
public void ConvertFromRASIPADDRTest() { byte[] expected = IPAddress.Loopback.GetAddressBytes(); NativeMethods.RASIPADDR value = new NativeMethods.RASIPADDR(); value.addr = expected; IPAddressConverter target = new IPAddressConverter(); IPAddress result = (IPAddress)target.ConvertFrom(value); byte[] actual = result.GetAddressBytes(); Assert.AreEqual <System.Net.Sockets.AddressFamily>(System.Net.Sockets.AddressFamily.InterNetwork, result.AddressFamily); CollectionAssert.AreEqual(expected, actual); }
////#if (WIN2K8 || WIN7 || WIN8) //// /// <summary> //// /// Gets a <see cref="NativeMethods.RASIPV6ADDR"/> for the <paramref name="value"/> specified. //// /// </summary> //// /// <param name="value">An <see cref="System.Net.IPAddress"/> to use.</param> //// /// <returns>A new <see cref="NativeMethods.RASIPADDR"/> structure.</returns> //// /// <exception cref="System.ArgumentException"><paramref name="value"/> is the wrong address family.</exception> //// public static NativeMethods.RASIPV6ADDR GetRasIPv6Address(IPAddress value) //// { //// NativeMethods.RASIPV6ADDR retval = new NativeMethods.RASIPV6ADDR(); //// if (value != null) //// { //// if (value.AddressFamily != AddressFamily.InterNetworkV6) //// { //// ThrowHelper.ThrowArgumentException("value", Resources.Argument_IncorrectAddressFamily); //// } //// if (value == null) //// { //// retval.addr = IPAddress.IPv6Any.GetAddressBytes(); //// } //// else //// { //// retval.addr = value.GetAddressBytes(); //// } //// } //// return retval; //// } ////#endif #if (WIN7 || WIN8) /// <summary> /// Creates a new collection of IPv4 addresses at the pointer specified. /// </summary> /// <param name="ptr">The <see cref="System.IntPtr"/> where the addresses are located.</param> /// <param name="count">The number of addresses at the pointer.</param> /// <returns>A new collection of <see cref="System.Net.IPAddress"/> objects.</returns> public static Collection <IPAddress> CreateIPv4AddressCollection(IntPtr ptr, int count) { Collection <IPAddress> retval = new Collection <IPAddress>(); if (count > 0) { int size = Marshal.SizeOf(typeof(NativeMethods.RASIPADDR)); for (int index = 0; index < count; index++) { IntPtr current = new IntPtr(ptr.ToInt64() + (index * size)); NativeMethods.RASIPADDR address = Utilities.PtrToStructure <NativeMethods.RASIPADDR>(current); retval.Add(new IPAddress(address.addr)); } } return(retval); }