Source code-level Unix/Linux General Nic IP address acquisition method
These two functions are used: gethostname () and gethostbyname ()
int gethostname(char *name, size_t namelen);
Description
TheGethostname() Function shall return the standard host name for the current machine.NamelenArgument shall specify the size of the array pointed to byNameArgument. The returned name shall be null-terminated, doesn t that ifNamelenIs an insufficient length to hold the host name, then the returned name shall be truncated and it is unspecified whether the returned name is null-terminated.
Host names are limited to {host_name_max} bytes.
Struct hostent * gethostbyname (const char * Name );
The input value of this function is the domain name or host name, for example"Www.google.com "," WPC"And so on.
The output value is a hostent structure (as follows ). If the function call fails, null is returned.
Struct hostent {
Char * h_name;
Char ** h_aliases;
Int h_addrtype;
Int h_length;
Char ** h_addr_list;
};
Explain this structure:
Where,
Char * h_name indicates the specification name of the host. For exampleWww.google.comIs actuallyWww.l.google.com.
Char ** h_aliases indicates the host alias.Www.google.comThat is, Google's own Alias. Sometimes, some hosts may have several aliases, which are used to retrieve multiple names for their websites for ease of user memory.
Int h_addrtype indicates the Host IP Address type, whether it is IPv4 (af_inet) or IPv6 (af_inet6)
Int h_length indicates the length of the Host IP address.
Int ** h_addr_lisst indicates the IP address of the host. Note that the IP address is stored in byte sequence of the network. Do not directly use printf with the % S parameter to do this. It may be a problem. Therefore, if you really need to print this IP address, you need to call inet_ntop ().
Const char * inet_ntop (int af, const void * SRC, char * DST, socklen_t CNT ):
This function is to convert the network address structure SRC of Type af into a host-ordered string and store it in a string with the length of CNT.
This function actually returns a pointer to DST. If a function call error occurs, the return value is null.
The following is a routine with detailed comments.
# Include <stdio. h>
# Include <ARPA/inet. h> // For inet_ntop ()
# Include <unistd. h> // For gethostname ()
# Include <netdb. h> // For gethostbyname ()
# Include <sys/socket. h>
Int main (INT argc, char ** argv)
{
Char ** pptr;
Struct hostent * hptr;
Char hostname [32];
Char STR [32];
If (gethostname (hostname, sizeof (hostname )))
{
Printf ("gethostname calling error/N ");
Return 1;
}
/* Call gethostbyname (). All call results exist in hptr */
If (hptr = gethostbyname (hostname) = NULL)
{
Printf ("gethostbyname error for host: % s/n", hostname );
Return 0;/* if an error occurs when gethostbyname is called, 1 is returned */
}
/* Name the host specification */
Printf ("Official hostname: % s/n", hptr-> h_name );
/* The host may have multiple aliases, and all aliases can be typed out separately */
For (pptr = hptr-> h_aliases; * pptr! = NULL; pptr ++)
Printf ("Alias: % s/n", * pptr );
/* Type the address */
Switch (hptr-> h_addrtype)
{
Case af_inet:
Case af_inet6:
Pptr = hptr-> h_addr_list;
/* Type all the addresses you just obtained. The inet_ntop () function is called */
For (; * pptr! = NULL; pptr ++)
Printf ("Address: % s/n", inet_ntop (hptr-> h_addrtype, * pptr, STR, sizeof (STR )));
Break;
Default:
Printf ("unknown address type/N ");
Break;
}
Return 0;
}
Output result:
Official hostname: localhost. localdomain
Alias: localhost
Address: 127.0.0.1
(It seems that the IP address is meaningless. Run it on another machine and the IP address is 192.168.0.3, while the IP address provided by ifconfig is 192.168.2.100. What is the problem? Although the computer has dual NICS)
In UNIX and Linux systems, you can obtain the system IP address (gethostbyname and IOCTL) in two ways)
Gethostbyname obtains the network address of the corresponding computer through domain name resolution. IOCTL is a series of network functions to obtain the IP address of the local machine.
(The IOCTL method is recommended. The IP address displayed in this method is the same as the IP address displayed in the ifconfig command, and can run normally on the arm board without modification. The IP address provided by gethostname () and gethostbyname () methods is inconsistent with that provided by ifconfig. You cannot [do not understand why] and cannot run properly on the arm board .) IOCTL sample program
# Include <stdio. h>
# Include <sys/types. h>
# Include <net/If. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <Linux/sockios. h>
# Include <sys/IOCTL. h>
# Include <ARPA/inet. h>
Int main (void)
{
Int S;
Struct ifconf conf;
Struct ifreq * IFR;
Char buff [bufsiz];
Int num;
Int I;
S = socket (pf_inet, sock_dgram, 0 );
Conf. ifc_len = bufsiz;
Conf. ifc_buf = Buff;
IOCTL (S, siocgifconf, & conf );
Num = Conf. ifc_len/sizeof (struct ifreq );
IFR = Conf. ifc_req;
For (I = 0; I <num; I ++)
{
Struct sockaddr_in * sin = (struct sockaddr_in *) (& IFR-> ifr_addr );
IOCTL (S, siocgifflags, IFR );
If (IFR-> ifr_flags & iff_loopback) = 0) & (IFR-> ifr_flags & iff_up ))
{
Printf ("% s (% s)/n ",
IFR-> ifr_name,
Inet_ntoa (sin-> sin_addr ));
}
IFR ++;
}
}
Output:
Eth1 (10.60.68.127) Method 2: (not recommended, although the Code is a little simple, it is not clear for effective running scenarios)