/// <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; }
public object GetProjectionInfo(RasHandle handle, NativeMethods.RASPROJECTION projection) { if (Utilities.IsHandleInvalidOrClosed(handle)) { ThrowHelper.ThrowArgumentException("handle", Resources.Argument_InvalidHandle); } int size = 0; object retval = null; object structure = null; switch (projection) { case NativeMethods.RASPROJECTION.Amb: size = Marshal.SizeOf(typeof(NativeMethods.RASAMB)); NativeMethods.RASAMB amb = new NativeMethods.RASAMB(); amb.size = size; structure = amb; break; case NativeMethods.RASPROJECTION.Ccp: size = Marshal.SizeOf(typeof(NativeMethods.RASPPPCCP)); NativeMethods.RASPPPCCP ccp = new NativeMethods.RASPPPCCP(); ccp.size = size; structure = ccp; break; case NativeMethods.RASPROJECTION.IP: size = Marshal.SizeOf(typeof(NativeMethods.RASPPPIP)); NativeMethods.RASPPPIP ip = new NativeMethods.RASPPPIP(); ip.size = size; structure = ip; break; case NativeMethods.RASPROJECTION.Ipx: size = Marshal.SizeOf(typeof(NativeMethods.RASPPPIPX)); NativeMethods.RASPPPIPX ipx = new NativeMethods.RASPPPIPX(); ipx.size = size; structure = ipx; break; case NativeMethods.RASPROJECTION.Lcp: size = Marshal.SizeOf(typeof(NativeMethods.RASPPPLCP)); NativeMethods.RASPPPLCP lcp = new NativeMethods.RASPPPLCP(); lcp.size = size; structure = lcp; break; case NativeMethods.RASPROJECTION.Nbf: size = Marshal.SizeOf(typeof(NativeMethods.RASPPPNBF)); NativeMethods.RASPPPNBF nbf = new NativeMethods.RASPPPNBF(); nbf.size = size; structure = nbf; break; #if (WIN2K8 || WIN7 || WIN8) case NativeMethods.RASPROJECTION.IPv6: size = Marshal.SizeOf(typeof(NativeMethods.RASPPPIPV6)); NativeMethods.RASPPPIPV6 ipv6 = new NativeMethods.RASPPPIPV6(); ipv6.size = size; structure = ipv6; break; #endif case NativeMethods.RASPROJECTION.Slip: size = Marshal.SizeOf(typeof(NativeMethods.RASSLIP)); NativeMethods.RASSLIP slip = new NativeMethods.RASSLIP(); slip.size = size; structure = slip; break; } IntPtr lpCb = new IntPtr(size); IntPtr lpProjection = IntPtr.Zero; try { lpProjection = Marshal.AllocHGlobal(lpCb); Marshal.StructureToPtr(structure, lpProjection, true); int ret = SafeNativeMethods.Instance.GetProjectionInfo(handle, projection, lpProjection, ref lpCb); if (ret == NativeMethods.SUCCESS) { IPAddressConverter converter = new IPAddressConverter(); switch (projection) { case NativeMethods.RASPROJECTION.Amb: NativeMethods.RASAMB amb = Utilities.PtrToStructure<NativeMethods.RASAMB>(lpProjection); retval = new RasAmbInfo( amb.errorCode, amb.netBiosError, amb.lana); break; case NativeMethods.RASPROJECTION.Ccp: NativeMethods.RASPPPCCP ccp = Utilities.PtrToStructure<NativeMethods.RASPPPCCP>(lpProjection); retval = new RasCcpInfo( ccp.errorCode, ccp.compressionAlgorithm, new RasCompressionOptions(ccp.options), ccp.serverCompressionAlgorithm, new RasCompressionOptions(ccp.serverOptions)); break; case NativeMethods.RASPROJECTION.IP: NativeMethods.RASPPPIP ip = Utilities.PtrToStructure<NativeMethods.RASPPPIP>(lpProjection); retval = new RasIPInfo( ip.errorCode, (IPAddress)converter.ConvertFrom(ip.ipAddress), (IPAddress)converter.ConvertFrom(ip.serverIPAddress), new RasIPOptions(ip.options), new RasIPOptions(ip.serverOptions)); break; case NativeMethods.RASPROJECTION.Ipx: NativeMethods.RASPPPIPX ipx = Utilities.PtrToStructure<NativeMethods.RASPPPIPX>(lpProjection); retval = new RasIpxInfo( ipx.errorCode, (IPAddress)converter.ConvertFrom(ipx.ipxAddress)); break; case NativeMethods.RASPROJECTION.Lcp: NativeMethods.RASPPPLCP lcp = Utilities.PtrToStructure<NativeMethods.RASPPPLCP>(lpProjection); retval = new RasLcpInfo( lcp.bundled, lcp.errorCode, lcp.authenticationProtocol, lcp.authenticationData, lcp.eapTypeId, lcp.serverAuthenticationProtocol, lcp.serverAuthenticationData, lcp.serverEapTypeId, lcp.multilink, lcp.terminateReason, lcp.serverTerminateReason, lcp.replyMessage, new RasLcpOptions(lcp.options), new RasLcpOptions(lcp.serverOptions)); break; case NativeMethods.RASPROJECTION.Nbf: NativeMethods.RASPPPNBF nbf = Utilities.PtrToStructure<NativeMethods.RASPPPNBF>(lpProjection); retval = new RasNbfInfo( nbf.errorCode, nbf.netBiosErrorCode, nbf.netBiosErrorMessage, nbf.workstationName, nbf.lana); break; case NativeMethods.RASPROJECTION.Slip: NativeMethods.RASSLIP slip = Utilities.PtrToStructure<NativeMethods.RASSLIP>(lpProjection); retval = new RasSlipInfo( slip.errorCode, (IPAddress)converter.ConvertFrom(slip.ipAddress)); break; #if (WIN2K8 || WIN7 || WIN8) case NativeMethods.RASPROJECTION.IPv6: NativeMethods.RASPPPIPV6 ipv6 = Utilities.PtrToStructure<NativeMethods.RASPPPIPV6>(lpProjection); retval = new RasIPv6Info( ipv6.errorCode, Utilities.ConvertBytesToInt64(ipv6.localInterfaceIdentifier, 0), Utilities.ConvertBytesToInt64(ipv6.peerInterfaceIdentifier, 0), Utilities.ConvertBytesToInt16(ipv6.localCompressionProtocol, 0), Utilities.ConvertBytesToInt16(ipv6.peerCompressionProtocol, 0)); break; #endif } } else if (ret != NativeMethods.ERROR_INVALID_PARAMETER && ret != NativeMethods.ERROR_PROTOCOL_NOT_CONFIGURED) { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } finally { if (lpProjection != IntPtr.Zero) { Marshal.FreeHGlobal(lpProjection); } } return retval; }
/// <summary> /// Retrieves the connection status for the handle specified. /// </summary> /// <param name="handle">The remote access connection handle to retrieve.</param> /// <returns>A <see cref="DotRas.RasConnectionStatus"/> object containing connection status information.</returns> /// <exception cref="DotRas.InvalidHandleException"><paramref name="handle"/> is not a valid handle.</exception> public RasConnectionStatus GetConnectionStatus(RasHandle handle) { RasConnectionStatus retval = null; IntPtr lpRasConnStatus = IntPtr.Zero; try { int size = Marshal.SizeOf(typeof(NativeMethods.RASCONNSTATUS)); NativeMethods.RASCONNSTATUS status = new NativeMethods.RASCONNSTATUS(); status.size = size; lpRasConnStatus = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(status, lpRasConnStatus, true); int ret = SafeNativeMethods.Instance.GetConnectStatus(handle, lpRasConnStatus); if (ret == NativeMethods.ERROR_INVALID_HANDLE) { ThrowHelper.ThrowInvalidHandleException(handle, "handle", Resources.Argument_InvalidHandle); } else if (ret == NativeMethods.SUCCESS) { status = Utilities.PtrToStructure<NativeMethods.RASCONNSTATUS>(lpRasConnStatus); string errorMessage = null; if (status.errorCode != NativeMethods.SUCCESS) { errorMessage = RasHelper.Instance.GetRasErrorString(status.errorCode); } retval = new RasConnectionStatus(); retval.ConnectionState = status.connectionState; retval.ErrorCode = status.errorCode; retval.ErrorMessage = errorMessage; retval.PhoneNumber = status.phoneNumber; if (!string.IsNullOrEmpty(status.deviceName) && !string.IsNullOrEmpty(status.deviceType)) { retval.Device = RasDevice.Create(status.deviceName, status.deviceType); } #if (WIN7 || WIN8) IPAddressConverter converter = new IPAddressConverter(); retval.LocalEndPoint = (IPAddress)converter.ConvertFrom(status.localEndPoint); retval.RemoteEndPoint = (IPAddress)converter.ConvertFrom(status.remoteEndPoint); retval.ConnectionSubState = status.connectionSubState; #endif } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } finally { if (lpRasConnStatus != IntPtr.Zero) { Marshal.FreeHGlobal(lpRasConnStatus); } } return retval; }
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); }
public void ConvertFromUnknownRASTUNNELENDPOINTTest() { NativeMethods.RASTUNNELENDPOINT value = new NativeMethods.RASTUNNELENDPOINT(); value.type = NativeMethods.RASTUNNELENDPOINTTYPE.Unknown; IPAddressConverter target = new IPAddressConverter(); IPAddress result = (IPAddress)target.ConvertFrom(value); Assert.IsNull(result); }
public void ConvertFromIPv6RASTUNNELENDPOINTTest() { byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes(); NativeMethods.RASTUNNELENDPOINT value = new NativeMethods.RASTUNNELENDPOINT(); value.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv6; 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); }
public void ConvertFromUnsupportedTypeTest() { IPAddressConverter target = new IPAddressConverter(); target.ConvertFrom(true); }
public void ConvertFromStringTest() { string expected = IPAddress.Loopback.ToString(); IPAddressConverter target = new IPAddressConverter(); IPAddress result = (IPAddress)target.ConvertFrom(expected); Assert.IsNotNull(result); string actual = result.ToString(); Assert.AreEqual(expected, actual, true); }