コード例 #1
0
        /// <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));
        }
コード例 #2
0
        public void ConvertToRASIPV6ADDRFromNullTest()
        {
            byte[] expected = IPAddress.IPv6Any.GetAddressBytes();

            IPAddressConverter target = new IPAddressConverter();

            NativeMethods.RASIPV6ADDR result = (NativeMethods.RASIPV6ADDR)target.ConvertTo(null, typeof(NativeMethods.RASIPV6ADDR));

            byte[] actual = result.addr;

            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void ConvertFromRASIPV6ADDRTest()
        {
            byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes();

            NativeMethods.RASIPV6ADDR value = new NativeMethods.RASIPV6ADDR();
            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.InterNetworkV6, result.AddressFamily);
            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #4
0
        /// <summary>
        /// Creates a new collection of IPv6 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> CreateIPv6AddressCollection(IntPtr ptr, int count)
        {
            Collection <IPAddress> retval = new Collection <IPAddress>();

            if (count > 0)
            {
                int size = Marshal.SizeOf(typeof(NativeMethods.RASIPV6ADDR));

                for (int index = 0; index < count; index++)
                {
                    IntPtr current = new IntPtr(ptr.ToInt64() + (index * size));

                    NativeMethods.RASIPV6ADDR address = Utilities.PtrToStructure <NativeMethods.RASIPV6ADDR>(current);
                    retval.Add(new IPAddress(address.addr));
                }
            }

            return(retval);
        }