Exemplos de resolução de nomes
Resolução direta
- 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)) ; } }
Resolução reversa
- 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]) ; }