2 * User-defined destination (and option) support for CUPS.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
19 #endif /* HAVE_NOTIFY_H */
23 #endif /* HAVE_POLL */
27 #endif /* HAVE_DNSSD */
30 # include <avahi-client/client.h>
31 # include <avahi-client/lookup.h>
32 # include <avahi-common/simple-watch.h>
33 # include <avahi-common/domain.h>
34 # include <avahi-common/error.h>
35 # include <avahi-common/malloc.h>
36 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
37 #endif /* HAVE_AVAHI */
46 # include <SystemConfiguration/SystemConfiguration.h>
47 # define _CUPS_LOCATION_DEFAULTS 1
48 # endif /* !TARGET_OS_IOS */
49 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
50 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
51 # define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
52 # define kLocationNetworkKey CFSTR("Network")
53 # define kLocationPrinterIDKey CFSTR("PrinterID")
54 # define kUseLastPrinter CFSTR("UseLastPrinter")
55 #endif /* __APPLE__ */
57 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
58 # define _CUPS_DNSSD_GET_DESTS 250 /* Milliseconds for cupsGetDests */
59 # define _CUPS_DNSSD_MAXTIME 50 /* Milliseconds for maximum quantum of time */
61 # define _CUPS_DNSSD_GET_DESTS 0 /* Milliseconds for cupsGetDests */
62 #endif /* HAVE_DNSSD || HAVE_AVAHI */
69 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
70 typedef enum _cups_dnssd_state_e
/* Enumerated device state */
76 _CUPS_DNSSD_INCOMPATIBLE
,
78 } _cups_dnssd_state_t
;
80 typedef struct _cups_dnssd_data_s
/* Enumeration data */
83 DNSServiceRef main_ref
; /* Main service reference */
84 # else /* HAVE_AVAHI */
85 AvahiSimplePoll
*simple_poll
; /* Polling interface */
86 AvahiClient
*client
; /* Client information */
87 int got_data
; /* Did we get data? */
88 int browsers
; /* How many browsers are running? */
89 # endif /* HAVE_DNSSD */
90 cups_dest_cb_t cb
; /* Callback */
91 void *user_data
; /* User data pointer */
92 cups_ptype_t type
, /* Printer type filter */
93 mask
; /* Printer type mask */
94 cups_array_t
*devices
; /* Devices found so far */
95 int num_dests
; /* Number of lpoptions destinations */
96 cups_dest_t
*dests
; /* lpoptions destinations */
97 char def_name
[1024], /* Default printer name, if any */
98 *def_instance
; /* Default printer instance, if any */
101 typedef struct _cups_dnssd_device_s
/* Enumerated device */
103 _cups_dnssd_state_t state
; /* State of device listing */
105 DNSServiceRef ref
; /* Service reference for query */
106 # else /* HAVE_AVAHI */
107 AvahiRecordBrowser
*ref
; /* Browser for query */
108 # endif /* HAVE_DNSSD */
109 char *fullName
, /* Full name */
110 *regtype
, /* Registration type */
111 *domain
; /* Domain name */
112 cups_ptype_t type
; /* Device registration type */
113 cups_dest_t dest
; /* Destination record */
114 } _cups_dnssd_device_t
;
116 typedef struct _cups_dnssd_resolve_s
/* Data for resolving URI */
118 int *cancel
; /* Pointer to "cancel" variable */
119 struct timeval end_time
; /* Ending time */
120 } _cups_dnssd_resolve_t
;
121 #endif /* HAVE_DNSSD */
123 typedef struct _cups_getdata_s
125 int num_dests
; /* Number of destinations */
126 cups_dest_t
*dests
; /* Destinations */
127 char def_name
[1024], /* Default printer name, if any */
128 *def_instance
; /* Default printer instance, if any */
131 typedef struct _cups_namedata_s
133 const char *name
; /* Named destination */
134 cups_dest_t
*dest
; /* Destination */
142 #if _CUPS_LOCATION_DEFAULTS
143 static CFArrayRef
appleCopyLocations(void);
144 static CFStringRef
appleCopyNetwork(void);
145 #endif /* _CUPS_LOCATION_DEFAULTS */
147 static char *appleGetPaperSize(char *name
, size_t namesize
);
148 #endif /* __APPLE__ */
149 #if _CUPS_LOCATION_DEFAULTS
150 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
151 CFStringRef network
, CFIndex
*locindex
);
152 #endif /* _CUPS_LOCATION_DEFAULTS */
153 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
154 int *num_dests
, cups_dest_t
**dests
);
156 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
158 #endif /* __BLOCKS__ */
159 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
160 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
162 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
163 DNSServiceFlags flags
,
164 uint32_t interfaceIndex
,
165 DNSServiceErrorType errorCode
,
166 const char *serviceName
,
168 const char *replyDomain
,
170 # else /* HAVE_AVAHI */
171 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
172 AvahiIfIndex interface
,
173 AvahiProtocol protocol
,
174 AvahiBrowserEvent event
,
175 const char *serviceName
,
177 const char *replyDomain
,
178 AvahiLookupResultFlags flags
,
180 static void cups_dnssd_client_cb(AvahiClient
*client
,
181 AvahiClientState state
,
183 # endif /* HAVE_DNSSD */
184 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
185 _cups_dnssd_device_t
*b
);
186 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
187 _cups_dnssd_data_t
*data
);
188 static _cups_dnssd_device_t
*
189 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
190 const char *serviceName
,
192 const char *replyDomain
);
194 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
195 DNSServiceFlags flags
,
196 uint32_t interfaceIndex
,
197 DNSServiceErrorType errorCode
,
198 const char *fullName
,
199 uint16_t rrtype
, uint16_t rrclass
,
200 uint16_t rdlen
, const void *rdata
,
201 uint32_t ttl
, void *context
);
202 # else /* HAVE_AVAHI */
203 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
204 unsigned int num_pollfds
,
205 int timeout
, void *context
);
206 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
207 AvahiIfIndex interface
,
208 AvahiProtocol protocol
,
209 AvahiBrowserEvent event
,
210 const char *name
, uint16_t rrclass
,
211 uint16_t rrtype
, const void *rdata
,
213 AvahiLookupResultFlags flags
,
215 # endif /* HAVE_DNSSD */
216 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
217 int msec
, int *cancel
,
218 cups_dest_cb_t cb
, void *user_data
);
219 static int cups_dnssd_resolve_cb(void *context
);
220 static void cups_dnssd_unquote(char *dst
, const char *src
,
222 static int cups_elapsed(struct timeval
*t
);
223 #endif /* HAVE_DNSSD || HAVE_AVAHI */
224 static int cups_enum_dests(http_t
*http
, unsigned flags
, int msec
, int *cancel
, cups_ptype_t type
, cups_ptype_t mask
, cups_dest_cb_t cb
, void *user_data
);
225 static int cups_find_dest(const char *name
, const char *instance
,
226 int num_dests
, cups_dest_t
*dests
, int prev
,
228 static int cups_get_cb(_cups_getdata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
229 static char *cups_get_default(const char *filename
, char *namebuf
,
230 size_t namesize
, const char **instance
);
231 static int cups_get_dests(const char *filename
, const char *match_name
, const char *match_inst
, int load_all
, int user_default_set
, int num_dests
, cups_dest_t
**dests
);
232 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
234 static int cups_name_cb(_cups_namedata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
235 static void cups_queue_name(char *name
, const char *serviceName
, size_t namesize
);
239 * 'cupsAddDest()' - Add a destination to the list of destinations.
241 * This function cannot be used to add a new class or printer queue,
242 * it only adds a new container of saved options for the named
243 * destination or instance.
245 * If the named destination already exists, the destination list is
246 * returned unchanged. Adding a new instance of a destination creates
247 * a copy of that destination's options.
249 * Use the @link cupsSaveDests@ function to save the updated list of
250 * destinations to the user's lpoptions file.
253 int /* O - New number of destinations */
254 cupsAddDest(const char *name
, /* I - Destination name */
255 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
256 int num_dests
, /* I - Number of destinations */
257 cups_dest_t
**dests
) /* IO - Destinations */
259 int i
; /* Looping var */
260 cups_dest_t
*dest
; /* Destination pointer */
261 cups_dest_t
*parent
= NULL
; /* Parent destination */
262 cups_option_t
*doption
, /* Current destination option */
263 *poption
; /* Current parent option */
269 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
271 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
274 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
278 * Find the base dest again now the array has been realloc'd.
281 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
283 if (instance
&& parent
&& parent
->num_options
> 0)
286 * Copy options from parent...
289 dest
->options
= calloc(sizeof(cups_option_t
), (size_t)parent
->num_options
);
293 dest
->num_options
= parent
->num_options
;
295 for (i
= dest
->num_options
, doption
= dest
->options
,
296 poption
= parent
->options
;
298 i
--, doption
++, poption
++)
300 doption
->name
= _cupsStrRetain(poption
->name
);
301 doption
->value
= _cupsStrRetain(poption
->value
);
313 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
316 CFStringRef
/* O - Default paper ID */
317 _cupsAppleCopyDefaultPaperID(void)
319 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
320 kCUPSPrintingPrefs
));
325 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
328 CFStringRef
/* O - Default printer name */
329 _cupsAppleCopyDefaultPrinter(void)
331 # if _CUPS_LOCATION_DEFAULTS
332 CFStringRef network
; /* Network location */
333 CFArrayRef locations
; /* Location array */
334 CFStringRef locprinter
; /* Current printer */
338 * Use location-based defaults only if "use last printer" is selected in the
339 * system preferences...
342 if (!_cupsAppleGetUseLastPrinter())
344 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
350 * Get the current location...
353 if ((network
= appleCopyNetwork()) == NULL
)
355 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
361 * Lookup the network in the preferences...
364 if ((locations
= appleCopyLocations()) == NULL
)
367 * Missing or bad location array, so no location-based default...
370 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
378 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
379 (int)CFArrayGetCount(locations
)));
381 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
382 CFRetain(locprinter
);
385 CFRelease(locations
);
391 # endif /* _CUPS_LOCATION_DEFAULTS */
396 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
399 int /* O - 1 to use last printer, 0 otherwise */
400 _cupsAppleGetUseLastPrinter(void)
402 Boolean uselast
, /* Use last printer preference value */
403 uselast_set
; /* Valid is set? */
406 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
409 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
420 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
424 _cupsAppleSetDefaultPaperID(
425 CFStringRef name
) /* I - New paper ID */
427 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
428 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
430 # ifdef HAVE_NOTIFY_POST
431 notify_post("com.apple.printerPrefsChange");
432 # endif /* HAVE_NOTIFY_POST */
437 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
441 _cupsAppleSetDefaultPrinter(
442 CFStringRef name
) /* I - Default printer/class name */
444 # if _CUPS_LOCATION_DEFAULTS
445 CFStringRef network
; /* Current network */
446 CFArrayRef locations
; /* Old locations array */
447 CFIndex locindex
; /* Index in locations array */
448 CFStringRef locprinter
; /* Current printer */
449 CFMutableArrayRef newlocations
; /* New locations array */
450 CFMutableDictionaryRef newlocation
; /* New location */
454 * Get the current location...
457 if ((network
= appleCopyNetwork()) == NULL
)
459 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
464 * Lookup the network in the preferences...
467 if ((locations
= appleCopyLocations()) != NULL
)
468 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
475 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
478 * Need to change the locations array...
483 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
487 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
490 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
491 &kCFTypeArrayCallBacks
);
493 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
494 &kCFTypeDictionaryKeyCallBacks
,
495 &kCFTypeDictionaryValueCallBacks
);
497 if (newlocation
&& newlocations
)
500 * Put the new location at the front of the array...
503 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
504 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
505 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
508 * Limit the number of locations to 10...
511 while (CFArrayGetCount(newlocations
) > 10)
512 CFArrayRemoveValueAtIndex(newlocations
, 10);
515 * Push the changes out...
518 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
520 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
522 # ifdef HAVE_NOTIFY_POST
523 notify_post("com.apple.printerPrefsChange");
524 # endif /* HAVE_NOTIFY_POST */
528 CFRelease(newlocations
);
531 CFRelease(newlocation
);
535 CFRelease(locations
);
541 # endif /* _CUPS_LOCATION_DEFAULTS */
546 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
550 _cupsAppleSetUseLastPrinter(
551 int uselast
) /* O - 1 to use last printer, 0 otherwise */
553 CFPreferencesSetAppValue(kUseLastPrinter
,
554 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
556 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
558 # ifdef HAVE_NOTIFY_POST
559 notify_post("com.apple.printerPrefsChange");
560 # endif /* HAVE_NOTIFY_POST */
562 #endif /* __APPLE__ */
566 * 'cupsConnectDest()' - Open a conection to the destination.
568 * Connect to the destination, returning a new @code http_t@ connection object
569 * and optionally the resource path to use for the destination. These calls
570 * will block until a connection is made, the timeout expires, the integer
571 * pointed to by "cancel" is non-zero, or the callback function (or block)
572 * returns 0. The caller is responsible for calling @link httpClose@ on the
573 * returned connection.
575 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
576 * for the "flags" argument to connect directly to the device associated with
577 * the destination. Otherwise, the connection is made to the CUPS scheduler
578 * associated with the destination.
580 * @since CUPS 1.6/macOS 10.8@
583 http_t
* /* O - Connection to destination or @code NULL@ */
585 cups_dest_t
*dest
, /* I - Destination */
586 unsigned flags
, /* I - Connection flags */
587 int msec
, /* I - Timeout in milliseconds */
588 int *cancel
, /* I - Pointer to "cancel" variable */
589 char *resource
, /* I - Resource buffer */
590 size_t resourcesize
, /* I - Size of resource buffer */
591 cups_dest_cb_t cb
, /* I - Callback function */
592 void *user_data
) /* I - User data pointer */
594 const char *uri
; /* Printer URI */
595 char scheme
[32], /* URI scheme */
596 userpass
[256], /* Username and password (unused) */
597 hostname
[256], /* Hostname */
598 tempresource
[1024]; /* Temporary resource buffer */
599 int port
; /* Port number */
600 char portstr
[16]; /* Port number string */
601 http_encryption_t encryption
; /* Encryption to use */
602 http_addrlist_t
*addrlist
; /* Address list for server */
603 http_t
*http
; /* Connection to server */
606 DEBUG_printf(("cupsConnectDest(dest=%p, flags=0x%x, msec=%d, cancel=%p(%d), resource=\"%s\", resourcesize=" CUPS_LLFMT
", cb=%p, user_data=%p)", (void *)dest
, flags
, msec
, (void *)cancel
, cancel
? *cancel
: -1, resource
, CUPS_LLCAST resourcesize
, (void *)cb
, user_data
));
609 * Range check input...
617 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
621 if (!resource
|| resourcesize
< 1)
623 resource
= tempresource
;
624 resourcesize
= sizeof(tempresource
);
628 * Grab the printer URI...
631 if (flags
& CUPS_DEST_FLAGS_DEVICE
)
633 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
635 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
636 if (strstr(uri
, "._tcp"))
637 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
638 #endif /* HAVE_DNSSD || HAVE_AVAHI */
641 else if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
643 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
645 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
646 if (strstr(uri
, "._tcp"))
647 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
648 #endif /* HAVE_DNSSD || HAVE_AVAHI */
652 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
656 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
658 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
664 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
667 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
672 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
673 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
674 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
676 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
679 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
686 * Lookup the address for the server...
690 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
692 snprintf(portstr
, sizeof(portstr
), "%d", port
);
694 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
697 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
702 if (cancel
&& *cancel
)
704 httpAddrFreeList(addrlist
);
707 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
713 * Create the HTTP object pointing to the server referenced by the URI...
716 if (!strcmp(scheme
, "ipps") || port
== 443)
717 encryption
= HTTP_ENCRYPTION_ALWAYS
;
719 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
721 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
722 httpAddrFreeList(addrlist
);
725 * Connect if requested...
728 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
731 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
736 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
738 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
740 if (cancel
&& *cancel
)
741 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
743 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
746 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
755 * 'cupsConnectDestBlock()' - Open a connection to the destination.
757 * Connect to the destination, returning a new @code http_t@ connection object
758 * and optionally the resource path to use for the destination. These calls
759 * will block until a connection is made, the timeout expires, the integer
760 * pointed to by "cancel" is non-zero, or the block returns 0. The caller is
761 * responsible for calling @link httpClose@ on the returned connection.
763 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
764 * for the "flags" argument to connect directly to the device associated with
765 * the destination. Otherwise, the connection is made to the CUPS scheduler
766 * associated with the destination.
768 * @since CUPS 1.6/macOS 10.8@ @exclude all@
771 http_t
* /* O - Connection to destination or @code NULL@ */
772 cupsConnectDestBlock(
773 cups_dest_t
*dest
, /* I - Destination */
774 unsigned flags
, /* I - Connection flags */
775 int msec
, /* I - Timeout in milliseconds */
776 int *cancel
, /* I - Pointer to "cancel" variable */
777 char *resource
, /* I - Resource buffer */
778 size_t resourcesize
, /* I - Size of resource buffer */
779 cups_dest_block_t block
) /* I - Callback block */
781 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
782 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
784 #endif /* __BLOCKS__ */
788 * 'cupsCopyDest()' - Copy a destination.
790 * Make a copy of the destination to an array of destinations (or just a single
791 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
792 * for calling cupsFreeDests() on the returned object(s).
794 * @since CUPS 1.6/macOS 10.8@
797 int /* O - New number of destinations */
798 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
799 int num_dests
, /* I - Number of destinations */
800 cups_dest_t
**dests
) /* IO - Destination array */
802 int i
; /* Looping var */
803 cups_dest_t
*new_dest
; /* New destination pointer */
804 cups_option_t
*new_option
, /* Current destination option */
805 *option
; /* Current parent option */
809 * Range check input...
812 if (!dest
|| num_dests
< 0 || !dests
)
816 * See if the destination already exists...
819 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
823 * Protect against copying destination to itself...
826 if (new_dest
== dest
)
830 * Otherwise, free the options...
833 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
835 new_dest
->num_options
= 0;
836 new_dest
->options
= NULL
;
839 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
843 new_dest
->is_default
= dest
->is_default
;
845 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
846 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
848 new_dest
->num_options
= dest
->num_options
;
850 for (i
= dest
->num_options
, option
= dest
->options
,
851 new_option
= new_dest
->options
;
853 i
--, option
++, new_option
++)
855 new_option
->name
= _cupsStrRetain(option
->name
);
856 new_option
->value
= _cupsStrRetain(option
->value
);
865 * '_cupsCreateDest()' - Create a local (temporary) queue.
868 char * /* O - Printer URI or @code NULL@ on error */
869 _cupsCreateDest(const char *name
, /* I - Printer name */
870 const char *info
, /* I - Printer description of @code NULL@ */
871 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
872 const char *device_uri
, /* I - Device URI */
873 char *uri
, /* I - Printer URI buffer */
874 size_t urisize
) /* I - Size of URI buffer */
876 http_t
*http
; /* Connection to server */
877 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
878 *response
; /* CUPS-Create-Local-Printer response */
879 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
880 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
881 /* printer-state value */
884 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
887 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
890 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
892 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
893 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
895 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
896 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
898 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
900 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
902 response
= cupsDoRequest(http
, request
, "/");
904 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
905 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
913 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
914 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
916 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
921 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
923 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
924 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
925 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
927 response
= cupsDoRequest(http
, request
, "/");
929 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
930 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
942 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
944 * Destinations are enumerated from one or more sources. The callback function
945 * receives the @code user_data@ pointer and the destination pointer which can
946 * be used as input to the @link cupsCopyDest@ function. The function must
947 * return 1 to continue enumeration or 0 to stop.
949 * The @code type@ and @code mask@ arguments allow the caller to filter the
950 * destinations that are enumerated. Passing 0 for both will enumerate all
951 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
952 * destinations that are available but have not yet been added locally.
954 * Enumeration happens on the current thread and does not return until all
955 * destinations have been enumerated or the callback function returns 0.
957 * Note: The callback function will likely receive multiple updates for the same
958 * destinations - it is up to the caller to suppress any duplicate destinations.
960 * @since CUPS 1.6/macOS 10.8@
963 int /* O - 1 on success, 0 on failure */
965 unsigned flags
, /* I - Enumeration flags */
966 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
967 int *cancel
, /* I - Pointer to "cancel" variable */
968 cups_ptype_t type
, /* I - Printer type bits */
969 cups_ptype_t mask
, /* I - Mask for printer type bits */
970 cups_dest_cb_t cb
, /* I - Callback function */
971 void *user_data
) /* I - User data */
973 return (cups_enum_dests(CUPS_HTTP_DEFAULT
, flags
, msec
, cancel
, type
, mask
, cb
, user_data
));
979 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
981 * Destinations are enumerated from one or more sources. The block receives the
982 * @code user_data@ pointer and the destination pointer which can be used as
983 * input to the @link cupsCopyDest@ function. The block must return 1 to
984 * continue enumeration or 0 to stop.
986 * The @code type@ and @code mask@ arguments allow the caller to filter the
987 * destinations that are enumerated. Passing 0 for both will enumerate all
988 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
989 * destinations that are available but have not yet been added locally.
991 * Enumeration happens on the current thread and does not return until all
992 * destinations have been enumerated or the block returns 0.
994 * Note: The block will likely receive multiple updates for the same
995 * destinations - it is up to the caller to suppress any duplicate destinations.
997 * @since CUPS 1.6/macOS 10.8@ @exclude all@
1000 int /* O - 1 on success, 0 on failure */
1002 unsigned flags
, /* I - Enumeration flags */
1003 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1004 int *cancel
, /* I - Pointer to "cancel" variable */
1005 cups_ptype_t type
, /* I - Printer type bits */
1006 cups_ptype_t mask
, /* I - Mask for printer type bits */
1007 cups_dest_block_t block
) /* I - Block */
1009 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1010 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1012 # endif /* __BLOCKS__ */
1016 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1020 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1021 cups_dest_t
*dests
) /* I - Destinations */
1023 int i
; /* Looping var */
1024 cups_dest_t
*dest
; /* Current destination */
1027 if (num_dests
== 0 || dests
== NULL
)
1030 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1032 _cupsStrFree(dest
->name
);
1033 _cupsStrFree(dest
->instance
);
1035 cupsFreeOptions(dest
->num_options
, dest
->options
);
1043 * 'cupsGetDest()' - Get the named destination from the list.
1045 * Use the @link cupsEnumDests@ or @link cupsGetDests2@ functions to get a
1046 * list of supported destinations for the current user.
1049 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1050 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1051 const char *instance
, /* I - Instance name or @code NULL@ */
1052 int num_dests
, /* I - Number of destinations */
1053 cups_dest_t
*dests
) /* I - Destinations */
1055 int diff
, /* Result of comparison */
1056 match
; /* Matching index */
1059 if (num_dests
<= 0 || !dests
)
1065 * NULL name for default printer.
1068 while (num_dests
> 0)
1070 if (dests
->is_default
)
1080 * Lookup name and optionally the instance...
1083 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1086 return (dests
+ match
);
1094 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1097 const char * /* O - Printer URI */
1098 _cupsGetDestResource(
1099 cups_dest_t
*dest
, /* I - Destination */
1100 char *resource
, /* I - Resource buffer */
1101 size_t resourcesize
) /* I - Size of resource buffer */
1103 const char *uri
; /* Printer URI */
1104 char scheme
[32], /* URI scheme */
1105 userpass
[256], /* Username and password (unused) */
1106 hostname
[256]; /* Hostname */
1107 int port
; /* Port number */
1110 DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", (void *)dest
, dest
->name
, (void *)resource
, (int)resourcesize
));
1113 * Range check input...
1116 if (!dest
|| !resource
|| resourcesize
< 1)
1121 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1126 * Grab the printer URI...
1129 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
1131 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
1133 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1134 if (strstr(uri
, "._tcp"))
1135 uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
);
1136 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1141 DEBUG_printf(("1_cupsGetDestResource: Resolved printer-uri-supported=\"%s\"", uri
));
1143 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, resource
, resourcesize
);
1148 DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri
));
1150 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
1152 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1156 DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported found.");
1161 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1168 DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri
));
1170 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1171 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1172 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1174 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1180 DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource
));
1187 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1189 * "name" is the desired name for the printer. If @code NULL@, a name will be
1190 * created using the URI.
1192 * "uri" is the "ipp" or "ipps" URI for the printer.
1194 * @since CUPS 2.0/macOS 10.10@
1197 cups_dest_t
* /* O - Destination or @code NULL@ */
1198 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1199 const char *uri
) /* I - URI for the printer */
1201 cups_dest_t
*dest
; /* New destination */
1202 char temp
[1024], /* Temporary string */
1203 scheme
[256], /* Scheme from URI */
1204 userpass
[256], /* Username:password from URI */
1205 hostname
[256], /* Hostname from URI */
1206 resource
[1024], /* Resource path from URI */
1207 *ptr
; /* Pointer into string */
1208 const char *info
; /* printer-info string */
1209 int port
; /* Port number from URI */
1213 * Range check input...
1218 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1222 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1223 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1225 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1237 * Create the name from the URI...
1240 if (strstr(hostname
, "._tcp"))
1243 * Use the service instance name...
1246 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1249 cups_queue_name(temp
, hostname
, sizeof(temp
));
1253 else if (!strncmp(resource
, "/classes/", 9))
1255 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1256 name
= resource
+ 9;
1259 else if (!strncmp(resource
, "/printers/", 10))
1261 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1262 name
= resource
+ 10;
1273 * Create the destination...
1276 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1278 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1282 dest
->name
= _cupsStrAlloc(name
);
1283 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1284 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1291 * '_cupsGetDests()' - Get destinations from a server.
1293 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1294 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1297 * "name" is the name of an existing printer and is only used when "op" is
1298 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1300 * "dest" is initialized to point to the array of destinations.
1302 * 0 is returned if there are no printers, no default printer, or the named
1303 * printer does not exist, respectively.
1305 * Free the memory used by the destination array using the @link cupsFreeDests@
1308 * Note: On macOS this function also gets the default paper from the system
1309 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1310 * options array for each destination that supports it.
1313 int /* O - Number of destinations */
1314 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1315 * @code CUPS_HTTP_DEFAULT@ */
1316 ipp_op_t op
, /* I - IPP operation */
1317 const char *name
, /* I - Name of destination */
1318 cups_dest_t
**dests
, /* IO - Destinations */
1319 cups_ptype_t type
, /* I - Printer type bits */
1320 cups_ptype_t mask
) /* I - Printer type mask */
1322 int num_dests
= 0; /* Number of destinations */
1323 cups_dest_t
*dest
; /* Current destination */
1324 ipp_t
*request
, /* IPP Request */
1325 *response
; /* IPP Response */
1326 ipp_attribute_t
*attr
; /* Current attribute */
1327 const char *printer_name
; /* printer-name attribute */
1328 char uri
[1024]; /* printer-uri value */
1329 int num_options
; /* Number of options */
1330 cups_option_t
*options
; /* Options */
1332 char media_default
[41]; /* Default paper size */
1333 #endif /* __APPLE__ */
1334 char optname
[1024], /* Option name */
1335 value
[2048], /* Option value */
1336 *ptr
; /* Pointer into name/value */
1337 static const char * const pattrs
[] = /* Attributes we're interested in */
1339 "auth-info-required",
1341 "job-sheets-default",
1342 "marker-change-time",
1344 "marker-high-levels",
1346 "marker-low-levels",
1352 #endif /* __APPLE__ */
1356 "printer-is-accepting-jobs",
1357 "printer-is-shared",
1358 "printer-is-temporary",
1360 "printer-make-and-model",
1361 "printer-mandatory-job-attributes",
1364 "printer-state-change-time",
1365 "printer-state-reasons",
1367 "printer-uri-supported"
1371 DEBUG_printf(("_cupsGetDests(http=%p, op=%x(%s), name=\"%s\", dests=%p, type=%x, mask=%x)", (void *)http
, op
, ippOpString(op
), name
, (void *)dests
, type
, mask
));
1375 * Get the default paper size...
1378 appleGetPaperSize(media_default
, sizeof(media_default
));
1379 DEBUG_printf(("1_cupsGetDests: Default media is '%s'.", media_default
));
1380 #endif /* __APPLE__ */
1383 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1384 * require the following attributes:
1386 * attributes-charset
1387 * attributes-natural-language
1388 * requesting-user-name
1389 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1392 request
= ippNewRequest(op
);
1394 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1395 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1398 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1399 "requesting-user-name", NULL
, cupsUser());
1401 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1403 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1404 "localhost", ippPort(), "/printers/%s", name
);
1405 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1410 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1411 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1415 * Do the request and get back a response...
1418 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1420 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1423 * Skip leading attributes until we hit a printer...
1426 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1433 * Pull the needed attributes from this printer...
1436 printer_name
= NULL
;
1440 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1442 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1443 attr
->value_tag
!= IPP_TAG_ENUM
&&
1444 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1445 attr
->value_tag
!= IPP_TAG_TEXT
&&
1446 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1447 attr
->value_tag
!= IPP_TAG_NAME
&&
1448 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1449 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1450 attr
->value_tag
!= IPP_TAG_RANGE
&&
1451 attr
->value_tag
!= IPP_TAG_URI
)
1454 if (!strcmp(attr
->name
, "auth-info-required") ||
1455 !strcmp(attr
->name
, "device-uri") ||
1456 !strcmp(attr
->name
, "marker-change-time") ||
1457 !strcmp(attr
->name
, "marker-colors") ||
1458 !strcmp(attr
->name
, "marker-high-levels") ||
1459 !strcmp(attr
->name
, "marker-levels") ||
1460 !strcmp(attr
->name
, "marker-low-levels") ||
1461 !strcmp(attr
->name
, "marker-message") ||
1462 !strcmp(attr
->name
, "marker-names") ||
1463 !strcmp(attr
->name
, "marker-types") ||
1464 !strcmp(attr
->name
, "printer-commands") ||
1465 !strcmp(attr
->name
, "printer-info") ||
1466 !strcmp(attr
->name
, "printer-is-shared") ||
1467 !strcmp(attr
->name
, "printer-is-temporary") ||
1468 !strcmp(attr
->name
, "printer-make-and-model") ||
1469 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1470 !strcmp(attr
->name
, "printer-state") ||
1471 !strcmp(attr
->name
, "printer-state-change-time") ||
1472 !strcmp(attr
->name
, "printer-type") ||
1473 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1474 !strcmp(attr
->name
, "printer-location") ||
1475 !strcmp(attr
->name
, "printer-state-reasons") ||
1476 !strcmp(attr
->name
, "printer-uri-supported"))
1479 * Add a printer description attribute...
1482 num_options
= cupsAddOption(attr
->name
,
1483 cups_make_string(attr
, value
,
1485 num_options
, &options
);
1488 else if (!strcmp(attr
->name
, "media-supported") && media_default
[0])
1491 * See if we can set a default media size...
1494 int i
; /* Looping var */
1496 for (i
= 0; i
< attr
->num_values
; i
++)
1497 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1499 DEBUG_printf(("1_cupsGetDests: Setting media to '%s'.", media_default
));
1500 num_options
= cupsAddOption("media", media_default
, num_options
, &options
);
1504 #endif /* __APPLE__ */
1505 else if (!strcmp(attr
->name
, "printer-name") &&
1506 attr
->value_tag
== IPP_TAG_NAME
)
1507 printer_name
= attr
->values
[0].string
.text
;
1508 else if (strncmp(attr
->name
, "notify-", 7) &&
1509 strncmp(attr
->name
, "print-quality-", 14) &&
1510 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1511 attr
->value_tag
== IPP_TAG_ENUM
||
1512 attr
->value_tag
== IPP_TAG_INTEGER
||
1513 attr
->value_tag
== IPP_TAG_KEYWORD
||
1514 attr
->value_tag
== IPP_TAG_NAME
||
1515 attr
->value_tag
== IPP_TAG_RANGE
) &&
1516 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1519 * Add a default option...
1522 strlcpy(optname
, attr
->name
, sizeof(optname
));
1523 optname
[ptr
- attr
->name
] = '\0';
1525 if (_cups_strcasecmp(optname
, "media") || !cupsGetOption("media", num_options
, options
))
1526 num_options
= cupsAddOption(optname
, cups_make_string(attr
, value
, sizeof(value
)), num_options
, &options
);
1531 * See if we have everything needed...
1536 cupsFreeOptions(num_options
, options
);
1544 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1546 dest
->num_options
= num_options
;
1547 dest
->options
= options
;
1550 cupsFreeOptions(num_options
, options
);
1556 ippDelete(response
);
1560 * Return the count...
1568 * 'cupsGetDests()' - Get the list of destinations from the default server.
1570 * Starting with CUPS 1.2, the returned list of destinations include the
1571 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1572 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1573 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1574 * attributes as options.
1576 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1577 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1578 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1580 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1581 * be used. The "printer-uri-supported" option will be present for those IPP
1582 * printers that have been recently used.
1584 * Use the @link cupsFreeDests@ function to free the destination list and
1585 * the @link cupsGetDest@ function to find a particular destination.
1590 int /* O - Number of destinations */
1591 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1593 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1598 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1600 * Starting with CUPS 1.2, the returned list of destinations include the
1601 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1602 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1603 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1604 * attributes as options.
1606 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1607 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1608 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1610 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1611 * be used. The "printer-uri-supported" option will be present for those IPP
1612 * printers that have been recently used.
1614 * Use the @link cupsFreeDests@ function to free the destination list and
1615 * the @link cupsGetDest@ function to find a particular destination.
1617 * @since CUPS 1.1.21/macOS 10.4@
1620 int /* O - Number of destinations */
1621 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1622 cups_dest_t
**dests
) /* O - Destinations */
1624 _cups_getdata_t data
; /* Enumeration data */
1627 DEBUG_printf(("cupsGetDests2(http=%p, dests=%p)", (void *)http
, (void *)dests
));
1630 * Range check the input...
1635 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
1636 DEBUG_puts("1cupsGetDests2: NULL dests pointer, returning 0.");
1641 * Connect to the server as needed...
1646 if ((http
= _cupsConnect()) == NULL
)
1655 * Grab the printers and classes...
1661 if (!httpAddrLocalhost(httpGetAddress(http
)))
1664 * When talking to a remote cupsd, just enumerate printers on the remote
1668 cups_enum_dests(http
, 0, _CUPS_DNSSD_GET_DESTS
, NULL
, 0, CUPS_PRINTER_DISCOVERED
, (cups_dest_cb_t
)cups_get_cb
, &data
);
1673 * When talking to a local cupsd, enumerate both local printers and ones we
1674 * can find on the network...
1677 cups_enum_dests(http
, 0, _CUPS_DNSSD_GET_DESTS
, NULL
, 0, 0, (cups_dest_cb_t
)cups_get_cb
, &data
);
1681 * Return the number of destinations...
1684 *dests
= data
.dests
;
1686 if (data
.num_dests
> 0)
1687 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
1689 DEBUG_printf(("1cupsGetDests2: Returning %d destinations.", data
.num_dests
));
1691 return (data
.num_dests
);
1696 * 'cupsGetNamedDest()' - Get options for the named destination.
1698 * This function is optimized for retrieving a single destination and should
1699 * be used instead of @link cupsGetDests2@ and @link cupsGetDest@ when you
1700 * either know the name of the destination or want to print to the default
1701 * destination. If @code NULL@ is returned, the destination does not exist or
1702 * there is no default destination.
1704 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
1705 * server will be used.
1707 * If "name" is @code NULL@, the default printer for the current user will be
1710 * The returned destination must be freed using @link cupsFreeDests@ with a
1711 * "num_dests" value of 1.
1713 * @since CUPS 1.4/macOS 10.6@
1716 cups_dest_t
* /* O - Destination or @code NULL@ */
1717 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1718 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1719 const char *instance
) /* I - Instance name or @code NULL@ */
1721 const char *dest_name
; /* Working destination name */
1722 cups_dest_t
*dest
; /* Destination */
1723 char filename
[1024], /* Path to lpoptions */
1724 defname
[256]; /* Default printer name */
1725 const char *home
= getenv("HOME"); /* Home directory */
1726 int set_as_default
= 0; /* Set returned destination as default */
1727 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
1728 /* IPP operation to get server ops */
1729 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1732 DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", (void *)http
, name
, instance
));
1735 * If "name" is NULL, find the default destination...
1743 dest_name
= _cupsUserDefault(defname
, sizeof(defname
));
1747 char *ptr
; /* Temporary pointer... */
1749 if ((ptr
= strchr(defname
, '/')) != NULL
)
1760 * No default in the environment, try the user's lpoptions files...
1763 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1765 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1774 * Still not there? Try the system lpoptions file...
1777 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1778 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1787 * No locally-set default destination, ask the server...
1790 op
= IPP_OP_CUPS_GET_DEFAULT
;
1793 DEBUG_puts("1cupsGetNamedDest: Asking server for default printer...");
1796 DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name
));
1800 * Get the printer's attributes...
1803 if (!_cupsGetDests(http
, op
, dest_name
, &dest
, 0, 0))
1807 _cups_namedata_t data
; /* Callback data */
1809 DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network...");
1814 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_name_cb
, &data
);
1823 switch (set_as_default
)
1828 case 1 : /* Set from env vars */
1829 if (getenv("LPDEST"))
1830 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("LPDEST environment variable names default destination that does not exist."), 1);
1831 else if (getenv("PRINTER"))
1832 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("PRINTER environment variable names default destination that does not exist."), 1);
1834 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("No default destination."), 1);
1837 case 2 : /* Set from ~/.cups/lpoptions */
1838 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("~/.cups/lpoptions file names default destination that does not exist."), 1);
1841 case 3 : /* Set from /etc/cups/lpoptions */
1842 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("/etc/cups/lpoptions file names default destination that does not exist."), 1);
1845 case 4 : /* Set from server */
1846 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("No default destination."), 1);
1854 DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", (void *)dest
));
1857 dest
->instance
= _cupsStrAlloc(instance
);
1860 dest
->is_default
= 1;
1863 * Then add local options...
1866 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1867 cups_get_dests(filename
, dest_name
, instance
, 0, 1, 1, &dest
);
1871 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1873 cups_get_dests(filename
, dest_name
, instance
, 0, 1, 1, &dest
);
1877 * Return the result...
1885 * 'cupsRemoveDest()' - Remove a destination from the destination list.
1887 * Removing a destination/instance does not delete the class or printer
1888 * queue, merely the lpoptions for that destination/instance. Use the
1889 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
1890 * options for the user.
1892 * @since CUPS 1.3/macOS 10.5@
1895 int /* O - New number of destinations */
1896 cupsRemoveDest(const char *name
, /* I - Destination name */
1897 const char *instance
, /* I - Instance name or @code NULL@ */
1898 int num_dests
, /* I - Number of destinations */
1899 cups_dest_t
**dests
) /* IO - Destinations */
1901 int i
; /* Index into destinations */
1902 cups_dest_t
*dest
; /* Pointer to destination */
1906 * Find the destination...
1909 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1916 _cupsStrFree(dest
->name
);
1917 _cupsStrFree(dest
->instance
);
1918 cupsFreeOptions(dest
->num_options
, dest
->options
);
1921 * Remove the destination from the array...
1926 i
= (int)(dest
- *dests
);
1929 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
1936 * 'cupsSetDefaultDest()' - Set the default destination.
1938 * @since CUPS 1.3/macOS 10.5@
1943 const char *name
, /* I - Destination name */
1944 const char *instance
, /* I - Instance name or @code NULL@ */
1945 int num_dests
, /* I - Number of destinations */
1946 cups_dest_t
*dests
) /* I - Destinations */
1948 int i
; /* Looping var */
1949 cups_dest_t
*dest
; /* Current destination */
1953 * Range check input...
1956 if (!name
|| num_dests
<= 0 || !dests
)
1960 * Loop through the array and set the "is_default" flag for the matching
1964 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1965 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
1966 ((!instance
&& !dest
->instance
) ||
1967 (instance
&& dest
->instance
&&
1968 !_cups_strcasecmp(instance
, dest
->instance
)));
1973 * 'cupsSetDests()' - Save the list of destinations for the default server.
1975 * This function saves the destinations to /etc/cups/lpoptions when run
1976 * as root and ~/.cups/lpoptions when run as a normal user.
1982 cupsSetDests(int num_dests
, /* I - Number of destinations */
1983 cups_dest_t
*dests
) /* I - Destinations */
1985 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
1990 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
1992 * This function saves the destinations to /etc/cups/lpoptions when run
1993 * as root and ~/.cups/lpoptions when run as a normal user.
1995 * @since CUPS 1.1.21/macOS 10.4@
1998 int /* O - 0 on success, -1 on error */
1999 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2000 int num_dests
, /* I - Number of destinations */
2001 cups_dest_t
*dests
) /* I - Destinations */
2003 int i
, j
; /* Looping vars */
2004 int wrote
; /* Wrote definition? */
2005 cups_dest_t
*dest
; /* Current destination */
2006 cups_option_t
*option
; /* Current option */
2007 _ipp_option_t
*match
; /* Matching attribute for option */
2008 FILE *fp
; /* File pointer */
2010 const char *home
; /* HOME environment variable */
2012 char filename
[1024]; /* lpoptions file */
2013 int num_temps
; /* Number of temporary destinations */
2014 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2015 *temp
; /* Current temporary dest */
2016 const char *val
; /* Value of temporary option */
2017 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2021 * Range check the input...
2024 if (!num_dests
|| !dests
)
2028 * Get the server destinations...
2031 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2033 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2035 cupsFreeDests(num_temps
, temps
);
2040 * Figure out which file to write to...
2043 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2049 * Point to user defaults...
2052 if ((home
= getenv("HOME")) != NULL
)
2055 * Create ~/.cups subdirectory...
2058 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2059 if (access(filename
, 0))
2060 mkdir(filename
, 0700);
2062 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2068 * Try to open the file...
2071 if ((fp
= fopen(filename
, "w")) == NULL
)
2073 cupsFreeDests(num_temps
, temps
);
2079 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2084 fchmod(fileno(fp
), 0644);
2088 * Write each printer; each line looks like:
2090 * Dest name[/instance] options
2091 * Default name[/instance] options
2094 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2095 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2097 if (dest
->is_default
)
2099 fprintf(fp
, "Default %s", dest
->name
);
2101 fprintf(fp
, "/%s", dest
->instance
);
2108 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2110 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2113 * See if this option is a printer attribute; if so, skip it...
2116 if ((match
= _ippFindOption(option
->name
)) != NULL
&& match
->group_tag
== IPP_TAG_PRINTER
)
2120 * See if the server options match these; if so, don't write 'em.
2123 if (temp
&& (val
= cupsGetOption(option
->name
, temp
->num_options
, temp
->options
)) != NULL
&& !_cups_strcasecmp(val
, option
->value
))
2127 * Options don't match, write to the file...
2132 fprintf(fp
, "Dest %s", dest
->name
);
2134 fprintf(fp
, "/%s", dest
->instance
);
2138 if (option
->value
[0])
2140 if (strchr(option
->value
, ' ') || strchr(option
->value
, '\\') || strchr(option
->value
, '\"') || strchr(option
->value
, '\''))
2143 * Quote the value...
2146 fprintf(fp
, " %s=\"", option
->name
);
2148 for (val
= option
->value
; *val
; val
++)
2150 if (strchr("\"\'\\", *val
))
2161 * Store the literal value...
2164 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2168 fprintf(fp
, " %s", option
->name
);
2176 * Free the temporary destinations and close the file...
2179 cupsFreeDests(num_temps
, temps
);
2185 * Set the default printer for this location - this allows command-line
2186 * and GUI applications to share the same default destination...
2189 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2191 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
, dest
->name
, kCFStringEncodingUTF8
);
2192 /* Default printer name */
2196 _cupsAppleSetDefaultPrinter(name
);
2200 #endif /* __APPLE__ */
2202 #ifdef HAVE_NOTIFY_POST
2204 * Send a notification so that macOS applications can know about the
2208 notify_post("com.apple.printerListChange");
2209 #endif /* HAVE_NOTIFY_POST */
2216 * '_cupsUserDefault()' - Get the user default printer from environment
2217 * variables and location information.
2220 char * /* O - Default printer or NULL */
2221 _cupsUserDefault(char *name
, /* I - Name buffer */
2222 size_t namesize
) /* I - Size of name buffer */
2224 const char *env
; /* LPDEST or PRINTER env variable */
2226 CFStringRef locprinter
; /* Last printer as this location */
2227 #endif /* __APPLE__ */
2230 if ((env
= getenv("LPDEST")) == NULL
)
2231 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2236 strlcpy(name
, env
, namesize
);
2242 * Use location-based defaults if "use last printer" is selected in the
2243 * system preferences...
2246 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2248 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2249 CFRelease(locprinter
);
2254 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2256 return (*name
? name
: NULL
);
2260 * No location-based defaults on this platform...
2265 #endif /* __APPLE__ */
2269 #if _CUPS_LOCATION_DEFAULTS
2271 * 'appleCopyLocations()' - Copy the location history array.
2274 static CFArrayRef
/* O - Location array or NULL */
2275 appleCopyLocations(void)
2277 CFArrayRef locations
; /* Location array */
2281 * Look up the location array in the preferences...
2284 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2285 kCUPSPrintingPrefs
)) == NULL
)
2288 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2290 CFRelease(locations
);
2299 * 'appleCopyNetwork()' - Get the network ID for the current location.
2302 static CFStringRef
/* O - Network ID */
2303 appleCopyNetwork(void)
2305 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2306 CFStringRef key
; /* Current network configuration key */
2307 CFDictionaryRef ip_dict
; /* Network configuration data */
2308 CFStringRef network
= NULL
; /* Current network ID */
2311 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2315 * First use the IPv6 router address, if available, since that will generally
2316 * be a globally-unique link-local address.
2319 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2320 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2322 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2324 if ((network
= CFDictionaryGetValue(ip_dict
,
2325 kSCPropNetIPv6Router
)) != NULL
)
2335 * If that doesn't work, try the IPv4 router address. This isn't as unique
2336 * and will likely be a 10.x.y.z or 192.168.y.z address...
2341 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2342 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2344 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2346 if ((network
= CFDictionaryGetValue(ip_dict
,
2347 kSCPropNetIPv4Router
)) != NULL
)
2357 CFRelease(dynamicStore
);
2362 #endif /* _CUPS_LOCATION_DEFAULTS */
2367 * 'appleGetPaperSize()' - Get the default paper size.
2370 static char * /* O - Default paper size */
2371 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2372 size_t namesize
) /* I - Size of buffer */
2374 CFStringRef defaultPaperID
; /* Default paper ID */
2375 pwg_media_t
*pwgmedia
; /* PWG media size */
2378 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2379 if (!defaultPaperID
||
2380 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2381 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2383 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2384 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2387 CFRelease(defaultPaperID
);
2391 #endif /* __APPLE__ */
2394 #if _CUPS_LOCATION_DEFAULTS
2396 * 'appleGetPrinter()' - Get a printer from the history array.
2399 static CFStringRef
/* O - Printer name or NULL */
2400 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2401 CFStringRef network
, /* I - Network name */
2402 CFIndex
*locindex
) /* O - Index in array */
2404 CFIndex i
, /* Looping var */
2405 count
; /* Number of locations */
2406 CFDictionaryRef location
; /* Current location */
2407 CFStringRef locnetwork
, /* Current network */
2408 locprinter
; /* Current printer */
2411 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2412 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2413 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2415 if ((locnetwork
= CFDictionaryGetValue(location
,
2416 kLocationNetworkKey
)) != NULL
&&
2417 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2418 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2419 (locprinter
= CFDictionaryGetValue(location
,
2420 kLocationPrinterIDKey
)) != NULL
&&
2421 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2426 return (locprinter
);
2432 #endif /* _CUPS_LOCATION_DEFAULTS */
2436 * 'cups_add_dest()' - Add a destination to the array.
2438 * Unlike cupsAddDest(), this function does not check for duplicates.
2441 static cups_dest_t
* /* O - New destination */
2442 cups_add_dest(const char *name
, /* I - Name of destination */
2443 const char *instance
, /* I - Instance or NULL */
2444 int *num_dests
, /* IO - Number of destinations */
2445 cups_dest_t
**dests
) /* IO - Destinations */
2447 int insert
, /* Insertion point */
2448 diff
; /* Result of comparison */
2449 cups_dest_t
*dest
; /* Destination pointer */
2453 * Add new destination...
2456 if (*num_dests
== 0)
2457 dest
= malloc(sizeof(cups_dest_t
));
2459 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2467 * Find where to insert the destination...
2470 if (*num_dests
== 0)
2474 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2482 * Move the array elements as needed...
2485 if (insert
< *num_dests
)
2486 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2491 * Initialize the destination...
2494 dest
= *dests
+ insert
;
2495 dest
->name
= _cupsStrAlloc(name
);
2496 dest
->instance
= _cupsStrAlloc(instance
);
2497 dest
->is_default
= 0;
2498 dest
->num_options
= 0;
2499 dest
->options
= (cups_option_t
*)0;
2507 * 'cups_block_cb()' - Enumeration callback for block API.
2510 static int /* O - 1 to continue, 0 to stop */
2512 cups_dest_block_t block
, /* I - Block */
2513 unsigned flags
, /* I - Destination flags */
2514 cups_dest_t
*dest
) /* I - Destination */
2516 return ((block
)(flags
, dest
));
2518 # endif /* __BLOCKS__ */
2522 * 'cups_compare_dests()' - Compare two destinations.
2525 static int /* O - Result of comparison */
2526 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2527 cups_dest_t
*b
) /* I - Second destination */
2529 int diff
; /* Difference */
2532 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2534 else if (a
->instance
&& b
->instance
)
2535 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2537 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2541 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2544 * 'cups_dnssd_browse_cb()' - Browse for printers.
2548 cups_dnssd_browse_cb(
2549 DNSServiceRef sdRef
, /* I - Service reference */
2550 DNSServiceFlags flags
, /* I - Option flags */
2551 uint32_t interfaceIndex
, /* I - Interface number */
2552 DNSServiceErrorType errorCode
, /* I - Error, if any */
2553 const char *serviceName
, /* I - Name of service/device */
2554 const char *regtype
, /* I - Type of service */
2555 const char *replyDomain
, /* I - Service domain */
2556 void *context
) /* I - Enumeration data */
2558 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2559 /* Enumeration data */
2562 DEBUG_printf(("5cups_dnssd_browse_cb(sdRef=%p, flags=%x, interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\", context=%p)", (void *)sdRef
, flags
, interfaceIndex
, errorCode
, serviceName
, regtype
, replyDomain
, context
));
2565 * Don't do anything on error...
2568 if (errorCode
!= kDNSServiceErr_NoError
)
2575 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2579 # else /* HAVE_AVAHI */
2581 * 'cups_dnssd_browse_cb()' - Browse for printers.
2585 cups_dnssd_browse_cb(
2586 AvahiServiceBrowser
*browser
, /* I - Browser */
2587 AvahiIfIndex interface
, /* I - Interface index (unused) */
2588 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2589 AvahiBrowserEvent event
, /* I - What happened */
2590 const char *name
, /* I - Service name */
2591 const char *type
, /* I - Registration type */
2592 const char *domain
, /* I - Domain */
2593 AvahiLookupResultFlags flags
, /* I - Flags */
2594 void *context
) /* I - Devices array */
2597 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
2598 /* Client information */
2600 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2601 /* Enumeration data */
2608 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
2612 case AVAHI_BROWSER_FAILURE
:
2613 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
2614 avahi_simple_poll_quit(data
->simple_poll
);
2617 case AVAHI_BROWSER_NEW
:
2619 * This object is new on the network.
2622 cups_dnssd_get_device(data
, name
, type
, domain
);
2625 case AVAHI_BROWSER_REMOVE
:
2626 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
2629 case AVAHI_BROWSER_ALL_FOR_NOW
:
2630 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
2638 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2642 cups_dnssd_client_cb(
2643 AvahiClient
*client
, /* I - Client information (unused) */
2644 AvahiClientState state
, /* I - Current state */
2645 void *context
) /* I - User data (unused) */
2647 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2648 /* Enumeration data */
2653 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
2656 * If the connection drops, quit.
2659 if (state
== AVAHI_CLIENT_FAILURE
)
2661 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2662 avahi_simple_poll_quit(data
->simple_poll
);
2665 # endif /* HAVE_DNSSD */
2669 * 'cups_dnssd_compare_device()' - Compare two devices.
2672 static int /* O - Result of comparison */
2673 cups_dnssd_compare_devices(
2674 _cups_dnssd_device_t
*a
, /* I - First device */
2675 _cups_dnssd_device_t
*b
) /* I - Second device */
2677 return (strcmp(a
->dest
.name
, b
->dest
.name
));
2682 * 'cups_dnssd_free_device()' - Free the memory used by a device.
2686 cups_dnssd_free_device(
2687 _cups_dnssd_device_t
*device
, /* I - Device */
2688 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
2690 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
2694 DNSServiceRefDeallocate(device
->ref
);
2695 # else /* HAVE_AVAHI */
2697 avahi_record_browser_free(device
->ref
);
2698 # endif /* HAVE_DNSSD */
2700 _cupsStrFree(device
->domain
);
2701 _cupsStrFree(device
->fullName
);
2702 _cupsStrFree(device
->regtype
);
2703 _cupsStrFree(device
->dest
.name
);
2705 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
2712 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
2715 static _cups_dnssd_device_t
* /* O - Device */
2716 cups_dnssd_get_device(
2717 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
2718 const char *serviceName
, /* I - Service name */
2719 const char *regtype
, /* I - Registration type */
2720 const char *replyDomain
) /* I - Domain name */
2722 _cups_dnssd_device_t key
, /* Search key */
2723 *device
; /* Device */
2724 char fullName
[kDNSServiceMaxDomainName
],
2725 /* Full name for query */
2726 name
[128]; /* Queue name */
2729 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
2732 * See if this is an existing device...
2735 cups_queue_name(name
, serviceName
, sizeof(name
));
2737 key
.dest
.name
= name
;
2739 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
2742 * Yes, see if we need to do anything with this...
2745 int update
= 0; /* Non-zero if we need to update */
2747 if (!_cups_strcasecmp(replyDomain
, "local.") &&
2748 _cups_strcasecmp(device
->domain
, replyDomain
))
2751 * Update the "global" listing to use the .local domain name instead.
2754 _cupsStrFree(device
->domain
);
2755 device
->domain
= _cupsStrAlloc(replyDomain
);
2757 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
2758 "domain.", device
->dest
.name
));
2763 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
2764 _cups_strcasecmp(device
->regtype
, regtype
))
2767 * Prefer IPPS over IPP.
2770 _cupsStrFree(device
->regtype
);
2771 device
->regtype
= _cupsStrAlloc(regtype
);
2773 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
2774 device
->dest
.name
));
2781 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
2782 device
->dest
.name
));
2789 * No, add the device...
2792 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
2793 "'%s'.", serviceName
,
2794 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
2797 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
2798 device
->dest
.name
= _cupsStrAlloc(name
);
2799 device
->domain
= _cupsStrAlloc(replyDomain
);
2800 device
->regtype
= _cupsStrAlloc(regtype
);
2802 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
2804 cupsArrayAdd(data
->devices
, device
);
2808 * Set the "full name" of this service, which is used for queries...
2812 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
2813 # else /* HAVE_AVAHI */
2814 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
2815 # endif /* HAVE_DNSSD */
2817 _cupsStrFree(device
->fullName
);
2818 device
->fullName
= _cupsStrAlloc(fullName
);
2823 DNSServiceRefDeallocate(device
->ref
);
2824 # else /* HAVE_AVAHI */
2825 avahi_record_browser_free(device
->ref
);
2826 # endif /* HAVE_DNSSD */
2831 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
2833 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
2835 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
2836 device
->state
= _CUPS_DNSSD_NEW
;
2845 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
2847 * Note: This function is needed because avahi_simple_poll_iterate is broken
2848 * and always uses a timeout of 0 (!) milliseconds.
2849 * (https://github.com/lathiat/avahi/issues/127)
2854 static int /* O - Number of file descriptors matching */
2856 struct pollfd
*pollfds
, /* I - File descriptors */
2857 unsigned int num_pollfds
, /* I - Number of file descriptors */
2858 int timeout
, /* I - Timeout in milliseconds (unused) */
2859 void *context
) /* I - User data (unused) */
2861 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2862 /* Enumeration data */
2863 int val
; /* Return value */
2866 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
2870 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
2872 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
2876 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
2885 # endif /* HAVE_AVAHI */
2889 * 'cups_dnssd_query_cb()' - Process query data.
2893 cups_dnssd_query_cb(
2895 DNSServiceRef sdRef
, /* I - Service reference */
2896 DNSServiceFlags flags
, /* I - Data flags */
2897 uint32_t interfaceIndex
, /* I - Interface */
2898 DNSServiceErrorType errorCode
, /* I - Error, if any */
2899 const char *fullName
, /* I - Full service name */
2900 uint16_t rrtype
, /* I - Record type */
2901 uint16_t rrclass
, /* I - Record class */
2902 uint16_t rdlen
, /* I - Length of record data */
2903 const void *rdata
, /* I - Record data */
2904 uint32_t ttl
, /* I - Time-to-live */
2905 # else /* HAVE_AVAHI */
2906 AvahiRecordBrowser
*browser
, /* I - Record browser */
2907 AvahiIfIndex interfaceIndex
,
2908 /* I - Interface index (unused) */
2909 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2910 AvahiBrowserEvent event
, /* I - What happened? */
2911 const char *fullName
, /* I - Service name */
2912 uint16_t rrclass
, /* I - Record class */
2913 uint16_t rrtype
, /* I - Record type */
2914 const void *rdata
, /* I - TXT record */
2915 size_t rdlen
, /* I - Length of TXT record */
2916 AvahiLookupResultFlags flags
, /* I - Flags */
2917 # endif /* HAVE_DNSSD */
2918 void *context
) /* I - Enumeration data */
2920 # if defined(DEBUG) && defined(HAVE_AVAHI)
2921 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
2922 /* Client information */
2923 # endif /* DEBUG && HAVE_AVAHI */
2924 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2925 /* Enumeration data */
2926 char serviceName
[256],/* Service name */
2927 name
[128], /* Queue name */
2928 *ptr
; /* Pointer into string */
2929 _cups_dnssd_device_t dkey
, /* Search key */
2930 *device
; /* Device */
2934 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, context=%p)", (void *)sdRef
, flags
, interfaceIndex
, errorCode
, fullName
, rrtype
, rrclass
, rdlen
, rdata
, ttl
, context
));
2937 * Only process "add" data...
2940 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
2943 # else /* HAVE_AVAHI */
2944 DEBUG_printf(("cups_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
));
2947 * Only process "add" data...
2950 if (event
!= AVAHI_BROWSER_NEW
)
2952 if (event
== AVAHI_BROWSER_FAILURE
)
2953 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
2957 # endif /* HAVE_DNSSD */
2960 * Lookup the service in the devices array.
2963 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
2965 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
2968 cups_queue_name(name
, serviceName
, sizeof(name
));
2970 dkey
.dest
.name
= name
;
2972 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
2975 * Found it, pull out the make and model from the TXT record and save it...
2978 const uint8_t *txt
, /* Pointer into data */
2979 *txtnext
, /* Next key/value pair */
2980 *txtend
; /* End of entire TXT record */
2981 uint8_t txtlen
; /* Length of current key/value pair */
2982 char key
[256], /* Key string */
2983 value
[256], /* Value string */
2984 make_and_model
[512],
2985 /* Manufacturer and model */
2986 model
[256], /* Model */
2987 uriname
[1024], /* Name for URI */
2988 uri
[1024]; /* Printer URI */
2989 cups_ptype_t type
= CUPS_PRINTER_DISCOVERED
| CUPS_PRINTER_BW
;
2991 int saw_printer_type
= 0;
2992 /* Did we see a printer-type key? */
2994 device
->state
= _CUPS_DNSSD_PENDING
;
2995 make_and_model
[0] = '\0';
2997 strlcpy(model
, "Unknown", sizeof(model
));
2999 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3004 * Read a key/value pair starting with an 8-bit length. Since the
3005 * length is 8 bits and the size of the key/value buffers is 256, we
3006 * don't need to check for overflow...
3011 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3014 txtnext
= txt
+ txtlen
;
3016 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3017 *ptr
++ = (char)*txt
;
3020 if (txt
< txtnext
&& *txt
== '=')
3025 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3026 value
[txtnext
- txt
] = '\0';
3028 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3032 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3036 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3037 !_cups_strcasecmp(key
, "usb_MANU") ||
3038 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3039 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3040 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3041 !_cups_strcasecmp(key
, "usb_MODEL"))
3042 strlcpy(model
, value
, sizeof(model
));
3043 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3045 if (value
[0] == '(')
3048 * Strip parenthesis...
3051 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3054 strlcpy(model
, value
+ 1, sizeof(model
));
3057 strlcpy(model
, value
, sizeof(model
));
3059 else if (!_cups_strcasecmp(key
, "ty"))
3061 strlcpy(model
, value
, sizeof(model
));
3063 if ((ptr
= strchr(model
, ',')) != NULL
)
3066 else if (!_cups_strcasecmp(key
, "note"))
3067 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3068 device
->dest
.num_options
,
3069 &device
->dest
.options
);
3070 else if (!_cups_strcasecmp(key
, "pdl"))
3073 * Look for PDF-capable printers; only PDF-capable printers are shown.
3076 const char *start
, *next
; /* Pointer into value */
3077 int have_pdf
= 0, /* Have PDF? */
3078 have_raster
= 0;/* Have raster format support? */
3080 for (start
= value
; start
&& *start
; start
= next
)
3082 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3087 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3088 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3094 if ((next
= strchr(start
, ',')) != NULL
)
3098 if (!have_pdf
&& !have_raster
)
3099 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3101 else if (!_cups_strcasecmp(key
, "printer-type"))
3104 * Value is either NNNN or 0xXXXX
3107 saw_printer_type
= 1;
3108 type
= (cups_ptype_t
)strtol(value
, NULL
, 0) | CUPS_PRINTER_DISCOVERED
;
3110 else if (!saw_printer_type
)
3112 if (!_cups_strcasecmp(key
, "air") &&
3113 !_cups_strcasecmp(value
, "t"))
3114 type
|= CUPS_PRINTER_AUTHENTICATED
;
3115 else if (!_cups_strcasecmp(key
, "bind") &&
3116 !_cups_strcasecmp(value
, "t"))
3117 type
|= CUPS_PRINTER_BIND
;
3118 else if (!_cups_strcasecmp(key
, "collate") &&
3119 !_cups_strcasecmp(value
, "t"))
3120 type
|= CUPS_PRINTER_COLLATE
;
3121 else if (!_cups_strcasecmp(key
, "color") &&
3122 !_cups_strcasecmp(value
, "t"))
3123 type
|= CUPS_PRINTER_COLOR
;
3124 else if (!_cups_strcasecmp(key
, "copies") &&
3125 !_cups_strcasecmp(value
, "t"))
3126 type
|= CUPS_PRINTER_COPIES
;
3127 else if (!_cups_strcasecmp(key
, "duplex") &&
3128 !_cups_strcasecmp(value
, "t"))
3129 type
|= CUPS_PRINTER_DUPLEX
;
3130 else if (!_cups_strcasecmp(key
, "fax") &&
3131 !_cups_strcasecmp(value
, "t"))
3132 type
|= CUPS_PRINTER_MFP
;
3133 else if (!_cups_strcasecmp(key
, "papercustom") &&
3134 !_cups_strcasecmp(value
, "t"))
3135 type
|= CUPS_PRINTER_VARIABLE
;
3136 else if (!_cups_strcasecmp(key
, "papermax"))
3138 if (!_cups_strcasecmp(value
, "legal-a4"))
3139 type
|= CUPS_PRINTER_SMALL
;
3140 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3141 type
|= CUPS_PRINTER_MEDIUM
;
3142 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3143 type
|= CUPS_PRINTER_LARGE
;
3145 else if (!_cups_strcasecmp(key
, "punch") &&
3146 !_cups_strcasecmp(value
, "t"))
3147 type
|= CUPS_PRINTER_PUNCH
;
3148 else if (!_cups_strcasecmp(key
, "scan") &&
3149 !_cups_strcasecmp(value
, "t"))
3150 type
|= CUPS_PRINTER_MFP
;
3151 else if (!_cups_strcasecmp(key
, "sort") &&
3152 !_cups_strcasecmp(value
, "t"))
3153 type
|= CUPS_PRINTER_SORT
;
3154 else if (!_cups_strcasecmp(key
, "staple") &&
3155 !_cups_strcasecmp(value
, "t"))
3156 type
|= CUPS_PRINTER_STAPLE
;
3161 * Save the printer-xxx values...
3164 if (make_and_model
[0])
3166 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3167 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3169 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3172 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3174 device
->type
= type
;
3175 snprintf(value
, sizeof(value
), "%u", type
);
3176 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3182 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3183 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3184 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3185 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3187 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3189 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3192 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3198 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3201 static const char * /* O - Resolved URI or NULL */
3203 cups_dest_t
*dest
, /* I - Destination */
3204 const char *uri
, /* I - Current printer URI */
3205 int msec
, /* I - Time in milliseconds */
3206 int *cancel
, /* I - Pointer to "cancel" variable */
3207 cups_dest_cb_t cb
, /* I - Callback */
3208 void *user_data
) /* I - User data for callback */
3210 char tempuri
[1024]; /* Temporary URI buffer */
3211 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3215 * Resolve the URI...
3218 resolve
.cancel
= cancel
;
3219 gettimeofday(&resolve
.end_time
, NULL
);
3222 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3223 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3225 while (resolve
.end_time
.tv_usec
>= 1000000)
3227 resolve
.end_time
.tv_sec
++;
3228 resolve
.end_time
.tv_usec
-= 1000000;
3232 resolve
.end_time
.tv_sec
+= 75;
3235 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3237 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3239 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3242 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3248 * Save the resolved URI...
3251 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &dest
->options
);
3253 return (cupsGetOption("device-uri", dest
->num_options
, dest
->options
));
3258 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3261 static int /* O - 1 to continue, 0 to stop */
3262 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3264 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3266 struct timeval curtime
; /* Current time */
3270 * If the cancel variable is set, return immediately.
3273 if (resolve
->cancel
&& *(resolve
->cancel
))
3275 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3280 * Otherwise check the end time...
3283 gettimeofday(&curtime
, NULL
);
3285 DEBUG_printf(("4cups_dnssd_resolve_cb: curtime=%d.%06d, end_time=%d.%06d", (int)curtime
.tv_sec
, (int)curtime
.tv_usec
, (int)resolve
->end_time
.tv_sec
, (int)resolve
->end_time
.tv_usec
));
3287 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3288 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3289 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3294 * 'cups_dnssd_unquote()' - Unquote a name string.
3298 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3299 const char *src
, /* I - Source string */
3300 size_t dstsize
) /* I - Size of destination buffer */
3302 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3305 while (*src
&& dst
< dstend
)
3310 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3311 isdigit(src
[2] & 255))
3313 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3325 #endif /* HAVE_DNSSD */
3328 #if defined(HAVE_AVAHI) || defined(HAVE_DNSSD)
3330 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3333 static int /* O - Elapsed time in milliseconds */
3334 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3336 int msecs
; /* Milliseconds */
3337 struct timeval nt
; /* New time */
3340 gettimeofday(&nt
, NULL
);
3342 msecs
= (int)(1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000);
3348 #endif /* HAVE_AVAHI || HAVE_DNSSD */
3352 * 'cups_enum_dests()' - Enumerate destinations from a specific server.
3355 static int /* O - 1 on success, 0 on failure */
3357 http_t
*http
, /* I - Connection to scheduler */
3358 unsigned flags
, /* I - Enumeration flags */
3359 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
3360 int *cancel
, /* I - Pointer to "cancel" variable */
3361 cups_ptype_t type
, /* I - Printer type bits */
3362 cups_ptype_t mask
, /* I - Mask for printer type bits */
3363 cups_dest_cb_t cb
, /* I - Callback function */
3364 void *user_data
) /* I - User data */
3366 int i
, j
, /* Looping vars */
3367 num_dests
; /* Number of destinations */
3368 cups_dest_t
*dests
= NULL
, /* Destinations */
3369 *dest
, /* Current destination */
3370 *user_dest
; /* User destination */
3371 cups_option_t
*option
; /* Current option */
3372 char *user_default
; /* User default printer */
3373 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3374 int count
, /* Number of queries started */
3375 completed
, /* Number of completed queries */
3376 remaining
; /* Remainder of timeout */
3377 struct timeval curtime
; /* Current time */
3378 _cups_dnssd_data_t data
; /* Data for callback */
3379 _cups_dnssd_device_t
*device
; /* Current device */
3381 int nfds
, /* Number of files responded */
3382 main_fd
; /* File descriptor for lookups */
3383 DNSServiceRef ipp_ref
= NULL
; /* IPP browser */
3385 DNSServiceRef ipps_ref
= NULL
; /* IPPS browser */
3386 # endif /* HAVE_SSL */
3388 struct pollfd pfd
; /* Polling data */
3390 fd_set input
; /* Input set for select() */
3391 struct timeval timeout
; /* Timeout for select() */
3392 # endif /* HAVE_POLL */
3393 # else /* HAVE_AVAHI */
3394 int error
; /* Error value */
3395 AvahiServiceBrowser
*ipp_ref
= NULL
; /* IPP browser */
3397 AvahiServiceBrowser
*ipps_ref
= NULL
; /* IPPS browser */
3398 # endif /* HAVE_SSL */
3399 # endif /* HAVE_DNSSD */
3401 _cups_getdata_t data
; /* Data for callback */
3402 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3403 const char *home
; /* HOME environment variable */
3404 char filename
[1024]; /* Local lpoptions file */
3405 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
3408 DEBUG_printf(("cups_enum_dests(flags=%x, msec=%d, cancel=%p, type=%x, mask=%x, cb=%p, user_data=%p)", flags
, msec
, (void *)cancel
, type
, mask
, (void *)cb
, (void *)user_data
));
3411 * Range check input...
3418 DEBUG_puts("1cups_enum_dests: No callback, returning 0.");
3423 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
3426 memset(&data
, 0, sizeof(data
));
3428 if ((user_default
= _cupsUserDefault(data
.def_name
, sizeof(data
.def_name
))) == NULL
)
3430 const char *defprinter
= cupsGetDefault2(http
);
3431 /* Server default, if any */
3434 strlcpy(data
.def_name
, defprinter
, sizeof(data
.def_name
));
3437 if (data
.def_name
[0])
3440 * Separate printer and instance name...
3443 if ((data
.def_instance
= strchr(data
.def_name
, '/')) != NULL
)
3444 *data
.def_instance
++ = '\0';
3447 DEBUG_printf(("1cups_enum_dests: def_name=\"%s\", def_instance=\"%s\"", data
.def_name
, data
.def_instance
));
3449 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
3450 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, 1, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
3452 if ((home
= getenv("HOME")) != NULL
)
3454 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
3456 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, 1, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
3460 * Get ready to enumerate...
3463 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3467 data
.user_data
= user_data
;
3468 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
3469 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3471 if (!(mask
& CUPS_PRINTER_DISCOVERED
) || !(type
& CUPS_PRINTER_DISCOVERED
))
3474 * Get the list of local printers and pass them to the callback function...
3477 num_dests
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &dests
, type
, mask
);
3479 if (data
.def_name
[0])
3482 * Lookup the named default printer and instance and make it the default...
3485 if ((dest
= cupsGetDest(data
.def_name
, data
.def_instance
, num_dests
, dests
)) != NULL
)
3487 DEBUG_printf(("1cups_enum_dests: Setting is_default on \"%s/%s\".", dest
->name
, dest
->instance
));
3488 dest
->is_default
= 1;
3492 for (i
= num_dests
, dest
= dests
;
3493 i
> 0 && (!cancel
|| !*cancel
);
3496 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3497 const char *device_uri
; /* Device URI */
3498 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3500 if ((user_dest
= cupsGetDest(dest
->name
, dest
->instance
, data
.num_dests
, data
.dests
)) != NULL
)
3503 * Apply user defaults to this destination...
3506 for (j
= user_dest
->num_options
, option
= user_dest
->options
; j
> 0; j
--, option
++)
3507 dest
->num_options
= cupsAddOption(option
->name
, option
->value
, dest
->num_options
, &dest
->options
);
3510 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
, dest
))
3513 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3514 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
3517 * Add existing queue using service name, etc. so we don't list it again...
3520 char scheme
[32], /* URI scheme */
3521 userpass
[32], /* Username:password */
3522 serviceName
[256], /* Service name (host field) */
3523 resource
[256], /* Resource (options) */
3524 *regtype
, /* Registration type */
3525 *replyDomain
; /* Registration domain */
3526 int port
; /* Port number (not used) */
3528 if (httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), serviceName
, sizeof(serviceName
), &port
, resource
, sizeof(resource
)) >= HTTP_URI_STATUS_OK
)
3530 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
3534 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
3536 replyDomain
[5] = '\0';
3539 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
3540 device
->state
= _CUPS_DNSSD_ACTIVE
;
3545 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3548 cupsFreeDests(num_dests
, dests
);
3550 if (i
> 0 || msec
== 0)
3555 * Return early if the caller doesn't want to do discovery...
3558 if ((mask
& CUPS_PRINTER_DISCOVERED
) && !(type
& CUPS_PRINTER_DISCOVERED
))
3561 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3563 * Get Bonjour-shared printers...
3566 gettimeofday(&curtime
, NULL
);
3569 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
3571 DEBUG_puts("1cups_enum_dests: Unable to create service browser, returning 0.");
3573 cupsFreeDests(data
.num_dests
, data
.dests
);
3578 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
3580 ipp_ref
= data
.main_ref
;
3581 if (DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipp._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3583 DEBUG_puts("1cups_enum_dests: Unable to create IPP browser, returning 0.");
3584 DNSServiceRefDeallocate(data
.main_ref
);
3586 cupsFreeDests(data
.num_dests
, data
.dests
);
3592 ipps_ref
= data
.main_ref
;
3593 if (DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipps._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3595 DEBUG_puts("1cups_enum_dests: Unable to create IPPS browser, returning 0.");
3596 DNSServiceRefDeallocate(data
.main_ref
);
3598 cupsFreeDests(data
.num_dests
, data
.dests
);
3602 # endif /* HAVE_SSL */
3604 # else /* HAVE_AVAHI */
3605 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
3607 DEBUG_puts("1cups_enum_dests: Unable to create Avahi poll, returning 0.");
3609 cupsFreeDests(data
.num_dests
, data
.dests
);
3614 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
3616 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
3617 0, cups_dnssd_client_cb
, &data
,
3621 DEBUG_puts("1cups_enum_dests: Unable to create Avahi client, returning 0.");
3622 avahi_simple_poll_free(data
.simple_poll
);
3624 cupsFreeDests(data
.num_dests
, data
.dests
);
3630 if ((ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
)) == NULL
)
3632 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPP browser, returning 0.");
3634 avahi_client_free(data
.client
);
3635 avahi_simple_poll_free(data
.simple_poll
);
3637 cupsFreeDests(data
.num_dests
, data
.dests
);
3644 if ((ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
, 0, cups_dnssd_browse_cb
, &data
)) == NULL
)
3646 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPPS browser, returning 0.");
3648 avahi_service_browser_free(ipp_ref
);
3649 avahi_client_free(data
.client
);
3650 avahi_simple_poll_free(data
.simple_poll
);
3652 cupsFreeDests(data
.num_dests
, data
.dests
);
3656 # endif /* HAVE_SSL */
3657 # endif /* HAVE_DNSSD */
3660 remaining
= INT_MAX
;
3664 while (remaining
> 0 && (!cancel
|| !*cancel
))
3667 * Check for input...
3670 DEBUG_printf(("1cups_enum_dests: remaining=%d", remaining
));
3672 cups_elapsed(&curtime
);
3677 pfd
.events
= POLLIN
;
3679 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3683 FD_SET(main_fd
, &input
);
3686 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3688 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
3689 # endif /* HAVE_POLL */
3692 DNSServiceProcessResult(data
.main_ref
);
3693 else if (nfds
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
3696 # else /* HAVE_AVAHI */
3699 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
3702 * We've been told to exit the loop. Perhaps the connection to
3709 DEBUG_printf(("1cups_enum_dests: got_data=%d", data
.got_data
));
3710 # endif /* HAVE_DNSSD */
3712 remaining
-= cups_elapsed(&curtime
);
3714 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
3715 count
= 0, completed
= 0;
3717 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
3722 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3725 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
3727 DEBUG_printf(("1cups_enum_dests: Querying '%s'.", device
->fullName
));
3730 device
->ref
= data
.main_ref
;
3732 if (DNSServiceQueryRecord(&(device
->ref
), kDNSServiceFlagsShareConnection
, 0, device
->fullName
, kDNSServiceType_TXT
, kDNSServiceClass_IN
, (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
, &data
) == kDNSServiceErr_NoError
)
3739 device
->state
= _CUPS_DNSSD_ERROR
;
3741 DEBUG_puts("1cups_enum_dests: Query failed.");
3744 # else /* HAVE_AVAHI */
3745 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
)
3747 DEBUG_printf(("1cups_enum_dests: Query ref=%p", device
->ref
));
3752 device
->state
= _CUPS_DNSSD_ERROR
;
3754 DEBUG_printf(("1cups_enum_dests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
3756 # endif /* HAVE_DNSSD */
3758 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
3762 DEBUG_printf(("1cups_enum_dests: Query for \"%s\" is complete.", device
->fullName
));
3764 if ((device
->type
& mask
) == type
)
3766 dest
= &device
->dest
;
3768 if ((user_dest
= cupsGetDest(dest
->name
, dest
->instance
, data
.num_dests
, data
.dests
)) != NULL
)
3771 * Apply user defaults to this destination...
3774 for (j
= user_dest
->num_options
, option
= user_dest
->options
; j
> 0; j
--, option
++)
3775 dest
->num_options
= cupsAddOption(option
->name
, option
->value
, dest
->num_options
, &dest
->options
);
3778 if (!strcasecmp(dest
->name
, data
.def_name
) && !data
.def_instance
)
3780 DEBUG_printf(("1cups_enum_dests: Setting is_default on discovered \"%s\".", dest
->name
));
3781 dest
->is_default
= 1;
3784 DEBUG_printf(("1cups_enum_dests: Add callback for \"%s\".", device
->dest
.name
));
3785 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
))
3792 device
->state
= _CUPS_DNSSD_ACTIVE
;
3797 DEBUG_printf(("1cups_enum_dests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
3799 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
3802 DEBUG_printf(("1cups_enum_dests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
3804 if (completed
== cupsArrayCount(data
.devices
))
3806 # endif /* HAVE_AVAHI */
3808 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3816 cupsFreeDests(data
.num_dests
, data
.dests
);
3818 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3819 cupsArrayDelete(data
.devices
);
3823 DNSServiceRefDeallocate(ipp_ref
);
3827 DNSServiceRefDeallocate(ipps_ref
);
3828 # endif /* HAVE_SSL */
3831 DNSServiceRefDeallocate(data
.main_ref
);
3833 # else /* HAVE_AVAHI */
3835 avahi_service_browser_free(ipp_ref
);
3838 avahi_service_browser_free(ipps_ref
);
3839 # endif /* HAVE_SSL */
3842 avahi_client_free(data
.client
);
3843 if (data
.simple_poll
)
3844 avahi_simple_poll_free(data
.simple_poll
);
3845 # endif /* HAVE_DNSSD */
3846 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3848 DEBUG_puts("1cups_enum_dests: Returning 1.");
3855 * 'cups_find_dest()' - Find a destination using a binary search.
3858 static int /* O - Index of match */
3859 cups_find_dest(const char *name
, /* I - Destination name */
3860 const char *instance
, /* I - Instance or NULL */
3861 int num_dests
, /* I - Number of destinations */
3862 cups_dest_t
*dests
, /* I - Destinations */
3863 int prev
, /* I - Previous index */
3864 int *rdiff
) /* O - Difference of match */
3866 int left
, /* Low mark for binary search */
3867 right
, /* High mark for binary search */
3868 current
, /* Current index */
3869 diff
; /* Result of comparison */
3870 cups_dest_t key
; /* Search key */
3873 key
.name
= (char *)name
;
3874 key
.instance
= (char *)instance
;
3879 * Start search on either side of previous...
3882 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3883 (diff
< 0 && prev
== 0) ||
3884 (diff
> 0 && prev
== (num_dests
- 1)))
3892 * Start with previous on right side...
3901 * Start wih previous on left side...
3905 right
= num_dests
- 1;
3911 * Start search in the middle...
3915 right
= num_dests
- 1;
3920 current
= (left
+ right
) / 2;
3921 diff
= cups_compare_dests(&key
, dests
+ current
);
3930 while ((right
- left
) > 1);
3935 * Check the last 1 or 2 elements...
3938 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3942 diff
= cups_compare_dests(&key
, dests
+ right
);
3948 * Return the closest destination and the difference...
3958 * 'cups_get_cb()' - Collect enumerated destinations.
3961 static int /* O - 1 to continue, 0 to stop */
3962 cups_get_cb(_cups_getdata_t
*data
, /* I - Data from cupsGetDests */
3963 unsigned flags
, /* I - Enumeration flags */
3964 cups_dest_t
*dest
) /* I - Destination */
3966 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
3969 * Remove destination from array...
3972 data
->num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
->num_dests
, &data
->dests
);
3977 * Add destination to array...
3980 data
->num_dests
= cupsCopyDest(dest
, data
->num_dests
, &data
->dests
);
3988 * 'cups_get_default()' - Get the default destination from an lpoptions file.
3991 static char * /* O - Default destination or NULL */
3992 cups_get_default(const char *filename
, /* I - File to read */
3993 char *namebuf
, /* I - Name buffer */
3994 size_t namesize
, /* I - Size of name buffer */
3995 const char **instance
) /* I - Instance */
3997 cups_file_t
*fp
; /* lpoptions file */
3998 char line
[8192], /* Line from file */
3999 *value
, /* Value for line */
4000 *nameptr
; /* Pointer into name */
4001 int linenum
; /* Current line */
4006 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
4010 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4012 if (!_cups_strcasecmp(line
, "default") && value
)
4014 strlcpy(namebuf
, value
, namesize
);
4016 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
4018 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
4021 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
4024 *instance
= nameptr
;
4032 return (*namebuf
? namebuf
: NULL
);
4037 * 'cups_get_dests()' - Get destinations from a file.
4040 static int /* O - Number of destinations */
4042 const char *filename
, /* I - File to read from */
4043 const char *match_name
, /* I - Destination name we want */
4044 const char *match_inst
, /* I - Instance name we want */
4045 int load_all
, /* I - Load all saved destinations? */
4046 int user_default_set
, /* I - User default printer set? */
4047 int num_dests
, /* I - Number of destinations */
4048 cups_dest_t
**dests
) /* IO - Destinations */
4050 int i
; /* Looping var */
4051 cups_dest_t
*dest
; /* Current destination */
4052 cups_file_t
*fp
; /* File pointer */
4053 char line
[8192], /* Line from file */
4054 *lineptr
, /* Pointer into line */
4055 *name
, /* Name of destination/option */
4056 *instance
; /* Instance of destination */
4057 int linenum
; /* Current line number */
4060 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", match_inst=\"%s\", load_all=%d, user_default_set=%d, num_dests=%d, dests=%p)", filename
, match_name
, match_inst
, load_all
, user_default_set
, num_dests
, (void *)dests
));
4063 * Try to open the file...
4066 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4070 * Read each printer; each line looks like:
4072 * Dest name[/instance] options
4073 * Default name[/instance] options
4078 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
4081 * See what type of line it is...
4084 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
4085 linenum
, line
, lineptr
));
4087 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
4089 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
4096 * Search for an instance...
4099 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
4102 if (*lineptr
== '/')
4105 * Found an instance...
4112 * Search for an instance...
4115 while (!isspace(*lineptr
& 255) && *lineptr
)
4124 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
4128 * Match and/or ignore missing destinations...
4133 if (_cups_strcasecmp(name
, match_name
) ||
4134 (!instance
&& match_inst
) ||
4135 (instance
&& !match_inst
) ||
4136 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
4141 else if (!load_all
&& cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
4143 DEBUG_puts("9cups_get_dests: Not found!");
4149 * Add the destination...
4152 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4154 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4160 DEBUG_puts("9cups_get_dests: Out of memory!");
4166 * Add options until we hit the end of the line...
4169 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
, &(dest
->options
));
4172 * If we found what we were looking for, stop now...
4179 * Set this as default if needed...
4182 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4184 DEBUG_puts("9cups_get_dests: Setting as default...");
4186 for (i
= 0; i
< num_dests
; i
++)
4187 (*dests
)[i
].is_default
= 0;
4189 dest
->is_default
= 1;
4194 * Close the file and return...
4204 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4208 static char * /* O - New string */
4210 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4211 char *buffer
, /* I - Buffer */
4212 size_t bufsize
) /* I - Size of buffer */
4214 int i
; /* Looping var */
4215 char *ptr
, /* Pointer into buffer */
4216 *end
, /* Pointer to end of buffer */
4217 *valptr
; /* Pointer into string attribute */
4221 * Return quickly if we have a single string value...
4224 if (attr
->num_values
== 1 &&
4225 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4226 attr
->value_tag
!= IPP_TAG_ENUM
&&
4227 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4228 attr
->value_tag
!= IPP_TAG_RANGE
)
4229 return (attr
->values
[0].string
.text
);
4232 * Copy the values to the string, separating with commas and escaping strings
4236 end
= buffer
+ bufsize
- 1;
4238 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4243 switch (attr
->value_tag
)
4245 case IPP_TAG_INTEGER
:
4247 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4250 case IPP_TAG_BOOLEAN
:
4251 if (attr
->values
[i
].boolean
)
4252 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4254 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4257 case IPP_TAG_RANGE
:
4258 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4259 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4261 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4265 for (valptr
= attr
->values
[i
].string
.text
;
4266 *valptr
&& ptr
< end
;)
4268 if (strchr(" \t\n\\\'\"", *valptr
))
4270 if (ptr
>= (end
- 1))
4293 * 'cups_name_cb()' - Find an enumerated destination.
4296 static int /* O - 1 to continue, 0 to stop */
4297 cups_name_cb(_cups_namedata_t
*data
, /* I - Data from cupsGetNamedDest */
4298 unsigned flags
, /* I - Enumeration flags */
4299 cups_dest_t
*dest
) /* I - Destination */
4301 DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", (void *)data
, data
->name
, flags
, (void *)dest
, dest
->name
));
4303 if (!(flags
& CUPS_DEST_FLAGS_REMOVED
) && !dest
->instance
&& !strcasecmp(data
->name
, dest
->name
))
4306 * Copy destination and stop enumeration...
4309 cupsCopyDest(dest
, 0, &data
->dest
);
4318 * 'cups_queue_name()' - Create a local queue name based on the service name.
4323 char *name
, /* I - Name buffer */
4324 const char *serviceName
, /* I - Service name */
4325 size_t namesize
) /* I - Size of name buffer */
4327 const char *ptr
; /* Pointer into serviceName */
4328 char *nameptr
; /* Pointer into name */
4331 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4334 * Sanitize the printer name...
4337 if (_cups_isalnum(*ptr
))
4339 else if (nameptr
== name
|| nameptr
[-1] != '_')