]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/dest.c
Fix build errors on Fedora.
[thirdparty/cups.git] / cups / dest.c
index bf9a0dcc46b35c0a70d724c32045e53f32093d8a..f9853c8cd45c94a809332315655a265afb3e8457 100644 (file)
@@ -1,81 +1,18 @@
 /*
- * "$Id: dest.c 9568 2011-02-25 06:13:56Z mike $"
+ * "$Id$"
  *
- *   User-defined destination (and option) support for CUPS.
+ * User-defined destination (and option) support for CUPS.
  *
- *   Copyright 2007-2012 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   cupsAddDest()                 - Add a destination to the list of
- *                                   destinations.
- *   _cupsAppleCopyDefaultPaperID() - Get the default paper ID.
- *   _cupsAppleCopyDefaultPrinter() - Get the default printer at this location.
- *   _cupsAppleGetUseLastPrinter()  - Get whether to use the last used printer.
- *   _cupsAppleSetDefaultPaperID()  - Set the default paper id.
- *   _cupsAppleSetDefaultPrinter()  - Set the default printer for this
- *                                   location.
- *   _cupsAppleSetUseLastPrinter()  - Set whether to use the last used printer.
- *   cupsConnectDest()             - Connect to the server for a destination.
- *   cupsConnectDestBlock()        - Connect to the server for a destination.
- *   cupsCopyDest()                - Copy a destination.
- *   cupsEnumDests()               - Enumerate available destinations with a
- *                                   callback function.
- *   cupsEnumDestsBlock()          - Enumerate available destinations with a
- *                                   block.
- *   cupsFreeDests()               - Free the memory used by the list of
- *                                   destinations.
- *   cupsGetDest()                 - Get the named destination from the list.
- *   _cupsGetDestResource()        - Get the resource path and URI for a
- *                                   destination.
- *   _cupsGetDests()               - Get destinations from a server.
- *   cupsGetDests()                - Get the list of destinations from the
- *                                   default server.
- *   cupsGetDests2()               - Get the list of destinations from the
- *                                   specified server.
- *   cupsGetNamedDest()            - Get options for the named destination.
- *   cupsRemoveDest()              - Remove a destination from the destination
- *                                   list.
- *   cupsSetDefaultDest()          - Set the default destination.
- *   cupsSetDests()                - Save the list of destinations for the
- *                                   default server.
- *   cupsSetDests2()               - Save the list of destinations for the
- *                                   specified server.
- *   _cupsUserDefault()            - Get the user default printer from
- *                                   environment variables and location
- *                                   information.
- *   appleCopyLocations()          - Copy the location history array.
- *   appleCopyNetwork()            - Get the network ID for the current
- *                                   location.
- *   appleGetPaperSize()           - Get the default paper size.
- *   appleGetPrinter()             - Get a printer from the history array.
- *   cups_add_dest()               - Add a destination to the array.
- *   cups_block_cb()               - Enumeration callback for block API.
- *   cups_compare_dests()          - Compare two destinations.
- *   cups_dnssd_browse_cb()        - Browse for printers.
- *   cups_dnssd_compare_device()    - Compare two devices.
- *   cups_dnssd_free_device()      - Free the memory used by a device.
- *   cups_dnssd_get_device()       - Lookup a device and create it as needed.
- *   cups_dnssd_local_cb()         - Browse for local printers.
- *   cups_dnssd_query_cb()         - Process query data.
- *   cups_dnssd_resolve()           - Resolve a Bonjour printer URI.
- *   cups_dnssd_resolve_cb()        - See if we should continue resolving.
- *   cups_dnssd_unquote()          - Unquote a name string.
- *   cups_find_dest()              - Find a destination using a binary search.
- *   cups_get_default()            - Get the default destination from an
- *                                   lpoptions file.
- *   cups_get_dests()              - Get destinations from a file.
- *   cups_make_string()            - Make a comma-separated string of values
- *                                   from an IPP attribute.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
 #  include <dns_sd.h>
 #endif /* HAVE_DNSSD */
 
