3
0

ipcalc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* vi: set sw=4 ts=4 ai: */
  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. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #include <getopt.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include "busybox.h"
  20. #define IPCALC_MSG(CMD,ALTCMD) if (mode & SILENT) {ALTCMD;} else {CMD;}
  21. #define CLASS_A_NETMASK ntohl(0xFF000000)
  22. #define CLASS_B_NETMASK ntohl(0xFFFF0000)
  23. #define CLASS_C_NETMASK ntohl(0xFFFFFF00)
  24. static unsigned long get_netmask(unsigned long ipaddr)
  25. {
  26. ipaddr = htonl(ipaddr);
  27. if ((ipaddr & 0xC0000000) == 0xC0000000)
  28. return CLASS_C_NETMASK;
  29. else if ((ipaddr & 0x80000000) == 0x80000000)
  30. return CLASS_B_NETMASK;
  31. else if ((ipaddr & 0x80000000) == 0)
  32. return CLASS_A_NETMASK;
  33. else
  34. return 0;
  35. }
  36. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  37. static int get_prefix(unsigned long netmask)
  38. {
  39. unsigned long msk = 0x80000000;
  40. int ret = 0;
  41. netmask = htonl(netmask);
  42. while(msk) {
  43. if (netmask & msk)
  44. ret++;
  45. msk >>= 1;
  46. }
  47. return ret;
  48. }
  49. #endif
  50. #define NETMASK 0x01
  51. #define BROADCAST 0x02
  52. #define NETWORK 0x04
  53. #define NETPREFIX 0x08
  54. #define HOSTNAME 0x10
  55. #define SILENT 0x20
  56. int ipcalc_main(int argc, char **argv)
  57. {
  58. unsigned long mode;
  59. in_addr_t netmask;
  60. in_addr_t broadcast;
  61. in_addr_t network;
  62. in_addr_t ipaddr;
  63. struct in_addr a;
  64. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  65. unsigned long netprefix = 0;
  66. int have_netmask = 0;
  67. char *ipstr, *prefixstr;
  68. #endif
  69. static const struct option long_options[] = {
  70. {"netmask", no_argument, NULL, 'm'},
  71. {"broadcast", no_argument, NULL, 'b'},
  72. {"network", no_argument, NULL, 'n'},
  73. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  74. {"prefix", no_argument, NULL, 'p'},
  75. {"hostname", no_argument, NULL, 'h'},
  76. {"silent", no_argument, NULL, 's'},
  77. #endif
  78. {NULL, 0, NULL, 0}
  79. };
  80. bb_applet_long_options = long_options;
  81. mode = bb_getopt_ulflags(argc, argv,
  82. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  83. "mbnphs"
  84. #else
  85. "mbn"
  86. #endif
  87. );
  88. argc -= optind;
  89. argv += optind;
  90. if (mode & (BROADCAST | NETWORK | NETPREFIX)) {
  91. if (argc > 2 || argc <= 0)
  92. bb_show_usage();
  93. } else {
  94. if (argc != 1)
  95. bb_show_usage();
  96. }
  97. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  98. prefixstr = ipstr = argv[0];
  99. while(*prefixstr) {
  100. if (*prefixstr == '/') {
  101. *prefixstr = (char)0;
  102. prefixstr++;
  103. if (*prefixstr) {
  104. unsigned int msk;
  105. if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
  106. IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
  107. exit(EXIT_FAILURE));
  108. }
  109. netmask = 0;
  110. msk = 0x80000000;
  111. while (netprefix > 0) {
  112. netmask |= msk;
  113. msk >>= 1;
  114. netprefix--;
  115. }
  116. netmask = htonl(netmask);
  117. /* Even if it was 0, we will signify that we have a netmask. This allows */
  118. /* for specification of default routes, etc which have a 0 netmask/prefix */
  119. have_netmask = 1;
  120. }
  121. break;
  122. }
  123. prefixstr++;
  124. }
  125. ipaddr = inet_aton(ipstr, &a);
  126. #else
  127. ipaddr = inet_aton(argv[0], &a);
  128. #endif
  129. if (ipaddr == 0) {
  130. IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s", argv[0]),
  131. exit(EXIT_FAILURE));
  132. }
  133. ipaddr = a.s_addr;
  134. if (argc == 2) {
  135. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  136. if (have_netmask) {
  137. IPCALC_MSG(bb_error_msg_and_die("Both prefix and netmask were specified, use one or the other.\n"),
  138. exit(EXIT_FAILURE));
  139. }
  140. #endif
  141. netmask = inet_aton(argv[1], &a);
  142. if (netmask == 0) {
  143. IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s", argv[1]),
  144. exit(EXIT_FAILURE));
  145. }
  146. netmask = a.s_addr;
  147. } else {
  148. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  149. if (!have_netmask)
  150. #endif
  151. /* JHC - If the netmask wasn't provided then calculate it */
  152. netmask = get_netmask(ipaddr);
  153. }
  154. if (mode & NETMASK) {
  155. printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
  156. }
  157. if (mode & BROADCAST) {
  158. broadcast = (ipaddr & netmask) | ~netmask;
  159. printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
  160. }
  161. if (mode & NETWORK) {
  162. network = ipaddr & netmask;
  163. printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
  164. }
  165. #ifdef CONFIG_FEATURE_IPCALC_FANCY
  166. if (mode & NETPREFIX) {
  167. printf("PREFIX=%i\n", get_prefix(netmask));
  168. }
  169. if (mode & HOSTNAME) {
  170. struct hostent *hostinfo;
  171. int x;
  172. hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
  173. if (!hostinfo) {
  174. IPCALC_MSG(bb_herror_msg_and_die(
  175. "cannot find hostname for %s", argv[0]),);
  176. exit(EXIT_FAILURE);
  177. }
  178. for (x = 0; hostinfo->h_name[x]; x++) {
  179. hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
  180. }
  181. printf("HOSTNAME=%s\n", hostinfo->h_name);
  182. }
  183. #endif
  184. return EXIT_SUCCESS;
  185. }
  186. /* END CODE */
  187. /*
  188. Local Variables:
  189. c-file-style: "linux"
  190. c-basic-offset: 4
  191. tab-width: 4
  192. End:
  193. */