| | | 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.Buffers.Binary; |
| | | 5 | | using System.Net.Sockets; |
| | | 6 | | |
| | | 7 | | namespace 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) |
| | 0 | 17 | | { |
| | 0 | 18 | | return (AddressFamily)BitConverter.ToInt16(buffer); |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public static void SetAddressFamily(Span<byte> buffer, AddressFamily family) |
| | 0 | 22 | | { |
| | 0 | 23 | | if ((int)(family) > ushort.MaxValue) |
| | 0 | 24 | | { |
| | | 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. |
| | 0 | 28 | | throw new PlatformNotSupportedException(); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | #if BIGENDIAN |
| | | 32 | | BinaryPrimitives.WriteUInt16BigEndian(buffer, (ushort)family); |
| | | 33 | | #else |
| | 0 | 34 | | BinaryPrimitives.WriteUInt16LittleEndian(buffer, (ushort)family); |
| | | 35 | | #endif |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public static ushort GetPort(ReadOnlySpan<byte> buffer) |
| | 0 | 39 | | => BinaryPrimitives.ReadUInt16BigEndian(buffer.Slice(2)); |
| | | 40 | | |
| | | 41 | | public static void SetPort(Span<byte> buffer, ushort port) |
| | 0 | 42 | | => BinaryPrimitives.WriteUInt16BigEndian(buffer.Slice(2), port); |
| | | 43 | | |
| | | 44 | | public static uint GetIPv4Address(ReadOnlySpan<byte> buffer) |
| | 0 | 45 | | => BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(4)); |
| | | 46 | | |
| | | 47 | | public static void GetIPv6Address(ReadOnlySpan<byte> buffer, Span<byte> address, out uint scope) |
| | 0 | 48 | | { |
| | 0 | 49 | | buffer.Slice(8, address.Length).CopyTo(address); |
| | | 50 | | |
| | 0 | 51 | | scope = BinaryPrimitives.ReadUInt32LittleEndian(buffer.Slice(24)); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | public static void SetIPv4Address(Span<byte> buffer, uint address) |
| | 0 | 55 | | { |
| | | 56 | | // IPv4 Address serialization |
| | 0 | 57 | | BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(4), address); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public static void SetIPv6Address(Span<byte> buffer, Span<byte> address, uint scope) |
| | 0 | 61 | | { |
| | | 62 | | // No handling for Flow Information |
| | 0 | 63 | | BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(4), 0); |
| | | 64 | | |
| | | 65 | | // Scope serialization |
| | 0 | 66 | | BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(24), scope); |
| | | 67 | | |
| | | 68 | | // Address serialization |
| | 0 | 69 | | address.CopyTo(buffer.Slice(8)); |
| | 0 | 70 | | } |
| | | 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 | | |