Replaced P/Invoke DNS implementation by HijdenDNS pure C# MIT-licensed library, to hopefully prevent crashes caused by WebApp feature.

This commit is contained in:
Patrick Simpson 2017-02-01 10:46:35 +01:00
parent efb9c3aa60
commit f6495a12f5
4 changed files with 15 additions and 71 deletions

View File

@ -133,8 +133,12 @@
-->
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="Heijden.Dns, Version=2.0.0.6, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Heijden.Dns.2.0.0\lib\net35\Heijden.Dns.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.2.3\lib\net45\NLog.dll</HintPath>
<HintPath>..\packages\NLog.4.4.1\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" />

View File

@ -124,7 +124,7 @@ namespace Acacia.Features.WebApp
private string PerformAutoDiscover(ZPushAccount account)
{
// Fetch the txt record
List<string> txt = DnsUtil.GetTxtRecord(account.DomainName);
IList<string> txt = DnsUtil.GetTxtRecord(account.DomainName);
if (txt == null)
return null;

View File

@ -1,4 +1,6 @@
/// Copyright 2016 Kopano b.v.

using Heijden.DNS;
/// Copyright 2016 Kopano b.v.
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License, version 3,
@ -13,7 +15,6 @@
/// along with this program.If not, see<http://www.gnu.org/licenses/>.
///
/// Consult LICENSE file for details
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -26,73 +27,11 @@ namespace Acacia.Utils
{
public static class DnsUtil
{
public static List<string> GetTxtRecord(string name)
public static IList<string> GetTxtRecord(string name)
{
const Int16 DNS_TYPE_TEXT = 0x0010;
const Int32 DNS_QUERY_STANDARD = 0x00000000;
const Int32 DNS_ERROR_RCODE_NAME_ERROR = 9003;
const Int32 DNS_INFO_NO_RECORDS = 9501;
var queryResultsSet = IntPtr.Zero;
try
{
var dnsStatus = DnsQuery(
name,
DNS_TYPE_TEXT,
DNS_QUERY_STANDARD,
IntPtr.Zero,
ref queryResultsSet,
IntPtr.Zero
);
if (dnsStatus == DNS_ERROR_RCODE_NAME_ERROR || dnsStatus == DNS_INFO_NO_RECORDS)
return null;
if (dnsStatus != 0)
throw new Win32Exception(dnsStatus);
DnsRecordTxt dnsRecord;
var lines = new List<String>();
for (var pointer = queryResultsSet; pointer != IntPtr.Zero; pointer = dnsRecord.pNext)
{
dnsRecord = (DnsRecordTxt)Marshal.PtrToStructure(pointer, typeof(DnsRecordTxt));
if (dnsRecord.wType == DNS_TYPE_TEXT)
{
var stringArrayPointer = pointer + Marshal.OffsetOf(typeof(DnsRecordTxt), "pStringArray").ToInt32();
for (var i = 0; i < dnsRecord.dwStringCount; ++i)
{
var stringPointer = (IntPtr)Marshal.PtrToStructure(stringArrayPointer, typeof(IntPtr));
lines.Add(Marshal.PtrToStringUni(stringPointer));
stringArrayPointer += IntPtr.Size;
}
}
}
if (lines.Count == 0)
return null;
return lines;
}
finally
{
const Int32 DnsFreeRecordList = 1;
if (queryResultsSet != IntPtr.Zero)
DnsRecordListFree(queryResultsSet, DnsFreeRecordList);
}
}
[DllImport("Dnsapi.dll", EntryPoint = "DnsQuery_W", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
static extern Int32 DnsQuery(String lpstrName, Int16 wType, Int32 options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
[DllImport("Dnsapi.dll")]
static extern void DnsRecordListFree(IntPtr pRecordList, Int32 freeType);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DnsRecordTxt
{
public IntPtr pNext;
public String pName;
public Int16 wType;
public Int16 wDataLength;
public Int32 flags;
public Int32 dwTtl;
public Int32 dwReserved;
public Int32 dwStringCount;
public String pStringArray;
Resolver resolver = new Resolver();
Response response = resolver.Query(name, QType.TXT, QClass.IN);
return response.RecordsTXT.Select(r => r.ToString()).ToList();
}
}
}

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="4.2.3" targetFramework="net452" />
<package id="Heijden.Dns" version="2.0.0" targetFramework="net452" />
<package id="NLog" version="4.4.1" targetFramework="net452" />
</packages>