-
Notifications
You must be signed in to change notification settings - Fork 1
/
airscan-wsdd.c
1779 lines (1515 loc) · 50.8 KB
/
airscan-wsdd.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
*
* Web Services Dynamic Discovery (WS-Discovery)
*/
#define _GNU_SOURCE
#include "airscan.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>
#ifdef BSD
# include <net/if_dl.h>
#endif
#include <sys/socket.h>
/* Protocol times, in milliseconds
*/
#define WSDD_RETRANSMIT_MIN 100 /* Min retransmit time */
#define WSDD_RETRANSMIT_MAX 250 /* Max retransmit time */
#define WSDD_DISCOVERY_TIME 2500 /* Overall discovery time */
/* This delay is taken, if we have discovered, say, device's IPv6
* addresses and have a strong suspicion that device has not yet
* discovered IPv4 addresses as well
*/
#define WSDD_PUBLISH_DELAY 1000
/* WS-Discovery stable endpoint path
*/
#define WSDD_STABLE_ENDPOINT \
"/StableWSDiscoveryEndpoint/schemas-xmlsoap-org_ws_2005_04_discovery"
/* wsdd_resolver represents a per-interface WSDD resolver
*/
typedef struct {
int fd; /* File descriptor */
int ifindex; /* Interface index */
bool ipv6; /* We are on IPv6 */
eloop_fdpoll *fdpoll; /* Socket fdpoll */
eloop_timer *timer; /* Retransmit timer */
uint32_t total_time; /* Total elapsed time */
ip_straddr str_ifaddr; /* Interface address */
ip_straddr str_sockaddr; /* Per-interface socket address */
bool initscan; /* Initial scan in progress */
} wsdd_resolver;
/* wsdd_finding represents zeroconf_finding for WSD
* device discovery
*/
typedef struct {
zeroconf_finding finding; /* Base class */
const char *address; /* Device "address" in WS-SD sense */
ll_head xaddrs; /* List of wsdd_xaddr */
http_client *http_client; /* HTTP client */
ll_node list_node; /* In wsdd_finding_list */
eloop_timer *publish_timer; /* WSDD_PUBLISH_DELAY timer */
bool published; /* This finding is published */
} wsdd_finding;
/* wsdd_xaddr represents device transport address
*/
typedef struct {
http_uri *uri; /* Device URI */
ll_node list_node; /* In wsdd_finding::xaddrs */
} wsdd_xaddr;
/* WSDD_ACTION represents WSDD message action
*/
typedef enum {
WSDD_ACTION_UNKNOWN,
WSDD_ACTION_HELLO,
WSDD_ACTION_BYE,
WSDD_ACTION_PROBEMATCHES
} WSDD_ACTION;
/* wsdd_message represents a parsed WSDD message
*/
typedef struct {
WSDD_ACTION action; /* Message action */
const char *address; /* Endpoint reference */
ll_head xaddrs; /* List of wsdd_xaddr */
bool is_scanner; /* Device is scanner */
bool is_printer; /* Device is printer */
} wsdd_message;
/* Forward declarations
*/
static void
wsdd_message_free(wsdd_message *msg);
static void
wsdd_resolver_send_probe (wsdd_resolver *resolver);
static wsdd_resolver*
wsdd_netif_resolver_by_ifindex (int ifindex);
/* Static variables
*/
static log_ctx *wsdd_log;
static netif_notifier *wsdd_netif_notifier;
static netif_addr *wsdd_netif_addr_list;
static int wsdd_mcsock_ipv4 = -1;
static int wsdd_mcsock_ipv6 = -1;
static eloop_fdpoll *wsdd_fdpoll_ipv4;
static eloop_fdpoll *wsdd_fdpoll_ipv6;
static char wsdd_buf[65536];
static struct sockaddr_in wsdd_mcast_ipv4;
static struct sockaddr_in6 wsdd_mcast_ipv6;
static ll_head wsdd_finding_list;
static int wsdd_initscan_count;
static http_client *wsdd_http_client;
static ip_addrset *wsdd_addrs_probing;
/* WS-DD Probe template
*/
static const char *wsdd_probe_template =
"<?xml version=\"1.0\"?>"
"<soap:Envelope xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsd=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:wsdp=\"http://schemas.xmlsoap.org/ws/2006/02/devprof\">"
"<soap:Header>"
"<wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action>"
"<wsa:MessageID>%s</wsa:MessageID>"
"<wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>"
"</soap:Header>"
"<soap:Body>"
"<wsd:Probe>"
"<wsd:Types>wsdp:Device</wsd:Types>"
"</wsd:Probe>"
"</soap:Body>"
"</soap:Envelope>";
/* WS-DD Get (metadata) template
*/
static const char *wsdd_get_metadata_template =
"<?xml version=\"1.0\"?>"
"<soap:Envelope xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">"
"<soap:Header>"
"<wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/Get</wsa:Action>"
"<wsa:MessageID>%s</wsa:MessageID>"
"<wsa:To>%s</wsa:To>"
"<wsa:ReplyTo>"
"<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>"
"</wsa:ReplyTo>"
"</soap:Header>"
"<soap:Body/>"
"</soap:Envelope>";
/* XML namespace translation
*/
static const xml_ns wsdd_ns_rules[] = {
{"s", "http*://schemas.xmlsoap.org/soap/envelope"}, /* SOAP 1.1 */
{"s", "http*://www.w3.org/2003/05/soap-envelope"}, /* SOAP 1.2 */
{"d", "http*://schemas.xmlsoap.org/ws/2005/04/discovery"},
{"a", "http*://schemas.xmlsoap.org/ws/2004/08/addressing"},
{"devprof", "http*://schemas.xmlsoap.org/ws/2006/02/devprof"},
{"mex", "http*://schemas.xmlsoap.org/ws/2004/09/mex"},
{"pnpx", "http*://schemas.microsoft.com/windows/pnpx/2005/10"},
{NULL, NULL}
};
/******************** wsdd_xaddr operations ********************/
/* Create new wsdd_xaddr. Newly created wsdd_xaddr takes uri ownership
*/
static wsdd_xaddr*
wsdd_xaddr_new (http_uri *uri)
{
wsdd_xaddr *xaddr = mem_new(wsdd_xaddr, 1);
xaddr->uri = uri;
return xaddr;
}
/* Destroy wsdd_xaddr
*/
static void
wsdd_xaddr_free (wsdd_xaddr *xaddr)
{
http_uri_free(xaddr->uri);
mem_free(xaddr);
}
/* Add wsdd_xaddr to the list.
* Takes ownership on URI.
*/
static void
wsdd_xaddr_list_add (ll_head *list, http_uri *uri)
{
wsdd_xaddr *xaddr;
ll_node *node;
/* Check for duplicates */
for (LL_FOR_EACH(node, list)) {
xaddr = OUTER_STRUCT(node, wsdd_xaddr, list_node);
if (http_uri_equal(xaddr->uri, uri)) {
http_uri_free(uri);
return;
}
}
/* Add new xaddr */
xaddr = wsdd_xaddr_new(uri);
ll_push_end(list, &xaddr->list_node);
}
/* Purge list of wsdd_xaddr
*/
static void
wsdd_xaddr_list_purge (ll_head *list)
{
ll_node *node;
while ((node = ll_pop_beg(list)) != NULL) {
wsdd_xaddr *xaddr = OUTER_STRUCT(node, wsdd_xaddr, list_node);
wsdd_xaddr_free(xaddr);
}
}
/******************** wsdd_initscan_count operations ********************/
/* Increment wsdd_initscan_count
*/
static void
wsdd_initscan_count_inc (void)
{
wsdd_initscan_count ++;
}
/* Decrement wsdd_initscan_count
*/
static void
wsdd_initscan_count_dec (void)
{
log_assert(wsdd_log, wsdd_initscan_count > 0);
wsdd_initscan_count --;
if (wsdd_initscan_count == 0) {
zeroconf_finding_done(ZEROCONF_WSD);
}
}
/******************** wsdd_finding operations ********************/
/* Create new wsdd_finding
*/
static wsdd_finding*
wsdd_finding_new (int ifindex, const char *address)
{
wsdd_finding *wsdd = mem_new(wsdd_finding, 1);
wsdd->finding.method = ZEROCONF_WSD;
wsdd->finding.uuid = uuid_parse(address);
if (!uuid_valid(wsdd->finding.uuid)) {
wsdd->finding.uuid = uuid_hash(address);
}
wsdd->finding.addrs = ip_addrset_new();
wsdd->finding.ifindex = ifindex;
wsdd->address = str_dup(address);
ll_init(&wsdd->xaddrs);
wsdd->http_client = http_client_new(wsdd_log, wsdd);
return wsdd;
}
/* Destroy wsdd_finding
*/
static void
wsdd_finding_free (wsdd_finding *wsdd)
{
if (wsdd->published) {
zeroconf_finding_withdraw(&wsdd->finding);
}
http_client_cancel(wsdd->http_client);
http_client_free(wsdd->http_client);
if (wsdd->publish_timer != NULL) {
eloop_timer_cancel(wsdd->publish_timer);
}
zeroconf_endpoint_list_free(wsdd->finding.endpoints);
mem_free((char*) wsdd->address);
wsdd_xaddr_list_purge(&wsdd->xaddrs);
ip_addrset_free(wsdd->finding.addrs);
mem_free((char*) wsdd->finding.model);
mem_free((char*) wsdd->finding.name);
mem_free(wsdd);
}
/* Publish wsdd_finding
*/
static void
wsdd_finding_publish (wsdd_finding *wsdd)
{
if (wsdd->published) {
return;
}
wsdd->published = true;
wsdd->finding.endpoints = zeroconf_endpoint_list_sort_dedup(
wsdd->finding.endpoints);
if (wsdd->publish_timer != NULL) {
log_debug(wsdd_log, "\"%s\": publish-delay timer canceled",
wsdd->finding.model);
eloop_timer_cancel(wsdd->publish_timer);
wsdd->publish_timer = NULL;
}
zeroconf_finding_publish(&wsdd->finding);
}
/* WSDD_PUBLISH_DELAY timer callback
*/
static void
wsdd_finding_publish_delay_timer_callback (void *data)
{
wsdd_finding *wsdd = data;
wsdd->publish_timer = NULL;
log_debug(wsdd_log, "\"%s\": publish-delay timer expired",
wsdd->finding.model);
wsdd_finding_publish(wsdd);
}
/* Publish wsdd_finding with optional delay
*/
static void
wsdd_finding_publish_delay (wsdd_finding *wsdd)
{
bool delay = false;
if (wsdd->published) {
return;
}
/* Continue discovery, if interface has IPv4/IPv6 address,
* and we have not yet discovered address of the same address
* family of device
*
* Some devices doesn't return their IPv4 endpoints, if
* metadata is queried via IPv6, and visa versa. This is
* possible that we will finish discovery of the particular
* address family, before we'll ever know that the device
* may have address of another address family, so part
* of addresses will be never discovered (see #44 for details).
*
* To prevent this situation, we continue discovery with
* some reasonable delay, if network interface has IPv4/IPv6
* address, but device is not yet.
*/
if (netif_has_non_link_local_addr(AF_INET, wsdd->finding.ifindex) &&
!zeroconf_endpoint_list_has_non_link_local_addr(AF_INET,
wsdd->finding.endpoints)) {
log_debug(wsdd_log,
"\"%s\": IPv4 address expected but not yet discovered",
wsdd->finding.model);
delay = true;
}
if (netif_has_non_link_local_addr(AF_INET6, wsdd->finding.ifindex) &&
!zeroconf_endpoint_list_has_non_link_local_addr(AF_INET6,
wsdd->finding.endpoints)) {
log_debug(wsdd_log,
"\"%s\": IPv6 address expected but not yet discovered",
wsdd->finding.model);
delay = true;
}
if (delay) {
if (wsdd->publish_timer == NULL) {
wsdd->publish_timer = eloop_timer_new(WSDD_PUBLISH_DELAY,
wsdd_finding_publish_delay_timer_callback, wsdd);
}
return;
}
wsdd_finding_publish(wsdd);
}
/* Get existent finding or add a new one
*/
static wsdd_finding*
wsdd_finding_get (int ifindex, const char *address)
{
ll_node *node;
wsdd_finding *wsdd;
/* Check for duplicates */
for (LL_FOR_EACH(node, &wsdd_finding_list)) {
wsdd = OUTER_STRUCT(node, wsdd_finding, list_node);
if (wsdd->finding.ifindex == ifindex &&
!strcmp(wsdd->address, address)) {
return wsdd;
}
}
/* Add new finding */
wsdd = wsdd_finding_new(ifindex, address);
ll_push_end(&wsdd_finding_list, &wsdd->list_node);
return wsdd;
}
/* Lookup wsdd_finding by IP address
*/
static wsdd_finding*
wsdd_finding_by_address (ip_addr addr)
{
ll_node *node, *node2;
wsdd_finding *wsdd;
wsdd_xaddr *xaddr;
const struct sockaddr *sockaddr;
/* Check for duplicates */
for (LL_FOR_EACH(node, &wsdd_finding_list)) {
wsdd = OUTER_STRUCT(node, wsdd_finding, list_node);
for (LL_FOR_EACH(node2, &wsdd->xaddrs)) {
xaddr = OUTER_STRUCT(node2, wsdd_xaddr, list_node);
sockaddr = http_uri_addr(xaddr->uri);
if (sockaddr != NULL) {
ip_addr addr2 = ip_addr_from_sockaddr(sockaddr);
if (ip_addr_equal(addr, addr2)) {
return wsdd;
}
}
}
}
return NULL;
}
/* Check if finding already has particular xaddr
*/
static bool
wsdd_finding_has_xaddr (wsdd_finding *wsdd, const wsdd_xaddr *xaddr)
{
ll_node *node;
wsdd_xaddr *xaddr2;
for (LL_FOR_EACH(node, &wsdd->xaddrs)) {
xaddr2 = OUTER_STRUCT(node, wsdd_xaddr, list_node);
if (http_uri_equal(xaddr->uri, xaddr2->uri)) {
return true;
}
}
return false;
}
/* Delete wsdd_finding from the wsdd_finding_list
*/
static void
wsdd_finding_del (const char *address)
{
ll_node *node;
/* Lookup finding in the list */
for (LL_FOR_EACH(node, &wsdd_finding_list)) {
wsdd_finding *wsdd = OUTER_STRUCT(node, wsdd_finding, list_node);
if (!strcmp(wsdd->address, address)) {
ll_del(&wsdd->list_node);
wsdd_finding_free(wsdd);
return;
}
}
}
/* Delete all findings from the wsdd_finding_list
*/
static void
wsdd_finding_list_purge (void)
{
ll_node *node;
while ((node = ll_first(&wsdd_finding_list)) != NULL) {
wsdd_finding *wsdd = OUTER_STRUCT(node, wsdd_finding, list_node);
ll_del(&wsdd->list_node);
wsdd_finding_free(wsdd);
}
}
/* Parse endpoint addresses from the devprof:Hosted section of the
* device metadata:
* <devprof:Hosted>
* <wsa:EndpointReference>
* <wsa:Address>http://192.168.1.102:5358/WSDScanner</wsa:Address>
* </addressing:EndpointReference>
* <devprof:Types>scan:ScannerServiceType</devprof:Types>
* <devprof:ServiceId>uri:4509a320-00a0-008f-00b6-002507510eca/WSDScanner</devprof:ServiceId>
* <pnpx:CompatibleId>http://schemas.microsoft.com/windows/2006/08/wdp/scan/ScannerServiceType</pnpx:CompatibleId>
* <pnpx:HardwareId>VEN_0103&DEV_069D</pnpx:HardwareId>
* </devprof:Hosted>
*
* It ignores all endpoints except ScannerServiceType, extracts endpoint
* URLs and prepends them to the wsdd->finding.endpoints
*
* Returns true if some endpoints were extracted, false otherwise
*/
static bool
wsdd_finding_parse_endpoints (wsdd_finding *wsdd, xml_rd *xml)
{
unsigned int level = xml_rd_depth(xml);
size_t prefixlen = strlen(xml_rd_node_path(xml));
bool is_scanner = false;
zeroconf_endpoint *endpoints = NULL;
bool ok = false;
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml) + prefixlen;
const char *val;
if (!strcmp(path, "/devprof:Types")) {
val = xml_rd_node_value(xml);
if (strstr(val, "ScannerServiceType") != NULL) {
is_scanner = true;
}
} else if (!strcmp(path, "/a:EndpointReference/a:Address")) {
http_uri *uri;
zeroconf_endpoint *ep;
val = xml_rd_node_value(xml);
uri = http_uri_new(val, true);
if (uri != NULL) {
http_uri_fix_ipv6_zone(uri, wsdd->finding.ifindex);
ep = zeroconf_endpoint_new(ID_PROTO_WSD, uri);
ep->next = endpoints;
endpoints = ep;
}
}
xml_rd_deep_next(xml, level);
}
if (!is_scanner) {
zeroconf_endpoint_list_free(endpoints);
return false;
}
ok = endpoints != NULL;
while (endpoints != NULL) {
zeroconf_endpoint *ep = endpoints;
const struct sockaddr *addr = http_uri_addr(ep->uri);
if (addr != NULL) {
ip_addrset_add(wsdd->finding.addrs, ip_addr_from_sockaddr(addr));
}
endpoints = endpoints->next;
ep->next = wsdd->finding.endpoints;
wsdd->finding.endpoints = ep;
}
return ok;
}
/* Get metadata callback
*/
static void
wsdd_finding_get_metadata_callback (void *ptr, http_query *q)
{
error err;
xml_rd *xml = NULL;
http_data *data;
wsdd_finding *wsdd = ptr;
char *model = NULL, *manufacturer = NULL;
bool ok = false;
(void) ptr;
/* Check query status */
err = http_query_error(q);
if (err != NULL) {
log_trace(wsdd_log, "metadata query: %s", ESTRING(err));
goto DONE;
}
/* Parse XML */
data = http_query_get_response_data(q);
if (data->size == 0) {
log_trace(wsdd_log, "metadata query: no data");
goto DONE;
}
err = xml_rd_begin(&xml, data->bytes, data->size, wsdd_ns_rules);
if (err != NULL) {
log_trace(wsdd_log, "metadata query: %s", ESTRING(err));
goto DONE;
}
/* Decode XML */
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml);
if (!strcmp(path, "s:Envelope/s:Body/mex:Metadata/mex:MetadataSection"
"/devprof:Relationship/devprof:Hosted")) {
ok = wsdd_finding_parse_endpoints(wsdd, xml) || ok;
} else if (!strcmp(path, "s:Envelope/s:Body/mex:Metadata/mex:MetadataSection"
"/devprof:ThisModel/devprof:Manufacturer")) {
if (manufacturer == NULL) {
manufacturer = str_dup(xml_rd_node_value(xml));
}
} else if (!strcmp(path, "s:Envelope/s:Body/mex:Metadata/mex:MetadataSection"
"/devprof:ThisModel/devprof:ModelName")) {
if (model == NULL) {
model = str_dup(xml_rd_node_value(xml));
}
}
xml_rd_deep_next(xml, 0);
}
if (wsdd->finding.model == NULL) {
if (model != NULL && manufacturer != NULL &&
str_has_prefix(model, manufacturer)) {
mem_free(manufacturer);
manufacturer = NULL;
}
if (model != NULL && manufacturer != NULL) {
wsdd->finding.model = str_printf("%s %s", manufacturer, model);
} else if (model != NULL) {
wsdd->finding.model = model;
model = NULL;
} else if (manufacturer != NULL) {
wsdd->finding.model = manufacturer;
manufacturer = NULL;
} else {
wsdd->finding.model = str_dup(wsdd->address);
}
}
/* Cancel cancel all unnecessary metadata requests.
*
* Note, we consider request is unnecessary if:
* * it has the same address family
* * it belongs to the same network interface
*/
if (ok) {
http_client_cancel_af_uintptr(wsdd->http_client,
http_uri_af(http_query_uri(q)), http_query_get_uintptr(q));
}
/* Cleanup and exit */
DONE:
xml_rd_finish(&xml);
mem_free(model);
mem_free(manufacturer);
if (http_client_has_pending(wsdd->http_client) == 0) {
wsdd_finding_publish_delay(wsdd);
}
}
/* Query device metadata
*/
static void
wsdd_finding_get_metadata (wsdd_finding *wsdd, int ifindex, wsdd_xaddr *xaddr)
{
uuid u = uuid_rand();
http_query *q;
log_trace(wsdd_log, "querying metadata from %s", http_uri_str(xaddr->uri));
sprintf(wsdd_buf, wsdd_get_metadata_template, u.text, wsdd->address);
q = http_query_new(wsdd->http_client, http_uri_clone(xaddr->uri),
"POST", str_dup(wsdd_buf), "application/soap+xml; charset=utf-8");
http_query_set_uintptr(q, ifindex);
http_query_submit(q, wsdd_finding_get_metadata_callback);
}
/******************** wsdd_message operations ********************/
/* Parse transport addresses. Universal function
* for Hello/Bye/ProbeMatch message
*/
static void
wsdd_message_parse_endpoint (wsdd_message *msg, xml_rd *xml)
{
unsigned int level = xml_rd_depth(xml);
char *xaddrs_text = NULL;
size_t prefixlen = strlen(xml_rd_node_path(xml));
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml) + prefixlen;
const char *val;
if (!strcmp(path, "/d:Types")) {
val = xml_rd_node_value(xml);
msg->is_scanner = !!strstr(val, "ScanDeviceType");
msg->is_printer = !!strstr(val, "PrintDeviceType");
} else if (!strcmp(path, "/d:XAddrs")) {
mem_free(xaddrs_text);
xaddrs_text = str_dup(xml_rd_node_value(xml));
} else if (!strcmp(path, "/a:EndpointReference/a:Address")) {
mem_free((char*) msg->address);
msg->address = str_dup(xml_rd_node_value(xml));
}
xml_rd_deep_next(xml, level);
}
if (xaddrs_text != NULL) {
char *tok, *saveptr;
static const char *delim = "\t\n\v\f\r \x85\xA0";
for (tok = strtok_r(xaddrs_text, delim, &saveptr); tok != NULL;
tok = strtok_r(NULL, delim, &saveptr)) {
http_uri *uri = http_uri_new(tok, true);
if (uri != NULL) {
wsdd_xaddr_list_add(&msg->xaddrs, uri);
}
}
}
mem_free(xaddrs_text);
}
/* Parse WSDD message
*/
static wsdd_message*
wsdd_message_parse (const char *xml_text, size_t xml_len)
{
wsdd_message *msg = mem_new(wsdd_message, 1);
xml_rd *xml;
error err;
ll_init(&msg->xaddrs);
err = xml_rd_begin(&xml, xml_text, xml_len, wsdd_ns_rules);
if (err != NULL) {
goto DONE;
}
while (!xml_rd_end(xml)) {
const char *path = xml_rd_node_path(xml);
const char *val;
if (!strcmp(path, "s:Envelope/s:Header/a:Action")) {
val = xml_rd_node_value(xml);
if (strstr(val, "Hello")) {
msg->action = WSDD_ACTION_HELLO;
} else if (strstr(val, "Bye")) {
msg->action = WSDD_ACTION_BYE;
} else if (strstr(val, "ProbeMatches")) {
msg->action = WSDD_ACTION_PROBEMATCHES;
}
} else if (!strcmp(path, "s:Envelope/s:Body/d:Hello") ||
!strcmp(path, "s:Envelope/s:Body/d:Bye") ||
!strcmp(path, "s:Envelope/s:Body/d:ProbeMatches/d:ProbeMatch")) {
wsdd_message_parse_endpoint(msg, xml);
}
xml_rd_deep_next(xml, 0);
}
DONE:
xml_rd_finish(&xml);
if (err != NULL ||
msg->action == WSDD_ACTION_UNKNOWN ||
msg->address == NULL ||
(msg->action == WSDD_ACTION_HELLO && ll_empty(&msg->xaddrs)) ||
(msg->action == WSDD_ACTION_PROBEMATCHES && ll_empty(&msg->xaddrs))) {
wsdd_message_free(msg);
msg = NULL;
}
return msg;
}
/* Free wsdd_message
*/
static void
wsdd_message_free (wsdd_message *msg)
{
if (msg != NULL) {
mem_free((char*) msg->address);
wsdd_xaddr_list_purge(&msg->xaddrs);
mem_free(msg);
}
}
/* Get message action name, for debugging
*/
static const char*
wsdd_message_action_name (const wsdd_message *msg)
{
switch (msg->action) {
case WSDD_ACTION_UNKNOWN:
break;
case WSDD_ACTION_HELLO: return "Hello";
case WSDD_ACTION_BYE: return "Bye";
case WSDD_ACTION_PROBEMATCHES: return "ProbeMatches";
}
return "UNKNOWN";
}
/******************** Advanced socket options ********************/
/* Setup IP_PKTINFO/IP_RECVIF reception for IPv6 sockets
*/
static int
wsdd_sock_enable_pktinfo_ip6 (int fd)
{
static int yes = 1;
int rc;
rc = setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes));
if (rc < 0) {
log_debug(wsdd_log, "setsockopt(AF_INET6, IPV6_RECVPKTINFO): %s",
strerror(errno));
}
return rc;
}
/* Setup IP_PKTINFO/IP_RECVIF reception for IPv4 sockets
*/
static int
wsdd_sock_enable_pktinfo_ip4 (int fd)
{
static int yes = 1;
int rc;
#ifdef IP_PKTINFO
/* Linux version */
rc = setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes));
#elif defined(IP_RECVIF)
/* OpenBSD */
rc = setsockopt(fd, IPPROTO_IP, IP_RECVIF, &yes, sizeof(yes));
#else
# error FIX ME
#endif
if (rc < 0) {
log_debug(wsdd_log, "setsockopt(AF_INET,IP_PKTINFO/IP_RECVIF): %s",
strerror(errno));
}
return rc;
}
/******************** wsdd_resolver operations ********************/
/* Dispatch received WSDD message
*/
static void
wsdd_resolver_message_dispatch (wsdd_resolver *resolver,
wsdd_message *msg, const char *from)
{
wsdd_finding *wsdd;
wsdd_xaddr *xaddr;
ll_node *node;
/* Fixup ipv6 zones */
for (LL_FOR_EACH(node, &msg->xaddrs)) {
xaddr = OUTER_STRUCT(node, wsdd_xaddr, list_node);
http_uri_fix_ipv6_zone(xaddr->uri, resolver->ifindex);
}
/* Write trace messages */
log_trace(wsdd_log, "%s message received from %s:",
wsdd_message_action_name(msg), from);
log_trace(wsdd_log, " address: %s", msg->address);
log_trace(wsdd_log, " is_scanner: %s", msg->is_scanner ? "yes" : "no");
log_trace(wsdd_log, " is_printer: %s", msg->is_printer ? "yes" : "no");
for (LL_FOR_EACH(node, &msg->xaddrs)) {
xaddr = OUTER_STRUCT(node, wsdd_xaddr, list_node);
log_trace(wsdd_log, " xaddr: %s", http_uri_str(xaddr->uri));
}
/* Handle the message */
switch (msg->action) {
case WSDD_ACTION_HELLO:
case WSDD_ACTION_PROBEMATCHES:
/* Ignore devices that are neither scanner not printer */
if (!(msg->is_scanner || msg->is_printer)) {
log_trace(wsdd_log,
"skipped: device is neither scanner not printer");
goto DONE;
}
/* Add a finding or get existent one */
wsdd = wsdd_finding_get(resolver->ifindex, msg->address);
/* Import newly discovered xaddrs and initiate metadata
* query
*/
while ((node = ll_pop_beg(&msg->xaddrs)) != NULL) {
xaddr = OUTER_STRUCT(node, wsdd_xaddr, list_node);
if (wsdd_finding_has_xaddr(wsdd, xaddr)) {
wsdd_xaddr_free(xaddr);
continue;
}
ll_push_end(&wsdd->xaddrs, &xaddr->list_node);
if (msg->is_scanner) {
wsdd_finding_get_metadata(wsdd, resolver->ifindex, xaddr);
}
}
/* If there is no pending metadata queries, it may mean
* one of the following:
* 1) device is not scanner, metadata won't be requested
* 2) there is no xaddrs (which is very unlikely, but
* just in case...)
* 3) device already known and all metadata queries
* already finished
*
* At this case we can publish device now
*/
if (http_client_has_pending(wsdd->http_client) == 0) {
if (msg->is_scanner) {
wsdd_finding_publish_delay(wsdd);
} else {
wsdd_finding_publish(wsdd);
}
}
break;
case WSDD_ACTION_BYE:
wsdd_finding_del(msg->address);
break;
default:
break;
}
/* Cleanup and exit */
DONE:
wsdd_message_free(msg);
log_trace(wsdd_log, "");
}
/* Resolver read callback
*/
static void
wsdd_resolver_read_callback (int fd, void *data, ELOOP_FDPOLL_MASK mask)
{
struct sockaddr_storage from, to;
socklen_t tolen = sizeof(to);
ip_straddr str_from, str_to;
int rc;
wsdd_message *msg;
struct iovec vec = {wsdd_buf, sizeof(wsdd_buf)};
uint8_t aux[8192];
struct cmsghdr *cmsg;
int ifindex = 0;
wsdd_resolver *resolver;
struct msghdr msghdr = {
.msg_name = &from,
.msg_namelen = sizeof(from),
.msg_iov = &vec,
.msg_iovlen = 1,
.msg_control = aux,
.msg_controllen = sizeof(aux)
};
(void) mask;
(void) data;
/* Receive a packet */
rc = recvmsg(fd, &msghdr, 0);
if (rc <= 0) {
return;
}
/* Fetch interface index from auxiliary data */
for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
if (cmsg->cmsg_level == IPPROTO_IPV6 &&
cmsg->cmsg_type == IPV6_PKTINFO) {
struct in6_pktinfo *pkt = (struct in6_pktinfo*) CMSG_DATA(cmsg);
ifindex = pkt->ipi6_ifindex;
}
#ifdef IP_PKTINFO
/* Linux version */
else if (cmsg->cmsg_level == IPPROTO_IP &&
cmsg->cmsg_type == IP_PKTINFO) {
struct in_pktinfo *pkt = (struct in_pktinfo*) CMSG_DATA(cmsg);
ifindex = pkt->ipi_ifindex;
}
#elif defined(IP_RECVIF)