Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spanize interop in System.Net.NetworkInformation #35098

Merged
merged 1 commit into from
Apr 18, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -424,19 +424,17 @@ internal struct MibTcp6TableOwnerPid
}

[StructLayout(LayoutKind.Sequential)]
internal struct MibTcp6RowOwnerPid
internal unsafe struct MibTcp6RowOwnerPid
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal byte[] localAddr;
internal fixed byte localAddr[16];
internal uint localScopeId;
internal byte localPort1;
internal byte localPort2;
// Ports are only 16 bit values (in network WORD order, 3,4,1,2).
// There are reports where the high order bytes have garbage in them.
internal byte ignoreLocalPort3;
internal byte ignoreLocalPort4;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal byte[] remoteAddr;
internal fixed byte remoteAddr[16];
internal uint remoteScopeId;
internal byte remotePort1;
internal byte remotePort2;
Expand All @@ -446,6 +444,9 @@ internal struct MibTcp6RowOwnerPid
internal byte ignoreRemotePort4;
internal TcpState state;
internal uint owningPid;

internal ReadOnlySpan<byte> localAddrAsSpan => MemoryMarshal.CreateSpan(ref localAddr[0], 16);
internal ReadOnlySpan<byte> remoteAddrAsSpan => MemoryMarshal.CreateSpan(ref remoteAddr[0], 16);
}

internal enum TcpTableClass
Expand Down Expand Up @@ -493,10 +494,9 @@ internal struct MibUdp6TableOwnerPid
}

[StructLayout(LayoutKind.Sequential)]
internal struct MibUdp6RowOwnerPid
internal unsafe struct MibUdp6RowOwnerPid
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal byte[] localAddr;
internal fixed byte localAddr[16];
internal uint localScopeId;
internal byte localPort1;
internal byte localPort2;
Expand All @@ -505,6 +505,8 @@ internal struct MibUdp6RowOwnerPid
internal byte ignoreLocalPort3;
internal byte ignoreLocalPort4;
internal uint owningPid;

internal ReadOnlySpan<byte> localAddrAsSpan => MemoryMarshal.CreateSpan(ref localAddr[0], 16);
}

