]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/dnssd.c
The dnssd backend now prefers IPPS over IPP.
[thirdparty/cups.git] / backend / dnssd.c
CommitLineData
ad9fc101 1/*
2 * "$Id$"
3 *
3929e7fd 4 * DNS-SD discovery backend for CUPS.
ad9fc101 5 *
9a766dc2 6 * Copyright 2008-2012 by Apple Inc.
ad9fc101 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
18 * main() - Browse for printers.
19 * browse_callback() - Browse devices.
20 * browse_local_callback() - Browse local devices.
21 * compare_devices() - Compare two devices.
97b02572 22 * exec_backend() - Execute the backend that corresponds to the
23 * resolved service name.
ad9fc101 24 * get_device() - Create or update a device.
25 * query_callback() - Process query data.
f0dce363 26 * sigterm_handler() - Handle termination signals...
ad9fc101 27 * unquote() - Unquote a name string.
28 */
29
30/*
31 * Include necessary headers.
32 */
33
34#include "backend-private.h"
35#include <cups/array.h>
36#include <dns_sd.h>
37
38
39/*
40 * Device structure...
41 */
42
43typedef enum
44{
45 CUPS_DEVICE_PRINTER = 0, /* lpd://... */
07055d0c 46 CUPS_DEVICE_IPPS, /* ipps://... */
9a766dc2 47 CUPS_DEVICE_IPP, /* ipp://... */
ad9fc101 48 CUPS_DEVICE_FAX_IPP, /* ipp://... */
49 CUPS_DEVICE_PDL_DATASTREAM, /* socket://... */
50 CUPS_DEVICE_RIOUSBPRINT /* riousbprint://... */
51} cups_devtype_t;
52
53
54typedef struct
55{
56 DNSServiceRef ref; /* Service reference for resolve */
57 char *name, /* Service name */
58 *domain, /* Domain name */
59 *fullName, /* Full name */
a306e19f 60 *make_and_model, /* Make and model from TXT record */
61 *device_id; /* 1284 device ID from TXT record */
ad9fc101 62 cups_devtype_t type; /* Device registration type */
0f1b6cf2 63 int priority, /* Priority associated with type */
64 cups_shared, /* CUPS shared printer? */
ad9fc101 65 sent; /* Did we list the device? */
66} cups_device_t;
67
68
f0dce363 69/*
70 * Local globals...
71 */
72
73static int job_canceled = 0;
74 /* Set to 1 on SIGTERM */
75
76
ad9fc101 77/*
78 * Local functions...
79 */
80
81static void browse_callback(DNSServiceRef sdRef,
82 DNSServiceFlags flags,
83 uint32_t interfaceIndex,
84 DNSServiceErrorType errorCode,
85 const char *serviceName,
86 const char *regtype,
87 const char *replyDomain, void *context);
88static void browse_local_callback(DNSServiceRef sdRef,
89 DNSServiceFlags flags,
90 uint32_t interfaceIndex,
91 DNSServiceErrorType errorCode,
92 const char *serviceName,
93 const char *regtype,
94 const char *replyDomain,
95 void *context);
96static int compare_devices(cups_device_t *a, cups_device_t *b);
97static void exec_backend(char **argv);
98static cups_device_t *get_device(cups_array_t *devices,
99 const char *serviceName,
100 const char *regtype,
101 const char *replyDomain);
102static void query_callback(DNSServiceRef sdRef,
103 DNSServiceFlags flags,
104 uint32_t interfaceIndex,
105 DNSServiceErrorType errorCode,
106 const char *fullName, uint16_t rrtype,
107 uint16_t rrclass, uint16_t rdlen,
108 const void *rdata, uint32_t ttl,
109 void *context);
f0dce363 110static void sigterm_handler(int sig);
ad9fc101 111static void unquote(char *dst, const char *src, size_t dstsize);
112
113
114/*
115 * 'main()' - Browse for printers.
116 */
117
118int /* O - Exit status */
119main(int argc, /* I - Number of command-line args */
120 char *argv[]) /* I - Command-line arguments */
121{
e57340ae 122 const char *name; /* Backend name */
ad9fc101 123 DNSServiceRef main_ref, /* Main service reference */
124 fax_ipp_ref, /* IPP fax service reference */
125 ipp_ref, /* IPP service reference */
126 ipp_tls_ref, /* IPP w/TLS service reference */
07055d0c 127 ipps_ref, /* IPP service reference */
ad9fc101 128 local_fax_ipp_ref, /* Local IPP fax service reference */
129 local_ipp_ref, /* Local IPP service reference */
130 local_ipp_tls_ref, /* Local IPP w/TLS service reference */
07055d0c 131 local_ipps_ref, /* Local IPP service reference */
ad9fc101 132 local_printer_ref, /* Local LPD service reference */
133 pdl_datastream_ref, /* AppSocket service reference */
134 printer_ref, /* LPD service reference */
135 riousbprint_ref; /* Remote IO service reference */
136 int fd; /* Main file descriptor */
137 fd_set input; /* Input set for select() */
138 struct timeval timeout; /* Timeout for select() */
139 cups_array_t *devices; /* Device array */
140 cups_device_t *device; /* Current device */
855b21a1 141 char uriName[1024]; /* Unquoted fullName for URI */
f0dce363 142#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
143 struct sigaction action; /* Actions for POSIX signals */
144#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
ad9fc101 145
146
147 /*
f0dce363 148 * Don't buffer stderr, and catch SIGTERM...
ad9fc101 149 */
150
151 setbuf(stderr, NULL);
152
f0dce363 153#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
154 sigset(SIGTERM, sigterm_handler);
155#elif defined(HAVE_SIGACTION)
156 memset(&action, 0, sizeof(action));
157
158 sigemptyset(&action.sa_mask);
1c026e33 159 action.sa_handler = sigterm_handler;
160 sigaction(SIGTERM, &action, NULL);
f0dce363 161#else
162 signal(SIGTERM, sigterm_handler);
163#endif /* HAVE_SIGSET */
164
165 /*
166 * Check command-line...
167 */
168
ad9fc101 169 if (argc >= 6)
170 exec_backend(argv);
171 else if (argc != 1)
172 {
4c4eea89 173 _cupsLangPrintf(stderr,
174 _("Usage: %s job-id user title copies options [file]"),
175 argv[0]);
ad9fc101 176 return (1);
177 }
178
e57340ae 179 /*
180 * Only do discovery when run as "dnssd"...
181 */
182
183 if ((name = strrchr(argv[0], '/')) != NULL)
184 name ++;
185 else
186 name = argv[0];
187
188 if (strcmp(name, "dnssd"))
189 return (0);
190
ad9fc101 191 /*
192 * Create an array to track devices...
193 */
194
195 devices = cupsArrayNew((cups_array_func_t)compare_devices, NULL);
196
197 /*
198 * Browse for different kinds of printers...
199 */
200
201 if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError)
202 {
203 perror("ERROR: Unable to create service connection");
204 return (1);
205 }
206
207 fd = DNSServiceRefSockFD(main_ref);
208
209 fax_ipp_ref = main_ref;
210 DNSServiceBrowse(&fax_ipp_ref, kDNSServiceFlagsShareConnection, 0,
211 "_fax-ipp._tcp", NULL, browse_callback, devices);
212
213 ipp_ref = main_ref;
214 DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0,
215 "_ipp._tcp", NULL, browse_callback, devices);
216
217 ipp_tls_ref = main_ref;
218 DNSServiceBrowse(&ipp_tls_ref, kDNSServiceFlagsShareConnection, 0,
219 "_ipp-tls._tcp", NULL, browse_callback, devices);
220
07055d0c 221 ipps_ref = main_ref;
56391af7 222 DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0,
07055d0c 223 "_ipps._tcp", NULL, browse_callback, devices);
224
ad9fc101 225 local_fax_ipp_ref = main_ref;
226 DNSServiceBrowse(&local_fax_ipp_ref, kDNSServiceFlagsShareConnection,
227 kDNSServiceInterfaceIndexLocalOnly,
228 "_fax-ipp._tcp", NULL, browse_local_callback, devices);
229
230 local_ipp_ref = main_ref;
231 DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection,
232 kDNSServiceInterfaceIndexLocalOnly,
233 "_ipp._tcp", NULL, browse_local_callback, devices);
234
235 local_ipp_tls_ref = main_ref;
236 DNSServiceBrowse(&local_ipp_tls_ref, kDNSServiceFlagsShareConnection,
237 kDNSServiceInterfaceIndexLocalOnly,
238 "_ipp-tls._tcp", NULL, browse_local_callback, devices);
239
07055d0c 240 local_ipps_ref = main_ref;
56391af7 241 DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection,
07055d0c 242 kDNSServiceInterfaceIndexLocalOnly,
243 "_ipps._tcp", NULL, browse_local_callback, devices);
244
ad9fc101 245 local_printer_ref = main_ref;
246 DNSServiceBrowse(&local_printer_ref, kDNSServiceFlagsShareConnection,
247 kDNSServiceInterfaceIndexLocalOnly,
248 "_printer._tcp", NULL, browse_local_callback, devices);
249
c6fab96f 250 pdl_datastream_ref = main_ref;
ad9fc101 251 DNSServiceBrowse(&pdl_datastream_ref, kDNSServiceFlagsShareConnection, 0,
252 "_pdl-datastream._tcp", NULL, browse_callback, devices);
253
254 printer_ref = main_ref;
255 DNSServiceBrowse(&printer_ref, kDNSServiceFlagsShareConnection, 0,
256 "_printer._tcp", NULL, browse_callback, devices);
257
258 riousbprint_ref = main_ref;
259 DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0,
260 "_riousbprint._tcp", NULL, browse_callback, devices);
261
262 /*
263 * Loop until we are killed...
264 */
265
f0dce363 266 while (!job_canceled)
ad9fc101 267 {
268 FD_ZERO(&input);
269 FD_SET(fd, &input);
270
ba58cd0c 271 timeout.tv_sec = 0;
272 timeout.tv_usec = 250000;
ad9fc101 273
274 if (select(fd + 1, &input, NULL, NULL, &timeout) < 0)
275 continue;
276
277 if (FD_ISSET(fd, &input))
278 {
279 /*
280 * Process results of our browsing...
281 */
282
283 DNSServiceProcessResult(main_ref);
284 }
285 else
286 {
287 /*
288 * Announce any devices we've found...
289 */
290
5169bfee 291 DNSServiceErrorType status; /* DNS query status */
0f1b6cf2 292 cups_device_t *best; /* Best matching device */
ad9fc101 293 char device_uri[1024]; /* Device URI */
012d7a28 294 int count; /* Number of queries */
ba58cd0c 295 int sent; /* Number of sent */
ad9fc101 296
0f1b6cf2 297 for (device = (cups_device_t *)cupsArrayFirst(devices),
ba58cd0c 298 best = NULL, count = 0, sent = 0;
ad9fc101 299 device;
300 device = (cups_device_t *)cupsArrayNext(devices))
ba58cd0c 301 {
302 if (device->sent)
303 sent ++;
304
305 if (device->ref)
306 count ++;
307
ad9fc101 308 if (!device->ref && !device->sent)
309 {
310 /*
311 * Found the device, now get the TXT record(s) for it...
312 */
313
ba58cd0c 314 if (count < 20)
012d7a28 315 {
316 device->ref = main_ref;
317
318 fprintf(stderr, "DEBUG: Querying \"%s\"...\n", device->fullName);
319
5169bfee 320 status = DNSServiceQueryRecord(&(device->ref),
321 kDNSServiceFlagsShareConnection,
322 0, device->fullName,
323 kDNSServiceType_TXT,
324 kDNSServiceClass_IN, query_callback,
325 devices);
326 if (status != kDNSServiceErr_NoError)
327 {
012d7a28 328 fputs("ERROR: Unable to query for TXT records!\n", stderr);
5169bfee 329 fprintf(stderr, "DEBUG: DNSServiceQueryRecord returned %d\n",
330 status);
331 }
332 else
012d7a28 333 count ++;
334 }
ad9fc101 335 }
336 else if (!device->sent)
337 {
338 /*
339 * Got the TXT records, now report the device...
340 */
341
342 DNSServiceRefDeallocate(device->ref);
343 device->ref = 0;
344
0f1b6cf2 345 if (!best)
346 best = device;
c6fab96f 347 else if (_cups_strcasecmp(best->name, device->name) ||
348 _cups_strcasecmp(best->domain, device->domain))
0f1b6cf2 349 {
855b21a1 350 unquote(uriName, best->fullName, sizeof(uriName));
351
0f1b6cf2 352 httpAssembleURI(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri),
49b3ea56 353 "dnssd", NULL, uriName, 0,
0f1b6cf2 354 best->cups_shared ? "/cups" : "/");
355
2317ac1e 356 cupsBackendReport("network", device_uri, best->make_and_model,
a306e19f 357 best->name, best->device_id, NULL);
0f1b6cf2 358 best->sent = 1;
359 best = device;
ba58cd0c 360
361 sent ++;
0f1b6cf2 362 }
363 else if (best->priority > device->priority ||
364 (best->priority == device->priority &&
365 best->type < device->type))
366 {
367 best->sent = 1;
368 best = device;
ba58cd0c 369
370 sent ++;
0f1b6cf2 371 }
372 else
ba58cd0c 373 {
0f1b6cf2 374 device->sent = 1;
ba58cd0c 375
376 sent ++;
377 }
0f1b6cf2 378 }
ba58cd0c 379 }
ad9fc101 380
0f1b6cf2 381 if (best)
382 {
855b21a1 383 unquote(uriName, best->fullName, sizeof(uriName));
384
0f1b6cf2 385 httpAssembleURI(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri),
49b3ea56 386 "dnssd", NULL, uriName, 0,
0f1b6cf2 387 best->cups_shared ? "/cups" : "/");
ad9fc101 388
2317ac1e 389 cupsBackendReport("network", device_uri, best->make_and_model,
a306e19f 390 best->name, best->device_id, NULL);
0f1b6cf2 391 best->sent = 1;
ba58cd0c 392 sent ++;
0f1b6cf2 393 }
ba58cd0c 394
395 if (sent == cupsArrayCount(devices))
396 break;
ad9fc101 397 }
398 }
f0dce363 399
400 return (CUPS_BACKEND_OK);
ad9fc101 401}
402
403
404/*
405 * 'browse_callback()' - Browse devices.
406 */
407
408static void
409browse_callback(
410 DNSServiceRef sdRef, /* I - Service reference */
411 DNSServiceFlags flags, /* I - Option flags */
412 uint32_t interfaceIndex, /* I - Interface number */
413 DNSServiceErrorType errorCode, /* I - Error, if any */
414 const char *serviceName, /* I - Name of service/device */
415 const char *regtype, /* I - Type of service */
416 const char *replyDomain, /* I - Service domain */
417 void *context) /* I - Devices array */
418{
419 fprintf(stderr, "DEBUG2: browse_callback(sdRef=%p, flags=%x, "
420 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
421 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
422 sdRef, flags, interfaceIndex, errorCode,
423 serviceName ? serviceName : "(null)",
424 regtype ? regtype : "(null)",
425 replyDomain ? replyDomain : "(null)",
426 context);
427
428 /*
429 * Only process "add" data...
430 */
431
432 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
433 return;
434
435 /*
436 * Get the device...
437 */
438
439 get_device((cups_array_t *)context, serviceName, regtype, replyDomain);
440}
441
442
443/*
444 * 'browse_local_callback()' - Browse local devices.
445 */
446
447static void
448browse_local_callback(
449 DNSServiceRef sdRef, /* I - Service reference */
450 DNSServiceFlags flags, /* I - Option flags */
451 uint32_t interfaceIndex, /* I - Interface number */
452 DNSServiceErrorType errorCode, /* I - Error, if any */
453 const char *serviceName, /* I - Name of service/device */
454 const char *regtype, /* I - Type of service */
455 const char *replyDomain, /* I - Service domain */
456 void *context) /* I - Devices array */
457{
458 cups_device_t *device; /* Device */
459
460
461 fprintf(stderr, "DEBUG2: browse_local_callback(sdRef=%p, flags=%x, "
462 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
463 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
464 sdRef, flags, interfaceIndex, errorCode,
465 serviceName ? serviceName : "(null)",
466 regtype ? regtype : "(null)",
467 replyDomain ? replyDomain : "(null)",
468 context);
469
470 /*
471 * Only process "add" data...
472 */
473
474 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
475 return;
476
477 /*
478 * Get the device...
479 */
480
481 device = get_device((cups_array_t *)context, serviceName, regtype,
482 replyDomain);
483
484 /*
485 * Hide locally-registered devices...
486 */
487
488 fprintf(stderr, "DEBUG: Hiding local printer \"%s\"...\n",
489 device->fullName);
490 device->sent = 1;
491}
492
493
494/*
495 * 'compare_devices()' - Compare two devices.
496 */
497
498static int /* O - Result of comparison */
499compare_devices(cups_device_t *a, /* I - First device */
500 cups_device_t *b) /* I - Second device */
501{
49b3ea56 502 return (strcmp(a->name, b->name));
ad9fc101 503}
504
505
506/*
507 * 'exec_backend()' - Execute the backend that corresponds to the
508 * resolved service name.
509 */
510
511static void
512exec_backend(char **argv) /* I - Command-line arguments */
513{
514 const char *resolved_uri, /* Resolved device URI */
515 *cups_serverbin; /* Location of programs */
516 char scheme[1024], /* Scheme from URI */
517 *ptr, /* Pointer into scheme */
518 filename[1024]; /* Backend filename */
519
520
521 /*
522 * Resolve the device URI...
523 */
524
90d07b87 525 job_canceled = -1;
526
3929e7fd 527 while ((resolved_uri = cupsBackendDeviceURI(argv)) == NULL)
528 {
529 _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer."));
530 sleep(10);
531
532 if (getenv("CLASS") != NULL)
79646d87 533 exit(CUPS_BACKEND_FAILED);
3929e7fd 534 }
ad9fc101 535
536 /*
537 * Extract the scheme from the URI...
538 */
539
540 strlcpy(scheme, resolved_uri, sizeof(scheme));
541 if ((ptr = strchr(scheme, ':')) != NULL)
542 *ptr = '\0';
543
544 /*
545 * Get the filename of the backend...
546 */
547
548 if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
549 cups_serverbin = CUPS_SERVERBIN;
550
551 snprintf(filename, sizeof(filename), "%s/backend/%s", cups_serverbin, scheme);
552
553 /*
f0dce363 554 * Overwrite the device URI and run the new backend...
ad9fc101 555 */
556
557 setenv("DEVICE_URI", resolved_uri, 1);
558
559 argv[0] = (char *)resolved_uri;
560
561 fprintf(stderr, "DEBUG: Executing backend \"%s\"...\n", filename);
562
563 execv(filename, argv);
564
565 fprintf(stderr, "ERROR: Unable to execute backend \"%s\": %s\n", filename,
566 strerror(errno));
567 exit(CUPS_BACKEND_STOP);
568}
569
570
571/*
572 * 'get_device()' - Create or update a device.
573 */
574
575static cups_device_t * /* O - Device */
576get_device(cups_array_t *devices, /* I - Device array */
577 const char *serviceName, /* I - Name of service/device */
578 const char *regtype, /* I - Type of service */
579 const char *replyDomain) /* I - Service domain */
580{
581 cups_device_t key, /* Search key */
582 *device; /* Device */
5169bfee 583 char fullName[kDNSServiceMaxDomainName];
584 /* Full name for query */
ad9fc101 585
586
587 /*
588 * See if this is a new device...
589 */
590
49b3ea56 591 key.name = (char *)serviceName;
ad9fc101 592
07055d0c 593 if (!strcmp(regtype, "_ipp._tcp."))
ad9fc101 594 key.type = CUPS_DEVICE_IPP;
07055d0c 595 else if (!strcmp(regtype, "_ipps._tcp.") ||
596 !strcmp(regtype, "_ipp-tls._tcp."))
597 key.type = CUPS_DEVICE_IPPS;
ad9fc101 598 else if (!strcmp(regtype, "_fax-ipp._tcp."))
599 key.type = CUPS_DEVICE_FAX_IPP;
600 else if (!strcmp(regtype, "_printer._tcp."))
601 key.type = CUPS_DEVICE_PRINTER;
602 else if (!strcmp(regtype, "_pdl-datastream._tcp."))
603 key.type = CUPS_DEVICE_PDL_DATASTREAM;
604 else
605 key.type = CUPS_DEVICE_RIOUSBPRINT;
606
0f1b6cf2 607 for (device = cupsArrayFind(devices, &key);
608 device;
609 device = cupsArrayNext(devices))
c6fab96f 610 if (_cups_strcasecmp(device->name, key.name))
0f1b6cf2 611 break;
612 else if (device->type == key.type)
49b3ea56 613 {
c6fab96f 614 if (!_cups_strcasecmp(device->domain, "local.") &&
615 _cups_strcasecmp(device->domain, replyDomain))
49b3ea56 616 {
617 /*
618 * Update the .local listing to use the "global" domain name instead.
619 * The backend will try local lookups first, then the global domain name.
620 */
621
622 free(device->domain);
623 device->domain = strdup(replyDomain);
624
625 DNSServiceConstructFullName(fullName, device->name, regtype,
626 replyDomain);
627 free(device->fullName);
628 device->fullName = strdup(fullName);
629 }
630
0f1b6cf2 631 return (device);
49b3ea56 632 }
ad9fc101 633
0f1b6cf2 634 /*
635 * Yes, add the device...
636 */
ad9fc101 637
0f1b6cf2 638 fprintf(stderr, "DEBUG: Found \"%s.%s%s\"...\n", serviceName, regtype,
639 replyDomain);
ad9fc101 640
0f1b6cf2 641 device = calloc(sizeof(cups_device_t), 1);
642 device->name = strdup(serviceName);
643 device->domain = strdup(replyDomain);
644 device->type = key.type;
645 device->priority = 50;
ad9fc101 646
0f1b6cf2 647 cupsArrayAdd(devices, device);
ad9fc101 648
649 /*
0f1b6cf2 650 * Set the "full name" of this service, which is used for queries...
ad9fc101 651 */
652
5169bfee 653 DNSServiceConstructFullName(fullName, serviceName, regtype, replyDomain);
ad9fc101 654 device->fullName = strdup(fullName);
655
656 return (device);
657}
658
659
660/*
661 * 'query_callback()' - Process query data.
662 */
663
664static void
665query_callback(
666 DNSServiceRef sdRef, /* I - Service reference */
667 DNSServiceFlags flags, /* I - Data flags */
668 uint32_t interfaceIndex, /* I - Interface */
669 DNSServiceErrorType errorCode, /* I - Error, if any */
670 const char *fullName, /* I - Full service name */
671 uint16_t rrtype, /* I - Record type */
672 uint16_t rrclass, /* I - Record class */
673 uint16_t rdlen, /* I - Length of record data */
674 const void *rdata, /* I - Record data */
675 uint32_t ttl, /* I - Time-to-live */
676 void *context) /* I - Devices array */
677{
678 cups_array_t *devices; /* Device array */
679 char name[1024], /* Service name */
a306e19f 680 *ptr; /* Pointer into string */
681 cups_device_t dkey, /* Search key */
ad9fc101 682 *device; /* Device */
683
684
685 fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
686 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
687 "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
688 "context=%p)\n",
689 sdRef, flags, interfaceIndex, errorCode,
690 fullName ? fullName : "(null)", rrtype, rrclass, rdlen, rdata, ttl,
691 context);
692
693 /*
694 * Only process "add" data...
695 */
696
697 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
698 return;
699
700 /*
701 * Lookup the service in the devices array.
702 */
703
a306e19f 704 devices = (cups_array_t *)context;
705 dkey.name = name;
ad9fc101 706
707 unquote(name, fullName, sizeof(name));
708
a306e19f 709 if ((dkey.domain = strstr(name, "._tcp.")) != NULL)
710 dkey.domain += 6;
ad9fc101 711 else
a306e19f 712 dkey.domain = (char *)"local.";
ad9fc101 713
714 if ((ptr = strstr(name, "._")) != NULL)
715 *ptr = '\0';
716
07055d0c 717 if (strstr(fullName, "_ipp._tcp."))
a306e19f 718 dkey.type = CUPS_DEVICE_IPP;
07055d0c 719 else if (strstr(fullName, "_ipps._tcp.") ||
720 strstr(fullName, "_ipp-tls._tcp."))
721 dkey.type = CUPS_DEVICE_IPPS;
0f1b6cf2 722 else if (strstr(fullName, "_fax-ipp._tcp."))
a306e19f 723 dkey.type = CUPS_DEVICE_FAX_IPP;
0f1b6cf2 724 else if (strstr(fullName, "_printer._tcp."))
a306e19f 725 dkey.type = CUPS_DEVICE_PRINTER;
0f1b6cf2 726 else if (strstr(fullName, "_pdl-datastream._tcp."))
a306e19f 727 dkey.type = CUPS_DEVICE_PDL_DATASTREAM;
0f1b6cf2 728 else
a306e19f 729 dkey.type = CUPS_DEVICE_RIOUSBPRINT;
0f1b6cf2 730
a306e19f 731 for (device = cupsArrayFind(devices, &dkey);
0f1b6cf2 732 device;
733 device = cupsArrayNext(devices))
ad9fc101 734 {
c6fab96f 735 if (_cups_strcasecmp(device->name, dkey.name) ||
736 _cups_strcasecmp(device->domain, dkey.domain))
0f1b6cf2 737 {
738 device = NULL;
739 break;
740 }
a306e19f 741 else if (device->type == dkey.type)
0f1b6cf2 742 {
743 /*
744 * Found it, pull out the priority and make and model from the TXT
745 * record and save it...
746 */
ad9fc101 747
a306e19f 748 const uint8_t *data, /* Pointer into data */
749 *datanext, /* Next key/value pair */
750 *dataend; /* End of entire TXT record */
751 uint8_t datalen; /* Length of current key/value pair */
752 char key[256], /* Key string */
753 value[256], /* Value string */
754 make_and_model[512],
755 /* Manufacturer and model */
756 model[256], /* Model */
757 device_id[2048];/* 1284 device ID */
ad9fc101 758
759
a306e19f 760 device_id[0] = '\0';
761 make_and_model[0] = '\0';
ad9fc101 762
a306e19f 763 strcpy(model, "Unknown");
764
765 for (data = rdata, dataend = data + rdlen;
766 data < dataend;
767 data = datanext)
0f1b6cf2 768 {
a306e19f 769 /*
770 * Read a key/value pair starting with an 8-bit length. Since the
771 * length is 8 bits and the size of the key/value buffers is 256, we
772 * don't need to check for overflow...
773 */
ad9fc101 774
a306e19f 775 datalen = *data++;
ad9fc101 776
259c1c0b 777 if (!datalen || (data + datalen) > dataend)
a306e19f 778 break;
0f1b6cf2 779
a306e19f 780 datanext = data + datalen;
0f1b6cf2 781
a306e19f 782 for (ptr = key; data < datanext && *data != '='; data ++)
783 *ptr++ = *data;
784 *ptr = '\0';
785
786 if (data < datanext && *data == '=')
0f1b6cf2 787 {
a306e19f 788 data ++;
0f1b6cf2 789
a306e19f 790 if (data < datanext)
791 memcpy(value, data, datanext - data);
792 value[datanext - data] = '\0';
07055d0c 793
794 fprintf(stderr, "DEBUG2: query_callback: \"%s=%s\".\n",
795 key, value);
0f1b6cf2 796 }
797 else
07055d0c 798 {
799 fprintf(stderr, "DEBUG2: query_callback: \"%s\" with no value.\n",
800 key);
a306e19f 801 continue;
07055d0c 802 }
a306e19f 803
c6fab96f 804 if (!_cups_strncasecmp(key, "usb_", 4))
0f1b6cf2 805 {
a306e19f 806 /*
807 * Add USB device ID information...
808 */
ad9fc101 809
a306e19f 810 ptr = device_id + strlen(device_id);
811 snprintf(ptr, sizeof(device_id) - (ptr - device_id), "%s:%s;",
812 key + 4, value);
813 }
814
c6fab96f 815 if (!_cups_strcasecmp(key, "usb_MFG") || !_cups_strcasecmp(key, "usb_MANU") ||
816 !_cups_strcasecmp(key, "usb_MANUFACTURER"))
a306e19f 817 strcpy(make_and_model, value);
c6fab96f 818 else if (!_cups_strcasecmp(key, "usb_MDL") || !_cups_strcasecmp(key, "usb_MODEL"))
a306e19f 819 strcpy(model, value);
c6fab96f 820 else if (!_cups_strcasecmp(key, "product") && !strstr(value, "Ghostscript"))
0f1b6cf2 821 {
a306e19f 822 if (value[0] == '(')
0f1b6cf2 823 {
a306e19f 824 /*
825 * Strip parenthesis...
826 */
0f1b6cf2 827
a306e19f 828 if ((ptr = value + strlen(value) - 1) > value && *ptr == ')')
0f1b6cf2 829 *ptr = '\0';
a306e19f 830
831 strcpy(model, value + 1);
0f1b6cf2 832 }
833 else
a306e19f 834 strcpy(model, value);
835 }
c6fab96f 836 else if (!_cups_strcasecmp(key, "ty"))
a306e19f 837 {
838 strcpy(model, value);
839
840 if ((ptr = strchr(model, ',')) != NULL)
841 *ptr = '\0';
ad9fc101 842 }
c6fab96f 843 else if (!_cups_strcasecmp(key, "priority"))
a306e19f 844 device->priority = atoi(value);
845 else if ((device->type == CUPS_DEVICE_IPP ||
07055d0c 846 device->type == CUPS_DEVICE_IPPS ||
a306e19f 847 device->type == CUPS_DEVICE_PRINTER) &&
c6fab96f 848 !_cups_strcasecmp(key, "printer-type"))
a306e19f 849 {
850 /*
851 * This is a CUPS printer!
852 */
853
854 device->cups_shared = 1;
855
856 if (device->type == CUPS_DEVICE_PRINTER)
857 device->sent = 1;
858 }
859 }
860
861 if (device->device_id)
862 free(device->device_id);
863
864 if (!device_id[0] && strcmp(model, "Unknown"))
865 {
866 if (make_and_model[0])
867 snprintf(device_id, sizeof(device_id), "MFG:%s;MDL:%s;",
868 make_and_model, model);
c6fab96f 869 else if (!_cups_strncasecmp(model, "designjet ", 10))
a306e19f 870 snprintf(device_id, sizeof(device_id), "MFG:HP;MDL:%s", model + 10);
c6fab96f 871 else if (!_cups_strncasecmp(model, "stylus ", 7))
a306e19f 872 snprintf(device_id, sizeof(device_id), "MFG:EPSON;MDL:%s", model + 7);
873 else if ((ptr = strchr(model, ' ')) != NULL)
874 {
875 /*
876 * Assume the first word is the make...
877 */
878
879 memcpy(make_and_model, model, ptr - model);
880 make_and_model[ptr - model] = '\0';
881
882 snprintf(device_id, sizeof(device_id), "MFG:%s;MDL:%s",
883 make_and_model, ptr + 1);
884 }
ad9fc101 885 }
a306e19f 886
887 if (device_id[0])
888 device->device_id = strdup(device_id);
0f1b6cf2 889 else
a306e19f 890 device->device_id = NULL;
ad9fc101 891
0f1b6cf2 892 if (device->make_and_model)
893 free(device->make_and_model);
ad9fc101 894
0f1b6cf2 895 if (make_and_model[0])
896 {
897 strlcat(make_and_model, " ", sizeof(make_and_model));
898 strlcat(make_and_model, model, sizeof(make_and_model));
a306e19f 899
0f1b6cf2 900 device->make_and_model = strdup(make_and_model);
901 }
902 else
903 device->make_and_model = strdup(model);
0f1b6cf2 904 break;
ad9fc101 905 }
906 }
0f1b6cf2 907
908 if (!device)
ad9fc101 909 fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
910}
911
912
f0dce363 913/*
914 * 'sigterm_handler()' - Handle termination signals...
915 */
916
917static void
918sigterm_handler(int sig) /* I - Signal number (unused) */
919{
4a241564 920 (void)sig;
921
90d07b87 922 if (job_canceled)
923 exit(CUPS_BACKEND_OK);
924 else
925 job_canceled = 1;
f0dce363 926}
927
928
ad9fc101 929/*
930 * 'unquote()' - Unquote a name string.
931 */
932
933static void
934unquote(char *dst, /* I - Destination buffer */
935 const char *src, /* I - Source string */
936 size_t dstsize) /* I - Size of destination buffer */
937{
938 char *dstend = dst + dstsize - 1; /* End of destination buffer */
939
940
941 while (*src && dst < dstend)
942 {
943 if (*src == '\\')
944 {
945 src ++;
946 if (isdigit(src[0] & 255) && isdigit(src[1] & 255) &&
947 isdigit(src[2] & 255))
948 {
949 *dst++ = ((((src[0] - '0') * 10) + src[1] - '0') * 10) + src[2] - '0';
950 src += 3;
951 }
952 else
953 *dst++ = *src++;
954 }
955 else
956 *dst++ = *src ++;
957 }
958
959 *dst = '\0';
960}
961
962
963/*
964 * End of "$Id$".
965 */