| | | 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; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Sockets |
| | | 8 | | { |
| | | 9 | | internal static partial class SocketAddressExtensions |
| | | 10 | | { |
| | 0 | 11 | | public static IPAddress GetIPAddress(this SocketAddress socketAddress) => IPEndPointExtensions.GetIPAddress(sock |
| | | 12 | | public static int GetPort(this SocketAddress socketAddress) |
| | 0 | 13 | | { |
| | 0 | 14 | | Debug.Assert(socketAddress.Family == AddressFamily.InterNetwork || socketAddress.Family == AddressFamily.Int |
| | 0 | 15 | | return (int)SocketAddressPal.GetPort(socketAddress.Buffer.Span); |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public static IPEndPoint GetIPEndPoint(this SocketAddress socketAddress) |
| | 0 | 19 | | { |
| | 0 | 20 | | return new IPEndPoint(socketAddress.GetIPAddress(), socketAddress.GetPort()); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public static bool Equals(this SocketAddress socketAddress, EndPoint? endPoint) |
| | | 24 | | { |
| | | 25 | | if (socketAddress.Family == endPoint?.AddressFamily && endPoint is IPEndPoint ipe) |
| | | 26 | | { |
| | | 27 | | return ipe.Equals(socketAddress.Buffer.Span); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | // We could serialize other EndPoints and compare socket addresses. |
| | | 31 | | // But that would do two allocations and is probably as expensive as |
| | | 32 | | // allocating new EndPoint. |
| | | 33 | | // This may change if https://github.com/dotnet/runtime/issues/78993 is done |
| | | 34 | | return false; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |