Linux network programming--IP address and Domain name resolution (DNS)

Source: Internet
Author: User
Tags domain name server

In daily life, people often know the host domain name without knowing the host IP address, and the socket API is based on IP address, so need to use DNS domain name resolution server to parse.
So the first simple introduction of the principle of DNS, DNS server system is tree-based structure, the top-level domain name server under a number of two-level domain name servers, two-level domain name server under a number of subordinate domain name servers, each server under the jurisdiction of some hosts:

So how does a host query the IP address of a domain name? First need to think of local domain name server query, if not to the superior Domain Name Service query, if the level two is also not found will be to the top-level domain Name server query, if the top-level can not be found, then will return an error. Is the DNS query process for the local host query target host:

The Linux system provides a function to get host information, gethostbyname (), gethostbyaddr (), both of which use a struct, and the structure information is as follows:

struct hostent {        char    *h_name;        /* official name of host */        char    **h_aliases;    /* alias list */        int     h_addrtype;     /* host address type */        int     h_length;       /* length of address */        char    **h_addr_list;  /* list of addresses */  #define h_addr  h_addr_list[0]  /* for backward compatibility */

Functions gethostbyname and gethostbyaddr are also non-reentrant, and the returned results are stored in static variables, which are overwritten by the former and are used with care.
Examples of using gethostbyname and gethostbyaddr:

 #include <netdb.h> #include <string.h> #include <stdio.h> #include <sys/socket.h> intMainintargcChar*argv[]) {structHostent *ht=null;/ * The host domain name of the query * /    Charhost[]="www.sina.com.cn";#if 1    structHostent *ht1=null, *ht2=null;Charhost1[]="Www.sohu.com";/ * Query Host www.sina.com.cn * /HT1 = gethostbyname (host); Ht2 = gethostbyname (host1);//The non-reentrant nature of the function, the former result has been overwritten    intj =0;#else    structIN_ADDR in; IN.S_ADDR = inet_addr ("60.215.128.140"); HT = GETHOSTBYADDR (&in,sizeof(in), af_inet);#endif  for(j =0;j<2; j + +) {if(J = =0) HT = HT1;ElseHT =ht2;printf("----------------------\ n");if(HT) {inti =0;printf("Get the host:%s addr\n", host);/ * Original domain name * /        printf("name:%s\n", ht->h_name);/ * Name * /        /* Protocol family af_inet for IPV4 or Af_inet6 for ipv6*/        printf("type:%s\n", ht->h_addrtype==af_inet?"Af_inet":"Af_inet6");/ * Length of IP address * /        printf("legnth:%d\n", ht->h_length);/ * Print IP address * /         for(i=0;; i++) {if(Ht->h_addr_list[i]! = NULL) {/ * is not the end of an array of IP addresses * /                printf("ip:%s\n", Inet_ntoa (unsigned int*) (Ht->h_addr_list[i]));/ * Print IP address * /}Else{/ * Reach end * /                 Break;/ * Exit For loop * /}        }/ * Print domain address * /         for(i=0;; i++) {/ * loop * /            if(Ht->h_aliases[i]! = NULL) {/ * did not reach the end of the domain name array * /                printf("Alias%d:%s\n", I,ht->h_aliases[i]);/ * Print domain * /}Else{/ * END * /                 Break;/ * Exit loop * /}        }    }   }return 0; }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux network programming--IP address and Domain name resolution (DNS)

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.