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
33 GMainContext *context;
38 void (*callback) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata);
42 gboolean avahi_netlink_work(AvahiNetlink *nl, gboolean block) {
46 guint8 replybuf[64*1024];
48 struct nlmsghdr *p = (struct nlmsghdr *) replybuf;
50 if ((bytes = recv(nl->fd, replybuf, sizeof(replybuf), block ? 0 : MSG_DONTWAIT)) < 0) {
52 if (errno == EAGAIN || errno == EINTR)
55 g_warning("NETLINK: recv() failed");
60 for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
61 if (!NLMSG_OK(p, (size_t) bytes)) {
62 g_warning("NETLINK: packet truncated");
66 nl->callback(nl, p, nl->userdata);
77 static gboolean prepare_func(GSource *source, gint *timeout) {
85 static gboolean check_func(GSource *source) {
89 nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource)));
92 return nl->poll_fd.revents & (G_IO_IN|G_IO_HUP|G_IO_ERR);
95 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
99 nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource)));
102 return avahi_netlink_work(nl, FALSE);
105 AvahiNetlink *avahi_netlink_new(GMainContext *context, gint priority, guint32 groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata), gpointer userdata) {
107 struct sockaddr_nl addr;
110 static GSourceFuncs source_funcs = {
122 if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
123 g_critical("NETLINK: socket(PF_NETLINK): %s", strerror(errno));
127 memset(&addr, 0, sizeof(addr));
128 addr.nl_family = AF_NETLINK;
129 addr.nl_groups = groups;
130 addr.nl_pid = getpid();
132 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
134 g_critical("bind(): %s", strerror(errno));
138 nl = g_new(AvahiNetlink, 1);
139 nl->context = context;
140 g_main_context_ref(context);
144 nl->userdata = userdata;
146 nl->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiNetlink*));
147 *((AvahiNetlink**) (((guint8*) nl->source) + sizeof(GSource))) = nl;
149 g_source_set_priority(nl->source, priority);
151 memset(&nl->poll_fd, 0, sizeof(GPollFD));
153 nl->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
154 g_source_add_poll(nl->source, &nl->poll_fd);
156 g_source_attach(nl->source, nl->context);
161 void avahi_netlink_free(AvahiNetlink *nl) {
164 g_source_destroy(nl->source);
165 g_source_unref(nl->source);
166 g_main_context_unref(nl->context);
171 int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, guint *ret_seq) {
175 m->nlmsg_seq = nl->seq++;
176 m->nlmsg_flags |= NLM_F_ACK;
178 if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
179 g_warning("NETLINK: send(): %s\n", strerror(errno));
184 *ret_seq = m->nlmsg_seq;