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