windows网络连接表 根据连接远程的ip端口判断本地程序
来源:互联网 发布:威露士消毒液成分知乎 编辑:程序博客网 时间:2024/05/11 23:18
#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#endiftypedef 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#endifULONG 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");//vistaif (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//xpstatic 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#endifULONG 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");//vistaif (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;}
只有几个结构体的问题
阅读全文
0 0
- windows网络连接表 根据连接远程的ip端口判断本地程序
- 修改windows远程终端的默认连接端口
- 本地网络连接不能自动获取IP的解决办法
- 通过远程连接socket得到客户端的IP和连接端口
- 网络判断网络连接有多种办法,通过C#程序也可以判断与远程主机的连接状态。具体实现方法
- 判断本地的电脑是否与Internet网络连接
- 判断网络的连接
- 网络连接的判断
- 请问,建立一个与远程的udp连接,在有没有函数可以取得本地的ip地址和端口号
- ping +IP如何判断网络是否连接
- Linux 本地启动mysql对应的3306端口也启动的情况下无法远程连接
- 更改远程连接端口
- 远程连接端口修改
- [小代码]通过IP和端口连接到远程摄像机
- windows判断本机网络连接的方法
- Windows远程连接的实现
- 本地远程连接实例
- 通过指定的端口进行远程连接
- Makefile经典教程(掌握这些足够)
- JavaFx入门教程(一)
- 王爽.汇编.第三版.实验七.答案
- css基础24
- 汉字在字库中的偏移地址计算
- windows网络连接表 根据连接远程的ip端口判断本地程序
- PHP代码的执行的过程
- Qualcomm MSM ION
- 设计模式之单例模式
- C 的宏和 C++的宏
- 案头的jQuery--前言
- [附中OJ]细胞问题
- 读facebook babi数据集的preprocess代码,感觉还是有必要记下,省不少事
- Git分支的使用