Was ist falsch mit "gethostbyname"?

Ich bin mit diesem code-snippet fand ich in http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/ zum durchführen von dns-lookups

#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;
}

Ich bin vor einer seltsamen Tatsache

./test www.google.com
Hostname: www.l.google.com
IP Address: 209.85.148.103

funktioniert gut, aber wenn ich versuche, Sie zu lösen eine unvollständige IP-Adresse bekomme ich diese

./test 10.1.1
Hostname: 10.1.1
IP Address: 10.1.0.1

Ich würde erwarten, dass eine Fehlermeldung wie die folgende

./test www.google
gethostbyname(): : Unknown host

aber das Programm scheint zu funktionieren.

Ahnung warum?

  • Ich habe gerade versucht den code von en.wikipedia.org/wiki/Getaddrinfo für getaddrinfo, aber 10.1.1 Schweller löst in 10.1.0.1 ... Ist es ein bug? Bin ich etwas fehlt?
Schreibe einen Kommentar