#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <Tlhelp32.h>
#include <winsock.h>
#include <iphlpapi.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
#ifdef NTDDI_VERSION
#if(NTDDI_VERSION>=0x0600)
typedef enum {
TcpConnectionOffloadStateInHost,
TcpConnectionOffloadStateOffloading,
TcpConnectionOffloadStateOffloaded,
TcpConnectionOffloadStateUploading,
TcpConnectionOffloadStateMax
} TCP_CONNECTION_OFFLOAD_STATE, *PTCP_CONNECTION_OFFLOAD_STATE;
typedef struct _MIB_TCPROW2 {
DWORD dwState;
DWORD dwLocalAddr;
DWORD dwLocalPort;
DWORD dwRemoteAddr;
DWORD dwRemotePort;
DWORD dwOwningPid;
TCP_CONNECTION_OFFLOAD_STATE dwOffloadState;
} MIB_TCPROW2, *PMIB_TCPROW2;
typedef struct _MIB_TCPTABLE2 {
DWORD dwNumEntries;
MIB_TCPROW2 table[ANY_SIZE];
} MIB_TCPTABLE2, *PMIB_TCPTABLE2;
typedef DWORD(WINAPI *_InternalGetTcpTable2)(
PMIB_TCPTABLE2 pTcpTable_Vista,
PULONG SizePointer,
BOOL Order
);
static _InternalGetTcpTable2 pGetTcpTable = NULL;
#endif
#endif
typedef struct tagMIB_TCPEXROW{
DWORD dwState; // 连接状态.
DWORD dwLocalAddr; // 本地地址.
DWORD dwLocalPort; // 本地端口.
DWORD dwRemoteAddr; // 远程地址.
DWORD dwRemotePort; // 远程端口.
int dwProcessId; //进程pid
} MIB_TCPEXROW, *PMIB_TCPEXROW;
typedef struct tagMIB_TCPEXTABLE{
DWORD dwNumEntries;
MIB_TCPEXROW table[100];
} MIB_TCPEXTABLE, *PMIB_TCPEXTABLE;
typedef DWORD(WINAPI *PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)(
PMIB_TCPEXTABLE *pTcpTable,
BOOL bOrder,
HANDLE heap,
DWORD zero,
DWORD flags
);
static PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK pAllocateAndGetTcpExTableFromStack = NULL;
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int filter(char *ip, DWORD port)
{
DWORD i;
#ifdef NTDDI_VERSION
#if(NTDDI_VERSION>=0x0600)
PMIB_TCPTABLE2 TCPTable2ForWin7;
#endif
#endif
ULONG ulSize = 0;
DWORD dwRetVal = 0;
char szLocalAddr[128];
char szRemoteAddr[128];
PMIB_TCPEXTABLE TCPExTable;
struct in_addr IpAddr;
HMODULE hIpDLL = LoadLibraryA("iphlpapi.dll");
if (!hIpDLL)
{
printf("LoadLibrary error!\n");
return 0;
}
int return_code = 0;
pAllocateAndGetTcpExTableFromStack =
(PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)
GetProcAddress(hIpDLL, "AllocateAndGetTcpExTableFromStack");
//vista
if (pAllocateAndGetTcpExTableFromStack == NULL)
{
#ifdef NTDDI_VERSION
#if(NTDDI_VERSION>=0x0600)
pGetTcpTable = (_InternalGetTcpTable2)GetProcAddress(hIpDLL, "GetTcpTable2");
if (pGetTcpTable != NULL)
{
TCPTable2ForWin7 = (MIB_TCPTABLE2 *)HeapAlloc(GetProcessHeap(), 0, sizeof (MIB_TCPTABLE2));
ulSize = sizeof (MIB_TCPTABLE);
if (NULL == TCPTable2ForWin7)
{
printf("allocating memory Error\n");
FreeLibrary(hIpDLL);
return 0;
}
if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(TCPTable2ForWin7);
TCPTable2ForWin7 = (MIB_TCPTABLE2 *)MALLOC(ulSize);
if (TCPTable2ForWin7 == NULL) {
printf("Error allocating memory\n");
FreeLibrary(hIpDLL);
return 0;
}
}
if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) == NO_ERROR) {
for (i = 0; i < (int)TCPTable2ForWin7->dwNumEntries; i++) {
IpAddr.S_un.S_addr = (u_long)TCPTable2ForWin7->table[i].dwRemoteAddr;
strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPTable2ForWin7->table[i].dwRemotePort) == port)
{
return_code = TCPTable2ForWin7->table[i].dwOwningPid;
}
}
}
}
FREE(TCPTable2ForWin7);
#endif
#endif
}
else
{
//FALSE or TRUE 表明数据是否排序
if (pAllocateAndGetTcpExTableFromStack(&TCPExTable, FALSE, GetProcessHeap(), 2, AF_INET))
{
printf("AllocateAndGetTcpExTableFromStack Error!\n");
FreeLibrary(hIpDLL);
return 0;
}
int i;
for (i = 0; i < TCPExTable->dwNumEntries; i++)
{
IpAddr.S_un.S_addr = (u_long)TCPExTable->table[i].dwRemoteAddr;
strcpy(szRemoteAddr, inet_ntoa(IpAddr));
if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPExTable->table[i].dwRemotePort) == port)
{
return_code = TCPExTable->table[i].dwProcessId;
}
}
FREE(TCPExTable);
}
FreeLibrary(hIpDLL);
return return_code;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *ip = "47.88.12.92";
DWORD port = 9528;
int a = filter(ip, port);
printf("%d\n", a);
system("pause");
return 0;
}
gcc test.c -o test -lws2_32 -liphlpapi
windows编译出现重定义
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <Tlhelp32.h>
#include <winsock.h>
#include <iphlpapi.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
typedef struct tagMIB_TCPEXROW{
DWORD dwState; // 连接状态.
DWORD dwLocalAddr; // 本地地址.
DWORD dwLocalPort; // 本地端口.
DWORD dwRemoteAddr; // 远程地址.
DWORD dwRemotePort; // 远程端口.
int dwProcessId; //进程pid
} MIB_TCPEXROW, *PMIB_TCPEXROW;
typedef struct tagMIB_TCPEXTABLE{
DWORD dwNumEntries;
MIB_TCPEXROW table[100];
} MIB_TCPEXTABLE, *PMIB_TCPEXTABLE;
typedef DWORD(WINAPI *PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)(
PMIB_TCPEXTABLE *pTcpTable,
BOOL bOrder,
HANDLE heap,
DWORD zero,
DWORD flags
);
#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
typedef DWORD(WINAPI *_InternalGetTcpTable2)(
PMIB_TCPTABLE2 pTcpTable_Vista,
PULONG SizePointer,
BOOL Order
);
#endif
#endif
//xp
static PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK pAllocateAndGetTcpExTableFromStack = NULL;
//vista
#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
static _InternalGetTcpTable2 pGetTcpTable = NULL;
#endif
#endif
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int filter(char *ip, DWORD port)
{
DWORD i;
#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
PMIB_TCPTABLE2 TCPTable2ForWin7;
#endif
#endif
ULONG ulSize = 0;
DWORD dwRetVal = 0;
char szLocalAddr[128];
char szRemoteAddr[128];
PMIB_TCPEXTABLE TCPExTable;
struct in_addr IpAddr;
HMODULE hIpDLL = LoadLibraryA("iphlpapi.dll");
if (!hIpDLL)
{
printf("LoadLibrary error!\n");
return 0;
}
int return_code = 0;
pAllocateAndGetTcpExTableFromStack =
(PALLOCATE_AND_GET_TCPEXTABLE_FROM_STACK)
GetProcAddress(hIpDLL, "AllocateAndGetTcpExTableFromStack");
//vista
if (pAllocateAndGetTcpExTableFromStack == NULL)
{
#ifdef _WIN32_WINNT
#if(_WIN32_WINNT>=0x0600)
pGetTcpTable = (_InternalGetTcpTable2)GetProcAddress(hIpDLL, "GetTcpTable2");
if (pGetTcpTable != NULL)
{
TCPTable2ForWin7 = (MIB_TCPTABLE2 *)HeapAlloc(GetProcessHeap(), 0, sizeof (MIB_TCPTABLE2));
ulSize = sizeof (MIB_TCPTABLE);
if (NULL == TCPTable2ForWin7)
{
printf("allocating memory Error\n");
FreeLibrary(hIpDLL);
return 0;
}
if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(TCPTable2ForWin7);
TCPTable2ForWin7 = (MIB_TCPTABLE2 *)MALLOC(ulSize);
if (TCPTable2ForWin7 == NULL) {
printf("Error allocating memory\n");
FreeLibrary(hIpDLL);
return 0;
}
}
if ((dwRetVal = GetTcpTable2(TCPTable2ForWin7, &ulSize, TRUE)) == NO_ERROR) {
for (i = 0; i < (int)TCPTable2ForWin7->dwNumEntries; i++) {
IpAddr.S_un.S_addr = (u_long)TCPTable2ForWin7->table[i].dwRemoteAddr;
strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPTable2ForWin7->table[i].dwRemotePort) == port)
{
return_code = TCPTable2ForWin7->table[i].dwOwningPid;
}
}
}
}
FREE(TCPTable2ForWin7);
#endif
#endif
}
else
{
//FALSE or TRUE 表明数据是否排序
if (pAllocateAndGetTcpExTableFromStack(&TCPExTable, FALSE, GetProcessHeap(), 2, AF_INET))
{
printf("AllocateAndGetTcpExTableFromStack Error!\n");
FreeLibrary(hIpDLL);
return 0;
}
for (int i = 0; i < TCPExTable->dwNumEntries; i++)
{
IpAddr.S_un.S_addr = (u_long)TCPExTable->table[i].dwRemoteAddr;
strcpy(szRemoteAddr, inet_ntoa(IpAddr));
if (strcmp(szRemoteAddr, ip) == 0 && ntohs((u_short)TCPExTable->table[i].dwRemotePort) == port)
{
return_code = TCPExTable->table[i].dwProcessId;
}
}
FREE(TCPExTable);
}
FreeLibrary(hIpDLL);
return return_code;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *ip = "47.88.12.92";
DWORD port =9528;
int a = filter(ip, port);
printf("%d\n", a);
system("pause");
return 0;
}
只有几个结构体的问题