Пример #1
0
        /// <summary>
        /// Sets the entry properties for an existing phone book entry, or creates a new entry.
        /// </summary>
        /// <param name="phoneBook">Required. The <see cref="DotRas.RasPhoneBook"/> that will contain the entry.</param>
        /// <param name="value">An <see cref="DotRas.RasEntry"/> object whose properties to set.</param>        
        /// <returns><b>true</b> if the operation was successful, otherwise <b>false</b>.</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="phoneBook"/> or <paramref name="value"/> are a null reference (<b>Nothing</b> in Visual Basic).</exception>
        /// <exception cref="System.UnauthorizedAccessException">The caller does not have the required permission to perform the action requested.</exception>
        public bool SetEntryProperties(RasPhoneBook phoneBook, RasEntry value)
        {
            if (phoneBook == null)
            {
                ThrowHelper.ThrowArgumentNullException("phoneBook");
            }

            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException("value");
            }

            if (string.IsNullOrEmpty(value.Name))
            {
                ThrowHelper.ThrowArgumentException("Entry name", Resources.Argument_StringCannotBeNullOrEmpty);
            }

            if (!RasHelper.Instance.IsValidEntryName(phoneBook, value.Name, NativeMethods.ERROR_ALREADY_EXISTS, NativeMethods.ERROR_CANNOT_OPEN_PHONEBOOK))
            {
                ThrowHelper.ThrowArgumentException("entry name", Resources.Argument_InvalidEntryName, "entry name", value.Name);
            }

            // Ensure the entry meets the minimum requirements to create or update a phone book entry.
            if ((value.PhoneNumber == null && value.AlternatePhoneNumbers.Count == 0) || (value.Device == null || (value.Device != null && string.IsNullOrEmpty(value.Device.Name))) || value.FramingProtocol == RasFramingProtocol.None || value.EntryType == RasEntryType.None)
            {
                ThrowHelper.ThrowArgumentException("entry", Resources.Argument_MissingRequiredInfo);
            }

            bool retval = false;
            int size = Marshal.SizeOf(typeof(NativeMethods.RASENTRY));
            int lpCb = size;

            IntPtr lpRasEntry = IntPtr.Zero;
            try
            {
                NativeMethods.RASENTRY entry = new NativeMethods.RASENTRY();
                entry.size = size;

#pragma warning disable 0618
                entry.autoDialDll = value.AutoDialDll;
                entry.autoDialFunc = value.AutoDialFunc;
#pragma warning restore 0618

                entry.areaCode = value.AreaCode;
                entry.channels = value.Channels;
                entry.countryCode = value.CountryCode;
                entry.countryId = value.CountryId;
                entry.customAuthKey = value.CustomAuthKey;
                entry.customDialDll = value.CustomDialDll;

                if (value.Device != null)
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(RasDeviceType));
                    if (converter == null)
                    {
                        ThrowHelper.ThrowInvalidOperationException(Resources.Exception_TypeConverterNotFound, "RasDeviceType");
                    }

                    entry.deviceName = value.Device.Name;
                    entry.deviceType = converter.ConvertToString(value.Device.DeviceType);
                }

                IPAddressConverter ipAddrConverter = new IPAddressConverter();

                entry.dialExtraPercent = value.DialExtraPercent;
                entry.dialExtraSampleSeconds = value.DialExtraSampleSeconds;
                entry.dialMode = value.DialMode;
                entry.dnsAddress = (NativeMethods.RASIPADDR)ipAddrConverter.ConvertTo(value.DnsAddress, typeof(NativeMethods.RASIPADDR));
                entry.dnsAddressAlt = (NativeMethods.RASIPADDR)ipAddrConverter.ConvertTo(value.DnsAddressAlt, typeof(NativeMethods.RASIPADDR));
                entry.encryptionType = value.EncryptionType;
                entry.entryType = value.EntryType;
                entry.frameSize = value.FrameSize;
                entry.framingProtocol = value.FramingProtocol;
                entry.hangUpExtraPercent = value.HangUpExtraPercent;
                entry.hangUpExtraSampleSeconds = value.HangUpExtraSampleSeconds;
                entry.id = value.Id;
                entry.idleDisconnectSeconds = value.IdleDisconnectSeconds;
                entry.ipAddress = (NativeMethods.RASIPADDR)ipAddrConverter.ConvertTo(value.IPAddress, typeof(NativeMethods.RASIPADDR));
                entry.networkProtocols = Utilities.GetRasNetworkProtocols(value.NetworkProtocols);
                entry.options = Utilities.GetRasEntryOptions(value);
                entry.phoneNumber = value.PhoneNumber;
                entry.script = value.Script;

                // This member should be set to zero and the subentries should be added after the entry has been created.
                entry.subentries = 0;

                entry.vpnStrategy = value.VpnStrategy;
                entry.winsAddress = (NativeMethods.RASIPADDR)ipAddrConverter.ConvertTo(value.WinsAddress, typeof(NativeMethods.RASIPADDR));
                entry.winsAddressAlt = (NativeMethods.RASIPADDR)ipAddrConverter.ConvertTo(value.WinsAddressAlt, typeof(NativeMethods.RASIPADDR));
                entry.x25Address = value.X25Address;
                entry.x25Facilities = value.X25Facilities;
                entry.x25PadType = value.X25PadType;
                entry.x25UserData = value.X25UserData;

