1 Description: Avahi support
2 Bug: http://www.cups.org/str.php?L3066
4 Origin: git clone http://twaugh.fedorapeople.org/cups-avahi.git (Aug 11, 2011)
12 + * next_txt_record() - Get next TXT record from a cups_txt_records_t.
13 + * parse_txt_record_pair() - Read key/value pair in cups_txt_records_t.
14 * main() - Browse for printers.
15 * browse_callback() - Browse devices.
16 * browse_local_callback() - Browse local devices.
17 * compare_devices() - Compare two devices.
18 * exec_backend() - Execute the backend that corresponds to the
19 * resolved service name.
20 + * device_type() - Get DNS-SD type enumeration from string.
21 * get_device() - Create or update a device.
22 * query_callback() - Process query data.
23 + * avahi_client_callback() - Avahi client callback function.
24 + * avahi_query_callback() - Avahi query callback function.
25 + * avahi_browse_callback() - Avahi browse callback function.
26 + * find_device() - Find a device from its name and domain.
27 * sigterm_handler() - Handle termination signals...
28 * unquote() - Unquote a name string.
32 #include "backend-private.h"
33 #include <cups/array.h>
37 +#endif /* HAVE_DNSSD */
39 +# include <avahi-client/client.h>
40 +# include <avahi-client/lookup.h>
41 +# include <avahi-common/simple-watch.h>
42 +# include <avahi-common/domain.h>
43 +# include <avahi-common/error.h>
44 +# include <avahi-common/malloc.h>
45 +#define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
46 +#endif /* HAVE_AVAHI */
55 DNSServiceRef ref; /* Service reference for resolve */
56 +#endif /* HAVE_DNSSD */
58 + int resolved; /* Did we resolve the device? */
59 +#endif /* HAVE_AVAHI */
60 char *name, /* Service name */
61 *domain, /* Domain name */
62 *fullName, /* Full name */
64 sent; /* Did we list the device? */
73 + const uint8_t *data;
74 + const uint8_t *datanext;
75 + const uint8_t *dataend;
76 +#else /* HAVE_AVAHI */
77 + AvahiStringList *txt;
78 +#endif /* HAVE_DNSSD */
79 +} cups_txt_records_t;
89 static void browse_callback(DNSServiceRef sdRef,
90 DNSServiceFlags flags,
91 uint32_t interfaceIndex,
94 const char *replyDomain,
96 -static int compare_devices(cups_device_t *a, cups_device_t *b);
97 -static void exec_backend(char **argv);
98 -static cups_device_t *get_device(cups_array_t *devices,
99 - const char *serviceName,
100 - const char *regtype,
101 - const char *replyDomain);
102 static void query_callback(DNSServiceRef sdRef,
103 DNSServiceFlags flags,
104 uint32_t interfaceIndex,
105 @@ -107,9 +139,118 @@
106 uint16_t rrclass, uint16_t rdlen,
107 const void *rdata, uint32_t ttl,
109 +#endif /* HAVE_DNSSD */
111 +static void avahi_client_callback (AvahiClient *client,
112 + AvahiClientState state,
114 +static void avahi_browse_callback (AvahiServiceBrowser *browser,
115 + AvahiIfIndex interface,
116 + AvahiProtocol protocol,
117 + AvahiBrowserEvent event,
118 + const char *serviceName,
119 + const char *regtype,
120 + const char *replyDomain,
121 + AvahiLookupResultFlags flags,
123 +#endif /* HAVE_AVAHI */
125 +static cups_device_t * find_device (cups_array_t *devices,
126 + cups_txt_records_t *txt,
127 + cups_device_t *dkey);
128 +static int compare_devices(cups_device_t *a, cups_device_t *b);
129 +static void exec_backend(char **argv);
130 +static cups_device_t *get_device(cups_array_t *devices,
131 + const char *serviceName,
132 + const char *regtype,
133 + const char *replyDomain);
134 static void sigterm_handler(int sig);
135 static void unquote(char *dst, const char *src, size_t dstsize);
138 +static AvahiSimplePoll *simple_poll = NULL;
139 +static int avahi_got_callback;
140 +#endif /* HAVE_AVAHI */
144 + * 'next_txt_record()' - Get next TXT record from a cups_txt_records_t.
147 +static cups_txt_records_t *
148 +next_txt_record (cups_txt_records_t *txt)
151 + txt->data = txt->datanext;
152 +#else /* HAVE_AVAHI */
153 + txt->txt = avahi_string_list_get_next (txt->txt);
154 + if (txt->txt == NULL)
156 +#endif /* HAVE_DNSSD */
163 + * 'parse_txt_record_pair()' - Read key/value pair in cups_txt_records_t.
167 +parse_txt_record_pair (cups_txt_records_t *txt)
171 + uint8_t *data = txt->data;
175 + * Read a key/value pair starting with an 8-bit length. Since the
176 + * length is 8 bits and the size of the key/value buffers is 256, we
177 + * don't need to check for overflow...
181 + if (!datalen || (data + datalen) >= txt->dataend)
183 + txt->datanext = data + datalen;
185 + for (ptr = txt->key; data < txt->datanext && *data != '='; data ++)
189 + if (data < txt->datanext && *data == '=')
193 + if (data < datanext)
194 + memcpy (txt->value, data, txt->datanext - data);
195 + value[txt->datanext - data] = '\0';
199 +#else /* HAVE_AVAHI */
202 + avahi_string_list_get_pair (txt->txt, &key, &value, &len);
203 + if (len > sizeof (txt->value) - 1)
204 + len = sizeof (txt->value) - 1;
206 + memcpy (txt->value, value, len);
207 + txt->value[len] = '\0';
208 + len = strlen (key);
209 + if (len > sizeof (txt->key) - 1)
210 + len = sizeof (txt->key) - 1;
212 + memcpy (txt->key, key, len);
213 + txt->key[len] = '\0';
215 + avahi_free (value);
216 +#endif /* HAVE_AVAHI */
223 * 'main()' - Browse for printers.
225 char *argv[]) /* I - Command-line arguments */
227 const char *name; /* Backend name */
228 + cups_array_t *devices; /* Device array */
229 + cups_device_t *device; /* Current device */
230 + char uriName[1024]; /* Unquoted fullName for URI */
232 + int fd; /* Main file descriptor */
233 + fd_set input; /* Input set for select() */
234 + struct timeval timeout; /* Timeout for select() */
235 DNSServiceRef main_ref, /* Main service reference */
236 fax_ipp_ref, /* IPP fax service reference */
237 ipp_ref, /* IPP service reference */
238 @@ -133,12 +281,11 @@
239 pdl_datastream_ref, /* AppSocket service reference */
240 printer_ref, /* LPD service reference */
241 riousbprint_ref; /* Remote IO service reference */
242 - int fd; /* Main file descriptor */
243 - fd_set input; /* Input set for select() */
244 - struct timeval timeout; /* Timeout for select() */
245 - cups_array_t *devices; /* Device array */
246 - cups_device_t *device; /* Current device */
247 - char uriName[1024]; /* Unquoted fullName for URI */
248 +#endif /* HAVE_DNSSD */
250 + AvahiClient *client;
252 +#endif /* HAVE_AVAHI */
253 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
254 struct sigaction action; /* Actions for POSIX signals */
255 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
257 * Browse for different kinds of printers...
261 + if ((simple_poll = avahi_simple_poll_new ()) == NULL)
263 + perror ("ERROR: Unable to create avahi simple poll object");
267 + client = avahi_client_new (avahi_simple_poll_get (simple_poll),
268 + 0, avahi_client_callback, NULL, &error);
271 + perror ("DEBUG: Unable to create avahi client");
275 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
276 + AVAHI_PROTO_UNSPEC,
277 + "_fax-ipp._tcp", NULL, 0,
278 + avahi_browse_callback, devices);
279 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
280 + AVAHI_PROTO_UNSPEC,
281 + "_ipp._tcp", NULL, 0,
282 + avahi_browse_callback, devices);
283 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
284 + AVAHI_PROTO_UNSPEC,
285 + "_ipp-tls._tcp", NULL, 0,
286 + avahi_browse_callback, devices);
287 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
288 + AVAHI_PROTO_UNSPEC,
289 + "_pdl-datastream._tcp",
291 + avahi_browse_callback,
293 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
294 + AVAHI_PROTO_UNSPEC,
295 + "_printer._tcp", NULL, 0,
296 + avahi_browse_callback, devices);
297 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
298 + AVAHI_PROTO_UNSPEC,
299 + "_riousbprint._tcp", NULL, 0,
300 + avahi_browse_callback, devices);
301 +#endif /* HAVE_AVAHI */
303 if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError)
305 perror("ERROR: Unable to create service connection");
307 riousbprint_ref = main_ref;
308 DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0,
309 "_riousbprint._tcp", NULL, browse_callback, devices);
310 +#endif /* HAVE_DNSSD */
313 * Loop until we are killed...
316 while (!job_canceled)
324 @@ -284,11 +478,35 @@
330 +#else /* HAVE_AVAHI */
332 + avahi_got_callback = 0;
333 + r = avahi_simple_poll_iterate (simple_poll, 1);
334 + if (r != 0 && r != EINTR)
337 + * We've been told to exit the loop. Perhaps the connection to
344 + if (avahi_got_callback)
346 +#endif /* HAVE_DNSSD */
351 * Announce any devices we've found...
355 DNSServiceErrorType status; /* DNS query status */
356 +#endif /* HAVE_DNSSD */
357 cups_device_t *best; /* Best matching device */
358 char device_uri[1024]; /* Device URI */
359 int count; /* Number of queries */
368 @@ -333,14 +552,23 @@
372 - else if (!device->sent)
374 +#endif /* HAVE_DNSSD */
376 + if (!device->resolved)
379 +#endif /* HAVE_AVAHI */
384 * Got the TXT records, now report the device...
387 DNSServiceRefDeallocate(device->ref);
389 +#endif /* HAVE_DNSSD */
399 * 'browse_callback()' - Browse devices.
405 +#endif /* HAVE_DNSSD */
413 + * 'device_type()' - Get DNS-SD type enumeration from string.
417 +device_type (const char *regtype)
420 + if (!strcmp(regtype, "_ipp._tcp"))
421 + return (CUPS_DEVICE_IPP);
422 + else if (!strcmp(regtype, "_ipps._tcp") ||
423 + !strcmp(regtype, "_ipp-tls._tcp"))
424 + return (CUPS_DEVICE_IPPS);
425 + else if (!strcmp(regtype, "_fax-ipp._tcp"))
426 + return (CUPS_DEVICE_FAX_IPP);
427 + else if (!strcmp(regtype, "_printer._tcp"))
428 + return (CUPS_DEVICE_PDL_DATASTREAM);
430 + if (!strcmp(regtype, "_ipp._tcp."))
431 + return (CUPS_DEVICE_IPP);
432 + else if (!strcmp(regtype, "_ipps._tcp.") ||
433 + !strcmp(regtype, "_ipp-tls._tcp."))
434 + return (CUPS_DEVICE_IPPS);
435 + else if (!strcmp(regtype, "_fax-ipp._tcp."))
436 + return (CUPS_DEVICE_FAX_IPP);
437 + else if (!strcmp(regtype, "_printer._tcp."))
438 + return (CUPS_DEVICE_PRINTER);
439 + else if (!strcmp(regtype, "_pdl-datastream._tcp."))
440 + return (CUPS_DEVICE_PDL_DATASTREAM);
441 +#endif /* HAVE_AVAHI */
443 + return (CUPS_DEVICE_RIOUSBPRINT);
448 * 'get_device()' - Create or update a device.
454 key.name = (char *)serviceName;
456 - if (!strcmp(regtype, "_ipp._tcp."))
457 - key.type = CUPS_DEVICE_IPP;
458 - else if (!strcmp(regtype, "_ipps._tcp.") ||
459 - !strcmp(regtype, "_ipp-tls._tcp."))
460 - key.type = CUPS_DEVICE_IPPS;
461 - else if (!strcmp(regtype, "_fax-ipp._tcp."))
462 - key.type = CUPS_DEVICE_FAX_IPP;
463 - else if (!strcmp(regtype, "_printer._tcp."))
464 - key.type = CUPS_DEVICE_PRINTER;
465 - else if (!strcmp(regtype, "_pdl-datastream._tcp."))
466 - key.type = CUPS_DEVICE_PDL_DATASTREAM;
468 - key.type = CUPS_DEVICE_RIOUSBPRINT;
469 + key.type = device_type (regtype);
471 for (device = cupsArrayFind(devices, &key);
474 free(device->domain);
475 device->domain = strdup(replyDomain);
478 DNSServiceConstructFullName(fullName, device->name, regtype,
480 +#else /* HAVE_AVAHI */
481 + avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
482 + serviceName, regtype, replyDomain);
483 +#endif /* HAVE_DNSSD */
485 free(device->fullName);
486 device->fullName = strdup(fullName);
489 device->domain = strdup(replyDomain);
490 device->type = key.type;
491 device->priority = 50;
493 + device->resolved = 0;
494 +#endif /* HAVE_AVAHI */
496 cupsArrayAdd(devices, device);
498 @@ -650,13 +911,20 @@
499 * Set the "full name" of this service, which is used for queries...
503 DNSServiceConstructFullName(fullName, serviceName, regtype, replyDomain);
504 +#else /* HAVE_AVAHI */
505 + avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
506 + serviceName, regtype, replyDomain);
507 +#endif /* HAVE_DNSSD */
509 device->fullName = strdup(fullName);
517 * 'query_callback()' - Process query data.
520 *ptr; /* Pointer into string */
521 cups_device_t dkey, /* Search key */
522 *device; /* Device */
524 + cups_txt_records_t txt;
526 fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
527 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
528 @@ -714,94 +982,233 @@
529 if ((ptr = strstr(name, "._")) != NULL)
532 - if (strstr(fullName, "_ipp._tcp."))
533 - dkey.type = CUPS_DEVICE_IPP;
534 - else if (strstr(fullName, "_ipps._tcp.") ||
535 - strstr(fullName, "_ipp-tls._tcp."))
536 - dkey.type = CUPS_DEVICE_IPPS;
537 - else if (strstr(fullName, "_fax-ipp._tcp."))
538 - dkey.type = CUPS_DEVICE_FAX_IPP;
539 - else if (strstr(fullName, "_printer._tcp."))
540 - dkey.type = CUPS_DEVICE_PRINTER;
541 - else if (strstr(fullName, "_pdl-datastream._tcp."))
542 - dkey.type = CUPS_DEVICE_PDL_DATASTREAM;
543 + dkey.type = device_type (fullName);
546 + txt.dataend = rdata + rdlen;
547 + device = find_device ((cups_array_t *) context, &txt, &dkey);
549 + fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
551 +#endif /* HAVE_DNSSD */
556 + * 'avahi_client_callback()' - Avahi client callback function.
560 +avahi_client_callback(AvahiClient *client,
561 + AvahiClientState state,
565 + * If the connection drops, quit.
568 + if (state == AVAHI_CLIENT_FAILURE)
570 + fprintf (stderr, "ERROR: Avahi connection failed\n");
571 + avahi_simple_poll_quit (simple_poll);
577 + * 'avahi_query_callback()' - Avahi query callback function.
581 +avahi_query_callback(AvahiServiceResolver *resolver,
582 + AvahiIfIndex interface,
583 + AvahiProtocol protocol,
584 + AvahiResolverEvent event,
587 + const char *domain,
588 + const char *host_name,
589 + const AvahiAddress *address,
591 + AvahiStringList *txt,
592 + AvahiLookupResultFlags flags,
595 + AvahiClient *client;
600 + cups_txt_records_t txtr;
602 + client = avahi_service_resolver_get_client (resolver);
603 + if (event != AVAHI_RESOLVER_FOUND)
605 + if (event == AVAHI_RESOLVER_FAILURE)
607 + fprintf (stderr, "ERROR: %s\n",
608 + avahi_strerror (avahi_client_errno (client)));
611 + avahi_service_resolver_free (resolver);
616 + * Set search key for device.
620 + unquote (uqname, name, sizeof (uqname));
621 + if ((ptr = strstr(name, "._")) != NULL)
624 + key.domain = (char *) domain;
625 + key.type = device_type (type);
628 + * Find the device and the the TXT information.
632 + device = find_device ((cups_array_t *) context, &txtr, &key);
636 + * Let the main loop know to announce the device.
639 + device->resolved = 1;
640 + avahi_got_callback = 1;
643 - dkey.type = CUPS_DEVICE_RIOUSBPRINT;
644 + fprintf (stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", name);
646 + avahi_service_resolver_free (resolver);
651 + * 'avahi_browse_callback()' - Avahi browse callback function.
655 +avahi_browse_callback(AvahiServiceBrowser *browser,
656 + AvahiIfIndex interface,
657 + AvahiProtocol protocol,
658 + AvahiBrowserEvent event,
661 + const char *domain,
662 + AvahiLookupResultFlags flags,
665 + AvahiClient *client = avahi_service_browser_get_client (browser);
669 + case AVAHI_BROWSER_FAILURE:
670 + fprintf (stderr, "ERROR: %s\n",
671 + avahi_strerror (avahi_client_errno (client)));
672 + avahi_simple_poll_quit (simple_poll);
675 + case AVAHI_BROWSER_NEW:
677 + * This object is new on the network.
680 + if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
683 + * This comes from the local machine so ignore it.
686 + fprintf (stderr, "DEBUG: ignoring local service %s\n", name);
691 + * Create a device entry for it if it doesn't yet exist.
694 + get_device ((cups_array_t *)context, name, type, domain);
697 + * Now look for a TXT entry.
700 + if (avahi_service_resolver_new (client, interface, protocol,
701 + name, type, domain,
702 + AVAHI_PROTO_UNSPEC, 0,
703 + avahi_query_callback, context) == NULL)
705 + fprintf (stderr, "ERROR: failed to resolve service %s: %s\n",
706 + name, avahi_strerror (avahi_client_errno (client)));
712 + case AVAHI_BROWSER_REMOVE:
713 + case AVAHI_BROWSER_ALL_FOR_NOW:
714 + case AVAHI_BROWSER_CACHE_EXHAUSTED:
718 +#endif /* HAVE_AVAHI */
721 - for (device = cupsArrayFind(devices, &dkey);
723 + * 'find_device()' - Find a device from its name and domain.
726 +static cups_device_t *
727 +find_device (cups_array_t *devices,
728 + cups_txt_records_t *txt,
729 + cups_device_t *dkey)
731 + cups_device_t *device;
734 + for (device = cupsArrayFind(devices, dkey);
736 device = cupsArrayNext(devices))
738 - if (_cups_strcasecmp(device->name, dkey.name) ||
739 - _cups_strcasecmp(device->domain, dkey.domain))
740 + if (_cups_strcasecmp(device->name, dkey->name) ||
741 + _cups_strcasecmp(device->domain, dkey->domain))
746 - else if (device->type == dkey.type)
747 + else if (device->type == dkey->type)
750 * Found it, pull out the priority and make and model from the TXT
751 * record and save it...
754 - const uint8_t *data, /* Pointer into data */
755 - *datanext, /* Next key/value pair */
756 - *dataend; /* End of entire TXT record */
757 - uint8_t datalen; /* Length of current key/value pair */
758 - char key[256], /* Key string */
759 - value[256], /* Value string */
760 - make_and_model[512],
761 + char make_and_model[512],
762 /* Manufacturer and model */
763 model[256], /* Model */
764 - device_id[2048];/* 1284 device ID */
766 + device_id[2048]; /* 1284 device ID */
769 make_and_model[0] = '\0';
771 strcpy(model, "Unknown");
773 - for (data = rdata, dataend = data + rdlen;
779 - * Read a key/value pair starting with an 8-bit length. Since the
780 - * length is 8 bits and the size of the key/value buffers is 256, we
781 - * don't need to check for overflow...
786 - if (!datalen || (data + datalen) >= dataend)
789 - datanext = data + datalen;
791 - for (ptr = key; data < datanext && *data != '='; data ++)
797 - if (data < datanext && *data == '=')
801 - if (data < datanext)
802 - memcpy(value, data, datanext - data);
803 - value[datanext - data] = '\0';
804 + if (parse_txt_record_pair (txt))
807 - fprintf(stderr, "DEBUG2: query_callback: \"%s=%s\".\n",
812 - fprintf(stderr, "DEBUG2: query_callback: \"%s\" with no value.\n",
817 - if (!_cups_strncasecmp(key, "usb_", 4))
819 + value = txt->value;
820 + if (!strncasecmp(key, "usb_", 4))
823 * Add USB device ID information...
824 @@ -856,6 +1263,10 @@
825 if (device->type == CUPS_DEVICE_PRINTER)
830 + if (next_txt_record (txt) == NULL)
834 if (device->device_id)
835 @@ -905,11 +1316,9 @@
840 - fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
846 * 'sigterm_handler()' - Handle termination signals...
848 --- a/config-scripts/cups-dnssd.m4
849 +++ b/config-scripts/cups-dnssd.m4
854 +AC_ARG_ENABLE(avahi, [ --enable-avahi turn on DNS Service Discovery support, default=no],
855 + [if test x$enable_avahi = xyes; then
856 + AC_MSG_CHECKING(for Avahi)
857 + if $PKGCONFIG --exists avahi-client; then
859 + CFLAGS="$CFLAGS `$PKGCONFIG --cflags avahi-client`"
860 + DNSSDLIBS="`$PKGCONFIG --libs avahi-client`"
861 + DNSSD_BACKEND="dnssd"
862 + AC_DEFINE(HAVE_AVAHI)
869 if test x$enable_dnssd != xno; then
870 AC_CHECK_HEADER(dns_sd.h, [
878 + * Do we have Avahi for DNS Service Discovery?
885 * Do we have <sys/ioctl.h>?
888 --- a/cups/http-support.c
889 +++ b/cups/http-support.c
891 * http_copy_decode() - Copy and decode a URI.
892 * http_copy_encode() - Copy and encode a URI.
893 * http_resolve_cb() - Build a device URI for the given service name.
894 + * avahi_resolve_uri_client_cb()
895 + * - Avahi client callback for resolving URI.
896 + * avahi_resolve_uri_resolver_cb()
897 + * - Avahi resolver callback for resolving URI.
902 # include <sys/select.h>
904 #endif /* HAVE_DNSSD */
906 +# include <avahi-client/client.h>
907 +# include <avahi-client/lookup.h>
908 +# include <avahi-common/simple-watch.h>
909 +#endif /* HAVE_AVAHI */
915 #endif /* HAVE_DNSSD */
918 +static void avahi_resolve_uri_client_cb(AvahiClient *client,
919 + AvahiClientState state,
920 + void *simple_poll);
921 +static void avahi_resolve_uri_resolver_cb(AvahiServiceResolver *resolver,
922 + AvahiIfIndex interface,
923 + AvahiProtocol protocol,
924 + AvahiResolverEvent event,
927 + const char *domain,
928 + const char *host_name,
929 + const AvahiAddress *address,
931 + AvahiStringList *txt,
932 + AvahiLookupResultFlags flags,
934 +#endif /* HAVE_AVAHI */
937 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
938 @@ -1431,6 +1458,9 @@
940 if (strstr(hostname, "._tcp"))
942 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
943 + char *regtype, /* Pointer to type in hostname */
944 + *domain; /* Pointer to domain in hostname */
947 # pragma comment(lib, "dnssd.lib")
948 @@ -1449,6 +1479,17 @@
949 fd_set input_set; /* Input set for select() */
950 struct timeval stimeout; /* Timeout value for select() */
951 #endif /* HAVE_POLL */
952 +#else /* HAVE_AVAHI */
953 + AvahiSimplePoll *simple_poll;
954 + AvahiClient *client;
958 + AvahiSimplePoll *poll;
959 + _http_uribuf_t uribuf;
961 +#endif /* HAVE_DNSSD */
964 if (options & _HTTP_RESOLVE_STDERR)
965 fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
966 @@ -1485,9 +1526,16 @@
971 uribuf.buffer = resolved_uri;
972 uribuf.bufsize = resolved_size;
973 uribuf.options = options;
975 + user_data.uribuf.buffer = resolved_uri;
976 + user_data.uribuf.bufsize = resolved_size;
977 + user_data.uribuf.options = options;
980 resolved_uri[0] = '\0';
982 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
983 @@ -1501,6 +1549,7 @@
988 if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
991 @@ -1608,6 +1657,36 @@
993 DNSServiceRefDeallocate(ref);
995 +#else /* HAVE_AVAHI */
996 + if ((simple_poll = avahi_simple_poll_new ()) != NULL)
998 + if ((client = avahi_client_new (avahi_simple_poll_get (simple_poll),
999 + 0, avahi_resolve_uri_client_cb,
1000 + &simple_poll, &error)) != NULL)
1002 + user_data.poll = simple_poll;
1003 + if (avahi_service_resolver_new (client, AVAHI_IF_UNSPEC,
1004 + AVAHI_PROTO_UNSPEC, hostname,
1005 + regtype, domain, AVAHI_PROTO_UNSPEC, 0,
1006 + avahi_resolve_uri_resolver_cb,
1007 + &user_data) != NULL)
1009 + avahi_simple_poll_loop (simple_poll);
1012 + * Collect the result.
1015 + if (resolved_uri[0])
1016 + uri = resolved_uri;
1019 + avahi_client_free (client);
1022 + avahi_simple_poll_free (simple_poll);
1024 +#endif /* HAVE_DNSSD */
1026 if (options & _HTTP_RESOLVE_STDERR)
1028 @@ -1619,13 +1698,13 @@
1029 fputs("STATE: -connecting-to-device,offline-report\n", stderr);
1033 +#else /* HAVE_DNSSD || HAVE_AVAHI */
1035 * No DNS-SD support...
1039 -#endif /* HAVE_DNSSD */
1040 +#endif /* HAVE_DNSSD || HAVE_AVAHI */
1042 if ((options & _HTTP_RESOLVE_STDERR) && !uri)
1043 _cupsLangPrintFilter(stderr, "ERROR", _("Unable to find printer."));
1044 @@ -1895,6 +1974,116 @@
1045 #endif /* HAVE_DNSSD */
1050 + * 'avahi_resolve_uri_client_cb()' - Avahi client callback for resolving URI.
1054 +avahi_resolve_uri_client_cb (AvahiClient *client,
1055 + AvahiClientState state,
1056 + void *simple_poll)
1058 + DEBUG_printf(("avahi_resolve_uri_client_callback(client=%p, state=%d, "
1059 + "simple_poll=%p)\n", client, state, simple_poll));
1062 + * If the connection drops, quit.
1065 + if (state == AVAHI_CLIENT_FAILURE)
1066 + avahi_simple_poll_quit (simple_poll);
1071 + * 'avahi_resolve_uri_resolver_cb()' - Avahi resolver callback for resolving
1076 +avahi_resolve_uri_resolver_cb (AvahiServiceResolver *resolver,
1077 + AvahiIfIndex interface,
1078 + AvahiProtocol protocol,
1079 + AvahiResolverEvent event,
1082 + const char *domain,
1083 + const char *host_name,
1084 + const AvahiAddress *address,
1086 + AvahiStringList *txt,
1087 + AvahiLookupResultFlags flags,
1090 + const char *scheme; /* URI scheme */
1091 + char rp[256]; /* Remote printer */
1092 + AvahiStringList *pair;
1094 + size_t valueLen = 0;
1095 + char addr[AVAHI_ADDRESS_STR_MAX];
1098 + AvahiSimplePoll *poll;
1099 + _http_uribuf_t uribuf;
1100 + } *poll_uribuf = context;
1102 + DEBUG_printf(("avahi_resolve_uri_resolver_callback(resolver=%p, "
1103 + "interface=%d, protocol=%d, event=%d, name=\"%s\", "
1104 + "type=\"%s\", domain=\"%s\", host_name=\"%s\", address=%p, "
1105 + "port=%d, txt=%p, flags=%d, context=%p)\n",
1106 + resolver, interface, protocol, event, name, type, domain,
1107 + host_name, address, port, txt, flags, context));
1109 + if (event != AVAHI_RESOLVER_FOUND)
1111 + avahi_service_resolver_free (resolver);
1112 + avahi_simple_poll_quit (poll_uribuf->poll);
1117 + * Figure out the scheme from the full name...
1120 + if (strstr(type, "_ipp."))
1122 + else if (strstr(type, "_printer."))
1124 + else if (strstr(type, "_pdl-datastream."))
1125 + scheme = "socket";
1127 + scheme = "riousbprint";
1130 + * Extract the "remote printer key from the TXT record...
1133 + if ((pair = avahi_string_list_find (txt, "rp")) != NULL)
1135 + avahi_string_list_get_pair (pair, NULL, &value, &valueLen);
1137 + memcpy (rp + 1, value, valueLen);
1138 + rp[valueLen + 1] = '\0';
1144 + * Assemble the final device URI...
1147 + avahi_address_snprint (addr, AVAHI_ADDRESS_STR_MAX, address);
1148 + httpAssembleURI(HTTP_URI_CODING_ALL, poll_uribuf->uribuf.buffer,
1149 + poll_uribuf->uribuf.bufsize, scheme, NULL,
1151 + DEBUG_printf(("avahi_resolve_uri_resolver_callback: Resolved URI is \"%s\"\n",
1152 + poll_uribuf->uribuf.buffer));
1153 + avahi_simple_poll_quit (poll_uribuf->poll);
1155 +#endif /* HAVE_AVAHI */
1159 * End of "$Id: http-support.c 9820 2011-06-10 22:06:26Z mike $".
1161 --- a/scheduler/Makefile
1162 +++ b/scheduler/Makefile
1182 +++ b/scheduler/avahi.c
1187 + * Avahi poll implementation for the CUPS scheduler.
1189 + * Copyright (C) 2010 Red Hat, Inc.
1191 + * Tim Waugh <twaugh@redhat.com>
1193 + * Distribution and use rights are outlined in the file "LICENSE.txt"
1194 + * "LICENSE" which should have been included with this file. If this
1195 + * file is missing or damaged, see the license at "http://www.cups.org/".
1199 + * watch_read_cb - Read callback for file descriptor
1200 + * watch_write_cb - Write callback for file descriptor
1201 + * watched_fd_add_select() - Call cupsdAddSelect() as needed
1202 + * watch_new() - Create a new file descriptor watch
1203 + * watch_free() - Free a file descriptor watch
1204 + * watch_update() - Update watched events for a file descriptor
1205 + * watch_get_events() - Get events that happened for a file descriptor
1206 + * timeout_cb() - Run a timed Avahi callback
1207 + * timeout_new() - Set a wakeup time
1208 + * timeout_update() - Update the expiration time for a timeout
1209 + * timeout_free() - Free a timeout
1210 + * compare_watched_fds() - Compare watched file descriptors for array sorting
1211 + * compare_timeouts() - Compare timeouts for array sorting
1212 + * avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
1213 + * avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
1214 + * avahi_cups_poll_get() - Get the abstract poll API structure
1217 +#include <config.h>
1219 +#ifdef HAVE_AVAHI /* Applies to entire file... */
1222 + * Include necessary headers...
1227 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
1228 +# include <malloc.h>
1229 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
1232 +# include <avahi-common/timeval.h>
1233 +#endif /* HAVE_AVAHI */
1238 + AvahiCupsPoll *cups_poll;
1241 + AvahiWatchEvent occurred;
1242 + cups_array_t *watches;
1243 +} cupsd_watched_fd_t;
1247 + cupsd_watched_fd_t *watched_fd;
1249 + AvahiWatchEvent events;
1250 + AvahiWatchCallback callback;
1254 +struct AvahiTimeout
1256 + AvahiCupsPoll *cups_poll;
1257 + AvahiTimeoutCallback callback;
1259 + cupsd_timeout_t *cupsd_timeout;
1263 + * Local functions...
1266 +static AvahiWatch * watch_new(const AvahiPoll *api,
1268 + AvahiWatchEvent events,
1269 + AvahiWatchCallback callback,
1271 +static void watch_free(AvahiWatch *watch);
1272 +static void watch_update(AvahiWatch *watch,
1273 + AvahiWatchEvent events);
1274 +static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
1275 +static int compare_watches(AvahiWatch *p0,
1280 + * 'watch_read_cb' - Read callback for file descriptor
1284 +watch_read_cb (void *userdata)
1286 + AvahiWatch *watch;
1287 + cupsd_watched_fd_t *watched_fd = userdata;
1288 + watched_fd->occurred |= AVAHI_WATCH_IN;
1289 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
1291 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
1292 + if (watch->events & watched_fd->occurred) {
1293 + (watch->callback) (watch, watched_fd->fd,
1294 + AVAHI_WATCH_IN, watch->userdata);
1295 + watched_fd->occurred &= ~AVAHI_WATCH_IN;
1303 + * 'watch_write_cb' - Write callback for file descriptor
1307 +watch_write_cb (void *userdata)
1309 + AvahiWatch *watch;
1310 + cupsd_watched_fd_t *watched_fd = userdata;
1311 + watched_fd->occurred |= AVAHI_WATCH_OUT;
1312 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
1314 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
1315 + if (watch->events & watched_fd->occurred) {
1316 + (watch->callback) (watch, watched_fd->fd,
1317 + AVAHI_WATCH_OUT, watch->userdata);
1318 + watched_fd->occurred &= ~AVAHI_WATCH_OUT;
1326 + * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
1329 +static int /* O - Watches? */
1330 +watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
1332 + AvahiWatch *watch;
1333 + cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
1335 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
1337 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
1338 + if (watch->events & (AVAHI_WATCH_IN |
1340 + AVAHI_WATCH_HUP)) {
1341 + read_cb = (cupsd_selfunc_t)watch_read_cb;
1342 + if (write_cb != NULL)
1346 + if (watch->events & AVAHI_WATCH_OUT) {
1347 + write_cb = (cupsd_selfunc_t)watch_write_cb;
1348 + if (read_cb != NULL)
1353 + if (read_cb || write_cb)
1354 + cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
1356 + cupsdRemoveSelect (watched_fd->fd);
1358 + return (read_cb || write_cb);
1362 + * 'watch_new' - Create a new file descriptor watch
1365 +static AvahiWatch *
1366 +watch_new (const AvahiPoll *api,
1368 + AvahiWatchEvent events,
1369 + AvahiWatchCallback callback,
1372 + cupsd_watched_fd_t key, *watched_fd;
1373 + AvahiCupsPoll *cups_poll = api->userdata;
1374 + AvahiWatch *watch = malloc(sizeof(AvahiWatch));
1375 + if (watch == NULL)
1378 + watch->events = events;
1379 + watch->callback = callback;
1380 + watch->userdata = userdata;
1383 + watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
1384 + if (watched_fd == NULL) {
1385 + watched_fd = malloc(sizeof(cupsd_watched_fd_t));
1386 + if (watched_fd == NULL) {
1391 + watched_fd->fd = fd;
1392 + watched_fd->occurred = 0;
1393 + watched_fd->cups_poll = cups_poll;
1394 + watched_fd->watches = cupsArrayNew ((cups_array_func_t)compare_watches,
1398 + watch->watched_fd = watched_fd;
1399 + cupsArrayAdd(watched_fd->watches, watch);
1400 + watched_fd_add_select (watched_fd);
1406 + * 'watch_free' - Free a file descriptor watch
1410 +watch_free (AvahiWatch *watch)
1412 + cupsd_watched_fd_t *watched_fd = watch->watched_fd;
1413 + AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
1415 + cupsArrayRemove (watched_fd->watches, watch);
1418 + if (!watched_fd_add_select (watched_fd)) {
1419 + /* No more watches */
1420 + cupsArrayRemove (cups_poll->watched_fds, watched_fd);
1421 + free (watched_fd);
1427 + * 'watch_update' - Update watched events for a file descriptor
1431 +watch_update (AvahiWatch *watch,
1432 + AvahiWatchEvent events)
1434 + watch->events = events;
1435 + watched_fd_add_select (watch->watched_fd);
1440 + * 'watch_get_events' - Get events that happened for a file descriptor
1443 +static AvahiWatchEvent
1444 +watch_get_events (AvahiWatch *watch)
1446 + return (watch->watched_fd->occurred);
1451 + * 'compare_watches' - Compare watches for array sorting
1455 +compare_watches (AvahiWatch *p0,
1458 + if (p0->watched_fd->fd < p1->watched_fd->fd)
1461 + return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
1466 + * 'timeout_cb()' - Run a timed Avahi callback
1470 +timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
1472 + AvahiTimeout *timeout = userdata;
1473 + (timeout->callback) (timeout, timeout->userdata);
1478 + * 'timeout_new' - Set a wakeup time
1481 +static AvahiTimeout *
1482 +timeout_new (const AvahiPoll *api,
1483 + const struct timeval *tv,
1484 + AvahiTimeoutCallback callback,
1487 + AvahiTimeout *timeout;
1488 + AvahiCupsPoll *cups_poll = api->userdata;
1490 + timeout = malloc(sizeof(AvahiTimeout));
1491 + if (timeout == NULL)
1494 + timeout->cups_poll = cups_poll;
1495 + timeout->callback = callback;
1496 + timeout->userdata = userdata;
1497 + timeout->cupsd_timeout = cupsdAddTimeout (tv,
1498 + (cupsd_timeoutfunc_t)timeout_cb,
1500 + cupsArrayAdd (cups_poll->timeouts, timeout);
1506 + * 'timeout_update' - Update the expiration time for a timeout
1510 +timeout_update (AvahiTimeout *timeout,
1511 + const struct timeval *tv)
1513 + cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
1518 + * ' timeout_free' - Free a timeout
1522 +timeout_free (AvahiTimeout *timeout)
1524 + cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
1525 + cupsdRemoveTimeout (timeout->cupsd_timeout);
1531 + * 'compare_watched_fds' - Compare watched file descriptors for array sorting
1534 +compare_watched_fds(cupsd_watched_fd_t *p0,
1535 + cupsd_watched_fd_t *p1)
1537 + if (p0->fd != p1->fd)
1538 + return (p0->fd < p1->fd ? -1 : 1);
1543 + return (p0 < p1 ? -1 : 1);
1548 + * 'compare_timeouts' - Compare timeouts for array sorting
1551 +compare_timeouts(AvahiTimeout *p0,
1555 + * Just compare pointers to make it a stable sort.
1558 + if (p0->cupsd_timeout < p1->cupsd_timeout)
1560 + return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
1565 + * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
1569 +avahi_cups_poll_new (void)
1571 + AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
1572 + if (cups_poll == NULL)
1575 + cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
1577 + cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts,
1580 + cups_poll->api.userdata = cups_poll;
1581 + cups_poll->api.watch_new = watch_new;
1582 + cups_poll->api.watch_free = watch_free;
1583 + cups_poll->api.watch_update = watch_update;
1584 + cups_poll->api.watch_get_events = watch_get_events;
1586 + cups_poll->api.timeout_new = timeout_new;
1587 + cups_poll->api.timeout_update = timeout_update;
1588 + cups_poll->api.timeout_free = timeout_free;
1590 + return (cups_poll);
1595 + * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
1598 +avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
1600 + cupsd_watched_fd_t *watched_fd;
1602 + for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
1604 + watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds)){
1605 + cupsArrayClear (watched_fd->watches);
1608 + cupsArrayClear (cups_poll->watched_fds);
1609 + cupsArrayClear (cups_poll->timeouts);
1614 + * 'avahi_cups_poll_get' - Get the abstract poll API structure
1618 +avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
1620 + return (&cups_poll->api);
1624 +#endif /* HAVE_AVAHI ... from top of file */
1630 +++ b/scheduler/avahi.h
1635 + * Avahi poll implementation for the CUPS scheduler.
1637 + * Copyright (C) 2010 Red Hat, Inc.
1639 + * Tim Waugh <twaugh@redhat.com>
1641 + * Distribution and use rights are outlined in the file "LICENSE.txt"
1642 + * which should have been included with this file. If this file is
1643 + * file is missing or damaged, see the license at "http://www.cups.org/".
1646 +#include <config.h>
1649 +# include <avahi-client/client.h>
1650 +# include <avahi-client/publish.h>
1651 +#endif /* HAVE_AVAHI */
1653 +#ifdef HAVE_AUTHORIZATION_H
1654 +# include <Security/Authorization.h>
1655 +#endif /* HAVE_AUTHORIZATION_H */
1662 + cups_array_t *watched_fds;
1663 + cups_array_t *timeouts;
1665 +#endif /* HAVE_AVAHI */
1672 +extern AvahiCupsPoll * avahi_cups_poll_new(void);
1673 +extern void avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
1674 +extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
1675 +#endif /* HAVE_AVAHI */
1681 --- a/scheduler/cupsd.h
1682 +++ b/scheduler/cupsd.h
1683 @@ -140,6 +140,15 @@
1685 typedef void (*cupsd_selfunc_t)(void *data);
1689 + * Timeout callback function type...
1692 +typedef struct _cupsd_timeout_s cupsd_timeout_t;
1693 +typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
1694 +#endif /* HAVE_AVAHI */
1699 @@ -173,6 +182,11 @@
1700 /* Running from launchd */
1701 #endif /* HAVE_LAUNCH_H */
1704 +VAR cups_array_t *Timeouts; /* Timed callbacks for main loop */
1705 +#endif /* HAVE_AVAHI */
1711 @@ -242,6 +256,20 @@
1712 extern void cupsdStartServer(void);
1713 extern void cupsdStopServer(void);
1716 +extern void cupsdInitTimeouts(void);
1717 +extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
1718 + cupsd_timeoutfunc_t cb,
1720 +extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
1721 +extern void cupsdRunTimeout (cupsd_timeout_t *timeout);
1722 +extern void cupsdUpdateTimeout (cupsd_timeout_t *timeout,
1723 + const struct timeval *tv);
1724 +extern void cupsdRemoveTimeout (cupsd_timeout_t *timeout);
1725 +#endif /* HAVE_AVAHI */
1727 +extern int cupsdRemoveFile(const char *filename);
1731 * End of "$Id: cupsd.h 9766 2011-05-11 22:17:34Z mike $".
1732 --- a/scheduler/main.c
1733 +++ b/scheduler/main.c
1734 @@ -122,6 +122,10 @@
1735 cupsd_listener_t *lis; /* Current listener */
1736 time_t current_time, /* Current time */
1737 activity, /* Client activity timer */
1739 + avahi_client_time, /* Time for next Avahi client
1741 +#endif /* HAVE_AVAHI */
1742 browse_time, /* Next browse send time */
1743 senddoc_time, /* Send-Document time */
1744 expire_time, /* Subscription expire time */
1745 @@ -148,6 +152,10 @@
1746 int launchd_idle_exit;
1747 /* Idle exit on select timeout? */
1748 #endif /* HAVE_LAUNCHD */
1750 + cupsd_timeout_t *tmo; /* Next scheduled timed callback */
1751 + long tmo_delay; /* Time before it must be called */
1752 +#endif /* HAVE_AVAHI */
1756 @@ -527,6 +535,14 @@
1762 + * Initialize timed callback structures.
1765 + cupsdInitTimeouts();
1766 +#endif /* HAVE_AVAHI */
1774 current_time = time(NULL);
1776 + avahi_client_time = current_time;
1777 +#endif /* HAVE_AVAHI */
1778 browse_time = current_time;
1779 event_time = current_time;
1780 expire_time = current_time;
1781 @@ -871,6 +890,26 @@
1783 #endif /* __APPLE__ */
1787 + * If a timed callback is due, run it.
1790 + tmo = cupsdNextTimeout (&tmo_delay);
1791 + if (tmo && tmo_delay == 0)
1792 + cupsdRunTimeout (tmo);
1795 + * Try to restart the Avahi client every 10 seconds if needed...
1798 + if ((current_time - avahi_client_time) >= 10)
1800 + avahi_client_time = current_time;
1801 + cupsdStartAvahiClient();
1803 +#endif /* HAVE_AVAHI */
1807 * Update the network interfaces once a minute...
1808 @@ -1815,6 +1854,10 @@
1809 cupsd_job_t *job; /* Job information */
1810 cupsd_subscription_t *sub; /* Subscription information */
1811 const char *why; /* Debugging aid */
1813 + cupsd_timeout_t *tmo; /* Timed callback */
1814 + long tmo_delay; /* Seconds before calling it */
1815 +#endif /* HAVE_AVAHI */
1819 @@ -1857,6 +1900,19 @@
1821 #endif /* __APPLE__ */
1825 + * See if there are any scheduled timed callbacks to run.
1828 + tmo = cupsdNextTimeout (&tmo_delay);
1831 + timeout = tmo_delay;
1832 + why = "run a timed callback";
1834 +#endif /* HAVE_AVAHI */
1837 * Check whether we are accepting new connections...
1840 +++ b/scheduler/timeout.c
1845 + * Timeout functions for the Common UNIX Printing System (CUPS).
1847 + * Copyright (C) 2010 Red Hat, Inc.
1849 + * Tim Waugh <twaugh@redhat.com>
1851 + * Distribution and use rights are outlined in the file "LICENSE.txt"
1852 + * which should have been included with this file. If this file is
1853 + * file is missing or damaged, see the license at "http://www.cups.org/".
1857 + * cupsdInitTimeouts() - Initialise timeout structure.
1858 + * cupsdAddTimeout() - Add a timed callback.
1859 + * cupsdNextTimeout() - Find the next enabled timed callback.
1860 + * cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
1861 + * cupsdRemoveTimeout() - Discard a timed callback.
1862 + * compare_timeouts() - Compare timed callbacks for array sorting.
1865 +#include <config.h>
1867 +#ifdef HAVE_AVAHI /* Applies to entire file... */
1870 + * Include necessary headers...
1875 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
1876 +# include <malloc.h>
1877 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
1880 +# include <avahi-common/timeval.h>
1881 +#endif /* HAVE_AVAHI */
1884 +struct _cupsd_timeout_s
1886 + struct timeval when;
1888 + cupsd_timeoutfunc_t callback;
1893 + * Local functions...
1897 + * 'compare_timeouts()' - Compare timed callbacks for array sorting.
1901 +compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
1903 + if (!p0->enabled || !p1->enabled)
1905 + if (!p0->enabled && !p1->enabled)
1908 + return (p0->enabled ? -1 : 1);
1911 + return (avahi_timeval_compare (&p0->when, &p1->when));
1916 + * 'cupsdInitTimeouts()' - Initialise timeout structures.
1920 +cupsdInitTimeouts(void)
1922 + Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
1927 + * 'cupsdAddTimeout()' - Add a timed callback.
1930 +cupsd_timeout_t * /* O - Timeout handle */
1931 +cupsdAddTimeout(const struct timeval *tv, /* I - Absolute time */
1932 + cupsd_timeoutfunc_t cb, /* I - Callback function */
1933 + void *data) /* I - User data */
1935 + cupsd_timeout_t *timeout;
1937 + timeout = malloc (sizeof(cupsd_timeout_t));
1938 + if (timeout != NULL)
1940 + timeout->enabled = (tv != NULL);
1943 + timeout->when.tv_sec = tv->tv_sec;
1944 + timeout->when.tv_usec = tv->tv_usec;
1947 + timeout->callback = cb;
1948 + timeout->data = data;
1949 + cupsArrayAdd (Timeouts, timeout);
1957 + * 'cupsdNextTimeout()' - Find the next enabled timed callback.
1960 +cupsd_timeout_t * /* O - Next enabled timeout or NULL */
1961 +cupsdNextTimeout(long *delay) /* O - Seconds before scheduled */
1963 + cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
1964 + struct timeval curtime;
1966 + if (first && !first->enabled)
1969 + if (first && delay)
1971 + gettimeofday (&curtime, NULL);
1972 + if (avahi_timeval_compare (&curtime, &first->when) > 0)
1976 + *delay = 1 + first->when.tv_sec - curtime.tv_sec;
1977 + if (first->when.tv_usec < curtime.tv_usec)
1987 + * 'cupsdRunTimeout()' - Run a timed callback.
1991 +cupsdRunTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
1995 + timeout->enabled = 0;
1996 + if (!timeout->callback)
1998 + timeout->callback (timeout, timeout->data);
2002 + * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
2006 +cupsdUpdateTimeout(cupsd_timeout_t *timeout, /* I - Timeout */
2007 + const struct timeval *tv) /* I - Absolute time or NULL */
2009 + cupsArrayRemove (Timeouts, timeout);
2010 + timeout->enabled = (tv != NULL);
2013 + timeout->when.tv_sec = tv->tv_sec;
2014 + timeout->when.tv_usec = tv->tv_usec;
2016 + cupsArrayAdd (Timeouts, timeout);
2021 + * 'cupsdRemoveTimeout()' - Discard a timed callback.
2025 +cupsdRemoveTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
2027 + cupsArrayRemove (Timeouts, timeout);
2032 +#endif /* HAVE_AVAHI ... from top of file */
2037 --- a/cgi-bin/admin.c
2038 +++ b/cgi-bin/admin.c
2039 @@ -1643,7 +1643,7 @@
2041 local_protocols[0] = '\0';
2044 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2045 if (cgiGetVariable("BROWSE_LOCAL_DNSSD"))
2047 if (local_protocols[0])
2048 @@ -1651,7 +1651,7 @@
2050 strcat(local_protocols, "dnssd");
2052 -#endif /* HAVE_DNSSD */
2053 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2056 if (cgiGetVariable("BROWSE_LOCAL_LDAP"))
2057 @@ -2718,9 +2718,9 @@
2058 #endif /* HAVE_GSSAPI */
2059 cgiSetVariable("KERBEROS", "");
2062 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2063 cgiSetVariable("HAVE_DNSSD", "1");
2064 -#endif /* HAVE_DNSSD */
2065 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2068 cgiSetVariable("HAVE_LDAP", "1");
2069 --- a/scheduler/client.c
2070 +++ b/scheduler/client.c
2071 @@ -4987,7 +4987,7 @@
2072 !strncmp(host, "[::1]:", 6));
2076 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2078 * Check if the hostname is something.local (Bonjour); if so, allow it.
2080 @@ -4996,7 +4996,7 @@
2081 (!_cups_strcasecmp(end, ".local") || !_cups_strncasecmp(end, ".local:", 7) ||
2082 !_cups_strcasecmp(end, ".local.") || !_cups_strncasecmp(end, ".local.:", 8)))
2084 -#endif /* HAVE_DNSSD */
2085 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2088 * Check if the hostname is an IP address...
2089 --- a/scheduler/dirsvc.c
2090 +++ b/scheduler/dirsvc.c
2092 * ldap_connect() - Start new LDAP connection
2093 * ldap_reconnect() - Reconnect to LDAP Server
2094 * ldap_disconnect() - Disconnect from LDAP Server
2095 + * cupsdStartAvahiClient() - Start an Avahi client if needed
2096 * cupsdStartBrowsing() - Start sending and receiving broadcast
2098 * cupsdStartPolling() - Start polling servers as needed.
2101 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
2103 + * avahiPackTxtRecord() - Pack an array of key/value pairs into an
2104 + * AvahiStringList.
2105 * dnssdRegisterCallback() - DNSServiceRegister callback.
2106 * dnssdRegisterPrinter() - Start sending broadcast information for a
2107 * printer or update the broadcast contents.
2112 +#include <assert.h>
2117 # endif /* HAVE_SYSTEMCONFIGURATION */
2118 # endif /* __APPLE__ */
2119 #endif /* HAVE_DNSSD */
2121 +# include <avahi-common/domain.h>
2122 +#endif /* HAVE_AVAHI */
2126 +typedef char *cupsd_txt_record_t;
2127 +#endif /* HAVE_DNSSD */
2129 +typedef AvahiStringList *cupsd_txt_record_t;
2130 +#endif /* HAVE_AVAHI */
2134 @@ -159,27 +174,39 @@
2135 static void update_smb(int onoff);
2138 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2139 +static cupsd_txt_record_t dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
2141 +static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
2142 +static void dnssdDeregisterPrinter(cupsd_printer_t *p);
2143 +static void dnssdRegisterPrinter(cupsd_printer_t *p);
2144 +static void dnssdStop(void);
2145 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2148 # ifdef HAVE_COREFOUNDATION
2149 static void dnssdAddAlias(const void *key, const void *value,
2151 # endif /* HAVE_COREFOUNDATION */
2152 -static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
2154 -static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
2155 -static void dnssdDeregisterPrinter(cupsd_printer_t *p);
2156 -static char *dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2],
2158 static void dnssdRegisterCallback(DNSServiceRef sdRef,
2159 DNSServiceFlags flags,
2160 DNSServiceErrorType errorCode,
2161 const char *name, const char *regtype,
2162 const char *domain, void *context);
2163 -static void dnssdRegisterPrinter(cupsd_printer_t *p);
2164 -static void dnssdStop(void);
2165 static void dnssdUpdate(void);
2166 #endif /* HAVE_DNSSD */
2169 +static AvahiStringList *avahiPackTxtRecord(char *keyvalue[][2],
2171 +static void avahi_entry_group_cb (AvahiEntryGroup *group,
2172 + AvahiEntryGroupState state,
2174 +static void avahi_client_cb (AvahiClient *client,
2175 + AvahiClientState state,
2177 +#endif /* HAVE_AVAHI */
2180 static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
2182 @@ -283,10 +310,10 @@
2183 ldap_dereg_printer(p);
2184 #endif /* HAVE_LDAP */
2187 - if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
2188 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2189 + if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD))
2190 dnssdDeregisterPrinter(p);
2191 -#endif /* HAVE_DNSSD */
2192 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2196 @@ -702,10 +729,10 @@
2197 slpRegisterPrinter(p); */
2198 #endif /* HAVE_LIBSLP */
2201 - if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
2202 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2203 + if ((BrowseLocalProtocols & BROWSE_DNSSD))
2204 dnssdRegisterPrinter(p);
2205 -#endif /* HAVE_DNSSD */
2206 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2210 @@ -1419,6 +1446,27 @@
2211 #endif /* HAVE_LDAP */
2216 + * 'cupsdStartAvahiClient()' - Start an Avahi client if needed
2220 +cupsdStartAvahiClient(void)
2222 + if (!AvahiCupsClient && !AvahiCupsClientConnecting)
2224 + if (!AvahiCupsPollHandle)
2225 + AvahiCupsPollHandle = avahi_cups_poll_new ();
2227 + if (AvahiCupsPollHandle)
2228 + avahi_client_new (avahi_cups_poll_get (AvahiCupsPollHandle),
2229 + AVAHI_CLIENT_NO_FAIL, avahi_client_cb, NULL, NULL);
2232 +#endif /* HAVE_AVAHI */
2236 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
2238 @@ -1542,13 +1590,16 @@
2243 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2244 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
2247 DNSServiceErrorType error; /* Error from service creation */
2248 +#endif /* HAVE_DNSSD */
2249 cupsd_listener_t *lis; /* Current listening socket */
2254 * First create a "master" connection for all registrations...
2256 @@ -1573,6 +1624,7 @@
2257 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
2259 cupsdAddSelect(fd, (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
2260 +#endif /* HAVE_DNSSD */
2263 * Then get the port we use for registrations. If we are not listening
2264 @@ -1606,9 +1658,16 @@
2267 cupsdUpdateDNSSDName();
2270 + cupsdStartAvahiClient ();
2271 +#endif /* HAVE_AVAHI */
2276 #endif /* HAVE_DNSSD */
2278 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2281 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
2282 @@ -1834,10 +1893,10 @@
2287 - if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
2288 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2289 + if ((BrowseLocalProtocols & BROWSE_DNSSD))
2291 -#endif /* HAVE_DNSSD */
2292 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2295 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
2296 @@ -1902,7 +1961,7 @@
2301 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2303 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
2305 @@ -1910,8 +1969,14 @@
2307 cupsdUpdateDNSSDName(void)
2310 DNSServiceErrorType error; /* Error from service creation */
2311 char webif[1024]; /* Web interface share name */
2312 +#endif /* HAVE_DNSSD */
2314 + int ret; /* Error from service creation */
2315 + char webif[AVAHI_LABEL_MAX]; /* Web interface share name */
2316 +#endif /* HAVE_AVAHI */
2317 # ifdef HAVE_SYSTEMCONFIGURATION
2318 SCDynamicStoreRef sc; /* Context for dynamic store */
2319 CFDictionaryRef btmm; /* Back-to-My-Mac domains */
2320 @@ -2042,6 +2107,7 @@
2322 strlcpy(webif, "CUPS Web Interface", sizeof(webif));
2326 DNSServiceRefDeallocate(WebIFRef);
2328 @@ -2054,9 +2120,45 @@
2329 NULL)) != kDNSServiceErr_NoError)
2330 cupsdLogMessage(CUPSD_LOG_ERROR,
2331 "DNS-SD web interface registration failed: %d", error);
2332 +#endif /* HAVE_DNSSD */
2335 + if (!AvahiCupsClient)
2337 + * Client not yet running.
2341 + if (AvahiWebIFGroup)
2342 + avahi_entry_group_reset (AvahiWebIFGroup);
2344 + AvahiWebIFGroup = avahi_entry_group_new (AvahiCupsClient,
2345 + avahi_entry_group_cb,
2348 + if (AvahiWebIFGroup)
2350 + ret = avahi_entry_group_add_service (AvahiWebIFGroup,
2352 + AVAHI_PROTO_UNSPEC,
2355 + "_http._tcp", /* type */
2356 + NULL, /* domain */
2358 + DNSSDPort, /* port */
2361 + ret = avahi_entry_group_commit (AvahiWebIFGroup);
2364 + cupsdLogMessage (CUPSD_LOG_ERROR,
2365 + "Avahi web interface registration failed: %d", ret);
2367 +#endif /* HAVE_AVAHI */
2370 -#endif /* HAVE_DNSSD */
2371 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2375 @@ -2334,13 +2436,15 @@
2376 "Bad Back to My Mac domain in dynamic store!");
2378 # endif /* HAVE_COREFOUNDATION */
2379 +#endif /* HAVE_DNSSD */
2382 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2384 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2387 -static char * /* O - TXT record */
2388 +static cupsd_txt_record_t /* O - TXT record */
2389 dnssdBuildTxtRecord(
2390 int *txt_len, /* O - TXT record length */
2391 cupsd_printer_t *p, /* I - Printer information */
2392 @@ -2379,7 +2483,12 @@
2393 keyvalue[i ][0] = "ty";
2394 keyvalue[i++][1] = p->make_model ? p->make_model : "Unknown";
2396 - snprintf(admin_hostname, sizeof(admin_hostname), "%s.local.", DNSSDHostName);
2397 + snprintf(admin_hostname, sizeof(admin_hostname),
2400 + "." /* terminating dot no good for Avahi */
2401 +#endif /* HAVE_DNSSD */
2403 httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
2404 "http", NULL, admin_hostname, DNSSDPort, "/%s/%s",
2405 (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
2406 @@ -2462,7 +2571,12 @@
2407 * Then pack them into a proper txt record...
2411 return (dnssdPackTxtRecord(txt_len, keyvalue, i));
2412 +#endif /* HAVE_DNSSD */
2414 + return (avahiPackTxtRecord(keyvalue, i));
2415 +#endif /* HAVE_AVAHI */
2419 @@ -2474,7 +2588,16 @@
2420 dnssdComparePrinters(cupsd_printer_t *a,/* I - First printer */
2421 cupsd_printer_t *b)/* I - Second printer */
2423 - return (_cups_strcasecmp(a->reg_name, b->reg_name));
2433 + return (_cups_strcasecmp(a->reg_name, b->reg_name));
2437 @@ -2489,6 +2612,10 @@
2439 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
2446 * Closing the socket deregisters the service
2448 @@ -2524,6 +2651,24 @@
2449 free(p->printer_txt);
2450 p->printer_txt = NULL;
2452 +#endif /* HAVE_DNSSD */
2455 + if (p->avahi_group)
2457 + avahi_entry_group_reset (p->avahi_group);
2458 + avahi_entry_group_free (p->avahi_group);
2459 + p->avahi_group = NULL;
2462 + avahi_string_list_free (p->ipp_txt);
2464 + if (p->printer_txt)
2465 + avahi_string_list_free (p->printer_txt);
2467 + p->ipp_txt = p->printer_txt = NULL;
2469 +#endif /* HAVE_AVAHI */
2472 * Remove the printer from the array of DNS-SD printers, then clear the
2473 @@ -2533,8 +2678,10 @@
2474 cupsArrayRemove(DNSSDPrinters, p);
2475 cupsdClearString(&p->reg_name);
2477 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2482 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2483 * TXT record format.
2484 @@ -2644,8 +2791,10 @@
2485 LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
2488 +#endif /* HAVE_DNSSD */
2491 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2493 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2494 * or update the broadcast contents.
2495 @@ -2654,20 +2803,40 @@
2497 dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
2500 DNSServiceErrorType se; /* dnssd errors */
2501 char *ipp_txt, /* IPP TXT record buffer */
2502 *printer_txt, /* LPD TXT record buffer */
2503 - name[1024], /* Service name */
2504 - *nameptr; /* Pointer into name */
2505 + name[1024]; /* Service name */
2506 int ipp_len, /* IPP TXT record length */
2507 printer_len, /* LPD TXT record length */
2508 printer_port; /* LPD port number */
2509 +#endif /* HAVE_DNSSD */
2511 + int ret; /* Error code */
2512 + AvahiStringList *ipp_txt, /* IPP TXT record */
2513 + *printer_txt; /* LPD TXT record */
2514 + char name[AVAHI_LABEL_MAX], /* Service name */
2515 + fullsubtype[AVAHI_LABEL_MAX]; /* Full subtype */
2516 + char *regtype_copy, /* Writeable copy of reg type */
2517 + *subtype, /* Current service sub type */
2518 + *nextsubtype; /* Next service sub type */
2519 +#endif /* HAVE_AVAHI */
2520 + char *nameptr; /* Pointer into name */
2521 const char *regtype; /* Registration type */
2528 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2529 !p->ipp_ref ? "new" : "update");
2531 +#endif /* HAVE_DNSSD */
2533 + cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2534 + !p->avahi_group ? "new" : "update");
2535 +#endif /* HAVE_AVAHI */
2537 * If per-printer sharing was just disabled make sure we're not
2538 * registered before returning.
2539 @@ -2686,12 +2855,36 @@
2540 if (p->info && strlen(p->info) > 0)
2542 if (DNSSDComputerName)
2543 - snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDComputerName);
2546 + * Make sure there is room for at least 15 characters of
2547 + * DNSSDComputerName.
2550 + assert(sizeof(name) >= 15 + 4);
2551 + nameptr = name + strlcpy(name, p->info,
2552 + sizeof(name) - 4 -
2553 + strnlen(DNSSDComputerName, 15));
2554 + nameptr += strlcpy(nameptr, " @ ", sizeof(name) - (nameptr - name));
2555 + strlcpy(nameptr, DNSSDComputerName, sizeof(name) - (nameptr - name));
2558 strlcpy(name, p->info, sizeof(name));
2560 else if (DNSSDComputerName)
2561 - snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDComputerName);
2564 + * Make sure there is room for at least 15 characters of
2565 + * DNSSDComputerName.
2568 + assert(sizeof(name) >= 15 + 4);
2569 + nameptr = name + strlcpy(name, p->info,
2570 + sizeof(name) - 4 -
2571 + strnlen(DNSSDComputerName, 15));
2572 + nameptr += strlcpy(nameptr, " @ ", sizeof(name) - (nameptr - name));
2573 + strlcpy(nameptr, DNSSDComputerName, sizeof(name) - (nameptr - name));
2576 strlcpy(name, p->name, sizeof(name));
2578 @@ -2712,6 +2905,7 @@
2579 * Register IPP and (optionally) LPD...
2583 ipp_len = 0; /* anti-compiler-warning-code */
2584 ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
2586 @@ -2884,6 +3078,209 @@
2590 +#endif /* HAVE_DNSSD */
2592 + if (!AvahiCupsClient)
2594 + * Client not running yet. The client callback will call us again later.
2598 + ipp_txt = dnssdBuildTxtRecord(NULL, p, 0);
2599 + printer_txt = dnssdBuildTxtRecord(NULL, p, 1);
2600 + regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" : DNSSDRegType;
2602 + if (p->avahi_group && p->ipp_txt && ipp_txt &&
2603 + !avahi_string_list_equal (p->ipp_txt, ipp_txt))
2606 + * Update the existing registration...
2609 + avahi_string_list_free (p->ipp_txt);
2611 + if (p->printer_txt)
2612 + avahi_string_list_free (p->printer_txt);
2615 + * Update the service group entry.
2618 + regtype_copy = strdup (regtype);
2619 + subtype = strchr (regtype_copy, ',');
2623 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2624 + "Updating TXT record for %s (%s)", name, regtype_copy);
2625 + ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
2627 + AVAHI_PROTO_UNSPEC,
2631 + free (regtype_copy);
2634 + goto update_failed;
2636 + p->ipp_txt = ipp_txt;
2639 + if (BrowseLocalProtocols & BROWSE_LPD)
2641 + ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
2643 + AVAHI_PROTO_UNSPEC,
2645 + "_printer._tcp", NULL,
2648 + goto update_failed;
2650 + p->printer_txt = printer_txt;
2651 + printer_txt = NULL;
2654 + ret = avahi_entry_group_commit (p->avahi_group);
2658 + cupsdLogMessage (CUPSD_LOG_ERROR,
2659 + "Failed to update TXT record for %s: %d",
2661 + avahi_entry_group_reset (p->avahi_group);
2662 + avahi_entry_group_free (p->avahi_group);
2663 + p->avahi_group = NULL;
2664 + ipp_txt = p->ipp_txt;
2665 + p->ipp_txt = NULL;
2669 + if (!p->avahi_group)
2672 + * Initial registration. Use the _fax subtype for fax queues...
2675 + p->avahi_group = avahi_entry_group_new (AvahiCupsClient,
2676 + avahi_entry_group_cb,
2679 + cupsdLogMessage(CUPSD_LOG_DEBUG,
2680 + "Registering Avahi printer %s with name \"%s\" and "
2681 + "type \"%s\"", p->name, name, regtype);
2683 + if (!p->avahi_group)
2690 + * Add each service type (DNSSDRegType may contain several,
2691 + * separated by commas).
2694 + subtype = regtype_copy = strdup (regtype);
2695 + while (subtype && *subtype)
2697 + nextsubtype = strchr (subtype, ',');
2699 + *nextsubtype++ = '\0';
2701 + if (subtype == regtype_copy)
2704 + * Main type entry.
2707 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2708 + "Adding TXT record for %s (%s)", name, regtype_copy);
2709 + ret = avahi_entry_group_add_service_strlst (p->avahi_group,
2711 + AVAHI_PROTO_UNSPEC,
2712 + 0, name, regtype_copy,
2723 + snprintf (fullsubtype, sizeof(fullsubtype),
2724 + "%s._sub.%s", subtype, regtype_copy);
2725 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2726 + "Adding TXT record for %s (%s)", name, fullsubtype);
2727 + ret = avahi_entry_group_add_service_subtype (p->avahi_group,
2729 + AVAHI_PROTO_UNSPEC,
2732 + NULL, fullsubtype);
2737 + free (regtype_copy);
2741 + subtype = nextsubtype;
2744 + free (regtype_copy);
2745 + p->ipp_txt = ipp_txt;
2748 + if (BrowseLocalProtocols & BROWSE_LPD)
2750 + cupsdLogMessage(CUPSD_LOG_DEBUG,
2751 + "Registering Avahi printer %s with name \"%s\" and "
2752 + "type \"_printer._tcp\"", p->name, name);
2754 + ret = avahi_entry_group_add_service_strlst (p->avahi_group,
2756 + AVAHI_PROTO_UNSPEC,
2758 + "_printer._tcp", NULL, NULL,
2764 + p->printer_txt = printer_txt;
2765 + printer_txt = NULL;
2768 + ret = avahi_entry_group_commit (p->avahi_group);
2773 + cupsdLogMessage (CUPSD_LOG_ERROR,
2774 + "Failed to add Avahi entry for %s: %d",
2776 + if (p->avahi_group)
2778 + avahi_entry_group_reset (p->avahi_group);
2779 + avahi_entry_group_free (p->avahi_group);
2780 + p->avahi_group = NULL;
2782 + ipp_txt = p->ipp_txt;
2783 + p->ipp_txt = NULL;
2788 + avahi_string_list_free (ipp_txt);
2791 + avahi_string_list_free (printer_txt);
2792 +#endif /* HAVE_AVAHI */
2796 @@ -2896,6 +3293,10 @@
2798 cupsd_printer_t *p; /* Current printer */
2803 +#endif /* HAVE_DNSSD */
2806 * De-register the individual printers
2807 @@ -2906,6 +3307,7 @@
2808 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2809 dnssdDeregisterPrinter(p);
2813 * Shutdown the rest of the service refs...
2815 @@ -2926,14 +3328,17 @@
2817 DNSServiceRefDeallocate(DNSSDRef);
2819 +#endif /* HAVE_DNSSD */
2821 cupsArrayDelete(DNSSDPrinters);
2822 DNSSDPrinters = NULL;
2826 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2831 * 'dnssdUpdate()' - Handle DNS-SD queries.
2833 @@ -2955,6 +3360,147 @@
2834 #endif /* HAVE_DNSSD */
2839 + * 'avahiPackTxtRecord()' - Pack an array of key/value pairs into an
2840 + * AvahiStringList.
2843 +static AvahiStringList * /* O - new string list */
2844 +avahiPackTxtRecord(char *keyvalue[][2], /* I - Table of key value pairs */
2845 + int count) /* I - Number of items in table */
2847 + AvahiStringList *strlst = NULL;
2852 + elements = malloc ((1 + count) * sizeof (char *));
2856 + for (i = 0; i < count; i++)
2858 + len = (1 + strlen (keyvalue[i][0]) +
2859 + (keyvalue[i][1] ? 1 + strlen (keyvalue[i][1]) : 1));
2860 + elements[i] = malloc (len * sizeof (char));
2864 + snprintf (elements[i], len, "%s=%s", keyvalue[i][0], keyvalue[i][1]);
2867 + strlst = avahi_string_list_new_from_array ((const char **) elements, count);
2871 + free (elements[i]);
2879 + * 'avahi_entry_group_cb()' - Avahi entry group callback function.
2882 +avahi_entry_group_cb (AvahiEntryGroup *group,
2883 + AvahiEntryGroupState state,
2889 + name = ((cupsd_printer_t *) userdata)->reg_name;
2891 + name = "CUPS web interface";
2895 + case AVAHI_ENTRY_GROUP_UNCOMMITED:
2896 + case AVAHI_ENTRY_GROUP_REGISTERING:
2899 + case AVAHI_ENTRY_GROUP_ESTABLISHED:
2900 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2901 + "Avahi entry group established for %s", name);
2905 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2906 + "Avahi entry group %s has state %d",
2914 + * 'avahi_client_cb()' - Avahi client callback function.
2917 +avahi_client_cb (AvahiClient *client,
2918 + AvahiClientState state,
2921 + cupsd_printer_t *printer;
2924 + case AVAHI_CLIENT_S_RUNNING:
2926 + * Avahi client started successfully.
2928 + AvahiCupsClient = client;
2929 + AvahiCupsClientConnecting = 0;
2930 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client started");
2932 + cupsdUpdateDNSSDName ();
2934 + for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
2936 + printer = (cupsd_printer_t *)cupsArrayNext(Printers))
2937 + if (Browsing && (BrowseLocalProtocols & BROWSE_DNSSD) &&
2938 + (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
2939 + CUPS_PRINTER_SCANNER))) && printer->shared)
2940 + dnssdRegisterPrinter (printer);
2944 + case AVAHI_CLIENT_CONNECTING:
2946 + * No Avahi daemon, client is waiting.
2948 + AvahiCupsClientConnecting = 1;
2949 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client connecting");
2952 + case AVAHI_CLIENT_FAILURE:
2954 + * Avahi client failed, close it to allow a clean restart.
2956 + cupsdLogMessage (CUPSD_LOG_ERROR,
2957 + "Avahi client failed, "
2958 + "closing client to allow a clean restart");
2960 + for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
2962 + printer = (cupsd_printer_t *)cupsArrayNext(Printers))
2963 + dnssdDeregisterPrinter (printer);
2965 + avahi_client_free(client);
2966 + AvahiCupsClientConnecting = 0;
2967 + AvahiCupsClient = NULL;
2972 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client state: %d", state);
2975 +#endif /* HAVE_AVAHI */
2979 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2981 --- a/scheduler/dirsvc.h
2982 +++ b/scheduler/dirsvc.h
2984 # endif /* HAVE_LDAP_SSL_H */
2985 #endif /* HAVE_LDAP */
2988 +# include <avahi-client/publish.h>
2989 +#endif /* HAVE_AVAHI */
2992 * Browse protocols...
2994 @@ -131,19 +135,22 @@
2995 VAR cupsd_statbuf_t *PollStatusBuffer VALUE(NULL);
2996 /* Status buffer for pollers */
2999 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3000 VAR char *DNSSDComputerName VALUE(NULL),
3001 /* Computer/server name */
3002 *DNSSDHostName VALUE(NULL),
3004 *DNSSDRegType VALUE(NULL);
3005 /* Bonjour registration type */
3006 -VAR cups_array_t *DNSSDAlias VALUE(NULL);
3007 - /* List of dynamic ServerAlias's */
3008 VAR int DNSSDPort VALUE(0);
3009 /* Port number to register */
3010 VAR cups_array_t *DNSSDPrinters VALUE(NULL);
3011 /* Printers we have registered */
3012 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3015 +VAR cups_array_t *DNSSDAlias VALUE(NULL);
3016 + /* List of dynamic ServerAlias's */
3017 VAR DNSServiceRef DNSSDRef VALUE(NULL),
3018 /* Master DNS-SD service reference */
3019 WebIFRef VALUE(NULL),
3020 @@ -152,6 +159,17 @@
3021 /* Remote printer browse reference */
3022 #endif /* HAVE_DNSSD */
3025 +VAR AvahiCupsPoll *AvahiCupsPollHandle VALUE(NULL);
3026 + /* AvahiCupsPoll object */
3027 +VAR AvahiClient *AvahiCupsClient VALUE(NULL);
3028 + /* AvahiClient object */
3029 +VAR int AvahiCupsClientConnecting VALUE(0);
3030 + /* Is AvahiClient object connecting? */
3031 +VAR AvahiEntryGroup *AvahiWebIFGroup VALUE(NULL);
3032 + /* Web interface entry group */
3033 +#endif /* HAVE_AVAHI */
3036 VAR SLPHandle BrowseSLPHandle VALUE(NULL);
3037 /* SLP API handle */
3038 @@ -195,13 +213,14 @@
3039 extern void cupsdRestartPolling(void);
3040 extern void cupsdSaveRemoteCache(void);
3041 extern void cupsdSendBrowseList(void);
3042 +extern void cupsdStartAvahiClient(void);
3043 extern void cupsdStartBrowsing(void);
3044 extern void cupsdStartPolling(void);
3045 extern void cupsdStopBrowsing(void);
3046 extern void cupsdStopPolling(void);
3048 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3049 extern void cupsdUpdateDNSSDName(void);
3050 -#endif /* HAVE_DNSSD */
3051 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3053 extern void cupsdUpdateLDAPBrowse(void);
3054 #endif /* HAVE_LDAP */
3055 --- a/scheduler/ipp.c
3056 +++ b/scheduler/ipp.c
3057 @@ -6087,7 +6087,7 @@
3058 ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
3059 ippTimeToDate(curtime));
3062 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3063 if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
3065 if (printer->reg_name)
3066 @@ -6097,7 +6097,7 @@
3067 ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3068 "printer-dns-sd-name", 0);
3070 -#endif /* HAVE_DNSSD */
3071 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3073 if (!ra || cupsArrayFind(ra, "printer-error-policy"))
3074 ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
3075 --- a/scheduler/printers.c
3076 +++ b/scheduler/printers.c
3078 cupsdClearString(&p->alert);
3079 cupsdClearString(&p->alert_description);
3082 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3083 cupsdClearString(&p->pdl);
3084 -#endif /* HAVE_DNSSD */
3085 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3087 cupsArrayDelete(p->filetypes);
3089 @@ -3765,7 +3765,7 @@
3090 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3094 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3096 char pdl[1024]; /* Buffer to build pdl list */
3097 mime_filter_t *filter; /* MIME filter looping var */
3098 @@ -3821,7 +3821,7 @@
3100 cupsdSetString(&p->pdl, pdl);
3102 -#endif /* HAVE_DNSSD */
3103 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3107 --- a/scheduler/printers.h
3108 +++ b/scheduler/printers.h
3111 # include <dns_sd.h>
3112 #endif /* HAVE_DNSSD */
3114 +# include "avahi.h"
3115 +#endif /* HAVE_AVAHI */
3116 #include <cups/pwg-private.h>
3120 time_t marker_time; /* Last time marker attributes were updated */
3121 _ppd_cache_t *pc; /* PPD cache and mapping data */
3124 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3125 char *reg_name, /* Name used for service registration */
3126 - *pdl, /* pdl value for TXT record */
3127 - *ipp_txt, /* IPP TXT record contents */
3128 + *pdl; /* pdl value for TXT record */
3129 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3131 + char *ipp_txt, /* IPP TXT record contents */
3132 *printer_txt; /* LPD TXT record contents */
3133 int ipp_len, /* IPP TXT record length */
3134 printer_len; /* LPD TXT record length */
3135 DNSServiceRef ipp_ref, /* Reference for _ipp._tcp,_cups */
3136 printer_ref; /* Reference for _printer._tcp */
3137 #endif /* HAVE_DNSSD */
3139 + AvahiStringList *ipp_txt, /* IPP TXT record */
3140 + *printer_txt; /* LPD TXT record */
3141 + AvahiEntryGroup *avahi_group; /* Avahi entry group */
3142 +#endif /* HAVE_AVAHI */
3146 --- a/scheduler/conf.c
3147 +++ b/scheduler/conf.c
3149 Browsing = CUPS_DEFAULT_BROWSING;
3150 DefaultShared = CUPS_DEFAULT_DEFAULT_SHARED;
3153 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3154 cupsdSetString(&DNSSDRegType, "_ipp._tcp,_cups");
3155 #endif /* HAVE_DNSSD */