Exemplos de resolução de nomes

direct-dns.c
#include <stdio.h>
#include <netdb.h>
 
int main (int argc, char *argv[])
{
   struct hostent *host ;
   struct in_addr address ;
   int i ;
 
   if (argc != 2)
   {
      fprintf (stderr,"Uso: resolve nome ");
      exit (1);
   }
 
   host = gethostbyname (argv[1]) ;
   if ( ! host )
   {
      herror ("gethostbyname");
      exit (1);
   }
 
   printf ("Endereços:\n") ;
   for (i=0; host->h_addr_list[i]; i++)
   {
      bcopy (host->h_addr_list[i], &address, host->h_length);
      printf ("  %s\n", inet_ntoa (address)) ;     
   }
}
reverse-dns.c
#include <stdio.h>
#include <netdb.h>
 
int main (int argc, char *argv[])
{
   struct hostent *host ;
   struct in_addr address ;
   int i ;
 
   if (argc != 2)
   {
      fprintf (stderr,"Uso: resolver nome ");
      exit (1);
   }
 
   if ( ! inet_aton (argv[1], &address) )
   {
      perror ("inet_aton") ;
      exit (1) ;
   }
 
   host = gethostbyaddr (&address, sizeof (address), AF_INET) ;
   if ( ! host )
   {
      herror ("gethostbyaddr");
      exit (1);
   }
 
   printf ("Nome principal: %s\n", host->h_name) ;
 
   printf ("Outros nomes  :\n") ;
   for (i=0; host->h_aliases[i]; i++)
      printf ("  %s\n", host->h_aliases[i]) ;
}
  • pua/resolucao_de_nomes.txt
  • Última modificação: 2014/03/27 09:30
  • por maziero