| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Numerics; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace System.Net.NetworkInformation |
| | | 10 | | { |
| | | 11 | | internal static class InterfaceInfoPal |
| | | 12 | | { |
| | | 13 | | // Measured in characters (the native data type for this platform's API.) |
| | | 14 | | private const int StackAllocationThreshold = 256; |
| | | 15 | | |
| | | 16 | | public static unsafe uint InterfaceNameToIndex<TChar>(ReadOnlySpan<TChar> interfaceName) |
| | | 17 | | where TChar : unmanaged, IBinaryNumber<TChar> |
| | 268 | 18 | | { |
| | 268 | 19 | | Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte)); |
| | | 20 | | |
| | | 21 | | // Measured in characters, including null terminator. |
| | 268 | 22 | | int bufferSize = 0; |
| | 268 | 23 | | char* nativeMemory = null; |
| | 268 | 24 | | ulong interfaceLuid = 0; |
| | 268 | 25 | | uint interfaceIndex = 0; |
| | | 26 | | |
| | | 27 | | // The underlying API for this method accepts a null-terminated Unicode string containing the interface name |
| | | 28 | | // If TChar is char, the only work required is a byte copy. If TChar is byte, there's a transcoding step fro |
| | | 29 | | // to Unicode. |
| | | 30 | | // Notably: the encoding passed to the underlying API is different between Linux and Windows. |
| | 268 | 31 | | if (typeof(TChar) == typeof(char)) |
| | 134 | 32 | | { |
| | 134 | 33 | | bufferSize = interfaceName.Length + 1; |
| | 134 | 34 | | } |
| | 134 | 35 | | else if (typeof(TChar) == typeof(byte)) |
| | 134 | 36 | | { |
| | 134 | 37 | | ReadOnlySpan<byte> castInterfaceName = MemoryMarshal.Cast<TChar, byte>(interfaceName); |
| | | 38 | | |
| | 134 | 39 | | bufferSize = Encoding.UTF8.GetCharCount(castInterfaceName) + 1; |
| | 134 | 40 | | } |
| | 268 | 41 | | Debug.Assert(bufferSize <= int.MaxValue - 1); |
| | | 42 | | |
| | | 43 | | try |
| | 268 | 44 | | { |
| | 268 | 45 | | nativeMemory = (uint)bufferSize <= StackAllocationThreshold ? null : (char*)NativeMemory.Alloc((nuint)(b |
| | | 46 | | |
| | 268 | 47 | | Span<char> buffer = nativeMemory == null ? stackalloc char[StackAllocationThreshold].Slice(0, bufferSize |
| | | 48 | | |
| | 268 | 49 | | if (typeof(TChar) == typeof(char)) |
| | 134 | 50 | | { |
| | 134 | 51 | | ReadOnlySpan<char> castInterfaceName = MemoryMarshal.Cast<TChar, char>(interfaceName); |
| | | 52 | | |
| | 134 | 53 | | castInterfaceName.CopyTo(buffer); |
| | 134 | 54 | | } |
| | 134 | 55 | | else if (typeof(TChar) == typeof(byte)) |
| | 134 | 56 | | { |
| | 134 | 57 | | ReadOnlySpan<byte> castInterfaceName = MemoryMarshal.Cast<TChar, byte>(interfaceName); |
| | | 58 | | |
| | 134 | 59 | | Encoding.UTF8.GetChars(castInterfaceName, buffer); |
| | 134 | 60 | | } |
| | 268 | 61 | | buffer[buffer.Length - 1] = '\0'; |
| | | 62 | | |
| | 268 | 63 | | if (Interop.IpHlpApi.ConvertInterfaceNameToLuid(buffer, ref interfaceLuid) != 0) |
| | 262 | 64 | | { |
| | 262 | 65 | | return 0; |
| | | 66 | | } |
| | 6 | 67 | | } |
| | | 68 | | finally |
| | 268 | 69 | | { |
| | 268 | 70 | | if (nativeMemory != null) |
| | 16 | 71 | | { |
| | 16 | 72 | | NativeMemory.Free(nativeMemory); |
| | 16 | 73 | | } |
| | 268 | 74 | | } |
| | | 75 | | |
| | 6 | 76 | | return Interop.IpHlpApi.ConvertInterfaceLuidToIndex(interfaceLuid, ref interfaceIndex) == 0 |
| | 6 | 77 | | ? interfaceIndex |
| | 6 | 78 | | : 0; |
| | 268 | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |