forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan-mdns.c
908 lines (752 loc) · 23.2 KB
/
airscan-mdns.c
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* MDNS device discovery
*/
#include "airscan.h"
#include <arpa/inet.h>
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-common/error.h>
#include <string.h>
#include <stdarg.h>
/******************** Constants *********************/
/* If failed, AVAHI client will be automatically
* restarted after the following timeout expires,
* in seconds
*/
#define MDNS_AVAHI_CLIENT_RESTART_TIMEOUT 1
/* Max time to wait until device table is ready, in seconds
*/
#define MDNS_READY_TIMEOUT 5
/* MDNS_SERVICE represents numerical identifiers for
* DNS-SD service types we are interested in
*/
typedef enum {
MDNS_SERVICE_UNKNOWN = -1,
MDNS_SERVICE_IPP_TCP, /* _ipp._tcp */
MDNS_SERVICE_IPPS_TCP, /* _ipps._tcp */
MDNS_SERVICE_USCAN_TCP, /* _uscan._tcp */
MDNS_SERVICE_USCANS_TCP, /* _uscans._tcp */
MDNS_SERVICE_SCANNER_TCP, /* _scanner._tcp */
NUM_MDNS_SERVICE
} MDNS_SERVICE;
/******************** Local Types *********************/
/* mdns_finding represents zeroconf_finding for MDNS
* device discovery
*/
typedef struct {
zeroconf_finding finding; /* Base class */
GPtrArray *resolvers; /* Array of pending AvahiServiceResolver */
ll_node node_list; /* In mdns_finding_list */
bool should_publish; /* Should we publish this finding */
bool is_published; /* Finding actually published */
bool initscan; /* Device discovered during initial scan */
} mdns_finding;
/* Static variables
*/
static log_ctx *mdns_log;
static ll_head mdns_finding_list;
static AvahiGLibPoll *mdns_avahi_glib_poll;
static const AvahiPoll *mdns_avahi_poll;
static AvahiTimeout *mdns_avahi_restart_timer;
static AvahiClient *mdns_avahi_client;
static bool mdns_avahi_browser_running;
static AvahiServiceBrowser *mdns_avahi_browser[NUM_MDNS_SERVICE];
static bool mdns_initscan[NUM_MDNS_SERVICE];
static int mdns_initscan_count[NUM_ZEROCONF_METHOD];
/* Forward declarations
*/
static void
mdns_avahi_browser_stop (void);
static void
mdns_avahi_client_start (void);
static void
mdns_avahi_client_restart_defer (void);
/* Print debug message
*/
static void
mdns_debug (const char *name, AvahiProtocol protocol, const char *action,
const char *fmt, ...)
{
char prefix[128], message[1024];
va_list ap;
const char *af = protocol == AVAHI_PROTO_INET ? "ipv4" : "ipv6";
int n;
n = snprintf(prefix, sizeof(prefix), "%s/%s", action, af);
if (name != NULL) {
snprintf(prefix + n, sizeof(prefix) - n, " \"%s\"", name);
}
va_start(ap, fmt);
vsnprintf(message, sizeof(message) - n, fmt, ap);
va_end(ap);
log_debug(mdns_log, "%s: %s", prefix, message);
}
/* Print error message
*/
static void
mdns_perror (const char *name, AvahiProtocol protocol, const char *action)
{
mdns_debug(name, protocol, action,
avahi_strerror(avahi_client_errno(mdns_avahi_client)));
}
/* Get MDNS_SERVICE name
*/
static const char*
mdns_service_name (MDNS_SERVICE service)
{
switch (service) {
case MDNS_SERVICE_IPP_TCP: return "_ipp._tcp";
case MDNS_SERVICE_IPPS_TCP: return "_ipps._tcp";
case MDNS_SERVICE_USCAN_TCP: return "_uscan._tcp";
case MDNS_SERVICE_USCANS_TCP: return "_uscans._tcp";
case MDNS_SERVICE_SCANNER_TCP: return "_scanner._tcp";
case MDNS_SERVICE_UNKNOWN:
case NUM_MDNS_SERVICE:
break;
}
log_internal_error(mdns_log);
return NULL;
}
/* Get MDNS_SERVICE by name
*/
static MDNS_SERVICE
mdns_service_by_name (const char *name)
{
int i;
for (i = 0; i < NUM_MDNS_SERVICE; i ++) {
if (!strcasecmp(name, mdns_service_name(i))) {
return i;
}
}
return MDNS_SERVICE_UNKNOWN;
}
/* Map MDNS_SERVICE to ZEROCONF_METHOD
*/
static ZEROCONF_METHOD
mdns_service_to_method (MDNS_SERVICE service)
{
switch (service) {
case MDNS_SERVICE_USCAN_TCP: return ZEROCONF_USCAN_TCP;
case MDNS_SERVICE_USCANS_TCP: return ZEROCONF_USCANS_TCP;
default: return ZEROCONF_MDNS_HINT;
}
}
/* Get AvahiResolverEvent name, for debugging
*/
static const char*
mdns_avahi_resolver_event_name (AvahiResolverEvent e)
{
static char buf[64];
switch (e) {
case AVAHI_RESOLVER_FOUND: return "AVAHI_RESOLVER_FOUND";
case AVAHI_RESOLVER_FAILURE: return "AVAHI_RESOLVER_FAILURE";
}
/* Safe, because used only from the work thread */
sprintf(buf, "AVAHI_RESOLVER_UNKNOWN(%d)", e);
return buf;
}
/* Get AvahiBrowserEvent name, for debugging
*/
static const char*
mdns_avahi_browser_event_name (AvahiBrowserEvent e)
{
static char buf[64];
switch (e) {
case AVAHI_BROWSER_NEW: return "AVAHI_BROWSER_NEW";
case AVAHI_BROWSER_REMOVE: return "AVAHI_BROWSER_REMOVE";
case AVAHI_BROWSER_CACHE_EXHAUSTED: return "AVAHI_BROWSER_CACHE_EXHAUSTED";
case AVAHI_BROWSER_ALL_FOR_NOW: return "AVAHI_BROWSER_ALL_FOR_NOW";
case AVAHI_BROWSER_FAILURE: return "AVAHI_BROWSER_FAILURE";
}
/* Safe, because used only from the work thread */
sprintf(buf, "AVAHI_BROWSER_UNKNOWN(%d)", e);
return buf;
}
/* Get AvahiClientState name, for debugging
*/
static const char*
mdns_avahi_client_state_name (AvahiClientState s)
{
static char buf[64];
switch (s) {
case AVAHI_CLIENT_S_REGISTERING: return "AVAHI_CLIENT_S_REGISTERING";
case AVAHI_CLIENT_S_RUNNING: return "AVAHI_CLIENT_S_RUNNING";
case AVAHI_CLIENT_S_COLLISION: return "AVAHI_CLIENT_S_COLLISION";
case AVAHI_CLIENT_FAILURE: return "AVAHI_CLIENT_FAILURE";
case AVAHI_CLIENT_CONNECTING: return "AVAHI_CLIENT_CONNECTING";
}
/* Safe, because used only from the work thread */
sprintf(buf, "AVAHI_BROWSER_UNKNOWN(%d)", s);
return buf;
}
/* Increment count of initial scan tasks
*/
static void
mdns_initscan_count_inc (ZEROCONF_METHOD method)
{
mdns_initscan_count[method] ++;
}
/* Decrement count if initial scan tasks
*/
static void
mdns_initscan_count_dec (ZEROCONF_METHOD method)
{
log_assert(mdns_log, mdns_initscan_count[method] > 0);
mdns_initscan_count[method] --;
if (mdns_initscan_count[method] == 0) {
zeroconf_finding_done(method);
}
}
/* Create new mdns_finding structure
*/
static mdns_finding*
mdns_finding_new (ZEROCONF_METHOD method, int ifindex, const char *name,
bool initscan)
{
mdns_finding *mdns = g_new0(mdns_finding, 1);
mdns->finding.method = method;
mdns->finding.ifindex = ifindex;
mdns->finding.name = g_strdup(name);
mdns->resolvers = g_ptr_array_new();
mdns->initscan = initscan;
if (mdns->initscan) {
mdns_initscan_count_inc(mdns->finding.method);
}
return mdns;
}
/* Free mdns_finding structure
*/
static void
mdns_finding_free (mdns_finding *mdns)
{
g_free((char*) mdns->finding.name);
g_free((char*) mdns->finding.model);
zeroconf_endpoint_list_free(mdns->finding.endpoints);
if (mdns->initscan) {
mdns_initscan_count_dec(mdns->finding.method);
}
g_ptr_array_free(mdns->resolvers, TRUE);
g_free(mdns);
}
/* Find mdns_finding
*/
static mdns_finding*
mdns_finding_find (ZEROCONF_METHOD method, int ifindex, const char *name)
{
ll_node *node;
for (LL_FOR_EACH(node, &mdns_finding_list)) {
mdns_finding *mdns;
mdns = OUTER_STRUCT(node, mdns_finding, node_list);
if (mdns->finding.method == method &&
mdns->finding.ifindex == ifindex &&
!strcasecmp(mdns->finding.name, name)) {
return mdns;
}
}
return NULL;
}
/* Get mdns_finding: find existing or add a new one
*/
static mdns_finding*
mdns_finding_get (ZEROCONF_METHOD method, int ifindex, const char *name,
bool initscan)
{
mdns_finding *mdns;
/* Check for duplicated device */
mdns = mdns_finding_find(method, ifindex, name);
if (mdns != NULL) {
return mdns;
}
/* Add new mdns_finding state */
mdns = mdns_finding_new(method, ifindex, name, initscan);
ll_push_end(&mdns_finding_list, &mdns->node_list);
return mdns;
}
/* Kill pending resolvers
*/
static void
mdns_finding_kill_resolvers (mdns_finding *mdns)
{
while (mdns->resolvers->len > 0) {
avahi_service_resolver_free(mdns->resolvers->pdata[0]);
g_ptr_array_remove_index(mdns->resolvers, 0);
}
}
/* Del the mdns_finding
*/
static void
mdns_finding_del (mdns_finding *mdns)
{
if (mdns->is_published) {
zeroconf_finding_withdraw(&mdns->finding);
}
ll_del(&mdns->node_list);
mdns_finding_kill_resolvers(mdns);
mdns_finding_free(mdns);
}
/* Delete all mdns_finding
*/
static void
mdns_finding_del_all (void)
{
ll_node *node;
while ((node = ll_first(&mdns_finding_list)) != NULL) {
mdns_finding *mdns;
mdns = OUTER_STRUCT(node, mdns_finding, node_list);
mdns_finding_del(mdns);
}
}
/* Make zeroconf_endpoint for eSCL
*/
static zeroconf_endpoint*
mdns_make_escl_endpoint (ZEROCONF_METHOD method, const AvahiAddress *addr,
uint16_t port, const char *rs, AvahiIfIndex interface)
{
char str_addr[128];
int rs_len;
char *u;
http_uri *uri;
const char *scheme;
if (method == ZEROCONF_USCAN_TCP) {
scheme = "http";
} else {
scheme = "https";
}
if (addr->proto == AVAHI_PROTO_INET) {
avahi_address_snprint(str_addr, sizeof(str_addr), addr);
} else {
size_t len;
str_addr[0] = '[';
avahi_address_snprint(str_addr + 1, sizeof(str_addr) - 2, addr);
len = strlen(str_addr);
/* Connect to link-local address requires explicit scope */
if (ip_is_linklocal(AF_INET6, addr->data.data)) {
/* Percent character in the IPv6 address literal
* needs to be properly escaped, so it becomes %25
* See RFC6874 for details
*/
len += sprintf(str_addr + len, "%%25%d", interface);
}
str_addr[len++] = ']';
str_addr[len] = '\0';
}
/* Normalize rs */
rs_len = 0;
if (rs != NULL) {
while (*rs == '/') {
rs ++;
}
rs_len = (int) strlen(rs);
while (rs_len != 0 && rs[rs_len - 1] == '/') {
rs_len --;
}
}
/* Make eSCL URL */
if (rs == NULL) {
/* Assume /eSCL by default */
u = g_strdup_printf("%s://%s:%d/eSCL/", scheme, str_addr, port);
} else if (rs_len == 0) {
/* Empty rs, avoid double '/' */
u = g_strdup_printf("%s://%s:%d/", scheme, str_addr, port);
} else {
u = g_strdup_printf("%s://%s:%d/%.*s/", scheme, str_addr, port,
rs_len, rs);
}
uri = http_uri_new(u, true);
log_assert(mdns_log, uri != NULL);
g_free(u);
return zeroconf_endpoint_new(ID_PROTO_ESCL, uri);
}
/* Handle AVAHI_RESOLVER_FOUND event
*/
static void
mdns_avahi_resolver_found (mdns_finding *mdns, MDNS_SERVICE service,
AvahiStringList *txt, const AvahiAddress *addr, uint16_t port,
AvahiIfIndex interface)
{
const char *txt_ty = NULL;
const char *txt_uuid = NULL;
const char *txt_scan = NULL;
const char *txt_rs = NULL;
AvahiStringList *s;
zeroconf_endpoint *endpoint;
ZEROCONF_METHOD method = mdns->finding.method;
/* Decode TXT record */
s = avahi_string_list_find(txt, "ty");
if (s != NULL && s->size > 3) {
txt_ty = (char*) s->text + 3;
}
s = avahi_string_list_find(txt, "uuid");
if (s != NULL && s->size > 5) {
txt_uuid = (char*) s->text + 5;
}
switch (service) {
case MDNS_SERVICE_IPP_TCP:
case MDNS_SERVICE_IPPS_TCP:
s = avahi_string_list_find(txt, "scan");
if (s != NULL && s->size > 5) {
txt_scan = (char*) s->text + 5;
}
break;
default:
break;
}
switch (service) {
case MDNS_SERVICE_USCAN_TCP:
case MDNS_SERVICE_USCANS_TCP:
s = avahi_string_list_find(txt, "rs");
if (s != NULL && s->size > 3) {
txt_rs = (char*) s->text + 3;
}
break;
default:
break;
}
/* Update finding */
if (mdns->finding.model == NULL && txt_ty != NULL) {
mdns->finding.model = g_strdup(txt_ty);
}
if (!uuid_valid(mdns->finding.uuid) && txt_uuid != NULL) {
mdns->finding.uuid = uuid_parse(txt_uuid);
}
/* Handle the event */
switch (service) {
case MDNS_SERVICE_IPP_TCP:
case MDNS_SERVICE_IPPS_TCP:
if (txt_scan != NULL && !strcasecmp(txt_scan, "t")) {
mdns->should_publish = true;
}
break;
case MDNS_SERVICE_USCAN_TCP:
case MDNS_SERVICE_USCANS_TCP:
endpoint = mdns_make_escl_endpoint(method, addr, port,
txt_rs, interface);
endpoint->next = mdns->finding.endpoints;
mdns->finding.endpoints = endpoint;
mdns->should_publish = true;
break;
case MDNS_SERVICE_SCANNER_TCP:
mdns->should_publish = true;
break;
case MDNS_SERVICE_UNKNOWN:
case NUM_MDNS_SERVICE:
log_internal_error(mdns_log);
}
if (mdns->finding.method == ZEROCONF_MDNS_HINT && mdns->should_publish) {
/* We already have enough info for this kind of finding
*/
mdns_finding_kill_resolvers(mdns);
}
}
/* AVAHI service resolver callback
*/
static void
mdns_avahi_resolver_callback (AvahiServiceResolver *r,
AvahiIfIndex interface, AvahiProtocol protocol,
AvahiResolverEvent event, const char *name, const char *type,
const char *domain, const char *host_name, const AvahiAddress *addr,
uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags,
void *userdata)
{
mdns_finding *mdns = userdata;
MDNS_SERVICE service = mdns_service_by_name(type);
int af;
(void) domain;
(void) host_name;
(void) flags;
/* Print debug message */
mdns_debug(name, protocol, "resolve", "%s %s",
mdns_avahi_resolver_event_name(event), type);
if (event == AVAHI_RESOLVER_FAILURE) {
mdns_perror(name, protocol, "resolve");
}
/* Remove resolver from list of pending ones */
if (!g_ptr_array_remove(mdns->resolvers, r)) {
mdns_debug(name, protocol, "resolve", "spurious avahi callback");
return;
}
/* Handle event */
switch (event) {
case AVAHI_RESOLVER_FOUND:
mdns_avahi_resolver_found(mdns, service, txt, addr, port, interface);
break;
case AVAHI_RESOLVER_FAILURE:
break;
}
/* Perform appropriate actions, if resolving is done */
if (mdns->resolvers->len == 0) {
/* Fixup endpoints */
mdns->finding.endpoints = zeroconf_endpoint_list_sort_dedup(
mdns->finding.endpoints);
/* Fixup model and UUID */
if (mdns->finding.model == NULL) {
/* Very unlikely, just paranoia */
mdns->finding.model = g_strdup(mdns->finding.name);
}
if (!uuid_valid(mdns->finding.uuid)) {
/* Paranoia too
*
* If device UUID is not available from DNS-SD (which
* is very unlikely), we generate a synthetic UUID,
* based on device name hash
*/
mdns->finding.uuid = uuid_hash(mdns->finding.name);
}
/* Update initscan count */
if (mdns->initscan) {
mdns->initscan = false;
mdns_initscan_count_dec(mdns->finding.method);
}
/* Publish the finding */
if (mdns->should_publish && !mdns->is_published) {
mdns->is_published = true;
zeroconf_finding_publish(&mdns->finding);
}
}
/* Notify WSDD about newly discovered address */
af = addr->proto == AVAHI_PROTO_INET ? AF_INET : AF_INET6;
wsdd_send_directed_probe(interface, af, &addr->data);
}
/* AVAHI browser callback
*/
static void
mdns_avahi_browser_callback (AvahiServiceBrowser *b, AvahiIfIndex interface,
AvahiProtocol protocol, AvahiBrowserEvent event,
const char *name, const char *type, const char *domain,
AvahiLookupResultFlags flags, void* userdata)
{
mdns_finding *mdns;
MDNS_SERVICE service = (MDNS_SERVICE) userdata;
ZEROCONF_METHOD method = mdns_service_to_method(service);
bool initscan = mdns_initscan[service];
(void) b;
(void) flags;
/* Print debug message */
mdns_debug(name, protocol, "browse", "%s %s",
mdns_avahi_browser_event_name(event), type);
if (event == AVAHI_BROWSER_FAILURE) {
mdns_perror(name, protocol, "browse");
}
switch (event) {
case AVAHI_BROWSER_NEW:
/* Add a device (or lookup for already added) */
mdns = mdns_finding_get(method, interface, name, initscan);
/* Initiate resolver */
AvahiServiceResolver *r;
r = avahi_service_resolver_new(mdns_avahi_client, interface,
protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0,
mdns_avahi_resolver_callback, mdns);
if (r == NULL) {
mdns_perror(name, protocol, "resolve");
mdns_avahi_client_restart_defer();
break;
}
/* Attach resolver to device state */
g_ptr_array_add(mdns->resolvers, r);
break;
case AVAHI_BROWSER_REMOVE:
mdns = mdns_finding_find(method, interface, name);
if (mdns != NULL) {
mdns_finding_del(mdns);
}
break;
case AVAHI_BROWSER_FAILURE:
mdns_avahi_client_restart_defer();
break;
case AVAHI_BROWSER_CACHE_EXHAUSTED:
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
if (mdns_initscan[service]) {
mdns_initscan[service] = false;
mdns_initscan_count_dec(method);
}
break;
}
}
/* Start browser for specified service type
*/
static bool
mdns_avahi_browser_start_for_type (MDNS_SERVICE service, const char *type)
{
bool ok;
log_assert(mdns_log, mdns_avahi_browser[service] == NULL);
mdns_avahi_browser[service] = avahi_service_browser_new(mdns_avahi_client,
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, type, NULL,
0, mdns_avahi_browser_callback, (void*) service);
ok = mdns_avahi_browser[service] != NULL;
if (!ok) {
log_debug(mdns_log, "avahi_service_browser_new(%s): %s",
type, avahi_strerror(avahi_client_errno(mdns_avahi_client)));
}
if (ok && mdns_initscan[service]) {
mdns_initscan_count_inc(mdns_service_to_method(service));
}
return mdns_avahi_browser[service] != NULL;
}
/* Start/restart service browser
*/
static bool
mdns_avahi_browser_start (void)
{
int i;
bool ok = true;
log_assert(mdns_log, !mdns_avahi_browser_running);
for (i = 0; ok && i < NUM_MDNS_SERVICE; i ++) {
ok = mdns_avahi_browser_start_for_type(i, mdns_service_name(i));
}
mdns_avahi_browser_running = true;
return ok;
}
/* Stop service browser
*/
static void
mdns_avahi_browser_stop (void)
{
MDNS_SERVICE service;
for (service = 0; service < NUM_MDNS_SERVICE; service ++) {
if (mdns_avahi_browser[service] != NULL) {
avahi_service_browser_free(mdns_avahi_browser[service]);
mdns_avahi_browser[service] = NULL;
if (mdns_initscan[service]) {
mdns_initscan_count_dec(mdns_service_to_method(service));
}
}
}
mdns_finding_del_all();
mdns_avahi_browser_running = false;
}
/* AVAHI client callback
*/
static void
mdns_avahi_client_callback (AvahiClient *client, AvahiClientState state,
void *userdata)
{
(void) client;
(void) userdata;
log_debug(mdns_log, "%s", mdns_avahi_client_state_name(state));
switch (state) {
case AVAHI_CLIENT_S_REGISTERING:
case AVAHI_CLIENT_S_RUNNING:
case AVAHI_CLIENT_S_COLLISION:
/* Note, first callback may come before avahi_client_new()
* return, so mdns_avahi_client may be still unset.
* Fix it here
*/
mdns_avahi_client = client;
if (!mdns_avahi_browser_running) {
if (!mdns_avahi_browser_start()) {
mdns_avahi_client_restart_defer();
}
}
break;
case AVAHI_CLIENT_FAILURE:
mdns_avahi_client_restart_defer();
break;
case AVAHI_CLIENT_CONNECTING:
break;
}
}
/* Timer for differed AVAHI client restart
*/
static void
mdns_avahi_restart_timer_callback(AvahiTimeout *t, void *userdata)
{
(void) t;
(void) userdata;
mdns_avahi_client_start();
}
/* Stop AVAHI client
*/
static void
mdns_avahi_client_stop (void)
{
if (mdns_avahi_client != NULL) {
avahi_client_free(mdns_avahi_client);
mdns_avahi_client = NULL;
}
}
/* Start/restart the AVAHI client
*/
static void
mdns_avahi_client_start (void)
{
int error;
log_assert(mdns_log, mdns_avahi_client == NULL);
mdns_avahi_client = avahi_client_new (mdns_avahi_poll,
AVAHI_CLIENT_NO_FAIL, mdns_avahi_client_callback, NULL, &error);
}
/* Deferred client restart
*/
static void
mdns_avahi_client_restart_defer (void)
{
struct timeval tv;
mdns_avahi_browser_stop();
mdns_avahi_client_stop();
gettimeofday(&tv, NULL);
tv.tv_sec += MDNS_AVAHI_CLIENT_RESTART_TIMEOUT;
mdns_avahi_poll->timeout_update(mdns_avahi_restart_timer, &tv);
}
/* Initialize MDNS
*/
SANE_Status
mdns_init (void)
{
int i;
mdns_log = log_ctx_new("MDNS", zeroconf_log);
ll_init(&mdns_finding_list);
if (!conf.discovery) {
log_debug(mdns_log, "devices discovery disabled");
zeroconf_finding_done(ZEROCONF_MDNS_HINT);
zeroconf_finding_done(ZEROCONF_USCAN_TCP);
zeroconf_finding_done(ZEROCONF_USCANS_TCP);
return SANE_STATUS_GOOD;
}
for (i = 0; i < NUM_MDNS_SERVICE; i ++) {
mdns_initscan[i] = true;
}
for (i = 0; i < NUM_ZEROCONF_METHOD; i ++) {
mdns_initscan_count[i] = 0;
}
mdns_avahi_glib_poll = eloop_new_avahi_poll();
if (mdns_avahi_glib_poll == NULL) {
return SANE_STATUS_NO_MEM;
}
mdns_avahi_poll = avahi_glib_poll_get(mdns_avahi_glib_poll);
mdns_avahi_restart_timer =
mdns_avahi_poll->timeout_new(mdns_avahi_poll, NULL,
mdns_avahi_restart_timer_callback, NULL);
if (mdns_avahi_restart_timer == NULL) {
return SANE_STATUS_NO_MEM;
}
mdns_avahi_client_start();
if (mdns_avahi_client == NULL) {
return SANE_STATUS_NO_MEM;
}
return SANE_STATUS_GOOD;
}
/* Cleanup MDNS
*/
void
mdns_cleanup (void)
{
if (mdns_avahi_glib_poll != NULL) {
mdns_avahi_browser_stop();
mdns_avahi_client_stop();
mdns_finding_del_all();
if (mdns_avahi_restart_timer != NULL) {
mdns_avahi_poll->timeout_free(mdns_avahi_restart_timer);
mdns_avahi_restart_timer = NULL;
}
avahi_glib_poll_free(mdns_avahi_glib_poll);
mdns_avahi_poll = NULL;
mdns_avahi_glib_poll = NULL;
}
log_ctx_free(mdns_log);
mdns_log = NULL;
}
/* vim:ts=8:sw=4:et
*/