1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of nss-myhostname.
6 Copyright 2008-2011 Lennart Poettering
8 nss-myhostname is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 nss-myhostname is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with nss-myhostname; If not, see
20 <http://www.gnu.org/licenses/>.
23 #include <sys/socket.h>
25 #include <asm/types.h>
27 #include <linux/netlink.h>
28 #include <linux/rtnetlink.h>
33 #include <arpa/inet.h>
40 int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) {
49 struct address *list = NULL;
52 fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
56 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
61 memset(&req, 0, sizeof(req));
62 req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
63 req.hdr.nlmsg_type = RTM_GETADDR;
64 req.hdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK;
65 req.hdr.nlmsg_seq = seq;
66 req.hdr.nlmsg_pid = 0;
68 gen = NLMSG_DATA(&req.hdr);
69 gen->rtgen_family = AF_UNSPEC;
71 if (send(fd, &req, req.hdr.nlmsg_len, 0) < 0) {
83 uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
86 struct ifaddrmsg ifaddrmsg;
87 uint8_t payload[16*1024];
90 memset(&iov, 0, sizeof(iov));
92 iov.iov_len = sizeof(resp);
94 memset(&msg, 0, sizeof(msg));
99 msg.msg_control = cred_buffer;
100 msg.msg_controllen = sizeof(cred_buffer);
103 bytes = recvmsg(fd, &msg, 0);
109 cmsg = CMSG_FIRSTHDR(&msg);
110 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
115 ucred = (struct ucred*) CMSG_DATA(cmsg);
116 if (ucred->uid != 0 || ucred->pid != 0)
119 for (p = &resp.hdr; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
120 struct ifaddrmsg *ifaddrmsg;
123 void *local = NULL, *address = NULL;
125 if (!NLMSG_OK(p, (size_t) bytes)) {
130 if (p->nlmsg_seq != seq)
133 if (p->nlmsg_type == NLMSG_DONE) {
138 if (p->nlmsg_type == NLMSG_ERROR) {
139 struct nlmsgerr *nlmsgerr;
141 nlmsgerr = NLMSG_DATA(p);
142 r = -nlmsgerr->error;
146 if (p->nlmsg_type != RTM_NEWADDR)
149 ifaddrmsg = NLMSG_DATA(p);
151 if (ifaddrmsg->ifa_family != AF_INET &&
152 ifaddrmsg->ifa_family != AF_INET6)
155 if (ifaddrmsg->ifa_scope == RT_SCOPE_HOST ||
156 ifaddrmsg->ifa_scope == RT_SCOPE_NOWHERE)
159 if (ifaddrmsg->ifa_flags & IFA_F_DEPRECATED)
162 l = NLMSG_PAYLOAD(p, sizeof(struct ifaddrmsg));
163 a = IFA_RTA(ifaddrmsg);
165 while (RTA_OK(a, l)) {
167 if (a->rta_type == IFA_ADDRESS)
168 address = RTA_DATA(a);
169 else if (a->rta_type == IFA_LOCAL)
181 list = realloc(list, (n_list+1) * sizeof(struct address));
187 list[n_list].family = ifaddrmsg->ifa_family;
188 list[n_list].scope = ifaddrmsg->ifa_scope;
189 memcpy(list[n_list].address, address, ifaddrmsg->ifa_family == AF_INET ? 4 : 16);
190 list[n_list].ifindex = ifaddrmsg->ifa_index;
202 qsort(list, n_list, sizeof(struct address), address_compare);