ipcalc.c 6.0 KB

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