dd27f8
diff -up cups-1.4.0/backend/dnssd.c.avahi cups-1.4.0/backend/dnssd.c
dd27f8
--- cups-1.4.0/backend/dnssd.c.avahi	2009-08-07 23:27:12.000000000 +0100
081f3e
+++ cups-1.4.0/backend/dnssd.c	2009-09-04 14:57:04.730388833 +0100
111438
@@ -22,6 +22,7 @@
a1088d
  *   exec_backend()          - Execute the backend that corresponds to the
a1088d
  *                             resolved service name.
b85faf
  *   get_device()            - Create or update a device.
111438
+*    find_device()
111438
  *   query_callback()        - Process query data.
111438
  *   sigterm_handler()       - Handle termination signals...
b85faf
  *   unquote()               - Unquote a name string.
111438
@@ -33,7 +34,18 @@
b85faf
 
b85faf
 #include "backend-private.h"
b85faf
 #include <cups array.h="">
b85faf
-#include <dns_sd.h>
111438
+#ifdef HAVE_DNSSD
111438
+#  include <dns_sd.h>
111438
+#endif /* HAVE_DNSSD */
b85faf
+#ifdef HAVE_AVAHI
111438
+#  include <avahi-client client.h="">
111438
+#  include <avahi-client lookup.h="">
111438
+#  include <avahi-common simple-watch.h="">
111438
+#  include <avahi-common domain.h="">
111438
+#  include <avahi-common error.h="">
111438
+#  include <avahi-common malloc.h="">
b85faf
+#define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
111438
+#endif /* HAVE_AVAHI */
b85faf
 
b85faf
 
b85faf
 /*
081f3e
@@ -52,7 +64,12 @@ typedef enum
b85faf
 
b85faf
 typedef struct
b85faf
 {
b85faf
+#ifdef HAVE_DNSSD
b85faf
   DNSServiceRef	ref;			/* Service reference for resolve */
b85faf
+#endif /* HAVE_DNSSD */
081f3e
+#ifdef HAVE_AVAHI
081f3e
+  int		resolved;		/* Did we resolve the device? */
081f3e
+#endif /* HAVE_AVAHI */
b85faf
   char		*name,			/* Service name */
b85faf
 		*domain,		/* Domain name */
b85faf
 		*fullName,		/* Full name */
081f3e
@@ -64,6 +81,20 @@ typedef struct
111438
 		sent;			/* Did we list the device? */
111438
 } cups_device_t;
111438
 
111438
+typedef struct
111438
+{
111438
+  char key[256];
111438
+  char value[256];
111438
+
111438
+#ifdef HAVE_DNSSD
111438
+  const uint8_t *data;
111438
+  const uint8_t *datanext;
111438
+  const uint8_t *dataend;
111438
+#else /* HAVE_AVAHI */
111438
+  AvahiStringList *txt;
111438
+#endif /* HAVE_DNSSD */
111438
+} cups_txt_records_t;
111438
+
111438
 
111438
 /*
111438
  * Local globals...
081f3e
@@ -77,6 +108,7 @@ static int		job_canceled = 0;
b85faf
  * Local functions...
b85faf
  */
b85faf
 
111438
+#ifdef HAVE_DNSSD
b85faf
 static void		browse_callback(DNSServiceRef sdRef,
b85faf
 			                DNSServiceFlags flags,
b85faf
 				        uint32_t interfaceIndex,
081f3e
@@ -92,12 +124,6 @@ static void		browse_local_callback(DNSSe
b85faf
 					      const char *regtype,
b85faf
 					      const char *replyDomain,
b85faf
 					      void *context);
b85faf
-static int		compare_devices(cups_device_t *a, cups_device_t *b);
b85faf
-static void		exec_backend(char **argv);
b85faf
-static cups_device_t	*get_device(cups_array_t *devices,
b85faf
-			            const char *serviceName,
b85faf
-			            const char *regtype,
b85faf
-				    const char *replyDomain);
b85faf
 static void		query_callback(DNSServiceRef sdRef,
b85faf
 			               DNSServiceFlags flags,
b85faf
 				       uint32_t interfaceIndex,
081f3e
@@ -106,9 +132,111 @@ static void		query_callback(DNSServiceRe
b85faf
 				       uint16_t rrclass, uint16_t rdlen,
b85faf
 				       const void *rdata, uint32_t ttl,
b85faf
 				       void *context);
111438
+#endif /* HAVE_DNSSD */
111438
+#ifdef HAVE_AVAHI
111438
+static void		avahi_client_callback (AvahiClient *client,
111438
+					       AvahiClientState state,
111438
+					       void *context);
111438
+static void		avahi_browse_callback (AvahiServiceBrowser *browser,
111438
+					       AvahiIfIndex interface,
111438
+					       AvahiProtocol protocol,
111438
+					       AvahiBrowserEvent event,
111438
+					       const char *serviceName,
111438
+					       const char *regtype,
111438
+					       const char *replyDomain,
111438
+					       AvahiLookupResultFlags flags,
111438
+					       void *context);
111438
+#endif /* HAVE_AVAHI */
b85faf
+
111438
+static cups_device_t *	find_device (cups_array_t *devices,
111438
+				     cups_txt_records_t *txt,
111438
+				     cups_device_t *dkey);
b85faf
+static int		compare_devices(cups_device_t *a, cups_device_t *b);
b85faf
+static void		exec_backend(char **argv);
b85faf
+static cups_device_t	*get_device(cups_array_t *devices,
b85faf
+			            const char *serviceName,
b85faf
+			            const char *regtype,
b85faf
+				    const char *replyDomain);
111438
 static void		sigterm_handler(int sig);
b85faf
 static void		unquote(char *dst, const char *src, size_t dstsize);
111438
 
b85faf
+#ifdef HAVE_AVAHI
b85faf
+static AvahiSimplePoll *simple_poll = NULL;
b85faf
+static int avahi_got_callback;
b85faf
+#endif /* HAVE_AVAHI */
111438
+
111438
+
111438
+/*
111438
+ * cups_txt_records_t access functions
111438
+ */
111438
+static cups_txt_records_t *
111438
+next_txt_record (cups_txt_records_t *txt)
111438
+{
111438
+#ifdef HAVE_DNSSD
111438
+  txt->data = txt->datanext;
111438
+#else /* HAVE_AVAHI */
111438
+  txt->txt = avahi_string_list_get_next (txt->txt);
111438
+  if (txt->txt == NULL)
111438
+    return NULL;
111438
+#endif /* HAVE_DNSSD */
111438
+
111438
+  return txt;
111438
+}
111438
+
111438
+static int
111438
+parse_txt_record_pair (cups_txt_records_t *txt)
111438
+{
111438
+#ifdef HAVE_DNSSD
111438
+  uint8_t	datalen;
111438
+  uint8_t	*data = txt->data;
111438
+  char		*ptr;
111438
+
111438
+ /*
111438
+  * Read a key/value pair starting with an 8-bit length.  Since the
111438
+  * length is 8 bits and the size of the key/value buffers is 256, we
111438
+  * don't need to check for overflow...
111438
+  */
111438
+
111438
+  datalen = *data++;
111438
+  if (!datalen || (data + datalen) >= txt->dataend)
111438
+    return NULL;
111438
+  txt->datanext = data + datalen;
111438
+
111438
+  for (ptr = txt->key; data < txt->datanext && *data != '='; data ++)
111438
+    *ptr++ = *data;
111438
+  *ptr = '\0';
111438
+
111438
+  if (data < txt->datanext && *data == '=')
111438
+  {
111438
+    data++;
111438
+
111438
+    if (data < datanext)
111438
+      memcpy (txt->value, data, txt->datanext - data);
111438
+    value[txt->datanext - data] = '\0';
111438
+  }
111438
+  else
111438
+    return 1;
111438
+#else /* HAVE_AVAHI */
111438
+  char *key, *value;
111438
+  size_t len;
111438
+  avahi_string_list_get_pair (txt->txt, &key, &value, &len);
111438
+  if (len > sizeof (txt->value) - 1)
111438
+    len = sizeof (txt->value) - 1;
111438
+
111438
+  memcpy (txt->value, value, len);
111438
+  txt->value[len] = '\0';
111438
+  len = strlen (key);
111438
+  if (len > sizeof (txt->key) - 1)
111438
+    len = sizeof (txt->key) - 1;
111438
+
111438
+  memcpy (txt->key, key, len);
111438
+  txt->key[len] = '\0';
111438
+  avahi_free (key);
111438
+  avahi_free (value);
111438
+#endif /* HAVE_AVAHI */
111438
+
111438
+  return 0;
111438
+}
b85faf
 
b85faf
 /*
111438
  * 'main()' - Browse for printers.
081f3e
@@ -119,6 +247,13 @@ main(int  argc,				/* I - Number of comm
b85faf
      char *argv[])			/* I - Command-line arguments */
b85faf
 {
b85faf
   const char	*name;			/* Backend name */
b85faf
+  cups_array_t	*devices;		/* Device array */
b85faf
+  cups_device_t	*device;		/* Current device */
b85faf
+  char		uriName[1024];		/* Unquoted fullName for URI */
111438
+#ifdef HAVE_DNSSD
111438
+  int		fd;			/* Main file descriptor */
111438
+  fd_set	input;			/* Input set for select() */
111438
+  struct timeval timeout;		/* Timeout for select() */
b85faf
   DNSServiceRef	main_ref,		/* Main service reference */
b85faf
 		fax_ipp_ref,		/* IPP fax service reference */
b85faf
 		ipp_ref,		/* IPP service reference */
081f3e
@@ -130,12 +265,11 @@ main(int  argc,				/* I - Number of comm
b85faf
 		pdl_datastream_ref,	/* AppSocket service reference */
b85faf
 		printer_ref,		/* LPD service reference */
b85faf
 		riousbprint_ref;	/* Remote IO service reference */
b85faf
-  int		fd;			/* Main file descriptor */
b85faf
-  fd_set	input;			/* Input set for select() */
b85faf
-  struct timeval timeout;		/* Timeout for select() */
b85faf
-  cups_array_t	*devices;		/* Device array */
b85faf
-  cups_device_t	*device;		/* Current device */
a1088d
-  char		uriName[1024];		/* Unquoted fullName for URI */
111438
+#endif /* HAVE_DNSSD */
111438
+#ifdef HAVE_AVAHI
111438
+  AvahiClient	*client;
111438
+  int		error;
111438
+#endif /* HAVE_AVAHI */
111438
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
111438
   struct sigaction action;		/* Actions for POSIX signals */
111438
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
081f3e
@@ -194,6 +328,49 @@ main(int  argc,				/* I - Number of comm
b85faf
   * Browse for different kinds of printers...
b85faf
   */
b85faf
 
b85faf
+#ifdef HAVE_AVAHI
b85faf
+  if ((simple_poll = avahi_simple_poll_new ()) == NULL)
b85faf
+  {
b85faf
+    perror ("ERROR: Unable to create avahi simple poll object");
b85faf
+    return (1);
b85faf
+  }
b85faf
+
b85faf
+  client = avahi_client_new (avahi_simple_poll_get (simple_poll),
b85faf
+			     0, avahi_client_callback, NULL, &error);
b85faf
+  if (!client)
b85faf
+  {
b85faf
+    perror ("ERROR: Unable to create avahi client");
b85faf
+    return (1);
b85faf
+  }
b85faf
+
b85faf
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
b85faf
+			     AVAHI_PROTO_UNSPEC,
b85faf
+			     "_fax-ipp._tcp", NULL, 0,
b85faf
+			     avahi_browse_callback, devices);
b85faf
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
b85faf
+			     AVAHI_PROTO_UNSPEC,
b85faf
+			     "_ipp._tcp", NULL, 0,
b85faf
+			     avahi_browse_callback, devices);
b85faf
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
b85faf
+			     AVAHI_PROTO_UNSPEC,
b85faf
+			     "_ipp-tls._tcp", NULL, 0,
b85faf
+			     avahi_browse_callback, devices);
b85faf
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
b85faf
+			     AVAHI_PROTO_UNSPEC,
b85faf
+			     "_pdl-datastream._tcp",
b85faf
+			     NULL, 0,
b85faf
+			     avahi_browse_callback,
b85faf
+			     devices);
b85faf
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
b85faf
+			     AVAHI_PROTO_UNSPEC,
b85faf
+			     "_printer._tcp", NULL, 0,
b85faf
+			     avahi_browse_callback, devices);
b85faf
+  avahi_service_browser_new (client, AVAHI_IF_UNSPEC,
b85faf
+			     AVAHI_PROTO_UNSPEC,
b85faf
+			     "_riousbprint._tcp", NULL, 0,
b85faf
+			     avahi_browse_callback, devices);
111438
+#endif /* HAVE_AVAHI */
111438
+#ifdef HAVE_DNSSD
b85faf
   if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError)
