| 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-1301 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 | |
| 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 | |
| 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 |
| 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 | exit(0); |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /*static char * load_default_groups[]= { "resolveip","client",0 }; */ |
| 88 | |
| 89 | static int get_options(int *argc,char ***argv) |
| 90 | { |
| 91 | int ho_error; |
| 92 | |
| 93 | if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option))) |
| 94 | exit(ho_error); |
| 95 | |
| 96 | if (*argc == 0) |
| 97 | { |
| 98 | usage(); |
| 99 | return 1; |
| 100 | } |
| 101 | return 0; |
| 102 | } /* get_options */ |
| 103 | |
| 104 | |
| 105 | |
| 106 | int main(int argc, char **argv) |
| 107 | { |
| 108 | struct hostent *hpaddr; |
| 109 | in_addr_t taddr; |
| 110 | char *ip,**q; |
| 111 | int error=0; |
| 112 | |
| 113 | MY_INIT(argv[0]); |
| 114 | |
| 115 | if (get_options(&argc,&argv)) |
| 116 | exit(1); |
| 117 | |
| 118 | while (argc--) |
| 119 | { |
| 120 | #ifndef WIN32 |
| 121 | struct in_addr addr; |
| 122 | #endif |
| 123 | ip = *argv++; |
| 124 | |
| 125 | /* Not compatible with IPv6! Probably should use getnameinfo(). */ |
| 126 | #ifdef WIN32 |
| 127 | taddr = inet_addr(ip); |
| 128 | if(taddr != INADDR_NONE) |
| 129 | { |
| 130 | #else |
| 131 | if (inet_aton(ip, &addr) != 0) |
| 132 | { |
| 133 | taddr= addr.s_addr; |
| 134 | #endif |
| 135 | if (taddr == htonl(INADDR_BROADCAST)) |
| 136 | { |
| 137 | puts("Broadcast" ); |
| 138 | continue; |
| 139 | } |
| 140 | if (taddr == htonl(INADDR_ANY)) |
| 141 | { |
| 142 | if (!taddr) |
| 143 | puts("Null-IP-Addr" ); |
| 144 | else |
| 145 | puts("Old-Bcast" ); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | hpaddr = gethostbyaddr((char*) &(taddr), sizeof(struct in_addr),AF_INET); |
| 150 | if (hpaddr) |
| 151 | { |
| 152 | if (silent) |
| 153 | puts(hpaddr->h_name); |
| 154 | else |
| 155 | { |
| 156 | printf ("Host name of %s is %s" , ip,hpaddr->h_name); |
| 157 | for (q = hpaddr->h_aliases; *q != 0; q++) |
| 158 | (void) printf(", %s" , *q); |
| 159 | puts("" ); |
| 160 | } |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | error=2; |
| 165 | fprintf(stderr,"%s: Unable to find hostname for '%s'\n" ,my_progname, |
| 166 | ip); |
| 167 | } |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | hpaddr = gethostbyname(ip); |
| 172 | if (!hpaddr) |
| 173 | { |
| 174 | const char *err; |
| 175 | fprintf(stderr,"%s: Unable to find hostid for '%s'" ,my_progname,ip); |
| 176 | switch (h_errno) { |
| 177 | case HOST_NOT_FOUND: err="host not found" ; break; |
| 178 | case TRY_AGAIN: err="try again" ; break; |
| 179 | case NO_RECOVERY: err="no recovery" ; break; |
| 180 | case NO_DATA: err="no_data" ; break; |
| 181 | default: err=0; |
| 182 | } |
| 183 | if (err) |
| 184 | fprintf(stderr,": %s\n" ,err); |
| 185 | else |
| 186 | fprintf(stderr,"\n" ); |
| 187 | error=2; |
| 188 | } |
| 189 | else if (silent) |
| 190 | { |
| 191 | struct in_addr in; |
| 192 | memcpy((char*) &in.s_addr, (char*) *hpaddr->h_addr_list, |
| 193 | sizeof (in.s_addr)); |
| 194 | puts(inet_ntoa(in)); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | char **p; |
| 199 | for (p = hpaddr->h_addr_list; *p != 0; p++) |
| 200 | { |
| 201 | struct in_addr in; |
| 202 | memcpy(&in.s_addr, *p, sizeof (in.s_addr)); |
| 203 | printf ("IP address of %s is %s\n" ,ip,inet_ntoa(in)); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | exit(error); |
| 209 | } |
| 210 | |