chiark / gitweb /
core: add new ConditionNeedsUpdate= unit condition
[elogind.git] / src / nss-myhostname / netlink.c
index 53c3b501be0f0636fad58cf9d3f24d8ba12b0859..b4a464c06a084b7952b65add7cd63b7a72e731f4 100644 (file)
@@ -4,6 +4,7 @@
   This file is part of systemd.
 
   Copyright 2008-2011 Lennart Poettering
+  Copyright 2014 Tom Gundersen
 
   systemd is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <asm/types.h>
-#include <inttypes.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
-#include <string.h>
-#include <assert.h>
-#include <errno.h>
-#include <limits.h>
-#include <arpa/inet.h>
-#include <unistd.h>
-#include <inttypes.h>
-#include <stdlib.h>
+#include "sd-rtnl.h"
+#include "rtnl-util.h"
+#include "macro.h"
 
 #include "ifconf.h"
 
 int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) {
-
-        struct {
-                struct nlmsghdr hdr;
-                struct rtgenmsg gen;
-        } req;
-        struct rtgenmsg *gen;
-        int fd, r, on = 1;
-        uint32_t seq = 4711;
-        struct address *list = NULL;
+        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
+        sd_rtnl_message *m;
+        _cleanup_free_ struct address *list = NULL;
+        struct address *new_list = NULL;
         unsigned n_list = 0;
+        int r;
 
-        fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
-        if (fd < 0)
-                return -errno;
-
-        if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
-                r = -errno;
-                goto finish;
-        }
-
-        memset(&req, 0, sizeof(req));
-        req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
-        req.hdr.nlmsg_type = RTM_GETADDR;
-        req.hdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK;
-        req.hdr.nlmsg_seq = seq;
-        req.hdr.nlmsg_pid = 0;
-
-        gen = NLMSG_DATA(&req.hdr);
-        gen->rtgen_family = AF_UNSPEC;
-
-        if (send(fd, &req, req.hdr.nlmsg_len, 0) < 0) {
-                r = -errno;
-                goto finish;
-        }
-
-        for (;;) {
-                ssize_t bytes;
-                struct msghdr msg;
-                struct cmsghdr *cmsg;
-                struct ucred *ucred;
-                struct iovec iov;
-                struct nlmsghdr *p;
-                uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
-                struct {
-                        struct nlmsghdr hdr;
-                        struct ifaddrmsg ifaddrmsg;
-                        uint8_t payload[16*1024];
-                } resp;
-
-                memset(&iov, 0, sizeof(iov));
-                iov.iov_base = &resp;
-                iov.iov_len = sizeof(resp);
-
-                memset(&msg, 0, sizeof(msg));
-                msg.msg_name = NULL;
-                msg.msg_namelen = 0;
-                msg.msg_iov = &iov;
-                msg.msg_iovlen = 1;
-                msg.msg_control = cred_buffer;
-                msg.msg_controllen = sizeof(cred_buffer);
-                msg.msg_flags = 0;
-
-                bytes = recvmsg(fd, &msg, 0);
-                if (bytes < 0) {
-                        r = -errno;
-                        goto finish;
-                }
+        r = sd_rtnl_open(&rtnl, 0);
+        if (r < 0)
+                return r;
 
-                cmsg = CMSG_FIRSTHDR(&msg);
-                if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
-                        r = -EIO;
-                        goto finish;
-                }
+        r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
+        if (r < 0)
+                return r;
 
