blob: 969d0b19b00a0c1e332433b660bfb3e15313b017 [file] [log] [blame]
John Beppub332e772000-01-29 12:59:01 +00001/*
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 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
44static const char nslookup_usage[] =
John Beppu50bc1012000-01-30 09:47:16 +000045 "nslookup [HOST]\n\n"
John Beppub332e772000-01-29 12:59:01 +000046;
47
48
John Beppu50bc1012000-01-30 09:47:16 +000049/* 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 Beppub332e772000-01-29 12:59:01 +000053static void
54server_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 */
61static int
62addr_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 Beppu50bc1012000-01-30 09:47:16 +000080/* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
81 * into a uint32_t
82 */
John Beppub332e772000-01-29 12:59:01 +000083static uint32_t
84str_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 Beppu50bc1012000-01-30 09:47:16 +0000101/* takes the NULL-terminated array h_addr_list, and
102 * prints its contents appropriately
103 */
John Beppub332e772000-01-29 12:59:01 +0000104static int
105addr_list_fprint(char **h_addr_list, FILE *dst)
106{
John Beppu50bc1012000-01-30 09:47:16 +0000107 int i, j;
John Beppub332e772000-01-29 12:59:01 +0000108 char *addr_string = (h_addr_list[1])
109 ? "Addresses"
110 : "Address";
111
112 fprintf(dst, "%s: ", addr_string);
John Beppu50bc1012000-01-30 09:47:16 +0000113 for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
John Beppub332e772000-01-29 12:59:01 +0000114 addr_fprint(h_addr_list[i], dst);
John Beppu50bc1012000-01-30 09:47:16 +0000115
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 Beppub332e772000-01-29 12:59:01 +0000126 }
John Beppu50bc1012000-01-30 09:47:16 +0000127
John Beppub332e772000-01-29 12:59:01 +0000128 }
129 fprintf(dst,"\n");
130 return 0;
131}
132
John Beppu50bc1012000-01-30 09:47:16 +0000133/* gethostbyaddr wrapper */
John Beppub332e772000-01-29 12:59:01 +0000134static struct hostent *
John Beppu50bc1012000-01-30 09:47:16 +0000135gethostbyaddr_wrapper(const char *address)
John Beppub332e772000-01-29 12:59:01 +0000136{
John Beppu50bc1012000-01-30 09:47:16 +0000137 struct in_addr addr;
John Beppub332e772000-01-29 12:59:01 +0000138
John Beppu50bc1012000-01-30 09:47:16 +0000139 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 */
144static struct hostent *
145hostent_fprint(struct hostent *host, FILE *dst)
146{
John Beppub332e772000-01-29 12:59:01 +0000147 if (host) {
John Beppu50bc1012000-01-30 09:47:16 +0000148 fprintf(dst, "Name: %s\n", host->h_name);
149 addr_list_fprint(host->h_addr_list, dst);
John Beppub332e772000-01-29 12:59:01 +0000150 } else {
John Beppu50bc1012000-01-30 09:47:16 +0000151 fprintf(dst, "*** %s\n", hstrerror(h_errno));
John Beppub332e772000-01-29 12:59:01 +0000152 }
153 return host;
154}
155
John Beppub332e772000-01-29 12:59:01 +0000156
John Beppu50bc1012000-01-30 09:47:16 +0000157/* naive function to check whether char *s is an ip address */
John Beppub332e772000-01-29 12:59:01 +0000158static int
159is_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/* ________________________________________________________________________ */
169int
170nslookup_main(int argc, char **argv)
171{
John Beppu50bc1012000-01-30 09:47:16 +0000172 struct hostent *host;
173
174 if (argc < 2) {
175 usage(nslookup_usage);
176 }
John Beppub332e772000-01-29 12:59:01 +0000177
178 server_fprint(stdout);
179 if (is_ip_address(argv[1])) {
John Beppu50bc1012000-01-30 09:47:16 +0000180 host = gethostbyaddr_wrapper(argv[1]);
John Beppub332e772000-01-29 12:59:01 +0000181 } else {
John Beppu50bc1012000-01-30 09:47:16 +0000182 host = gethostbyname(argv[1]);
John Beppub332e772000-01-29 12:59:01 +0000183 }
John Beppu50bc1012000-01-30 09:47:16 +0000184 hostent_fprint(host, stdout);
John Beppub332e772000-01-29 12:59:01 +0000185 return 0;
186}
187
Erik Andersenfac10d72000-02-07 05:29:42 +0000188/* $Id: nslookup.c,v 1.3 2000/02/07 05:29:42 erik Exp $ */