< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 80
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetAddressFamily(...)100%110%
SetAddressFamily(...)0%220%
GetPort(...)100%110%
SetPort(...)100%110%
GetIPv4Address(...)100%110%
GetIPv6Address(...)100%110%
SetIPv4Address(...)100%110%
SetIPv6Address(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/System/Net/SocketAddressPal.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.Buffers.Binary;
 5using System.Net.Sockets;
 6
 7namespace System.Net
 8{
 9    internal static class SocketAddressPal
 10    {
 11        public const int IPv6AddressSize = 28;
 12        public const int IPv4AddressSize = 16;
 13        public const int UdsAddressSize = 110;
 14        public const int MaxAddressSize = 128;
 15
 16        public static AddressFamily GetAddressFamily(ReadOnlySpan<byte> buffer)
 017        {
 018            return (AddressFamily)BitConverter.ToInt16(buffer);
 019        }
 20
 21        public static void SetAddressFamily(Span<byte> buffer, AddressFamily family)
 022        {
 023            if ((int)(family) > ushort.MaxValue)
 024            {
 25                // For legacy values family maps directly to Winsock value.
 26                // Other values will need mapping if/when supported.
 27                // Currently, that is Netlink, Packet and ControllerAreaNetwork, neither of them supported on Windows.
 028                throw new PlatformNotSupportedException();
 29            }
 30
 31#if BIGENDIAN
 32            BinaryPrimitives.WriteUInt16BigEndian(buffer, (ushort)family);
 33#else
 034            BinaryPrimitives.WriteUInt16LittleEndian(buffer, (ushort)family);
 35#endif
 036        }
 37
 38        public static ushort GetPort(ReadOnlySpan<byte> buffer)
 039            => BinaryPrimitives.ReadUInt16BigEndian(buffer.Slice(2));
 40
 41        public static void SetPort(Span<byte> buffer, ushort port)
 042            => BinaryPrimitives.WriteUInt16BigEndian(buffer.Slice(2), port);
 43
 44        public static uint GetIPv4Address(ReadOnlySpan<byte> buffer)
 045            => BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(4));
 46
 47        public static void GetIPv6Address(ReadOnlySpan<byte> buffer, Span<byte> address, out uint scope)
 048        {
 049            buffer.Slice(8, address.Length).CopyTo(address);
 50
 051            scope = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(24));
 052        }
 53
 54        public static void SetIPv4Address(Span<byte> buffer, uint address)
 055        {
 56            // IPv4 Address serialization
 057            BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(4), address);
 058        }
 59
 60        public static void SetIPv6Address(Span<byte> buffer, Span<byte> address, uint scope)
 061        {
 62            // No handling for Flow Information
 063            BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(4), 0);
 64
 65            // Scope serialization
 066            BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(24), scope);
 67
 68            // Address serialization
 069            address.CopyTo(buffer.Slice(8));
 070        }
 71
 72        public static void Clear(Span<byte> buffer)
 73        {
 74            AddressFamily family = GetAddressFamily(buffer);
 75            buffer.Clear();
 76            SetAddressFamily(buffer, family);
 77        }
 78    }
 79}
 80