#if (WINXP || WIN2K8 || WIN7 || WIN8)
                entry.options2 = Utilities.GetRasEntryExtendedOptions(value);
                entry.options3 = 0;
                entry.dnsSuffix = value.DnsSuffix;
                entry.tcpWindowSize = value.TcpWindowSize;
                entry.prerequisitePhoneBook = value.PrerequisitePhoneBook;
                entry.prerequisiteEntryName = value.PrerequisiteEntryName;
                entry.redialCount = value.RedialCount;
                entry.redialPause = value.RedialPause;
#endif
#if (WIN2K8 || WIN7 || WIN8)
                entry.ipv4InterfaceMetric = value.IPv4InterfaceMetric;
                entry.ipv6DnsAddress = (NativeMethods.RASIPV6ADDR)ipAddrConverter.ConvertTo(value.IPv6DnsAddress, typeof(NativeMethods.RASIPV6ADDR));
                entry.ipv6DnsAddressAlt = (NativeMethods.RASIPV6ADDR)ipAddrConverter.ConvertTo(value.IPv6DnsAddressAlt, typeof(NativeMethods.RASIPV6ADDR));
                entry.ipv6InterfaceMetric = value.IPv6InterfaceMetric;
#endif
#if (WIN7 || WIN8)
                entry.ipv6Address = (NativeMethods.RASIPV6ADDR)ipAddrConverter.ConvertTo(value.IPv6Address, typeof(NativeMethods.RASIPV6ADDR));
                entry.ipv6PrefixLength = value.IPv6PrefixLength;
                entry.networkOutageTime = value.NetworkOutageTime;
#endif

                int alternatesLength = 0;
                string alternatesList = Utilities.BuildStringList(value.AlternatePhoneNumbers, '\x00', out alternatesLength);
                if (alternatesLength > 0)
                {
                    lpCb = size + alternatesLength;
                    entry.alternateOffset = size;
                }

                lpRasEntry = Marshal.AllocHGlobal(lpCb);
                Marshal.StructureToPtr(entry, lpRasEntry, true);

                if (alternatesLength > 0)
                {
                    // Now that the pointer has been allocated, copy the string to the location.
                    Utilities.CopyString(lpRasEntry, size, alternatesList, alternatesLength);
                }

                try
                {
                    int ret = UnsafeNativeMethods.Instance.SetEntryProperties(phoneBook.Path, value.Name, lpRasEntry, lpCb, IntPtr.Zero, 0);
                    if (ret == NativeMethods.ERROR_ACCESS_DENIED)
                    {
                        ThrowHelper.ThrowUnauthorizedAccessException(Resources.Exception_AccessDeniedBySecurity);
                    }
                    else if (ret == NativeMethods.ERROR_INVALID_PARAMETER)
                    {
                        ThrowHelper.ThrowArgumentException("entry", Resources.Argument_InvalidOrConflictingEntrySettings);
                    }
                    else if (ret == NativeMethods.SUCCESS)
                    {
                        retval = true;

                        if (value.SubEntries.Count > 0)
                        {
                            // The entry has subentries associated with it, add them to the phone book.
                            for (int index = 0; index < value.SubEntries.Count; index++)
                            {
                                RasSubEntry subEntry = value.SubEntries[index];
                                if (subEntry != null)
                                {
                                    RasHelper.Instance.SetSubEntryProperties(value.Owner, value, index, subEntry);
                                }
                            }
                        }

                        if (value.Id == Guid.Empty)
                        {
                            // The entry being set is new, update any properties that need an existing entry.
                            RasEntry newEntry = null;
                            try
                            {
                                // Grab the entry from the phone book.
                                newEntry = RasHelper.Instance.GetEntryProperties(phoneBook, value.Name);
                                value.Id = newEntry.Id;
                            }
                            finally
                            {
                                newEntry = null;
                            }
                        }
                    }
                    else
                    {
                        ThrowHelper.ThrowRasException(ret);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
            }
            finally
            {
                if (lpRasEntry != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpRasEntry);
                }
            }

            return retval;
        }
