utils.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. if (family != AF_UNSPEC && family != AF_INET)
  76. return -1;
  77. /* Try to parse it as IPv4 */
  78. addr->family = AF_INET;
  79. #if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */
  80. if (inet_pton(AF_INET, name, addr->data) <= 0)
  81. return -1;
  82. #else
  83. {
  84. unsigned i = 0;
  85. unsigned n = 0;
  86. const char *cp = name - 1;
  87. while (*++cp) {
  88. if ((unsigned char)(*cp - '0') <= 9) {
  89. n = 10 * n + (unsigned char)(*cp - '0');
  90. if (n >= 256)
  91. return -1;
  92. ((uint8_t*)addr->data)[i] = n;
  93. continue;
  94. }
  95. if (*cp == '.' && ++i <= 3) {
  96. n = 0;
  97. continue;
  98. }
  99. return -1;
  100. }
  101. }
  102. #endif
  103. addr->bytelen = 4;
  104. addr->bitlen = -1;
  105. return 0;
  106. }
  107. static void get_prefix_1(inet_prefix *dst, char *arg, int family)
  108. {
  109. char *slash;
  110. memset(dst, 0, sizeof(*dst));
  111. if (strcmp(arg, "default") == 0
  112. || strcmp(arg, "all") == 0
  113. || strcmp(arg, "any") == 0
  114. ) {
  115. dst->family = family;
  116. /*dst->bytelen = 0; - done by memset */
  117. /*dst->bitlen = 0;*/
  118. return;
  119. }
  120. slash = strchr(arg, '/');
  121. if (slash)
  122. *slash = '\0';
  123. if (get_addr_1(dst, arg, family) == 0) {
  124. dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
  125. if (slash) {
  126. unsigned plen;
  127. inet_prefix netmask_pfx;
  128. netmask_pfx.family = AF_UNSPEC;
  129. plen = bb_strtou(slash + 1, NULL, 0);
  130. if ((errno || plen > dst->bitlen)
  131. && get_addr_1(&netmask_pfx, slash + 1, family) != 0
  132. ) {
  133. goto bad;
  134. }
  135. if (netmask_pfx.family == AF_INET) {
  136. /* fill in prefix length of dotted quad */
  137. uint32_t mask = ntohl(netmask_pfx.data[0]);
  138. uint32_t host = ~mask;
  139. /* a valid netmask must be 2^n - 1 */
  140. if (host & (host + 1))
  141. goto bad;
  142. for (plen = 0; mask; mask <<= 1)
  143. ++plen;
  144. if (plen > dst->bitlen)
  145. goto bad;
  146. /* dst->flags |= PREFIXLEN_SPECIFIED; */
  147. }
  148. dst->bitlen = plen;
  149. }
  150. }
  151. if (slash)
  152. *slash = '/';
  153. return;
  154. bad:
  155. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
  156. }
  157. int get_addr(inet_prefix *dst, char *arg, int family)
  158. {
  159. if (family == AF_PACKET) {
  160. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
  161. }
  162. if (get_addr_1(dst, arg, family)) {
  163. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
  164. }
  165. return 0;
  166. }
  167. void get_prefix(inet_prefix *dst, char *arg, int family)
  168. {
  169. if (family == AF_PACKET) {
  170. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
  171. }
  172. get_prefix_1(dst, arg, family);
  173. }
  174. uint32_t get_addr32(char *name)
  175. {
  176. inet_prefix addr;
  177. if (get_addr_1(&addr, name, AF_INET)) {
  178. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
  179. }
  180. return addr.data[0];
  181. }
  182. void incomplete_command(void)
  183. {
  184. bb_error_msg_and_die("command line is not complete, try option \"help\"");
  185. }
  186. void invarg(const char *arg, const char *opt)
  187. {
  188. bb_error_msg_and_die(bb_msg_invalid_arg, arg, opt);
  189. }
  190. void duparg(const char *key, const char *arg)
  191. {
  192. bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
  193. }
  194. void duparg2(const char *key, const char *arg)
  195. {
  196. bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
  197. }
  198. int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
  199. {
  200. const uint32_t *a1 = a->data;
  201. const uint32_t *a2 = b->data;
  202. int words = bits >> 5;
  203. bits &= 0x1f;
  204. if (words)
  205. if (memcmp(a1, a2, words << 2))
  206. return -1;
  207. if (bits) {
  208. uint32_t w1, w2;
  209. uint32_t mask;
  210. w1 = a1[words];
  211. w2 = a2[words];
  212. mask = htonl((0xffffffff) << (0x20 - bits));
  213. if ((w1 ^ w2) & mask)
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. const char *rt_addr_n2a(int af,
  219. void *addr, char *buf, int buflen)
  220. {
  221. switch (af) {
  222. case AF_INET:
  223. case AF_INET6:
  224. return inet_ntop(af, addr, buf, buflen);
  225. default:
  226. return "???";
  227. }
  228. }
  229. #ifdef RESOLVE_HOSTNAMES
  230. const char *format_host(int af, int len, void *addr, char *buf, int buflen)
  231. {
  232. if (resolve_hosts) {
  233. struct hostent *h_ent;
  234. if (len <= 0) {
  235. switch (af) {
  236. case AF_INET:
  237. len = 4;
  238. break;
  239. case AF_INET6:
  240. len = 16;
  241. break;
  242. default:;
  243. }
  244. }
  245. if (len > 0) {
  246. h_ent = gethostbyaddr(addr, len, af);
  247. if (h_ent != NULL) {
  248. safe_strncpy(buf, h_ent->h_name, buflen);
  249. return buf;
  250. }
  251. }
  252. }
  253. return rt_addr_n2a(af, addr, buf, buflen);
  254. }
  255. #endif