使用GetTCPTable,SetTCPTable枚举/删除TCP端口连接情况(类似Netstat)

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:37   1475   0

/*
*IP Helper Function中提供了很多功能强大,使用简单的网络函数
*
*这里写了两个调用GetTCPTable,SetTCPTable的函数,来说明其使用:
*使用GetTCPTable来枚举TCP端口连接情况,模拟Netstat
*使用SetTCPTable来关闭一个TCP连接,目前为止SetTCPTable只支持删除一个TCP连接功能
*
*Code by yztgx at 2005-1-11
*参考:MSND(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/setipstatistics.asp)
/

#include "Iphlpapi.h"
#include <malloc.h>
#include "Winsock2.h"
#include <stdlib.h>

#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

static char TcpState[][32] =
{
"???",
"CLOSED",
"LISTENING",
"SYN_SENT",
"SEN_RECEIVED",
"ESTABLISHED",
"FIN_WAIT",
"FIN_WAIT2",
"CLOSE_WAIT",
"CLOSING",
"LAST_ACK",
"TIME_WAIT"
};


DWORD EnumTCPTable()
{
PMIB_TCPTABLE pTcpTable = NULL;
DWORD dwSize = 0;
DWORD dwRetVal = ERROR_SUCCESS;

struct in_addr rip;
struct in_addr lip;
char szrip[32] = {0};
char szlip[32] = {0};

//获得pTcpTable所需要的真实长度,dwSize
if (GetTcpTable(pTcpTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER)
{
pTcpTable = (MIB_TCPTABLE*) malloc ((UINT) dwSize);
}
else
return dwRetVal;

printf("Active Connections/n/n");
printf(" Proto/t%-24s%-24s%s/n","Local Address","Foreign Address","State");

if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR)
{
for (int i = 0; i < (int) pTcpTable->dwNumEntries; i++)
{
rip.S_un.S_addr = pTcpTable->table[i].dwRemoteAddr;
lip.S_un.S_addr = pTcpTable->table[i].dwLocalAddr;
//监听端口,远程主机端口为0,但函数返回是有值的,不知道它是怎么考虑的
if (pTcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN)
pTcpTable->table[i].dwRemotePort = 0;

//dwLocalPort,dwRemotePort 是网络字节
_snprintf(szlip,sizeof(szlip),"%s:%d",inet_ntoa(lip),htons((u_short)pTcpTable->table[i].dwLocalPort));
_snprintf(szrip,sizeof(szrip),"%s:%d",inet_ntoa(rip),htons((u_short)pTcpTable->table[i].dwRemotePort));
printf(" TCP/t%-24s%-24s%s/n",szlip,szrip,TcpState[pTcpTable->table[i].dwState]);
}
}
else
{
printf("/tCall to GetTcpTable failed./n");

LPVOID lpMsgBuf;

if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRetVal,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
printf("/tError: %s", lpMsgBuf);
}
LocalFree( lpMsgBuf );
}
GlobalFree(pTcpTable);
return dwRetVal;
}


DWORD DelTCPConnect(const char *lpszLocalAddr,
const char * lpszRemoteAddr,
DWORD dwLocalPort,
DWORD dwRemotePort)
{
DWORD dwRetVal = ERROR_NOT_FOUND;
MIB_TCPROW srtTcpRow;
srtTcpRow.dwLocalAddr = inet_addr(lpszLocalAddr);
srtTcpRow.dwRemoteAddr = inet_addr(lpszRemoteAddr);
srtTcpRow.dwLocalPort = htons(dwLocalPort);
srtTcpRow.dwRemotePort = htons(dwRemotePort);
srtTcpRow.dwState = MIB_TCP_STATE_DELETE_TCB; //目前为止,settcpEntry只支持该参数
dwRetVal = SetTcpEntry(&srtTcpRow);
if (dwRetVal != ERROR_SUCCESS)
{
LPVOID lpMsgBuf;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRetVal,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
printf("/tError: %s", lpMsgBuf);
}
LocalFree( lpMsgBuf );
}
return dwRetVal;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP