source: trunk/athena/bin/hostinfo/hostinfo.c @ 12494

Revision 12494, 6.2 KB checked in by ghudson, 26 years ago (diff)
Autoconfiscate.
Line 
1/* This program looks up a given hostname on the nameserver and prints
2 * its Internet address, official name, etc.
3 *
4 * Win Treese
5 * MIT Project Athena
6 *
7 * Copyright (c) 1986 by the Massachusetts Institute of Technology.
8 *
9 * Created:     08/29/86
10 *
11 * Modified:    02/03/87        Doug Alan
12 *              Let input also be in form of an internet address.
13 *
14 * Modified:    02/04/87        Doug Alan
15 *              Take flags to control output.
16 *
17 * Modified:    08/20/90        Tom Coppeto
18 *              Added HINFO and MX queries.
19 *              List all returned addresses
20 *              Allow for multiple hostnames.
21 *                     
22 */
23
24#ifndef lint
25static char rcsid[] = "$Id: hostinfo.c,v 1.14 1999-02-15 15:27:12 ghudson Exp $";
26#endif
27
28#include <stdio.h>                      /* Standard IO */
29#include <sys/types.h>                  /* System type defs. */
30#include <netdb.h>                      /* Networking defs. */
31#include <sys/socket.h>                 /* More network defs. */
32#include <string.h>                     /* String fct. declarations. */
33#include <ctype.h>                      /* Character type macros. */
34#include <netinet/in.h>                 /* Internet defs. */
35#include <arpa/inet.h>                  /* For inet_addr */
36
37typedef int bool;
38
39#define TRUE            1
40#define FALSE           0
41#define LINE_LENGTH     128
42#define ERROR           -1
43#define CPNULL          ((char *) NULL)
44
45extern char *gethinfobyname(), *getmxbyname();
46extern struct hostent *hostinfo_gethostbyname(), *hostinfo_gethostbyaddr();
47
48static char *usage[] = {
49  "Usage: %s <options> <host-names-or-addresses>",
50  "   -h: output only hostname",
51  "   -a: output only internet address",
52  "   -i: output only host info record",
53  "   -m: output only mx record",
54  "   -q: disable additional query for hinfo & mx",
55  "   -ns <server>: ask specified name server",
56  CPNULL,
57};
58
59char *myname;                           /* How program was invoked. */
60char *hostname = NULL;                  /* desired host */
61
62struct in_addr server_addr;
63int server_specified;
64
65bool h_flag = FALSE;
66bool a_flag = FALSE;
67bool n_flag = FALSE;
68bool m_flag = FALSE;
69bool i_flag = FALSE;
70bool q_flag = FALSE;
71
72static void usage_error();
73static void clear_flags();
74
75main(argc, argv)
76     int argc;
77     char **argv;
78     
79{
80  extern int h_errno;
81  struct hostent *host_entry = (struct hostent *)0;     
82  unsigned long host_address;
83  char *server = NULL;
84  char *hinfo = NULL;
85  char *mx = NULL;
86  int status = 0;
87  int set_server = 0;
88
89  myname = (myname = strrchr(argv[0], '/')) ? myname + 1 : argv[0];
90  if(argc == 1)
91    usage_error();
92  if(strcmp(argv[1], "-help") == 0)
93    usage_error();
94
95  while(*++argv)
96    {
97      if(hostname && !(a_flag || m_flag || i_flag || h_flag))
98        putchar('\n');
99
100      set_server = 0;
101      if(**argv == '-')
102        switch((*argv)[1])
103          {
104          case 'h':
105            clear_flags();
106            h_flag = TRUE;
107            continue;
108          case 'a':
109            clear_flags();
110            a_flag = TRUE;
111            continue;
112          case 'm':
113            clear_flags();
114            m_flag = TRUE;
115            continue;
116          case 'i':
117            clear_flags();
118            i_flag = TRUE;
119            continue;
120          case 'q':
121            clear_flags();
122            q_flag = TRUE;
123            continue;
124          case 'n':
125            if(!*++argv)
126              usage_error();
127            server = *argv;
128            set_server = 1;
129            break;
130          case '?':
131          default:
132            usage_error();
133            break;
134          }
135     
136      hinfo = NULL;
137      mx = NULL;
138      host_address = 0;
139      host_entry = NULL;
140      hostname = *argv;
141     
142      if (*hostname >= '0' && *hostname <= '9')
143        {
144          host_address = inet_addr(hostname);
145          if (host_address)
146            {
147              host_entry = hostinfo_gethostbyaddr((char *) &host_address, 4,
148                                                  AF_INET);
149              if(host_entry)
150                if(set_server)           
151                  memcpy(&server_addr, host_entry->h_addr,
152                         sizeof(server_addr));
153                else             
154                  print_host(host_entry);
155            }
156        }
157
158      if (!host_entry)
159        {
160          host_entry = hostinfo_gethostbyname(hostname);
161          if(host_entry)
162            {
163              if(set_server)
164                memcpy(&server_addr, host_entry->h_addr, sizeof(server_addr));
165              else
166                {
167                  print_host(host_entry);
168                  if(!q_flag)
169                    {
170                      hinfo = (char *) gethinfobyname(hostname);
171                      if(hinfo)
172                        {
173                          if(!h_flag && !a_flag && !m_flag)
174                            {
175                              int i = (int) hinfo[0];
176                              hinfo[i+1] = '/';
177                              if(i_flag)
178                                printf("%s\n", hinfo+1);
179                              else
180                                printf("Host info:\t%s\n", hinfo+1);
181                            }
182                        }
183                      if(!h_flag && !a_flag && !i_flag)
184                        if(mx = (char *) getmxbyname(hostname))
185                          if(m_flag)
186                            printf("%s\n", mx);
187                          else
188                            printf("MX address:\t%s\n", mx);             
189                    }
190                }
191            }
192        }
193 
194      if(host_entry && set_server)
195        {
196          struct in_addr internet_address;
197
198          server_specified = 1;
199          memcpy(&internet_address, host_entry->h_addr_list[0],
200                host_entry->h_length);
201          printf("Using domain server:\nHost name:\t%s\nHost Address\t%s\n",
202                 host_entry->h_name, inet_ntoa(internet_address));
203          continue;
204        }
205
206      if (!host_entry)
207        {
208          switch (h_errno)
209            {
210            case HOST_NOT_FOUND:
211              printf("No such host '%s'.\n",hostname);
212              status = ERROR;
213              continue;
214              break;
215            default:
216            case TRY_AGAIN:
217            case NO_RECOVERY:
218              printf("Cannot resolve name '%s' due to network difficulties.\n",
219                     hostname);
220              status = ERROR;
221              continue;
222              break;
223            case NO_ADDRESS:
224            /* should look up MX record? */
225              /* this error return appears if there is some entry for the
226                 requested name, but no A record (e.g. only an NS record) */
227              printf("No address for '%s'.\n", hostname);
228              status = ERROR;
229              continue;
230              break;
231            }
232        }
233    }
234 
235  exit(status);
236} /* main */
237
238
239print_host(h)
240     struct hostent *h;
241{
242  struct in_addr internet_address;
243  char **alias;                           
244  char **addr;
245
246  if(m_flag || i_flag)
247    return;
248
249  if (h_flag)
250    printf("%s\n", h->h_name);
251  else 
252    {
253      if(!a_flag)
254        {
255          printf("Desired host:\t%s\n", hostname);
256          printf("Official name:\t%s\n", h->h_name);
257          for (alias = h->h_aliases; *alias; alias++)
258            printf("Alias:\t\t%s\n", *alias);
259        }
260         
261      for (addr = h->h_addr_list; *addr; addr++)
262        {
263          memcpy(&internet_address, *addr, h->h_length);
264          if(a_flag)
265            printf("%s\n", inet_ntoa(internet_address));
266          else
267            printf("Host address:\t%s\n", inet_ntoa(internet_address));
268        }
269    }
270}
271
272
273static void
274usage_error()
275{
276  int line = 0;                         /* current line */
277  while (usage[line] != CPNULL)
278    {
279      fprintf (stderr, usage[line++], myname);
280      putc('\n', stderr);
281    }
282  exit(ERROR);
283} /* usage_error */
284
285
286static void
287clear_flags()
288{
289  i_flag = m_flag = h_flag = a_flag = 0;
290}
Note: See TracBrowser for help on using the repository browser.