dnsresolve.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 19 Nov 2010 00:34:26 +0100
changeset 155 a54dcc86addb
parent 154 142730cfecee
permissions -rw-r--r--
cunit needs libcunit and libncurses committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 61
diff changeset
     2
 * dnsresolve.c
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 8
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
154
142730cfecee proper includes for getaddrinfo and inet_ntop
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
     9
#include <sys/types.h>
142730cfecee proper includes for getaddrinfo and inet_ntop
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    10
#include <sys/socket.h>
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <arpa/inet.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <netdb.h>
154
142730cfecee proper includes for getaddrinfo and inet_ntop
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 153
diff changeset
    13
#include <netinet/in.h>
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
int main (int argc, char **argv)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
{
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    17
    struct addrinfo hints;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    18
    struct addrinfo *result, *rp;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    19
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    20
    void *addr = NULL;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    21
    char ip[INET6_ADDRSTRLEN];
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    if (argc != 2) {
61
4b4c97f179da Lazy BNF / EBNF Help Messages
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 48
diff changeset
    24
        fprintf (stderr, "Usage: %s <hostname>\n", argv[0]);
48
b94d657a9acb Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 27
diff changeset
    25
        return EXIT_FAILURE;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    28
    memset (&hints, 0, sizeof (hints));
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    29
    hints.ai_family = AF_UNSPEC;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    30
    hints.ai_socktype = SOCK_STREAM;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    31
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    32
    if (getaddrinfo (argv[1], NULL, &hints, &result) != 0) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    33
        perror ("getaddrinfo");
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    34
        return EXIT_FAILURE;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    35
    }
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    37
    for (rp = result; rp != NULL; rp = rp->ai_next) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    38
        if (rp->ai_family == AF_INET6) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    39
            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)rp->ai_addr;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    40
            addr = &(ipv6->sin6_addr);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    41
        } else if (rp->ai_family == AF_INET) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    42
            struct sockaddr_in *ipv4 = (struct sockaddr_in *)rp->ai_addr;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    43
            addr = &(ipv4->sin_addr);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    44
        }
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    45
        if (addr != NULL) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    46
            if (inet_ntop (rp->ai_family, addr, ip, sizeof (ip)) != NULL)
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    47
                printf ("%s: %s\n", argv[1], ip);
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    51
    if (result != NULL)
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    52
        freeaddrinfo (result);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    53
8
96d16dfe787a We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    54
    return EXIT_SUCCESS;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
}