chiark / gitweb /
6b2c83f91eca307f2e49d42752c81f6c96a9a31f
[elogind.git] / ifconf.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #ifndef fooifconfhfoo
4 #define fooifconfhfoo
5
6 #include <sys/socket.h>
7
8 /***
9   This file is part of nss-myhostname.
10
11   Copyright 2008-2011 Lennart Poettering
12
13   nss-myhostname is free software; you can redistribute it and/or
14   modify it under the terms of the GNU Lesser General Public License
15   as published by the Free Software Foundation; either version 2.1 of
16   the License, or (at your option) any later version.
17
18   nss-myhostname is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21   Lesser General Public License for more details.
22
23   You should have received a copy of the GNU Lesser General Public
24   License along with nss-myhostname; If not, see
25   <http://www.gnu.org/licenses/>.
26 ***/
27
28 #include <inttypes.h>
29 #include <sys/types.h>
30 #include <assert.h>
31
32 struct address {
33         unsigned char family;
34         uint8_t address[16];
35         unsigned char scope;
36         int ifindex;
37 };
38
39 #define _public_ __attribute__ ((visibility("default")))
40 #define _hidden_ __attribute__ ((visibility("hidden")))
41
42 int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) _hidden_;
43
44 static inline size_t PROTO_ADDRESS_SIZE(int proto) {
45         assert(proto == AF_INET || proto == AF_INET6);
46
47         return proto == AF_INET6 ? 16 : 4;
48 }
49
50 static inline int address_compare(const void *_a, const void *_b) {
51         const struct address *a = _a, *b = _b;
52
53         /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
54
55         if (a->scope < b->scope)
56                 return -1;
57         if (a->scope > b->scope)
58                 return 1;
59
60         if (a->family == AF_INET && b->family == AF_INET6)
61                 return -1;
62         if (a->family == AF_INET6 && b->family == AF_INET)
63                 return 1;
64
65         if (a->ifindex < b->ifindex)
66                 return -1;
67         if (a->ifindex > b->ifindex)
68                 return 1;
69
70         return 0;
71 }
72
73
74 #endif