diff options
Diffstat (limited to 'nslookup.c')
-rw-r--r-- | nslookup.c | 185 |
1 files changed, 87 insertions, 98 deletions
diff --git a/nslookup.c b/nslookup.c index 969d0b19b..ffa720174 100644 --- a/nslookup.c +++ b/nslookup.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini nslookup implementation for busybox | 3 | * Mini nslookup implementation for busybox |
3 | * | 4 | * |
@@ -41,148 +42,136 @@ | |||
41 | | + find out how the real nslookup gets the default name server | 42 | | + find out how the real nslookup gets the default name server |
42 | */ | 43 | */ |
43 | 44 | ||
44 | static const char nslookup_usage[] = | 45 | static const char nslookup_usage[] = "nslookup [HOST]\n\n"; |
45 | "nslookup [HOST]\n\n" | ||
46 | ; | ||
47 | 46 | ||
48 | 47 | ||
49 | /* I have to see how the real nslookup does this. | 48 | /* I have to see how the real nslookup does this. |
50 | * I could dig through /etc/resolv.conf, but is there a | 49 | * I could dig through /etc/resolv.conf, but is there a |
51 | * better (programatic) way? | 50 | * better (programatic) way? |
52 | */ | 51 | */ |
53 | static void | 52 | static void server_fprint(FILE * dst) |
54 | server_fprint(FILE *dst) | ||
55 | { | 53 | { |
56 | fprintf(dst, "Server: %s\n", "something"); | 54 | fprintf(dst, "Server: %s\n", "something"); |
57 | fprintf(dst, "Address: %s\n\n", "something"); | 55 | fprintf(dst, "Address: %s\n\n", "something"); |
58 | } | 56 | } |
59 | 57 | ||
60 | /* only works for IPv4 */ | 58 | /* only works for IPv4 */ |
61 | static int | 59 | static int addr_fprint(char *addr, FILE * dst) |
62 | addr_fprint(char *addr, FILE *dst) | ||
63 | { | 60 | { |
64 | uint8_t split[4]; | 61 | uint8_t split[4]; |
65 | uint32_t ip; | 62 | uint32_t ip; |
66 | uint32_t *x = (uint32_t *) addr; | 63 | uint32_t *x = (uint32_t *) addr; |
67 | 64 | ||
68 | ip = ntohl(*x); | 65 | ip = ntohl(*x); |
69 | split[0] = (ip & 0xff000000) >> 24; | 66 | split[0] = (ip & 0xff000000) >> 24; |
70 | split[1] = (ip & 0x00ff0000) >> 16; | 67 | split[1] = (ip & 0x00ff0000) >> 16; |
71 | split[2] = (ip & 0x0000ff00) >> 8; | 68 | split[2] = (ip & 0x0000ff00) >> 8; |
72 | split[3] = (ip & 0x000000ff); | 69 | split[3] = (ip & 0x000000ff); |
73 | fprintf ( | 70 | fprintf(dst, "%d.%d.%d.%d", split[0], split[1], split[2], split[3] |
74 | dst, "%d.%d.%d.%d", | 71 | ); |
75 | split[0], split[1], split[2], split[3] | 72 | return 0; |
76 | ); | ||
77 | return 0; | ||
78 | } | 73 | } |
79 | 74 | ||
80 | /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+ | 75 | /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+ |
81 | * into a uint32_t | 76 | * into a uint32_t |
82 | */ | 77 | */ |
83 | static uint32_t | 78 | static uint32_t str_to_addr(const char *addr) |
84 | str_to_addr(const char *addr) | ||
85 | { | 79 | { |
86 | uint32_t split[4]; | 80 | uint32_t split[4]; |
87 | uint32_t ip; | 81 | uint32_t ip; |
88 | 82 | ||
89 | sscanf(addr, "%d.%d.%d.%d", | 83 | sscanf(addr, "%d.%d.%d.%d", |
90 | &split[0], &split[1], &split[2], &split[3]); | 84 | &split[0], &split[1], &split[2], &split[3]); |
91 | 85 | ||
92 | /* assuming sscanf worked */ | 86 | /* assuming sscanf worked */ |
93 | ip = (split[0] << 24) | | 87 | ip = (split[0] << 24) | |
94 | (split[1] << 16) | | 88 | (split[1] << 16) | (split[2] << 8) | (split[3]); |
95 | (split[2] << 8) | | ||
96 | (split[3]); | ||
97 | 89 | ||
98 | return htonl(ip); | 90 | return htonl(ip); |
99 | } | 91 | } |
100 | 92 | ||
101 | /* takes the NULL-terminated array h_addr_list, and | 93 | /* takes the NULL-terminated array h_addr_list, and |
102 | * prints its contents appropriately | 94 | * prints its contents appropriately |
103 | */ | 95 | */ |
104 | static int | 96 | static int addr_list_fprint(char **h_addr_list, FILE * dst) |
105 | addr_list_fprint(char **h_addr_list, FILE *dst) | ||
106 | { | 97 | { |
107 | int i, j; | 98 | int i, j; |
108 | char *addr_string = (h_addr_list[1]) | 99 | char *addr_string = (h_addr_list[1]) |
109 | ? "Addresses" | 100 | ? "Addresses" : "Address"; |
110 | : "Address"; | 101 | |
111 | 102 | fprintf(dst, "%s: ", addr_string); | |
112 | fprintf(dst, "%s: ", addr_string); | 103 | for (i = 0, j = 0; h_addr_list[i]; i++, j++) { |
113 | for (i = 0, j = 0; h_addr_list[i]; i++, j++) { | 104 | addr_fprint(h_addr_list[i], dst); |
114 | addr_fprint(h_addr_list[i], dst); | 105 | |
115 | 106 | /* real nslookup does this */ | |
116 | /* real nslookup does this */ | 107 | if (j == 4) { |
117 | if (j == 4) { | 108 | if (h_addr_list[i + 1]) { |
118 | if (h_addr_list[i+1]) { | 109 | fprintf(dst, "\n "); |
119 | fprintf(dst, "\n "); | 110 | } |
120 | } | 111 | j = 0; |
121 | j = 0; | 112 | } else { |
122 | } else { | 113 | if (h_addr_list[i + 1]) { |
123 | if (h_addr_list[i+1]) { | 114 | fprintf(dst, ", "); |
124 | fprintf(dst, ", "); | 115 | } |
125 | } | 116 | } |
126 | } | ||
127 | 117 | ||
128 | } | 118 | } |
129 | fprintf(dst,"\n"); | 119 | fprintf(dst, "\n"); |
130 | return 0; | 120 | return 0; |
131 | } | 121 | } |
132 | 122 | ||
133 | /* gethostbyaddr wrapper */ | 123 | /* gethostbyaddr wrapper */ |
134 | static struct hostent * | 124 | static struct hostent *gethostbyaddr_wrapper(const char *address) |
135 | gethostbyaddr_wrapper(const char *address) | ||
136 | { | 125 | { |
137 | struct in_addr addr; | 126 | struct in_addr addr; |
138 | 127 | ||
139 | addr.s_addr = str_to_addr(address); | 128 | addr.s_addr = str_to_addr(address); |
140 | return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */ | 129 | return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */ |
141 | } | 130 | } |
142 | 131 | ||
143 | /* print the results as nslookup would */ | 132 | /* print the results as nslookup would */ |
144 | static struct hostent * | 133 | static struct hostent *hostent_fprint(struct hostent *host, FILE * dst) |
145 | hostent_fprint(struct hostent *host, FILE *dst) | ||
146 | { | 134 | { |
147 | if (host) { | 135 | if (host) { |
148 | fprintf(dst, "Name: %s\n", host->h_name); | 136 | fprintf(dst, "Name: %s\n", host->h_name); |
149 | addr_list_fprint(host->h_addr_list, dst); | 137 | addr_list_fprint(host->h_addr_list, dst); |
150 | } else { | 138 | } else { |
151 | fprintf(dst, "*** %s\n", hstrerror(h_errno)); | 139 | fprintf(dst, "*** %s\n", hstrerror(h_errno)); |
152 | } | 140 | } |
153 | return host; | 141 | return host; |
154 | } | 142 | } |
155 | 143 | ||
156 | 144 | ||
157 | /* naive function to check whether char *s is an ip address */ | 145 | /* naive function to check whether char *s is an ip address */ |
158 | static int | 146 | static int is_ip_address(const char *s) |
159 | is_ip_address(const char *s) | ||
160 | { | 147 | { |
161 | while (*s) { | 148 | while (*s) { |
162 | if ((isdigit(*s)) || (*s == '.')) { s++; continue; } | 149 | if ((isdigit(*s)) || (*s == '.')) { |
163 | return 0; | 150 | s++; |
164 | } | 151 | continue; |
165 | return 1; | 152 | } |
153 | return 0; | ||
154 | } | ||
155 | return 1; | ||
166 | } | 156 | } |
167 | 157 | ||
168 | /* ________________________________________________________________________ */ | 158 | /* ________________________________________________________________________ */ |
169 | int | 159 | int nslookup_main(int argc, char **argv) |
170 | nslookup_main(int argc, char **argv) | ||
171 | { | 160 | { |
172 | struct hostent *host; | 161 | struct hostent *host; |
173 | 162 | ||
174 | if (argc < 2) { | 163 | if (argc < 2) { |
175 | usage(nslookup_usage); | 164 | usage(nslookup_usage); |
176 | } | 165 | } |
177 | 166 | ||
178 | server_fprint(stdout); | 167 | server_fprint(stdout); |
179 | if (is_ip_address(argv[1])) { | 168 | if (is_ip_address(argv[1])) { |
180 | host = gethostbyaddr_wrapper(argv[1]); | 169 | host = gethostbyaddr_wrapper(argv[1]); |
181 | } else { | 170 | } else { |
182 | host = gethostbyname(argv[1]); | 171 | host = gethostbyname(argv[1]); |
183 | } | 172 | } |
184 | hostent_fprint(host, stdout); | 173 | hostent_fprint(host, stdout); |
185 | return 0; | 174 | return 0; |
186 | } | 175 | } |
187 | 176 | ||
188 | /* $Id: nslookup.c,v 1.3 2000/02/07 05:29:42 erik Exp $ */ | 177 | /* $Id: nslookup.c,v 1.4 2000/02/08 19:58:47 erik Exp $ */ |