Name & Address Conversions

 

 

 

Ø Domain Name Servers 

Ø Basic Functions

Ø Support for both IPv4 and IPv6

Ø Example of Protocol-Independent Client:



Ø Domain Name Servers 

ü File:  /etc/resolv.conf
contains the IP addresses of the local name servers

ü File:  /etc/services
contains the names and ports of services.

 



Ø Basic Functions

 

ü gethostbyname  

& 

ü gethostbyaddr   

 

ª  Hosent struct


    addres1

 

       h_name (official name)
       h_aliases       
       h_addr_list

 

à  Example:  GetHostInfo.c

% cd /home/cs779/stevens3rd.book/unpv13e/names

% GetHostInfo  fast  cnn.com 128.82.4.210  mail  ftp  www 

à  GetHostInfo.c
 
Code Outline:
      . . . . 
      if ( (inetaddr = inet_addr(ptr)) != -1){
         if ((hptr = gethostbyaddr((char *)&inetaddr, sizeof(inetaddr), AF_INET)) == NULL) {
            printf("\t(gethostbyaddr failed %s)\n", ptr);
            continue;
         }
      } else{
         if ( (hptr = gethostbyname(ptr)) == NULL) {
            printf("\t(gethostbyname failed %s)\n", ptr);
            continue;
         }
      }
      printf("official hostname: %s\n", hptr->h_name);
      for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
         printf("\talias: %s\n", *pptr);
      switch (hptr->h_addrtype) {
      case AF_INET:
         pptr = hptr->h_addr_list;
         for ( ; *pptr != NULL; pptr++)
            printf("\taddress: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
         break;
 . . . .
 

à  Example: daytimetcpcli2.c

% daytimetcpsrv1 10313

% daytimetcpsrv1 daytime   // must be root, since daytime port is 13

% daytimetcpcli2 128.82.4.210 10313
% daytimetcpcli2  something  10313
% daytimetcpcli2 128.82.4.210 daytime
% daytimetcpcli2  something  daytime

% daytimetcpcli2  fast 10313  // try connecting to all fast aliases.

 

ª  Other Functions     getipnodebyname   &  getipnodebyaddr.pdf 




Ø Support for both IPv4 and IPv6

 
 

ª  Latest Functions

 

getaddrinfo   &  getnameinfo   

 

ª  Addrinfo struct

 

addres2short

 

 

à  Example:  GetGenHostInfo.c

% cd /home/cs779/stevens3rd.book/unpv13e/names

% GetGenHostInfo  fast  cnn.com 128.82.4.210  mail  ftp  www

% GetGenHostInfo   fe80::203:baff:fe2a:ab9b  

 

à  GetGenHostInfo.c

 

. . . .

      if ( (n = getaddrinfo(ptr, NULL, &hints, &res)) != 0)
         err_quit("udp_server error for %s: %s", ptr, gai_strerror(n));
 
      do{
 
         if (res->ai_canonname)
            printf("ai_canonname = %s\n", res->ai_canonname);
 
         printf("address:(%s) %s\n",str_fam(res->ai_family), 
                        Sock_ntop(res->ai_addr, res->ai_addrlen));
 
         
 
         rc = getnameinfo(res->ai_addr, res->ai_addrlen, rhost, 
                              NI_MAXHOST, NULL, NULL, NI_NAMEREQD);
         if (rc == 0) {
            printf("getnameinfo: host = %s\n", rhost );
         } else
            printf("getnameinfo returned %d (%s)\n", rc, gai_strerror(rc));
 
      } while ( (res = res->ai_next) != NULL);
 
 

 

 

 


Ø Example of Protocol-Independent Client:

 

à    cgu.c (c4u+c6u)  - client general  (4+6) udp:

     
     . . . 
 
     struct sockaddr_storage     servaddr;
   struct addrinfo hints, *res;
   
  
   bzero(&hints, sizeof(struct addrinfo));
   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_DGRAM;
   
 
   getaddrinfo(argv[1], "1313", &hints, &res);
         
   sockfd = socket(res->ai_family, SOCK_DGRAM, 0);
 
   connect(sockfd, (SA *)res->ai_addr, res->ai_addrlen);     
 

. . .

 

ü Testing:

something % cd /home/cs779/stevens3rd.book/unpv13e/intro

something % s6u &

something % cgu fe80::203:baff:fe2a:67ff

something % cgu 128.82.4.210

//replace s6u with s4u:

something % s4u &

something % cgu 128.82.4.210

something % cgu ::ffff:128.82.4.210