+#ifdef HAVE_AVAHI
+#  include <avahi-client/client.h>
+#  include <avahi-client/lookup.h>
+#  include <avahi-common/simple-watch.h>
+#  include <avahi-common/domain.h>
+#  include <avahi-common/error.h>
+#  include <avahi-common/malloc.h>
+#define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
+#endif /* HAVE_AVAHI */
+
 
 /*
  * Constants...
  * Types...
  */
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
 typedef enum _cups_dnssd_state_e       /* Enumerated device state */
 {
   _CUPS_DNSSD_NEW,
@@ -125,12 +72,19 @@ typedef enum _cups_dnssd_state_e   /* Enumerated device state */
   _CUPS_DNSSD_PENDING,
   _CUPS_DNSSD_ACTIVE,
   _CUPS_DNSSD_LOCAL,
+  _CUPS_DNSSD_INCOMPATIBLE,
   _CUPS_DNSSD_ERROR
 } _cups_dnssd_state_t;
 
 typedef struct _cups_dnssd_data_s      /* Enumeration data */
 {
+#  ifdef HAVE_DNSSD
   DNSServiceRef                main_ref;       /* Main service reference */
+#  else /* HAVE_AVAHI */
+  AvahiSimplePoll      *simple_poll;   /* Polling interface */
+  AvahiClient          *client;        /* Client information */
+  int                  got_data;       /* Did we get data? */
+#  endif /* HAVE_DNSSD */
   cups_dest_cb_t       cb;             /* Callback */
   void                 *user_data;     /* User data pointer */
   cups_ptype_t         type,           /* Printer type filter */
@@ -141,7 +95,11 @@ typedef struct _cups_dnssd_data_s   /* Enumeration data */
 typedef struct _cups_dnssd_device_s    /* Enumerated device */
 {
   _cups_dnssd_state_t  state;          /* State of device listing */
+#  ifdef HAVE_DNSSD
   DNSServiceRef                ref;            /* Service reference for query */
+#  else /* HAVE_AVAHI */
+  AvahiRecordBrowser   *ref;           /* Browser for query */
+#  endif /* HAVE_DNSSD */
   char                 *domain,        /* Domain name */
                        *fullName,      /* Full name */
                        *regtype;       /* Registration type */
@@ -165,8 +123,8 @@ typedef struct _cups_dnssd_resolve_s        /* Data for resolving URI */
 static CFArrayRef      appleCopyLocations(void);
 static CFStringRef     appleCopyNetwork(void);
 static char            *appleGetPaperSize(char *name, int namesize);
-static CFStringRef     appleGetPrinter(CFArrayRef locations, CFStringRef network,
-                                       CFIndex *locindex);
+static CFStringRef     appleGetPrinter(CFArrayRef locations,
+                                       CFStringRef network, CFIndex *locindex);
 #endif /* __APPLE__ */
 static cups_dest_t     *cups_add_dest(const char *name, const char *instance,
                                       int *num_dests, cups_dest_t **dests);
@@ -175,7 +133,8 @@ static int          cups_block_cb(cups_dest_block_t block, unsigned flags,
                                      cups_dest_t *dest);
 #endif /* __BLOCKS__ */
 static int             cups_compare_dests(cups_dest_t *a, cups_dest_t *b);
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+#  ifdef HAVE_DNSSD
 static void            cups_dnssd_browse_cb(DNSServiceRef sdRef,
                                             DNSServiceFlags flags,
                                             uint32_t interfaceIndex,
@@ -184,6 +143,20 @@ static void                cups_dnssd_browse_cb(DNSServiceRef sdRef,
                                             const char *regtype,
                                             const char *replyDomain,
                                             void *context);
+#  else /* HAVE_AVAHI */
+static void            cups_dnssd_browse_cb(AvahiServiceBrowser *browser,
+                                            AvahiIfIndex interface,
+                                            AvahiProtocol protocol,
+                                            AvahiBrowserEvent event,
+                                            const char *serviceName,
+                                            const char *regtype,
+                                            const char *replyDomain,
+                                            AvahiLookupResultFlags flags,
+                                            void *context);
+static void            cups_dnssd_client_cb(AvahiClient *client,
+                                            AvahiClientState state,
+                                            void *context);
+#  endif /* HAVE_DNSSD */
 static int             cups_dnssd_compare_devices(_cups_dnssd_device_t *a,
                                                   _cups_dnssd_device_t *b);
 static void            cups_dnssd_free_device(_cups_dnssd_device_t *device,
@@ -193,6 +166,7 @@ static _cups_dnssd_device_t *
                                              const char *serviceName,
                                              const char *regtype,
                                              const char *replyDomain);
+#  ifdef HAVE_DNSSD
 static void            cups_dnssd_local_cb(DNSServiceRef sdRef,
                                            DNSServiceFlags flags,
                                            uint32_t interfaceIndex,
@@ -209,13 +183,27 @@ static void               cups_dnssd_query_cb(DNSServiceRef sdRef,
                                            uint16_t rrtype, uint16_t rrclass,
                                            uint16_t rdlen, const void *rdata,
                                            uint32_t ttl, void *context);
+#  else /* HAVE_AVAHI */
+static int             cups_dnssd_poll_cb(struct pollfd *pollfds,
+                                          unsigned int num_pollfds,
+                                          int timeout, void *context);
+static void            cups_dnssd_query_cb(AvahiRecordBrowser *browser,
+                                           AvahiIfIndex interface,
+                                           AvahiProtocol protocol,
+                                           AvahiBrowserEvent event,
+                                           const char *name, uint16_t rrclass,
+                                           uint16_t rrtype, const void *rdata,
+                                           size_t rdlen,
+                                           AvahiLookupResultFlags flags,
+                                           void *context);
+#  endif /* HAVE_DNSSD */
 static const char      *cups_dnssd_resolve(cups_dest_t *dest, const char *uri,
                                            int msec, int *cancel,
                                            cups_dest_cb_t cb, void *user_data);
 static int             cups_dnssd_resolve_cb(void *context);
 static void            cups_dnssd_unquote(char *dst, const char *src,
                                           size_t dstsize);
-#endif /* HAVE_DNSSD */
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
 static int             cups_find_dest(const char *name, const char *instance,
                                       int num_dests, cups_dest_t *dests, int prev,
                                       int *rdiff);
@@ -264,7 +252,8 @@ cupsAddDest(const char  *name,              /* I  - Destination name */
     if (instance && !cupsGetDest(name, NULL, num_dests, *dests))
       return (num_dests);
 
-    dest = cups_add_dest(name, instance, &num_dests, dests);
+    if ((dest = cups_add_dest(name, instance, &num_dests, dests)) == NULL)
+      return (num_dests);
 
    /*
     * Find the base dest again now the array has been realloc'd.
@@ -278,7 +267,7 @@ cupsAddDest(const char  *name,              /* I  - Destination name */
       * Copy options from parent...
       */
 
-      dest->options = calloc(sizeof(cups_option_t), parent->num_options);
+      dest->options = calloc(sizeof(cups_option_t), (size_t)parent->num_options);
 
       if (dest->options)
       {
@@ -544,7 +533,7 @@ _cupsAppleSetUseLastPrinter(
  * to by "cancel" is non-zero, or the callback function (or block) returns 0,
  * The caller is responsible for calling httpClose() on the returned object.
  *
- * @since CUPS 1.6@
+ * @since CUPS 1.6/OS X 10.8@
  */
 
 http_t *                               /* O - Connection to server or @code NULL@ */
@@ -579,7 +568,7 @@ cupsConnectDest(
     if (resource)
       *resource = '\0';
 
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
     return (NULL);
   }
 
@@ -596,7 +585,7 @@ cupsConnectDest(
   if ((uri = cupsGetOption("printer-uri-supported", dest->num_options,
                            dest->options)) == NULL)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOENT), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0);
 
     if (cb)
       (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR,
@@ -605,20 +594,20 @@ cupsConnectDest(
     return (NULL);
   }
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
   if (strstr(uri, "._tcp"))
   {
     if ((uri = cups_dnssd_resolve(dest, uri, msec, cancel, cb,
                                   user_data)) == NULL)
       return (NULL);
   }
-#endif /* HAVE_DNSSD */
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
 
   if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
                       userpass, sizeof(userpass), hostname, sizeof(hostname),
-                      &port, resource, resourcesize) < HTTP_URI_OK)
+                      &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, _("Bad printer URI."), 1);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer URI."), 1);
 
     if (cb)
       (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR,
@@ -662,11 +651,12 @@ cupsConnectDest(
   */
 
   if (!strcmp(scheme, "ipps") || port == 443)
-    encryption = HTTP_ENCRYPT_ALWAYS;
+    encryption = HTTP_ENCRYPTION_ALWAYS;
   else
-    encryption = HTTP_ENCRYPT_IF_REQUESTED;
+    encryption = HTTP_ENCRYPTION_IF_REQUESTED;
 
-  http = _httpCreate(hostname, port, addrlist, encryption, AF_UNSPEC);
+  http = httpConnect2(hostname, port, addrlist, AF_UNSPEC, encryption, 1, 0,
+                      NULL);
 
  /*
   * Connect if requested...
@@ -710,7 +700,7 @@ cupsConnectDest(
  * to by "cancel" is non-zero, or the callback function (or block) returns 0,
  * The caller is responsible for calling httpClose() on the returned object.
  *
- * @since CUPS 1.6@
+ * @since CUPS 1.6/OS X 10.8@
  */
 
 http_t *                               /* O - Connection to server or @code NULL@ */
@@ -736,7 +726,7 @@ cupsConnectDestBlock(
  * copy) - for use with the cupsEnumDests* functions. The caller is responsible
  * for calling cupsFreeDests() on the returned object(s).
  *
- * @since CUPS 1.6@
+ * @since CUPS 1.6/OS X 10.8@
  */
 
 int
@@ -785,8 +775,7 @@ cupsCopyDest(cups_dest_t *dest,
 
   if (new_dest)
   {
-    if ((new_dest->options = calloc(sizeof(cups_option_t),
-                                    dest->num_options)) == NULL)
+    if ((new_dest->options = calloc(sizeof(cups_option_t), (size_t)dest->num_options)) == NULL)
       return (cupsRemoveDest(dest->name, dest->instance, num_dests, dests));
 
     new_dest->num_options = dest->num_options;
@@ -816,7 +805,7 @@ cupsCopyDest(cups_dest_t *dest,
  * Enumeration happens on the current thread and does not return until all
  * destinations have been enumerated or the callback function returns 0.
  *
- * @since CUPS 1.6@
+ * @since CUPS 1.6/OS X 10.8@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
@@ -834,32 +823,41 @@ cupsEnumDests(
                        num_dests;      /* Number of destinations */
   cups_dest_t          *dests = NULL,  /* Destinations */
                        *dest;          /* Current destination */
-#ifdef HAVE_DNSSD
-  int                  nfds,           /* Number of files responded */
-                       count,          /* Number of queries started */
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+  int                  count,          /* Number of queries started */
                        remaining;      /* Remainder of timeout */
   _cups_dnssd_data_t   data;           /* Data for callback */
   _cups_dnssd_device_t *device;        /* Current device */
-  int                  main_fd;        /* File descriptor for lookups */
+#  ifdef HAVE_DNSSD
+  int                  nfds,           /* Number of files responded */
+                       main_fd;        /* File descriptor for lookups */
   DNSServiceRef                ipp_ref,        /* IPP browser */
                        local_ipp_ref;  /* Local IPP browser */
-#  ifdef HAVE_SSL
+#    ifdef HAVE_SSL
   DNSServiceRef                ipps_ref,       /* IPPS browser */
                        local_ipps_ref; /* Local IPPS browser */
-#  endif /* HAVE_SSL */
-#  ifdef HAVE_POLL
+#    endif /* HAVE_SSL */
+#    ifdef HAVE_POLL
   struct pollfd                pfd;            /* Polling data */
-#  else
+#    else
   fd_set               input;          /* Input set for select() */
   struct timeval       timeout;        /* Timeout for select() */
-#  endif /* HAVE_POLL */
-#endif /* HAVE_DNSSD */
-
+#    endif /* HAVE_POLL */
+#  else /* HAVE_AVAHI */
+  int                  error;          /* Error value */
+  AvahiServiceBrowser  *ipp_ref;       /* IPP browser */
+#    ifdef HAVE_SSL
+  AvahiServiceBrowser  *ipps_ref;      /* IPPS browser */
+#    endif /* HAVE_SSL */
+#  endif /* HAVE_DNSSD */
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
 
  /*
   * Range check input...
   */
 
+  (void)flags;
+
   if (!cb)
     return (0);
 
@@ -867,8 +865,8 @@ cupsEnumDests(
   * Get the list of local printers and pass them to the callback function...
   */
 
-  num_dests = _cupsGetDests(CUPS_HTTP_DEFAULT, CUPS_GET_PRINTERS, NULL, &dests,
-                            type, mask);
+  num_dests = _cupsGetDests(CUPS_HTTP_DEFAULT, IPP_OP_CUPS_GET_PRINTERS, NULL,
+                            &dests, type, mask);
 
   for (i = num_dests, dest = dests;
        i > 0 && (!cancel || !*cancel);
@@ -882,7 +880,7 @@ cupsEnumDests(
   if (i > 0 || msec == 0)
     return (1);
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
  /*
   * Get Bonjour-shared printers...
   */
@@ -893,6 +891,7 @@ cupsEnumDests(
                                NULL, NULL, 0, NULL,
                                (cups_afree_func_t)cups_dnssd_free_device);
 
+#  ifdef HAVE_DNSSD
   if (DNSServiceCreateConnection(&data.main_ref) != kDNSServiceErr_NoError)
     return (0);
 
@@ -900,27 +899,56 @@ cupsEnumDests(
 
   ipp_ref = data.main_ref;
   DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0,
-                   "_ipp._tcp,_cups", NULL,
+                   "_ipp._tcp", NULL,
                    (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data);
 
   local_ipp_ref = data.main_ref;
   DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection,
                    kDNSServiceInterfaceIndexLocalOnly,
-                   "_ipp._tcp,_cups", NULL,
+                   "_ipp._tcp", NULL,
                    (DNSServiceBrowseReply)cups_dnssd_local_cb, &data);
 
-#  ifdef HAVE_SSL
+#    ifdef HAVE_SSL
   ipps_ref = data.main_ref;
   DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0,
-                   "_ipps._tcp,_cups", NULL,
+                   "_ipps._tcp", NULL,
                    (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data);
 
   local_ipps_ref = data.main_ref;
   DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection,
                    kDNSServiceInterfaceIndexLocalOnly,
-                   "_ipps._tcp,_cups", NULL,
+                   "_ipps._tcp", NULL,
                    (DNSServiceBrowseReply)cups_dnssd_local_cb, &data);
-#  endif /* HAVE_SSL */
+#    endif /* HAVE_SSL */
+
+#  else /* HAVE_AVAHI */
+  if ((data.simple_poll = avahi_simple_poll_new()) == NULL)
+  {
+    DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
+    return (1);
+  }
+
+  avahi_simple_poll_set_func(data.simple_poll, cups_dnssd_poll_cb, &data);
+
+  data.client = avahi_client_new(avahi_simple_poll_get(data.simple_poll),
+                                0, cups_dnssd_client_cb, &data,
+                                &error);
+  if (!data.client)
+  {
+    DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
+    avahi_simple_poll_free(data.simple_poll);
+    return (1);
+  }
+
+  ipp_ref  = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC,
+                                      AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL,
+                                      0, cups_dnssd_browse_cb, &data);
+#    ifdef HAVE_SSL
+  ipps_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC,
+                                      AVAHI_PROTO_UNSPEC, "_ipps._tcp", NULL,
+                                      0, cups_dnssd_browse_cb, &data);
+#    endif /* HAVE_SSL */
+#  endif /* HAVE_DNSSD */
 
   if (msec < 0)
     remaining = INT_MAX;
@@ -933,13 +961,14 @@ cupsEnumDests(
     * Check for input...
     */
 
-#  ifdef HAVE_POLL
+#  ifdef HAVE_DNSSD
+#    ifdef HAVE_POLL
     pfd.fd     = main_fd;
     pfd.events = POLLIN;
 
     nfds = poll(&pfd, 1, remaining > 250 ? 250 : remaining);
 
-#  else
+#    else
     FD_ZERO(&input);
     FD_SET(main_fd, &input);
 
@@ -947,13 +976,30 @@ cupsEnumDests(
     timeout.tv_usec = remaining > 250 ? 250000 : remaining * 1000;
 
     nfds = select(main_fd + 1, &input, NULL, NULL, &timeout);
-#  endif /* HAVE_POLL */
+#    endif /* HAVE_POLL */
 
     if (nfds > 0)
       DNSServiceProcessResult(data.main_ref);
     else if (nfds == 0)
       remaining -= 250;
 
+#  else /* HAVE_AVAHI */
+    data.got_data = 0;
+
+    if ((error = avahi_simple_poll_iterate(data.simple_poll, 250)) > 0)
+    {
+     /*
+      * We've been told to exit the loop.  Perhaps the connection to
+      * Avahi failed.
+      */
+
+      break;
+    }
+
+    if (!data.got_data)
+      remaining -= 250;
+#  endif /* HAVE_DNSSD */
+
     for (device = (_cups_dnssd_device_t *)cupsArrayFirst(data.devices),
              count = 0;
          device;
@@ -964,10 +1010,11 @@ cupsEnumDests(
 
       if (!device->ref && device->state == _CUPS_DNSSD_NEW)
       {
-        device->ref = data.main_ref;
-
        DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device->fullName));
 
+#  ifdef HAVE_DNSSD
+        device->ref = data.main_ref;
+
        if (DNSServiceQueryRecord(&(device->ref),
                                  kDNSServiceFlagsShareConnection,
                                  0, device->fullName,
@@ -985,14 +1032,39 @@ cupsEnumDests(
 
          DEBUG_puts("1cupsEnumDests: Query failed.");
        }
+
+#  else /* HAVE_AVAHI */
+       if ((device->ref = avahi_record_browser_new(data.client,
+                                                   AVAHI_IF_UNSPEC,
+                                                   AVAHI_PROTO_UNSPEC,
+                                                   device->fullName,
+                                                   AVAHI_DNS_CLASS_IN,
+                                                   AVAHI_DNS_TYPE_TXT,
+                                                   0,
+                                                   cups_dnssd_query_cb,
+                                                   &data)) != NULL)
+        {
+         count ++;
+       }
+       else
+       {
+         device->state = _CUPS_DNSSD_ERROR;
+
+         DEBUG_printf(("1cupsEnumDests: Query failed: %s",
+                       avahi_strerror(avahi_client_errno(data.client))));
+       }
+#  endif /* HAVE_DNSSD */
       }
       else if (device->ref && device->state == _CUPS_DNSSD_PENDING)
       {
-       if (!(*cb)(user_data, CUPS_DEST_FLAGS_NONE, &device->dest))
-       {
-         remaining = -1;
-         break;
-       }
+        if ((device->type & mask) == type)
+        {
+         if (!(*cb)(user_data, CUPS_DEST_FLAGS_NONE, &device->dest))
+         {
+           remaining = -1;
+           break;
+         }
+        }
 
         device->state = _CUPS_DNSSD_ACTIVE;
       }
@@ -1001,16 +1073,27 @@ cupsEnumDests(
 
   cupsArrayDelete(data.devices);
 
+#  ifdef HAVE_DNSSD
   DNSServiceRefDeallocate(ipp_ref);
   DNSServiceRefDeallocate(local_ipp_ref);
 
-#  ifdef HAVE_SSL
+#    ifdef HAVE_SSL
   DNSServiceRefDeallocate(ipp_ref);
   DNSServiceRefDeallocate(local_ipp_ref);
-#  endif /* HAVE_SSL */
+#    endif /* HAVE_SSL */
 
   DNSServiceRefDeallocate(data.main_ref);
-#endif /* HAVE_DNSSD */
+
+#  else /* HAVE_AVAHI */
+  avahi_service_browser_free(ipp_ref);
+#    ifdef HAVE_SSL
+  avahi_service_browser_free(ipps_ref);
+#    endif /* HAVE_SSL */
+
+  avahi_client_free(data.client);
+  avahi_simple_poll_free(data.simple_poll);
+#  endif /* HAVE_DNSSD */
+#endif /* HAVE_DNSSD || HAVE_DNSSD */
 
   return (1);
 }
@@ -1028,7 +1111,7 @@ cupsEnumDests(
  * Enumeration happens on the current thread and does not return until all
  * destinations have been enumerated or the block returns 0.
  *
- * @since CUPS 1.6@
+ * @since CUPS 1.6/OS X 10.8@
  */
 
 int                                    /* O - 1 on success, 0 on failure */
@@ -1150,7 +1233,7 @@ _cupsGetDestResource(
     if (resource)
       *resource = '\0';
 
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
     return (NULL);
   }
 
@@ -1164,7 +1247,7 @@ _cupsGetDestResource(
     if (resource)
       *resource = '\0';
 
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOENT), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0);
 
     return (NULL);
   }
@@ -1179,9 +1262,9 @@ _cupsGetDestResource(
 
   if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
                       userpass, sizeof(userpass), hostname, sizeof(hostname),
-                      &port, resource, resourcesize) < HTTP_URI_OK)
+                      &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, _("Bad printer URI."), 1);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer URI."), 1);
 
     return (NULL);
   }
@@ -1193,12 +1276,12 @@ _cupsGetDestResource(
 /*
  * '_cupsGetDests()' - Get destinations from a server.
  *
- * "op" is CUPS_GET_PRINTERS to get a full list, CUPS_GET_DEFAULT to get the
- * system-wide default printer, or IPP_GET_PRINTER_ATTRIBUTES for a known
- * printer.
+ * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
+ * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
+ * a known printer.
  *
  * "name" is the name of an existing printer and is only used when "op" is
- * IPP_GET_PRINTER_ATTRIBUTES.
+ * IPP_OP_GET_PRINTER_ATTRIBUTES.
  *
  * "dest" is initialized to point to the array of destinations.
  *
@@ -1208,7 +1291,7 @@ _cupsGetDestResource(
  * Free the memory used by the destination array using the @link cupsFreeDests@
  * function.
  *
- * Note: On Mac OS X this function also gets the default paper from the system
+ * Note: On OS X this function also gets the default paper from the system
  * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
  * options array for each destination that supports it.
  */
@@ -1260,6 +1343,7 @@ _cupsGetDests(http_t       *http, /* I  - Connection to server or
                  "printer-is-shared",
                  "printer-location",
                  "printer-make-and-model",
+                 "printer-mandatory-job-attributes",
                  "printer-name",
                  "printer-state",
                  "printer-state-change-time",
@@ -1278,13 +1362,13 @@ _cupsGetDests(http_t       *http,       /* I  - Connection to server or
 #endif /* __APPLE__ */
 
  /*
-  * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which
+  * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
   * require the following attributes:
   *
   *    attributes-charset
   *    attributes-natural-language
   *    requesting-user-name
-  *    printer-uri [for IPP_GET_PRINTER_ATTRIBUTES]
+  *    printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
   */
 
   request = ippNewRequest(op);
@@ -1296,7 +1380,7 @@ _cupsGetDests(http_t       *http, /* I  - Connection to server or
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
                "requesting-user-name", NULL, cupsUser());
 
-  if (name && op != CUPS_GET_DEFAULT)
+  if (name && op != IPP_OP_CUPS_GET_DEFAULT)
   {
     httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
                      "localhost", ippPort(), "/printers/%s", name);
@@ -1305,10 +1389,8 @@ _cupsGetDests(http_t       *http,        /* I  - Connection to server or
   }
   else if (mask)
   {
-    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type",
-                  type);
-    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask",
-                  mask);
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type", (int)type);
+    ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask", (int)mask);
   }
 
  /*
@@ -1365,6 +1447,7 @@ _cupsGetDests(http_t       *http, /* I  - Connection to server or
            !strcmp(attr->name, "printer-info") ||
            !strcmp(attr->name, "printer-is-shared") ||
            !strcmp(attr->name, "printer-make-and-model") ||
+           !strcmp(attr->name, "printer-mandatory-job-attributes") ||
            !strcmp(attr->name, "printer-state") ||
            !strcmp(attr->name, "printer-state-change-time") ||
            !strcmp(attr->name, "printer-type") ||
@@ -1501,14 +1584,13 @@ cupsGetDests(cups_dest_t **dests)       /* O - Destinations */
  * Use the @link cupsFreeDests@ function to free the destination list and
  * the @link cupsGetDest@ function to find a particular destination.
  *
- * @since CUPS 1.1.21/Mac OS X 10.4@
+ * @since CUPS 1.1.21/OS X 10.4@
  */
 
 int                                    /* O - Number of destinations */
 cupsGetDests2(http_t      *http,       /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
               cups_dest_t **dests)     /* O - Destinations */
 {
-  int          i;                      /* Looping var */
   int          num_dests;              /* Number of destinations */
   cups_dest_t  *dest;                  /* Destination pointer */
   const char   *home;                  /* HOME environment variable */
@@ -1528,7 +1610,7 @@ cupsGetDests2(http_t      *http,  /* I - Connection to server or @code CUPS_HTTP_
 
   if (!dests)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, _("Bad NULL dests pointer"), 1);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad NULL dests pointer"), 1);
     return (0);
   }
 
@@ -1537,9 +1619,9 @@ cupsGetDests2(http_t      *http,  /* I - Connection to server or @code CUPS_HTTP_
   */
 
   *dests    = (cups_dest_t *)0;
-  num_dests = _cupsGetDests(http, CUPS_GET_PRINTERS, NULL, dests, 0, 0);
+  num_dests = _cupsGetDests(http, IPP_OP_CUPS_GET_PRINTERS, NULL, dests, 0, 0);
 
-  if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE)
+  if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE)
   {
     cupsFreeDests(num_dests, *dests);
     *dests = (cups_dest_t *)0;
@@ -1553,10 +1635,10 @@ cupsGetDests2(http_t      *http,        /* I - Connection to server or @code CUPS_HTTP_
   if (num_dests > 0)
   {
     num_reals = num_dests;
-    reals     = calloc(num_reals, sizeof(cups_dest_t));
+    reals     = calloc((size_t)num_reals, sizeof(cups_dest_t));
 
     if (reals)
-      memcpy(reals, *dests, num_reals * sizeof(cups_dest_t));
+      memcpy(reals, *dests, (size_t)num_reals * sizeof(cups_dest_t));
     else
       num_reals = 0;
   }
@@ -1631,21 +1713,16 @@ cupsGetDests2(http_t      *http,        /* I - Connection to server or @code CUPS_HTTP_
       * Have a default; see if it is real...
       */
 
-      dest = cupsGetDest(dest->name, NULL, num_reals, reals);
-    }
-
-   /*
-    * If dest is NULL, then no default (that exists) is set, so we
-    * need to set a default if one exists...
-    */
-
-    if (dest == NULL && defprinter != NULL)
-    {
-      for (i = 0; i < num_dests; i ++)
-        (*dests)[i].is_default = 0;
+      if (!cupsGetDest(dest->name, NULL, num_reals, reals))
+      {
+       /*
+        * Remove the non-real printer from the list, since we don't want jobs
+        * going to an unexpected printer... (<rdar://problem/14216472>)
+        */
 
-      if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL)
-       dest->is_default = 1;
+        num_dests = cupsRemoveDest(dest->name, dest->instance, num_dests,
+                                   dests);
+      }
     }
 
    /*
@@ -1660,7 +1737,7 @@ cupsGetDests2(http_t      *http,  /* I - Connection to server or @code CUPS_HTTP_
   */
 
   if (num_dests > 0)
-    _cupsSetError(IPP_OK, NULL, 0);
+    _cupsSetError(IPP_STATUS_OK, NULL, 0);
 
   return (num_dests);
 }
@@ -1684,7 +1761,7 @@ cupsGetDests2(http_t      *http,  /* I - Connection to server or @code CUPS_HTTP_
  * The returned destination must be freed using @link cupsFreeDests@ with a
  * "num_dests" value of 1.
  *
- * @since CUPS 1.4/Mac OS X 10.6@
+ * @since CUPS 1.4/OS X 10.6@
  */
 
 cups_dest_t *                          /* O - Destination or @code NULL@ */
@@ -1697,7 +1774,7 @@ cupsGetNamedDest(http_t     *http,        /* I - Connection to server or @code CUPS_HTT
                defname[256];           /* Default printer name */
   const char   *home = getenv("HOME"); /* Home directory */
   int          set_as_default = 0;     /* Set returned destination as default */
-  ipp_op_t     op = IPP_GET_PRINTER_ATTRIBUTES;
+  ipp_op_t     op = IPP_OP_GET_PRINTER_ATTRIBUTES;
                                        /* IPP operation to get server ops */
   _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals */
 
@@ -1751,7 +1828,7 @@ cupsGetNamedDest(http_t     *http,        /* I - Connection to server or @code CUPS_HTT
       * No locally-set default destination, ask the server...
       */
 
-      op = CUPS_GET_DEFAULT;
+      op = IPP_OP_CUPS_GET_DEFAULT;
     }
   }
 
@@ -1760,18 +1837,7 @@ cupsGetNamedDest(http_t     *http,       /* I - Connection to server or @code CUPS_HTT
   */
 
   if (!_cupsGetDests(http, op, name, &dest, 0, 0))
-  {
-    if (op == CUPS_GET_DEFAULT || (name && !set_as_default))
-      return (NULL);
-
-   /*
-    * The default printer from environment variables or from a
-    * configuration file does not exist.  Find out the real default.
-    */
-
-    if (!_cupsGetDests(http, CUPS_GET_DEFAULT, NULL, &dest, 0, 0))
-      return (NULL);
-  }
+    return (NULL);
 
   if (instance)
     dest->instance = _cupsStrAlloc(instance);
@@ -1809,7 +1875,7 @@ cupsGetNamedDest(http_t     *http,        /* I - Connection to server or @code CUPS_HTT
  * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
  * options for the user.
  *
- * @since CUPS 1.3/Mac OS X 10.5@
+ * @since CUPS 1.3/OS X 10.5@
  */
 
 int                                    /* O  - New number of destinations */
@@ -1843,10 +1909,10 @@ cupsRemoveDest(const char  *name,       /* I  - Destination name */
 
   num_dests --;
 
-  i = dest - *dests;
+  i = (int)(dest - *dests);
 
   if (i < num_dests)
-    memmove(dest, dest + 1, (num_dests - i) * sizeof(cups_dest_t));
+    memmove(dest, dest + 1, (size_t)(num_dests - i) * sizeof(cups_dest_t));
 
   return (num_dests);
 }
@@ -1855,7 +1921,7 @@ cupsRemoveDest(const char  *name, /* I  - Destination name */
 /*
  * 'cupsSetDefaultDest()' - Set the default destination.
  *
- * @since CUPS 1.3/Mac OS X 10.5@
+ * @since CUPS 1.3/OS X 10.5@
  */
 
 void
@@ -1910,7 +1976,7 @@ cupsSetDests(int         num_dests,       /* I - Number of destinations */
  * This function saves the destinations to /etc/cups/lpoptions when run
  * as root and ~/.cups/lpoptions when run as a normal user.
  *
- * @since CUPS 1.1.21/Mac OS X 10.4@
+ * @since CUPS 1.1.21/OS X 10.4@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -1946,9 +2012,9 @@ cupsSetDests2(http_t      *http,  /* I - Connection to server or @code CUPS_HTTP_
   * Get the server destinations...
   */
 
-  num_temps = _cupsGetDests(http, CUPS_GET_PRINTERS, NULL, &temps, 0, 0);
+  num_temps = _cupsGetDests(http, IPP_OP_CUPS_GET_PRINTERS, NULL, &temps, 0, 0);
 
-  if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE)
+  if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE)
   {
     cupsFreeDests(num_temps, temps);
     return (-1);
@@ -2180,7 +2246,7 @@ _cupsUserDefault(char   *name,            /* I - Name buffer */
 
   if ((locprinter = _cupsAppleCopyDefaultPrinter()) != NULL)
   {
-    CFStringGetCString(locprinter, name, namesize, kCFStringEncodingUTF8);
+    CFStringGetCString(locprinter, name, (CFIndex)namesize, kCFStringEncodingUTF8);
     CFRelease(locprinter);
   }
   else
@@ -2300,12 +2366,12 @@ appleCopyNetwork(void)
  * 'appleGetPaperSize()' - Get the default paper size.
  */
 
-char *                                 /* O - Default paper size */
+static char *                          /* O - Default paper size */
 appleGetPaperSize(char *name,          /* I - Paper size name buffer */
                   int  namesize)       /* I - Size of buffer */
 {
   CFStringRef  defaultPaperID;         /* Default paper ID */
-  _pwg_media_t *pwgmedia;              /* PWG media size */
+  pwg_media_t  *pwgmedia;              /* PWG media size */
 
 
   defaultPaperID = _cupsAppleCopyDefaultPaperID();
@@ -2314,7 +2380,7 @@ appleGetPaperSize(char *name,             /* I - Paper size name buffer */
       !CFStringGetCString(defaultPaperID, name, namesize,
                          kCFStringEncodingUTF8))
     name[0] = '\0';
-  else if ((pwgmedia = _pwgMediaForLegacy(name)) != NULL)
+  else if ((pwgmedia = pwgMediaForLegacy(name)) != NULL)
     strlcpy(name, pwgmedia->pwg, namesize);
 
   if (defaultPaperID)
@@ -2388,7 +2454,7 @@ cups_add_dest(const char  *name,  /* I  - Name of destination */
   if (*num_dests == 0)
     dest = malloc(sizeof(cups_dest_t));
   else
-    dest = realloc(*dests, sizeof(cups_dest_t) * (*num_dests + 1));
+    dest = realloc(*dests, sizeof(cups_dest_t) * (size_t)(*num_dests + 1));
 
   if (!dest)
     return (NULL);
@@ -2415,8 +2481,7 @@ cups_add_dest(const char  *name,  /* I  - Name of destination */
   */
 
   if (insert < *num_dests)
-    memmove(*dests + insert + 1, *dests + insert,
-            (*num_dests - insert) * sizeof(cups_dest_t));
+    memmove(*dests + insert + 1, *dests + insert, (size_t)(*num_dests - insert) * sizeof(cups_dest_t));
 
   (*num_dests) ++;
 
@@ -2471,7 +2536,8 @@ cups_compare_dests(cups_dest_t *a,        /* I - First destination */
 }
 
 
-#ifdef HAVE_DNSSD
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+#  ifdef HAVE_DNSSD
 /*
  * 'cups_dnssd_browse_cb()' - Browse for printers.
  */
@@ -2512,6 +2578,104 @@ cups_dnssd_browse_cb(
 }
 
 
+#  else /* HAVE_AVAHI */
+/*
+ * 'cups_dnssd_browse_cb()' - Browse for printers.
+ */
+
+static void
+cups_dnssd_browse_cb(
+    AvahiServiceBrowser    *browser,   /* I - Browser */
+    AvahiIfIndex           interface,  /* I - Interface index (unused) */
+    AvahiProtocol          protocol,   /* I - Network protocol (unused) */
+    AvahiBrowserEvent      event,      /* I - What happened */
+    const char             *name,      /* I - Service name */
+    const char             *type,      /* I - Registration type */
+    const char             *domain,    /* I - Domain */
+    AvahiLookupResultFlags flags,      /* I - Flags */
+    void                   *context)   /* I - Devices array */
+{
+#ifdef DEBUG
+  AvahiClient          *client = avahi_service_browser_get_client(browser);
+                                       /* Client information */
+#endif /* DEBUG */
+  _cups_dnssd_data_t   *data = (_cups_dnssd_data_t *)context;
+                                       /* Enumeration data */
+
+
+  (void)interface;
+  (void)protocol;
+  (void)context;
+
+  switch (event)
+  {
+    case AVAHI_BROWSER_FAILURE:
+       DEBUG_printf(("cups_dnssd_browse_cb: %s",
+                     avahi_strerror(avahi_client_errno(client))));
+       avahi_simple_poll_quit(data->simple_poll);
+       break;
+
+    case AVAHI_BROWSER_NEW:
+       /*
+       * This object is new on the network.
+       */
+
+       if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
+       {
+        /*
+         * This comes from the local machine so ignore it.
+         */
+
+         DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".",
+                       name));
+       }
+       else
+       {
+        /*
+         * Create a device entry for it if it doesn't yet exist.
+         */
+
+         cups_dnssd_get_device(data, name, type, domain);
+       }
+       break;
+
+    case AVAHI_BROWSER_REMOVE:
+    case AVAHI_BROWSER_ALL_FOR_NOW:
+    case AVAHI_BROWSER_CACHE_EXHAUSTED:
+        break;
+  }
+}
+
+
+/*
+ * 'cups_dnssd_client_cb()' - Avahi client callback function.
+ */
+
+static void
+cups_dnssd_client_cb(
+    AvahiClient      *client,          /* I - Client information (unused) */
+    AvahiClientState state,            /* I - Current state */
+    void             *context)         /* I - User data (unused) */
+{
+  _cups_dnssd_data_t   *data = (_cups_dnssd_data_t *)context;
+                                       /* Enumeration data */
+
+
+  (void)client;
+
+ /*
+  * If the connection drops, quit.
+  */
+
+  if (state == AVAHI_CLIENT_FAILURE)
+  {
+    DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
+    avahi_simple_poll_quit(data->simple_poll);
+  }
+}
+#  endif /* HAVE_DNSSD */
+
+
 /*
  * 'cups_dnssd_compare_device()' - Compare two devices.
  */
@@ -2537,8 +2701,13 @@ cups_dnssd_free_device(
   DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", device,
                 device->dest.name, data));
 
+#  ifdef HAVE_DNSSD
   if (device->ref)
     DNSServiceRefDeallocate(device->ref);
+#  else /* HAVE_AVAHI */
+  if (device->ref)
+    avahi_record_browser_free(device->ref);
+#  endif /* HAVE_DNSSD */
 
   _cupsStrFree(device->domain);
   _cupsStrFree(device->fullName);
@@ -2648,14 +2817,25 @@ cups_dnssd_get_device(
   * Set the "full name" of this service, which is used for queries...
   */
 
+#  ifdef HAVE_DNSSD
   DNSServiceConstructFullName(fullName, device->dest.name, device->regtype,
                              device->domain);
+#  else /* HAVE_AVAHI */
+  avahi_service_name_join(fullName, kDNSServiceMaxDomainName, serviceName,
+                          regtype, replyDomain);
+#  endif /* HAVE_DNSSD */
+
   _cupsStrFree(device->fullName);
   device->fullName = _cupsStrAlloc(fullName);
 
   if (device->ref)
   {
+#  ifdef HAVE_DNSSD
     DNSServiceRefDeallocate(device->ref);
+#  else /* HAVE_AVAHI */
+    avahi_record_browser_free(device->ref);
+#  endif /* HAVE_DNSSD */
+
     device->ref = 0;
   }
 
@@ -2669,6 +2849,7 @@ cups_dnssd_get_device(
 }
 
 
+#  ifdef HAVE_DNSSD
 /*
  * 'cups_dnssd_local_cb()' - Browse for local printers.
  */
@@ -2726,12 +2907,51 @@ cups_dnssd_local_cb(
 
   device->state = _CUPS_DNSSD_LOCAL;
 }
+#  endif /* HAVE_DNSSD */
+
+
+#  ifdef HAVE_AVAHI
+/*
+ * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
+ *
+ * Note: This function is needed because avahi_simple_poll_iterate is broken
+ *       and always uses a timeout of 0 (!) milliseconds.
+ *       (Avahi Ticket #364)
+ */
+
+static int                             /* O - Number of file descriptors matching */
+cups_dnssd_poll_cb(
+    struct pollfd *pollfds,            /* I - File descriptors */
+    unsigned int  num_pollfds,         /* I - Number of file descriptors */
+    int           timeout,             /* I - Timeout in milliseconds (unused) */
+    void          *context)            /* I - User data (unused) */
+{
+  _cups_dnssd_data_t   *data = (_cups_dnssd_data_t *)context;
+                                       /* Enumeration data */
+  int                  val;            /* Return value */
+
+
+  (void)timeout;
+
+  val = poll(pollfds, num_pollfds, 250);
+
+  if (val < 0)
+  {
+    DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno)));
+  }
+  else if (val > 0)
+    data->got_data = 1;
+
+  return (val);
+}
+#  endif /* HAVE_AVAHI */
 
 
 /*
  * 'cups_dnssd_query_cb()' - Process query data.
  */
 
+#  ifdef HAVE_DNSSD
 static void
 cups_dnssd_query_cb(
     DNSServiceRef       sdRef,         /* I - Service reference */
@@ -2746,6 +2966,27 @@ cups_dnssd_query_cb(
     uint32_t            ttl,           /* I - Time-to-live */
     void                *context)      /* I - Enumeration data */
 {
+#  else /* HAVE_AVAHI */
+static void
+cups_dnssd_query_cb(
+    AvahiRecordBrowser     *browser,   /* I - Record browser */
+    AvahiIfIndex           interfaceIndex,
+                                       /* I - Interface index (unused) */
+    AvahiProtocol          protocol,   /* I - Network protocol (unused) */
+    AvahiBrowserEvent      event,      /* I - What happened? */
+    const char             *fullName,  /* I - Service name */
+    uint16_t               rrclass,    /* I - Record class */
+    uint16_t               rrtype,     /* I - Record type */
+    const void             *rdata,     /* I - TXT record */
+    size_t                 rdlen,      /* I - Length of TXT record */
+    AvahiLookupResultFlags flags,      /* I - Flags */
+    void                   *context)   /* I - Enumeration data */
+{
+#    ifdef DEBUG
+  AvahiClient          *client = avahi_record_browser_get_client(browser);
+                                       /* Client information */
+#    endif /* DEBUG */
+#  endif /* HAVE_DNSSD */
   _cups_dnssd_data_t   *data = (_cups_dnssd_data_t *)context;
                                        /* Enumeration data */
   char                 name[1024],     /* Service name */
@@ -2754,6 +2995,7 @@ cups_dnssd_query_cb(
                        *device;        /* Device */
 
 
+#  ifdef HAVE_DNSSD
   DEBUG_printf(("5cups_dnssd_query_cb(sdRef=%p, flags=%x, "
                "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
                "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
@@ -2767,6 +3009,27 @@ cups_dnssd_query_cb(
   if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd))
     return;
 
+#  else /* HAVE_AVAHI */
+  DEBUG_printf(("5cups_dnssd_query_cb(browser=%p, interfaceIndex=%d, "
+               "protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, "
+               "rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)",
+               browser, interfaceIndex, protocol, event, fullName, rrclass,
+               rrtype, rdata, (unsigned)rdlen, flags, context));
+
+ /*
+  * Only process "add" data...
+  */
+
+  if (event != AVAHI_BROWSER_NEW)
+  {
+    if (event == AVAHI_BROWSER_FAILURE)
+      DEBUG_printf(("cups_dnssd_query_cb: %s",
+                   avahi_strerror(avahi_client_errno(client))));
+
+    return;
+  }
+#  endif /* HAVE_DNSSD */
+
  /*
   * Lookup the service in the devices array.
   */
@@ -2781,8 +3044,7 @@ cups_dnssd_query_cb(
   if ((device = cupsArrayFind(data->devices, &dkey)) != NULL)
   {
    /*
-    * Found it, pull out the priority and make and model from the TXT
-    * record and save it...
+    * Found it, pull out the make and model from the TXT record and save it...
     */
 
     const uint8_t      *txt,           /* Pointer into data */
@@ -2796,11 +3058,15 @@ cups_dnssd_query_cb(
                        model[256],     /* Model */
                        uriname[1024],  /* Name for URI */
                        uri[1024];      /* Printer URI */
+    cups_ptype_t       type = CUPS_PRINTER_REMOTE | CUPS_PRINTER_BW;
+                                       /* Printer type */
+    int                        saw_printer_type = 0;
+                                       /* Did we see a printer-type key? */
 
     device->state     = _CUPS_DNSSD_PENDING;
     make_and_model[0] = '\0';
 
-    strcpy(model, "Unknown");
+    strlcpy(model, "Unknown", sizeof(model));
 
     for (txt = rdata, txtend = txt + rdlen;
         txt < txtend;
@@ -2820,7 +3086,7 @@ cups_dnssd_query_cb(
       txtnext = txt + txtlen;
 
       for (ptr = key; txt < txtnext && *txt != '='; txt ++)
-       *ptr++ = *txt;
+       *ptr++ = (char)*txt;
       *ptr = '\0';
 
       if (txt < txtnext && *txt == '=')
@@ -2828,7 +3094,7 @@ cups_dnssd_query_cb(
        txt ++;
 
        if (txt < txtnext)
-         memcpy(value, txt, txtnext - txt);
+         memcpy(value, txt, (size_t)(txtnext - txt));
        value[txtnext - txt] = '\0';
 
        DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key, value));
@@ -2842,10 +3108,10 @@ cups_dnssd_query_cb(
       if (!_cups_strcasecmp(key, "usb_MFG") ||
           !_cups_strcasecmp(key, "usb_MANU") ||
          !_cups_strcasecmp(key, "usb_MANUFACTURER"))
-       strcpy(make_and_model, value);
+       strlcpy(make_and_model, value, sizeof(make_and_model));
       else if (!_cups_strcasecmp(key, "usb_MDL") ||
                !_cups_strcasecmp(key, "usb_MODEL"))
-       strcpy(model, value);
+       strlcpy(model, value, sizeof(model));
       else if (!_cups_strcasecmp(key, "product") && !strstr(value, "Ghostscript"))
       {
        if (value[0] == '(')
@@ -2857,30 +3123,114 @@ cups_dnssd_query_cb(
          if ((ptr = value + strlen(value) - 1) > value && *ptr == ')')
            *ptr = '\0';
 
-         strcpy(model, value + 1);
+         strlcpy(model, value + 1, sizeof(model));
        }
        else
-         strcpy(model, value);
+         strlcpy(model, value, sizeof(model));
       }
       else if (!_cups_strcasecmp(key, "ty"))
       {
-       strcpy(model, value);
+       strlcpy(model, value, sizeof(model));
 
        if ((ptr = strchr(model, ',')) != NULL)
          *ptr = '\0';
       }
+      else if (!_cups_strcasecmp(key, "note"))
+        device->dest.num_options = cupsAddOption("printer-location", value,
+                                                device->dest.num_options,
+                                                &device->dest.options);
+      else if (!_cups_strcasecmp(key, "pdl"))
+      {
+       /*
+        * Look for PDF-capable printers; only PDF-capable printers are shown.
+        */
+
+        const char     *start, *next;  /* Pointer into value */
+        int            have_pdf = 0;   /* Have PDF? */
+
+        for (start = value; start && *start; start = next)
+        {
+          if (!_cups_strncasecmp(start, "application/pdf", 15) &&
+              (!start[15] || start[15] == ','))
+          {
+            have_pdf = 1;
+            break;
+          }
+
+          if ((next = strchr(start, ',')) != NULL)
+            next ++;
+        }
+
+        if (!have_pdf)
+          device->state = _CUPS_DNSSD_INCOMPATIBLE;
+      }
       else if (!_cups_strcasecmp(key, "printer-type"))
       {
-        device->dest.num_options = cupsAddOption("printer-type", value,
-                                                 device->dest.num_options,
-                                                 &device->dest.options);
+       /*
+        * Value is either NNNN or 0xXXXX
+        */
+
+       saw_printer_type = 1;
+        type             = (cups_ptype_t)strtol(value, NULL, 0);
+      }
+      else if (!saw_printer_type)
+      {
+       if (!_cups_strcasecmp(key, "air") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_AUTHENTICATED;
+       else if (!_cups_strcasecmp(key, "bind") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_BIND;
+       else if (!_cups_strcasecmp(key, "collate") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_COLLATE;
+       else if (!_cups_strcasecmp(key, "color") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_COLOR;
+       else if (!_cups_strcasecmp(key, "copies") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_COPIES;
+       else if (!_cups_strcasecmp(key, "duplex") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_DUPLEX;
+       else if (!_cups_strcasecmp(key, "fax") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_MFP;
+       else if (!_cups_strcasecmp(key, "papercustom") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_VARIABLE;
+       else if (!_cups_strcasecmp(key, "papermax"))
+       {
+         if (!_cups_strcasecmp(value, "legal-a4"))
+           type |= CUPS_PRINTER_SMALL;
+         else if (!_cups_strcasecmp(value, "isoc-a2"))
+           type |= CUPS_PRINTER_MEDIUM;
+         else if (!_cups_strcasecmp(value, ">isoc-a2"))
+           type |= CUPS_PRINTER_LARGE;
+       }
+       else if (!_cups_strcasecmp(key, "punch") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_PUNCH;
+       else if (!_cups_strcasecmp(key, "scan") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_MFP;
+       else if (!_cups_strcasecmp(key, "sort") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_SORT;
+       else if (!_cups_strcasecmp(key, "staple") &&
+                !_cups_strcasecmp(value, "t"))
+         type |= CUPS_PRINTER_STAPLE;
       }
     }
 
    /*
-    * Save the make-and-model...
+    * Save the printer-xxx values...
     */
 
+    device->dest.num_options = cupsAddOption("printer-info", name,
+                                            device->dest.num_options,
+                                            &device->dest.options);
+
     if (make_and_model[0])
     {
       strlcat(make_and_model, " ", sizeof(make_and_model));
@@ -2897,6 +3247,12 @@ cups_dnssd_query_cb(
                                               device->dest.num_options,
                                               &device->dest.options);
 
+    device->type = type;
+    snprintf(value, sizeof(value), "%u", type);
+    device->dest.num_options = cupsAddOption("printer-type", value,
+                                            device->dest.num_options,
+                                            &device->dest.options);
+
    /*
     * Save the URI...
     */
@@ -2904,7 +3260,7 @@ cups_dnssd_query_cb(
     cups_dnssd_unquote(uriname, device->fullName, sizeof(uriname));
     httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri),
                     !strcmp(device->regtype, "_ipps._tcp") ? "ipps" : "ipp",
-                    NULL, uriname, 0, "/cups");
+                    NULL, uriname, 0, saw_printer_type ? "/cups" : "/");
 
     DEBUG_printf(("6cups_dnssd_query: printer-uri-supported=\"%s\"", uri));
 
@@ -2963,7 +3319,7 @@ cups_dnssd_resolve(
                             _HTTP_RESOLVE_FQDN, cups_dnssd_resolve_cb,
                             &resolve)) == NULL)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to resolve printer URI."), 1);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to resolve printer URI."), 1);
 
     if (cb)
       (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR,
@@ -3417,22 +3773,21 @@ cups_make_string(
     {
       case IPP_TAG_INTEGER :
       case IPP_TAG_ENUM :
-         snprintf(ptr, end - ptr + 1, "%d", attr->values[i].integer);
+         snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].integer);
          break;
 
       case IPP_TAG_BOOLEAN :
          if (attr->values[i].boolean)
-           strlcpy(ptr, "true", end - ptr + 1);
+           strlcpy(ptr, "true", (size_t)(end - ptr + 1));
          else
-           strlcpy(ptr, "false", end - ptr + 1);
+           strlcpy(ptr, "false", (size_t)(end - ptr + 1));
          break;
 
       case IPP_TAG_RANGE :
          if (attr->values[i].range.lower == attr->values[i].range.upper)
-           snprintf(ptr, end - ptr + 1, "%d", attr->values[i].range.lower);
+           snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].range.lower);
          else
-           snprintf(ptr, end - ptr + 1, "%d-%d", attr->values[i].range.lower,
-                    attr->values[i].range.upper);
+           snprintf(ptr, (size_t)(end - ptr + 1), "%d-%d", attr->values[i].range.lower, attr->values[i].range.upper);
          break;
 
       default :
@@ -3464,5 +3819,5 @@ cups_make_string(
 
 
 /*
- * End of "$Id: dest.c 9568 2011-02-25 06:13:56Z mike $".
+ * End of "$Id$".
  */