| | | 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.Net.Sockets; |
| | | 6 | | |
| | | 7 | | namespace System.Net |
| | | 8 | | { |
| | | 9 | | // This class is used when subclassing EndPoint, and provides indication |
| | | 10 | | // on how to format the memory buffers that the platform uses for network addresses. |
| | | 11 | | public class SocketAddress : IEquatable<SocketAddress> |
| | | 12 | | { |
| | | 13 | | #pragma warning disable CA1802 // these could be const on Windows but need to be static readonly for Unix |
| | 0 | 14 | | internal static readonly int IPv6AddressSize = SocketAddressPal.IPv6AddressSize; |
| | 0 | 15 | | internal static readonly int IPv4AddressSize = SocketAddressPal.IPv4AddressSize; |
| | 0 | 16 | | internal static readonly int UdsAddressSize = SocketAddressPal.UdsAddressSize; |
| | 0 | 17 | | internal static readonly int MaxAddressSize = SocketAddressPal.MaxAddressSize; |
| | | 18 | | #pragma warning restore CA1802 |
| | | 19 | | |
| | | 20 | | private int _size; |
| | | 21 | | private byte[] _buffer; |
| | | 22 | | |
| | | 23 | | private const int MinSize = 2; |
| | | 24 | | private const int DataOffset = 2; |
| | | 25 | | |
| | | 26 | | public AddressFamily Family |
| | | 27 | | { |
| | | 28 | | get |
| | 0 | 29 | | { |
| | 0 | 30 | | return SocketAddressPal.GetAddressFamily(_buffer); |
| | 0 | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public int Size |
| | | 35 | | { |
| | | 36 | | get |
| | 0 | 37 | | { |
| | 0 | 38 | | return _size; |
| | 0 | 39 | | } |
| | | 40 | | set |
| | 0 | 41 | | { |
| | 0 | 42 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(value, _buffer.Length); |
| | 0 | 43 | | ArgumentOutOfRangeException.ThrowIfLessThan(value, 0); |
| | 0 | 44 | | _size = value; |
| | 0 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // Access to unmanaged serialized data. This doesn't |
| | | 49 | | // allow access to the first 2 bytes of unmanaged memory |
| | | 50 | | // that are supposed to contain the address family which |
| | | 51 | | // is readonly. |
| | | 52 | | public byte this[int offset] |
| | | 53 | | { |
| | | 54 | | get |
| | 0 | 55 | | { |
| | 0 | 56 | | if ((uint)offset >= (uint)Size) |
| | 0 | 57 | | { |
| | 0 | 58 | | throw new IndexOutOfRangeException(); |
| | | 59 | | } |
| | 0 | 60 | | return _buffer[offset]; |
| | 0 | 61 | | } |
| | | 62 | | set |
| | 0 | 63 | | { |
| | 0 | 64 | | if ((uint)offset >= (uint)Size) |
| | 0 | 65 | | { |
| | 0 | 66 | | throw new IndexOutOfRangeException(); |
| | | 67 | | } |
| | 0 | 68 | | _buffer[offset] = value; |
| | 0 | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | public static int GetMaximumAddressSize(AddressFamily addressFamily) => addressFamily switch |
| | 0 | 73 | | { |
| | 0 | 74 | | AddressFamily.InterNetwork => IPv4AddressSize, |
| | 0 | 75 | | AddressFamily.InterNetworkV6 => IPv6AddressSize, |
| | 0 | 76 | | AddressFamily.Unix => UdsAddressSize, |
| | 0 | 77 | | _ => MaxAddressSize |
| | 0 | 78 | | }; |
| | | 79 | | |
| | 0 | 80 | | public SocketAddress(AddressFamily family) : this(family, GetMaximumAddressSize(family)) |
| | 0 | 81 | | { |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | public SocketAddress(AddressFamily family, int size) |
| | 0 | 85 | | { |
| | 0 | 86 | | ArgumentOutOfRangeException.ThrowIfLessThan(size, MinSize); |
| | | 87 | | |
| | 0 | 88 | | _size = size; |
| | 0 | 89 | | _buffer = new byte[size]; |
| | 0 | 90 | | _buffer[0] = (byte)_size; |
| | | 91 | | |
| | 0 | 92 | | SocketAddressPal.SetAddressFamily(_buffer, family); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | internal unsafe SocketAddress(IPAddress ipAddress) |
| | 0 | 96 | | : this(ipAddress.AddressFamily, |
| | 0 | 97 | | ((ipAddress.AddressFamily == AddressFamily.InterNetwork) ? IPv4AddressSize : IPv6AddressSize)) |
| | 0 | 98 | | { |
| | | 99 | | |
| | | 100 | | // No Port. |
| | 0 | 101 | | SocketAddressPal.SetPort(_buffer, 0); |
| | | 102 | | |
| | 0 | 103 | | if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6) |
| | 0 | 104 | | { |
| | 0 | 105 | | Span<byte> addressBytes = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes]; |
| | 0 | 106 | | ipAddress.TryWriteBytes(addressBytes, out int bytesWritten); |
| | 0 | 107 | | Debug.Assert(bytesWritten == IPAddressParserStatics.IPv6AddressBytes); |
| | | 108 | | |
| | 0 | 109 | | SocketAddressPal.SetIPv6Address(_buffer, addressBytes, (uint)ipAddress.ScopeId); |
| | 0 | 110 | | } |
| | | 111 | | else |
| | 0 | 112 | | { |
| | | 113 | | #pragma warning disable CS0618 // using Obsolete Address API because it's the more efficient option in this case |
| | 0 | 114 | | uint address = unchecked((uint)ipAddress.Address); |
| | | 115 | | #pragma warning restore CS0618 |
| | | 116 | | |
| | 0 | 117 | | Debug.Assert(ipAddress.AddressFamily == AddressFamily.InterNetwork); |
| | 0 | 118 | | SocketAddressPal.SetIPv4Address(_buffer, address); |
| | 0 | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | internal SocketAddress(IPAddress ipaddress, int port) |
| | 0 | 123 | | : this(ipaddress) |
| | 0 | 124 | | { |
| | 0 | 125 | | SocketAddressPal.SetPort(_buffer, unchecked((ushort)port)); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary>This represents underlying memory that can be passed to native OS calls.</summary> |
| | | 129 | | /// <remarks> |
| | | 130 | | /// Content of the memory can be invalidated if <see cref="Size"/> is changed or if the SocketAddress is used in |
| | | 131 | | /// </remarks> |
| | | 132 | | public Memory<byte> Buffer |
| | | 133 | | { |
| | | 134 | | get |
| | 0 | 135 | | { |
| | 0 | 136 | | return new Memory<byte>(_buffer); |
| | 0 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | |
| | | 141 | | public override bool Equals(object? comparand) => |
| | 0 | 142 | | comparand is SocketAddress other && Equals(other); |
| | | 143 | | |
| | 0 | 144 | | public bool Equals(SocketAddress? comparand) => comparand != null && Buffer.Span.SequenceEqual(comparand.Buffer. |
| | | 145 | | |
| | | 146 | | public override int GetHashCode() |
| | 0 | 147 | | { |
| | 0 | 148 | | HashCode hash = default; |
| | 0 | 149 | | hash.AddBytes(new ReadOnlySpan<byte>(_buffer, 0, _size)); |
| | 0 | 150 | | return hash.ToHashCode(); |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public override unsafe string ToString() |
| | 0 | 154 | | { |
| | | 155 | | // Get the address family string. In almost all cases, this should be a cached string |
| | | 156 | | // from the enum and won't actually allocate. |
| | 0 | 157 | | string familyString = Family.ToString(); |
| | | 158 | | |
| | | 159 | | // Determine the maximum length needed to format. |
| | 0 | 160 | | int maxLength = |
| | 0 | 161 | | checked( |
| | 0 | 162 | | familyString.Length + // AddressFamily |
| | 0 | 163 | | 1 + // : |
| | 0 | 164 | | 10 + // Size (max length for a positive Int32) |
| | 0 | 165 | | 2 + // :{ |
| | 0 | 166 | | (Size - DataOffset) * 4 + // at most ','+3digits per byte |
| | 0 | 167 | | 1 // } |
| | 0 | 168 | | ); |
| | | 169 | | |
| | 0 | 170 | | Span<char> result = (uint)maxLength <= 256 ? // arbitrary limit that should be large enough for the vast maj |
| | 0 | 171 | | stackalloc char[256] : |
| | 0 | 172 | | new char[maxLength]; |
| | | 173 | | |
| | 0 | 174 | | familyString.CopyTo(result); |
| | 0 | 175 | | int length = familyString.Length; |
| | | 176 | | |
| | 0 | 177 | | result[length++] = ':'; |
| | | 178 | | |
| | 0 | 179 | | bool formatted = Size.TryFormat(result.Slice(length), out int charsWritten); |
| | 0 | 180 | | Debug.Assert(formatted); |
| | 0 | 181 | | length += charsWritten; |
| | | 182 | | |
| | 0 | 183 | | result[length++] = ':'; |
| | 0 | 184 | | result[length++] = '{'; |
| | | 185 | | |
| | 0 | 186 | | byte[] buffer = _buffer; |
| | 0 | 187 | | for (int i = DataOffset; i < Size; i++) |
| | 0 | 188 | | { |
| | 0 | 189 | | if (i > DataOffset) |
| | 0 | 190 | | { |
| | 0 | 191 | | result[length++] = ','; |
| | 0 | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | formatted = buffer[i].TryFormat(result.Slice(length), out charsWritten); |
| | 0 | 195 | | Debug.Assert(formatted); |
| | 0 | 196 | | length += charsWritten; |
| | 0 | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | result[length++] = '}'; |
| | 0 | 200 | | return result.Slice(0, length).ToString(); |
| | 0 | 201 | | } |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | |