ipcalc.c 5.9 KB

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