0d86476
diff -up cups-1.5.2/cgi-bin/admin.c.avahi-5-services cups-1.5.2/cgi-bin/admin.c
0d86476
--- cups-1.5.2/cgi-bin/admin.c.avahi-5-services	2011-08-17 22:01:53.000000000 +0100
0d86476
+++ cups-1.5.2/cgi-bin/admin.c	2012-03-14 15:08:25.701611799 +0000
6a13918
@@ -1643,7 +1643,7 @@ do_config_server(http_t *http)		/* I - H
9829b01
       else
9829b01
 	local_protocols[0] = '\0';
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
       if (cgiGetVariable("BROWSE_LOCAL_DNSSD"))
9829b01
       {
9829b01
 	if (local_protocols[0])
6a13918
@@ -1651,7 +1651,7 @@ do_config_server(http_t *http)		/* I - H
9829b01
 	else
9829b01
 	  strcat(local_protocols, "dnssd");
9829b01
       }
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
 #ifdef HAVE_LDAP
9829b01
       if (cgiGetVariable("BROWSE_LOCAL_LDAP"))
6a13918
@@ -2718,9 +2718,9 @@ do_menu(http_t *http)			/* I - HTTP conn
9829b01
 #endif /* HAVE_GSSAPI */
9829b01
   cgiSetVariable("KERBEROS", "");
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
   cgiSetVariable("HAVE_DNSSD", "1");
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
 #ifdef HAVE_LDAP
9829b01
   cgiSetVariable("HAVE_LDAP", "1");
0d86476
diff -up cups-1.5.2/scheduler/avahi.h.avahi-5-services cups-1.5.2/scheduler/avahi.h
0d86476
--- cups-1.5.2/scheduler/avahi.h.avahi-5-services	2012-03-14 15:07:29.477542381 +0000
0d86476
+++ cups-1.5.2/scheduler/avahi.h	2012-03-14 15:08:25.701611799 +0000
0d86476
@@ -3,7 +3,7 @@
0d86476
  *
0d86476
  *   Avahi poll implementation for the CUPS scheduler.
0d86476
  *
0d86476
- *   Copyright (C) 2010, 2011 Red Hat, Inc.
0d86476
+ *   Copyright (C) 2010, 2011, 2012 Red Hat, Inc.
0d86476
  *   Authors:
0d86476
  *    Tim Waugh <twaugh@redhat.com>
0d86476
  *
0d86476
@@ -32,37 +32,40 @@
0d86476
  *   OF THE POSSIBILITY OF SUCH DAMAGE.
0d86476
  */
0d86476
 
0d86476
-#include <config.h>
0d86476
+#ifndef _CUPS_AVAHI_H_
0d86476
+#  define _CUPS_AVAHI_H_
0d86476
 
0d86476
-#ifdef HAVE_AVAHI
0d86476
-#  include <avahi-client/client.h>
0d86476
-#  include <avahi-client/publish.h>
0d86476
-#endif /* HAVE_AVAHI */
0d86476
+/*
0d86476
+ * Include necessary headers...
0d86476
+ */
0d86476
 
0d86476
-#ifdef HAVE_AUTHORIZATION_H
0d86476
-#  include <Security/Authorization.h>
0d86476
-#endif /* HAVE_AUTHORIZATION_H */
0d86476
+#  include <config.h>
0d86476
 
0d86476
+#  ifdef HAVE_AVAHI
0d86476
+#    include <avahi-client/client.h>
0d86476
+#    include <avahi-client/publish.h>
0d86476
+#  endif /* HAVE_AVAHI */
0d86476
 
0d86476
-#ifdef HAVE_AVAHI
0d86476
+#  ifdef HAVE_AVAHI
0d86476
 typedef struct
0d86476
 {
0d86476
     AvahiPoll api;
0d86476
     cups_array_t *watched_fds;
0d86476
     cups_array_t *timeouts;
0d86476
 } AvahiCupsPoll;
0d86476
-#endif /* HAVE_AVAHI */
0d86476
+#  endif /* HAVE_AVAHI */
0d86476
 
0d86476
 /*
0d86476
  * Prototypes...
0d86476
  */
0d86476
 
0d86476
-#ifdef HAVE_AVAHI
0d86476
+#  ifdef HAVE_AVAHI
0d86476
 extern AvahiCupsPoll *	avahi_cups_poll_new(void);
0d86476
 extern void		avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
0d86476
 extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
0d86476
-#endif /* HAVE_AVAHI */
0d86476
+#  endif /* HAVE_AVAHI */
0d86476
 
0d86476
+#endif /* !_CUPS_AVAHI_H_ */
0d86476
 
0d86476
 /*
0d86476
  * End of "$Id$".
0d86476
diff -up cups-1.5.2/scheduler/client.c.avahi-5-services cups-1.5.2/scheduler/client.c
0d86476
--- cups-1.5.2/scheduler/client.c.avahi-5-services	2012-01-13 23:00:22.000000000 +0000
0d86476
+++ cups-1.5.2/scheduler/client.c	2012-03-14 15:08:25.703611797 +0000
0d86476
@@ -4989,7 +4989,7 @@ valid_host(cupsd_client_t *con)		/* I -
9829b01
 	    !strncmp(host, "[::1]:", 6));
9829b01
   }
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
  /*
9829b01
   * Check if the hostname is something.local (Bonjour); if so, allow it.
9829b01
   */
0d86476
@@ -4998,7 +4998,7 @@ valid_host(cupsd_client_t *con)		/* I -
9829b01
       (!_cups_strcasecmp(end, ".local") || !_cups_strncasecmp(end, ".local:", 7) ||
9829b01
        !_cups_strcasecmp(end, ".local.") || !_cups_strncasecmp(end, ".local.:", 8)))
9829b01
     return (1);
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
  /*
9829b01
   * Check if the hostname is an IP address...
0d86476
diff -up cups-1.5.2/scheduler/conf.c.avahi-5-services cups-1.5.2/scheduler/conf.c
0d86476
--- cups-1.5.2/scheduler/conf.c.avahi-5-services	2012-03-14 15:04:17.636305526 +0000
0d86476
+++ cups-1.5.2/scheduler/conf.c	2012-03-14 15:08:25.706611803 +0000
0d86476
@@ -652,7 +652,7 @@ cupsdReadConfiguration(void)
e462076
   Browsing                 = CUPS_DEFAULT_BROWSING;
e462076
   DefaultShared            = CUPS_DEFAULT_DEFAULT_SHARED;
e462076
 
e462076
-#ifdef HAVE_DNSSD
e462076
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
   cupsdSetString(&DNSSDRegType, "_ipp._tcp,_cups");
9829b01
 #endif /* HAVE_DNSSD */
9829b01
 
