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