Domain name and IP information parsing
/etc/hosts file has some IP address and domain name hostname information
/ETC/RESOLV.CONF has the IP address of the DNS server
struct hostent{ char *h_name; // official name of the host Char **h_aliases; // host alternate name, null-terminated list int h_addrtype; // the type of return address has two af_inet or Af_inet6 int h_length; // address length in bytes Char **h_addr_list; // Host network address, null-terminated list #define };
Returning host information by domain name
That is, through the domain name such as www.baidu.com to get the corresponding IP address, aliases and other information.
struct hostent *gethostbyname (__const char *__name) : parameter is host domain name
struct hostent *gethostbyname2 (__const char *__name, int __af) : Parameter 2 is the address protocol type
Example
#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<netdb.h>extern intH_errno;intMainintargcChar**argv) { intx, x2; structHostent *HP; for(x =1; x < argc; ++x) {//read domain name from parameter, return host informationHP =gethostbyname (argv[x]); if(!hp) {fprintf (stderr,"%s:host '%s ' \ n", Hstrerror (H_errno), argv[x]); Continue; } printf ("Host%s: \ n", argv[x]); printf ("officially:\t%s\n", hp->h_name);//official nameFputs"aliases:\t", stdout); for(x2 =0; hp->h_aliases[x2]; ++X2)//Other names { if(x2) fputs (", ", stdout); Fputs (HP-h_aliases[x2], stdout); } FPUTC ('\ n', stdout); printf ("type:\t\t%s\n", Hp->h_addrtype = = af_inet?"af_inet":"Af_inet6"); //returns the dot decimal IP address if(Hp->h_addrtype = =af_inet) { for(x2 =0; hp->h_addr_list[x2]; ++x2) printf ("address:\t%s\n", Inet_ntoa (* (structin_addr*) hp->h_addr_list[x2])); } Putchar ('\ n'); } return 0;}
Return host information via domain name and IP
struct hostent * gethostbyaddr (__const void *__addr, __socklen_t __len, int __type)
Parameter 1: Network byte order of the host's IP address. Need to cast to char*
Parameter 2: Address length, IP address length 4
Parameter 3: Address type (IP address type af_inet)
Example
#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<netdb.h>voidMainintargcConst Char**argv) {U_int addr; structHostent *HP; Char**p; if(ARGC! =2) {printf ("Usage:%s ip-address\n", argv[0]); Exit (Exit_failure); } //Point-to-decimal conversion to 32-bit network byte order if((int) (addr = inet_addr (argv[1])) == -1) {printf ("ip-address must be of the form a.b.c.d\n"); Exit (Exit_failure); } HP= Gethostbyaddr ((Char*) &addr,sizeof(addr), af_inet);//Read host information if(HP = =NULL) {printf ("host information for%s no found\n", argv[1]); Exit (Exit_failure); } for(p = hp->h_addr_list; *p! =0; p++) { structIn_addrinch; Char**Q; memccpy (&inch. s_addr, *p,sizeof(inch. s_addr)); printf ("%s\t%s", Inet_ntoa (inch), hp->h_name); for(q = hp->h_aliases; *q! =0; q++) printf ("%s", *q); printf ("\ n"); } exit (exit_success); }
As above: A lot of things can't be found.
int getaddrinfo (const char *node, const char *service, const struct addrinfo *hints, struct addrinfo ** res)
Protocol-independent, available for IPV4 and IPV6
Parameter 1: node name, which can be a hostname or binary address information. IPV4 for Point 10, or 16 for IPV6
Parameter 2: The port number of a decimal number or service name, such as FTP, HTTP
Parameter 3: The caller fills in the type of information it wants to return.
Parameter 4: Holds the pointer address information that returns the ADDRINFO structure list.
void Freeaddrinfo (struct addrinfo *ai): The res of the getaddrinfo function is obtained dynamically and needs to be returned to the system using the function
const char *gai_strerror (int error): Query Error message
"Linux Advanced Programming" (15th) UDP Network Programming Application 5