0d86476
diff -up cups-1.5.2/scheduler/dirsvc.c.avahi-5-services cups-1.5.2/scheduler/dirsvc.c
0d86476
--- cups-1.5.2/scheduler/dirsvc.c.avahi-5-services	2012-03-14 15:04:17.674305572 +0000
0d86476
+++ cups-1.5.2/scheduler/dirsvc.c	2012-03-14 15:08:25.709611806 +0000
9829b01
@@ -27,6 +27,7 @@
9829b01
  *   ldap_connect()             - Start new LDAP connection
9829b01
  *   ldap_reconnect()           - Reconnect to LDAP Server
9829b01
  *   ldap_disconnect()          - Disconnect from LDAP Server
9829b01
+ *   cupsdStartAvahiClient()    - Start an Avahi client if needed
9829b01
  *   cupsdStartBrowsing()       - Start sending and receiving broadcast
9829b01
  *                                information.
9829b01
  *   cupsdStartPolling()        - Start polling servers as needed.
6a13918
@@ -40,11 +41,12 @@
6a13918
  *   dequote()                  - Remote quotes from a string.
6a13918
  *   dnssdAddAlias()            - Add a DNS-SD alias name.
6a13918
  *   dnssdBuildTxtRecord()      - Build a TXT record from printer info.
6a13918
- *   dnssdComparePrinters()     - Compare the registered names of two printers.
6a13918
  *   dnssdDeregisterPrinter()   - Stop sending broadcast information for a
9829b01
  *                                printer.
9829b01
  *   dnssdPackTxtRecord()       - Pack an array of key/value pairs into the TXT
9829b01
  *                                record format.
9829b01
+ *   avahiPackTxtRecord()       - Pack an array of key/value pairs into an
9829b01
+ *                                AvahiStringList.
9829b01
  *   dnssdRegisterCallback()    - DNSServiceRegister callback.
9829b01
  *   dnssdRegisterPrinter()     - Start sending broadcast information for a
9829b01
  *                                printer or update the broadcast contents.
6a13918
@@ -83,6 +85,7 @@
9829b01
  */
9829b01
 
9829b01
 #include "cupsd.h"
9829b01
+#include <assert.h>
9829b01
 #include <grp.h>
9829b01
 
9829b01
 #ifdef HAVE_DNSSD
6a13918
@@ -97,6 +100,17 @@
9829b01
 #    endif /* HAVE_SYSTEMCONFIGURATION */
9829b01
 #  endif /* __APPLE__ */
9829b01
 #endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+#  include <avahi-common/domain.h>
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
+
9829b01
+#ifdef HAVE_DNSSD
9829b01
+typedef char *cupsd_txt_record_t;
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+typedef AvahiStringList *cupsd_txt_record_t;
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 
9829b01
 
9829b01
 /*
6a13918
@@ -159,27 +173,38 @@ static void	update_polling(void);
9829b01
 static void	update_smb(int onoff);
9829b01
 
9829b01
 
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
+static cupsd_txt_record_t dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
9829b01
+					      int for_lpd);
9829b01
+static void	dnssdDeregisterPrinter(cupsd_printer_t *p);
9829b01
+static void	dnssdRegisterPrinter(cupsd_printer_t *p);
9829b01
+static void	dnssdStop(void);
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
+
9829b01
 #ifdef HAVE_DNSSD
9829b01
 #  ifdef HAVE_COREFOUNDATION
9829b01
 static void	dnssdAddAlias(const void *key, const void *value,
9829b01
 		              void *context);
9829b01
 #  endif /* HAVE_COREFOUNDATION */
9829b01
-static char	*dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
9829b01
-		                     int for_lpd);
9829b01
-static int	dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
9829b01
-static void	dnssdDeregisterPrinter(cupsd_printer_t *p);
9829b01
-static char	*dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2],
9829b01
-		                    int count);
9829b01
 static void	dnssdRegisterCallback(DNSServiceRef sdRef,
9829b01
 		                      DNSServiceFlags flags,
9829b01
 				      DNSServiceErrorType errorCode,
9829b01
 				      const char *name, const char *regtype,
9829b01
 				      const char *domain, void *context);
9829b01
-static void	dnssdRegisterPrinter(cupsd_printer_t *p);
9829b01
-static void	dnssdStop(void);
9829b01
 static void	dnssdUpdate(void);
9829b01
 #endif /* HAVE_DNSSD */
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+static AvahiStringList *avahiPackTxtRecord(char *keyvalue[][2],
9829b01
+					   int count);
9829b01
+static void	avahi_entry_group_cb (AvahiEntryGroup *group,
9829b01
+				      AvahiEntryGroupState state,
9829b01
+				      void *userdata);
9829b01
+static void	avahi_client_cb (AvahiClient *client,
9829b01
+				 AvahiClientState state,
9829b01
+				 void *userdata);
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
 #ifdef HAVE_LDAP
9829b01
 static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
9829b01
 		{
6a13918
@@ -283,10 +308,10 @@ cupsdDeregisterPrinter(
9829b01
     ldap_dereg_printer(p);
9829b01
 #endif /* HAVE_LDAP */
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
-  if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
+  if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD))
9829b01
     dnssdDeregisterPrinter(p);
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 }
9829b01
 
9829b01
 
6a13918
@@ -702,10 +727,10 @@ cupsdRegisterPrinter(cupsd_printer_t *p)
9829b01
     slpRegisterPrinter(p); */
9829b01
 #endif /* HAVE_LIBSLP */
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
-  if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
+  if ((BrowseLocalProtocols & BROWSE_DNSSD))
9829b01
     dnssdRegisterPrinter(p);
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 }
9829b01
 
9829b01
 
a660226
@@ -1419,6 +1444,36 @@ ldap_disconnect(LDAP *ld)		/* I - LDAP h
9829b01
 #endif /* HAVE_LDAP */
9829b01
 
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+/*
9829b01
+ * 'cupsdStartAvahiClient()' - Start an Avahi client if needed
9829b01
+ */
9829b01
+
9829b01
+void
9829b01
+cupsdStartAvahiClient(void)
9829b01
+{
a660226
+  int error = 0;
a660226
+
9829b01
+  if (!AvahiCupsClient && !AvahiCupsClientConnecting)
9829b01
+  {
9829b01
+    if (!AvahiCupsPollHandle)
9829b01
+      AvahiCupsPollHandle = avahi_cups_poll_new ();
9829b01
+
9829b01
+    if (AvahiCupsPollHandle)
a660226
+    {
a660226
+      if (avahi_client_new (avahi_cups_poll_get (AvahiCupsPollHandle),
a660226
+			    AVAHI_CLIENT_NO_FAIL,
a660226
+			    avahi_client_cb, NULL,
a660226
+			    &error) != NULL)
a660226
+	AvahiCupsClientConnecting = 1;
a660226
+      else
a660226
+	cupsdLogMessage (CUPSD_LOG_WARN, "Avahi client failed: %d", error);
a660226
+    }
9829b01
+  }
9829b01
+}
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
+  
9829b01
 /*
9829b01
  * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
9829b01
  */
a660226
@@ -1542,13 +1597,16 @@ cupsdStartBrowsing(void)
9829b01
   else
9829b01
     BrowseSocket = -1;
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
   if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
9829b01
   {
9829b01
+#ifdef HAVE_DNSSD
9829b01
     DNSServiceErrorType error;		/* Error from service creation */
9829b01
+#endif /* HAVE_DNSSD */
9829b01
     cupsd_listener_t	*lis;		/* Current listening socket */
9829b01
 
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
    /*
9829b01
     * First create a "master" connection for all registrations...
9829b01
     */
a660226
@@ -1573,6 +1631,7 @@ cupsdStartBrowsing(void)
9829b01
       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
9829b01
 
9829b01
       cupsdAddSelect(fd, (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
9829b01
+#endif /* HAVE_DNSSD */
9829b01
 
9829b01
      /*
9829b01
       * Then get the port we use for registrations.  If we are not listening
a660226
@@ -1598,17 +1657,23 @@ cupsdStartBrowsing(void)
6a13918
       */
6a13918
 
6a13918
       if (BrowseRemoteProtocols & BROWSE_DNSSD)
6a13918
-        DNSSDPrinters = cupsArrayNew((cups_array_func_t)dnssdComparePrinters,
6a13918
-	                             NULL);
6a13918
+	  DNSSDPrinters = cupsArrayNew(NULL, NULL);
6a13918
 
6a13918
      /*
6a13918
       * Set the computer name and register the web interface...
9829b01
       */
9829b01
 
9829b01
       cupsdUpdateDNSSDName();
9829b01
+
9829b01
+#ifdef HAVE_AVAHI
9829b01
+      cupsdStartAvahiClient ();
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
+#ifdef HAVE_DNSSD
9829b01
     }
9829b01
-  }
9829b01
 #endif /* HAVE_DNSSD */
9829b01
+  }
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
 #ifdef HAVE_LIBSLP
9829b01
   if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
a660226
@@ -1834,10 +1899,10 @@ cupsdStopBrowsing(void)
9829b01
     BrowseSocket = -1;
9829b01
   }
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
-  if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
+  if ((BrowseLocalProtocols & BROWSE_DNSSD))
9829b01
     dnssdStop();
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
 #ifdef HAVE_LIBSLP
9829b01
   if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
a660226
@@ -1902,7 +1967,7 @@ cupsdStopPolling(void)
9829b01
 }
9829b01
 
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
 /*
9829b01
  * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
9829b01
  */
a660226
@@ -1910,8 +1975,14 @@ cupsdStopPolling(void)
9829b01
 void
9829b01
 cupsdUpdateDNSSDName(void)
9829b01
 {
9829b01
+#ifdef HAVE_DNSSD
9829b01
   DNSServiceErrorType error;		/* Error from service creation */
9829b01
   char		webif[1024];		/* Web interface share name */
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  int		ret;			/* Error from service creation */
9829b01
+  char		webif[AVAHI_LABEL_MAX];	/* Web interface share name */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 #  ifdef HAVE_SYSTEMCONFIGURATION
9829b01
   SCDynamicStoreRef sc;			/* Context for dynamic store */
9829b01
   CFDictionaryRef btmm;			/* Back-to-My-Mac domains */
a660226
@@ -2042,6 +2113,7 @@ cupsdUpdateDNSSDName(void)
9829b01
     else
9829b01
       strlcpy(webif, "CUPS Web Interface", sizeof(webif));
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
     if (WebIFRef)
9829b01
       DNSServiceRefDeallocate(WebIFRef);
9829b01
 
a660226
@@ -2054,9 +2126,45 @@ cupsdUpdateDNSSDName(void)
9829b01
 				    NULL)) != kDNSServiceErr_NoError)
9829b01
       cupsdLogMessage(CUPSD_LOG_ERROR,
9829b01
 		      "DNS-SD web interface registration failed: %d", error);
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+
9829b01
+#ifdef HAVE_AVAHI
9829b01
+    if (!AvahiCupsClient)
9829b01
+     /*
9829b01
+      * Client not yet running.
9829b01
+      */
9829b01
+      return;
9829b01
+
9829b01
+    if (AvahiWebIFGroup)
9829b01
+      avahi_entry_group_reset (AvahiWebIFGroup);
9829b01
+    else
9829b01
+      AvahiWebIFGroup = avahi_entry_group_new (AvahiCupsClient,
9829b01
+					       avahi_entry_group_cb,
9829b01
+					       NULL);
9829b01
+
9829b01
+    if (AvahiWebIFGroup)
9829b01
+    {
9829b01
+      ret = avahi_entry_group_add_service (AvahiWebIFGroup,
9829b01
+					   AVAHI_IF_UNSPEC,
9829b01
+					   AVAHI_PROTO_UNSPEC,
9829b01
+					   0, /* flags */
9829b01
+					   webif, /* name */
9829b01
+					   "_http._tcp", /* type */
9829b01
+					   NULL, /* domain */
9829b01
+					   NULL, /* host */
9829b01
+					   DNSSDPort, /* port */
9829b01
+					   "path=/", NULL);
9829b01
+      if (ret == 0)
9829b01
+	ret = avahi_entry_group_commit (AvahiWebIFGroup);
9829b01
+
9829b01
+      if (ret != 0)
9829b01
+	cupsdLogMessage (CUPSD_LOG_ERROR,
9829b01
+			 "Avahi web interface registration failed: %d", ret);
9829b01
+    }
9829b01
+#endif /* HAVE_AVAHI */
9829b01
   }
9829b01
 }
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
 
9829b01
 #ifdef HAVE_LDAP
