utils.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. *
  7. * Changes:
  8. *
  9. * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
  10. */
  11. #include "libbb.h"
  12. #include "utils.h"
  13. #include "inet_common.h"
  14. unsigned get_unsigned(char *arg, const char *errmsg)
  15. {
  16. unsigned long res;
  17. char *ptr;
  18. if (*arg) {
  19. res = strtoul(arg, &ptr, 0);
  20. //FIXME: "" will be accepted too, is it correct?!
  21. if (!*ptr && res <= UINT_MAX) {
  22. return res;
  23. }
  24. }
  25. invarg(arg, errmsg); /* does not return */
  26. }
  27. uint32_t get_u32(char *arg, const char *errmsg)
  28. {
  29. unsigned long res;
  30. char *ptr;
  31. if (*arg) {
  32. res = strtoul(arg, &ptr, 0);
  33. //FIXME: "" will be accepted too, is it correct?!
  34. if (!*ptr && res <= 0xFFFFFFFFUL) {
  35. return res;
  36. }
  37. }
  38. invarg(arg, errmsg); /* does not return */
  39. }
  40. uint16_t get_u16(char *arg, const char *errmsg)
  41. {
  42. unsigned long res;
  43. char *ptr;
  44. if (*arg) {
  45. res = strtoul(arg, &ptr, 0);
  46. //FIXME: "" will be accepted too, is it correct?!
  47. if (!*ptr && res <= 0xFFFF) {
  48. return res;
  49. }
  50. }
  51. invarg(arg, errmsg); /* does not return */
  52. }
  53. int get_addr_1(inet_prefix *addr, char *name, int family)
  54. {
  55. memset(addr, 0, sizeof(*addr));
  56. if (strcmp(name, "default") == 0
  57. || strcmp(name, "all") == 0
  58. || strcmp(name, "any") == 0
  59. ) {
  60. addr->family = family;
  61. addr->bytelen = (family == AF_INET6 ? 16 : 4);
  62. addr->bitlen = -1;
  63. return 0;
  64. }
  65. if (strchr(name, ':')) {
  66. addr->family = AF_INET6;
  67. if (family != AF_UNSPEC && family != AF_INET6)
  68. return -1;
  69. if (inet_pton(AF_INET6, name, addr->data) <= 0)
  70. return -1;
  71. addr->bytelen = 16;
  72. addr->bitlen = -1;
  73. return 0;
  74. }
  75. addr->family = AF_INET;
  76. if (family != AF_UNSPEC && family != AF_INET)
  77. return -1;
  78. if (inet_pton(AF_INET, name, addr->data) <= 0)
  79. return -1;
  80. addr->bytelen = 4;
  81. addr->bitlen = -1;
  82. return 0;
  83. }
  84. static int get_prefix_1(inet_prefix *dst, char *arg, int family)
  85. {
  86. int err;
  87. unsigned plen;
  88. char *slash;
  89. memset(dst, 0, sizeof(*dst));
  90. if (strcmp(arg, "default") == 0
  91. || strcmp(arg, "all") == 0
  92. || strcmp(arg, "any") == 0
  93. ) {
  94. dst->family = family;
  95. /*dst->bytelen = 0; - done by memset */
  96. /*dst->bitlen = 0;*/
  97. return 0;
  98. }
  99. slash = strchr(arg, '/');
  100. if (slash)
  101. *slash = '\0';
  102. err = get_addr_1(dst, arg, family);
  103. if (err == 0) {
  104. dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
  105. if (slash) {
  106. inet_prefix netmask_pfx;
  107. netmask_pfx.family = AF_UNSPEC;
  108. plen = bb_strtou(slash + 1, NULL, 0);
  109. if ((errno || plen > dst->bitlen)
  110. && (get_addr_1(&netmask_pfx, slash + 1, family)))
  111. err = -1;
  112. else if (netmask_pfx.family == AF_INET) {
  113. /* fill in prefix length of dotted quad */
  114. uint32_t mask = ntohl(netmask_pfx.data[0]);
  115. uint32_t host = ~mask;
  116. /* a valid netmask must be 2^n - 1 */
  117. if (!(host & (host + 1))) {
  118. for (plen = 0; mask; mask <<= 1)
  119. ++plen;
  120. if (plen <= dst->bitlen) {
  121. dst->bitlen = plen;
  122. /* dst->flags |= PREFIXLEN_SPECIFIED; */
  123. } else
  124. err = -1;
  125. } else
  126. err = -1;
  127. } else {
  128. /* plain prefix */
  129. dst->bitlen = plen;
  130. }
  131. }
  132. }
  133. if (slash)
  134. *slash = '/';
  135. return err;
  136. }
  137. int get_addr(inet_prefix *dst, char *arg, int family)
  138. {
  139. if (family == AF_PACKET) {
  140. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
  141. }
  142. if (get_addr_1(dst, arg, family)) {
  143. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
  144. }
  145. return 0;
  146. }
  147. int get_prefix(inet_prefix *dst, char *arg, int family)
  148. {
  149. if (family == AF_PACKET) {
  150. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
  151. }
  152. if (get_prefix_1(dst, arg, family)) {
  153. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
  154. }
  155. return 0;
  156. }
  157. uint32_t get_addr32(char *name)
  158. {
  159. inet_prefix addr;
  160. if (get_addr_1(&addr, name, AF_INET)) {
  161. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
  162. }
  163. return addr.data[0];
  164. }
  165. void incomplete_command(void)
  166. {
  167. bb_error_msg_and_die("command line is not complete, try option \"help\"");
  168. }
  169. void invarg(const char *arg, const char *opt)
  170. {
  171. bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
  172. }
  173. void duparg(const char *key, const char *arg)
  174. {
  175. bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
  176. }
  177. void duparg2(const char *key, const char *arg)
  178. {
  179. bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
  180. }
  181. int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
  182. {
  183. uint32_t *a1 = a->data;
  184. uint32_t *a2 = b->data;
  185. int words = bits >> 5;
  186. bits &= 0x1f;
  187. if (words)
  188. if (memcmp(a1, a2, words << 2))
  189. return -1;
  190. if (bits) {
  191. uint32_t w1, w2;
  192. uint32_t mask;
  193. w1 = a1[words];
  194. w2 = a2[words];
  195. mask = htonl((0xffffffff) << (0x20 - bits));
  196. if ((w1 ^ w2) & mask)
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. const char *rt_addr_n2a(int af,
  202. void *addr, char *buf, int buflen)
  203. {
  204. switch (af) {
  205. case AF_INET:
  206. case AF_INET6:
  207. return inet_ntop(af, addr, buf, buflen);
  208. default:
  209. return "???";
  210. }
  211. }
  212. #ifdef RESOLVE_HOSTNAMES
  213. const char *format_host(int af, int len, void *addr, char *buf, int buflen)
  214. {
  215. if (resolve_hosts) {
  216. struct hostent *h_ent;
  217. if (len <= 0) {
  218. switch (af) {
  219. case AF_INET:
  220. len = 4;
  221. break;
  222. case AF_INET6:
  223. len = 16;
  224. break;
  225. default:;
  226. }
  227. }
  228. if (len > 0) {
  229. h_ent = gethostbyaddr(addr, len, af);
  230. if (h_ent != NULL) {
  231. safe_strncpy(buf, h_ent->h_name, buflen);
  232. return buf;
  233. }
  234. }
  235. }
  236. return rt_addr_n2a(af, addr, buf, buflen);
  237. }
  238. #endif