Commit 42cbf631 authored by Anton Ryzhov's avatar Anton Ryzhov

Additonal optimizations in GetProcessIdByLocalPort

parent 78297c2b
......@@ -37,9 +37,9 @@ namespace Titanium.Web.Proxy.Helpers
{
public TcpState state;
public uint localAddr;
public TcpPort localPort;
public uint localPort; // in network byte order (order of bytes - 1,0,3,2)
public uint remoteAddr;
public TcpPort remotePort;
public uint remotePort; // in network byte order (order of bytes - 1,0,3,2)
public int owningPid;
}
......@@ -51,23 +51,12 @@ namespace Titanium.Web.Proxy.Helpers
{
public fixed byte localAddr[16];
public uint localScopeId;
public TcpPort localPort;
public uint localPort; // in network byte order (order of bytes - 1,0,3,2)
public fixed byte remoteAddr[16];
public uint remoteScopeId;
public TcpPort remotePort;
public uint remotePort; // in network byte order (order of bytes - 1,0,3,2)
public TcpState state;
public int owningPid;
}
[StructLayout(LayoutKind.Sequential)]
internal struct TcpPort
{
public byte port1;
public byte port2;
public byte port3;
public byte port4;
public int Port => (port1 << 8) + port2 + (port3 << 24) + (port4 << 16);
}
}
}
......@@ -37,6 +37,7 @@ namespace Titanium.Web.Proxy.Helpers
0) == 0)
{
int rowCount = *(int*)tcpTable;
uint portInNetworkByteOrder = ToNetworkByteOrder((uint)localPort);
if (ipVersion == IpVersion.Ipv4)
{
......@@ -44,7 +45,7 @@ namespace Titanium.Web.Proxy.Helpers
for (int i = 0; i < rowCount; ++i)
{
if (rowPtr->localPort.Port == localPort)
if (rowPtr->localPort == portInNetworkByteOrder)
{
return rowPtr->owningPid;
}
......@@ -58,7 +59,7 @@ namespace Titanium.Web.Proxy.Helpers
for (int i = 0; i < rowCount; ++i)
{
if (rowPtr->localPort.Port == localPort)
if (rowPtr->localPort == portInNetworkByteOrder)
{
return rowPtr->owningPid;
}
......@@ -80,6 +81,18 @@ namespace Titanium.Web.Proxy.Helpers
return 0;
}
/// <summary>
/// Converts 32-bit integer from native byte order (little-endian)
/// to network byte order for port,
/// switches 0th and 1st bytes, and 2nd and 3rd bytes
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
private static uint ToNetworkByteOrder(uint port)
{
return ((port >> 8) & 0x00FF00FFu) | ((port << 8) & 0xFF00FF00u);
}
/// <summary>
/// relays the input clientStream to the server at the specified host name and port with the given httpCmd and headers
/// as prefix
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment