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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
19 #endif /* HAVE_NOTIFY_H */
23 #endif /* HAVE_POLL */
27 #endif /* HAVE_DNSSD */
30 # include <avahi-client/client.h>
31 # include <avahi-client/lookup.h>
32 # include <avahi-common/simple-watch.h>
33 # include <avahi-common/domain.h>
34 # include <avahi-common/error.h>
35 # include <avahi-common/malloc.h>
36 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
37 #endif /* HAVE_AVAHI */
46 # include <SystemConfiguration/SystemConfiguration.h>
47 # define _CUPS_LOCATION_DEFAULTS 1
48 # endif /* !TARGET_OS_IOS */
49 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
50 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
51 # define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
52 # define kLocationNetworkKey CFSTR("Network")
53 # define kLocationPrinterIDKey CFSTR("PrinterID")
54 # define kUseLastPrinter CFSTR("UseLastPrinter")
55 #endif /* __APPLE__ */
57 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
58 # define _CUPS_DNSSD_GET_DESTS 250 /* Milliseconds for cupsGetDests */
59 # define _CUPS_DNSSD_MAXTIME 50 /* Milliseconds for maximum quantum of time */
61 # define _CUPS_DNSSD_GET_DESTS 0 /* Milliseconds for cupsGetDests */
62 #endif /* HAVE_DNSSD || HAVE_AVAHI */
69 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
70 typedef enum _cups_dnssd_state_e
/* Enumerated device state */
77 _CUPS_DNSSD_INCOMPATIBLE
,
79 } _cups_dnssd_state_t
;
81 typedef struct _cups_dnssd_data_s
/* Enumeration data */
84 DNSServiceRef main_ref
; /* Main service reference */
85 # else /* HAVE_AVAHI */
86 AvahiSimplePoll
*simple_poll
; /* Polling interface */
87 AvahiClient
*client
; /* Client information */
88 int got_data
; /* Did we get data? */
89 int browsers
; /* How many browsers are running? */
90 # endif /* HAVE_DNSSD */
91 cups_dest_cb_t cb
; /* Callback */
92 void *user_data
; /* User data pointer */
93 cups_ptype_t type
, /* Printer type filter */
94 mask
; /* Printer type mask */
95 cups_array_t
*devices
; /* Devices found so far */
98 typedef struct _cups_dnssd_device_s
/* Enumerated device */
100 _cups_dnssd_state_t state
; /* State of device listing */
102 DNSServiceRef ref
; /* Service reference for query */
103 # else /* HAVE_AVAHI */
104 AvahiRecordBrowser
*ref
; /* Browser for query */
105 # endif /* HAVE_DNSSD */
106 char *fullName
, /* Full name */
107 *regtype
, /* Registration type */
108 *domain
; /* Domain name */
109 cups_ptype_t type
; /* Device registration type */
110 cups_dest_t dest
; /* Destination record */
111 } _cups_dnssd_device_t
;
113 typedef struct _cups_dnssd_resolve_s
/* Data for resolving URI */
115 int *cancel
; /* Pointer to "cancel" variable */
116 struct timeval end_time
; /* Ending time */
117 } _cups_dnssd_resolve_t
;
118 #endif /* HAVE_DNSSD */
120 typedef struct _cups_getdata_s
122 int num_dests
; /* Number of destinations */
123 cups_dest_t
*dests
; /* Destinations */
126 typedef struct _cups_namedata_s
128 const char *name
; /* Named destination */
129 cups_dest_t
*dest
; /* Destination */
137 #if _CUPS_LOCATION_DEFAULTS
138 static CFArrayRef
appleCopyLocations(void);
139 static CFStringRef
appleCopyNetwork(void);
140 #endif /* _CUPS_LOCATION_DEFAULTS */
142 static char *appleGetPaperSize(char *name
, size_t namesize
);
143 #endif /* __APPLE__ */
144 #if _CUPS_LOCATION_DEFAULTS
145 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
146 CFStringRef network
, CFIndex
*locindex
);
147 #endif /* _CUPS_LOCATION_DEFAULTS */
148 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
149 int *num_dests
, cups_dest_t
**dests
);
151 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
153 #endif /* __BLOCKS__ */
154 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
155 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
157 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
158 DNSServiceFlags flags
,
159 uint32_t interfaceIndex
,
160 DNSServiceErrorType errorCode
,
161 const char *serviceName
,
163 const char *replyDomain
,
165 # else /* HAVE_AVAHI */
166 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
167 AvahiIfIndex interface
,
168 AvahiProtocol protocol
,
169 AvahiBrowserEvent event
,
170 const char *serviceName
,
172 const char *replyDomain
,
173 AvahiLookupResultFlags flags
,
175 static void cups_dnssd_client_cb(AvahiClient
*client
,
176 AvahiClientState state
,
178 # endif /* HAVE_DNSSD */
179 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
180 _cups_dnssd_device_t
*b
);
181 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
182 _cups_dnssd_data_t
*data
);
183 static _cups_dnssd_device_t
*
184 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
185 const char *serviceName
,
187 const char *replyDomain
);
189 static void cups_dnssd_local_cb(DNSServiceRef sdRef
,
190 DNSServiceFlags flags
,
191 uint32_t interfaceIndex
,
192 DNSServiceErrorType errorCode
,
193 const char *serviceName
,
195 const char *replyDomain
,
197 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
198 DNSServiceFlags flags
,
199 uint32_t interfaceIndex
,
200 DNSServiceErrorType errorCode
,
201 const char *fullName
,
202 uint16_t rrtype
, uint16_t rrclass
,
203 uint16_t rdlen
, const void *rdata
,
204 uint32_t ttl
, void *context
);
205 # else /* HAVE_AVAHI */
206 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
207 unsigned int num_pollfds
,
208 int timeout
, void *context
);
209 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
210 AvahiIfIndex interface
,
211 AvahiProtocol protocol
,
212 AvahiBrowserEvent event
,
213 const char *name
, uint16_t rrclass
,
214 uint16_t rrtype
, const void *rdata
,
216 AvahiLookupResultFlags flags
,
218 # endif /* HAVE_DNSSD */
219 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
220 int msec
, int *cancel
,
221 cups_dest_cb_t cb
, void *user_data
);
222 static int cups_dnssd_resolve_cb(void *context
);
223 static void cups_dnssd_unquote(char *dst
, const char *src
,
225 static int cups_elapsed(struct timeval
*t
);
226 #endif /* HAVE_DNSSD || HAVE_AVAHI */
227 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
);
228 static int cups_find_dest(const char *name
, const char *instance
,
229 int num_dests
, cups_dest_t
*dests
, int prev
,
231 static int cups_get_cb(_cups_getdata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
232 static char *cups_get_default(const char *filename
, char *namebuf
,
233 size_t namesize
, const char **instance
);
234 static int cups_get_dests(const char *filename
, const char *match_name
,
235 const char *match_inst
, int user_default_set
,
236 int num_dests
, cups_dest_t
**dests
);
237 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
239 static int cups_name_cb(_cups_namedata_t
*data
, unsigned flags
, cups_dest_t
*dest
);
240 static void cups_queue_name(char *name
, const char *serviceName
, size_t namesize
);
244 * 'cupsAddDest()' - Add a destination to the list of destinations.
246 * This function cannot be used to add a new class or printer queue,
247 * it only adds a new container of saved options for the named
248 * destination or instance.
250 * If the named destination already exists, the destination list is
251 * returned unchanged. Adding a new instance of a destination creates
252 * a copy of that destination's options.
254 * Use the @link cupsSaveDests@ function to save the updated list of
255 * destinations to the user's lpoptions file.
258 int /* O - New number of destinations */
259 cupsAddDest(const char *name
, /* I - Destination name */
260 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
261 int num_dests
, /* I - Number of destinations */
262 cups_dest_t
**dests
) /* IO - Destinations */
264 int i
; /* Looping var */
265 cups_dest_t
*dest
; /* Destination pointer */
266 cups_dest_t
*parent
= NULL
; /* Parent destination */
267 cups_option_t
*doption
, /* Current destination option */
268 *poption
; /* Current parent option */
274 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
276 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
279 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
283 * Find the base dest again now the array has been realloc'd.
286 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
288 if (instance
&& parent
&& parent
->num_options
> 0)
291 * Copy options from parent...
294 dest
->options
= calloc(sizeof(cups_option_t
), (size_t)parent
->num_options
);
298 dest
->num_options
= parent
->num_options
;
300 for (i
= dest
->num_options
, doption
= dest
->options
,
301 poption
= parent
->options
;
303 i
--, doption
++, poption
++)
305 doption
->name
= _cupsStrRetain(poption
->name
);
306 doption
->value
= _cupsStrRetain(poption
->value
);
318 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
321 CFStringRef
/* O - Default paper ID */
322 _cupsAppleCopyDefaultPaperID(void)
324 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
325 kCUPSPrintingPrefs
));
330 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
333 CFStringRef
/* O - Default printer name */
334 _cupsAppleCopyDefaultPrinter(void)
336 # if _CUPS_LOCATION_DEFAULTS
337 CFStringRef network
; /* Network location */
338 CFArrayRef locations
; /* Location array */
339 CFStringRef locprinter
; /* Current printer */
343 * Use location-based defaults only if "use last printer" is selected in the
344 * system preferences...
347 if (!_cupsAppleGetUseLastPrinter())
349 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
355 * Get the current location...
358 if ((network
= appleCopyNetwork()) == NULL
)
360 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
366 * Lookup the network in the preferences...
369 if ((locations
= appleCopyLocations()) == NULL
)
372 * Missing or bad location array, so no location-based default...
375 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
383 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
384 (int)CFArrayGetCount(locations
)));
386 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
387 CFRetain(locprinter
);
390 CFRelease(locations
);
396 # endif /* _CUPS_LOCATION_DEFAULTS */
401 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
404 int /* O - 1 to use last printer, 0 otherwise */
405 _cupsAppleGetUseLastPrinter(void)
407 Boolean uselast
, /* Use last printer preference value */
408 uselast_set
; /* Valid is set? */
411 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
414 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
425 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
429 _cupsAppleSetDefaultPaperID(
430 CFStringRef name
) /* I - New paper ID */
432 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
433 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
435 # ifdef HAVE_NOTIFY_POST
436 notify_post("com.apple.printerPrefsChange");
437 # endif /* HAVE_NOTIFY_POST */
442 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
446 _cupsAppleSetDefaultPrinter(
447 CFStringRef name
) /* I - Default printer/class name */
449 # if _CUPS_LOCATION_DEFAULTS
450 CFStringRef network
; /* Current network */
451 CFArrayRef locations
; /* Old locations array */
452 CFIndex locindex
; /* Index in locations array */
453 CFStringRef locprinter
; /* Current printer */
454 CFMutableArrayRef newlocations
; /* New locations array */
455 CFMutableDictionaryRef newlocation
; /* New location */
459 * Get the current location...
462 if ((network
= appleCopyNetwork()) == NULL
)
464 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
469 * Lookup the network in the preferences...
472 if ((locations
= appleCopyLocations()) != NULL
)
473 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
480 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
483 * Need to change the locations array...
488 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
492 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
495 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
496 &kCFTypeArrayCallBacks
);
498 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
499 &kCFTypeDictionaryKeyCallBacks
,
500 &kCFTypeDictionaryValueCallBacks
);
502 if (newlocation
&& newlocations
)
505 * Put the new location at the front of the array...
508 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
509 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
510 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
513 * Limit the number of locations to 10...
516 while (CFArrayGetCount(newlocations
) > 10)
517 CFArrayRemoveValueAtIndex(newlocations
, 10);
520 * Push the changes out...
523 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
525 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
527 # ifdef HAVE_NOTIFY_POST
528 notify_post("com.apple.printerPrefsChange");
529 # endif /* HAVE_NOTIFY_POST */
533 CFRelease(newlocations
);
536 CFRelease(newlocation
);
540 CFRelease(locations
);
546 # endif /* _CUPS_LOCATION_DEFAULTS */
551 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
555 _cupsAppleSetUseLastPrinter(
556 int uselast
) /* O - 1 to use last printer, 0 otherwise */
558 CFPreferencesSetAppValue(kUseLastPrinter
,
559 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
561 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
563 # ifdef HAVE_NOTIFY_POST
564 notify_post("com.apple.printerPrefsChange");
565 # endif /* HAVE_NOTIFY_POST */
567 #endif /* __APPLE__ */
571 * 'cupsConnectDest()' - Open a conection to the destination.
573 * Connect to the destination, returning a new @code http_t@ connection object
574 * and optionally the resource path to use for the destination. These calls
575 * will block until a connection is made, the timeout expires, the integer
576 * pointed to by "cancel" is non-zero, or the callback function (or block)
577 * returns 0. The caller is responsible for calling @link httpClose@ on the
578 * returned connection.
580 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
581 * for the "flags" argument to connect directly to the device associated with
582 * the destination. Otherwise, the connection is made to the CUPS scheduler
583 * associated with the destination.
585 * @since CUPS 1.6/macOS 10.8@
588 http_t
* /* O - Connection to destination or @code NULL@ */
590 cups_dest_t
*dest
, /* I - Destination */
591 unsigned flags
, /* I - Connection flags */
592 int msec
, /* I - Timeout in milliseconds */
593 int *cancel
, /* I - Pointer to "cancel" variable */
594 char *resource
, /* I - Resource buffer */
595 size_t resourcesize
, /* I - Size of resource buffer */
596 cups_dest_cb_t cb
, /* I - Callback function */
597 void *user_data
) /* I - User data pointer */
599 const char *uri
; /* Printer URI */
600 char scheme
[32], /* URI scheme */
601 userpass
[256], /* Username and password (unused) */
602 hostname
[256], /* Hostname */
603 tempresource
[1024]; /* Temporary resource buffer */
604 int port
; /* Port number */
605 char portstr
[16]; /* Port number string */
606 http_encryption_t encryption
; /* Encryption to use */
607 http_addrlist_t
*addrlist
; /* Address list for server */
608 http_t
*http
; /* Connection to server */
611 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
));
614 * Range check input...
622 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
626 if (!resource
|| resourcesize
< 1)
628 resource
= tempresource
;
629 resourcesize
= sizeof(tempresource
);
633 * Grab the printer URI...
636 if (flags
& CUPS_DEST_FLAGS_DEVICE
)
638 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
640 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
641 if (strstr(uri
, "._tcp"))
642 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
643 #endif /* HAVE_DNSSD || HAVE_AVAHI */
646 else if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
648 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
650 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
651 if (strstr(uri
, "._tcp"))
652 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
653 #endif /* HAVE_DNSSD || HAVE_AVAHI */
657 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
661 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
663 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
669 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
672 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
677 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
678 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
679 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
681 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
684 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
691 * Lookup the address for the server...
695 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
697 snprintf(portstr
, sizeof(portstr
), "%d", port
);
699 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
702 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
707 if (cancel
&& *cancel
)
709 httpAddrFreeList(addrlist
);
712 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
718 * Create the HTTP object pointing to the server referenced by the URI...
721 if (!strcmp(scheme
, "ipps") || port
== 443)
722 encryption
= HTTP_ENCRYPTION_ALWAYS
;
724 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
726 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
727 httpAddrFreeList(addrlist
);
730 * Connect if requested...
733 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
736 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
741 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
743 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
745 if (cancel
&& *cancel
)
746 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
748 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
751 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
760 * 'cupsConnectDestBlock()' - Open a connection to the destination.
762 * Connect to the destination, returning a new @code http_t@ connection object
763 * and optionally the resource path to use for the destination. These calls
764 * will block until a connection is made, the timeout expires, the integer
765 * pointed to by "cancel" is non-zero, or the block returns 0. The caller is
766 * responsible for calling @link httpClose@ on the returned connection.
768 * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
769 * for the "flags" argument to connect directly to the device associated with
770 * the destination. Otherwise, the connection is made to the CUPS scheduler
771 * associated with the destination.
773 * @since CUPS 1.6/macOS 10.8@ @exclude all@
776 http_t
* /* O - Connection to destination or @code NULL@ */
777 cupsConnectDestBlock(
778 cups_dest_t
*dest
, /* I - Destination */
779 unsigned flags
, /* I - Connection flags */
780 int msec
, /* I - Timeout in milliseconds */
781 int *cancel
, /* I - Pointer to "cancel" variable */
782 char *resource
, /* I - Resource buffer */
783 size_t resourcesize
, /* I - Size of resource buffer */
784 cups_dest_block_t block
) /* I - Callback block */
786 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
787 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
789 #endif /* __BLOCKS__ */
793 * 'cupsCopyDest()' - Copy a destination.
795 * Make a copy of the destination to an array of destinations (or just a single
796 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
797 * for calling cupsFreeDests() on the returned object(s).
799 * @since CUPS 1.6/macOS 10.8@
802 int /* O - New number of destinations */
803 cupsCopyDest(cups_dest_t
*dest
, /* I - Destination to copy */
804 int num_dests
, /* I - Number of destinations */
805 cups_dest_t
**dests
) /* IO - Destination array */
807 int i
; /* Looping var */
808 cups_dest_t
*new_dest
; /* New destination pointer */
809 cups_option_t
*new_option
, /* Current destination option */
810 *option
; /* Current parent option */
814 * Range check input...
817 if (!dest
|| num_dests
< 0 || !dests
)
821 * See if the destination already exists...
824 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
828 * Protect against copying destination to itself...
831 if (new_dest
== dest
)
835 * Otherwise, free the options...
838 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
840 new_dest
->num_options
= 0;
841 new_dest
->options
= NULL
;
844 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
848 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
849 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
851 new_dest
->num_options
= dest
->num_options
;
853 for (i
= dest
->num_options
, option
= dest
->options
,
854 new_option
= new_dest
->options
;
856 i
--, option
++, new_option
++)
858 new_option
->name
= _cupsStrRetain(option
->name
);
859 new_option
->value
= _cupsStrRetain(option
->value
);
868 * '_cupsCreateDest()' - Create a local (temporary) queue.
871 char * /* O - Printer URI or @code NULL@ on error */
872 _cupsCreateDest(const char *name
, /* I - Printer name */
873 const char *info
, /* I - Printer description of @code NULL@ */
874 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
875 const char *device_uri
, /* I - Device URI */
876 char *uri
, /* I - Printer URI buffer */
877 size_t urisize
) /* I - Size of URI buffer */
879 http_t
*http
; /* Connection to server */
880 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
881 *response
; /* CUPS-Create-Local-Printer response */
882 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
883 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
884 /* printer-state value */
887 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
890 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
893 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
895 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
896 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
898 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
899 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
901 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
903 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
905 response
= cupsDoRequest(http
, request
, "/");
907 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
908 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
916 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
917 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
919 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
924 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
926 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
927 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
928 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
930 response
= cupsDoRequest(http
, request
, "/");
932 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
933 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
945 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
947 * Destinations are enumerated from one or more sources. The callback function
948 * receives the @code user_data@ pointer and the destination pointer which can
949 * be used as input to the @link cupsCopyDest@ function. The function must
950 * return 1 to continue enumeration or 0 to stop.
952 * The @code type@ and @code mask@ arguments allow the caller to filter the
953 * destinations that are enumerated. Passing 0 for both will enumerate all
954 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
955 * destinations that are available but have not yet been added locally.
957 * Enumeration happens on the current thread and does not return until all
958 * destinations have been enumerated or the callback function returns 0.
960 * Note: The callback function will likely receive multiple updates for the same
961 * destinations - it is up to the caller to suppress any duplicate destinations.
963 * @since CUPS 1.6/macOS 10.8@
966 int /* O - 1 on success, 0 on failure */
968 unsigned flags
, /* I - Enumeration flags */
969 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
970 int *cancel
, /* I - Pointer to "cancel" variable */
971 cups_ptype_t type
, /* I - Printer type bits */
972 cups_ptype_t mask
, /* I - Mask for printer type bits */
973 cups_dest_cb_t cb
, /* I - Callback function */
974 void *user_data
) /* I - User data */
976 return (cups_enum_dests(CUPS_HTTP_DEFAULT
, flags
, msec
, cancel
, type
, mask
, cb
, user_data
));
982 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
984 * Destinations are enumerated from one or more sources. The block receives the
985 * @code user_data@ pointer and the destination pointer which can be used as
986 * input to the @link cupsCopyDest@ function. The block must return 1 to
987 * continue enumeration or 0 to stop.
989 * The @code type@ and @code mask@ arguments allow the caller to filter the
990 * destinations that are enumerated. Passing 0 for both will enumerate all
991 * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on
992 * destinations that are available but have not yet been added locally.
994 * Enumeration happens on the current thread and does not return until all
995 * destinations have been enumerated or the block returns 0.
997 * Note: The block will likely receive multiple updates for the same
998 * destinations - it is up to the caller to suppress any duplicate destinations.
1000 * @since CUPS 1.6/macOS 10.8@ @exclude all@
1003 int /* O - 1 on success, 0 on failure */
1005 unsigned flags
, /* I - Enumeration flags */
1006 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1007 int *cancel
, /* I - Pointer to "cancel" variable */
1008 cups_ptype_t type
, /* I - Printer type bits */
1009 cups_ptype_t mask
, /* I - Mask for printer type bits */
1010 cups_dest_block_t block
) /* I - Block */
1012 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1013 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1015 # endif /* __BLOCKS__ */
1019 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1023 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1024 cups_dest_t
*dests
) /* I - Destinations */
1026 int i
; /* Looping var */
1027 cups_dest_t
*dest
; /* Current destination */
1030 if (num_dests
== 0 || dests
== NULL
)
1033 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1035 _cupsStrFree(dest
->name
);
1036 _cupsStrFree(dest
->instance
);
1038 cupsFreeOptions(dest
->num_options
, dest
->options
);
1046 * 'cupsGetDest()' - Get the named destination from the list.
1048 * Use the @link cupsEnumDests@ or @link cupsGetDests2@ functions to get a
1049 * list of supported destinations for the current user.
1052 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1053 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1054 const char *instance
, /* I - Instance name or @code NULL@ */
1055 int num_dests
, /* I - Number of destinations */
1056 cups_dest_t
*dests
) /* I - Destinations */
1058 int diff
, /* Result of comparison */
1059 match
; /* Matching index */
1062 if (num_dests
<= 0 || !dests
)
1068 * NULL name for default printer.
1071 while (num_dests
> 0)
1073 if (dests
->is_default
)
1083 * Lookup name and optionally the instance...
1086 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1089 return (dests
+ match
);
1097 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1100 const char * /* O - Printer URI */
1101 _cupsGetDestResource(
1102 cups_dest_t
*dest
, /* I - Destination */
1103 char *resource
, /* I - Resource buffer */
1104 size_t resourcesize
) /* I - Size of resource buffer */
1106 const char *uri
; /* Printer URI */
1107 char scheme
[32], /* URI scheme */
1108 userpass
[256], /* Username and password (unused) */
1109 hostname
[256]; /* Hostname */
1110 int port
; /* Port number */
1113 DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", (void *)dest
, dest
->name
, (void *)resource
, (int)resourcesize
));
1116 * Range check input...
1119 if (!dest
|| !resource
|| resourcesize
< 1)
1124 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1129 * Grab the printer URI...
1132 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
1134 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
1136 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1137 if (strstr(uri
, "._tcp"))
1138 uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
);
1139 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1144 DEBUG_printf(("1_cupsGetDestResource: Resolved printer-uri-supported=\"%s\"", uri
));
1146 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, resource
, resourcesize
);
1151 DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri
));
1153 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
1155 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
1159 DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported found.");
1164 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1171 DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri
));
1173 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1174 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1175 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1177 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1183 DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource
));
1190 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1192 * "name" is the desired name for the printer. If @code NULL@, a name will be
1193 * created using the URI.
1195 * "uri" is the "ipp" or "ipps" URI for the printer.
1197 * @since CUPS 2.0/macOS 10.10@
1200 cups_dest_t
* /* O - Destination or @code NULL@ */
1201 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1202 const char *uri
) /* I - URI for the printer */
1204 cups_dest_t
*dest
; /* New destination */
1205 char temp
[1024], /* Temporary string */
1206 scheme
[256], /* Scheme from URI */
1207 userpass
[256], /* Username:password from URI */
1208 hostname
[256], /* Hostname from URI */
1209 resource
[1024], /* Resource path from URI */
1210 *ptr
; /* Pointer into string */
1211 const char *info
; /* printer-info string */
1212 int port
; /* Port number from URI */
1216 * Range check input...
1221 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1225 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1226 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1228 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1240 * Create the name from the URI...
1243 if (strstr(hostname
, "._tcp"))
1246 * Use the service instance name...
1249 if ((ptr
= strstr(hostname
, "._")) != NULL
)
1252 cups_queue_name(temp
, hostname
, sizeof(temp
));
1256 else if (!strncmp(resource
, "/classes/", 9))
1258 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1259 name
= resource
+ 9;
1262 else if (!strncmp(resource
, "/printers/", 10))
1264 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1265 name
= resource
+ 10;
1276 * Create the destination...
1279 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1281 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1285 dest
->name
= _cupsStrAlloc(name
);
1286 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1287 dest
->num_options
= cupsAddOption("printer-info", info
, dest
->num_options
, &(dest
->options
));
1294 * '_cupsGetDests()' - Get destinations from a server.
1296 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1297 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1300 * "name" is the name of an existing printer and is only used when "op" is
1301 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1303 * "dest" is initialized to point to the array of destinations.
1305 * 0 is returned if there are no printers, no default printer, or the named
1306 * printer does not exist, respectively.
1308 * Free the memory used by the destination array using the @link cupsFreeDests@
1311 * Note: On macOS this function also gets the default paper from the system
1312 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1313 * options array for each destination that supports it.
1316 int /* O - Number of destinations */
1317 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1318 * @code CUPS_HTTP_DEFAULT@ */
1319 ipp_op_t op
, /* I - IPP operation */
1320 const char *name
, /* I - Name of destination */
1321 cups_dest_t
**dests
, /* IO - Destinations */
1322 cups_ptype_t type
, /* I - Printer type bits */
1323 cups_ptype_t mask
) /* I - Printer type mask */
1325 int num_dests
= 0; /* Number of destinations */
1326 cups_dest_t
*dest
; /* Current destination */
1327 ipp_t
*request
, /* IPP Request */
1328 *response
; /* IPP Response */
1329 ipp_attribute_t
*attr
; /* Current attribute */
1330 const char *printer_name
; /* printer-name attribute */
1331 char uri
[1024]; /* printer-uri value */
1332 int num_options
; /* Number of options */
1333 cups_option_t
*options
; /* Options */
1335 char media_default
[41]; /* Default paper size */
1336 #endif /* __APPLE__ */
1337 char optname
[1024], /* Option name */
1338 value
[2048], /* Option value */
1339 *ptr
; /* Pointer into name/value */
1340 static const char * const pattrs
[] = /* Attributes we're interested in */
1342 "auth-info-required",
1344 "job-sheets-default",
1345 "marker-change-time",
1347 "marker-high-levels",
1349 "marker-low-levels",
1355 #endif /* __APPLE__ */
1359 "printer-is-accepting-jobs",
1360 "printer-is-shared",
1361 "printer-is-temporary",
1363 "printer-make-and-model",
1364 "printer-mandatory-job-attributes",
1367 "printer-state-change-time",
1368 "printer-state-reasons",
1370 "printer-uri-supported"
1374 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
));
1378 * Get the default paper size...
1381 appleGetPaperSize(media_default
, sizeof(media_default
));
1382 DEBUG_printf(("1_cupsGetDests: Default media is '%s'.", media_default
));
1383 #endif /* __APPLE__ */
1386 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1387 * require the following attributes:
1389 * attributes-charset
1390 * attributes-natural-language
1391 * requesting-user-name
1392 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1395 request
= ippNewRequest(op
);
1397 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1398 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1401 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1402 "requesting-user-name", NULL
, cupsUser());
1404 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1406 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1407 "localhost", ippPort(), "/printers/%s", name
);
1408 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1413 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1414 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1418 * Do the request and get back a response...
1421 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1423 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1426 * Skip leading attributes until we hit a printer...
1429 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1436 * Pull the needed attributes from this printer...
1439 printer_name
= NULL
;
1443 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1445 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1446 attr
->value_tag
!= IPP_TAG_ENUM
&&
1447 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1448 attr
->value_tag
!= IPP_TAG_TEXT
&&
1449 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1450 attr
->value_tag
!= IPP_TAG_NAME
&&
1451 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1452 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1453 attr
->value_tag
!= IPP_TAG_RANGE
&&
1454 attr
->value_tag
!= IPP_TAG_URI
)
1457 if (!strcmp(attr
->name
, "auth-info-required") ||
1458 !strcmp(attr
->name
, "device-uri") ||
1459 !strcmp(attr
->name
, "marker-change-time") ||
1460 !strcmp(attr
->name
, "marker-colors") ||
1461 !strcmp(attr
->name
, "marker-high-levels") ||
1462 !strcmp(attr
->name
, "marker-levels") ||
1463 !strcmp(attr
->name
, "marker-low-levels") ||
1464 !strcmp(attr
->name
, "marker-message") ||
1465 !strcmp(attr
->name
, "marker-names") ||
1466 !strcmp(attr
->name
, "marker-types") ||
1467 !strcmp(attr
->name
, "printer-commands") ||
1468 !strcmp(attr
->name
, "printer-info") ||
1469 !strcmp(attr
->name
, "printer-is-shared") ||
1470 !strcmp(attr
->name
, "printer-is-temporary") ||
1471 !strcmp(attr
->name
, "printer-make-and-model") ||
1472 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1473 !strcmp(attr
->name
, "printer-state") ||
1474 !strcmp(attr
->name
, "printer-state-change-time") ||
1475 !strcmp(attr
->name
, "printer-type") ||
1476 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1477 !strcmp(attr
->name
, "printer-location") ||
1478 !strcmp(attr
->name
, "printer-state-reasons") ||
1479 !strcmp(attr
->name
, "printer-uri-supported"))
1482 * Add a printer description attribute...
1485 num_options
= cupsAddOption(attr
->name
,
1486 cups_make_string(attr
, value
,
1488 num_options
, &options
);
1491 else if (!strcmp(attr
->name
, "media-supported") && media_default
[0])
1494 * See if we can set a default media size...
1497 int i
; /* Looping var */
1499 for (i
= 0; i
< attr
->num_values
; i
++)
1500 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1502 DEBUG_printf(("1_cupsGetDests: Setting media to '%s'.", media_default
));
1503 num_options
= cupsAddOption("media", media_default
, num_options
, &options
);
1507 #endif /* __APPLE__ */
1508 else if (!strcmp(attr
->name
, "printer-name") &&
1509 attr
->value_tag
== IPP_TAG_NAME
)
1510 printer_name
= attr
->values
[0].string
.text
;
1511 else if (strncmp(attr
->name
, "notify-", 7) &&
1512 strncmp(attr
->name
, "print-quality-", 14) &&
1513 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1514 attr
->value_tag
== IPP_TAG_ENUM
||
1515 attr
->value_tag
== IPP_TAG_INTEGER
||
1516 attr
->value_tag
== IPP_TAG_KEYWORD
||
1517 attr
->value_tag
== IPP_TAG_NAME
||
1518 attr
->value_tag
== IPP_TAG_RANGE
) &&
1519 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1522 * Add a default option...
1525 strlcpy(optname
, attr
->name
, sizeof(optname
));
1526 optname
[ptr
- attr
->name
] = '\0';
1528 if (_cups_strcasecmp(optname
, "media") || !cupsGetOption("media", num_options
, options
))
1529 num_options
= cupsAddOption(optname
, cups_make_string(attr
, value
, sizeof(value
)), num_options
, &options
);
1534 * See if we have everything needed...
1539 cupsFreeOptions(num_options
, options
);
1547 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1549 dest
->num_options
= num_options
;
1550 dest
->options
= options
;
1553 cupsFreeOptions(num_options
, options
);
1559 ippDelete(response
);
1563 * Return the count...
1571 * 'cupsGetDests()' - Get the list of destinations from the default server.
1573 * Starting with CUPS 1.2, the returned list of destinations include the
1574 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1575 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1576 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1577 * attributes as options.
1579 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1580 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1581 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1583 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1584 * be used. The "printer-uri-supported" option will be present for those IPP
1585 * printers that have been recently used.
1587 * Use the @link cupsFreeDests@ function to free the destination list and
1588 * the @link cupsGetDest@ function to find a particular destination.
1593 int /* O - Number of destinations */
1594 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1596 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1601 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1603 * Starting with CUPS 1.2, the returned list of destinations include the
1604 * "printer-info", "printer-is-accepting-jobs", "printer-is-shared",
1605 * "printer-make-and-model", "printer-state", "printer-state-change-time",
1606 * "printer-state-reasons", "printer-type", and "printer-uri-supported"
1607 * attributes as options.
1609 * CUPS 1.4 adds the "marker-change-time", "marker-colors",
1610 * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message",
1611 * "marker-names", "marker-types", and "printer-commands" attributes as options.
1613 * CUPS 2.2 adds accessible IPP printers to the list of destinations that can
1614 * be used. The "printer-uri-supported" option will be present for those IPP
1615 * printers that have been recently used.
1617 * Use the @link cupsFreeDests@ function to free the destination list and
1618 * the @link cupsGetDest@ function to find a particular destination.
1620 * @since CUPS 1.1.21/macOS 10.4@
1623 int /* O - Number of destinations */
1624 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1625 cups_dest_t
**dests
) /* O - Destinations */
1627 _cups_getdata_t data
; /* Enumeration data */
1628 cups_dest_t
*dest
; /* Current destination */
1629 const char *home
; /* HOME environment variable */
1630 char filename
[1024]; /* Local ~/.cups/lpoptions file */
1631 const char *defprinter
; /* Default printer */
1632 char name
[1024], /* Copy of printer name */
1633 *instance
, /* Pointer to instance name */
1634 *user_default
; /* User default printer */
1635 int num_reals
; /* Number of real queues */
1636 cups_dest_t
*reals
; /* Real queues */
1637 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1640 DEBUG_printf(("cupsGetDests2(http=%p, dests=%p)", (void *)http
, (void *)dests
));
1643 * Range check the input...
1648 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
1649 DEBUG_puts("1cupsGetDests2: NULL dests pointer, returning 0.");
1654 * Connect to the server as needed...
1659 if ((http
= _cupsConnect()) == NULL
)
1668 * Grab the printers and classes...
1674 if (!httpAddrLocalhost(httpGetAddress(http
)))
1677 * When talking to a remote cupsd, just enumerate printers on the remote
1681 cups_enum_dests(http
, 0, _CUPS_DNSSD_GET_DESTS
, NULL
, 0, CUPS_PRINTER_DISCOVERED
, (cups_dest_cb_t
)cups_get_cb
, &data
);
1686 * When talking to a local cupsd, enumerate both local printers and ones we
1687 * can find on the network...
1690 cups_enum_dests(http
, 0, _CUPS_DNSSD_GET_DESTS
, NULL
, 0, 0, (cups_dest_cb_t
)cups_get_cb
, &data
);
1694 * Make a copy of the "real" queues for a later sanity check...
1697 if (data
.num_dests
> 0)
1699 num_reals
= data
.num_dests
;
1700 reals
= calloc((size_t)num_reals
, sizeof(cups_dest_t
));
1703 memcpy(reals
, data
.dests
, (size_t)num_reals
* sizeof(cups_dest_t
));
1714 * Grab the default destination...
1717 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1719 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
1721 strlcpy(name
, defprinter
, sizeof(name
));
1728 * Separate printer and instance name...
1731 if ((instance
= strchr(name
, '/')) != NULL
)
1735 * Lookup the printer and instance and make it the default...
1738 if ((dest
= cupsGetDest(name
, instance
, data
.num_dests
, data
.dests
)) != NULL
)
1739 dest
->is_default
= 1;
1745 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
1748 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1749 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
1751 if ((home
= getenv("HOME")) != NULL
)
1753 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1755 data
.num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
, data
.num_dests
, &data
.dests
);
1759 * Validate the current default destination - this prevents old
1760 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
1761 * pointing to a non-existent printer or class...
1767 * See if we have a default printer...
1770 if ((dest
= cupsGetDest(NULL
, NULL
, data
.num_dests
, data
.dests
)) != NULL
)
1773 * Have a default; see if it is real...
1776 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
1779 * Remove the non-real printer from the list, since we don't want jobs
1780 * going to an unexpected printer... (<rdar://problem/14216472>)
1783 data
.num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
.num_dests
, &data
.dests
);
1795 * Return the number of destinations...
1798 *dests
= data
.dests
;
1800 if (data
.num_dests
> 0)
1801 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
1803 DEBUG_printf(("1cupsGetDests2: Returning %d destinations.", data
.num_dests
));
1805 return (data
.num_dests
);
1810 * 'cupsGetNamedDest()' - Get options for the named destination.
1812 * This function is optimized for retrieving a single destination and should
1813 * be used instead of @link cupsGetDests2@ and @link cupsGetDest@ when you
1814 * either know the name of the destination or want to print to the default
1815 * destination. If @code NULL@ is returned, the destination does not exist or
1816 * there is no default destination.
1818 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
1819 * server will be used.
1821 * If "name" is @code NULL@, the default printer for the current user will be
1824 * The returned destination must be freed using @link cupsFreeDests@ with a
1825 * "num_dests" value of 1.
1827 * @since CUPS 1.4/macOS 10.6@
1830 cups_dest_t
* /* O - Destination or @code NULL@ */
1831 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1832 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1833 const char *instance
) /* I - Instance name or @code NULL@ */
1835 const char *dest_name
; /* Working destination name */
1836 cups_dest_t
*dest
; /* Destination */
1837 char filename
[1024], /* Path to lpoptions */
1838 defname
[256]; /* Default printer name */
1839 const char *home
= getenv("HOME"); /* Home directory */
1840 int set_as_default
= 0; /* Set returned destination as default */
1841 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
1842 /* IPP operation to get server ops */
1843 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1846 DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", (void *)http
, name
, instance
));
1849 * If "name" is NULL, find the default destination...
1857 dest_name
= _cupsUserDefault(defname
, sizeof(defname
));
1861 char *ptr
; /* Temporary pointer... */
1863 if ((ptr
= strchr(defname
, '/')) != NULL
)
1874 * No default in the environment, try the user's lpoptions files...
1877 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1879 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1888 * Still not there? Try the system lpoptions file...
1891 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1892 dest_name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
1901 * No locally-set default destination, ask the server...
1904 op
= IPP_OP_CUPS_GET_DEFAULT
;
1907 DEBUG_puts("1cupsGetNamedDest: Asking server for default printer...");
1910 DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name
));
1914 * Get the printer's attributes...
1917 if (!_cupsGetDests(http
, op
, dest_name
, &dest
, 0, 0))
1921 _cups_namedata_t data
; /* Callback data */
1923 DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network...");
1928 cupsEnumDests(0, 1000, NULL
, 0, 0, (cups_dest_cb_t
)cups_name_cb
, &data
);
1937 switch (set_as_default
)
1942 case 1 : /* Set from env vars */
1943 if (getenv("LPDEST"))
1944 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("LPDEST environment variable names default destination that does not exist."), 1);
1945 else if (getenv("PRINTER"))
1946 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("PRINTER environment variable names default destination that does not exist."), 1);
1948 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("No default destination."), 1);
1951 case 2 : /* Set from ~/.cups/lpoptions */
1952 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("~/.cups/lpoptions file names default destination that does not exist."), 1);
1955 case 3 : /* Set from /etc/cups/lpoptions */
1956 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("/etc/cups/lpoptions file names default destination that does not exist."), 1);
1959 case 4 : /* Set from server */
1960 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, _("No default destination."), 1);
1968 DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", (void *)dest
));
1971 dest
->instance
= _cupsStrAlloc(instance
);
1974 dest
->is_default
= 1;
1977 * Then add local options...
1980 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1981 cups_get_dests(filename
, dest_name
, instance
, 1, 1, &dest
);
1985 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1987 cups_get_dests(filename
, dest_name
, instance
, 1, 1, &dest
);
1991 * Return the result...
1999 * 'cupsRemoveDest()' - Remove a destination from the destination list.
2001 * Removing a destination/instance does not delete the class or printer
2002 * queue, merely the lpoptions for that destination/instance. Use the
2003 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
2004 * options for the user.
2006 * @since CUPS 1.3/macOS 10.5@
2009 int /* O - New number of destinations */
2010 cupsRemoveDest(const char *name
, /* I - Destination name */
2011 const char *instance
, /* I - Instance name or @code NULL@ */
2012 int num_dests
, /* I - Number of destinations */
2013 cups_dest_t
**dests
) /* IO - Destinations */
2015 int i
; /* Index into destinations */
2016 cups_dest_t
*dest
; /* Pointer to destination */
2020 * Find the destination...
2023 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
2030 _cupsStrFree(dest
->name
);
2031 _cupsStrFree(dest
->instance
);
2032 cupsFreeOptions(dest
->num_options
, dest
->options
);
2035 * Remove the destination from the array...
2040 i
= (int)(dest
- *dests
);
2043 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
2050 * 'cupsSetDefaultDest()' - Set the default destination.
2052 * @since CUPS 1.3/macOS 10.5@
2057 const char *name
, /* I - Destination name */
2058 const char *instance
, /* I - Instance name or @code NULL@ */
2059 int num_dests
, /* I - Number of destinations */
2060 cups_dest_t
*dests
) /* I - Destinations */
2062 int i
; /* Looping var */
2063 cups_dest_t
*dest
; /* Current destination */
2067 * Range check input...
2070 if (!name
|| num_dests
<= 0 || !dests
)
2074 * Loop through the array and set the "is_default" flag for the matching
2078 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2079 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2080 ((!instance
&& !dest
->instance
) ||
2081 (instance
&& dest
->instance
&&
2082 !_cups_strcasecmp(instance
, dest
->instance
)));
2087 * 'cupsSetDests()' - Save the list of destinations for the default server.
2089 * This function saves the destinations to /etc/cups/lpoptions when run
2090 * as root and ~/.cups/lpoptions when run as a normal user.
2096 cupsSetDests(int num_dests
, /* I - Number of destinations */
2097 cups_dest_t
*dests
) /* I - Destinations */
2099 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2104 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2106 * This function saves the destinations to /etc/cups/lpoptions when run
2107 * as root and ~/.cups/lpoptions when run as a normal user.
2109 * @since CUPS 1.1.21/macOS 10.4@
2112 int /* O - 0 on success, -1 on error */
2113 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2114 int num_dests
, /* I - Number of destinations */
2115 cups_dest_t
*dests
) /* I - Destinations */
2117 int i
, j
; /* Looping vars */
2118 int wrote
; /* Wrote definition? */
2119 cups_dest_t
*dest
; /* Current destination */
2120 cups_option_t
*option
; /* Current option */
2121 _ipp_option_t
*match
; /* Matching attribute for option */
2122 FILE *fp
; /* File pointer */
2124 const char *home
; /* HOME environment variable */
2126 char filename
[1024]; /* lpoptions file */
2127 int num_temps
; /* Number of temporary destinations */
2128 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2129 *temp
; /* Current temporary dest */
2130 const char *val
; /* Value of temporary option */
2131 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2135 * Range check the input...
2138 if (!num_dests
|| !dests
)
2142 * Get the server destinations...
2145 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, 0);
2147 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2149 cupsFreeDests(num_temps
, temps
);
2154 * Figure out which file to write to...
2157 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2163 * Merge in server defaults...
2166 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2169 * Point to user defaults...
2172 if ((home
= getenv("HOME")) != NULL
)
2175 * Create ~/.cups subdirectory...
2178 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2179 if (access(filename
, 0))
2180 mkdir(filename
, 0700);
2182 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2188 * Try to open the file...
2191 if ((fp
= fopen(filename
, "w")) == NULL
)
2193 cupsFreeDests(num_temps
, temps
);
2199 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2204 fchmod(fileno(fp
), 0644);
2208 * Write each printer; each line looks like:
2210 * Dest name[/instance] options
2211 * Default name[/instance] options
2214 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2215 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2217 if (dest
->is_default
)
2219 fprintf(fp
, "Default %s", dest
->name
);
2221 fprintf(fp
, "/%s", dest
->instance
);
2228 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2229 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2231 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2234 * See if this option is a printer attribute; if so, skip it...
2237 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2238 match
->group_tag
== IPP_TAG_PRINTER
)
2242 * See if the server/global options match these; if so, don't
2247 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2248 temp
->options
)) != NULL
&&
2249 !_cups_strcasecmp(val
, option
->value
))
2253 * Options don't match, write to the file...
2258 fprintf(fp
, "Dest %s", dest
->name
);
2260 fprintf(fp
, "/%s", dest
->instance
);
2264 if (option
->value
[0])
2266 if (strchr(option
->value
, ' ') ||
2267 strchr(option
->value
, '\\') ||
2268 strchr(option
->value
, '\"') ||
2269 strchr(option
->value
, '\''))
2272 * Quote the value...
2275 fprintf(fp
, " %s=\"", option
->name
);
2277 for (val
= option
->value
; *val
; val
++)
2279 if (strchr("\"\'\\", *val
))
2290 * Store the literal value...
2293 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2297 fprintf(fp
, " %s", option
->name
);
2305 * Free the temporary destinations and close the file...
2308 cupsFreeDests(num_temps
, temps
);
2314 * Set the default printer for this location - this allows command-line
2315 * and GUI applications to share the same default destination...
2318 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2320 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2322 kCFStringEncodingUTF8
);
2323 /* Default printer name */
2327 _cupsAppleSetDefaultPrinter(name
);
2331 #endif /* __APPLE__ */
2333 #ifdef HAVE_NOTIFY_POST
2335 * Send a notification so that macOS applications can know about the
2339 notify_post("com.apple.printerListChange");
2340 #endif /* HAVE_NOTIFY_POST */
2347 * '_cupsUserDefault()' - Get the user default printer from environment
2348 * variables and location information.
2351 char * /* O - Default printer or NULL */
2352 _cupsUserDefault(char *name
, /* I - Name buffer */
2353 size_t namesize
) /* I - Size of name buffer */
2355 const char *env
; /* LPDEST or PRINTER env variable */
2357 CFStringRef locprinter
; /* Last printer as this location */
2358 #endif /* __APPLE__ */
2361 if ((env
= getenv("LPDEST")) == NULL
)
2362 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2367 strlcpy(name
, env
, namesize
);
2373 * Use location-based defaults if "use last printer" is selected in the
2374 * system preferences...
2377 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2379 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2380 CFRelease(locprinter
);
2385 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2387 return (*name
? name
: NULL
);
2391 * No location-based defaults on this platform...
2396 #endif /* __APPLE__ */
2400 #if _CUPS_LOCATION_DEFAULTS
2402 * 'appleCopyLocations()' - Copy the location history array.
2405 static CFArrayRef
/* O - Location array or NULL */
2406 appleCopyLocations(void)
2408 CFArrayRef locations
; /* Location array */
2412 * Look up the location array in the preferences...
2415 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2416 kCUPSPrintingPrefs
)) == NULL
)
2419 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2421 CFRelease(locations
);
2430 * 'appleCopyNetwork()' - Get the network ID for the current location.
2433 static CFStringRef
/* O - Network ID */
2434 appleCopyNetwork(void)
2436 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2437 CFStringRef key
; /* Current network configuration key */
2438 CFDictionaryRef ip_dict
; /* Network configuration data */
2439 CFStringRef network
= NULL
; /* Current network ID */
2442 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2446 * First use the IPv6 router address, if available, since that will generally
2447 * be a globally-unique link-local address.
2450 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2451 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2453 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2455 if ((network
= CFDictionaryGetValue(ip_dict
,
2456 kSCPropNetIPv6Router
)) != NULL
)
2466 * If that doesn't work, try the IPv4 router address. This isn't as unique
2467 * and will likely be a 10.x.y.z or 192.168.y.z address...
2472 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2473 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2475 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2477 if ((network
= CFDictionaryGetValue(ip_dict
,
2478 kSCPropNetIPv4Router
)) != NULL
)
2488 CFRelease(dynamicStore
);
2493 #endif /* _CUPS_LOCATION_DEFAULTS */
2498 * 'appleGetPaperSize()' - Get the default paper size.
2501 static char * /* O - Default paper size */
2502 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2503 size_t namesize
) /* I - Size of buffer */
2505 CFStringRef defaultPaperID
; /* Default paper ID */
2506 pwg_media_t
*pwgmedia
; /* PWG media size */
2509 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2510 if (!defaultPaperID
||
2511 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2512 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2514 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2515 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2518 CFRelease(defaultPaperID
);
2522 #endif /* __APPLE__ */
2525 #if _CUPS_LOCATION_DEFAULTS
2527 * 'appleGetPrinter()' - Get a printer from the history array.
2530 static CFStringRef
/* O - Printer name or NULL */
2531 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2532 CFStringRef network
, /* I - Network name */
2533 CFIndex
*locindex
) /* O - Index in array */
2535 CFIndex i
, /* Looping var */
2536 count
; /* Number of locations */
2537 CFDictionaryRef location
; /* Current location */
2538 CFStringRef locnetwork
, /* Current network */
2539 locprinter
; /* Current printer */
2542 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2543 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2544 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2546 if ((locnetwork
= CFDictionaryGetValue(location
,
2547 kLocationNetworkKey
)) != NULL
&&
2548 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2549 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2550 (locprinter
= CFDictionaryGetValue(location
,
2551 kLocationPrinterIDKey
)) != NULL
&&
2552 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2557 return (locprinter
);
2563 #endif /* _CUPS_LOCATION_DEFAULTS */
2567 * 'cups_add_dest()' - Add a destination to the array.
2569 * Unlike cupsAddDest(), this function does not check for duplicates.
2572 static cups_dest_t
* /* O - New destination */
2573 cups_add_dest(const char *name
, /* I - Name of destination */
2574 const char *instance
, /* I - Instance or NULL */
2575 int *num_dests
, /* IO - Number of destinations */
2576 cups_dest_t
**dests
) /* IO - Destinations */
2578 int insert
, /* Insertion point */
2579 diff
; /* Result of comparison */
2580 cups_dest_t
*dest
; /* Destination pointer */
2584 * Add new destination...
2587 if (*num_dests
== 0)
2588 dest
= malloc(sizeof(cups_dest_t
));
2590 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2598 * Find where to insert the destination...
2601 if (*num_dests
== 0)
2605 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2613 * Move the array elements as needed...
2616 if (insert
< *num_dests
)
2617 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2622 * Initialize the destination...
2625 dest
= *dests
+ insert
;
2626 dest
->name
= _cupsStrAlloc(name
);
2627 dest
->instance
= _cupsStrAlloc(instance
);
2628 dest
->is_default
= 0;
2629 dest
->num_options
= 0;
2630 dest
->options
= (cups_option_t
*)0;
2638 * 'cups_block_cb()' - Enumeration callback for block API.
2641 static int /* O - 1 to continue, 0 to stop */
2643 cups_dest_block_t block
, /* I - Block */
2644 unsigned flags
, /* I - Destination flags */
2645 cups_dest_t
*dest
) /* I - Destination */
2647 return ((block
)(flags
, dest
));
2649 # endif /* __BLOCKS__ */
2653 * 'cups_compare_dests()' - Compare two destinations.
2656 static int /* O - Result of comparison */
2657 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2658 cups_dest_t
*b
) /* I - Second destination */
2660 int diff
; /* Difference */
2663 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2665 else if (a
->instance
&& b
->instance
)
2666 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2668 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2672 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2675 * 'cups_dnssd_browse_cb()' - Browse for printers.
2679 cups_dnssd_browse_cb(
2680 DNSServiceRef sdRef
, /* I - Service reference */
2681 DNSServiceFlags flags
, /* I - Option flags */
2682 uint32_t interfaceIndex
, /* I - Interface number */
2683 DNSServiceErrorType errorCode
, /* I - Error, if any */
2684 const char *serviceName
, /* I - Name of service/device */
2685 const char *regtype
, /* I - Type of service */
2686 const char *replyDomain
, /* I - Service domain */
2687 void *context
) /* I - Enumeration data */
2689 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2690 /* Enumeration data */
2693 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
));
2696 * Don't do anything on error...
2699 if (errorCode
!= kDNSServiceErr_NoError
)
2706 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2710 # else /* HAVE_AVAHI */
2712 * 'cups_dnssd_browse_cb()' - Browse for printers.
2716 cups_dnssd_browse_cb(
2717 AvahiServiceBrowser
*browser
, /* I - Browser */
2718 AvahiIfIndex interface
, /* I - Interface index (unused) */
2719 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2720 AvahiBrowserEvent event
, /* I - What happened */
2721 const char *name
, /* I - Service name */
2722 const char *type
, /* I - Registration type */
2723 const char *domain
, /* I - Domain */
2724 AvahiLookupResultFlags flags
, /* I - Flags */
2725 void *context
) /* I - Devices array */
2728 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
2729 /* Client information */
2731 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2732 /* Enumeration data */
2739 DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name
, type
, domain
));
2743 case AVAHI_BROWSER_FAILURE
:
2744 DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client
))));
2745 avahi_simple_poll_quit(data
->simple_poll
);
2748 case AVAHI_BROWSER_NEW
:
2750 * This object is new on the network.
2753 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
2756 * This comes from the local machine so ignore it.
2759 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", name
));
2764 * Create a device entry for it if it doesn't yet exist.
2767 cups_dnssd_get_device(data
, name
, type
, domain
);
2771 case AVAHI_BROWSER_REMOVE
:
2772 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
2775 case AVAHI_BROWSER_ALL_FOR_NOW
:
2776 DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW");
2784 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2788 cups_dnssd_client_cb(
2789 AvahiClient
*client
, /* I - Client information (unused) */
2790 AvahiClientState state
, /* I - Current state */
2791 void *context
) /* I - User data (unused) */
2793 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2794 /* Enumeration data */
2799 DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client
, state
, context
));
2802 * If the connection drops, quit.
2805 if (state
== AVAHI_CLIENT_FAILURE
)
2807 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2808 avahi_simple_poll_quit(data
->simple_poll
);
2811 # endif /* HAVE_DNSSD */
2815 * 'cups_dnssd_compare_device()' - Compare two devices.
2818 static int /* O - Result of comparison */
2819 cups_dnssd_compare_devices(
2820 _cups_dnssd_device_t
*a
, /* I - First device */
2821 _cups_dnssd_device_t
*b
) /* I - Second device */
2823 return (strcmp(a
->dest
.name
, b
->dest
.name
));
2828 * 'cups_dnssd_free_device()' - Free the memory used by a device.
2832 cups_dnssd_free_device(
2833 _cups_dnssd_device_t
*device
, /* I - Device */
2834 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
2836 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
2840 DNSServiceRefDeallocate(device
->ref
);
2841 # else /* HAVE_AVAHI */
2843 avahi_record_browser_free(device
->ref
);
2844 # endif /* HAVE_DNSSD */
2846 _cupsStrFree(device
->domain
);
2847 _cupsStrFree(device
->fullName
);
2848 _cupsStrFree(device
->regtype
);
2849 _cupsStrFree(device
->dest
.name
);
2851 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
2858 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
2861 static _cups_dnssd_device_t
* /* O - Device */
2862 cups_dnssd_get_device(
2863 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
2864 const char *serviceName
, /* I - Service name */
2865 const char *regtype
, /* I - Registration type */
2866 const char *replyDomain
) /* I - Domain name */
2868 _cups_dnssd_device_t key
, /* Search key */
2869 *device
; /* Device */
2870 char fullName
[kDNSServiceMaxDomainName
],
2871 /* Full name for query */
2872 name
[128]; /* Queue name */
2875 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
2878 * See if this is an existing device...
2881 cups_queue_name(name
, serviceName
, sizeof(name
));
2883 key
.dest
.name
= name
;
2885 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
2888 * Yes, see if we need to do anything with this...
2891 int update
= 0; /* Non-zero if we need to update */
2893 if (!_cups_strcasecmp(replyDomain
, "local.") &&
2894 _cups_strcasecmp(device
->domain
, replyDomain
))
2897 * Update the "global" listing to use the .local domain name instead.
2900 _cupsStrFree(device
->domain
);
2901 device
->domain
= _cupsStrAlloc(replyDomain
);
2903 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
2904 "domain.", device
->dest
.name
));
2909 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
2910 _cups_strcasecmp(device
->regtype
, regtype
))
2913 * Prefer IPPS over IPP.
2916 _cupsStrFree(device
->regtype
);
2917 device
->regtype
= _cupsStrAlloc(regtype
);
2919 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
2920 device
->dest
.name
));
2927 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
2928 device
->dest
.name
));
2935 * No, add the device...
2938 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
2939 "'%s'.", serviceName
,
2940 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
2943 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
2944 device
->dest
.name
= _cupsStrAlloc(name
);
2945 device
->domain
= _cupsStrAlloc(replyDomain
);
2946 device
->regtype
= _cupsStrAlloc(regtype
);
2948 device
->dest
.num_options
= cupsAddOption("printer-info", serviceName
, 0, &device
->dest
.options
);
2950 cupsArrayAdd(data
->devices
, device
);
2954 * Set the "full name" of this service, which is used for queries...
2958 DNSServiceConstructFullName(fullName
, serviceName
, regtype
, replyDomain
);
2959 # else /* HAVE_AVAHI */
2960 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
, regtype
, replyDomain
);
2961 # endif /* HAVE_DNSSD */
2963 _cupsStrFree(device
->fullName
);
2964 device
->fullName
= _cupsStrAlloc(fullName
);
2969 DNSServiceRefDeallocate(device
->ref
);
2970 # else /* HAVE_AVAHI */
2971 avahi_record_browser_free(device
->ref
);
2972 # endif /* HAVE_DNSSD */
2977 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
2979 DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device
->dest
.name
));
2981 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
2982 device
->state
= _CUPS_DNSSD_NEW
;
2991 * 'cups_dnssd_local_cb()' - Browse for local printers.
2995 cups_dnssd_local_cb(
2996 DNSServiceRef sdRef
, /* I - Service reference */
2997 DNSServiceFlags flags
, /* I - Option flags */
2998 uint32_t interfaceIndex
, /* I - Interface number */
2999 DNSServiceErrorType errorCode
, /* I - Error, if any */
3000 const char *serviceName
, /* I - Name of service/device */
3001 const char *regtype
, /* I - Type of service */
3002 const char *replyDomain
, /* I - Service domain */
3003 void *context
) /* I - Devices array */
3005 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3006 /* Enumeration data */
3007 _cups_dnssd_device_t
*device
; /* Device */
3010 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
));
3013 * Only process "add" data...
3016 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3023 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3026 * Hide locally-registered devices...
3029 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
3034 DNSServiceRefDeallocate(device
->ref
);
3038 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3040 DEBUG_printf(("6cups_dnssd_local_cb: Remove callback for \"%s\".", device
->dest
.name
));
3041 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3044 device
->state
= _CUPS_DNSSD_LOCAL
;
3046 # endif /* HAVE_DNSSD */
3051 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
3053 * Note: This function is needed because avahi_simple_poll_iterate is broken
3054 * and always uses a timeout of 0 (!) milliseconds.
3055 * (https://github.com/lathiat/avahi/issues/127)
3060 static int /* O - Number of file descriptors matching */
3062 struct pollfd
*pollfds
, /* I - File descriptors */
3063 unsigned int num_pollfds
, /* I - Number of file descriptors */
3064 int timeout
, /* I - Timeout in milliseconds (unused) */
3065 void *context
) /* I - User data (unused) */
3067 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3068 /* Enumeration data */
3069 int val
; /* Return value */
3072 DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds
, num_pollfds
, timeout
, context
));
3076 val
= poll(pollfds
, num_pollfds
, _CUPS_DNSSD_MAXTIME
);
3078 DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val
));
3082 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3091 # endif /* HAVE_AVAHI */
3095 * 'cups_dnssd_query_cb()' - Process query data.
3100 cups_dnssd_query_cb(
3101 DNSServiceRef sdRef
, /* I - Service reference */
3102 DNSServiceFlags flags
, /* I - Data flags */
3103 uint32_t interfaceIndex
, /* I - Interface */
3104 DNSServiceErrorType errorCode
, /* I - Error, if any */
3105 const char *fullName
, /* I - Full service name */
3106 uint16_t rrtype
, /* I - Record type */
3107 uint16_t rrclass
, /* I - Record class */
3108 uint16_t rdlen
, /* I - Length of record data */
3109 const void *rdata
, /* I - Record data */
3110 uint32_t ttl
, /* I - Time-to-live */
3111 void *context
) /* I - Enumeration data */
3113 # else /* HAVE_AVAHI */
3115 cups_dnssd_query_cb(
3116 AvahiRecordBrowser
*browser
, /* I - Record browser */
3117 AvahiIfIndex interfaceIndex
,
3118 /* I - Interface index (unused) */
3119 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3120 AvahiBrowserEvent event
, /* I - What happened? */
3121 const char *fullName
, /* I - Service name */
3122 uint16_t rrclass
, /* I - Record class */
3123 uint16_t rrtype
, /* I - Record type */
3124 const void *rdata
, /* I - TXT record */
3125 size_t rdlen
, /* I - Length of TXT record */
3126 AvahiLookupResultFlags flags
, /* I - Flags */
3127 void *context
) /* I - Enumeration data */
3130 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3131 /* Client information */
3133 # endif /* HAVE_DNSSD */
3134 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3135 /* Enumeration data */
3136 char serviceName
[256],/* Service name */
3137 name
[128], /* Queue name */
3138 *ptr
; /* Pointer into string */
3139 _cups_dnssd_device_t dkey
, /* Search key */
3140 *device
; /* Device */
3144 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
));
3147 * Only process "add" data...
3150 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3153 # else /* HAVE_AVAHI */
3154 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
));
3157 * Only process "add" data...
3160 if (event
!= AVAHI_BROWSER_NEW
)
3162 if (event
== AVAHI_BROWSER_FAILURE
)
3163 DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client
))));
3167 # endif /* HAVE_DNSSD */
3170 * Lookup the service in the devices array.
3173 cups_dnssd_unquote(serviceName
, fullName
, sizeof(serviceName
));
3175 if ((ptr
= strstr(serviceName
, "._")) != NULL
)
3178 cups_queue_name(name
, serviceName
, sizeof(name
));
3180 dkey
.dest
.name
= name
;
3182 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
&& device
->state
== _CUPS_DNSSD_NEW
)
3185 * Found it, pull out the make and model from the TXT record and save it...
3188 const uint8_t *txt
, /* Pointer into data */
3189 *txtnext
, /* Next key/value pair */
3190 *txtend
; /* End of entire TXT record */
3191 uint8_t txtlen
; /* Length of current key/value pair */
3192 char key
[256], /* Key string */
3193 value
[256], /* Value string */
3194 make_and_model
[512],
3195 /* Manufacturer and model */
3196 model
[256], /* Model */
3197 uriname
[1024], /* Name for URI */
3198 uri
[1024]; /* Printer URI */
3199 cups_ptype_t type
= CUPS_PRINTER_DISCOVERED
| CUPS_PRINTER_BW
;
3201 int saw_printer_type
= 0;
3202 /* Did we see a printer-type key? */
3204 device
->state
= _CUPS_DNSSD_PENDING
;
3205 make_and_model
[0] = '\0';
3207 strlcpy(model
, "Unknown", sizeof(model
));
3209 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3214 * Read a key/value pair starting with an 8-bit length. Since the
3215 * length is 8 bits and the size of the key/value buffers is 256, we
3216 * don't need to check for overflow...
3221 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3224 txtnext
= txt
+ txtlen
;
3226 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3227 *ptr
++ = (char)*txt
;
3230 if (txt
< txtnext
&& *txt
== '=')
3235 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3236 value
[txtnext
- txt
] = '\0';
3238 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3242 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3246 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3247 !_cups_strcasecmp(key
, "usb_MANU") ||
3248 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3249 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3250 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3251 !_cups_strcasecmp(key
, "usb_MODEL"))
3252 strlcpy(model
, value
, sizeof(model
));
3253 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3255 if (value
[0] == '(')
3258 * Strip parenthesis...
3261 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3264 strlcpy(model
, value
+ 1, sizeof(model
));
3267 strlcpy(model
, value
, sizeof(model
));
3269 else if (!_cups_strcasecmp(key
, "ty"))
3271 strlcpy(model
, value
, sizeof(model
));
3273 if ((ptr
= strchr(model
, ',')) != NULL
)
3276 else if (!_cups_strcasecmp(key
, "note"))
3277 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3278 device
->dest
.num_options
,
3279 &device
->dest
.options
);
3280 else if (!_cups_strcasecmp(key
, "pdl"))
3283 * Look for PDF-capable printers; only PDF-capable printers are shown.
3286 const char *start
, *next
; /* Pointer into value */
3287 int have_pdf
= 0, /* Have PDF? */
3288 have_raster
= 0;/* Have raster format support? */
3290 for (start
= value
; start
&& *start
; start
= next
)
3292 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3297 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3298 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3304 if ((next
= strchr(start
, ',')) != NULL
)
3308 if (!have_pdf
&& !have_raster
)
3309 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3311 else if (!_cups_strcasecmp(key
, "printer-type"))
3314 * Value is either NNNN or 0xXXXX
3317 saw_printer_type
= 1;
3318 type
= (cups_ptype_t
)strtol(value
, NULL
, 0) | CUPS_PRINTER_DISCOVERED
;
3320 else if (!saw_printer_type
)
3322 if (!_cups_strcasecmp(key
, "air") &&
3323 !_cups_strcasecmp(value
, "t"))
3324 type
|= CUPS_PRINTER_AUTHENTICATED
;
3325 else if (!_cups_strcasecmp(key
, "bind") &&
3326 !_cups_strcasecmp(value
, "t"))
3327 type
|= CUPS_PRINTER_BIND
;
3328 else if (!_cups_strcasecmp(key
, "collate") &&
3329 !_cups_strcasecmp(value
, "t"))
3330 type
|= CUPS_PRINTER_COLLATE
;
3331 else if (!_cups_strcasecmp(key
, "color") &&
3332 !_cups_strcasecmp(value
, "t"))
3333 type
|= CUPS_PRINTER_COLOR
;
3334 else if (!_cups_strcasecmp(key
, "copies") &&
3335 !_cups_strcasecmp(value
, "t"))
3336 type
|= CUPS_PRINTER_COPIES
;
3337 else if (!_cups_strcasecmp(key
, "duplex") &&
3338 !_cups_strcasecmp(value
, "t"))
3339 type
|= CUPS_PRINTER_DUPLEX
;
3340 else if (!_cups_strcasecmp(key
, "fax") &&
3341 !_cups_strcasecmp(value
, "t"))
3342 type
|= CUPS_PRINTER_MFP
;
3343 else if (!_cups_strcasecmp(key
, "papercustom") &&
3344 !_cups_strcasecmp(value
, "t"))
3345 type
|= CUPS_PRINTER_VARIABLE
;
3346 else if (!_cups_strcasecmp(key
, "papermax"))
3348 if (!_cups_strcasecmp(value
, "legal-a4"))
3349 type
|= CUPS_PRINTER_SMALL
;
3350 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3351 type
|= CUPS_PRINTER_MEDIUM
;
3352 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3353 type
|= CUPS_PRINTER_LARGE
;
3355 else if (!_cups_strcasecmp(key
, "punch") &&
3356 !_cups_strcasecmp(value
, "t"))
3357 type
|= CUPS_PRINTER_PUNCH
;
3358 else if (!_cups_strcasecmp(key
, "scan") &&
3359 !_cups_strcasecmp(value
, "t"))
3360 type
|= CUPS_PRINTER_MFP
;
3361 else if (!_cups_strcasecmp(key
, "sort") &&
3362 !_cups_strcasecmp(value
, "t"))
3363 type
|= CUPS_PRINTER_SORT
;
3364 else if (!_cups_strcasecmp(key
, "staple") &&
3365 !_cups_strcasecmp(value
, "t"))
3366 type
|= CUPS_PRINTER_STAPLE
;
3371 * Save the printer-xxx values...
3374 if (make_and_model
[0])
3376 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3377 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3379 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3382 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3384 device
->type
= type
;
3385 snprintf(value
, sizeof(value
), "%u", type
);
3386 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3392 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3393 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3394 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3395 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3397 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3399 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3402 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3408 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3411 static const char * /* O - Resolved URI or NULL */
3413 cups_dest_t
*dest
, /* I - Destination */
3414 const char *uri
, /* I - Current printer URI */
3415 int msec
, /* I - Time in milliseconds */
3416 int *cancel
, /* I - Pointer to "cancel" variable */
3417 cups_dest_cb_t cb
, /* I - Callback */
3418 void *user_data
) /* I - User data for callback */
3420 char tempuri
[1024]; /* Temporary URI buffer */
3421 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3425 * Resolve the URI...
3428 resolve
.cancel
= cancel
;
3429 gettimeofday(&resolve
.end_time
, NULL
);
3432 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3433 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3435 while (resolve
.end_time
.tv_usec
>= 1000000)
3437 resolve
.end_time
.tv_sec
++;
3438 resolve
.end_time
.tv_usec
-= 1000000;
3442 resolve
.end_time
.tv_sec
+= 75;
3445 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3447 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3449 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3452 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3458 * Save the resolved URI...
3461 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &dest
->options
);
3463 return (cupsGetOption("device-uri", dest
->num_options
, dest
->options
));
3468 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3471 static int /* O - 1 to continue, 0 to stop */
3472 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3474 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3476 struct timeval curtime
; /* Current time */
3480 * If the cancel variable is set, return immediately.
3483 if (resolve
->cancel
&& *(resolve
->cancel
))
3485 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3490 * Otherwise check the end time...
3493 gettimeofday(&curtime
, NULL
);
3495 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
));
3497 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3498 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3499 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3504 * 'cups_dnssd_unquote()' - Unquote a name string.
3508 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3509 const char *src
, /* I - Source string */
3510 size_t dstsize
) /* I - Size of destination buffer */
3512 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3515 while (*src
&& dst
< dstend
)
3520 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3521 isdigit(src
[2] & 255))
3523 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3535 #endif /* HAVE_DNSSD */
3538 #if defined(HAVE_AVAHI) || defined(HAVE_DNSSD)
3540 * 'cups_elapsed()' - Return the elapsed time in milliseconds.
3543 static int /* O - Elapsed time in milliseconds */
3544 cups_elapsed(struct timeval
*t
) /* IO - Previous time */
3546 int msecs
; /* Milliseconds */
3547 struct timeval nt
; /* New time */
3550 gettimeofday(&nt
, NULL
);
3552 msecs
= (int)(1000 * (nt
.tv_sec
- t
->tv_sec
) + (nt
.tv_usec
- t
->tv_usec
) / 1000);
3558 #endif /* HAVE_AVAHI || HAVE_DNSSD */
3562 * 'cups_enum_dests()' - Enumerate destinations from a specific server.
3565 static int /* O - 1 on success, 0 on failure */
3567 http_t
*http
, /* I - Connection to scheduler */
3568 unsigned flags
, /* I - Enumeration flags */
3569 int msec
, /* I - Timeout in milliseconds, -1 for indefinite */
3570 int *cancel
, /* I - Pointer to "cancel" variable */
3571 cups_ptype_t type
, /* I - Printer type bits */
3572 cups_ptype_t mask
, /* I - Mask for printer type bits */
3573 cups_dest_cb_t cb
, /* I - Callback function */
3574 void *user_data
) /* I - User data */
3576 int i
, /* Looping var */
3577 num_dests
; /* Number of destinations */
3578 cups_dest_t
*dests
= NULL
, /* Destinations */
3579 *dest
; /* Current destination */
3580 const char *defprinter
; /* Default printer */
3581 char name
[1024], /* Copy of printer name */
3582 *instance
, /* Pointer to instance name */
3583 *user_default
; /* User default printer */
3584 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3585 int count
, /* Number of queries started */
3586 completed
, /* Number of completed queries */
3587 remaining
; /* Remainder of timeout */
3588 struct timeval curtime
; /* Current time */
3589 _cups_dnssd_data_t data
; /* Data for callback */
3590 _cups_dnssd_device_t
*device
; /* Current device */
3592 int nfds
, /* Number of files responded */
3593 main_fd
; /* File descriptor for lookups */
3594 DNSServiceRef ipp_ref
= NULL
, /* IPP browser */
3595 local_ipp_ref
= NULL
; /* Local IPP browser */
3597 DNSServiceRef ipps_ref
= NULL
, /* IPPS browser */
3598 local_ipps_ref
= NULL
; /* Local IPPS browser */
3599 # endif /* HAVE_SSL */
3601 struct pollfd pfd
; /* Polling data */
3603 fd_set input
; /* Input set for select() */
3604 struct timeval timeout
; /* Timeout for select() */
3605 # endif /* HAVE_POLL */
3606 # else /* HAVE_AVAHI */
3607 int error
; /* Error value */
3608 AvahiServiceBrowser
*ipp_ref
= NULL
; /* IPP browser */
3610 AvahiServiceBrowser
*ipps_ref
= NULL
; /* IPPS browser */
3611 # endif /* HAVE_SSL */
3612 # endif /* HAVE_DNSSD */
3613 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3616 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
));
3619 * Range check input...
3626 DEBUG_puts("1cups_enum_dests: No callback, returning 0.");
3631 * Get ready to enumerate...
3634 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3635 memset(&data
, 0, sizeof(data
));
3640 data
.user_data
= user_data
;
3641 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
3642 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3644 if (!(mask
& CUPS_PRINTER_DISCOVERED
) || !(type
& CUPS_PRINTER_DISCOVERED
))
3647 * Get the list of local printers and pass them to the callback function...
3650 num_dests
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &dests
, type
, mask
);
3652 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
3654 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
3656 strlcpy(name
, defprinter
, sizeof(name
));
3663 * Separate printer and instance name...
3666 if ((instance
= strchr(name
, '/')) != NULL
)
3670 * Lookup the printer and instance and make it the default...
3673 if ((dest
= cupsGetDest(name
, instance
, num_dests
, dests
)) != NULL
)
3674 dest
->is_default
= 1;
3677 for (i
= num_dests
, dest
= dests
;
3678 i
> 0 && (!cancel
|| !*cancel
);
3681 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3682 const char *device_uri
; /* Device URI */
3683 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3685 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
, dest
))
3688 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3689 if (!dest
->instance
&& (device_uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
&& !strncmp(device_uri
, "dnssd://", 8))
3692 * Add existing queue using service name, etc. so we don't list it again...
3695 char scheme
[32], /* URI scheme */
3696 userpass
[32], /* Username:password */
3697 serviceName
[256], /* Service name (host field) */
3698 resource
[256], /* Resource (options) */
3699 *regtype
, /* Registration type */
3700 *replyDomain
; /* Registration domain */
3701 int port
; /* Port number (not used) */
3703 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
)
3705 if ((regtype
= strstr(serviceName
, "._ipp")) != NULL
)
3709 if ((replyDomain
= strstr(regtype
, "._tcp.")) != NULL
)
3711 replyDomain
[5] = '\0';
3714 if ((device
= cups_dnssd_get_device(&data
, serviceName
, regtype
, replyDomain
)) != NULL
)
3715 device
->state
= _CUPS_DNSSD_ACTIVE
;
3720 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3723 cupsFreeDests(num_dests
, dests
);
3725 if (i
> 0 || msec
== 0)
3730 * Return early if the caller doesn't want to do discovery...
3733 if ((mask
& CUPS_PRINTER_DISCOVERED
) && !(type
& CUPS_PRINTER_DISCOVERED
))
3736 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3738 * Get Bonjour-shared printers...
3741 gettimeofday(&curtime
, NULL
);
3744 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
3746 DEBUG_puts("1cups_enum_dests: Unable to create service browser, returning 0.");
3750 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
3752 ipp_ref
= data
.main_ref
;
3753 if (DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipp._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3755 DEBUG_puts("1cups_enum_dests: Unable to create IPP browser, returning 0.");
3756 DNSServiceRefDeallocate(data
.main_ref
);
3760 local_ipp_ref
= data
.main_ref
;
3761 if (DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
, kDNSServiceInterfaceIndexLocalOnly
, "_ipp._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
) != kDNSServiceErr_NoError
)
3763 DEBUG_puts("1cups_enum_dests: Unable to create local IPP browser, returning 0.");
3764 DNSServiceRefDeallocate(data
.main_ref
);
3769 ipps_ref
= data
.main_ref
;
3770 if (DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0, "_ipps._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
) != kDNSServiceErr_NoError
)
3772 DEBUG_puts("1cups_enum_dests: Unable to create IPPS browser, returning 0.");
3773 DNSServiceRefDeallocate(data
.main_ref
);
3777 local_ipps_ref
= data
.main_ref
;
3778 if (DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
, kDNSServiceInterfaceIndexLocalOnly
, "_ipps._tcp", NULL
, (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
) != kDNSServiceErr_NoError
)
3780 DEBUG_puts("1cups_enum_dests: Unable to create local IPPS browser, returning 0.");
3781 DNSServiceRefDeallocate(data
.main_ref
);
3784 # endif /* HAVE_SSL */
3786 # else /* HAVE_AVAHI */
3787 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
3789 DEBUG_puts("1cups_enum_dests: Unable to create Avahi poll, returning 0.");
3793 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
3795 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
3796 0, cups_dnssd_client_cb
, &data
,
3800 DEBUG_puts("1cups_enum_dests: Unable to create Avahi client, returning 0.");
3801 avahi_simple_poll_free(data
.simple_poll
);
3806 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
)
3808 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPP browser, returning 0.");
3810 avahi_client_free(data
.client
);
3811 avahi_simple_poll_free(data
.simple_poll
);
3817 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
)
3819 DEBUG_puts("1cups_enum_dests: Unable to create Avahi IPPS browser, returning 0.");
3821 avahi_service_browser_free(ipp_ref
);
3822 avahi_client_free(data
.client
);
3823 avahi_simple_poll_free(data
.simple_poll
);
3826 # endif /* HAVE_SSL */
3827 # endif /* HAVE_DNSSD */
3830 remaining
= INT_MAX
;
3834 while (remaining
> 0 && (!cancel
|| !*cancel
))
3837 * Check for input...
3840 DEBUG_printf(("1cups_enum_dests: remaining=%d", remaining
));
3842 cups_elapsed(&curtime
);
3847 pfd
.events
= POLLIN
;
3849 nfds
= poll(&pfd
, 1, remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3853 FD_SET(main_fd
, &input
);
3856 timeout
.tv_usec
= 1000 * (remaining
> _CUPS_DNSSD_MAXTIME
? _CUPS_DNSSD_MAXTIME
: remaining
);
3858 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
3859 # endif /* HAVE_POLL */
3862 DNSServiceProcessResult(data
.main_ref
);
3863 else if (nfds
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
3866 # else /* HAVE_AVAHI */
3869 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, _CUPS_DNSSD_MAXTIME
)) > 0)
3872 * We've been told to exit the loop. Perhaps the connection to
3879 DEBUG_printf(("1cups_enum_dests: got_data=%d", data
.got_data
));
3880 # endif /* HAVE_DNSSD */
3882 remaining
-= cups_elapsed(&curtime
);
3884 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
3885 count
= 0, completed
= 0;
3887 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
3892 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3895 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
3897 DEBUG_printf(("1cups_enum_dests: Querying '%s'.", device
->fullName
));
3900 device
->ref
= data
.main_ref
;
3902 if (DNSServiceQueryRecord(&(device
->ref
), kDNSServiceFlagsShareConnection
, 0, device
->fullName
, kDNSServiceType_TXT
, kDNSServiceClass_IN
, (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
, &data
) == kDNSServiceErr_NoError
)
3909 device
->state
= _CUPS_DNSSD_ERROR
;
3911 DEBUG_puts("1cups_enum_dests: Query failed.");
3914 # else /* HAVE_AVAHI */
3915 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
)
3917 DEBUG_printf(("1cups_enum_dests: Query ref=%p", device
->ref
));
3922 device
->state
= _CUPS_DNSSD_ERROR
;
3924 DEBUG_printf(("1cups_enum_dests: Query failed: %s", avahi_strerror(avahi_client_errno(data
.client
))));
3926 # endif /* HAVE_DNSSD */
3928 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
3932 DEBUG_printf(("1cups_enum_dests: Query for \"%s\" is complete.", device
->fullName
));
3934 if ((device
->type
& mask
) == type
)
3936 DEBUG_printf(("1cups_enum_dests: Add callback for \"%s\".", device
->dest
.name
));
3937 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
3944 device
->state
= _CUPS_DNSSD_ACTIVE
;
3949 DEBUG_printf(("1cups_enum_dests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining
, data
.browsers
, completed
, count
, cupsArrayCount(data
.devices
)));
3951 if (data
.browsers
== 0 && completed
== cupsArrayCount(data
.devices
))
3954 DEBUG_printf(("1cups_enum_dests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining
, completed
, count
, cupsArrayCount(data
.devices
)));
3956 if (completed
== cupsArrayCount(data
.devices
))
3958 # endif /* HAVE_AVAHI */
3960 #endif /* HAVE_DNSSD || HAVE_AVAHI */
3968 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
3969 cupsArrayDelete(data
.devices
);
3973 DNSServiceRefDeallocate(ipp_ref
);
3975 DNSServiceRefDeallocate(local_ipp_ref
);
3979 DNSServiceRefDeallocate(ipps_ref
);
3981 DNSServiceRefDeallocate(local_ipps_ref
);
3982 # endif /* HAVE_SSL */
3985 DNSServiceRefDeallocate(data
.main_ref
);
3987 # else /* HAVE_AVAHI */
3989 avahi_service_browser_free(ipp_ref
);
3992 avahi_service_browser_free(ipps_ref
);
3993 # endif /* HAVE_SSL */
3996 avahi_client_free(data
.client
);
3997 if (data
.simple_poll
)
3998 avahi_simple_poll_free(data
.simple_poll
);
3999 # endif /* HAVE_DNSSD */
4000 #endif /* HAVE_DNSSD || HAVE_AVAHI */
4002 DEBUG_puts("1cups_enum_dests: Returning 1.");
4009 * 'cups_find_dest()' - Find a destination using a binary search.
4012 static int /* O - Index of match */
4013 cups_find_dest(const char *name
, /* I - Destination name */
4014 const char *instance
, /* I - Instance or NULL */
4015 int num_dests
, /* I - Number of destinations */
4016 cups_dest_t
*dests
, /* I - Destinations */
4017 int prev
, /* I - Previous index */
4018 int *rdiff
) /* O - Difference of match */
4020 int left
, /* Low mark for binary search */
4021 right
, /* High mark for binary search */
4022 current
, /* Current index */
4023 diff
; /* Result of comparison */
4024 cups_dest_t key
; /* Search key */
4027 key
.name
= (char *)name
;
4028 key
.instance
= (char *)instance
;
4033 * Start search on either side of previous...
4036 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
4037 (diff
< 0 && prev
== 0) ||
4038 (diff
> 0 && prev
== (num_dests
- 1)))
4046 * Start with previous on right side...
4055 * Start wih previous on left side...
4059 right
= num_dests
- 1;
4065 * Start search in the middle...
4069 right
= num_dests
- 1;
4074 current
= (left
+ right
) / 2;
4075 diff
= cups_compare_dests(&key
, dests
+ current
);
4084 while ((right
- left
) > 1);
4089 * Check the last 1 or 2 elements...
4092 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
4096 diff
= cups_compare_dests(&key
, dests
+ right
);
4102 * Return the closest destination and the difference...
4112 * 'cups_get_cb()' - Collect enumerated destinations.
4115 static int /* O - 1 to continue, 0 to stop */
4116 cups_get_cb(_cups_getdata_t
*data
, /* I - Data from cupsGetDests */
4117 unsigned flags
, /* I - Enumeration flags */
4118 cups_dest_t
*dest
) /* I - Destination */
4120 if (flags
& CUPS_DEST_FLAGS_REMOVED
)
4123 * Remove destination from array...
4126 data
->num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, data
->num_dests
, &data
->dests
);
4131 * Add destination to array...
4134 data
->num_dests
= cupsCopyDest(dest
, data
->num_dests
, &data
->dests
);
4142 * 'cups_get_default()' - Get the default destination from an lpoptions file.
4145 static char * /* O - Default destination or NULL */
4146 cups_get_default(const char *filename
, /* I - File to read */
4147 char *namebuf
, /* I - Name buffer */
4148 size_t namesize
, /* I - Size of name buffer */
4149 const char **instance
) /* I - Instance */
4151 cups_file_t
*fp
; /* lpoptions file */
4152 char line
[8192], /* Line from file */
4153 *value
, /* Value for line */
4154 *nameptr
; /* Pointer into name */
4155 int linenum
; /* Current line */
4160 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
4164 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
4166 if (!_cups_strcasecmp(line
, "default") && value
)
4168 strlcpy(namebuf
, value
, namesize
);
4170 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
4172 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
4175 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
4178 *instance
= nameptr
;
4186 return (*namebuf
? namebuf
: NULL
);
4191 * 'cups_get_dests()' - Get destinations from a file.
4194 static int /* O - Number of destinations */
4196 const char *filename
, /* I - File to read from */
4197 const char *match_name
, /* I - Destination name we want */
4198 const char *match_inst
, /* I - Instance name we want */
4199 int user_default_set
, /* I - User default printer set? */
4200 int num_dests
, /* I - Number of destinations */
4201 cups_dest_t
**dests
) /* IO - Destinations */
4203 int i
; /* Looping var */
4204 cups_dest_t
*dest
; /* Current destination */
4205 cups_file_t
*fp
; /* File pointer */
4206 char line
[8192], /* Line from file */
4207 *lineptr
, /* Pointer into line */
4208 *name
, /* Name of destination/option */
4209 *instance
; /* Instance of destination */
4210 int linenum
; /* Current line number */
4213 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
));
4216 * Try to open the file...
4219 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
4223 * Read each printer; each line looks like:
4225 * Dest name[/instance] options
4226 * Default name[/instance] options
4231 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
4234 * See what type of line it is...
4237 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
4238 linenum
, line
, lineptr
));
4240 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
4242 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
4249 * Search for an instance...
4252 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
4255 if (*lineptr
== '/')
4258 * Found an instance...
4265 * Search for an instance...
4268 while (!isspace(*lineptr
& 255) && *lineptr
)
4277 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
4281 * See if the primary instance of the destination exists; if not,
4282 * ignore this entry and move on...
4287 if (_cups_strcasecmp(name
, match_name
) ||
4288 (!instance
&& match_inst
) ||
4289 (instance
&& !match_inst
) ||
4290 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
4295 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
4297 DEBUG_puts("9cups_get_dests: Not found!");
4303 * Add the destination...
4306 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
4308 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
4314 DEBUG_puts("9cups_get_dests: Out of memory!");
4320 * Add options until we hit the end of the line...
4323 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
4327 * If we found what we were looking for, stop now...
4334 * Set this as default if needed...
4337 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
4339 DEBUG_puts("9cups_get_dests: Setting as default...");
4341 for (i
= 0; i
< num_dests
; i
++)
4342 (*dests
)[i
].is_default
= 0;
4344 dest
->is_default
= 1;
4349 * Close the file and return...
4359 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
4363 static char * /* O - New string */
4365 ipp_attribute_t
*attr
, /* I - Attribute to convert */
4366 char *buffer
, /* I - Buffer */
4367 size_t bufsize
) /* I - Size of buffer */
4369 int i
; /* Looping var */
4370 char *ptr
, /* Pointer into buffer */
4371 *end
, /* Pointer to end of buffer */
4372 *valptr
; /* Pointer into string attribute */
4376 * Return quickly if we have a single string value...
4379 if (attr
->num_values
== 1 &&
4380 attr
->value_tag
!= IPP_TAG_INTEGER
&&
4381 attr
->value_tag
!= IPP_TAG_ENUM
&&
4382 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
4383 attr
->value_tag
!= IPP_TAG_RANGE
)
4384 return (attr
->values
[0].string
.text
);
4387 * Copy the values to the string, separating with commas and escaping strings
4391 end
= buffer
+ bufsize
- 1;
4393 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
4398 switch (attr
->value_tag
)
4400 case IPP_TAG_INTEGER
:
4402 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
4405 case IPP_TAG_BOOLEAN
:
4406 if (attr
->values
[i
].boolean
)
4407 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
4409 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
4412 case IPP_TAG_RANGE
:
4413 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
4414 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4416 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4420 for (valptr
= attr
->values
[i
].string
.text
;
4421 *valptr
&& ptr
< end
;)
4423 if (strchr(" \t\n\\\'\"", *valptr
))
4425 if (ptr
>= (end
- 1))
4448 * 'cups_name_cb()' - Find an enumerated destination.
4451 static int /* O - 1 to continue, 0 to stop */
4452 cups_name_cb(_cups_namedata_t
*data
, /* I - Data from cupsGetNamedDest */
4453 unsigned flags
, /* I - Enumeration flags */
4454 cups_dest_t
*dest
) /* I - Destination */
4456 DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", (void *)data
, data
->name
, flags
, (void *)dest
, dest
->name
));
4458 if (!(flags
& CUPS_DEST_FLAGS_REMOVED
) && !dest
->instance
&& !strcasecmp(data
->name
, dest
->name
))
4461 * Copy destination and stop enumeration...
4464 cupsCopyDest(dest
, 0, &data
->dest
);
4473 * 'cups_queue_name()' - Create a local queue name based on the service name.
4478 char *name
, /* I - Name buffer */
4479 const char *serviceName
, /* I - Service name */
4480 size_t namesize
) /* I - Size of name buffer */
4482 const char *ptr
; /* Pointer into serviceName */
4483 char *nameptr
; /* Pointer into name */
4486 for (nameptr
= name
, ptr
= serviceName
; *ptr
&& nameptr
< (name
+ namesize
- 1); ptr
++)
4489 * Sanitize the printer name...
4492 if (_cups_isalnum(*ptr
))
4494 else if (nameptr
== name
|| nameptr
[-1] != '_')