]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/mdns.c
f7f78aae64fede4e8c1336424355064ef7b20539
[thirdparty/cups.git] / backend / mdns.c
1 /*
2 * "$Id$"
3 *
4 * DNS-SD discovery backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2008 by Apple Inc.
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
40 typedef 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
50 typedef 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
67 static 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);
74 static 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);
82 static int compare_devices(cups_device_t *a, cups_device_t *b);
83 static void exec_backend(char **argv);
84 static cups_device_t *get_device(cups_array_t *devices,
85 const char *serviceName,
86 const char *regtype,
87 const char *replyDomain);
88 static 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);
96 static void unquote(char *dst, const char *src, size_t dstsize);
97
98
99 /*
100 * 'main()' - Browse for printers.
101 */
102
103 int /* O - Exit status */
104 main(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
265 httpAssembleURIf(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri),
266 schemes[device->type], NULL,
267 device->cups_shared ? "cups" : "", 0,
268 "/%s", device->fullName);
269
270 printf("network %s \"%s\" \"%s\"\n", device_uri,
271 device->make_and_model ? device->make_and_model : "Unknown",
272 device->name);
273 fflush(stdout);
274
275 device->sent = 1;
276 }
277 }
278 }
279 }
280
281
282 /*
283 * 'browse_callback()' - Browse devices.
284 */
285
286 static void
287 browse_callback(
288 DNSServiceRef sdRef, /* I - Service reference */
289 DNSServiceFlags flags, /* I - Option flags */
290 uint32_t interfaceIndex, /* I - Interface number */
291 DNSServiceErrorType errorCode, /* I - Error, if any */
292 const char *serviceName, /* I - Name of service/device */
293 const char *regtype, /* I - Type of service */
294 const char *replyDomain, /* I - Service domain */
295 void *context) /* I - Devices array */
296 {
297 fprintf(stderr, "DEBUG2: browse_callback(sdRef=%p, flags=%x, "
298 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
299 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
300 sdRef, flags, interfaceIndex, errorCode,
301 serviceName ? serviceName : "(null)",
302 regtype ? regtype : "(null)",
303 replyDomain ? replyDomain : "(null)",
304 context);
305
306 /*
307 * Only process "add" data...
308 */
309
310 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
311 return;
312
313 /*
314 * Get the device...
315 */
316
317 get_device((cups_array_t *)context, serviceName, regtype, replyDomain);
318 }
319
320
321 /*
322 * 'browse_local_callback()' - Browse local devices.
323 */
324
325 static void
326 browse_local_callback(
327 DNSServiceRef sdRef, /* I - Service reference */
328 DNSServiceFlags flags, /* I - Option flags */
329 uint32_t interfaceIndex, /* I - Interface number */
330 DNSServiceErrorType errorCode, /* I - Error, if any */
331 const char *serviceName, /* I - Name of service/device */
332 const char *regtype, /* I - Type of service */
333 const char *replyDomain, /* I - Service domain */
334 void *context) /* I - Devices array */
335 {
336 cups_device_t *device; /* Device */
337
338
339 fprintf(stderr, "DEBUG2: browse_local_callback(sdRef=%p, flags=%x, "
340 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
341 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
342 sdRef, flags, interfaceIndex, errorCode,
343 serviceName ? serviceName : "(null)",
344 regtype ? regtype : "(null)",
345 replyDomain ? replyDomain : "(null)",
346 context);
347
348 /*
349 * Only process "add" data...
350 */
351
352 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
353 return;
354
355 /*
356 * Get the device...
357 */
358
359 device = get_device((cups_array_t *)context, serviceName, regtype,
360 replyDomain);
361
362 /*
363 * Hide locally-registered devices...
364 */
365
366 fprintf(stderr, "DEBUG: Hiding local printer \"%s\"...\n",
367 device->fullName);
368 device->sent = 1;
369 }
370
371
372 /*
373 * 'compare_devices()' - Compare two devices.
374 */
375
376 static int /* O - Result of comparison */
377 compare_devices(cups_device_t *a, /* I - First device */
378 cups_device_t *b) /* I - Second device */
379 {
380 int result = strcmp(a->name, b->name);
381
382 if (result)
383 return (result);
384 else
385 return (strcmp(a->domain, b->domain));
386 }
387
388
389 /*
390 * 'exec_backend()' - Execute the backend that corresponds to the
391 * resolved service name.
392 */
393
394 static void
395 exec_backend(char **argv) /* I - Command-line arguments */
396 {
397 const char *resolved_uri, /* Resolved device URI */
398 *cups_serverbin; /* Location of programs */
399 char scheme[1024], /* Scheme from URI */
400 *ptr, /* Pointer into scheme */
401 filename[1024]; /* Backend filename */
402
403
404 /*
405 * Resolve the device URI...
406 */
407
408 resolved_uri = backendResolveURI(argv);
409
410 /*
411 * Extract the scheme from the URI...
412 */
413
414 strlcpy(scheme, resolved_uri, sizeof(scheme));
415 if ((ptr = strchr(scheme, ':')) != NULL)
416 *ptr = '\0';
417
418 /*
419 * Get the filename of the backend...
420 */
421
422 if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
423 cups_serverbin = CUPS_SERVERBIN;
424
425 snprintf(filename, sizeof(filename), "%s/backend/%s", cups_serverbin, scheme);
426
427 /*
428 * Overwrite the device URIs and run the new backend...
429 */
430
431 setenv("DEVICE_URI", resolved_uri, 1);
432
433 argv[0] = (char *)resolved_uri;
434
435 fprintf(stderr, "DEBUG: Executing backend \"%s\"...\n", filename);
436
437 execv(filename, argv);
438
439 fprintf(stderr, "ERROR: Unable to execute backend \"%s\": %s\n", filename,
440 strerror(errno));
441 exit(CUPS_BACKEND_STOP);
442 }
443
444
445 /*
446 * 'get_device()' - Create or update a device.
447 */
448
449 static cups_device_t * /* O - Device */
450 get_device(cups_array_t *devices, /* I - Device array */
451 const char *serviceName, /* I - Name of service/device */
452 const char *regtype, /* I - Type of service */
453 const char *replyDomain) /* I - Service domain */
454 {
455 cups_device_t key, /* Search key */
456 *device; /* Device */
457 char fullName[1024]; /* Full name for query */
458
459
460 /*
461 * See if this is a new device...
462 */
463
464 key.name = (char *)serviceName;
465 key.domain = (char *)replyDomain;
466
467 if (!strcmp(regtype, "_ipp._tcp.") ||
468 !strcmp(regtype, "_ipp-tls._tcp."))
469 key.type = CUPS_DEVICE_IPP;
470 else if (!strcmp(regtype, "_fax-ipp._tcp."))
471 key.type = CUPS_DEVICE_FAX_IPP;
472 else if (!strcmp(regtype, "_printer._tcp."))
473 key.type = CUPS_DEVICE_PRINTER;
474 else if (!strcmp(regtype, "_pdl-datastream._tcp."))
475 key.type = CUPS_DEVICE_PDL_DATASTREAM;
476 else
477 key.type = CUPS_DEVICE_RIOUSBPRINT;
478
479 if ((device = cupsArrayFind(devices, &key)) != NULL)
480 {
481 /*
482 * No, see if this registration is a higher priority protocol...
483 */
484
485 if (key.type > device->type)
486 {
487 fprintf(stderr, "DEBUG: Updating \"%s\" to \"%s.%s%s\"...\n",
488 device->fullName, serviceName, regtype, replyDomain);
489
490 device->type = key.type;
491 }
492 }
493 else
494 {
495 /*
496 * Yes, add the device...
497 */
498
499 fprintf(stderr, "DEBUG: Found \"%s.%s%s\"...\n", serviceName, regtype,
500 replyDomain);
501
502 device = calloc(sizeof(cups_device_t), 1);
503 device->name = strdup(serviceName);
504 device->domain = strdup(replyDomain);
505 device->type = key.type;
506
507 cupsArrayAdd(devices, device);
508 }
509
510 /*
511 * Update the "full name" of this service, which is used for queries...
512 */
513
514 snprintf(fullName, sizeof(fullName), "%s.%s%s", serviceName, regtype,
515 replyDomain);
516
517 if (device->fullName)
518 free(device->fullName);
519
520 device->fullName = strdup(fullName);
521
522 return (device);
523 }
524
525
526 /*
527 * 'query_callback()' - Process query data.
528 */
529
530 static void
531 query_callback(
532 DNSServiceRef sdRef, /* I - Service reference */
533 DNSServiceFlags flags, /* I - Data flags */
534 uint32_t interfaceIndex, /* I - Interface */
535 DNSServiceErrorType errorCode, /* I - Error, if any */
536 const char *fullName, /* I - Full service name */
537 uint16_t rrtype, /* I - Record type */
538 uint16_t rrclass, /* I - Record class */
539 uint16_t rdlen, /* I - Length of record data */
540 const void *rdata, /* I - Record data */
541 uint32_t ttl, /* I - Time-to-live */
542 void *context) /* I - Devices array */
543 {
544 cups_array_t *devices; /* Device array */
545 char name[1024], /* Service name */
546 *ptr; /* Pointer into name */
547 cups_device_t key, /* Search key */
548 *device; /* Device */
549
550
551 fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
552 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
553 "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
554 "context=%p)\n",
555 sdRef, flags, interfaceIndex, errorCode,
556 fullName ? fullName : "(null)", rrtype, rrclass, rdlen, rdata, ttl,
557 context);
558
559 /*
560 * Only process "add" data...
561 */
562
563 if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
564 return;
565
566 /*
567 * Lookup the service in the devices array.
568 */
569
570 devices = (cups_array_t *)context;
571 key.name = name;
572
573 unquote(name, fullName, sizeof(name));
574
575 if ((key.domain = strstr(name, "._tcp.")) != NULL)
576 key.domain += 6;
577 else
578 key.domain = (char *)"local.";
579
580 if ((ptr = strstr(name, "._")) != NULL)
581 *ptr = '\0';
582
583 if ((device = cupsArrayFind(devices, &key)) != NULL)
584 {
585 /*
586 * Found it, pull out the make and model from the TXT record and save it...
587 */
588
589 const void *value; /* Pointer to value */
590 uint8_t valueLen; /* Length of value (max 255) */
591 char make_and_model[512], /* Manufacturer and model */
592 model[256]; /* Model */
593
594
595 if ((value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MFG",
596 &valueLen)) == NULL)
597 value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MANUFACTURER", &valueLen);
598
599 if (value && valueLen)
600 {
601 memcpy(make_and_model, value, valueLen);
602 make_and_model[valueLen] = '\0';
603 }
604 else
605 make_and_model[0] = '\0';
606
607 if ((value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MDL",
608 &valueLen)) == NULL)
609 value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MODEL", &valueLen);
610
611 if (value && valueLen)
612 {
613 memcpy(model, value, valueLen);
614 model[valueLen] = '\0';
615 }
616 else if ((value = TXTRecordGetValuePtr(rdlen, rdata, "product",
617 &valueLen)) != NULL && valueLen > 2)
618 {
619 memcpy(model, value + 1, valueLen - 2);
620 model[valueLen - 2] = '\0';
621
622 if (!strcasecmp(model, "GPL Ghostscript") ||
623 !strcasecmp(model, "GNU Ghostscript") ||
624 !strcasecmp(model, "ESP Ghostscript"))
625 {
626 if ((value = TXTRecordGetValuePtr(rdlen, rdata, "ty",
627 &valueLen)) != NULL)
628 {
629 memcpy(model, value, valueLen);
630 model[valueLen] = '\0';
631
632 if ((ptr = strchr(model, ',')) != NULL)
633 *ptr = '\0';
634 }
635 else
636 strcpy(model, "Unknown");
637 }
638 }
639 else
640 strcpy(model, "Unknown");
641
642 if (device->make_and_model)
643 free(device->make_and_model);
644
645 if (make_and_model[0])
646 {
647 strlcat(make_and_model, " ", sizeof(make_and_model));
648 strlcat(make_and_model, model, sizeof(make_and_model));
649 device->make_and_model = strdup(make_and_model);
650 }
651 else
652 device->make_and_model = strdup(model);
653
654 if ((device->type == CUPS_DEVICE_IPP ||
655 device->type == CUPS_DEVICE_PRINTER) &&
656 (value = TXTRecordGetValuePtr(rdlen, rdata, "printer-type",
657 &valueLen)) != NULL)
658 {
659 /*
660 * This is a CUPS printer!
661 */
662
663 device->cups_shared = 1;
664
665 if (device->type == CUPS_DEVICE_PRINTER)
666 device->sent = 1;
667 }
668 }
669 else
670 fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
671 }
672
673
674 /*
675 * 'unquote()' - Unquote a name string.
676 */
677
678 static void
679 unquote(char *dst, /* I - Destination buffer */
680 const char *src, /* I - Source string */
681 size_t dstsize) /* I - Size of destination buffer */
682 {
683 char *dstend = dst + dstsize - 1; /* End of destination buffer */
684
685
686 while (*src && dst < dstend)
687 {
688 if (*src == '\\')
689 {
690 src ++;
691 if (isdigit(src[0] & 255) && isdigit(src[1] & 255) &&
692 isdigit(src[2] & 255))
693 {
694 *dst++ = ((((src[0] - '0') * 10) + src[1] - '0') * 10) + src[2] - '0';
695 src += 3;
696 }
697 else
698 *dst++ = *src++;
699 }
700 else
701 *dst++ = *src ++;
702 }
703
704 *dst = '\0';
705 }
706
707
708 /*
709 * End of "$Id$".
710 */