chiark / gitweb /
update TODO
[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(sd_rtnl *context, int ifindex, 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         if (context)
62                 rtnl = sd_rtnl_ref(context);
63         else {
64                 r = sd_rtnl_open(&rtnl, 0);
65                 if (r < 0)
66                         return r;
67         }
68
69         r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
70         if (r < 0)
71                 return r;
72
73         r = sd_rtnl_call(rtnl, req, 0, &reply);
74         if (r < 0)
75                 return r;
76
77         for (m = reply; m; m = sd_rtnl_message_next(m)) {
78                 struct local_address *a;
79                 unsigned char flags;
80                 uint16_t type;
81                 int ifi;
82
83                 r = sd_rtnl_message_get_errno(m);
84                 if (r < 0)
85                         return r;
86
87                 r = sd_rtnl_message_get_type(m, &type);
88                 if (r < 0)
89                         return r;
90
91                 if (type != RTM_NEWADDR)
92                         continue;
93
94                 r = sd_rtnl_message_addr_get_ifindex(m, &ifi);
95                 if (r < 0)
96                         return r;
97
98                 if (ifindex != 0 && ifi != ifindex)
99                         continue;
100
101                 r = sd_rtnl_message_addr_get_flags(m, &flags);
102                 if (r < 0)
103                         return r;
104
105                 if (flags & IFA_F_DEPRECATED)
106                         continue;
107
108                 if (!GREEDY_REALLOC(list, n_allocated, n_list+1))
109                         return -ENOMEM;
110
111                 a = list + n_list;
112
113                 r = sd_rtnl_message_addr_get_scope(m, &a->scope);
114                 if (r < 0)
115                         return r;
116
117                 if (ifindex == 0 && (a->scope == RT_SCOPE_HOST || a->scope == RT_SCOPE_NOWHERE))
118                         continue;
119
120                 r = sd_rtnl_message_addr_get_family(m, &a->family);
121                 if (r < 0)
122                         return r;
123
124                 switch (a->family) {
125
126                 case AF_INET:
127                         r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &a->address.in);
128                         if (r < 0) {
129                                 r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &a->address.in);
130                                 if (r < 0)
131                                         continue;
132                         }
133                         break;
134
135                 case AF_INET6:
136                         r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6);
137                         if (r < 0) {
138                                 r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6);
139                                 if (r < 0)
140                                         continue;
141                         }
142                         break;
143
144                 default:
145                         continue;
146                 }
147
148                 a->ifindex = ifi;
149
150                 n_list++;
151         };
152
153         if (n_list)
154                 qsort(list, n_list, sizeof(struct local_address), address_compare);
155
156         *ret = list;
157         list = NULL;
158
159         return (int) n_list;
160 }