@@ -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