-
Notifications
You must be signed in to change notification settings - Fork 44
/
airscan-zeroconf.c
1491 lines (1231 loc) · 38.7 KB
/
airscan-zeroconf.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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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
*
* ZeroConf (device discovery)
*/
#include "airscan.h"
#include <arpa/inet.h>
#include <net/if.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <string.h>
/******************** Constants *********************/
/* Max time to wait until device table is ready, in milliseconds
*/
#define ZEROCONF_READY_TIMEOUT 5000
/******************** Local Types *********************/
/* zeroconf_device represents a single device
*/
struct zeroconf_device {
unsigned int devid; /* Unique ident */
uuid uuid; /* Device UUID */
ip_addrset *addrs; /* Device's addresses */
const char *mdns_name; /* Device's MDNS name, NULL for WSDD */
const char *model; /* Device model name */
unsigned int protocols; /* Supported protocols, set of 1<<ID_PROTO */
unsigned int methods; /* How device was discovered, set of
1 << ZEROCONF_METHOD */
ll_node node_list; /* In zeroconf_device_list */
ll_head findings; /* zeroconf_finding, by method */
zeroconf_device *buddy; /* "Buddy" device, MDNS vs WSDD */
};
/* Global variables
*/
log_ctx *zeroconf_log;
/* Static variables
*/
static ll_head zeroconf_device_list;
static pthread_cond_t zeroconf_initscan_cond;
static int zeroconf_initscan_bits;
static eloop_timer *zeroconf_initscan_timer;
/******************** Forward declarations *********************/
static zeroconf_endpoint*
zeroconf_endpoint_copy_single (const zeroconf_endpoint *endpoint);
static const char*
zeroconf_ident_split (const char *ident, unsigned int *devid, ID_PROTO *proto);
/******************** Discovery methods *********************/
/* Map ZEROCONF_METHOD to ID_PROTO
*/
static ID_PROTO
zeroconf_method_to_proto (ZEROCONF_METHOD method)
{
switch (method) {
case ZEROCONF_MDNS_HINT:
return ID_PROTO_UNKNOWN;
case ZEROCONF_USCAN_TCP:
case ZEROCONF_USCANS_TCP:
return ID_PROTO_ESCL;
case ZEROCONF_WSD:
return ID_PROTO_WSD;
case NUM_ZEROCONF_METHOD:
break;
}
return ID_PROTO_UNKNOWN;
}
/* Get ZEROCONF_METHOD, for debugging
*/
static const char*
zeroconf_method_name (ZEROCONF_METHOD method)
{
switch (method) {
case ZEROCONF_MDNS_HINT: return "ZEROCONF_MDNS_HINT";
case ZEROCONF_USCAN_TCP: return "ZEROCONF_USCAN_TCP";
case ZEROCONF_USCANS_TCP: return "ZEROCONF_USCANS_TCP";
case ZEROCONF_WSD: return "ZEROCONF_WSD";
case NUM_ZEROCONF_METHOD:
break;
}
return NULL;
}
/******************** Devices *********************/
/* Add new zeroconf_device
*/
static zeroconf_device*
zeroconf_device_add (zeroconf_finding *finding)
{
zeroconf_device *device = mem_new(zeroconf_device, 1);
device->devid = devid_alloc();
device->uuid = finding->uuid;
device->addrs = ip_addrset_new();
if (finding->name != NULL) {
device->mdns_name = str_dup(finding->name);
}
device->model = finding->model;
ll_init(&device->findings);
ll_push_end(&zeroconf_device_list, &device->node_list);
return device;
}
/* Delete the device
*/
static void
zeroconf_device_del (zeroconf_device *device)
{
ll_del(&device->node_list);
ip_addrset_free(device->addrs);
mem_free((char*) device->mdns_name);
devid_free(device->devid);
mem_free(device);
}
/* Check if device is MDNS device
*/
static bool
zeroconf_device_is_mdns (zeroconf_device *device)
{
return device->mdns_name != NULL;
}
/* Rebuild device->ifaces, device->protocols and device->methods
*/
static void
zeroconf_device_rebuild_sets (zeroconf_device *device)
{
ll_node *node;
device->protocols = 0;
device->methods = 0;
for (LL_FOR_EACH(node, &device->findings)) {
zeroconf_finding *finding;
ID_PROTO proto;
finding = OUTER_STRUCT(node, zeroconf_finding, list_node);
proto = zeroconf_method_to_proto(finding->method);
if (proto != ID_PROTO_UNKNOWN) {
device->protocols |= 1 << proto;
}
device->methods |= 1 << finding->method;
}
}
/* Update device->model
*/
static void
zeroconf_device_update_model (zeroconf_device *device)
{
ll_node *node;
zeroconf_finding *hint = NULL, *wsd = NULL;
for (LL_FOR_EACH(node, &device->findings)) {
zeroconf_finding *finding;
finding = OUTER_STRUCT(node, zeroconf_finding, list_node);
switch (finding->method) {
case ZEROCONF_USCAN_TCP:
case ZEROCONF_USCANS_TCP:
device->model = finding->model;
return;
case ZEROCONF_MDNS_HINT:
if (hint == NULL) {
hint = finding;
}
break;
case ZEROCONF_WSD:
if (wsd == NULL) {
wsd = finding;
}
break;
default:
log_internal_error(zeroconf_log);
}
}
device->model = hint ? hint->model : wsd->model;
}
/* Add zeroconf_finding to zeroconf_device
*/
static void
zeroconf_device_add_finding (zeroconf_device *device,
zeroconf_finding *finding)
{
log_assert(zeroconf_log, finding->device == NULL);
finding->device = device;
ll_push_end(&device->findings, &finding->list_node);
ip_addrset_merge(device->addrs, finding->addrs);
if (finding->endpoints != NULL) {
ID_PROTO proto = zeroconf_method_to_proto(finding->method);
if (proto != ID_PROTO_UNKNOWN) {
device->protocols |= 1 << proto;
}
device->methods |= 1 << finding->method;
}
zeroconf_device_update_model(device);
}
/* Delete zeroconf_finding from zeroconf_device
*/
static void
zeroconf_device_del_finding (zeroconf_finding *finding)
{
zeroconf_device *device = finding->device;
log_assert(zeroconf_log, device != NULL);
ll_del(&finding->list_node);
if (ll_empty(&device->findings)) {
zeroconf_device_del(device);
return;
}
zeroconf_device_rebuild_sets(device);
zeroconf_device_update_model(device);
}
/* Get model name
*/
static const char*
zeroconf_device_model (zeroconf_device *device)
{
if (device->model != NULL) {
return device->model;
}
/* If model name is not available, fall back to UUID */
return device->uuid.text;
}
/* Get device name
*/
static const char*
zeroconf_device_name (zeroconf_device *device)
{
if (zeroconf_device_is_mdns(device)) {
return device->mdns_name;
}
if (device->buddy != NULL) {
return device->buddy->mdns_name;
}
return zeroconf_device_model(device);
}
/* Get protocols, exposed by device
*/
static unsigned int
zeroconf_device_protocols (zeroconf_device *device)
{
unsigned int protocols = device->protocols;
if (!conf.proto_auto) {
return protocols;
}
if ((protocols & (1 << ID_PROTO_ESCL)) != 0) {
return 1 << ID_PROTO_ESCL;
}
if ((protocols & (1 << ID_PROTO_WSD)) != 0) {
return 1 << ID_PROTO_WSD;
}
return 0;
}
/* Get device endpoints.
* Caller is responsible to free the returned list
*/
static zeroconf_endpoint*
zeroconf_device_endpoints (zeroconf_device *device, ID_PROTO proto)
{
zeroconf_endpoint *endpoints = NULL;
ll_node *node;
for (LL_FOR_EACH(node, &device->findings)) {
zeroconf_finding *finding;
finding = OUTER_STRUCT(node, zeroconf_finding, list_node);
if (zeroconf_method_to_proto(finding->method) == proto) {
zeroconf_endpoint *ep, *ep2;
for (ep = finding->endpoints; ep != NULL; ep = ep->next) {
ep2 = zeroconf_endpoint_copy_single(ep);
ep2->next = endpoints;
endpoints = ep2;
}
}
}
return zeroconf_endpoint_list_sort_dedup(endpoints);
}
/* Find zeroconf_device by ident
* Protocol, encoded into ident, returned via second parameter
*/
static zeroconf_device*
zeroconf_device_find_by_ident (const char *ident, ID_PROTO *proto)
{
unsigned int devid;
const char *name;
ll_node *node;
zeroconf_device *device = NULL;
name = zeroconf_ident_split(ident, &devid, proto);
if (name == NULL) {
return NULL;
}
/* Lookup device */
for (LL_FOR_EACH(node, &zeroconf_device_list)) {
device = OUTER_STRUCT(node, zeroconf_device, node_list);
if (device->devid == devid &&
!strcmp(name, zeroconf_device_name(device))) {
break;
}
}
if (device == NULL)
return NULL;
/* Check that device supports requested protocol */
if ((device->protocols & (1 << *proto)) != 0) {
return device;
}
return NULL;
}
/* Check if device is blacklisted
*
* Returns reason string, if device is blacklisted, NULL, if not
*/
static const char*
zeroconf_device_is_blacklisted (zeroconf_device *device)
{
conf_blacklist *ent;
const char *name, *model;
if (conf.blacklist == NULL) {
return NULL;
}
name = zeroconf_device_name(device);
model = zeroconf_device_model(device);
for (ent = conf.blacklist; ent != NULL; ent = ent->next) {
if (ent->name != NULL && !fnmatch(ent->name, name, 0)) {
return "name";
}
if (ent->model != NULL && !fnmatch(ent->model, model, 0)) {
return "model";
}
if (ent->net.addr.af != AF_UNSPEC &&
ip_addrset_on_network(device->addrs, ent->net)) {
return "address";
}
}
return NULL;
}
/******************** Merging devices *********************/
/* Recompute device->buddy for all devices
*/
static void
zeroconf_merge_recompute_buddies (void)
{
ll_node *node, *node2;
zeroconf_device *device, *device2;
for (LL_FOR_EACH(node, &zeroconf_device_list)) {
device = OUTER_STRUCT(node, zeroconf_device, node_list);
device->buddy = NULL;
}
for (LL_FOR_EACH(node, &zeroconf_device_list)) {
device = OUTER_STRUCT(node, zeroconf_device, node_list);
for (node2 = ll_next(&zeroconf_device_list, node); node2 != NULL;
node2 = ll_next(&zeroconf_device_list, node2)) {
device2 = OUTER_STRUCT(node2, zeroconf_device, node_list);
if (zeroconf_device_is_mdns(device) !=
zeroconf_device_is_mdns(device2)) {
if (ip_addrset_is_intersect(device->addrs, device2->addrs)) {
device->buddy = device2;
device2->buddy = device;
}
}
}
}
}
/* Check that new finding should me merged with existent device
*/
static bool
zeroconf_merge_check (zeroconf_device *device, zeroconf_finding *finding)
{
if ((device->mdns_name == NULL) != (finding->name == NULL)) {
return false;
}
if (device->mdns_name != NULL &&
strcasecmp(device->mdns_name, finding->name)) {
return false;
}
if (uuid_equal(device->uuid, finding->uuid)) {
return true;
}
return false;
}
/* Find device, suitable for merging with specified finding
*/
static zeroconf_device*
zeroconf_merge_find (zeroconf_finding *finding)
{
ll_node *node;
for (LL_FOR_EACH(node, &zeroconf_device_list)) {
zeroconf_device *device;
device = OUTER_STRUCT(node, zeroconf_device, node_list);
if (zeroconf_merge_check(device, finding)) {
return device;
}
}
return NULL;
}
/******************** Ident Strings *********************/
/* Encode ID_PROTO for device ident
*/
static char
zeroconf_ident_proto_encode (ID_PROTO proto)
{
switch (proto) {
case ID_PROTO_ESCL: return 'e';
case ID_PROTO_WSD: return 'w';
case ID_PROTO_UNKNOWN:
case NUM_ID_PROTO:
break;
}
log_internal_error(zeroconf_log);
return 0;
}
/* Decode ID_PROTO from device ident
*/
static ID_PROTO
zeroconf_ident_proto_decode (char c)
{
switch (c) {
case 'e': return ID_PROTO_ESCL;
case 'w': return ID_PROTO_WSD;
}
return ID_PROTO_UNKNOWN;
}
/* Make device ident string
* The returned string must be released with mem_free()
*/
static const char*
zeroconf_ident_make (const char *name, unsigned int devid, ID_PROTO proto)
{
return str_printf("%c%x:%s", zeroconf_ident_proto_encode(proto),
devid, name);
}
/* Split device ident string.
* Returns NULL on error, device name on success.
* Device name points somewhere into the input buffer
*/
static const char*
zeroconf_ident_split (const char *ident, unsigned int *devid, ID_PROTO *proto)
{
const char *name;
char *end;
/* Find name */
name = strchr(ident, ':');
if (name == NULL) {
return NULL;
}
name ++;
/* Decode proto and devid */
*proto = zeroconf_ident_proto_decode(*ident);
if (*proto == ID_PROTO_UNKNOWN) {
return NULL;
}
ident ++;
*devid = (unsigned int) strtoul(ident, &end, 16);
if (end == ident || *end != ':') {
return NULL;
}
return name;
}
/******************** Endpoints *********************/
/* Create new zeroconf_endpoint. Newly created endpoint
* takes ownership of uri string
*/
zeroconf_endpoint*
zeroconf_endpoint_new (ID_PROTO proto, http_uri *uri)
{
zeroconf_endpoint *endpoint = mem_new(zeroconf_endpoint, 1);
endpoint->proto = proto;
endpoint->uri = uri;
if (proto == ID_PROTO_ESCL) {
// We own the uri, so modify without making a separate copy.
http_uri_fix_end_slash(endpoint->uri);
}
return endpoint;
}
/* Clone a single zeroconf_endpoint
*/
static zeroconf_endpoint*
zeroconf_endpoint_copy_single (const zeroconf_endpoint *endpoint)
{
zeroconf_endpoint *endpoint2 = mem_new(zeroconf_endpoint, 1);
*endpoint2 = *endpoint;
endpoint2->uri = http_uri_clone(endpoint->uri);
endpoint2->next = NULL;
return endpoint2;
}
/* Free single zeroconf_endpoint
*/
void
zeroconf_endpoint_free_single (zeroconf_endpoint *endpoint)
{
http_uri_free(endpoint->uri);
mem_free(endpoint);
}
/* Create a copy of zeroconf_endpoint list
*/
zeroconf_endpoint*
zeroconf_endpoint_list_copy (const zeroconf_endpoint *list)
{
zeroconf_endpoint *newlist = NULL, *last = NULL, *endpoint;
while (list != NULL) {
endpoint = zeroconf_endpoint_copy_single(list);
if (last != NULL) {
last->next = endpoint;
} else {
newlist = endpoint;
}
last = endpoint;
list = list->next;
}
return newlist;
}
/* Free zeroconf_endpoint list
*/
void
zeroconf_endpoint_list_free (zeroconf_endpoint *list)
{
while (list != NULL) {
zeroconf_endpoint *next = list->next;
zeroconf_endpoint_free_single(list);
list = next;
}
}
/* Compare two endpoints, for sorting
*/
static int
zeroconf_endpoint_cmp (const zeroconf_endpoint *e1, const zeroconf_endpoint *e2)
{
const struct sockaddr *a1 = http_uri_addr(e1->uri);
const struct sockaddr *a2 = http_uri_addr(e2->uri);
if (a1 != NULL && a2 != NULL) {
bool ll1 = ip_sockaddr_is_linklocal(a1);
bool ll2 = ip_sockaddr_is_linklocal(a2);
int cmp;
/* Prefer directly reachable addresses */
cmp = netif_distance_cmp(a1, a2);
if (cmp != 0) {
return cmp;
}
/* Prefer normal addresses, rather that link-local */
if (ll1 != ll2) {
return ll1 ? 1 : -1;
}
/* Be in trend: prefer IPv6 addresses */
if (a1->sa_family != a2->sa_family) {
return a1->sa_family == AF_INET6 ? -1 : 1;
}
}
/* Otherwise, sort lexicographically */
return strcmp(http_uri_str(e1->uri), http_uri_str(e2->uri));
}
/* Revert zeroconf_endpoint list
*/
static zeroconf_endpoint*
zeroconf_endpoint_list_revert (zeroconf_endpoint *list)
{
zeroconf_endpoint *prev = NULL, *next;
while (list != NULL) {
next = list->next;
list->next = prev;
prev = list;
list = next;
}
return prev;
}
/* Sort list of endpoints
*/
zeroconf_endpoint*
zeroconf_endpoint_list_sort (zeroconf_endpoint *list)
{
zeroconf_endpoint *halves[2] = {NULL, NULL};
int half = 0;
if (list == NULL || list->next == NULL) {
return list;
}
/* Split list into halves */
while (list != NULL) {
zeroconf_endpoint *next = list->next;
list->next = halves[half];
halves[half] = list;
half ^= 1;
list = next;
}
/* Sort each half, recursively */
for (half = 0; half < 2; half ++) {
halves[half] = zeroconf_endpoint_list_sort(halves[half]);
}
/* Now merge the sorted halves */
list = NULL;
while (halves[0] != NULL || halves[1] != NULL) {
zeroconf_endpoint *next;
if (halves[0] == NULL) {
half = 1;
} else if (halves[1] == NULL) {
half = 0;
} else if (zeroconf_endpoint_cmp(halves[0], halves[1]) < 0) {
half = 0;
} else {
half = 1;
}
next = halves[half]->next;
halves[half]->next = list;
list = halves[half];
halves[half] = next;
}
/* And revert the list, as after merging it is reverted */
return zeroconf_endpoint_list_revert(list);
}
/* Sort list of endpoints and remove duplicates
*/
zeroconf_endpoint*
zeroconf_endpoint_list_sort_dedup (zeroconf_endpoint *list)
{
zeroconf_endpoint *addr, *next;
if (list == NULL) {
return NULL;
}
list = zeroconf_endpoint_list_sort(list);
addr = list;
while ((next = addr->next) != NULL) {
if (zeroconf_endpoint_cmp(addr, next) == 0) {
addr->next = next->next;
zeroconf_endpoint_free_single(next);
} else {
addr = next;
}
}
return list;
}
/* Check if list of endpoints already contains the given
* endpoint (i.e., endpoint with the same URI and protocol)
*/
bool
zeroconf_endpoint_list_contains (const zeroconf_endpoint *list,
const zeroconf_endpoint *endpoint)
{
while (list != NULL) {
if (list->proto == endpoint->proto &&
http_uri_equal(list->uri, endpoint->uri)) {
return true;
}
list = list->next;
}
return false;
}
/* Check if endpoints list contains a non-link-local address
* of the specified address family
*/
bool
zeroconf_endpoint_list_has_non_link_local_addr (int af,
const zeroconf_endpoint *list)
{
for (;list != NULL; list = list->next) {
const struct sockaddr *addr = http_uri_addr(list->uri);
if (addr != NULL && addr->sa_family == af) {
if (!ip_sockaddr_is_linklocal(addr)) {
return true;
}
}
}
return false;
}
/******************** Static configuration *********************/
/* Look for device's static configuration by device name
*/
static conf_device*
zeroconf_find_static_by_name (const char *name)
{
conf_device *dev_conf;
for (dev_conf = conf.devices; dev_conf != NULL; dev_conf = dev_conf->next) {
if (!strcasecmp(dev_conf->name, name)) {
return dev_conf;
}
}
return NULL;
}
/* Look for device's static configuration by device ident
*/
static conf_device*
zeroconf_find_static_by_ident (const char *ident)
{
conf_device *dev_conf;
ID_PROTO proto;
unsigned int devid;
const char *name;
name = zeroconf_ident_split(ident, &devid, &proto);
if (name == NULL) {
return NULL;
}
for (dev_conf = conf.devices; dev_conf != NULL; dev_conf = dev_conf->next) {
if (dev_conf->devid == devid &&
dev_conf->proto == proto &&
!strcmp(dev_conf->name, name)) {
return dev_conf;
}
}
return NULL;
}
/***** Miscellaneous functions for zeroconf_finding *****/
/* Compare two pointers to pointers to zeroconf_finding (zeroconf_finding**)
* by index+name, for qsort
*/
int
zeroconf_finding_qsort_by_index_name (const void *p1, const void *p2)
{
const zeroconf_finding *f1 = *(const zeroconf_finding * const *) p1;
const zeroconf_finding *f2 = *(const zeroconf_finding * const *) p2;
if (f1->ifindex < f2->ifindex) {
return -1;
}
if (f1->ifindex > f2->ifindex) {
return 1;
}
return strcmp(f1->name, f2->name);
}
/******************** Events from discovery providers *********************/
/* Publish the zeroconf_finding.
*/
void
zeroconf_finding_publish (zeroconf_finding *finding)
{
size_t count, i;
zeroconf_device *device;
char ifname[IF_NAMESIZE];
const ip_addr *addrs;
ID_PROTO proto = zeroconf_method_to_proto(finding->method);
/* Print log messages */
if (if_indextoname(finding->ifindex, ifname) == NULL) {
strcpy(ifname, "?");
}
log_debug(zeroconf_log, "found %s", finding->uuid.text);
log_debug(zeroconf_log, " method: %s",
zeroconf_method_name(finding->method));
log_debug(zeroconf_log, " interface: %d (%s)", finding->ifindex, ifname);
log_debug(zeroconf_log, " name: %s",
finding->name ? finding->name : "-");
log_debug(zeroconf_log, " model: %s",
finding->model ? finding->model : "-");
log_debug(zeroconf_log, " addresses:");
addrs = ip_addrset_addresses(finding->addrs, &count);
for (i = 0; i < count; i ++) {
ip_straddr straddr = ip_addr_to_straddr(addrs[i], true);
log_debug(zeroconf_log, " %s", straddr.text);
}
if (proto != ID_PROTO_UNKNOWN) {
zeroconf_endpoint *ep;
log_debug(zeroconf_log, " protocol: %s", id_proto_name(proto));
log_debug(zeroconf_log, " endpoints:");
for (ep = finding->endpoints; ep != NULL; ep = ep->next) {
log_debug(zeroconf_log, " %s", http_uri_str(ep->uri));
}
}
/* Handle new finding */
device = zeroconf_merge_find(finding);
if (device != NULL) {
log_debug(zeroconf_log, " device: %4.4x (found)", device->devid);
} else {
device = zeroconf_device_add(finding);
log_debug(zeroconf_log, " device: %4.4x (created)", device->devid);
}
zeroconf_device_add_finding(device, finding);
zeroconf_merge_recompute_buddies();
pthread_cond_broadcast(&zeroconf_initscan_cond);
}
/* Withdraw the finding
*/
void
zeroconf_finding_withdraw (zeroconf_finding *finding)
{
char ifname[IF_NAMESIZE] = "?";
if_indextoname(finding->ifindex, ifname);
log_debug(zeroconf_log, "device gone %s", finding->uuid.text);
log_debug(zeroconf_log, " method: %s", zeroconf_method_name(finding->method));
log_debug(zeroconf_log, " interface: %d (%s)", finding->ifindex, ifname);
zeroconf_device_del_finding(finding);
zeroconf_merge_recompute_buddies();
pthread_cond_broadcast(&zeroconf_initscan_cond);
}
/* Notify zeroconf subsystem that initial scan
* for the method is done
*/
void
zeroconf_finding_done (ZEROCONF_METHOD method)
{
log_debug(zeroconf_log, "%s: initial scan finished",
zeroconf_method_name(method));
zeroconf_initscan_bits &= ~(1 << method);
pthread_cond_broadcast(&zeroconf_initscan_cond);
}
/******************** Support for SANE API *********************/
/* zeroconf_initscan_timer callback
*/
static void
zeroconf_initscan_timer_callback (void *unused)
{
(void) unused;
log_debug(zeroconf_log, "initial scan timer expired");
mdns_initscan_timer_expired();
wsdd_initscan_timer_expired();
zeroconf_initscan_timer = NULL;
pthread_cond_broadcast(&zeroconf_initscan_cond);
}
/* Check if initial scan is done
*/
static bool
zeroconf_initscan_done (void)
{
ll_node *node;
zeroconf_device *device;
/* If all discovery methods are done, we are done */
if (zeroconf_initscan_bits == 0) {
return true;
}
/* Regardless of options, all DNS-SD methods must be done */
if ((zeroconf_initscan_bits & ~(1 << ZEROCONF_WSD)) != 0) {
log_debug(zeroconf_log, "device_list wait: DNS-SD not finished...");
return false;
}
/* If we are here, ZEROCONF_WSD is not done yet,
* and if we are not in fast-wsdd mode, we must wait
*/
log_assert(zeroconf_log,
(zeroconf_initscan_bits & (1 << ZEROCONF_WSD)) != 0);
if (conf.wsdd_mode != WSDD_FAST) {
log_debug(zeroconf_log, "device_list wait: WSDD not finished...");
return false;
}
/* Check for completion, device by device:
*
* In manual protocol switch mode, WSDD buddy must be
* found for device, so we have a choice. Otherwise, it's
* enough if device has supported protocols
*/
for (LL_FOR_EACH(node, &zeroconf_device_list)) {
device = OUTER_STRUCT(node, zeroconf_device, node_list);
if (!conf.proto_auto) {
if (zeroconf_device_is_mdns(device) && device->buddy == NULL) {
log_debug(zeroconf_log,
"device_list wait: waiting for WSDD buddy for '%s' (%d)",
zeroconf_device_name(device), device->devid);