dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.75k stars 190 forks source link

PInvoke.IpHlpApi.GetExtendedUdpTable give wrong port #446

Closed BigPino67 closed 3 months ago

BigPino67 commented 4 months ago

Describe the bug and how to reproduce

PInvoke.IpHlpApi.GetExtendedUdpTable give wrong port when compared to the windows command "netstat -ano"

What code is involved

private static List<TCPUDPConnection> GetIpV4UdpConnections(int pid)
{
    List<TCPUDPConnection> udpConnList = new List<TCPUDPConnection>();
    try
    {
        using (var results = GetExtendedUdpTable<MIB_UDPTABLE_OWNER_MODULE>(Vanara.PInvoke.IpHlpApi.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, Ws2_32.ADDRESS_FAMILY.AF_INET))
        {
            foreach (var r in results)
            {
                if (pid < 0 || r.dwOwningPid == pid)
                {
                    IPAddress ipa = IPAddress.Parse(r.dwLocalAddr.ToString());
                    TCPUDPConnection row = new TCPUDPConnection(Utils.GetLocalHostName());
                    row.Protocol = Protocol.UDP;
                    row.Local = new System.Net.IPEndPoint(ipa, (int)r.dwLocalPort);
                    row.PID = pid;
                    udpConnList.Add(row);
                }
            }
        }
    }
    catch (Exception)
    {
        //Nothing to do here
    }

    return udpConnList;
}

private static List<TCPUDPConnection> GetIpV6UdpConnections(int pid)
{
    List<TCPUDPConnection> udpConnList = new List<TCPUDPConnection>();
    try
    {
        using (var results = GetExtendedUdpTable<MIB_UDP6TABLE_OWNER_PID>(Vanara.PInvoke.IpHlpApi.UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, Ws2_32.ADDRESS_FAMILY.AF_INET6))
        {
            foreach (var r in results)
            {
                if (pid < 0 || r.dwOwningPid == pid)
                {
                    IPAddress ipa = IPAddress.Parse(r.ucLocalAddr.ToString());
                    TCPUDPConnection row = new TCPUDPConnection(Utils.GetLocalHostName());
                    row.Protocol = Protocol.UDP;
                    row.Local = new System.Net.IPEndPoint(ipa, (int)r.dwLocalPort);
                    row.PID = pid;
                    udpConnList.Add(row);
                }
            }
        }
    }
    catch (Exception)
    {
        //Nothing to do here
    }

    return udpConnList;
}

Expected behavior

I expect that "netstat -ano" give the same results as PInvoke.IpHlpApi.GetExtendedUdpTable. It's not the case here : Netstat port : 56359 PInvoke.IpHlpApi.GetExtendedUdpTable : 10204

Screenshots

"Netstat -ano" results : image

PInvoke.IpHlpApi.GetExtendedUdpTable results : image

dahall commented 3 months ago

Haha. I deleted my last comment. You and I both failed to read the docs for dwLocaPort:

The port number of the UDP endpoint on the local computer. This member is stored in network byte order.

In the next 4.0 release, I've added a new property called dwHostLocalPort that puts dwLocalPort in host order.

BigPino67 commented 2 months ago

@dahall Following the newest release (v.4.0.1), MIB_UDP6TABLE_OWNER_PID and MIB_UDPTABLE_OWNER_MODULE does not have any properties calleddwHostLocalPort. Is it normal?

dahall commented 2 months ago

Thanks for the callout to make all the structures consistent. This is done for 4.0.2