blob: 936fa33cb5b970eea25ec670059969dd76eff2b8 [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 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
John Beppub332e772000-01-29 12:59:01 +00007 *
Eric Andersen39cdf4e2004-01-30 22:40:05 +00008 * Correct default name server display and explicit name server option
Robert Griebl31a2e202002-07-24 00:56:56 +00009 * added by Ben Zeckel <bzeckel@hmc.edu> June 2001
10 *
John Beppub332e772000-01-29 12:59:01 +000011 * 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 Beppub332e772000-01-29 12:59:01 +000027#include <ctype.h>
28#include <errno.h>
29#include <stdio.h>
30#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000031#include <stdlib.h>
John Beppub332e772000-01-29 12:59:01 +000032
Eric Andersen39cdf4e2004-01-30 22:40:05 +000033#include <stdint.h>
John Beppub332e772000-01-29 12:59:01 +000034#include <netdb.h>
35#include <sys/socket.h>
36#include <sys/types.h>
37#include <netinet/in.h>
Eric Andersendab3d462001-06-12 22:21:24 +000038#include <resolv.h>
39#include <arpa/inet.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000040#include "busybox.h"
John Beppub332e772000-01-29 12:59:01 +000041
John Beppu50bc1012000-01-30 09:47:16 +000042/*
43 | I'm only implementing non-interactive mode;
44 | I totally forgot nslookup even had an interactive mode.
John Beppu50bc1012000-01-30 09:47:16 +000045 */
John Beppub332e772000-01-29 12:59:01 +000046
John Beppub332e772000-01-29 12:59:01 +000047/* only works for IPv4 */
Eric Andersen5f825ee2001-01-20 16:22:58 +000048static int addr_fprint(char *addr)
John Beppub332e772000-01-29 12:59:01 +000049{
Eric Andersen39cdf4e2004-01-30 22:40:05 +000050 uint8_t split[4];
51 uint32_t ip;
52 uint32_t *x = (uint32_t *) addr;
John Beppub332e772000-01-29 12:59:01 +000053
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 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 Andersen5f825ee2001-01-20 16:22:58 +000059 printf("%d.%d.%d.%d", split[0], split[1], split[2], split[3]);
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 return 0;
John Beppub332e772000-01-29 12:59:01 +000061}
62
John Beppu50bc1012000-01-30 09:47:16 +000063/* takes the NULL-terminated array h_addr_list, and
64 * prints its contents appropriately
65 */
Eric Andersen5f825ee2001-01-20 16:22:58 +000066static int addr_list_fprint(char **h_addr_list)
John Beppub332e772000-01-29 12:59:01 +000067{
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 int i, j;
69 char *addr_string = (h_addr_list[1])
Erik Andersen5e1189e2000-04-15 16:34:54 +000070 ? "Addresses: " : "Address: ";
John Beppub332e772000-01-29 12:59:01 +000071
Eric Andersen5f825ee2001-01-20 16:22:58 +000072 printf("%s ", addr_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
Eric Andersen5f825ee2001-01-20 16:22:58 +000074 addr_fprint(h_addr_list[i]);
John Beppu50bc1012000-01-30 09:47:16 +000075
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 /* real nslookup does this */
77 if (j == 4) {
78 if (h_addr_list[i + 1]) {
Eric Andersen5f825ee2001-01-20 16:22:58 +000079 printf("\n ");
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 }
81 j = 0;
82 } else {
83 if (h_addr_list[i + 1]) {
Eric Andersen5f825ee2001-01-20 16:22:58 +000084 printf(", ");
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 }
86 }
87
John Beppub332e772000-01-29 12:59:01 +000088 }
Eric Andersen5f825ee2001-01-20 16:22:58 +000089 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 return 0;
John Beppub332e772000-01-29 12:59:01 +000091}
92
John Beppu50bc1012000-01-30 09:47:16 +000093/* print the results as nslookup would */
Eric Andersencd8c4362001-11-10 11:22:46 +000094static struct hostent *hostent_fprint(struct hostent *host, const char *server_host)
John Beppu50bc1012000-01-30 09:47:16 +000095{
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 if (host) {
Eric Andersencd8c4362001-11-10 11:22:46 +000097 printf("%s %s\n", server_host, host->h_name);
Eric Andersen5f825ee2001-01-20 16:22:58 +000098 addr_list_fprint(host->h_addr_list);
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 } else {
Eric Andersen5f825ee2001-01-20 16:22:58 +0000100 printf("*** Unknown host\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 }
102 return host;
John Beppub332e772000-01-29 12:59:01 +0000103}
104
Eric Andersendab3d462001-06-12 22:21:24 +0000105/* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000106 * into a uint32_t
Eric Andersendab3d462001-06-12 22:21:24 +0000107 */
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000108static uint32_t str_to_addr(const char *addr)
Eric Andersendab3d462001-06-12 22:21:24 +0000109{
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000110 uint32_t split[4];
111 uint32_t ip;
Eric Andersendab3d462001-06-12 22:21:24 +0000112
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 */
124static 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 */
133static 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 Andersencd8c4362001-11-10 11:22:46 +0000138 hostent_fprint(gethostbyaddr_wrapper(ip), "Server:");
Eric Andersendab3d462001-06-12 22:21:24 +0000139 printf("\n");
140}
John Beppub332e772000-01-29 12:59:01 +0000141
Robert Griebl31a2e202002-07-24 00:56:56 +0000142/* alter the global _res nameserver structure to use
143 an explicit dns server instead of what is in /etc/resolv.h */
144static inline void set_default_dns(char *server)
145{
146 struct in_addr server_in_addr;
147
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000148 if(inet_aton(server,&server_in_addr))
Robert Griebl31a2e202002-07-24 00:56:56 +0000149 {
150 _res.nscount = 1;
151 _res.nsaddr_list[0].sin_addr = server_in_addr;
152 }
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000153}
Robert Griebl31a2e202002-07-24 00:56:56 +0000154
John Beppu50bc1012000-01-30 09:47:16 +0000155/* naive function to check whether char *s is an ip address */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156static int is_ip_address(const char *s)
John Beppub332e772000-01-29 12:59:01 +0000157{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 while (*s) {
159 if ((isdigit(*s)) || (*s == '.')) {
160 s++;
161 continue;
162 }
163 return 0;
164 }
165 return 1;
John Beppub332e772000-01-29 12:59:01 +0000166}
167
168/* ________________________________________________________________________ */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169int nslookup_main(int argc, char **argv)
John Beppub332e772000-01-29 12:59:01 +0000170{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 struct hostent *host;
John Beppu50bc1012000-01-30 09:47:16 +0000172
Robert Griebl31a2e202002-07-24 00:56:56 +0000173 /*
174 * initialize DNS structure _res used in printing the default
175 * name server and in the explicit name server option feature.
176 */
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000177
Eric Andersendab3d462001-06-12 22:21:24 +0000178 res_init();
Robert Griebl31a2e202002-07-24 00:56:56 +0000179
180 /*
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000181 * 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 Griebl31a2e202002-07-24 00:56:56 +0000184 * More than 3 arguments is an error to follow the pattern of the
185 * standard nslookup
186 */
187
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000188 if (argc < 2 || *argv[1]=='-' || argc > 3)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 bb_show_usage();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000190 else if(argc == 3)
Robert Griebl31a2e202002-07-24 00:56:56 +0000191 set_default_dns(argv[2]);
192
Eric Andersenfe9888a2001-01-20 21:51:21 +0000193 server_print();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 if (is_ip_address(argv[1])) {
195 host = gethostbyaddr_wrapper(argv[1]);
196 } else {
Matt Kraai524fcb92001-10-01 17:50:25 +0000197 host = xgethostbyname(argv[1]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 }
Eric Andersencd8c4362001-11-10 11:22:46 +0000199 hostent_fprint(host, "Name: ");
Eric Andersen9789bf12004-10-13 07:25:01 +0000200 if (host) {
201 return EXIT_SUCCESS;
202 }
203 return EXIT_FAILURE;
John Beppub332e772000-01-29 12:59:01 +0000204}
205
Eric Andersen9789bf12004-10-13 07:25:01 +0000206/* $Id: nslookup.c,v 1.33 2004/10/13 07:25:01 andersen Exp $ */