Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions src/Common/src/System/Net/Internals/IPEndPointExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,65 @@ internal static class IPEndPointExtensions
{
public static Internals.SocketAddress Serialize(EndPoint endpoint)
{
Debug.Assert(endpoint is IPEndPoint);
Debug.Assert(!(endpoint is DnsEndPoint));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your comment below:

There are already existing tests for DnsEndpoint. The code checks if it's DNS end point before it calls IPEndPointExtensions.

Please update this assert instead of removing it. (as Debug.Assert(!(endpoint is DnsEndpoint)) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense


return new Internals.SocketAddress(((IPEndPoint)endpoint).Address, ((IPEndPoint)endpoint).Port);
var ipEndPoint = endpoint as IPEndPoint;
if (ipEndPoint != null)
{
return new Internals.SocketAddress(ipEndPoint.Address, ipEndPoint.Port);
}

System.Net.SocketAddress address = endpoint.Serialize();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this work for a DnsEndPoint? Please add a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are already existing tests for DnsEndpoint. The code checks if it's DNS end point before it calls IPEndPointExtensions.

return GetInternalSocketAddress(address);
}

public static EndPoint Create(this EndPoint thisObj, Internals.SocketAddress socketAddress)
{
if (socketAddress.Family != thisObj.AddressFamily)
AddressFamily family = socketAddress.Family;
if (family != thisObj.AddressFamily)
{
throw new ArgumentException(SR.Format(SR.net_InvalidAddressFamily, socketAddress.Family.ToString(), thisObj.GetType().FullName, thisObj.AddressFamily.ToString()), "socketAddress");
throw new ArgumentException(SR.Format(SR.net_InvalidAddressFamily, family.ToString(), thisObj.GetType().FullName, thisObj.AddressFamily.ToString()), "socketAddress");
}
if (socketAddress.Size < 8)

if (family == AddressFamily.InterNetwork || family == AddressFamily.InterNetworkV6)
{
throw new ArgumentException(SR.Format(SR.net_InvalidSocketAddressSize, socketAddress.GetType().FullName, thisObj.GetType().FullName), "socketAddress");
if (socketAddress.Size < 8)
{
throw new ArgumentException(SR.Format(SR.net_InvalidSocketAddressSize, socketAddress.GetType().FullName, thisObj.GetType().FullName), "socketAddress");
}

return socketAddress.GetIPEndPoint();
}

return socketAddress.GetIPEndPoint();
System.Net.SocketAddress address = GetNetSocketAddress(socketAddress);
return thisObj.Create(address);
}

internal static IPEndPoint Snapshot(this IPEndPoint thisObj)
{
return new IPEndPoint(thisObj.Address.Snapshot(), thisObj.Port);
}

private static Internals.SocketAddress GetInternalSocketAddress(System.Net.SocketAddress address)
{
var result = new Internals.SocketAddress(address.Family, address.Size);
for (int index = 0; index < address.Size; index++)
{
result[index] = address[index];
}

return result;
}

private static System.Net.SocketAddress GetNetSocketAddress(Internals.SocketAddress address)
{
var result = new System.Net.SocketAddress(address.Family, address.Size);
for (int index = 0; index < address.Size; index++)
{
result[index] = address[index];
}

return result;
}
}
}
15 changes: 9 additions & 6 deletions src/Common/tests/System/Net/Sockets/SocketTestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public abstract partial class SocketTestServer : IDisposable

protected abstract int Port { get; }

public static SocketTestServer SocketTestServerFactory(EndPoint endpoint)
public static SocketTestServer SocketTestServerFactory(EndPoint endpoint, ProtocolType protocolType = ProtocolType.Tcp)
{
return SocketTestServerFactory(DefaultNumConnections, DefaultReceiveBufferSize, endpoint);
return SocketTestServerFactory(DefaultNumConnections, DefaultReceiveBufferSize, endpoint, protocolType);
}

public static SocketTestServer SocketTestServerFactory(IPAddress address, out int port)
Expand All @@ -25,13 +25,15 @@ public static SocketTestServer SocketTestServerFactory(IPAddress address, out in
public static SocketTestServer SocketTestServerFactory(
int numConnections,
int receiveBufferSize,
EndPoint localEndPoint)
EndPoint localEndPoint,
ProtocolType protocolType = ProtocolType.Tcp)
{
return SocketTestServerFactory(
s_implementationType,
numConnections,
receiveBufferSize,
localEndPoint);
localEndPoint,
protocolType);
}