internal delegate void StableUnicastIpAddressTableDelegate(IntPtr context, IntPtr table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public override IPEndPoint[] GetActiveTcpListeners()

///
/// Gets the active TCP connections. Uses the native GetTcpTable API.
private List<SystemTcpConnectionInformation> GetAllTcpConnections()
private unsafe List<SystemTcpConnectionInformation> GetAllTcpConnections()
{
uint size = 0;
uint result = 0;
Expand All @@ -128,21 +128,20 @@ private List<SystemTcpConnectionInformation> GetAllTcpConnections()

if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{
var span = new ReadOnlySpan<byte>((byte*)buffer, (int)size);

// The table info just gives us the number of rows.
Interop.IpHlpApi.MibTcpTable tcpTableInfo = Marshal.PtrToStructure<Interop.IpHlpApi.MibTcpTable>(buffer);
ref readonly Interop.IpHlpApi.MibTcpTable tcpTableInfo = ref MemoryMarshal.AsRef<Interop.IpHlpApi.MibTcpTable>(span);

if (tcpTableInfo.numberOfEntries > 0)
{
// Skip over the tableinfo to get the inline rows.
IntPtr newPtr = (IntPtr)((long)buffer + Marshal.SizeOf(tcpTableInfo.numberOfEntries));
span = span.Slice(sizeof(Interop.IpHlpApi.MibTcpTable));

for (int i = 0; i < tcpTableInfo.numberOfEntries; i++)
{
Interop.IpHlpApi.MibTcpRow tcpRow = Marshal.PtrToStructure<Interop.IpHlpApi.MibTcpRow>(newPtr);
tcpConnections.Add(new SystemTcpConnectionInformation(tcpRow));

// Increment the pointer to the next row.
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcpRow));
tcpConnections.Add(new SystemTcpConnectionInformation(in MemoryMarshal.AsRef<Interop.IpHlpApi.MibTcpRow>(span)));
span = span.Slice(sizeof(Interop.IpHlpApi.MibTcpRow));
}
}
}
Expand Down Expand Up @@ -179,21 +178,22 @@ private List<SystemTcpConnectionInformation> GetAllTcpConnections()
Interop.IpHlpApi.TcpTableClass.TcpTableOwnerPidAll, 0);
if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{
var span = new ReadOnlySpan<byte>((byte*)buffer, (int)size);

// The table info just gives us the number of rows.
Interop.IpHlpApi.MibTcp6TableOwnerPid tcpTable6OwnerPid = Marshal.PtrToStructure<Interop.IpHlpApi.MibTcp6TableOwnerPid>(buffer);
ref readonly Interop.IpHlpApi.MibTcp6TableOwnerPid tcpTable6OwnerPid = ref MemoryMarshal.AsRef<Interop.IpHlpApi.MibTcp6TableOwnerPid>(span);

if (tcpTable6OwnerPid.numberOfEntries > 0)
{
// Skip over the tableinfo to get the inline rows.
IntPtr newPtr = (IntPtr)((long)buffer + Marshal.SizeOf(tcpTable6OwnerPid.numberOfEntries));
span = span.Slice(sizeof(Interop.IpHlpApi.MibTcp6TableOwnerPid));

for (int i = 0; i < tcpTable6OwnerPid.numberOfEntries; i++)
{
Interop.IpHlpApi.MibTcp6RowOwnerPid tcp6RowOwnerPid = Marshal.PtrToStructure<Interop.IpHlpApi.MibTcp6RowOwnerPid>(newPtr);
tcpConnections.Add(new SystemTcpConnectionInformation(tcp6RowOwnerPid));
tcpConnections.Add(new SystemTcpConnectionInformation(in MemoryMarshal.AsRef<Interop.IpHlpApi.MibTcp6RowOwnerPid>(span)));

// We increment the pointer to the next row.
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcp6RowOwnerPid));
span = span.Slice(sizeof(Interop.IpHlpApi.MibTcp6RowOwnerPid));
}
}
}
Expand All @@ -215,7 +215,7 @@ private List<SystemTcpConnectionInformation> GetAllTcpConnections()
}

/// Gets the active UDP listeners. Uses the native GetUdpTable API.
public override IPEndPoint[] GetActiveUdpListeners()
public unsafe override IPEndPoint[] GetActiveUdpListeners()
{
uint size = 0;
uint result = 0;
Expand All @@ -237,22 +237,25 @@ public override IPEndPoint[] GetActiveUdpListeners()

if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{
var span = new ReadOnlySpan<byte>((byte*)buffer, (int)size);

// The table info just gives us the number of rows.
Interop.IpHlpApi.MibUdpTable udpTableInfo = Marshal.PtrToStructure<Interop.IpHlpApi.MibUdpTable>(buffer);
ref readonly Interop.IpHlpApi.MibUdpTable udpTableInfo = ref MemoryMarshal.AsRef<Interop.IpHlpApi.MibUdpTable>(span);

if (udpTableInfo.numberOfEntries > 0)
{
// Skip over the tableinfo to get the inline rows.
IntPtr newPtr = (IntPtr)((long)buffer + Marshal.SizeOf(udpTableInfo.numberOfEntries));
span = span.Slice(sizeof(Interop.IpHlpApi.MibUdpTable));

for (int i = 0; i < udpTableInfo.numberOfEntries; i++)
{
Interop.IpHlpApi.MibUdpRow udpRow = Marshal.PtrToStructure<Interop.IpHlpApi.MibUdpRow>(newPtr);
ref readonly Interop.IpHlpApi.MibUdpRow udpRow = ref MemoryMarshal.AsRef<Interop.IpHlpApi.MibUdpRow>(span);
int localPort = udpRow.localPort1 << 8 | udpRow.localPort2;

udpListeners.Add(new IPEndPoint(udpRow.localAddr, (int)localPort));

// We increment the pointer to the next row.
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udpRow));
span = span.Slice(sizeof(Interop.IpHlpApi.MibUdpRow));
}
}
}
Expand Down Expand Up @@ -289,23 +292,26 @@ public override IPEndPoint[] GetActiveUdpListeners()

