ipcalc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 source tree.
  13. */
  14. #include "libbb.h"
  15. /* After libbb.h, because on some systems it needs other includes */
  16. #include <arpa/inet.h>
  17. #define CLASS_A_NETMASK ntohl(0xFF000000)
  18. #define CLASS_B_NETMASK ntohl(0xFFFF0000)
  19. #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
  20. static unsigned long get_netmask(unsigned long ipaddr)
  21. {
  22. ipaddr = htonl(ipaddr);
  23. if ((ipaddr & 0xC0000000) == 0xC0000000)
  24. return CLASS_C_NETMASK;
  25. else if ((ipaddr & 0x80000000) == 0x80000000)
  26. return CLASS_B_NETMASK;
  27. else if ((ipaddr & 0x80000000) == 0)
  28. return CLASS_A_NETMASK;
  29. else
  30. return 0;
  31. }
  32. #if ENABLE_FEATURE_IPCALC_FANCY
  33. static int get_prefix(unsigned long netmask)
  34. {
  35. unsigned long msk = 0x80000000;
  36. int ret = 0;
  37. netmask = htonl(netmask);
  38. while (msk) {
  39. if (netmask & msk)
  40. ret++;
  41. msk >>= 1;
  42. }
  43. return ret;
  44. }
  45. #else
  46. int get_prefix(unsigned long netmask);
  47. #endif
  48. #define NETMASK 0x01
  49. #define BROADCAST 0x02
  50. #define NETWORK 0x04
  51. #define NETPREFIX 0x08
  52. #define HOSTNAME 0x10
  53. #define SILENT 0x20
  54. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
  55. static const char ipcalc_longopts[] ALIGN1 =
  56. "netmask\0" No_argument "m" // netmask from IP (assuming complete class A, B, or C network)
  57. "broadcast\0" No_argument "b" // broadcast from IP [netmask]
  58. "network\0" No_argument "n" // network from IP [netmask]
  59. # if ENABLE_FEATURE_IPCALC_FANCY
  60. "prefix\0" No_argument "p" // prefix from IP[/prefix] [netmask]
  61. "hostname\0" No_argument "h" // hostname from IP
  62. "silent\0" No_argument "s" // don’t ever display error messages
  63. # endif
  64. ;
  65. #endif
  66. int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  67. int ipcalc_main(int argc UNUSED_PARAM, char **argv)
  68. {
  69. unsigned opt;
  70. bool have_netmask = 0;
  71. struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
  72. /* struct in_addr { in_addr_t s_addr; } and in_addr_t
  73. * (which in turn is just a typedef to uint32_t)
  74. * are essentially the same type. A few macros for less verbosity: */
  75. #define netmask (s_netmask.s_addr)
  76. #define broadcast (s_broadcast.s_addr)
  77. #define network (s_network.s_addr)
  78. #define ipaddr (s_ipaddr.s_addr)
  79. char *ipstr;
  80. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
  81. applet_long_options = ipcalc_longopts;
  82. #endif
  83. opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
  84. opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
  85. argv += optind;
  86. if (opt & SILENT)
  87. logmode = LOGMODE_NONE; /* suppress error_msg() output */
  88. opt &= ~SILENT;
  89. if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
  90. /* if no options at all or
  91. * (no broadcast,network,prefix) and (two args)... */
  92. if (!opt || argv[1])
  93. bb_show_usage();
  94. }
  95. ipstr = argv[0];
  96. if (ENABLE_FEATURE_IPCALC_FANCY) {
  97. unsigned long netprefix = 0;
  98. char *prefixstr;
  99. prefixstr = ipstr;
  100. while (*prefixstr) {
  101. if (*prefixstr == '/') {
  102. *prefixstr++ = '\0';
  103. if (*prefixstr) {
  104. unsigned msk;
  105. netprefix = xatoul_range(prefixstr, 0, 32);
  106. netmask = 0;
  107. msk = 0x80000000;
  108. while (netprefix > 0) {
  109. netmask |= msk;
  110. msk >>= 1;
  111. netprefix--;
  112. }
  113. netmask = htonl(netmask);
  114. /* Even if it was 0, we will signify that we have a netmask. This allows */
  115. /* for specification of default routes, etc which have a 0 netmask/prefix */
  116. have_netmask = 1;
  117. }
  118. break;
  119. }
  120. prefixstr++;
  121. }
  122. }
  123. if (inet_aton(ipstr, &s_ipaddr) == 0) {
  124. bb_error_msg_and_die("bad IP address: %s", argv[0]);
  125. }
  126. if (argv[1]) {
  127. if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
  128. bb_error_msg_and_die("use prefix or netmask, not both");
  129. }
  130. if (inet_aton(argv[1], &s_netmask) == 0) {
  131. bb_error_msg_and_die("bad netmask: %s", argv[1]);
  132. }
  133. } else {
  134. /* JHC - If the netmask wasn't provided then calculate it */
  135. if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
  136. netmask = get_netmask(ipaddr);
  137. }
  138. if (opt & NETMASK) {
  139. printf("NETMASK=%s\n", inet_ntoa(s_netmask));
  140. }
  141. if (opt & BROADCAST) {
  142. broadcast = (ipaddr & netmask) | ~netmask;
  143. printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
  144. }
  145. if (opt & NETWORK) {
  146. network = ipaddr & netmask;
  147. printf("NETWORK=%s\n", inet_ntoa(s_network));
  148. }
  149. if (ENABLE_FEATURE_IPCALC_FANCY) {
  150. if (opt & NETPREFIX) {
  151. printf("PREFIX=%i\n", get_prefix(netmask));
  152. }
  153. if (opt & HOSTNAME) {
  154. struct hostent *hostinfo;
  155. hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
  156. if (!hostinfo) {
  157. bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
  158. }
  159. str_tolower(hostinfo->h_name);
  160. printf("HOSTNAME=%s\n", hostinfo->h_name);
  161. }
  162. }
  163. return EXIT_SUCCESS;
  164. }