4 * DNS-SD discovery backend for CUPS.
6 * Copyright 2008-2012 by Apple Inc.
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/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * main() - Browse for printers.
19 * browse_callback() - Browse devices.
20 * browse_local_callback() - Browse local devices.
21 * client_callback() - Avahi client callback function.
22 * compare_devices() - Compare two devices.
23 * exec_backend() - Execute the backend that corresponds to the
24 * resolved service name.
25 * device_type() - Get DNS-SD type enumeration from string.
26 * get_device() - Create or update a device.
27 * query_callback() - Process query data.
28 * find_device() - Find a device from its name and domain.
29 * sigterm_handler() - Handle termination signals.
30 * unquote() - Unquote a name string.
34 * Include necessary headers.
37 #include "backend-private.h"
38 #include <cups/array.h>
41 #endif /* HAVE_DNSSD */
43 # include <avahi-client/client.h>
44 # include <avahi-client/lookup.h>
45 # include <avahi-common/simple-watch.h>
46 # include <avahi-common/domain.h>
47 # include <avahi-common/error.h>
48 # include <avahi-common/malloc.h>
49 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
50 #endif /* HAVE_AVAHI */
59 CUPS_DEVICE_PRINTER
= 0, /* lpd://... */
60 CUPS_DEVICE_IPPS
, /* ipps://... */
61 CUPS_DEVICE_IPP
, /* ipp://... */
62 CUPS_DEVICE_FAX_IPP
, /* ipp://... */
63 CUPS_DEVICE_PDL_DATASTREAM
, /* socket://... */
64 CUPS_DEVICE_RIOUSBPRINT
/* riousbprint://... */
71 DNSServiceRef ref
; /* Service reference for query */
72 #endif /* HAVE_DNSSD */
74 AvahiRecordBrowser
*ref
; /* Browser for query */
75 #endif /* HAVE_AVAHI */
76 char *name
, /* Service name */
77 *domain
, /* Domain name */
78 *fullName
, /* Full name */
79 *make_and_model
, /* Make and model from TXT record */
80 *device_id
, /* 1284 device ID from TXT record */
81 *uuid
; /* UUID from TXT record */
82 cups_devtype_t type
; /* Device registration type */
83 int priority
, /* Priority associated with type */
84 cups_shared
, /* CUPS shared printer? */
85 sent
; /* Did we list the device? */
93 static int job_canceled
= 0;
94 /* Set to 1 on SIGTERM */
96 static AvahiSimplePoll
*simple_poll
= NULL
;
97 /* Poll information */
98 static int got_data
= 0; /* Got data from poll? */
99 #endif /* HAVE_AVAHI */
107 static void browse_callback(DNSServiceRef sdRef
,
108 DNSServiceFlags flags
,
109 uint32_t interfaceIndex
,
110 DNSServiceErrorType errorCode
,
111 const char *serviceName
,
113 const char *replyDomain
, void *context
)
114 __attribute__((nonnull(1,5,6,7,8)));
115 static void browse_local_callback(DNSServiceRef sdRef
,
116 DNSServiceFlags flags
,
117 uint32_t interfaceIndex
,
118 DNSServiceErrorType errorCode
,
119 const char *serviceName
,
121 const char *replyDomain
,
123 __attribute__((nonnull(1,5,6,7,8)));
124 #endif /* HAVE_DNSSD */
126 static void browse_callback(AvahiServiceBrowser
*browser
,
127 AvahiIfIndex interface
,
128 AvahiProtocol protocol
,
129 AvahiBrowserEvent event
,
130 const char *serviceName
,
132 const char *replyDomain
,
133 AvahiLookupResultFlags flags
,
135 static void client_callback(AvahiClient
*client
,
136 AvahiClientState state
,
138 #endif /* HAVE_AVAHI */
140 static int compare_devices(cups_device_t
*a
, cups_device_t
*b
);
141 static void exec_backend(char **argv
);
142 static cups_device_t
*get_device(cups_array_t
*devices
,
143 const char *serviceName
,
145 const char *replyDomain
)
146 __attribute__((nonnull(1,2,3,4)));
148 static void query_callback(DNSServiceRef sdRef
,
149 DNSServiceFlags flags
,
150 uint32_t interfaceIndex
,
151 DNSServiceErrorType errorCode
,
152 const char *fullName
, uint16_t rrtype
,
153 uint16_t rrclass
, uint16_t rdlen
,
154 const void *rdata
, uint32_t ttl
,
156 __attribute__((nonnull(1,5,9,11)));
157 #elif defined(HAVE_AVAHI)
158 static int poll_callback(struct pollfd
*pollfds
,
159 unsigned int num_pollfds
, int timeout
,
161 static void query_callback(AvahiRecordBrowser
*browser
,
162 AvahiIfIndex interface
,
163 AvahiProtocol protocol
,
164 AvahiBrowserEvent event
,
165 const char *name
, uint16_t rrclass
,
166 uint16_t rrtype
, const void *rdata
,
168 AvahiLookupResultFlags flags
,
170 #endif /* HAVE_DNSSD */
171 static void sigterm_handler(int sig
);
172 static void unquote(char *dst
, const char *src
, size_t dstsize
)
173 __attribute__((nonnull(1,2)));
177 * 'main()' - Browse for printers.
180 int /* O - Exit status */
181 main(int argc
, /* I - Number of command-line args */
182 char *argv
[]) /* I - Command-line arguments */
184 const char *name
; /* Backend name */
185 cups_array_t
*devices
; /* Device array */
186 cups_device_t
*device
; /* Current device */
187 char uriName
[1024]; /* Unquoted fullName for URI */
189 int fd
; /* Main file descriptor */
190 fd_set input
; /* Input set for select() */
191 struct timeval timeout
; /* Timeout for select() */
192 DNSServiceRef main_ref
, /* Main service reference */
193 fax_ipp_ref
, /* IPP fax service reference */
194 ipp_ref
, /* IPP service reference */
195 ipp_tls_ref
, /* IPP w/TLS service reference */
196 ipps_ref
, /* IPP service reference */
197 local_fax_ipp_ref
, /* Local IPP fax service reference */
198 local_ipp_ref
, /* Local IPP service reference */
199 local_ipp_tls_ref
, /* Local IPP w/TLS service reference */
200 local_ipps_ref
, /* Local IPP service reference */
201 local_printer_ref
, /* Local LPD service reference */
202 pdl_datastream_ref
, /* AppSocket service reference */
203 printer_ref
, /* LPD service reference */
204 riousbprint_ref
; /* Remote IO service reference */
205 #endif /* HAVE_DNSSD */
207 AvahiClient
*client
; /* Client information */
208 int error
; /* Error code, if any */
209 #endif /* HAVE_AVAHI */
210 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
211 struct sigaction action
; /* Actions for POSIX signals */
212 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
216 * Don't buffer stderr, and catch SIGTERM...
219 setbuf(stderr
, NULL
);
221 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
222 sigset(SIGTERM
, sigterm_handler
);
223 #elif defined(HAVE_SIGACTION)
224 memset(&action
, 0, sizeof(action
));
226 sigemptyset(&action
.sa_mask
);
227 action
.sa_handler
= sigterm_handler
;
228 sigaction(SIGTERM
, &action
, NULL
);
230 signal(SIGTERM
, sigterm_handler
);
231 #endif /* HAVE_SIGSET */
234 * Check command-line...
241 _cupsLangPrintf(stderr
,
242 _("Usage: %s job-id user title copies options [file]"),
248 * Only do discovery when run as "dnssd"...
251 if ((name
= strrchr(argv
[0], '/')) != NULL
)
256 if (strcmp(name
, "dnssd"))
260 * Create an array to track devices...
263 devices
= cupsArrayNew((cups_array_func_t
)compare_devices
, NULL
);
266 * Browse for different kinds of printers...
270 if (DNSServiceCreateConnection(&main_ref
) != kDNSServiceErr_NoError
)
272 perror("ERROR: Unable to create service connection");
276 fd
= DNSServiceRefSockFD(main_ref
);
278 fax_ipp_ref
= main_ref
;
279 DNSServiceBrowse(&fax_ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
280 "_fax-ipp._tcp", NULL
, browse_callback
, devices
);
283 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
284 "_ipp._tcp", NULL
, browse_callback
, devices
);
286 ipp_tls_ref
= main_ref
;
287 DNSServiceBrowse(&ipp_tls_ref
, kDNSServiceFlagsShareConnection
, 0,
288 "_ipp-tls._tcp", NULL
, browse_callback
, devices
);
291 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
292 "_ipps._tcp", NULL
, browse_callback
, devices
);
294 local_fax_ipp_ref
= main_ref
;
295 DNSServiceBrowse(&local_fax_ipp_ref
, kDNSServiceFlagsShareConnection
,
296 kDNSServiceInterfaceIndexLocalOnly
,
297 "_fax-ipp._tcp", NULL
, browse_local_callback
, devices
);
299 local_ipp_ref
= main_ref
;
300 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
301 kDNSServiceInterfaceIndexLocalOnly
,
302 "_ipp._tcp", NULL
, browse_local_callback
, devices
);
304 local_ipp_tls_ref
= main_ref
;
305 DNSServiceBrowse(&local_ipp_tls_ref
, kDNSServiceFlagsShareConnection
,
306 kDNSServiceInterfaceIndexLocalOnly
,
307 "_ipp-tls._tcp", NULL
, browse_local_callback
, devices
);
309 local_ipps_ref
= main_ref
;
310 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
311 kDNSServiceInterfaceIndexLocalOnly
,
312 "_ipps._tcp", NULL
, browse_local_callback
, devices
);
314 local_printer_ref
= main_ref
;
315 DNSServiceBrowse(&local_printer_ref
, kDNSServiceFlagsShareConnection
,
316 kDNSServiceInterfaceIndexLocalOnly
,
317 "_printer._tcp", NULL
, browse_local_callback
, devices
);
319 pdl_datastream_ref
= main_ref
;
320 DNSServiceBrowse(&pdl_datastream_ref
, kDNSServiceFlagsShareConnection
, 0,
321 "_pdl-datastream._tcp", NULL
, browse_callback
, devices
);
323 printer_ref
= main_ref
;
324 DNSServiceBrowse(&printer_ref
, kDNSServiceFlagsShareConnection
, 0,
325 "_printer._tcp", NULL
, browse_callback
, devices
);
327 riousbprint_ref
= main_ref
;
328 DNSServiceBrowse(&riousbprint_ref
, kDNSServiceFlagsShareConnection
, 0,
329 "_riousbprint._tcp", NULL
, browse_callback
, devices
);
330 #endif /* HAVE_DNSSD */
333 if ((simple_poll
= avahi_simple_poll_new()) == NULL
)
335 fputs("DEBUG: Unable to create Avahi simple poll object.\n", stderr
);
339 avahi_simple_poll_set_func(simple_poll
, poll_callback
, NULL
);
341 client
= avahi_client_new(avahi_simple_poll_get(simple_poll
),
342 0, client_callback
, simple_poll
, &error
);
345 fputs("DEBUG: Unable to create Avahi client.\n", stderr
);
349 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
351 "_fax-ipp._tcp", NULL
, 0,
352 browse_callback
, devices
);
353 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
355 "_ipp._tcp", NULL
, 0,
356 browse_callback
, devices
);
357 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
359 "_ipp-tls._tcp", NULL
, 0,
360 browse_callback
, devices
);
361 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
363 "_ipps._tcp", NULL
, 0,
364 browse_callback
, devices
);
365 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
367 "_pdl-datastream._tcp",
371 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
373 "_printer._tcp", NULL
, 0,
374 browse_callback
, devices
);
375 #endif /* HAVE_AVAHI */
378 * Loop until we are killed...
381 while (!job_canceled
)
383 int announce
= 0; /* Announce printers? */
390 timeout
.tv_usec
= 500000;
392 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
395 if (FD_ISSET(fd
, &input
))
398 * Process results of our browsing...
401 DNSServiceProcessResult(main_ref
);
406 #elif defined(HAVE_AVAHI)
409 if ((error
= avahi_simple_poll_iterate(simple_poll
, 500)) > 0)
412 * We've been told to exit the loop. Perhaps the connection to
421 #endif /* HAVE_DNSSD */
423 /* fprintf(stderr, "DEBUG: announce=%d\n", announce);*/
428 * Announce any devices we've found...
432 DNSServiceErrorType status
; /* DNS query status */
433 #endif /* HAVE_DNSSD */
434 cups_device_t
*best
; /* Best matching device */
435 char device_uri
[1024]; /* Device URI */
436 int count
; /* Number of queries */
437 int sent
; /* Number of sent */
439 for (device
= (cups_device_t
*)cupsArrayFirst(devices
),
440 best
= NULL
, count
= 0, sent
= 0;
442 device
= (cups_device_t
*)cupsArrayNext(devices
))
450 if (!device
->ref
&& !device
->sent
)
453 * Found the device, now get the TXT record(s) for it...
458 fprintf(stderr
, "DEBUG: Querying \"%s\"...\n", device
->fullName
);
461 device
->ref
= main_ref
;
463 status
= DNSServiceQueryRecord(&(device
->ref
),
464 kDNSServiceFlagsShareConnection
,
467 kDNSServiceClass_IN
, query_callback
,
469 if (status
!= kDNSServiceErr_NoError
)
471 "ERROR: Unable to query \"%s\" for TXT records: %d\n",
472 device
->fullName
, status
);
473 /* Users never see this */
478 if ((device
->ref
= avahi_record_browser_new(client
, AVAHI_IF_UNSPEC
,
487 "ERROR: Unable to query \"%s\" for TXT records: %s\n",
489 avahi_strerror(avahi_client_errno(client
)));
490 /* Users never see this */
493 #endif /* HAVE_AVAHI */
496 else if (!device
->sent
)
500 * Got the TXT records, now report the device...
503 DNSServiceRefDeallocate(device
->ref
);
505 avahi_record_browser_free(device
->ref
);
506 #endif /* HAVE_DNSSD */
512 else if (_cups_strcasecmp(best
->name
, device
->name
) ||
513 _cups_strcasecmp(best
->domain
, device
->domain
))
515 unquote(uriName
, best
->fullName
, sizeof(uriName
));
518 httpAssembleURIf(HTTP_URI_CODING_ALL
, device_uri
,
519 sizeof(device_uri
), "dnssd", NULL
, uriName
, 0,
520 best
->cups_shared
? "/cups?uuid=%s" : "/?uuid=%s",
523 httpAssembleURI(HTTP_URI_CODING_ALL
, device_uri
,
524 sizeof(device_uri
), "dnssd", NULL
, uriName
, 0,
525 best
->cups_shared
? "/cups" : "/");
527 cupsBackendReport("network", device_uri
, best
->make_and_model
,
528 best
->name
, best
->device_id
, NULL
);
534 else if (best
->priority
> device
->priority
||
535 (best
->priority
== device
->priority
&&
536 best
->type
< device
->type
))
554 unquote(uriName
, best
->fullName
, sizeof(uriName
));
557 httpAssembleURIf(HTTP_URI_CODING_ALL
, device_uri
,
558 sizeof(device_uri
), "dnssd", NULL
, uriName
, 0,
559 best
->cups_shared
? "/cups?uuid=%s" : "/?uuid=%s",
562 httpAssembleURI(HTTP_URI_CODING_ALL
, device_uri
,
563 sizeof(device_uri
), "dnssd", NULL
, uriName
, 0,
564 best
->cups_shared
? "/cups" : "/");
566 cupsBackendReport("network", device_uri
, best
->make_and_model
,
567 best
->name
, best
->device_id
, NULL
);
572 fprintf(stderr
, "DEBUG: sent=%d, count=%d\n", sent
, count
);
574 if (sent
== cupsArrayCount(devices
))
579 return (CUPS_BACKEND_OK
);
585 * 'browse_callback()' - Browse devices.
590 DNSServiceRef sdRef
, /* I - Service reference */
591 DNSServiceFlags flags
, /* I - Option flags */
592 uint32_t interfaceIndex
, /* I - Interface number */
593 DNSServiceErrorType errorCode
, /* I - Error, if any */
594 const char *serviceName
, /* I - Name of service/device */
595 const char *regtype
, /* I - Type of service */
596 const char *replyDomain
, /* I - Service domain */
597 void *context
) /* I - Devices array */
599 fprintf(stderr
, "DEBUG2: browse_callback(sdRef=%p, flags=%x, "
600 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
601 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
602 sdRef
, flags
, interfaceIndex
, errorCode
,
603 serviceName
, regtype
, replyDomain
, context
);
606 * Only process "add" data...
609 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
616 get_device((cups_array_t
*)context
, serviceName
, regtype
, replyDomain
);
621 * 'browse_local_callback()' - Browse local devices.
625 browse_local_callback(
626 DNSServiceRef sdRef
, /* I - Service reference */
627 DNSServiceFlags flags
, /* I - Option flags */
628 uint32_t interfaceIndex
, /* I - Interface number */
629 DNSServiceErrorType errorCode
, /* I - Error, if any */
630 const char *serviceName
, /* I - Name of service/device */
631 const char *regtype
, /* I - Type of service */
632 const char *replyDomain
, /* I - Service domain */
633 void *context
) /* I - Devices array */
635 cups_device_t
*device
; /* Device */
638 fprintf(stderr
, "DEBUG2: browse_local_callback(sdRef=%p, flags=%x, "
639 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
640 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
641 sdRef
, flags
, interfaceIndex
, errorCode
,
642 serviceName
, regtype
, replyDomain
, context
);
645 * Only process "add" data...
648 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
655 device
= get_device((cups_array_t
*)context
, serviceName
, regtype
,
659 * Hide locally-registered devices...
662 fprintf(stderr
, "DEBUG: Hiding local printer \"%s\"...\n",
666 #endif /* HAVE_DNSSD */
671 * 'browse_callback()' - Browse devices.
676 AvahiServiceBrowser
*browser
, /* I - Browser */
677 AvahiIfIndex interface
, /* I - Interface index (unused) */
678 AvahiProtocol protocol
, /* I - Network protocol (unused) */
679 AvahiBrowserEvent event
, /* I - What happened */
680 const char *name
, /* I - Service name */
681 const char *type
, /* I - Registration type */
682 const char *domain
, /* I - Domain */
683 AvahiLookupResultFlags flags
, /* I - Flags */
684 void *context
) /* I - Devices array */
686 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
687 /* Client information */
696 case AVAHI_BROWSER_FAILURE
:
697 fprintf(stderr
, "DEBUG: browse_callback: %s\n",
698 avahi_strerror(avahi_client_errno(client
)));
699 avahi_simple_poll_quit(simple_poll
);
702 case AVAHI_BROWSER_NEW
:
704 * This object is new on the network.
707 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
710 * This comes from the local machine so ignore it.
713 fprintf(stderr
, "DEBUG: Ignoring local service %s.\n", name
);
718 * Create a device entry for it if it doesn't yet exist.
721 get_device((cups_array_t
*)context
, name
, type
, domain
);
725 case AVAHI_BROWSER_REMOVE
:
726 case AVAHI_BROWSER_ALL_FOR_NOW
:
727 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
734 * 'client_callback()' - Avahi client callback function.
739 AvahiClient
*client
, /* I - Client information (unused) */
740 AvahiClientState state
, /* I - Current state */
741 void *context
) /* I - User data (unused) */
747 * If the connection drops, quit.
750 if (state
== AVAHI_CLIENT_FAILURE
)
752 fputs("DEBUG: Avahi connection failed.\n", stderr
);
753 avahi_simple_poll_quit(simple_poll
);
756 #endif /* HAVE_AVAHI */
760 * 'compare_devices()' - Compare two devices.
763 static int /* O - Result of comparison */
764 compare_devices(cups_device_t
*a
, /* I - First device */
765 cups_device_t
*b
) /* I - Second device */
767 return (strcmp(a
->name
, b
->name
));
772 * 'exec_backend()' - Execute the backend that corresponds to the
773 * resolved service name.
777 exec_backend(char **argv
) /* I - Command-line arguments */
779 const char *resolved_uri
, /* Resolved device URI */
780 *cups_serverbin
; /* Location of programs */
781 char scheme
[1024], /* Scheme from URI */
782 *ptr
, /* Pointer into scheme */
783 filename
[1024]; /* Backend filename */
787 * Resolve the device URI...
792 while ((resolved_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
794 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
797 if (getenv("CLASS") != NULL
)
798 exit(CUPS_BACKEND_FAILED
);
802 * Extract the scheme from the URI...
805 strlcpy(scheme
, resolved_uri
, sizeof(scheme
));
806 if ((ptr
= strchr(scheme
, ':')) != NULL
)
810 * Get the filename of the backend...
813 if ((cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
814 cups_serverbin
= CUPS_SERVERBIN
;
816 snprintf(filename
, sizeof(filename
), "%s/backend/%s", cups_serverbin
, scheme
);
819 * Overwrite the device URI and run the new backend...
822 setenv("DEVICE_URI", resolved_uri
, 1);
824 argv
[0] = (char *)resolved_uri
;
826 fprintf(stderr
, "DEBUG: Executing backend \"%s\"...\n", filename
);
828 execv(filename
, argv
);
830 fprintf(stderr
, "ERROR: Unable to execute backend \"%s\": %s\n", filename
,
832 exit(CUPS_BACKEND_STOP
);
837 * 'device_type()' - Get DNS-SD type enumeration from string.
841 device_type(const char *regtype
)
844 if (!strcmp(regtype
, "_ipp._tcp"))
845 return (CUPS_DEVICE_IPP
);
846 else if (!strcmp(regtype
, "_ipps._tcp") ||
847 !strcmp(regtype
, "_ipp-tls._tcp"))
848 return (CUPS_DEVICE_IPPS
);
849 else if (!strcmp(regtype
, "_fax-ipp._tcp"))
850 return (CUPS_DEVICE_FAX_IPP
);
851 else if (!strcmp(regtype
, "_printer._tcp"))
852 return (CUPS_DEVICE_PDL_DATASTREAM
);
854 if (!strcmp(regtype
, "_ipp._tcp."))
855 return (CUPS_DEVICE_IPP
);
856 else if (!strcmp(regtype
, "_ipps._tcp.") ||
857 !strcmp(regtype
, "_ipp-tls._tcp."))
858 return (CUPS_DEVICE_IPPS
);
859 else if (!strcmp(regtype
, "_fax-ipp._tcp."))
860 return (CUPS_DEVICE_FAX_IPP
);
861 else if (!strcmp(regtype
, "_printer._tcp."))
862 return (CUPS_DEVICE_PRINTER
);
863 else if (!strcmp(regtype
, "_pdl-datastream._tcp."))
864 return (CUPS_DEVICE_PDL_DATASTREAM
);
865 #endif /* HAVE_AVAHI */
867 return (CUPS_DEVICE_RIOUSBPRINT
);
872 * 'get_device()' - Create or update a device.
875 static cups_device_t
* /* O - Device */
876 get_device(cups_array_t
*devices
, /* I - Device array */
877 const char *serviceName
, /* I - Name of service/device */
878 const char *regtype
, /* I - Type of service */
879 const char *replyDomain
) /* I - Service domain */
881 cups_device_t key
, /* Search key */
882 *device
; /* Device */
883 char fullName
[kDNSServiceMaxDomainName
];
884 /* Full name for query */
888 * See if this is a new device...
891 key
.name
= (char *)serviceName
;
892 key
.type
= device_type(regtype
);
894 for (device
= cupsArrayFind(devices
, &key
);
896 device
= cupsArrayNext(devices
))
897 if (_cups_strcasecmp(device
->name
, key
.name
))
899 else if (device
->type
== key
.type
)
901 if (!_cups_strcasecmp(device
->domain
, "local.") &&
902 _cups_strcasecmp(device
->domain
, replyDomain
))
905 * Update the .local listing to use the "global" domain name instead.
906 * The backend will try local lookups first, then the global domain name.
909 free(device
->domain
);
910 device
->domain
= strdup(replyDomain
);
913 DNSServiceConstructFullName(fullName
, device
->name
, regtype
,
915 #else /* HAVE_AVAHI */
916 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
,
917 serviceName
, regtype
, replyDomain
);
918 #endif /* HAVE_DNSSD */
920 free(device
->fullName
);
921 device
->fullName
= strdup(fullName
);
928 * Yes, add the device...
931 fprintf(stderr
, "DEBUG: Found \"%s.%s%s\"...\n", serviceName
, regtype
,
934 device
= calloc(sizeof(cups_device_t
), 1);
935 device
->name
= strdup(serviceName
);
936 device
->domain
= strdup(replyDomain
);
937 device
->type
= key
.type
;
938 device
->priority
= 50;
940 cupsArrayAdd(devices
, device
);
943 * Set the "full name" of this service, which is used for queries...
947 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
948 #else /* HAVE_AVAHI */
949 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
,
950 serviceName
, regtype
, replyDomain
);
951 #endif /* HAVE_DNSSD */
953 device
->fullName
= strdup(fullName
);
961 * 'poll_callback()' - Wait for input on the specified file descriptors.
963 * Note: This function is needed because avahi_simple_poll_iterate is broken
964 * and always uses a timeout of 0 (!) milliseconds.
965 * (Avahi Ticket #364)
968 static int /* O - Number of file descriptors matching */
970 struct pollfd
*pollfds
, /* I - File descriptors */
971 unsigned int num_pollfds
, /* I - Number of file descriptors */
972 int timeout
, /* I - Timeout in milliseconds (unused) */
973 void *context
) /* I - User data (unused) */
975 int val
; /* Return value */
981 val
= poll(pollfds
, num_pollfds
, 500);
984 fprintf(stderr
, "DEBUG: poll_callback: %s\n", strerror(errno
));
990 #endif /* HAVE_AVAHI */
993 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
996 * 'query_callback()' - Process query data.
1001 DNSServiceRef sdRef
, /* I - Service reference */
1002 DNSServiceFlags flags
, /* I - Data flags */
1003 uint32_t interfaceIndex
, /* I - Interface */
1004 DNSServiceErrorType errorCode
, /* I - Error, if any */
1005 const char *fullName
, /* I - Full service name */
1006 uint16_t rrtype
, /* I - Record type */
1007 uint16_t rrclass
, /* I - Record class */
1008 uint16_t rdlen
, /* I - Length of record data */
1009 const void *rdata
, /* I - Record data */
1010 uint32_t ttl
, /* I - Time-to-live */
1011 void *context
) /* I - Device */
1015 * 'query_callback()' - Process query data.
1020 AvahiRecordBrowser
*browser
, /* I - Record browser */
1021 AvahiIfIndex interfaceIndex
,
1022 /* I - Interface index (unused) */
1023 AvahiProtocol protocol
, /* I - Network protocol (unused) */
1024 AvahiBrowserEvent event
, /* I - What happened? */
1025 const char *fullName
, /* I - Service name */
1026 uint16_t rrclass
, /* I - Record class */
1027 uint16_t rrtype
, /* I - Record type */
1028 const void *rdata
, /* I - TXT record */
1029 size_t rdlen
, /* I - Length of TXT record */
1030 AvahiLookupResultFlags flags
, /* I - Flags */
1031 void *context
) /* I - Device */
1033 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
1034 /* Client information */
1035 # endif /* HAVE_DNSSD */
1036 char *ptr
; /* Pointer into string */
1037 cups_device_t
*device
= (cups_device_t
*)context
;
1039 const uint8_t *data
, /* Pointer into data */
1040 *datanext
, /* Next key/value pair */
1041 *dataend
; /* End of entire TXT record */
1042 uint8_t datalen
; /* Length of current key/value pair */
1043 char key
[256], /* Key string */
1044 value
[256], /* Value string */
1045 make_and_model
[512], /* Manufacturer and model */
1046 model
[256], /* Model */
1048 device_id
[2048]; /* 1284 device ID */
1052 fprintf(stderr
, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
1053 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
1054 "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
1056 sdRef
, flags
, interfaceIndex
, errorCode
,
1057 fullName
? fullName
: "(null)", rrtype
, rrclass
, rdlen
, rdata
, ttl
,
1061 * Only process "add" data...
1064 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
1068 fprintf(stderr
, "DEBUG2: query_callback(browser=%p, interfaceIndex=%d, "
1069 "protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, "
1070 "rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)\n",
1071 browser
, interfaceIndex
, protocol
, event
,
1072 fullName
? fullName
: "(null)", rrclass
, rrtype
, rdata
,
1073 (unsigned)rdlen
, flags
, context
);
1076 * Only process "add" data...
1079 if (event
!= AVAHI_BROWSER_NEW
)
1081 if (event
== AVAHI_BROWSER_FAILURE
)
1082 fprintf(stderr
, "ERROR: %s\n",
1083 avahi_strerror(avahi_client_errno(client
)));
1087 # endif /* HAVE_DNSSD */
1090 * Pull out the priority and make and model from the TXT
1091 * record and save it...
1094 device_id
[0] = '\0';
1095 make_and_model
[0] = '\0';
1098 strlcpy(model
, "Unknown", sizeof(model
));
1100 for (data
= rdata
, dataend
= data
+ rdlen
;
1105 * Read a key/value pair starting with an 8-bit length. Since the
1106 * length is 8 bits and the size of the key/value buffers is 256, we
1107 * don't need to check for overflow...
1112 if (!datalen
|| (data
+ datalen
) > dataend
)
1115 datanext
= data
+ datalen
;
1117 for (ptr
= key
; data
< datanext
&& *data
!= '='; data
++)
1121 if (data
< datanext
&& *data
== '=')
1125 if (data
< datanext
)
1126 memcpy(value
, data
, datanext
- data
);
1127 value
[datanext
- data
] = '\0';
1129 fprintf(stderr
, "DEBUG2: query_callback: \"%s=%s\".\n",
1134 fprintf(stderr
, "DEBUG2: query_callback: \"%s\" with no value.\n",
1139 if (!_cups_strncasecmp(key
, "usb_", 4))
1142 * Add USB device ID information...
1145 ptr
= device_id
+ strlen(device_id
);
1146 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%s:%s;",
1150 if (!_cups_strcasecmp(key
, "usb_MFG") || !_cups_strcasecmp(key
, "usb_MANU") ||
1151 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
1152 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
1153 else if (!_cups_strcasecmp(key
, "usb_MDL") || !_cups_strcasecmp(key
, "usb_MODEL"))
1154 strlcpy(model
, value
, sizeof(model
));
1155 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
1157 if (value
[0] == '(')
1160 * Strip parenthesis...
1163 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
1166 strlcpy(model
, value
+ 1, sizeof(model
));
1169 strlcpy(model
, value
, sizeof(model
));
1171 else if (!_cups_strcasecmp(key
, "ty"))
1173 strlcpy(model
, value
, sizeof(model
));
1175 if ((ptr
= strchr(model
, ',')) != NULL
)
1178 else if (!_cups_strcasecmp(key
, "pdl"))
1179 strlcpy(pdl
, value
, sizeof(pdl
));
1180 else if (!_cups_strcasecmp(key
, "priority"))
1181 device
->priority
= atoi(value
);
1182 else if ((device
->type
== CUPS_DEVICE_IPP
||
1183 device
->type
== CUPS_DEVICE_IPPS
||
1184 device
->type
== CUPS_DEVICE_PRINTER
) &&
1185 !_cups_strcasecmp(key
, "printer-type"))
1188 * This is a CUPS printer!
1191 device
->cups_shared
= 1;
1193 if (device
->type
== CUPS_DEVICE_PRINTER
)
1196 else if (!_cups_strcasecmp(key
, "UUID"))
1197 device
->uuid
= strdup(value
);
1200 if (device
->device_id
)
1201 free(device
->device_id
);
1203 if (!device_id
[0] && strcmp(model
, "Unknown"))
1205 if (make_and_model
[0])
1206 snprintf(device_id
, sizeof(device_id
), "MFG:%s;MDL:%s;",
1207 make_and_model
, model
);
1208 else if (!_cups_strncasecmp(model
, "designjet ", 10))
1209 snprintf(device_id
, sizeof(device_id
), "MFG:HP;MDL:%s", model
+ 10);
1210 else if (!_cups_strncasecmp(model
, "stylus ", 7))
1211 snprintf(device_id
, sizeof(device_id
), "MFG:EPSON;MDL:%s", model
+ 7);
1212 else if ((ptr
= strchr(model
, ' ')) != NULL
)
1215 * Assume the first word is the make...
1218 memcpy(make_and_model
, model
, ptr
- model
);
1219 make_and_model
[ptr
- model
] = '\0';
1221 snprintf(device_id
, sizeof(device_id
), "MFG:%s;MDL:%s",
1222 make_and_model
, ptr
+ 1);
1227 !strstr(device_id
, "CMD:") &&
1228 !strstr(device_id
, "COMMAND SET:") &&
1229 (strstr(pdl
, "application/pdf") ||
1230 strstr(pdl
, "application/postscript") ||
1231 strstr(pdl
, "application/vnd.hp-PCL") ||
1232 strstr(pdl
, "image/")))
1235 if (strstr(pdl
, "application/pdf"))
1236 strlcat(value
, ",PDF", sizeof(value
));
1237 if (strstr(pdl
, "application/postscript"))
1238 strlcat(value
, ",PS", sizeof(value
));
1239 if (strstr(pdl
, "application/vnd.hp-PCL"))
1240 strlcat(value
, ",PCL", sizeof(value
));
1241 for (ptr
= strstr(pdl
, "image/"); ptr
; ptr
= strstr(ptr
, "image/"))
1243 char *valptr
= value
+ strlen(value
);
1244 /* Pointer into value */
1246 if (valptr
< (value
+ sizeof(value
) - 1))
1250 while (isalnum(*ptr
& 255) || *ptr
== '-' || *ptr
== '.')
1252 if (isalnum(*ptr
& 255) && valptr
< (value
+ sizeof(value
) - 1))
1253 *valptr
++ = toupper(*ptr
++ & 255);
1261 ptr
= device_id
+ strlen(device_id
);
1262 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "CMD:%s;",
1267 device
->device_id
= strdup(device_id
);
1269 device
->device_id
= NULL
;
1271 if (device
->make_and_model
)
1272 free(device
->make_and_model
);
1274 if (make_and_model
[0])
1276 strlcat(make_and_model
, " ", sizeof(make_and_model
));
1277 strlcat(make_and_model
, model
, sizeof(make_and_model
));
1279 device
->make_and_model
= strdup(make_and_model
);
1282 device
->make_and_model
= strdup(model
);
1284 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1288 * 'sigterm_handler()' - Handle termination signals.
1292 sigterm_handler(int sig
) /* I - Signal number (unused) */
1297 exit(CUPS_BACKEND_OK
);
1304 * 'unquote()' - Unquote a name string.
1308 unquote(char *dst
, /* I - Destination buffer */
1309 const char *src
, /* I - Source string */
1310 size_t dstsize
) /* I - Size of destination buffer */
1312 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
1315 while (*src
&& dst
< dstend
)
1320 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
1321 isdigit(src
[2] & 255))
1323 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';