a660226
@@ -2334,13 +2442,15 @@ dnssdAddAlias(const void *key,		/* I - K
9829b01
                     "Bad Back to My Mac domain in dynamic store!");
9829b01
 }
9829b01
 #  endif /* HAVE_COREFOUNDATION */
9829b01
+#endif /* HAVE_DNSSD */
9829b01
 
9829b01
 
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
 /*
9829b01
  * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
9829b01
  */
9829b01
 
9829b01
-static char *				/* O - TXT record */
9829b01
+static cupsd_txt_record_t		/* O - TXT record */
9829b01
 dnssdBuildTxtRecord(
9829b01
     int             *txt_len,		/* O - TXT record length */
9829b01
     cupsd_printer_t *p,			/* I - Printer information */
a660226
@@ -2379,7 +2489,12 @@ dnssdBuildTxtRecord(
9829b01
   keyvalue[i  ][0] = "ty";
9829b01
   keyvalue[i++][1] = p->make_model ? p->make_model : "Unknown";
9829b01
 
9829b01
-  snprintf(admin_hostname, sizeof(admin_hostname), "%s.local.", DNSSDHostName);
9829b01
+  snprintf(admin_hostname, sizeof(admin_hostname),
9829b01
+	   "%s.local"
9829b01
+#ifdef HAVE_DNSSD
9829b01
+	   "." /* terminating dot no good for Avahi */
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+	   , DNSSDHostName);
9829b01
   httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
9829b01
                    "http", NULL, admin_hostname, DNSSDPort, "/%s/%s",
9829b01
 		   (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
a660226
@@ -2462,19 +2577,12 @@ dnssdBuildTxtRecord(
9829b01
   * Then pack them into a proper txt record...
9829b01
   */
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
   return (dnssdPackTxtRecord(txt_len, keyvalue, i));
6a13918
-}
6a13918
-
6a13918
-
6a13918
-/*
6a13918
- * 'dnssdComparePrinters()' - Compare the registered names of two printers.
6a13918
- */
6a13918
-
6a13918
-static int				/* O - Result of comparison */
6a13918
-dnssdComparePrinters(cupsd_printer_t *a,/* I - First printer */
6a13918
-                     cupsd_printer_t *b)/* I - Second printer */
6a13918
-{
6a13918
-  return (_cups_strcasecmp(a->reg_name, b->reg_name));
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  return (avahiPackTxtRecord(keyvalue, i));
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 }
9829b01
 
9829b01
 
a660226
@@ -2489,6 +2597,10 @@ dnssdDeregisterPrinter(
9829b01
 {
9829b01
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
+  if (!DNSSDRef)
9829b01
+    return;
9829b01
+
9829b01
  /*
9829b01
   * Closing the socket deregisters the service
9829b01
   */
a660226
@@ -2524,6 +2636,24 @@ dnssdDeregisterPrinter(
9829b01
     free(p->printer_txt);
9829b01
     p->printer_txt = NULL;
9829b01
   }
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  if (p->avahi_group)
9829b01
+    {
9829b01
+      avahi_entry_group_reset (p->avahi_group);
9829b01
+      avahi_entry_group_free (p->avahi_group);
9829b01
+      p->avahi_group = NULL;
9829b01
+
9829b01
+      if (p->ipp_txt)
9829b01
+	avahi_string_list_free (p->ipp_txt);
9829b01
+
9829b01
+      if (p->printer_txt)
9829b01
+	avahi_string_list_free (p->printer_txt);
9829b01
+
9829b01
+      p->ipp_txt = p->printer_txt = NULL;
9829b01
+    }
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 
9829b01
  /*
9829b01
   * Remove the printer from the array of DNS-SD printers, then clear the
a660226
@@ -2533,8 +2663,10 @@ dnssdDeregisterPrinter(
9829b01
   cupsArrayRemove(DNSSDPrinters, p);
9829b01
   cupsdClearString(&p->reg_name);
9829b01
 }
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
 /*
9829b01
  * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
9829b01
  *                          TXT record format.
a660226
@@ -2644,8 +2776,10 @@ dnssdRegisterCallback(
9829b01
     LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
9829b01
   }
9829b01
 }
9829b01
+#endif /* HAVE_DNSSD */
9829b01
 
9829b01
 
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
 /*
9829b01
  * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
9829b01
  *		              or update the broadcast contents.
a660226
@@ -2654,20 +2788,40 @@ dnssdRegisterCallback(
9829b01
 static void
9829b01
 dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
9829b01
 {
9829b01
+#ifdef HAVE_DNSSD
9829b01
   DNSServiceErrorType	se;		/* dnssd errors */
9829b01
   char			*ipp_txt,	/* IPP TXT record buffer */
9829b01
 			*printer_txt,	/* LPD TXT record buffer */
9829b01
-			name[1024],	/* Service name */
9829b01
-			*nameptr;	/* Pointer into name */
9829b01
+			name[1024];	/* Service name */
9829b01
   int			ipp_len,	/* IPP TXT record length */
9829b01
 			printer_len,	/* LPD TXT record length */
9829b01
 			printer_port;	/* LPD port number */
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  int			ret;		/* Error code */
9829b01
+  AvahiStringList	*ipp_txt,	/* IPP TXT record */
9829b01
+			*printer_txt;	/* LPD TXT record */
e462076
+  char			name[AVAHI_LABEL_MAX],	/* Service name */
e462076
+			fullsubtype[AVAHI_LABEL_MAX]; /* Full subtype */
9829b01
+  char			*regtype_copy,	/* Writeable copy of reg type */
e462076
+			*subtype,	/* Current service sub type */
e462076
+			*nextsubtype;	/* Next service sub type */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+  char			*nameptr;	/* Pointer into name */
9829b01
   const char		*regtype;	/* Registration type */
9829b01
 
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
+  if (!DNSSDRef)
9829b01
+    return;
9829b01
+
9829b01
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
9829b01
                   !p->ipp_ref ? "new" : "update");
9829b01
-
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
9829b01
+		  !p->avahi_group ? "new" : "update");
9829b01
+#endif /* HAVE_AVAHI */
9829b01
  /*
9829b01
   * If per-printer sharing was just disabled make sure we're not
9829b01
   * registered before returning.
a660226
@@ -2686,12 +2840,36 @@ dnssdRegisterPrinter(cupsd_printer_t *p)
9829b01
   if (p->info && strlen(p->info) > 0)
9829b01
   {
9829b01
     if (DNSSDComputerName)
9829b01
-      snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDComputerName);
9829b01
+    {
9829b01
+     /*
9829b01
+      * Make sure there is room for at least 15 characters of
9829b01
+      * DNSSDComputerName.
9829b01
+      */
9829b01
+
9829b01
+      assert(sizeof(name) >= 15 + 4);
9829b01
+      nameptr = name + strlcpy(name, p->info,
9829b01
+			       sizeof(name) - 4 -
9829b01
+			       strnlen(DNSSDComputerName, 15));
9829b01
+      nameptr += strlcpy(nameptr, " @ ", sizeof(name) - (nameptr - name));
9829b01
+      strlcpy(nameptr, DNSSDComputerName, sizeof(name) - (nameptr - name));
9829b01
+    }
9829b01
     else
9829b01
       strlcpy(name, p->info, sizeof(name));
9829b01
   }
9829b01
   else if (DNSSDComputerName)
9829b01
-    snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDComputerName);
9829b01
+  {
9829b01
+   /*
9829b01
+    * Make sure there is room for at least 15 characters of
9829b01
+    * DNSSDComputerName.
9829b01
+    */
9829b01
+
9829b01
+    assert(sizeof(name) >= 15 + 4);
9829b01
+    nameptr = name + strlcpy(name, p->info,
9829b01
+			     sizeof(name) - 4 -
9829b01
+			     strnlen(DNSSDComputerName, 15));
9829b01
+    nameptr += strlcpy(nameptr, " @ ", sizeof(name) - (nameptr - name));
9829b01
+    strlcpy(nameptr, DNSSDComputerName, sizeof(name) - (nameptr - name));
9829b01
+  }
9829b01
   else
9829b01
     strlcpy(name, p->name, sizeof(name));
9829b01
 
a660226
@@ -2712,6 +2890,7 @@ dnssdRegisterPrinter(cupsd_printer_t *p)
9829b01
   * Register IPP and (optionally) LPD...
9829b01
   */
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
   ipp_len = 0;				/* anti-compiler-warning-code */
9829b01
   ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
9829b01
 
a660226
@@ -2884,6 +3063,209 @@ dnssdRegisterPrinter(cupsd_printer_t *p)
9829b01
 
9829b01
   if (printer_txt)
9829b01
     free(printer_txt);
9829b01
+#endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  if (!AvahiCupsClient)
9829b01
+   /*
9829b01
+    * Client not running yet.  The client callback will call us again later.
9829b01
+    */
9829b01
+    return;
9829b01
+
9829b01
+  ipp_txt = dnssdBuildTxtRecord(NULL, p, 0);
9829b01
+  printer_txt = dnssdBuildTxtRecord(NULL, p, 1);
9829b01
+  regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" : DNSSDRegType;
9829b01
+
9829b01
+  if (p->avahi_group && p->ipp_txt && ipp_txt &&
9829b01
+      !avahi_string_list_equal (p->ipp_txt, ipp_txt))
9829b01
+  {
9829b01
+   /*
9829b01
+    * Update the existing registration...
9829b01
+    */
9829b01
+
9829b01
+    avahi_string_list_free (p->ipp_txt);
9829b01
+
9829b01
+    if (p->printer_txt)
9829b01
+      avahi_string_list_free (p->printer_txt);
9829b01
+
9829b01
+   /*
e462076
+    * Update the service group entry.
9829b01
+    */
9829b01
+
e462076
+    regtype_copy = strdup (regtype);
e462076
+    subtype = strchr (regtype_copy, ',');
e462076
+    if (subtype)
e462076
+      *subtype = '\0';
e462076
+
e462076
+    cupsdLogMessage (CUPSD_LOG_DEBUG,
e462076
+		     "Updating TXT record for %s (%s)", name, regtype_copy);
e462076
+    ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
e462076
+						       AVAHI_IF_UNSPEC,
e462076
+						       AVAHI_PROTO_UNSPEC,
e462076
+						       0, name,
e462076
+						       regtype_copy,
e462076
+						       NULL, ipp_txt);
e462076
+    free (regtype_copy);
9829b01
+
e462076
+    if (ret < 0)
e462076
+      goto update_failed;
9829b01
+
9829b01
+    p->ipp_txt = ipp_txt;
9829b01
+    ipp_txt = NULL;
9829b01
+
9829b01
+    if (BrowseLocalProtocols & BROWSE_LPD)
9829b01
+    {
9829b01
+      ret = avahi_entry_group_update_service_txt_strlst (p->avahi_group,
9829b01
+							 AVAHI_IF_UNSPEC,
9829b01
+							 AVAHI_PROTO_UNSPEC,
9829b01
+							 0, name,
9829b01
+							 "_printer._tcp", NULL,
9829b01
+							 printer_txt);
9829b01
+      if (ret < 0)
9829b01
+	goto update_failed;
9829b01
+
9829b01
+      p->printer_txt = printer_txt;
9829b01
+      printer_txt = NULL;
9829b01
+    }
9829b01
+
9829b01
+    ret = avahi_entry_group_commit (p->avahi_group);
9829b01
+    if (ret < 0)
9829b01
+    {
9829b01
+    update_failed:
9829b01
+      cupsdLogMessage (CUPSD_LOG_ERROR,
9829b01
+		       "Failed to update TXT record for %s: %d",
9829b01
+		       name, ret);
9829b01
+      avahi_entry_group_reset (p->avahi_group);
9829b01
+      avahi_entry_group_free (p->avahi_group);
9829b01
+      p->avahi_group = NULL;
9829b01
+      ipp_txt = p->ipp_txt;
9829b01
+      p->ipp_txt = NULL;
9829b01
+    }
9829b01
+  }
9829b01
+
9829b01
+  if (!p->avahi_group)
9829b01
+  {
9829b01
+   /*
9829b01
+    * Initial registration.  Use the _fax subtype for fax queues...
9829b01
+    */
9829b01
+
9829b01
+    p->avahi_group = avahi_entry_group_new (AvahiCupsClient,
9829b01
+					    avahi_entry_group_cb,
9829b01
+					    p);
9829b01
+
9829b01
+    cupsdLogMessage(CUPSD_LOG_DEBUG,
9829b01
+		    "Registering Avahi printer %s with name \"%s\" and "
9829b01
+		    "type \"%s\"", p->name, name, regtype);
9829b01
+
9829b01
+    if (!p->avahi_group)
9829b01
+    {
9829b01
+      ret = 0;
9829b01
+      goto add_failed;
9829b01
+    }
9829b01
+
9829b01
+   /*
9829b01
+    * Add each service type (DNSSDRegType may contain several,
9829b01
+    * separated by commas).
9829b01
+    */
9829b01
+
e462076
+    subtype = regtype_copy = strdup (regtype);
e462076
+    while (subtype && *subtype)
9829b01
+    {
e462076
+      nextsubtype = strchr (subtype, ',');
e462076
+      if (nextsubtype)
e462076
+	*nextsubtype++ = '\0';
9829b01
+
e462076
+      if (subtype == regtype_copy)
e462076
+      {
e462076
+       /*
e462076
+	* Main type entry.
e462076
+	*/
e462076
+
e462076
+	cupsdLogMessage (CUPSD_LOG_DEBUG,
e462076
+			 "Adding TXT record for %s (%s)", name, regtype_copy);
e462076
+	ret = avahi_entry_group_add_service_strlst (p->avahi_group,
e462076
+						    AVAHI_IF_UNSPEC,
e462076
+						    AVAHI_PROTO_UNSPEC,
e462076
+						    0, name, regtype_copy,
e462076
+						    NULL, NULL,
e462076
+						    DNSSDPort,
e462076
+						    ipp_txt);
e462076
+      }
e462076
+      else
e462076
+      {
e462076
+       /*
e462076
+	* Sub-type entry.
e462076
+	*/
e462076
+
e462076
+	snprintf (fullsubtype, sizeof(fullsubtype),
e462076
+		  "%s._sub.%s", subtype, regtype_copy);
e462076
+	cupsdLogMessage (CUPSD_LOG_DEBUG,
e462076
+			 "Adding TXT record for %s (%s)", name, fullsubtype);
e462076
+	ret = avahi_entry_group_add_service_subtype (p->avahi_group,
e462076
+						     AVAHI_IF_UNSPEC,
e462076
+						     AVAHI_PROTO_UNSPEC,
e462076
+						     0, name,
e462076
+						     regtype_copy,
e462076
+						     NULL, fullsubtype);
e462076
+      }
9829b01
+
9829b01
+      if (ret < 0)
9829b01
+      {
9829b01
+	free (regtype_copy);
9829b01
+	goto add_failed;
9829b01
+      }
9829b01
+
e462076
+      subtype = nextsubtype;
9829b01
+    }
9829b01
+
9829b01
+    free (regtype_copy);
9829b01
+    p->ipp_txt = ipp_txt;
9829b01
+    ipp_txt = NULL;
9829b01
+
9829b01
+    if (BrowseLocalProtocols & BROWSE_LPD)
9829b01
+    {
9829b01
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
9829b01
+		      "Registering Avahi printer %s with name \"%s\" and "
9829b01
+		      "type \"_printer._tcp\"", p->name, name);
9829b01
+
9829b01
+      ret = avahi_entry_group_add_service_strlst (p->avahi_group,
9829b01
+						  AVAHI_IF_UNSPEC,
9829b01
+						  AVAHI_PROTO_UNSPEC,
9829b01
+						  0, name,
9829b01
+						  "_printer._tcp", NULL, NULL,
9829b01
+						  515,
9829b01
+						  printer_txt);
9829b01
+      if (ret < 0)
9829b01
+	goto add_failed;
9829b01
+
9829b01
+      p->printer_txt = printer_txt;
9829b01
+      printer_txt = NULL;
9829b01
+    }
9829b01
+
9829b01
+    ret = avahi_entry_group_commit (p->avahi_group);
9829b01
+
9829b01
+    if (ret < 0)
9829b01
+    {
9829b01
+    add_failed:
9829b01
+      cupsdLogMessage (CUPSD_LOG_ERROR,
9829b01
+		       "Failed to add Avahi entry for %s: %d",
9829b01
+		       name, ret);
9829b01
+      if (p->avahi_group)
9829b01
+      {
9829b01
+	avahi_entry_group_reset (p->avahi_group);
9829b01
+	avahi_entry_group_free (p->avahi_group);
9829b01
+	p->avahi_group = NULL;
9829b01
+      }
9829b01
+      ipp_txt = p->ipp_txt;
9829b01
+      p->ipp_txt = NULL;
9829b01
+    }
9829b01
+  }
9829b01
+
9829b01
+  if (ipp_txt)
9829b01
+    avahi_string_list_free (ipp_txt);
9829b01
+
9829b01
+  if (printer_txt)
9829b01
+    avahi_string_list_free (printer_txt);
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 }
9829b01
 
