nslookup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini nslookup implementation for busybox
  4. *
  5. * Copyright (C) 1999,2000 by Lineo, inc. and John Beppu
  6. * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
  7. *
  8. * Correct default name server display and explicit name server option
  9. * added by Ben Zeckel <bzeckel@hmc.edu> June 2001
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <netdb.h>
  33. #include <sys/socket.h>
  34. #include <sys/types.h>
  35. #include <netinet/in.h>
  36. #include <resolv.h>
  37. #include <arpa/inet.h>
  38. #include "busybox.h"
  39. /*
  40. | I'm only implementing non-interactive mode;
  41. | I totally forgot nslookup even had an interactive mode.
  42. */
  43. /* only works for IPv4 */
  44. static int addr_fprint(char *addr)
  45. {
  46. uint8_t split[4];
  47. uint32_t ip;
  48. uint32_t *x = (uint32_t *) addr;
  49. ip = ntohl(*x);
  50. split[0] = (ip & 0xff000000) >> 24;
  51. split[1] = (ip & 0x00ff0000) >> 16;
  52. split[2] = (ip & 0x0000ff00) >> 8;
  53. split[3] = (ip & 0x000000ff);
  54. printf("%d.%d.%d.%d", split[0], split[1], split[2], split[3]);
  55. return 0;
  56. }
  57. /* takes the NULL-terminated array h_addr_list, and
  58. * prints its contents appropriately
  59. */
  60. static int addr_list_fprint(char **h_addr_list)
  61. {
  62. int i, j;
  63. char *addr_string = (h_addr_list[1])
  64. ? "Addresses: " : "Address: ";
  65. printf("%s ", addr_string);
  66. for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
  67. addr_fprint(h_addr_list[i]);
  68. /* real nslookup does this */
  69. if (j == 4) {
  70. if (h_addr_list[i + 1]) {
  71. printf("\n ");
  72. }
  73. j = 0;
  74. } else {
  75. if (h_addr_list[i + 1]) {
  76. printf(", ");
  77. }
  78. }
  79. }
  80. printf("\n");
  81. return 0;
  82. }
  83. /* print the results as nslookup would */
  84. static struct hostent *hostent_fprint(struct hostent *host, const char *server_host)
  85. {
  86. if (host) {
  87. printf("%s %s\n", server_host, host->h_name);
  88. addr_list_fprint(host->h_addr_list);
  89. } else {
  90. printf("*** Unknown host\n");
  91. }
  92. return host;
  93. }
  94. /* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
  95. * into a uint32_t
  96. */
  97. static uint32_t str_to_addr(const char *addr)
  98. {
  99. uint32_t split[4];
  100. uint32_t ip;
  101. sscanf(addr, "%d.%d.%d.%d",
  102. &split[0], &split[1], &split[2], &split[3]);
  103. /* assuming sscanf worked */
  104. ip = (split[0] << 24) |
  105. (split[1] << 16) | (split[2] << 8) | (split[3]);
  106. return htonl(ip);
  107. }
  108. /* gethostbyaddr wrapper */
  109. static struct hostent *gethostbyaddr_wrapper(const char *address)
  110. {
  111. struct in_addr addr;
  112. addr.s_addr = str_to_addr(address);
  113. return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */
  114. }
  115. /* lookup the default nameserver and display it */
  116. static inline void server_print(void)
  117. {
  118. struct sockaddr_in def = _res.nsaddr_list[0];
  119. char *ip = inet_ntoa(def.sin_addr);
  120. hostent_fprint(gethostbyaddr_wrapper(ip), "Server:");
  121. printf("\n");
  122. }
  123. /* alter the global _res nameserver structure to use
  124. an explicit dns server instead of what is in /etc/resolv.h */
  125. static inline void set_default_dns(char *server)
  126. {
  127. struct in_addr server_in_addr;
  128. if(inet_aton(server,&server_in_addr))
  129. {
  130. _res.nscount = 1;
  131. _res.nsaddr_list[0].sin_addr = server_in_addr;
  132. }
  133. }
  134. /* naive function to check whether char *s is an ip address */
  135. static int is_ip_address(const char *s)
  136. {
  137. while (*s) {
  138. if ((isdigit(*s)) || (*s == '.')) {
  139. s++;
  140. continue;
  141. }
  142. return 0;
  143. }
  144. return 1;
  145. }
  146. /* ________________________________________________________________________ */
  147. int nslookup_main(int argc, char **argv)
  148. {
  149. struct hostent *host;
  150. /*
  151. * initialize DNS structure _res used in printing the default
  152. * name server and in the explicit name server option feature.
  153. */
  154. res_init();
  155. /*
  156. * We allow 1 or 2 arguments.
  157. * The first is the name to be looked up and the second is an
  158. * optional DNS server with which to do the lookup.
  159. * More than 3 arguments is an error to follow the pattern of the
  160. * standard nslookup
  161. */
  162. if (argc < 2 || *argv[1]=='-' || argc > 3)
  163. bb_show_usage();
  164. else if(argc == 3)
  165. set_default_dns(argv[2]);
  166. server_print();
  167. if (is_ip_address(argv[1])) {
  168. host = gethostbyaddr_wrapper(argv[1]);
  169. } else {
  170. host = xgethostbyname(argv[1]);
  171. }
  172. hostent_fprint(host, "Name: ");
  173. if (host) {
  174. return EXIT_SUCCESS;
  175. }
  176. return EXIT_FAILURE;
  177. }
  178. /* $Id: nslookup.c,v 1.33 2004/10/13 07:25:01 andersen Exp $ */