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