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