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 | * |
| 5 | * |
| 6 | * Copyright (C) 2000 by Lineo, inc. |
| 7 | * Written by John Beppu <beppu@lineo.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "internal.h" |
| 26 | #include <ctype.h> |
| 27 | #include <errno.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include <netdb.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <netinet/in.h> |
| 35 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 36 | /* |
| 37 | | I'm only implementing non-interactive mode; |
| 38 | | I totally forgot nslookup even had an interactive mode. |
| 39 | | |
| 40 | | [ TODO ] |
| 41 | | + find out how to use non-default name servers |
| 42 | | + find out how the real nslookup gets the default name server |
| 43 | */ |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 44 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | static const char nslookup_usage[] = "nslookup [HOST]\n\n"; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 46 | |
| 47 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 48 | /* I have to see how the real nslookup does this. |
| 49 | * I could dig through /etc/resolv.conf, but is there a |
| 50 | * better (programatic) way? |
| 51 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | static void server_fprint(FILE * dst) |
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 | fprintf(dst, "Server: %s\n", "something"); |
| 55 | fprintf(dst, "Address: %s\n\n", "something"); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* only works for IPv4 */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | static int addr_fprint(char *addr, FILE * dst) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 60 | { |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 61 | u_int8_t split[4]; |
| 62 | u_int32_t ip; |
| 63 | u_int32_t *x = (u_int32_t *) addr; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 64 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | ip = ntohl(*x); |
| 66 | split[0] = (ip & 0xff000000) >> 24; |
| 67 | split[1] = (ip & 0x00ff0000) >> 16; |
| 68 | split[2] = (ip & 0x0000ff00) >> 8; |
| 69 | split[3] = (ip & 0x000000ff); |
| 70 | fprintf(dst, "%d.%d.%d.%d", split[0], split[1], split[2], split[3] |
| 71 | ); |
| 72 | return 0; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 75 | /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+ |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 76 | * into a u_int32_t |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 77 | */ |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 78 | static u_int32_t str_to_addr(const char *addr) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 79 | { |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 80 | u_int32_t split[4]; |
| 81 | u_int32_t ip; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 82 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | sscanf(addr, "%d.%d.%d.%d", |
| 84 | &split[0], &split[1], &split[2], &split[3]); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 85 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | /* assuming sscanf worked */ |
| 87 | ip = (split[0] << 24) | |
| 88 | (split[1] << 16) | (split[2] << 8) | (split[3]); |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 89 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | return htonl(ip); |
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 | /* takes the NULL-terminated array h_addr_list, and |
| 94 | * prints its contents appropriately |
| 95 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | static int addr_list_fprint(char **h_addr_list, FILE * dst) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 97 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | int i, j; |
| 99 | char *addr_string = (h_addr_list[1]) |
| 100 | ? "Addresses" : "Address"; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 101 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | fprintf(dst, "%s: ", addr_string); |
| 103 | for (i = 0, j = 0; h_addr_list[i]; i++, j++) { |
| 104 | addr_fprint(h_addr_list[i], dst); |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 105 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | /* real nslookup does this */ |
| 107 | if (j == 4) { |
| 108 | if (h_addr_list[i + 1]) { |
| 109 | fprintf(dst, "\n "); |
| 110 | } |
| 111 | j = 0; |
| 112 | } else { |
| 113 | if (h_addr_list[i + 1]) { |
| 114 | fprintf(dst, ", "); |
| 115 | } |
| 116 | } |
| 117 | |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 118 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 119 | fprintf(dst, "\n"); |
| 120 | return 0; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 121 | } |
| 122 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 123 | /* gethostbyaddr wrapper */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | static struct hostent *gethostbyaddr_wrapper(const char *address) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 125 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | struct in_addr addr; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 127 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | addr.s_addr = str_to_addr(address); |
| 129 | return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */ |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* print the results as nslookup would */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | static struct hostent *hostent_fprint(struct hostent *host, FILE * dst) |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 134 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 135 | if (host) { |
| 136 | fprintf(dst, "Name: %s\n", host->h_name); |
| 137 | addr_list_fprint(host->h_addr_list, dst); |
| 138 | } else { |
| 139 | fprintf(dst, "*** %s\n", hstrerror(h_errno)); |
| 140 | } |
| 141 | return host; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 142 | } |
| 143 | |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 144 | |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 145 | /* naive function to check whether char *s is an ip address */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | static int is_ip_address(const char *s) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 147 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 148 | while (*s) { |
| 149 | if ((isdigit(*s)) || (*s == '.')) { |
| 150 | s++; |
| 151 | continue; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | return 1; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* ________________________________________________________________________ */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | int nslookup_main(int argc, char **argv) |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 160 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | struct hostent *host; |
John Beppu | 50bc101 | 2000-01-30 09:47:16 +0000 | [diff] [blame] | 162 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 163 | if (argc < 2) { |
| 164 | usage(nslookup_usage); |
| 165 | } |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 166 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | server_fprint(stdout); |
| 168 | if (is_ip_address(argv[1])) { |
| 169 | host = gethostbyaddr_wrapper(argv[1]); |
| 170 | } else { |
| 171 | host = gethostbyname(argv[1]); |
| 172 | } |
| 173 | hostent_fprint(host, stdout); |
| 174 | return 0; |
John Beppu | b332e77 | 2000-01-29 12:59:01 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 177 | /* $Id: nslookup.c,v 1.5 2000/02/18 21:34:17 erik Exp $ */ |