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