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