Imported Upstream version 1.10.2
[platform/upstream/krb5.git] / src / tests / resolve / resolve.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/resolve/resolve.c */
3 /*
4  * Copyright 1995 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26
27 /*
28  * A simple program to test the functionality of the resolver library.
29  * It simply will try to get the IP address of the host, and then look
30  * up the name from the address. If the resulting name does not contain the
31  * domain name, then the resolve library is broken.
32  *
33  * Warning: It is possible to fool this program into thinking everything is
34  * alright by a clever use of /etc/hosts - but this is better than nothing.
35  *
36  * Usage:
37  *   resolve [hostname]
38  *
39  *   When invoked with no arguments, gethostname is used for the local host.
40  *
41  */
42
43 /* This program tests the resolve library and sees if it is broken... */
44
45 #include "autoconf.h"
46 #include <stdio.h>
47
48 #if STDC_HEADERS
49 #include <string.h>
50 #else
51 #ifndef HAVE_STRCHR
52 #define strchr index
53 #endif
54 char *strchr();
55 #endif
56
57 #ifdef HAVE_SYS_PARAM_H
58 #include <sys/param.h>
59 #endif
60
61 #ifdef HAVE_SYS_SOCKET_H
62 #include <sys/socket.h>
63 #endif
64
65 #ifdef HAVE_STDLIB_H
66 #include <stdlib.h>
67 #endif
68
69 #ifdef HAVE_UNISTD_H
70 #include <unistd.h>
71 #endif
72
73 #include <netinet/in.h>
74 #include <netdb.h>
75
76 int
77 main(argc, argv)
78     int argc;
79     char **argv;
80 {
81     char myname[MAXHOSTNAMELEN+1];
82     char *ptr, *fqdn;
83     struct in_addr addrcopy;
84     struct hostent *host;
85     int quiet = 0;
86
87     argc--; argv++;
88     while (argc) {
89         if ((strcmp(*argv, "--quiet") == 0) ||
90             (strcmp(*argv, "-q") == 0)) {
91             quiet++;
92         } else
93             break;
94         argc--; argv++;
95     }
96
97     if (argc >= 1) {
98         strncpy(myname, *argv, MAXHOSTNAMELEN);
99     } else {
100         if(gethostname(myname, MAXHOSTNAMELEN)) {
101             perror("gethostname failure");
102             exit(1);
103         }
104     }
105
106     myname[MAXHOSTNAMELEN] = '\0';  /* for safety */
107
108     /* Look up the address... */
109     if (!quiet)
110         printf("Hostname:  %s\n", myname);
111
112
113     /* Set the hosts db to close each time - effectively rewinding file */
114     sethostent(0);
115
116     if((host = gethostbyname (myname)) == NULL) {
117         fprintf(stderr,
118                 "Could not look up address for hostname '%s' - fatal\n",
119                 myname);
120         exit(2);
121     }
122
123     fqdn = strdup(host->h_name);
124     if (fqdn == NULL) {
125         perror("strdup");
126         exit(2);
127     }
128
129     ptr = host->h_addr_list[0];
130 #define UC(a) (((int)a)&0xff)
131     if (!quiet)
132         printf("Host address: %d.%d.%d.%d\n",
133                UC(ptr[0]), UC(ptr[1]), UC(ptr[2]), UC(ptr[3]));
134
135     memcpy(&addrcopy.s_addr, ptr, 4);
136
137     /* Convert back to full name */
138     if ((host = gethostbyaddr(&addrcopy.s_addr, 4, AF_INET)) == NULL) {
139         if (!quiet)
140             fprintf(stderr, "Error looking up IP address\n");
141     } else {
142         free(fqdn);
143         fqdn = strdup(host->h_name);
144         if (fqdn == NULL) {
145             perror("strdup");
146             exit (2);
147         }
148     }
149
150     if (quiet)
151         printf("%s\n", fqdn);
152     else
153         printf("FQDN: %s\n", fqdn);
154
155     if (!quiet)
156         printf("Resolve library appears to have passed the test\n");
157
158     /* All ok */
159     exit(0);
160
161 }