File: resolve.c

package info (click to toggle)
krb5 1.18.3-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 51,972 kB
  • sloc: ansic: 302,380; exp: 13,173; cpp: 9,693; python: 9,266; javascript: 9,118; makefile: 7,157; sh: 6,305; perl: 1,926; asm: 1,460; yacc: 1,005; awk: 396; csh: 147; xml: 135; lisp: 104; sed: 41
file content (119 lines) | stat: -rw-r--r-- 3,799 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* tests/resolve/resolve.c */
/*
 * Copyright 1995 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

/*
 * A simple program to test the functionality of the resolver library.
 * It simply will try to get the IP address of the host, and then look
 * up the name from the address. If the resulting name does not contain the
 * domain name, then the resolve library is broken.
 *
 * Warning: It is possible to fool this program into thinking everything is
 * alright by a clever use of /etc/hosts - but this is better than nothing.
 *
 * Usage:
 *   resolve [hostname]
 *
 *   When invoked with no arguments, gethostname is used for the local host.
 *
 */

/* This program tests the resolve library and sees if it is broken... */

#include "k5-platform.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 256
#endif

int
main(int argc, char **argv)
{
    struct addrinfo *ai = NULL, hint;
    char myname[MAXHOSTNAMELEN + 1], namebuf[NI_MAXHOST], abuf[256];
    const char *addrstr;
    int err, quiet = 0;

    argc--; argv++;
    while (argc) {
        if ((strcmp(*argv, "--quiet") == 0) ||
            (strcmp(*argv, "-q") == 0)) {
            quiet++;
        } else
            break;
        argc--; argv++;
    }

    if (argc >= 1) {
        strlcpy(myname, *argv, sizeof(myname));
    } else {
        if(gethostname(myname, MAXHOSTNAMELEN)) {
            perror("gethostname failure");
            exit(1);
        }
    }

    myname[MAXHOSTNAMELEN] = '\0';  /* for safety */

    /* Look up the address... */
    if (!quiet)
        printf("Hostname:  %s\n", myname);

    memset(&hint, 0, sizeof(hint));
    hint.ai_flags = AI_CANONNAME;
    err = getaddrinfo(myname, 0, &hint, &ai);
    if (err) {
        fprintf(stderr,
                "Could not look up address for hostname '%s' - fatal\n",
                myname);
        exit(2);
    }

    if (!quiet) {
        addrstr = inet_ntop(ai->ai_family, ai->ai_addr, abuf, sizeof(abuf));
        if (addrstr != NULL)
            printf("Host address: %s\n", addrstr);
    }

    err = getnameinfo(ai->ai_addr, ai->ai_addrlen, namebuf, sizeof(namebuf),
                      NULL, 0, NI_NAMEREQD);
    if (err && !quiet)
        fprintf(stderr, "Error looking up IP address\n");

    printf("%s%s\n", quiet ? "" : "FQDN: ", err ? ai->ai_canonname : namebuf);

    if (!quiet)
        printf("Resolve library appears to have passed the test\n");

    freeaddrinfo(ai);
    return 0;
}