public void TestUpstreamOptionsConverter()
        {
            IDnsProxyServerCallbackConfiguration dnsCallback = new DnsProxyServerCallbackConfiguration();
            DnsProxySettings currentDnsProxySettings         = new DnsProxySettings();
            IDnsProxyServer  proxyServer = new DnsProxyServer.DnsProxyServer(currentDnsProxySettings, dnsCallback);

            AGDnsApi.AGDnsProxyServerCallbacks serverCallbackNative =
                DnsApiConverter.ToNativeObject(dnsCallback, proxyServer);
            Assert.NotNull(serverCallbackNative);
        }
        /// <summary>
        /// Creates an instance of the adapter
        /// </summary>
        /// <param name="dnsServerCallbackConfiguration">An object implementing the callbacks interface
        /// (<seealso cref="IDnsProxyServerCallbackConfiguration"/>)</param>
        /// <param name="certificateVerificationCallback">An object implementing certificate verification interface
        /// (<seealso cref="ICertificateVerificationCallback"/>)</param>
        /// <param name="proxyServer">An instance of <see cref="IDnsProxyServer"/></param>
        internal ProxyServerCallbacksAdapter(
            IDnsProxyServerCallbackConfiguration dnsServerCallbackConfiguration,
            ICertificateVerificationCallback certificateVerificationCallback,
            IDnsProxyServer proxyServer)
        {
            m_DnsServerCallbackConfiguration  = dnsServerCallbackConfiguration;
            m_CertificateVerificationCallback = certificateVerificationCallback;
            m_ProxyServer = proxyServer;

            // Initialize a native callbacks object
            DnsProxyServerCallbacks =
                new AGDnsApi.AGDnsProxyServerCallbacks
            {
                ag_dns_request_processed_cb    = AGCOnDnsRequestProcessed,
                ag_certificate_verification_cb = AGCOnCertificationVerificationProcessed
            };
        }
Beispiel #3
0
        /// <summary>
        /// Starts the proxy server
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown, if cannot starting the proxy server
        /// for any reason</exception>
        public void Start()
        {
            lock (m_SyncRoot)
            {
                LOG.Info("Starting the DnsProxyServer");
                if (IsStarted)
                {
                    LOG.Info("DnsProxyServer is already started, doing nothing");
                    return;
                }

                Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();
                try
                {
                    AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC =
                        DnsApiConverter.ToNativeObject(m_DnsProxySettings, allocatedPointers);
                    m_callbackConfigurationC = DnsApiConverter.ToNativeObject(m_CallbackConfiguration, this);

                    IntPtr pDnsProxySettingsC = MarshalUtils.StructureToPtr(dnsProxySettingsC, allocatedPointers);
                    m_pCallbackConfigurationC = MarshalUtils.StructureToPtr(m_callbackConfigurationC);
                    m_pProxyServer            = AGDnsApi.ag_dnsproxy_init(
                        pDnsProxySettingsC,
                        m_pCallbackConfigurationC);
                    if (m_pProxyServer == IntPtr.Zero)
                    {
                        const string errorMessage = "Failed to start the DnsProxyServer due to an error";
                        throw new InvalidOperationException(errorMessage);
                    }

                    m_IsStarted = true;
                    LOG.Info("Finished starting the DnsProxyServer");
                }
                catch (Exception ex)
                {
                    Dispose();
                    throw new InvalidOperationException("error while starting the DnsProxyServer", ex);
                }
                finally
                {
                    MarshalUtils.SafeFreeHGlobal(allocatedPointers);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Starts the proxy server
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown, if cannot starting the proxy server
        /// for any reason</exception>
        public void Start()
        {
            lock (m_SyncRoot)
            {
                Logger.Info("Starting the DnsProxyServer");
                if (IsStarted)
                {
                    Logger.Info("DnsProxyServer is already started, doing nothing");
                    return;
                }

                Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();
                IntPtr         ppOutMessage      = IntPtr.Zero;
                IntPtr         pOutMessage       = IntPtr.Zero;
                IntPtr         pOutResult        = IntPtr.Zero;
                try
                {
                    AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC =
                        DnsApiConverter.ToNativeObject(m_DnsProxySettings, allocatedPointers);
                    m_callbackConfigurationC = DnsApiConverter.ToNativeObject(m_CallbackConfiguration, this);

                    IntPtr pDnsProxySettingsC = MarshalUtils.StructureToPtr(dnsProxySettingsC, allocatedPointers);
                    m_pCallbackConfigurationC = MarshalUtils.StructureToPtr(m_callbackConfigurationC);

                    pOutResult     = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
                    ppOutMessage   = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
                    m_pProxyServer = AGDnsApi.ag_dnsproxy_init(
                        pDnsProxySettingsC,
                        m_pCallbackConfigurationC,
                        pOutResult,
                        ppOutMessage);
                    AGDnsApi.ag_dnsproxy_init_result outResultEnum = AGDnsApi.ag_dnsproxy_init_result.AGDPIR_OK;
                    if (m_pProxyServer == IntPtr.Zero)
                    {
                        int?outResult = MarshalUtils.PtrToNullableInt(pOutResult);
                        if (outResult.HasValue)
                        {
                            outResultEnum = (AGDnsApi.ag_dnsproxy_init_result)outResult.Value;
                        }

                        pOutMessage = MarshalUtils.SafeReadIntPtr(ppOutMessage);
                        string outMessage   = MarshalUtils.PtrToString(pOutMessage);
                        string errorMessage = $"Failed to start the DnsProxyServer with the result {outResultEnum} and message {outMessage}";
                        throw new InvalidOperationException(errorMessage);
                    }

                    m_IsStarted = true;
                    Logger.Info("Finished starting the DnsProxyServer");
                }
                catch (Exception ex)
                {
                    Dispose();
                    throw new InvalidOperationException("error while starting the DnsProxyServer: ", ex);
                }
                finally
                {
                    MarshalUtils.SafeFreeHGlobal(allocatedPointers);
                    AGDnsApi.ag_str_free(pOutMessage);
                    MarshalUtils.SafeFreeHGlobal(ppOutMessage);
                    MarshalUtils.SafeFreeHGlobal(pOutResult);
                }
            }
        }