]> git.meshlink.io Git - catta/blob - avahi-core/netlink.c
119e19579c292db51959a547d1516cca31ba0f98
[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
31 #include "netlink.h"
32 #include "log.h"
33
34 struct AvahiNetlink {
35     GMainContext *context;
36     gint fd;
37     guint seq;
38     GPollFD poll_fd;
39     GSource *source;
40     void (*callback) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata);
41     gpointer userdata;
42     guint8* buffer;
43     guint buffer_length;
44 };
45
46 gboolean avahi_netlink_work(AvahiNetlink *nl, gboolean block) {
47     g_assert(nl);
48
49     for (;;) {
50         ssize_t bytes;
51         struct nlmsghdr *p;
52
53         for (;;) {
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 TRUE;
58                 
59                 avahi_log_warn("NETLINK: recv() failed: %s", strerror(errno));
60                 return FALSE;
61             }
62
63             break;
64         }
65
66         p = (struct nlmsghdr *) nl->buffer;
67         
68         if (nl->callback) {
69             for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
70                 if (!NLMSG_OK(p, (size_t) bytes)) {
71                     avahi_log_warn("NETLINK: packet truncated");
72                     return FALSE;
73                 }
74
75                 nl->callback(nl, p, nl->userdata);
76             }
77         }
78
79         if (block)
80             return TRUE;
81     }
82 }
83
84 static gboolean prepare_func(GSource *source, gint *timeout) {
85     g_assert(source);
86     g_assert(timeout);
87     
88     *timeout = -1;
89     return FALSE;
90 }
91
92 static gboolean check_func(GSource *source) {
93     AvahiNetlink* nl;
94     g_assert(source);
95
96     nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource)));
97     g_assert(nl);
98     
99     return nl->poll_fd.revents & (G_IO_IN|G_IO_HUP|G_IO_ERR);
100 }
101
102 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
103     AvahiNetlink* nl;
104     g_assert(source);
105
106     nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource)));
107     g_assert(nl);
108     
109     return avahi_netlink_work(nl, FALSE);
110 }
111
112 AvahiNetlink *avahi_netlink_new(GMainContext *context, gint priority, guint32 groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata), gpointer userdata) {
113     int fd;
114     struct sockaddr_nl addr;
115     AvahiNetlink *nl;
116
117     static GSourceFuncs source_funcs = {
118         prepare_func,
119         check_func,
120         dispatch_func,
121         NULL,
122         NULL,
123         NULL
124     };
125     
126     g_assert(cb);
127
128     if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
129         g_critical("NETLINK: socket(PF_NETLINK): %s", strerror(errno));
130         return NULL;
131     }
132     
133     memset(&addr, 0, sizeof(addr));
134     addr.nl_family = AF_NETLINK;
135     addr.nl_groups = groups;
136     addr.nl_pid = getpid();
137
138     if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
139         close(fd);
140         g_critical("bind(): %s", strerror(errno));
141         return NULL;
142     }
143
144     nl = g_new(AvahiNetlink, 1);
145     g_main_context_ref(nl->context = context ? context : g_main_context_default());
146     nl->fd = fd;
147     nl->seq = 0;
148     nl->callback = cb;
149     nl->userdata = userdata;
150     nl->buffer = g_new(guint8, nl->buffer_length = 64*1024);
151
152     nl->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiNetlink*));
153     *((AvahiNetlink**) (((guint8*) nl->source) + sizeof(GSource))) = nl;
154
155     g_source_set_priority(nl->source, priority);
156     
157     memset(&nl->poll_fd, 0, sizeof(GPollFD));
158     nl->poll_fd.fd = fd;
159     nl->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
160     g_source_add_poll(nl->source, &nl->poll_fd);
161     
162     g_source_attach(nl->source, nl->context);
163     
164     return nl;
165 }
166
167 void avahi_netlink_free(AvahiNetlink *nl) {
168     g_assert(nl);
169
170     g_source_destroy(nl->source);
171     g_source_unref(nl->source);
172     g_main_context_unref(nl->context);
173     close(nl->fd);
174     g_free(nl->buffer);
175     g_free(nl);
176 }
177
178 int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, guint *ret_seq) {
179     g_assert(nl);
180     g_assert(m);
181     
182     m->nlmsg_seq = nl->seq++;
183     m->nlmsg_flags |= NLM_F_ACK;
184
185     if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
186         avahi_log_warn("NETLINK: send(): %s\n", strerror(errno));
187         return -1;
188     }
189
190     if (ret_seq)
191         *ret_seq = m->nlmsg_seq;
192
193     return 0;
194 }