1 diff -up cups-1.4.5/backend/dnssd.c.avahi cups-1.4.5/backend/dnssd.c
2 --- cups-1.4.5/backend/dnssd.c.avahi 2010-12-24 13:11:33.760461375 +0000
3 +++ cups-1.4.5/backend/dnssd.c 2010-12-24 13:11:38.325341226 +0000
8 + * next_txt_record() - Get next TXT record from a cups_txt_records_t.
9 + * parse_txt_record_pair() - Read key/value pair in cups_txt_records_t.
10 * main() - Browse for printers.
11 * browse_callback() - Browse devices.
12 * browse_local_callback() - Browse local devices.
13 * compare_devices() - Compare two devices.
14 * exec_backend() - Execute the backend that corresponds to the
15 * resolved service name.
16 + * device_type() - Get DNS-SD type enumeration from string.
17 * get_device() - Create or update a device.
18 * query_callback() - Process query data.
19 + * avahi_client_callback() - Avahi client callback function.
20 + * avahi_query_callback() - Avahi query callback function.
21 + * avahi_browse_callback() - Avahi browse callback function.
22 + * find_device() - Find a device from its name and domain.
23 * sigterm_handler() - Handle termination signals...
24 * unquote() - Unquote a name string.
28 #include "backend-private.h"
29 #include <cups/array.h>
33 +#endif /* HAVE_DNSSD */
35 +# include <avahi-client/client.h>
36 +# include <avahi-client/lookup.h>
37 +# include <avahi-common/simple-watch.h>
38 +# include <avahi-common/domain.h>
39 +# include <avahi-common/error.h>
40 +# include <avahi-common/malloc.h>
41 +#define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
42 +#endif /* HAVE_AVAHI */
46 @@ -52,7 +70,12 @@ typedef enum
51 DNSServiceRef ref; /* Service reference for resolve */
52 +#endif /* HAVE_DNSSD */
54 + int resolved; /* Did we resolve the device? */
55 +#endif /* HAVE_AVAHI */
56 char *name, /* Service name */
57 *domain, /* Domain name */
58 *fullName, /* Full name */
59 @@ -64,6 +87,20 @@ typedef struct
60 sent; /* Did we list the device? */
69 + const uint8_t *data;
70 + const uint8_t *datanext;
71 + const uint8_t *dataend;
72 +#else /* HAVE_AVAHI */
73 + AvahiStringList *txt;
74 +#endif /* HAVE_DNSSD */
75 +} cups_txt_records_t;
80 @@ -77,6 +114,7 @@ static int job_canceled = 0;
85 static void browse_callback(DNSServiceRef sdRef,
86 DNSServiceFlags flags,
87 uint32_t interfaceIndex,
88 @@ -92,12 +130,6 @@ static void browse_local_callback(DNSSe
90 const char *replyDomain,
92 -static int compare_devices(cups_device_t *a, cups_device_t *b);
93 -static void exec_backend(char **argv);
94 -static cups_device_t *get_device(cups_array_t *devices,
95 - const char *serviceName,
96 - const char *regtype,
97 - const char *replyDomain);
98 static void query_callback(DNSServiceRef sdRef,
99 DNSServiceFlags flags,
100 uint32_t interfaceIndex,
101 @@ -106,9 +138,118 @@ static void query_callback(DNSServiceRe
102 uint16_t rrclass, uint16_t rdlen,
103 const void *rdata, uint32_t ttl,
105 +#endif /* HAVE_DNSSD */
107 +static void avahi_client_callback (AvahiClient *client,
108 + AvahiClientState state,
110 +static void avahi_browse_callback (AvahiServiceBrowser *browser,
111 + AvahiIfIndex interface,
112 + AvahiProtocol protocol,
113 + AvahiBrowserEvent event,
114 + const char *serviceName,
115 + const char *regtype,
116 + const char *replyDomain,
117 + AvahiLookupResultFlags flags,
119 +#endif /* HAVE_AVAHI */
121 +static cups_device_t * find_device (cups_array_t *devices,
122 + cups_txt_records_t *txt,
123 + cups_device_t *dkey);
124 +static int compare_devices(cups_device_t *a, cups_device_t *b);
125 +static void exec_backend(char **argv);
126 +static cups_device_t *get_device(cups_array_t *devices,
127 + const char *serviceName,
128 + const char *regtype,
129 + const char *replyDomain);
130 static void sigterm_handler(int sig);
131 static void unquote(char *dst, const char *src, size_t dstsize);
134 +static AvahiSimplePoll *simple_poll = NULL;
135 +static int avahi_got_callback;
136 +#endif /* HAVE_AVAHI */
140 + * 'next_txt_record()' - Get next TXT record from a cups_txt_records_t.
143 +static cups_txt_records_t *
144 +next_txt_record (cups_txt_records_t *txt)
147 + txt->data = txt->datanext;
148 +#else /* HAVE_AVAHI */
149 + txt->txt = avahi_string_list_get_next (txt->txt);
150 + if (txt->txt == NULL)
152 +#endif /* HAVE_DNSSD */
159 + * 'parse_txt_record_pair()' - Read key/value pair in cups_txt_records_t.
163 +parse_txt_record_pair (cups_txt_records_t *txt)
167 + uint8_t *data = txt->data;
171 + * Read a key/value pair starting with an 8-bit length. Since the
172 + * length is 8 bits and the size of the key/value buffers is 256, we
173 + * don't need to check for overflow...
177 + if (!datalen || (data + datalen) >= txt->dataend)
179 + txt->datanext = data + datalen;
181 + for (ptr = txt->key; data < txt->datanext && *data != '='; data ++)
185 + if (data < txt->datanext && *data == '=')
189 + if (data < datanext)
190 + memcpy (txt->value, data, txt->datanext - data);
191 + value[txt->datanext - data] = '\0';
195 +#else /* HAVE_AVAHI */
198 + avahi_string_list_get_pair (txt->txt, &key, &value, &len);
199 + if (len > sizeof (txt->value) - 1)
200 + len = sizeof (txt->value) - 1;
202 + memcpy (txt->value, value, len);
203 + txt->value[len] = '\0';
204 + len = strlen (key);
205 + if (len > sizeof (txt->key) - 1)
206 + len = sizeof (txt->key) - 1;
208 + memcpy (txt->key, key, len);
209 + txt->key[len] = '\0';
211 + avahi_free (value);
212 +#endif /* HAVE_AVAHI */
219 * 'main()' - Browse for printers.
220 @@ -119,6 +260,13 @@ main(int argc, /* I - Number of comm
221 char *argv[]) /* I - Command-line arguments */
223 const char *name; /* Backend name */
224 + cups_array_t *devices; /* Device array */
225 + cups_device_t *device; /* Current device */
226 + char uriName[1024]; /* Unquoted fullName for URI */
228 + int fd; /* Main file descriptor */
229 + fd_set input; /* Input set for select() */
230 + struct timeval timeout; /* Timeout for select() */
231 DNSServiceRef main_ref, /* Main service reference */
232 fax_ipp_ref, /* IPP fax service reference */
233 ipp_ref, /* IPP service reference */
234 @@ -130,12 +278,11 @@ main(int argc, /* I - Number of comm
235 pdl_datastream_ref, /* AppSocket service reference */
236 printer_ref, /* LPD service reference */
237 riousbprint_ref; /* Remote IO service reference */
238 - int fd; /* Main file descriptor */
239 - fd_set input; /* Input set for select() */
240 - struct timeval timeout; /* Timeout for select() */
241 - cups_array_t *devices; /* Device array */
242 - cups_device_t *device; /* Current device */
243 - char uriName[1024]; /* Unquoted fullName for URI */
244 +#endif /* HAVE_DNSSD */
246 + AvahiClient *client;
248 +#endif /* HAVE_AVAHI */
249 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
250 struct sigaction action; /* Actions for POSIX signals */
251 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
252 @@ -194,6 +341,49 @@ main(int argc, /* I - Number of comm
253 * Browse for different kinds of printers...
257 + if ((simple_poll = avahi_simple_poll_new ()) == NULL)
259 + perror ("ERROR: Unable to create avahi simple poll object");
263 + client = avahi_client_new (avahi_simple_poll_get (simple_poll),
264 + 0, avahi_client_callback, NULL, &error);
267 + perror ("ERROR: Unable to create avahi client");
271 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
272 + AVAHI_PROTO_UNSPEC,
273 + "_fax-ipp._tcp", NULL, 0,
274 + avahi_browse_callback, devices);
275 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
276 + AVAHI_PROTO_UNSPEC,
277 + "_ipp._tcp", NULL, 0,
278 + avahi_browse_callback, devices);
279 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
280 + AVAHI_PROTO_UNSPEC,
281 + "_ipp-tls._tcp", NULL, 0,
282 + avahi_browse_callback, devices);
283 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
284 + AVAHI_PROTO_UNSPEC,
285 + "_pdl-datastream._tcp",
287 + avahi_browse_callback,
289 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
290 + AVAHI_PROTO_UNSPEC,
291 + "_printer._tcp", NULL, 0,
292 + avahi_browse_callback, devices);
293 + avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
294 + AVAHI_PROTO_UNSPEC,
295 + "_riousbprint._tcp", NULL, 0,
296 + avahi_browse_callback, devices);
297 +#endif /* HAVE_AVAHI */
299 if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError)
301 perror("ERROR: Unable to create service connection");
302 @@ -245,6 +435,7 @@ main(int argc, /* I - Number of comm
303 riousbprint_ref = main_ref;
304 DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0,
305 "_riousbprint._tcp", NULL, browse_callback, devices);
306 +#endif /* HAVE_DNSSD */
309 * Loop until we are killed...
310 @@ -252,6 +443,9 @@ main(int argc, /* I - Number of comm
312 while (!job_canceled)
320 @@ -271,11 +465,35 @@ main(int argc, /* I - Number of comm
326 +#else /* HAVE_AVAHI */
328 + avahi_got_callback = 0;
329 + r = avahi_simple_poll_iterate (simple_poll, 1);
330 + if (r != 0 && r != EINTR)
333 + * We've been told to exit the loop. Perhaps the connection to
340 + if (avahi_got_callback)
342 +#endif /* HAVE_DNSSD */
347 * Announce any devices we've found...
351 DNSServiceErrorType status; /* DNS query status */
352 +#endif /* HAVE_DNSSD */
353 cups_device_t *best; /* Best matching device */
354 char device_uri[1024]; /* Device URI */
355 int count; /* Number of queries */
356 @@ -285,6 +503,7 @@ main(int argc, /* I - Number of comm
357 best = NULL, count = 0;
359 device = (cups_device_t *)cupsArrayNext(devices))
361 if (!device->ref && !device->sent)
364 @@ -313,14 +532,23 @@ main(int argc, /* I - Number of comm
368 - else if (!device->sent)
370 +#endif /* HAVE_DNSSD */
372 + if (!device->resolved)
375 +#endif /* HAVE_AVAHI */
380 * Got the TXT records, now report the device...
383 DNSServiceRefDeallocate(device->ref);
385 +#endif /* HAVE_DNSSD */
389 @@ -368,6 +596,7 @@ main(int argc, /* I - Number of comm
395 * 'browse_callback()' - Browse devices.
397 @@ -456,6 +685,7 @@ browse_local_callback(
401 +#endif /* HAVE_DNSSD */
405 @@ -530,6 +760,37 @@ exec_backend(char **argv) /* I - Comman
409 + * 'device_type()' - Get DNS-SD type enumeration from string.
413 +device_type (const char *regtype)
416 + if (!strcmp(regtype, "_ipp._tcp") ||
417 + !strcmp(regtype, "_ipp-tls._tcp"))
418 + return (CUPS_DEVICE_IPP);
419 + else if (!strcmp(regtype, "_fax-ipp._tcp"))
420 + return (CUPS_DEVICE_FAX_IPP);
421 + else if (!strcmp(regtype, "_printer._tcp"))
422 + return (CUPS_DEVICE_PDL_DATASTREAM);
424 + if (!strcmp(regtype, "_ipp._tcp.") ||
425 + !strcmp(regtype, "_ipp-tls._tcp."))
426 + return (CUPS_DEVICE_IPP);
427 + else if (!strcmp(regtype, "_fax-ipp._tcp."))
428 + return (CUPS_DEVICE_FAX_IPP);
429 + else if (!strcmp(regtype, "_printer._tcp."))
430 + return (CUPS_DEVICE_PRINTER);
431 + else if (!strcmp(regtype, "_pdl-datastream._tcp."))
432 + return (CUPS_DEVICE_PDL_DATASTREAM);
433 +#endif /* HAVE_AVAHI */
435 + return (CUPS_DEVICE_RIOUSBPRINT);
440 * 'get_device()' - Create or update a device.
443 @@ -550,18 +811,7 @@ get_device(cups_array_t *devices, /* I -
446 key.name = (char *)serviceName;
448 - if (!strcmp(regtype, "_ipp._tcp.") ||
449 - !strcmp(regtype, "_ipp-tls._tcp."))
450 - key.type = CUPS_DEVICE_IPP;
451 - else if (!strcmp(regtype, "_fax-ipp._tcp."))
452 - key.type = CUPS_DEVICE_FAX_IPP;
453 - else if (!strcmp(regtype, "_printer._tcp."))
454 - key.type = CUPS_DEVICE_PRINTER;
455 - else if (!strcmp(regtype, "_pdl-datastream._tcp."))
456 - key.type = CUPS_DEVICE_PDL_DATASTREAM;
458 - key.type = CUPS_DEVICE_RIOUSBPRINT;
459 + key.type = device_type (regtype);
461 for (device = cupsArrayFind(devices, &key);
463 @@ -581,8 +831,14 @@ get_device(cups_array_t *devices, /* I -
464 free(device->domain);
465 device->domain = strdup(replyDomain);
468 DNSServiceConstructFullName(fullName, device->name, regtype,
470 +#else /* HAVE_AVAHI */
471 + avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
472 + serviceName, regtype, replyDomain);
473 +#endif /* HAVE_DNSSD */
475 free(device->fullName);
476 device->fullName = strdup(fullName);
478 @@ -602,6 +858,9 @@ get_device(cups_array_t *devices, /* I -
479 device->domain = strdup(replyDomain);
480 device->type = key.type;
481 device->priority = 50;
483 + device->resolved = 0;
484 +#endif /* HAVE_AVAHI */
486 cupsArrayAdd(devices, device);
488 @@ -609,13 +868,20 @@ get_device(cups_array_t *devices, /* I -
489 * Set the "full name" of this service, which is used for queries...
493 DNSServiceConstructFullName(fullName, serviceName, regtype, replyDomain);
494 +#else /* HAVE_AVAHI */
495 + avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
496 + serviceName, regtype, replyDomain);
497 +#endif /* HAVE_DNSSD */
499 device->fullName = strdup(fullName);
507 * 'query_callback()' - Process query data.
509 @@ -639,7 +905,7 @@ query_callback(
510 *ptr; /* Pointer into string */
511 cups_device_t dkey, /* Search key */
512 *device; /* Device */
514 + cups_txt_records_t txt;
516 fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
517 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
518 @@ -673,84 +939,232 @@ query_callback(
519 if ((ptr = strstr(name, "._")) != NULL)
522 - if (strstr(fullName, "_ipp._tcp.") ||
523 - strstr(fullName, "_ipp-tls._tcp."))
524 - dkey.type = CUPS_DEVICE_IPP;
525 - else if (strstr(fullName, "_fax-ipp._tcp."))
526 - dkey.type = CUPS_DEVICE_FAX_IPP;
527 - else if (strstr(fullName, "_printer._tcp."))
528 - dkey.type = CUPS_DEVICE_PRINTER;
529 - else if (strstr(fullName, "_pdl-datastream._tcp."))
530 - dkey.type = CUPS_DEVICE_PDL_DATASTREAM;
531 + dkey.type = device_type (fullName);
534 + txt.dataend = rdata + rdlen;
535 + device = find_device ((cups_array_t *) context, &txt, &dkey);
537 + fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
539 +#endif /* HAVE_DNSSD */
544 + * 'avahi_client_callback()' - Avahi client callback function.
548 +avahi_client_callback(AvahiClient *client,
549 + AvahiClientState state,
553 + * If the connection drops, quit.
556 + if (state == AVAHI_CLIENT_FAILURE)
558 + fprintf (stderr, "ERROR: Avahi connection failed\n");
559 + avahi_simple_poll_quit (simple_poll);
565 + * 'avahi_query_callback()' - Avahi query callback function.
569 +avahi_query_callback(AvahiServiceResolver *resolver,
570 + AvahiIfIndex interface,
571 + AvahiProtocol protocol,
572 + AvahiResolverEvent event,
575 + const char *domain,
576 + const char *host_name,
577 + const AvahiAddress *address,
579 + AvahiStringList *txt,
580 + AvahiLookupResultFlags flags,
583 + AvahiClient *client;
588 + cups_txt_records_t txtr;
590 + client = avahi_service_resolver_get_client (resolver);
591 + if (event != AVAHI_RESOLVER_FOUND)
593 + if (event == AVAHI_RESOLVER_FAILURE)
595 + fprintf (stderr, "ERROR: %s\n",
596 + avahi_strerror (avahi_client_errno (client)));
599 + avahi_service_resolver_free (resolver);
604 + * Set search key for device.
608 + unquote (uqname, name, sizeof (uqname));
609 + if ((ptr = strstr(name, "._")) != NULL)
612 + key.domain = (char *) domain;
613 + key.type = device_type (type);
616 + * Find the device and the the TXT information.
620 + device = find_device ((cups_array_t *) context, &txtr, &key);
624 + * Let the main loop know to announce the device.
627 + device->resolved = 1;
628 + avahi_got_callback = 1;
631 - dkey.type = CUPS_DEVICE_RIOUSBPRINT;
632 + fprintf (stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", name);
634 + avahi_service_resolver_free (resolver);
639 + * 'avahi_browse_callback()' - Avahi browse callback function.
643 +avahi_browse_callback(AvahiServiceBrowser *browser,
644 + AvahiIfIndex interface,
645 + AvahiProtocol protocol,
646 + AvahiBrowserEvent event,
649 + const char *domain,
650 + AvahiLookupResultFlags flags,
653 + AvahiClient *client = avahi_service_browser_get_client (browser);
655 - for (device = cupsArrayFind(devices, &dkey);
658 + case AVAHI_BROWSER_FAILURE:
659 + fprintf (stderr, "ERROR: %s\n",
660 + avahi_strerror (avahi_client_errno (client)));
661 + avahi_simple_poll_quit (simple_poll);
664 + case AVAHI_BROWSER_NEW:
666 + * This object is new on the network.
669 + if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
672 + * This comes from the local machine so ignore it.
675 + fprintf (stderr, "DEBUG: ignoring local service %s\n", name);
680 + * Create a device entry for it if it doesn't yet exist.
683 + get_device ((cups_array_t *)context, name, type, domain);
686 + * Now look for a TXT entry.
689 + if (avahi_service_resolver_new (client, interface, protocol,
690 + name, type, domain,
691 + AVAHI_PROTO_UNSPEC, 0,
692 + avahi_query_callback, context) == NULL)
694 + fprintf (stderr, "ERROR: failed to resolve service %s: %s\n",
695 + name, avahi_strerror (avahi_client_errno (client)));
701 + case AVAHI_BROWSER_REMOVE:
702 + case AVAHI_BROWSER_ALL_FOR_NOW:
703 + case AVAHI_BROWSER_CACHE_EXHAUSTED:
707 +#endif /* HAVE_AVAHI */
711 + * 'find_device()' - Find a device from its name and domain.
714 +static cups_device_t *
715 +find_device (cups_array_t *devices,
716 + cups_txt_records_t *txt,
717 + cups_device_t *dkey)
719 + cups_device_t *device;
722 + for (device = cupsArrayFind(devices, dkey);
724 device = cupsArrayNext(devices))
726 - if (strcasecmp(device->name, dkey.name) ||
727 - strcasecmp(device->domain, dkey.domain))
728 + if (strcasecmp(device->name, dkey->name) ||
729 + strcasecmp(device->domain, dkey->domain))
734 - else if (device->type == dkey.type)
735 + else if (device->type == dkey->type)
738 * Found it, pull out the priority and make and model from the TXT
739 * record and save it...
742 - const uint8_t *data, /* Pointer into data */
743 - *datanext, /* Next key/value pair */
744 - *dataend; /* End of entire TXT record */
745 - uint8_t datalen; /* Length of current key/value pair */
746 - char key[256], /* Key string */
747 - value[256], /* Value string */
748 - make_and_model[512],
749 + char make_and_model[512],
750 /* Manufacturer and model */
751 model[256], /* Model */
752 - device_id[2048];/* 1284 device ID */
754 + device_id[2048]; /* 1284 device ID */
757 make_and_model[0] = '\0';
759 strcpy(model, "Unknown");
761 - for (data = rdata, dataend = data + rdlen;
767 - * Read a key/value pair starting with an 8-bit length. Since the
768 - * length is 8 bits and the size of the key/value buffers is 256, we
769 - * don't need to check for overflow...
774 - if (!datalen || (data + datalen) >= dataend)
779 - datanext = data + datalen;
781 - for (ptr = key; data < datanext && *data != '='; data ++)
785 - if (data < datanext && *data == '=')
789 - if (data < datanext)
790 - memcpy(value, data, datanext - data);
791 - value[datanext - data] = '\0';
795 + if (parse_txt_record_pair (txt))
799 + value = txt->value;
800 if (!strncasecmp(key, "usb_", 4))
803 @@ -805,6 +1219,10 @@ query_callback(
804 if (device->type == CUPS_DEVICE_PRINTER)
809 + if (next_txt_record (txt) == NULL)
813 if (device->device_id)
814 @@ -861,11 +1279,9 @@ query_callback(
819 - fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
825 * 'sigterm_handler()' - Handle termination signals...
827 diff -up cups-1.4.5/config.h.in.avahi cups-1.4.5/config.h.in
828 --- cups-1.4.5/config.h.in.avahi 2010-08-13 05:11:46.000000000 +0100
829 +++ cups-1.4.5/config.h.in 2010-12-24 13:11:38.327341170 +0000
834 + * Do we have Avahi for DNS Service Discovery?
841 * Do we have <sys/ioctl.h>?
844 diff -up cups-1.4.5/config-scripts/cups-dnssd.m4.avahi cups-1.4.5/config-scripts/cups-dnssd.m4
845 --- cups-1.4.5/config-scripts/cups-dnssd.m4.avahi 2009-08-28 23:54:34.000000000 +0100
846 +++ cups-1.4.5/config-scripts/cups-dnssd.m4 2010-12-24 13:11:38.326341199 +0000
847 @@ -27,6 +27,21 @@ AC_ARG_WITH(dnssd-includes, [ --with-dn
851 +AC_ARG_ENABLE(avahi, [ --enable-avahi turn on DNS Service Discovery support, default=no],
852 + [if test x$enable_avahi = xyes; then
853 + AC_MSG_CHECKING(for Avahi)
854 + if $PKGCONFIG --exists avahi-client; then
856 + CFLAGS="$CFLAGS `$PKGCONFIG --cflags avahi-client`"
857 + DNSSDLIBS="`$PKGCONFIG --libs avahi-client`"
858 + DNSSD_BACKEND="dnssd"
859 + AC_DEFINE(HAVE_AVAHI)
866 if test x$enable_dnssd != xno; then
867 AC_CHECK_HEADER(dns_sd.h, [
869 diff -up cups-1.4.5/cups/http-support.c.avahi cups-1.4.5/cups/http-support.c
870 --- cups-1.4.5/cups/http-support.c.avahi 2010-10-01 23:40:38.000000000 +0100
871 +++ cups-1.4.5/cups/http-support.c 2010-12-24 13:11:38.330341093 +0000
873 * http_copy_decode() - Copy and decode a URI.
874 * http_copy_encode() - Copy and encode a URI.
875 * resolve_callback() - Build a device URI for the given service name.
876 + * avahi_resolve_uri_client_cb()
877 + * - Avahi client callback for resolving URI.
878 + * avahi_resolve_uri_resolver_cb()
879 + * - Avahi resolver callback for resolving URI.
886 #endif /* HAVE_DNSSD */
888 +# include <avahi-client/client.h>
889 +# include <avahi-client/lookup.h>
890 +# include <avahi-common/simple-watch.h>
891 +#endif /* HAVE_AVAHI */
895 @@ -121,6 +130,24 @@ static void resolve_callback(DNSService
897 #endif /* HAVE_DNSSD */
900 +static void avahi_resolve_uri_client_cb(AvahiClient *client,
901 + AvahiClientState state,
902 + void *simple_poll);
903 +static void avahi_resolve_uri_resolver_cb(AvahiServiceResolver *resolver,
904 + AvahiIfIndex interface,
905 + AvahiProtocol protocol,
906 + AvahiResolverEvent event,
909 + const char *domain,
910 + const char *host_name,
911 + const AvahiAddress *address,
913 + AvahiStringList *txt,
914 + AvahiLookupResultFlags flags,
916 +#endif /* HAVE_AVAHI */
919 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
920 @@ -1351,6 +1378,9 @@ _httpResolveURI(
922 if (strstr(hostname, "._tcp"))
924 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
925 + char *regtype, /* Pointer to type in hostname */
926 + *domain; /* Pointer to domain in hostname */
928 DNSServiceRef ref, /* DNS-SD master service reference */
929 domainref, /* DNS-SD service reference for domain */
930 @@ -1361,6 +1391,16 @@ _httpResolveURI(
931 *domain; /* Pointer to domain in hostname */
932 _http_uribuf_t uribuf; /* URI buffer */
933 struct pollfd polldata; /* Polling data */
934 +#else /* HAVE_AVAHI */
935 + AvahiSimplePoll *simple_poll;
936 + AvahiClient *client;
940 + AvahiSimplePoll *poll;
941 + _http_uribuf_t uribuf;
943 +#endif /* HAVE_DNSSD */
947 @@ -1398,8 +1438,13 @@ _httpResolveURI(
952 uribuf.buffer = resolved_uri;
953 uribuf.bufsize = resolved_size;
955 + user_data.uribuf.buffer = resolved_uri;
956 + user_data.uribuf.bufsize = resolved_size;
959 resolved_uri[0] = '\0';
961 @@ -1414,6 +1459,7 @@ _httpResolveURI(
966 if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
969 @@ -1500,6 +1546,36 @@ _httpResolveURI(
971 DNSServiceRefDeallocate(ref);
973 +#else /* HAVE_AVAHI */
974 + if ((simple_poll = avahi_simple_poll_new ()) != NULL)
976 + if ((client = avahi_client_new (avahi_simple_poll_get (simple_poll),
977 + 0, avahi_resolve_uri_client_cb,
978 + &simple_poll, &error)) != NULL)
980 + user_data.poll = simple_poll;
981 + if (avahi_service_resolver_new (client, AVAHI_IF_UNSPEC,
982 + AVAHI_PROTO_UNSPEC, hostname,
983 + regtype, domain, AVAHI_PROTO_UNSPEC, 0,
984 + avahi_resolve_uri_resolver_cb,
985 + &user_data) != NULL)
987 + avahi_simple_poll_loop (simple_poll);
990 + * Collect the result.
993 + if (resolved_uri[0])
994 + uri = resolved_uri;
997 + avahi_client_free (client);
1000 + avahi_simple_poll_free (simple_poll);
1002 +#endif /* HAVE_DNSSD */
1006 @@ -1511,13 +1587,13 @@ _httpResolveURI(
1007 fputs("STATE: -connecting-to-device,offline-report\n", stderr);
1011 +#else /* HAVE_DNSSD || HAVE_AVAHI */
1013 * No DNS-SD support...
1017 -#endif /* HAVE_DNSSD */
1018 +#endif /* HAVE_DNSSD || HAVE_AVAHI */
1021 _cupsLangPuts(stderr, _("Unable to find printer!\n"));
1022 @@ -1723,6 +1799,116 @@ resolve_callback(
1023 #endif /* HAVE_DNSSD */
1028 + * 'avahi_resolve_uri_client_cb()' - Avahi client callback for resolving URI.
1032 +avahi_resolve_uri_client_cb (AvahiClient *client,
1033 + AvahiClientState state,
1034 + void *simple_poll)
1036 + DEBUG_printf(("avahi_resolve_uri_client_callback(client=%p, state=%d, "
1037 + "simple_poll=%p)\n", client, state, simple_poll));
1040 + * If the connection drops, quit.
1043 + if (state == AVAHI_CLIENT_FAILURE)
1044 + avahi_simple_poll_quit (simple_poll);
1049 + * 'avahi_resolve_uri_resolver_cb()' - Avahi resolver callback for resolving
1054 +avahi_resolve_uri_resolver_cb (AvahiServiceResolver *resolver,
1055 + AvahiIfIndex interface,
1056 + AvahiProtocol protocol,
1057 + AvahiResolverEvent event,
1060 + const char *domain,
1061 + const char *host_name,
1062 + const AvahiAddress *address,
1064 + AvahiStringList *txt,
1065 + AvahiLookupResultFlags flags,
1068 + const char *scheme; /* URI scheme */
1069 + char rp[256]; /* Remote printer */
1070 + AvahiStringList *pair;
1072 + size_t valueLen = 0;
1073 + char addr[AVAHI_ADDRESS_STR_MAX];
1076 + AvahiSimplePoll *poll;
1077 + _http_uribuf_t uribuf;
1078 + } *poll_uribuf = context;
1080 + DEBUG_printf(("avahi_resolve_uri_resolver_callback(resolver=%p, "
1081 + "interface=%d, protocol=%d, event=%d, name=\"%s\", "
1082 + "type=\"%s\", domain=\"%s\", host_name=\"%s\", address=%p, "
1083 + "port=%d, txt=%p, flags=%d, context=%p)\n",
1084 + resolver, interface, protocol, event, name, type, domain,
1085 + host_name, address, port, txt, flags, context));
1087 + if (event != AVAHI_RESOLVER_FOUND)
1089 + avahi_service_resolver_free (resolver);
1090 + avahi_simple_poll_quit (poll_uribuf->poll);
1095 + * Figure out the scheme from the full name...
1098 + if (strstr(type, "_ipp."))
1100 + else if (strstr(type, "_printer."))
1102 + else if (strstr(type, "_pdl-datastream."))
1103 + scheme = "socket";
1105 + scheme = "riousbprint";
1108 + * Extract the "remote printer key from the TXT record...
1111 + if ((pair = avahi_string_list_find (txt, "rp")) != NULL)
1113 + avahi_string_list_get_pair (pair, NULL, &value, &valueLen);
1115 + memcpy (rp + 1, value, valueLen);
1116 + rp[valueLen + 1] = '\0';
1122 + * Assemble the final device URI...
1125 + avahi_address_snprint (addr, AVAHI_ADDRESS_STR_MAX, address);
1126 + httpAssembleURI(HTTP_URI_CODING_ALL, poll_uribuf->uribuf.buffer,
1127 + poll_uribuf->uribuf.bufsize, scheme, NULL,
1129 + DEBUG_printf(("avahi_resolve_uri_resolver_callback: Resolved URI is \"%s\"\n",
1130 + poll_uribuf->uribuf.buffer));
1131 + avahi_simple_poll_quit (poll_uribuf->poll);
1133 +#endif /* HAVE_AVAHI */
1137 * End of "$Id: http-support.c 9322 2010-10-01 22:40:38Z mike $".
1139 diff -up cups-1.4.5/scheduler/avahi.c.avahi cups-1.4.5/scheduler/avahi.c
1140 --- cups-1.4.5/scheduler/avahi.c.avahi 2010-12-24 13:11:38.333341014 +0000
1141 +++ cups-1.4.5/scheduler/avahi.c 2010-12-24 13:11:38.333341014 +0000
1146 + * Avahi poll implementation for the CUPS scheduler.
1148 + * Copyright (C) 2010 Red Hat, Inc.
1150 + * Tim Waugh <twaugh@redhat.com>
1152 + * Distribution and use rights are outlined in the file "LICENSE.txt"
1153 + * "LICENSE" which should have been included with this file. If this
1154 + * file is missing or damaged, see the license at "http://www.cups.org/".
1158 + * watch_read_cb - Read callback for file descriptor
1159 + * watch_write_cb - Write callback for file descriptor
1160 + * watched_fd_add_select() - Call cupsdAddSelect() as needed
1161 + * watch_new() - Create a new file descriptor watch
1162 + * watch_free() - Free a file descriptor watch
1163 + * watch_update() - Update watched events for a file descriptor
1164 + * watch_get_events() - Get events that happened for a file descriptor
1165 + * timeout_cb() - Run a timed Avahi callback
1166 + * timeout_new() - Set a wakeup time
1167 + * timeout_update() - Update the expiration time for a timeout
1168 + * timeout_free() - Free a timeout
1169 + * compare_watched_fds() - Compare watched file descriptors for array sorting
1170 + * compare_timeouts() - Compare timeouts for array sorting
1171 + * avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
1172 + * avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
1173 + * avahi_cups_poll_get() - Get the abstract poll API structure
1176 +#include <config.h>
1178 +#ifdef HAVE_AVAHI /* Applies to entire file... */
1181 + * Include necessary headers...
1186 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
1187 +# include <malloc.h>
1188 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
1191 +# include <avahi-common/timeval.h>
1192 +#endif /* HAVE_AVAHI */
1197 + AvahiCupsPoll *cups_poll;
1200 + AvahiWatchEvent occurred;
1201 + cups_array_t *watches;
1202 +} cupsd_watched_fd_t;
1206 + cupsd_watched_fd_t *watched_fd;
1208 + AvahiWatchEvent events;
1209 + AvahiWatchCallback callback;
1213 +struct AvahiTimeout
1215 + AvahiCupsPoll *cups_poll;
1216 + AvahiTimeoutCallback callback;
1218 + cupsd_timeout_t *cupsd_timeout;
1222 + * Local functions...
1225 +static AvahiWatch * watch_new(const AvahiPoll *api,
1227 + AvahiWatchEvent events,
1228 + AvahiWatchCallback callback,
1230 +static void watch_free(AvahiWatch *watch);
1231 +static void watch_update(AvahiWatch *watch,
1232 + AvahiWatchEvent events);
1233 +static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
1234 +static int compare_watches(AvahiWatch *p0,
1239 + * 'watch_read_cb' - Read callback for file descriptor
1243 +watch_read_cb (void *userdata)
1245 + AvahiWatch *watch;
1246 + cupsd_watched_fd_t *watched_fd = userdata;
1247 + watched_fd->occurred |= AVAHI_WATCH_IN;
1248 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
1250 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
1251 + if (watch->events & watched_fd->occurred) {
1252 + (watch->callback) (watch, watched_fd->fd,
1253 + AVAHI_WATCH_IN, watch->userdata);
1254 + watched_fd->occurred &= ~AVAHI_WATCH_IN;
1262 + * 'watch_write_cb' - Write callback for file descriptor
1266 +watch_write_cb (void *userdata)
1268 + AvahiWatch *watch;
1269 + cupsd_watched_fd_t *watched_fd = userdata;
1270 + watched_fd->occurred |= AVAHI_WATCH_OUT;
1271 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
1273 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
1274 + if (watch->events & watched_fd->occurred) {
1275 + (watch->callback) (watch, watched_fd->fd,
1276 + AVAHI_WATCH_OUT, watch->userdata);
1277 + watched_fd->occurred &= ~AVAHI_WATCH_OUT;
1285 + * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
1288 +static int /* O - Watches? */
1289 +watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
1291 + AvahiWatch *watch;
1292 + cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
1294 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
1296 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
1297 + if (watch->events & (AVAHI_WATCH_IN |
1299 + AVAHI_WATCH_HUP)) {
1300 + read_cb = (cupsd_selfunc_t)watch_read_cb;
1301 + if (write_cb != NULL)
1305 + if (watch->events & AVAHI_WATCH_OUT) {
1306 + write_cb = (cupsd_selfunc_t)watch_write_cb;
1307 + if (read_cb != NULL)
1312 + if (read_cb || write_cb)
1313 + cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
1315 + cupsdRemoveSelect (watched_fd->fd);
1317 + return (read_cb || write_cb);
1321 + * 'watch_new' - Create a new file descriptor watch
1324 +static AvahiWatch *
1325 +watch_new (const AvahiPoll *api,
1327 + AvahiWatchEvent events,
1328 + AvahiWatchCallback callback,
1331 + cupsd_watched_fd_t key, *watched_fd;
1332 + AvahiCupsPoll *cups_poll = api->userdata;
1333 + AvahiWatch *watch = malloc(sizeof(AvahiWatch));
1334 + if (watch == NULL)
1337 + watch->events = events;
1338 + watch->callback = callback;
1339 + watch->userdata = userdata;
1342 + watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
1343 + if (watched_fd == NULL) {
1344 + watched_fd = malloc(sizeof(cupsd_watched_fd_t));
1345 + if (watched_fd == NULL)
1348 + watched_fd->fd = fd;
1349 + watched_fd->occurred = 0;
1350 + watched_fd->watches = cupsArrayNew ((cups_array_func_t)compare_watches,
1354 + cupsArrayAdd(watched_fd->watches, watch);
1355 + watched_fd_add_select (watched_fd);
1361 + * 'watch_free' - Free a file descriptor watch
1365 +watch_free (AvahiWatch *watch)
1367 + cupsd_watched_fd_t *watched_fd = watch->watched_fd;
1368 + AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
1370 + cupsArrayRemove (watched_fd->watches, watch);
1373 + if (!watched_fd_add_select (watched_fd)) {
1374 + /* No more watches */
1375 + cupsArrayRemove (cups_poll->watched_fds, watched_fd);
1376 + free (watched_fd);
1382 + * 'watch_update' - Update watched events for a file descriptor
1386 +watch_update (AvahiWatch *watch,
1387 + AvahiWatchEvent events)
1389 + watch->events = events;
1390 + watched_fd_add_select (watch->watched_fd);
1395 + * 'watch_get_events' - Get events that happened for a file descriptor
1398 +static AvahiWatchEvent
1399 +watch_get_events (AvahiWatch *watch)
1401 + return (watch->watched_fd->occurred);
1406 + * 'compare_watches' - Compare watches for array sorting
1410 +compare_watches (AvahiWatch *p0,
1413 + if (p0->watched_fd->fd < p1->watched_fd->fd)
1416 + return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
1421 + * 'timeout_cb()' - Run a timed Avahi callback
1425 +timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
1427 + AvahiTimeout *timeout = userdata;
1428 + (timeout->callback) (timeout, timeout->userdata);
1433 + * 'timeout_new' - Set a wakeup time
1436 +static AvahiTimeout *
1437 +timeout_new (const AvahiPoll *api,
1438 + const struct timeval *tv,
1439 + AvahiTimeoutCallback callback,
1442 + AvahiTimeout *timeout;
1443 + AvahiCupsPoll *cups_poll = api->userdata;
1445 + timeout = malloc(sizeof(AvahiTimeout));
1446 + if (timeout == NULL)
1449 + timeout->cups_poll = cups_poll;
1450 + timeout->callback = callback;
1451 + timeout->userdata = userdata;
1452 + timeout->cupsd_timeout = cupsdAddTimeout (tv,
1453 + (cupsd_timeoutfunc_t)timeout_cb,
1455 + cupsArrayAdd (cups_poll->timeouts, timeout);
1461 + * 'timeout_update' - Update the expiration time for a timeout
1465 +timeout_update (AvahiTimeout *timeout,
1466 + const struct timeval *tv)
1468 + cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
1473 + * ' timeout_free' - Free a timeout
1477 +timeout_free (AvahiTimeout *timeout)
1479 + cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
1480 + cupsdRemoveTimeout (timeout->cupsd_timeout);
1486 + * 'compare_watched_fds' - Compare watched file descriptors for array sorting
1489 +compare_watched_fds(cupsd_watched_fd_t *p0,
1490 + cupsd_watched_fd_t *p1)
1492 + if (p0->fd != p1->fd)
1493 + return (p0->fd < p1->fd ? -1 : 1);
1498 + return (p0 < p1 ? -1 : 1);
1503 + * 'compare_timeouts' - Compare timeouts for array sorting
1506 +compare_timeouts(AvahiTimeout *p0,
1510 + * Just compare pointers to make it a stable sort.
1513 + if (p0->cupsd_timeout < p1->cupsd_timeout)
1515 + return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
1520 + * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
1524 +avahi_cups_poll_new (void)
1526 + AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
1527 + if (cups_poll == NULL)
1530 + cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
1532 + cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts,
1535 + cups_poll->api.userdata = cups_poll;
1536 + cups_poll->api.watch_new = watch_new;
1537 + cups_poll->api.watch_free = watch_free;
1538 + cups_poll->api.watch_update = watch_update;
1539 + cups_poll->api.watch_get_events = watch_get_events;
1541 + cups_poll->api.timeout_new = timeout_new;
1542 + cups_poll->api.timeout_update = timeout_update;
1543 + cups_poll->api.timeout_free = timeout_free;
1545 + return (cups_poll);
1550 + * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
1553 +avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
1555 + cupsd_watched_fd_t *watched_fd;
1557 + for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
1559 + watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds)){
1560 + cupsArrayClear (watched_fd->watches);
1563 + cupsArrayClear (cups_poll->watched_fds);
1564 + cupsArrayClear (cups_poll->timeouts);
1569 + * 'avahi_cups_poll_get' - Get the abstract poll API structure
1573 +avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
1575 + return (&cups_poll->api);
1579 +#endif /* HAVE_AVAHI ... from top of file */
1584 diff -up cups-1.4.5/scheduler/avahi.h.avahi cups-1.4.5/scheduler/avahi.h
1585 --- cups-1.4.5/scheduler/avahi.h.avahi 2010-12-24 13:11:38.334340988 +0000
1586 +++ cups-1.4.5/scheduler/avahi.h 2010-12-24 13:11:38.334340988 +0000
1591 + * Avahi poll implementation for the CUPS scheduler.
1593 + * Copyright (C) 2010 Red Hat, Inc.
1595 + * Tim Waugh <twaugh@redhat.com>
1597 + * Distribution and use rights are outlined in the file "LICENSE.txt"
1598 + * which should have been included with this file. If this file is
1599 + * file is missing or damaged, see the license at "http://www.cups.org/".
1602 +#include <config.h>
1605 +# include <avahi-client/client.h>
1606 +# include <avahi-client/publish.h>
1607 +#endif /* HAVE_AVAHI */
1609 +#ifdef HAVE_AUTHORIZATION_H
1610 +# include <Security/Authorization.h>
1611 +#endif /* HAVE_AUTHORIZATION_H */
1618 + cups_array_t *watched_fds;
1619 + cups_array_t *timeouts;
1621 +#endif /* HAVE_AVAHI */
1628 +extern AvahiCupsPoll * avahi_cups_poll_new(void);
1629 +extern void avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
1630 +extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
1631 +#endif /* HAVE_AVAHI */
1637 diff -up cups-1.4.5/scheduler/cupsd.h.avahi cups-1.4.5/scheduler/cupsd.h
1638 --- cups-1.4.5/scheduler/cupsd.h.avahi 2010-09-21 23:34:57.000000000 +0100
1639 +++ cups-1.4.5/scheduler/cupsd.h 2010-12-24 13:11:38.335340961 +0000
1640 @@ -147,6 +147,15 @@ extern const char *cups_hstrerror(int);
1642 typedef void (*cupsd_selfunc_t)(void *data);
1646 + * Timeout callback function type...
1649 +typedef struct _cupsd_timeout_s cupsd_timeout_t;
1650 +typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
1651 +#endif /* HAVE_AVAHI */
1656 @@ -188,6 +197,9 @@ VAR PSQUpdateQuotaProcPtr PSQUpdateQuota
1657 /* Apple PrintService quota function */
1658 #endif /* __APPLE__ && HAVE_DLFCN_H */
1661 +VAR cups_array_t *Timeouts; /* Timed callbacks for main loop */
1662 +#endif /* HAVE_AVAHI */
1666 @@ -240,6 +252,18 @@ extern void cupsdRemoveSelect(int fd);
1667 extern void cupsdStartSelect(void);
1668 extern void cupsdStopSelect(void);
1671 +extern void cupsdInitTimeouts(void);
1672 +extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
1673 + cupsd_timeoutfunc_t cb,
1675 +extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
1676 +extern void cupsdRunTimeout (cupsd_timeout_t *timeout);
1677 +extern void cupsdUpdateTimeout (cupsd_timeout_t *timeout,
1678 + const struct timeval *tv);
1679 +extern void cupsdRemoveTimeout (cupsd_timeout_t *timeout);
1680 +#endif /* HAVE_AVAHI */
1682 extern int cupsdRemoveFile(const char *filename);
1685 diff -up cups-1.4.5/scheduler/dirsvc.c.avahi cups-1.4.5/scheduler/dirsvc.c
1686 --- cups-1.4.5/scheduler/dirsvc.c.avahi 2010-12-24 13:11:33.679463507 +0000
1687 +++ cups-1.4.5/scheduler/dirsvc.c 2010-12-24 13:11:38.343340751 +0000
1689 #endif /* HAVE_DNSSD */
1693 +typedef char *cupsd_txt_record_t;
1694 +#endif /* HAVE_DNSSD */
1696 +typedef AvahiStringList *cupsd_txt_record_t;
1697 +#endif /* HAVE_AVAHI */
1700 * Local functions...
1702 @@ -159,15 +166,20 @@ static void update_polling(void);
1703 static void update_smb(int onoff);
1706 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1707 +static cupsd_txt_record_t dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
1709 +static void dnssdDeregisterPrinter(cupsd_printer_t *p);
1710 +static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
1711 +static void dnssdRegisterPrinter(cupsd_printer_t *p);
1712 +static void dnssdStop(void);
1713 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
1716 # ifdef HAVE_COREFOUNDATION
1717 static void dnssdAddAlias(const void *key, const void *value,
1719 # endif /* HAVE_COREFOUNDATION */
1720 -static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
1722 -static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
1723 -static void dnssdDeregisterPrinter(cupsd_printer_t *p);
1724 static char *dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2],
1726 static void dnssdRegisterCallback(DNSServiceRef sdRef,
1727 @@ -175,11 +187,20 @@ static void dnssdRegisterCallback(DNSSer
1728 DNSServiceErrorType errorCode,
1729 const char *name, const char *regtype,
1730 const char *domain, void *context);
1731 -static void dnssdRegisterPrinter(cupsd_printer_t *p);
1732 -static void dnssdStop(void);
1733 static void dnssdUpdate(void);
1734 #endif /* HAVE_DNSSD */
1737 +static AvahiStringList *avahiPackTxtRecord(char *keyvalue[][2],
1739 +static void avahi_entry_group_cb (AvahiEntryGroup *group,
1740 + AvahiEntryGroupState state,
1742 +static void avahi_client_cb (AvahiClient *client,
1743 + AvahiClientState state,
1745 +#endif /* HAVE_AVAHI */
1748 static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
1750 @@ -283,10 +304,10 @@ cupsdDeregisterPrinter(
1751 ldap_dereg_printer(p);
1752 #endif /* HAVE_LDAP */
1755 - if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
1756 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1757 + if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD))
1758 dnssdDeregisterPrinter(p);
1759 -#endif /* HAVE_DNSSD */
1760 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
1764 @@ -694,10 +715,10 @@ cupsdRegisterPrinter(cupsd_printer_t *p)
1765 slpRegisterPrinter(p); */
1766 #endif /* HAVE_LIBSLP */
1769 - if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
1770 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1771 + if ((BrowseLocalProtocols & BROWSE_DNSSD))
1772 dnssdRegisterPrinter(p);
1773 -#endif /* HAVE_DNSSD */
1774 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
1778 @@ -1535,13 +1556,16 @@ cupsdStartBrowsing(void)
1783 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1784 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
1787 DNSServiceErrorType error; /* Error from service creation */
1788 +#endif /* HAVE_DNSSD */
1789 cupsd_listener_t *lis; /* Current listening socket */
1794 * First create a "master" connection for all registrations...
1796 @@ -1566,6 +1590,7 @@ cupsdStartBrowsing(void)
1797 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
1799 cupsdAddSelect(fd, (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
1800 +#endif /* HAVE_DNSSD */
1803 * Then get the port we use for registrations. If we are not listening
1804 @@ -1607,9 +1632,20 @@ cupsdStartBrowsing(void)
1807 cupsdUpdateDNSSDName();
1811 #endif /* HAVE_DNSSD */
1813 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
1816 + if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
1818 + AvahiCupsPollHandle = avahi_cups_poll_new ();
1819 + avahi_client_new (avahi_cups_poll_get (AvahiCupsPollHandle),
1820 + 0, avahi_client_cb, NULL, NULL);
1822 +#endif /* HAVE_AVAHI */
1825 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
1826 @@ -1835,10 +1871,10 @@ cupsdStopBrowsing(void)
1831 - if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
1832 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1833 + if ((BrowseLocalProtocols & BROWSE_DNSSD))
1835 -#endif /* HAVE_DNSSD */
1836 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
1839 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
1840 @@ -1903,7 +1939,7 @@ cupsdStopPolling(void)
1845 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1847 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1849 @@ -1911,7 +1947,12 @@ cupsdStopPolling(void)
1851 cupsdUpdateDNSSDName(void)
1854 DNSServiceErrorType error; /* Error from service creation */
1855 +#endif /* HAVE_DNSSD */
1857 + int ret; /* Error from service creation */
1858 +#endif /* HAVE_AVAHI */
1859 char webif[1024]; /* Web interface share name */
1860 #ifdef HAVE_COREFOUNDATION_H
1861 SCDynamicStoreRef sc; /* Context for dynamic store */
1862 @@ -2043,6 +2084,7 @@ cupsdUpdateDNSSDName(void)
1864 strlcpy(webif, "CUPS Web Interface", sizeof(webif));
1868 DNSServiceRefDeallocate(WebIFRef);
1870 @@ -2055,6 +2097,42 @@ cupsdUpdateDNSSDName(void)
1871 NULL)) != kDNSServiceErr_NoError)
1872 cupsdLogMessage(CUPSD_LOG_ERROR,
1873 "DNS-SD web interface registration failed: %d", error);
1874 +#endif /* HAVE_DNSSD */
1877 + if (!AvahiCupsClient)
1879 + * Client not yet running.
1883 + if (AvahiWebIFGroup)
1884 + avahi_entry_group_reset (AvahiWebIFGroup);
1886 + AvahiWebIFGroup = avahi_entry_group_new (AvahiCupsClient,
1887 + avahi_entry_group_cb,
1890 + if (AvahiWebIFGroup)
1892 + ret = avahi_entry_group_add_service (AvahiWebIFGroup,
1894 + AVAHI_PROTO_UNSPEC,
1897 + "_http._tcp", /* type */
1898 + NULL, /* domain */
1900 + htons(DNSSDPort), /* port */
1903 + ret = avahi_entry_group_commit (AvahiWebIFGroup);
1906 + cupsdLogMessage (CUPSD_LOG_ERROR,
1907 + "Avahi web interface registration failed: %d", ret);
1909 +#endif /* HAVE_AVAHI */
1912 #endif /* HAVE_DNSSD */
1913 @@ -2300,162 +2378,7 @@ dequote(char *d, /* I - Destinat
1918 -# ifdef HAVE_COREFOUNDATION
1920 - * 'dnssdAddAlias()' - Add a DNS-SD alias name.
1924 -dnssdAddAlias(const void *key, /* I - Key */
1925 - const void *value, /* I - Value (domain) */
1926 - void *context) /* I - Unused */
1928 - char valueStr[1024], /* Domain string */
1929 - hostname[1024]; /* Complete hostname */
1934 - if (CFGetTypeID((CFStringRef)value) == CFStringGetTypeID() &&
1935 - CFStringGetCString((CFStringRef)value, valueStr, sizeof(valueStr),
1936 - kCFStringEncodingUTF8))
1938 - snprintf(hostname, sizeof(hostname), "%s.%s", DNSSDHostName, valueStr);
1940 - DNSSDAlias = cupsArrayNew(NULL, NULL);
1942 - cupsdAddAlias(DNSSDAlias, hostname);
1943 - cupsdLogMessage(CUPSD_LOG_DEBUG, "Added Back to My Mac ServerAlias %s",
1947 - cupsdLogMessage(CUPSD_LOG_ERROR,
1948 - "Bad Back to My Mac domain in dynamic store!");
1950 -# endif /* HAVE_COREFOUNDATION */
1954 - * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
1957 -static char * /* O - TXT record */
1958 -dnssdBuildTxtRecord(
1959 - int *txt_len, /* O - TXT record length */
1960 - cupsd_printer_t *p, /* I - Printer information */
1961 - int for_lpd) /* I - 1 = LPD, 0 = IPP */
1963 - int i; /* Looping var */
1964 - char adminurl_str[256], /* URL for th admin page */
1965 - type_str[32], /* Type to string buffer */
1966 - state_str[32], /* State to string buffer */
1967 - rp_str[1024], /* Queue name string buffer */
1968 - air_str[1024], /* auth-info-required string buffer */
1969 - *keyvalue[32][2]; /* Table of key/value pairs */
1973 - * Load up the key value pairs...
1978 - keyvalue[i ][0] = "txtvers";
1979 - keyvalue[i++][1] = "1";
1981 - keyvalue[i ][0] = "qtotal";
1982 - keyvalue[i++][1] = "1";
1984 - keyvalue[i ][0] = "rp";
1985 - keyvalue[i++][1] = rp_str;
1987 - strlcpy(rp_str, p->name, sizeof(rp_str));
1989 - snprintf(rp_str, sizeof(rp_str), "%s/%s",
1990 - (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
1992 - keyvalue[i ][0] = "ty";
1993 - keyvalue[i++][1] = p->make_model ? p->make_model : "Unknown";
1995 - httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
1996 - "http", NULL, DNSSDHostName, DNSSDPort, "/%s/%s",
1997 - (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
1999 - keyvalue[i ][0] = "adminurl";
2000 - keyvalue[i++][1] = adminurl_str;
2002 - keyvalue[i ][0] = "note";
2003 - keyvalue[i++][1] = p->location ? p->location : "";
2005 - keyvalue[i ][0] = "priority";
2006 - keyvalue[i++][1] = for_lpd ? "100" : "0";
2008 - keyvalue[i ][0] = "product";
2009 - keyvalue[i++][1] = p->product ? p->product : "Unknown";
2011 - snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
2012 - snprintf(state_str, sizeof(state_str), "%d", p->state);
2014 - keyvalue[i ][0] = "printer-state";
2015 - keyvalue[i++][1] = state_str;
2017 - keyvalue[i ][0] = "printer-type";
2018 - keyvalue[i++][1] = type_str;
2020 - keyvalue[i ][0] = "Transparent";
2021 - keyvalue[i++][1] = "T";
2023 - keyvalue[i ][0] = "Binary";
2024 - keyvalue[i++][1] = "T";
2026 - keyvalue[i ][0] = "Fax";
2027 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_FAX) ? "T" : "F";
2029 - keyvalue[i ][0] = "Color";
2030 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_COLOR) ? "T" : "F";
2032 - keyvalue[i ][0] = "Duplex";
2033 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_DUPLEX) ? "T" : "F";
2035 - keyvalue[i ][0] = "Staple";
2036 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_STAPLE) ? "T" : "F";
2038 - keyvalue[i ][0] = "Copies";
2039 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_COPIES) ? "T" : "F";
2041 - keyvalue[i ][0] = "Collate";
2042 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_COLLATE) ? "T" : "F";
2044 - keyvalue[i ][0] = "Punch";
2045 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_PUNCH) ? "T" : "F";
2047 - keyvalue[i ][0] = "Bind";
2048 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_BIND) ? "T" : "F";
2050 - keyvalue[i ][0] = "Sort";
2051 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_SORT) ? "T" : "F";
2053 - keyvalue[i ][0] = "Scan";
2054 - keyvalue[i++][1] = (p->type & CUPS_PRINTER_MFP) ? "T" : "F";
2056 - keyvalue[i ][0] = "pdl";
2057 - keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
2059 - if (get_auth_info_required(p, air_str, sizeof(air_str)))
2061 - keyvalue[i ][0] = "air";
2062 - keyvalue[i++][1] = air_str;
2066 - * Then pack them into a proper txt record...
2069 - return (dnssdPackTxtRecord(txt_len, keyvalue, i));
2073 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2075 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2077 @@ -2479,6 +2402,10 @@ dnssdDeregisterPrinter(
2079 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
2086 * Closing the socket deregisters the service
2088 @@ -2514,6 +2441,23 @@ dnssdDeregisterPrinter(
2089 free(p->printer_txt);
2090 p->printer_txt = NULL;
2092 +#endif /* HAVE_DNSSD */
2094 + if (p->avahi_group)
2096 + avahi_entry_group_reset (p->avahi_group);
2097 + avahi_entry_group_free (p->avahi_group);
2098 + p->avahi_group = NULL;
2101 + avahi_string_list_free (p->ipp_txt);
2103 + if (p->printer_txt)
2104 + avahi_string_list_free (p->printer_txt);
2106 + p->ipp_txt = p->printer_txt = NULL;
2108 +#endif /* HAVE_AVAHI */
2111 * Remove the printer from the array of DNS-SD printers, then clear the
2112 @@ -2526,140 +2470,53 @@ dnssdDeregisterPrinter(
2116 - * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2117 - * TXT record format.
2118 + * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2119 + * or update the broadcast contents.
2122 -static char * /* O - TXT record */
2123 -dnssdPackTxtRecord(int *txt_len, /* O - TXT record length */
2124 - char *keyvalue[][2], /* I - Table of key value pairs */
2125 - int count) /* I - Items in table */
2127 +dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
2129 - int i; /* Looping var */
2130 - int length; /* Length of TXT record */
2131 - int length2; /* Length of value */
2132 - char *txtRecord; /* TXT record buffer */
2133 - char *cursor; /* Looping pointer */
2135 + DNSServiceErrorType se; /* dnssd errors */
2136 + char *ipp_txt, /* IPP TXT record buffer */
2137 + *printer_txt, /* LPD TXT record buffer */
2138 + *nameptr; /* Pointer into name */
2139 + int ipp_len, /* IPP TXT record length */
2140 + printer_len; /* LPD TXT record length */
2141 +#endif /* HAVE_DNSSD */
2143 + int ret; /* Error code */
2144 + AvahiStringList *ipp_txt, /* IPP TXT record list */
2145 + *printer_txt; /* LPD TXT record buffer */
2146 +#endif /* HAVE_AVAHI */
2147 + char name[1024]; /* Service name */
2148 + const char *regtype; /* Registration type */
2152 - * Calculate the buffer size
2158 - for (length = i = 0; i < count; i++)
2159 - length += 1 + strlen(keyvalue[i][0]) +
2160 - (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0);
2161 + cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2162 + !p->ipp_ref ? "new" : "update");
2164 +#endif /* HAVE_DNSSD */
2166 + cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2167 + !p->avahi_group ? "new" : "update");
2168 +#endif /* HAVE_AVAHI */
2171 - * Allocate and fill it
2172 + * If per-printer sharing was just disabled make sure we're not
2173 + * registered before returning.
2176 - txtRecord = malloc(length);
2180 - *txt_len = length;
2182 - for (cursor = txtRecord, i = 0; i < count; i++)
2185 - * Drop in the p-string style length byte followed by the data
2188 - length = strlen(keyvalue[i][0]);
2189 - length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0;
2191 - *cursor++ = (unsigned char)(length + length2);
2193 - memcpy(cursor, keyvalue[i][0], length);
2200 - memcpy(cursor, keyvalue[i][1], length2);
2201 - cursor += length2;
2206 - return (txtRecord);
2211 - * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2215 -dnssdRegisterCallback(
2216 - DNSServiceRef sdRef, /* I - DNS Service reference */
2217 - DNSServiceFlags flags, /* I - Reserved for future use */
2218 - DNSServiceErrorType errorCode, /* I - Error code */
2219 - const char *name, /* I - Service name */
2220 - const char *regtype, /* I - Service type */
2221 - const char *domain, /* I - Domain. ".local" for now */
2222 - void *context) /* I - User-defined context */
2224 - cupsd_printer_t *p = (cupsd_printer_t *)context;
2225 - /* Current printer */
2228 - cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2229 - name, regtype, p ? p->name : "Web Interface",
2230 - p ? (p->reg_name ? p->reg_name : "(null)") : "NA");
2234 - cupsdLogMessage(CUPSD_LOG_ERROR,
2235 - "DNSServiceRegister failed with error %d", (int)errorCode);
2238 - else if (p && (!p->reg_name || strcasecmp(name, p->reg_name)))
2240 - cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"",
2243 - cupsArrayRemove(DNSSDPrinters, p);
2244 - cupsdSetString(&p->reg_name, name);
2245 - cupsArrayAdd(DNSSDPrinters, p);
2247 - LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
2253 - * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2254 - * or update the broadcast contents.
2258 -dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
2260 - DNSServiceErrorType se; /* dnssd errors */
2261 - char *ipp_txt, /* IPP TXT record buffer */
2262 - *printer_txt, /* LPD TXT record buffer */
2263 - name[1024], /* Service name */
2264 - *nameptr; /* Pointer into name */
2265 - int ipp_len, /* IPP TXT record length */
2266 - printer_len; /* LPD TXT record length */
2267 - const char *regtype; /* Registration type */
2270 - cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2271 - !p->ipp_ref ? "new" : "update");
2274 - * If per-printer sharing was just disabled make sure we're not
2275 - * registered before returning.
2280 - dnssdDeregisterPrinter(p);
2283 + dnssdDeregisterPrinter(p);
2288 * The registered name takes the form of "<printer-info> @ <computer name>"...
2289 @@ -2694,6 +2551,7 @@ dnssdRegisterPrinter(cupsd_printer_t *p)
2290 * Register IPP and (optionally) LPD...
2294 ipp_len = 0; /* anti-compiler-warning-code */
2295 ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
2297 @@ -2860,6 +2718,140 @@ dnssdRegisterPrinter(cupsd_printer_t *p)
2301 +#endif /* HAVE_DNSSD */
2303 + if (!AvahiCupsClient)
2305 + * Client not running yet. The client callback will call us again later.
2309 + ipp_txt = dnssdBuildTxtRecord(NULL, p, 0);
2310 + printer_txt = dnssdBuildTxtRecord(NULL, p, 1);
2311 + regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" : "_ipp._tcp";
2313 + if (p->avahi_group && p->ipp_txt && ipp_txt &&
2314 + !avahi_string_list_equal (p->ipp_txt, ipp_txt))
2317 + * Update the existing registration...
2320 + avahi_string_list_free (p->ipp_txt);
2322 + if (p->printer_txt)
2323 + avahi_string_list_free (p->printer_txt);
2325 + ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
2327 + AVAHI_PROTO_UNSPEC,
2328 + 0, name, regtype, NULL,
2331 + goto update_failed;
2333 + p->ipp_txt = ipp_txt;
2336 + if (BrowseLocalProtocols & BROWSE_LPD)
2338 + ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
2340 + AVAHI_PROTO_UNSPEC,
2342 + "_printer._tcp", NULL,
2346 + goto update_failed;
2348 + p->printer_txt = printer_txt;
2349 + printer_txt = NULL;
2352 + ret = avahi_entry_group_commit (p->avahi_group);
2356 + cupsdLogMessage (CUPSD_LOG_ERROR,
2357 + "Failed to update TXT record for %s: %d",
2359 + avahi_entry_group_reset (p->avahi_group);
2360 + avahi_entry_group_free (p->avahi_group);
2361 + p->avahi_group = NULL;
2362 + ipp_txt = p->ipp_txt;
2363 + p->ipp_txt = NULL;
2367 + if (!p->avahi_group)
2370 + * Initial registration. Use the _fax subtype for fax queues...
2373 + p->avahi_group = avahi_entry_group_new (AvahiCupsClient,
2374 + avahi_entry_group_cb,
2377 + cupsdLogMessage(CUPSD_LOG_DEBUG,
2378 + "Registering Avahi printer %s with name \"%s\" and "
2379 + "type \"%s\"", p->name, name, regtype);
2381 + ret = avahi_entry_group_add_service_strlst (p->avahi_group,
2383 + AVAHI_PROTO_UNSPEC,
2384 + 0, name, regtype, NULL, NULL,
2390 + p->ipp_txt = ipp_txt;
2393 + if (BrowseLocalProtocols & BROWSE_LPD)
2395 + cupsdLogMessage(CUPSD_LOG_DEBUG,
2396 + "Registering Avahi printer %s with name \"%s\" and "
2397 + "type \"_printer._tcp\"", p->name, name);
2399 + ret = avahi_entry_group_add_service_strlst (p->avahi_group,
2401 + AVAHI_PROTO_UNSPEC,
2403 + "_printer._tcp", NULL, NULL,
2409 + p->printer_txt = printer_txt;
2410 + printer_txt = NULL;
2413 + ret = avahi_entry_group_commit (p->avahi_group);
2418 + cupsdLogMessage (CUPSD_LOG_ERROR,
2419 + "Failed to add Avahi entry for %s: %d",
2421 + avahi_entry_group_reset (p->avahi_group);
2422 + avahi_entry_group_free (p->avahi_group);
2423 + p->avahi_group = NULL;
2424 + ipp_txt = p->ipp_txt;
2425 + p->ipp_txt = NULL;
2430 + avahi_string_list_free (ipp_txt);
2433 + avahi_string_list_free (printer_txt);
2434 +#endif /* HAVE_AVAHI */
2438 @@ -2872,6 +2864,10 @@ dnssdStop(void)
2440 cupsd_printer_t *p; /* Current printer */
2445 +#endif /* HAVE_DNSSD */
2448 * De-register the individual printers
2449 @@ -2882,6 +2878,7 @@ dnssdStop(void)
2450 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2451 dnssdDeregisterPrinter(p);
2455 * Shutdown the rest of the service refs...
2457 @@ -2902,6 +2899,7 @@ dnssdStop(void)
2459 DNSServiceRefDeallocate(DNSSDRef);
2461 +#endif /* HAVE_DNSSD */
2463 cupsArrayDelete(DNSSDPrinters);
2464 DNSSDPrinters = NULL;
2465 @@ -2911,6 +2909,272 @@ dnssdStop(void)
2469 + * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2472 +static cupsd_txt_record_t /* O - TXT record */
2473 +dnssdBuildTxtRecord(
2474 + int *txt_len, /* O - TXT record length */
2475 + cupsd_printer_t *p, /* I - Printer information */
2476 + int for_lpd) /* I - 1 = LPD, 0 = IPP */
2478 + int i; /* Looping var */
2479 + char adminurl_str[256], /* URL for th admin page */
2480 + type_str[32], /* Type to string buffer */
2481 + state_str[32], /* State to string buffer */
2482 + rp_str[1024], /* Queue name string buffer */
2483 + air_str[1024], /* auth-info-required string buffer */
2484 + *keyvalue[32][2]; /* Table of key/value pairs */
2488 + * Load up the key value pairs...
2493 + keyvalue[i ][0] = "txtvers";
2494 + keyvalue[i++][1] = "1";
2496 + keyvalue[i ][0] = "qtotal";
2497 + keyvalue[i++][1] = "1";
2499 + keyvalue[i ][0] = "rp";
2500 + keyvalue[i++][1] = rp_str;
2502 + strlcpy(rp_str, p->name, sizeof(rp_str));
2504 + snprintf(rp_str, sizeof(rp_str), "%s/%s",
2505 + (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
2507 + keyvalue[i ][0] = "ty";
2508 + keyvalue[i++][1] = p->make_model ? p->make_model : "Unknown";
2510 + httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
2511 + "http", NULL, DNSSDHostName, DNSSDPort, "/%s/%s",
2512 + (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
2514 + keyvalue[i ][0] = "adminurl";
2515 + keyvalue[i++][1] = adminurl_str;
2517 + keyvalue[i ][0] = "note";
2518 + keyvalue[i++][1] = p->location ? p->location : "";
2520 + keyvalue[i ][0] = "priority";
2521 + keyvalue[i++][1] = for_lpd ? "100" : "0";
2523 + keyvalue[i ][0] = "product";
2524 + keyvalue[i++][1] = p->product ? p->product : "Unknown";
2526 + snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
2527 + snprintf(state_str, sizeof(state_str), "%d", p->state);
2529 + keyvalue[i ][0] = "printer-state";
2530 + keyvalue[i++][1] = state_str;
2532 + keyvalue[i ][0] = "printer-type";
2533 + keyvalue[i++][1] = type_str;
2535 + keyvalue[i ][0] = "Transparent";
2536 + keyvalue[i++][1] = "T";
2538 + keyvalue[i ][0] = "Binary";
2539 + keyvalue[i++][1] = "T";
2541 + keyvalue[i ][0] = "Fax";
2542 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_FAX) ? "T" : "F";
2544 + keyvalue[i ][0] = "Color";
2545 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_COLOR) ? "T" : "F";
2547 + keyvalue[i ][0] = "Duplex";
2548 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_DUPLEX) ? "T" : "F";
2550 + keyvalue[i ][0] = "Staple";
2551 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_STAPLE) ? "T" : "F";
2553 + keyvalue[i ][0] = "Copies";
2554 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_COPIES) ? "T" : "F";
2556 + keyvalue[i ][0] = "Collate";
2557 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_COLLATE) ? "T" : "F";
2559 + keyvalue[i ][0] = "Punch";
2560 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_PUNCH) ? "T" : "F";
2562 + keyvalue[i ][0] = "Bind";
2563 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_BIND) ? "T" : "F";
2565 + keyvalue[i ][0] = "Sort";
2566 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_SORT) ? "T" : "F";
2568 + keyvalue[i ][0] = "Scan";
2569 + keyvalue[i++][1] = (p->type & CUPS_PRINTER_MFP) ? "T" : "F";
2571 + keyvalue[i ][0] = "pdl";
2572 + keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
2574 + if (get_auth_info_required(p, air_str, sizeof(air_str)))
2576 + keyvalue[i ][0] = "air";
2577 + keyvalue[i++][1] = air_str;
2581 + * Then pack them into a proper txt record...
2585 + return (dnssdPackTxtRecord(txt_len, keyvalue, i));
2586 +#endif /* HAVE_DNSSD */
2588 + return (avahiPackTxtRecord(keyvalue, i));
2589 +#endif /* HAVE_AVAHI */
2591 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2595 +# ifdef HAVE_COREFOUNDATION
2597 + * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2601 +dnssdAddAlias(const void *key, /* I - Key */
2602 + const void *value, /* I - Value (domain) */
2603 + void *context) /* I - Unused */
2605 + char valueStr[1024], /* Domain string */
2606 + hostname[1024]; /* Complete hostname */
2611 + if (CFGetTypeID((CFStringRef)value) == CFStringGetTypeID() &&
2612 + CFStringGetCString((CFStringRef)value, valueStr, sizeof(valueStr),
2613 + kCFStringEncodingUTF8))
2615 + snprintf(hostname, sizeof(hostname), "%s.%s", DNSSDHostName, valueStr);
2617 + DNSSDAlias = cupsArrayNew(NULL, NULL);
2619 + cupsdAddAlias(DNSSDAlias, hostname);
2620 + cupsdLogMessage(CUPSD_LOG_DEBUG, "Added Back to My Mac ServerAlias %s",
2624 + cupsdLogMessage(CUPSD_LOG_ERROR,
2625 + "Bad Back to My Mac domain in dynamic store!");
2627 +# endif /* HAVE_COREFOUNDATION */
2631 + * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2632 + * TXT record format.
2635 +static char * /* O - TXT record */
2636 +dnssdPackTxtRecord(int *txt_len, /* O - TXT record length */
2637 + char *keyvalue[][2], /* I - Table of key value pairs */
2638 + int count) /* I - Items in table */
2640 + int i; /* Looping var */
2641 + int length; /* Length of TXT record */
2642 + int length2; /* Length of value */
2643 + char *txtRecord; /* TXT record buffer */
2644 + char *cursor; /* Looping pointer */
2648 + * Calculate the buffer size
2651 + for (length = i = 0; i < count; i++)
2652 + length += 1 + strlen(keyvalue[i][0]) +
2653 + (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0);
2656 + * Allocate and fill it
2659 + txtRecord = malloc(length);
2662 + *txt_len = length;
2664 + for (cursor = txtRecord, i = 0; i < count; i++)
2667 + * Drop in the p-string style length byte followed by the data
2670 + length = strlen(keyvalue[i][0]);
2671 + length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0;
2673 + *cursor++ = (unsigned char)(length + length2);
2675 + memcpy(cursor, keyvalue[i][0], length);
2682 + memcpy(cursor, keyvalue[i][1], length2);
2683 + cursor += length2;
2688 + return (txtRecord);
2693 + * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2697 +dnssdRegisterCallback(
2698 + DNSServiceRef sdRef, /* I - DNS Service reference */
2699 + DNSServiceFlags flags, /* I - Reserved for future use */
2700 + DNSServiceErrorType errorCode, /* I - Error code */
2701 + const char *name, /* I - Service name */
2702 + const char *regtype, /* I - Service type */
2703 + const char *domain, /* I - Domain. ".local" for now */
2704 + void *context) /* I - User-defined context */
2706 + cupsd_printer_t *p = (cupsd_printer_t *)context;
2707 + /* Current printer */
2710 + cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2711 + name, regtype, p ? p->name : "Web Interface",
2712 + p ? (p->reg_name ? p->reg_name : "(null)") : "NA");
2716 + cupsdLogMessage(CUPSD_LOG_ERROR,
2717 + "DNSServiceRegister failed with error %d", (int)errorCode);
2720 + else if (p && (!p->reg_name || strcasecmp(name, p->reg_name)))
2722 + cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"",
2725 + cupsArrayRemove(DNSSDPrinters, p);
2726 + cupsdSetString(&p->reg_name, name);
2727 + cupsArrayAdd(DNSSDPrinters, p);
2729 + LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
2735 * 'dnssdUpdate()' - Handle DNS-SD queries.
2738 @@ -2931,6 +3195,126 @@ dnssdUpdate(void)
2739 #endif /* HAVE_DNSSD */
2744 + * 'avahiPackTxtRecord()' - Pack an array of key/value pairs into an
2745 + * AvahiStringList.
2748 +static AvahiStringList * /* O - new string list */
2749 +avahiPackTxtRecord(char *keyvalue[][2], /* I - Table of key value pairs */
2750 + int count) /* I - Items in table */
2752 + AvahiStringList *strlst = NULL;
2757 + elements = malloc ((1 + count) * sizeof (char *));
2761 + for (i = 0; i < count; i++)
2763 + len = (1 + strlen (keyvalue[i][0]) +
2764 + (keyvalue[i][1] ? 1 + strlen (keyvalue[i][1]) : 1));
2765 + elements[i] = malloc (len * sizeof (char));
2769 + snprintf (elements[i], len, "%s=%s", keyvalue[i][0], keyvalue[i][1]);
2772 + strlst = avahi_string_list_new_from_array ((const char **) elements, count);
2776 + free (elements[i]);
2784 + * 'avahi_entry_group_cb()' - Avahi entry group callback function.
2787 +avahi_entry_group_cb (AvahiEntryGroup *group,
2788 + AvahiEntryGroupState state,
2794 + name = ((cupsd_printer_t *) userdata)->reg_name;
2796 + name = "CUPS web interface";
2800 + case AVAHI_ENTRY_GROUP_UNCOMMITED:
2801 + case AVAHI_ENTRY_GROUP_REGISTERING:
2804 + case AVAHI_ENTRY_GROUP_ESTABLISHED:
2805 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2806 + "Avahi entry group established for %s", name);
2810 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2811 + "Avahi entry group %s has state %d",
2818 + * 'avahi_client_cb()' - Avahi client callback function.
2821 +avahi_client_cb (AvahiClient *client,
2822 + AvahiClientState state,
2825 + cupsd_printer_t *printer;
2828 + case AVAHI_CLIENT_S_RUNNING:
2830 + * Avahi client started successfully.
2832 + AvahiCupsClient = client;
2833 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client started");
2835 + cupsdUpdateDNSSDName ();
2837 + for (printer = (cupsd_printer_t *)cupsArrayFirst(DNSSDPrinters);
2839 + printer = (cupsd_printer_t *)cupsArrayNext(DNSSDPrinters))
2841 + if (!printer->avahi_group)
2842 + dnssdRegisterPrinter (printer);
2847 + case AVAHI_CLIENT_CONNECTING:
2848 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client connecting");
2851 + case AVAHI_CLIENT_FAILURE:
2852 + cupsdLogMessage (CUPSD_LOG_ERROR, "Avahi client failed");
2856 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client state: %d", state);
2859 +#endif /* HAVE_AVAHI */
2863 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2865 diff -up cups-1.4.5/scheduler/dirsvc.h.avahi cups-1.4.5/scheduler/dirsvc.h
2866 --- cups-1.4.5/scheduler/dirsvc.h.avahi 2009-05-14 18:54:37.000000000 +0100
2867 +++ cups-1.4.5/scheduler/dirsvc.h 2010-12-24 13:11:38.344340724 +0000
2869 # endif /* HAVE_LDAP_SSL_H */
2870 #endif /* HAVE_LDAP */
2873 +# include <avahi-client/publish.h>
2874 +#endif /* HAVE_AVAHI */
2877 * Browse protocols...
2879 @@ -132,17 +136,20 @@ VAR int PollPipe VALUE(0);
2880 VAR cupsd_statbuf_t *PollStatusBuffer VALUE(NULL);
2881 /* Status buffer for pollers */
2884 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2885 +VAR int DNSSDPort VALUE(0);
2886 + /* Port number to register */
2887 VAR char *DNSSDComputerName VALUE(NULL),
2888 /* Computer/server name */
2889 *DNSSDHostName VALUE(NULL);
2891 -VAR cups_array_t *DNSSDAlias VALUE(NULL);
2892 - /* List of dynamic ServerAlias's */
2893 -VAR int DNSSDPort VALUE(0);
2894 - /* Port number to register */
2895 VAR cups_array_t *DNSSDPrinters VALUE(NULL);
2896 /* Printers we have registered */
2897 +#endif /* HAVE_DNSSD || HAVE_AVAHI */
2900 +VAR cups_array_t *DNSSDAlias VALUE(NULL);
2901 + /* List of dynamic ServerAlias's */
2902 VAR DNSServiceRef DNSSDRef VALUE(NULL),
2903 /* Master DNS-SD service reference */
2904 WebIFRef VALUE(NULL),
2905 @@ -151,6 +158,15 @@ VAR DNSServiceRef DNSSDRef VALUE(NULL),
2906 /* Remote printer browse reference */
2907 #endif /* HAVE_DNSSD */
2910 +VAR AvahiCupsPoll *AvahiCupsPollHandle VALUE(NULL);
2911 + /* AvahiCupsPoll object */
2912 +VAR AvahiClient *AvahiCupsClient VALUE(NULL);
2913 + /* AvahiClient object */
2914 +VAR AvahiEntryGroup *AvahiWebIFGroup VALUE(NULL);
2915 + /* Web interface entry group */
2916 +#endif /* HAVE_AVAHI */
2919 VAR SLPHandle BrowseSLPHandle VALUE(NULL);
2920 /* SLP API handle */
2921 @@ -198,9 +214,9 @@ extern void cupsdStartBrowsing(void);
2922 extern void cupsdStartPolling(void);
2923 extern void cupsdStopBrowsing(void);
2924 extern void cupsdStopPolling(void);
2926 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2927 extern void cupsdUpdateDNSSDName(void);
2928 -#endif /* HAVE_DNSSD */
2929 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2931 extern void cupsdUpdateLDAPBrowse(void);
2932 #endif /* HAVE_LDAP */
2933 diff -up cups-1.4.5/scheduler/main.c.avahi cups-1.4.5/scheduler/main.c
2934 --- cups-1.4.5/scheduler/main.c.avahi 2010-12-24 13:11:33.633464718 +0000
2935 +++ cups-1.4.5/scheduler/main.c 2010-12-24 13:11:38.348340619 +0000
2936 @@ -161,6 +161,10 @@ main(int argc, /* I - Number of comm
2937 int launchd_idle_exit;
2938 /* Idle exit on select timeout? */
2939 #endif /* HAVE_LAUNCHD */
2941 + cupsd_timeout_t *tmo; /* Next scheduled timed callback */
2942 + long tmo_delay; /* Time before it must be called */
2943 +#endif /* HAVE_AVAHI */
2947 @@ -561,6 +565,14 @@ main(int argc, /* I - Number of comm
2953 + * Initialize timed callback structures.
2956 + cupsdInitTimeouts();
2957 +#endif /* HAVE_AVAHI */
2962 @@ -900,6 +912,16 @@ main(int argc, /* I - Number of comm
2964 #endif /* __APPLE__ */
2968 + * If a timed callback is due, run it.
2971 + tmo = cupsdNextTimeout (&tmo_delay);
2972 + if (tmo && tmo_delay == 0)
2973 + cupsdRunTimeout (tmo);
2974 +#endif /* HAVE_AVAHI */
2978 * Update the network interfaces once a minute...
2979 @@ -1925,6 +1947,10 @@ select_timeout(int fds) /* I - Number
2980 cupsd_job_t *job; /* Job information */
2981 cupsd_subscription_t *sub; /* Subscription information */
2982 const char *why; /* Debugging aid */
2984 + cupsd_timeout_t *tmo; /* Timed callback */
2985 + long tmo_delay; /* Seconds before calling it */
2986 +#endif /* HAVE_AVAHI */
2990 @@ -1967,6 +1993,19 @@ select_timeout(int fds) /* I - Number
2992 #endif /* __APPLE__ */
2996 + * See if there are any scheduled timed callbacks to run.
2999 + tmo = cupsdNextTimeout (&tmo_delay);
3002 + timeout = tmo_delay;
3003 + why = "run a timed callback";
3005 +#endif /* HAVE_AVAHI */
3008 * Check whether we are accepting new connections...
3010 diff -up cups-1.4.5/scheduler/Makefile.avahi cups-1.4.5/scheduler/Makefile
3011 --- cups-1.4.5/scheduler/Makefile.avahi 2010-12-24 13:11:33.739461928 +0000
3012 +++ cups-1.4.5/scheduler/Makefile 2010-12-24 13:11:38.332341040 +0000
3013 @@ -17,6 +17,7 @@ include ../Makedefs
3021 @@ -39,7 +40,8 @@ CUPSDOBJS = \
3031 diff -up cups-1.4.5/scheduler/printers.c.avahi cups-1.4.5/scheduler/printers.c
3032 --- cups-1.4.5/scheduler/printers.c.avahi 2010-12-24 13:11:33.784460744 +0000
3033 +++ cups-1.4.5/scheduler/printers.c 2010-12-24 13:11:38.356340409 +0000
3034 @@ -929,10 +929,10 @@ cupsdDeletePrinter(
3035 cupsdClearString(&p->alert);
3036 cupsdClearString(&p->alert_description);
3039 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3040 cupsdClearString(&p->product);
3041 cupsdClearString(&p->pdl);
3042 -#endif /* HAVE_DNSSD */
3043 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3045 cupsArrayDelete(p->filetypes);
3047 @@ -1301,9 +1301,9 @@ cupsdLoadAllPrinters(void)
3052 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3053 p->product = _cupsStrAlloc(value);
3054 -#endif /* HAVE_DNSSD */
3055 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3058 cupsdLogMessage(CUPSD_LOG_ERROR,
3059 @@ -1717,10 +1717,10 @@ cupsdSaveAllPrinters(void)
3061 cupsFilePrintf(fp, "Type %d\n", printer->type);
3064 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3065 if (printer->product)
3066 cupsFilePutConf(fp, "Product", printer->product);
3067 -#endif /* HAVE_DNSSD */
3068 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3070 for (ptr = (char *)cupsArrayFirst(printer->filters);
3072 @@ -3860,7 +3860,7 @@ add_printer_formats(cupsd_printer_t *p)
3073 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3077 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3079 char pdl[1024]; /* Buffer to build pdl list */
3080 mime_filter_t *filter; /* MIME filter looping var */
3081 @@ -3914,7 +3914,7 @@ add_printer_formats(cupsd_printer_t *p)
3083 cupsdSetString(&p->pdl, pdl);
3085 -#endif /* HAVE_DNSSD */
3086 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3090 @@ -4951,9 +4951,9 @@ load_ppd(cupsd_printer_t *p) /* I - Pri
3091 attr->values[i].string.text = _cupsStrAlloc("bcp");
3095 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3096 cupsdSetString(&p->product, ppd->product);
3097 -#endif /* HAVE_DNSSD */
3098 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3100 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
3101 p->type |= CUPS_PRINTER_REMOTE;
3102 diff -up cups-1.4.5/scheduler/printers.h.avahi cups-1.4.5/scheduler/printers.h
3103 --- cups-1.4.5/scheduler/printers.h.avahi 2010-03-30 23:07:33.000000000 +0100
3104 +++ cups-1.4.5/scheduler/printers.h 2010-12-24 13:11:38.357340382 +0000
3107 # include <dns_sd.h>
3108 #endif /* HAVE_DNSSD */
3110 +# include "avahi.h"
3111 +#endif /* HAVE_AVAHI */
3112 #include <cups/pwg-private.h>
3115 @@ -99,17 +102,24 @@ typedef struct cupsd_printer_s
3116 char *recoverable; /* com.apple.print.recoverable-message */
3117 _pwg_t *pwg; /* PWG<->PPD mapping data */
3119 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3120 + char *reg_name; /* Name used for service registration */
3121 + char *product, /* PPD Product string */
3122 + *pdl; /* pdl value for TXT record */
3123 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
3125 - char *reg_name, /* Name used for service registration */
3126 - *product, /* PPD Product string */
3127 - *pdl, /* pdl value for TXT record */
3128 - *ipp_txt, /* IPP TXT record contents */
3129 + char *ipp_txt, /* IPP TXT record contents */
3130 *printer_txt; /* LPD TXT record contents */
3131 int ipp_len, /* IPP TXT record length */
3132 printer_len; /* LPD TXT record length */
3133 DNSServiceRef ipp_ref, /* Reference for _ipp._tcp,_cups */
3134 printer_ref; /* Reference for _printer._tcp */
3135 #endif /* HAVE_DNSSD */
3137 + AvahiStringList *ipp_txt, /* IPP TXT record */
3138 + *printer_txt; /* LPD TXT record */
3139 + AvahiEntryGroup *avahi_group; /* Avahi entry group */
3140 +#endif /* HAVE_AVAHI */
3144 diff -up cups-1.4.5/scheduler/timeout.c.avahi cups-1.4.5/scheduler/timeout.c
3145 --- cups-1.4.5/scheduler/timeout.c.avahi 2010-12-24 13:11:38.358340356 +0000
3146 +++ cups-1.4.5/scheduler/timeout.c 2010-12-24 13:11:38.358340356 +0000
3151 + * Timeout functions for the Common UNIX Printing System (CUPS).
3153 + * Copyright (C) 2010 Red Hat, Inc.
3155 + * Tim Waugh <twaugh@redhat.com>
3157 + * Distribution and use rights are outlined in the file "LICENSE.txt"
3158 + * which should have been included with this file. If this file is
3159 + * file is missing or damaged, see the license at "http://www.cups.org/".
3163 + * cupsdInitTimeouts() - Initialise timeout structure.
3164 + * cupsdAddTimeout() - Add a timed callback.
3165 + * cupsdNextTimeout() - Find the next enabled timed callback.
3166 + * cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
3167 + * cupsdRemoveTimeout() - Discard a timed callback.
3168 + * compare_timeouts() - Compare timed callbacks for array sorting.
3171 +#include <config.h>
3173 +#ifdef HAVE_AVAHI /* Applies to entire file... */
3176 + * Include necessary headers...
3181 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
3182 +# include <malloc.h>
3183 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
3186 +# include <avahi-common/timeval.h>
3187 +#endif /* HAVE_AVAHI */
3190 +struct _cupsd_timeout_s
3192 + struct timeval when;
3194 + cupsd_timeoutfunc_t callback;
3199 + * Local functions...
3203 + * 'compare_timeouts()' - Compare timed callbacks for array sorting.
3207 +compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
3209 + if (!p0->enabled || !p1->enabled)
3211 + if (!p0->enabled && !p1->enabled)
3214 + return (p0->enabled ? -1 : 1);
3217 + return (avahi_timeval_compare (&p0->when, &p1->when));
3222 + * 'cupsdInitTimeouts()' - Initialise timeout structures.
3226 +cupsdInitTimeouts(void)
3228 + Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
3233 + * 'cupsdAddTimeout()' - Add a timed callback.
3236 +cupsd_timeout_t * /* O - Timeout handle */
3237 +cupsdAddTimeout(const struct timeval *tv, /* I - Absolute time */
3238 + cupsd_timeoutfunc_t cb, /* I - Callback function */
3239 + void *data) /* I - User data */
3241 + cupsd_timeout_t *timeout;
3243 + timeout = malloc (sizeof(cupsd_timeout_t));
3244 + if (timeout != NULL)
3246 + timeout->enabled = (tv != NULL);
3249 + timeout->when.tv_sec = tv->tv_sec;
3250 + timeout->when.tv_usec = tv->tv_usec;
3253 + timeout->callback = cb;
3254 + timeout->data = data;
3255 + cupsArrayAdd (Timeouts, timeout);
3263 + * 'cupsdNextTimeout()' - Find the next enabled timed callback.
3266 +cupsd_timeout_t * /* O - Next enabled timeout or NULL */
3267 +cupsdNextTimeout(long *delay) /* O - Seconds before scheduled */
3269 + cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
3270 + struct timeval curtime;
3272 + if (first && !first->enabled)
3275 + if (first && delay)
3277 + gettimeofday (&curtime, NULL);
3278 + if (avahi_timeval_compare (&curtime, &first->when) > 0)
3282 + *delay = 1 + first->when.tv_sec - curtime.tv_sec;
3283 + if (first->when.tv_usec < curtime.tv_usec)
3293 + * 'cupsdRunTimeout()' - Run a timed callback.
3297 +cupsdRunTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
3299 + timeout->enabled = 0;
3300 + timeout->callback (timeout, timeout->data);
3304 + * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
3308 +cupsdUpdateTimeout(cupsd_timeout_t *timeout, /* I - Timeout */
3309 + const struct timeval *tv) /* I - Absolute time or NULL */
3311 + cupsArrayRemove (Timeouts, timeout);
3312 + timeout->enabled = (tv != NULL);
3315 + timeout->when.tv_sec = tv->tv_sec;
3316 + timeout->when.tv_usec = tv->tv_usec;
3318 + cupsArrayAdd (Timeouts, timeout);
3323 + * 'cupsdRemoveTimeout()' - Discard a timed callback.
3327 +cupsdRemoveTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
3329 + cupsArrayRemove (Timeouts, timeout);
3334 +#endif /* HAVE_AVAHI ... from top of file */