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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
Unix SMB/CIFS implementation.
Connect avahi to lib/tevents
Copyright (C) Volker Lendecke 2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include <avahi-common/watch.h>
struct avahi_poll_context {
struct tevent_context *ev;
AvahiWatch **watches;
AvahiTimeout **timeouts;
};
struct AvahiWatch {
struct avahi_poll_context *ctx;
struct tevent_fd *fde;
int fd;
AvahiWatchEvent latest_event;
AvahiWatchCallback callback;
void *userdata;
};
struct AvahiTimeout {
struct avahi_poll_context *ctx;
struct tevent_timer *te;
AvahiTimeoutCallback callback;
void *userdata;
};
static uint16_t avahi_flags_map_to_tevent(AvahiWatchEvent event)
{
return ((event & AVAHI_WATCH_IN) ? TEVENT_FD_READ : 0)
| ((event & AVAHI_WATCH_OUT) ? TEVENT_FD_WRITE : 0);
}
static void avahi_fd_handler(struct tevent_context *ev,
struct tevent_fd *fde, uint16_t flags,
void *private_data);
static AvahiWatch *avahi_watch_new(const AvahiPoll *api, int fd,
AvahiWatchEvent event,
AvahiWatchCallback callback,
void *userdata)
{
struct avahi_poll_context *ctx = talloc_get_type_abort(
api->userdata, struct avahi_poll_context);
int num_watches = talloc_array_length(ctx->watches);
AvahiWatch **tmp, *watch_ctx;
tmp = talloc_realloc(ctx, ctx->watches, AvahiWatch *, num_watches + 1);
if (tmp == NULL) {
return NULL;
}
ctx->watches = tmp;
watch_ctx = talloc(tmp, AvahiWatch);
if (watch_ctx == NULL) {
goto fail;
}
ctx->watches[num_watches] = watch_ctx;
watch_ctx->ctx = ctx;
watch_ctx->fde = tevent_add_fd(ctx->ev, watch_ctx, fd,
avahi_flags_map_to_tevent(event),
avahi_fd_handler, watch_ctx);
if (watch_ctx->fde == NULL) {
goto fail;
}
watch_ctx->callback = callback;
watch_ctx->userdata = userdata;
return watch_ctx;
fail:
TALLOC_FREE(watch_ctx);
ctx->watches = talloc_realloc(ctx, ctx->watches, AvahiWatch *,
num_watches);
return NULL;
}
static void avahi_fd_handler(struct tevent_context *ev,
struct tevent_fd *fde, uint16_t flags,
void *private_data)
{
AvahiWatch *watch_ctx = talloc_get_type_abort(private_data, AvahiWatch);
watch_ctx->latest_event =
((flags & TEVENT_FD_READ) ? AVAHI_WATCH_IN : 0)
| ((flags & TEVENT_FD_WRITE) ? AVAHI_WATCH_OUT : 0);
watch_ctx->callback(watch_ctx, watch_ctx->fd, watch_ctx->latest_event,
watch_ctx->userdata);
}
static void avahi_watch_update(AvahiWatch *w, AvahiWatchEvent event)
{
tevent_fd_set_flags(w->fde, avahi_flags_map_to_tevent(event));
}
static AvahiWatchEvent avahi_watch_get_events(AvahiWatch *w)
{
return w->latest_event;
}
static void avahi_watch_free(AvahiWatch *w)
{
int i, num_watches;
AvahiWatch **watches = w->ctx->watches;
struct avahi_poll_context *ctx;
num_watches = talloc_array_length(watches);
for (i=0; i<num_watches; i++) {
if (w == watches[i]) {
break;
}
}
if (i == num_watches) {
return;
}
ctx = w->ctx;
TALLOC_FREE(w);
memmove(&watches[i], &watches[i+1],
sizeof(*watches) * (num_watches - i - 1));
ctx->watches = talloc_realloc(ctx, watches, AvahiWatch *,
num_watches - 1);
}
static void avahi_timeout_handler(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval current_time,
void *private_data);
static AvahiTimeout *avahi_timeout_new(const AvahiPoll *api,
const struct timeval *tv,
AvahiTimeoutCallback callback,
void *userdata)
{
struct avahi_poll_context *ctx = talloc_get_type_abort(
api->userdata, struct avahi_poll_context);
int num_timeouts = talloc_array_length(ctx->timeouts);
AvahiTimeout **tmp, *timeout_ctx;
tmp = talloc_realloc(ctx, ctx->timeouts, AvahiTimeout *,
num_timeouts + 1);
if (tmp == NULL) {
return NULL;
}
ctx->timeouts = tmp;
timeout_ctx = talloc(tmp, AvahiTimeout);
if (timeout_ctx == NULL) {
goto fail;
}
ctx->timeouts[num_timeouts] = timeout_ctx;
timeout_ctx->ctx = ctx;
if (tv == NULL) {
timeout_ctx->te = NULL;
} else {
timeout_ctx->te = tevent_add_timer(ctx->ev, timeout_ctx,
*tv, avahi_timeout_handler,
timeout_ctx);
if (timeout_ctx->te == NULL) {
goto fail;
}
}
timeout_ctx->callback = callback;
timeout_ctx->userdata = userdata;
return timeout_ctx;
fail:
TALLOC_FREE(timeout_ctx);
ctx->timeouts = talloc_realloc(ctx, ctx->timeouts, AvahiTimeout *,
num_timeouts);
return NULL;
}
static void avahi_timeout_handler(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval current_time,
void *private_data)
{
AvahiTimeout *timeout_ctx = talloc_get_type_abort(
private_data, AvahiTimeout);
TALLOC_FREE(timeout_ctx->te);
timeout_ctx->callback(timeout_ctx, timeout_ctx->userdata);
}
static void avahi_timeout_update(AvahiTimeout *t, const struct timeval *tv)
{
TALLOC_FREE(t->te);
t->te = tevent_add_timer(t->ctx->ev, t, *tv, avahi_timeout_handler, t);
/*
* No failure mode defined here
*/
SMB_ASSERT(t->te != NULL);
}
static void avahi_timeout_free(AvahiTimeout *t)
{
int i, num_timeouts;
AvahiTimeout **timeouts = t->ctx->timeouts;
struct avahi_poll_context *ctx;
num_timeouts = talloc_array_length(timeouts);
for (i=0; i<num_timeouts; i++) {
if (t == timeouts[i]) {
break;
}
}
if (i == num_timeouts) {
return;
}
ctx = t->ctx;
TALLOC_FREE(t);
memmove(&timeouts[i], &timeouts[i+1],
sizeof(*timeouts) * (num_timeouts - i - 1));
ctx->timeouts = talloc_realloc(ctx, timeouts, AvahiTimeout *,
num_timeouts - 1);
}
struct AvahiPoll *tevent_avahi_poll(TALLOC_CTX *mem_ctx,
struct tevent_context *ev)
{
struct AvahiPoll *result;
struct avahi_poll_context *ctx;
result = talloc(mem_ctx, struct AvahiPoll);
if (result == NULL) {
return result;
}
ctx = talloc_zero(result, struct avahi_poll_context);
if (ctx == NULL) {
TALLOC_FREE(result);
return NULL;
}
ctx->ev = ev;
result->watch_new = avahi_watch_new;
result->watch_update = avahi_watch_update;
result->watch_get_events = avahi_watch_get_events;
result->watch_free = avahi_watch_free;
result->timeout_new = avahi_timeout_new;
result->timeout_update = avahi_timeout_update;
result->timeout_free = avahi_timeout_free;
result->userdata = ctx;
return result;
}
|