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 static const char nslookup_usage[] =
37 "only implementing non-interactive mode\n"
38 "I totally forgot nslookup even had an interactive mode\n"
44 server_fprint(FILE *dst)
46 fprintf(dst, "Server: %s\n", "something");
47 fprintf(dst, "Address: %s\n\n", "something");
50 /* only works for IPv4 */
52 addr_fprint(char *addr, FILE *dst)
56 uint32_t *x = (uint32_t *) addr;
59 split[0] = (ip & 0xff000000) >> 24;
60 split[1] = (ip & 0x00ff0000) >> 16;
61 split[2] = (ip & 0x0000ff00) >> 8;
62 split[3] = (ip & 0x000000ff);
65 split[0], split[1], split[2], split[3]
72 str_to_addr(const char *addr)
77 sscanf(addr, "%d.%d.%d.%d",
78 &split[0], &split[1], &split[2], &split[3]);
80 /* assuming sscanf worked */
81 ip = (split[0] << 24) |
91 addr_list_fprint(char **h_addr_list, FILE *dst)
94 char *addr_string = (h_addr_list[1])
98 fprintf(dst, "%s: ", addr_string);
99 for (i = 0; h_addr_list[i]; i++) {
100 addr_fprint(h_addr_list[i], dst);
101 if (h_addr_list[i+1]) {
110 static struct hostent *
111 lookup_by_name(const char *hostname)
113 struct hostent *host;
115 host = gethostbyname(hostname);
117 fprintf(stdout, "Name: %s\n", host->h_name);
118 addr_list_fprint(host->h_addr_list, stdout);
126 static struct hostent *
127 lookup_by_addr(const char *addr)
129 struct hostent *host;
131 host = gethostbyaddr(addr, 4, AF_INET); /* IPv4 only for now */
133 fprintf(stdout, "Name: %s\n", host->h_name);
134 addr_list_fprint(host->h_addr_list, stdout);
143 is_ip_address(const char *s)
146 if ((isdigit(*s)) || (*s == '.')) { s++; continue; }
152 /* ________________________________________________________________________ */
154 nslookup_main(int argc, char **argv)
158 server_fprint(stdout);
159 if (is_ip_address(argv[1])) {
160 addr.s_addr = str_to_addr(argv[1]);
161 lookup_by_addr((char *) &addr);
163 lookup_by_name(argv[1]);
168 /* $Id: nslookup.c,v 1.1 2000/01/29 12:59:01 beppu Exp $ */