2 * 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[] =
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?
54 server_fprint(FILE *dst)
56 fprintf(dst, "Server: %s\n", "something");
57 fprintf(dst, "Address: %s\n\n", "something");
60 /* only works for IPv4 */
62 addr_fprint(char *addr, FILE *dst)
66 uint32_t *x = (uint32_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);
75 split[0], split[1], split[2], split[3]
80 /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
84 str_to_addr(const char *addr)
89 sscanf(addr, "%d.%d.%d.%d",
90 &split[0], &split[1], &split[2], &split[3]);
92 /* assuming sscanf worked */
93 ip = (split[0] << 24) |
101 /* takes the NULL-terminated array h_addr_list, and
102 * prints its contents appropriately
105 addr_list_fprint(char **h_addr_list, FILE *dst)
108 char *addr_string = (h_addr_list[1])
112 fprintf(dst, "%s: ", addr_string);
113 for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
114 addr_fprint(h_addr_list[i], dst);
116 /* real nslookup does this */
118 if (h_addr_list[i+1]) {
123 if (h_addr_list[i+1]) {
133 /* gethostbyaddr wrapper */
134 static struct hostent *
135 gethostbyaddr_wrapper(const char *address)
139 addr.s_addr = str_to_addr(address);
140 return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */
143 /* print the results as nslookup would */
144 static struct hostent *
145 hostent_fprint(struct hostent *host, FILE *dst)
148 fprintf(dst, "Name: %s\n", host->h_name);
149 addr_list_fprint(host->h_addr_list, dst);
151 fprintf(dst, "*** %s\n", hstrerror(h_errno));
157 /* naive function to check whether char *s is an ip address */
159 is_ip_address(const char *s)
162 if ((isdigit(*s)) || (*s == '.')) { s++; continue; }
168 /* ________________________________________________________________________ */
170 nslookup_main(int argc, char **argv)
172 struct hostent *host;
175 usage(nslookup_usage);
178 server_fprint(stdout);
179 if (is_ip_address(argv[1])) {
180 host = gethostbyaddr_wrapper(argv[1]);
182 host = gethostbyname(argv[1]);
184 hostent_fprint(host, stdout);
188 /* $Id: nslookup.c,v 1.3 2000/02/07 05:29:42 erik Exp $ */