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