estoy usando este fragmento de código que he encontrado en http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/ para realizar búsquedas DNS¿Qué pasa con gethostbyname?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[ ]) {
struct hostent *h;
/* error check the command line */
if(argc != 2) {
fprintf(stderr, "Usage: %s hostname\n", argv[0]);
exit(1);
}
/* get the host info */
if((h=gethostbyname(argv[1])) == NULL) {
herror("gethostbyname(): ");
exit(1);
}
else
printf("Hostname: %s\n", h->h_name);
printf("IP Address: %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));
return 0;
}
Estoy frente a un hecho raro
./test www.google.com
Hostname: www.l.google.com
IP Address: 209.85.148.103
funciona bien, pero si trato de resolver una dirección IP incompleta me sale este
./test 10.1.1
Hostname: 10.1.1
IP Address: 10.1.0.1
yo esperaría que un error como el siguiente
./test www.google
gethostbyname(): : Unknown host
pero el programa parece funcionar.
¿Alguna idea de por qué?
Acabo de probar el código de http://en.wikipedia.org/wiki/Getaddrinfo para getaddrinfo, pero el alféizar 10.1.1 se resuelve en 10.1.0.1 ... ¿Es un error? ¿Me estoy perdiendo de algo? –