utils.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include <sys/socket.h>
  20. #ifdef __APPLE__
  21. #include <libproc.h>
  22. #endif
  23. void
  24. __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
  25. {
  26. INIT_LIST_HEAD(&tree->list);
  27. tree->version = 1;
  28. tree->head_offset = offset;
  29. }
  30. void
  31. vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
  32. {
  33. char *ptr;
  34. list_del(&node->list);
  35. ptr = (char *) node - tree->head_offset;
  36. free(ptr);
  37. }
  38. void
  39. vlist_simple_flush(struct vlist_simple_tree *tree)
  40. {
  41. struct vlist_simple_node *n, *tmp;
  42. list_for_each_entry_safe(n, tmp, &tree->list, list) {
  43. if ((n->version == tree->version || n->version == -1) &&
  44. tree->version != -1)
  45. continue;
  46. vlist_simple_delete(tree, n);
  47. }
  48. }
  49. void
  50. vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old)
  51. {
  52. struct vlist_simple_node *n, *tmp;
  53. vlist_simple_update(dest);
  54. list_for_each_entry_safe(n, tmp, &old->list, list) {
  55. list_del(&n->list);
  56. vlist_simple_add(dest, n);
  57. }
  58. vlist_simple_flush(dest);
  59. }
  60. void
  61. vlist_simple_flush_all(struct vlist_simple_tree *tree)
  62. {
  63. tree->version = -1;
  64. vlist_simple_flush(tree);
  65. }
  66. unsigned int
  67. parse_netmask_string(const char *str, bool v6)
  68. {
  69. struct in_addr addr;
  70. unsigned int ret;
  71. char *err = NULL;
  72. if (!strchr(str, '.')) {
  73. ret = strtoul(str, &err, 0);
  74. if (err && *err)
  75. goto error;
  76. return ret;
  77. }
  78. if (v6)
  79. goto error;
  80. if (inet_aton(str, &addr) != 1)
  81. goto error;
  82. return 32 - fls(~(ntohl(addr.s_addr)));
  83. error:
  84. return ~0;
  85. }
  86. bool
  87. split_netmask(char *str, unsigned int *netmask, bool v6)
  88. {
  89. char *delim = strchr(str, '/');
  90. if (delim) {
  91. *(delim++) = 0;
  92. *netmask = parse_netmask_string(delim, v6);
  93. }
  94. return true;
  95. }
  96. int
  97. parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
  98. {
  99. char *astr = alloca(strlen(str) + 1);
  100. strcpy(astr, str);
  101. if (!split_netmask(astr, netmask, af == AF_INET6))
  102. return 0;
  103. if (af == AF_INET6) {
  104. if (*netmask > 128)
  105. return 0;
  106. } else {
  107. if (*netmask > 32)
  108. return 0;
  109. }
  110. return inet_pton(af, astr, addr);
  111. }
  112. char *
  113. format_macaddr(uint8_t *mac)
  114. {
  115. static char str[sizeof("ff:ff:ff:ff:ff:ff ")];
  116. snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
  117. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  118. return str;
  119. }
  120. uint32_t
  121. crc32_file(FILE *fp)
  122. {
  123. static uint32_t *crcvals = NULL;
  124. if (!crcvals) {
  125. crcvals = malloc(sizeof(*crcvals) * 256);
  126. for (size_t i = 0; i < 256; ++i) {
  127. uint32_t c = i;
  128. for (size_t j = 0; j < 8; ++j)
  129. c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
  130. crcvals[i] = c;
  131. }
  132. }
  133. uint8_t buf[1024];
  134. size_t len;
  135. uint32_t c = 0xFFFFFFFF;
  136. do {
  137. len = fread(buf, 1, sizeof(buf), fp);
  138. for (size_t i = 0; i < len; ++i)
  139. c = crcvals[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
  140. } while (len == sizeof(buf));
  141. return c ^ 0xFFFFFFFF;
  142. }
  143. bool check_pid_path(int pid, const char *exe)
  144. {
  145. int proc_exe_len;
  146. int exe_len = strlen(exe);
  147. #ifdef __APPLE__
  148. char proc_exe_buf[PROC_PIDPATHINFO_SIZE];
  149. proc_exe_len = proc_pidpath(pid, proc_exe_buf, sizeof(proc_exe_buf));
  150. #else
  151. char proc_exe[32];
  152. char *proc_exe_buf = alloca(exe_len);
  153. sprintf(proc_exe, "/proc/%d/exe", pid);
  154. proc_exe_len = readlink(proc_exe, proc_exe_buf, exe_len);
  155. #endif
  156. if (proc_exe_len != exe_len)
  157. return false;
  158. return !memcmp(exe, proc_exe_buf, exe_len);
  159. }
  160. static const char * const uci_validate_name[__BLOBMSG_TYPE_LAST] = {
  161. [BLOBMSG_TYPE_STRING] = "string",
  162. [BLOBMSG_TYPE_ARRAY] = "list(string)",
  163. [BLOBMSG_TYPE_INT32] = "uinteger",
  164. [BLOBMSG_TYPE_BOOL] = "bool",
  165. };
  166. const char*
  167. uci_get_validate_string(const struct uci_blob_param_list *p, int i)
  168. {
  169. if (p->validate[i])
  170. return p->validate[i];
  171. else if (uci_validate_name[p->params[i].type])
  172. return uci_validate_name[p->params[i].type];
  173. return p->validate[BLOBMSG_TYPE_STRING];
  174. }