chiark / gitweb /
nss-myhostname: move local address listing logic into shared, so that we can make...
[elogind.git] / src / libsystemd / sd-rtnl / local-addresses.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2008-2011 Lennart Poettering
7   Copyright 2014 Tom Gundersen
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include "sd-rtnl.h"
24 #include "rtnl-util.h"
25 #include "macro.h"
26 #include "local-addresses.h"
27
28 static int address_compare(const void *_a, const void *_b) {
29         const struct local_address *a = _a, *b = _b;
30
31         /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
32
33         if (a->scope < b->scope)
34                 return -1;
35         if (a->scope > b->scope)
36                 return 1;
37
38         if (a->family == AF_INET && b->family == AF_INET6)
39                 return -1;
40         if (a->family == AF_INET6 && b->family == AF_INET)
41                 return 1;
42
43         if (a->ifindex < b->ifindex)
44                 return -1;
45         if (a->ifindex > b->ifindex)
46                 return 1;
47
48         return 0;
49 }
50
51 int local_addresses(struct local_address **ret) {
52         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
53         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
54         _cleanup_free_ struct local_address *list = NULL;
55         size_t n_list = 0, n_allocated = 0;
56         sd_rtnl_message *m;
57         int r;
58
59         assert(ret);
60
61         r = sd_rtnl_open(&rtnl, 0);
62         if (r < 0)
63                 return r;
64
65         r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
66         if (r < 0)
67                 return r;
68
69         r = sd_rtnl_call(rtnl, req, 0, &reply);
70         if (r < 0)
71                 return r;
72
73         for (m = reply; m; m = sd_rtnl_message_next(m)) {
74                 struct local_address *a;
75                 unsigned char flags;
76                 uint16_t type;
77
78                 r = sd_rtnl_message_get_errno(m);
79                 if (r < 0)
80                         return r;
81
82                 r = sd_rtnl_message_get_type(m, &type);
83                 if (r < 0)
84                         return r;
85
86                 if (type != RTM_NEWADDR)
87                         continue;
88
89                 r = sd_rtnl_message_addr_get_flags(m, &flags);
90                 if (r < 0)
91                         return r;
92
93                 if (flags & IFA_F_DEPRECATED)
94                         continue;
95
96                 if (!GREEDY_REALLOC(list, n_allocated, n_list+1))
97                         return -ENOMEM;
98
99                 a = list + n_list;
100
101                 r = sd_rtnl_message_addr_get_scope(m, &a->scope);
102                 if (r < 0)
103                         return r;
104
105                 if (a->scope == RT_SCOPE_HOST || a->scope == RT_SCOPE_NOWHERE)
106                         continue;
107
108                 r = sd_rtnl_message_addr_get_family(m, &a->family);
109                 if (r < 0)
110                         return r;
111
112                 switch (a->family) {
113
114                 case AF_INET:
115                         r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &a->address.in);
116                         if (r < 0) {
117                                 r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &a->address.in);
118                                 if (r < 0)
119                                         continue;
120                         }
121                         break;
122
123                 case AF_INET6:
124                         r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6);
125                         if (r < 0) {
126                                 r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6);
127                                 if (r < 0)
128                                         continue;
129                         }
130                         break;
131
132                 default:
133                         continue;
134                 }
135
136                 r = sd_rtnl_message_addr_get_ifindex(m, &a->ifindex);
137                 if (r < 0)
138                         return r;
139
140                 n_list++;
141         };
142
143         if (n_list)
144                 qsort(list, n_list, sizeof(struct local_address), address_compare);
145
146         *ret = list;
147         list = NULL;
148
149         return (int) n_list;
150 }