4 * User-defined destination (and option) support for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsAddDest() - Add a destination to the list of
21 * _cupsAppleCopyDefaultPaperID() - Get the default paper ID.
22 * _cupsAppleCopyDefaultPrinter() - Get the default printer at this location.
23 * _cupsAppleGetUseLastPrinter() - Get whether to use the last used printer.
24 * _cupsAppleSetDefaultPaperID() - Set the default paper id.
25 * _cupsAppleSetDefaultPrinter() - Set the default printer for this
27 * _cupsAppleSetUseLastPrinter() - Set whether to use the last used printer.
28 * cupsConnectDest() - Connect to the server for a destination.
29 * cupsConnectDestBlock() - Connect to the server for a destination.
30 * cupsCopyDest() - Copy a destination.
31 * cupsEnumDests() - Enumerate available destinations with a
33 * cupsEnumDestsBlock() - Enumerate available destinations with a
35 * cupsFreeDests() - Free the memory used by the list of
37 * cupsGetDest() - Get the named destination from the list.
38 * _cupsGetDestResource() - Get the resource path and URI for a
40 * _cupsGetDests() - Get destinations from a server.
41 * cupsGetDests() - Get the list of destinations from the
43 * cupsGetDests2() - Get the list of destinations from the
45 * cupsGetNamedDest() - Get options for the named destination.
46 * cupsRemoveDest() - Remove a destination from the destination
48 * cupsSetDefaultDest() - Set the default destination.
49 * cupsSetDests() - Save the list of destinations for the
51 * cupsSetDests2() - Save the list of destinations for the
53 * _cupsUserDefault() - Get the user default printer from
54 * environment variables and location
56 * appleCopyLocations() - Copy the location history array.
57 * appleCopyNetwork() - Get the network ID for the current
59 * appleGetPaperSize() - Get the default paper size.
60 * appleGetPrinter() - Get a printer from the history array.
61 * cups_add_dest() - Add a destination to the array.
62 * cups_block_cb() - Enumeration callback for block API.
63 * cups_compare_dests() - Compare two destinations.
64 * cups_dnssd_browse_cb() - Browse for printers.
65 * cups_dnssd_browse_cb() - Browse for printers.
66 * cups_dnssd_client_cb() - Avahi client callback function.
67 * cups_dnssd_compare_device() - Compare two devices.
68 * cups_dnssd_free_device() - Free the memory used by a device.
69 * cups_dnssd_get_device() - Lookup a device and create it as needed.
70 * cups_dnssd_local_cb() - Browse for local printers.
71 * cups_dnssd_poll_cb() - Wait for input on the specified file
73 * cups_dnssd_query_cb() - Process query data.
74 * cups_dnssd_resolve() - Resolve a Bonjour printer URI.
75 * cups_dnssd_resolve_cb() - See if we should continue resolving.
76 * cups_dnssd_unquote() - Unquote a name string.
77 * cups_find_dest() - Find a destination using a binary search.
78 * cups_get_default() - Get the default destination from an
80 * cups_get_dests() - Get destinations from a file.
81 * cups_make_string() - Make a comma-separated string of values
82 * from an IPP attribute.
86 * Include necessary headers...
89 #include "cups-private.h"
94 #endif /* HAVE_NOTIFY_H */
98 #endif /* HAVE_POLL */
102 #endif /* HAVE_DNSSD */
105 # include <avahi-client/client.h>
106 # include <avahi-client/lookup.h>
107 # include <avahi-common/simple-watch.h>
108 # include <avahi-common/domain.h>
109 # include <avahi-common/error.h>
110 # include <avahi-common/malloc.h>
111 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
112 #endif /* HAVE_AVAHI */
120 # include <SystemConfiguration/SystemConfiguration.h>
121 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
122 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
123 # define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
124 # define kLocationNetworkKey CFSTR("Network")
125 # define kLocationPrinterIDKey CFSTR("PrinterID")
126 # define kUseLastPrinter CFSTR("UseLastPrinter")
127 #endif /* __APPLE__ */
134 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
135 typedef enum _cups_dnssd_state_e
/* Enumerated device state */
142 _CUPS_DNSSD_INCOMPATIBLE
,
144 } _cups_dnssd_state_t
;
146 typedef struct _cups_dnssd_data_s
/* Enumeration data */
149 DNSServiceRef main_ref
; /* Main service reference */
150 # else /* HAVE_AVAHI */
151 AvahiSimplePoll
*simple_poll
; /* Polling interface */
152 AvahiClient
*client
; /* Client information */
153 int got_data
; /* Did we get data? */
154 # endif /* HAVE_DNSSD */
155 cups_dest_cb_t cb
; /* Callback */
156 void *user_data
; /* User data pointer */
157 cups_ptype_t type
, /* Printer type filter */
158 mask
; /* Printer type mask */
159 cups_array_t
*devices
; /* Devices found so far */
160 } _cups_dnssd_data_t
;
162 typedef struct _cups_dnssd_device_s
/* Enumerated device */
164 _cups_dnssd_state_t state
; /* State of device listing */
166 DNSServiceRef ref
; /* Service reference for query */
167 # else /* HAVE_AVAHI */
168 AvahiRecordBrowser
*ref
; /* Browser for query */
169 # endif /* HAVE_DNSSD */
170 char *domain
, /* Domain name */
171 *fullName
, /* Full name */
172 *regtype
; /* Registration type */
173 cups_ptype_t type
; /* Device registration type */
174 cups_dest_t dest
; /* Destination record */
175 } _cups_dnssd_device_t
;
177 typedef struct _cups_dnssd_resolve_s
/* Data for resolving URI */
179 int *cancel
; /* Pointer to "cancel" variable */
180 struct timeval end_time
; /* Ending time */
181 } _cups_dnssd_resolve_t
;
182 #endif /* HAVE_DNSSD */
190 static CFArrayRef
appleCopyLocations(void);
191 static CFStringRef
appleCopyNetwork(void);
192 static char *appleGetPaperSize(char *name
, int namesize
);
193 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
194 CFStringRef network
, CFIndex
*locindex
);
195 #endif /* __APPLE__ */
196 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
197 int *num_dests
, cups_dest_t
**dests
);
199 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
201 #endif /* __BLOCKS__ */
202 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
203 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
205 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
206 DNSServiceFlags flags
,
207 uint32_t interfaceIndex
,
208 DNSServiceErrorType errorCode
,
209 const char *serviceName
,
211 const char *replyDomain
,
213 # else /* HAVE_AVAHI */
214 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
215 AvahiIfIndex interface
,
216 AvahiProtocol protocol
,
217 AvahiBrowserEvent event
,
218 const char *serviceName
,
220 const char *replyDomain
,
221 AvahiLookupResultFlags flags
,
223 static void cups_dnssd_client_cb(AvahiClient
*client
,
224 AvahiClientState state
,
226 # endif /* HAVE_DNSSD */
227 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
228 _cups_dnssd_device_t
*b
);
229 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
230 _cups_dnssd_data_t
*data
);
231 static _cups_dnssd_device_t
*
232 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
233 const char *serviceName
,
235 const char *replyDomain
);
237 static void cups_dnssd_local_cb(DNSServiceRef sdRef
,
238 DNSServiceFlags flags
,
239 uint32_t interfaceIndex
,
240 DNSServiceErrorType errorCode
,
241 const char *serviceName
,
243 const char *replyDomain
,
245 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
246 DNSServiceFlags flags
,
247 uint32_t interfaceIndex
,
248 DNSServiceErrorType errorCode
,
249 const char *fullName
,
250 uint16_t rrtype
, uint16_t rrclass
,
251 uint16_t rdlen
, const void *rdata
,
252 uint32_t ttl
, void *context
);
253 # else /* HAVE_AVAHI */
254 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
255 unsigned int num_pollfds
,
256 int timeout
, void *context
);
257 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
258 AvahiIfIndex interface
,
259 AvahiProtocol protocol
,
260 AvahiBrowserEvent event
,
261 const char *name
, uint16_t rrclass
,
262 uint16_t rrtype
, const void *rdata
,
264 AvahiLookupResultFlags flags
,
266 # endif /* HAVE_DNSSD */
267 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
268 int msec
, int *cancel
,
269 cups_dest_cb_t cb
, void *user_data
);
270 static int cups_dnssd_resolve_cb(void *context
);
271 static void cups_dnssd_unquote(char *dst
, const char *src
,
273 #endif /* HAVE_DNSSD || HAVE_AVAHI */
274 static int cups_find_dest(const char *name
, const char *instance
,
275 int num_dests
, cups_dest_t
*dests
, int prev
,
277 static char *cups_get_default(const char *filename
, char *namebuf
,
278 size_t namesize
, const char **instance
);
279 static int cups_get_dests(const char *filename
, const char *match_name
,
280 const char *match_inst
, int user_default_set
,
281 int num_dests
, cups_dest_t
**dests
);
282 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
287 * 'cupsAddDest()' - Add a destination to the list of destinations.
289 * This function cannot be used to add a new class or printer queue,
290 * it only adds a new container of saved options for the named
291 * destination or instance.
293 * If the named destination already exists, the destination list is
294 * returned unchanged. Adding a new instance of a destination creates
295 * a copy of that destination's options.
297 * Use the @link cupsSaveDests@ function to save the updated list of
298 * destinations to the user's lpoptions file.
301 int /* O - New number of destinations */
302 cupsAddDest(const char *name
, /* I - Destination name */
303 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
304 int num_dests
, /* I - Number of destinations */
305 cups_dest_t
**dests
) /* IO - Destinations */
307 int i
; /* Looping var */
308 cups_dest_t
*dest
; /* Destination pointer */
309 cups_dest_t
*parent
= NULL
; /* Parent destination */
310 cups_option_t
*doption
, /* Current destination option */
311 *poption
; /* Current parent option */
317 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
319 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
322 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
326 * Find the base dest again now the array has been realloc'd.
329 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
331 if (instance
&& parent
&& parent
->num_options
> 0)
334 * Copy options from parent...
337 dest
->options
= calloc(sizeof(cups_option_t
), parent
->num_options
);
341 dest
->num_options
= parent
->num_options
;
343 for (i
= dest
->num_options
, doption
= dest
->options
,
344 poption
= parent
->options
;
346 i
--, doption
++, poption
++)
348 doption
->name
= _cupsStrRetain(poption
->name
);
349 doption
->value
= _cupsStrRetain(poption
->value
);
361 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
364 CFStringRef
/* O - Default paper ID */
365 _cupsAppleCopyDefaultPaperID(void)
367 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
368 kCUPSPrintingPrefs
));
373 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
376 CFStringRef
/* O - Default printer name */
377 _cupsAppleCopyDefaultPrinter(void)
379 CFStringRef network
; /* Network location */
380 CFArrayRef locations
; /* Location array */
381 CFStringRef locprinter
; /* Current printer */
385 * Use location-based defaults only if "use last printer" is selected in the
386 * system preferences...
389 if (!_cupsAppleGetUseLastPrinter())
391 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
397 * Get the current location...
400 if ((network
= appleCopyNetwork()) == NULL
)
402 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
408 * Lookup the network in the preferences...
411 if ((locations
= appleCopyLocations()) == NULL
)
414 * Missing or bad location array, so no location-based default...
417 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
425 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
426 (int)CFArrayGetCount(locations
)));
428 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
429 CFRetain(locprinter
);
432 CFRelease(locations
);
439 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
442 int /* O - 1 to use last printer, 0 otherwise */
443 _cupsAppleGetUseLastPrinter(void)
445 Boolean uselast
, /* Use last printer preference value */
446 uselast_set
; /* Valid is set? */
449 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
452 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
463 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
467 _cupsAppleSetDefaultPaperID(
468 CFStringRef name
) /* I - New paper ID */
470 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
471 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
472 notify_post("com.apple.printerPrefsChange");
477 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
481 _cupsAppleSetDefaultPrinter(
482 CFStringRef name
) /* I - Default printer/class name */
484 CFStringRef network
; /* Current network */
485 CFArrayRef locations
; /* Old locations array */
486 CFIndex locindex
; /* Index in locations array */
487 CFStringRef locprinter
; /* Current printer */
488 CFMutableArrayRef newlocations
; /* New locations array */
489 CFMutableDictionaryRef newlocation
; /* New location */
493 * Get the current location...
496 if ((network
= appleCopyNetwork()) == NULL
)
498 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
503 * Lookup the network in the preferences...
506 if ((locations
= appleCopyLocations()) != NULL
)
507 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
514 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
517 * Need to change the locations array...
522 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
526 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
529 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
530 &kCFTypeArrayCallBacks
);
532 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
533 &kCFTypeDictionaryKeyCallBacks
,
534 &kCFTypeDictionaryValueCallBacks
);
536 if (newlocation
&& newlocations
)
539 * Put the new location at the front of the array...
542 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
543 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
544 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
547 * Limit the number of locations to 10...
550 while (CFArrayGetCount(newlocations
) > 10)
551 CFArrayRemoveValueAtIndex(newlocations
, 10);
554 * Push the changes out...
557 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
559 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
560 notify_post("com.apple.printerPrefsChange");
564 CFRelease(newlocations
);
567 CFRelease(newlocation
);
571 CFRelease(locations
);
578 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
582 _cupsAppleSetUseLastPrinter(
583 int uselast
) /* O - 1 to use last printer, 0 otherwise */
585 CFPreferencesSetAppValue(kUseLastPrinter
,
586 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
588 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
589 notify_post("com.apple.printerPrefsChange");
591 #endif /* __APPLE__ */
595 * 'cupsConnectDest()' - Connect to the server for a destination.
597 * Connect to the destination, returning a new http_t connection object and
598 * optionally the resource path to use for the destination. These calls will
599 * block until a connection is made, the timeout expires, the integer pointed
600 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
601 * The caller is responsible for calling httpClose() on the returned object.
603 * @since CUPS 1.6/OS X 10.8@
606 http_t
* /* O - Connection to server or @code NULL@ */
608 cups_dest_t
*dest
, /* I - Destination */
609 unsigned flags
, /* I - Connection flags */
610 int msec
, /* I - Timeout in milliseconds */
611 int *cancel
, /* I - Pointer to "cancel" variable */
612 char *resource
, /* I - Resource buffer */
613 size_t resourcesize
, /* I - Size of resource buffer */
614 cups_dest_cb_t cb
, /* I - Callback function */
615 void *user_data
) /* I - User data pointer */
617 const char *uri
; /* Printer URI */
618 char scheme
[32], /* URI scheme */
619 userpass
[256], /* Username and password (unused) */
620 hostname
[256], /* Hostname */
621 tempresource
[1024]; /* Temporary resource buffer */
622 int port
; /* Port number */
623 char portstr
[16]; /* Port number string */
624 http_encryption_t encryption
; /* Encryption to use */
625 http_addrlist_t
*addrlist
; /* Address list for server */
626 http_t
*http
; /* Connection to server */
630 * Range check input...
638 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
642 if (!resource
|| resourcesize
< 1)
644 resource
= tempresource
;
645 resourcesize
= sizeof(tempresource
);
649 * Grab the printer URI...
652 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
,
653 dest
->options
)) == NULL
)
655 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
658 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
664 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
665 if (strstr(uri
, "._tcp"))
667 if ((uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
,
671 #endif /* HAVE_DNSSD || HAVE_AVAHI */
673 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
674 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
675 &port
, resource
, resourcesize
) < HTTP_URI_STATUS_OK
)
677 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer URI."), 1);
680 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
687 * Lookup the address for the server...
691 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
,
694 snprintf(portstr
, sizeof(portstr
), "%d", port
);
696 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
699 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
705 if (cancel
&& *cancel
)
707 httpAddrFreeList(addrlist
);
710 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
,
717 * Create the HTTP object pointing to the server referenced by the URI...
720 if (!strcmp(scheme
, "ipps") || port
== 443)
721 encryption
= HTTP_ENCRYPTION_ALWAYS
;
723 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
725 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0,
729 * Connect if requested...
732 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
735 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
740 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
,
743 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
745 if (cancel
&& *cancel
)
747 CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
749 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
753 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
762 * 'cupsConnectDestBlock()' - Connect to the server for a destination.
764 * Connect to the destination, returning a new http_t connection object and
765 * optionally the resource path to use for the destination. These calls will
766 * block until a connection is made, the timeout expires, the integer pointed
767 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
768 * The caller is responsible for calling httpClose() on the returned object.
770 * @since CUPS 1.6/OS X 10.8@
773 http_t
* /* O - Connection to server or @code NULL@ */
774 cupsConnectDestBlock(
775 cups_dest_t
*dest
, /* I - Destination */
776 unsigned flags
, /* I - Connection flags */
777 int msec
, /* I - Timeout in milliseconds */
778 int *cancel
, /* I - Pointer to "cancel" variable */
779 char *resource
, /* I - Resource buffer */
780 size_t resourcesize
, /* I - Size of resource buffer */
781 cups_dest_block_t block
) /* I - Callback block */
783 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
784 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
786 #endif /* __BLOCKS__ */
790 * 'cupsCopyDest()' - Copy a destination.
792 * Make a copy of the destination to an array of destinations (or just a single
793 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
794 * for calling cupsFreeDests() on the returned object(s).
796 * @since CUPS 1.6/OS X 10.8@
800 cupsCopyDest(cups_dest_t
*dest
,
804 int i
; /* Looping var */
805 cups_dest_t
*new_dest
; /* New destination pointer */
806 cups_option_t
*new_option
, /* Current destination option */
807 *option
; /* Current parent option */
811 * Range check input...
814 if (!dest
|| num_dests
< 0 || !dests
)
818 * See if the destination already exists...
821 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
825 * Protect against copying destination to itself...
828 if (new_dest
== dest
)
832 * Otherwise, free the options...
835 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
837 new_dest
->num_options
= 0;
838 new_dest
->options
= NULL
;
841 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
845 if ((new_dest
->options
= calloc(sizeof(cups_option_t
),
846 dest
->num_options
)) == NULL
)
847 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
849 new_dest
->num_options
= dest
->num_options
;
851 for (i
= dest
->num_options
, option
= dest
->options
,
852 new_option
= new_dest
->options
;
854 i
--, option
++, new_option
++)
856 new_option
->name
= _cupsStrRetain(option
->name
);
857 new_option
->value
= _cupsStrRetain(option
->value
);
866 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
868 * Destinations are enumerated from one or more sources. The callback function
869 * receives the @code user_data@ pointer, destination name, instance, number of
870 * options, and options which can be used as input to the @link cupsAddDest@
871 * function. The function must return 1 to continue enumeration or 0 to stop.
873 * Enumeration happens on the current thread and does not return until all
874 * destinations have been enumerated or the callback function returns 0.
876 * @since CUPS 1.6/OS X 10.8@
879 int /* O - 1 on success, 0 on failure */
881 unsigned flags
, /* I - Enumeration flags */
882 int msec
, /* I - Timeout in milliseconds,
883 * -1 for indefinite */
884 int *cancel
, /* I - Pointer to "cancel" variable */
885 cups_ptype_t type
, /* I - Printer type bits */
886 cups_ptype_t mask
, /* I - Mask for printer type bits */
887 cups_dest_cb_t cb
, /* I - Callback function */
888 void *user_data
) /* I - User data */
890 int i
, /* Looping var */
891 num_dests
; /* Number of destinations */
892 cups_dest_t
*dests
= NULL
, /* Destinations */
893 *dest
; /* Current destination */
894 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
895 int count
, /* Number of queries started */
896 remaining
; /* Remainder of timeout */
897 _cups_dnssd_data_t data
; /* Data for callback */
898 _cups_dnssd_device_t
*device
; /* Current device */
900 int nfds
, /* Number of files responded */
901 main_fd
; /* File descriptor for lookups */
902 DNSServiceRef ipp_ref
, /* IPP browser */
903 local_ipp_ref
; /* Local IPP browser */
905 DNSServiceRef ipps_ref
, /* IPPS browser */
906 local_ipps_ref
; /* Local IPPS browser */
907 # endif /* HAVE_SSL */
909 struct pollfd pfd
; /* Polling data */
911 fd_set input
; /* Input set for select() */
912 struct timeval timeout
; /* Timeout for select() */
913 # endif /* HAVE_POLL */
914 # else /* HAVE_AVAHI */
915 int error
; /* Error value */
916 AvahiServiceBrowser
*ipp_ref
; /* IPP browser */
918 AvahiServiceBrowser
*ipps_ref
; /* IPPS browser */
919 # endif /* HAVE_SSL */
920 # endif /* HAVE_DNSSD */
921 #endif /* HAVE_DNSSD || HAVE_AVAHI */
924 * Range check input...
933 * Get the list of local printers and pass them to the callback function...
936 num_dests
= _cupsGetDests(CUPS_HTTP_DEFAULT
, IPP_OP_CUPS_GET_PRINTERS
, NULL
,
939 for (i
= num_dests
, dest
= dests
;
940 i
> 0 && (!cancel
|| !*cancel
);
942 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
,
946 cupsFreeDests(num_dests
, dests
);
948 if (i
> 0 || msec
== 0)
951 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
953 * Get Bonjour-shared printers...
958 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
,
960 (cups_afree_func_t
)cups_dnssd_free_device
);
963 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
966 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
968 ipp_ref
= data
.main_ref
;
969 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
971 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
973 local_ipp_ref
= data
.main_ref
;
974 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
975 kDNSServiceInterfaceIndexLocalOnly
,
977 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
980 ipps_ref
= data
.main_ref
;
981 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
983 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
985 local_ipps_ref
= data
.main_ref
;
986 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
987 kDNSServiceInterfaceIndexLocalOnly
,
989 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
990 # endif /* HAVE_SSL */
992 # else /* HAVE_AVAHI */
993 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
995 DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
999 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
1001 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
1002 0, cups_dnssd_client_cb
, &data
,
1006 DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
1007 avahi_simple_poll_free(data
.simple_poll
);
1011 ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
,
1012 AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
,
1013 0, cups_dnssd_browse_cb
, &data
);
1015 ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
,
1016 AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
,
1017 0, cups_dnssd_browse_cb
, &data
);
1018 # endif /* HAVE_SSL */
1019 # endif /* HAVE_DNSSD */
1022 remaining
= INT_MAX
;
1026 while (remaining
> 0 && (!cancel
|| !*cancel
))
1029 * Check for input...
1035 pfd
.events
= POLLIN
;
1037 nfds
= poll(&pfd
, 1, remaining
> 250 ? 250 : remaining
);
1041 FD_SET(main_fd
, &input
);
1044 timeout
.tv_usec
= remaining
> 250 ? 250000 : remaining
* 1000;
1046 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
1047 # endif /* HAVE_POLL */
1050 DNSServiceProcessResult(data
.main_ref
);
1054 # else /* HAVE_AVAHI */
1057 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, 250)) > 0)
1060 * We've been told to exit the loop. Perhaps the connection to
1069 # endif /* HAVE_DNSSD */
1071 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
1074 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
1079 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
1081 DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device
->fullName
));
1084 device
->ref
= data
.main_ref
;
1086 if (DNSServiceQueryRecord(&(device
->ref
),
1087 kDNSServiceFlagsShareConnection
,
1088 0, device
->fullName
,
1089 kDNSServiceType_TXT
,
1090 kDNSServiceClass_IN
,
1091 (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
,
1092 &data
) == kDNSServiceErr_NoError
)
1099 device
->state
= _CUPS_DNSSD_ERROR
;
1101 DEBUG_puts("1cupsEnumDests: Query failed.");
1104 # else /* HAVE_AVAHI */
1105 if ((device
->ref
= avahi_record_browser_new(data
.client
,
1112 cups_dnssd_query_cb
,
1119 device
->state
= _CUPS_DNSSD_ERROR
;
1121 DEBUG_printf(("1cupsEnumDests: Query failed: %s",
1122 avahi_strerror(avahi_client_errno(data
.client
))));
1124 # endif /* HAVE_DNSSD */
1126 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
1128 if ((device
->type
& mask
) == type
)
1130 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
1137 device
->state
= _CUPS_DNSSD_ACTIVE
;
1142 cupsArrayDelete(data
.devices
);
1145 DNSServiceRefDeallocate(ipp_ref
);
1146 DNSServiceRefDeallocate(local_ipp_ref
);
1149 DNSServiceRefDeallocate(ipp_ref
);
1150 DNSServiceRefDeallocate(local_ipp_ref
);
1151 # endif /* HAVE_SSL */
1153 DNSServiceRefDeallocate(data
.main_ref
);
1155 # else /* HAVE_AVAHI */
1156 avahi_service_browser_free(ipp_ref
);
1158 avahi_service_browser_free(ipps_ref
);
1159 # endif /* HAVE_SSL */
1161 avahi_client_free(data
.client
);
1162 avahi_simple_poll_free(data
.simple_poll
);
1163 # endif /* HAVE_DNSSD */
1164 #endif /* HAVE_DNSSD || HAVE_DNSSD */
1172 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
1174 * Destinations are enumerated from one or more sources. The block receives the
1175 * destination name, instance, number of options, and options which can be used
1176 * as input to the @link cupsAddDest@ function. The block must return 1 to
1177 * continue enumeration or 0 to stop.
1179 * Enumeration happens on the current thread and does not return until all
1180 * destinations have been enumerated or the block returns 0.
1182 * @since CUPS 1.6/OS X 10.8@
1185 int /* O - 1 on success, 0 on failure */
1187 unsigned flags
, /* I - Enumeration flags */
1188 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1189 int *cancel
, /* I - Pointer to "cancel" variable */
1190 cups_ptype_t type
, /* I - Printer type bits */
1191 cups_ptype_t mask
, /* I - Mask for printer type bits */
1192 cups_dest_block_t block
) /* I - Block */
1194 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1195 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1197 # endif /* __BLOCKS__ */
1201 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1205 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1206 cups_dest_t
*dests
) /* I - Destinations */
1208 int i
; /* Looping var */
1209 cups_dest_t
*dest
; /* Current destination */
1212 if (num_dests
== 0 || dests
== NULL
)
1215 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1217 _cupsStrFree(dest
->name
);
1218 _cupsStrFree(dest
->instance
);
1220 cupsFreeOptions(dest
->num_options
, dest
->options
);
1228 * 'cupsGetDest()' - Get the named destination from the list.
1230 * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
1231 * list of supported destinations for the current user.
1234 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1235 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1236 const char *instance
, /* I - Instance name or @code NULL@ */
1237 int num_dests
, /* I - Number of destinations */
1238 cups_dest_t
*dests
) /* I - Destinations */
1240 int diff
, /* Result of comparison */
1241 match
; /* Matching index */
1244 if (num_dests
<= 0 || !dests
)
1250 * NULL name for default printer.
1253 while (num_dests
> 0)
1255 if (dests
->is_default
)
1265 * Lookup name and optionally the instance...
1268 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1271 return (dests
+ match
);
1279 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1282 const char * /* O - Printer URI */
1283 _cupsGetDestResource(
1284 cups_dest_t
*dest
, /* I - Destination */
1285 char *resource
, /* I - Resource buffer */
1286 size_t resourcesize
) /* I - Size of resource buffer */
1288 const char *uri
; /* Printer URI */
1289 char scheme
[32], /* URI scheme */
1290 userpass
[256], /* Username and password (unused) */
1291 hostname
[256]; /* Hostname */
1292 int port
; /* Port number */
1296 * Range check input...
1299 if (!dest
|| !resource
|| resourcesize
< 1)
1304 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1309 * Grab the printer URI...
1312 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
,
1313 dest
->options
)) == NULL
)
1318 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1324 if (strstr(uri
, "._tcp"))
1326 if ((uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
)) == NULL
)
1329 #endif /* HAVE_DNSSD */
1331 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1332 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1333 &port
, resource
, resourcesize
) < HTTP_URI_STATUS_OK
)
1335 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer URI."), 1);
1345 * '_cupsGetDests()' - Get destinations from a server.
1347 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1348 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1351 * "name" is the name of an existing printer and is only used when "op" is
1352 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1354 * "dest" is initialized to point to the array of destinations.
1356 * 0 is returned if there are no printers, no default printer, or the named
1357 * printer does not exist, respectively.
1359 * Free the memory used by the destination array using the @link cupsFreeDests@
1362 * Note: On OS X this function also gets the default paper from the system
1363 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1364 * options array for each destination that supports it.
1367 int /* O - Number of destinations */
1368 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1369 * @code CUPS_HTTP_DEFAULT@ */
1370 ipp_op_t op
, /* I - IPP operation */
1371 const char *name
, /* I - Name of destination */
1372 cups_dest_t
**dests
, /* IO - Destinations */
1373 cups_ptype_t type
, /* I - Printer type bits */
1374 cups_ptype_t mask
) /* I - Printer type mask */
1376 int num_dests
= 0; /* Number of destinations */
1377 cups_dest_t
*dest
; /* Current destination */
1378 ipp_t
*request
, /* IPP Request */
1379 *response
; /* IPP Response */
1380 ipp_attribute_t
*attr
; /* Current attribute */
1381 const char *printer_name
; /* printer-name attribute */
1382 char uri
[1024]; /* printer-uri value */
1383 int num_options
; /* Number of options */
1384 cups_option_t
*options
; /* Options */
1386 char media_default
[41]; /* Default paper size */
1387 #endif /* __APPLE__ */
1388 char optname
[1024], /* Option name */
1389 value
[2048], /* Option value */
1390 *ptr
; /* Pointer into name/value */
1391 static const char * const pattrs
[] = /* Attributes we're interested in */
1393 "auth-info-required",
1395 "job-sheets-default",
1396 "marker-change-time",
1398 "marker-high-levels",
1400 "marker-low-levels",
1406 #endif /* __APPLE__ */
1410 "printer-is-accepting-jobs",
1411 "printer-is-shared",
1413 "printer-make-and-model",
1414 "printer-mandatory-job-attributes",
1417 "printer-state-change-time",
1418 "printer-state-reasons",
1420 "printer-uri-supported"
1426 * Get the default paper size...
1429 appleGetPaperSize(media_default
, sizeof(media_default
));
1430 #endif /* __APPLE__ */
1433 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1434 * require the following attributes:
1436 * attributes-charset
1437 * attributes-natural-language
1438 * requesting-user-name
1439 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1442 request
= ippNewRequest(op
);
1444 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1445 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1448 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1449 "requesting-user-name", NULL
, cupsUser());
1451 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1453 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1454 "localhost", ippPort(), "/printers/%s", name
);
1455 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1460 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type",
1462 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask",
1467 * Do the request and get back a response...
1470 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1472 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1475 * Skip leading attributes until we hit a printer...
1478 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1485 * Pull the needed attributes from this printer...
1488 printer_name
= NULL
;
1492 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1494 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1495 attr
->value_tag
!= IPP_TAG_ENUM
&&
1496 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1497 attr
->value_tag
!= IPP_TAG_TEXT
&&
1498 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1499 attr
->value_tag
!= IPP_TAG_NAME
&&
1500 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1501 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1502 attr
->value_tag
!= IPP_TAG_RANGE
&&
1503 attr
->value_tag
!= IPP_TAG_URI
)
1506 if (!strcmp(attr
->name
, "auth-info-required") ||
1507 !strcmp(attr
->name
, "device-uri") ||
1508 !strcmp(attr
->name
, "marker-change-time") ||
1509 !strcmp(attr
->name
, "marker-colors") ||
1510 !strcmp(attr
->name
, "marker-high-levels") ||
1511 !strcmp(attr
->name
, "marker-levels") ||
1512 !strcmp(attr
->name
, "marker-low-levels") ||
1513 !strcmp(attr
->name
, "marker-message") ||
1514 !strcmp(attr
->name
, "marker-names") ||
1515 !strcmp(attr
->name
, "marker-types") ||
1516 !strcmp(attr
->name
, "printer-commands") ||
1517 !strcmp(attr
->name
, "printer-info") ||
1518 !strcmp(attr
->name
, "printer-is-shared") ||
1519 !strcmp(attr
->name
, "printer-make-and-model") ||
1520 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1521 !strcmp(attr
->name
, "printer-state") ||
1522 !strcmp(attr
->name
, "printer-state-change-time") ||
1523 !strcmp(attr
->name
, "printer-type") ||
1524 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1525 !strcmp(attr
->name
, "printer-location") ||
1526 !strcmp(attr
->name
, "printer-state-reasons") ||
1527 !strcmp(attr
->name
, "printer-uri-supported"))
1530 * Add a printer description attribute...
1533 num_options
= cupsAddOption(attr
->name
,
1534 cups_make_string(attr
, value
,
1536 num_options
, &options
);
1539 else if (!strcmp(attr
->name
, "media-supported"))
1542 * See if we can set a default media size...
1545 int i
; /* Looping var */
1547 for (i
= 0; i
< attr
->num_values
; i
++)
1548 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1550 num_options
= cupsAddOption("media", media_default
, num_options
,
1555 #endif /* __APPLE__ */
1556 else if (!strcmp(attr
->name
, "printer-name") &&
1557 attr
->value_tag
== IPP_TAG_NAME
)
1558 printer_name
= attr
->values
[0].string
.text
;
1559 else if (strncmp(attr
->name
, "notify-", 7) &&
1560 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1561 attr
->value_tag
== IPP_TAG_ENUM
||
1562 attr
->value_tag
== IPP_TAG_INTEGER
||
1563 attr
->value_tag
== IPP_TAG_KEYWORD
||
1564 attr
->value_tag
== IPP_TAG_NAME
||
1565 attr
->value_tag
== IPP_TAG_RANGE
) &&
1566 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1569 * Add a default option...
1572 strlcpy(optname
, attr
->name
, sizeof(optname
));
1573 optname
[ptr
- attr
->name
] = '\0';
1575 if (_cups_strcasecmp(optname
, "media") ||
1576 !cupsGetOption("media", num_options
, options
))
1577 num_options
= cupsAddOption(optname
,
1578 cups_make_string(attr
, value
,
1580 num_options
, &options
);
1585 * See if we have everything needed...
1590 cupsFreeOptions(num_options
, options
);
1598 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1600 dest
->num_options
= num_options
;
1601 dest
->options
= options
;
1604 cupsFreeOptions(num_options
, options
);
1610 ippDelete(response
);
1614 * Return the count...
1622 * 'cupsGetDests()' - Get the list of destinations from the default server.
1624 * Starting with CUPS 1.2, the returned list of destinations include the
1625 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1626 * printer-make-and-model, printer-state, printer-state-change-time,
1627 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1628 * adds the marker-change-time, marker-colors, marker-high-levels,
1629 * marker-levels, marker-low-levels, marker-message, marker-names,
1630 * marker-types, and printer-commands attributes as well.
1632 * Use the @link cupsFreeDests@ function to free the destination list and
1633 * the @link cupsGetDest@ function to find a particular destination.
1636 int /* O - Number of destinations */
1637 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1639 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1644 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1646 * Starting with CUPS 1.2, the returned list of destinations include the
1647 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1648 * printer-make-and-model, printer-state, printer-state-change-time,
1649 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1650 * adds the marker-change-time, marker-colors, marker-high-levels,
1651 * marker-levels, marker-low-levels, marker-message, marker-names,
1652 * marker-types, and printer-commands attributes as well.
1654 * Use the @link cupsFreeDests@ function to free the destination list and
1655 * the @link cupsGetDest@ function to find a particular destination.
1657 * @since CUPS 1.1.21/OS X 10.4@
1660 int /* O - Number of destinations */
1661 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1662 cups_dest_t
**dests
) /* O - Destinations */
1664 int num_dests
; /* Number of destinations */
1665 cups_dest_t
*dest
; /* Destination pointer */
1666 const char *home
; /* HOME environment variable */
1667 char filename
[1024]; /* Local ~/.cups/lpoptions file */
1668 const char *defprinter
; /* Default printer */
1669 char name
[1024], /* Copy of printer name */
1670 *instance
, /* Pointer to instance name */
1671 *user_default
; /* User default printer */
1672 int num_reals
; /* Number of real queues */
1673 cups_dest_t
*reals
; /* Real queues */
1674 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1678 * Range check the input...
1683 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
1688 * Grab the printers and classes...
1691 *dests
= (cups_dest_t
*)0;
1692 num_dests
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, dests
, 0, 0);
1694 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
1696 cupsFreeDests(num_dests
, *dests
);
1697 *dests
= (cups_dest_t
*)0;
1702 * Make a copy of the "real" queues for a later sanity check...
1707 num_reals
= num_dests
;
1708 reals
= calloc(num_reals
, sizeof(cups_dest_t
));
1711 memcpy(reals
, *dests
, num_reals
* sizeof(cups_dest_t
));
1722 * Grab the default destination...
1725 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1727 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
1729 strlcpy(name
, defprinter
, sizeof(name
));
1736 * Separate printer and instance name...
1739 if ((instance
= strchr(name
, '/')) != NULL
)
1743 * Lookup the printer and instance and make it the default...
1746 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
1747 dest
->is_default
= 1;
1753 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
1756 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1757 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
1760 if ((home
= getenv("HOME")) != NULL
)
1762 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1764 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
1769 * Validate the current default destination - this prevents old
1770 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
1771 * pointing to a non-existent printer or class...
1777 * See if we have a default printer...
1780 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
1783 * Have a default; see if it is real...
1786 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
1789 * Remove the non-real printer from the list, since we don't want jobs
1790 * going to an unexpected printer... (<rdar://problem/14216472>)
1793 num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
,
1806 * Return the number of destinations...
1810 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
1817 * 'cupsGetNamedDest()' - Get options for the named destination.
1819 * This function is optimized for retrieving a single destination and should
1820 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
1821 * know the name of the destination or want to print to the default destination.
1822 * If @code NULL@ is returned, the destination does not exist or there is no
1823 * default destination.
1825 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
1826 * server will be used.
1828 * If "name" is @code NULL@, the default printer for the current user will be
1831 * The returned destination must be freed using @link cupsFreeDests@ with a
1832 * "num_dests" value of 1.
1834 * @since CUPS 1.4/OS X 10.6@
1837 cups_dest_t
* /* O - Destination or @code NULL@ */
1838 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1839 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1840 const char *instance
) /* I - Instance name or @code NULL@ */
1842 cups_dest_t
*dest
; /* Destination */
1843 char filename
[1024], /* Path to lpoptions */
1844 defname
[256]; /* Default printer name */
1845 const char *home
= getenv("HOME"); /* Home directory */
1846 int set_as_default
= 0; /* Set returned destination as default */
1847 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
1848 /* IPP operation to get server ops */
1849 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1853 * If "name" is NULL, find the default destination...
1859 name
= _cupsUserDefault(defname
, sizeof(defname
));
1863 char *ptr
; /* Temporary pointer... */
1865 if ((ptr
= strchr(defname
, '/')) != NULL
)
1876 * No default in the environment, try the user's lpoptions files...
1879 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1881 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1887 * Still not there? Try the system lpoptions file...
1890 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
1891 cg
->cups_serverroot
);
1892 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1898 * No locally-set default destination, ask the server...
1901 op
= IPP_OP_CUPS_GET_DEFAULT
;
1906 * Get the printer's attributes...
1909 if (!_cupsGetDests(http
, op
, name
, &dest
, 0, 0))
1911 if (op
== IPP_OP_CUPS_GET_DEFAULT
|| (name
&& !set_as_default
))
1915 * The default printer from environment variables or from a
1916 * configuration file does not exist. Find out the real default.
1919 if (!_cupsGetDests(http
, IPP_OP_CUPS_GET_DEFAULT
, NULL
, &dest
, 0, 0))
1924 dest
->instance
= _cupsStrAlloc(instance
);
1927 dest
->is_default
= 1;
1930 * Then add local options...
1933 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1934 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
1938 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1940 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
1944 * Return the result...
1952 * 'cupsRemoveDest()' - Remove a destination from the destination list.
1954 * Removing a destination/instance does not delete the class or printer
1955 * queue, merely the lpoptions for that destination/instance. Use the
1956 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
1957 * options for the user.
1959 * @since CUPS 1.3/OS X 10.5@
1962 int /* O - New number of destinations */
1963 cupsRemoveDest(const char *name
, /* I - Destination name */
1964 const char *instance
, /* I - Instance name or @code NULL@ */
1965 int num_dests
, /* I - Number of destinations */
1966 cups_dest_t
**dests
) /* IO - Destinations */
1968 int i
; /* Index into destinations */
1969 cups_dest_t
*dest
; /* Pointer to destination */
1973 * Find the destination...
1976 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1983 _cupsStrFree(dest
->name
);
1984 _cupsStrFree(dest
->instance
);
1985 cupsFreeOptions(dest
->num_options
, dest
->options
);
1988 * Remove the destination from the array...
1996 memmove(dest
, dest
+ 1, (num_dests
- i
) * sizeof(cups_dest_t
));
2003 * 'cupsSetDefaultDest()' - Set the default destination.
2005 * @since CUPS 1.3/OS X 10.5@
2010 const char *name
, /* I - Destination name */
2011 const char *instance
, /* I - Instance name or @code NULL@ */
2012 int num_dests
, /* I - Number of destinations */
2013 cups_dest_t
*dests
) /* I - Destinations */
2015 int i
; /* Looping var */
2016 cups_dest_t
*dest
; /* Current destination */
2020 * Range check input...
2023 if (!name
|| num_dests
<= 0 || !dests
)
2027 * Loop through the array and set the "is_default" flag for the matching
2031 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2032 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2033 ((!instance
&& !dest
->instance
) ||
2034 (instance
&& dest
->instance
&&
2035 !_cups_strcasecmp(instance
, dest
->instance
)));
2040 * 'cupsSetDests()' - Save the list of destinations for the default server.
2042 * This function saves the destinations to /etc/cups/lpoptions when run
2043 * as root and ~/.cups/lpoptions when run as a normal user.
2047 cupsSetDests(int num_dests
, /* I - Number of destinations */
2048 cups_dest_t
*dests
) /* I - Destinations */
2050 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2055 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2057 * This function saves the destinations to /etc/cups/lpoptions when run
2058 * as root and ~/.cups/lpoptions when run as a normal user.
2060 * @since CUPS 1.1.21/OS X 10.4@
2063 int /* O - 0 on success, -1 on error */
2064 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2065 int num_dests
, /* I - Number of destinations */
2066 cups_dest_t
*dests
) /* I - Destinations */
2068 int i
, j
; /* Looping vars */
2069 int wrote
; /* Wrote definition? */
2070 cups_dest_t
*dest
; /* Current destination */
2071 cups_option_t
*option
; /* Current option */
2072 _ipp_option_t
*match
; /* Matching attribute for option */
2073 FILE *fp
; /* File pointer */
2075 const char *home
; /* HOME environment variable */
2077 char filename
[1024]; /* lpoptions file */
2078 int num_temps
; /* Number of temporary destinations */
2079 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2080 *temp
; /* Current temporary dest */
2081 const char *val
; /* Value of temporary option */
2082 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2086 * Range check the input...
2089 if (!num_dests
|| !dests
)
2093 * Get the server destinations...
2096 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2098 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2100 cupsFreeDests(num_temps
, temps
);
2105 * Figure out which file to write to...
2108 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2114 * Merge in server defaults...
2117 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2120 * Point to user defaults...
2123 if ((home
= getenv("HOME")) != NULL
)
2126 * Create ~/.cups subdirectory...
2129 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2130 if (access(filename
, 0))
2131 mkdir(filename
, 0700);
2133 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2139 * Try to open the file...
2142 if ((fp
= fopen(filename
, "w")) == NULL
)
2144 cupsFreeDests(num_temps
, temps
);
2150 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2155 fchmod(fileno(fp
), 0644);
2159 * Write each printer; each line looks like:
2161 * Dest name[/instance] options
2162 * Default name[/instance] options
2165 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2166 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2168 if (dest
->is_default
)
2170 fprintf(fp
, "Default %s", dest
->name
);
2172 fprintf(fp
, "/%s", dest
->instance
);
2179 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2180 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2182 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2185 * See if this option is a printer attribute; if so, skip it...
2188 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2189 match
->group_tag
== IPP_TAG_PRINTER
)
2193 * See if the server/global options match these; if so, don't
2198 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2199 temp
->options
)) != NULL
&&
2200 !_cups_strcasecmp(val
, option
->value
))
2204 * Options don't match, write to the file...
2209 fprintf(fp
, "Dest %s", dest
->name
);
2211 fprintf(fp
, "/%s", dest
->instance
);
2215 if (option
->value
[0])
2217 if (strchr(option
->value
, ' ') ||
2218 strchr(option
->value
, '\\') ||
2219 strchr(option
->value
, '\"') ||
2220 strchr(option
->value
, '\''))
2223 * Quote the value...
2226 fprintf(fp
, " %s=\"", option
->name
);
2228 for (val
= option
->value
; *val
; val
++)
2230 if (strchr("\"\'\\", *val
))
2241 * Store the literal value...
2244 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2248 fprintf(fp
, " %s", option
->name
);
2256 * Free the temporary destinations and close the file...
2259 cupsFreeDests(num_temps
, temps
);
2265 * Set the default printer for this location - this allows command-line
2266 * and GUI applications to share the same default destination...
2269 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2271 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2273 kCFStringEncodingUTF8
);
2274 /* Default printer name */
2278 _cupsAppleSetDefaultPrinter(name
);
2282 #endif /* __APPLE__ */
2284 #ifdef HAVE_NOTIFY_POST
2286 * Send a notification so that MacOS X applications can know about the
2290 notify_post("com.apple.printerListChange");
2291 #endif /* HAVE_NOTIFY_POST */
2298 * '_cupsUserDefault()' - Get the user default printer from environment
2299 * variables and location information.
2302 char * /* O - Default printer or NULL */
2303 _cupsUserDefault(char *name
, /* I - Name buffer */
2304 size_t namesize
) /* I - Size of name buffer */
2306 const char *env
; /* LPDEST or PRINTER env variable */
2308 CFStringRef locprinter
; /* Last printer as this location */
2309 #endif /* __APPLE__ */
2312 if ((env
= getenv("LPDEST")) == NULL
)
2313 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2318 strlcpy(name
, env
, namesize
);
2324 * Use location-based defaults if "use last printer" is selected in the
2325 * system preferences...
2328 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2330 CFStringGetCString(locprinter
, name
, namesize
, kCFStringEncodingUTF8
);
2331 CFRelease(locprinter
);
2336 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2338 return (*name
? name
: NULL
);
2342 * No location-based defaults on this platform...
2347 #endif /* __APPLE__ */
2353 * 'appleCopyLocations()' - Copy the location history array.
2356 static CFArrayRef
/* O - Location array or NULL */
2357 appleCopyLocations(void)
2359 CFArrayRef locations
; /* Location array */
2363 * Look up the location array in the preferences...
2366 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2367 kCUPSPrintingPrefs
)) == NULL
)
2370 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2372 CFRelease(locations
);
2381 * 'appleCopyNetwork()' - Get the network ID for the current location.
2384 static CFStringRef
/* O - Network ID */
2385 appleCopyNetwork(void)
2387 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2388 CFStringRef key
; /* Current network configuration key */
2389 CFDictionaryRef ip_dict
; /* Network configuration data */
2390 CFStringRef network
= NULL
; /* Current network ID */
2393 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2397 * First use the IPv6 router address, if available, since that will generally
2398 * be a globally-unique link-local address.
2401 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2402 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2404 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2406 if ((network
= CFDictionaryGetValue(ip_dict
,
2407 kSCPropNetIPv6Router
)) != NULL
)
2417 * If that doesn't work, try the IPv4 router address. This isn't as unique
2418 * and will likely be a 10.x.y.z or 192.168.y.z address...
2423 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2424 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2426 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2428 if ((network
= CFDictionaryGetValue(ip_dict
,
2429 kSCPropNetIPv4Router
)) != NULL
)
2439 CFRelease(dynamicStore
);
2447 * 'appleGetPaperSize()' - Get the default paper size.
2450 static char * /* O - Default paper size */
2451 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2452 int namesize
) /* I - Size of buffer */
2454 CFStringRef defaultPaperID
; /* Default paper ID */
2455 pwg_media_t
*pwgmedia
; /* PWG media size */
2458 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2459 if (!defaultPaperID
||
2460 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2461 !CFStringGetCString(defaultPaperID
, name
, namesize
,
2462 kCFStringEncodingUTF8
))
2464 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2465 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2468 CFRelease(defaultPaperID
);
2475 * 'appleGetPrinter()' - Get a printer from the history array.
2478 static CFStringRef
/* O - Printer name or NULL */
2479 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2480 CFStringRef network
, /* I - Network name */
2481 CFIndex
*locindex
) /* O - Index in array */
2483 CFIndex i
, /* Looping var */
2484 count
; /* Number of locations */
2485 CFDictionaryRef location
; /* Current location */
2486 CFStringRef locnetwork
, /* Current network */
2487 locprinter
; /* Current printer */
2490 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2491 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2492 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2494 if ((locnetwork
= CFDictionaryGetValue(location
,
2495 kLocationNetworkKey
)) != NULL
&&
2496 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2497 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2498 (locprinter
= CFDictionaryGetValue(location
,
2499 kLocationPrinterIDKey
)) != NULL
&&
2500 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2505 return (locprinter
);
2511 #endif /* __APPLE__ */
2515 * 'cups_add_dest()' - Add a destination to the array.
2517 * Unlike cupsAddDest(), this function does not check for duplicates.
2520 static cups_dest_t
* /* O - New destination */
2521 cups_add_dest(const char *name
, /* I - Name of destination */
2522 const char *instance
, /* I - Instance or NULL */
2523 int *num_dests
, /* IO - Number of destinations */
2524 cups_dest_t
**dests
) /* IO - Destinations */
2526 int insert
, /* Insertion point */
2527 diff
; /* Result of comparison */
2528 cups_dest_t
*dest
; /* Destination pointer */
2532 * Add new destination...
2535 if (*num_dests
== 0)
2536 dest
= malloc(sizeof(cups_dest_t
));
2538 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (*num_dests
+ 1));
2546 * Find where to insert the destination...
2549 if (*num_dests
== 0)
2553 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2561 * Move the array elements as needed...
2564 if (insert
< *num_dests
)
2565 memmove(*dests
+ insert
+ 1, *dests
+ insert
,
2566 (*num_dests
- insert
) * sizeof(cups_dest_t
));
2571 * Initialize the destination...
2574 dest
= *dests
+ insert
;
2575 dest
->name
= _cupsStrAlloc(name
);
2576 dest
->instance
= _cupsStrAlloc(instance
);
2577 dest
->is_default
= 0;
2578 dest
->num_options
= 0;
2579 dest
->options
= (cups_option_t
*)0;
2587 * 'cups_block_cb()' - Enumeration callback for block API.
2590 static int /* O - 1 to continue, 0 to stop */
2592 cups_dest_block_t block
, /* I - Block */
2593 unsigned flags
, /* I - Destination flags */
2594 cups_dest_t
*dest
) /* I - Destination */
2596 return ((block
)(flags
, dest
));
2598 # endif /* __BLOCKS__ */
2602 * 'cups_compare_dests()' - Compare two destinations.
2605 static int /* O - Result of comparison */
2606 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2607 cups_dest_t
*b
) /* I - Second destination */
2609 int diff
; /* Difference */
2612 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2614 else if (a
->instance
&& b
->instance
)
2615 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2617 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2621 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2624 * 'cups_dnssd_browse_cb()' - Browse for printers.
2628 cups_dnssd_browse_cb(
2629 DNSServiceRef sdRef
, /* I - Service reference */
2630 DNSServiceFlags flags
, /* I - Option flags */
2631 uint32_t interfaceIndex
, /* I - Interface number */
2632 DNSServiceErrorType errorCode
, /* I - Error, if any */
2633 const char *serviceName
, /* I - Name of service/device */
2634 const char *regtype
, /* I - Type of service */
2635 const char *replyDomain
, /* I - Service domain */
2636 void *context
) /* I - Enumeration data */
2638 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2639 /* Enumeration data */
2642 DEBUG_printf(("5cups_dnssd_browse_cb(sdRef=%p, flags=%x, "
2643 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
2644 "regtype=\"%s\", replyDomain=\"%s\", context=%p)",
2645 sdRef
, flags
, interfaceIndex
, errorCode
, serviceName
, regtype
,
2646 replyDomain
, context
));
2649 * Don't do anything on error...
2652 if (errorCode
!= kDNSServiceErr_NoError
)
2659 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2663 # else /* HAVE_AVAHI */
2665 * 'cups_dnssd_browse_cb()' - Browse for printers.
2669 cups_dnssd_browse_cb(
2670 AvahiServiceBrowser
*browser
, /* I - Browser */
2671 AvahiIfIndex interface
, /* I - Interface index (unused) */
2672 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2673 AvahiBrowserEvent event
, /* I - What happened */
2674 const char *name
, /* I - Service name */
2675 const char *type
, /* I - Registration type */
2676 const char *domain
, /* I - Domain */
2677 AvahiLookupResultFlags flags
, /* I - Flags */
2678 void *context
) /* I - Devices array */
2681 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
2682 /* Client information */
2684 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2685 /* Enumeration data */
2694 case AVAHI_BROWSER_FAILURE
:
2695 DEBUG_printf(("cups_dnssd_browse_cb: %s",
2696 avahi_strerror(avahi_client_errno(client
))));
2697 avahi_simple_poll_quit(data
->simple_poll
);
2700 case AVAHI_BROWSER_NEW
:
2702 * This object is new on the network.
2705 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
2708 * This comes from the local machine so ignore it.
2711 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".",
2717 * Create a device entry for it if it doesn't yet exist.
2720 cups_dnssd_get_device(data
, name
, type
, domain
);
2724 case AVAHI_BROWSER_REMOVE
:
2725 case AVAHI_BROWSER_ALL_FOR_NOW
:
2726 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
2733 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2737 cups_dnssd_client_cb(
2738 AvahiClient
*client
, /* I - Client information (unused) */
2739 AvahiClientState state
, /* I - Current state */
2740 void *context
) /* I - User data (unused) */
2742 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2743 /* Enumeration data */
2749 * If the connection drops, quit.
2752 if (state
== AVAHI_CLIENT_FAILURE
)
2754 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2755 avahi_simple_poll_quit(data
->simple_poll
);
2758 # endif /* HAVE_DNSSD */
2762 * 'cups_dnssd_compare_device()' - Compare two devices.
2765 static int /* O - Result of comparison */
2766 cups_dnssd_compare_devices(
2767 _cups_dnssd_device_t
*a
, /* I - First device */
2768 _cups_dnssd_device_t
*b
) /* I - Second device */
2770 return (strcmp(a
->dest
.name
, b
->dest
.name
));
2775 * 'cups_dnssd_free_device()' - Free the memory used by a device.
2779 cups_dnssd_free_device(
2780 _cups_dnssd_device_t
*device
, /* I - Device */
2781 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
2783 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", device
,
2784 device
->dest
.name
, data
));
2788 DNSServiceRefDeallocate(device
->ref
);
2789 # else /* HAVE_AVAHI */
2791 avahi_record_browser_free(device
->ref
);
2792 # endif /* HAVE_DNSSD */
2794 _cupsStrFree(device
->domain
);
2795 _cupsStrFree(device
->fullName
);
2796 _cupsStrFree(device
->regtype
);
2797 _cupsStrFree(device
->dest
.name
);
2799 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
2806 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
2809 static _cups_dnssd_device_t
* /* O - Device */
2810 cups_dnssd_get_device(
2811 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
2812 const char *serviceName
, /* I - Service name */
2813 const char *regtype
, /* I - Registration type */
2814 const char *replyDomain
) /* I - Domain name */
2816 _cups_dnssd_device_t key
, /* Search key */
2817 *device
; /* Device */
2818 char fullName
[kDNSServiceMaxDomainName
];
2819 /* Full name for query */
2822 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", "
2823 "regtype=\"%s\", replyDomain=\"%s\")", data
, serviceName
,
2824 regtype
, replyDomain
));
2827 * See if this is an existing device...
2830 key
.dest
.name
= (char *)serviceName
;
2832 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
2835 * Yes, see if we need to do anything with this...
2838 int update
= 0; /* Non-zero if we need to update */
2840 if (!_cups_strcasecmp(replyDomain
, "local.") &&
2841 _cups_strcasecmp(device
->domain
, replyDomain
))
2844 * Update the "global" listing to use the .local domain name instead.
2847 _cupsStrFree(device
->domain
);
2848 device
->domain
= _cupsStrAlloc(replyDomain
);
2850 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
2851 "domain.", device
->dest
.name
));
2856 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
2857 _cups_strcasecmp(device
->regtype
, regtype
))
2860 * Prefer IPPS over IPP.
2863 _cupsStrFree(device
->regtype
);
2864 device
->regtype
= _cupsStrAlloc(regtype
);
2866 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
2867 device
->dest
.name
));
2874 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
2875 device
->dest
.name
));
2882 * No, add the device...
2885 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
2886 "'%s'.", serviceName
,
2887 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
2890 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
2891 device
->dest
.name
= _cupsStrAlloc(serviceName
);
2892 device
->domain
= _cupsStrAlloc(replyDomain
);
2893 device
->regtype
= _cupsStrAlloc(regtype
);
2895 cupsArrayAdd(data
->devices
, device
);
2899 * Set the "full name" of this service, which is used for queries...
2903 DNSServiceConstructFullName(fullName
, device
->dest
.name
, device
->regtype
,
2905 # else /* HAVE_AVAHI */
2906 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
,
2907 regtype
, replyDomain
);
2908 # endif /* HAVE_DNSSD */
2910 _cupsStrFree(device
->fullName
);
2911 device
->fullName
= _cupsStrAlloc(fullName
);
2916 DNSServiceRefDeallocate(device
->ref
);
2917 # else /* HAVE_AVAHI */
2918 avahi_record_browser_free(device
->ref
);
2919 # endif /* HAVE_DNSSD */
2924 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
2926 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
2927 device
->state
= _CUPS_DNSSD_NEW
;
2936 * 'cups_dnssd_local_cb()' - Browse for local printers.
2940 cups_dnssd_local_cb(
2941 DNSServiceRef sdRef
, /* I - Service reference */
2942 DNSServiceFlags flags
, /* I - Option flags */
2943 uint32_t interfaceIndex
, /* I - Interface number */
2944 DNSServiceErrorType errorCode
, /* I - Error, if any */
2945 const char *serviceName
, /* I - Name of service/device */
2946 const char *regtype
, /* I - Type of service */
2947 const char *replyDomain
, /* I - Service domain */
2948 void *context
) /* I - Devices array */
2950 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2951 /* Enumeration data */
2952 _cups_dnssd_device_t
*device
; /* Device */
2955 DEBUG_printf(("5cups_dnssd_local_cb(sdRef=%p, flags=%x, "
2956 "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", "
2957 "regtype=\"%s\", replyDomain=\"%s\", context=%p)",
2958 sdRef
, flags
, interfaceIndex
, errorCode
, serviceName
,
2959 regtype
, replyDomain
, context
));
2962 * Only process "add" data...
2965 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
2972 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2975 * Hide locally-registered devices...
2978 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
2983 DNSServiceRefDeallocate(device
->ref
);
2987 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
2988 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
2990 device
->state
= _CUPS_DNSSD_LOCAL
;
2992 # endif /* HAVE_DNSSD */
2997 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
2999 * Note: This function is needed because avahi_simple_poll_iterate is broken
3000 * and always uses a timeout of 0 (!) milliseconds.
3001 * (Avahi Ticket #364)
3004 static int /* O - Number of file descriptors matching */
3006 struct pollfd
*pollfds
, /* I - File descriptors */
3007 unsigned int num_pollfds
, /* I - Number of file descriptors */
3008 int timeout
, /* I - Timeout in milliseconds (unused) */
3009 void *context
) /* I - User data (unused) */
3011 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3012 /* Enumeration data */
3013 int val
; /* Return value */
3018 val
= poll(pollfds
, num_pollfds
, 250);
3022 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3029 # endif /* HAVE_AVAHI */
3033 * 'cups_dnssd_query_cb()' - Process query data.
3038 cups_dnssd_query_cb(
3039 DNSServiceRef sdRef
, /* I - Service reference */
3040 DNSServiceFlags flags
, /* I - Data flags */
3041 uint32_t interfaceIndex
, /* I - Interface */
3042 DNSServiceErrorType errorCode
, /* I - Error, if any */
3043 const char *fullName
, /* I - Full service name */
3044 uint16_t rrtype
, /* I - Record type */
3045 uint16_t rrclass
, /* I - Record class */
3046 uint16_t rdlen
, /* I - Length of record data */
3047 const void *rdata
, /* I - Record data */
3048 uint32_t ttl
, /* I - Time-to-live */
3049 void *context
) /* I - Enumeration data */
3051 # else /* HAVE_AVAHI */
3053 cups_dnssd_query_cb(
3054 AvahiRecordBrowser
*browser
, /* I - Record browser */
3055 AvahiIfIndex interfaceIndex
,
3056 /* I - Interface index (unused) */
3057 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3058 AvahiBrowserEvent event
, /* I - What happened? */
3059 const char *fullName
, /* I - Service name */
3060 uint16_t rrclass
, /* I - Record class */
3061 uint16_t rrtype
, /* I - Record type */
3062 const void *rdata
, /* I - TXT record */
3063 size_t rdlen
, /* I - Length of TXT record */
3064 AvahiLookupResultFlags flags
, /* I - Flags */
3065 void *context
) /* I - Enumeration data */
3068 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3069 /* Client information */
3071 # endif /* HAVE_DNSSD */
3072 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3073 /* Enumeration data */
3074 char name
[1024], /* Service name */
3075 *ptr
; /* Pointer into string */
3076 _cups_dnssd_device_t dkey
, /* Search key */
3077 *device
; /* Device */
3081 DEBUG_printf(("5cups_dnssd_query_cb(sdRef=%p, flags=%x, "
3082 "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", "
3083 "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, "
3084 "context=%p)", sdRef
, flags
, interfaceIndex
, errorCode
,
3085 fullName
, rrtype
, rrclass
, rdlen
, rdata
, ttl
, context
));
3088 * Only process "add" data...
3091 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3094 # else /* HAVE_AVAHI */
3095 DEBUG_printf(("5cups_dnssd_query_cb(browser=%p, interfaceIndex=%d, "
3096 "protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, "
3097 "rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)",
3098 browser
, interfaceIndex
, protocol
, event
, fullName
, rrclass
,
3099 rrtype
, rdata
, (unsigned)rdlen
, flags
, context
));
3102 * Only process "add" data...
3105 if (event
!= AVAHI_BROWSER_NEW
)
3107 if (event
== AVAHI_BROWSER_FAILURE
)
3108 DEBUG_printf(("cups_dnssd_query_cb: %s",
3109 avahi_strerror(avahi_client_errno(client
))));
3113 # endif /* HAVE_DNSSD */
3116 * Lookup the service in the devices array.
3119 dkey
.dest
.name
= name
;
3121 cups_dnssd_unquote(name
, fullName
, sizeof(name
));
3123 if ((ptr
= strstr(name
, "._")) != NULL
)
3126 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
)
3129 * Found it, pull out the make and model from the TXT record and save it...
3132 const uint8_t *txt
, /* Pointer into data */
3133 *txtnext
, /* Next key/value pair */
3134 *txtend
; /* End of entire TXT record */
3135 uint8_t txtlen
; /* Length of current key/value pair */
3136 char key
[256], /* Key string */
3137 value
[256], /* Value string */
3138 make_and_model
[512],
3139 /* Manufacturer and model */
3140 model
[256], /* Model */
3141 uriname
[1024], /* Name for URI */
3142 uri
[1024]; /* Printer URI */
3143 cups_ptype_t type
= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_BW
;
3145 int saw_printer_type
= 0;
3146 /* Did we see a printer-type key? */
3148 device
->state
= _CUPS_DNSSD_PENDING
;
3149 make_and_model
[0] = '\0';
3151 strlcpy(model
, "Unknown", sizeof(model
));
3153 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3158 * Read a key/value pair starting with an 8-bit length. Since the
3159 * length is 8 bits and the size of the key/value buffers is 256, we
3160 * don't need to check for overflow...
3165 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3168 txtnext
= txt
+ txtlen
;
3170 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3174 if (txt
< txtnext
&& *txt
== '=')
3179 memcpy(value
, txt
, txtnext
- txt
);
3180 value
[txtnext
- txt
] = '\0';
3182 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3186 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3190 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3191 !_cups_strcasecmp(key
, "usb_MANU") ||
3192 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3193 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3194 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3195 !_cups_strcasecmp(key
, "usb_MODEL"))
3196 strlcpy(model
, value
, sizeof(model
));
3197 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3199 if (value
[0] == '(')
3202 * Strip parenthesis...
3205 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3208 strlcpy(model
, value
+ 1, sizeof(model
));
3211 strlcpy(model
, value
, sizeof(model
));
3213 else if (!_cups_strcasecmp(key
, "ty"))
3215 strlcpy(model
, value
, sizeof(model
));
3217 if ((ptr
= strchr(model
, ',')) != NULL
)
3220 else if (!_cups_strcasecmp(key
, "note"))
3221 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3222 device
->dest
.num_options
,
3223 &device
->dest
.options
);
3224 else if (!_cups_strcasecmp(key
, "pdl"))
3227 * Look for PDF-capable printers; only PDF-capable printers are shown.
3230 const char *start
, *next
; /* Pointer into value */
3231 int have_pdf
= 0; /* Have PDF? */
3233 for (start
= value
; start
&& *start
; start
= next
)
3235 if (!_cups_strncasecmp(start
, "application/pdf", 15) &&
3236 (!start
[15] || start
[15] == ','))
3242 if ((next
= strchr(start
, ',')) != NULL
)
3247 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3249 else if (!_cups_strcasecmp(key
, "printer-type"))
3252 * Value is either NNNN or 0xXXXX
3255 saw_printer_type
= 1;
3256 type
= strtol(value
, NULL
, 0);
3258 else if (!saw_printer_type
)
3260 if (!_cups_strcasecmp(key
, "air") &&
3261 !_cups_strcasecmp(value
, "t"))
3262 type
|= CUPS_PRINTER_AUTHENTICATED
;
3263 else if (!_cups_strcasecmp(key
, "bind") &&
3264 !_cups_strcasecmp(value
, "t"))
3265 type
|= CUPS_PRINTER_BIND
;
3266 else if (!_cups_strcasecmp(key
, "collate") &&
3267 !_cups_strcasecmp(value
, "t"))
3268 type
|= CUPS_PRINTER_COLLATE
;
3269 else if (!_cups_strcasecmp(key
, "color") &&
3270 !_cups_strcasecmp(value
, "t"))
3271 type
|= CUPS_PRINTER_COLOR
;
3272 else if (!_cups_strcasecmp(key
, "copies") &&
3273 !_cups_strcasecmp(value
, "t"))
3274 type
|= CUPS_PRINTER_COPIES
;
3275 else if (!_cups_strcasecmp(key
, "duplex") &&
3276 !_cups_strcasecmp(value
, "t"))
3277 type
|= CUPS_PRINTER_DUPLEX
;
3278 else if (!_cups_strcasecmp(key
, "fax") &&
3279 !_cups_strcasecmp(value
, "t"))
3280 type
|= CUPS_PRINTER_MFP
;
3281 else if (!_cups_strcasecmp(key
, "papercustom") &&
3282 !_cups_strcasecmp(value
, "t"))
3283 type
|= CUPS_PRINTER_VARIABLE
;
3284 else if (!_cups_strcasecmp(key
, "papermax"))
3286 if (!_cups_strcasecmp(value
, "legal-a4"))
3287 type
|= CUPS_PRINTER_SMALL
;
3288 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3289 type
|= CUPS_PRINTER_MEDIUM
;
3290 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3291 type
|= CUPS_PRINTER_LARGE
;
3293 else if (!_cups_strcasecmp(key
, "punch") &&
3294 !_cups_strcasecmp(value
, "t"))
3295 type
|= CUPS_PRINTER_PUNCH
;
3296 else if (!_cups_strcasecmp(key
, "scan") &&
3297 !_cups_strcasecmp(value
, "t"))
3298 type
|= CUPS_PRINTER_MFP
;
3299 else if (!_cups_strcasecmp(key
, "sort") &&
3300 !_cups_strcasecmp(value
, "t"))
3301 type
|= CUPS_PRINTER_SORT
;
3302 else if (!_cups_strcasecmp(key
, "staple") &&
3303 !_cups_strcasecmp(value
, "t"))
3304 type
|= CUPS_PRINTER_STAPLE
;
3309 * Save the printer-xxx values...
3312 device
->dest
.num_options
= cupsAddOption("printer-info", name
,
3313 device
->dest
.num_options
,
3314 &device
->dest
.options
);
3316 if (make_and_model
[0])
3318 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3319 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3321 device
->dest
.num_options
= cupsAddOption("printer-make-and-model",
3323 device
->dest
.num_options
,
3324 &device
->dest
.options
);
3327 device
->dest
.num_options
= cupsAddOption("printer-make-and-model",
3329 device
->dest
.num_options
,
3330 &device
->dest
.options
);
3332 device
->type
= type
;
3333 snprintf(value
, sizeof(value
), "%u", type
);
3334 device
->dest
.num_options
= cupsAddOption("printer-type", value
,
3335 device
->dest
.num_options
,
3336 &device
->dest
.options
);
3342 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3343 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3344 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3345 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3347 DEBUG_printf(("6cups_dnssd_query: printer-uri-supported=\"%s\"", uri
));
3349 device
->dest
.num_options
= cupsAddOption("printer-uri-supported", uri
,
3350 device
->dest
.num_options
,
3351 &device
->dest
.options
);
3354 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3360 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3363 static const char * /* O - Resolved URI or NULL */
3365 cups_dest_t
*dest
, /* I - Destination */
3366 const char *uri
, /* I - Current printer URI */
3367 int msec
, /* I - Time in milliseconds */
3368 int *cancel
, /* I - Pointer to "cancel" variable */
3369 cups_dest_cb_t cb
, /* I - Callback */
3370 void *user_data
) /* I - User data for callback */
3372 char tempuri
[1024]; /* Temporary URI buffer */
3373 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3377 * Resolve the URI...
3380 resolve
.cancel
= cancel
;
3381 gettimeofday(&resolve
.end_time
, NULL
);
3384 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3385 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3387 while (resolve
.end_time
.tv_usec
>= 1000000)
3389 resolve
.end_time
.tv_sec
++;
3390 resolve
.end_time
.tv_usec
-= 1000000;
3394 resolve
.end_time
.tv_sec
+= 75;
3397 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
,
3400 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
),
3401 _HTTP_RESOLVE_FQDN
, cups_dnssd_resolve_cb
,
3404 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer URI."), 1);
3407 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
3414 * Save the resolved URI...
3417 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
,
3418 dest
->num_options
, &dest
->options
);
3420 return (cupsGetOption("printer-uri-supported", dest
->num_options
,
3426 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3429 static int /* O - 1 to continue, 0 to stop */
3430 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3432 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3434 struct timeval curtime
; /* Current time */
3438 * If the cancel variable is set, return immediately.
3441 if (*resolve
->cancel
)
3445 * Otherwise check the end time...
3448 gettimeofday(&curtime
, NULL
);
3450 return (curtime
.tv_sec
> resolve
->end_time
.tv_sec
||
3451 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3452 curtime
.tv_usec
> resolve
->end_time
.tv_usec
));
3457 * 'cups_dnssd_unquote()' - Unquote a name string.
3461 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3462 const char *src
, /* I - Source string */
3463 size_t dstsize
) /* I - Size of destination buffer */
3465 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3468 while (*src
&& dst
< dstend
)
3473 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3474 isdigit(src
[2] & 255))
3476 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3488 #endif /* HAVE_DNSSD */
3492 * 'cups_find_dest()' - Find a destination using a binary search.
3495 static int /* O - Index of match */
3496 cups_find_dest(const char *name
, /* I - Destination name */
3497 const char *instance
, /* I - Instance or NULL */
3498 int num_dests
, /* I - Number of destinations */
3499 cups_dest_t
*dests
, /* I - Destinations */
3500 int prev
, /* I - Previous index */
3501 int *rdiff
) /* O - Difference of match */
3503 int left
, /* Low mark for binary search */
3504 right
, /* High mark for binary search */
3505 current
, /* Current index */
3506 diff
; /* Result of comparison */
3507 cups_dest_t key
; /* Search key */
3510 key
.name
= (char *)name
;
3511 key
.instance
= (char *)instance
;
3516 * Start search on either side of previous...
3519 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3520 (diff
< 0 && prev
== 0) ||
3521 (diff
> 0 && prev
== (num_dests
- 1)))
3529 * Start with previous on right side...
3538 * Start wih previous on left side...
3542 right
= num_dests
- 1;
3548 * Start search in the middle...
3552 right
= num_dests
- 1;
3557 current
= (left
+ right
) / 2;
3558 diff
= cups_compare_dests(&key
, dests
+ current
);
3567 while ((right
- left
) > 1);
3572 * Check the last 1 or 2 elements...
3575 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3579 diff
= cups_compare_dests(&key
, dests
+ right
);
3585 * Return the closest destination and the difference...
3595 * 'cups_get_default()' - Get the default destination from an lpoptions file.
3598 static char * /* O - Default destination or NULL */
3599 cups_get_default(const char *filename
, /* I - File to read */
3600 char *namebuf
, /* I - Name buffer */
3601 size_t namesize
, /* I - Size of name buffer */
3602 const char **instance
) /* I - Instance */
3604 cups_file_t
*fp
; /* lpoptions file */
3605 char line
[8192], /* Line from file */
3606 *value
, /* Value for line */
3607 *nameptr
; /* Pointer into name */
3608 int linenum
; /* Current line */
3613 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
3617 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3619 if (!_cups_strcasecmp(line
, "default") && value
)
3621 strlcpy(namebuf
, value
, namesize
);
3623 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
3625 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
3628 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
3631 *instance
= nameptr
;
3639 return (*namebuf
? namebuf
: NULL
);
3644 * 'cups_get_dests()' - Get destinations from a file.
3647 static int /* O - Number of destinations */
3649 const char *filename
, /* I - File to read from */
3650 const char *match_name
, /* I - Destination name we want */
3651 const char *match_inst
, /* I - Instance name we want */
3652 int user_default_set
, /* I - User default printer set? */
3653 int num_dests
, /* I - Number of destinations */
3654 cups_dest_t
**dests
) /* IO - Destinations */
3656 int i
; /* Looping var */
3657 cups_dest_t
*dest
; /* Current destination */
3658 cups_file_t
*fp
; /* File pointer */
3659 char line
[8192], /* Line from file */
3660 *lineptr
, /* Pointer into line */
3661 *name
, /* Name of destination/option */
3662 *instance
; /* Instance of destination */
3663 int linenum
; /* Current line number */
3666 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", "
3667 "match_inst=\"%s\", user_default_set=%d, num_dests=%d, "
3668 "dests=%p)", filename
, match_name
, match_inst
,
3669 user_default_set
, num_dests
, dests
));
3672 * Try to open the file...
3675 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3679 * Read each printer; each line looks like:
3681 * Dest name[/instance] options
3682 * Default name[/instance] options
3687 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
3690 * See what type of line it is...
3693 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
3694 linenum
, line
, lineptr
));
3696 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
3698 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
3705 * Search for an instance...
3708 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
3711 if (*lineptr
== '/')
3714 * Found an instance...
3721 * Search for an instance...
3724 while (!isspace(*lineptr
& 255) && *lineptr
)
3733 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
3737 * See if the primary instance of the destination exists; if not,
3738 * ignore this entry and move on...
3743 if (_cups_strcasecmp(name
, match_name
) ||
3744 (!instance
&& match_inst
) ||
3745 (instance
&& !match_inst
) ||
3746 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
3751 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
3753 DEBUG_puts("9cups_get_dests: Not found!");
3759 * Add the destination...
3762 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
3764 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
3770 DEBUG_puts("9cups_get_dests: Out of memory!");
3776 * Add options until we hit the end of the line...
3779 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
3783 * If we found what we were looking for, stop now...
3790 * Set this as default if needed...
3793 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
3795 DEBUG_puts("9cups_get_dests: Setting as default...");
3797 for (i
= 0; i
< num_dests
; i
++)
3798 (*dests
)[i
].is_default
= 0;
3800 dest
->is_default
= 1;
3805 * Close the file and return...
3815 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
3819 static char * /* O - New string */
3821 ipp_attribute_t
*attr
, /* I - Attribute to convert */
3822 char *buffer
, /* I - Buffer */
3823 size_t bufsize
) /* I - Size of buffer */
3825 int i
; /* Looping var */
3826 char *ptr
, /* Pointer into buffer */
3827 *end
, /* Pointer to end of buffer */
3828 *valptr
; /* Pointer into string attribute */
3832 * Return quickly if we have a single string value...
3835 if (attr
->num_values
== 1 &&
3836 attr
->value_tag
!= IPP_TAG_INTEGER
&&
3837 attr
->value_tag
!= IPP_TAG_ENUM
&&
3838 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
3839 attr
->value_tag
!= IPP_TAG_RANGE
)
3840 return (attr
->values
[0].string
.text
);
3843 * Copy the values to the string, separating with commas and escaping strings
3847 end
= buffer
+ bufsize
- 1;
3849 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
3854 switch (attr
->value_tag
)
3856 case IPP_TAG_INTEGER
:
3858 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].integer
);
3861 case IPP_TAG_BOOLEAN
:
3862 if (attr
->values
[i
].boolean
)
3863 strlcpy(ptr
, "true", end
- ptr
+ 1);
3865 strlcpy(ptr
, "false", end
- ptr
+ 1);
3868 case IPP_TAG_RANGE
:
3869 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3870 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].range
.lower
);
3872 snprintf(ptr
, end
- ptr
+ 1, "%d-%d", attr
->values
[i
].range
.lower
,
3873 attr
->values
[i
].range
.upper
);
3877 for (valptr
= attr
->values
[i
].string
.text
;
3878 *valptr
&& ptr
< end
;)
3880 if (strchr(" \t\n\\\'\"", *valptr
))
3882 if (ptr
>= (end
- 1))