1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/resolve/resolve.c */
4 * Copyright 1995 by the Massachusetts Institute of Technology.
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.
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.
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.
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.
39 * When invoked with no arguments, gethostname is used for the local host.
43 /* This program tests the resolve library and sees if it is broken... */
57 #ifdef HAVE_SYS_PARAM_H
58 #include <sys/param.h>
61 #ifdef HAVE_SYS_SOCKET_H
62 #include <sys/socket.h>
73 #include <netinet/in.h>
81 char myname[MAXHOSTNAMELEN+1];
83 struct in_addr addrcopy;
89 if ((strcmp(*argv, "--quiet") == 0) ||
90 (strcmp(*argv, "-q") == 0)) {
98 strncpy(myname, *argv, MAXHOSTNAMELEN);
100 if(gethostname(myname, MAXHOSTNAMELEN)) {
101 perror("gethostname failure");
106 myname[MAXHOSTNAMELEN] = '\0'; /* for safety */
108 /* Look up the address... */
110 printf("Hostname: %s\n", myname);
113 /* Set the hosts db to close each time - effectively rewinding file */
116 if((host = gethostbyname (myname)) == NULL) {
118 "Could not look up address for hostname '%s' - fatal\n",
123 fqdn = strdup(host->h_name);
129 ptr = host->h_addr_list[0];
130 #define UC(a) (((int)a)&0xff)
132 printf("Host address: %d.%d.%d.%d\n",
133 UC(ptr[0]), UC(ptr[1]), UC(ptr[2]), UC(ptr[3]));
135 memcpy(&addrcopy.s_addr, ptr, 4);
137 /* Convert back to full name */
138 if ((host = gethostbyaddr(&addrcopy.s_addr, 4, AF_INET)) == NULL) {
140 fprintf(stderr, "Error looking up IP address\n");
143 fqdn = strdup(host->h_name);
151 printf("%s\n", fqdn);
153 printf("FQDN: %s\n", fqdn);
156 printf("Resolve library appears to have passed the test\n");