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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/*
* virnetservermdns.c: advertise server sockets
*
* Copyright (C) 2011-2012 Red Hat, Inc.
* Copyright (C) 2007 Daniel P. Berrange
*
* Derived from Avahi example service provider code.
*
* This library 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.
*
* This library 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 this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Daniel P. Berrange <berrange@redhat.com>
*/
#include <config.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#if WITH_AVAHI
# include <avahi-client/client.h>
# include <avahi-client/publish.h>
# include <avahi-common/alternative.h>
# include <avahi-common/simple-watch.h>
# include <avahi-common/malloc.h>
# include <avahi-common/error.h>
# include <avahi-common/timeval.h>
#endif
#include "virnetservermdns.h"
#include "vireventpoll.h"
#include "viralloc.h"
#include "virerror.h"
#include "virlog.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_RPC
VIR_LOG_INIT("rpc.netservermdns");
struct _virNetServerMDNSEntry {
char *type;
int port;
virNetServerMDNSEntryPtr next;
};
struct _virNetServerMDNSGroup {
virNetServerMDNSPtr mdns;
#if WITH_AVAHI
AvahiEntryGroup *handle;
#endif
char *name;
virNetServerMDNSEntryPtr entry;
virNetServerMDNSGroupPtr next;
};
struct _virNetServerMDNS {
#if WITH_AVAHI
AvahiClient *client;
AvahiPoll *poller;
#endif
virNetServerMDNSGroupPtr group;
};
#if WITH_AVAHI
/* Avahi API requires this struct name in the app :-( */
struct AvahiWatch {
int watch;
int fd;
int revents;
AvahiWatchCallback callback;
void *userdata;
};
/* Avahi API requires this struct name in the app :-( */
struct AvahiTimeout {
int timer;
AvahiTimeoutCallback callback;
void *userdata;
};
static void virNetServerMDNSCreateServices(virNetServerMDNSGroupPtr group);
/* Called whenever the entry group state changes */
static void virNetServerMDNSGroupCallback(AvahiEntryGroup *g ATTRIBUTE_UNUSED,
AvahiEntryGroupState state,
void *data)
{
virNetServerMDNSGroupPtr group = data;
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED:
/* The entry group has been established successfully */
VIR_DEBUG("Group '%s' established", group->name);
break;
case AVAHI_ENTRY_GROUP_COLLISION:
{
char *n;
/* A service name collision happened. Let's pick a new name */
n = avahi_alternative_service_name(group->name);
VIR_FREE(group->name);
group->name = n;
VIR_DEBUG("Group name collision, renaming service to '%s'", group->name);
/* And recreate the services */
virNetServerMDNSCreateServices(group);
}
break;
case AVAHI_ENTRY_GROUP_FAILURE :
VIR_DEBUG("Group failure: %s",
avahi_strerror(avahi_client_errno(group->mdns->client)));
/* Some kind of failure happened while we were registering our services */
/* avahi_simple_poll_quit(simple_poll); */
break;
case AVAHI_ENTRY_GROUP_UNCOMMITED:
case AVAHI_ENTRY_GROUP_REGISTERING:
;
}
}
static void virNetServerMDNSCreateServices(virNetServerMDNSGroupPtr group)
{
virNetServerMDNSPtr mdns = group->mdns;
virNetServerMDNSEntryPtr entry;
int ret;
VIR_DEBUG("Adding services to '%s'", group->name);
/* If we've no services to advertise, just reset the group to make
* sure it is emptied of any previously advertised services */
if (!group->entry) {
if (group->handle)
avahi_entry_group_reset(group->handle);
return;
}
/* If this is the first time we're called, let's create a new entry group */
if (!group->handle) {
VIR_DEBUG("Creating initial group %s", group->name);
if (!(group->handle =
avahi_entry_group_new(mdns->client,
virNetServerMDNSGroupCallback,
group))) {
VIR_DEBUG("avahi_entry_group_new() failed: %s",
avahi_strerror(avahi_client_errno(mdns->client)));
return;
}
}
entry = group->entry;
while (entry) {
if ((ret = avahi_entry_group_add_service(group->handle,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
0,
group->name,
entry->type,
NULL,
NULL,
entry->port,
NULL)) < 0) {
VIR_DEBUG("Failed to add %s service on port %d: %s",
entry->type, entry->port, avahi_strerror(ret));
avahi_entry_group_reset(group->handle);
return;
}
entry = entry->next;
}
/* Tell the server to register the service */
if ((ret = avahi_entry_group_commit(group->handle)) < 0) {
avahi_entry_group_reset(group->handle);
VIR_DEBUG("Failed to commit entry_group: %s",
avahi_strerror(ret));
return;
}
}
static void virNetServerMDNSClientCallback(AvahiClient *c,
AvahiClientState state,
void *data)
{
virNetServerMDNSPtr mdns = data;
virNetServerMDNSGroupPtr group;
if (!mdns->client)
mdns->client = c;
VIR_DEBUG("Callback state=%d", state);
/* Called whenever the client or server state changes */
switch (state) {
case AVAHI_CLIENT_S_RUNNING:
/* The server has startup successfully and registered its host
* name on the network, so it's time to create our services */
VIR_DEBUG("Client running %p", mdns->client);
group = mdns->group;
while (group) {
virNetServerMDNSCreateServices(group);
group = group->next;
}
break;
case AVAHI_CLIENT_FAILURE:
VIR_DEBUG("Client failure: %s",
avahi_strerror(avahi_client_errno(c)));
virNetServerMDNSStop(mdns);
virNetServerMDNSStart(mdns);
break;
case AVAHI_CLIENT_S_COLLISION:
/* Let's drop our registered services. When the server is back
* in AVAHI_SERVER_RUNNING state we will register them
* again with the new host name. */
ATTRIBUTE_FALLTHROUGH;
case AVAHI_CLIENT_S_REGISTERING:
/* The server records are now being established. This
* might be caused by a host name change. We need to wait
* for our own records to register until the host name is
* properly established. */
VIR_DEBUG("Client collision/connecting %p", mdns->client);
group = mdns->group;
while (group) {
if (group->handle)
avahi_entry_group_reset(group->handle);
group = group->next;
}
break;
case AVAHI_CLIENT_CONNECTING:
VIR_DEBUG("Client connecting.... %p", mdns->client);
;
}
}
static void virNetServerMDNSWatchDispatch(int watch, int fd, int events, void *opaque)
{
AvahiWatch *w = opaque;
int fd_events = virEventPollToNativeEvents(events);
VIR_DEBUG("Dispatch watch %d FD %d Event %d", watch, fd, fd_events);
w->revents = fd_events;
w->callback(w, fd, fd_events, w->userdata);
}
static void virNetServerMDNSWatchDofree(void *w)
{
VIR_FREE(w);
}
static AvahiWatch *virNetServerMDNSWatchNew(const AvahiPoll *api ATTRIBUTE_UNUSED,
int fd, AvahiWatchEvent event,
AvahiWatchCallback cb, void *userdata)
{
AvahiWatch *w;
virEventHandleType hEvents;
if (VIR_ALLOC(w) < 0)
return NULL;
w->fd = fd;
w->revents = 0;
w->callback = cb;
w->userdata = userdata;
VIR_DEBUG("New handle %p FD %d Event %d", w, w->fd, event);
hEvents = virEventPollFromNativeEvents(event);
if ((w->watch = virEventAddHandle(fd, hEvents,
virNetServerMDNSWatchDispatch,
w,
virNetServerMDNSWatchDofree)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to add watch for fd %d events %d"), fd, hEvents);
VIR_FREE(w);
return NULL;
}
return w;
}
static void virNetServerMDNSWatchUpdate(AvahiWatch *w, AvahiWatchEvent event)
{
VIR_DEBUG("Update handle %p FD %d Event %d", w, w->fd, event);
virEventUpdateHandle(w->watch, event);
}
static AvahiWatchEvent virNetServerMDNSWatchGetEvents(AvahiWatch *w)
{
VIR_DEBUG("Get handle events %p %d", w, w->fd);
return w->revents;
}
static void virNetServerMDNSWatchFree(AvahiWatch *w)
{
VIR_DEBUG("Free handle %p %d", w, w->fd);
virEventRemoveHandle(w->watch);
}
static void virNetServerMDNSTimeoutDispatch(int timer ATTRIBUTE_UNUSED, void *opaque)
{
AvahiTimeout *t = (AvahiTimeout*)opaque;
VIR_DEBUG("Dispatch timeout %p %d", t, timer);
virEventUpdateTimeout(t->timer, -1);
t->callback(t, t->userdata);
}
static void virNetServerMDNSTimeoutDofree(void *t)
{
VIR_FREE(t);
}
static AvahiTimeout *virNetServerMDNSTimeoutNew(const AvahiPoll *api ATTRIBUTE_UNUSED,
const struct timeval *tv,
AvahiTimeoutCallback cb,
void *userdata)
{
AvahiTimeout *t;
struct timeval now;
long long nowms, thenms, timeout;
VIR_DEBUG("Add timeout TV %p", tv);
if (VIR_ALLOC(t) < 0)
return NULL;
if (gettimeofday(&now, NULL) < 0) {
virReportSystemError(errno, "%s",
_("Unable to get current time"));
VIR_FREE(t);
return NULL;
}
VIR_DEBUG("Trigger timed for %d %d %d %d",
(int)now.tv_sec, (int)now.tv_usec,
(int)(tv ? tv->tv_sec : 0), (int)(tv ? tv->tv_usec : 0));
nowms = (now.tv_sec * 1000ll) + (now.tv_usec / 1000ll);
if (tv) {
thenms = (tv->tv_sec * 1000ll) + (tv->tv_usec/1000ll);
timeout = thenms > nowms ? nowms - thenms : 0;
if (timeout < 0)
timeout = 0;
} else {
timeout = -1;
}
t->timer = virEventAddTimeout(timeout,
virNetServerMDNSTimeoutDispatch,
t,
virNetServerMDNSTimeoutDofree);
t->callback = cb;
t->userdata = userdata;
if (t->timer < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to add timer with timeout %lld"), timeout);
VIR_FREE(t);
return NULL;
}
return t;
}
static void virNetServerMDNSTimeoutUpdate(AvahiTimeout *t, const struct timeval *tv)
{
struct timeval now;
long long nowms, thenms, timeout;
VIR_DEBUG("Update timeout %p TV %p", t, tv);
if (gettimeofday(&now, NULL) < 0) {
VIR_FREE(t);
return;
}
nowms = (now.tv_sec * 1000ll) + (now.tv_usec / 1000ll);
if (tv) {
thenms = ((tv->tv_sec * 1000ll) + (tv->tv_usec/1000ll));
timeout = thenms > nowms ? nowms - thenms : 0;
if (timeout < 0)
timeout = 0;
} else {
timeout = -1;
}
virEventUpdateTimeout(t->timer, timeout);
}
static void virNetServerMDNSTimeoutFree(AvahiTimeout *t)
{
VIR_DEBUG("Free timeout %p", t);
virEventRemoveTimeout(t->timer);
}
static AvahiPoll *virNetServerMDNSCreatePoll(void)
{
AvahiPoll *p;
if (VIR_ALLOC(p) < 0)
return NULL;
p->userdata = NULL;
p->watch_new = virNetServerMDNSWatchNew;
p->watch_update = virNetServerMDNSWatchUpdate;
p->watch_get_events = virNetServerMDNSWatchGetEvents;
p->watch_free = virNetServerMDNSWatchFree;
p->timeout_new = virNetServerMDNSTimeoutNew;
p->timeout_update = virNetServerMDNSTimeoutUpdate;
p->timeout_free = virNetServerMDNSTimeoutFree;
return p;
}
virNetServerMDNS *virNetServerMDNSNew(void)
{
virNetServerMDNS *mdns;
if (VIR_ALLOC(mdns) < 0)
return NULL;
/* Allocate main loop object */
if (!(mdns->poller = virNetServerMDNSCreatePoll())) {
VIR_FREE(mdns);
return NULL;
}
return mdns;
}
int virNetServerMDNSStart(virNetServerMDNS *mdns)
{
int error;
VIR_DEBUG("Starting client %p", mdns);
mdns->client = avahi_client_new(mdns->poller,
AVAHI_CLIENT_NO_FAIL,
virNetServerMDNSClientCallback,
mdns, &error);
if (!mdns->client) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to create mDNS client: %s"),
avahi_strerror(error));
return -1;
}
return 0;
}
virNetServerMDNSGroupPtr virNetServerMDNSAddGroup(virNetServerMDNS *mdns,
const char *name)
{
virNetServerMDNSGroupPtr group;
VIR_DEBUG("Adding group '%s'", name);
if (VIR_ALLOC(group) < 0)
return NULL;
if (VIR_STRDUP(group->name, name) < 0) {
VIR_FREE(group);
return NULL;
}
group->mdns = mdns;
group->next = mdns->group;
mdns->group = group;
return group;
}
void virNetServerMDNSRemoveGroup(virNetServerMDNSPtr mdns,
virNetServerMDNSGroupPtr group)
{
virNetServerMDNSGroupPtr tmp = mdns->group, prev = NULL;
while (tmp) {
if (tmp == group) {
VIR_FREE(group->name);
if (prev)
prev->next = group->next;
else
group->mdns->group = group->next;
VIR_FREE(group);
return;
}
prev = tmp;
tmp = tmp->next;
}
}
virNetServerMDNSEntryPtr virNetServerMDNSAddEntry(virNetServerMDNSGroupPtr group,
const char *type,
int port)
{
virNetServerMDNSEntryPtr entry;
VIR_DEBUG("Adding entry %s %d to group %s", type, port, group->name);
if (VIR_ALLOC(entry) < 0)
return NULL;
entry->port = port;
if (VIR_STRDUP(entry->type, type) < 0) {
VIR_FREE(entry);
return NULL;
}
entry->next = group->entry;
group->entry = entry;
return entry;
}
void virNetServerMDNSRemoveEntry(virNetServerMDNSGroupPtr group,
virNetServerMDNSEntryPtr entry)
{
virNetServerMDNSEntryPtr tmp = group->entry, prev = NULL;
while (tmp) {
if (tmp == entry) {
VIR_FREE(entry->type);
if (prev)
prev->next = entry->next;
else
group->entry = entry->next;
return;
}
prev = tmp;
tmp = tmp->next;
}
}
void virNetServerMDNSStop(virNetServerMDNSPtr mdns)
{
virNetServerMDNSGroupPtr group = mdns->group;
while (group) {
if (group->handle) {
avahi_entry_group_free(group->handle);
group->handle = NULL;
}
group = group->next;
}
if (mdns->client)
avahi_client_free(mdns->client);
mdns->client = NULL;
}
void virNetServerMDNSFree(virNetServerMDNSPtr mdns)
{
virNetServerMDNSGroupPtr group, tmp;
if (!mdns)
return;
group = mdns->group;
while (group) {
tmp = group->next;
virNetServerMDNSGroupFree(group);
group = tmp;
}
VIR_FREE(mdns->poller);
VIR_FREE(mdns);
}
void virNetServerMDNSGroupFree(virNetServerMDNSGroupPtr grp)
{
virNetServerMDNSEntryPtr entry, tmp;
if (!grp)
return;
entry = grp->entry;
while (entry) {
tmp = entry->next;
virNetServerMDNSEntryFree(entry);
entry = tmp;
}
VIR_FREE(grp->name);
VIR_FREE(grp);
}
void virNetServerMDNSEntryFree(virNetServerMDNSEntryPtr entry)
{
if (!entry)
return;
VIR_FREE(entry->type);
VIR_FREE(entry);
}
#else /* ! WITH_AVAHI */
static const char *unsupported = N_("avahi not available at build time");
virNetServerMDNS *
virNetServerMDNSNew(void)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return NULL;
}
int
virNetServerMDNSStart(virNetServerMDNS *mdns ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;
}
virNetServerMDNSGroupPtr
virNetServerMDNSAddGroup(virNetServerMDNS *mdns ATTRIBUTE_UNUSED,
const char *name ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return NULL;
}
void
virNetServerMDNSRemoveGroup(virNetServerMDNSPtr mdns ATTRIBUTE_UNUSED,
virNetServerMDNSGroupPtr group ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
}
virNetServerMDNSEntryPtr
virNetServerMDNSAddEntry(virNetServerMDNSGroupPtr group ATTRIBUTE_UNUSED,
const char *type ATTRIBUTE_UNUSED,
int port ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return NULL;
}
void
virNetServerMDNSRemoveEntry(virNetServerMDNSGroupPtr group ATTRIBUTE_UNUSED,
virNetServerMDNSEntryPtr entry ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
}
void
virNetServerMDNSStop(virNetServerMDNSPtr mdns ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
}
void
virNetServerMDNSFree(virNetServerMDNSPtr mdns ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
}
void
virNetServerMDNSGroupFree(virNetServerMDNSGroupPtr grp ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
}
void
virNetServerMDNSEntryFree(virNetServerMDNSEntryPtr entry ATTRIBUTE_UNUSED)
{
VIR_DEBUG("%s", _(unsupported));
}
#endif /* ! WITH_AVAHI */
|