blob: 0a2a57437b164775d3d8f863d2880417843538ae [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
35
36static const char nslookup_usage[] =
37"only implementing non-interactive mode\n"
38"I totally forgot nslookup even had an interactive mode\n"
39;
40
41
42/* */
43static void
44server_fprint(FILE *dst)
45{
46 fprintf(dst, "Server: %s\n", "something");
47 fprintf(dst, "Address: %s\n\n", "something");
48}
49
50/* only works for IPv4 */
51static int
52addr_fprint(char *addr, FILE *dst)
53{
54 uint8_t split[4];
55 uint32_t ip;
56 uint32_t *x = (uint32_t *) addr;
57
58 ip = ntohl(*x);
59 split[0] = (ip & 0xff000000) >> 24;
60 split[1] = (ip & 0x00ff0000) >> 16;
61 split[2] = (ip & 0x0000ff00) >> 8;
62 split[3] = (ip & 0x000000ff);
63 fprintf (
64 dst, "%d.%d.%d.%d",
65 split[0], split[1], split[2], split[3]
66 );
67 return 0;
68}
69
70/* */
71static uint32_t
72str_to_addr(const char *addr)
73{
74 uint32_t split[4];
75 uint32_t ip;
76
77 sscanf(addr, "%d.%d.%d.%d",
78 &split[0], &split[1], &split[2], &split[3]);
79
80 /* assuming sscanf worked */
81 ip = (split[0] << 24) |
82 (split[1] << 16) |
83 (split[2] << 8) |
84 (split[3]);
85
86 return htonl(ip);
87}
88
89/* */
90static int
91addr_list_fprint(char **h_addr_list, FILE *dst)
92{
93 int i;
94 char *addr_string = (h_addr_list[1])
95 ? "Addresses"
96 : "Address";
97
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]) {
102 fprintf(dst, ", ");
103 }
104 }
105 fprintf(dst,"\n");
106 return 0;
107}
108
109/* */
110static struct hostent *
111lookup_by_name(const char *hostname)
112{
113 struct hostent *host;
114
115 host = gethostbyname(hostname);
116 if (host) {
117 fprintf(stdout, "Name: %s\n", host->h_name);
118 addr_list_fprint(host->h_addr_list, stdout);
119 } else {
120 herror("crap");
121 }
122 return host;
123}
124
125/* */
126static struct hostent *
127lookup_by_addr(const char *addr)
128{
129 struct hostent *host;
130
131 host = gethostbyaddr(addr, 4, AF_INET); /* IPv4 only for now */
132 if (host) {
133 fprintf(stdout, "Name: %s\n", host->h_name);
134 addr_list_fprint(host->h_addr_list, stdout);
135 } else {
136 herror("crap");
137 }
138 return host;
139}
140
141/* */
142static int
143is_ip_address(const char *s)
144{
145 while (*s) {
146 if ((isdigit(*s)) || (*s == '.')) { s++; continue; }
147 return 0;
148 }
149 return 1;
150}
151
152/* ________________________________________________________________________ */
153int
154nslookup_main(int argc, char **argv)
155{
156 struct in_addr addr;
157
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);
162 } else {
163 lookup_by_name(argv[1]);
164 }
165 return 0;
166}
167
168/* $Id: nslookup.c,v 1.1 2000/01/29 12:59:01 beppu Exp $ */