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 cups_devtype_t type
; /* Device registration type */
82 int priority
, /* Priority associated with type */
83 cups_shared
, /* CUPS shared printer? */
84 sent
; /* Did we list the device? */
92 static int job_canceled
= 0;
93 /* Set to 1 on SIGTERM */
95 static AvahiSimplePoll
*simple_poll
= NULL
;
96 /* Poll information */
97 static int got_data
= 0; /* Got data from poll? */
98 #endif /* HAVE_AVAHI */
106 static void browse_callback(DNSServiceRef sdRef
,
107 DNSServiceFlags flags
,
108 uint32_t interfaceIndex
,
109 DNSServiceErrorType errorCode
,
110 const char *serviceName
,
112 const char *replyDomain
, void *context
)
113 __attribute__((nonnull(1,5,6,7,8)));
114 static void browse_local_callback(DNSServiceRef sdRef
,
115 DNSServiceFlags flags
,
116 uint32_t interfaceIndex
,
117 DNSServiceErrorType errorCode
,
118 const char *serviceName
,
120 const char *replyDomain
,
122 __attribute__((nonnull(1,5,6,7,8)));
123 #endif /* HAVE_DNSSD */
125 static void browse_callback(AvahiServiceBrowser
*browser
,
126 AvahiIfIndex interface
,
127 AvahiProtocol protocol
,
128 AvahiBrowserEvent event
,
129 const char *serviceName
,
131 const char *replyDomain
,
132 AvahiLookupResultFlags flags
,
134 static void client_callback(AvahiClient
*client
,
135 AvahiClientState state
,
137 #endif /* HAVE_AVAHI */
139 static int compare_devices(cups_device_t
*a
, cups_device_t
*b
);
140 static void exec_backend(char **argv
);
141 static cups_device_t
*get_device(cups_array_t
*devices
,
142 const char *serviceName
,
144 const char *replyDomain
)
145 __attribute__((nonnull(1,2,3,4)));
147 static void query_callback(DNSServiceRef sdRef
,
148 DNSServiceFlags flags
,
149 uint32_t interfaceIndex
,
150 DNSServiceErrorType errorCode
,
151 const char *fullName
, uint16_t rrtype
,
152 uint16_t rrclass
, uint16_t rdlen
,
153 const void *rdata
, uint32_t ttl
,
155 __attribute__((nonnull(1,5,9,11)));
156 #elif defined(HAVE_AVAHI)
157 static int poll_callback(struct pollfd
*pollfds
,
158 unsigned int num_pollfds
, int timeout
,
160 static void query_callback(AvahiRecordBrowser
*browser
,
161 AvahiIfIndex interface
,
162 AvahiProtocol protocol
,
163 AvahiBrowserEvent event
,
164 const char *name
, uint16_t rrclass
,
165 uint16_t rrtype
, const void *rdata
,
167 AvahiLookupResultFlags flags
,
169 #endif /* HAVE_DNSSD */
170 static void sigterm_handler(int sig
);
171 static void unquote(char *dst
, const char *src
, size_t dstsize
)
172 __attribute__((nonnull(1,2)));
176 * 'main()' - Browse for printers.
179 int /* O - Exit status */
180 main(int argc
, /* I - Number of command-line args */
181 char *argv
[]) /* I - Command-line arguments */
183 const char *name
; /* Backend name */
184 cups_array_t
*devices
; /* Device array */
185 cups_device_t
*device
; /* Current device */
186 char uriName
[1024]; /* Unquoted fullName for URI */
188 int fd
; /* Main file descriptor */
189 fd_set input
; /* Input set for select() */
190 struct timeval timeout
; /* Timeout for select() */
191 DNSServiceRef main_ref
, /* Main service reference */
192 fax_ipp_ref
, /* IPP fax service reference */
193 ipp_ref
, /* IPP service reference */
194 ipp_tls_ref
, /* IPP w/TLS service reference */
195 ipps_ref
, /* IPP service reference */
196 local_fax_ipp_ref
, /* Local IPP fax service reference */
197 local_ipp_ref
, /* Local IPP service reference */
198 local_ipp_tls_ref
, /* Local IPP w/TLS service reference */
199 local_ipps_ref
, /* Local IPP service reference */
200 local_printer_ref
, /* Local LPD service reference */
201 pdl_datastream_ref
, /* AppSocket service reference */
202 printer_ref
, /* LPD service reference */
203 riousbprint_ref
; /* Remote IO service reference */
204 #endif /* HAVE_DNSSD */
206 AvahiClient
*client
; /* Client information */
207 int error
; /* Error code, if any */
208 #endif /* HAVE_AVAHI */
209 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
210 struct sigaction action
; /* Actions for POSIX signals */
211 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
215 * Don't buffer stderr, and catch SIGTERM...
218 setbuf(stderr
, NULL
);
220 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
221 sigset(SIGTERM
, sigterm_handler
);
222 #elif defined(HAVE_SIGACTION)
223 memset(&action
, 0, sizeof(action
));
225 sigemptyset(&action
.sa_mask
);
226 action
.sa_handler
= sigterm_handler
;
227 sigaction(SIGTERM
, &action
, NULL
);
229 signal(SIGTERM
, sigterm_handler
);
230 #endif /* HAVE_SIGSET */
233 * Check command-line...
240 _cupsLangPrintf(stderr
,
241 _("Usage: %s job-id user title copies options [file]"),
247 * Only do discovery when run as "dnssd"...
250 if ((name
= strrchr(argv
[0], '/')) != NULL
)
255 if (strcmp(name
, "dnssd"))
259 * Create an array to track devices...
262 devices
= cupsArrayNew((cups_array_func_t
)compare_devices
, NULL
);
265 * Browse for different kinds of printers...
269 if (DNSServiceCreateConnection(&main_ref
) != kDNSServiceErr_NoError
)
271 perror("ERROR: Unable to create service connection");
275 fd
= DNSServiceRefSockFD(main_ref
);
277 fax_ipp_ref
= main_ref
;
278 DNSServiceBrowse(&fax_ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
279 "_fax-ipp._tcp", NULL
, browse_callback
, devices
);
282 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
283 "_ipp._tcp", NULL
, browse_callback
, devices
);
285 ipp_tls_ref
= main_ref
;
286 DNSServiceBrowse(&ipp_tls_ref
, kDNSServiceFlagsShareConnection
, 0,
287 "_ipp-tls._tcp", NULL
, browse_callback
, devices
);
290 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
291 "_ipps._tcp", NULL
, browse_callback
, devices
);
293 local_fax_ipp_ref
= main_ref
;
294 DNSServiceBrowse(&local_fax_ipp_ref
, kDNSServiceFlagsShareConnection
,
295 kDNSServiceInterfaceIndexLocalOnly
,
296 "_fax-ipp._tcp", NULL
, browse_local_callback
, devices
);
298 local_ipp_ref
= main_ref
;
299 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
300 kDNSServiceInterfaceIndexLocalOnly
,
301 "_ipp._tcp", NULL
, browse_local_callback
, devices
);
303 local_ipp_tls_ref
= main_ref
;
304 DNSServiceBrowse(&local_ipp_tls_ref
, kDNSServiceFlagsShareConnection
,
305 kDNSServiceInterfaceIndexLocalOnly
,
306 "_ipp-tls._tcp", NULL
, browse_local_callback
, devices
);
308 local_ipps_ref
= main_ref
;
309 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
310 kDNSServiceInterfaceIndexLocalOnly
,
311 "_ipps._tcp", NULL
, browse_local_callback
, devices
);
313 local_printer_ref
= main_ref
;
314 DNSServiceBrowse(&local_printer_ref
, kDNSServiceFlagsShareConnection
,
315 kDNSServiceInterfaceIndexLocalOnly
,
316 "_printer._tcp", NULL
, browse_local_callback
, devices
);
318 pdl_datastream_ref
= main_ref
;
319 DNSServiceBrowse(&pdl_datastream_ref
, kDNSServiceFlagsShareConnection
, 0,
320 "_pdl-datastream._tcp", NULL
, browse_callback
, devices
);
322 printer_ref
= main_ref
;
323 DNSServiceBrowse(&printer_ref
, kDNSServiceFlagsShareConnection
, 0,
324 "_printer._tcp", NULL
, browse_callback
, devices
);
326 riousbprint_ref
= main_ref
;
327 DNSServiceBrowse(&riousbprint_ref
, kDNSServiceFlagsShareConnection
, 0,
328 "_riousbprint._tcp", NULL
, browse_callback
, devices
);
329 #endif /* HAVE_DNSSD */
332 if ((simple_poll
= avahi_simple_poll_new()) == NULL
)
334 fputs("DEBUG: Unable to create Avahi simple poll object.\n", stderr
);
338 avahi_simple_poll_set_func(simple_poll
, poll_callback
, NULL
);
340 client
= avahi_client_new(avahi_simple_poll_get(simple_poll
),
341 0, client_callback
, simple_poll
, &error
);
344 fputs("DEBUG: Unable to create Avahi client.\n", stderr
);
348 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
350 "_fax-ipp._tcp", NULL
, 0,
351 browse_callback
, devices
);
352 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
354 "_ipp._tcp", NULL
, 0,
355 browse_callback
, devices
);
356 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
358 "_ipp-tls._tcp", NULL
, 0,
359 browse_callback
, devices
);
360 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
362 "_ipps._tcp", NULL
, 0,
363 browse_callback
, devices
);
364 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
366 "_pdl-datastream._tcp",
370 avahi_service_browser_new(client
, AVAHI_IF_UNSPEC
,
372 "_printer._tcp", NULL
, 0,
373 browse_callback
, devices
);
374 #endif /* HAVE_AVAHI */
377 * Loop until we are killed...
380 while (!job_canceled
)
382 int announce
= 0; /* Announce printers? */
389 timeout
.tv_usec
= 500000;
391 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
394 if (FD_ISSET(fd
, &input
))
397 * Process results of our browsing...
400 DNSServiceProcessResult(main_ref
);
405 #elif defined(HAVE_AVAHI)
408 if ((error
= avahi_simple_poll_iterate(simple_poll
, 500)) > 0)
411 * We've been told to exit the loop. Perhaps the connection to
420 #endif /* HAVE_DNSSD */
422 /* fprintf(stderr, "DEBUG: announce=%d\n", announce);*/
427 * Announce any devices we've found...
431 DNSServiceErrorType status
; /* DNS query status */
432 #endif /* HAVE_DNSSD */
433 cups_device_t
*best
; /* Best matching device */
434 char device_uri
[1024]; /* Device URI */
435 int count
; /* Number of queries */
436 int sent
; /* Number of sent */
438 for (device
= (cups_device_t
*)cupsArrayFirst(devices
),
439 best
= NULL
, count
= 0, sent
= 0;
441 device
= (cups_device_t
*)cupsArrayNext(devices
))
449 if (!device
->ref
&& !device
->sent
)
452 * Found the device, now get the TXT record(s) for it...
457 fprintf(stderr
, "DEBUG: Querying \"%s\"...\n", device
->fullName
);
460 device
->ref
= main_ref
;
462 status
= DNSServiceQueryRecord(&(device
->ref
),
463 kDNSServiceFlagsShareConnection
,
466 kDNSServiceClass_IN
, query_callback
,
468 if (status
!= kDNSServiceErr_NoError
)
470 "ERROR: Unable to query \"%s\" for TXT records: %d\n",
471 device
->fullName
, status
);
472 /* Users never see this */
477 if ((device
->ref
= avahi_record_browser_new(client
, AVAHI_IF_UNSPEC
,
486 "ERROR: Unable to query \"%s\" for TXT records: %s\n",
488 avahi_strerror(avahi_client_errno(client
)));
489 /* Users never see this */
492 #endif /* HAVE_AVAHI */
495 else if (!device
->sent
)
499 * Got the TXT records, now report the device...
502 DNSServiceRefDeallocate(device
->ref
);
504 avahi_record_browser_free(device
->ref
);
505 #endif /* HAVE_DNSSD */
511 else if (_cups_strcasecmp(best
->name
, device
->name
) ||
512 _cups_strcasecmp(best
->domain
, device
->domain
))
514 unquote(uriName
, best
->fullName
, sizeof(uriName
));
516 httpAssembleURI(HTTP_URI_CODING_ALL
, device_uri
, sizeof(device_uri
),
517 "dnssd", NULL
, uriName
, 0,
518 best
->cups_shared
? "/cups" : "/");
520 cupsBackendReport("network", device_uri
, best
->make_and_model
,
521 best
->name
, best
->device_id
, NULL
);
527 else if (best
->priority
> device
->priority
||
528 (best
->priority
== device
->priority
&&
529 best
->type
< device
->type
))
547 unquote(uriName
, best
->fullName
, sizeof(uriName
));
549 httpAssembleURI(HTTP_URI_CODING_ALL
, device_uri
, sizeof(device_uri
),
550 "dnssd", NULL
, uriName
, 0,
551 best
->cups_shared
? "/cups" : "/");
553 cupsBackendReport("network", device_uri
, best
->make_and_model
,
554 best
->name
, best
->device_id
, NULL
);
559 fprintf(stderr
, "DEBUG: sent=%d, count=%d\n", sent
, count
);
561 if (sent
== cupsArrayCount(devices
))
566 return (CUPS_BACKEND_OK
);
572 * 'browse_callback()' - Browse devices.
577 DNSServiceRef sdRef
, /* I - Service reference */
578 DNSServiceFlags flags
, /* I - Option flags */
579 uint32_t interfaceIndex
, /* I - Interface number */
580 DNSServiceErrorType errorCode
, /* I - Error, if any */
581 const char *serviceName
, /* I - Name of service/device */
582 const char *regtype
, /* I - Type of service */
583 const char *replyDomain
, /* I - Service domain */
584 void *context
) /* I - Devices array */
586 fprintf(stderr
, "DEBUG2: browse_callback(sdRef=%p, flags=%x, "
587 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
588 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
589 sdRef
, flags
, interfaceIndex
, errorCode
,
590 serviceName
, regtype
, replyDomain
, context
);
593 * Only process "add" data...
596 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
603 get_device((cups_array_t
*)context
, serviceName
, regtype
, replyDomain
);
608 * 'browse_local_callback()' - Browse local devices.
612 browse_local_callback(
613 DNSServiceRef sdRef
, /* I - Service reference */
614 DNSServiceFlags flags
, /* I - Option flags */
615 uint32_t interfaceIndex
, /* I - Interface number */
616 DNSServiceErrorType errorCode
, /* I - Error, if any */
617 const char *serviceName
, /* I - Name of service/device */
618 const char *regtype
, /* I - Type of service */
619 const char *replyDomain
, /* I - Service domain */
620 void *context
) /* I - Devices array */
622 cups_device_t
*device
; /* Device */
625 fprintf(stderr
, "DEBUG2: browse_local_callback(sdRef=%p, flags=%x, "
626 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
627 "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n",
628 sdRef
, flags
, interfaceIndex
, errorCode
,
629 serviceName
, regtype
, replyDomain
, context
);
632 * Only process "add" data...
635 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
642 device
= get_device((cups_array_t
*)context
, serviceName
, regtype
,
646 * Hide locally-registered devices...
649 fprintf(stderr
, "DEBUG: Hiding local printer \"%s\"...\n",
653 #endif /* HAVE_DNSSD */
658 * 'browse_callback()' - Browse devices.
663 AvahiServiceBrowser
*browser
, /* I - Browser */
664 AvahiIfIndex interface
, /* I - Interface index (unused) */
665 AvahiProtocol protocol
, /* I - Network protocol (unused) */
666 AvahiBrowserEvent event
, /* I - What happened */
667 const char *name
, /* I - Service name */
668 const char *type
, /* I - Registration type */
669 const char *domain
, /* I - Domain */
670 AvahiLookupResultFlags flags
, /* I - Flags */
671 void *context
) /* I - Devices array */
673 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
674 /* Client information */
683 case AVAHI_BROWSER_FAILURE
:
684 fprintf(stderr
, "DEBUG: browse_callback: %s\n",
685 avahi_strerror(avahi_client_errno(client
)));
686 avahi_simple_poll_quit(simple_poll
);
689 case AVAHI_BROWSER_NEW
:
691 * This object is new on the network.
694 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
697 * This comes from the local machine so ignore it.
700 fprintf(stderr
, "DEBUG: Ignoring local service %s.\n", name
);
705 * Create a device entry for it if it doesn't yet exist.
708 get_device((cups_array_t
*)context
, name
, type
, domain
);
712 case AVAHI_BROWSER_REMOVE
:
713 case AVAHI_BROWSER_ALL_FOR_NOW
:
714 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
721 * 'client_callback()' - Avahi client callback function.
726 AvahiClient
*client
, /* I - Client information (unused) */
727 AvahiClientState state
, /* I - Current state */
728 void *context
) /* I - User data (unused) */
734 * If the connection drops, quit.
737 if (state
== AVAHI_CLIENT_FAILURE
)
739 fputs("DEBUG: Avahi connection failed.\n", stderr
);
740 avahi_simple_poll_quit(simple_poll
);
743 #endif /* HAVE_AVAHI */
747 * 'compare_devices()' - Compare two devices.
750 static int /* O - Result of comparison */
751 compare_devices(cups_device_t
*a
, /* I - First device */
752 cups_device_t
*b
) /* I - Second device */
754 return (strcmp(a
->name
, b
->name
));
759 * 'exec_backend()' - Execute the backend that corresponds to the
760 * resolved service name.
764 exec_backend(char **argv
) /* I - Command-line arguments */
766 const char *resolved_uri
, /* Resolved device URI */
767 *cups_serverbin
; /* Location of programs */
768 char scheme
[1024], /* Scheme from URI */
769 *ptr
, /* Pointer into scheme */
770 filename
[1024]; /* Backend filename */
774 * Resolve the device URI...
779 while ((resolved_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
781 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
784 if (getenv("CLASS") != NULL
)
785 exit(CUPS_BACKEND_FAILED
);
789 * Extract the scheme from the URI...
792 strlcpy(scheme
, resolved_uri
, sizeof(scheme
));
793 if ((ptr
= strchr(scheme
, ':')) != NULL
)
797 * Get the filename of the backend...
800 if ((cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
801 cups_serverbin
= CUPS_SERVERBIN
;
803 snprintf(filename
, sizeof(filename
), "%s/backend/%s", cups_serverbin
, scheme
);
806 * Overwrite the device URI and run the new backend...
809 setenv("DEVICE_URI", resolved_uri
, 1);
811 argv
[0] = (char *)resolved_uri
;
813 fprintf(stderr
, "DEBUG: Executing backend \"%s\"...\n", filename
);
815 execv(filename
, argv
);
817 fprintf(stderr
, "ERROR: Unable to execute backend \"%s\": %s\n", filename
,
819 exit(CUPS_BACKEND_STOP
);
824 * 'device_type()' - Get DNS-SD type enumeration from string.
828 device_type(const char *regtype
)
831 if (!strcmp(regtype
, "_ipp._tcp"))
832 return (CUPS_DEVICE_IPP
);
833 else if (!strcmp(regtype
, "_ipps._tcp") ||
834 !strcmp(regtype
, "_ipp-tls._tcp"))
835 return (CUPS_DEVICE_IPPS
);
836 else if (!strcmp(regtype
, "_fax-ipp._tcp"))
837 return (CUPS_DEVICE_FAX_IPP
);
838 else if (!strcmp(regtype
, "_printer._tcp"))
839 return (CUPS_DEVICE_PDL_DATASTREAM
);
841 if (!strcmp(regtype
, "_ipp._tcp."))
842 return (CUPS_DEVICE_IPP
);
843 else if (!strcmp(regtype
, "_ipps._tcp.") ||
844 !strcmp(regtype
, "_ipp-tls._tcp."))
845 return (CUPS_DEVICE_IPPS
);
846 else if (!strcmp(regtype
, "_fax-ipp._tcp."))
847 return (CUPS_DEVICE_FAX_IPP
);
848 else if (!strcmp(regtype
, "_printer._tcp."))
849 return (CUPS_DEVICE_PRINTER
);
850 else if (!strcmp(regtype
, "_pdl-datastream._tcp."))
851 return (CUPS_DEVICE_PDL_DATASTREAM
);
852 #endif /* HAVE_AVAHI */
854 return (CUPS_DEVICE_RIOUSBPRINT
);
859 * 'get_device()' - Create or update a device.
862 static cups_device_t
* /* O - Device */
863 get_device(cups_array_t
*devices
, /* I - Device array */
864 const char *serviceName
, /* I - Name of service/device */
865 const char *regtype
, /* I - Type of service */
866 const char *replyDomain
) /* I - Service domain */
868 cups_device_t key
, /* Search key */
869 *device
; /* Device */
870 char fullName
[kDNSServiceMaxDomainName
];
871 /* Full name for query */
875 * See if this is a new device...
878 key
.name
= (char *)serviceName
;
879 key
.type
= device_type(regtype
);
881 for (device
= cupsArrayFind(devices
, &key
);
883 device
= cupsArrayNext(devices
))
884 if (_cups_strcasecmp(device
->name
, key
.name
))
886 else if (device
->type
== key
.type
)
888 if (!_cups_strcasecmp(device
->domain
, "local.") &&
889 _cups_strcasecmp(device
->domain
, replyDomain
))
892 * Update the .local listing to use the "global" domain name instead.
893 * The backend will try local lookups first, then the global domain name.
896 free(device
->domain
);
897 device
->domain
= strdup(replyDomain
);
900 DNSServiceConstructFullName(fullName
, device
->name
, regtype
,
902 #else /* HAVE_AVAHI */
903 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
,
904 serviceName
, regtype
, replyDomain
);
905 #endif /* HAVE_DNSSD */
907 free(device
->fullName
);
908 device
->fullName
= strdup(fullName
);
915 * Yes, add the device...
918 fprintf(stderr
, "DEBUG: Found \"%s.%s%s\"...\n", serviceName
, regtype
,
921 device
= calloc(sizeof(cups_device_t
), 1);
922 device
->name
= strdup(serviceName
);
923 device
->domain
= strdup(replyDomain
);
924 device
->type
= key
.type
;
925 device
->priority
= 50;
927 cupsArrayAdd(devices
, device
);
930 * Set the "full name" of this service, which is used for queries...
934 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
935 #else /* HAVE_AVAHI */
936 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
,
937 serviceName
, regtype
, replyDomain
);
938 #endif /* HAVE_DNSSD */
940 device
->fullName
= strdup(fullName
);
948 * 'poll_callback()' - Wait for input on the specified file descriptors.
950 * Note: This function is needed because avahi_simple_poll_iterate is broken
951 * and always uses a timeout of 0 (!) milliseconds.
952 * (Avahi Ticket #364)
955 static int /* O - Number of file descriptors matching */
957 struct pollfd
*pollfds
, /* I - File descriptors */
958 unsigned int num_pollfds
, /* I - Number of file descriptors */
959 int timeout
, /* I - Timeout in milliseconds (unused) */
960 void *context
) /* I - User data (unused) */
962 int val
; /* Return value */
968 val
= poll(pollfds
, num_pollfds
, 500);
971 fprintf(stderr
, "DEBUG: poll_callback: %s\n", strerror(errno
));
977 #endif /* HAVE_AVAHI */
980 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
983 * 'query_callback()' - Process query data.
988 DNSServiceRef sdRef
, /* I - Service reference */
989 DNSServiceFlags flags
, /* I - Data flags */
990 uint32_t interfaceIndex
, /* I - Interface */
991 DNSServiceErrorType errorCode
, /* I - Error, if any */
992 const char *fullName
, /* I - Full service name */
993 uint16_t rrtype
, /* I - Record type */
994 uint16_t rrclass
, /* I - Record class */
995 uint16_t rdlen
, /* I - Length of record data */
996 const void *rdata
, /* I - Record data */
997 uint32_t ttl
, /* I - Time-to-live */
998 void *context
) /* I - Device */
1002 * 'query_callback()' - Process query data.
1007 AvahiRecordBrowser
*browser
, /* I - Record browser */
1008 AvahiIfIndex interfaceIndex
,
1009 /* I - Interface index (unused) */
1010 AvahiProtocol protocol
, /* I - Network protocol (unused) */
1011 AvahiBrowserEvent event
, /* I - What happened? */
1012 const char *fullName
, /* I - Service name */
1013 uint16_t rrclass
, /* I - Record class */
1014 uint16_t rrtype
, /* I - Record type */
1015 const void *rdata
, /* I - TXT record */
1016 size_t rdlen
, /* I - Length of TXT record */
1017 AvahiLookupResultFlags flags
, /* I - Flags */
1018 void *context
) /* I - Device */
1020 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
1021 /* Client information */
1022 # endif /* HAVE_DNSSD */
1023 char *ptr
; /* Pointer into string */
1024 cups_device_t
*device
= (cups_device_t
*)context
;
1026 const uint8_t *data
, /* Pointer into data */
1027 *datanext
, /* Next key/value pair */
1028 *dataend
; /* End of entire TXT record */
1029 uint8_t datalen
; /* Length of current key/value pair */
1030 char key
[256], /* Key string */
1031 value
[256], /* Value string */
1032 make_and_model
[512], /* Manufacturer and model */
1033 model
[256], /* Model */
1034 device_id
[2048]; /* 1284 device ID */
1038 fprintf(stderr
, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
1039 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
1040 "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
1042 sdRef
, flags
, interfaceIndex
, errorCode
,
1043 fullName
? fullName
: "(null)", rrtype
, rrclass
, rdlen
, rdata
, ttl
,
1047 * Only process "add" data...
1050 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
1054 fprintf(stderr
, "DEBUG2: query_callback(browser=%p, interfaceIndex=%d, "
1055 "protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, "
1056 "rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)\n",
1057 browser
, interfaceIndex
, protocol
, event
,
1058 fullName
? fullName
: "(null)", rrclass
, rrtype
, rdata
,
1059 (unsigned)rdlen
, flags
, context
);
1062 * Only process "add" data...
1065 if (event
!= AVAHI_BROWSER_NEW
)
1067 if (event
== AVAHI_BROWSER_FAILURE
)
1068 fprintf(stderr
, "ERROR: %s\n",
1069 avahi_strerror(avahi_client_errno(client
)));
1073 # endif /* HAVE_DNSSD */
1076 * Pull out the priority and make and model from the TXT
1077 * record and save it...
1080 device_id
[0] = '\0';
1081 make_and_model
[0] = '\0';
1083 strlcpy(model
, "Unknown", sizeof(model
));
1085 for (data
= rdata
, dataend
= data
+ rdlen
;
1090 * Read a key/value pair starting with an 8-bit length. Since the
1091 * length is 8 bits and the size of the key/value buffers is 256, we
1092 * don't need to check for overflow...
1097 if (!datalen
|| (data
+ datalen
) > dataend
)
1100 datanext
= data
+ datalen
;
1102 for (ptr
= key
; data
< datanext
&& *data
!= '='; data
++)
1106 if (data
< datanext
&& *data
== '=')
1110 if (data
< datanext
)
1111 memcpy(value
, data
, datanext
- data
);
1112 value
[datanext
- data
] = '\0';
1114 fprintf(stderr
, "DEBUG2: query_callback: \"%s=%s\".\n",
1119 fprintf(stderr
, "DEBUG2: query_callback: \"%s\" with no value.\n",
1124 if (!_cups_strncasecmp(key
, "usb_", 4))
1127 * Add USB device ID information...
1130 ptr
= device_id
+ strlen(device_id
);
1131 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%s:%s;",
1135 if (!_cups_strcasecmp(key
, "usb_MFG") || !_cups_strcasecmp(key
, "usb_MANU") ||
1136 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
1137 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
1138 else if (!_cups_strcasecmp(key
, "usb_MDL") || !_cups_strcasecmp(key
, "usb_MODEL"))
1139 strlcpy(model
, value
, sizeof(model
));
1140 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
1142 if (value
[0] == '(')
1145 * Strip parenthesis...
1148 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
1151 strlcpy(model
, value
+ 1, sizeof(model
));
1154 strlcpy(model
, value
, sizeof(model
));
1156 else if (!_cups_strcasecmp(key
, "ty"))
1158 strlcpy(model
, value
, sizeof(model
));
1160 if ((ptr
= strchr(model
, ',')) != NULL
)
1163 else if (!_cups_strcasecmp(key
, "priority"))
1164 device
->priority
= atoi(value
);
1165 else if ((device
->type
== CUPS_DEVICE_IPP
||
1166 device
->type
== CUPS_DEVICE_IPPS
||
1167 device
->type
== CUPS_DEVICE_PRINTER
) &&
1168 !_cups_strcasecmp(key
, "printer-type"))
1171 * This is a CUPS printer!
1174 device
->cups_shared
= 1;
1176 if (device
->type
== CUPS_DEVICE_PRINTER
)
1181 if (device
->device_id
)
1182 free(device
->device_id
);
1184 if (!device_id
[0] && strcmp(model
, "Unknown"))
1186 if (make_and_model
[0])
1187 snprintf(device_id
, sizeof(device_id
), "MFG:%s;MDL:%s;",
1188 make_and_model
, model
);
1189 else if (!_cups_strncasecmp(model
, "designjet ", 10))
1190 snprintf(device_id
, sizeof(device_id
), "MFG:HP;MDL:%s", model
+ 10);
1191 else if (!_cups_strncasecmp(model
, "stylus ", 7))
1192 snprintf(device_id
, sizeof(device_id
), "MFG:EPSON;MDL:%s", model
+ 7);
1193 else if ((ptr
= strchr(model
, ' ')) != NULL
)
1196 * Assume the first word is the make...
1199 memcpy(make_and_model
, model
, ptr
- model
);
1200 make_and_model
[ptr
- model
] = '\0';
1202 snprintf(device_id
, sizeof(device_id
), "MFG:%s;MDL:%s",
1203 make_and_model
, ptr
+ 1);
1208 device
->device_id
= strdup(device_id
);
1210 device
->device_id
= NULL
;
1212 if (device
->make_and_model
)
1213 free(device
->make_and_model
);
1215 if (make_and_model
[0])
1217 strlcat(make_and_model
, " ", sizeof(make_and_model
));
1218 strlcat(make_and_model
, model
, sizeof(make_and_model
));
1220 device
->make_and_model
= strdup(make_and_model
);
1223 device
->make_and_model
= strdup(model
);
1225 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1229 * 'sigterm_handler()' - Handle termination signals.
1233 sigterm_handler(int sig
) /* I - Signal number (unused) */
1238 exit(CUPS_BACKEND_OK
);
1245 * 'unquote()' - Unquote a name string.
1249 unquote(char *dst
, /* I - Destination buffer */
1250 const char *src
, /* I - Source string */
1251 size_t dstsize
) /* I - Size of destination buffer */
1253 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
1256 while (*src
&& dst
< dstend
)
1261 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
1262 isdigit(src
[2] & 255))
1264 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';