枚举所有网络连接

// TcpPid.cpp : Defines the entry point for the console application.
//
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include<TlHelp32.h>
#include <iostream>
#include <iomanip>
using namespace std;

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

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

bool GetProcessNameByIdW(DWORD ProcessId, WCHAR* Buf, DWORD Length)
{
	PROCESSENTRY32W PE;
	memset(&PE, 0, sizeof(PROCESSENTRY32W));
	PE.dwSize = sizeof(PROCESSENTRY32W);

	if (ProcessId == 0)
	{
		wcscpy_s(Buf, Length, L"Idle");
		return true;
	}
	if (ProcessId == 4)
	{
		wcscpy_s(Buf, Length, L"System");
		return true;
	}

	auto hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapshot == INVALID_HANDLE_VALUE)
	{
		return false;
	}

	if (!Process32FirstW(hSnapshot, &PE))
	{
		CloseHandle(hSnapshot);
		return false;
	}
	bool bRet = false;
	do
	{
		if (PE.th32ProcessID == ProcessId)
		{
			wcscpy_s(Buf, Length, PE.szExeFile);
			bRet = true;
			break;
		}
	} while (Process32NextW(hSnapshot, &PE));
	CloseHandle(hSnapshot);
	return bRet;
}

//判断是否是内网IP  是内网IP 返回1  不是  外网IP 0  参数错误 返回-1
int IsIntranet(const char* IP)
{
	int bRet = 1;
	if (!IP || strlen(IP) == 0)
	{
		return -1;
	}

	do
	{
		ULONG dwIP = inet_addr(IP);
		DWORD a = dwIP;
		a = a << 24;
		a = a | ((dwIP & 0xFF00) << 8);
		a = a | ((dwIP & 0xFF0000) >> 8);
		a = a | ((dwIP & 0xFF000000) >> 24);
		dwIP = a;
		if (dwIP == 0)
			break;
		if (dwIP >= 0x7F000000 && dwIP <= 0x7FFFFFFF)//127.0.0.1 - 127.255.255.255
			break;
		if (dwIP >= 0x0A000000 && dwIP <= 0x0AFFFFFF)//10.0.0.0 - 10.255.255.255
			break;
		if (dwIP >= 0xAC100000 && dwIP <= 0xAC1FFFFF)//172.16.0.0 - 172.31.255.255
			break;
		if (dwIP >= 0xC0A80000 && dwIP <= 0xC0A8FFFF)//192.168.0.0 - 192.168.255.255
			break;
		bRet = 0;
	} while (false);
	return bRet;
}

//删除内存使用delete []
bool WideToAnsi(const wchar_t* in_str, std::string& out_str)
{
	int NeedBytes = WideCharToMultiByte(CP_ACP, 0, in_str, (int)wcslen(in_str), NULL, 0, NULL, NULL);
	if (0 == NeedBytes)
	{
		return false;
	}
	//多分配一个字节用于0结尾
	char* Buffer = new char[(ULONG_PTR)NeedBytes + 1];
	if (!Buffer)
	{
		return false;
	}
	ZeroMemory(Buffer, (ULONG_PTR)NeedBytes + 1);

	int TurnedByets = WideCharToMultiByte(CP_ACP, 0, in_str, (int)wcslen(in_str), Buffer, NeedBytes, NULL, NULL);
	if (0 == TurnedByets || TurnedByets != NeedBytes)
	{
		delete[]Buffer;;
		Buffer = NULL;
		return false;
	}
	out_str = Buffer;
	return true;
}

int main(int argc, char* argv[])
{
	PMIB_TCPTABLE_OWNER_PID ptTable;
	DWORD dwSize = { 0 };
	DWORD dwRetValue = { 0 };
	struct in_addr inAddr;

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

	ptTable = (MIB_TCPTABLE_OWNER_PID*)MALLOC(sizeof(MIB_TCPTABLE_OWNER_PID));
	dwSize = sizeof(MIB_TCPTABLE);

	if ((dwRetValue = GetExtendedTcpTable(ptTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == ERROR_INSUFFICIENT_BUFFER)
	{
		FREE(ptTable);
		ptTable = NULL;
		ptTable = (MIB_TCPTABLE_OWNER_PID*)MALLOC(dwSize);
		if (ptTable == NULL)
			return 0;
	}

	char szRemoteAddr[128];

	if ((dwRetValue = GetExtendedTcpTable(ptTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0)) == NO_ERROR)
	{
		for (int i = 0; i < (int)ptTable->dwNumEntries; i++)
		{
			inAddr.S_un.S_addr = (u_long)ptTable->table[i].dwRemoteAddr;
			if (IsIntranet(inet_ntoa(inAddr))!=0)
			{
				continue;
			}
			
			cout << "进程id:" << ptTable->table[i].dwOwningPid;
			sprintf_s(szRemoteAddr, sizeof(szRemoteAddr), "%s:%d", inet_ntoa(inAddr)
				, htons(ptTable->table[i].dwRemotePort));
			
			printf("\t%20s", szRemoteAddr);

			WCHAR ProcessName[260] = { 0 };
			if (GetProcessNameByIdW(ptTable->table[i].dwOwningPid, ProcessName,260) == true)
			{
				std::string sName;
				if (WideToAnsi(ProcessName, sName) == true)
				{
					printf("\t%s\n", sName.c_str());
				}
				
			}
		}
	}

	if (dwRetValue == ERROR_INSUFFICIENT_BUFFER)
	{
		cout << "Insufficent Buffer." << endl;
	}
	if (dwRetValue == ERROR_INVALID_PARAMETER)
	{
		cout << "Invalid Paramters Given." << endl;
	}
	cin.get();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值