[Original]socket communication for domain name resolution

Source: Internet
Author: User
Tags socket error htons

//extern unsigned char gsntpserverip[20];intGetntptime (unsignedChar*ntpserverip,unsignedintntpport,unsignedint*data) {      intSockfd=0; Charntpservername[ -];                Stnp_header H_NTP; H_NTP. Livnmode=0x1b;//LI (2bit) VN (3bit version) mode (3bit client mode)H_NTP. Stratum =stratum; H_NTP. Poll=POLL; H_NTP. Precision=PREC; memset (Ntpservername,0,sizeof(Ntpservername)); sprintf (Ntpservername,"%s", Ntpserverip); Nf_log (Debug_k,"@[email protected],sync time from%S,H_NTP. stratum=%d\n", NTPSERVERNAME,H_NTP.     Stratum); #if0//how the domain name cannot be resolved    structsockaddr_in server; Server.sin_family=af_inet; Server.sin_addr.s_addr= Gethostipbyname ((Const Char*) ntpservername); Server.sin_port=htons (Ntpport); if(-1== (int) {Nf_log (server.sin_addr.s_addr) {error_l,"[%s,%d]s_addr error...\n", __function__,__line__); return-1; } SOCKFD= Socket (Af_inet, SOCK_DGRAM,0); if(sockfd<0) {Nf_log (error_l,"[%s,%d]socket error...\n", __function__,__line__); return-1; }    #endif        #if1//how getaddrinfo can parse a domain name    intRC; Addrinfo hints,*res = null,*cur=NULL; memset (&hints,0,sizeof(hints)); Hints.ai_family=Af_unspec; Hints.ai_socktype=Sock_dgram; Hints.ai_protocol=ipproto_udp; Charport[Ten]; memset (Port,0,sizeof(port)); sprintf (Port,"%d", Ntpport); RC= Getaddrinfo ((Const Char*) Ntpservername, port, &hints, &res); if(RC! =0) {Nf_log (error_l,"[%s,%d]getaddrinfo error...rc=%s\n", __function__,__line__,gai_strerror (RC)); return-1; }    structSOCKADDR_IN *addr; Charm_ipaddr[ -]; memset (M_IPADDR,0,sizeof(M_IPADDR));  for(cur = res; cur! = NULL; cur = cur->Ai_next) {Addr= (structsockaddr_in *) cur->ai_addr; Inet_pton (Af_inet, m_ipaddr, (void*) addr); Inet_ntop (Af_inet, (void*) &addr, M_IPADDR, -);//Inverse Conversionprintf"@[email protected]:P arse ip:%s\n", M_IPADDR); } SOCKFD= Socket (res->ai_family, Res->ai_socktype, res->Ai_protocol); if(SOCKFD <0) {Nf_log (error_l,"[%s,%d]socket error...\n", __function__,__line__); return-1; }    #endif    //if (SendTo (SOCKFD, (void*) &h_ntp, sizeof (Stnp_header), 0, (struct sockaddr*) &server, sizeof (server)) <0 )      if(SendTo (SOCKFD, (void*) &AMP;H_NTP,sizeof(Stnp_header),0, RES-&GT;AI_ADDR, Res->ai_addrlen) <0) {Nf_log (error_l,"[%s,%d]sendto error...\n", __function__,__line__); return-1;      } fd_set R; Fd_zero (&R); Fd_set (SOCKFD,&R); structtimeval timeout; Timeout.tv_sec=Ten; Timeout.tv_usec=0; if(1!=Select(sockfd+1, &r, NULL, NULL, &timeout)) {Nf_log (error_l,"[%s,%d] Select error...\n", __function__,__line__); return-1; }        if(Recv (SOCKFD, (void*) data, -,0) <0)//48 follow-up to define macro{nf_log (error_l,"[%s,%d]recv error...\n", __function__,__line__); return-1;      } close (SOCKFD); return 0; }  
getaddrinfo function

Use of the structure of the body to understand
Addrinfo
Sockaddr_in
/* Internet address.   */

typedef __U16 IN_PORT_T;
typedef __U32 in_addr_t;

structin_addr {in_addr_t s_addr; };/*Structure describing an Internet socket address. */structsockaddr_in {__sockaddr_common (sin_);            in_port_t Sin_port; /*Port number. */    structIN_ADDR sin_addr;/*Internet address. */    /*Pad to size of ' struct sockaddr '. */unsignedCharsin_zero[sizeof(structSOCKADDR)-__sockaddr_common_size-sizeof(in_port_t)-sizeof(structin_addr)]; };

Print the address of the sockaddr_in format in dotted decimal mode:

struct sockaddr_in *addr;
Char m_ipaddr[16];
memset (m_ipaddr,0,sizeof (M_IPADDR));


for (cur = res; cur = NULL; cur = cur->= (struct sockaddr_in *) cur->Ai_addr;inet_pton (AF _inet, M_ipaddr, (void *) addr) inet_ntop (Af_inet, (void); // Reverse Convert printf ("@[email protected]:P arse ip:%s\n", m_ipaddr);}

======

The differences between these structures need to be differentiated

Sockaddr_in is used for IPV4 network Internet ip/port, so its struct member sin_family must be filled in af_inet??

 struct   sockaddr_in server;      server.sin_family  = af_inet; Server.sin_addr.s_addr  = Gethostipbyname ((const       char  * = htons (Ntpport);  if  (-1  = = ( int  ) server.sin_addr.s_addr) {Nf_log (error_l,   "
     [%s,%d]s_addr error...\n   " ,__function__,__line__);  return -1       ; }

Addrinfo

Addrinfo hints, *res = null,*cur=NULL; memset (&hints,0,sizeof(hints)); Hints.ai_family=Af_unspec; Hints.ai_socktype=Sock_dgram; Hints.ai_protocol=ipproto_udp; Charport[Ten]; memset (Port,0,sizeof(port)); sprintf (Port,"%d", Ntpport); RC= Getaddrinfo ((Const Char*) Ntpservername, port, &hints, &res);

Information obtained from the Address field is stored in the Res

operation Res Gets the address and port information to link and send data: SOCKFD= Socket (res->ai_family, Res->ai_socktype, res->Ai_protocol); if(SOCKFD <0) {Nf_log (error_l,"[%s,%d]socket error...\n", __function__,__line__); return-1; }if(SendTo (SOCKFD, (void*) &AMP;H_NTP,sizeof(Ntp_header),0, RES-&GT;AI_ADDR, Res->ai_addrlen) <0) {Nf_log (error_l,"[%s,%d]sendto error...\n", __function__,__line__); return-1; } 

Getaddrinfo can get multiple IP addresses, here you can go to bind and try to see which one can be optimized:

8591729

Pending confirmation:
Gethostipbyname cannot resolve address domain??? The reason why this was not resolved was because no DNS server was added to the resolv.conf






[Original]socket communication for domain name resolution

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.