utils.c 5.6 KB

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