3
0

ipcalc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.4 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. #if ENABLE_FEATURE_IPCALC_FANCY
  67. static int get_prefix(unsigned long netmask)
  68. {
  69. unsigned long msk = 0x80000000;
  70. int ret = 0;
  71. netmask = htonl(netmask);
  72. while (msk) {
  73. if (netmask & msk)
  74. ret++;
  75. msk >>= 1;
  76. }
  77. return ret;
  78. }
  79. #else
  80. int get_prefix(unsigned long netmask);
  81. #endif
  82. #define NETMASK 0x01
  83. #define BROADCAST 0x02
  84. #define NETWORK 0x04
  85. #define NETPREFIX 0x08
  86. #define HOSTNAME 0x10
  87. #define SILENT 0x20
  88. #if ENABLE_FEATURE_IPCALC_LONG_OPTIONS
  89. static const char ipcalc_longopts[] ALIGN1 =
  90. "netmask\0" No_argument "m" // netmask from IP (assuming complete class A, B, or C network)
  91. "broadcast\0" No_argument "b" // broadcast from IP [netmask]
  92. "network\0" No_argument "n" // network from IP [netmask]
  93. # if ENABLE_FEATURE_IPCALC_FANCY
  94. "prefix\0" No_argument "p" // prefix from IP[/prefix] [netmask]
  95. "hostname\0" No_argument "h" // hostname from IP
  96. "silent\0" No_argument "s" // don’t ever display error messages
  97. # endif
  98. ;
  99. # define GETOPT32 getopt32long
  100. # define LONGOPTS ,ipcalc_longopts
  101. #else
  102. # define GETOPT32 getopt32
  103. # define LONGOPTS
  104. #endif
  105. int ipcalc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  106. int ipcalc_main(int argc UNUSED_PARAM, char **argv)
  107. {
  108. unsigned opt;
  109. bool have_netmask = 0;
  110. struct in_addr s_netmask, s_broadcast, s_network, s_ipaddr;
  111. /* struct in_addr { in_addr_t s_addr; } and in_addr_t
  112. * (which in turn is just a typedef to uint32_t)
  113. * are essentially the same type. A few macros for less verbosity: */
  114. #define netmask (s_netmask.s_addr)
  115. #define broadcast (s_broadcast.s_addr)
  116. #define network (s_network.s_addr)
  117. #define ipaddr (s_ipaddr.s_addr)
  118. char *ipstr;
  119. opt = GETOPT32(argv, "^"
  120. "mbn" IF_FEATURE_IPCALC_FANCY("phs")
  121. "\0" "-1:?2"/*min 1, max 2 args*/
  122. LONGOPTS
  123. );
  124. argv += optind;
  125. if (opt & SILENT)
  126. logmode = LOGMODE_NONE; /* suppress error_msg() output */
  127. opt &= ~SILENT;
  128. if (!(opt & (BROADCAST | NETWORK | NETPREFIX))) {
  129. /* if no options at all or
  130. * (no broadcast,network,prefix) and (two args)... */
  131. if (!opt || argv[1])
  132. bb_show_usage();
  133. }
  134. ipstr = argv[0];
  135. if (ENABLE_FEATURE_IPCALC_FANCY) {
  136. unsigned long netprefix = 0;
  137. char *prefixstr;
  138. prefixstr = ipstr;
  139. while (*prefixstr) {
  140. if (*prefixstr == '/') {
  141. *prefixstr++ = '\0';
  142. if (*prefixstr) {
  143. unsigned msk;
  144. netprefix = xatoul_range(prefixstr, 0, 32);
  145. netmask = 0;
  146. msk = 0x80000000;
  147. while (netprefix > 0) {
  148. netmask |= msk;
  149. msk >>= 1;
  150. netprefix--;
  151. }
  152. netmask = htonl(netmask);
  153. /* Even if it was 0, we will signify that we have a netmask. This allows */
  154. /* for specification of default routes, etc which have a 0 netmask/prefix */
  155. have_netmask = 1;
  156. }
  157. break;
  158. }
  159. prefixstr++;
  160. }
  161. }
  162. if (inet_aton(ipstr, &s_ipaddr) == 0) {
  163. bb_error_msg_and_die("bad IP address: %s", argv[0]);
  164. }
  165. if (argv[1]) {
  166. if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
  167. bb_simple_error_msg_and_die("use prefix or netmask, not both");
  168. }
  169. if (inet_aton(argv[1], &s_netmask) == 0) {
  170. bb_error_msg_and_die("bad netmask: %s", argv[1]);
  171. }
  172. } else {
  173. /* JHC - If the netmask wasn't provided then calculate it */
  174. if (!ENABLE_FEATURE_IPCALC_FANCY || !have_netmask)
  175. netmask = get_netmask(ipaddr);
  176. }
  177. if (opt & NETMASK) {
  178. printf("NETMASK=%s\n", inet_ntoa(s_netmask));
  179. }
  180. if (opt & BROADCAST) {
  181. broadcast = (ipaddr & netmask) | ~netmask;
  182. printf("BROADCAST=%s\n", inet_ntoa(s_broadcast));
  183. }
  184. if (opt & NETWORK) {
  185. network = ipaddr & netmask;
  186. printf("NETWORK=%s\n", inet_ntoa(s_network));
  187. }
  188. if (ENABLE_FEATURE_IPCALC_FANCY) {
  189. if (opt & NETPREFIX) {
  190. printf("PREFIX=%i\n", get_prefix(netmask));
  191. }
  192. if (opt & HOSTNAME) {
  193. struct hostent *hostinfo;
  194. hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
  195. if (!hostinfo) {
  196. bb_herror_msg_and_die("can't find hostname for %s", argv[0]);
  197. }
  198. str_tolower(hostinfo->h_name);
  199. printf("HOSTNAME=%s\n", hostinfo->h_name);
  200. }
  201. }
  202. return EXIT_SUCCESS;
  203. }