diff options
author | John Beppu <beppu@lbox.org> | 2000-01-29 12:59:01 +0000 |
---|---|---|
committer | John Beppu <beppu@lbox.org> | 2000-01-29 12:59:01 +0000 |
commit | b332e779e6709908b203054c687cd1dec660f50e (patch) | |
tree | 99c37e076b7416f1c3612ba2ac2568acb6fe4fe7 | |
parent | 04e97024e78f452526f710a5b3969c78a65f2b33 (diff) | |
download | busybox-w32-b332e779e6709908b203054c687cd1dec660f50e.tar.gz busybox-w32-b332e779e6709908b203054c687cd1dec660f50e.tar.bz2 busybox-w32-b332e779e6709908b203054c687cd1dec660f50e.zip |
nslookup -- a work in progress...
-rw-r--r-- | applets/busybox.c | 3 | ||||
-rw-r--r-- | busybox.c | 3 | ||||
-rw-r--r-- | busybox.def.h | 1 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | networking/nslookup.c | 168 | ||||
-rw-r--r-- | nslookup.c | 168 |
6 files changed, 344 insertions, 0 deletions
diff --git a/applets/busybox.c b/applets/busybox.c index d59b2855e..7f14473e0 100644 --- a/applets/busybox.c +++ b/applets/busybox.c | |||
@@ -173,6 +173,9 @@ static const struct Applet applets[] = { | |||
173 | #ifdef BB_MV //bin | 173 | #ifdef BB_MV //bin |
174 | {"mv", mv_main}, | 174 | {"mv", mv_main}, |
175 | #endif | 175 | #endif |
176 | #ifdef BB_NSLOOKUP //bin | ||
177 | {"nslookup", nslookup_main}, | ||
178 | #endif | ||
176 | #ifdef BB_PING //bin | 179 | #ifdef BB_PING //bin |
177 | {"ping", ping_main}, | 180 | {"ping", ping_main}, |
178 | #endif | 181 | #endif |
@@ -173,6 +173,9 @@ static const struct Applet applets[] = { | |||
173 | #ifdef BB_MV //bin | 173 | #ifdef BB_MV //bin |
174 | {"mv", mv_main}, | 174 | {"mv", mv_main}, |
175 | #endif | 175 | #endif |
176 | #ifdef BB_NSLOOKUP //bin | ||
177 | {"nslookup", nslookup_main}, | ||
178 | #endif | ||
176 | #ifdef BB_PING //bin | 179 | #ifdef BB_PING //bin |
177 | {"ping", ping_main}, | 180 | {"ping", ping_main}, |
178 | #endif | 181 | #endif |
diff --git a/busybox.def.h b/busybox.def.h index 8adccdc64..fa192205a 100644 --- a/busybox.def.h +++ b/busybox.def.h | |||
@@ -60,6 +60,7 @@ | |||
60 | //#define BB_MT | 60 | //#define BB_MT |
61 | //#define BB_MTAB | 61 | //#define BB_MTAB |
62 | #define BB_MV | 62 | #define BB_MV |
63 | #define BB_NSLOOKUP | ||
63 | #define BB_PING | 64 | #define BB_PING |
64 | #define BB_POWEROFF | 65 | #define BB_POWEROFF |
65 | //#define BB_PRINTF | 66 | //#define BB_PRINTF |
diff --git a/internal.h b/internal.h index 39f7e0c5e..1686054a8 100644 --- a/internal.h +++ b/internal.h | |||
@@ -106,6 +106,7 @@ extern int more_main(int argc, char** argv); | |||
106 | extern int mount_main(int argc, char** argv); | 106 | extern int mount_main(int argc, char** argv); |
107 | extern int mt_main(int argc, char** argv); | 107 | extern int mt_main(int argc, char** argv); |
108 | extern int mv_main(int argc, char** argv); | 108 | extern int mv_main(int argc, char** argv); |
109 | extern int nslookup_main(int argc, char** argv); | ||
109 | extern int ping_main(int argc, char **argv); | 110 | extern int ping_main(int argc, char **argv); |
110 | extern int poweroff_main(int argc, char **argv); | 111 | extern int poweroff_main(int argc, char **argv); |
111 | extern int printf_main(int argc, char** argv); | 112 | extern int printf_main(int argc, char** argv); |
diff --git a/networking/nslookup.c b/networking/nslookup.c new file mode 100644 index 000000000..0a2a57437 --- /dev/null +++ b/networking/nslookup.c | |||
@@ -0,0 +1,168 @@ | |||
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 | static 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 | /* */ | ||
43 | static void | ||
44 | server_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 */ | ||
51 | static int | ||
52 | addr_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 | /* */ | ||
71 | static uint32_t | ||
72 | str_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 | /* */ | ||
90 | static int | ||
91 | addr_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 | /* */ | ||
110 | static struct hostent * | ||
111 | lookup_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 | /* */ | ||
126 | static struct hostent * | ||
127 | lookup_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 | /* */ | ||
142 | static int | ||
143 | is_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 | /* ________________________________________________________________________ */ | ||
153 | int | ||
154 | nslookup_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 $ */ | ||
diff --git a/nslookup.c b/nslookup.c new file mode 100644 index 000000000..0a2a57437 --- /dev/null +++ b/nslookup.c | |||
@@ -0,0 +1,168 @@ | |||
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 | static 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 | /* */ | ||
43 | static void | ||
44 | server_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 */ | ||
51 | static int | ||
52 | addr_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 | /* */ | ||
71 | static uint32_t | ||
72 | str_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 | /* */ | ||
90 | static int | ||
91 | addr_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 | /* */ | ||
110 | static struct hostent * | ||
111 | lookup_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 | /* */ | ||
126 | static struct hostent * | ||
127 | lookup_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 | /* */ | ||
142 | static int | ||
143 | is_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 | /* ________________________________________________________________________ */ | ||
153 | int | ||
154 | nslookup_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 $ */ | ||