internal static AGDnsApi.ag_upstream_options ToNativeObject( UpstreamOptions upstreamOptions, Queue <IntPtr> allocatedPointers) { AGDnsApi.ag_list bootstrapC = MarshalUtils.ListToAgList( upstreamOptions.Bootstrap, MarshalUtils.StringToPtr, allocatedPointers); byte[] addressBytes = null; if (upstreamOptions.ResolvedIpAddress != null) { addressBytes = upstreamOptions.ResolvedIpAddress.GetAddressBytes(); } AGDnsApi.ag_buffer addressC = MarshalUtils.BytesToAgBuffer(addressBytes, allocatedPointers); AGDnsApi.ag_upstream_options upstreamOptionsC = new AGDnsApi.ag_upstream_options { bootstrap = bootstrapC, resolved_ip_address = addressC }; MarshalUtils.CopyPropertiesToFields(upstreamOptions, ref upstreamOptionsC); MarshalUtils.AllStringsToPtrs(upstreamOptions, ref upstreamOptionsC, allocatedPointers); return(upstreamOptionsC); }
/// <summary> /// Converts the specified <see cref="sourceList"/> with elements /// with the type <see cref="TSourceClassElement"/> to the <see cref="AGDnsApi.ag_list"/> instance. /// Result <see cref="AGDnsApi.ag_list"/> instance points to the array of structures /// with the type <see cref="TDestinationStructElement"/>. /// All the created pointers are stored in the <see cref="allocatedPointers"/> queue, /// in order to free them further with <see cref="SafeFreeHGlobal(Queue{IntPtr})"/> /// </summary> /// <param name="sourceList">Source list of elements with type <see cref="TSourceClassElement"/></param> /// <param name="converterFunc">Function to convert element with type <see cref="TSourceClassElement"/> /// to the struct with the type <see cref="TDestinationStructElement"/></param> /// <param name="allocatedPointers">Queue of pointers, /// which contains pointer for further freeing with <see cref="SafeFreeHGlobal(Queue{IntPtr})"/> /// All the pointers, which will be refer to a new allocated memory /// (within the process of marshalling the string to the pointers), /// will be added to this queue</param> /// <typeparam name="TSourceClassElement">Type of the elements from <see cref="sourceList"/>, /// which will be converted</typeparam> /// <typeparam name="TDestinationStructElement">Type of the result structure, /// where the element with the type <see cref="TSourceClassElement"/> will be converted to</typeparam> /// <exception cref="ArgumentNullException">Thrown, if the <see cref="sourceList"/> /// and/or <see cref="allocatedPointers"/> are null</exception> /// <returns>An <see cref="AGDnsApi.ag_list"/> instance</returns> internal static AGDnsApi.ag_list ListToAgList <TSourceClassElement, TDestinationStructElement>( List <TSourceClassElement> sourceList, Func <TSourceClassElement, Queue <IntPtr>, TDestinationStructElement> converterFunc, Queue <IntPtr> allocatedPointers) where TDestinationStructElement : struct { if (sourceList == null) { throw new ArgumentNullException("sourceList"); } if (allocatedPointers == null) { throw new ArgumentNullException("allocatedPointers"); } List <TDestinationStructElement> listC = new List <TDestinationStructElement>(); foreach (TSourceClassElement element in sourceList) { TDestinationStructElement elementC = converterFunc(element, allocatedPointers); listC.Add(elementC); } IntPtr pListC = StructureListToPtr(listC, allocatedPointers); AGDnsApi.ag_list agListC = new AGDnsApi.ag_list { entries = pListC, num_entries = (uint)sourceList.Count }; return(agListC); }
private static AGDnsApi.ag_filter_engine_params ToNativeObject( EngineParams engineParams, Queue <IntPtr> allocatedPointers) { AGDnsApi.ag_list filterParamsC = MarshalUtils.ListToAgList( engineParams.FilterParams, ToNativeObject, allocatedPointers); AGDnsApi.ag_filter_engine_params filterEngineParamsC = new AGDnsApi.ag_filter_engine_params { filters = filterParamsC }; return(filterEngineParamsC); }
private static AGDnsApi.ag_dns64_settings ToNativeObject( Dns64Settings dns64, Queue <IntPtr> allocatedPointers) { AGDnsApi.ag_list dns64upstreamsC = MarshalUtils.ListToAgList( dns64.Upstreams, ToNativeObject, allocatedPointers); AGDnsApi.ag_dns64_settings dns64C = new AGDnsApi.ag_dns64_settings { upstreams = dns64upstreamsC }; MarshalUtils.CopyPropertiesToFields(dns64, ref dns64C); return(dns64C); }
/// <summary> /// Converts the specified <see cref="sourceAgList"/> with the type <see cref="AGDnsApi.ag_list"/> /// and which points to the array of structures with the type <see cref="TSourceStructElement"/>. /// to the <see cref="List{T}"/> with elements with the type <see cref="TDestinationClassElement"/> /// </summary> /// <param name="sourceAgList">An <see cref="AGDnsApi.ag_list"/> instance</param> /// <param name="converterFunc">Function to convert element with type <see cref="TSourceStructElement"/> /// to the struct with the type <see cref="TDestinationClassElement"/></param> /// <typeparam name="TSourceStructElement">Type of the result structure, /// which will be converted</typeparam> /// <typeparam name="TDestinationClassElement">Type of the result element, /// where the element with the type <see cref="TSourceStructElement"/> will be converted to</typeparam> /// <returns>An <see cref="AGDnsApi.ag_list"/> instance</returns> internal static List <TDestinationClassElement> AgListToList <TSourceStructElement, TDestinationClassElement>( AGDnsApi.ag_list sourceAgList, Func <TSourceStructElement, TDestinationClassElement> converterFunc) where TSourceStructElement : struct { List <TSourceStructElement> listC = PtrToStructureList <TSourceStructElement>(sourceAgList.entries, (int)sourceAgList.num_entries); List <TDestinationClassElement> list = new List <TDestinationClassElement>(); foreach (TSourceStructElement elementC in listC) { TDestinationClassElement element = converterFunc(elementC); list.Add(element); } return(list); }
/// <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) { AGDnsApi.ag_list upstreamsC = MarshalUtils.ListToAgList( dnsProxySettings.Upstreams, ToNativeObject, allocatedPointers); AGDnsApi.ag_list fallbacksC = MarshalUtils.ListToAgList( dnsProxySettings.Fallbacks, ToNativeObject, 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); AGDnsApi.ag_list listenersC = MarshalUtils.ListToAgList( dnsProxySettings.Listeners, ToNativeObject, allocatedPointers); AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC = new AGDnsApi.ag_dnsproxy_settings { upstreams = upstreamsC, fallbacks = fallbacksC, pDns64 = pDns64C, FilterParams = filterEngineParamsC, listeners = listenersC, }; MarshalUtils.CopyPropertiesToFields(dnsProxySettings, ref dnsProxySettingsC); MarshalUtils.AllStringsToPtrs(dnsProxySettings, ref dnsProxySettingsC, allocatedPointers); return(dnsProxySettingsC); }