1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2008-2011 Lennart Poettering
7 Copyright 2014 Tom Gundersen
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.
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.
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/>.
24 #include "rtnl-util.h"
29 int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) {
30 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
31 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
33 _cleanup_free_ struct address *list = NULL;
34 struct address *new_list = NULL;
38 r = sd_rtnl_open(&rtnl, 0);
42 r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
46 r = sd_rtnl_call(rtnl, req, 0, &reply);
62 r = sd_rtnl_message_get_errno(m);
66 r = sd_rtnl_message_get_type(m, &type);
70 if (type != RTM_NEWADDR)
73 r = sd_rtnl_message_addr_get_scope(m, &scope);
77 if (scope == RT_SCOPE_HOST || scope == RT_SCOPE_NOWHERE)
80 r = sd_rtnl_message_addr_get_flags(m, &flags);
84 if (flags & IFA_F_DEPRECATED)
87 r = sd_rtnl_message_addr_get_family(m, &family);
93 r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &address.in);
95 r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &address.in);
101 r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &address.in6);
103 r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &address.in6);
112 r = sd_rtnl_message_addr_get_ifindex(m, &ifindex);
116 new_list = realloc(list, (n_list+1) * sizeof(struct address));
122 assert_cc(sizeof(address) <= 16);
124 list[n_list].family = family;
125 list[n_list].scope = scope;
126 memcpy(list[n_list].address, &address, sizeof(address));
127 list[n_list].ifindex = ifindex;
131 } while ((m = sd_rtnl_message_next(m)));
134 qsort(list, n_list, sizeof(struct address), address_compare);