chiark / gitweb /
d75e850494374b99b11d2e38a040dd4a1b82ce32
[elogind.git] / src / nss-myhostname / 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 "addresses.h"
27
28 static int address_compare(const void *_a, const void *_b) {
29         const struct 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 acquire_addresses(struct address **_list, unsigned *_n_list) {
52         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
53         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
54         _cleanup_free_ struct address *list = NULL;
55         size_t n_list = 0, n_allocated = 0;
56         sd_rtnl_message *m;
57         int r;
58
59         r = sd_rtnl_open(&rtnl, 0);
60         if (r < 0)
61                 return r;
62
63         r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
64         if (r < 0)
65                 return r;
66
67         r = sd_rtnl_call(rtnl, req, 0, &reply);
68         if (r < 0)
69                 return r;
70
71         for (m = reply; m; m = sd_rtnl_message_next(m)) {
72                 struct address *a;
73                 unsigned char flags;
74                 uint16_t type;
75
76                 r = sd_rtnl_message_get_errno(m);
77                 if (r < 0)
78                         return r;
79
80                 r = sd_rtnl_message_get_type(m, &type);
81                 if (r < 0)
82                         return r;
83
84                 if (type != RTM_NEWADDR)
85                         continue;
86
87                 r = sd_rtnl_message_addr_get_flags(m, &flags);
88                 if (r < 0)
89                         return r;
90
91                 if (flags & IFA_F_DEPRECATED)
92                         continue;
93
94                 if (!GREEDY_REALLOC(list, n_allocated, n_list+1))
95                         return -ENOMEM;
96
97                 a = list + n_list;
98
99                 r = sd_rtnl_message_addr_get_scope(m, &a->scope);
100                 if (r < 0)
101                         return r;
102
103                 if (a->scope == RT_SCOPE_HOST || a->scope == RT_SCOPE_NOWHERE)
104                         continue;
105
106                 r = sd_rtnl_message_addr_get_family(m, &a->family);
107                 if (r < 0)
108                         return r;
109
110                 switch (a->family) {
111
112                 case AF_INET:
113                         r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &a->address.in);
114                         if (r < 0) {
115                                 r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &a->address.in);
116                                 if (r < 0)
117                                         continue;
118                         }
119                         break;
120
121                 case AF_INET6:
122                         r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6);
123                         if (r < 0) {
124                                 r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6);
125                                 if (r < 0)
126                                         continue;
127                         }
128                         break;
129
130                 default:
131                         continue;
132                 }
133
134                 r = sd_rtnl_message_addr_get_ifindex(m, &a->ifindex);
135                 if (r < 0)
136                         return r;
137
138                 n_list++;
139         };
140
141         if (n_list)
142                 qsort(list, n_list, sizeof(struct address), address_compare);
143
144         *_list = list;
145         list = NULL;
146         *_n_list = n_list;
147
148         return 0;
149 }