b85faf
   {
b85faf
     perror("ERROR: Unable to create service connection");
081f3e
@@ -245,6 +422,7 @@ main(int  argc,				/* I - Number of comm
b85faf
   riousbprint_ref = main_ref;
b85faf
   DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0,
b85faf
                    "_riousbprint._tcp", NULL, browse_callback, devices);
111438
+#endif /* HAVE_DNSSD */
b85faf
 
b85faf
  /*
b85faf
   * Loop until we are killed...
081f3e
@@ -252,6 +430,9 @@ main(int  argc,				/* I - Number of comm
b85faf
 
111438
   while (!job_canceled)
b85faf
   {
b85faf
+    int announce = 0;
b85faf
+
111438
+#ifdef HAVE_DNSSD
111438
     FD_ZERO(&input);
111438
     FD_SET(fd, &input);
111438
 
081f3e
@@ -271,11 +452,35 @@ main(int  argc,				/* I - Number of comm
111438
     }
111438
     else
111438
     {
111438
+      announce = 1;
111438
+    }
111438
+#else /* HAVE_AVAHI */
b85faf
+    int r;
b85faf
+    avahi_got_callback = 0;
b85faf
+    r = avahi_simple_poll_iterate (simple_poll, 1);
b85faf
+    if (r != 0 && r != EINTR)
b85faf
+    {
b85faf
+     /*
b85faf
+      * We've been told to exit the loop.  Perhaps the connection to
b85faf
+      * avahi failed.
b85faf
+      */
b85faf
+
b85faf
+      break;
b85faf
+    }
b85faf
+
b85faf
+    if (avahi_got_callback)
b85faf
+      announce = 1;
111438
+#endif /* HAVE_DNSSD */
b85faf
+
b85faf
+    if (announce)
b85faf
+    {
b85faf
      /*
b85faf
       * Announce any devices we've found...
b85faf
       */
b85faf
 
111438
+#ifdef HAVE_DNSSD
b85faf
       DNSServiceErrorType status;	/* DNS query status */
111438
+#endif /* HAVE_DNSSD */
b85faf
       cups_device_t *best;		/* Best matching device */
b85faf
       char	device_uri[1024];	/* Device URI */
b85faf
       int	count;			/* Number of queries */
081f3e
@@ -285,6 +490,7 @@ main(int  argc,				/* I - Number of comm
b85faf
                best = NULL, count = 0;
b85faf
            device;
b85faf
 	   device = (cups_device_t *)cupsArrayNext(devices))
111438
+#ifdef HAVE_DNSSD
b85faf
         if (!device->ref && !device->sent)
b85faf
 	{
b85faf
 	 /*
081f3e
@@ -313,14 +519,23 @@ main(int  argc,				/* I - Number of comm
b85faf
 	      count ++;
b85faf
           }
b85faf
 	}
b85faf
-	else if (!device->sent)
b85faf
+	else
111438
+#endif /* HAVE_DNSSD */
081f3e
+#ifdef HAVE_AVAHI
081f3e
+	if (!device->resolved)
081f3e
+	  continue;
081f3e
+        else
081f3e
+#endif /* HAVE_AVAHI */
b85faf
+	if (!device->sent)
b85faf
 	{
111438
+#ifdef HAVE_DNSSD
b85faf
 	 /*
b85faf
 	  * Got the TXT records, now report the device...
b85faf
 	  */
b85faf
 
b85faf
 	  DNSServiceRefDeallocate(device->ref);
b85faf
 	  device->ref = 0;
111438
+#endif /* HAVE_DNSSD */
b85faf
 
b85faf
           if (!best)
b85faf
 	    best = device;
081f3e
@@ -372,6 +587,7 @@ main(int  argc,				/* I - Number of comm
111438
  * 'browse_callback()' - Browse devices.
111438
  */
111438
 
111438
+#ifdef HAVE_DNSSD
111438
 static void
111438
 browse_callback(
111438
     DNSServiceRef       sdRef,		/* I - Service reference */
081f3e
@@ -405,12 +621,14 @@ browse_callback(
111438
 
111438
   get_device((cups_array_t *)context, serviceName, regtype, replyDomain);
111438
 }
111438
+#endif /* HAVE_DNSSD */
111438
 
111438
 
111438
 /*
111438
  * 'browse_local_callback()' - Browse local devices.
111438
  */
111438
 
111438
+#ifdef HAVE_DNSSD
111438
 static void
111438
 browse_local_callback(
111438
     DNSServiceRef       sdRef,		/* I - Service reference */
081f3e
@@ -456,6 +674,7 @@ browse_local_callback(
111438
 	  device->fullName);
111438
   device->sent = 1;
b85faf
 }
111438
+#endif /* HAVE_DNSSD */
b85faf
 
b85faf
 
111438
 /*
081f3e
@@ -528,6 +747,32 @@ exec_backend(char **argv)		/* I - Comman
111438
   exit(CUPS_BACKEND_STOP);
111438
 }
111438
 
111438
+static int
111438
+device_type (const char *regtype)
111438
+{
111438
+#ifdef HAVE_AVAHI
111438
+  if (!strcmp(regtype, "_ipp._tcp") ||
111438
+      !strcmp(regtype, "_ipp-tls._tcp"))
111438
+    return (CUPS_DEVICE_IPP);
111438
+  else if (!strcmp(regtype, "_fax-ipp._tcp"))
111438
+    return (CUPS_DEVICE_FAX_IPP);
111438
+  else if (!strcmp(regtype, "_printer._tcp"))
111438
+    return (CUPS_DEVICE_PDL_DATASTREAM);
111438
+#else
111438
+  if (!strcmp(regtype, "_ipp._tcp.") ||
111438
+      !strcmp(regtype, "_ipp-tls._tcp."))
111438
+    return (CUPS_DEVICE_IPP);
111438
+  else if (!strcmp(regtype, "_fax-ipp._tcp."))
111438
+    return (CUPS_DEVICE_FAX_IPP);
111438
+  else if (!strcmp(regtype, "_printer._tcp."))
111438
+    return (CUPS_DEVICE_PRINTER);
111438
+  else if (!strcmp(regtype, "_pdl-datastream._tcp."))
111438
+    return (CUPS_DEVICE_PDL_DATASTREAM);
111438
+#endif /* HAVE_AVAHI */
111438
+
111438
+  return (CUPS_DEVICE_RIOUSBPRINT);
111438
+}
111438
+
111438
 
111438
 /*
111438
  * 'get_device()' - Create or update a device.
081f3e
@@ -550,18 +795,7 @@ get_device(cups_array_t *devices,	/* I -
111438
   */
111438
 
111438
   key.name = (char *)serviceName;
111438
-
111438
-  if (!strcmp(regtype, "_ipp._tcp.") ||
111438
-      !strcmp(regtype, "_ipp-tls._tcp."))
111438
-    key.type = CUPS_DEVICE_IPP;
111438
-  else if (!strcmp(regtype, "_fax-ipp._tcp."))
111438
-    key.type = CUPS_DEVICE_FAX_IPP;
111438
-  else if (!strcmp(regtype, "_printer._tcp."))
111438
-    key.type = CUPS_DEVICE_PRINTER;
111438
-  else if (!strcmp(regtype, "_pdl-datastream._tcp."))
111438
-    key.type = CUPS_DEVICE_PDL_DATASTREAM;
111438
-  else
111438
-    key.type = CUPS_DEVICE_RIOUSBPRINT;
111438
+  key.type = device_type (regtype);
111438
 
111438
   for (device = cupsArrayFind(devices, &key);
111438
        device;
081f3e
@@ -581,8 +815,14 @@ get_device(cups_array_t *devices,	/* I -
111438
         free(device->domain);
111438
 	device->domain = strdup(replyDomain);
111438
 
111438
+#ifdef HAVE_DNSSD
111438
 	DNSServiceConstructFullName(fullName, device->name, regtype,
111438
 	                            replyDomain);
111438
+#else /* HAVE_AVAHI */
111438
+	avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
111438
+				 serviceName, regtype, replyDomain);
111438
+#endif /* HAVE_DNSSD */
111438
+
111438
 	free(device->fullName);
111438
 	device->fullName = strdup(fullName);
111438
       }
081f3e
@@ -602,6 +842,9 @@ get_device(cups_array_t *devices,	/* I -
081f3e
   device->domain   = strdup(replyDomain);
081f3e
   device->type     = key.type;
081f3e
   device->priority = 50;
081f3e
+#ifdef HAVE_AVAHI
081f3e
+  device->resolved = 0;
081f3e
+#endif /* HAVE_AVAHI */
081f3e
 
081f3e
   cupsArrayAdd(devices, device);
081f3e
 
081f3e
@@ -609,7 +852,13 @@ get_device(cups_array_t *devices,	/* I -
111438
   * Set the "full name" of this service, which is used for queries...
111438
   */
111438
 
111438
+#ifdef HAVE_DNSSD
111438
   DNSServiceConstructFullName(fullName, serviceName, regtype, replyDomain);
111438
+#else /* HAVE_AVAHI */
111438
+  avahi_service_name_join (fullName, kDNSServiceMaxDomainName,
111438
+			   serviceName, regtype, replyDomain);
111438
+#endif /* HAVE_DNSSD */
111438
+
111438
   device->fullName = strdup(fullName);
111438
 
111438
   return (device);
081f3e
@@ -620,6 +869,7 @@ get_device(cups_array_t *devices,	/* I -
111438
  * 'query_callback()' - Process query data.
111438
  */
111438
 
111438
+#ifdef HAVE_DNSSD
111438
 static void
111438
 query_callback(
111438
     DNSServiceRef       sdRef,		/* I - Service reference */
081f3e
@@ -639,7 +889,7 @@ query_callback(
111438
 		*ptr;			/* Pointer into string */
111438
   cups_device_t	dkey,			/* Search key */
111438
 		*device;		/* Device */
111438
-
111438
+  cups_txt_records_t txt;
111438
 
111438
   fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, "
111438
                   "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
081f3e
@@ -673,84 +923,212 @@ query_callback(
111438
   if ((ptr = strstr(name, "._")) != NULL)
111438
     *ptr = '\0';
111438
 
111438
-  if (strstr(fullName, "_ipp._tcp.") ||
111438
-      strstr(fullName, "_ipp-tls._tcp."))
111438
-    dkey.type = CUPS_DEVICE_IPP;
111438
-  else if (strstr(fullName, "_fax-ipp._tcp."))
111438
-    dkey.type = CUPS_DEVICE_FAX_IPP;
111438
-  else if (strstr(fullName, "_printer._tcp."))
111438
-    dkey.type = CUPS_DEVICE_PRINTER;
111438
-  else if (strstr(fullName, "_pdl-datastream._tcp."))
111438
-    dkey.type = CUPS_DEVICE_PDL_DATASTREAM;
111438
+  dkey.type = device_type (fullName);
111438
+
111438
+  txt.data = rdata;
111438
+  txt.dataend = rdata + rdlen;
111438
+  device = find_device ((cups_array_t *) context, &txt, &dkey);
111438
+  if (!device)
111438
+    fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
111438
+}
111438
+#endif /* HAVE_DNSSD */
111438
+
b85faf
+#ifdef HAVE_AVAHI
b85faf
+static void
111438
+avahi_client_callback(AvahiClient *client,
111438
+		      AvahiClientState state,
111438
+		      void *context)
b85faf
+{
b85faf
+ /*
b85faf
+  * If the connection drops, quit.
b85faf
+  */
b85faf
+
b85faf
+  if (state == AVAHI_CLIENT_FAILURE)
b85faf
+  {
b85faf
+    fprintf (stderr, "ERROR: Avahi connection failed\n");
b85faf
+    avahi_simple_poll_quit (simple_poll);
b85faf
+  }
b85faf
+}
b85faf
+
b85faf
+static void
111438
+avahi_query_callback(AvahiServiceResolver *resolver,
111438
+		     AvahiIfIndex interface,
111438
+		     AvahiProtocol protocol,
111438
+		     AvahiResolverEvent event,
111438
+		     const char *name,
111438
+		     const char *type,
111438
+		     const char *domain,
111438
+		     const char *host_name,
111438
+		     const AvahiAddress *address,
111438
+		     uint16_t port,
111438
+		     AvahiStringList *txt,
111438
+		     AvahiLookupResultFlags flags,
111438
+		     void *context)
b85faf
+{
b85faf
+  AvahiClient		*client;
b85faf
+  cups_device_t		key,
b85faf
+			*device;
b85faf
+  char			uqname[1024],
b85faf
+			*ptr;
111438
+  cups_txt_records_t	txtr;
b85faf
+
b85faf
+  client = avahi_service_resolver_get_client (resolver);
b85faf
+  if (event != AVAHI_RESOLVER_FOUND)
b85faf
+  {
b85faf
+    if (event == AVAHI_RESOLVER_FAILURE)
b85faf
+    {
b85faf
+      fprintf (stderr, "ERROR: %s\n",
b85faf
+	       avahi_strerror (avahi_client_errno (client)));
b85faf
+    }
b85faf
+
b85faf
+    avahi_service_resolver_free (resolver);
b85faf
+    return;
b85faf
+  }
b85faf
+
b85faf
+ /*
b85faf
+  * Set search key for device.
b85faf
+  */
b85faf
+
b85faf
+  key.name = uqname;
b85faf
+  unquote (uqname, name, sizeof (uqname));
b85faf
+  if ((ptr = strstr(name, "._")) != NULL)
b85faf
+    *ptr = '\0';
b85faf
+
b85faf
+  key.domain = (char *) domain;
b85faf
+  key.type = device_type (type);
b85faf
+
b85faf
+ /*
b85faf
+  * Find the device and the the TXT information.
b85faf
+  */
b85faf
+
111438
+  txtr.txt = txt;
111438
+  device = find_device ((cups_array_t *) context, &txtr, &key);
b85faf
+  if (device)
b85faf
+  {
b85faf
+   /*
b85faf
+    * Let the main loop know to announce the device.
b85faf
+    */
b85faf
+
081f3e
+    device->resolved = 1;
b85faf
+    avahi_got_callback = 1;
b85faf
+  }
111438
   else
111438
-    dkey.type = CUPS_DEVICE_RIOUSBPRINT;
b85faf
+    fprintf (stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", name);
081f3e
+
b85faf
+  avahi_service_resolver_free (resolver);
b85faf
+}
b85faf
+
b85faf
+static void
111438
+avahi_browse_callback(AvahiServiceBrowser *browser,
111438
+		      AvahiIfIndex interface,
111438
+		      AvahiProtocol protocol,
111438
+		      AvahiBrowserEvent event,
111438
+		      const char *name,
111438
+		      const char *type,
111438
+		      const char *domain,
111438
+		      AvahiLookupResultFlags flags,
111438
+		      void *context)
b85faf
+{
b85faf
+  AvahiClient *client = avahi_service_browser_get_client (browser);
b85faf
+
b85faf
+  switch (event)
b85faf
+  {
b85faf
+  case AVAHI_BROWSER_FAILURE:
b85faf
+    fprintf (stderr, "ERROR: %s\n",
b85faf
+	     avahi_strerror (avahi_client_errno (client)));
b85faf
+    avahi_simple_poll_quit (simple_poll);
b85faf
+    return;
b85faf
+
b85faf
+  case AVAHI_BROWSER_NEW:
b85faf
+   /*
b85faf
+    * This object is new on the network.
b85faf
+    */
b85faf
+
b85faf
+    if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
b85faf
+    {
b85faf
+     /*
b85faf
+      * This comes from the local machine so ignore it.
b85faf
+      */
b85faf
+
b85faf
+      fprintf (stderr, "DEBUG: ignoring local service %s\n", name);
b85faf
+    }
b85faf
+    else
b85faf
+    {
b85faf
+     /*
b85faf
+      * Create a device entry for it if it doesn't yet exist.
b85faf
+      */
b85faf
+
b85faf
+      get_device ((cups_array_t *)context, name, type, domain);
b85faf
+
b85faf
+     /*
b85faf
+      * Now look for a TXT entry.
b85faf
+      */
b85faf
+
b85faf
+      if (avahi_service_resolver_new (client, interface, protocol,
b85faf
+				      name, type, domain,
b85faf
+				      AVAHI_PROTO_UNSPEC, 0,
b85faf
+				      avahi_query_callback, context) == NULL)
b85faf
+      {
b85faf
+	fprintf (stderr, "ERROR: failed to resolve service %s: %s\n",
b85faf
+		 name, avahi_strerror (avahi_client_errno (client)));
b85faf
+      }
b85faf
+    }
b85faf
+
b85faf
+    break;
081f3e
 
081f3e
-  for (device = cupsArrayFind(devices, &dkey);
b85faf
+  case AVAHI_BROWSER_REMOVE:
b85faf
+  case AVAHI_BROWSER_ALL_FOR_NOW:
b85faf
+  case AVAHI_BROWSER_CACHE_EXHAUSTED:
b85faf
+    break;
b85faf
+  }  
b85faf
+}
111438
+#endif /* HAVE_AVAHI */
b85faf
+
b85faf
+static cups_device_t *
b85faf
+find_device (cups_array_t *devices,
111438
+	     cups_txt_records_t *txt,
111438
+	     cups_device_t *dkey)
b85faf
+{
111438
+  cups_device_t	*device;
111438
+  char		*ptr;
b85faf
+
111438
+  for (device = cupsArrayFind(devices, dkey);
b85faf
        device;
b85faf
        device = cupsArrayNext(devices))
b85faf
   {
111438
-    if (strcasecmp(device->name, dkey.name) ||
111438
-        strcasecmp(device->domain, dkey.domain))
111438
+    if (strcasecmp(device->name, dkey->name) ||
111438
+        strcasecmp(device->domain, dkey->domain))
b85faf
     {
b85faf
       device = NULL;
b85faf
       break;
b85faf
     }
111438
-    else if (device->type == dkey.type)
111438
+    else if (device->type == dkey->type)
b85faf
     {
b85faf
      /*
b85faf
       * Found it, pull out the priority and make and model from the TXT
b85faf
       * record and save it...
b85faf
       */
b85faf
 
111438
-      const uint8_t	*data,		/* Pointer into data */
111438
-			*datanext,	/* Next key/value pair */
111438
-			*dataend;	/* End of entire TXT record */
111438
-      uint8_t		datalen;	/* Length of current key/value pair */
111438
-      char		key[256],	/* Key string */
111438
-			value[256],	/* Value string */
111438
-			make_and_model[512],
111438
+      char		make_and_model[512],
111438
 				      	/* Manufacturer and model */
111438
 			model[256],	/* Model */
111438
-			device_id[2048];/* 1284 device ID */
b85faf
-
111438
+			device_id[2048]; /* 1284 device ID */
b85faf
 
111438
       device_id[0]      = '\0';
111438
       make_and_model[0] = '\0';
b85faf
 
111438
       strcpy(model, "Unknown");
b85faf
 
111438
-      for (data = rdata, dataend = data + rdlen;
111438
-           data < dataend;
111438
-           data = datanext)
111438
+      for (;;)
b85faf
       {
111438
-       /*
111438
-        * Read a key/value pair starting with an 8-bit length.  Since the
111438
-	* length is 8 bits and the size of the key/value buffers is 256, we
111438
-	* don't need to check for overflow...
111438
-	*/
111438
-
111438
-        datalen = *data++;
111438
-
111438
-        if (!datalen || (data + datalen) >= dataend)
111438
-	  break;
111438
-
111438
-        datanext = data + datalen;
111438
+	char *key;
111438
+	char *value;
b85faf
 
111438
-        for (ptr = key; data < datanext && *data != '='; data ++)
111438
-	  *ptr++ = *data;
111438
-	*ptr = '\0';
b85faf
-
111438
-	if (data < datanext && *data == '=')
111438
-	{
111438
-	  data ++;
111438
-
111438
-	  if (data < datanext)
111438
-	    memcpy(value, data, datanext - data);
111438
-	  value[datanext - data] = '\0';
111438
-	}
111438
-	else
111438
-	  continue;
111438
+	if (parse_txt_record_pair (txt))
111438
+	  goto next;
111438
 
111438
+	key = txt->key;
111438
+	value = txt->value;
111438
         if (!strncasecmp(key, "usb_", 4))
b85faf
 	{
b85faf
 	 /*
081f3e
@@ -805,6 +1183,10 @@ query_callback(
111438
 	  if (device->type == CUPS_DEVICE_PRINTER)
111438
 	    device->sent = 1;
b85faf
 	}
111438
+
111438
+      next:
111438
+	if (next_txt_record (txt) == NULL)
111438
+	  break;
111438
       }
b85faf
 
111438
       if (device->device_id)
081f3e
@@ -854,11 +1236,9 @@ query_callback(
b85faf
     }
b85faf
   }
b85faf
 
b85faf
-  if (!device)
b85faf
-    fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName);
b85faf
+  return device;
b85faf
 }
b85faf
 
111438
-
b85faf
 /*
111438
  * 'sigterm_handler()' - Handle termination signals...
b85faf
  */
dd27f8
diff -up cups-1.4.0/config.h.in.avahi cups-1.4.0/config.h.in
dd27f8
--- cups-1.4.0/config.h.in.avahi	2009-06-10 16:51:21.000000000 +0100
081f3e
+++ cups-1.4.0/config.h.in	2009-09-04 14:57:04.733388405 +0100
dd27f8
@@ -336,6 +336,13 @@
b85faf
 
b85faf
 
b85faf
 /*
b85faf
+ * Do we have Avahi for DNS Service Discovery?
b85faf
+ */
b85faf
+
b85faf
+#undef HAVE_AVAHI
b85faf
+
b85faf
+
b85faf
+/*
b85faf
  * Do we have <sys ioctl.h="">?
b85faf
  */
b85faf
 
dd27f8
diff -up cups-1.4.0/config-scripts/cups-dnssd.m4.avahi cups-1.4.0/config-scripts/cups-dnssd.m4
dd27f8
--- cups-1.4.0/config-scripts/cups-dnssd.m4.avahi	2009-02-10 17:05:35.000000000 +0000
081f3e
+++ cups-1.4.0/config-scripts/cups-dnssd.m4	2009-09-04 14:57:04.731388902 +0100
b85faf
@@ -27,6 +27,21 @@ AC_ARG_WITH(dnssd-includes, [  --with-dn
b85faf
 DNSSDLIBS=""
b85faf
 DNSSD_BACKEND=""
b85faf
 
b85faf
+AC_ARG_ENABLE(avahi, [  --enable-avahi          turn on DNS Service Discovery support, default=no],
b85faf
+	      [if test x$enable_avahi = xyes; then
b85faf
+		       AC_MSG_CHECKING(for Avahi)
b85faf
+		       if $PKGCONFIG --exists avahi-client; then
b85faf
+			       AC_MSG_RESULT(yes)
b85faf
+			       CFLAGS="$CFLAGS `$PKGCONFIG --cflags avahi-client`"
b85faf
+			       DNSSDLIBS="`$PKGCONFIG --libs avahi-client`"
b85faf
+			       DNSSD_BACKEND="dnssd"
b85faf
+			       AC_DEFINE(HAVE_AVAHI)
b85faf
+			       enable_dnssd=no
b85faf
+		       else
b85faf
+			       AC_MSG_RESULT(no)
b85faf
+		       fi
b85faf
+	       fi])
b85faf
+
b85faf
 if test x$enable_dnssd != xno; then
b85faf
 	AC_CHECK_HEADER(dns_sd.h, [
b85faf
 		case "$uname" in
dd27f8
diff -up cups-1.4.0/cups/http-support.c.avahi cups-1.4.0/cups/http-support.c
dd27f8
--- cups-1.4.0/cups/http-support.c.avahi	2009-06-12 01:21:58.000000000 +0100
081f3e
+++ cups-1.4.0/cups/http-support.c	2009-09-04 14:57:04.736398674 +0100
dd27f8
@@ -55,6 +55,11 @@
b85faf
 #  include <dns_sd.h>
111438
 #  include <poll.h>
b85faf
 #endif /* HAVE_DNSSD */
b85faf
+#ifdef HAVE_AVAHI
111438
+#  include <avahi-client client.h="">
111438
+#  include <avahi-client lookup.h="">
111438
+#  include <avahi-common simple-watch.h="">
b85faf
+#endif /* HAVE_AVAHI */
b85faf
 
b85faf
 
b85faf
 /*
dd27f8
@@ -121,6 +126,24 @@ static void		resolve_callback(DNSService
b85faf
 					 void *context);
b85faf
 #endif /* HAVE_DNSSD */
b85faf
 
b85faf
+#ifdef HAVE_AVAHI
111438
+static void	avahi_resolve_uri_client_cb(AvahiClient *client,
111438
+					    AvahiClientState state,
111438
+					    void *simple_poll);
111438
+static void	avahi_resolve_uri_resolver_cb(AvahiServiceResolver *resolver,
111438
+					      AvahiIfIndex interface,
111438
+					      AvahiProtocol protocol,
111438
+					      AvahiResolverEvent event,
111438
+					      const char *name,
111438
+					      const char *type,
111438
+					      const char *domain,
111438
+					      const char *host_name,
111438
+					      const AvahiAddress *address,
111438
+					      uint16_t port,
111438
+					      AvahiStringList *txt,
111438
+					      AvahiLookupResultFlags flags,
111438
+					      void *context);
b85faf
+#endif /* HAVE_AVAHI */
b85faf
 
b85faf
 /*
b85faf
  * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
dd27f8
@@ -1349,15 +1372,26 @@ _httpResolveURI(
b85faf
 
b85faf
   if (strstr(hostname, "._tcp"))
b85faf
   {
b85faf
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
111438
+    char		*regtype,	/* Pointer to type in hostname */
111438
+			*domain;	/* Pointer to domain in hostname */
b85faf
 #ifdef HAVE_DNSSD
111438
     DNSServiceRef	ref,		/* DNS-SD master service reference */
111438
 			domainref,	/* DNS-SD service reference for domain */
111438
 			localref;	/* DNS-SD service reference for .local */
111438
     int			domainsent = 0;	/* Send the domain resolve? */
111438
-    char		*regtype,	/* Pointer to type in hostname */
111438
-			*domain;	/* Pointer to domain in hostname */
111438
     _http_uribuf_t	uribuf;		/* URI buffer */
111438
     struct pollfd	polldata;	/* Polling data */
111438
+#else /* HAVE_AVAHI */
b85faf
+    AvahiSimplePoll	*simple_poll;
b85faf
+    AvahiClient		*client;
b85faf
+    int			error;
111438
+    struct
b85faf
+    {
b85faf
+      AvahiSimplePoll	*poll;
b85faf
+      _http_uribuf_t	uribuf;
111438
+    } user_data;
b85faf
+#endif /* HAVE_DNSSD */
b85faf
 
111438
 
111438
     if (logit)
dd27f8
@@ -1395,8 +1429,13 @@ _httpResolveURI(
b85faf
     if (domain)
b85faf
       *domain++ = '\0';
b85faf
 
b85faf
+#ifdef HAVE_DNSSD
b85faf
     uribuf.buffer  = resolved_uri;
b85faf
     uribuf.bufsize = resolved_size;
b85faf
+#else
b85faf
+    user_data.uribuf.buffer = resolved_uri;
b85faf
+    user_data.uribuf.bufsize = resolved_size;
b85faf
+#endif
b85faf
 
b85faf
     resolved_uri[0] = '\0';
b85faf
 
111438
@@ -1411,6 +1450,7 @@ _httpResolveURI(
111438
 
111438
     uri = NULL;
b85faf
 
b85faf
+#ifdef HAVE_DNSSD
111438
     if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
111438
     {
111438
       localref = ref;
dd27f8
@@ -1486,6 +1526,36 @@ _httpResolveURI(
111438
 
111438
       DNSServiceRefDeallocate(ref);
a1088d
     }
111438
+#else /* HAVE_AVAHI */
b85faf
+    if ((simple_poll = avahi_simple_poll_new ()) != NULL)
b85faf
+    {
b85faf
+      if ((client = avahi_client_new (avahi_simple_poll_get (simple_poll),
111438
+				      0, avahi_resolve_uri_client_cb,
b85faf
+				      &simple_poll, &error)) != NULL)
b85faf
+      {
b85faf
+	user_data.poll = simple_poll;
b85faf
+	if (avahi_service_resolver_new (client, AVAHI_IF_UNSPEC,
b85faf
+					AVAHI_PROTO_UNSPEC, hostname,
b85faf
+					regtype, domain, AVAHI_PROTO_UNSPEC, 0,
111438
+					avahi_resolve_uri_resolver_cb,
b85faf
+					&user_data) != NULL)
b85faf
+	{
b85faf
+	  avahi_simple_poll_loop (simple_poll);
b85faf
+
b85faf
+	 /*
b85faf
+	  * Collect the result.
b85faf
+	  */
b85faf
+
b85faf
+	  if (resolved_uri[0])
b85faf
+	    uri = resolved_uri;
b85faf
+	}
b85faf
+
b85faf
+	avahi_client_free (client);
b85faf
+      }
b85faf
+
b85faf
+      avahi_simple_poll_free (simple_poll);
b85faf
+    }
111438
+#endif /* HAVE_DNSSD */
a1088d
 
a1088d
     if (logit)
111438
     {
dd27f8
@@ -1497,13 +1567,13 @@ _httpResolveURI(
b85faf
       fputs("STATE: -connecting-to-device\n", stderr);
111438
     }
b85faf
 
b85faf
-#else
b85faf
+#else /* HAVE_DNSSD || HAVE_AVAHI */
a1088d
    /*
a1088d
     * No DNS-SD support...
a1088d
     */
a1088d
 
b85faf
     uri = NULL;
b85faf
-#endif /* HAVE_DNSSD */
b85faf
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
b85faf
 
a1088d
     if (logit && !uri)
b85faf
       _cupsLangPuts(stderr, _("Unable to find printer!\n"));
dd27f8
@@ -1708,6 +1778,105 @@ resolve_callback(
b85faf
 }
b85faf
 #endif /* HAVE_DNSSD */
b85faf
 
b85faf
+#ifdef HAVE_AVAHI
b85faf
+static void
111438
+avahi_resolve_uri_client_cb (AvahiClient *client,
111438
+			     AvahiClientState state,
111438
+			     void *simple_poll)
b85faf
+{
b85faf
+  DEBUG_printf(("avahi_resolve_uri_client_callback(client=%p, state=%d, "
b85faf
+		"simple_poll=%p)\n", client, state, simple_poll));
b85faf
+
b85faf
+  /*
b85faf
+   * If the connection drops, quit.
b85faf
+   */
b85faf
+
b85faf
+  if (state == AVAHI_CLIENT_FAILURE)
b85faf
+    avahi_simple_poll_quit (simple_poll);
b85faf
+}
b85faf
+
b85faf
+static void
111438
+avahi_resolve_uri_resolver_cb (AvahiServiceResolver *resolver,
111438
+			       AvahiIfIndex interface,
111438
+			       AvahiProtocol protocol,
111438
+			       AvahiResolverEvent event,
111438
+			       const char *name,
111438
+			       const char *type,
111438
+			       const char *domain,
111438
+			       const char *host_name,
111438
+			       const AvahiAddress *address,
111438
+			       uint16_t port,
111438
+			       AvahiStringList *txt,
111438
+			       AvahiLookupResultFlags flags,
111438
+			       void *context)
b85faf
+{
b85faf
+  const char		*scheme;	/* URI scheme */
b85faf
+  char			rp[256];	/* Remote printer */
b85faf
+  AvahiStringList	*pair;
b85faf
+  char			*value;
b85faf
+  size_t		valueLen = 0;
b85faf
+  char			addr[AVAHI_ADDRESS_STR_MAX];
b85faf
+  struct
b85faf
+  {
b85faf
+    AvahiSimplePoll	*poll;
b85faf
+    _http_uribuf_t	uribuf;
b85faf
+  }		*poll_uribuf = context;
b85faf
+
b85faf
+  DEBUG_printf(("avahi_resolve_uri_resolver_callback(resolver=%p, "
b85faf
+		"interface=%d, protocol=%d, event=%d, name=\"%s\", "
b85faf
+		"type=\"%s\", domain=\"%s\", host_name=\"%s\", address=%p, "
b85faf
+		"port=%d, txt=%p, flags=%d, context=%p)\n",
b85faf
+		resolver, interface, protocol, event, name, type, domain,
b85faf
+		host_name, address, port, txt, flags, context));
b85faf
+
b85faf
+  if (event != AVAHI_RESOLVER_FOUND)
b85faf
+  {
b85faf
+    avahi_service_resolver_free (resolver);
b85faf
+    avahi_simple_poll_quit (poll_uribuf->poll);
b85faf
+    return;
b85faf
+  }
b85faf
+
b85faf
+ /*
b85faf
+  * Figure out the scheme from the full name...
b85faf
+  */
b85faf
+
b85faf
+  if (strstr(type, "_ipp."))
b85faf
+    scheme = "ipp";
b85faf
+  else if (strstr(type, "_printer."))
b85faf
+    scheme = "lpd";
b85faf
+  else if (strstr(type, "_pdl-datastream."))
b85faf
+    scheme = "socket";
b85faf
+  else
b85faf
+    scheme = "riousbprint";
b85faf
+
b85faf
+ /*
b85faf
+  * Extract the "remote printer key from the TXT record...
b85faf
+  */
b85faf
+
b85faf
+  if ((pair = avahi_string_list_find (txt, "rp")) != NULL)
b85faf
+  {
b85faf
+    avahi_string_list_get_pair (pair, NULL, &value, &valueLen);
b85faf
+    rp[0] = '/';
b85faf
+    memcpy (rp + 1, value, valueLen);
b85faf
+    rp[valueLen + 1] = '\0';
b85faf
+  }
b85faf
+  else
b85faf
+    rp[0] = '\0';
b85faf
+
b85faf
+ /*
b85faf
+  * Assemble the final device URI...
b85faf
+  */
b85faf
+
b85faf
+  avahi_address_snprint (addr, AVAHI_ADDRESS_STR_MAX, address);
b85faf
+  httpAssembleURI(HTTP_URI_CODING_ALL, poll_uribuf->uribuf.buffer,
b85faf
+		  poll_uribuf->uribuf.bufsize, scheme, NULL,
b85faf
+		  addr, port, rp);
b85faf
+  DEBUG_printf(("avahi_resolve_uri_resolver_callback: Resolved URI is \"%s\"\n",
b85faf
+		poll_uribuf->uribuf.buffer));
b85faf
+  avahi_simple_poll_quit (poll_uribuf->poll);
b85faf
+}
b85faf
+#endif /* HAVE_AVAHI */
b85faf
+
b85faf
 
b85faf
 /*
dd27f8
  * End of "$Id: http-support.c 8705 2009-06-12 00:21:58Z mike $".