/// <summary> /// Gets the DNS stamp string specified by <see cref="DnsStamp"/> object /// </summary> /// <param name="dnsStamp">DNS stamp object /// (<seealso cref="DnsStamp"/>)</param> /// <returns>DNS stamp as a string</returns> public static string GetDnsStampString(DnsStamp dnsStamp) { // Don't invoke "dnsStamp.ToString()" within this method to prevent infinite recursion Logger.Verbose("Start getting DNS stamp string from {0}", dnsStamp.ServerAddress); IntPtr pDnsStampString = IntPtr.Zero; Queue <IntPtr> allocatedPointers = new Queue <IntPtr>(); try { AGDnsApi.ag_dns_stamp dnsStampC = DnsApiConverter.ToNativeObject(dnsStamp, allocatedPointers); IntPtr pDnsStampC = MarshalUtils.StructureToPtr(dnsStampC, allocatedPointers); pDnsStampString = AGDnsApi.ag_dns_stamp_to_str(pDnsStampC); string dnsStampString = MarshalUtils.PtrToString(pDnsStampString); Logger.Verbose("Getting DNS stamp string has been successfully completed"); return(dnsStampString); } catch (Exception ex) { Logger.Verbose("Getting DNS stamp string failed with an error {0}", ex); return(null); } finally { MarshalUtils.SafeFreeHGlobal(allocatedPointers); AGDnsApi.ag_str_free(pDnsStampString); } }
/// <summary> /// Gets the DNS stamp pretty url specified by <see cref="DnsStamp"/> object /// </summary> /// <param name="dnsStamp">DNS stamp object /// (<seealso cref="DnsStamp"/>)</param> /// <returns>DNS stamp as a string</returns> public static string GetDnsStampPrettyUrl(DnsStamp dnsStamp) { IntPtr pPrettyUrl = IntPtr.Zero; Queue <IntPtr> allocatedPointers = new Queue <IntPtr>(); try { AGDnsApi.ag_dns_stamp dnsStampC = DnsApiConverter.ToNativeObject(dnsStamp, allocatedPointers); IntPtr pDnsStampC = MarshalUtils.StructureToPtr(dnsStampC, allocatedPointers); pPrettyUrl = AGDnsApi.ag_dns_stamp_pretty_url(pDnsStampC); string prettyUrl = MarshalUtils.PtrToString(pPrettyUrl); return(prettyUrl); } catch (Exception ex) { Logger.Verbose("Getting DNS stamp pretty url failed with an error {0}", ex); return(null); } finally { MarshalUtils.SafeFreeHGlobal(allocatedPointers); AGDnsApi.ag_str_free(pPrettyUrl); } }
public void TestDnsStampConverter() { DnsStamp dnsStamp = new DnsStamp { ProtoType = new AGDnsApi.ag_stamp_proto_type(), ServerAddress = "addressTest", ProviderName = "Nametest", DoHPath = "DoHPathTest", Hashes = new List <byte[]> { new byte[] { 12, 34, 15 }, new byte[] { 10, 8, 16, 3 } }, Properties = new AGDnsApi.ag_server_informal_properties() }; Queue <IntPtr> allocatedPointers = new Queue <IntPtr>(); AGDnsApi.ag_dns_stamp stampNative = DnsApiConverter.ToNativeObject(dnsStamp, allocatedPointers); string address = MarshalUtils.PtrToString(stampNative.ServerAddress); Assert.AreEqual(address, dnsStamp.ServerAddress); DnsStamp dnsStampConverted = DnsApiConverter.FromNativeObject(stampNative); Assert.AreEqual(dnsStamp.ServerAddress, dnsStampConverted.ServerAddress); Assert.AreEqual(dnsStamp.Hashes.Count, dnsStampConverted.Hashes.Count); for (int i = 0; i < dnsStamp.Hashes.Count; i++) { Assert.IsTrue(CollectionUtils.CollectionsEquals(dnsStamp.Hashes[i], dnsStampConverted.Hashes[i])); } Assert.AreEqual(dnsStamp.Hashes.Capacity, dnsStampConverted.Hashes.Capacity); }
internal static DnsStamp FromNativeObject(AGDnsApi.ag_dns_stamp agDnsStampC) { DnsStamp dnsStamp = new DnsStamp(); MarshalUtils.AllPtrsToStrings(agDnsStampC, dnsStamp); MarshalUtils.CopyFieldsToProperties(agDnsStampC, dnsStamp); return(dnsStamp); }
internal static DnsStamp FromNativeObject(AGDnsApi.ag_dns_stamp agDnsStampC) { byte[] publicKey = MarshalUtils.AgBufferToBytes(agDnsStampC.server_public_key); List <byte[]> hashes = MarshalUtils.AgListToList <MarshalUtils.ag_buffer, byte[]>( agDnsStampC.hashes, MarshalUtils.AgBufferToBytes); DnsStamp dnsStamp = new DnsStamp { PublicKey = publicKey, Hashes = hashes }; MarshalUtils.AllPtrsToStrings(agDnsStampC, dnsStamp); MarshalUtils.CopyFieldsToProperties(agDnsStampC, dnsStamp); return(dnsStamp); }
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С); }
/// <summary> /// Parses a specified DNS stamp string (<seealso cref="dnsStampStr"/>) /// into the <see cref="DnsStamp"/> object. /// </summary> /// <param name="dnsStampStr">DNS stamp string</param> /// <returns>DNS stamp as a <see cref="DnsStamp"/> instance or null if smth went wrong</returns> public static DnsStamp ParseDnsStamp(string dnsStampStr) { Logger.Info("Start parsing DNS stamp {0}", dnsStampStr); IntPtr ppError = IntPtr.Zero; IntPtr pError = IntPtr.Zero; IntPtr pDnsStampResult = IntPtr.Zero; try { ppError = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); pDnsStampResult = AGDnsApi.ag_dns_stamp_from_str(dnsStampStr, ppError); if (pDnsStampResult == IntPtr.Zero) { pError = MarshalUtils.SafeReadIntPtr(ppError); string error = MarshalUtils.PtrToString(pError); Logger.Info("Parsing DNS stamp {0} failed with an error {1}", dnsStampStr, error); return(null); } AGDnsApi.ag_dns_stamp dnsStampResult = MarshalUtils.PtrToStructure <AGDnsApi.ag_dns_stamp>(pDnsStampResult); DnsStamp dnsStamp = DnsApiConverter.FromNativeObject(dnsStampResult); Logger.Info("Parsing DNS stamp has been completed successfully"); return(dnsStamp); } catch (Exception ex) { Logger.Info("Parsing DNS stamp failed with an error {0}", ex); return(null); } finally { AGDnsApi.ag_dns_stamp_free(pDnsStampResult); AGDnsApi.ag_str_free(pError); MarshalUtils.SafeFreeHGlobal(ppError); } }