system-linux: Replace device_get by device_find where appropriate
[project/netifd.git] / utils.c
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 #include <string.h>
15 #include <stdlib.h>
16 #include "utils.h"
17
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
20 #include <sys/socket.h>
21
22 #ifdef __APPLE__
23 #include <libproc.h>
24 #endif
25
26 void
27 __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
28 {
29 INIT_LIST_HEAD(&tree->list);
30 tree->version = 1;
31 tree->head_offset = offset;
32 }
33
34 void
35 vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
36 {
37 char *ptr;
38
39 list_del(&node->list);
40 ptr = (char *) node - tree->head_offset;
41 free(ptr);
42 }
43
44 void
45 vlist_simple_flush(struct vlist_simple_tree *tree)
46 {
47 struct vlist_simple_node *n, *tmp;
48
49 list_for_each_entry_safe(n, tmp, &tree->list, list) {
50 if ((n->version == tree->version || n->version == -1) &&
51 tree->version != -1)
52 continue;
53
54 vlist_simple_delete(tree, n);
55 }
56 }
57
58 void
59 vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old)
60 {
61 struct vlist_simple_node *n, *tmp;
62
63 vlist_simple_update(dest);
64 list_for_each_entry_safe(n, tmp, &old->list, list) {
65 list_del(&n->list);
66 vlist_simple_add(dest, n);
67 }
68 vlist_simple_flush(dest);
69 }
70
71 void
72 vlist_simple_flush_all(struct vlist_simple_tree *tree)
73 {
74 tree->version = -1;
75 vlist_simple_flush(tree);
76 }
77
78 unsigned int
79 parse_netmask_string(const char *str, bool v6)
80 {
81 struct in_addr addr;
82 unsigned int ret;
83 char *err = NULL;
84
85 if (!strchr(str, '.')) {
86 ret = strtoul(str, &err, 0);
87 if (err && *err)
88 goto error;
89
90 return ret;
91 }
92
93 if (v6)
94 goto error;
95
96 if (inet_aton(str, &addr) != 1)
97 goto error;
98
99 return 32 - fls(~(ntohl(addr.s_addr)));
100
101 error:
102 return ~0;
103 }
104
105 bool
106 split_netmask(char *str, unsigned int *netmask, bool v6)
107 {
108 char *delim = strchr(str, '/');
109
110 if (delim) {
111 *(delim++) = 0;
112
113 *netmask = parse_netmask_string(delim, v6);
114 }
115 return true;
116 }
117
118 int
119 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
120 {
121 char *astr = alloca(strlen(str) + 1);
122 int ret = 0;
123
124 strcpy(astr, str);
125 if (!split_netmask(astr, netmask, af == AF_INET6))
126 return 0;
127
128 if (af == AF_INET6) {
129 if (*netmask > 128)
130 return 0;
131 } else {
132 if (*netmask > 32)
133 return 0;
134 }
135
136 ret = inet_pton(af, astr, addr);
137 if (ret > 0) {
138 if (af == AF_INET) {
139 struct in_addr *ip4_addr = (struct in_addr *)addr;
140 uint32_t host_addr = ntohl(ip4_addr->s_addr);
141
142 if (IN_EXPERIMENTAL(host_addr)) {
143 return 0;
144 }
145 }
146 else if (af == AF_INET6) {
147 if (IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) {
148 return 0;
149 }
150 }
151 }
152 return ret;
153 }
154
155 char *
156 format_macaddr(uint8_t *mac)
157 {
158 static char str[sizeof("ff:ff:ff:ff:ff:ff ")];
159
160 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
161 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
162
163 return str;
164 }
165
166 uint32_t
167 crc32_file(FILE *fp)
168 {
169 static uint32_t *crcvals = NULL;
170 if (!crcvals) {
171 crcvals = malloc(sizeof(*crcvals) * 256);
172
173 for (size_t i = 0; i < 256; ++i) {
174 uint32_t c = i;
175 for (size_t j = 0; j < 8; ++j)
176 c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
177 crcvals[i] = c;
178 }
179 }
180
181 uint8_t buf[1024];
182 size_t len;
183 uint32_t c = 0xFFFFFFFF;
184
185 do {
186 len = fread(buf, 1, sizeof(buf), fp);
187 for (size_t i = 0; i < len; ++i)
188 c = crcvals[(c ^ buf[i]) & 0xFF] ^ (c >> 8);
189 } while (len == sizeof(buf));
190
191 return c ^ 0xFFFFFFFF;
192 }
193
194 bool check_pid_path(int pid, const char *exe)
195 {
196 int proc_exe_len;
197 int exe_len = strlen(exe);
198
199 #ifdef __APPLE__
200 char proc_exe_buf[PROC_PIDPATHINFO_SIZE];
201
202 proc_exe_len = proc_pidpath(pid, proc_exe_buf, sizeof(proc_exe_buf));
203 #else
204 char proc_exe[32];
205 char *proc_exe_buf = alloca(exe_len);
206
207 sprintf(proc_exe, "/proc/%d/exe", pid);
208 proc_exe_len = readlink(proc_exe, proc_exe_buf, exe_len);
209 #endif
210
211 if (proc_exe_len != exe_len)
212 return false;
213
214 return !memcmp(exe, proc_exe_buf, exe_len);
215 }
216
217 static const char * const uci_validate_name[__BLOBMSG_TYPE_LAST] = {
218 [BLOBMSG_TYPE_STRING] = "string",
219 [BLOBMSG_TYPE_ARRAY] = "list(string)",
220 [BLOBMSG_TYPE_INT32] = "uinteger",
221 [BLOBMSG_TYPE_BOOL] = "bool",
222 };
223
224 const char*
225 uci_get_validate_string(const struct uci_blob_param_list *p, int i)
226 {
227 if (p->validate[i])
228 return p->validate[i];
229
230 else if (uci_validate_name[p->params[i].type])
231 return uci_validate_name[p->params[i].type];
232
233 return p->validate[BLOBMSG_TYPE_STRING];
234 }