您的位置:首页 > 理论基础 > 计算机网络

WindowsAPI获取主机网络配置和网络连接信息

2017-07-13 09:54 316 查看
//获取主机网络配置
int GetNetConfigInfo ()
{
/* Declare and initialize variables */

// It is possible for an adapter to have multiple
// IPv4 addresses, gateways, and secondary WINS servers
// assigned to the adapter.
//
// Note that this sample code only prints out the
// first entry for the IP address/mask, and gateway, and
// the primary and secondary WINS server for each adapter.

PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
UINT i;

/* variables used to print DHCP time info */
struct tm newtime;
char buffer[32];
errno_t error;
char szLogBuffer[512]={0};

ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
}

if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {

printf("ComboIndex:     %d\n", pAdapter->ComboIndex);
printf("Adapter Name:     %s\n", pAdapter->AdapterName);
printf("Adapter Desc:     %s\n", pAdapter->Description);
printf("Adapter Addr: ");

for (i = 0; i < pAdapter->AddressLength; i++) {
if (i == (pAdapter->AddressLength - 1))
{
printf("%.2X\n", (int) pAdapter->Address[i]);
}
else
{
printf("%.2X-", (int) pAdapter->Address[i]);
}
}
printf("Index:     %d\n", pAdapter->Index);
printf("Type: ");

switch (pAdapter->Type) {
case MIB_IF_TYPE_OTHER:
printf("Other\n");
break;
case MIB_IF_TYPE_ETHERNET:

printf("Ethernet\n");
break;
case MIB_IF_TYPE_TOKENRING:

printf("Token Ring\n");
break;
case MIB_IF_TYPE_FDDI:

printf("FDDI\n");
break;
case MIB_IF_TYPE_PPP:

printf("PPP\n");
break;
case MIB_IF_TYPE_LOOPBACK:

printf("Lookback\n");
break;
case MIB_IF_TYPE_SLIP:

printf("Slip\n");
break;
default:

printf("Unknown type %ld\n", pAdapter->Type);
break;
}
printf("IP Address:     %s\n",pAdapter->IpAddressList.IpAddress.String);

printf("IP Mask:     %s\n", pAdapter->IpAddressList.IpMask.String);

printf("Gateway:     %s\n", pAdapter->GatewayList.IpAddress.String);

printf("***\n");

if (pAdapter->DhcpEnabled) {
printf("DHCP Enabled: Yes\n");
printf("  DHCP Server:     %s\n",pAdapter->DhcpServer.IpAddress.String);
printf("  Lease Obtained: ");
/* Display local time */

error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseObtained);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else {
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
/* asctime_s returns the string terminated by \n\0 */
{
printf("%s", buffer);
}

}

printf("  Lease Expires:  ");

error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseExpires);
if (error)

printf("Invalid Argument to _localtime32_s\n");
else {
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
/* asctime_s returns the string terminated by \n\0 */
{
printf("%s", buffer);
}
}
} else
{
printf("DHCP Enabled: No\n");
}

if (pAdapter->HaveWins) {
printf("Have Wins: Yes\n");
printf("  Primary Wins Server:    %s\n",pAdapter->PrimaryWinsServer.IpAddress.String);

printf("  Secondary Wins Server:  %s\n",pAdapter->SecondaryWinsServer.IpAddress.String);
} else
{
printf("Have Wins: No\n");
}

pAdapter = pAdapter->Next;
printf("\n");
}
} else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
if (pAdapterInfo)
FREE(pAdapterInfo);
return 0;
}

//获取主机连接
int GetNetStat()
{
// Declare and initialize variables
PMIB_TCPTABLE_OWNER_PID pTcpTable;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
char szLogBuffer[512]={0};
char szLocalAddr[128];
char szRemoteAddr[128];

struct in_addr IpAddr;

int i;

pTcpTable = (MIB_TCPTABLE_OWNER_PID *) MALLOC(sizeof (MIB_TCPTABLE_OWNER_PID));
if (pTcpTable == NULL) {
printf("Error allocating memory\n");
return 1;
}

dwSize = sizeof (MIB_TCPTABLE_OWNER_PID);
// Make an initial call to GetTcpTable to
// get the necessary size into the dwSize variable
if ((dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize, TRUE,AF_INET,TCP_TABLE_OWNER_PID_ALL,0)) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(pTcpTable);
pTcpTable = (MIB_TCPTABLE_OWNER_PID *) MALLOC(dwSize);
if (pTcpTable == NULL) {
printf("Error allocating memory\n");
return 1;
}
}
// Make a second call to GetTcpTable to get
// the actual data we require
if ((dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize, TRUE,AF_INET,TCP_TABLE_OWNER_PID_ALL,0)) == NO_ERROR) {

printf("\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);

for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {

printf("\n\tTCP[%d] State: %ld - ", i,pTcpTable->table[i].dwState);

switch (pTcpTable->table[i].dwState) {
case MIB_TCP_STATE_CLOSED:
printf("CLOSED\n");
break;
case MIB_TCP_STATE_LISTEN:
printf("LISTEN\n");
break;
case MIB_TCP_STATE_SYN_SENT:
printf("SYN-SENT\n");
break;
case MIB_TCP_STATE_SYN_RCVD:
printf("SYN-RECEIVED\n");
break;
case MIB_TCP_STATE_ESTAB:
printf("ESTABLISHED\n");
break;
case MIB_TCP_STATE_FIN_WAIT1:
printf("FIN-WAIT-1\n");
break;
case MIB_TCP_STATE_FIN_WAIT2:
printf("FIN-WAIT-2 \n");
break;
case MIB_TCP_STATE_CLOSE_WAIT:
printf("CLOSE-WAIT\n");
break;
case MIB_TCP_STATE_CLOSING:
printf("CLOSING\n");
break;
case MIB_TCP_STATE_LAST_ACK:
printf("LAST-ACK\n");
break;
case MIB_TCP_STATE_TIME_WAIT:
printf("TIME-WAIT\n");
break;
case MIB_TCP_STATE_DELETE_TCB:
printf("DELETE-TCB\n");
break;
default:
printf("UNKNOWN dwState value: %d\n", pTcpTable->table[i].dwState);
break;
}

IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));

printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);

printf("\tTCP[%d] Local Port: %d \n", i,ntohs((u_short)pTcpTable->table[i].dwLocalPort));

IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);

printf("\tTCP[%d] Remote Port: %d\n", i,ntohs((u_short)pTcpTable->table[i].dwRemotePort));

printf("\tTCP[%d] Pid: %d\n", i,ntohs((u_short)pTcpTable->table[i].dwOwningPid));

HANDLE Handle = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
pTcpTable->table[i].dwOwningPid /* This is the PID, you can find one from windows task manager */
);
if (Handle)
{
TCHAR Buffer[MAX_PATH];
if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
{
// At this point, buffer contains the full path to the executable
printf("\tTCP[%d] name: %s\n", i,Buffer);
}
else
{
// You better call GetLastError() here
}
CloseHandle(Handle);
}
}
} else {
printf("\tGetTcpTable failed with %d\n", dwRetVal);
FREE(pTcpTable);
return 1;
}

if (pTcpTable != NULL) {
FREE(pTcpTable);
pTcpTable = NULL;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