Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 498e964

Browse files
authored
Merge pull request #415 from antrv/feature/opt-getprocessid
Additonal optimizations in GetProcessIdByLocalPort
2 parents f2dbf77 + 48d50b6 commit 498e964

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Titanium.Web.Proxy/Helpers/NativeMethods.Tcp.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ internal struct TcpRow
3737
{
3838
public TcpState state;
3939
public uint localAddr;
40-
public TcpPort localPort;
40+
public uint localPort; // in network byte order (order of bytes - 1,0,3,2)
4141
public uint remoteAddr;
42-
public TcpPort remotePort;
42+
public uint remotePort; // in network byte order (order of bytes - 1,0,3,2)
4343
public int owningPid;
4444
}
4545

@@ -51,23 +51,12 @@ internal unsafe struct Tcp6Row
5151
{
5252
public fixed byte localAddr[16];
5353
public uint localScopeId;
54-
public TcpPort localPort;
54+
public uint localPort; // in network byte order (order of bytes - 1,0,3,2)
5555
public fixed byte remoteAddr[16];
5656
public uint remoteScopeId;
57-
public TcpPort remotePort;
57+
public uint remotePort; // in network byte order (order of bytes - 1,0,3,2)
5858
public TcpState state;
5959
public int owningPid;
6060
}
61-
62-
[StructLayout(LayoutKind.Sequential)]
63-
internal struct TcpPort
64-
{
65-
public byte port1;
66-
public byte port2;
67-
public byte port3;
68-
public byte port4;
69-
70-
public int Port => (port1 << 8) + port2 + (port3 << 24) + (port4 << 16);
71-
}
7261
}
7362
}

Titanium.Web.Proxy/Helpers/Tcp.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ internal static unsafe int GetProcessIdByLocalPort(IpVersion ipVersion, int loca
3737
0) == 0)
3838
{
3939
int rowCount = *(int*)tcpTable;
40+
uint portInNetworkByteOrder = ToNetworkByteOrder((uint)localPort);
4041

4142
if (ipVersion == IpVersion.Ipv4)
4243
{
4344
NativeMethods.TcpRow* rowPtr = (NativeMethods.TcpRow*)(tcpTable + 4);
4445

4546
for (int i = 0; i < rowCount; ++i)
4647
{
47-
if (rowPtr->localPort.Port == localPort)
48+
if (rowPtr->localPort == portInNetworkByteOrder)
4849
{
4950
return rowPtr->owningPid;
5051
}
@@ -58,7 +59,7 @@ internal static unsafe int GetProcessIdByLocalPort(IpVersion ipVersion, int loca
5859

5960
for (int i = 0; i < rowCount; ++i)
6061
{
61-
if (rowPtr->localPort.Port == localPort)
62+
if (rowPtr->localPort == portInNetworkByteOrder)
6263
{
6364
return rowPtr->owningPid;
6465
}
@@ -80,6 +81,18 @@ internal static unsafe int GetProcessIdByLocalPort(IpVersion ipVersion, int loca
8081
return 0;
8182
}
8283

84+
/// <summary>
85+
/// Converts 32-bit integer from native byte order (little-endian)
86+
/// to network byte order for port,
87+
/// switches 0th and 1st bytes, and 2nd and 3rd bytes
88+
/// </summary>
89+
/// <param name="port"></param>
90+
/// <returns></returns>
91+
private static uint ToNetworkByteOrder(uint port)
92+
{
93+
return ((port >> 8) & 0x00FF00FFu) | ((port << 8) & 0xFF00FF00u);
94+
}
95+
8396
/// <summary>
8497
/// relays the input clientStream to the server at the specified host name and port with the given httpCmd and headers
8598
/// as prefix

0 commit comments

Comments
 (0)