9829b01
 
a660226
@@ -2896,6 +3278,10 @@ dnssdStop(void)
9829b01
 {
9829b01
   cupsd_printer_t	*p;		/* Current printer */
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
+  if (!DNSSDRef)
9829b01
+    return;
9829b01
+#endif /* HAVE_DNSSD */
9829b01
 
9829b01
  /*
9829b01
   * De-register the individual printers
0d86476
@@ -2910,12 +3296,23 @@ dnssdStop(void)
9829b01
   * Shutdown the rest of the service refs...
9829b01
   */
0d86476
 
0d86476
+#ifdef HAVE_DNSSD
0d86476
   if (WebIFRef)
0d86476
   {
0d86476
     DNSServiceRefDeallocate(WebIFRef);
0d86476
     WebIFRef = NULL;
0d86476
   }
0d86476
+#endif /* HAVE_DNSSD */
0d86476
+#ifdef HAVE_AVAHI
0d86476
+  if (AvahiWebIFGroup)
0d86476
+  {
0d86476
+    avahi_entry_group_reset (AvahiWebIFGroup);
0d86476
+    avahi_entry_group_free (AvahiWebIFGroup);
0d86476
+    AvahiWebIFGroup = NULL;
0d86476
+  }
0d86476
+#endif /* HAVE_AVAHI */
0d86476
 
0d86476
+#ifdef HAVE_DNSSD
0d86476
   if (RemoteRef)
0d86476
   {
0d86476
     DNSServiceRefDeallocate(RemoteRef);
0d86476
@@ -2926,14 +3323,17 @@ dnssdStop(void)
9829b01
 
9829b01
   DNSServiceRefDeallocate(DNSSDRef);
9829b01
   DNSSDRef = NULL;
9829b01
+#endif /* HAVE_DNSSD */
9829b01
 
9829b01
   cupsArrayDelete(DNSSDPrinters);
9829b01
   DNSSDPrinters = NULL;
9829b01
 
9829b01
   DNSSDPort = 0;
9829b01
 }
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
 
9829b01
+#ifdef HAVE_DNSSD
9829b01
 /*
9829b01
  * 'dnssdUpdate()' - Handle DNS-SD queries.
9829b01
  */
0d86476
@@ -2955,6 +3355,153 @@ dnssdUpdate(void)
9829b01
 #endif /* HAVE_DNSSD */
9829b01
 
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+/*
9829b01
+ * 'avahiPackTxtRecord()' - Pack an array of key/value pairs into an
9829b01
+ *                          AvahiStringList.
9829b01
+ */
9829b01
+
9829b01
+static AvahiStringList *		/* O - new string list */
9829b01
+avahiPackTxtRecord(char *keyvalue[][2],	/* I - Table of key value pairs */
9829b01
+		   int count)		/* I - Number of items in table */
9829b01
+{
9829b01
+  AvahiStringList *strlst = NULL;
9829b01
+  char **elements;
9829b01
+  size_t len;
9829b01
+  int i = 0;
9829b01
+
9829b01
+  elements = malloc ((1 + count) * sizeof (char *));
9829b01
+  if (!elements)
9829b01
+    goto cleanup;
9829b01
+
9829b01
+  for (i = 0; i < count; i++)
9829b01
+    {
9829b01
+      len = (1 + strlen (keyvalue[i][0]) +
9829b01
+	     (keyvalue[i][1] ? 1 + strlen (keyvalue[i][1]) : 1));
9829b01
+      elements[i] = malloc (len * sizeof (char));
9829b01
+      if (!elements[i])
9829b01
+	goto cleanup;
9829b01
+
9829b01
+      snprintf (elements[i], len, "%s=%s", keyvalue[i][0], keyvalue[i][1]);
9829b01
+    }
9829b01
+
9829b01
+  strlst = avahi_string_list_new_from_array ((const char **) elements, count);
9829b01
+
9829b01
+cleanup:
9829b01
+  while (--i >= 0)
9829b01
+    free (elements[i]);
9829b01
+
9829b01
+  free (elements);
9829b01
+  return (strlst);
9829b01
+}
9829b01
+
9829b01
+
9829b01
+/*
9829b01
+ * 'avahi_entry_group_cb()' - Avahi entry group callback function.
9829b01
+ */
9829b01
+static void
9829b01
+avahi_entry_group_cb (AvahiEntryGroup *group,
9829b01
+		      AvahiEntryGroupState state,
9829b01
+		      void *userdata)
9829b01
+{
9829b01
+  char *name;
9829b01
+
9829b01
+  if (userdata)
9829b01
+    name = ((cupsd_printer_t *) userdata)->reg_name;
9829b01
+  else
9829b01
+    name = "CUPS web interface";
9829b01
+
9829b01
+  switch (state)
9829b01
+  {
9829b01
+  case AVAHI_ENTRY_GROUP_UNCOMMITED:
9829b01
+  case AVAHI_ENTRY_GROUP_REGISTERING:
9829b01
+    break;
9829b01
+
9829b01
+  case AVAHI_ENTRY_GROUP_ESTABLISHED:
9829b01
+    cupsdLogMessage (CUPSD_LOG_DEBUG,
9829b01
+		     "Avahi entry group established for %s", name);
9829b01
+    break;
9829b01
+
9829b01
+  default:
9829b01
+    cupsdLogMessage (CUPSD_LOG_DEBUG,
9829b01
+		     "Avahi entry group %s has state %d",
9829b01
+		     name, state);
9829b01
+    break;
9829b01
+  }
9829b01
+}
9829b01
+
9829b01
+
9829b01
+/*
9829b01
+ * 'avahi_client_cb()' - Avahi client callback function.
9829b01
+ */
9829b01
+static void
9829b01
+avahi_client_cb (AvahiClient *client,
9829b01
+		 AvahiClientState state,
9829b01
+		 void *userdata)
9829b01
+{
9829b01
+  cupsd_printer_t *printer;
9829b01
+  switch (state)
9829b01
+  {
9829b01
+  case AVAHI_CLIENT_S_RUNNING:
9829b01
+   /*
9829b01
+    * Avahi client started successfully.
9829b01
+    */
9829b01
+    AvahiCupsClient = client;
9829b01
+    AvahiCupsClientConnecting = 0;
9829b01
+    cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client started");
9829b01
+
9829b01
+    cupsdUpdateDNSSDName ();
9829b01
+
9829b01
+    for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
9829b01
+	 printer;
9829b01
+	 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
9829b01
+      if (Browsing && (BrowseLocalProtocols & BROWSE_DNSSD) &&
9829b01
+	  (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
9829b01
+			      CUPS_PRINTER_SCANNER))) && printer->shared)
9829b01
+	dnssdRegisterPrinter (printer);
9829b01
+
9829b01
+    break;
9829b01
+
9829b01
+  case AVAHI_CLIENT_CONNECTING:
9829b01
+   /*
9829b01
+    * No Avahi daemon, client is waiting.
9829b01
+    */
9829b01
+    cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client connecting");
9829b01
+    break;
9829b01
+
a660226
+  case AVAHI_CLIENT_S_REGISTERING:
a660226
+    /*
a660226
+     * Not yet registered.
a660226
+     */
a660226
+    cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client registering");
a660226
+    break;
a660226
+
9829b01
+  case AVAHI_CLIENT_FAILURE:
9829b01
+   /*
9829b01
+    * Avahi client failed, close it to allow a clean restart.
9829b01
+    */
9829b01
+    cupsdLogMessage (CUPSD_LOG_ERROR,
9829b01
+		     "Avahi client failed, "
9829b01
+		     "closing client to allow a clean restart");
9829b01
+
9829b01
+    for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
9829b01
+	 printer;
9829b01
+	 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
9829b01
+      dnssdDeregisterPrinter (printer);
9829b01
+
9829b01
+    avahi_client_free(client);
9829b01
+    AvahiCupsClientConnecting = 0;
9829b01
+    AvahiCupsClient = NULL;
9829b01
+
9829b01
+    break;
9829b01
+
9829b01
+  default:
9829b01
+    cupsdLogMessage (CUPSD_LOG_DEBUG, "Avahi client state: %d", state);
9829b01
+  }
9829b01
+}
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
+
9829b01
 /*
9829b01
  * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
9829b01
  */
