3
0

ipcalc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini ipcalc implementation for busybox
  4. *
  5. * By Jordan Crouse <jordan@cosmicpenguin.net>
  6. * Stephan Linz <linz@li-pro.net>
  7. *
  8. * This is a complete reimplementation of the ipcalc program
  9. * from Red Hat. I didn't look at their source code, but there
  10. * is no denying that this is a loving reimplementation
  11. *
  12. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  13. */
  14. #include "busybox.h"
  15. #include <ctype.h>
  16. #include <getopt.h>
  17. #include <sys/socket.h>
  18. #include <arpa/inet.h>
  19. #define CLASS_A_NETMASK ntohl(0xFF000000)
  20. #define CLASS_B_NETMASK ntohl(0xFFFF0000)
  21. #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
  22. static unsigned long get_netmask(unsigned long ipaddr)
  23. {
  24. ipaddr = htonl(ipaddr);
  25. if ((ipaddr & 0xC0000000) == 0xC0000000)
  26. return CLASS_C_NETMASK;
  27. else if ((ipaddr & 0x80000000) == 0x80000000)
  28. return CLASS_B_NETMASK;
  29. else if ((ipaddr & 0x80000000) == 0)
  30. return CLASS_A_NETMASK;
  31. else
  32. return 0;
  33. }
  34. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  35. static int get_prefix(unsigned long netmask)
  36. {
  37. unsigned long msk = 0x80000000;
  38. int ret = 0;
  39. netmask = htonl(netmask);
  40. while (msk) {
  41. if (netmask & msk)
  42. ret++;
  43. msk >>= 1;
  44. }
  45. return ret;
  46. }
  47. #else
  48. int get_prefix(unsigned long netmask);
  49. #endif
  50. #define NETMASK 0x01
  51. #define BROADCAST 0x02
  52. #define NETWORK 0x04
  53. #define NETPREFIX 0x08
  54. #define HOSTNAME 0x10
  55. #define SILENT 0x20
  56. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
  57. static const struct option long_options[] = {
  58. { "netmask", no_argument, NULL, 'm' },
  59. { "broadcast", no_argument, NULL, 'b' },
  60. { "network", no_argument, NULL, 'n' },
  61. # if ENABLE_FEATURE_IPCALC_FANCY
  62. { "prefix", no_argument, NULL, 'p' },
  63. { "hostname", no_argument, NULL, 'h' },
  64. { "silent", no_argument, NULL, 's' },
  65. # endif
  66. { NULL, 0, NULL, 0 }
  67. };
  68. #endif
  69. int ipcalc_main(int argc, char **argv)
  70. {
  71. unsigned opt;
  72. int have_netmask = 0;
  73. in_addr_t netmask, broadcast, network, ipaddr;
  74. struct in_addr a;
  75. char *ipstr;
  76. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
  77. applet_long_options = long_options;
  78. #endif
  79. opt = getopt32(argc, argv, "mbn" USE_FEATURE_IPCALC_FANCY("phs"));
  80. argc -= optind;
  81. argv += optind;
  82. if (opt & (BROADCAST | NETWORK | NETPREFIX)) {
  83. if (argc > 2 || argc <= 0)
  84. bb_show_usage();
  85. } else {
  86. if (argc != 1)
  87. bb_show_usage();
  88. }
  89. if (opt & SILENT)
  90. logmode = LOGMODE_NONE; /* Suppress error_msg() output */
  91. ipstr = argv[0];
  92. if (ENABLE_FEATURE_IPCALC_FANCY) {
  93. unsigned long netprefix = 0;
  94. char *prefixstr;
  95. prefixstr = ipstr;
  96. while (*prefixstr) {
  97. if (*prefixstr == '/') {
  98. *prefixstr = (char)0;
  99. prefixstr++;
  100. if (*prefixstr) {
  101. unsigned msk;
  102. netprefix = xatoul_range(prefixstr, 0, 32);
  103. netmask = 0;
  104. msk = 0x80000000;
  105. while (netprefix > 0) {
  106. netmask |= msk;
  107. msk >>= 1;
  108. netprefix--;
  109. }
  110. netmask = htonl(netmask);
  111. /* Even if it was 0, we will signify that we have a netmask. This allows */
  112. /* for specification of default routes, etc which have a 0 netmask/prefix */
  113. have_netmask = 1;
  114. }
  115. break;
  116. }
  117. prefixstr++;
  118. }
  119. }
  120. ipaddr = inet_aton(ipstr, &a);
  121. if (ipaddr == 0) {
  122. bb_error_msg_and_die("bad IP address: %s", argv[0]);
  123. }
  124. ipaddr = a.s_addr;
  125. if (argc == 2) {
  126. if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
  127. bb_error_msg_and_die("use prefix or netmask, not both");
  128. }
  129. netmask = inet_aton(argv[1], &a);
  130. if (netmask == 0) {
  131. bb_error_msg_and_die("bad netmask: %s", argv[1]);
  132. }
  133. netmask = a.s_addr;
  134. } else {
  135. /* JHC - If the netmask wasn't provided then calculate it */
  136. if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
  137. netmask = get_netmask(ipaddr);
  138. }
  139. if (opt & NETMASK) {
  140. printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
  141. }
  142. if (opt & BROADCAST) {
  143. broadcast = (ipaddr & netmask) | ~netmask;
  144. printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
  145. }
  146. if (opt & NETWORK) {
  147. network = ipaddr & netmask;
  148. printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
  149. }
  150. if (ENABLE_FEATURE_IPCALC_FANCY) {
  151. if (opt & NETPREFIX) {
  152. printf("PREFIX=%i\n", get_prefix(netmask));
  153. }
  154. if (opt & HOSTNAME) {
  155. struct hostent *hostinfo;
  156. int x;
  157. hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
  158. if (!hostinfo) {
  159. bb_herror_msg_and_die("cannot find hostname for %s", argv[0]);
  160. }
  161. for (x = 0; hostinfo->h_name[x]; x++) {
  162. hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
  163. }
  164. printf("HOSTNAME=%s\n", hostinfo->h_name);
  165. }
  166. }
  167. return EXIT_SUCCESS;
  168. }