util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <sys/time.h>
  23. #include <time.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <limits.h>
  27. #include <assert.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include "babeld.h"
  33. #include "util.h"
  34. int
  35. roughly(int value)
  36. {
  37. if(value < 0)
  38. return -roughly(-value);
  39. else if(value <= 1)
  40. return value;
  41. else
  42. return value * 3 / 4 + random() % (value / 2);
  43. }
  44. void
  45. timeval_minus(struct timeval *d,
  46. const struct timeval *s1, const struct timeval *s2)
  47. {
  48. if(s1->tv_usec >= s2->tv_usec) {
  49. d->tv_usec = s1->tv_usec - s2->tv_usec;
  50. d->tv_sec = s1->tv_sec - s2->tv_sec;
  51. } else {
  52. d->tv_usec = s1->tv_usec + 1000000 - s2->tv_usec;
  53. d->tv_sec = s1->tv_sec - s2->tv_sec - 1;
  54. }
  55. }
  56. unsigned
  57. timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
  58. {
  59. if(s1->tv_sec < s2->tv_sec)
  60. return 0;
  61. /* Avoid overflow. */
  62. if(s1->tv_sec - s2->tv_sec > 2000000)
  63. return 2000000000;
  64. if(s1->tv_sec > s2->tv_sec)
  65. return
  66. (unsigned)((unsigned)(s1->tv_sec - s2->tv_sec) * 1000 +
  67. ((int)s1->tv_usec - s2->tv_usec) / 1000);
  68. if(s1->tv_usec <= s2->tv_usec)
  69. return 0;
  70. return (unsigned)(s1->tv_usec - s2->tv_usec) / 1000u;
  71. }
  72. void
  73. timeval_add_msec(struct timeval *d, const struct timeval *s, int msecs)
  74. {
  75. int usecs;
  76. d->tv_sec = s->tv_sec + msecs / 1000;
  77. usecs = s->tv_usec + (msecs % 1000) * 1000;
  78. if(usecs < 1000000) {
  79. d->tv_usec = usecs;
  80. } else {
  81. d->tv_usec = usecs - 1000000;
  82. d->tv_sec++;
  83. }
  84. }
  85. int
  86. timeval_compare(const struct timeval *s1, const struct timeval *s2)
  87. {
  88. if(s1->tv_sec < s2->tv_sec)
  89. return -1;
  90. else if(s1->tv_sec > s2->tv_sec)
  91. return 1;
  92. else if(s1->tv_usec < s2->tv_usec)
  93. return -1;
  94. else if(s1->tv_usec > s2->tv_usec)
  95. return 1;
  96. else
  97. return 0;
  98. }
  99. /* {0, 0} represents infinity */
  100. void
  101. timeval_min(struct timeval *d, const struct timeval *s)
  102. {
  103. if(s->tv_sec == 0)
  104. return;
  105. if(d->tv_sec == 0 || timeval_compare(d, s) > 0) {
  106. *d = *s;
  107. }
  108. }
  109. void
  110. timeval_min_sec(struct timeval *d, time_t secs)
  111. {
  112. if(d->tv_sec == 0 || d->tv_sec > secs) {
  113. d->tv_sec = secs;
  114. d->tv_usec = random() % 1000000;
  115. }
  116. }
  117. /* There's no good name for a positive int in C, call it nat. */
  118. int
  119. parse_nat(const char *string)
  120. {
  121. long l;
  122. char *end;
  123. l = strtol(string, &end, 0);
  124. while(*end == ' ' || *end == '\t')
  125. end++;
  126. if(*end != '\0')
  127. return -1;
  128. if(l < 0 || l > INT_MAX)
  129. return -1;
  130. return (int)l;
  131. }
  132. /* Given a fixed-point string such as "42.1337", returns 1000 times
  133. the value of the string, here 42133. */
  134. int
  135. parse_thousands(const char *string)
  136. {
  137. unsigned int in, fl;
  138. int i, j;
  139. in = fl = 0;
  140. i = 0;
  141. while(string[i] == ' ' || string[i] == '\t')
  142. i++;
  143. while(string[i] >= '0' && string[i] <= '9') {
  144. in = in * 10 + string[i] - '0';
  145. i++;
  146. }
  147. if(string[i] == '.') {
  148. i++;
  149. j = 0;
  150. while(string[i] >= '0' && string[i] <= '9') {
  151. fl = fl * 10 + string[i] - '0';
  152. i++;
  153. j++;
  154. }
  155. while(j > 3) {
  156. fl /= 10;
  157. j--;
  158. }
  159. while(j < 3) {
  160. fl *= 10;
  161. j++;
  162. }
  163. }
  164. while(string[i] == ' ' || string[i] == '\t')
  165. i++;
  166. if(string[i] == '\0')
  167. return in * 1000 + fl;
  168. return -1;
  169. }
  170. void
  171. do_debugf(int level, const char *format, ...)
  172. {
  173. va_list args;
  174. va_start(args, format);
  175. if(debug >= level) {
  176. vfprintf(stderr, format, args);
  177. fflush(stderr);
  178. }
  179. va_end(args);
  180. }
  181. int
  182. in_prefix(const unsigned char *restrict address,
  183. const unsigned char *restrict prefix, unsigned char plen)
  184. {
  185. unsigned char m;
  186. if(plen > 128)
  187. plen = 128;
  188. if(memcmp(address, prefix, plen / 8) != 0)
  189. return 0;
  190. if(plen % 8 == 0)
  191. return 1;
  192. m = 0xFF << (8 - (plen % 8));
  193. return ((address[plen / 8] & m) == (prefix[plen / 8] & m));
  194. }
  195. unsigned char *
  196. normalize_prefix(unsigned char *restrict ret,
  197. const unsigned char *restrict prefix, unsigned char plen)
  198. {
  199. if(plen >= 128) {
  200. memcpy(ret, prefix, 16);
  201. return ret;
  202. }
  203. memset(ret, 0, 16);
  204. memcpy(ret, prefix, plen / 8);
  205. if(plen % 8 != 0)
  206. ret[plen / 8] =
  207. (prefix[plen / 8] & ((0xFF << (8 - (plen % 8))) & 0xFF));
  208. return ret;
  209. }
  210. const unsigned char v4prefix[16] =
  211. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
  212. static const unsigned char llprefix[16] =
  213. {0xFE, 0x80};
  214. const char *
  215. format_address(const unsigned char *address)
  216. {
  217. static char buf[4][INET6_ADDRSTRLEN];
  218. static int i = 0;
  219. i = (i + 1) % 4;
  220. if(v4mapped(address))
  221. inet_ntop(AF_INET, address + 12, buf[i], INET6_ADDRSTRLEN);
  222. else
  223. inet_ntop(AF_INET6, address, buf[i], INET6_ADDRSTRLEN);
  224. return buf[i];
  225. }
  226. const char *
  227. format_prefix(const unsigned char *prefix, unsigned char plen)
  228. {
  229. static char buf[4][INET6_ADDRSTRLEN + 4];
  230. static int i = 0;
  231. int n;
  232. i = (i + 1) % 4;
  233. if(plen >= 96 && v4mapped(prefix)) {
  234. inet_ntop(AF_INET, prefix + 12, buf[i], INET6_ADDRSTRLEN);
  235. n = strlen(buf[i]);
  236. snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen - 96);
  237. } else {
  238. inet_ntop(AF_INET6, prefix, buf[i], INET6_ADDRSTRLEN);
  239. n = strlen(buf[i]);
  240. snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen);
  241. }
  242. return buf[i];
  243. }
  244. const char *
  245. format_eui64(const unsigned char *eui)
  246. {
  247. static char buf[4][28];
  248. static int i = 0;
  249. i = (i + 1) % 4;
  250. snprintf(buf[i], 28, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
  251. eui[0], eui[1], eui[2], eui[3],
  252. eui[4], eui[5], eui[6], eui[7]);
  253. return buf[i];
  254. }
  255. const char *
  256. format_thousands(unsigned int value)
  257. {
  258. static char buf[4][15];
  259. static int i = 0;
  260. i = (i + 1) % 4;
  261. snprintf(buf[i], 15, "%d.%.3d", value / 1000, value % 1000);
  262. return buf[i];
  263. }
  264. int
  265. parse_address(const char *address, unsigned char *addr_r, int *af_r)
  266. {
  267. struct in_addr ina;
  268. struct in6_addr ina6;
  269. int rc;
  270. rc = inet_pton(AF_INET, address, &ina);
  271. if(rc > 0) {
  272. memcpy(addr_r, v4prefix, 12);
  273. memcpy(addr_r + 12, &ina, 4);
  274. if(af_r) *af_r = AF_INET;
  275. return 0;
  276. }
  277. rc = inet_pton(AF_INET6, address, &ina6);
  278. if(rc > 0) {
  279. memcpy(addr_r, &ina6, 16);
  280. if(af_r) *af_r = AF_INET6;
  281. return 0;
  282. }
  283. return -1;
  284. }
  285. int
  286. parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
  287. int *af_r)
  288. {
  289. char buf[INET6_ADDRSTRLEN];
  290. char *slash, *end;
  291. unsigned char prefix[16];
  292. long plen;
  293. int af;
  294. struct in_addr ina;
  295. struct in6_addr ina6;
  296. int rc;
  297. if(strcmp(net, "default") == 0) {
  298. memset(prefix, 0, 16);
  299. plen = 0;
  300. af = AF_INET6;
  301. } else {
  302. slash = strchr(net, '/');
  303. if(slash == NULL) {
  304. rc = parse_address(net, prefix, &af);
  305. if(rc < 0)
  306. return rc;
  307. plen = 128;
  308. } else {
  309. if(slash - net >= INET6_ADDRSTRLEN)
  310. return -1;
  311. memcpy(buf, net, slash - net);
  312. buf[slash - net] = '\0';
  313. rc = inet_pton(AF_INET, buf, &ina);
  314. if(rc > 0) {
  315. memcpy(prefix, v4prefix, 12);
  316. memcpy(prefix + 12, &ina, 4);
  317. plen = strtol(slash + 1, &end, 0);
  318. if(*end != '\0' || plen < 0 || plen > 32)
  319. return -1;
  320. plen += 96;
  321. af = AF_INET;
  322. } else {
  323. rc = inet_pton(AF_INET6, buf, &ina6);
  324. if(rc > 0) {
  325. memcpy(prefix, &ina6, 16);
  326. plen = strtol(slash + 1, &end, 0);
  327. if(*end != '\0' || plen < 0 || plen > 128)
  328. return -1;
  329. af = AF_INET6;
  330. } else {
  331. return -1;
  332. }
  333. }
  334. }
  335. }
  336. normalize_prefix(prefix_r, prefix, plen);
  337. *plen_r = plen;
  338. if(af_r) *af_r = af;
  339. return 0;
  340. }
  341. int
  342. parse_eui64(const char *eui, unsigned char *eui_r)
  343. {
  344. int n;
  345. n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  346. &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
  347. &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
  348. if(n == 8)
  349. return 0;
  350. n = sscanf(eui, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx",
  351. &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
  352. &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
  353. if(n == 8)
  354. return 0;
  355. n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  356. &eui_r[0], &eui_r[1], &eui_r[2],
  357. &eui_r[5], &eui_r[6], &eui_r[7]);
  358. if(n == 6) {
  359. eui_r[3] = 0xFF;
  360. eui_r[4] = 0xFE;
  361. return 0;
  362. }
  363. return -1;
  364. }
  365. int
  366. wait_for_fd(int direction, int fd, int msecs)
  367. {
  368. fd_set fds;
  369. int rc;
  370. struct timeval tv;
  371. tv.tv_sec = msecs / 1000;
  372. tv.tv_usec = (msecs % 1000) * 1000;
  373. FD_ZERO(&fds);
  374. FD_SET(fd, &fds);
  375. if(direction)
  376. rc = select(fd + 1, NULL, &fds, NULL, &tv);
  377. else
  378. rc = select(fd + 1, &fds, NULL, NULL, &tv);
  379. return rc;
  380. }
  381. int
  382. martian_prefix(const unsigned char *prefix, int plen)
  383. {
  384. return
  385. (plen >= 8 && prefix[0] == 0xFF) ||
  386. (plen >= 10 && prefix[0] == 0xFE && (prefix[1] & 0xC0) == 0x80) ||
  387. (plen >= 128 && memcmp(prefix, zeroes, 15) == 0 &&
  388. (prefix[15] == 0 || prefix[15] == 1)) ||
  389. (plen >= 96 && v4mapped(prefix) &&
  390. ((plen >= 104 && (prefix[12] == 127 || prefix[12] == 0)) ||
  391. (plen >= 100 && (prefix[12] & 0xE0) == 0xE0)));
  392. }
  393. int
  394. linklocal(const unsigned char *address)
  395. {
  396. return memcmp(address, llprefix, 8) == 0;
  397. }
  398. int
  399. v4mapped(const unsigned char *address)
  400. {
  401. return memcmp(address, v4prefix, 12) == 0;
  402. }
  403. void
  404. v4tov6(unsigned char *dst, const unsigned char *src)
  405. {
  406. memcpy(dst, v4prefix, 12);
  407. memcpy(dst + 12, src, 4);
  408. }
  409. int
  410. daemonise()
  411. {
  412. int rc;
  413. fflush(stdout);
  414. fflush(stderr);
  415. rc = fork();
  416. if(rc < 0)
  417. return -1;
  418. if(rc > 0)
  419. exit(0);
  420. rc = setsid();
  421. if(rc < 0)
  422. return -1;
  423. return 1;
  424. }
  425. enum prefix_status
  426. prefix_cmp(const unsigned char *p1, unsigned char plen1,
  427. const unsigned char *p2, unsigned char plen2)
  428. {
  429. int plen = MIN(plen1, plen2);
  430. if(v4mapped(p1) != v4mapped(p2))
  431. return PST_DISJOINT;
  432. if(memcmp(p1, p2, plen / 8) != 0)
  433. return PST_DISJOINT;
  434. if(plen % 8 != 0) {
  435. int i = plen / 8 + 1;
  436. unsigned char mask = (0xFF << (plen % 8)) & 0xFF;
  437. if((p1[i] & mask) != (p2[i] & mask))
  438. return PST_DISJOINT;
  439. }
  440. if(plen1 < plen2)
  441. return PST_LESS_SPECIFIC;
  442. else if(plen1 > plen2)
  443. return PST_MORE_SPECIFIC;
  444. else
  445. return PST_EQUALS;
  446. }