forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairscan-netif.c
410 lines (343 loc) · 9.65 KB
/
airscan-netif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Network interfaces addresses
*/
#include "airscan.h"
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <sys/socket.h>
/* Forward declarations */
static netif_addr*
netif_addr_list_sort (netif_addr *list);
/* Get list of network interfaces addresses
*/
netif_addr*
netif_addr_list_get (void)
{
struct ifaddrs *ifa, *ifp;
netif_addr *list = NULL, *addr;
if (getifaddrs(&ifa) < 0) {
return NULL;
}
for (ifp = ifa; ifp != NULL; ifp = ifp->ifa_next) {
/* Skip interface without address */
if (ifp->ifa_addr == NULL) {
continue;
}
/* Skip loopback interface */
if ((ifp->ifa_flags & IFF_LOOPBACK) != 0) {
continue;
}
/* Obtain interface index. Skip address, if it failed */
int idx = if_nametoindex(ifp->ifa_name);
if (idx <= 0) {
continue;
}
/* Translate struct ifaddrs to netif_addr */
addr = g_new0(netif_addr, 1);
addr->next = list;
addr->ifindex = idx;
strncpy(addr->ifname.text, ifp->ifa_name,
sizeof(addr->ifname.text) - 1);
switch (ifp->ifa_addr->sa_family) {
case AF_INET:
addr->ip.v4 = ((struct sockaddr_in*) ifp->ifa_addr)->sin_addr;
inet_ntop(AF_INET, &addr->ip.v4,
addr->straddr, sizeof(addr->straddr));
break;
case AF_INET6:
addr->ipv6 = true;
addr->ip.v6 = ((struct sockaddr_in6*) ifp->ifa_addr)->sin6_addr;
inet_ntop(AF_INET6, &addr->ip.v6,
addr->straddr, sizeof(addr->straddr));
break;
default:
/* Paranoia; should not actually happen */
g_free(addr);
addr = NULL;
break;
}
if (addr != NULL) {
addr->next = list;
list = addr;
}
}
freeifaddrs(ifa);
return netif_addr_list_sort(list);
}
/* Free a single netif_addr
*/
static void
netif_addr_free_single (netif_addr *addr)
{
g_free(addr);
}
/* Free list of network interfaces addresses
*/
void
netif_addr_list_free (netif_addr *list)
{
while (list != NULL) {
netif_addr *next = list->next;
netif_addr_free_single(list);
list = next;
}
}
/* Compare two netif_addr addresses, for sorting
*/
static int
netif_addr_cmp (netif_addr *a1, netif_addr *a2)
{
bool ll1, ll2;
/* Compare interface indices */
if (a1->ifindex != a2->ifindex) {
return a1->ifindex - a2->ifindex;
}
/* Prefer normal addresses, rather that link-local */
ll1 = ip_is_linklocal(a1->ipv6 ? AF_INET6 : AF_INET, &a1->ip);
ll2 = ip_is_linklocal(a2->ipv6 ? AF_INET6 : AF_INET, &a2->ip);
if (ll1 != ll2) {
return ll1 ? 1 : -1;
}
/* Be in trend: prefer IPv6 addresses */
if (a1->ipv6 != a2->ipv6) {
return (int) a2->ipv6 - (int) a1->ipv6;
}
/* Otherwise, sort lexicographically */
return strcmp(a1->straddr, a2->straddr);
}
/* Revert netif_addr list
*/
static netif_addr*
netif_addr_list_revert (netif_addr *list)
{
netif_addr *prev = NULL, *next;
while (list != NULL) {
next = list->next;
list->next = prev;
prev = list;
list = next;
}
return prev;
}
/* Sort list of addresses
*/
static netif_addr*
netif_addr_list_sort (netif_addr *list)
{
netif_addr *halves[2] = {NULL, NULL};
int half = 0;
if (list == NULL || list->next == NULL) {
return list;
}
/* Split list into halves */
while (list != NULL) {
netif_addr *next = list->next;
list->next = halves[half];
halves[half] = list;
half ^= 1;
list = next;
}
/* Sort each half, recursively */
for (half = 0; half < 2; half ++) {
halves[half] = netif_addr_list_sort(halves[half]);
}
/* Now merge the sorted halves */
list = NULL;
while (halves[0] != NULL || halves[1] != NULL) {
netif_addr *next;
if (halves[0] == NULL) {
half = 1;
} else if (halves[1] == NULL) {
half = 0;
} else if (netif_addr_cmp(halves[0], halves[1]) < 0) {
half = 0;
} else {
half = 1;
}
next = halves[half]->next;
halves[half]->next = list;
list = halves[half];
halves[half] = next;
}
/* And revert the list, as after merging it is reverted */
return netif_addr_list_revert(list);
}
/* Compute a difference between two lists of addresses.
*
* It works by tossing nodes between 3 output lists:
* * if node is present in list2 only, it is moved
* to netif_diff.added
* * if node is present in list1 only, it is moved
* to netif_diff.removed
* * if node is present in both lists, node from
* list1 is moved to preserved, and node from
* list2 is released
*
* It assumes, both lists are sorted, as returned
* by netif_addr_get(). Returned lists are also sorted
*/
netif_diff
netif_diff_compute (netif_addr *list1, netif_addr *list2)
{
netif_diff diff = {NULL, NULL, NULL};
while (list1 != NULL || list2 != NULL) {
netif_addr *addr;
int cmp;
if (list1 == NULL) {
cmp = 1;
} else if (list2 == NULL) {
cmp = -1;
} else {
cmp = netif_addr_cmp(list1, list2);
}
if (cmp < 0) {
addr = list1;
list1 = list1->next;
addr->next = diff.removed;
diff.removed = addr;
} else if (cmp > 0) {
addr = list2;
list2 = list2->next;
addr->next = diff.added;
diff.added = addr;
} else {
addr = list1;
list1 = list1->next;
addr->next = diff.preserved;
diff.preserved = addr;
addr = list2;
list2 = list2->next;
netif_addr_free_single(addr);
}
}
diff.added = netif_addr_list_revert(diff.added);
diff.removed = netif_addr_list_revert(diff.removed);
diff.preserved = netif_addr_list_revert(diff.preserved);
return diff;
}
/* Merge two lists of addresses
*
* Input lists are consumed and new list is created.
*
* Input lists are assumed to be sorted, and output
* list will be sorted as well
*/
netif_addr*
netif_addr_list_merge (netif_addr *list1, netif_addr *list2)
{
netif_addr *list = NULL;
while (list1 != NULL || list2 != NULL) {
netif_addr *addr;
int cmp;
if (list1 == NULL) {
cmp = 1;
} else if (list2 == NULL) {
cmp = -1;
} else {
cmp = netif_addr_cmp(list1, list2);
}
if (cmp < 0) {
addr = list1;
list1 = list1->next;
} else {
addr = list2;
list2 = list2->next;
}
addr->next = list;
list = addr;
}
return netif_addr_list_revert(list);
}
/* Network interfaces addresses change notifier
*/
struct netif_notifier {
int rtnetlink; /* rtnetlink socket */
eloop_fdpoll *fdpoll; /* fdpoll for rtnetlink */
void (*callback)(void*); /* Notification callback */
void *data; /* Callback data */
uint8_t buf[16384]; /* Input buffer */
};
/* netif_notifier read callback
*/
static void
netif_notifier_read_callback (int fd, void *data, ELOOP_FDPOLL_MASK mask)
{
netif_notifier *notifier = (netif_notifier*) data;
int rc = read(fd, notifier->buf, sizeof(notifier->buf));
struct nlmsghdr *p;
size_t sz;
(void) mask;
/* Parse rtnetlink message */
if (rc < 0) {
return;
}
sz = (size_t) rc;
for (p = (struct nlmsghdr*) notifier->buf;
sz >= sizeof(struct nlmsghdr); p = NLMSG_NEXT(p, sz)) {
if (!NLMSG_OK(p, sz) || sz < p->nlmsg_len) {
return;
}
switch (p->nlmsg_type) {
case NLMSG_DONE:
return;
case RTM_NEWADDR:
case RTM_DELADDR:
notifier->callback(notifier->data);
return;
}
}
}
/* Create netif_notifier
*/
netif_notifier*
netif_notifier_create (void (*callback) (void*), void *data)
{
int rtnetlink, rc;
struct sockaddr_nl addr;
netif_notifier *notifier;
/* Open rtnetlink socket */
rtnetlink = socket(AF_NETLINK,
SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, NETLINK_ROUTE);
if (rtnetlink < 0) {
return NULL;
}
/* Subscribe to notifications */
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
rc = bind(rtnetlink, (struct sockaddr*) &addr, sizeof(addr));
if (rc < 0) {
close(rtnetlink);
return NULL;
}
/* Create netif_notifier structure */
notifier = g_new0(netif_notifier, 1);
notifier->rtnetlink = rtnetlink;
notifier->callback = callback;
notifier->data = data;
/* Register in event loop */
notifier->fdpoll = eloop_fdpoll_new(rtnetlink,
netif_notifier_read_callback, notifier);
eloop_fdpoll_set_mask(notifier->fdpoll, ELOOP_FDPOLL_READ);
return notifier;
}
/* Destroy netif_notifier
*/
void
netif_notifier_free (netif_notifier *notifier)
{
eloop_fdpoll_free(notifier->fdpoll);
close(notifier->rtnetlink);
g_free(notifier);
}
/* vim:ts=8:sw=4:et
*/