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