internal static DnsProxySettings FromNativeObject( AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC) { List <UpstreamOptions> upstreams = MarshalUtils.AgListToList <AGDnsApi.ag_upstream_options, UpstreamOptions>( dnsProxySettingsC.upstreams, FromNativeObject); List <UpstreamOptions> fallbacks = MarshalUtils.AgListToList <AGDnsApi.ag_upstream_options, UpstreamOptions>( dnsProxySettingsC.fallbacks, FromNativeObject); Dns64Settings dns64 = FromNativeObject(dnsProxySettingsC.pDns64); EngineParams engineParams = FromNativeObject(dnsProxySettingsC.FilterParams); List <ListenerSettings> listeners = MarshalUtils.AgListToList <AGDnsApi.ag_listener_settings, ListenerSettings>( dnsProxySettingsC.listeners, FromNativeObject); DnsProxySettings dnsProxySettings = new DnsProxySettings { Upstreams = upstreams, Fallbacks = fallbacks, Dns64 = dns64, EngineParams = engineParams, Listeners = listeners, }; MarshalUtils.CopyFieldsToProperties(dnsProxySettingsC, dnsProxySettings); MarshalUtils.AllPtrsToStrings(dnsProxySettingsC, dnsProxySettings); return(dnsProxySettings); }
/// <summary> /// Converts the managed <see cref="dnsProxySettings"/> /// (<seealso cref="DnsProxySettings"/>) to the native <see cref="AGDnsApi.ag_dnsproxy_settings"/> object /// </summary> /// <param name="dnsProxySettings"><see cref="DnsProxySettings"/> instance to convert</param> /// <param name="allocatedPointers">List of pointers, which were allocated. /// Pointers, which will be referred to a newly allocated memory /// (within the process of marshaling the string to the pointer) /// will be added to this list. /// If this list is not specified (null), /// a new created pointer will not be added anywhere</param> /// The resulting pointer (<seealso cref="IntPtr"/>) must be freed /// with <see cref="MarshalUtils.SafeFreeHGlobal(IntPtr)"/>> /// <returns>An instance of <see cref="AGDnsApi.ag_dnsproxy_settings"/></returns> internal static AGDnsApi.ag_dnsproxy_settings ToNativeObject( DnsProxySettings dnsProxySettings, Queue <IntPtr> allocatedPointers) { MarshalUtils.ag_list upstreamsC = MarshalUtils.ListToAgList( dnsProxySettings.Upstreams, ToNativeObject, allocatedPointers); MarshalUtils.ag_list fallbacksC = MarshalUtils.ListToAgList( dnsProxySettings.Fallbacks, ToNativeObject, allocatedPointers); MarshalUtils.ag_list fallbackDomains = MarshalUtils.ListToAgList( dnsProxySettings.FallbackDomains, MarshalUtils.StringToPtr, allocatedPointers); IntPtr pDns64C = IntPtr.Zero; if (dnsProxySettings.Dns64 != null) { AGDnsApi.ag_dns64_settings dns64C = ToNativeObject(dnsProxySettings.Dns64, allocatedPointers); pDns64C = MarshalUtils.StructureToPtr(dns64C, allocatedPointers); } AGDnsApi.ag_filter_engine_params filterEngineParamsC = ToNativeObject(dnsProxySettings.EngineParams, allocatedPointers); MarshalUtils.ag_list listenersC = MarshalUtils.ListToAgList( dnsProxySettings.Listeners, ToNativeObject, allocatedPointers); IntPtr pOutboundProxySessionC = IntPtr.Zero; if (dnsProxySettings.OutboundProxySettings != null) { AGDnsApi.ag_outbound_proxy_settings outboundProxySettingsC = ToNativeObject(dnsProxySettings.OutboundProxySettings, allocatedPointers); pOutboundProxySessionC = MarshalUtils.StructureToPtr(outboundProxySettingsC, allocatedPointers); } AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC = new AGDnsApi.ag_dnsproxy_settings { upstreams = upstreamsC, fallbacks = fallbacksC, pDns64 = pDns64C, FilterParams = filterEngineParamsC, listeners = listenersC, outbound_proxy = pOutboundProxySessionC, fallbackDomains = fallbackDomains, }; MarshalUtils.CopyPropertiesToFields(dnsProxySettings, ref dnsProxySettingsC); MarshalUtils.AllStringsToPtrs(dnsProxySettings, ref dnsProxySettingsC, allocatedPointers); return(dnsProxySettingsC); }
/// <summary> /// Gets the DNS proxy settings, /// according to the specified <see cref="pCurrentDnsProxySettings"/> /// </summary> /// <param name="pCurrentDnsProxySettings">DNS proxy settings /// (<seealso cref="Func{TResult}"/>)</param> /// <exception cref="InvalidOperationException">Thrown, /// if cannot get the DNS proxy settings via native method</exception> /// <returns>The <see cref="DnsProxySettings"/> object</returns> private static DnsProxySettings GetDnsProxySettings(IntPtr pCurrentDnsProxySettings) { Logger.Info("Get DNS proxy settings settings"); if (pCurrentDnsProxySettings == IntPtr.Zero) { throw new InvalidOperationException("Cannot get the DNS proxy settings"); } AGDnsApi.ag_dnsproxy_settings currentDnsProxySettingsC = MarshalUtils.PtrToStructure <AGDnsApi.ag_dnsproxy_settings>(pCurrentDnsProxySettings); DnsProxySettings currentDnsProxySettings = DnsApiConverter.FromNativeObject(currentDnsProxySettingsC); Logger.Info("Finished getting the DNS proxy settings"); return(currentDnsProxySettings); }
/// <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); } } }
internal static DnsProxySettings FromNativeObject( AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC) { List <UpstreamOptions> upstreams = MarshalUtils.AgListToList <AGDnsApi.ag_upstream_options, UpstreamOptions>( dnsProxySettingsC.upstreams, FromNativeObject); List <UpstreamOptions> fallbacks = MarshalUtils.AgListToList <AGDnsApi.ag_upstream_options, UpstreamOptions>( dnsProxySettingsC.fallbacks, FromNativeObject); List <string> fallbackDomains = MarshalUtils.AgListToList <IntPtr, string>( dnsProxySettingsC.fallbackDomains, MarshalUtils.PtrToString); AGDnsApi.ag_dns64_settings dns64C = MarshalUtils.PtrToStructure <AGDnsApi.ag_dns64_settings>(dnsProxySettingsC.pDns64); Dns64Settings dns64 = FromNativeObject(dns64C); EngineParams engineParams = FromNativeObject(dnsProxySettingsC.FilterParams); List <ListenerSettings> listeners = MarshalUtils.AgListToList <AGDnsApi.ag_listener_settings, ListenerSettings>( dnsProxySettingsC.listeners, FromNativeObject); AGDnsApi.ag_outbound_proxy_settings outboundProxySettingsC = MarshalUtils.PtrToStructure <AGDnsApi.ag_outbound_proxy_settings>(dnsProxySettingsC.outbound_proxy); OutboundProxySettings outboundProxySettings = FromNativeObject(outboundProxySettingsC); DnsProxySettings dnsProxySettings = new DnsProxySettings { Upstreams = upstreams, Fallbacks = fallbacks, FallbackDomains = fallbackDomains, Dns64 = dns64, EngineParams = engineParams, Listeners = listeners, OutboundProxySettings = outboundProxySettings }; MarshalUtils.CopyFieldsToProperties(dnsProxySettingsC, dnsProxySettings); MarshalUtils.AllPtrsToStrings(dnsProxySettingsC, dnsProxySettings); return(dnsProxySettings); }
/// <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); } } }
public void TestDnsProxySettingsConverter() { DnsProxySettings dnsSettings = new DnsProxySettings { Upstreams = new List <UpstreamOptions> { new UpstreamOptions { Id = 1, Bootstrap = new List <string>() }, new UpstreamOptions { Id = 2, Bootstrap = new List <string>() }, new UpstreamOptions { Id = 3, Bootstrap = new List <string> { "bootStrapBegin", "bootStrapEnd" } } }, Fallbacks = new List <UpstreamOptions>() { new UpstreamOptions { Bootstrap = new List <string>() } }, BlockedResponseTtlSec = 64, Dns64 = new Dns64Settings { Upstreams = new List <UpstreamOptions>() }, EngineParams = new EngineParams { FilterParams = new List <FilterParams>() }, Listeners = new List <ListenerSettings> { new ListenerSettings { EndPoint = new IPEndPoint(1234567, 9898) } }, FallbackDomains = new List <string> { "Test.com" }, Ipv6Available = true, BlockIpv6 = true, AdblockRulesBlockingMode = AGDnsApi.ag_dnsproxy_blocking_mode.AGBM_ADDRESS, HostsRulesBlockingMode = AGDnsApi.ag_dnsproxy_blocking_mode.AGBM_ADDRESS, CustomBlockingIpv4 = "1.2.3.4", CustomBlockingIpv6 = "::AA", DnsCacheSize = 23, OptimisticCache = true }; Queue <IntPtr> allocatedPointers = new Queue <IntPtr>(); AGDnsApi.ag_dnsproxy_settings nativeDnsSettings = DnsApiConverter.ToNativeObject(dnsSettings, allocatedPointers); Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.fallbacks.entries); Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.fallbackDomains.entries); Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.listeners.entries); Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.upstreams.entries); Assert.AreEqual(nativeDnsSettings.BlockedResponseTtlSec, dnsSettings.BlockedResponseTtlSec); Assert.AreEqual(nativeDnsSettings.Ipv6Available, dnsSettings.Ipv6Available); Assert.AreEqual(nativeDnsSettings.BlockIpv6, dnsSettings.BlockIpv6); Assert.AreEqual(nativeDnsSettings.AdblockRulesBlockingMode, dnsSettings.AdblockRulesBlockingMode); Assert.AreEqual(nativeDnsSettings.HostsRulesBlockingMode, dnsSettings.HostsRulesBlockingMode); Assert.AreEqual(MarshalUtils.PtrToString(nativeDnsSettings.CustomBlockingIpv4), dnsSettings.CustomBlockingIpv4); Assert.AreEqual(MarshalUtils.PtrToString(nativeDnsSettings.CustomBlockingIpv6), dnsSettings.CustomBlockingIpv6); Assert.AreEqual(nativeDnsSettings.DnsCacheSize, dnsSettings.DnsCacheSize); Assert.AreEqual(nativeDnsSettings.OptimisticCache, dnsSettings.OptimisticCache); DnsProxySettings dnsSettingsConverted = DnsApiConverter.FromNativeObject(nativeDnsSettings); Assert.AreEqual(dnsSettings.FallbackDomains, dnsSettingsConverted.FallbackDomains); Assert.AreEqual(dnsSettings.BlockedResponseTtlSec, dnsSettingsConverted.BlockedResponseTtlSec); Assert.AreEqual(dnsSettings.CustomBlockingIpv4, dnsSettingsConverted.CustomBlockingIpv4); Assert.AreEqual(dnsSettings.CustomBlockingIpv6, dnsSettingsConverted.CustomBlockingIpv6); Assert.AreEqual(dnsSettings.AdblockRulesBlockingMode, dnsSettingsConverted.AdblockRulesBlockingMode); Assert.AreEqual(dnsSettings.HostsRulesBlockingMode, dnsSettingsConverted.HostsRulesBlockingMode); bool isUpstreamsEqual = CollectionUtils.CollectionsEquals(dnsSettingsConverted.Upstreams, dnsSettings.Upstreams); Assert.IsTrue(isUpstreamsEqual); }