0d86476
diff -up cups-1.5.2/scheduler/dirsvc.h.avahi-5-services cups-1.5.2/scheduler/dirsvc.h
0d86476
--- cups-1.5.2/scheduler/dirsvc.h.avahi-5-services	2011-03-21 02:12:14.000000000 +0000
0d86476
+++ cups-1.5.2/scheduler/dirsvc.h	2012-03-14 15:08:25.711611808 +0000
9829b01
@@ -31,6 +31,10 @@
9829b01
 #  endif /* HAVE_LDAP_SSL_H */
9829b01
 #endif /* HAVE_LDAP */
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+#  include <avahi-client/publish.h>
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
 /*
9829b01
  * Browse protocols...
9829b01
  */
9829b01
@@ -131,19 +135,22 @@ VAR int			PollPipe	VALUE(0);
9829b01
 VAR cupsd_statbuf_t	*PollStatusBuffer VALUE(NULL);
9829b01
 					/* Status buffer for pollers */
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
 VAR char		*DNSSDComputerName VALUE(NULL),
9829b01
 					/* Computer/server name */
9829b01
 			*DNSSDHostName	VALUE(NULL),
9829b01
 					/* Hostname */
9829b01
 			*DNSSDRegType VALUE(NULL);
9829b01
 					/* Bonjour registration type */
