ProcPort udp support

This commit is contained in:
Doug MacEachern 2005-03-20 03:20:28 +00:00
parent 2fb7a2fab3
commit 2582ada2b9
2 changed files with 61 additions and 28 deletions

View File

@ -84,6 +84,9 @@ typedef DWORD (CALLBACK *LPGETUDPTABLE)(PMIB_UDPTABLE, PDWORD, BOOL);
typedef DWORD (CALLBACK *LPGETTCPEXTABLE)(PMIB_TCPEXTABLE *, BOOL, HANDLE,
DWORD, DWORD);
typedef DWORD (CALLBACK *LPGETUDPEXTABLE)(PMIB_UDPEXTABLE *, BOOL, HANDLE,
DWORD, DWORD);
typedef DWORD (CALLBACK *LPSYSINFO)(DWORD, PVOID, ULONG, PULONG);
/* no longer in the standard header files */
@ -124,6 +127,7 @@ struct sigar_t {
LPGETTCPTABLE get_tcp_table;
LPGETTCPEXTABLE get_tcpx_table;
LPGETUDPTABLE get_udp_table;
LPGETUDPEXTABLE get_udpx_table;
LPSYSINFO get_ntsys_info;
sigar_win32_pinfo_t pinfo;
WORD ws_version;

View File

@ -209,6 +209,10 @@ int sigar_os_open(sigar_t **sigar)
"TcpExTableFromStack");
(*sigar)->get_udp_table =
(LPGETUDPTABLE)GetProcAddress(h, "GetUdpTable");
(*sigar)->get_udpx_table =
(LPGETUDPEXTABLE)GetProcAddress(h,
"AllocateAndGet"
"UdpExTableFromStack");
(*sigar)->ip_handle = h;
}
else {
@ -2156,38 +2160,63 @@ SIGAR_DECLARE(int) sigar_proc_port_get(sigar_t *sigar,
unsigned long port,
sigar_pid_t *pid)
{
int status;
DWORD rc, i;
PMIB_TCPEXTABLE tcp;
if (!sigar->get_tcpx_table) {
if (protocol == SIGAR_NETCONN_TCP) {
PMIB_TCPEXTABLE tcp;
if (!sigar->get_tcpx_table) {
return SIGAR_ENOTIMPL;
}
rc = sigar->get_tcpx_table(&tcp, FALSE, GetProcessHeap(),
2, 2);
if (rc) {
return GetLastError();
}
for (i=0; i<tcp->dwNumEntries; i++) {
if (tcp->table[i].dwState != MIB_TCP_STATE_LISTEN) {
continue;
}
if (htons((WORD)tcp->table[i].dwLocalPort) != port) {
continue;
}
*pid = tcp->table[i].dwProcessId;
return SIGAR_OK;
}
}
else if (protocol == SIGAR_NETCONN_UDP) {
PMIB_UDPEXTABLE udp;
if (!sigar->get_udpx_table) {
return SIGAR_ENOTIMPL;
}
rc = sigar->get_udpx_table(&udp, FALSE, GetProcessHeap(),
2, 2);
if (rc) {
return GetLastError();
}
for (i=0; i<udp->dwNumEntries; i++) {
if (htons((WORD)udp->table[i].dwLocalPort) != port) {
continue;
}
*pid = udp->table[i].dwProcessId;
return SIGAR_OK;
}
}
else {
return SIGAR_ENOTIMPL;
}
if (protocol != SIGAR_NETCONN_TCP) {
return SIGAR_ENOTIMPL; /* XXX UDP */
}
rc = sigar->get_tcpx_table(&tcp, FALSE, GetProcessHeap(),
2, 2);
if (rc) {
return GetLastError();
}
for (i=0; i<tcp->dwNumEntries; i++) {
if (tcp->table[i].dwState != MIB_TCP_STATE_LISTEN) {
continue;
}
if (htons((WORD)tcp->table[i].dwLocalPort) != port) {
continue;
}
*pid = tcp->table[i].dwProcessId;
return SIGAR_OK;
}
return ENOENT;
}