4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/ioctl.h>
32 #include <avahi-common/malloc.h>
39 AvahiNetlinkCallback callback;
44 const AvahiPoll *poll_api;
48 int avahi_netlink_work(AvahiNetlink *nl, int block) {
55 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
59 iov.iov_base = nl->buffer;
60 iov.iov_len = nl->buffer_length;
62 smsg.msg_name = (void*) NULL;
66 smsg.msg_control = cred_msg;
67 smsg.msg_controllen = sizeof(cred_msg);
68 smsg.msg_flags = (block ? 0 : MSG_DONTWAIT);
70 if ((bytes = recvmsg(nl->fd, &smsg, 0)) < 0) {
71 if (errno == EAGAIN || errno == EINTR)
74 avahi_log_error(__FILE__": recvmsg() failed: %s", strerror(errno));
78 cmsg = CMSG_FIRSTHDR(&smsg);
79 cred = (struct ucred *) CMSG_DATA (cmsg);
81 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
82 avahi_log_error("No sender credentials received, ignoring data.");
87 avahi_log_warn("Netlink message received from cred->uid != 0 (%d)", cred->uid);
91 p = (struct nlmsghdr *) nl->buffer;
95 for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
96 if (!NLMSG_OK(p, (size_t) bytes)) {
97 avahi_log_warn(__FILE__": packet truncated");
101 nl->callback(nl, p, nl->userdata);
107 static void socket_event(AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, void *userdata) {
108 AvahiNetlink *nl = userdata;
112 assert(fd == nl->fd);
114 avahi_netlink_work(nl, 0);
117 AvahiNetlink *avahi_netlink_new(const AvahiPoll *poll_api, uint32_t groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, void* userdata), void* userdata) {
120 struct sockaddr_nl addr;
121 AvahiNetlink *nl = NULL;
126 if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
127 avahi_log_error(__FILE__": socket(PF_NETLINK): %s", strerror(errno));
131 memset(&addr, 0, sizeof(addr));
132 addr.nl_family = AF_NETLINK;
133 addr.nl_groups = groups;
134 addr.nl_pid = getpid();
136 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
137 avahi_log_error(__FILE__": bind(): %s", strerror(errno));
141 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
142 avahi_log_error(__FILE__": bind(): %s", strerror(errno));
146 if (!(nl = avahi_new(AvahiNetlink, 1))) {
147 avahi_log_error(__FILE__": avahi_new() failed.");
151 nl->poll_api = poll_api;
155 nl->userdata = userdata;
157 if (!(nl->buffer = avahi_new(uint8_t, nl->buffer_length = 64*1024))) {
158 avahi_log_error(__FILE__": avahi_new() failed.");
162 if (!(nl->watch = poll_api->watch_new(poll_api, fd, AVAHI_WATCH_IN, socket_event, nl))) {
163 avahi_log_error(__FILE__": Failed to create watch.");
175 avahi_free(nl->buffer);
182 void avahi_netlink_free(AvahiNetlink *nl) {
186 nl->poll_api->watch_free(nl->watch);
191 avahi_free(nl->buffer);
195 int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, unsigned *ret_seq) {
199 m->nlmsg_seq = nl->seq++;
200 m->nlmsg_flags |= NLM_F_ACK;
202 if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
203 avahi_log_error(__FILE__": send(): %s", strerror(errno));
208 *ret_seq = m->nlmsg_seq;