utils.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef __NETIFD_UTILS_H
  15. #define __NETIFD_UTILS_H
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <uci_blob.h>
  19. #include <libubox/list.h>
  20. #include <libubox/avl.h>
  21. #include <libubox/avl-cmp.h>
  22. #include <libubox/blobmsg.h>
  23. #include <libubox/vlist.h>
  24. #include <libubox/utils.h>
  25. static inline bool blobmsg_get_bool_default(struct blob_attr *attr, bool val)
  26. {
  27. if (!attr)
  28. return val;
  29. return blobmsg_get_bool(attr);
  30. }
  31. #define __init __attribute__((constructor))
  32. struct vlist_simple_tree {
  33. struct list_head list;
  34. int head_offset;
  35. int version;
  36. };
  37. struct vlist_simple_node {
  38. struct list_head list;
  39. int version;
  40. };
  41. #define vlist_for_each_element_safe(tree, element, node_member, ptr) \
  42. avl_for_each_element_safe(&(tree)->avl, element, node_member.avl, ptr)
  43. #define vlist_simple_init(tree, node, member) \
  44. __vlist_simple_init(tree, offsetof(node, member))
  45. void __vlist_simple_init(struct vlist_simple_tree *tree, int offset);
  46. void vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node);
  47. void vlist_simple_flush(struct vlist_simple_tree *tree);
  48. void vlist_simple_flush_all(struct vlist_simple_tree *tree);
  49. void vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old);
  50. static inline void vlist_simple_update(struct vlist_simple_tree *tree)
  51. {
  52. tree->version++;
  53. }
  54. static inline void vlist_simple_add(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
  55. {
  56. node->version = tree->version;
  57. list_add_tail(&node->list, &tree->list);
  58. }
  59. #define vlist_simple_for_each_element(tree, element, node_member) \
  60. list_for_each_entry(element, &(tree)->list, node_member.list)
  61. #define vlist_simple_empty(tree) \
  62. list_empty(&(tree)->list)
  63. #ifdef __linux__
  64. static inline int fls(int x)
  65. {
  66. int r = 32;
  67. if (!x)
  68. return 0;
  69. if (!(x & 0xffff0000u)) {
  70. x <<= 16;
  71. r -= 16;
  72. }
  73. if (!(x & 0xff000000u)) {
  74. x <<= 8;
  75. r -= 8;
  76. }
  77. if (!(x & 0xf0000000u)) {
  78. x <<= 4;
  79. r -= 4;
  80. }
  81. if (!(x & 0xc0000000u)) {
  82. x <<= 2;
  83. r -= 2;
  84. }
  85. if (!(x & 0x80000000u))
  86. r -= 1;
  87. return r;
  88. }
  89. #endif
  90. unsigned int parse_netmask_string(const char *str, bool v6);
  91. bool split_netmask(char *str, unsigned int *netmask, bool v6);
  92. int parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask);
  93. bool check_pid_path(int pid, const char *exe);
  94. char * format_macaddr(uint8_t *mac);
  95. uint32_t crc32_file(FILE *fp);
  96. const char * uci_get_validate_string(const struct uci_blob_param_list *p, int i);
  97. #ifdef __APPLE__
  98. #define s6_addr32 __u6_addr.__u6_addr32
  99. #endif
  100. #endif