Introduction to IP helper API

Source: Internet
Author: User

IP helper is an API used to manage local network settings. Using this API can easily change the computer's network settings or extract relevant information. It also provides a message mechanism to notify the application when the network settings of the Local Computer change. In fact, it can not only extract the network settings of the local machine, but also obtain the IP usage and MAC address of other computers on the network.

Start with the simplest
The simplest thing is to view the network settings on the computer without modifying them.

Extract Nic Information

Hinst = loadlibrary ("iphlpapi. dll ");
If (! Hinst)
Cout <"iphlpapi. dll not supported in this platform! \ N ";

These three lines of code are used to load the iphlpapi library file,

The following lines of code show the process of calling a function in the DLL:

Pgainfo = (pgainfo) getprocaddress (hinst, "getadaptersinfo ");
Ulong ulsize = 0;
Pgainfo (pinfo, & ulsize );
Pinfo = (pip_adapter_info) New (char [ulsize]);
Pgainfo (pinfo, & ulsize );

The first line of code is to obtain the entry address of the getadaptersinfo function, so that the function can be called by pointer later. This function extracts the information of the network card and receives two parameters. The first parameter is the first address of the memory buffer used to save the information of the network card, the second parameter is the buffer size. Because you do not know the number of NICs on the local machine in advance, you cannot know how much cache should be allocated. Fortunately, when the buffer size is insufficient, the getadaptersinfo function will fill in the second parameter, which is the ulsize, with the buffer size to be allocated. In this way, you can call getadaptersinfo twice to obtain the buffer size for the first time, allocate the buffer, and then call it again to obtain the actual Nic information.

The information returned by getadaptersinfo through pinfo is organized as a linked list. The following code accesses the linked list:

While (pinfo)
{
// Access NIC data
// Move the current pointer to the next node
Pinfo = pinfo-> next;
}

The code is omitted to illustrate the problem.

Let's take a look at what information getadaptersinfo returns. The following is an explanation of the data structure pointed to by pinfo:

Typedef struct _ ip_adapter_info {
Struct _ ip_adapter_info * Next; // linked list pointer field, which traverses the static key table

DWORD comboindex; // reserved unused

Char adaptername [max_adapter_name_length + 4]; // Nic name

Char description [max_adapter_description_length + 4]; // Nic description

Uint addresslength; // The length of the physical address. The physical address in the following array is displayed.

Byte Address [max_adapter_address_length]; // physical address. Each byte stores a hexadecimal value. In combination with the previous data field, each byte is output in X format in printf.

DWORD index; // Nic Index Number

Uint type; // network card type

Uint dhcpenabled; // whether DHCP Dynamic IP allocation is enabled

Pip_addr_string currentipaddress; // the currently used IP address.

Ip_addr_string ipaddresslist; // list of IP addresses bound to this Nic. Important items

Ip_addr_string gatewaylist; // gateway address linked list, important project

Ip_addr_string dhcpserver; // DHCP server address, valid only when dhcpenabled = true

Bool havewins; // whether wins is enabled

Ip_addr_string primarywinsserver; // master wins address

Ip_addr_string secondarywinsserver; // The secondary wins address.

Time_t leaseobtained; // time obtained by the current DHCP lease

Time_t leaseexpires; // The expiration time of the Current DHCP lease. These two data structures are only useful when DHCP is enabled.

} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;

The IP address strings ipaddresslist and gatewaylist in this data structure are all organized as linked lists. In fact, all data can be viewed in the advanced options on the control panel | Network | properties | TCP/IP properties page.

Extract Network Interface Information

The two most important functions are getnumberofinterfaces and getinterfaceinfo. The former indicates the number of network interfaces, and the latter extracts the information of network interfaces. One thing to note about the first function is that according to msdn: a network interface is the logical abstraction of the NIC, and they are a one-to-one relationship. In fact, because each system has a network connection port for debugging, the IP address of this interface is 127.0.0.1 and the subnet mask is 255.0.0.0. The ip_interface_info structure returned by getinterfaceinfo also contains a numadapters integer data field that records the correct Nic. Then you need to note the getinterfaceinfo
It means that it must also be called twice. Obtain the buffer size for the first time, and set the value for the second time. The ip_interface_info returned by getinterfaceinfo does not use a linked list instead of a dynamic array. Therefore, the code for Traversing each element becomes:

For (INT I = 0; inumadapters; I ++)
{
Cout <"adapter index:" <adapter [I]. index <adapter [I]. Name <Endl;
}

The structure of ip_interface_info is described as follows:

Typedef struct _ ip_interface_info {
Long numadapters; // number of network interface elements in a dynamic array, which can be used to traverse the Array

Ip_adapter_index_map adapter [1]; // Network Interface data array

} IP_INTERFACE_INFO,*PIP_INTERFACE_INFO;

The ip_adapter_index_map structure is as follows:

Typedef struct _ ip_adapter_index_map {
Ulong index; // Nic Index

Wchar name [max_adapter_name]; // Nic name

} IP_ADAPTER_INDEX_MAP, * PIP_ADAPTER_INDEX_MAP;

Extract IP information
The extracted network interface information is the same.

Set Local Network
The setting process is similar to the extraction process.

Other API functions
These functions can view or set the information of network datagram. For example, the getipstatistics and geticmpstatistics functions can view the traffic of the current IP datagram and ICMP datagram, and the number of discarded datagram. Using these functions, you can build a network monitoring program to check faults in the network. You can also use the setipstatistics function to set the corresponding IP protocol stack attribute to shorten or extend the default TTL value of IP datagram. You can also use getipforwardtable, createipforwardentry, deleteipforwardentry, and setipforwardentry to obtain the IP route table information, create route table items, delete route table items, and modify route table items.
. You can also use getbestroute and getbestinterface to obtain the best route point and network interface to reach the specified IP address.

In fact, through these functions, we can get many MIB variables (second volume of "using TCP/IP protocol for Internet connection"). Through these MIB variables, we can quickly create a network management software.

Sendarp function in IP helper API. It can send ARP packets and return the MAC address of the target machine.

This API provides the asynchronous notification function to send messages to applications when network settings change. Because they are very simple, they are used in the same way as wsaasyncselect in Winsock.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.