ipcalc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.3 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(ipcalc, BB_DIR_BIN, BB_SUID_DROP))
  34. //kbuild:lib-$(CONFIG_IPCALC) += ipcalc.o
  35. //usage:#define ipcalc_trivial_usage
  36. //usage: "[OPTIONS] ADDRESS"
  37. //usage: IF_FEATURE_IPCALC_FANCY("[/PREFIX]") " [NETMASK]"
  38. //usage:#define ipcalc_full_usage "\n\n"
  39. //usage: "Calculate IP network settings from a IP address\n"
  40. //usage: IF_FEATURE_IPCALC_LONG_OPTIONS(
  41. //usage: "\n -b,--broadcast Display calculated broadcast address"
  42. //usage: "\n -n,--network Display calculated network address"
  43. //usage: "\n -m,--netmask Display default netmask for IP"
  44. //usage: IF_FEATURE_IPCALC_FANCY(
  45. //usage: "\n -p,--prefix Display the prefix for IP/NETMASK"
  46. //usage: "\n -h,--hostname Display first resolved host name"
  47. //usage: "\n -s,--silent Don't ever display error messages"
  48. //usage: )
  49. //usage: )
  50. //usage: IF_NOT_FEATURE_IPCALC_LONG_OPTIONS(
  51. //usage: "\n -b Display calculated broadcast address"
  52. //usage: "\n -n Display calculated network address"
  53. //usage: "\n -m Display default netmask for IP"
  54. //usage: IF_FEATURE_IPCALC_FANCY(
  55. //usage: "\n -p Display the prefix for IP/NETMASK"
  56. //usage: "\n -h Display first resolved host name"
  57. //usage: "\n -s Don't ever display error messages"
  58. //usage: )
  59. //usage: )
  60. #include "libbb.h"
  61. /* After libbb.h, because on some systems it needs other includes */
  62. #include <arpa/inet.h>
  63. #define CLASS_A_NETMASK ntohl(0xFF000000)
  64. #define CLASS_B_NETMASK ntohl(0xFFFF0000)
  65. #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
  66. static unsigned long get_netmask(unsigned long ipaddr)
  67. {
  68. ipaddr = htonl(ipaddr);
  69. if ((ipaddr & 0xC0000000) == 0xC0000000)
  70. return CLASS_C_NETMASK;
  71. else if ((ipaddr & 0x80000000) == 0x80000000)
  72. return CLASS_B_NETMASK;
  73. else if ((ipaddr & 0x80000000) == 0)
  74. return CLASS_A_NETMASK;
  75. else
  76. return 0;
  77. }
  78. #if ENABLE_FEATURE_IPCALC_FANCY
  79. static int get_prefix(unsigned long netmask)
  80. {
  81. unsigned long msk = 0x80000000;
  82. int ret = 0;
  83. netmask = htonl(netmask);
  84. while (msk) {
  85. if (netmask & msk)
  86. ret++;
  87. msk >>= 1;
  88. }
  89. return ret;
  90. }
  91. #else
  92. int get_prefix(unsigned long netmask);
  93. #endif
  94. #define NETMASK 0x01
  95. #define BROADCAST 0x02
  96. #define NETWORK 0x04
  97. #define NETPREFIX 0x08
  98. #define HOSTNAME 0x10
  99. #define SILENT 0x20
  100. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
  101. static const char ipcalc_longopts[] ALIGN1 =
  102. "netmask\0" No_argument "m" // netmask from IP (assuming complete class A, B, or C network)
  103. "broadcast\0" No_argument "b" // broadcast from IP [netmask]
  104. "network\0" No_argument "n" // network from IP [netmask]
  105. # if ENABLE_FEATURE_IPCALC_FANCY
  106. "prefix\0" No_argument "p" // prefix from IP[/prefix] [netmask]
  107. "hostname\0" No_argument "h" // hostname from IP
  108. "silent\0" No_argument "s" // don’t ever display error messages
  109. # endif
  110. ;
  111. #endif
  112. int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  113. int ipcalc_main(int argc UNUSED_PARAM, char **argv)
  114. {
  115. unsigned opt;
  116. bool have_netmask = 0;
  117. struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
  118. /* struct in_addr { in_addr_t s_addr; } and in_addr_t
  119. * (which in turn is just a typedef to uint32_t)
  120. * are essentially the same type. A few macros for less verbosity: */
  121. #define netmask (s_netmask.s_addr)
  122. #define broadcast (s_broadcast.s_addr)
  123. #define network (s_network.s_addr)
  124. #define ipaddr (s_ipaddr.s_addr)
  125. char *ipstr;
  126. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
  127. applet_long_options = ipcalc_longopts;
  128. #endif
  129. opt_complementary = "-1:?2"; /* minimum 1 arg, maximum 2 args */
  130. opt = getopt32(argv, "mbn" IF_FEATURE_IPCALC_FANCY("phs"));
  131. argv += optind;
  132. if (opt & SILENT)
  133. logmode = LOGMODE_NONE; /* suppress error_msg() output */
  134. opt &= ~SILENT;
  135. if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
  136. /* if no options at all or
  137. * (no broadcast,network,prefix) and (two args)... */
  138. if (!opt || argv[1])
  139. bb_show_usage();
  140. }
  141. ipstr = argv[0];
  142. if (ENABLE_FEATURE_IPCALC_FANCY) {
  143. unsigned long netprefix = 0;
  144. char *prefixstr;
  145. prefixstr = ipstr;
  146. while (*prefixstr) {
  147. if (*prefixstr == '/') {
  148. *prefixstr++ = '\0';
  149. if (*prefixstr) {
  150. unsigned msk;
  151. netprefix = xatoul_range(prefixstr, 0, 32);
  152. netmask = 0;
  153. msk = 0x80000000;
  154. while (netprefix > 0) {
  155. netmask |= msk;
  156. msk >>= 1;
  157. netprefix--;
  158. }
  159. netmask = htonl(netmask);
  160. /* Even if it was 0, we will signify that we have a netmask. This allows */
  161. /* for specification of default routes, etc which have a 0 netmask/prefix */
  162. have_netmask = 1;
  163. }
  164. break;
  165. }
  166. prefixstr++;
  167. }
  168. }
  169. if (inet_aton(ipstr, &s_ipaddr) == 0) {
  170. bb_error_msg_and_die("bad IP address: %s", argv[0]);
  171. }
  172. if (argv[1]) {
  173. if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
  174. bb_error_msg_and_die("use prefix or netmask, not both");
  175. }
  176. if (inet_aton(argv[1], &s_netmask) == 0) {
  177. bb_error_msg_and_die("bad netmask: %s", argv[1]);
  178. }
  179. } else {
  180. /* JHC - If the netmask wasn't provided then calculate it */
  181. if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
  182. netmask = get_netmask(ipaddr);
  183. }
  184. if (opt & NETMASK) {
  185. printf("NETMASK=%s\n", inet_ntoa(s_netmask));
  186. }
  187. if (opt & BROADCAST) {
  188. broadcast = (ipaddr & netmask) | ~netmask;
  189. printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
  190. }
  191. if (opt & NETWORK) {
  192. network = ipaddr & netmask;
  193. printf("NETWORK=%s\n", inet_ntoa(s_network));
  194. }
  195. if (ENABLE_FEATURE_IPCALC_FANCY) {
  196. if (opt & NETPREFIX) {
  197. printf("PREFIX=%i\n", get_prefix(netmask));
  198. }
  199. if (opt & HOSTNAME) {
  200. struct hostent *hostinfo;
  201. hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
  202. if (!hostinfo) {
  203. bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
  204. }
  205. str_tolower(hostinfo->h_name);
  206. printf("HOSTNAME=%s\n", hostinfo->h_name);
  207. }
  208. }
  209. return EXIT_SUCCESS;
  210. }