Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini nslookup implementation for busybox |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu |
| 6 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 8 | * Correct default name server display and explicit name server option |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 9 | * added by Ben Zeckel <bzeckel@hmc.edu> June 2001 |
| 10 | * |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | */ |
| 26 | |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | #include <errno.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 32 | |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 33 | #include <stdint.h> |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 34 | #include <netdb.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <netinet/in.h> |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 38 | #include <resolv.h> |
| 39 | #include <arpa/inet.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 40 | #include "busybox.h" |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 41 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 42 | /* |
| 43 | | I'm only implementing non-interactive mode; |
| 44 | | I totally forgot nslookup even had an interactive mode. |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 45 | */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 46 | |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 47 | /* only works for IPv4 */ |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 48 | static int addr_fprint(char *addr) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 49 | { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 50 | uint8_t split[4]; |
| 51 | uint32_t ip; |
| 52 | uint32_t *x = (uint32_t *) addr; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 53 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | ip = ntohl(*x); |
| 55 | split[0] = (ip & 0xff000000) >> 24; |
| 56 | split[1] = (ip & 0x00ff0000) >> 16; |
| 57 | split[2] = (ip & 0x0000ff00) >> 8; |
| 58 | split[3] = (ip & 0x000000ff); |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 59 | printf("%d.%d.%d.%d", split[0], split[1], split[2], split[3]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | return 0; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 61 | } |
| 62 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 63 | /* takes the NULL-terminated array h_addr_list, and |
| 64 | * prints its contents appropriately |
| 65 | */ |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 66 | static int addr_list_fprint(char **h_addr_list) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 67 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | int i, j; |
| 69 | char *addr_string = (h_addr_list[1]) |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 70 | ? "Addresses: " : "Address: "; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 71 | |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 72 | printf("%s ", addr_string); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | for (i = 0, j = 0; h_addr_list[i]; i++, j++) { |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 74 | addr_fprint(h_addr_list[i]); |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 75 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | /* real nslookup does this */ |
| 77 | if (j == 4) { |
| 78 | if (h_addr_list[i + 1]) { |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 79 | printf("\n "); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | } |
| 81 | j = 0; |
| 82 | } else { |
| 83 | if (h_addr_list[i + 1]) { |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 84 | printf(", "); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 88 | } |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 89 | printf("\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | return 0; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 91 | } |
| 92 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 93 | /* print the results as nslookup would */ |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 94 | static struct hostent *hostent_fprint(struct hostent *host, const char *server_host) |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 95 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | if (host) { |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 97 | printf("%s %s\n", server_host, host->h_name); |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 98 | addr_list_fprint(host->h_addr_list); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 99 | } else { |
Eric Andersen | 5f825ee | 2001-01-20 16:22:58 +0000 | [diff] [blame] | 100 | printf("*** Unknown host\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | } |
| 102 | return host; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 105 | /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+ |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 106 | * into a uint32_t |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 107 | */ |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 108 | static uint32_t str_to_addr(const char *addr) |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 109 | { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 110 | uint32_t split[4]; |
| 111 | uint32_t ip; |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 112 | |
| 113 | sscanf(addr, "%d.%d.%d.%d", |
| 114 | &split[0], &split[1], &split[2], &split[3]); |
| 115 | |
| 116 | /* assuming sscanf worked */ |
| 117 | ip = (split[0] << 24) | |
| 118 | (split[1] << 16) | (split[2] << 8) | (split[3]); |
| 119 | |
| 120 | return htonl(ip); |
| 121 | } |
| 122 | |
| 123 | /* gethostbyaddr wrapper */ |
| 124 | static struct hostent *gethostbyaddr_wrapper(const char *address) |
| 125 | { |
| 126 | struct in_addr addr; |
| 127 | |
| 128 | addr.s_addr = str_to_addr(address); |
| 129 | return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */ |
| 130 | } |
| 131 | |
| 132 | /* lookup the default nameserver and display it */ |
| 133 | static inline void server_print(void) |
| 134 | { |
| 135 | struct sockaddr_in def = _res.nsaddr_list[0]; |
| 136 | char *ip = inet_ntoa(def.sin_addr); |
| 137 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 138 | hostent_fprint(gethostbyaddr_wrapper(ip), "Server:"); |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 139 | printf("\n"); |
| 140 | } |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 141 | |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 142 | /* alter the global _res nameserver structure to use |
| 143 | an explicit dns server instead of what is in /etc/resolv.h */ |
| 144 | static inline void set_default_dns(char *server) |
| 145 | { |
| 146 | struct in_addr server_in_addr; |
| 147 | |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 148 | if(inet_aton(server,&server_in_addr)) |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 149 | { |
| 150 | _res.nscount = 1; |
| 151 | _res.nsaddr_list[0].sin_addr = server_in_addr; |
| 152 | } |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 153 | } |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 154 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 155 | /* naive function to check whether char *s is an ip address */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | static int is_ip_address(const char *s) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 157 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | while (*s) { |
| 159 | if ((isdigit(*s)) || (*s == '.')) { |
| 160 | s++; |
| 161 | continue; |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | return 1; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* ________________________________________________________________________ */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | int nslookup_main(int argc, char **argv) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 170 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | struct hostent *host; |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 172 | |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 173 | /* |
| 174 | * initialize DNS structure _res used in printing the default |
| 175 | * name server and in the explicit name server option feature. |
| 176 | */ |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 177 | |
Eric Andersen | dab3d46 | 2001-06-12 22:21:24 +0000 | [diff] [blame] | 178 | res_init(); |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 181 | * We allow 1 or 2 arguments. |
| 182 | * The first is the name to be looked up and the second is an |
| 183 | * optional DNS server with which to do the lookup. |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 184 | * More than 3 arguments is an error to follow the pattern of the |
| 185 | * standard nslookup |
| 186 | */ |
| 187 | |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 188 | if (argc < 2 || *argv[1]=='-' || argc > 3) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 189 | bb_show_usage(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 190 | else if(argc == 3) |
Robert Griebl | 31a2e20 | 2002-07-24 00:56:56 +0000 | [diff] [blame] | 191 | set_default_dns(argv[2]); |
| 192 | |
Eric Andersen | fe9888a | 2001-01-20 21:51:21 +0000 | [diff] [blame] | 193 | server_print(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 194 | if (is_ip_address(argv[1])) { |
| 195 | host = gethostbyaddr_wrapper(argv[1]); |
| 196 | } else { |
Matt Kraai | 524fcb9 | 2001-10-01 17:50:25 +0000 | [diff] [blame] | 197 | host = xgethostbyname(argv[1]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | } |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 199 | hostent_fprint(host, "Name: "); |
Eric Andersen | 9789bf1 | 2004-10-13 07:25:01 +0000 | [diff] [blame^] | 200 | if (host) { |
| 201 | return EXIT_SUCCESS; |
| 202 | } |
| 203 | return EXIT_FAILURE; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Eric Andersen | 9789bf1 | 2004-10-13 07:25:01 +0000 | [diff] [blame^] | 206 | /* $Id: nslookup.c,v 1.33 2004/10/13 07:25:01 andersen Exp $ */ |