dnsresolve.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Nov 2010 04:05:14 +0100
changeset 153 b223089872b6
parent 77 49e0babccb23
child 154 142730cfecee
permissions -rw-r--r--
ipv6 support added 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>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <arpa/inet.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <netdb.h>
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
int main (int argc, char **argv)
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
{
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    14
    struct addrinfo hints;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    15
    struct addrinfo *result, *rp;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    16
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    17
    void *addr = NULL;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    18
    char ip[INET6_ADDRSTRLEN];
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    if (argc != 2) {
61
4b4c97f179da Lazy BNF / EBNF Help Messages
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 48
diff changeset
    21
        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
    22
        return EXIT_FAILURE;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    25
    memset (&hints, 0, sizeof (hints));
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    26
    hints.ai_family = AF_UNSPEC;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    27
    hints.ai_socktype = SOCK_STREAM;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    28
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    29
    if (getaddrinfo (argv[1], NULL, &hints, &result) != 0) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    30
        perror ("getaddrinfo");
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    31
        return EXIT_FAILURE;
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    32
    }
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    34
    for (rp = result; rp != NULL; rp = rp->ai_next) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    35
        if (rp->ai_family == AF_INET6) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    36
            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
    37
            addr = &(ipv6->sin6_addr);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    38
        } else if (rp->ai_family == AF_INET) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    39
            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
    40
            addr = &(ipv4->sin_addr);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    41
        }
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    42
        if (addr != NULL) {
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    43
            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
    44
                printf ("%s: %s\n", argv[1], ip);
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    }
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
153
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    48
    if (result != NULL)
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    49
        freeaddrinfo (result);
b223089872b6 ipv6 support added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    50
8
96d16dfe787a We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    51
    return EXIT_SUCCESS;
0
af501b0c1716 demos cvs copy
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
}