]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/mdns.c
Merge changes from CUPS trunk, r7566.
[thirdparty/cups.git] / backend / mdns.c
CommitLineData
7a14d768
MS
1/*
2 * "$Id$"
3 *
4 * DNS-SD discovery backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2008 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.
22 * get_device() - Create or update a device.
23 * query_callback() - Process query data.
24 * unquote() - Unquote a name string.
25 */
26
27/*
28 * Include necessary headers.
29 */
30
31#include "backend-private.h"
32#include <cups/array.h>
33#include <dns_sd.h>
34
35
36/*
37 * Device structure...
38 */
39
40typedef enum
41{
42 CUPS_DEVICE_PRINTER = 0, /* lpd://... */
43 CUPS_DEVICE_IPP, /* ipp://... */
44 CUPS_DEVICE_FAX_IPP, /* ipp://... */
45 CUPS_DEVICE_PDL_DATASTREAM, /* socket://... */
46 CUPS_DEVICE_RIOUSBPRINT /* riousbprint://... */
47} cups_devtype_t;
48
49
50typedef struct
51{
52 DNSServiceRef ref; /* Service reference for resolve */
53 char *name, /* Service name */
54 *domain, /* Domain name */
55 *fullName, /* Full name */
56 *make_and_model; /* Make and model from TXT record */
57 cups_devtype_t type; /* Device registration type */
58 int cups_shared, /* CUPS shared printer? */
59 sent; /* Did we list the device? */
60} cups_device_t;
61
62
63/*
64 * Local functions...
65 */
66
67static void browse_callback(DNSServiceRef sdRef,
68 DNSServiceFlags flags,
69 uint32_t interfaceIndex,
70 DNSServiceErrorType errorCode,
71 const char *serviceName,
72 const char *regtype,
73 const char *replyDomain, void *context);
74static void browse_local_callback(DNSServiceRef sdRef,
75 DNSServiceFlags flags,
76 uint32_t interfaceIndex,
77 DNSServiceErrorType errorCode,
78 const char *serviceName,
79 const char *regtype,
80 const char *replyDomain,
81 void *context);
82static int compare_devices(cups_device_t *a, cups_device_t *b);
83static void exec_backend(char **argv);
84static cups_device_t *get_device(cups_array_t *devices,
85 const char *serviceName,
86 const char *regtype,
87 const char *replyDomain);
88static void query_callback(DNSServiceRef sdRef,
89 DNSServiceFlags flags,
90 uint32_t interfaceIndex,
91 DNSServiceErrorType errorCode,
92 const char *fullName, uint16_t rrtype,
93 uint16_t rrclass, uint16_t rdlen,
94 const void *rdata, uint32_t ttl,
95 void *context);
96static void unquote(char *dst, const char *src, size_t dstsize);
97
98
99/*
100 * 'main()' - Browse for printers.
101 */
102
103int /* O - Exit status */
104main(int argc, /* I - Number of command-line args */
105 char *argv[]) /* I - Command-line arguments */
106{
107 DNSServiceRef main_ref, /* Main service reference */
108 fax_ipp_ref, /* IPP fax service reference */
109 ipp_ref, /* IPP service reference */
110 ipp_tls_ref, /* IPP w/TLS service reference */
111 local_fax_ipp_ref, /* Local IPP fax service reference */
112 local_ipp_ref, /* Local IPP service reference */
113 local_ipp_tls_ref, /* Local IPP w/TLS service reference */
114 local_printer_ref, /* Local LPD service reference */
115 pdl_datastream_ref, /* AppSocket service reference */
116 printer_ref, /* LPD service reference */
117 riousbprint_ref; /* Remote IO service reference */
118 int fd; /* Main file descriptor */
119 fd_set input; /* Input set for select() */
120 struct timeval timeout; /* Timeout for select() */
121 cups_array_t *devices; /* Device array */
122 cups_device_t *device; /* Current device */
123
124
125 /*
126 * Check command-line...
127 */
128
129 setbuf(stderr, NULL);
130
131 if (argc >= 6)
132 exec_backend(argv);
133 else if (argc != 1)
134 {
135 fputs("Usage: mdns job user title copies options [filename(s)]\n", stderr);
136 return (1);
137 }
138
139 /*
140 * Create an array to track devices...
141 */
142
143 devices = cupsArrayNew((cups_array_func_t)compare_devices, NULL);
144
145 /*
146 * Browse for different kinds of printers...
147 */
148
149 if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError)
150 {
151 perror("ERROR: Unable to create service connection");
152 return (1);
153 }
154
155 fd = DNSServiceRefSockFD(main_ref);
156
157 fax_ipp_ref = main_ref;
158 DNSServiceBrowse(&fax_ipp_ref, kDNSServiceFlagsShareConnection, 0,
159 "_fax-ipp._tcp", NULL, browse_callback, devices);
160
161 ipp_ref = main_ref;
162 DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0,
163 "_ipp._tcp", NULL, browse_callback, devices);
164
165 ipp_tls_ref = main_ref;
166 DNSServiceBrowse(&ipp_tls_ref, kDNSServiceFlagsShareConnection, 0,
167 "_ipp-tls._tcp", NULL, browse_callback, devices);
168
169 local_fax_ipp_ref = main_ref;
170 DNSServiceBrowse(&local_fax_ipp_ref, kDNSServiceFlagsShareConnection,
171 kDNSServiceInterfaceIndexLocalOnly,
172 "_fax-ipp._tcp", NULL, browse_local_callback, devices);
173
174 local_ipp_ref = main_ref;
175 DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection,
176 kDNSServiceInterfaceIndexLocalOnly,
177 "_ipp._tcp", NULL, browse_local_callback, devices);
178
179 local_ipp_tls_ref = main_ref;
180 DNSServiceBrowse(&local_ipp_tls_ref, kDNSServiceFlagsShareConnection,
181 kDNSServiceInterfaceIndexLocalOnly,
182 "_ipp-tls._tcp", NULL, browse_local_callback, devices);
183
184 local_printer_ref = main_ref;
185 DNSServiceBrowse(&local_printer_ref, kDNSServiceFlagsShareConnection,
186 kDNSServiceInterfaceIndexLocalOnly,
187 "_printer._tcp", NULL, browse_local_callback, devices);
188
189 pdl_datastream_ref = main_ref;
190 DNSServiceBrowse(&pdl_datastream_ref, kDNSServiceFlagsShareConnection, 0,
191 "_pdl-datastream._tcp", NULL, browse_callback, devices);
192
193 printer_ref = main_ref;
194 DNSServiceBrowse(&printer_ref, kDNSServiceFlagsShareConnection, 0,
195 "_printer._tcp", NULL, browse_callback, devices);
196
197 riousbprint_ref = main_ref;
198 DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0,
199 "_riousbprint._tcp", NULL, browse_callback, devices);
200
201 /*
202 * Loop until we are killed...
203 */
204
205 for (;;)
206 {
207 FD_ZERO(&input);
208 FD_SET(fd, &input);
209
210 timeout.tv_sec = 1;
211 timeout.tv_usec = 0;
212
213 if (select(fd + 1, &input, NULL, NULL, &timeout) < 0)
214 continue;
215
216 if (FD_ISSET(fd, &input))
217 {
218 /*
219 * Process results of our browsing...
220 */
221
222 DNSServiceProcessResult(main_ref);
223 }
224 else
225 {
226 /*
227 * Announce any devices we've found...
228 */
229
230 char device_uri[1024]; /* Device URI */
231 static const char * const schemes[] =
232 { "lpd", "ipp", "ipp", "socket", "riousbprint" };
233 /* URI schemes for devices */
234
235
236 for (device = (cups_device_t *)cupsArrayFirst(devices);
237 device;
238 device = (cups_device_t *)cupsArrayNext(devices))
239 if (!device->ref && !device->sent)
240 {
241 /*
242 * Found the device, now get the TXT record(s) for it...
243 */
244
245 device->ref = main_ref;
246
247 fprintf(stderr, "DEBUG: Querying \"%s\"...\n", device->fullName);
248
249 if (DNSServiceQueryRecord(&(device->ref),
250 kDNSServiceFlagsShareConnection,
251 0, device->fullName, kDNSServiceType_TXT,
252 kDNSServiceClass_IN, query_callback,
253 devices) != kDNSServiceErr_NoError)
254 fputs("ERROR: Unable to query for TXT records!\n", stderr);
255 }
256 else if (!device->sent)
257 {
258 /*
259 * Got the TXT records, now report the device...
260 */
261
262 DNSServiceRefDeallocate(device->ref);
263 device->ref = 0;
264
c9fc04c6
MS
265 httpAssembleURI(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri),
266 schemes[device->type], NULL, device->fullName, 0,
267 device->cups_shared ? "/cups" : "/");
7a14d768
MS
268
269 printf("network %s \"%s\" \"%s\"\n", device_uri,
270 device->make_and_model ? device->make_and_model : "Unknown",
271 device->name);
a0f6818e 272 fflush(stdout);
7a14d768
MS
273
274 device->sent = 1;
275 }
276 }
277 }
278}
279
280
281/*
282 * 'browse_callback()' - Browse devices.
283 */
284
285static void
286browse_callback(
287 DNSServiceRef sdRef, /* I - Service reference */
288 DNSServiceFlags flags, /* I - Option flags */
289 uint32_t interfaceIndex, /* I - Interface number */
290 DNSServiceErrorType errorCode, /* I - Error, if any */
291 const char *serviceName, /* I - Name of service/device */
292 const char *regtype, /* I - Type of service */
293 const char *replyDomain, /* I - Service domain */
294 void *context) /* I - Devices array */
295{
296 fprintf(stderr, "DEBUG2: browse_callback(sdRef=%p, flags=%x, "
297 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
298 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
299 sdRef, flags, interfaceIndex, errorCode,
300 serviceName ? serviceName : "(null)",
301 regtype ? regtype : "(null)",
302 replyDomain ? replyDomain : "(null)",
303 context);
304
305 /*
306 * Only process "add" data...
307 */
308
309 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
310 return;
311
312 /*
313 * Get the device...
314 */
315
316 get_device((cups_array_t *)context, serviceName, regtype, replyDomain);
317}
318
319
320/*
321 * 'browse_local_callback()' - Browse local devices.
322 */
323
324static void
325browse_local_callback(
326 DNSServiceRef sdRef, /* I - Service reference */
327 DNSServiceFlags flags, /* I - Option flags */
328 uint32_t interfaceIndex, /* I - Interface number */
329 DNSServiceErrorType errorCode, /* I - Error, if any */
330 const char *serviceName, /* I - Name of service/device */
331 const char *regtype, /* I - Type of service */
332 const char *replyDomain, /* I - Service domain */
333 void *context) /* I - Devices array */
334{
335 cups_device_t *device; /* Device */
336
337
338 fprintf(stderr, "DEBUG2: browse_local_callback(sdRef=%p, flags=%x, "
339 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
340 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
341 sdRef, flags, interfaceIndex, errorCode,
342 serviceName ? serviceName : "(null)",
343 regtype ? regtype : "(null)",
344 replyDomain ? replyDomain : "(null)",
345 context);
346
347 /*
348 * Only process "add" data...
349 */
350
351 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
352 return;
353
354 /*
355 * Get the device...
356 */
357
358 device = get_device((cups_array_t *)context, serviceName, regtype,
359 replyDomain);
360
361 /*
362 * Hide locally-registered devices...
363 */
364
365 fprintf(stderr, "DEBUG: Hiding local printer \"%s\"...\n",
366 device->fullName);
367 device->sent = 1;
368}
369
370
371/*
372 * 'compare_devices()' - Compare two devices.
373 */
374
375static int /* O - Result of comparison */
376compare_devices(cups_device_t *a, /* I - First device */
377 cups_device_t *b) /* I - Second device */
378{
379 int result = strcmp(a->name, b->name);
380
381 if (result)
382 return (result);
383 else
384 return (strcmp(a->domain, b->domain));
385}
386
387
388/*
389 * 'exec_backend()' - Execute the backend that corresponds to the
390 * resolved service name.
391 */
392
393static void
394exec_backend(char **argv) /* I - Command-line arguments */
395{
396 const char *resolved_uri, /* Resolved device URI */
397 *cups_serverbin; /* Location of programs */
398 char scheme[1024], /* Scheme from URI */
399 *ptr, /* Pointer into scheme */
400 filename[1024]; /* Backend filename */
401
402
403 /*
404 * Resolve the device URI...
405 */
406
407 resolved_uri = backendResolveURI(argv);
408
409 /*
410 * Extract the scheme from the URI...
411 */
412
413 strlcpy(scheme, resolved_uri, sizeof(scheme));
414 if ((ptr = strchr(scheme, ':')) != NULL)
415 *ptr = '\0';
416
417 /*
418 * Get the filename of the backend...
419 */
420
421 if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
422 cups_serverbin = CUPS_SERVERBIN;
423
424 snprintf(filename, sizeof(filename), "%s/backend/%s", cups_serverbin, scheme);
425
426 /*
427 * Overwrite the device URIs and run the new backend...
428 */
429
430 setenv("DEVICE_URI", resolved_uri, 1);
431
432 argv[0] = (char *)resolved_uri;
433
434 fprintf(stderr, "DEBUG: Executing backend \"%s\"...\n", filename);
435
436 execv(filename, argv);
437
438 fprintf(stderr, "ERROR: Unable to execute backend \"%s\": %s\n", filename,
439 strerror(errno));
440 exit(CUPS_BACKEND_STOP);
441}
442
443
444/*
445 * 'get_device()' - Create or update a device.
446 */
447
448static cups_device_t * /* O - Device */
449get_device(cups_array_t *devices, /* I - Device array */
450 const char *serviceName, /* I - Name of service/device */
451 const char *regtype, /* I - Type of service */
452 const char *replyDomain) /* I - Service domain */
453{
454 cups_device_t key, /* Search key */
455 *device; /* Device */
456 char fullName[1024]; /* Full name for query */
457
458
459 /*
460 * See if this is a new device...
461 */
462
463 key.name = (char *)serviceName;
464 key.domain = (char *)replyDomain;
465
466 if (!strcmp(regtype, "_ipp._tcp.") ||
467 !strcmp(regtype, "_ipp-tls._tcp."))
468 key.type = CUPS_DEVICE_IPP;
469 else if (!strcmp(regtype, "_fax-ipp._tcp."))
470 key.type = CUPS_DEVICE_FAX_IPP;
471 else if (!strcmp(regtype, "_printer._tcp."))
472 key.type = CUPS_DEVICE_PRINTER;
473 else if (!strcmp(regtype, "_pdl-datastream._tcp."))
474 key.type = CUPS_DEVICE_PDL_DATASTREAM;
475 else
476 key.type = CUPS_DEVICE_RIOUSBPRINT;
477
478 if ((device = cupsArrayFind(devices, &key)) != NULL)
479 {
480 /*
481 * No, see if this registration is a higher priority protocol...
482 */
483
484 if (key.type > device->type)
485 {
486 fprintf(stderr, "DEBUG: Updating \"%s\" to \"%s.%s%s\"...\n",
487 device->fullName, serviceName, regtype, replyDomain);
488
489 device->type = key.type;
490 }
491 }
492 else
493 {
494 /*
495 * Yes, add the device...
496 */
497
498 fprintf(stderr, "DEBUG: Found \"%s.%s%s\"...\n", serviceName, regtype,
499 replyDomain);
500
501 device = calloc(sizeof(cups_device_t), 1);
502 device->name = strdup(serviceName);
503 device->domain = strdup(replyDomain);
504 device->type = key.type;
505
506 cupsArrayAdd(devices, device);
507 }
508
509 /*
510 * Update the "full name" of this service, which is used for queries...
511 */
512
513 snprintf(fullName, sizeof(fullName), "%s.%s%s", serviceName, regtype,
514 replyDomain);
515
516 if (device->fullName)
517 free(device->fullName);
518
519 device->fullName = strdup(fullName);
520
521 return (device);
522}
523
524
525/*
526 * 'query_callback()' - Process query data.
527 */
528
529static void
530query_callback(
531 DNSServiceRef sdRef, /* I - Service reference */
532 DNSServiceFlags flags, /* I - Data flags */
533 uint32_t interfaceIndex, /* I - Interface */
534 DNSServiceErrorType errorCode, /* I - Error, if any */
535 const char *fullName, /* I - Full service name */
536 uint16_t rrtype, /* I - Record type */
537 uint16_t rrclass, /* I - Record class */
538 uint16_t rdlen, /* I - Length of record data */
539 const void *rdata, /* I - Record data */
540 uint32_t ttl, /* I - Time-to-live */
541 void *context) /* I - Devices array */
542{
543 cups_array_t *devices; /* Device array */
544 char name[1024], /* Service name */
545 *ptr; /* Pointer into name */
546 cups_device_t key, /* Search key */
547 *device; /* Device */
548
549
550 fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
551 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
552 "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
553 "context=%p)\n",
554 sdRef, flags, interfaceIndex, errorCode,
555 fullName ? fullName : "(null)", rrtype, rrclass, rdlen, rdata, ttl,
556 context);
557
558 /*
559 * Only process "add" data...
560 */
561
562 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
563 return;
564
565 /*
566 * Lookup the service in the devices array.
567 */
568
569 devices = (cups_array_t *)context;
570 key.name = name;
571
572 unquote(name, fullName, sizeof(name));
573
574 if ((key.domain = strstr(name, "._tcp.")) != NULL)
575 key.domain += 6;
576 else
577 key.domain = (char *)"local.";
578
579 if ((ptr = strstr(name, "._")) != NULL)
580 *ptr = '\0';
581
582 if ((device = cupsArrayFind(devices, &key)) != NULL)
583 {
584 /*
585 * Found it, pull out the make and model from the TXT record and save it...
586 */
587
588 const void *value; /* Pointer to value */
589 uint8_t valueLen; /* Length of value (max 255) */
590 char make_and_model[512], /* Manufacturer and model */
591 model[256]; /* Model */
592
593
594 if ((value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MFG",
595 &valueLen)) == NULL)
596 value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MANUFACTURER", &valueLen);
597
598 if (value && valueLen)
599 {
600 memcpy(make_and_model, value, valueLen);
601 make_and_model[valueLen] = '\0';
602 }
603 else
604 make_and_model[0] = '\0';
605
606 if ((value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MDL",
607 &valueLen)) == NULL)
608 value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MODEL", &valueLen);
609
610 if (value && valueLen)
611 {
612 memcpy(model, value, valueLen);
613 model[valueLen] = '\0';
614 }
615 else if ((value = TXTRecordGetValuePtr(rdlen, rdata, "product",
616 &valueLen)) != NULL && valueLen > 2)
617 {
618 memcpy(model, value + 1, valueLen - 2);
619 model[valueLen - 2] = '\0';
620
621 if (!strcasecmp(model, "GPL Ghostscript") ||
622 !strcasecmp(model, "GNU Ghostscript") ||
623 !strcasecmp(model, "ESP Ghostscript"))
624 {
625 if ((value = TXTRecordGetValuePtr(rdlen, rdata, "ty",
626 &valueLen)) != NULL)
627 {
628 memcpy(model, value, valueLen);
629 model[valueLen] = '\0';
630
631 if ((ptr = strchr(model, ',')) != NULL)
632 *ptr = '\0';
633 }
634 else
635 strcpy(model, "Unknown");
636 }
637 }
638 else
639 strcpy(model, "Unknown");
640
641 if (device->make_and_model)
642 free(device->make_and_model);
643
644 if (make_and_model[0])
645 {
646 strlcat(make_and_model, " ", sizeof(make_and_model));
647 strlcat(make_and_model, model, sizeof(make_and_model));
648 device->make_and_model = strdup(make_and_model);
649 }
650 else
651 device->make_and_model = strdup(model);
652
a0f6818e
MS
653 if ((device->type == CUPS_DEVICE_IPP ||
654 device->type == CUPS_DEVICE_PRINTER) &&
7a14d768
MS
655 (value = TXTRecordGetValuePtr(rdlen, rdata, "printer-type",
656 &valueLen)) != NULL)
657 {
658 /*
659 * This is a CUPS printer!
660 */
661
662 device->cups_shared = 1;
a0f6818e
MS
663
664 if (device->type == CUPS_DEVICE_PRINTER)
665 device->sent = 1;
7a14d768
MS
666 }
667 }
668 else
669 fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
670}
671
672
673/*
674 * 'unquote()' - Unquote a name string.
675 */
676
677static void
678unquote(char *dst, /* I - Destination buffer */
679 const char *src, /* I - Source string */
680 size_t dstsize) /* I - Size of destination buffer */
681{
682 char *dstend = dst + dstsize - 1; /* End of destination buffer */
683
684
685 while (*src && dst < dstend)
686 {
687 if (*src == '\\')
688 {
689 src ++;
690 if (isdigit(src[0] & 255) && isdigit(src[1] & 255) &&
691 isdigit(src[2] & 255))
692 {
693 *dst++ = ((((src[0] - '0') * 10) + src[1] - '0') * 10) + src[2] - '0';
694 src += 3;
695 }
696 else
697 *dst++ = *src++;
698 }
699 else
700 *dst++ = *src ++;
701 }
702
703 *dst = '\0';
704}
705
706
707/*
708 * End of "$Id$".
709 */