1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/* $Id$ */
/***
This file is part of avahi.
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "netlink.h"
#include "log.h"
struct AvahiNetlink {
GMainContext *context;
gint fd;
guint seq;
GPollFD poll_fd;
GSource *source;
void (*callback) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata);
gpointer userdata;
guint8* buffer;
guint buffer_length;
};
gboolean avahi_netlink_work(AvahiNetlink *nl, gboolean block) {
g_assert(nl);
for (;;) {
ssize_t bytes;
struct nlmsghdr *p;
for (;;) {
if ((bytes = recv(nl->fd, nl->buffer, nl->buffer_length, block ? 0 : MSG_DONTWAIT)) < 0) {
if (errno == EAGAIN || errno == EINTR)
return TRUE;
avahi_log_warn("NETLINK: recv() failed: %s", strerror(errno));
return FALSE;
}
break;
}
p = (struct nlmsghdr *) nl->buffer;
if (nl->callback) {
for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
if (!NLMSG_OK(p, (size_t) bytes)) {
avahi_log_warn("NETLINK: packet truncated");
return FALSE;
}
nl->callback(nl, p, nl->userdata);
}
}
if (block)
return TRUE;
}
}
static gboolean prepare_func(GSource *source, gint *timeout) {
g_assert(source);
g_assert(timeout);
*timeout = -1;
return FALSE;
}
static gboolean check_func(GSource *source) {
AvahiNetlink* nl;
g_assert(source);
nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource)));
g_assert(nl);
return nl->poll_fd.revents & (G_IO_IN|G_IO_HUP|G_IO_ERR);
}
static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
AvahiNetlink* nl;
g_assert(source);
nl = *((AvahiNetlink**) (((guint8*) source) + sizeof(GSource)));
g_assert(nl);
return avahi_netlink_work(nl, FALSE);
}
AvahiNetlink *avahi_netlink_new(GMainContext *context, gint priority, guint32 groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata), gpointer userdata) {
int fd;
struct sockaddr_nl addr;
AvahiNetlink *nl;
static GSourceFuncs source_funcs = {
prepare_func,
check_func,
dispatch_func,
NULL,
NULL,
NULL
};
g_assert(cb);
if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
g_critical("NETLINK: socket(PF_NETLINK): %s", strerror(errno));
return NULL;
}
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = groups;
addr.nl_pid = getpid();
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(fd);
g_critical("bind(): %s", strerror(errno));
return NULL;
}
nl = g_new(AvahiNetlink, 1);
g_main_context_ref(nl->context = context ? context : g_main_context_default());
nl->fd = fd;
nl->seq = 0;
nl->callback = cb;
nl->userdata = userdata;
nl->buffer = g_new(guint8, nl->buffer_length = 64*1024);
nl->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiNetlink*));
*((AvahiNetlink**) (((guint8*) nl->source) + sizeof(GSource))) = nl;
g_source_set_priority(nl->source, priority);
memset(&nl->poll_fd, 0, sizeof(GPollFD));
nl->poll_fd.fd = fd;
nl->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
g_source_add_poll(nl->source, &nl->poll_fd);
g_source_attach(nl->source, nl->context);
return nl;
}
void avahi_netlink_free(AvahiNetlink *nl) {
g_assert(nl);
g_source_destroy(nl->source);
g_source_unref(nl->source);
g_main_context_unref(nl->context);
close(nl->fd);
g_free(nl->buffer);
g_free(nl);
}
int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, guint *ret_seq) {
g_assert(nl);
g_assert(m);
m->nlmsg_seq = nl->seq++;
m->nlmsg_flags |= NLM_F_ACK;
if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
avahi_log_warn("NETLINK: send(): %s\n", strerror(errno));
return -1;
}
if (ret_seq)
*ret_seq = m->nlmsg_seq;
return 0;
}
|