MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
resolveip.c
1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /* Resolves IP's to hostname and hostnames to IP's */
17 
18 #define RESOLVE_VERSION "2.3"
19 
20 #include <my_global.h>
21 #include <m_ctype.h>
22 #include <my_sys.h>
23 #include <m_string.h>
24 #ifndef WIN32
25 # include <sys/types.h>
26 # include <sys/socket.h>
27 # ifndef HAVE_BROKEN_NETINET_INCLUDES
28 # include <netinet/in.h>
29 # endif
30 # include <arpa/inet.h>
31 # include <netdb.h>
32 #endif
33 #include <my_net.h>
34 #include <my_getopt.h>
35 
36 #if !defined(_AIX) && !defined(h_errno)
37 extern int h_errno;
38 #endif
39 
40 static my_bool silent;
41 
42 static struct my_option my_long_options[] =
43 {
44  {"help", '?', "Displays this help and exits.",
45  0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
46  {"info", 'I', "Synonym for --help.",
47  0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
48  {"silent", 's', "Be more silent.", &silent, &silent,
49  0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
50  {"version", 'V', "Displays version information and exits.",
51  0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
52  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
53 };
54 
55 
56 static void print_version(void)
57 {
58  printf("%s Ver %s, for %s (%s)\n",my_progname,RESOLVE_VERSION,
59  SYSTEM_TYPE,MACHINE_TYPE);
60 }
61 
62 
63 static void usage(void)
64 {
65  print_version();
66  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
67  puts("Get hostname based on IP-address or IP-address based on hostname.\n");
68  printf("Usage: %s [OPTIONS] hostname or IP-address\n",my_progname);
69  my_print_help(my_long_options);
70  my_print_variables(my_long_options);
71 }
72 
73 
74 static my_bool
75 get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
76  char *argument __attribute__((unused)))
77 {
78  switch (optid) {
79  case 'V': print_version(); exit(0);
80  case 'I':
81  case '?':
82  usage();
83  exit(0);
84  }
85  return 0;
86 }
87 
88 /*static char * load_default_groups[]= { "resolveip","client",0 }; */
89 
90 static int get_options(int *argc,char ***argv)
91 {
92  int ho_error;
93 
94  if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
95  exit(ho_error);
96 
97  if (*argc == 0)
98  {
99  usage();
100  return 1;
101  }
102  return 0;
103 } /* get_options */
104 
105 
106 
107 int main(int argc, char **argv)
108 {
109  struct hostent *hpaddr;
110  in_addr_t taddr;
111  char *ip,**q;
112  int error=0;
113 
114  MY_INIT(argv[0]);
115 
116  if (get_options(&argc,&argv))
117  exit(1);
118 
119  while (argc--)
120  {
121 #ifndef WIN32
122  struct in_addr addr;
123 #endif
124  ip = *argv++;
125 
126  /* Not compatible with IPv6! Probably should use getnameinfo(). */
127 #ifdef WIN32
128  taddr = inet_addr(ip);
129  if(taddr != INADDR_NONE)
130  {
131 #else
132  if (inet_aton(ip, &addr) != 0)
133  {
134  taddr= addr.s_addr;
135 #endif
136  if (taddr == htonl(INADDR_BROADCAST))
137  {
138  puts("Broadcast");
139  continue;
140  }
141  if (taddr == htonl(INADDR_ANY))
142  {
143  if (!taddr)
144  puts("Null-IP-Addr");
145  else
146  puts("Old-Bcast");
147  continue;
148  }
149 
150  hpaddr = gethostbyaddr((char*) &(taddr), sizeof(struct in_addr),AF_INET);
151  if (hpaddr)
152  {
153  if (silent)
154  puts(hpaddr->h_name);
155  else
156  {
157  printf ("Host name of %s is %s", ip,hpaddr->h_name);
158  for (q = hpaddr->h_aliases; *q != 0; q++)
159  (void) printf(", %s", *q);
160  puts("");
161  }
162  }
163  else
164  {
165  error=2;
166  fprintf(stderr,"%s: Unable to find hostname for '%s'\n",my_progname,
167  ip);
168  }
169  }
170  else
171  {
172  hpaddr = gethostbyname(ip);
173  if (!hpaddr)
174  {
175  const char *err;
176  fprintf(stderr,"%s: Unable to find hostid for '%s'",my_progname,ip);
177  switch (h_errno) {
178  case HOST_NOT_FOUND: err="host not found"; break;
179  case TRY_AGAIN: err="try again"; break;
180  case NO_RECOVERY: err="no recovery"; break;
181  case NO_DATA: err="no_data"; break;
182  default: err=0;
183  }
184  if (err)
185  fprintf(stderr,": %s\n",err);
186  else
187  fprintf(stderr,"\n");
188  error=2;
189  }
190  else if (silent)
191  {
192  struct in_addr in;
193  memcpy((char*) &in.s_addr, (char*) *hpaddr->h_addr_list,
194  sizeof (in.s_addr));
195  puts(inet_ntoa(in));
196  }
197  else
198  {
199  char **p;
200  for (p = hpaddr->h_addr_list; *p != 0; p++)
201  {
202  struct in_addr in;
203  memcpy(&in.s_addr, *p, sizeof (in.s_addr));
204  printf ("IP address of %s is %s\n",ip,inet_ntoa(in));
205  }
206  }
207  }
208  }
209  exit(error);
210 }