Click here to Skip to main content
15,905,877 members
Articles / Desktop Programming / MFC
Article

Review of the network adapter parameters

Rate me:
Please Sign up or sign in to vote.
2.70/5 (17 votes)
6 May 20031 min read 57.2K   1.1K   18   5
This article describes a program that shows the information about different network interfaces operating at the current moment.

Introduction

This article describes a program that shows the information about different network interfaces operating at the current moment.

The basis of this utility is the use of such API functions as GetAdaptersInfo(), inet_addr() and gethostbyaddr(). The program displays the adapters’ names in the system, protocol type (PPP, Ethernet, etc.), IP addresses of the given computers and gateway and their NetBIOS names.

You need to include in the program, the following header files besides the standard: Iphlpapi.h, IPIfCons.h and Winsock2.h. And it is also necessary to include in the project, such libraries as: Iphlpapi.lib and Ws2_32.lib.

Let's examine the function prototype GetAdaptersInfo():

DWORD GetAdaptersInfo(
     PIP_ADAPTER_INFO pAdapterInfo,
     PULONG pOutBufLen
);

The first argument – is a pointer to a buffer, where the connected list of structures IP_ADAPTER_INFO is stored. Each structure carries information about one of the system network adapters. The second argument is a pointer to the variable of the unsigned long type that stores the buffer size.

Let's examine the structure IP_ADAPTER_INFO:

struct IP_ADAPTER_INFO {
     struct _IP_ADAPTER_INFO* Next; 
     DWORD ComboIndex; 
     char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
     char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
     UINT AddressLength; 
     BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
     DWORD Index; 
     UINT Type; 
     UINT DhcpEnabled; 
     PIP_ADDR_STRING CurrentIpAddress; 
     IP_ADDR_STRING IpAddressList; 
     IP_ADDR_STRING GatewayList; 
     IP_ADDR_STRING DhcpServer; 
     BOOL HaveWins; 
     IP_ADDR_STRING PrimaryWinsServer; 
     IP_ADDR_STRING SecondaryWinsServer; 
     time_t LeaseObtained; 
     time_t LeaseExpires; 
}

The fields' names are made clear for understanding. The structure contains a lot of useful information.

After we have the connected list of these structures, we have to go through it and get the needed information. It can be done this way, for example:

PIP_ADAPTER_INFO pCurAdapt = &buffer[0];
do {
     //getting the needed information from the structure.
} while ((pCurAdapt = pCurAdapt->Next) != NULL);

The type of the network protocol can be got from the structure IP_ADAPTER_INFO's member type, in the following way:

char* GetType(UINT type) {
     switch (type) {
     case MIB_IF_TYPE_OTHER:
          return "Unknown";
     case MIB_IF_TYPE_ETHERNET:
          return "Ethernet";
     case MIB_IF_TYPE_TOKENRING:
          return "Token ring";
     case MIB_IF_TYPE_FDDI:
          return "FDDI";
     case MIB_IF_TYPE_PPP:
          return "PPP";
     case MIB_IF_TYPE_LOOPBACK:
          return "Loop Back";
     case MIB_IF_TYPE_SLIP:
          return "SLIP";
      }
     return "Unknown";
}

At last, we have to examine the process of receiving the computer name through IP address. IP address, we will get through IP_ADDR_STRING structure.

struct IP_ADDR_STRING {
     struct _IP_ADDR_STRING* Next; 
     IP_ADDRESS_STRING IpAddress;
     IP_MASK_STRING IpMask; 
     DWORD Context;
}

The function that receives the host name through IP address is shown below:

bool GetDN(char* ip, char* host, unsigned int len) {
     unsigned long res;
     WSADATA wsaData;
     HOSTENT* pHost;
     res = inet_addr(ip);
     if ((res == INADDR_NONE) || (res == 0)) {
          strcpy(host, "unable to resolve");
          return false;
     }
     if (WSAStartup(MAKEWORD(1, 1), &wsaData)) {
          strcpy(host, "unable to resolve");
          return false;
     }
     pHost = gethostbyaddr((char*)&res, sizeof(res), AF_INET);
     WSACleanup();
     if (pHost == NULL) {
          strcpy(host, "unable to resolve");
          return false;
     }
     if (strlen(pHost->h_name)>len) {
          strcpy(host, "buffer too small");
          return false;
     }
     strcpy(host, pHost->h_name);
     return true;
}

It is called in the following way:

// ...
char host[1024];
GetDN(pCurIP->IpAddress.String, host, sizeof(host));

Links

  • Home site (software, IT related links, sources, articles)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow create Adapter Card Pin
Thai Duong Nguyen6-Apr-08 21:23
Thai Duong Nguyen6-Apr-08 21:23 
QuestionSmall command line app based on your work Pin
mrstu15-Nov-07 10:47
mrstu15-Nov-07 10:47 
Generalgetadaptersinfo fails on WinNT Pin
mytechiedata16-Feb-05 17:28
mytechiedata16-Feb-05 17:28 
GeneralGeting information Pin
one_eddie4-Aug-04 0:58
one_eddie4-Aug-04 0:58 
i need to get info about (from inet-connected lan card):
1. ip address
2. net mask
3. check if DHCP is used
4. gateway IP and DNS IP adresses

Could IP helper APIs help me, would they work on Win98?
GeneralIPHlpApi.h might be missing on some computers Pin
MoakyMoak7-Feb-04 8:16
sussMoakyMoak7-Feb-04 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.