Пример #2
0
        /// <summary>
        /// Updates the tunnel endpoints of an Internet Key Exchange (IKEv2) connection.
        /// </summary>
        /// <param name="handle">The handle of the connection.</param>
        /// <param name="interfaceIndex">The new interface index of the endpoint.</param>
        /// <param name="localEndPoint">The new local client endpoint of the connection.</param>
        /// <param name="remoteEndPoint">The new remote server endpoint of the connection.</param>
        public void UpdateConnection(RasHandle handle, int interfaceIndex, IPAddress localEndPoint, IPAddress remoteEndPoint)
        {
            int size = Marshal.SizeOf(typeof(NativeMethods.RASUPDATECONN));

            IntPtr pRasUpdateConn = IntPtr.Zero;
            try
            {
                IPAddressConverter converter = new IPAddressConverter();

                NativeMethods.RASUPDATECONN updateConn = new NativeMethods.RASUPDATECONN();
                updateConn.version = GetCurrentApiVersion();
                updateConn.size = size;
                updateConn.interfaceIndex = interfaceIndex;
                updateConn.localEndPoint = (NativeMethods.RASTUNNELENDPOINT)converter.ConvertTo(localEndPoint, typeof(NativeMethods.RASTUNNELENDPOINT));
                updateConn.remoteEndPoint = (NativeMethods.RASTUNNELENDPOINT)converter.ConvertTo(remoteEndPoint, typeof(NativeMethods.RASTUNNELENDPOINT));

                pRasUpdateConn = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(updateConn, pRasUpdateConn, true);

                int ret = UnsafeNativeMethods.Instance.UpdateConnection(handle, pRasUpdateConn);
                if (ret == NativeMethods.ERROR_INVALID_PARAMETER)
                {
                    ThrowHelper.ThrowInvalidOperationException(Resources.Exception_InvalidIkeV2Handle);
                }
                else if (ret != NativeMethods.SUCCESS)
                {
                    ThrowHelper.ThrowRasException(ret);
                }
            }
            catch (EntryPointNotFoundException)
            {
                ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (pRasUpdateConn != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pRasUpdateConn);
                }
            }
        }
        public void ConvertToIPv6RASTUNNELENDPOINTTest()
        {
            byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes();

            IPAddressConverter target = new IPAddressConverter();
            NativeMethods.RASTUNNELENDPOINT result = (NativeMethods.RASTUNNELENDPOINT)target.ConvertTo(IPAddress.IPv6Loopback, typeof(NativeMethods.RASTUNNELENDPOINT));

            byte[] actual = result.addr;

            Assert.AreEqual<NativeMethods.RASTUNNELENDPOINTTYPE>(NativeMethods.RASTUNNELENDPOINTTYPE.IPv6, result.type);
            CollectionAssert.AreEqual(expected, actual);
        }
        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);
        }
 public void ConvertToUnsupportedTypeTest()
 {
     IPAddressConverter target = new IPAddressConverter();
     target.ConvertTo(null, typeof(bool));
 }
 public void ConvertToRASIPADDRFromIPv6Test()
 {
     IPAddressConverter target = new IPAddressConverter();
     target.ConvertTo(IPAddress.IPv6Loopback, typeof(NativeMethods.RASIPADDR));
 }
        public void ConvertToRASIPADDRTest()
        {
            byte[] expected = IPAddress.Loopback.GetAddressBytes();

            IPAddressConverter target = new IPAddressConverter();
            NativeMethods.RASIPADDR result = (NativeMethods.RASIPADDR)target.ConvertTo(IPAddress.Loopback, typeof(NativeMethods.RASIPADDR));

            byte[] actual = result.addr;

            CollectionAssert.AreEqual(expected, actual);
        }