获取Tcp和UDP的连接信息
/**
* 作者:flyingleaf
* 功能:使用GetTcpTableFun、GetUdpTableFun获取tcp、udp信息
*
*/
typedef DWORD (WINAPI *GetTcpTableFun)(PMIB_TCPTABLE,PDWORD,BOOL);
typedef DWORD (WINAPI *GetUdpTableFun)(PMIB_UDPTABLE,PDWORD,BOOL);
GetTcpTableFun pGetTcp; //取TCP连接的函数
GetUdpTableFun pGetUdp; //取UDP连接的函数
PMIB_TCPTABLE mibtcp; //保存TCP的链表
PMIB_UDPTABLE mibUdp; //保存UDP的链表
DWORD dTcpNum; //tcp 数目
DWORD dUdpNum; //udp 数目
CString sTcpDetail; //TCP 具体内容 格式是用\n格开每条记录,用^格开每个字段
CString sUdpDetail; //UDP 具体内容 格式是用\n格开每条记录,用^格开每个字段
HINSTANCE hInst;
void GetTcpUdpNumber
{
BYTE pTcpBuf[500*20+4];
BYTE pUdpBuf[500*8+4];
DWORD dwTableSize;
DWORD lret;
int i = 0;
in_addr addrLoc,addrRem;
char szLocAddr[500],szRemAddr[500];
DWORD dwLocIP,dwRemIP;
unsigned short nLocalPort,nRemotePort;
CString strTmp,strStatus;
hInst = LoadLibrary("iphlpapi.dll"); //载入动态库
if(hInst==NULL)
{
OutputDebugString("载入动态库iphlpapi.dll错误");
return;
}
pGetTcp=NULL;
pGetUdp=NULL;
pGetTcp = (GetTcpTableFun)GetProcAddress(hInst, "GetTcpTable"); //获取TCP连接的函数
if(pGetTcp==NULL)
{
OutputDebugString("GET FUNCTION GetTcpTable Failed" );
return ;
}
pGetUdp=(GetUdpTableFun)GetProcAddress(hInst,"GetUdpTable"); //获取UDP连接的函数
if(pGetUdp==NULL)
{
OutputDebugString("GET FUNCTION GetUdpTable Failed");
return;
}
dwTableSize=500*20+4;
lret=pGetTcp((PMIB_TCPTABLE)pTcpBuf,&dwTableSize,false); //得到TCP的信息
if (lret != NO_ERROR) return;
mibtcp=(PMIB_TCPTABLE)pTcpBuf;
dTcpNum = mibtcp->dwNumEntries; //保存到成员函数
/*********************************************************************************
格式:编号^状态^本地IP^本地端口^外部IP^外部端口\r\n
**********************************************************************************/
for(i = 0;i < dTcpNum; i++) //取出具体的内容
{
dwRemIP=htonl(mibtcp->table[i].dwRemoteAddr);
addrRem.S_un.S_addr = ntohl(dwRemIP);
strcpy(szRemAddr, inet_ntoa(addrRem));
switch (mibtcp->table[i].dwState)
{
case MIB_TCP_STATE_CLOSED :
strStatus="CLOSED";
break;
case MIB_TCP_STATE_LISTEN :
strStatus="LISTEN";
break;
case MIB_TCP_STATE_SYN_SENT :
strStatus="SYN_SENT";
break;
case MIB_TCP_STATE_SYN_RCVD :
strStatus="SYN_RCVD";
break;
case MIB_TCP_STATE_ESTAB :
strStatus="ESTAB";
break;
case MIB_TCP_STATE_FIN_WAIT1 :
strStatus="FIN_WAIT1";
break;
case MIB_TCP_STATE_FIN_WAIT2 :
strStatus="FIN_WAIT2";
break;
case MIB_TCP_STATE_CLOSE_WAIT :
strStatus="CLOSE WAIT";
break;
case MIB_TCP_STATE_CLOSING :
strStatus="CLOSING";
break;
case MIB_TCP_STATE_LAST_ACK :
strStatus="LAST ACK";
break;
case MIB_TCP_STATE_TIME_WAIT :
strStatus="TIME WAIT";
break;
case MIB_TCP_STATE_DELETE_TCB :
strStatus="DELETE TCB";
break;
default:
strStatus="<ERR>";
}
strTmp.Format ("%.3d",i);
sTcpDetail += strTmp;
sTcpDetail += "^";
sTcpDetail += strStatus;
sTcpDetail += "^";
//Local IP
dwLocIP=htonl(mibtcp->table[i].dwLocalAddr);
addrLoc.S_un.S_addr = ntohl(dwLocIP);
strcpy(szLocAddr, inet_ntoa(addrLoc));
sTcpDetail += szLocAddr;
sTcpDetail += "^";
nLocalPort = htons(mibtcp->table[i].dwLocalPort);
strTmp.Format ("%u",nLocalPort);
sTcpDetail += strTmp;
sTcpDetail += "^";
dwRemIP=htonl(mibtcp->table[i].dwRemoteAddr);
addrRem.S_un.S_addr = ntohl(dwRemIP);
strcpy(szRemAddr, inet_ntoa(addrRem));
sTcpDetail += szRemAddr;
sTcpDetail += "^";
nRemotePort = htons(mibtcp->table[i].dwRemotePort );
strTmp.Format ("%u",nRemotePort);
sTcpDetail += strTmp;
sTcpDetail += "\r\n";
}
dwTableSize=500*8+4;
lret=pGetUdp((PMIB_UDPTABLE)pUdpBuf,&dwTableSize,false); //得到UDP的信息
if (lret != NO_ERROR) return;
mibUdp=(PMIB_UDPTABLE)pUdpBuf;
dUdpNum = mibUdp->dwNumEntries; //保存到成员函数
/*********************************************************************************
格式:编号^IP^端口\r\n
**********************************************************************************/
for(i=0;i<(int)mibUdp->dwNumEntries;i++) //暂时现在不使用
{
strTmp.Format ("%.3d",i);
sUdpDetail += strTmp;
sUdpDetail += "^";
dwLocIP=htonl(mibUdp->table[i].dwLocalAddr);
addrLoc.S_un.S_addr = ntohl(dwLocIP);
strcpy(szLocAddr, inet_ntoa(addrLoc));
nLocalPort = htons(mibUdp->table[i].dwLocalPort);
strTmp.Format ("%u",nLocalPort); //sprintf(szTemp, "%u", nLocalPort);
sUdpDetail += szLocAddr;
sUdpDetail += "^";
sUdpDetail += strTmp;
sUdpDetail += "\r\n";
}
}