if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{
var span = new ReadOnlySpan<byte>((byte*)buffer, (int)size);

// The table info just gives us the number of rows.
Interop.IpHlpApi.MibUdp6TableOwnerPid udp6TableOwnerPid = Marshal.PtrToStructure<Interop.IpHlpApi.MibUdp6TableOwnerPid>(buffer);
ref readonly Interop.IpHlpApi.MibUdp6TableOwnerPid udp6TableOwnerPid = ref MemoryMarshal.AsRef<Interop.IpHlpApi.MibUdp6TableOwnerPid>(span);

if (udp6TableOwnerPid.numberOfEntries > 0)
{
// Skip over the tableinfo to get the inline rows.
IntPtr newPtr = (IntPtr)((long)buffer + Marshal.SizeOf(udp6TableOwnerPid.numberOfEntries));
span = span.Slice(sizeof(Interop.IpHlpApi.MibUdp6TableOwnerPid));

for (int i = 0; i < udp6TableOwnerPid.numberOfEntries; i++)
{
Interop.IpHlpApi.MibUdp6RowOwnerPid udp6RowOwnerPid = Marshal.PtrToStructure<Interop.IpHlpApi.MibUdp6RowOwnerPid>(newPtr);
ref readonly Interop.IpHlpApi.MibUdp6RowOwnerPid udp6RowOwnerPid = ref MemoryMarshal.AsRef<Interop.IpHlpApi.MibUdp6RowOwnerPid>(span);
int localPort = udp6RowOwnerPid.localPort1 << 8 | udp6RowOwnerPid.localPort2;

udpListeners.Add(new IPEndPoint(new IPAddress(udp6RowOwnerPid.localAddr,
udpListeners.Add(new IPEndPoint(new IPAddress(udp6RowOwnerPid.localAddrAsSpan,
udp6RowOwnerPid.localScopeId), localPort));

// We increment the pointer to the next row.
newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(udp6RowOwnerPid));
span = span.Slice(sizeof(Interop.IpHlpApi.MibUdp6RowOwnerPid));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class SystemTcpConnectionInformation : TcpConnectionInformation
private readonly IPEndPoint _remoteEndPoint;
private readonly TcpState _state;

internal SystemTcpConnectionInformation(Interop.IpHlpApi.MibTcpRow row)
internal SystemTcpConnectionInformation(in Interop.IpHlpApi.MibTcpRow row)
jkotas marked this conversation as resolved.
Show resolved Hide resolved
{
_state = row.state;

Expand All @@ -25,7 +25,7 @@ internal SystemTcpConnectionInformation(Interop.IpHlpApi.MibTcpRow row)
}

// IPV6 version of the Tcp row.
internal SystemTcpConnectionInformation(Interop.IpHlpApi.MibTcp6RowOwnerPid row)
internal unsafe SystemTcpConnectionInformation(in Interop.IpHlpApi.MibTcp6RowOwnerPid row)
{
_state = row.state;

Expand All @@ -34,8 +34,8 @@ internal SystemTcpConnectionInformation(Interop.IpHlpApi.MibTcp6RowOwnerPid row)
int localPort = row.localPort1 << 8 | row.localPort2;
int remotePort = ((_state == TcpState.Listen) ? 0 : row.remotePort1 << 8 | row.remotePort2);

_localEndPoint = new IPEndPoint(new IPAddress(row.localAddr, row.localScopeId), (int)localPort);
_remoteEndPoint = new IPEndPoint(new IPAddress(row.remoteAddr, row.remoteScopeId), (int)remotePort);
_localEndPoint = new IPEndPoint(new IPAddress(row.localAddrAsSpan, row.localScopeId), (int)localPort);
_remoteEndPoint = new IPEndPoint(new IPAddress(row.remoteAddrAsSpan, row.remoteScopeId), (int)remotePort);
}

public override TcpState State { get { return _state; } }
Expand Down