< Summary

Information
Class: System.Net.NetworkInformation.InterfaceInfoPal
Assembly: System.Net.Primitives
File(s): D:\runner\runtime\src\libraries\Common\src\System\Net\NetworkInformation\InterfaceInfoPal.Windows.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 81
Line coverage: 100%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
InterfaceNameToIndex(...)95%2020100%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Net\NetworkInformation\InterfaceInfoPal.Windows.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Numerics;
 6using System.Runtime.InteropServices;
 7using System.Text;
 8
 9namespace 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>
 18418        {
 18419            Debug.Assert(typeof(TChar) == typeof(char) ||  typeof(TChar) == typeof(byte));
 20
 21            // Measured in characters, including null terminator.
 18422            int bufferSize = 0;
 18423            char* nativeMemory = null;
 18424            ulong interfaceLuid = 0;
 18425            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.
 18431            if (typeof(TChar) == typeof(char))
 9232            {
 9233                bufferSize = interfaceName.Length + 1;
 9234            }
 9235            else if (typeof(TChar) == typeof(byte))
 9236            {
 9237                ReadOnlySpan<byte> castInterfaceName = MemoryMarshal.Cast<TChar, byte>(interfaceName);
 38
 9239                bufferSize = Encoding.UTF8.GetCharCount(castInterfaceName) + 1;
 9240            }
 18441            Debug.Assert(bufferSize <= int.MaxValue - 1);
 42
 43            try
 18444            {
 18445                nativeMemory = (uint)bufferSize <= StackAllocationThreshold ? null : (char*)NativeMemory.Alloc((nuint)(b
 46
 18447                Span<char> buffer = nativeMemory == null ? stackalloc char[StackAllocationThreshold].Slice(0, bufferSize
 48
 18449                if (typeof(TChar) == typeof(char))
 9250                {
 9251                    ReadOnlySpan<char> castInterfaceName = MemoryMarshal.Cast<TChar, char>(interfaceName);
 52
 9253                    castInterfaceName.CopyTo(buffer);
 9254                }
 9255                else if (typeof(TChar) == typeof(byte))
 9256                {
 9257                    ReadOnlySpan<byte> castInterfaceName = MemoryMarshal.Cast<TChar, byte>(interfaceName);
 58
 9259                    Encoding.UTF8.GetChars(castInterfaceName, buffer);
 9260                }
 18461                buffer[buffer.Length - 1] = '\0';
 62
 18463                if (Interop.IpHlpApi.ConvertInterfaceNameToLuid(buffer, ref interfaceLuid) != 0)
 16464                {
 16465                    return 0;
 66                }
 2067            }
 68            finally
 18469            {
 18470                if (nativeMemory != null)
 671                {
 672                    NativeMemory.Free(nativeMemory);
 673                }
 18474            }
 75
 2076            return Interop.IpHlpApi.ConvertInterfaceLuidToIndex(interfaceLuid, ref interfaceIndex) == 0
 2077                ? interfaceIndex
 2078                : 0;
 18479        }
 80    }
 81}