2 * User-defined destination (and option) support for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
25 #endif /* HAVE_NOTIFY_H */
29 #endif /* HAVE_POLL */
33 #endif /* HAVE_DNSSD */
36 # include <avahi-client/client.h>
37 # include <avahi-client/lookup.h>
38 # include <avahi-common/simple-watch.h>
39 # include <avahi-common/domain.h>
40 # include <avahi-common/error.h>
41 # include <avahi-common/malloc.h>
42 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
43 #endif /* HAVE_AVAHI */
52 # include <SystemConfiguration/SystemConfiguration.h>
53 # define _CUPS_LOCATION_DEFAULTS 1
54 # endif /* !TARGET_OS_IOS */
55 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
56 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
57 # define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
58 # define kLocationNetworkKey CFSTR("Network")
59 # define kLocationPrinterIDKey CFSTR("PrinterID")
60 # define kUseLastPrinter CFSTR("UseLastPrinter")
61 #endif /* __APPLE__ */
63 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
64 # define _CUPS_DNSSD_GET_DESTS 250 /* Milliseconds for cupsGetDests */
65 # define _CUPS_DNSSD_MAXTIME 50 /* Milliseconds for maximum quantum of time */
67 # define _CUPS_DNSSD_GET_DESTS 0 /* Milliseconds for cupsGetDests */
68 #endif /* HAVE_DNSSD || HAVE_AVAHI */
75 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
76 typedef enum _cups_dnssd_state_e
/* Enumerated device state */
83 _CUPS_DNSSD_INCOMPATIBLE
,
85 } _cups_dnssd_state_t
;
87 typedef struct _cups_dnssd_data_s
/* Enumeration data */
90 DNSServiceRef main_ref
; /* Main service reference */
91 # else /* HAVE_AVAHI */
92 AvahiSimplePoll
*simple_poll
; /* Polling interface */
93 AvahiClient
*client
; /* Client information */
94 int got_data
; /* Did we get data? */
95 int browsers
; /* How many browsers are running? */
96 # endif /* HAVE_DNSSD */
97 cups_dest_cb_t cb
; /* Callback */
98 void *user_data
; /* User data pointer */
99 cups_ptype_t type
, /* Printer type filter */
100 mask
; /* Printer type mask */
101 cups_array_t
*devices
; /* Devices found so far */
102 } _cups_dnssd_data_t
;
104 typedef struct _cups_dnssd_device_s
/* Enumerated device */
106 _cups_dnssd_state_t state
; /* State of device listing */
108 DNSServiceRef ref
; /* Service reference for query */
109 # else /* HAVE_AVAHI */
110 AvahiRecordBrowser
*ref
; /* Browser for query */
111 # endif /* HAVE_DNSSD */
112 char *fullName
, /* Full name */
113 *regtype
, /* Registration type */
114 *domain
; /* Domain name */
115 cups_ptype_t type
; /* Device registration type */
116 cups_dest_t dest
; /* Destination record */
117 } _cups_dnssd_device_t
;
119 typedef struct _cups_dnssd_resolve_s
/* Data for resolving URI */
121 int *cancel
; /* Pointer to "cancel" variable */
122 struct timeval end_time
; /* Ending time */
123 } _cups_dnssd_resolve_t
;
124 #endif /* HAVE_DNSSD */
126 typedef struct _cups_getdata_s
128 int num_dests
; /* Number of destinations */
129 cups_dest_t
*dests
; /* Destinations */
132 typedef struct _cups_namedata_s
134 const char *name
; /* Named destination */
135 cups_dest_t
*dest
; /* Destination */
143 #if _CUPS_LOCATION_DEFAULTS
144 static CFArrayRef
appleCopyLocations(void);
145 static CFStringRef
appleCopyNetwork(void);
146 #endif /* _CUPS_LOCATION_DEFAULTS */
148 static char *appleGetPaperSize(char *name
, size_t namesize
);
149 #endif /* __APPLE__ */
150 #if _CUPS_LOCATION_DEFAULTS
151 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
152 CFStringRef network
, CFIndex
*locindex
);
153 #endif /* _CUPS_LOCATION_DEFAULTS */
154 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
155 int *num_dests
, cups_dest_t
**dests
);
157 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
159 #endif /* __BLOCKS__ */
160 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
161 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
163 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
164 DNSServiceFlags flags
,
165 uint32_t interfaceIndex
,
166 DNSServiceErrorType errorCode
,
167 const char *serviceName
,
169 const char *replyDomain
,
171 # else /* HAVE_AVAHI */
172 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
173 AvahiIfIndex interface
,
174 AvahiProtocol protocol
,
175 AvahiBrowserEvent event
,
176 const char *serviceName
,
178 const char *replyDomain
,
179 AvahiLookupResultFlags flags
,
181 static void cups_dnssd_client_cb(AvahiClient
*client
,
182 AvahiClientState state
,
184 # endif /* HAVE_DNSSD */
185 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
186 _cups_dnssd_device_t
*b
);
187 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
188 _cups_dnssd_data_t
*data
);
189 static _cups_dnssd_device_t
*
190 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
191 const char *serviceName
,
193 const char *replyDomain
);
195 static void cups_dnssd_local_cb(DNSServiceRef sdRef
,
196 DNSServiceFlags flags
,
197 uint32_t interfaceIndex
,
198 DNSServiceErrorType errorCode
,
199 const char *serviceName
,
201 const char *replyDomain
,
203 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
204 DNSServiceFlags flags
,
205 uint32_t interfaceIndex
,
206 DNSServiceErrorType errorCode
,
207 const char *fullName
,
208 uint16_t rrtype
, uint16_t rrclass
,
209 uint16_t rdlen
, const void *rdata
,
210 uint32_t ttl
, void *context
);
211 # else /* HAVE_AVAHI */
212 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
213 unsigned int num_pollfds
,
214 int timeout
, void *context
);
215 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
216 AvahiIfIndex interface
,
217 AvahiProtocol protocol
,
218 AvahiBrowserEvent event
,
219 const char *name
, uint16_t rrclass
,
220 uint16_t rrtype
, const void *rdata
,
222 AvahiLookupResultFlags flags
,
224 # endif /* HAVE_DNSSD */
225 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
226 int msec
, int *cancel
,
227 cups_dest_cb_t cb
, void *user_data
);
228 static int cups_dnssd_resolve_cb(void *context
);
229 static void cups_dnssd_unquote(char *dst
, const char *src
,
231 static int cups_elapsed(struct timeval
*t
);
232 #endif /* HAVE_DNSSD || HAVE_AVAHI */
233 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
);
234 static int cups_find_dest(const char *name
, const char *instance
,
235 int num_dests
, cups_dest_t
*dests
, int prev
,
237 static int cups_get_cb(_cups_getdata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
238 static char *cups_get_default(const char *filename
, char *namebuf
,
239 size_t namesize
, const char **instance
);
240 static int cups_get_dests(const char *filename
, const char *match_name
,
241 const char *match_inst
, int user_default_set
,
242 int num_dests
, cups_dest_t
**dests
);
243 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
245 static int cups_name_cb(_cups_namedata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
246 static void cups_queue_name(char *name
, const char *serviceName
, size_t namesize
);
250 * 'cupsAddDest()' - Add a destination to the list of destinations.
252 * This function cannot be used to add a new class or printer queue,
253 * it only adds a new container of saved options for the named
254 * destination or instance.
256 * If the named destination already exists, the destination list is
257 * returned unchanged. Adding a new instance of a destination creates
258 * a copy of that destination's options.
260 * Use the @link cupsSaveDests@ function to save the updated list of
261 * destinations to the user's lpoptions file.
264 int /* O - New number of destinations */
265 cupsAddDest(const char *name
, /* I - Destination name */
266 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
267 int num_dests
, /* I - Number of destinations */
268 cups_dest_t
**dests
) /* IO - Destinations */
270 int i
; /* Looping var */
271 cups_dest_t
*dest
; /* Destination pointer */
272 cups_dest_t
*parent
= NULL
; /* Parent destination */
273 cups_option_t
*doption
, /* Current destination option */
274 *poption
; /* Current parent option */
280 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
282 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
285 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
289 * Find the base dest again now the array has been realloc'd.
292 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
294 if (instance
&& parent
&& parent
->num_options
> 0)
297 * Copy options from parent...
300 dest
->options
= calloc(sizeof(cups_option_t
), (size_t)parent
->num_options
);
304 dest
->num_options
= parent
->num_options
;
306 for (i
= dest
->num_options
, doption
= dest
->options
,
307 poption
= parent
->options
;
309 i
--, doption
++, poption
++)
311 doption
->name
= _cupsStrRetain(poption
->name
);
312 doption
->value
= _cupsStrRetain(poption
->value
);
324 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
327 CFStringRef
/* O - Default paper ID */
328 _cupsAppleCopyDefaultPaperID(void)
330 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
331 kCUPSPrintingPrefs
));
336 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
339 CFStringRef
/* O - Default printer name */
340 _cupsAppleCopyDefaultPrinter(void)
342 # if _CUPS_LOCATION_DEFAULTS
343 CFStringRef network
; /* Network location */
344 CFArrayRef locations
; /* Location array */
345 CFStringRef locprinter
; /* Current printer */
349 * Use location-based defaults only if "use last printer" is selected in the
350 * system preferences...
353 if (!_cupsAppleGetUseLastPrinter())
355 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
361 * Get the current location...
364 if ((network
= appleCopyNetwork()) == NULL
)
366 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
372 * Lookup the network in the preferences...
375 if ((locations
= appleCopyLocations()) == NULL
)
378 * Missing or bad location array, so no location-based default...
381 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
389 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
390 (int)CFArrayGetCount(locations
)));
392 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
393 CFRetain(locprinter
);
396 CFRelease(locations
);
402 # endif /* _CUPS_LOCATION_DEFAULTS */
407 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
410 int /* O - 1 to use last printer, 0 otherwise */
411 _cupsAppleGetUseLastPrinter(void)
413 Boolean uselast
, /* Use last printer preference value */
414 uselast_set
; /* Valid is set? */
417 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
420 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
431 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
435 _cupsAppleSetDefaultPaperID(
436 CFStringRef name
) /* I - New paper ID */
438 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
439 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
441 # ifdef HAVE_NOTIFY_POST
442 notify_post("com.apple.printerPrefsChange");
443 # endif /* HAVE_NOTIFY_POST */
448 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
452 _cupsAppleSetDefaultPrinter(
453 CFStringRef name
) /* I - Default printer/class name */
455 # if _CUPS_LOCATION_DEFAULTS
456 CFStringRef network
; /* Current network */
457 CFArrayRef locations
; /* Old locations array */
458 CFIndex locindex
; /* Index in locations array */
459 CFStringRef locprinter
; /* Current printer */
460 CFMutableArrayRef newlocations
; /* New locations array */
461 CFMutableDictionaryRef newlocation
; /* New location */
465 * Get the current location...
468 if ((network
= appleCopyNetwork()) == NULL
)
470 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
475 * Lookup the network in the preferences...
478 if ((locations
= appleCopyLocations()) != NULL
)
479 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
486 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
489 * Need to change the locations array...
494 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
498 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
501 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
502 &kCFTypeArrayCallBacks
);
504 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
505 &kCFTypeDictionaryKeyCallBacks
,
506 &kCFTypeDictionaryValueCallBacks
);
508 if (newlocation
&& newlocations
)
511 * Put the new location at the front of the array...
514 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
515 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
516 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
519 * Limit the number of locations to 10...
522 while (CFArrayGetCount(newlocations
) > 10)
523 CFArrayRemoveValueAtIndex(newlocations
, 10);
526 * Push the changes out...
529 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
531 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
533 # ifdef HAVE_NOTIFY_POST
534 notify_post("com.apple.printerPrefsChange");
535 # endif /* HAVE_NOTIFY_POST */
539 CFRelease(newlocations
);
542 CFRelease(newlocation
);
546 CFRelease(locations
);
552 # endif /* _CUPS_LOCATION_DEFAULTS */
557 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
561 _cupsAppleSetUseLastPrinter(
562 int uselast
) /* O - 1 to use last printer, 0 otherwise */
564 CFPreferencesSetAppValue(kUseLastPrinter
,
565 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
567 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
569 # ifdef HAVE_NOTIFY_POST
570 notify_post("com.apple.printerPrefsChange");
571 # endif /* HAVE_NOTIFY_POST */
573 #endif /* __APPLE__ */
577 * 'cupsConnectDest()' - Open a conection to the destination.
579 * Connect to the destination, returning a new @code http_t@ connection object
580 * and optionally the resource path to use for the destination. These calls
581 * will block until a connection is made, the timeout expires, the integer
582 * pointed to by "cancel" is non-zero, or the callback function (or block)
583 * returns 0. The caller is responsible for calling @link httpClose@ on the
584 * returned connection.
586 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
587 * for the "flags" argument to connect directly to the device associated with
588 * the destination. Otherwise, the connection is made to the CUPS scheduler
589 * associated with the destination.
591 * @since CUPS 1.6/macOS 10.8@
594 http_t
* /* O - Connection to destination or @code NULL@ */
596 cups_dest_t
*dest
, /* I - Destination */
597 unsigned flags
, /* I - Connection flags */
598 int msec
, /* I - Timeout in milliseconds */
599 int *cancel
, /* I - Pointer to "cancel" variable */
600 char *resource
, /* I - Resource buffer */
601 size_t resourcesize
, /* I - Size of resource buffer */
602 cups_dest_cb_t cb
, /* I - Callback function */
603 void *user_data
) /* I - User data pointer */
605 const char *uri
; /* Printer URI */
606 char scheme
[32], /* URI scheme */
607 userpass
[256], /* Username and password (unused) */
608 hostname
[256], /* Hostname */
609 tempresource
[1024]; /* Temporary resource buffer */
610 int port
; /* Port number */
611 char portstr
[16]; /* Port number string */
612 http_encryption_t encryption
; /* Encryption to use */
613 http_addrlist_t
*addrlist
; /* Address list for server */
614 http_t
*http
; /* Connection to server */
617 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
));
620 * Range check input...
628 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
632 if (!resource
|| resourcesize
< 1)
634 resource
= tempresource
;
635 resourcesize
= sizeof(tempresource
);
639 * Grab the printer URI...
642 if (flags
& CUPS_DEST_FLAGS_DEVICE
)
644 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
646 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
647 if (strstr(uri
, "._tcp"))
648 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
649 #endif /* HAVE_DNSSD || HAVE_AVAHI */
652 else if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
654 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
656 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
657 if (strstr(uri
, "._tcp"))
658 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
659 #endif /* HAVE_DNSSD || HAVE_AVAHI */
663 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
667 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
669 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
675 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
678 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
683 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
684 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
685 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
687 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
690 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
697 * Lookup the address for the server...
701 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
703 snprintf(portstr
, sizeof(portstr
), "%d", port
);
705 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
708 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
713 if (cancel
&& *cancel
)
715 httpAddrFreeList(addrlist
);
718 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
724 * Create the HTTP object pointing to the server referenced by the URI...
727 if (!strcmp(scheme
, "ipps") || port
== 443)
728 encryption
= HTTP_ENCRYPTION_ALWAYS
;
730 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
732 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
733 httpAddrFreeList(addrlist
);
736 * Connect if requested...
739 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
742 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
747 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
749 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
751 if (cancel
&& *cancel
)
752 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
754 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
757 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
766 * 'cupsConnectDestBlock()' - Open a connection to the destination.
768 * Connect to the destination, returning a new @code http_t@ connection object
769 * and optionally the resource path to use for the destination. These calls
770 * will block until a connection is made, the timeout expires, the integer
771 * pointed to by "cancel" is non-zero, or the block returns 0. The caller is
772 * responsible for calling @link httpClose@ on the returned connection.
774 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
775 * for the "flags" argument to connect directly to the device associated with
776 * the destination. Otherwise, the connection is made to the CUPS scheduler
777 * associated with the destination.
779 * @since CUPS 1.6/macOS 10.8@ @exclude all@
782 http_t
* /* O - Connection to destination or @code NULL@ */
783 cupsConnectDestBlock(
784 cups_dest_t
*dest
, /* I - Destination */
785 unsigned flags
, /* I - Connection flags */
786 int msec
, /* I - Timeout in milliseconds */
787 int *cancel
, /* I - Pointer to "cancel" variable */
788 char *resource
, /* I - Resource buffer */
789 size_t resourcesize
, /* I - Size of resource buffer */
790 cups_dest_block_t block
) /* I - Callback block */
792 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
793 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
795 #endif /* __BLOCKS__ */
799 * 'cupsCopyDest()' - Copy a destination.
801 * Make a copy of the destination to an array of destinations (or just a single
802 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
803 * for calling cupsFreeDests() on the returned object(s).
805 * @since CUPS 1.6/macOS 10.8@
808 int /* O - New number of destinations */
809 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
810 int num_dests
, /* I - Number of destinations */
811 cups_dest_t
**dests
) /* IO - Destination array */
813 int i
; /* Looping var */
814 cups_dest_t
*new_dest
; /* New destination pointer */
815 cups_option_t
*new_option
, /* Current destination option */
816 *option
; /* Current parent option */
820 * Range check input...
823 if (!dest
|| num_dests
< 0 || !dests
)
827 * See if the destination already exists...
830 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
834 * Protect against copying destination to itself...
837 if (new_dest
== dest
)
841 * Otherwise, free the options...
844 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
846 new_dest
->num_options
= 0;
847 new_dest
->options
= NULL
;
850 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
854 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
855 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
857 new_dest
->num_options
= dest
->num_options
;
859 for (i
= dest
->num_options
, option
= dest
->options
,
860 new_option
= new_dest
->options
;
862 i
--, option
++, new_option
++)
864 new_option
->name
= _cupsStrRetain(option
->name
);
865 new_option
->value
= _cupsStrRetain(option
->value
);
874 * '_cupsCreateDest()' - Create a local (temporary) queue.
877 char * /* O - Printer URI or @code NULL@ on error */
878 _cupsCreateDest(const char *name
, /* I - Printer name */
879 const char *info
, /* I - Printer description of @code NULL@ */
880 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
881 const char *device_uri
, /* I - Device URI */
882 char *uri
, /* I - Printer URI buffer */
883 size_t urisize
) /* I - Size of URI buffer */
885 http_t
*http
; /* Connection to server */
886 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
887 *response
; /* CUPS-Create-Local-Printer response */
888 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
889 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
890 /* printer-state value */
893 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
896 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
899 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
901 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
902 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
904 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
905 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
907 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
909 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
911 response
= cupsDoRequest(http
, request
, "/");
913 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
914 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
922 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
923 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
925 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
930 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
932 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
933 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
934 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
936 response
= cupsDoRequest(http
, request
, "/");
938 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
939 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
951 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
953 * Destinations are enumerated from one or more sources. The callback function
954 * receives the @code user_data@ pointer and the destination pointer which can
955 * be used as input to the @link cupsCopyDest@ function. The function must
956 * return 1 to continue enumeration or 0 to stop.
958 * The @code type@ and @code mask@ arguments allow the caller to filter the
959 * destinations that are enumerated. Passing 0 for both will enumerate all
960 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
961 * destinations that are available but have not yet been added locally.
963 * Enumeration happens on the current thread and does not return until all
964 * destinations have been enumerated or the callback function returns 0.
966 * Note: The callback function will likely receive multiple updates for the same
967 * destinations - it is up to the caller to suppress any duplicate destinations.
969 * @since CUPS 1.6/macOS 10.8@
972 int /* O - 1 on success, 0 on failure */
974 unsigned flags
, /* I - Enumeration flags */
975 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
976 int *cancel
, /* I - Pointer to "cancel" variable */
977 cups_ptype_t type
, /* I - Printer type bits */
978 cups_ptype_t mask
, /* I - Mask for printer type bits */
979 cups_dest_cb_t cb
, /* I - Callback function */
980 void *user_data
) /* I - User data */
982 return (cups_enum_dests(CUPS_HTTP_DEFAULT
, flags
, msec
, cancel
, type
, mask
, cb
, user_data
));
988 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
990 * Destinations are enumerated from one or more sources. The block receives the
991 * @code user_data@ pointer and the destination pointer which can be used as
992 * input to the @link cupsCopyDest@ function. The block must return 1 to
993 * continue enumeration or 0 to stop.
995 * The @code type@ and @code mask@ arguments allow the caller to filter the
996 * destinations that are enumerated. Passing 0 for both will enumerate all
997 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
998 * destinations that are available but have not yet been added locally.
1000 * Enumeration happens on the current thread and does not return until all
1001 * destinations have been enumerated or the block returns 0.
1003 * Note: The block will likely receive multiple updates for the same
1004 * destinations - it is up to the caller to suppress any duplicate destinations.
1006 * @since CUPS 1.6/macOS 10.8@ @exclude all@
1009 int /* O - 1 on success, 0 on failure */
1011 unsigned flags
, /* I - Enumeration flags */
1012 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1013 int *cancel
, /* I - Pointer to "cancel" variable */
1014 cups_ptype_t type
, /* I - Printer type bits */
1015 cups_ptype_t mask
, /* I - Mask for printer type bits */
1016 cups_dest_block_t block
) /* I - Block */
1018 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1019 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1021 # endif /* __BLOCKS__ */
1025 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1029 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1030 cups_dest_t
*dests
) /* I - Destinations */
1032 int i
; /* Looping var */
1033 cups_dest_t
*dest
; /* Current destination */
1036 if (num_dests
== 0 || dests
== NULL
)
1039 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1041 _cupsStrFree(dest
->name
);
1042 _cupsStrFree(dest
->instance
);
1044 cupsFreeOptions(dest
->num_options
, dest
->options
);
1052 * 'cupsGetDest()' - Get the named destination from the list.
1054 * Use the @link cupsEnumDests@ or @link cupsGetDests2@ functions to get a
1055 * list of supported destinations for the current user.
1058 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1059 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1060 const char *instance
, /* I - Instance name or @code NULL@ */
1061 int num_dests
, /* I - Number of destinations */
1062 cups_dest_t
*dests
) /* I - Destinations */
1064 int diff
, /* Result of comparison */
1065 match
; /* Matching index */
1068 if (num_dests
<= 0 || !dests
)
1074 * NULL name for default printer.
1077 while (num_dests
> 0)
1079 if (dests
->is_default
)
1089 * Lookup name and optionally the instance...
1092 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1095 return (dests
+ match
);
1103 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1106 const char * /* O - Printer URI */
1107 _cupsGetDestResource(
1108 cups_dest_t
*dest
, /* I - Destination */
1109 char *resource
, /* I - Resource buffer */
1110 size_t resourcesize
) /* I - Size of resource buffer */
1112 const char *uri
; /* Printer URI */
1113 char scheme
[32], /* URI scheme */
1114 userpass
[256], /* Username and password (unused) */
1115 hostname
[256]; /* Hostname */
1116 int port
; /* Port number */
1119 DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", (void *)dest
, dest
->name
, (void *)resource
, (int)resourcesize
));
1122 * Range check input...
1125 if (!dest
|| !resource
|| resourcesize
< 1)
1130 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1135 * Grab the printer URI...
1138 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
1140 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
1142 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1143 if (strstr(uri
, "._tcp"))
1144 uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
);
1145 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1150 DEBUG_printf(("1_cupsGetDestResource: Resolved printer-uri-supported=\"%s\"", uri
));
1152 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, resource
, resourcesize
);
1157 DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri
));
1159 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
1161 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1165 DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported found.");
1170 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1177 DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri
));
1179 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1180 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1181 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1183 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1189 DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource
));
1196 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1198 * "name" is the desired name for the printer. If @code NULL@, a name will be
1199 * created using the URI.
1201 * "uri" is the "ipp" or "ipps" URI for the printer.
1203 * @since CUPS 2.0/macOS 10.10@
1206 cups_dest_t
* /* O - Destination or @code NULL@ */
1207 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1208 const char *uri
) /* I - URI for the printer */
1210 cups_dest_t
*dest
; /* New destination */
1211 char temp
[1024], /* Temporary string */
1212 scheme
[256], /* Scheme from URI */
1213 userpass
[256], /* Username:password from URI */
1214 hostname
[256], /* Hostname from URI */
1215 resource
[1024], /* Resource path from URI */
1216 *ptr
; /* Pointer into string */
1217 const char *info
; /* printer-info string */
1218 int port
; /* Port number from URI */
1222 * Range check input...
1227 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1231 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1232 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1234 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1246 * Create the name from the URI...
1249 if (strstr(hostname
, "._tcp"))
1252 * Use the service instance name...
1255 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1258 cups_queue_name(temp
, hostname
, sizeof(temp
));
1262 else if (!strncmp(resource
, "/classes/", 9))
1264 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1265 name
= resource
+ 9;
1268 else if (!strncmp(resource
, "/printers/", 10))
1270 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1271 name
= resource
+ 10;
1282 * Create the destination...
1285 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1287 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1291 dest
->name
= _cupsStrAlloc(name
);
1292 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1293 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1300 * '_cupsGetDests()' - Get destinations from a server.
1302 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1303 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1306 * "name" is the name of an existing printer and is only used when "op" is
1307 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1309 * "dest" is initialized to point to the array of destinations.
1311 * 0 is returned if there are no printers, no default printer, or the named
1312 * printer does not exist, respectively.
1314 * Free the memory used by the destination array using the @link cupsFreeDests@
1317 * Note: On macOS this function also gets the default paper from the system
1318 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1319 * options array for each destination that supports it.
1322 int /* O - Number of destinations */
1323 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1324 * @code CUPS_HTTP_DEFAULT@ */
1325 ipp_op_t op
, /* I - IPP operation */
1326 const char *name
, /* I - Name of destination */
1327 cups_dest_t
**dests
, /* IO - Destinations */
1328 cups_ptype_t type
, /* I - Printer type bits */
1329 cups_ptype_t mask
) /* I - Printer type mask */
1331 int num_dests
= 0; /* Number of destinations */
1332 cups_dest_t
*dest
; /* Current destination */
1333 ipp_t
*request
, /* IPP Request */
1334 *response
; /* IPP Response */
1335 ipp_attribute_t
*attr
; /* Current attribute */
1336 const char *printer_name
; /* printer-name attribute */
1337 char uri
[1024]; /* printer-uri value */
1338 int num_options
; /* Number of options */
1339 cups_option_t
*options
; /* Options */
1341 char media_default
[41]; /* Default paper size */
1342 #endif /* __APPLE__ */
1343 char optname
[1024], /* Option name */
1344 value
[2048], /* Option value */
1345 *ptr
; /* Pointer into name/value */
1346 static const char * const pattrs
[] = /* Attributes we're interested in */
1348 "auth-info-required",
1350 "job-sheets-default",
1351 "marker-change-time",
1353 "marker-high-levels",
1355 "marker-low-levels",
1361 #endif /* __APPLE__ */
1365 "printer-is-accepting-jobs",
1366 "printer-is-shared",
1367 "printer-is-temporary",
1369 "printer-make-and-model",
1370 "printer-mandatory-job-attributes",
1373 "printer-state-change-time",
1374 "printer-state-reasons",
1376 "printer-uri-supported"
1380 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
));
1384 * Get the default paper size...
1387 appleGetPaperSize(media_default
, sizeof(media_default
));
1388 DEBUG_printf(("1_cupsGetDests: Default media is '%s'.", media_default
));
1389 #endif /* __APPLE__ */
1392 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1393 * require the following attributes:
1395 * attributes-charset
1396 * attributes-natural-language
1397 * requesting-user-name
1398 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1401 request
= ippNewRequest(op
);
1403 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1404 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1407 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1408 "requesting-user-name", NULL
, cupsUser());
1410 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1412 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1413 "localhost", ippPort(), "/printers/%s", name
);
1414 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1419 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1420 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1424 * Do the request and get back a response...
1427 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1429 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1432 * Skip leading attributes until we hit a printer...
1435 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1442 * Pull the needed attributes from this printer...
1445 printer_name
= NULL
;
1449 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1451 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1452 attr
->value_tag
!= IPP_TAG_ENUM
&&
1453 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1454 attr
->value_tag
!= IPP_TAG_TEXT
&&
1455 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1456 attr
->value_tag
!= IPP_TAG_NAME
&&
1457 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1458 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1459 attr
->value_tag
!= IPP_TAG_RANGE
&&
1460 attr
->value_tag
!= IPP_TAG_URI
)
1463 if (!strcmp(attr
->name
, "auth-info-required") ||
1464 !strcmp(attr
->name
, "device-uri") ||
1465 !strcmp(attr
->name
, "marker-change-time") ||
1466 !strcmp(attr
->name
, "marker-colors") ||
1467 !strcmp(attr
->name
, "marker-high-levels") ||
1468 !strcmp(attr
->name
, "marker-levels") ||
1469 !strcmp(attr
->name
, "marker-low-levels") ||
1470 !strcmp(attr
->name
, "marker-message") ||
1471 !strcmp(attr
->name
, "marker-names") ||
1472 !strcmp(attr
->name
, "marker-types") ||
1473 !strcmp(attr
->name
, "printer-commands") ||
1474 !strcmp(attr
->name
, "printer-info") ||
1475 !strcmp(attr
->name
, "printer-is-shared") ||
1476 !strcmp(attr
->name
, "printer-is-temporary") ||
1477 !strcmp(attr
->name
, "printer-make-and-model") ||
1478 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1479 !strcmp(attr
->name
, "printer-state") ||
1480 !strcmp(attr
->name
, "printer-state-change-time") ||
1481 !strcmp(attr
->name
, "printer-type") ||
1482 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1483 !strcmp(attr
->name
, "printer-location") ||
1484 !strcmp(attr
->name
, "printer-state-reasons") ||
1485 !strcmp(attr
->name
, "printer-uri-supported"))
1488 * Add a printer description attribute...
1491 num_options
= cupsAddOption(attr
->name
,
1492 cups_make_string(attr
, value
,
1494 num_options
, &options
);
1497 else if (!strcmp(attr
->name
, "media-supported") && media_default
[0])
1500 * See if we can set a default media size...
1503 int i
; /* Looping var */
1505 for (i
= 0; i
< attr
->num_values
; i
++)
1506 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1508 DEBUG_printf(("1_cupsGetDests: Setting media to '%s'.", media_default
));
1509 num_options
= cupsAddOption("media", media_default
, num_options
, &options
);
1513 #endif /* __APPLE__ */
1514 else if (!strcmp(attr
->name
, "printer-name") &&
1515 attr
->value_tag
== IPP_TAG_NAME
)
1516 printer_name
= attr
->values
[0].string
.text
;
1517 else if (strncmp(attr
->name
, "notify-", 7) &&
1518 strncmp(attr
->name
, "print-quality-", 14) &&
1519 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1520 attr
->value_tag
== IPP_TAG_ENUM
||
1521 attr
->value_tag
== IPP_TAG_INTEGER
||
1522 attr
->value_tag
== IPP_TAG_KEYWORD
||
1523 attr
->value_tag
== IPP_TAG_NAME
||
1524 attr
->value_tag
== IPP_TAG_RANGE
) &&
1525 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1528 * Add a default option...
1531 strlcpy(optname
, attr
->name
, sizeof(optname
));
1532 optname
[ptr
- attr
->name
] = '\0';
1534 if (_cups_strcasecmp(optname
, "media") || !cupsGetOption("media", num_options
, options
))
1535 num_options
= cupsAddOption(optname
, cups_make_string(attr
, value
, sizeof(value
)), num_options
, &options
);
1540 * See if we have everything needed...
1545 cupsFreeOptions(num_options
, options
);
1553 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1555 dest
->num_options
= num_options
;
1556 dest
->options
= options
;
1559 cupsFreeOptions(num_options
, options
);
1565 ippDelete(response
);
1569 * Return the count...
1577 * 'cupsGetDests()' - Get the list of destinations from the default server.
1579 * Starting with CUPS 1.2, the returned list of destinations include the
1580 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1581 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1582 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1583 * attributes as options.
1585 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1586 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1587 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1589 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1590 * be used. The "printer-uri-supported" option will be present for those IPP
1591 * printers that have been recently used.
1593 * Use the @link cupsFreeDests@ function to free the destination list and
1594 * the @link cupsGetDest@ function to find a particular destination.
1599 int /* O - Number of destinations */
1600 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1602 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1607 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1609 * Starting with CUPS 1.2, the returned list of destinations include the
1610 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1611 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1612 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1613 * attributes as options.
1615 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1616 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1617 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1619 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1620 * be used. The "printer-uri-supported" option will be present for those IPP
1621 * printers that have been recently used.
1623 * Use the @link cupsFreeDests@ function to free the destination list and
1624 * the @link cupsGetDest@ function to find a particular destination.
1626 * @since CUPS 1.1.21/macOS 10.4@
1629 int /* O - Number of destinations */
1630 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1631 cups_dest_t
**dests
) /* O - Destinations */
1633 _cups_getdata_t data
; /* Enumeration data */
1634 cups_dest_t
*dest
; /* Current destination */
1635 const char *home
; /* HOME environment variable */
1636 char filename
[1024]; /* Local ~/.cups/lpoptions file */
1637 const char *defprinter
; /* Default printer */
1638 char name
[1024], /* Copy of printer name */
1639 *instance
, /* Pointer to instance name */
1640 *user_default
; /* User default printer */
1641 int num_reals
; /* Number of real queues */
1642 cups_dest_t
*reals
; /* Real queues */
1643 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1646 DEBUG_printf(("cupsGetDests2(http=%p, dests=%p)", (void *)http
, (void *)dests
));
1649 * Range check the input...
1654 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
1655 DEBUG_puts("1cupsGetDests2: NULL dests pointer, returning 0.");
1660 * Grab the printers and classes...
1666 cups_enum_dests(http
, 0, _CUPS_DNSSD_GET_DESTS
, NULL
, 0, 0, (cups_dest_cb_t
)cups_get_cb
, &data
);
1669 * Make a copy of the "real" queues for a later sanity check...
1672 if (data
.num_dests
> 0)
1674 num_reals
= data
.num_dests
;
1675 reals
= calloc((size_t)num_reals
, sizeof(cups_dest_t
));
1678 memcpy(reals
, data
.dests
, (size_t)num_reals
* sizeof(cups_dest_t
));
1689 * Grab the default destination...
1692 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1694 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
1696 strlcpy(name
, defprinter
, sizeof(name
));
1703 * Separate printer and instance name...
1706 if ((instance
= strchr(name
, '/')) != NULL
)
1710 * Lookup the printer and instance and make it the default...
1713 if ((dest
= cupsGetDest(name
, instance
, data
.num_dests
, data
.dests
)) != NULL
)
1714 dest
->is_default
= 1;
1720 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
1723 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1724 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
1726 if ((home
= getenv("HOME")) != NULL
)
1728 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1730 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
1734 * Validate the current default destination - this prevents old
1735 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
1736 * pointing to a non-existent printer or class...
1742 * See if we have a default printer...
1745 if ((dest
= cupsGetDest(NULL
, NULL
, data
.num_dests
, data
.dests
)) != NULL
)
1748 * Have a default; see if it is real...
1751 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
1754 * Remove the non-real printer from the list, since we don't want jobs
1755 * going to an unexpected printer... (<rdar://problem/14216472>)
1758 data
.num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
.num_dests
, &data
.dests
);
1770 * Return the number of destinations...
1773 *dests
= data
.dests
;
1775 if (data
.num_dests
> 0)
1776 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
1778 DEBUG_printf(("1cupsGetDests2: Returning %d destinations.", data
.num_dests
));
1780 return (data
.num_dests
);
1785 * 'cupsGetNamedDest()' - Get options for the named destination.
1787 * This function is optimized for retrieving a single destination and should
1788 * be used instead of @link cupsGetDests2@ and @link cupsGetDest@ when you
1789 * either know the name of the destination or want to print to the default
1790 * destination. If @code NULL@ is returned, the destination does not exist or
1791 * there is no default destination.
1793 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
1794 * server will be used.
1796 * If "name" is @code NULL@, the default printer for the current user will be
1799 * The returned destination must be freed using @link cupsFreeDests@ with a
1800 * "num_dests" value of 1.
1802 * @since CUPS 1.4/macOS 10.6@
1805 cups_dest_t
* /* O - Destination or @code NULL@ */
1806 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1807 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1808 const char *instance
) /* I - Instance name or @code NULL@ */
1810 const char *dest_name
; /* Working destination name */
1811 cups_dest_t
*dest
; /* Destination */
1812 char filename
[1024], /* Path to lpoptions */
1813 defname
[256]; /* Default printer name */
1814 const char *home
= getenv("HOME"); /* Home directory */
1815 int set_as_default
= 0; /* Set returned destination as default */
1816 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
1817 /* IPP operation to get server ops */
1818 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1821 DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", (void *)http
, name
, instance
));
1824 * If "name" is NULL, find the default destination...
1832 dest_name
= _cupsUserDefault(defname
, sizeof(defname
));
1836 char *ptr
; /* Temporary pointer... */
1838 if ((ptr
= strchr(defname
, '/')) != NULL
)
1849 * No default in the environment, try the user's lpoptions files...
1852 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1854 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1860 * Still not there? Try the system lpoptions file...
1863 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1864 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1870 * No locally-set default destination, ask the server...
1873 op
= IPP_OP_CUPS_GET_DEFAULT
;
1875 DEBUG_puts("1cupsGetNamedDest: Asking server for default printer...");
1878 DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name
));
1882 * Get the printer's attributes...
1885 if (!_cupsGetDests(http
, op
, dest_name
, &dest
, 0, 0))
1889 _cups_namedata_t data
; /* Callback data */
1891 DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network...");
1896 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_name_cb
, &data
);
1907 DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", (void *)dest
));
1910 dest
->instance
= _cupsStrAlloc(instance
);
1913 dest
->is_default
= 1;
1916 * Then add local options...
1919 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1920 cups_get_dests(filename
, dest_name
, instance
, 1, 1, &dest
);
1924 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1926 cups_get_dests(filename
, dest_name
, instance
, 1, 1, &dest
);
1930 * Return the result...
1938 * 'cupsRemoveDest()' - Remove a destination from the destination list.
1940 * Removing a destination/instance does not delete the class or printer
1941 * queue, merely the lpoptions for that destination/instance. Use the
1942 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
1943 * options for the user.
1945 * @since CUPS 1.3/macOS 10.5@
1948 int /* O - New number of destinations */
1949 cupsRemoveDest(const char *name
, /* I - Destination name */
1950 const char *instance
, /* I - Instance name or @code NULL@ */
1951 int num_dests
, /* I - Number of destinations */
1952 cups_dest_t
**dests
) /* IO - Destinations */
1954 int i
; /* Index into destinations */
1955 cups_dest_t
*dest
; /* Pointer to destination */
1959 * Find the destination...
1962 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1969 _cupsStrFree(dest
->name
);
1970 _cupsStrFree(dest
->instance
);
1971 cupsFreeOptions(dest
->num_options
, dest
->options
);
1974 * Remove the destination from the array...
1979 i
= (int)(dest
- *dests
);
1982 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
1989 * 'cupsSetDefaultDest()' - Set the default destination.
1991 * @since CUPS 1.3/macOS 10.5@
1996 const char *name
, /* I - Destination name */
1997 const char *instance
, /* I - Instance name or @code NULL@ */
1998 int num_dests
, /* I - Number of destinations */
1999 cups_dest_t
*dests
) /* I - Destinations */
2001 int i
; /* Looping var */
2002 cups_dest_t
*dest
; /* Current destination */
2006 * Range check input...
2009 if (!name
|| num_dests
<= 0 || !dests
)
2013 * Loop through the array and set the "is_default" flag for the matching
2017 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2018 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2019 ((!instance
&& !dest
->instance
) ||
2020 (instance
&& dest
->instance
&&
2021 !_cups_strcasecmp(instance
, dest
->instance
)));
2026 * 'cupsSetDests()' - Save the list of destinations for the default server.
2028 * This function saves the destinations to /etc/cups/lpoptions when run
2029 * as root and ~/.cups/lpoptions when run as a normal user.
2035 cupsSetDests(int num_dests
, /* I - Number of destinations */
2036 cups_dest_t
*dests
) /* I - Destinations */
2038 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2043 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2045 * This function saves the destinations to /etc/cups/lpoptions when run
2046 * as root and ~/.cups/lpoptions when run as a normal user.
2048 * @since CUPS 1.1.21/macOS 10.4@
2051 int /* O - 0 on success, -1 on error */
2052 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2053 int num_dests
, /* I - Number of destinations */
2054 cups_dest_t
*dests
) /* I - Destinations */
2056 int i
, j
; /* Looping vars */
2057 int wrote
; /* Wrote definition? */
2058 cups_dest_t
*dest
; /* Current destination */
2059 cups_option_t
*option
; /* Current option */
2060 _ipp_option_t
*match
; /* Matching attribute for option */
2061 FILE *fp
; /* File pointer */
2063 const char *home
; /* HOME environment variable */
2065 char filename
[1024]; /* lpoptions file */
2066 int num_temps
; /* Number of temporary destinations */
2067 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2068 *temp
; /* Current temporary dest */
2069 const char *val
; /* Value of temporary option */
2070 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2074 * Range check the input...
2077 if (!num_dests
|| !dests
)
2081 * Get the server destinations...
2084 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2086 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2088 cupsFreeDests(num_temps
, temps
);
2093 * Figure out which file to write to...
2096 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2102 * Merge in server defaults...
2105 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2108 * Point to user defaults...
2111 if ((home
= getenv("HOME")) != NULL
)
2114 * Create ~/.cups subdirectory...
2117 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2118 if (access(filename
, 0))
2119 mkdir(filename
, 0700);
2121 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2127 * Try to open the file...
2130 if ((fp
= fopen(filename
, "w")) == NULL
)
2132 cupsFreeDests(num_temps
, temps
);
2138 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2143 fchmod(fileno(fp
), 0644);
2147 * Write each printer; each line looks like:
2149 * Dest name[/instance] options
2150 * Default name[/instance] options
2153 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2154 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2156 if (dest
->is_default
)
2158 fprintf(fp
, "Default %s", dest
->name
);
2160 fprintf(fp
, "/%s", dest
->instance
);
2167 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2168 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2170 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2173 * See if this option is a printer attribute; if so, skip it...
2176 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2177 match
->group_tag
== IPP_TAG_PRINTER
)
2181 * See if the server/global options match these; if so, don't
2186 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2187 temp
->options
)) != NULL
&&
2188 !_cups_strcasecmp(val
, option
->value
))
2192 * Options don't match, write to the file...
2197 fprintf(fp
, "Dest %s", dest
->name
);
2199 fprintf(fp
, "/%s", dest
->instance
);
2203 if (option
->value
[0])
2205 if (strchr(option
->value
, ' ') ||
2206 strchr(option
->value
, '\\') ||
2207 strchr(option
->value
, '\"') ||
2208 strchr(option
->value
, '\''))
2211 * Quote the value...
2214 fprintf(fp
, " %s=\"", option
->name
);
2216 for (val
= option
->value
; *val
; val
++)
2218 if (strchr("\"\'\\", *val
))
2229 * Store the literal value...
2232 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2236 fprintf(fp
, " %s", option
->name
);
2244 * Free the temporary destinations and close the file...
2247 cupsFreeDests(num_temps
, temps
);
2253 * Set the default printer for this location - this allows command-line
2254 * and GUI applications to share the same default destination...
2257 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2259 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2261 kCFStringEncodingUTF8
);
2262 /* Default printer name */
2266 _cupsAppleSetDefaultPrinter(name
);
2270 #endif /* __APPLE__ */
2272 #ifdef HAVE_NOTIFY_POST
2274 * Send a notification so that macOS applications can know about the
2278 notify_post("com.apple.printerListChange");
2279 #endif /* HAVE_NOTIFY_POST */
2286 * '_cupsUserDefault()' - Get the user default printer from environment
2287 * variables and location information.
2290 char * /* O - Default printer or NULL */
2291 _cupsUserDefault(char *name
, /* I - Name buffer */
2292 size_t namesize
) /* I - Size of name buffer */
2294 const char *env
; /* LPDEST or PRINTER env variable */
2296 CFStringRef locprinter
; /* Last printer as this location */
2297 #endif /* __APPLE__ */
2300 if ((env
= getenv("LPDEST")) == NULL
)
2301 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2306 strlcpy(name
, env
, namesize
);
2312 * Use location-based defaults if "use last printer" is selected in the
2313 * system preferences...
2316 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2318 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2319 CFRelease(locprinter
);
2324 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2326 return (*name
? name
: NULL
);
2330 * No location-based defaults on this platform...
2335 #endif /* __APPLE__ */
2339 #if _CUPS_LOCATION_DEFAULTS
2341 * 'appleCopyLocations()' - Copy the location history array.
2344 static CFArrayRef
/* O - Location array or NULL */
2345 appleCopyLocations(void)
2347 CFArrayRef locations
; /* Location array */
2351 * Look up the location array in the preferences...
2354 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2355 kCUPSPrintingPrefs
)) == NULL
)
2358 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2360 CFRelease(locations
);
2369 * 'appleCopyNetwork()' - Get the network ID for the current location.
2372 static CFStringRef
/* O - Network ID */
2373 appleCopyNetwork(void)
2375 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2376 CFStringRef key
; /* Current network configuration key */
2377 CFDictionaryRef ip_dict
; /* Network configuration data */
2378 CFStringRef network
= NULL
; /* Current network ID */
2381 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2385 * First use the IPv6 router address, if available, since that will generally
2386 * be a globally-unique link-local address.
2389 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2390 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2392 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2394 if ((network
= CFDictionaryGetValue(ip_dict
,
2395 kSCPropNetIPv6Router
)) != NULL
)
2405 * If that doesn't work, try the IPv4 router address. This isn't as unique
2406 * and will likely be a 10.x.y.z or 192.168.y.z address...
2411 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2412 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2414 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2416 if ((network
= CFDictionaryGetValue(ip_dict
,
2417 kSCPropNetIPv4Router
)) != NULL
)
2427 CFRelease(dynamicStore
);
2432 #endif /* _CUPS_LOCATION_DEFAULTS */
2437 * 'appleGetPaperSize()' - Get the default paper size.
2440 static char * /* O - Default paper size */
2441 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2442 size_t namesize
) /* I - Size of buffer */
2444 CFStringRef defaultPaperID
; /* Default paper ID */
2445 pwg_media_t
*pwgmedia
; /* PWG media size */
2448 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2449 if (!defaultPaperID
||
2450 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2451 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2453 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2454 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2457 CFRelease(defaultPaperID
);
2461 #endif /* __APPLE__ */
2464 #if _CUPS_LOCATION_DEFAULTS
2466 * 'appleGetPrinter()' - Get a printer from the history array.
2469 static CFStringRef
/* O - Printer name or NULL */
2470 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2471 CFStringRef network
, /* I - Network name */
2472 CFIndex
*locindex
) /* O - Index in array */
2474 CFIndex i
, /* Looping var */
2475 count
; /* Number of locations */
2476 CFDictionaryRef location
; /* Current location */
2477 CFStringRef locnetwork
, /* Current network */
2478 locprinter
; /* Current printer */
2481 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2482 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2483 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2485 if ((locnetwork
= CFDictionaryGetValue(location
,
2486 kLocationNetworkKey
)) != NULL
&&
2487 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2488 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2489 (locprinter
= CFDictionaryGetValue(location
,
2490 kLocationPrinterIDKey
)) != NULL
&&
2491 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2496 return (locprinter
);
2502 #endif /* _CUPS_LOCATION_DEFAULTS */
2506 * 'cups_add_dest()' - Add a destination to the array.
2508 * Unlike cupsAddDest(), this function does not check for duplicates.
2511 static cups_dest_t
* /* O - New destination */
2512 cups_add_dest(const char *name
, /* I - Name of destination */
2513 const char *instance
, /* I - Instance or NULL */
2514 int *num_dests
, /* IO - Number of destinations */
2515 cups_dest_t
**dests
) /* IO - Destinations */
2517 int insert
, /* Insertion point */
2518 diff
; /* Result of comparison */
2519 cups_dest_t
*dest
; /* Destination pointer */
2523 * Add new destination...
2526 if (*num_dests
== 0)
2527 dest
= malloc(sizeof(cups_dest_t
));
2529 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2537 * Find where to insert the destination...
2540 if (*num_dests
== 0)
2544 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2552 * Move the array elements as needed...
2555 if (insert
< *num_dests
)
2556 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2561 * Initialize the destination...
2564 dest
= *dests
+ insert
;
2565 dest
->name
= _cupsStrAlloc(name
);
2566 dest
->instance
= _cupsStrAlloc(instance
);
2567 dest
->is_default
= 0;
2568 dest
->num_options
= 0;
2569 dest
->options
= (cups_option_t
*)0;
2577 * 'cups_block_cb()' - Enumeration callback for block API.
2580 static int /* O - 1 to continue, 0 to stop */
2582 cups_dest_block_t block
, /* I - Block */
2583 unsigned flags
, /* I - Destination flags */
2584 cups_dest_t
*dest
) /* I - Destination */
2586 return ((block
)(flags
, dest
));
2588 # endif /* __BLOCKS__ */
2592 * 'cups_compare_dests()' - Compare two destinations.
2595 static int /* O - Result of comparison */
2596 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2597 cups_dest_t
*b
) /* I - Second destination */
2599 int diff
; /* Difference */
2602 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2604 else if (a
->instance
&& b
->instance
)
2605 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2607 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2611 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2614 * 'cups_dnssd_browse_cb()' - Browse for printers.
2618 cups_dnssd_browse_cb(
2619 DNSServiceRef sdRef
, /* I - Service reference */
2620 DNSServiceFlags flags
, /* I - Option flags */
2621 uint32_t interfaceIndex
, /* I - Interface number */
2622 DNSServiceErrorType errorCode
, /* I - Error, if any */
2623 const char *serviceName
, /* I - Name of service/device */
2624 const char *regtype
, /* I - Type of service */
2625 const char *replyDomain
, /* I - Service domain */
2626 void *context
) /* I - Enumeration data */
2628 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2629 /* Enumeration data */
2632 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
));
2635 * Don't do anything on error...
2638 if (errorCode
!= kDNSServiceErr_NoError
)
2645 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2649 # else /* HAVE_AVAHI */
2651 * 'cups_dnssd_browse_cb()' - Browse for printers.
2655 cups_dnssd_browse_cb(
2656 AvahiServiceBrowser
*browser
, /* I - Browser */
2657 AvahiIfIndex interface
, /* I - Interface index (unused) */
2658 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2659 AvahiBrowserEvent event
, /* I - What happened */
2660 const char *name
, /* I - Service name */
2661 const char *type
, /* I - Registration type */
2662 const char *domain
, /* I - Domain */
2663 AvahiLookupResultFlags flags
, /* I - Flags */
2664 void *context
) /* I - Devices array */
2667 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
2668 /* Client information */
2670 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2671 /* Enumeration data */
2678 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
2682 case AVAHI_BROWSER_FAILURE
:
2683 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
2684 avahi_simple_poll_quit(data
->simple_poll
);
2687 case AVAHI_BROWSER_NEW
:
2689 * This object is new on the network.
2692 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
2695 * This comes from the local machine so ignore it.
2698 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", name
));
2703 * Create a device entry for it if it doesn't yet exist.
2706 cups_dnssd_get_device(data
, name
, type
, domain
);
2710 case AVAHI_BROWSER_REMOVE
:
2711 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
2714 case AVAHI_BROWSER_ALL_FOR_NOW
:
2715 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
2723 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2727 cups_dnssd_client_cb(
2728 AvahiClient
*client
, /* I - Client information (unused) */
2729 AvahiClientState state
, /* I - Current state */
2730 void *context
) /* I - User data (unused) */
2732 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2733 /* Enumeration data */
2738 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
2741 * If the connection drops, quit.
2744 if (state
== AVAHI_CLIENT_FAILURE
)
2746 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2747 avahi_simple_poll_quit(data
->simple_poll
);
2750 # endif /* HAVE_DNSSD */
2754 * 'cups_dnssd_compare_device()' - Compare two devices.
2757 static int /* O - Result of comparison */
2758 cups_dnssd_compare_devices(
2759 _cups_dnssd_device_t
*a
, /* I - First device */
2760 _cups_dnssd_device_t
*b
) /* I - Second device */
2762 return (strcmp(a
->dest
.name
, b
->dest
.name
));
2767 * 'cups_dnssd_free_device()' - Free the memory used by a device.
2771 cups_dnssd_free_device(
2772 _cups_dnssd_device_t
*device
, /* I - Device */
2773 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
2775 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
2779 DNSServiceRefDeallocate(device
->ref
);
2780 # else /* HAVE_AVAHI */
2782 avahi_record_browser_free(device
->ref
);
2783 # endif /* HAVE_DNSSD */
2785 _cupsStrFree(device
->domain
);
2786 _cupsStrFree(device
->fullName
);
2787 _cupsStrFree(device
->regtype
);
2788 _cupsStrFree(device
->dest
.name
);
2790 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
2797 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
2800 static _cups_dnssd_device_t
* /* O - Device */
2801 cups_dnssd_get_device(
2802 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
2803 const char *serviceName
, /* I - Service name */
2804 const char *regtype
, /* I - Registration type */
2805 const char *replyDomain
) /* I - Domain name */
2807 _cups_dnssd_device_t key
, /* Search key */
2808 *device
; /* Device */
2809 char fullName
[kDNSServiceMaxDomainName
],
2810 /* Full name for query */
2811 name
[128]; /* Queue name */
2814 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
2817 * See if this is an existing device...
2820 cups_queue_name(name
, serviceName
, sizeof(name
));
2822 key
.dest
.name
= name
;
2824 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
2827 * Yes, see if we need to do anything with this...
2830 int update
= 0; /* Non-zero if we need to update */
2832 if (!_cups_strcasecmp(replyDomain
, "local.") &&
2833 _cups_strcasecmp(device
->domain
, replyDomain
))
2836 * Update the "global" listing to use the .local domain name instead.
2839 _cupsStrFree(device
->domain
);
2840 device
->domain
= _cupsStrAlloc(replyDomain
);
2842 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
2843 "domain.", device
->dest
.name
));
2848 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
2849 _cups_strcasecmp(device
->regtype
, regtype
))
2852 * Prefer IPPS over IPP.
2855 _cupsStrFree(device
->regtype
);
2856 device
->regtype
= _cupsStrAlloc(regtype
);
2858 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
2859 device
->dest
.name
));
2866 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
2867 device
->dest
.name
));
2874 * No, add the device...
2877 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
2878 "'%s'.", serviceName
,
2879 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
2882 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
2883 device
->dest
.name
= _cupsStrAlloc(name
);
2884 device
->domain
= _cupsStrAlloc(replyDomain
);
2885 device
->regtype
= _cupsStrAlloc(regtype
);
2887 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
2889 cupsArrayAdd(data
->devices
, device
);
2893 * Set the "full name" of this service, which is used for queries...
2897 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
2898 # else /* HAVE_AVAHI */
2899 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
2900 # endif /* HAVE_DNSSD */
2902 _cupsStrFree(device
->fullName
);
2903 device
->fullName
= _cupsStrAlloc(fullName
);
2908 DNSServiceRefDeallocate(device
->ref
);
2909 # else /* HAVE_AVAHI */
2910 avahi_record_browser_free(device
->ref
);
2911 # endif /* HAVE_DNSSD */
2916 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
2918 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
2920 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
2921 device
->state
= _CUPS_DNSSD_NEW
;
2930 * 'cups_dnssd_local_cb()' - Browse for local printers.
2934 cups_dnssd_local_cb(
2935 DNSServiceRef sdRef
, /* I - Service reference */
2936 DNSServiceFlags flags
, /* I - Option flags */
2937 uint32_t interfaceIndex
, /* I - Interface number */
2938 DNSServiceErrorType errorCode
, /* I - Error, if any */
2939 const char *serviceName
, /* I - Name of service/device */
2940 const char *regtype
, /* I - Type of service */
2941 const char *replyDomain
, /* I - Service domain */
2942 void *context
) /* I - Devices array */
2944 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2945 /* Enumeration data */
2946 _cups_dnssd_device_t
*device
; /* Device */
2949 DEBUG_printf(("5cups_dnssd_local_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
));
2952 * Only process "add" data...
2955 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
2962 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2965 * Hide locally-registered devices...
2968 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
2973 DNSServiceRefDeallocate(device
->ref
);
2977 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
2979 DEBUG_printf(("6cups_dnssd_local_cb: Remove callback for \"%s\".", device
->dest
.name
));
2980 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
2983 device
->state
= _CUPS_DNSSD_LOCAL
;
2985 # endif /* HAVE_DNSSD */
2990 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
2992 * Note: This function is needed because avahi_simple_poll_iterate is broken
2993 * and always uses a timeout of 0 (!) milliseconds.
2994 * (https://github.com/lathiat/avahi/issues/127)
2999 static int /* O - Number of file descriptors matching */
3001 struct pollfd
*pollfds
, /* I - File descriptors */
3002 unsigned int num_pollfds
, /* I - Number of file descriptors */
3003 int timeout
, /* I - Timeout in milliseconds (unused) */
3004 void *context
) /* I - User data (unused) */
3006 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3007 /* Enumeration data */
3008 int val
; /* Return value */
3011 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
3015 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
3017 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
3021 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3030 # endif /* HAVE_AVAHI */
3034 * 'cups_dnssd_query_cb()' - Process query data.
3039 cups_dnssd_query_cb(
3040 DNSServiceRef sdRef
, /* I - Service reference */
3041 DNSServiceFlags flags
, /* I - Data flags */
3042 uint32_t interfaceIndex
, /* I - Interface */
3043 DNSServiceErrorType errorCode
, /* I - Error, if any */
3044 const char *fullName
, /* I - Full service name */
3045 uint16_t rrtype
, /* I - Record type */
3046 uint16_t rrclass
, /* I - Record class */
3047 uint16_t rdlen
, /* I - Length of record data */
3048 const void *rdata
, /* I - Record data */
3049 uint32_t ttl
, /* I - Time-to-live */
3050 void *context
) /* I - Enumeration data */
3052 # else /* HAVE_AVAHI */
3054 cups_dnssd_query_cb(
3055 AvahiRecordBrowser
*browser
, /* I - Record browser */
3056 AvahiIfIndex interfaceIndex
,
3057 /* I - Interface index (unused) */
3058 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3059 AvahiBrowserEvent event
, /* I - What happened? */
3060 const char *fullName
, /* I - Service name */
3061 uint16_t rrclass
, /* I - Record class */
3062 uint16_t rrtype
, /* I - Record type */
3063 const void *rdata
, /* I - TXT record */
3064 size_t rdlen
, /* I - Length of TXT record */
3065 AvahiLookupResultFlags flags
, /* I - Flags */
3066 void *context
) /* I - Enumeration data */
3069 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3070 /* Client information */
3072 # endif /* HAVE_DNSSD */
3073 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3074 /* Enumeration data */
3075 char serviceName
[256],/* Service name */
3076 name
[128], /* Queue name */
3077 *ptr
; /* Pointer into string */
3078 _cups_dnssd_device_t dkey
, /* Search key */
3079 *device
; /* Device */
3083 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
));
3086 * Only process "add" data...
3089 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3092 # else /* HAVE_AVAHI */
3093 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
));
3096 * Only process "add" data...
3099 if (event
!= AVAHI_BROWSER_NEW
)
3101 if (event
== AVAHI_BROWSER_FAILURE
)
3102 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3106 # endif /* HAVE_DNSSD */
3109 * Lookup the service in the devices array.
3112 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
3114 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
3117 cups_queue_name(name
, serviceName
, sizeof(name
));
3119 dkey
.dest
.name
= name
;
3121 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
3124 * Found it, pull out the make and model from the TXT record and save it...
3127 const uint8_t *txt
, /* Pointer into data */
3128 *txtnext
, /* Next key/value pair */
3129 *txtend
; /* End of entire TXT record */
3130 uint8_t txtlen
; /* Length of current key/value pair */
3131 char key
[256], /* Key string */
3132 value
[256], /* Value string */
3133 make_and_model
[512],
3134 /* Manufacturer and model */
3135 model
[256], /* Model */
3136 uriname
[1024], /* Name for URI */
3137 uri
[1024]; /* Printer URI */
3138 cups_ptype_t type
= CUPS_PRINTER_DISCOVERED
| CUPS_PRINTER_BW
;
3140 int saw_printer_type
= 0;
3141 /* Did we see a printer-type key? */
3143 device
->state
= _CUPS_DNSSD_PENDING
;
3144 make_and_model
[0] = '\0';
3146 strlcpy(model
, "Unknown", sizeof(model
));
3148 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3153 * Read a key/value pair starting with an 8-bit length. Since the
3154 * length is 8 bits and the size of the key/value buffers is 256, we
3155 * don't need to check for overflow...
3160 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3163 txtnext
= txt
+ txtlen
;
3165 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3166 *ptr
++ = (char)*txt
;
3169 if (txt
< txtnext
&& *txt
== '=')
3174 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3175 value
[txtnext
- txt
] = '\0';
3177 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3181 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3185 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3186 !_cups_strcasecmp(key
, "usb_MANU") ||
3187 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3188 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3189 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3190 !_cups_strcasecmp(key
, "usb_MODEL"))
3191 strlcpy(model
, value
, sizeof(model
));
3192 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3194 if (value
[0] == '(')
3197 * Strip parenthesis...
3200 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3203 strlcpy(model
, value
+ 1, sizeof(model
));
3206 strlcpy(model
, value
, sizeof(model
));
3208 else if (!_cups_strcasecmp(key
, "ty"))
3210 strlcpy(model
, value
, sizeof(model
));
3212 if ((ptr
= strchr(model
, ',')) != NULL
)
3215 else if (!_cups_strcasecmp(key
, "note"))
3216 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3217 device
->dest
.num_options
,
3218 &device
->dest
.options
);
3219 else if (!_cups_strcasecmp(key
, "pdl"))
3222 * Look for PDF-capable printers; only PDF-capable printers are shown.
3225 const char *start
, *next
; /* Pointer into value */
3226 int have_pdf
= 0, /* Have PDF? */
3227 have_raster
= 0;/* Have raster format support? */
3229 for (start
= value
; start
&& *start
; start
= next
)
3231 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3236 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3237 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3243 if ((next
= strchr(start
, ',')) != NULL
)
3247 if (!have_pdf
&& !have_raster
)
3248 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3250 else if (!_cups_strcasecmp(key
, "printer-type"))
3253 * Value is either NNNN or 0xXXXX
3256 saw_printer_type
= 1;
3257 type
= (cups_ptype_t
)strtol(value
, NULL
, 0) | CUPS_PRINTER_DISCOVERED
;
3259 else if (!saw_printer_type
)
3261 if (!_cups_strcasecmp(key
, "air") &&
3262 !_cups_strcasecmp(value
, "t"))
3263 type
|= CUPS_PRINTER_AUTHENTICATED
;
3264 else if (!_cups_strcasecmp(key
, "bind") &&
3265 !_cups_strcasecmp(value
, "t"))
3266 type
|= CUPS_PRINTER_BIND
;
3267 else if (!_cups_strcasecmp(key
, "collate") &&
3268 !_cups_strcasecmp(value
, "t"))
3269 type
|= CUPS_PRINTER_COLLATE
;
3270 else if (!_cups_strcasecmp(key
, "color") &&
3271 !_cups_strcasecmp(value
, "t"))
3272 type
|= CUPS_PRINTER_COLOR
;
3273 else if (!_cups_strcasecmp(key
, "copies") &&
3274 !_cups_strcasecmp(value
, "t"))
3275 type
|= CUPS_PRINTER_COPIES
;
3276 else if (!_cups_strcasecmp(key
, "duplex") &&
3277 !_cups_strcasecmp(value
, "t"))
3278 type
|= CUPS_PRINTER_DUPLEX
;
3279 else if (!_cups_strcasecmp(key
, "fax") &&
3280 !_cups_strcasecmp(value
, "t"))
3281 type
|= CUPS_PRINTER_MFP
;
3282 else if (!_cups_strcasecmp(key
, "papercustom") &&
3283 !_cups_strcasecmp(value
, "t"))
3284 type
|= CUPS_PRINTER_VARIABLE
;
3285 else if (!_cups_strcasecmp(key
, "papermax"))
3287 if (!_cups_strcasecmp(value
, "legal-a4"))
3288 type
|= CUPS_PRINTER_SMALL
;
3289 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3290 type
|= CUPS_PRINTER_MEDIUM
;
3291 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3292 type
|= CUPS_PRINTER_LARGE
;
3294 else if (!_cups_strcasecmp(key
, "punch") &&
3295 !_cups_strcasecmp(value
, "t"))
3296 type
|= CUPS_PRINTER_PUNCH
;
3297 else if (!_cups_strcasecmp(key
, "scan") &&
3298 !_cups_strcasecmp(value
, "t"))
3299 type
|= CUPS_PRINTER_MFP
;
3300 else if (!_cups_strcasecmp(key
, "sort") &&
3301 !_cups_strcasecmp(value
, "t"))
3302 type
|= CUPS_PRINTER_SORT
;
3303 else if (!_cups_strcasecmp(key
, "staple") &&
3304 !_cups_strcasecmp(value
, "t"))
3305 type
|= CUPS_PRINTER_STAPLE
;
3310 * Save the printer-xxx values...
3313 if (make_and_model
[0])
3315 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3316 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3318 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3321 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3323 device
->type
= type
;
3324 snprintf(value
, sizeof(value
), "%u", type
);
3325 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3331 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3332 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3333 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3334 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3336 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3338 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3341 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3347 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3350 static const char * /* O - Resolved URI or NULL */
3352 cups_dest_t
*dest
, /* I - Destination */
3353 const char *uri
, /* I - Current printer URI */
3354 int msec
, /* I - Time in milliseconds */
3355 int *cancel
, /* I - Pointer to "cancel" variable */
3356 cups_dest_cb_t cb
, /* I - Callback */
3357 void *user_data
) /* I - User data for callback */
3359 char tempuri
[1024]; /* Temporary URI buffer */
3360 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3364 * Resolve the URI...
3367 resolve
.cancel
= cancel
;
3368 gettimeofday(&resolve
.end_time
, NULL
);
3371 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3372 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3374 while (resolve
.end_time
.tv_usec
>= 1000000)
3376 resolve
.end_time
.tv_sec
++;
3377 resolve
.end_time
.tv_usec
-= 1000000;
3381 resolve
.end_time
.tv_sec
+= 75;
3384 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3386 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3388 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3391 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3397 * Save the resolved URI...
3400 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &dest
->options
);
3402 return (cupsGetOption("device-uri", dest
->num_options
, dest
->options
));
3407 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3410 static int /* O - 1 to continue, 0 to stop */
3411 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3413 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3415 struct timeval curtime
; /* Current time */
3419 * If the cancel variable is set, return immediately.
3422 if (resolve
->cancel
&& *(resolve
->cancel
))
3424 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3429 * Otherwise check the end time...
3432 gettimeofday(&curtime
, NULL
);
3434 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
));
3436 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3437 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3438 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3443 * 'cups_dnssd_unquote()' - Unquote a name string.
3447 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3448 const char *src
, /* I - Source string */
3449 size_t dstsize
) /* I - Size of destination buffer */
3451 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3454 while (*src
&& dst
< dstend
)
3459 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3460 isdigit(src
[2] & 255))
3462 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3474 #endif /* HAVE_DNSSD */
3477 #if defined(HAVE_AVAHI) || defined(HAVE_DNSSD)
3479 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3482 static int /* O - Elapsed time in milliseconds */
3483 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3485 int msecs
; /* Milliseconds */
3486 struct timeval nt
; /* New time */
3489 gettimeofday(&nt
, NULL
);
3491 msecs
= (int)(1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000);
3497 #endif /* HAVE_AVAHI || HAVE_DNSSD */
3501 * 'cups_enum_dests()' - Enumerate destinations from a specific server.
3504 static int /* O - 1 on success, 0 on failure */
3506 http_t
*http
, /* I - Connection to scheduler */
3507 unsigned flags
, /* I - Enumeration flags */
3508 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
3509 int *cancel
, /* I - Pointer to "cancel" variable */
3510 cups_ptype_t type
, /* I - Printer type bits */
3511 cups_ptype_t mask
, /* I - Mask for printer type bits */
3512 cups_dest_cb_t cb
, /* I - Callback function */
3513 void *user_data
) /* I - User data */
3515 int i
, /* Looping var */
3516 num_dests
; /* Number of destinations */
3517 cups_dest_t
*dests
= NULL
, /* Destinations */
3518 *dest
; /* Current destination */
3519 const char *defprinter
; /* Default printer */
3520 char name
[1024], /* Copy of printer name */
3521 *instance
, /* Pointer to instance name */
3522 *user_default
; /* User default printer */
3523 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3524 int count
, /* Number of queries started */
3525 completed
, /* Number of completed queries */
3526 remaining
; /* Remainder of timeout */
3527 struct timeval curtime
; /* Current time */
3528 _cups_dnssd_data_t data
; /* Data for callback */
3529 _cups_dnssd_device_t
*device
; /* Current device */
3531 int nfds
, /* Number of files responded */
3532 main_fd
; /* File descriptor for lookups */
3533 DNSServiceRef ipp_ref
= NULL
, /* IPP browser */
3534 local_ipp_ref
= NULL
; /* Local IPP browser */
3536 DNSServiceRef ipps_ref
= NULL
, /* IPPS browser */
3537 local_ipps_ref
= NULL
; /* Local IPPS browser */
3538 # endif /* HAVE_SSL */
3540 struct pollfd pfd
; /* Polling data */
3542 fd_set input
; /* Input set for select() */
3543 struct timeval timeout
; /* Timeout for select() */
3544 # endif /* HAVE_POLL */
3545 # else /* HAVE_AVAHI */
3546 int error
; /* Error value */
3547 AvahiServiceBrowser
*ipp_ref
= NULL
; /* IPP browser */
3549 AvahiServiceBrowser
*ipps_ref
= NULL
; /* IPPS browser */
3550 # endif /* HAVE_SSL */
3551 # endif /* HAVE_DNSSD */
3552 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3555 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
));
3558 * Range check input...
3565 DEBUG_puts("1cups_enum_dests: No callback, returning 0.");
3570 * Get ready to enumerate...
3573 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3574 memset(&data
, 0, sizeof(data
));
3579 data
.user_data
= user_data
;
3580 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
3581 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3583 if (!(mask
& CUPS_PRINTER_DISCOVERED
) || !(type
& CUPS_PRINTER_DISCOVERED
))
3586 * Get the list of local printers and pass them to the callback function...
3589 num_dests
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &dests
, type
, mask
);
3591 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
3593 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
3595 strlcpy(name
, defprinter
, sizeof(name
));
3602 * Separate printer and instance name...
3605 if ((instance
= strchr(name
, '/')) != NULL
)
3609 * Lookup the printer and instance and make it the default...
3612 if ((dest
= cupsGetDest(name
, instance
, num_dests
, dests
)) != NULL
)
3613 dest
->is_default
= 1;
3616 for (i
= num_dests
, dest
= dests
;
3617 i
> 0 && (!cancel
|| !*cancel
);
3620 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3621 const char *device_uri
; /* Device URI */
3622 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3624 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
, dest
))
3627 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3628 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
3631 * Add existing queue using service name, etc. so we don't list it again...
3634 char scheme
[32], /* URI scheme */
3635 userpass
[32], /* Username:password */
3636 serviceName
[256], /* Service name (host field) */
3637 resource
[256], /* Resource (options) */
3638 *regtype
, /* Registration type */
3639 *replyDomain
; /* Registration domain */
3640 int port
; /* Port number (not used) */
3642 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
)
3644 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
3648 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
3650 replyDomain
[5] = '\0';
3653 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
3654 device
->state
= _CUPS_DNSSD_ACTIVE
;
3659 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3662 cupsFreeDests(num_dests
, dests
);
3664 if (i
> 0 || msec
== 0)
3669 * Return early if the caller doesn't want to do discovery...
3672 if ((mask
& CUPS_PRINTER_DISCOVERED
) && !(type
& CUPS_PRINTER_DISCOVERED
))
3675 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3677 * Get Bonjour-shared printers...
3680 gettimeofday(&curtime
, NULL
);
3683 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
3685 DEBUG_puts("1cups_enum_dests: Unable to create service browser, returning 0.");
3689 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
3691 ipp_ref
= data
.main_ref
;
3692 if (DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipp._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3694 DEBUG_puts("1cups_enum_dests: Unable to create IPP browser, returning 0.");
3695 DNSServiceRefDeallocate(data
.main_ref
);
3699 local_ipp_ref
= data
.main_ref
;
3700 if (DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
, kDNSServiceInterfaceIndexLocalOnly
, "_ipp._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
) != kDNSServiceErr_NoError
)
3702 DEBUG_puts("1cups_enum_dests: Unable to create local IPP browser, returning 0.");
3703 DNSServiceRefDeallocate(data
.main_ref
);
3708 ipps_ref
= data
.main_ref
;
3709 if (DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipps._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3711 DEBUG_puts("1cups_enum_dests: Unable to create IPPS browser, returning 0.");
3712 DNSServiceRefDeallocate(data
.main_ref
);
3716 local_ipps_ref
= data
.main_ref
;
3717 if (DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
, kDNSServiceInterfaceIndexLocalOnly
, "_ipps._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
) != kDNSServiceErr_NoError
)
3719 DEBUG_puts("1cups_enum_dests: Unable to create local IPPS browser, returning 0.");
3720 DNSServiceRefDeallocate(data
.main_ref
);
3723 # endif /* HAVE_SSL */
3725 # else /* HAVE_AVAHI */
3726 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
3728 DEBUG_puts("1cups_enum_dests: Unable to create Avahi poll, returning 0.");
3732 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
3734 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
3735 0, cups_dnssd_client_cb
, &data
,
3739 DEBUG_puts("1cups_enum_dests: Unable to create Avahi client, returning 0.");
3740 avahi_simple_poll_free(data
.simple_poll
);
3745 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
)
3747 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPP browser, returning 0.");
3749 avahi_client_free(data
.client
);
3750 avahi_simple_poll_free(data
.simple_poll
);
3756 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
)
3758 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPPS browser, returning 0.");
3760 avahi_service_browser_free(ipp_ref
);
3761 avahi_client_free(data
.client
);
3762 avahi_simple_poll_free(data
.simple_poll
);
3765 # endif /* HAVE_SSL */
3766 # endif /* HAVE_DNSSD */
3769 remaining
= INT_MAX
;
3773 while (remaining
> 0 && (!cancel
|| !*cancel
))
3776 * Check for input...
3779 DEBUG_printf(("1cups_enum_dests: remaining=%d", remaining
));
3781 cups_elapsed(&curtime
);
3786 pfd
.events
= POLLIN
;
3788 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3792 FD_SET(main_fd
, &input
);
3795 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3797 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
3798 # endif /* HAVE_POLL */
3801 DNSServiceProcessResult(data
.main_ref
);
3802 else if (nfds
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
3805 # else /* HAVE_AVAHI */
3808 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
3811 * We've been told to exit the loop. Perhaps the connection to
3818 DEBUG_printf(("1cups_enum_dests: got_data=%d", data
.got_data
));
3819 # endif /* HAVE_DNSSD */
3821 remaining
-= cups_elapsed(&curtime
);
3823 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
3824 count
= 0, completed
= 0;
3826 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
3831 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3834 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
3836 DEBUG_printf(("1cups_enum_dests: Querying '%s'.", device
->fullName
));
3839 device
->ref
= data
.main_ref
;
3841 if (DNSServiceQueryRecord(&(device
->ref
), kDNSServiceFlagsShareConnection
, 0, device
->fullName
, kDNSServiceType_TXT
, kDNSServiceClass_IN
, (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
, &data
) == kDNSServiceErr_NoError
)
3848 device
->state
= _CUPS_DNSSD_ERROR
;
3850 DEBUG_puts("1cups_enum_dests: Query failed.");
3853 # else /* HAVE_AVAHI */
3854 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
)
3856 DEBUG_printf(("1cups_enum_dests: Query ref=%p", device
->ref
));
3861 device
->state
= _CUPS_DNSSD_ERROR
;
3863 DEBUG_printf(("1cups_enum_dests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
3865 # endif /* HAVE_DNSSD */
3867 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
3871 DEBUG_printf(("1cups_enum_dests: Query for \"%s\" is complete.", device
->fullName
));
3873 if ((device
->type
& mask
) == type
)
3875 DEBUG_printf(("1cups_enum_dests: Add callback for \"%s\".", device
->dest
.name
));
3876 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
3883 device
->state
= _CUPS_DNSSD_ACTIVE
;
3888 DEBUG_printf(("1cups_enum_dests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
3890 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
3893 DEBUG_printf(("1cups_enum_dests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
3895 if (completed
== cupsArrayCount(data
.devices
))
3897 # endif /* HAVE_AVAHI */
3899 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3907 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3908 cupsArrayDelete(data
.devices
);
3912 DNSServiceRefDeallocate(ipp_ref
);
3914 DNSServiceRefDeallocate(local_ipp_ref
);
3918 DNSServiceRefDeallocate(ipps_ref
);
3920 DNSServiceRefDeallocate(local_ipps_ref
);
3921 # endif /* HAVE_SSL */
3924 DNSServiceRefDeallocate(data
.main_ref
);
3926 # else /* HAVE_AVAHI */
3928 avahi_service_browser_free(ipp_ref
);
3931 avahi_service_browser_free(ipps_ref
);
3932 # endif /* HAVE_SSL */
3935 avahi_client_free(data
.client
);
3936 if (data
.simple_poll
)
3937 avahi_simple_poll_free(data
.simple_poll
);
3938 # endif /* HAVE_DNSSD */
3939 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3941 DEBUG_puts("1cups_enum_dests: Returning 1.");
3948 * 'cups_find_dest()' - Find a destination using a binary search.
3951 static int /* O - Index of match */
3952 cups_find_dest(const char *name
, /* I - Destination name */
3953 const char *instance
, /* I - Instance or NULL */
3954 int num_dests
, /* I - Number of destinations */
3955 cups_dest_t
*dests
, /* I - Destinations */
3956 int prev
, /* I - Previous index */
3957 int *rdiff
) /* O - Difference of match */
3959 int left
, /* Low mark for binary search */
3960 right
, /* High mark for binary search */
3961 current
, /* Current index */
3962 diff
; /* Result of comparison */
3963 cups_dest_t key
; /* Search key */
3966 key
.name
= (char *)name
;
3967 key
.instance
= (char *)instance
;
3972 * Start search on either side of previous...
3975 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3976 (diff
< 0 && prev
== 0) ||
3977 (diff
> 0 && prev
== (num_dests
- 1)))
3985 * Start with previous on right side...
3994 * Start wih previous on left side...
3998 right
= num_dests
- 1;
4004 * Start search in the middle...
4008 right
= num_dests
- 1;
4013 current
= (left
+ right
) / 2;
4014 diff
= cups_compare_dests(&key
, dests
+ current
);
4023 while ((right
- left
) > 1);
4028 * Check the last 1 or 2 elements...
4031 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
4035 diff
= cups_compare_dests(&key
, dests
+ right
);
4041 * Return the closest destination and the difference...
4051 * 'cups_get_cb()' - Collect enumerated destinations.
4054 static int /* O - 1 to continue, 0 to stop */
4055 cups_get_cb(_cups_getdata_t
*data
, /* I - Data from cupsGetDests */
4056 unsigned flags
, /* I - Enumeration flags */
4057 cups_dest_t
*dest
) /* I - Destination */
4059 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
4062 * Remove destination from array...
4065 data
->num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
->num_dests
, &data
->dests
);
4070 * Add destination to array...
4073 data
->num_dests
= cupsCopyDest(dest
, data
->num_dests
, &data
->dests
);
4081 * 'cups_get_default()' - Get the default destination from an lpoptions file.
4084 static char * /* O - Default destination or NULL */
4085 cups_get_default(const char *filename
, /* I - File to read */
4086 char *namebuf
, /* I - Name buffer */
4087 size_t namesize
, /* I - Size of name buffer */
4088 const char **instance
) /* I - Instance */
4090 cups_file_t
*fp
; /* lpoptions file */
4091 char line
[8192], /* Line from file */
4092 *value
, /* Value for line */
4093 *nameptr
; /* Pointer into name */
4094 int linenum
; /* Current line */
4099 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
4103 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4105 if (!_cups_strcasecmp(line
, "default") && value
)
4107 strlcpy(namebuf
, value
, namesize
);
4109 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
4111 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
4114 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
4117 *instance
= nameptr
;
4125 return (*namebuf
? namebuf
: NULL
);
4130 * 'cups_get_dests()' - Get destinations from a file.
4133 static int /* O - Number of destinations */
4135 const char *filename
, /* I - File to read from */
4136 const char *match_name
, /* I - Destination name we want */
4137 const char *match_inst
, /* I - Instance name we want */
4138 int user_default_set
, /* I - User default printer set? */
4139 int num_dests
, /* I - Number of destinations */
4140 cups_dest_t
**dests
) /* IO - Destinations */
4142 int i
; /* Looping var */
4143 cups_dest_t
*dest
; /* Current destination */
4144 cups_file_t
*fp
; /* File pointer */
4145 char line
[8192], /* Line from file */
4146 *lineptr
, /* Pointer into line */
4147 *name
, /* Name of destination/option */
4148 *instance
; /* Instance of destination */
4149 int linenum
; /* Current line number */
4152 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", match_inst=\"%s\", user_default_set=%d, num_dests=%d, dests=%p)", filename
, match_name
, match_inst
, user_default_set
, num_dests
, (void *)dests
));
4155 * Try to open the file...
4158 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4162 * Read each printer; each line looks like:
4164 * Dest name[/instance] options
4165 * Default name[/instance] options
4170 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
4173 * See what type of line it is...
4176 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
4177 linenum
, line
, lineptr
));
4179 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
4181 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
4188 * Search for an instance...
4191 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
4194 if (*lineptr
== '/')
4197 * Found an instance...
4204 * Search for an instance...
4207 while (!isspace(*lineptr
& 255) && *lineptr
)
4216 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
4220 * See if the primary instance of the destination exists; if not,
4221 * ignore this entry and move on...
4226 if (_cups_strcasecmp(name
, match_name
) ||
4227 (!instance
&& match_inst
) ||
4228 (instance
&& !match_inst
) ||
4229 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
4234 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
4236 DEBUG_puts("9cups_get_dests: Not found!");
4242 * Add the destination...
4245 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4247 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4253 DEBUG_puts("9cups_get_dests: Out of memory!");
4259 * Add options until we hit the end of the line...
4262 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
4266 * If we found what we were looking for, stop now...
4273 * Set this as default if needed...
4276 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4278 DEBUG_puts("9cups_get_dests: Setting as default...");
4280 for (i
= 0; i
< num_dests
; i
++)
4281 (*dests
)[i
].is_default
= 0;
4283 dest
->is_default
= 1;
4288 * Close the file and return...
4298 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4302 static char * /* O - New string */
4304 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4305 char *buffer
, /* I - Buffer */
4306 size_t bufsize
) /* I - Size of buffer */
4308 int i
; /* Looping var */
4309 char *ptr
, /* Pointer into buffer */
4310 *end
, /* Pointer to end of buffer */
4311 *valptr
; /* Pointer into string attribute */
4315 * Return quickly if we have a single string value...
4318 if (attr
->num_values
== 1 &&
4319 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4320 attr
->value_tag
!= IPP_TAG_ENUM
&&
4321 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4322 attr
->value_tag
!= IPP_TAG_RANGE
)
4323 return (attr
->values
[0].string
.text
);
4326 * Copy the values to the string, separating with commas and escaping strings
4330 end
= buffer
+ bufsize
- 1;
4332 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4337 switch (attr
->value_tag
)
4339 case IPP_TAG_INTEGER
:
4341 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4344 case IPP_TAG_BOOLEAN
:
4345 if (attr
->values
[i
].boolean
)
4346 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4348 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4351 case IPP_TAG_RANGE
:
4352 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4353 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4355 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4359 for (valptr
= attr
->values
[i
].string
.text
;
4360 *valptr
&& ptr
< end
;)
4362 if (strchr(" \t\n\\\'\"", *valptr
))
4364 if (ptr
>= (end
- 1))
4387 * 'cups_name_cb()' - Find an enumerated destination.
4390 static int /* O - 1 to continue, 0 to stop */
4391 cups_name_cb(_cups_namedata_t
*data
, /* I - Data from cupsGetNamedDest */
4392 unsigned flags
, /* I - Enumeration flags */
4393 cups_dest_t
*dest
) /* I - Destination */
4395 DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", (void *)data
, data
->name
, flags
, (void *)dest
, dest
->name
));
4397 if (!(flags
& CUPS_DEST_FLAGS_REMOVED
) && !dest
->instance
&& !strcasecmp(data
->name
, dest
->name
))
4400 * Copy destination and stop enumeration...
4403 cupsCopyDest(dest
, 0, &data
->dest
);
4412 * 'cups_queue_name()' - Create a local queue name based on the service name.
4417 char *name
, /* I - Name buffer */
4418 const char *serviceName
, /* I - Service name */
4419 size_t namesize
) /* I - Size of name buffer */
4421 const char *ptr
; /* Pointer into serviceName */
4422 char *nameptr
; /* Pointer into name */
4425 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4428 * Sanitize the printer name...
4431 if (_cups_isalnum(*ptr
))
4433 else if (nameptr
== name
|| nameptr
[-1] != '_')