9829b01
-VAR cups_array_t	*DNSSDAlias	VALUE(NULL);
9829b01
-					/* List of dynamic ServerAlias's */
9829b01
 VAR int			DNSSDPort	VALUE(0);
9829b01
 					/* Port number to register */
9829b01
 VAR cups_array_t	*DNSSDPrinters	VALUE(NULL);
9829b01
 					/* Printers we have registered */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
+
9829b01
+#ifdef HAVE_DNSSD
9829b01
+VAR cups_array_t	*DNSSDAlias	VALUE(NULL);
9829b01
+					/* List of dynamic ServerAlias's */
9829b01
 VAR DNSServiceRef	DNSSDRef	VALUE(NULL),
9829b01
 					/* Master DNS-SD service reference */
9829b01
 			WebIFRef	VALUE(NULL),
9829b01
@@ -152,6 +159,17 @@ VAR DNSServiceRef	DNSSDRef	VALUE(NULL),
9829b01
 					/* Remote printer browse reference */
9829b01
 #endif /* HAVE_DNSSD */
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+VAR AvahiCupsPoll	*AvahiCupsPollHandle	VALUE(NULL);
9829b01
+					/* AvahiCupsPoll object */
9829b01
+VAR AvahiClient		*AvahiCupsClient	VALUE(NULL);
9829b01
+					/* AvahiClient object */
9829b01
+VAR int			AvahiCupsClientConnecting	VALUE(0);
9829b01
+					/* Is AvahiClient object connecting? */
9829b01
+VAR AvahiEntryGroup	*AvahiWebIFGroup	VALUE(NULL);
9829b01
+					/* Web interface entry group */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
 #ifdef HAVE_LIBSLP
9829b01
 VAR SLPHandle		BrowseSLPHandle	VALUE(NULL);
9829b01
 					/* SLP API handle */
