updated on Thu Jan 19 20:01:47 UTC 2012
[aur-mirror.git] / cups-usblp / cups-avahi.patch
blob7e9c8ecea66c175b8e42305aaba508aca16cacbd
1 Description: Avahi support
2 Bug: http://www.cups.org/str.php?L3066
3 Author: Tim Waugh
4 Origin: git clone http://twaugh.fedorapeople.org/cups-avahi.git (Aug 11, 2011)
6 --- a/backend/dnssd.c
7 +++ b/backend/dnssd.c
8 @@ -15,14 +15,21 @@
10 * Contents:
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.
30 @@ -33,7 +40,18 @@
32 #include "backend-private.h"
33 #include <cups/array.h>
34 -#include <dns_sd.h>
35 +#ifdef HAVE_DNSSD
36 +# include <dns_sd.h>
37 +#endif /* HAVE_DNSSD */
38 +#ifdef HAVE_AVAHI
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 */
50 @@ -53,7 +71,12 @@
52 typedef struct
54 +#ifdef HAVE_DNSSD
55 DNSServiceRef ref; /* Service reference for resolve */
56 +#endif /* HAVE_DNSSD */
57 +#ifdef HAVE_AVAHI
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 */
63 @@ -65,6 +88,20 @@
64 sent; /* Did we list the device? */
65 } cups_device_t;
67 +typedef struct
69 + char key[256];
70 + char value[256];
72 +#ifdef HAVE_DNSSD
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;
83 * Local globals...
84 @@ -78,6 +115,7 @@
85 * Local functions...
88 +#ifdef HAVE_DNSSD
89 static void browse_callback(DNSServiceRef sdRef,
90 DNSServiceFlags flags,
91 uint32_t interfaceIndex,
92 @@ -93,12 +131,6 @@
93 const char *regtype,
94 const char *replyDomain,
95 void *context);
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,
108 void *context);
109 +#endif /* HAVE_DNSSD */
110 +#ifdef HAVE_AVAHI
111 +static void avahi_client_callback (AvahiClient *client,
112 + AvahiClientState state,
113 + void *context);
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,
122 + void *context);
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);
137 +#ifdef HAVE_AVAHI
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.
145 + */
147 +static cups_txt_records_t *
148 +next_txt_record (cups_txt_records_t *txt)
150 +#ifdef HAVE_DNSSD
151 + txt->data = txt->datanext;
152 +#else /* HAVE_AVAHI */
153 + txt->txt = avahi_string_list_get_next (txt->txt);
154 + if (txt->txt == NULL)
155 + return NULL;
156 +#endif /* HAVE_DNSSD */
158 + return txt;
163 + * 'parse_txt_record_pair()' - Read key/value pair in cups_txt_records_t.
164 + */
166 +static int
167 +parse_txt_record_pair (cups_txt_records_t *txt)
169 +#ifdef HAVE_DNSSD
170 + uint8_t datalen;
171 + uint8_t *data = txt->data;
172 + char *ptr;
174 + /*
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...
178 + */
180 + datalen = *data++;
181 + if (!datalen || (data + datalen) >= txt->dataend)
182 + return NULL;
183 + txt->datanext = data + datalen;
185 + for (ptr = txt->key; data < txt->datanext && *data != '='; data ++)
186 + *ptr++ = *data;
187 + *ptr = '\0';
189 + if (data < txt->datanext && *data == '=')
191 + data++;
193 + if (data < datanext)
194 + memcpy (txt->value, data, txt->datanext - data);
195 + value[txt->datanext - data] = '\0';
197 + else
198 + return 1;
199 +#else /* HAVE_AVAHI */
200 + char *key, *value;
201 + size_t len;
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';
214 + avahi_free (key);
215 + avahi_free (value);
216 +#endif /* HAVE_AVAHI */
218 + return 0;
223 * 'main()' - Browse for printers.
224 @@ -120,6 +261,13 @@
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 */
231 +#ifdef HAVE_DNSSD
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 */
249 +#ifdef HAVE_AVAHI
250 + AvahiClient *client;
251 + int error;
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 */
256 @@ -198,6 +345,49 @@
257 * Browse for different kinds of printers...
260 +#ifdef HAVE_AVAHI
261 + if ((simple_poll = avahi_simple_poll_new ()) == NULL)
263 + perror ("ERROR: Unable to create avahi simple poll object");
264 + return (1);
267 + client = avahi_client_new (avahi_simple_poll_get (simple_poll),
268 + 0, avahi_client_callback, NULL, &error);
269 + if (!client)
271 + perror ("DEBUG: Unable to create avahi client");
272 + return (0);
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",
290 + NULL, 0,
291 + avahi_browse_callback,
292 + devices);
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 */
302 +#ifdef HAVE_DNSSD
303 if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError)
305 perror("ERROR: Unable to create service connection");
306 @@ -258,6 +448,7 @@
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...
314 @@ -265,6 +456,9 @@
316 while (!job_canceled)
318 + int announce = 0;
320 +#ifdef HAVE_DNSSD
321 FD_ZERO(&input);
322 FD_SET(fd, &input);
324 @@ -284,11 +478,35 @@
326 else
328 + announce = 1;
330 +#else /* HAVE_AVAHI */
331 + int r;
332 + avahi_got_callback = 0;
333 + r = avahi_simple_poll_iterate (simple_poll, 1);
334 + if (r != 0 && r != EINTR)
336 + /*
337 + * We've been told to exit the loop. Perhaps the connection to
338 + * avahi failed.
339 + */
341 + break;
344 + if (avahi_got_callback)
345 + announce = 1;
346 +#endif /* HAVE_DNSSD */
348 + if (announce)
351 * Announce any devices we've found...
354 +#ifdef HAVE_DNSSD
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 */
360 @@ -302,6 +520,7 @@
361 if (device->sent)
362 sent ++;
364 +#ifdef HAVE_DNSSD
365 if (device->ref)
366 count ++;
368 @@ -333,14 +552,23 @@
369 count ++;
372 - else if (!device->sent)
373 + else
374 +#endif /* HAVE_DNSSD */
375 +#ifdef HAVE_AVAHI
376 + if (!device->resolved)
377 + continue;
378 + else
379 +#endif /* HAVE_AVAHI */
380 + if (!device->sent)
382 +#ifdef HAVE_DNSSD
384 * Got the TXT records, now report the device...
387 DNSServiceRefDeallocate(device->ref);
388 device->ref = 0;
389 +#endif /* HAVE_DNSSD */
391 if (!best)
392 best = device;
393 @@ -401,6 +629,7 @@
397 +#ifdef HAVE_DNSSD
399 * 'browse_callback()' - Browse devices.
401 @@ -489,6 +718,7 @@
402 device->fullName);
403 device->sent = 1;
405 +#endif /* HAVE_DNSSD */
409 @@ -569,6 +799,41 @@
413 + * 'device_type()' - Get DNS-SD type enumeration from string.
414 + */
416 +static int
417 +device_type (const char *regtype)
419 +#ifdef HAVE_AVAHI
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);
429 +#else
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.
451 @@ -589,20 +854,7 @@
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;
467 - else
468 - key.type = CUPS_DEVICE_RIOUSBPRINT;
469 + key.type = device_type (regtype);
471 for (device = cupsArrayFind(devices, &key);
472 device;
473 @@ -622,8 +874,14 @@
474 free(device->domain);
475 device->domain = strdup(replyDomain);
477 +#ifdef HAVE_DNSSD
478 DNSServiceConstructFullName(fullName, device->name, regtype,
479 replyDomain);
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);
488 @@ -643,6 +901,9 @@
489 device->domain = strdup(replyDomain);
490 device->type = key.type;
491 device->priority = 50;
492 +#ifdef HAVE_AVAHI
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...
502 +#ifdef HAVE_DNSSD
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);
511 return (device);
515 +#ifdef HAVE_DNSSD
517 * 'query_callback()' - Process query data.
519 @@ -680,7 +948,7 @@
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)
530 *ptr = '\0';
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);
545 + txt.data = rdata;
546 + txt.dataend = rdata + rdlen;
547 + device = find_device ((cups_array_t *) context, &txt, &dkey);
548 + if (!device)
549 + fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
551 +#endif /* HAVE_DNSSD */
554 +#ifdef HAVE_AVAHI
556 + * 'avahi_client_callback()' - Avahi client callback function.
557 + */
559 +static void
560 +avahi_client_callback(AvahiClient *client,
561 + AvahiClientState state,
562 + void *context)
564 + /*
565 + * If the connection drops, quit.
566 + */
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.
578 + */
580 +static void
581 +avahi_query_callback(AvahiServiceResolver *resolver,
582 + AvahiIfIndex interface,
583 + AvahiProtocol protocol,
584 + AvahiResolverEvent event,
585 + const char *name,
586 + const char *type,
587 + const char *domain,
588 + const char *host_name,
589 + const AvahiAddress *address,
590 + uint16_t port,
591 + AvahiStringList *txt,
592 + AvahiLookupResultFlags flags,
593 + void *context)
595 + AvahiClient *client;
596 + cups_device_t key,
597 + *device;
598 + char uqname[1024],
599 + *ptr;
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);
612 + return;
615 + /*
616 + * Set search key for device.
617 + */
619 + key.name = uqname;
620 + unquote (uqname, name, sizeof (uqname));
621 + if ((ptr = strstr(name, "._")) != NULL)
622 + *ptr = '\0';
624 + key.domain = (char *) domain;
625 + key.type = device_type (type);
627 + /*
628 + * Find the device and the the TXT information.
629 + */
631 + txtr.txt = txt;
632 + device = find_device ((cups_array_t *) context, &txtr, &key);
633 + if (device)
635 + /*
636 + * Let the main loop know to announce the device.
637 + */
639 + device->resolved = 1;
640 + avahi_got_callback = 1;
642 else
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.
652 + */
654 +static void
655 +avahi_browse_callback(AvahiServiceBrowser *browser,
656 + AvahiIfIndex interface,
657 + AvahiProtocol protocol,
658 + AvahiBrowserEvent event,
659 + const char *name,
660 + const char *type,
661 + const char *domain,
662 + AvahiLookupResultFlags flags,
663 + void *context)
665 + AvahiClient *client = avahi_service_browser_get_client (browser);
667 + switch (event)
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);
673 + return;
675 + case AVAHI_BROWSER_NEW:
676 + /*
677 + * This object is new on the network.
678 + */
680 + if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
682 + /*
683 + * This comes from the local machine so ignore it.
684 + */
686 + fprintf (stderr, "DEBUG: ignoring local service %s\n", name);
688 + else
690 + /*
691 + * Create a device entry for it if it doesn't yet exist.
692 + */
694 + get_device ((cups_array_t *)context, name, type, domain);
696 + /*
697 + * Now look for a TXT entry.
698 + */
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)));
710 + break;
712 + case AVAHI_BROWSER_REMOVE:
713 + case AVAHI_BROWSER_ALL_FOR_NOW:
714 + case AVAHI_BROWSER_CACHE_EXHAUSTED:
715 + break;
718 +#endif /* HAVE_AVAHI */
721 - for (device = cupsArrayFind(devices, &dkey);
723 + * 'find_device()' - Find a device from its name and domain.
724 + */
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;
732 + char *ptr;
734 + for (device = cupsArrayFind(devices, dkey);
735 device;
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))
743 device = NULL;
744 break;
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 */
768 device_id[0] = '\0';
769 make_and_model[0] = '\0';
771 strcpy(model, "Unknown");
773 - for (data = rdata, dataend = data + rdlen;
774 - data < dataend;
775 - data = datanext)
776 + for (;;)
778 - /*
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...
782 - */
784 - datalen = *data++;
786 - if (!datalen || (data + datalen) >= dataend)
787 - break;
789 - datanext = data + datalen;
791 - for (ptr = key; data < datanext && *data != '='; data ++)
792 - *ptr++ = *data;
793 - *ptr = '\0';
794 + char *key;
795 + char *value;
797 - if (data < datanext && *data == '=')
799 - data ++;
801 - if (data < datanext)
802 - memcpy(value, data, datanext - data);
803 - value[datanext - data] = '\0';
804 + if (parse_txt_record_pair (txt))
805 + goto next;
807 - fprintf(stderr, "DEBUG2: query_callback: \"%s=%s\".\n",
808 - key, value);
810 - else
812 - fprintf(stderr, "DEBUG2: query_callback: \"%s\" with no value.\n",
813 - key);
814 - continue;
817 - if (!_cups_strncasecmp(key, "usb_", 4))
818 + key = txt->key;
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)
826 device->sent = 1;
829 + next:
830 + if (next_txt_record (txt) == NULL)
831 + break;
834 if (device->device_id)
835 @@ -905,11 +1316,9 @@
839 - if (!device)
840 - fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
841 + return device;
846 * 'sigterm_handler()' - Handle termination signals...
848 --- a/config-scripts/cups-dnssd.m4
849 +++ b/config-scripts/cups-dnssd.m4
850 @@ -23,6 +23,21 @@
851 DNSSDLIBS=""
852 DNSSD_BACKEND=""
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
858 + AC_MSG_RESULT(yes)
859 + CFLAGS="$CFLAGS `$PKGCONFIG --cflags avahi-client`"
860 + DNSSDLIBS="`$PKGCONFIG --libs avahi-client`"
861 + DNSSD_BACKEND="dnssd"
862 + AC_DEFINE(HAVE_AVAHI)
863 + enable_dnssd=no
864 + else
865 + AC_MSG_RESULT(no)
866 + fi
867 + fi])
869 if test x$enable_dnssd != xno; then
870 AC_CHECK_HEADER(dns_sd.h, [
871 case "$uname" in
872 --- a/config.h.in
873 +++ b/config.h.in
874 @@ -390,6 +390,13 @@
878 + * Do we have Avahi for DNS Service Discovery?
879 + */
881 +#undef HAVE_AVAHI
885 * Do we have <sys/ioctl.h>?
888 --- a/cups/http-support.c
889 +++ b/cups/http-support.c
890 @@ -43,6 +43,10 @@
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.
901 @@ -60,6 +64,11 @@
902 # include <sys/select.h>
903 # endif /* WIN32 */
904 #endif /* HAVE_DNSSD */
905 +#ifdef HAVE_AVAHI
906 +# include <avahi-client/client.h>
907 +# include <avahi-client/lookup.h>
908 +# include <avahi-common/simple-watch.h>
909 +#endif /* HAVE_AVAHI */
913 @@ -127,6 +136,24 @@
914 void *context);
915 #endif /* HAVE_DNSSD */
917 +#ifdef HAVE_AVAHI
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,
925 + const char *name,
926 + const char *type,
927 + const char *domain,
928 + const char *host_name,
929 + const AvahiAddress *address,
930 + uint16_t port,
931 + AvahiStringList *txt,
932 + AvahiLookupResultFlags flags,
933 + void *context);
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 */
945 #ifdef HAVE_DNSSD
946 # ifdef WIN32
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;
955 + int error;
956 + struct
958 + AvahiSimplePoll *poll;
959 + _http_uribuf_t uribuf;
960 + } user_data;
961 +#endif /* HAVE_DNSSD */
964 if (options & _HTTP_RESOLVE_STDERR)
965 fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
966 @@ -1485,9 +1526,16 @@
967 if (domain)
968 *domain++ = '\0';
970 +#ifdef HAVE_DNSSD
971 uribuf.buffer = resolved_uri;
972 uribuf.bufsize = resolved_size;
973 uribuf.options = options;
974 +#else
975 + user_data.uribuf.buffer = resolved_uri;
976 + user_data.uribuf.bufsize = resolved_size;
977 + user_data.uribuf.options = options;
978 +#endif
980 resolved_uri[0] = '\0';
982 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
983 @@ -1501,6 +1549,7 @@
985 uri = NULL;
987 +#ifdef HAVE_DNSSD
988 if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
990 localref = ref;
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);
1011 + /*
1012 + * Collect the result.
1013 + */
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);
1032 -#else
1033 +#else /* HAVE_DNSSD || HAVE_AVAHI */
1035 * No DNS-SD support...
1038 uri = NULL;
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 */
1048 +#ifdef HAVE_AVAHI
1050 + * 'avahi_resolve_uri_client_cb()' - Avahi client callback for resolving URI.
1051 + */
1053 +static void
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));
1061 + /*
1062 + * If the connection drops, quit.
1063 + */
1065 + if (state == AVAHI_CLIENT_FAILURE)
1066 + avahi_simple_poll_quit (simple_poll);
1071 + * 'avahi_resolve_uri_resolver_cb()' - Avahi resolver callback for resolving
1072 + * URI.
1073 + */
1075 +static void
1076 +avahi_resolve_uri_resolver_cb (AvahiServiceResolver *resolver,
1077 + AvahiIfIndex interface,
1078 + AvahiProtocol protocol,
1079 + AvahiResolverEvent event,
1080 + const char *name,
1081 + const char *type,
1082 + const char *domain,
1083 + const char *host_name,
1084 + const AvahiAddress *address,
1085 + uint16_t port,
1086 + AvahiStringList *txt,
1087 + AvahiLookupResultFlags flags,
1088 + void *context)
1090 + const char *scheme; /* URI scheme */
1091 + char rp[256]; /* Remote printer */
1092 + AvahiStringList *pair;
1093 + char *value;
1094 + size_t valueLen = 0;
1095 + char addr[AVAHI_ADDRESS_STR_MAX];
1096 + struct
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);
1113 + return;
1116 + /*
1117 + * Figure out the scheme from the full name...
1118 + */
1120 + if (strstr(type, "_ipp."))
1121 + scheme = "ipp";
1122 + else if (strstr(type, "_printer."))
1123 + scheme = "lpd";
1124 + else if (strstr(type, "_pdl-datastream."))
1125 + scheme = "socket";
1126 + else
1127 + scheme = "riousbprint";
1129 + /*
1130 + * Extract the "remote printer key from the TXT record...
1131 + */
1133 + if ((pair = avahi_string_list_find (txt, "rp")) != NULL)
1135 + avahi_string_list_get_pair (pair, NULL, &value, &valueLen);
1136 + rp[0] = '/';
1137 + memcpy (rp + 1, value, valueLen);
1138 + rp[valueLen + 1] = '\0';
1140 + else
1141 + rp[0] = '\0';
1143 + /*
1144 + * Assemble the final device URI...
1145 + */
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,
1150 + addr, port, rp);
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
1163 @@ -17,6 +17,7 @@
1165 CUPSDOBJS = \
1166 auth.o \
1167 + avahi.o \
1168 banners.o \
1169 cert.o \
1170 classes.o \
1171 @@ -39,7 +40,8 @@
1172 server.o \
1173 statbuf.o \
1174 subscriptions.o \
1175 - sysman.o
1176 + sysman.o \
1177 + timeout.o
1178 LIBOBJS = \
1179 filter.o \
1180 mime.o \
1181 --- /dev/null
1182 +++ b/scheduler/avahi.c
1183 @@ -0,0 +1,445 @@
1185 + * "$Id$"
1187 + * Avahi poll implementation for the CUPS scheduler.
1189 + * Copyright (C) 2010 Red Hat, Inc.
1190 + * Authors:
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/".
1197 + * Contents:
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
1215 + */
1217 +#include <config.h>
1219 +#ifdef HAVE_AVAHI /* Applies to entire file... */
1222 + * Include necessary headers...
1223 + */
1225 +#include "cupsd.h"
1227 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
1228 +# include <malloc.h>
1229 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
1231 +#ifdef HAVE_AVAHI
1232 +# include <avahi-common/timeval.h>
1233 +#endif /* HAVE_AVAHI */
1236 +typedef struct
1238 + AvahiCupsPoll *cups_poll;
1240 + int fd;
1241 + AvahiWatchEvent occurred;
1242 + cups_array_t *watches;
1243 +} cupsd_watched_fd_t;
1245 +struct AvahiWatch
1247 + cupsd_watched_fd_t *watched_fd;
1249 + AvahiWatchEvent events;
1250 + AvahiWatchCallback callback;
1251 + void *userdata;
1254 +struct AvahiTimeout
1256 + AvahiCupsPoll *cups_poll;
1257 + AvahiTimeoutCallback callback;
1258 + void *userdata;
1259 + cupsd_timeout_t *cupsd_timeout;
1263 + * Local functions...
1264 + */
1266 +static AvahiWatch * watch_new(const AvahiPoll *api,
1267 + int fd,
1268 + AvahiWatchEvent events,
1269 + AvahiWatchCallback callback,
1270 + void *userdata);
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,
1276 + AvahiWatch *p1);
1280 + * 'watch_read_cb' - Read callback for file descriptor
1281 + */
1283 +static void
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);
1290 + watch;
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;
1296 + break;
1303 + * 'watch_write_cb' - Write callback for file descriptor
1304 + */
1306 +static void
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);
1313 + watch;
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;
1319 + break;
1326 + * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
1327 + */
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);
1336 + watch;
1337 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
1338 + if (watch->events & (AVAHI_WATCH_IN |
1339 + AVAHI_WATCH_ERR |
1340 + AVAHI_WATCH_HUP)) {
1341 + read_cb = (cupsd_selfunc_t)watch_read_cb;
1342 + if (write_cb != NULL)
1343 + break;
1346 + if (watch->events & AVAHI_WATCH_OUT) {
1347 + write_cb = (cupsd_selfunc_t)watch_write_cb;
1348 + if (read_cb != NULL)
1349 + break;
1353 + if (read_cb || write_cb)
1354 + cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
1355 + else
1356 + cupsdRemoveSelect (watched_fd->fd);
1358 + return (read_cb || write_cb);
1362 + * 'watch_new' - Create a new file descriptor watch
1363 + */
1365 +static AvahiWatch *
1366 +watch_new (const AvahiPoll *api,
1367 + int fd,
1368 + AvahiWatchEvent events,
1369 + AvahiWatchCallback callback,
1370 + void *userdata)
1372 + cupsd_watched_fd_t key, *watched_fd;
1373 + AvahiCupsPoll *cups_poll = api->userdata;
1374 + AvahiWatch *watch = malloc(sizeof(AvahiWatch));
1375 + if (watch == NULL)
1376 + return (NULL);
1378 + watch->events = events;
1379 + watch->callback = callback;
1380 + watch->userdata = userdata;
1382 + key.fd = fd;
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) {
1387 + free (watch);
1388 + return (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,
1395 + NULL);
1398 + watch->watched_fd = watched_fd;
1399 + cupsArrayAdd(watched_fd->watches, watch);
1400 + watched_fd_add_select (watched_fd);
1401 + return (watch);
1406 + * 'watch_free' - Free a file descriptor watch
1407 + */
1409 +static void
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);
1416 + free (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
1428 + */
1430 +static void
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
1441 + */
1443 +static AvahiWatchEvent
1444 +watch_get_events (AvahiWatch *watch)
1446 + return (watch->watched_fd->occurred);
1451 + * 'compare_watches' - Compare watches for array sorting
1452 + */
1454 +static int
1455 +compare_watches (AvahiWatch *p0,
1456 + AvahiWatch *p1)
1458 + if (p0->watched_fd->fd < p1->watched_fd->fd)
1459 + return (-1);
1461 + return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
1466 + * 'timeout_cb()' - Run a timed Avahi callback
1467 + */
1469 +static void
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
1479 + */
1481 +static AvahiTimeout *
1482 +timeout_new (const AvahiPoll *api,
1483 + const struct timeval *tv,
1484 + AvahiTimeoutCallback callback,
1485 + void *userdata)
1487 + AvahiTimeout *timeout;
1488 + AvahiCupsPoll *cups_poll = api->userdata;
1490 + timeout = malloc(sizeof(AvahiTimeout));
1491 + if (timeout == NULL)
1492 + return (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,
1499 + timeout);
1500 + cupsArrayAdd (cups_poll->timeouts, timeout);
1501 + return (timeout);
1506 + * 'timeout_update' - Update the expiration time for a timeout
1507 + */
1509 +static void
1510 +timeout_update (AvahiTimeout *timeout,
1511 + const struct timeval *tv)
1513 + cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
1518 + * ' timeout_free' - Free a timeout
1519 + */
1521 +static void
1522 +timeout_free (AvahiTimeout *timeout)
1524 + cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
1525 + cupsdRemoveTimeout (timeout->cupsd_timeout);
1526 + free (timeout);
1531 + * 'compare_watched_fds' - Compare watched file descriptors for array sorting
1532 + */
1533 +static int
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);
1540 + if (p0 == p1)
1541 + return (0);
1543 + return (p0 < p1 ? -1 : 1);
1548 + * 'compare_timeouts' - Compare timeouts for array sorting
1549 + */
1550 +static int
1551 +compare_timeouts(AvahiTimeout *p0,
1552 + AvahiTimeout *p1)
1554 + /*
1555 + * Just compare pointers to make it a stable sort.
1556 + */
1558 + if (p0->cupsd_timeout < p1->cupsd_timeout)
1559 + return (-1);
1560 + return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
1565 + * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
1566 + */
1568 +AvahiCupsPoll *
1569 +avahi_cups_poll_new (void)
1571 + AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
1572 + if (cups_poll == NULL)
1573 + return (NULL);
1575 + cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
1576 + NULL);
1577 + cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts,
1578 + NULL);
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
1596 + */
1597 +void
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);
1603 + watched_fd;
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
1615 + */
1617 +const AvahiPoll *
1618 +avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
1620 + return (&cups_poll->api);
1624 +#endif /* HAVE_AVAHI ... from top of file */
1627 + * End of "$Id$".
1628 + */
1629 --- /dev/null
1630 +++ b/scheduler/avahi.h
1631 @@ -0,0 +1,49 @@
1633 + * "$Id$"
1635 + * Avahi poll implementation for the CUPS scheduler.
1637 + * Copyright (C) 2010 Red Hat, Inc.
1638 + * Authors:
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/".
1644 + */
1646 +#include <config.h>
1648 +#ifdef HAVE_AVAHI
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 */
1658 +#ifdef HAVE_AVAHI
1659 +typedef struct
1661 + AvahiPoll api;
1662 + cups_array_t *watched_fds;
1663 + cups_array_t *timeouts;
1664 +} AvahiCupsPoll;
1665 +#endif /* HAVE_AVAHI */
1668 + * Prototypes...
1669 + */
1671 +#ifdef 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 */
1679 + * End of "$Id$".
1680 + */
1681 --- a/scheduler/cupsd.h
1682 +++ b/scheduler/cupsd.h
1683 @@ -140,6 +140,15 @@
1685 typedef void (*cupsd_selfunc_t)(void *data);
1687 +#ifdef HAVE_AVAHI
1689 + * Timeout callback function type...
1690 + */
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 */
1698 * Globals...
1699 @@ -173,6 +182,11 @@
1700 /* Running from launchd */
1701 #endif /* HAVE_LAUNCH_H */
1703 +#ifdef HAVE_AVAHI
1704 +VAR cups_array_t *Timeouts; /* Timed callbacks for main loop */
1705 +#endif /* HAVE_AVAHI */
1710 * Prototypes...
1711 @@ -242,6 +256,20 @@
1712 extern void cupsdStartServer(void);
1713 extern void cupsdStopServer(void);
1715 +#ifdef HAVE_AVAHI
1716 +extern void cupsdInitTimeouts(void);
1717 +extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
1718 + cupsd_timeoutfunc_t cb,
1719 + void *data);
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 */
1738 +#ifdef HAVE_AVAHI
1739 + avahi_client_time, /* Time for next Avahi client
1740 + check */
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 */
1749 +#ifdef HAVE_AVAHI
1750 + cupsd_timeout_t *tmo; /* Next scheduled timed callback */
1751 + long tmo_delay; /* Time before it must be called */
1752 +#endif /* HAVE_AVAHI */
1755 #ifdef HAVE_GETEUID
1756 @@ -527,6 +535,14 @@
1758 httpInitialize();
1760 +#ifdef HAVE_AVAHI
1761 + /*
1762 + * Initialize timed callback structures.
1763 + */
1765 + cupsdInitTimeouts();
1766 +#endif /* HAVE_AVAHI */
1768 cupsdStartServer();
1771 @@ -657,6 +673,9 @@
1774 current_time = time(NULL);
1775 +#ifdef HAVE_AVAHI
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__ */
1785 +#ifdef HAVE_AVAHI
1786 + /*
1787 + * If a timed callback is due, run it.
1788 + */
1790 + tmo = cupsdNextTimeout (&tmo_delay);
1791 + if (tmo && tmo_delay == 0)
1792 + cupsdRunTimeout (tmo);
1794 + /*
1795 + * Try to restart the Avahi client every 10 seconds if needed...
1796 + */
1798 + if ((current_time - avahi_client_time) >= 10)
1800 + avahi_client_time = current_time;
1801 + cupsdStartAvahiClient();
1803 +#endif /* HAVE_AVAHI */
1805 #ifndef __APPLE__
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 */
1812 +#ifdef HAVE_AVAHI
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__ */
1823 +#ifdef HAVE_AVAHI
1824 + /*
1825 + * See if there are any scheduled timed callbacks to run.
1826 + */
1828 + tmo = cupsdNextTimeout (&tmo_delay);
1829 + if (tmo)
1831 + timeout = tmo_delay;
1832 + why = "run a timed callback";
1834 +#endif /* HAVE_AVAHI */
1837 * Check whether we are accepting new connections...
1839 --- /dev/null
1840 +++ b/scheduler/timeout.c
1841 @@ -0,0 +1,195 @@
1843 + * "$Id$"
1845 + * Timeout functions for the Common UNIX Printing System (CUPS).
1847 + * Copyright (C) 2010 Red Hat, Inc.
1848 + * Authors:
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/".
1855 + * Contents:
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.
1863 + */
1865 +#include <config.h>
1867 +#ifdef HAVE_AVAHI /* Applies to entire file... */
1870 + * Include necessary headers...
1871 + */
1873 +#include "cupsd.h"
1875 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
1876 +# include <malloc.h>
1877 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
1879 +#ifdef HAVE_AVAHI
1880 +# include <avahi-common/timeval.h>
1881 +#endif /* HAVE_AVAHI */
1884 +struct _cupsd_timeout_s
1886 + struct timeval when;
1887 + int enabled;
1888 + cupsd_timeoutfunc_t callback;
1889 + void *data;
1893 + * Local functions...
1894 + */
1897 + * 'compare_timeouts()' - Compare timed callbacks for array sorting.
1898 + */
1900 +static int
1901 +compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
1903 + if (!p0->enabled || !p1->enabled)
1905 + if (!p0->enabled && !p1->enabled)
1906 + return (0);
1908 + return (p0->enabled ? -1 : 1);
1911 + return (avahi_timeval_compare (&p0->when, &p1->when));
1916 + * 'cupsdInitTimeouts()' - Initialise timeout structures.
1917 + */
1919 +void
1920 +cupsdInitTimeouts(void)
1922 + Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
1927 + * 'cupsdAddTimeout()' - Add a timed callback.
1928 + */
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);
1941 + if (tv)
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);
1952 + return timeout;
1957 + * 'cupsdNextTimeout()' - Find the next enabled timed callback.
1958 + */
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)
1967 + first = NULL;
1969 + if (first && delay)
1971 + gettimeofday (&curtime, NULL);
1972 + if (avahi_timeval_compare (&curtime, &first->when) > 0)
1974 + *delay = 0;
1975 + } else {
1976 + *delay = 1 + first->when.tv_sec - curtime.tv_sec;
1977 + if (first->when.tv_usec < curtime.tv_usec)
1978 + (*delay)--;
1982 + return (first);
1987 + * 'cupsdRunTimeout()' - Run a timed callback.
1988 + */
1990 +void
1991 +cupsdRunTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
1993 + if (!timeout)
1994 + return;
1995 + timeout->enabled = 0;
1996 + if (!timeout->callback)
1997 + return;
1998 + timeout->callback (timeout, timeout->data);
2002 + * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
2003 + */
2005 +void
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);
2011 + if (tv)
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.
2022 + */
2024 +void
2025 +cupsdRemoveTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
2027 + cupsArrayRemove (Timeouts, timeout);
2028 + free (timeout);
2032 +#endif /* HAVE_AVAHI ... from top of file */
2035 + * End of "$Id$".
2036 + */
2037 --- a/cgi-bin/admin.c
2038 +++ b/cgi-bin/admin.c
2039 @@ -1643,7 +1643,7 @@
2040 else
2041 local_protocols[0] = '\0';
2043 -#ifdef HAVE_DNSSD
2044 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2045 if (cgiGetVariable("BROWSE_LOCAL_DNSSD"))
2047 if (local_protocols[0])
2048 @@ -1651,7 +1651,7 @@
2049 else
2050 strcat(local_protocols, "dnssd");
2052 -#endif /* HAVE_DNSSD */
2053 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2055 #ifdef HAVE_LDAP
2056 if (cgiGetVariable("BROWSE_LOCAL_LDAP"))
2057 @@ -2718,9 +2718,9 @@
2058 #endif /* HAVE_GSSAPI */
2059 cgiSetVariable("KERBEROS", "");
2061 -#ifdef HAVE_DNSSD
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) */
2067 #ifdef HAVE_LDAP
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));
2075 -#ifdef HAVE_DNSSD
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)))
2083 return (1);
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
2091 @@ -27,6 +27,7 @@
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
2097 * information.
2098 * cupsdStartPolling() - Start polling servers as needed.
2099 @@ -45,6 +46,8 @@
2100 * printer.
2101 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
2102 * record format.
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.
2108 @@ -83,6 +86,7 @@
2111 #include "cupsd.h"
2112 +#include <assert.h>
2113 #include <grp.h>
2115 #ifdef HAVE_DNSSD
2116 @@ -97,6 +101,17 @@
2117 # endif /* HAVE_SYSTEMCONFIGURATION */
2118 # endif /* __APPLE__ */
2119 #endif /* HAVE_DNSSD */
2120 +#ifdef HAVE_AVAHI
2121 +# include <avahi-common/domain.h>
2122 +#endif /* HAVE_AVAHI */
2125 +#ifdef HAVE_DNSSD
2126 +typedef char *cupsd_txt_record_t;
2127 +#endif /* HAVE_DNSSD */
2128 +#ifdef HAVE_AVAHI
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,
2140 + int for_lpd);
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) */
2147 #ifdef HAVE_DNSSD
2148 # ifdef HAVE_COREFOUNDATION
2149 static void dnssdAddAlias(const void *key, const void *value,
2150 void *context);
2151 # endif /* HAVE_COREFOUNDATION */
2152 -static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
2153 - int for_lpd);
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],
2157 - int count);
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 */
2168 +#ifdef HAVE_AVAHI
2169 +static AvahiStringList *avahiPackTxtRecord(char *keyvalue[][2],
2170 + int count);
2171 +static void avahi_entry_group_cb (AvahiEntryGroup *group,
2172 + AvahiEntryGroupState state,
2173 + void *userdata);
2174 +static void avahi_client_cb (AvahiClient *client,
2175 + AvahiClientState state,
2176 + void *userdata);
2177 +#endif /* HAVE_AVAHI */
2179 #ifdef HAVE_LDAP
2180 static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
2182 @@ -283,10 +310,10 @@
2183 ldap_dereg_printer(p);
2184 #endif /* HAVE_LDAP */
2186 -#ifdef HAVE_DNSSD
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 */
2200 -#ifdef HAVE_DNSSD
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 */
2214 +#ifdef HAVE_AVAHI
2216 + * 'cupsdStartAvahiClient()' - Start an Avahi client if needed
2217 + */
2219 +void
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 @@
2239 else
2240 BrowseSocket = -1;
2242 -#ifdef HAVE_DNSSD
2243 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2244 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
2246 +#ifdef HAVE_DNSSD
2247 DNSServiceErrorType error; /* Error from service creation */
2248 +#endif /* HAVE_DNSSD */
2249 cupsd_listener_t *lis; /* Current listening socket */
2252 +#ifdef HAVE_DNSSD
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();
2269 +#ifdef HAVE_AVAHI
2270 + cupsdStartAvahiClient ();
2271 +#endif /* HAVE_AVAHI */
2273 +#ifdef HAVE_DNSSD
2276 #endif /* HAVE_DNSSD */
2278 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2280 #ifdef HAVE_LIBSLP
2281 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
2282 @@ -1834,10 +1893,10 @@
2283 BrowseSocket = -1;
2286 -#ifdef HAVE_DNSSD
2287 - if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
2288 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2289 + if ((BrowseLocalProtocols & BROWSE_DNSSD))
2290 dnssdStop();
2291 -#endif /* HAVE_DNSSD */
2292 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2294 #ifdef HAVE_LIBSLP
2295 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
2296 @@ -1902,7 +1961,7 @@
2300 -#ifdef HAVE_DNSSD
2301 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2303 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
2305 @@ -1910,8 +1969,14 @@
2306 void
2307 cupsdUpdateDNSSDName(void)
2309 +#ifdef HAVE_DNSSD
2310 DNSServiceErrorType error; /* Error from service creation */
2311 char webif[1024]; /* Web interface share name */
2312 +#endif /* HAVE_DNSSD */
2313 +#ifdef HAVE_AVAHI
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 @@
2321 else
2322 strlcpy(webif, "CUPS Web Interface", sizeof(webif));
2324 +#ifdef HAVE_DNSSD
2325 if (WebIFRef)
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 */
2334 +#ifdef HAVE_AVAHI
2335 + if (!AvahiCupsClient)
2336 + /*
2337 + * Client not yet running.
2338 + */
2339 + return;
2341 + if (AvahiWebIFGroup)
2342 + avahi_entry_group_reset (AvahiWebIFGroup);
2343 + else
2344 + AvahiWebIFGroup = avahi_entry_group_new (AvahiCupsClient,
2345 + avahi_entry_group_cb,
2346 + NULL);
2348 + if (AvahiWebIFGroup)
2350 + ret = avahi_entry_group_add_service (AvahiWebIFGroup,
2351 + AVAHI_IF_UNSPEC,
2352 + AVAHI_PROTO_UNSPEC,
2353 + 0, /* flags */
2354 + webif, /* name */
2355 + "_http._tcp", /* type */
2356 + NULL, /* domain */
2357 + NULL, /* host */
2358 + DNSSDPort, /* port */
2359 + "path=/", NULL);
2360 + if (ret == 0)
2361 + ret = avahi_entry_group_commit (AvahiWebIFGroup);
2363 + if (ret != 0)
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) */
2374 #ifdef HAVE_LDAP
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),
2398 + "%s.local"
2399 +#ifdef HAVE_DNSSD
2400 + "." /* terminating dot no good for Avahi */
2401 +#endif /* HAVE_DNSSD */
2402 + , DNSSDHostName);
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...
2410 +#ifdef HAVE_DNSSD
2411 return (dnssdPackTxtRecord(txt_len, keyvalue, i));
2412 +#endif /* HAVE_DNSSD */
2413 +#ifdef HAVE_AVAHI
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));
2424 + if (!a->reg_name)
2425 + if (!b->reg_name)
2426 + return 0;
2427 + else
2428 + return -1;
2429 + else
2430 + if (!b->reg_name)
2431 + return 1;
2432 + else
2433 + return (_cups_strcasecmp(a->reg_name, b->reg_name));
2437 @@ -2489,6 +2612,10 @@
2439 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
2441 +#ifdef HAVE_DNSSD
2442 + if (!DNSSDRef)
2443 + return;
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 */
2454 +#ifdef HAVE_AVAHI
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;
2461 + if (p->ipp_txt)
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) */
2480 +#ifdef HAVE_DNSSD
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 @@
2496 static void
2497 dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
2499 +#ifdef HAVE_DNSSD
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 */
2510 +#ifdef HAVE_AVAHI
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 */
2524 +#ifdef HAVE_DNSSD
2525 + if (!DNSSDRef)
2526 + return;
2528 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2529 !p->ipp_ref ? "new" : "update");
2531 +#endif /* HAVE_DNSSD */
2532 +#ifdef HAVE_AVAHI
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);
2545 + /*
2546 + * Make sure there is room for at least 15 characters of
2547 + * DNSSDComputerName.
2548 + */
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));
2557 else
2558 strlcpy(name, p->info, sizeof(name));
2560 else if (DNSSDComputerName)
2561 - snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDComputerName);
2563 + /*
2564 + * Make sure there is room for at least 15 characters of
2565 + * DNSSDComputerName.
2566 + */
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));
2575 else
2576 strlcpy(name, p->name, sizeof(name));
2578 @@ -2712,6 +2905,7 @@
2579 * Register IPP and (optionally) LPD...
2582 +#ifdef HAVE_DNSSD
2583 ipp_len = 0; /* anti-compiler-warning-code */
2584 ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
2586 @@ -2884,6 +3078,209 @@
2588 if (printer_txt)
2589 free(printer_txt);
2590 +#endif /* HAVE_DNSSD */
2591 +#ifdef HAVE_AVAHI
2592 + if (!AvahiCupsClient)
2593 + /*
2594 + * Client not running yet. The client callback will call us again later.
2595 + */
2596 + return;
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))
2605 + /*
2606 + * Update the existing registration...
2607 + */
2609 + avahi_string_list_free (p->ipp_txt);
2611 + if (p->printer_txt)
2612 + avahi_string_list_free (p->printer_txt);
2614 + /*
2615 + * Update the service group entry.
2616 + */
2618 + regtype_copy = strdup (regtype);
2619 + subtype = strchr (regtype_copy, ',');
2620 + if (subtype)
2621 + *subtype = '\0';
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,
2626 + AVAHI_IF_UNSPEC,
2627 + AVAHI_PROTO_UNSPEC,
2628 + 0, name,
2629 + regtype_copy,
2630 + NULL, ipp_txt);
2631 + free (regtype_copy);
2633 + if (ret < 0)
2634 + goto update_failed;
2636 + p->ipp_txt = ipp_txt;
2637 + ipp_txt = NULL;
2639 + if (BrowseLocalProtocols & BROWSE_LPD)
2641 + ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
2642 + AVAHI_IF_UNSPEC,
2643 + AVAHI_PROTO_UNSPEC,
2644 + 0, name,
2645 + "_printer._tcp", NULL,
2646 + printer_txt);
2647 + if (ret < 0)
2648 + goto update_failed;
2650 + p->printer_txt = printer_txt;
2651 + printer_txt = NULL;
2654 + ret = avahi_entry_group_commit (p->avahi_group);
2655 + if (ret < 0)
2657 + update_failed:
2658 + cupsdLogMessage (CUPSD_LOG_ERROR,
2659 + "Failed to update TXT record for %s: %d",
2660 + name, ret);
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)
2671 + /*
2672 + * Initial registration. Use the _fax subtype for fax queues...
2673 + */
2675 + p->avahi_group = avahi_entry_group_new (AvahiCupsClient,
2676 + avahi_entry_group_cb,
2677 + p);
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)
2685 + ret = 0;
2686 + goto add_failed;
2689 + /*
2690 + * Add each service type (DNSSDRegType may contain several,
2691 + * separated by commas).
2692 + */
2694 + subtype = regtype_copy = strdup (regtype);
2695 + while (subtype && *subtype)
2697 + nextsubtype = strchr (subtype, ',');
2698 + if (nextsubtype)
2699 + *nextsubtype++ = '\0';
2701 + if (subtype == regtype_copy)
2703 + /*
2704 + * Main type entry.
2705 + */
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,
2710 + AVAHI_IF_UNSPEC,
2711 + AVAHI_PROTO_UNSPEC,
2712 + 0, name, regtype_copy,
2713 + NULL, NULL,
2714 + DNSSDPort,
2715 + ipp_txt);
2717 + else
2719 + /*
2720 + * Sub-type entry.
2721 + */
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,
2728 + AVAHI_IF_UNSPEC,
2729 + AVAHI_PROTO_UNSPEC,
2730 + 0, name,
2731 + regtype_copy,
2732 + NULL, fullsubtype);
2735 + if (ret < 0)
2737 + free (regtype_copy);
2738 + goto add_failed;
2741 + subtype = nextsubtype;
2744 + free (regtype_copy);
2745 + p->ipp_txt = ipp_txt;
2746 + ipp_txt = NULL;
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,
2755 + AVAHI_IF_UNSPEC,
2756 + AVAHI_PROTO_UNSPEC,
2757 + 0, name,
2758 + "_printer._tcp", NULL, NULL,
2759 + 515,
2760 + printer_txt);
2761 + if (ret < 0)
2762 + goto add_failed;
2764 + p->printer_txt = printer_txt;
2765 + printer_txt = NULL;
2768 + ret = avahi_entry_group_commit (p->avahi_group);
2770 + if (ret < 0)
2772 + add_failed:
2773 + cupsdLogMessage (CUPSD_LOG_ERROR,
2774 + "Failed to add Avahi entry for %s: %d",
2775 + name, ret);
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;
2787 + if (ipp_txt)
2788 + avahi_string_list_free (ipp_txt);
2790 + if (printer_txt)
2791 + avahi_string_list_free (printer_txt);
2792 +#endif /* HAVE_AVAHI */
2796 @@ -2896,6 +3293,10 @@
2798 cupsd_printer_t *p; /* Current printer */
2800 +#ifdef HAVE_DNSSD
2801 + if (!DNSSDRef)
2802 + return;
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);
2811 +#ifdef HAVE_DNSSD
2813 * Shutdown the rest of the service refs...
2815 @@ -2926,14 +3328,17 @@
2817 DNSServiceRefDeallocate(DNSSDRef);
2818 DNSSDRef = NULL;
2819 +#endif /* HAVE_DNSSD */
2821 cupsArrayDelete(DNSSDPrinters);
2822 DNSSDPrinters = NULL;
2824 DNSSDPort = 0;
2826 +#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
2829 +#ifdef HAVE_DNSSD
2831 * 'dnssdUpdate()' - Handle DNS-SD queries.
2833 @@ -2955,6 +3360,147 @@
2834 #endif /* HAVE_DNSSD */
2837 +#ifdef HAVE_AVAHI
2839 + * 'avahiPackTxtRecord()' - Pack an array of key/value pairs into an
2840 + * AvahiStringList.
2841 + */
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;
2848 + char **elements;
2849 + size_t len;
2850 + int i = 0;
2852 + elements = malloc ((1 + count) * sizeof (char *));
2853 + if (!elements)
2854 + goto cleanup;
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));
2861 + if (!elements[i])
2862 + goto cleanup;
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);
2869 +cleanup:
2870 + while (--i >= 0)
2871 + free (elements[i]);
2873 + free (elements);
2874 + return (strlst);
2879 + * 'avahi_entry_group_cb()' - Avahi entry group callback function.
2880 + */
2881 +static void
2882 +avahi_entry_group_cb (AvahiEntryGroup *group,
2883 + AvahiEntryGroupState state,
2884 + void *userdata)
2886 + char *name;
2888 + if (userdata)
2889 + name = ((cupsd_printer_t *) userdata)->reg_name;
2890 + else
2891 + name = "CUPS web interface";
2893 + switch (state)
2895 + case AVAHI_ENTRY_GROUP_UNCOMMITED:
2896 + case AVAHI_ENTRY_GROUP_REGISTERING:
2897 + break;
2899 + case AVAHI_ENTRY_GROUP_ESTABLISHED:
2900 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2901 + "Avahi entry group established for %s", name);
2902 + break;
2904 + default:
2905 + cupsdLogMessage (CUPSD_LOG_DEBUG,
2906 + "Avahi entry group %s has state %d",
2907 + name, state);
2908 + break;
2914 + * 'avahi_client_cb()' - Avahi client callback function.
2915 + */
2916 +static void
2917 +avahi_client_cb (AvahiClient *client,
2918 + AvahiClientState state,
2919 + void *userdata)
2921 + cupsd_printer_t *printer;
2922 + switch (state)
2924 + case AVAHI_CLIENT_S_RUNNING:
2925 + /*
2926 + * Avahi client started successfully.
2927 + */
2928 + AvahiCupsClient = client;
2929 + AvahiCupsClientConnecting = 0;
2930 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client started");
2932 + cupsdUpdateDNSSDName ();
2934 + for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
2935 + printer;
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);
2942 + break;
2944 + case AVAHI_CLIENT_CONNECTING:
2945 + /*
2946 + * No Avahi daemon, client is waiting.
2947 + */
2948 + AvahiCupsClientConnecting = 1;
2949 + cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client connecting");
2950 + break;
2952 + case AVAHI_CLIENT_FAILURE:
2953 + /*
2954 + * Avahi client failed, close it to allow a clean restart.
2955 + */
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);
2961 + printer;
2962 + printer = (cupsd_printer_t *)cupsArrayNext(Printers))
2963 + dnssdDeregisterPrinter (printer);
2965 + avahi_client_free(client);
2966 + AvahiCupsClientConnecting = 0;
2967 + AvahiCupsClient = NULL;
2969 + break;
2971 + default:
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
2983 @@ -31,6 +31,10 @@
2984 # endif /* HAVE_LDAP_SSL_H */
2985 #endif /* HAVE_LDAP */
2987 +#ifdef HAVE_AVAHI
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 */
2998 -#ifdef HAVE_DNSSD
2999 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3000 VAR char *DNSSDComputerName VALUE(NULL),
3001 /* Computer/server name */
3002 *DNSSDHostName VALUE(NULL),
3003 /* Hostname */
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) */
3014 +#ifdef HAVE_DNSSD
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 */
3024 +#ifdef HAVE_AVAHI
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 */
3035 #ifdef HAVE_LIBSLP
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);
3047 -#ifdef HAVE_DNSSD
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) */
3052 #ifdef HAVE_LDAP
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));
3061 -#ifdef HAVE_DNSSD
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
3077 @@ -883,9 +883,9 @@
3078 cupsdClearString(&p->alert);
3079 cupsdClearString(&p->alert_description);
3081 -#ifdef HAVE_DNSSD
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);
3093 -#ifdef HAVE_DNSSD
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
3109 @@ -16,6 +16,9 @@
3110 #ifdef HAVE_DNSSD
3111 # include <dns_sd.h>
3112 #endif /* HAVE_DNSSD */
3113 +#ifdef HAVE_AVAHI
3114 +# include "avahi.h"
3115 +#endif /* HAVE_AVAHI */
3116 #include <cups/pwg-private.h>
3119 @@ -95,16 +98,23 @@
3120 time_t marker_time; /* Last time marker attributes were updated */
3121 _ppd_cache_t *pc; /* PPD cache and mapping data */
3123 -#ifdef HAVE_DNSSD
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) */
3130 +#ifdef HAVE_DNSSD
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 */
3138 +#ifdef HAVE_AVAHI
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
3148 @@ -650,7 +650,7 @@
3149 Browsing = CUPS_DEFAULT_BROWSING;
3150 DefaultShared = CUPS_DEFAULT_DEFAULT_SHARED;
3152 -#ifdef HAVE_DNSSD
3153 +#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3154 cupsdSetString(&DNSSDRegType, "_ipp._tcp,_cups");
3155 #endif /* HAVE_DNSSD */