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