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