Пример #1
0
        internal static AGDnsApi.ag_upstream_options ToNativeObject(
            UpstreamOptions upstreamOptions,
            Queue <IntPtr> allocatedPointers)
        {
            MarshalUtils.ag_list bootstrapC = MarshalUtils.ListToAgList(
                upstreamOptions.Bootstrap,
                MarshalUtils.StringToPtr,
                allocatedPointers);

            byte[] addressBytes = null;
            if (upstreamOptions.ResolvedIpAddress != null)
            {
                addressBytes = upstreamOptions.ResolvedIpAddress.GetAddressBytes();
            }

            MarshalUtils.ag_buffer addressC = MarshalUtils.BytesToAgBuffer(addressBytes);

            if (allocatedPointers != null)
            {
                allocatedPointers.Enqueue(addressC.data);
            }

            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);
        }
Пример #2
0
        /// <summary>
        /// This method is called from the CoreLibs and passed to managed code
        /// </summary>
        /// <param name="attachment">Pointer to the native logger</param>
        /// <param name="logLevel">Log level</param>
        /// <param name="pMessage">Pointer to the log message</param>
        /// <param name="length">Message length</param>
        private static void AGOnDnsLogged(
            IntPtr attachment,
            AGDnsApi.ag_log_level logLevel,
            IntPtr pMessage,
            UInt32 length)
        {
            try
            {
                LogBylogLevel          logByLogLevel = LOG_LEVELS_MAPPING[logLevel];
                MarshalUtils.ag_buffer agBuffer      = new MarshalUtils.ag_buffer
                {
                    data = pMessage,
                    size = length
                };

                string message = MarshalUtils.AgBufferToString(agBuffer);
                // We have to forcibly trim trailing CR due to
                // https://bit.adguard.com/projects/ADGUARD-CORE-LIBS/repos/dns-libs/pull-requests/306/diff#platform/windows/capi/include/ag_dns.h
                message = message.TrimEnd(Environment.NewLine.ToCharArray());
                logByLogLevel(message);
            }
            catch (Exception ex)
            {
                DnsExceptionHandler.HandleManagedException(ex);
            }
        }
Пример #3
0
 public static AGDnsApi.ag_dns_stamp ToNativeObject(
     DnsStamp dnsStamp,
     Queue <IntPtr> allocatedPointers)
 {
     MarshalUtils.ag_buffer publicKeyC = MarshalUtils.BytesToAgBuffer(dnsStamp.PublicKey);
     MarshalUtils.ag_list   hashesC    = MarshalUtils.ListToAgList(
         dnsStamp.Hashes,
         (x, y) => MarshalUtils.BytesToAgBuffer(x),
         allocatedPointers);
     AGDnsApi.ag_dns_stamp dnsStampС = new AGDnsApi.ag_dns_stamp
     {
         ProtoType         = dnsStamp.ProtoType,
         ServerAddress     = MarshalUtils.StringToPtr(dnsStamp.ServerAddress),
         ProviderName      = MarshalUtils.StringToPtr(dnsStamp.ProviderName),
         DoHPath           = MarshalUtils.StringToPtr(dnsStamp.DoHPath),
         server_public_key = publicKeyC,
         hashes            = hashesC,
         Properties        = dnsStamp.Properties
     };
     return(dnsStampС);
 }
Пример #4
0
        /// <summary>
        /// Creates the <see cref="IPAddress"/> object from the specified pointer to address byte array,
        /// addressLength
        /// </summary>
        /// <param name="agAddress">The <see cref="MarshalUtils.ag_buffer"/> instance</param>
        /// <exception cref="ArgumentException">Thrown,
        /// if passed <see cref="MarshalUtils.ag_buffer.size"/> is not acceptable</exception>
        /// <returns><see cref="IPAddress"/> object or null if the pointer is null or addressLength is zero</returns>
        private static IPAddress CreateIpAddress(MarshalUtils.ag_buffer agAddress)
        {
            if (agAddress.data == IntPtr.Zero ||
                agAddress.size == 0)
            {
                return(null);
            }

            byte[] address = new byte[agAddress.size];
            Marshal.Copy(agAddress.data, address, 0, (int)agAddress.size);
            AddressFamily addressFamily = ADDRESSES_FAMILY_LENGTH
                                          .FirstOrDefault(addressPair => addressPair.Value == agAddress.size).Key;

            if (addressFamily == AddressFamily.Unknown)
            {
                string message = "Cannot create IPAddress because of unacceptable address length value";
                throw new ArgumentException(message, "agAddress");
            }

            IPAddress ipAddress = new IPAddress(address);

            return(ipAddress);
        }
Пример #5
0
 internal static extern void ag_buffer_free([MarshalAs(UnmanagedType.Struct)] MarshalUtils.ag_buffer buf);