1 /* vi: set sw=4 ts=4: */
3 * Mini nslookup implementation for busybox
5 * Copyright (C) 2000 by Lineo, inc.
6 * Written by John Beppu <beppu@lineo.com>
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.
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.
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
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
36 | I'm only implementing non-interactive mode;
37 | I totally forgot nslookup even had an interactive mode.
40 | + find out how to use non-default name servers
41 | + find out how the real nslookup gets the default name server
44 static 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"
51 /* 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?
55 static void server_fprint(FILE * dst)
57 fprintf(dst, "Server: %s\n", "default");
58 fprintf(dst, "Address: %s\n\n", "default");
61 /* only works for IPv4 */
62 static int addr_fprint(char *addr, FILE * dst)
66 u_int32_t *x = (u_int32_t *) addr;
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]
78 /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
81 static u_int32_t str_to_addr(const char *addr)
86 sscanf(addr, "%d.%d.%d.%d",
87 &split[0], &split[1], &split[2], &split[3]);
89 /* assuming sscanf worked */
90 ip = (split[0] << 24) |
91 (split[1] << 16) | (split[2] << 8) | (split[3]);
96 /* takes the NULL-terminated array h_addr_list, and
97 * prints its contents appropriately
99 static int addr_list_fprint(char **h_addr_list, FILE * dst)
102 char *addr_string = (h_addr_list[1])
103 ? "Addresses: " : "Address: ";
105 fprintf(dst, "%s ", addr_string);
106 for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
107 addr_fprint(h_addr_list[i], dst);
109 /* real nslookup does this */
111 if (h_addr_list[i + 1]) {
116 if (h_addr_list[i + 1]) {
126 /* gethostbyaddr wrapper */
127 static struct hostent *gethostbyaddr_wrapper(const char *address)
131 addr.s_addr = str_to_addr(address);
132 return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */
135 /* print the results as nslookup would */
136 static struct hostent *hostent_fprint(struct hostent *host, FILE * dst)
139 fprintf(dst, "Name: %s\n", host->h_name);
140 addr_list_fprint(host->h_addr_list, dst);
142 fprintf(dst, "*** Unknown host\n");
148 /* naive function to check whether char *s is an ip address */
149 static int is_ip_address(const char *s)
152 if ((isdigit(*s)) || (*s == '.')) {
161 /* ________________________________________________________________________ */
162 int nslookup_main(int argc, char **argv)
164 struct hostent *host;
166 if (argc < 2 || *argv[1]=='-') {
167 usage(nslookup_usage);
170 server_fprint(stdout);
171 if (is_ip_address(argv[1])) {
172 host = gethostbyaddr_wrapper(argv[1]);
174 host = gethostbyname(argv[1]);
176 hostent_fprint(host, stdout);
180 /* $Id: nslookup.c,v 1.9 2000/05/12 19:41:47 erik Exp $ */