A few minor updates. ;-)
[oweals/busybox.git] / nslookup.c
1 /*
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 /*
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  */
43
44 static const char nslookup_usage[] =
45     "nslookup [HOST]\n\n"
46 ;
47
48
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?
52  */
53 static void
54 server_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 */
61 static int
62 addr_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
80 /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
81  * into a uint32_t
82  */
83 static uint32_t
84 str_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
101 /* takes the NULL-terminated array h_addr_list, and
102  * prints its contents appropriately
103  */
104 static int
105 addr_list_fprint(char **h_addr_list, FILE *dst)
106 {
107     int     i, j;
108     char    *addr_string = (h_addr_list[1]) 
109         ? "Addresses" 
110         : "Address";
111
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);
115
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             }
126         }
127
128     }
129     fprintf(dst,"\n");
130     return 0;
131 }
132
133 /* gethostbyaddr wrapper */
134 static struct hostent *
135 gethostbyaddr_wrapper(const char *address)
136 {
137     struct in_addr  addr;
138
139     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 */
144 static struct hostent *
145 hostent_fprint(struct hostent *host, FILE *dst)
146 {
147     if (host) {
148         fprintf(dst, "Name:    %s\n", host->h_name);
149         addr_list_fprint(host->h_addr_list, dst);
150     } else {
151         fprintf(dst, "*** %s\n", hstrerror(h_errno));
152     }
153     return host;
154 }
155
156
157 /* naive function to check whether char *s is an ip address */
158 static int
159 is_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 /* ________________________________________________________________________ */
169 int
170 nslookup_main(int argc, char **argv)
171 {
172     struct hostent  *host;
173
174     if (argc < 2) {
175         usage(nslookup_usage);
176     }
177
178     server_fprint(stdout);
179     if (is_ip_address(argv[1])) {
180         host = gethostbyaddr_wrapper(argv[1]); 
181     } else {
182         host = gethostbyname(argv[1]);
183     }
184     hostent_fprint(host, stdout);
185     return 0;
186 }
187
188 /* $Id: nslookup.c,v 1.3 2000/02/07 05:29:42 erik Exp $ */