-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SendTo/ReceiveFrom with SocketAddress (#88970)
* check * update * update * set length * feedback * update to match approved API * quic * update * fixes * GetHashCode * cleanup * PalTests * GetMaximumAddressSize * wasi * feedback * feedback * loose ends * feedback
- Loading branch information
Showing
39 changed files
with
748 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/libraries/Common/src/System/Net/IPEndPointExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Net; | ||
|
||
namespace System.Net.Sockets | ||
{ | ||
internal static class IPEndPointExtensions | ||
{ | ||
public static IPAddress GetIPAddress(ReadOnlySpan<byte> socketAddressBuffer) | ||
{ | ||
AddressFamily family = SocketAddressPal.GetAddressFamily(socketAddressBuffer); | ||
|
||
if (family == AddressFamily.InterNetworkV6) | ||
{ | ||
Span<byte> address = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes]; | ||
uint scope; | ||
SocketAddressPal.GetIPv6Address(socketAddressBuffer, address, out scope); | ||
return new IPAddress(address, (long)scope); | ||
} | ||
else if (family == AddressFamily.InterNetwork) | ||
{ | ||
return new IPAddress((long)SocketAddressPal.GetIPv4Address(socketAddressBuffer) & 0x0FFFFFFFF); | ||
} | ||
|
||
throw new SocketException((int)SocketError.AddressFamilyNotSupported); | ||
} | ||
|
||
public static void SetIPAddress(Span<byte> socketAddressBuffer, IPAddress address) | ||
{ | ||
SocketAddressPal.SetAddressFamily(socketAddressBuffer, address.AddressFamily); | ||
SocketAddressPal.SetPort(socketAddressBuffer, 0); | ||
if (address.AddressFamily == AddressFamily.InterNetwork) | ||
{ | ||
#pragma warning disable CS0618 | ||
SocketAddressPal.SetIPv4Address(socketAddressBuffer, (uint)address.Address); | ||
#pragma warning restore CS0618 | ||
} | ||
else | ||
{ | ||
Span<byte> addressBuffer = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes]; | ||
address.TryWriteBytes(addressBuffer, out int written); | ||
Debug.Assert(written == IPAddressParserStatics.IPv6AddressBytes); | ||
SocketAddressPal.SetIPv6Address(socketAddressBuffer, addressBuffer, (uint)address.ScopeId); | ||
} | ||
} | ||
|
||
public static IPEndPoint CreateIPEndPoint(ReadOnlySpan<byte> socketAddressBuffer) | ||
{ | ||
return new IPEndPoint(GetIPAddress(socketAddressBuffer), SocketAddressPal.GetPort(socketAddressBuffer)); | ||
} | ||
|
||
// suggestion from https://github.com/dotnet/runtime/issues/78993 | ||
public static void Serialize(this IPEndPoint endPoint, Span<byte> destination) | ||
{ | ||
SocketAddressPal.SetAddressFamily(destination, endPoint.AddressFamily); | ||
SetIPAddress(destination, endPoint.Address); | ||
SocketAddressPal.SetPort(destination, (ushort)endPoint.Port); | ||
} | ||
} | ||
} |
Oops, something went wrong.