utils.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "utils.h"
  17. #include <arpa/inet.h>
  18. #include <netinet/in.h>
  19. void
  20. __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
  21. {
  22. INIT_LIST_HEAD(&tree->list);
  23. tree->version = 1;
  24. tree->head_offset = offset;
  25. }
  26. void
  27. vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
  28. {
  29. char *ptr;
  30. list_del(&node->list);
  31. ptr = (char *) node - tree->head_offset;
  32. free(ptr);
  33. }
  34. void
  35. vlist_simple_flush(struct vlist_simple_tree *tree)
  36. {
  37. struct vlist_simple_node *n, *tmp;
  38. list_for_each_entry_safe(n, tmp, &tree->list, list) {
  39. if ((n->version == tree->version || n->version == -1) &&
  40. tree->version != -1)
  41. continue;
  42. vlist_simple_delete(tree, n);
  43. }
  44. }
  45. void
  46. vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old)
  47. {
  48. struct vlist_simple_node *n, *tmp;
  49. vlist_simple_update(dest);
  50. list_for_each_entry_safe(n, tmp, &old->list, list) {
  51. list_del(&n->list);
  52. vlist_simple_add(dest, n);
  53. }
  54. vlist_simple_flush(dest);
  55. }
  56. void
  57. vlist_simple_flush_all(struct vlist_simple_tree *tree)
  58. {
  59. tree->version = -1;
  60. vlist_simple_flush(tree);
  61. }
  62. unsigned int
  63. parse_netmask_string(const char *str, bool v6)
  64. {
  65. struct in_addr addr;
  66. unsigned int ret;
  67. char *err = NULL;
  68. if (!strchr(str, '.')) {
  69. ret = strtoul(str, &err, 0);
  70. if (err && *err)
  71. goto error;
  72. return ret;
  73. }
  74. if (v6)
  75. goto error;
  76. if (inet_aton(str, &addr) != 1)
  77. goto error;
  78. return 32 - fls(~(ntohl(addr.s_addr)));
  79. error:
  80. return ~0;
  81. }
  82. bool
  83. split_netmask(char *str, unsigned int *netmask, bool v6)
  84. {
  85. char *delim = strchr(str, '/');
  86. if (delim) {
  87. *(delim++) = 0;
  88. *netmask = parse_netmask_string(delim, v6);
  89. }
  90. return true;
  91. }
  92. int
  93. parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
  94. {
  95. char *astr = alloca(strlen(str) + 1);
  96. strcpy(astr, str);
  97. if (!split_netmask(astr, netmask, af == AF_INET6))
  98. return 0;
  99. if (af == AF_INET6) {
  100. if (*netmask > 128)
  101. return 0;
  102. } else {
  103. if (*netmask > 32)
  104. return 0;
  105. }
  106. return inet_pton(af, astr, addr);
  107. }
  108. char *
  109. format_macaddr(uint8_t *mac)
  110. {
  111. static char str[sizeof("ff:ff:ff:ff:ff:ff ")];
  112. snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
  113. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  114. return str;
  115. }
  116. uint32_t
  117. crc32_file(FILE *fp)
  118. {
  119. static uint32_t *crcvals = NULL;
  120. if (!crcvals) {
  121. crcvals = malloc(sizeof(*crcvals) * 256);
  122. for (size_t i = 0; i < 256; ++i) {
  123. uint32_t c = i;
  124. for (size_t j = 0; j < 8; ++j)
  125. c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
  126. crcvals[i] = c;
  127. }
  128. }
  129. uint8_t buf[1024];
  130. size_t len;
  131. uint32_t c = 0xFFFFFFFF;
  132. do {
  133. len = fread(buf, 1, sizeof(buf), fp);
  134. for (size_t i = 0; i < len; ++i)
  135. c = crcvals[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
  136. } while (len == sizeof(buf));
  137. return c ^ 0xFFFFFFFF;
  138. }