utils.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 host = ~mask;
  157. /* a valid netmask must be 2^n - 1 */
  158. if (host & (host + 1))
  159. goto bad;
  160. for (plen = 0; mask; mask <<= 1)
  161. ++plen;
  162. if (plen > dst->bitlen)
  163. goto bad;
  164. /* dst->flags |= PREFIXLEN_SPECIFIED; */
  165. }
  166. dst->bitlen = plen;
  167. }
  168. }
  169. if (slash)
  170. *slash = '/';
  171. return;
  172. bad:
  173. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
  174. }
  175. int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family)
  176. {
  177. if (family == AF_PACKET) {
  178. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
  179. }
  180. if (get_addr_1(dst, arg, family)) {
  181. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
  182. }
  183. return 0;
  184. }
  185. void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family)
  186. {
  187. if (family == AF_PACKET) {
  188. bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
  189. }
  190. get_prefix_1(dst, arg, family);
  191. }
  192. uint32_t FAST_FUNC get_addr32(char *name)
  193. {
  194. inet_prefix addr;
  195. if (get_addr_1(&addr, name, AF_INET)) {
  196. bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
  197. }
  198. return addr.data[0];
  199. }
  200. char** FAST_FUNC next_arg(char **argv)
  201. {
  202. if (!*++argv)
  203. bb_error_msg_and_die("command line is not complete, try \"help\"");
  204. return argv;
  205. }
  206. void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt)
  207. {
  208. bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
  209. }
  210. void FAST_FUNC duparg(const char *key, const char *arg)
  211. {
  212. bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
  213. }
  214. void FAST_FUNC duparg2(const char *key, const char *arg)
  215. {
  216. bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
  217. }
  218. int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
  219. {
  220. const uint32_t *a1 = a->data;
  221. const uint32_t *a2 = b->data;
  222. int words = bits >> 5;
  223. bits &= 0x1f;
  224. if (words)
  225. if (memcmp(a1, a2, words << 2))
  226. return -1;
  227. if (bits) {
  228. uint32_t w1, w2;
  229. uint32_t mask;
  230. w1 = a1[words];
  231. w2 = a2[words];
  232. mask = htonl((0xffffffff) << (0x20 - bits));
  233. if ((w1 ^ w2) & mask)
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. const char* FAST_FUNC rt_addr_n2a(int af, void *addr)
  239. {
  240. switch (af) {
  241. case AF_INET:
  242. case AF_INET6:
  243. return inet_ntop(af, addr,
  244. auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
  245. );
  246. default:
  247. return "???";
  248. }
  249. }
  250. #ifdef RESOLVE_HOSTNAMES
  251. const char* FAST_FUNC format_host(int af, int len, void *addr)
  252. {
  253. if (resolve_hosts) {
  254. struct hostent *h_ent;
  255. if (len <= 0) {
  256. switch (af) {
  257. case AF_INET:
  258. len = 4;
  259. break;
  260. case AF_INET6:
  261. len = 16;
  262. break;
  263. default:;
  264. }
  265. }
  266. if (len > 0) {
  267. h_ent = gethostbyaddr(addr, len, af);
  268. if (h_ent != NULL) {
  269. return auto_string(xstrdup(h_ent->h_name));
  270. }
  271. }
  272. }
  273. return rt_addr_n2a(af, addr);
  274. }
  275. #endif