blob: 82bcf56a16e8bdf727234942dd1b954cbb76b894 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppub332e772000-01-29 12:59:01 +00002/*
3 * Mini nslookup implementation for busybox
4 *
John Beppub332e772000-01-29 12:59:01 +00005 * 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 Beppu50bc1012000-01-30 09:47:16 +000035/*
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 Beppub332e772000-01-29 12:59:01 +000043
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000044static const char nslookup_usage[] = "nslookup [HOST]\n"
45#ifndef BB_FEATURE_TRIVIAL_HELP
46 "\nQueries the nameserver for the IP address of the given HOST\n"
47#endif
48;
John Beppub332e772000-01-29 12:59:01 +000049
50
John Beppu50bc1012000-01-30 09:47:16 +000051/* I have to see how the real nslookup does this.
52 * I could dig through /etc/resolv.conf, but is there a
53 * better (programatic) way?
54 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000055static void server_fprint(FILE * dst)
John Beppub332e772000-01-29 12:59:01 +000056{
Erik Andersen5e1189e2000-04-15 16:34:54 +000057 fprintf(dst, "Server: %s\n", "default");
58 fprintf(dst, "Address: %s\n\n", "default");
John Beppub332e772000-01-29 12:59:01 +000059}
60
61/* only works for IPv4 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000062static int addr_fprint(char *addr, FILE * dst)
John Beppub332e772000-01-29 12:59:01 +000063{
Erik Andersene2729152000-02-18 21:34:17 +000064 u_int8_t split[4];
65 u_int32_t ip;
66 u_int32_t *x = (u_int32_t *) addr;
John Beppub332e772000-01-29 12:59:01 +000067
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 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(dst, "%d.%d.%d.%d", split[0], split[1], split[2], split[3]
74 );
75 return 0;
John Beppub332e772000-01-29 12:59:01 +000076}
77
John Beppu50bc1012000-01-30 09:47:16 +000078/* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
Erik Andersene2729152000-02-18 21:34:17 +000079 * into a u_int32_t
John Beppu50bc1012000-01-30 09:47:16 +000080 */
Erik Andersene2729152000-02-18 21:34:17 +000081static u_int32_t str_to_addr(const char *addr)
John Beppub332e772000-01-29 12:59:01 +000082{
Erik Andersene2729152000-02-18 21:34:17 +000083 u_int32_t split[4];
84 u_int32_t ip;
John Beppub332e772000-01-29 12:59:01 +000085
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 sscanf(addr, "%d.%d.%d.%d",
87 &split[0], &split[1], &split[2], &split[3]);
John Beppub332e772000-01-29 12:59:01 +000088
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 /* assuming sscanf worked */
90 ip = (split[0] << 24) |
91 (split[1] << 16) | (split[2] << 8) | (split[3]);
John Beppub332e772000-01-29 12:59:01 +000092
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 return htonl(ip);
John Beppub332e772000-01-29 12:59:01 +000094}
95
John Beppu50bc1012000-01-30 09:47:16 +000096/* takes the NULL-terminated array h_addr_list, and
97 * prints its contents appropriately
98 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000099static int addr_list_fprint(char **h_addr_list, FILE * dst)
John Beppub332e772000-01-29 12:59:01 +0000100{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 int i, j;
102 char *addr_string = (h_addr_list[1])
Erik Andersen5e1189e2000-04-15 16:34:54 +0000103 ? "Addresses: " : "Address: ";
John Beppub332e772000-01-29 12:59:01 +0000104
Erik Andersen5e1189e2000-04-15 16:34:54 +0000105 fprintf(dst, "%s ", addr_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
107 addr_fprint(h_addr_list[i], dst);
John Beppu50bc1012000-01-30 09:47:16 +0000108
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 /* real nslookup does this */
110 if (j == 4) {
111 if (h_addr_list[i + 1]) {
112 fprintf(dst, "\n ");
113 }
114 j = 0;
115 } else {
116 if (h_addr_list[i + 1]) {
117 fprintf(dst, ", ");
118 }
119 }
120
John Beppub332e772000-01-29 12:59:01 +0000121 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 fprintf(dst, "\n");
123 return 0;
John Beppub332e772000-01-29 12:59:01 +0000124}
125
John Beppu50bc1012000-01-30 09:47:16 +0000126/* gethostbyaddr wrapper */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127static struct hostent *gethostbyaddr_wrapper(const char *address)
John Beppub332e772000-01-29 12:59:01 +0000128{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 struct in_addr addr;
John Beppub332e772000-01-29 12:59:01 +0000130
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 addr.s_addr = str_to_addr(address);
132 return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */
John Beppu50bc1012000-01-30 09:47:16 +0000133}
134
135/* print the results as nslookup would */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136static struct hostent *hostent_fprint(struct hostent *host, FILE * dst)
John Beppu50bc1012000-01-30 09:47:16 +0000137{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 if (host) {
Erik Andersen5e1189e2000-04-15 16:34:54 +0000139 fprintf(dst, "Name: %s\n", host->h_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 addr_list_fprint(host->h_addr_list, dst);
141 } else {
Erik Andersen5afc8642000-05-02 00:07:56 +0000142 fprintf(dst, "*** Unknown host\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 }
144 return host;
John Beppub332e772000-01-29 12:59:01 +0000145}
146
John Beppub332e772000-01-29 12:59:01 +0000147
John Beppu50bc1012000-01-30 09:47:16 +0000148/* naive function to check whether char *s is an ip address */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149static int is_ip_address(const char *s)
John Beppub332e772000-01-29 12:59:01 +0000150{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 while (*s) {
152 if ((isdigit(*s)) || (*s == '.')) {
153 s++;
154 continue;
155 }
156 return 0;
157 }
158 return 1;
John Beppub332e772000-01-29 12:59:01 +0000159}
160
161/* ________________________________________________________________________ */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162int nslookup_main(int argc, char **argv)
John Beppub332e772000-01-29 12:59:01 +0000163{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000164 struct hostent *host;
John Beppu50bc1012000-01-30 09:47:16 +0000165
Erik Andersen5e1189e2000-04-15 16:34:54 +0000166 if (argc < 2 || *argv[1]=='-') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 usage(nslookup_usage);
168 }
John Beppub332e772000-01-29 12:59:01 +0000169
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 server_fprint(stdout);
171 if (is_ip_address(argv[1])) {
172 host = gethostbyaddr_wrapper(argv[1]);
173 } else {
174 host = gethostbyname(argv[1]);
175 }
176 hostent_fprint(host, stdout);
Erik Andersen5e1189e2000-04-15 16:34:54 +0000177 exit( TRUE);
John Beppub332e772000-01-29 12:59:01 +0000178}
179
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000180/* $Id: nslookup.c,v 1.9 2000/05/12 19:41:47 erik Exp $ */