public static SocketTestServer SocketTestServerFactory(
Expand All @@ -52,14 +54,15 @@ public static SocketTestServer SocketTestServerFactory(
SocketImplementationType type,
int numConnections,
int receiveBufferSize,
EndPoint localEndPoint)
EndPoint localEndPoint,
ProtocolType protocolType = ProtocolType.Tcp)
{
switch (type)
{
case SocketImplementationType.APM:
return new SocketTestServerAPM(numConnections, receiveBufferSize, localEndPoint);
case SocketImplementationType.Async:
return new SocketTestServerAsync(numConnections, receiveBufferSize, localEndPoint);
return new SocketTestServerAsync(numConnections, receiveBufferSize, localEndPoint, protocolType);
default:
throw new ArgumentOutOfRangeException("type");
}
Expand Down
6 changes: 4 additions & 2 deletions src/Common/tests/System/Net/Sockets/SocketTestServerAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public class SocketTestServerAsync : SocketTestServer
private int _numConnectedSockets; // The total number of clients connected to the server.
private Semaphore _maxNumberAcceptedClientsSemaphore;
private int _acceptRetryCount = 10;
private ProtocolType _protocolType;

private object _listenSocketLock = new object();

protected sealed override int Port { get { return ((IPEndPoint)_listenSocket.LocalEndPoint).Port; } }

public SocketTestServerAsync(int numConnections, int receiveBufferSize, EndPoint localEndPoint)
public SocketTestServerAsync(int numConnections, int receiveBufferSize, EndPoint localEndPoint, ProtocolType protocolType = ProtocolType.Tcp)
{
_log = VerboseTestLogging.GetInstance();
_totalBytesRead = 0;
Expand All @@ -51,6 +52,7 @@ public SocketTestServerAsync(int numConnections, int receiveBufferSize, EndPoint

_readWritePool = new SocketAsyncEventArgsPool(numConnections);
_maxNumberAcceptedClientsSemaphore = new Semaphore(numConnections, numConnections);
_protocolType = protocolType;
Init();
Start(localEndPoint);
}
Expand Down Expand Up @@ -108,7 +110,7 @@ private void Init()
private void Start(EndPoint localEndPoint)
{
// Create the socket which listens for incoming connections.
_listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, _protocolType);
_listenSocket.Bind(localEndPoint);

// Start the server with a listen backlog of 100 connections.
Expand Down
4 changes: 4 additions & 0 deletions src/Native/System.Native/pal_networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,10 @@ static bool TryConvertProtocolTypePalToPlatform(int32_t palProtocolType, int* pl

switch (palProtocolType)
{
case PAL_PT_UNSPECIFIED:
*platformProtocolType = 0;
return true;

case PAL_PT_ICMP:
*platformProtocolType = IPPROTO_ICMP;
return true;
Expand Down
1 change: 1 addition & 0 deletions src/Native/System.Native/pal_networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ enum SocketType : int32_t
*/
enum ProtocolType : int32_t
{
PAL_PT_UNSPECIFIED = 0, // System.Net.ProtocolType.Unspecified
PAL_PT_ICMP = 1, // System.Net.ProtocolType.Icmp
PAL_PT_TCP = 6, // System.Net.ProtocolType.Tcp
PAL_PT_UDP = 17, // System.Net.ProtocolType.Udp
Expand Down
2 changes: 1 addition & 1 deletion src/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4231,7 +4231,7 @@ public bool ConnectAsync(SocketAsyncEventArgs e)
{
InternalBind(new IPEndPoint(IPAddress.Any, 0));
}
else
else if (endPointSnapshot.AddressFamily != AddressFamily.Unix)
{
InternalBind(new IPEndPoint(IPAddress.IPv6Any, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<Compile Include="SocketAsyncExtensions.cs" />
<Compile Include="SocketOptionNameTest.cs" />
<Compile Include="SocketTestServerAPMMock.cs" />

<Compile Include="UnixDomainSocketTest.cs" />

<!-- Common Sockets files -->
<Compile Include="$(CommonTestPath)\System\Net\Sockets\Configuration.cs">
<Link>SocketCommon\Configuration.cs</Link>
Expand Down Expand Up @@ -77,7 +78,7 @@
<Name>System.Net.Sockets</Name>
</ProjectReference>
</ItemGroup>

<ItemGroup>
<None Include="project.json" />
</ItemGroup>
Expand Down
Loading