Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Merge pull request #3471 from sokket/pal
Browse files Browse the repository at this point in the history
Shim'ing out GetNameInfo, get/free addrinfo, and GetHostName
  • Loading branch information
Jonathan Miller committed Oct 1, 2015
2 parents 0d203d3 + f319198 commit 5da58c0
Show file tree
Hide file tree
Showing 35 changed files with 788 additions and 475 deletions.
53 changes: 0 additions & 53 deletions src/Common/src/Interop/Linux/libc/Interop.addrinfo.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/Common/src/Interop/Linux/libc/Interop.h_errno.cs

This file was deleted.

53 changes: 0 additions & 53 deletions src/Common/src/Interop/OSX/libc/Interop.addrinfo.cs

This file was deleted.

15 changes: 0 additions & 15 deletions src/Common/src/Interop/OSX/libc/Interop.h_errno.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

internal static partial class Interop
{
internal static partial class libc
internal static partial class Sys
{
[DllImport(Libraries.Libc, SetLastError = true)]
private static extern unsafe int gethostname(byte* name, int len);
[DllImport(Libraries.SystemNative, SetLastError = true)]
private unsafe static extern int GetHostName(byte* name, int nameLength);

internal static unsafe string gethostname()
internal static unsafe string GetHostName()
{
const int HOST_NAME_MAX = 255; // man gethostname
const int HOST_NAME_MAX = 255;
const int ArrLength = HOST_NAME_MAX + 1;

byte* name = stackalloc byte[ArrLength];
int err = gethostname(name, ArrLength);
int err = GetHostName(name, ArrLength);
if (err != 0)
{
// This should never happen. According to the man page,
Expand All @@ -31,7 +30,6 @@ internal static unsafe string gethostname()
throw new InvalidOperationException(string.Format("gethostname returned {0}", err));
}

// Marshal.PtrToStringAnsi uses UTF8 on Unix.
return Marshal.PtrToStringAnsi((IntPtr)name);
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/Common/src/Interop/Unix/System.Native/Interop.GetNameInfo.cs
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 src/Common/src/Interop/Unix/System.Native/Interop.HostEntries.cs
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 src/Common/src/Interop/Unix/System.Native/Interop.IPAddress.cs
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);
}
}
}
15 changes: 0 additions & 15 deletions src/Common/src/Interop/Unix/libc/Interop.freeaddrinfo.cs

This file was deleted.

15 changes: 0 additions & 15 deletions src/Common/src/Interop/Unix/libc/Interop.getaddrinfo.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/Common/src/Interop/Unix/libc/Interop.getnameinfo.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Common/src/Interop/Unix/libc/Interop.hostent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ internal static partial class Interop
{
internal static partial class libc
{
public const int HOST_NOT_FOUND = 1;
public const int TRY_AGAIN = 2;
public const int NO_RECOVERY = 3;
public const int NO_DATA = 4;
public const int NO_ADDRESS = NO_DATA;

// Disable CS0169 (The field 'Interop.libc.hostent.h_length' is never used) and CS0649
// (Field 'Interop.libc.hostent.sa_family' is never assigned to, and will always have its
// default value 0)
Expand Down
6 changes: 6 additions & 0 deletions src/Common/src/Interop/Unix/libc/Interop.socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ internal static partial class Interop
{
internal static partial class libc
{
internal const int SOCK_STREAM = 1;
internal const int SOCK_DGRAM = 2;
internal const int SOCK_RAW = 3;
internal const int SOCK_RDM = 4;
internal const int SOCK_SEQPACKET = 5;

[DllImport(Libraries.Libc, SetLastError = true)]
internal static extern int socket(int domain, int type, int protocol);
}
Expand Down
Loading

0 comments on commit 5da58c0

Please sign in to comment.