Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/libraries/Common/src/Interop/OSX/Interop.Dnssd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static unsafe partial class Dnssd
{
internal const uint kDNSServiceFlagsMoreComing = 0x1;
internal const uint kDNSServiceFlagsAdd = 0x2;
internal const uint kDNSServiceFlagsReturnIntermediates = 0x1000;
internal const uint kDNSServiceFlagsTimeout = 0x10000;

internal const int kDNSServiceErr_NoError = 0;
internal const int kDNSServiceErr_Unknown = -65537;
internal const int kDNSServiceErr_NoSuchName = -65538;
internal const int kDNSServiceErr_NoMemory = -65539;
internal const int kDNSServiceErr_BadParam = -65540;
internal const int kDNSServiceErr_Unsupported = -65544;
internal const int kDNSServiceErr_Refused = -65553;
internal const int kDNSServiceErr_NoSuchRecord = -65554;
internal const int kDNSServiceErr_ServiceNotRunning = -65563;
internal const int kDNSServiceErr_Timeout = -65568;
internal const int kDNSServiceErr_DefunctConnection = -65569;
internal const int kDNSServiceErr_PolicyDenied = -65570;
internal const int kDNSServiceErr_NotPermitted = -65571;

internal const ushort kDNSServiceClass_IN = 1;

internal const ushort kDNSServiceType_A = 1;
internal const ushort kDNSServiceType_NS = 2;
internal const ushort kDNSServiceType_CNAME = 5;
internal const ushort kDNSServiceType_PTR = 12;
internal const ushort kDNSServiceType_MX = 15;
internal const ushort kDNSServiceType_TXT = 16;
internal const ushort kDNSServiceType_AAAA = 28;
internal const ushort kDNSServiceType_SRV = 33;

[LibraryImport(Libraries.libSystem, StringMarshalling = StringMarshalling.Utf8)]
internal static partial int DNSServiceQueryRecord(
out SafeDnsServiceHandle sdRef,
uint flags,
uint interfaceIndex,
string fullname,
ushort rrtype,
ushort rrclass,
delegate* unmanaged[Cdecl]<IntPtr, uint, uint, int, byte*, ushort, ushort, ushort, void*, uint, IntPtr, void> callBack,
IntPtr context);

[LibraryImport(Libraries.libSystem)]
internal static partial int DNSServiceRefSockFD(SafeDnsServiceHandle sdRef);

[LibraryImport(Libraries.libSystem)]
internal static partial int DNSServiceProcessResult(SafeDnsServiceHandle sdRef);

[LibraryImport(Libraries.libSystem)]
internal static partial void DNSServiceRefDeallocate(IntPtr sdRef);
}
}

internal sealed class SafeDnsServiceHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public SafeDnsServiceHandle() : base(ownsHandle: true) { }

protected override bool ReleaseHandle()
{
Interop.Dnssd.DNSServiceRefDeallocate(handle);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-wasi;$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-wasi;$(NetCoreAppCurrent)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
</PropertyGroup>
Expand Down Expand Up @@ -86,17 +86,52 @@
Link="Common\Interop\Windows\WinSock\Interop.GetAddrInfoExW.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix'">
<Compile Include="System\Net\NameResolutionPal.Unix.cs" />
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix' Or '$(TargetPlatformIdentifier)' == 'osx'">
<Compile Include="System\Net\DnsResolverPal.Managed.cs" />
<Compile Include="System\Net\DnsSocket.cs" />
<Compile Include="System\Net\ResolvConf.cs" />
<Compile Include="System\Net\DnsWireEnums.cs" />
<Compile Include="System\Net\DnsMessageHeader.cs" />
<Compile Include="System\Net\DnsMessageReader.cs" />
<Compile Include="System\Net\DnsMessageWriter.cs" />
<Compile Include="System\Net\DnsEncodedName.cs" />
<Compile Include="System\Net\DnsRecordParsing.cs" />
<Compile Include="System\Net\ResolvConf.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix'">
<Compile Include="System\Net\NameResolutionPal.Unix.cs" />
<Compile Include="$(CommonPath)System\Net\InteropIPAddressExtensions.Unix.cs"
Link="Common\System\Net\InteropIPAddressExtensions.Unix.cs" />
<Compile Include="$(CommonPath)System\Net\SocketAddressPal.Unix.cs"
Link="Common\System\Net\Internals\SocketAddressPal.Unix.cs" />
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.Unix.cs"
Link="Common\System\Net\SocketProtocolSupportPal.Unix" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs"
Link="Common\Interop\CoreLib\Unix\Interop.Errors.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ErrNo.cs"
Link="Common\Interop\Unix\System.Native\Interop.ErrNo.cs"/>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Close.cs"
Link="Common\Interop\Unix\System.Native\Interop.Close.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs"
Link="Common\Interop\Unix\System.Native\Interop.GetHostName.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetNameInfo.cs"
Link="Common\Interop\Unix\System.Native\Interop.GetNameInfo.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.HostEntry.cs"
Link="Common\Interop\Unix\System.Native\Interop.HostEntries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.IPAddress.cs"
Link="Common\Interop\Unix\System.Native\Interop.IPAddress.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Socket.cs"
Link="Common\Interop\Unix\System.Native\Interop.Socket.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SocketAddress.cs"
Link="Common\Interop\Unix\System.Native\Interop.SocketAddress.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'osx'">
<Compile Include="System\Net\NameResolutionPal.Unix.cs" />
<Compile Include="System\Net\DnsResolverPal.OSX.cs" />
<Compile Include="System\Net\DnsSdRecordParsing.cs" />
<Compile Include="$(CommonPath)System\Net\InteropIPAddressExtensions.Unix.cs"
Link="Common\System\Net\InteropIPAddressExtensions.Unix.cs" />
<Compile Include="$(CommonPath)System\Net\SocketAddressPal.Unix.cs"
Expand All @@ -123,6 +158,10 @@
Link="Common\Interop\Unix\System.Native\Interop.Socket.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SocketAddress.cs"
Link="Common\Interop\Unix\System.Native\Interop.SocketAddress.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.Dnssd.cs"
Link="Common\Interop\OSX\Interop.Dnssd.cs" />
<Compile Include="$(CommonPath)Interop\OSX\Interop.Libraries.cs"
Link="Common\Interop\OSX\Interop.Libraries.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'browser'">
Expand Down Expand Up @@ -177,7 +216,7 @@
<ProjectReference Include="$(LibrariesProjectRoot)System.Threading.ThreadPool\src\System.Threading.ThreadPool.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix'">
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix' Or '$(TargetPlatformIdentifier)' == 'osx'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Cryptography\src\System.Security.Cryptography.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public bool MoveNext()
}

public readonly DnsTxtEnumerator GetEnumerator() => this;

public readonly bool IsValid => _remaining.IsEmpty;
}

internal readonly ref struct DnsPtrRecordData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,13 @@ private static TimeSpan MinNonZero(TimeSpan x, TimeSpan y)
private static void ValidateName(string name)
{
ArgumentException.ThrowIfNullOrEmpty(name);
// Every underlying resolver (Windows DnsQueryEx, macOS DNSServiceQueryRecord,
// and the managed stub resolver on Linux) passes the name to native code as a
// null-terminated string, so an embedded NUL would silently truncate the query.
if (name.Contains('\0'))
{
throw new ArgumentException(SR.net_hostname_invalid_character, nameof(name));
}
}

/// <summary>
Expand Down
Loading
Loading