utils.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 FAST_FUNC get_hz(void)
  15. {
  16. static unsigned hz_internal;
  17. FILE *fp;
  18. if (hz_internal)
  19. return hz_internal;
  20. fp = fopen_for_read("/proc/net/psched");
  21. if (fp) {
  22. unsigned nom, denom;
  23. if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
  24. if (nom == 1000000)
  25. hz_internal = denom;
  26. fclose(fp);
  27. }
  28. if (!hz_internal)
  29. hz_internal = bb_clk_tck();
  30. return hz_internal;
  31. }
  32. unsigned FAST_FUNC get_unsigned(char *arg, const char *errmsg)
  33. {
  34. unsigned long res;
  35. char *ptr;
  36. if (*arg) {
  37. res = strtoul(arg, &ptr, 0);
  38. //FIXME: "" will be accepted too, is it correct?!
  39. if (!*ptr && res <= UINT_MAX) {
  40. return res;
  41. }
  42. }
  43. invarg_1_to_2(arg, errmsg); /* does not return */
  44. }
  45. uint32_t FAST_FUNC get_u32(char *arg, const char *errmsg)
  46. {
  47. unsigned long res;
  48. char *ptr;
  49. if (*arg) {
  50. res = strtoul(arg, &ptr, 0);
  51. //FIXME: "" will be accepted too, is it correct?!
  52. if (!*ptr && res <= 0xFFFFFFFFUL) {
  53. return res;
  54. }
  55. }
  56. invarg_1_to_2(arg, errmsg); /* does not return */
  57. }
  58. uint16_t FAST_FUNC get_u16(char *arg, const char *errmsg)
  59. {
  60. unsigned long res;
  61. char *ptr;
  62. if (*arg) {
  63. res = strtoul(arg, &ptr, 0);
  64. //FIXME: "" will be accepted too, is it correct?!
  65. if (!*ptr && res <= 0xFFFF) {
  66. return res;
  67. }
  68. }
  69. invarg_1_to_2(arg, errmsg); /* does not return */
  70. }
  71. int FAST_FUNC get_addr_1(inet_prefix *addr, char *name, int family)
  72. {
  73. memset(addr, 0, sizeof(*addr));
  74. if (strcmp(name, "default") == 0
  75. || strcmp(name, "all") == 0
  76. || strcmp(name, "any") == 0
  77. ) {
  78. addr->family = family;
  79. addr->bytelen = (family == AF_INET6 ? 16 : 4);
  80. addr->bitlen = -1;
  81. return 0;
  82. }
  83. if (strchr(name, ':')) {
  84. addr->family = AF_INET6;
  85. if (family != AF_UNSPEC && family != AF_INET6)
  86. return -1;
  87. if (inet_pton(AF_INET6, name, addr->data) <= 0)
  88. return -1;
  89. addr->bytelen = 16;
  90. addr->bitlen = -1;
  91. return 0;
  92. }
  93. if (family != AF_UNSPEC && family != AF_INET)
  94. return -1;
  95. /* Try to parse it as IPv4 */
  96. addr->family = AF_INET;
  97. #if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */
  98. if (inet_pton(AF_INET, name, addr->data) <= 0)
  99. return -1;
  100. #else
  101. {
  102. unsigned i = 0;
  103. unsigned n = 0;
  104. const char *cp = name - 1;
  105. while (*++cp) {
  106. if ((unsigned char)(*cp - '0') <= 9) {
  107. n = 10 * n + (unsigned char)(*cp - '0');
  108. if (n >= 256)
  109. return -1;
  110. ((uint8_t*)addr->data)[i] = n;
  111. continue;
  112. }
  113. if (*cp == '.' && ++i <= 3) {
  114. n = 0;
  115. continue;
  116. }
  117. return -1;
  118. }
  119. }
  120. #endif
  121. addr->bytelen = 4;
  122. addr->bitlen = -1;
  123. return 0;
  124. }
  125. static void get_prefix_1(inet_prefix *dst, char *arg, int family)
  126. {
  127. char *slash;
  128. memset(dst, 0, sizeof(*dst));
  129. if (strcmp(arg, "default") == 0
  130. || strcmp(arg, "all") == 0
  131. || strcmp(arg, "any") == 0
  132. ) {
  133. dst->family = family;
  134. /*dst->bytelen = 0; - done by memset */
  135. /*dst->bitlen = 0;*/
  136. return;
  137. }
  138. slash = strchr(arg, '/');
  139. if (slash)
  140. *slash = '\0';
  141. if (get_addr_1(dst, arg, family) == 0) {
  142. dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
  143. if (slash) {
  144. unsigned plen;
  145. inet_prefix netmask_pfx;
  146. netmask_pfx.family = AF_UNSPEC;
  147. plen = bb_strtou(slash + 1, NULL, 0);
  148. if ((errno || plen > dst->bitlen)
  149. && get_addr_1(&netmask_pfx, slash + 1, family) != 0
  150. ) {
  151. goto bad;
  152. }
  153. if (netmask_pfx.family == AF_INET) {
  154. /* fill in prefix length of dotted quad */
  155. uint32_t mask = ntohl(netmask_pfx.data[0]);
  156. uint32_t inv = ~mask;
  157. /* a valid netmask must be 11..10..00 */
  158. if (inv & (inv + 1))
  159. goto bad; /* inv is not 00..01..11 */
  160. plen = bb_popcnt_32(mask);
  161. if (plen > dst->bitlen)
  162. goto bad;
  163. /* dst->flags |= PREFIXLEN_SPECIFIED; */
  164. }
  165. dst->bitlen = plen;
  166. }
  167. }
  168. if (slash)
  169. *slash = '/';
  170. return;
  171. bad:
  172. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
  173. }
  174. int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family)
  175. {
  176. if (family == AF_PACKET) {
  177. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
  178. }
  179. if (get_addr_1(dst, arg, family)) {
  180. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
  181. }
  182. return 0;
  183. }
  184. void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family)
  185. {
  186. if (family == AF_PACKET) {
  187. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
  188. }
  189. get_prefix_1(dst, arg, family);
  190. }
  191. uint32_t FAST_FUNC get_addr32(char *name)
  192. {
  193. inet_prefix addr;
  194. if (get_addr_1(&addr, name, AF_INET)) {
  195. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
  196. }
  197. return addr.data[0];
  198. }
  199. char** FAST_FUNC next_arg(char **argv)
  200. {
  201. if (!*++argv)
  202. bb_simple_error_msg_and_die("command line is not complete, try \"help\"");
  203. return argv;
  204. }
  205. void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt)
  206. {
  207. bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
  208. }
  209. void FAST_FUNC duparg(const char *key, const char *arg)
  210. {
  211. bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
  212. }
  213. void FAST_FUNC duparg2(const char *key, const char *arg)
  214. {
  215. bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
  216. }
  217. int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
  218. {
  219. const uint32_t *a1 = a->data;
  220. const uint32_t *a2 = b->data;
  221. int words = bits >> 5;
  222. bits &= 0x1f;
  223. if (words)
  224. if (memcmp(a1, a2, words << 2))
  225. return -1;
  226. if (bits) {
  227. uint32_t w1, w2;
  228. uint32_t mask;
  229. w1 = a1[words];
  230. w2 = a2[words];
  231. mask = htonl((0xffffffff) << (0x20 - bits));
  232. if ((w1 ^ w2) & mask)
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. const char* FAST_FUNC rt_addr_n2a(int af, void *addr)
  238. {
  239. switch (af) {
  240. case AF_INET:
  241. case AF_INET6:
  242. return inet_ntop(af, addr,
  243. auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
  244. );
  245. default:
  246. return "???";
  247. }
  248. }
  249. #ifdef RESOLVE_HOSTNAMES
  250. const char* FAST_FUNC format_host(int af, int len, void *addr)
  251. {
  252. if (resolve_hosts) {
  253. struct hostent *h_ent;
  254. if (len <= 0) {
  255. switch (af) {
  256. case AF_INET:
  257. len = 4;
  258. break;
  259. case AF_INET6:
  260. len = 16;
  261. break;
  262. default:;
  263. }
  264. }
  265. if (len > 0) {
  266. h_ent = gethostbyaddr(addr, len, af);
  267. if (h_ent != NULL) {
  268. return auto_string(xstrdup(h_ent->h_name));
  269. }
  270. }
  271. }
  272. return rt_addr_n2a(af, addr);
  273. }
  274. #endif