ipconfig.c
来自「一个类似windows」· C语言 代码 · 共 518 行 · 第 1/2 页
C
518 行
/*
* ReactOS Win32 Applications
* Copyright (C) 2005 ReactOS Team
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS arp utility
* FILE: apps/utils/net/ipconfig/ipconfig.c
* PURPOSE:
* PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
* REVISIONS:
* GM 14/09/05 Created
*
*/
/*
* TODO:
* fix renew / release
* implement flushdns, registerdns, displaydns, showclassid, setclassid
* allow globbing on adapter names
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <tchar.h>
#include <string.h>
#include <ctype.h>
#include <winsock2.h>
#include <iphlpapi.h>
#define UNICODE
#define _UNICODE
LPCTSTR GetNodeTypeName(UINT NodeType)
{
switch (NodeType) {
case 1: return _T("Broadcast");
case 2: return _T("Peer To Peer");
case 4: return _T("Mixed");
case 8: return _T("Hybrid");
default : return _T("unknown");
}
}
LPCTSTR GetInterfaceTypeName(UINT InterfaceType)
{
switch (InterfaceType) {
case MIB_IF_TYPE_OTHER: return _T("Other Type Of Adapter");
case MIB_IF_TYPE_ETHERNET: return _T("Ethernet Adapter");
case MIB_IF_TYPE_TOKENRING: return _T("Token Ring Adapter");
case MIB_IF_TYPE_FDDI: return _T("FDDI Adapter");
case MIB_IF_TYPE_PPP: return _T("PPP Adapter");
case MIB_IF_TYPE_LOOPBACK: return _T("Loopback Adapter");
case MIB_IF_TYPE_SLIP: return _T("SLIP Adapter");
default: return _T("unknown");
}
}
/* print MAC address */
PTCHAR PrintMacAddr(PBYTE Mac)
{
static TCHAR MacAddr[20];
_stprintf(MacAddr, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
Mac[0], Mac[1], Mac[2], Mac[3], Mac[4], Mac[5]);
return MacAddr;
}
DWORD DoFormatMessage(DWORD ErrorCode)
{
LPVOID lpMsgBuf;
DWORD RetVal;
if ((RetVal = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
ErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
(LPTSTR) &lpMsgBuf,
0,
NULL ))) {
_tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
/* return number of TCHAR's stored in output buffer
* excluding '\0' - as FormatMessage does*/
return RetVal;
}
else
return 0;
}
INT ShowInfo(BOOL bAll)
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
PIP_ADAPTER_INFO pAdapter = NULL;
ULONG adaptOutBufLen;
PFIXED_INFO pFixedInfo;
ULONG netOutBufLen;
PIP_ADDR_STRING pIPAddr = NULL;
DWORD ErrRet = 0;
/* assign memory for call to GetNetworkParams */
pFixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) );
netOutBufLen = sizeof(FIXED_INFO);
/* assign memory for call to GetAdapterInfo */
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
adaptOutBufLen = sizeof(IP_ADAPTER_INFO);
/* set required buffer size */
if(GetNetworkParams(pFixedInfo, &netOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
GlobalFree(pFixedInfo);
pFixedInfo = (FIXED_INFO *) GlobalAlloc(GPTR, netOutBufLen);
}
/* set required buffer size */
if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen);
}
if ((ErrRet = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen)) != NO_ERROR)
{
_tprintf(_T("GetAdaptersInfo failed : "));
DoFormatMessage(ErrRet);
return EXIT_FAILURE;
}
if ((ErrRet = GetNetworkParams(pFixedInfo, &netOutBufLen)) != NO_ERROR)
{
_tprintf(_T("GetNetworkParams failed : "));
DoFormatMessage(ErrRet);
return EXIT_FAILURE;
}
pAdapter = pAdapterInfo;
_tprintf(_T("\nReactOS IP Configuration\n\n"));
if (bAll)
{
_tprintf(_T("\tHost Name . . . . . . . . . . . . : %s\n"), pFixedInfo->HostName);
_tprintf(_T("\tPrimary DNS Suffix. . . . . . . . : \n"));
_tprintf(_T("\tNode Type . . . . . . . . . . . . : %s\n"), GetNodeTypeName(pFixedInfo->NodeType));
if (pFixedInfo->EnableRouting)
_tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
else
_tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
if (pAdapter->HaveWins)
_tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
else
_tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
_tprintf(_T("\tDNS Suffix Search List. . . . . . : %s\n"), pFixedInfo->DomainName);
}
while (pAdapter)
{
_tprintf(_T("\n%s ...... : \n\n"), GetInterfaceTypeName(pAdapter->Type));
/* check if the adapter is connected to the media */
if (_tcscmp(pAdapter->IpAddressList.IpAddress.String, "0.0.0.0") == 0)
{
_tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
pAdapter = pAdapter->Next;
continue;
}
_tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo->DomainName);
if (bAll)
{
_tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), pAdapter->Description);
_tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter->Address));
if (pAdapter->DhcpEnabled)
_tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
else
_tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
_tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
}
_tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter->IpAddressList.IpAddress.String);
_tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter->IpAddressList.IpMask.String);
_tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter->GatewayList.IpAddress.String);
if (bAll)
{
if (pAdapter->DhcpEnabled)
_tprintf(_T("\tDHCP Server . . . . . . . . . . . : %s\n"), pAdapter->DhcpServer.IpAddress.String);
_tprintf(_T("\tDNS Servers . . . . . . . . . . . : "));
_tprintf(_T("%s\n"), pFixedInfo->DnsServerList.IpAddress.String);
pIPAddr = pFixedInfo -> DnsServerList.Next;
while (pIPAddr)
{
_tprintf(_T("\t\t\t\t\t %s\n"), pIPAddr ->IpAddress.String );
pIPAddr = pIPAddr ->Next;
}
if (pAdapter->HaveWins)
{
_tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter->PrimaryWinsServer.IpAddress.String);
_tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter->SecondaryWinsServer.IpAddress.String);
}
if (pAdapter->DhcpEnabled)
{
_tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter->LeaseObtained)));
_tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter->LeaseExpires)));
}
}
_tprintf(_T("\n"));
pAdapter = pAdapter->Next;
}
return 0;
}
INT Release(TCHAR Index)
{
IP_ADAPTER_INDEX_MAP AdapterInfo;
DWORD dwRetVal = 0;
/* if interface is not given, query GetInterfaceInfo */
if (Index == (TCHAR)NULL)
{
PIP_INTERFACE_INFO pInfo;
ULONG ulOutBufLen;
pInfo = (IP_INTERFACE_INFO *) malloc(sizeof(IP_INTERFACE_INFO));
ulOutBufLen = 0;
/* Make an initial call to GetInterfaceInfo to get
* the necessary size into the ulOutBufLen variable */
if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
{
GlobalFree(pInfo);
pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen);
}
/* Make a second call to GetInterfaceInfo to get the actual data we want */
if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR )
{
AdapterInfo = pInfo->Adapter[0];
_tprintf(_T("name - %S\n"), pInfo->Adapter[0].Name);
}
else
{
_tprintf(_T("\nGetInterfaceInfo failed : "));
DoFormatMessage(dwRetVal);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?