public override void Initialize() { base.Initialize(); this._phoneBookPath = System.IO.Path.GetTempFileName(); this._entryName = Guid.NewGuid().ToString(); this._phoneBook = new RasPhoneBook(); this._phoneBook.Open(this._phoneBookPath); this._entry = new RasEntry(this._entryName); this._entry.Device = RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn); this._entry.EncryptionType = RasEncryptionType.Require; this._entry.EntryType = RasEntryType.Vpn; this._entry.FramingProtocol = RasFramingProtocol.Ppp; this._entry.NetworkProtocols.IP = true; this._entry.NetworkProtocols.IPv6 = true; this._entry.PhoneNumber = IPAddress.Loopback.ToString(); this._entry.VpnStrategy = RasVpnStrategy.Default; this._phoneBook.Entries.Add(this._entry); }
/// <summary> /// Retrieves the entry properties for an entry within a phone book. /// </summary> /// <param name="phoneBook">Required. The <see cref="DotRas.RasPhoneBook"/> containing the entry.</param> /// <param name="entryName">Required. The name of an entry to retrieve.</param> /// <returns>A <see cref="DotRas.RasEntry"/> object.</returns> /// <exception cref="System.ArgumentException"><paramref name="entryName"/> is an empty string or null reference (<b>Nothing</b> in Visual Basic).</exception> /// <exception cref="System.ArgumentNullException"><paramref name="phoneBook"/> is 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 RasEntry GetEntryProperties(RasPhoneBook phoneBook, string entryName) { if (phoneBook == null) { ThrowHelper.ThrowArgumentNullException("phoneBook"); } if (string.IsNullOrEmpty(entryName)) { ThrowHelper.ThrowArgumentException("entryName", Resources.Argument_StringCannotBeNullOrEmpty); } RasEntry retval = null; int size = Marshal.SizeOf(typeof(NativeMethods.RASENTRY)); bool retry = false; IntPtr lpCb = new IntPtr(size); do { NativeMethods.RASENTRY entry = new NativeMethods.RASENTRY(); entry.size = size; IntPtr lpRasEntry = IntPtr.Zero; try { lpRasEntry = Marshal.AllocHGlobal(lpCb); Marshal.StructureToPtr(entry, lpRasEntry, true); try { int ret = UnsafeNativeMethods.Instance.GetEntryProperties(phoneBook.Path, entryName, lpRasEntry, ref lpCb, IntPtr.Zero, IntPtr.Zero); if (ret == NativeMethods.SUCCESS) { entry = Utilities.PtrToStructure<NativeMethods.RASENTRY>(lpRasEntry); retval = new RasEntry(entryName); if (entry.alternateOffset != 0) { retval.AlternatePhoneNumbers = Utilities.CreateStringCollectionByLength(lpRasEntry, entry.alternateOffset, lpCb.ToInt32() - entry.alternateOffset); } if (entry.subentries > 1) { // The first subentry in the collection is always the default entry, need to check if there are two or // more subentries before loading the collection. retval.SubEntries.Load(phoneBook, entry.subentries - 1); } retval.AreaCode = entry.areaCode; // This warning is being disabled since the object is being loaded by the Win32 API and must have the // data placed into the object. #pragma warning disable 0618 retval.AutoDialDll = entry.autoDialDll; retval.AutoDialFunc = entry.autoDialFunc; #pragma warning restore 0618 retval.Channels = entry.channels; retval.CountryCode = entry.countryCode; retval.CountryId = entry.countryId; retval.CustomAuthKey = entry.customAuthKey; retval.CustomDialDll = entry.customDialDll; if (entry.deviceName != null && !string.IsNullOrEmpty(entry.deviceType)) { retval.Device = RasDevice.Create(entry.deviceName, entry.deviceType); } IPAddressConverter ipAddrConverter = new IPAddressConverter(); retval.DialExtraPercent = entry.dialExtraPercent; retval.DialExtraSampleSeconds = entry.dialExtraSampleSeconds; retval.DialMode = entry.dialMode; retval.DnsAddress = (IPAddress)ipAddrConverter.ConvertFrom(entry.dnsAddress); retval.DnsAddressAlt = (IPAddress)ipAddrConverter.ConvertFrom(entry.dnsAddressAlt); retval.EncryptionType = entry.encryptionType; retval.EntryType = entry.entryType; retval.FrameSize = entry.frameSize; retval.FramingProtocol = entry.framingProtocol; retval.HangUpExtraPercent = entry.hangUpExtraPercent; retval.HangUpExtraSampleSeconds = entry.hangUpExtraSampleSeconds; retval.Id = entry.id; retval.IdleDisconnectSeconds = entry.idleDisconnectSeconds; retval.IPAddress = (IPAddress)ipAddrConverter.ConvertFrom(entry.ipAddress); retval.PhoneNumber = entry.phoneNumber; retval.Script = entry.script; retval.VpnStrategy = entry.vpnStrategy; retval.WinsAddress = (IPAddress)ipAddrConverter.ConvertFrom(entry.winsAddress); retval.WinsAddressAlt = (IPAddress)ipAddrConverter.ConvertFrom(entry.winsAddressAlt); retval.X25Address = entry.x25Address; retval.X25Facilities = entry.x25Facilities; retval.X25PadType = entry.x25PadType; retval.X25UserData = entry.x25UserData; Utilities.SetRasEntryOptions(retval, entry.options); Utilities.SetRasNetworkProtocols(retval, entry.networkProtocols); #if (WINXP || WIN2K8 || WIN7 || WIN8) Utilities.SetRasEntryExtendedOptions(retval, entry.options2); retval.DnsSuffix = entry.dnsSuffix; retval.TcpWindowSize = entry.tcpWindowSize; retval.PrerequisitePhoneBook = entry.prerequisitePhoneBook; retval.PrerequisiteEntryName = entry.prerequisiteEntryName; retval.RedialCount = entry.redialCount; retval.RedialPause = entry.redialPause; #endif #if (WIN2K8 || WIN7 || WIN8) retval.IPv6DnsAddress = (IPAddress)ipAddrConverter.ConvertFrom(entry.ipv6DnsAddress); retval.IPv6DnsAddressAlt = (IPAddress)ipAddrConverter.ConvertFrom(entry.ipv6DnsAddressAlt); retval.IPv4InterfaceMetric = entry.ipv4InterfaceMetric; retval.IPv6InterfaceMetric = entry.ipv6InterfaceMetric; #endif #if (WIN7 || WIN8) retval.IPv6Address = (IPAddress)ipAddrConverter.ConvertFrom(entry.ipv6Address); retval.IPv6PrefixLength = entry.ipv6PrefixLength; retval.NetworkOutageTime = entry.networkOutageTime; #endif retry = false; } else if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL) { retry = true; } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } } finally { if (lpRasEntry != IntPtr.Zero) { Marshal.FreeHGlobal(lpRasEntry); } } } while (retry); return retval; }
/// <summary> /// Deletes a subentry from the specified phone book entry. /// </summary> /// <param name="phoneBook">Required. The <see cref="DotRas.RasPhoneBook"/> containing the entry.</param> /// <param name="entry">Required. The <see cref="DotRas.RasEntry"/> containing the subentry to be deleted.</param> /// <param name="subEntryId">The one-based index of the subentry to delete.</param> /// <returns><b>true</b> if the function succeeds, otherwise <b>false</b>.</returns> /// <exception cref="System.ArgumentNullException"><paramref name="phoneBook"/> or <paramref name="entry"/> is 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 DeleteSubEntry(RasPhoneBook phoneBook, RasEntry entry, int subEntryId) { if (phoneBook == null) { ThrowHelper.ThrowArgumentNullException("phoneBook"); } if (entry == null) { ThrowHelper.ThrowArgumentNullException("entry"); } if (subEntryId <= 0) { ThrowHelper.ThrowArgumentException("subEntryId", Resources.Argument_ValueCannotBeLessThanOrEqualToZero); } bool retval = false; int ret = UnsafeNativeMethods.Instance.DeleteSubEntry(phoneBook.Path, entry.Name, subEntryId); if (ret == NativeMethods.SUCCESS) { retval = true; } else { ThrowHelper.ThrowRasException(ret); } return retval; }
/// <summary> /// Sets the subentry properties for an existing subentry, or creates a new subentry. /// </summary> /// <param name="phoneBook">Required. The <see cref="DotRas.RasPhoneBook"/> that will contain the entry.</param> /// <param name="entry">Required. The <see cref="DotRas.RasEntry"/> whose subentry to set.</param> /// <param name="subEntryId">The zero-based index of the subentry to set.</param> /// <param name="value">An <see cref="DotRas.RasSubEntry"/> 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="entry"/> or <paramref name="value"/> is 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 SetSubEntryProperties(RasPhoneBook phoneBook, RasEntry entry, int subEntryId, RasSubEntry value) { if (phoneBook == null) { ThrowHelper.ThrowArgumentNullException("phoneBook"); } if (value == null) { ThrowHelper.ThrowArgumentNullException("value"); } if (entry == null) { ThrowHelper.ThrowArgumentNullException("entry"); } bool retval = false; int size = Marshal.SizeOf(typeof(NativeMethods.RASSUBENTRY)); int lpCb = size; IntPtr lpRasSubEntry = IntPtr.Zero; try { NativeMethods.RASSUBENTRY subentry = new NativeMethods.RASSUBENTRY(); subentry.size = size; subentry.phoneNumber = value.PhoneNumber; if (value.Device != null) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(RasDeviceType)); if (converter == null) { ThrowHelper.ThrowInvalidOperationException(Resources.Exception_TypeConverterNotFound, "RasDeviceType"); } subentry.deviceName = value.Device.Name; subentry.deviceType = converter.ConvertToString(value.Device.DeviceType); } int alternatesLength = 0; string alternatesList = Utilities.BuildStringList(value.AlternatePhoneNumbers, '\x00', out alternatesLength); if (alternatesLength > 0) { lpCb = size + alternatesLength; subentry.alternateOffset = size; } lpRasSubEntry = Marshal.AllocHGlobal(lpCb); Marshal.StructureToPtr(subentry, lpRasSubEntry, true); if (alternatesLength > 0) { Utilities.CopyString(lpRasSubEntry, size, alternatesList, alternatesLength); } try { int ret = UnsafeNativeMethods.Instance.SetSubEntryProperties(phoneBook.Path, entry.Name, subEntryId + 2, lpRasSubEntry, lpCb, IntPtr.Zero, 0); if (ret == NativeMethods.SUCCESS) { retval = true; } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } } finally { if (lpRasSubEntry != IntPtr.Zero) { Marshal.FreeHGlobal(lpRasSubEntry); } } return retval; }
/// <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; }
/// <summary> /// Retrieves the subentry properties for an entry within a phone book. /// </summary> /// <param name="phoneBook">Required. The <see cref="DotRas.RasPhoneBook"/> containing the entry.</param> /// <param name="entry">Required. The <see cref="DotRas.RasEntry"/> containing the subentry.</param> /// <param name="subEntryId">The zero-based index of the subentry to retrieve.</param> /// <returns>A new <see cref="DotRas.RasSubEntry"/> object.</returns> /// <exception cref="System.ArgumentNullException"><paramref name="phoneBook"/> or <paramref name="entry"/> is 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 RasSubEntry GetSubEntryProperties(RasPhoneBook phoneBook, RasEntry entry, int subEntryId) { if (phoneBook == null) { ThrowHelper.ThrowArgumentNullException("phoneBook"); } if (entry == null) { ThrowHelper.ThrowArgumentNullException("entry"); } RasSubEntry retval = null; int size = Marshal.SizeOf(typeof(NativeMethods.RASSUBENTRY)); bool retry = false; IntPtr lpCb = new IntPtr(size); do { NativeMethods.RASSUBENTRY subentry = new NativeMethods.RASSUBENTRY(); subentry.size = size; IntPtr lpRasSubEntry = IntPtr.Zero; try { lpRasSubEntry = Marshal.AllocHGlobal(lpCb); Marshal.StructureToPtr(subentry, lpRasSubEntry, true); try { int ret = UnsafeNativeMethods.Instance.GetSubEntryProperties(phoneBook.Path, entry.Name, subEntryId + 2, lpRasSubEntry, ref lpCb, IntPtr.Zero, IntPtr.Zero); if (ret == NativeMethods.SUCCESS) { subentry = Utilities.PtrToStructure<NativeMethods.RASSUBENTRY>(lpRasSubEntry); retval = new RasSubEntry(); retval.Device = RasDevice.Create(subentry.deviceName, subentry.deviceType); retval.PhoneNumber = subentry.phoneNumber; if (subentry.alternateOffset != 0) { retval.AlternatePhoneNumbers = Utilities.CreateStringCollectionByLength(lpRasSubEntry, subentry.alternateOffset, lpCb.ToInt32() - subentry.alternateOffset); } retry = false; } else if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL) { retry = true; } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } } finally { if (lpRasSubEntry != IntPtr.Zero) { Marshal.FreeHGlobal(lpRasSubEntry); } } } while (retry); return retval; }
public void IPv6RemoteDefaultGatewayAfterReloadTest() { bool expected = true; this._entry.Options.IPv6RemoteDefaultGateway = expected; this._entry.Update(); this._entry = null; this._phoneBook.Dispose(); this._phoneBook = null; this._phoneBook = new RasPhoneBook(); this._phoneBook.Open(this._phoneBookPath); this._entry = this._phoneBook.Entries[this._entryName]; bool actual = this._entry.Options.IPv6RemoteDefaultGateway; Assert.AreEqual(expected, actual); }
public void addEntry(RasEntry vpnentry) { pbook.Entries.Add(vpnentry); }