]> git.meshlink.io Git - catta/blob - avahi-core/netlink.c
893295dd741bc8ab4c321bc8dceef8cbe7cfe97d
[catta] / avahi-core / netlink.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <assert.h>
31
32 #include <avahi-common/malloc.h>
33 #include "netlink.h"
34 #include "log.h"
35
36 struct AvahiNetlink {
37     int fd;
38     unsigned seq;
39     AvahiNetlinkCallback callback;
40     void* userdata;
41     uint8_t* buffer;
42     size_t buffer_length;
43
44     const AvahiPoll *poll_api;
45     AvahiWatch *watch;
46 };
47
48 int avahi_netlink_work(AvahiNetlink *nl, int block) {
49     ssize_t bytes;
50     struct nlmsghdr *p;
51     
52     assert(nl);
53     
54     if ((bytes = recv(nl->fd, nl->buffer, nl->buffer_length, block ? 0 : MSG_DONTWAIT)) < 0) {
55         
56         if (errno == EAGAIN || errno == EINTR)
57             return 0;
58         
59         avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
60         return -1;
61     }
62
63     p = (struct nlmsghdr *) nl->buffer;
64     
65     /* Check that this message originated from the kernel,
66        or a request from avahi itself, and not another process */
67     if ((p->nlmsg_pid != 0) && (p->nlmsg_pid != getpid())) {
68         return -1;
69     }
70
71     assert(nl->callback);
72     
73     for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
74         if (!NLMSG_OK(p, (size_t) bytes)) {
75             avahi_log_warn(__FILE__": packet truncated");
76             return -1;
77         }
78         
79         nl->callback(nl, p, nl->userdata);
80     }
81     
82     return 0;
83 }
84
85 static void socket_event(AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, void *userdata) {
86     AvahiNetlink *nl = userdata;
87
88     assert(w);
89     assert(nl);
90     assert(fd == nl->fd);
91
92     avahi_netlink_work(nl, 0);
93 }
94
95 AvahiNetlink *avahi_netlink_new(const AvahiPoll *poll_api, uint32_t groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, void* userdata), void* userdata) {
96     int fd = -1;
97     struct sockaddr_nl addr;
98     AvahiNetlink *nl = NULL;
99
100     assert(poll_api);
101     assert(cb);
102
103     if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
104         avahi_log_error(__FILE__": socket(PF_NETLINK): %s", strerror(errno));
105         return NULL;
106     }
107     
108     memset(&addr, 0, sizeof(addr));
109     addr.nl_family = AF_NETLINK;
110     addr.nl_groups = groups;
111     addr.nl_pid = getpid();
112
113     if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
114         avahi_log_error(__FILE__": bind(): %s", strerror(errno));
115         goto fail;
116     }
117
118     if (!(nl = avahi_new(AvahiNetlink, 1))) {
119         avahi_log_error(__FILE__": avahi_new() failed.");
120         goto fail;
121     }
122
123     nl->poll_api = poll_api;
124     nl->fd = fd;
125     nl->seq = 0;
126     nl->callback = cb;
127     nl->userdata = userdata;
128
129     if (!(nl->buffer = avahi_new(uint8_t, nl->buffer_length = 64*1024))) {
130         avahi_log_error(__FILE__": avahi_new() failed.");
131         goto fail;
132     }
133
134     if (!(nl->watch = poll_api->watch_new(poll_api, fd, AVAHI_WATCH_IN, socket_event, nl))) {
135         avahi_log_error(__FILE__": Failed to create watch.");
136         goto fail;
137     }
138     
139     return nl;
140
141 fail:
142
143     if (fd >= 0)
144         close(fd);
145
146     if (nl) {
147         avahi_free(nl->buffer);
148         avahi_free(nl);
149     }
150
151     return NULL;
152 }
153
154 void avahi_netlink_free(AvahiNetlink *nl) {
155     assert(nl);
156
157     if (nl->watch)
158         nl->poll_api->watch_free(nl->watch);
159
160     if (nl->fd >= 0)
161         close(nl->fd);
162     
163     avahi_free(nl->buffer);
164     avahi_free(nl);
165 }
166
167 int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, unsigned *ret_seq) {
168     assert(nl);
169     assert(m);
170     
171     m->nlmsg_seq = nl->seq++;
172     m->nlmsg_flags |= NLM_F_ACK;
173
174     if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
175         avahi_log_error(__FILE__": send(): %s", strerror(errno));
176         return -1;
177     }
178
179     if (ret_seq)
180         *ret_seq = m->nlmsg_seq;
181
182     return 0;
183 }