-                ucred = (struct ucred*) CMSG_DATA(cmsg);
-                if (ucred->uid != 0 || ucred->pid != 0)
+        r = sd_rtnl_call(rtnl, req, 0, &reply);
+        if (r < 0)
+                return r;
+        m = reply;
+
+        do {
+                uint16_t type;
+                unsigned char scope;
+                unsigned char flags;
+                unsigned char family;
+                int ifindex;
+                union {
+                        struct in_addr in;
+                        struct in6_addr in6;
+                } address;
+
+                r = sd_rtnl_message_get_errno(m);
+                if (r < 0)
+                        return r;
+
+                r = sd_rtnl_message_get_type(m, &type);
+                if (r < 0)
+                        return r;
+
+                if (type != RTM_NEWADDR)
                         continue;
 
-                for (p = &resp.hdr; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
-                        struct ifaddrmsg *ifaddrmsg;
-                        struct rtattr *a;
-                        size_t l;
-                        void *local = NULL, *address = NULL;
+                r = sd_rtnl_message_addr_get_scope(m, &scope);
+                if (r < 0)
+                        return r;
 
-                        if (!NLMSG_OK(p, (size_t) bytes)) {
-                                r = -EIO;
-                                goto finish;
-                        }
-
-                        if (p->nlmsg_seq != seq)
-                                continue;
+                if (scope == RT_SCOPE_HOST || scope == RT_SCOPE_NOWHERE)
+                        continue;
 
-                        if (p->nlmsg_type == NLMSG_DONE) {
-                                r = 0;
-                                goto finish;
-                        }
+                r = sd_rtnl_message_addr_get_flags(m, &flags);
+                if (r < 0)
+                        return r;
 
-                        if (p->nlmsg_type == NLMSG_ERROR) {
-                                struct nlmsgerr *nlmsgerr;
+                if (flags & IFA_F_DEPRECATED)
+                        continue;
 
-                                nlmsgerr = NLMSG_DATA(p);
-                                r = -nlmsgerr->error;
-                                goto finish;
+                r = sd_rtnl_message_addr_get_family(m, &family);
+                if (r < 0)
+                        return r;
+
+                switch (family) {
+                case AF_INET:
+                        r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &address.in);
+                        if (r < 0) {
+                                r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &address.in);
+                                if (r < 0)
+                                        continue;
                         }
-
-                        if (p->nlmsg_type != RTM_NEWADDR)
-                                continue;
-
-                        ifaddrmsg = NLMSG_DATA(p);
-
-                        if (ifaddrmsg->ifa_family != AF_INET &&
-                            ifaddrmsg->ifa_family != AF_INET6)
-                                continue;
-
-                        if (ifaddrmsg->ifa_scope == RT_SCOPE_HOST ||
-                            ifaddrmsg->ifa_scope == RT_SCOPE_NOWHERE)
-                                continue;
-
-                        if (ifaddrmsg->ifa_flags & IFA_F_DEPRECATED)
-                                continue;
-
-                        l = NLMSG_PAYLOAD(p, sizeof(struct ifaddrmsg));
-                        a = IFA_RTA(ifaddrmsg);
-
-                        while (RTA_OK(a, l)) {
-
-                                if (a->rta_type == IFA_ADDRESS)
-                                        address = RTA_DATA(a);
-                                else if (a->rta_type == IFA_LOCAL)
-                                        local = RTA_DATA(a);
-
-                                a = RTA_NEXT(a, l);
+                        break;
+                case AF_INET6:
+                        r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &address.in6);
+                        if (r < 0) {
+                                r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &address.in6);
+                                if (r < 0)
+                                        continue;
                         }
+                        break;
+                default:
+                        continue;
+                }
 
-                        if (local)
-                                address = local;
+                r = sd_rtnl_message_addr_get_ifindex(m, &ifindex);
+                if (r < 0)
+                        return r;
 
-                        if (!address)
-                                continue;
+                new_list = realloc(list, (n_list+1) * sizeof(struct address));
+                if (!new_list)
+                        return -ENOMEM;
+                else
+                        list = new_list;
 
-                        list = realloc(list, (n_list+1) * sizeof(struct address));
-                        if (!list) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
+                assert_cc(sizeof(address) <= 16);
 
-                        list[n_list].family = ifaddrmsg->ifa_family;
-                        list[n_list].scope = ifaddrmsg->ifa_scope;
-                        memcpy(list[n_list].address, address, ifaddrmsg->ifa_family == AF_INET ? 4 : 16);
-                        list[n_list].ifindex = ifaddrmsg->ifa_index;
+                list[n_list].family = family;
+                list[n_list].scope = scope;
+                memcpy(list[n_list].address, &address, sizeof(address));
+                list[n_list].ifindex = ifindex;
 
-                        n_list++;
-                }
-        }
+                n_list++;
 
-finish:
-        close(fd);
+        } while ((m = sd_rtnl_message_next(m)));
 
-        if (r < 0)
-                free(list);
-        else {
+        if (n_list)
                 qsort(list, n_list, sizeof(struct address), address_compare);
 
-                *_list = list;
-                *_n_list = n_list;
-        }
+        *_n_list = n_list;
+        *_list = list;
+        list = NULL;
 
-        return r;
+        return 0;
 }