예제 #1
0
        /// <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;
        }
예제 #2
0
        public void GetConnectionStatusErrorCodeTest()
        {
            NativeMethods.RASCONNSTATUS expected = new NativeMethods.RASCONNSTATUS()
            {
                connectionState = RasConnectionState.Authenticate,
                deviceName = "WAN Miniport (PPTP)",
                deviceType = NativeMethods.RASDT_Vpn,
                errorCode = NativeMethods.ERROR_PROTOCOL_NOT_CONFIGURED,
                phoneNumber = IPAddress.Loopback.ToString()
            };

            CopyStructToAddrMock<NativeMethods.RASCONNSTATUS> target = new CopyStructToAddrMock<NativeMethods.RASCONNSTATUS>();
            target.Result = expected;

            Mock<ISafeNativeMethods> mock = new Mock<ISafeNativeMethods>();
            SafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.GetConnectStatus(It.IsAny<RasHandle>(), It.IsAny<IntPtr>())).Callback((RasHandle handle, IntPtr status) =>
                {
                    target.Execute(status);
                }).Returns(NativeMethods.SUCCESS);
            mock.Setup(o => o.GetErrorString(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>())).Callback((int errorCode, string result, int bufferSize) =>
                {
                    new SafeNativeMethods().GetErrorString(errorCode, result, bufferSize);
                }).Returns(NativeMethods.SUCCESS);

            RasConnectionStatus actual = RasHelper.Instance.GetConnectionStatus(new RasHandle(new IntPtr(1), false));

            Assert.AreEqual(expected.connectionState, actual.ConnectionState);
            Assert.AreEqual(expected.deviceName, actual.Device.Name);
            Assert.IsTrue(string.Equals(expected.deviceType, actual.Device.DeviceType.ToString(), StringComparison.CurrentCultureIgnoreCase));
            Assert.AreEqual(expected.errorCode, actual.ErrorCode);
            Assert.AreEqual(expected.phoneNumber, actual.PhoneNumber);
            Assert.IsTrue(!string.IsNullOrEmpty(actual.ErrorMessage));            
        }
예제 #3
0
        /// <summary>
        /// Indicates whether a connection is currently active.
        /// </summary>
        /// <param name="handle">The handle to check.</param>
        /// <returns><b>true</b> if the connection is active, otherwise <b>false</b>.</returns>
        public bool IsConnectionActive(RasHandle handle)
        {
            bool retval = false;

            int size = Marshal.SizeOf(typeof(NativeMethods.RASCONNSTATUS));

            IntPtr lpRasConnStatus = IntPtr.Zero;
            try
            {
                NativeMethods.RASCONNSTATUS status = new NativeMethods.RASCONNSTATUS();
                status.size = size;

                lpRasConnStatus = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(status, lpRasConnStatus, true);

                retval = SafeNativeMethods.Instance.GetConnectStatus(handle, lpRasConnStatus) == NativeMethods.SUCCESS;
            }
            finally
            {
                if (lpRasConnStatus != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpRasConnStatus);
                }
            }

            return retval;
        }
예제 #4
0
        public void GetConnectionStatusTest()
        {
            NativeMethods.RASCONNSTATUS expected = new NativeMethods.RASCONNSTATUS()
            {
                connectionState = RasConnectionState.AllDevicesConnected,
                deviceName = "WAN Miniport (PPTP)",
                deviceType = NativeMethods.RASDT_Vpn,
                errorCode = NativeMethods.SUCCESS,
                phoneNumber = IPAddress.Loopback.ToString(),
#if (WIN7 || WIN8)
                localEndPoint = new NativeMethods.RASTUNNELENDPOINT() { addr = new byte[] { 192, 168, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv4 },
                remoteEndPoint = new NativeMethods.RASTUNNELENDPOINT() { addr = new byte[] { 192, 168, 2, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv4 },
                connectionSubState = RasConnectionSubState.None
#endif
            };

            CopyStructToAddrMock<NativeMethods.RASCONNSTATUS> target = new CopyStructToAddrMock<NativeMethods.RASCONNSTATUS>();
            target.Result = expected;

            Mock<ISafeNativeMethods> mock = new Mock<ISafeNativeMethods>();
            SafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.GetConnectStatus(It.IsAny<RasHandle>(), It.IsAny<IntPtr>())).Callback((RasHandle handle, IntPtr connectionStatus) =>
                {
                    target.Execute(connectionStatus);
                }).Returns(NativeMethods.SUCCESS);

            RasConnectionStatus actual = RasHelper.Instance.GetConnectionStatus(new RasHandle(new IntPtr(1), false));

            Assert.AreEqual(expected.connectionState, actual.ConnectionState);
            Assert.AreEqual(expected.deviceName, actual.Device.Name);
            Assert.IsTrue(string.Equals(expected.deviceType, actual.Device.DeviceType.ToString(), StringComparison.CurrentCultureIgnoreCase));
            Assert.AreEqual(expected.errorCode, actual.ErrorCode);
            Assert.AreEqual(null, actual.ErrorMessage);
            Assert.AreEqual(expected.phoneNumber, actual.PhoneNumber);
#if (WIN7 || WIN8)
            TestUtilities.AssertEndPoint(expected.localEndPoint, actual.LocalEndPoint);
            TestUtilities.AssertEndPoint(expected.remoteEndPoint, actual.RemoteEndPoint);
            Assert.IsTrue(actual.LocalEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
            Assert.IsTrue(actual.RemoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
#endif
        }