This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3471 from sokket/pal
Shim'ing out GetNameInfo, get/free addrinfo, and GetHostName
- Loading branch information
Showing
35 changed files
with
788 additions
and
475 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Common/src/Interop/Unix/System.Native/Interop.GetNameInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[Flags] | ||
internal enum GetNameInfoFlags : int | ||
{ | ||
NI_NAMEREQD = 0x1, | ||
NI_NUMERICHOST = 0x2, | ||
} | ||
|
||
[DllImport(Libraries.SystemNative)] | ||
internal static unsafe extern int GetNameInfo( | ||
byte* address, | ||
uint addressLength, | ||
bool isIpv6, | ||
byte* host, | ||
uint hostLength, | ||
byte* service, | ||
uint serviceLength, | ||
GetNameInfoFlags flags); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Common/src/Interop/Unix/System.Native/Interop.HostEntries.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
internal const int NI_MAXHOST = 1025; | ||
internal const int NI_MAXSERV = 32; | ||
|
||
internal enum GetAddrInfoErrorFlags : int | ||
{ | ||
EAI_AGAIN = 1, // Temporary failure in name resolution. | ||
EAI_BADFLAGS = 2, // Invalid value for `ai_flags' field. | ||
EAI_FAIL = 3, // Non-recoverable failure in name resolution. | ||
EAI_FAMILY = 4, // 'ai_family' not supported. | ||
EAI_NONAME = 5, // NAME or SERVICE is unknown. | ||
} | ||
|
||
internal enum GetHostErrorCodes : int | ||
{ | ||
HOST_NOT_FOUND = 1, | ||
TRY_AGAIN = 2, | ||
NO_RECOVERY = 3, | ||
NO_DATA = 4, | ||
NO_ADDRESS = NO_DATA, | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal unsafe struct PalIpAddress | ||
{ | ||
internal byte* Address; // Buffer to fit IPv4 or IPv6 address | ||
internal int Count; // Number of bytes in the address buffer | ||
internal bool IsIpv6; // If this is an IPv6 Address or IPv4 | ||
private fixed byte padding[3]; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal unsafe struct HostEntry | ||
{ | ||
internal byte* CanonicalName; // Canonical Name of the Host | ||
internal PalIpAddress* Addresses; // List of IP Addresses associated with this host | ||
internal int Count; // Number of IP Addresses associated with this host | ||
private int reserved; | ||
} | ||
|
||
[DllImport(Libraries.SystemNative)] | ||
internal static unsafe extern int GetHostEntriesForName(string address, HostEntry** entry); | ||
|
||
[DllImport(Libraries.SystemNative)] | ||
internal static unsafe extern void FreeHostEntriesForName(HostEntry* entry); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Common/src/Interop/Unix/System.Native/Interop.IPAddress.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
internal const int IPv4AddressBytes = 4; | ||
internal const int IPv6AddressBytes = 16; | ||
|
||
internal const int INET_ADDRSTRLEN = 22; | ||
internal const int INET6_ADDRSTRLEN = 65; | ||
|
||
[DllImport(Libraries.SystemNative, SetLastError = true)] | ||
internal static extern int Ipv6StringToAddress(string address, string port, byte[] buffer, int bufferLength, out uint scope); | ||
|
||
[DllImport(Libraries.SystemNative, SetLastError = true)] | ||
internal static extern int Ipv4StringToAddress(string address, byte[] buffer, int bufferLength, out ushort port); | ||
|
||
[DllImport(Libraries.SystemNative)] | ||
internal unsafe static extern int IpAddressToString(byte* address, int addressLength, bool isIpv6, byte* str, int stringLength, uint scope = 0); | ||
|
||
internal unsafe static uint IpAddressToString(byte[] address, bool isIpV6, System.Text.StringBuilder addressString, uint scope = 0) | ||
{ | ||
Debug.Assert(address != null); | ||
Debug.Assert((address.Length == IPv4AddressBytes) || (address.Length == IPv6AddressBytes)); | ||
|
||
int err; | ||
fixed (byte* rawAddress = address) | ||
{ | ||
int bufferLength = isIpV6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN; | ||
byte* buffer = stackalloc byte[bufferLength]; | ||
err = IpAddressToString(rawAddress, address.Length, isIpV6, buffer, bufferLength, scope); | ||
if (err == 0) | ||
{ | ||
addressString.Append(Marshal.PtrToStringAnsi((IntPtr)buffer)); | ||
} | ||
} | ||
|
||
return unchecked((uint)err); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.