6a13918
@@ -195,13 +213,14 @@ extern void	cupsdRegisterPrinter(cupsd_p
9829b01
 extern void	cupsdRestartPolling(void);
9829b01
 extern void	cupsdSaveRemoteCache(void);
9829b01
 extern void	cupsdSendBrowseList(void);
9829b01
+extern void	cupsdStartAvahiClient(void);
9829b01
 extern void	cupsdStartBrowsing(void);
9829b01
 extern void	cupsdStartPolling(void);
9829b01
 extern void	cupsdStopBrowsing(void);
9829b01
 extern void	cupsdStopPolling(void);
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
 extern void	cupsdUpdateDNSSDName(void);
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 #ifdef HAVE_LDAP
9829b01
 extern void	cupsdUpdateLDAPBrowse(void);
9829b01
 #endif /* HAVE_LDAP */
0d86476
diff -up cups-1.5.2/scheduler/ipp.c.avahi-5-services cups-1.5.2/scheduler/ipp.c
0d86476
--- cups-1.5.2/scheduler/ipp.c.avahi-5-services	2012-03-14 15:04:17.665305560 +0000
0d86476
+++ cups-1.5.2/scheduler/ipp.c	2012-03-14 15:08:25.715611813 +0000
0d86476
@@ -6099,7 +6099,7 @@ copy_printer_attrs(
9829b01
     ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
9829b01
                ippTimeToDate(curtime));
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
   if (!ra || cupsArrayFind(ra, "printer-dns-sd-name"))
9829b01
   {
9829b01
     if (printer->reg_name)
0d86476
@@ -6109,7 +6109,7 @@ copy_printer_attrs(
9829b01
       ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
9829b01
                    "printer-dns-sd-name", 0);
9829b01
   }
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
   if (!ra || cupsArrayFind(ra, "printer-error-policy"))
9829b01
     ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
0d86476
diff -up cups-1.5.2/scheduler/main.c.avahi-5-services cups-1.5.2/scheduler/main.c
0d86476
--- cups-1.5.2/scheduler/main.c.avahi-5-services	2012-03-14 15:06:36.511476986 +0000
0d86476
+++ cups-1.5.2/scheduler/main.c	2012-03-14 15:08:25.718611817 +0000
6a13918
@@ -120,6 +120,10 @@ main(int  argc,				/* I - Number of comm
9829b01
   cupsd_listener_t	*lis;		/* Current listener */
9829b01
   time_t		current_time,	/* Current time */
9829b01
 			activity,	/* Client activity timer */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+			avahi_client_time, /* Time for next Avahi client
9829b01
+					      check */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 			browse_time,	/* Next browse send time */
9829b01
 			senddoc_time,	/* Send-Document time */
9829b01
 			expire_time,	/* Subscription expire time */
6a13918
@@ -672,6 +676,9 @@ main(int  argc,				/* I - Number of comm
9829b01
   */
9829b01
 
9829b01
   current_time  = time(NULL);
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  avahi_client_time = current_time;
9829b01
+#endif /* HAVE_AVAHI */
9829b01
   browse_time   = current_time;
9829b01
   event_time    = current_time;
9829b01
   expire_time   = current_time;
6a13918
@@ -894,6 +901,16 @@ main(int  argc,				/* I - Number of comm
9829b01
     tmo = cupsdNextTimeout (&tmo_delay);
9829b01
     if (tmo && tmo_delay == 0)
9829b01
       cupsdRunTimeout (tmo);
9829b01
+
9829b01
+   /*
9829b01
+    * Try to restart the Avahi client every 10 seconds if needed...
9829b01
+    */
9829b01
+
9829b01
+    if ((current_time - avahi_client_time) >= 10)
9829b01
+    {
9829b01
+      avahi_client_time = current_time;
9829b01
+      cupsdStartAvahiClient();
9829b01
+    }
9829b01
 #endif /* HAVE_AVAHI */
9829b01
 
9829b01
 #ifndef __APPLE__
0d86476
diff -up cups-1.5.2/scheduler/printers.c.avahi-5-services cups-1.5.2/scheduler/printers.c
0d86476
--- cups-1.5.2/scheduler/printers.c.avahi-5-services	2012-03-14 15:04:17.646305537 +0000
0d86476
+++ cups-1.5.2/scheduler/printers.c	2012-03-14 15:08:25.720611819 +0000
9829b01
@@ -883,9 +883,9 @@ cupsdDeletePrinter(
9829b01
   cupsdClearString(&p->alert);
9829b01
   cupsdClearString(&p->alert_description);
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
   cupsdClearString(&p->pdl);
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 
9829b01
   cupsArrayDelete(p->filetypes);
9829b01
 
6a13918
@@ -3787,7 +3787,7 @@ add_printer_formats(cupsd_printer_t *p)
9829b01
     attr->values[i].string.text = _cupsStrAlloc(mimetype);
9829b01
   }
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
   {
9829b01
     char		pdl[1024];	/* Buffer to build pdl list */
9829b01
     mime_filter_t	*filter;	/* MIME filter looping var */
6a13918
@@ -3843,7 +3843,7 @@ add_printer_formats(cupsd_printer_t *p)
9829b01
 
9829b01
     cupsdSetString(&p->pdl, pdl);
9829b01
   }
9829b01
-#endif /* HAVE_DNSSD */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
 }
9829b01
 
9829b01
 
0d86476
diff -up cups-1.5.2/scheduler/printers.h.avahi-5-services cups-1.5.2/scheduler/printers.h
0d86476
--- cups-1.5.2/scheduler/printers.h.avahi-5-services	2011-03-18 18:42:46.000000000 +0000
0d86476
+++ cups-1.5.2/scheduler/printers.h	2012-03-14 15:08:25.721611820 +0000
9829b01
@@ -16,6 +16,9 @@
9829b01
 #ifdef HAVE_DNSSD
9829b01
 #  include <dns_sd.h>
9829b01
 #endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+#  include "avahi.h"
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 #include <cups/pwg-private.h>
9829b01
 
9829b01
 
9829b01
@@ -95,16 +98,23 @@ struct cupsd_printer_s
9829b01
   time_t	marker_time;		/* Last time marker attributes were updated */
9829b01
   _ppd_cache_t	*pc;			/* PPD cache and mapping data */
9829b01
 
9829b01
-#ifdef HAVE_DNSSD
9829b01
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
9829b01
   char		*reg_name,		/* Name used for service registration */
9829b01
-		*pdl,			/* pdl value for TXT record */
9829b01
-		*ipp_txt,		/* IPP TXT record contents */
9829b01
+		*pdl;			/* pdl value for TXT record */
9829b01
+#endif /* defined(HAVE_DNSSD) || defined(HAVE_AVAHI) */
9829b01
+#ifdef HAVE_DNSSD
9829b01
+  char		*ipp_txt,		/* IPP TXT record contents */
9829b01
 		*printer_txt;		/* LPD TXT record contents */
9829b01
   int		ipp_len,		/* IPP TXT record length */
9829b01
 		printer_len;		/* LPD TXT record length */
9829b01
   DNSServiceRef	ipp_ref,		/* Reference for _ipp._tcp,_cups */
9829b01
 		printer_ref;		/* Reference for _printer._tcp */
9829b01
 #endif /* HAVE_DNSSD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  AvahiStringList *ipp_txt,		/* IPP TXT record */
9829b01
+		*printer_txt;		/* LPD TXT record */
9829b01
+  AvahiEntryGroup *avahi_group;		/* Avahi entry group */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 };
9829b01
 
9829b01