2 * User-defined destination (and option) support for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
25 #endif /* HAVE_NOTIFY_H */
29 #endif /* HAVE_POLL */
33 #endif /* HAVE_DNSSD */
36 # include <avahi-client/client.h>
37 # include <avahi-client/lookup.h>
38 # include <avahi-common/simple-watch.h>
39 # include <avahi-common/domain.h>
40 # include <avahi-common/error.h>
41 # include <avahi-common/malloc.h>
42 #define kDNSServiceMaxDomainName AVAHI_DOMAIN_NAME_MAX
43 #endif /* HAVE_AVAHI */
52 # include <SystemConfiguration/SystemConfiguration.h>
53 # define _CUPS_LOCATION_DEFAULTS 1
54 # endif /* !TARGET_OS_IOS */
55 # define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
56 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
57 # define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
58 # define kLocationNetworkKey CFSTR("Network")
59 # define kLocationPrinterIDKey CFSTR("PrinterID")
60 # define kUseLastPrinter CFSTR("UseLastPrinter")
61 #endif /* __APPLE__ */
68 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
69 typedef enum _cups_dnssd_state_e
/* Enumerated device state */
76 _CUPS_DNSSD_INCOMPATIBLE
,
78 } _cups_dnssd_state_t
;
80 typedef struct _cups_dnssd_data_s
/* Enumeration data */
83 DNSServiceRef main_ref
; /* Main service reference */
84 # else /* HAVE_AVAHI */
85 AvahiSimplePoll
*simple_poll
; /* Polling interface */
86 AvahiClient
*client
; /* Client information */
87 int got_data
; /* Did we get data? */
88 # endif /* HAVE_DNSSD */
89 cups_dest_cb_t cb
; /* Callback */
90 void *user_data
; /* User data pointer */
91 cups_ptype_t type
, /* Printer type filter */
92 mask
; /* Printer type mask */
93 cups_array_t
*devices
; /* Devices found so far */
96 typedef struct _cups_dnssd_device_s
/* Enumerated device */
98 _cups_dnssd_state_t state
; /* State of device listing */
100 DNSServiceRef ref
; /* Service reference for query */
101 # else /* HAVE_AVAHI */
102 AvahiRecordBrowser
*ref
; /* Browser for query */
103 # endif /* HAVE_DNSSD */
104 char *domain
, /* Domain name */
105 *fullName
, /* Full name */
106 *regtype
; /* Registration type */
107 cups_ptype_t type
; /* Device registration type */
108 cups_dest_t dest
; /* Destination record */
109 } _cups_dnssd_device_t
;
111 typedef struct _cups_dnssd_resolve_s
/* Data for resolving URI */
113 int *cancel
; /* Pointer to "cancel" variable */
114 struct timeval end_time
; /* Ending time */
115 } _cups_dnssd_resolve_t
;
116 #endif /* HAVE_DNSSD */
123 #if _CUPS_LOCATION_DEFAULTS
124 static CFArrayRef
appleCopyLocations(void);
125 static CFStringRef
appleCopyNetwork(void);
126 #endif /* _CUPS_LOCATION_DEFAULTS */
128 static char *appleGetPaperSize(char *name
, size_t namesize
);
129 #endif /* __APPLE__ */
130 #if _CUPS_LOCATION_DEFAULTS
131 static CFStringRef
appleGetPrinter(CFArrayRef locations
,
132 CFStringRef network
, CFIndex
*locindex
);
133 #endif /* _CUPS_LOCATION_DEFAULTS */
134 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
135 int *num_dests
, cups_dest_t
**dests
);
137 static int cups_block_cb(cups_dest_block_t block
, unsigned flags
,
139 #endif /* __BLOCKS__ */
140 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
141 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
143 static void cups_dnssd_browse_cb(DNSServiceRef sdRef
,
144 DNSServiceFlags flags
,
145 uint32_t interfaceIndex
,
146 DNSServiceErrorType errorCode
,
147 const char *serviceName
,
149 const char *replyDomain
,
151 # else /* HAVE_AVAHI */
152 static void cups_dnssd_browse_cb(AvahiServiceBrowser
*browser
,
153 AvahiIfIndex interface
,
154 AvahiProtocol protocol
,
155 AvahiBrowserEvent event
,
156 const char *serviceName
,
158 const char *replyDomain
,
159 AvahiLookupResultFlags flags
,
161 static void cups_dnssd_client_cb(AvahiClient
*client
,
162 AvahiClientState state
,
164 # endif /* HAVE_DNSSD */
165 static int cups_dnssd_compare_devices(_cups_dnssd_device_t
*a
,
166 _cups_dnssd_device_t
*b
);
167 static void cups_dnssd_free_device(_cups_dnssd_device_t
*device
,
168 _cups_dnssd_data_t
*data
);
169 static _cups_dnssd_device_t
*
170 cups_dnssd_get_device(_cups_dnssd_data_t
*data
,
171 const char *serviceName
,
173 const char *replyDomain
);
175 static void cups_dnssd_local_cb(DNSServiceRef sdRef
,
176 DNSServiceFlags flags
,
177 uint32_t interfaceIndex
,
178 DNSServiceErrorType errorCode
,
179 const char *serviceName
,
181 const char *replyDomain
,
183 static void cups_dnssd_query_cb(DNSServiceRef sdRef
,
184 DNSServiceFlags flags
,
185 uint32_t interfaceIndex
,
186 DNSServiceErrorType errorCode
,
187 const char *fullName
,
188 uint16_t rrtype
, uint16_t rrclass
,
189 uint16_t rdlen
, const void *rdata
,
190 uint32_t ttl
, void *context
);
191 # else /* HAVE_AVAHI */
192 static int cups_dnssd_poll_cb(struct pollfd
*pollfds
,
193 unsigned int num_pollfds
,
194 int timeout
, void *context
);
195 static void cups_dnssd_query_cb(AvahiRecordBrowser
*browser
,
196 AvahiIfIndex interface
,
197 AvahiProtocol protocol
,
198 AvahiBrowserEvent event
,
199 const char *name
, uint16_t rrclass
,
200 uint16_t rrtype
, const void *rdata
,
202 AvahiLookupResultFlags flags
,
204 # endif /* HAVE_DNSSD */
205 static const char *cups_dnssd_resolve(cups_dest_t
*dest
, const char *uri
,
206 int msec
, int *cancel
,
207 cups_dest_cb_t cb
, void *user_data
);
208 static int cups_dnssd_resolve_cb(void *context
);
209 static void cups_dnssd_unquote(char *dst
, const char *src
,
211 #endif /* HAVE_DNSSD || HAVE_AVAHI */
212 static int cups_find_dest(const char *name
, const char *instance
,
213 int num_dests
, cups_dest_t
*dests
, int prev
,
215 static char *cups_get_default(const char *filename
, char *namebuf
,
216 size_t namesize
, const char **instance
);
217 static int cups_get_dests(const char *filename
, const char *match_name
,
218 const char *match_inst
, int user_default_set
,
219 int num_dests
, cups_dest_t
**dests
);
220 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
225 * 'cupsAddDest()' - Add a destination to the list of destinations.
227 * This function cannot be used to add a new class or printer queue,
228 * it only adds a new container of saved options for the named
229 * destination or instance.
231 * If the named destination already exists, the destination list is
232 * returned unchanged. Adding a new instance of a destination creates
233 * a copy of that destination's options.
235 * Use the @link cupsSaveDests@ function to save the updated list of
236 * destinations to the user's lpoptions file.
239 int /* O - New number of destinations */
240 cupsAddDest(const char *name
, /* I - Destination name */
241 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
242 int num_dests
, /* I - Number of destinations */
243 cups_dest_t
**dests
) /* IO - Destinations */
245 int i
; /* Looping var */
246 cups_dest_t
*dest
; /* Destination pointer */
247 cups_dest_t
*parent
= NULL
; /* Parent destination */
248 cups_option_t
*doption
, /* Current destination option */
249 *poption
; /* Current parent option */
255 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
257 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
260 if ((dest
= cups_add_dest(name
, instance
, &num_dests
, dests
)) == NULL
)
264 * Find the base dest again now the array has been realloc'd.
267 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
269 if (instance
&& parent
&& parent
->num_options
> 0)
272 * Copy options from parent...
275 dest
->options
= calloc(sizeof(cups_option_t
), (size_t)parent
->num_options
);
279 dest
->num_options
= parent
->num_options
;
281 for (i
= dest
->num_options
, doption
= dest
->options
,
282 poption
= parent
->options
;
284 i
--, doption
++, poption
++)
286 doption
->name
= _cupsStrRetain(poption
->name
);
287 doption
->value
= _cupsStrRetain(poption
->value
);
299 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
302 CFStringRef
/* O - Default paper ID */
303 _cupsAppleCopyDefaultPaperID(void)
305 return (CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
306 kCUPSPrintingPrefs
));
311 * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
314 CFStringRef
/* O - Default printer name */
315 _cupsAppleCopyDefaultPrinter(void)
317 # if _CUPS_LOCATION_DEFAULTS
318 CFStringRef network
; /* Network location */
319 CFArrayRef locations
; /* Location array */
320 CFStringRef locprinter
; /* Current printer */
324 * Use location-based defaults only if "use last printer" is selected in the
325 * system preferences...
328 if (!_cupsAppleGetUseLastPrinter())
330 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
336 * Get the current location...
339 if ((network
= appleCopyNetwork()) == NULL
)
341 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
347 * Lookup the network in the preferences...
350 if ((locations
= appleCopyLocations()) == NULL
)
353 * Missing or bad location array, so no location-based default...
356 DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
364 DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
365 (int)CFArrayGetCount(locations
)));
367 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
368 CFRetain(locprinter
);
371 CFRelease(locations
);
377 # endif /* _CUPS_LOCATION_DEFAULTS */
382 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
385 int /* O - 1 to use last printer, 0 otherwise */
386 _cupsAppleGetUseLastPrinter(void)
388 Boolean uselast
, /* Use last printer preference value */
389 uselast_set
; /* Valid is set? */
392 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
395 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinter
,
406 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
410 _cupsAppleSetDefaultPaperID(
411 CFStringRef name
) /* I - New paper ID */
413 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPrefs
);
414 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
416 # ifdef HAVE_NOTIFY_POST
417 notify_post("com.apple.printerPrefsChange");
418 # endif /* HAVE_NOTIFY_POST */
423 * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
427 _cupsAppleSetDefaultPrinter(
428 CFStringRef name
) /* I - Default printer/class name */
430 # if _CUPS_LOCATION_DEFAULTS
431 CFStringRef network
; /* Current network */
432 CFArrayRef locations
; /* Old locations array */
433 CFIndex locindex
; /* Index in locations array */
434 CFStringRef locprinter
; /* Current printer */
435 CFMutableArrayRef newlocations
; /* New locations array */
436 CFMutableDictionaryRef newlocation
; /* New location */
440 * Get the current location...
443 if ((network
= appleCopyNetwork()) == NULL
)
445 DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
450 * Lookup the network in the preferences...
453 if ((locations
= appleCopyLocations()) != NULL
)
454 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
461 if (!locprinter
|| CFStringCompare(locprinter
, name
, 0) != kCFCompareEqualTo
)
464 * Need to change the locations array...
469 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
473 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
476 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
477 &kCFTypeArrayCallBacks
);
479 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
480 &kCFTypeDictionaryKeyCallBacks
,
481 &kCFTypeDictionaryValueCallBacks
);
483 if (newlocation
&& newlocations
)
486 * Put the new location at the front of the array...
489 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
490 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, name
);
491 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
494 * Limit the number of locations to 10...
497 while (CFArrayGetCount(newlocations
) > 10)
498 CFArrayRemoveValueAtIndex(newlocations
, 10);
501 * Push the changes out...
504 CFPreferencesSetAppValue(kLastUsedPrintersKey
, newlocations
,
506 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
508 # ifdef HAVE_NOTIFY_POST
509 notify_post("com.apple.printerPrefsChange");
510 # endif /* HAVE_NOTIFY_POST */
514 CFRelease(newlocations
);
517 CFRelease(newlocation
);
521 CFRelease(locations
);
527 # endif /* _CUPS_LOCATION_DEFAULTS */
532 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
536 _cupsAppleSetUseLastPrinter(
537 int uselast
) /* O - 1 to use last printer, 0 otherwise */
539 CFPreferencesSetAppValue(kUseLastPrinter
,
540 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
542 CFPreferencesAppSynchronize(kCUPSPrintingPrefs
);
544 # ifdef HAVE_NOTIFY_POST
545 notify_post("com.apple.printerPrefsChange");
546 # endif /* HAVE_NOTIFY_POST */
548 #endif /* __APPLE__ */
552 * 'cupsConnectDest()' - Connect to the server for a destination.
554 * Connect to the destination, returning a new http_t connection object and
555 * optionally the resource path to use for the destination. These calls will
556 * block until a connection is made, the timeout expires, the integer pointed
557 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
558 * The caller is responsible for calling httpClose() on the returned object.
560 * @since CUPS 1.6/macOS 10.8@
563 http_t
* /* O - Connection to server or @code NULL@ */
565 cups_dest_t
*dest
, /* I - Destination */
566 unsigned flags
, /* I - Connection flags */
567 int msec
, /* I - Timeout in milliseconds */
568 int *cancel
, /* I - Pointer to "cancel" variable */
569 char *resource
, /* I - Resource buffer */
570 size_t resourcesize
, /* I - Size of resource buffer */
571 cups_dest_cb_t cb
, /* I - Callback function */
572 void *user_data
) /* I - User data pointer */
574 const char *uri
; /* Printer URI */
575 char scheme
[32], /* URI scheme */
576 userpass
[256], /* Username and password (unused) */
577 hostname
[256], /* Hostname */
578 tempresource
[1024]; /* Temporary resource buffer */
579 int port
; /* Port number */
580 char portstr
[16]; /* Port number string */
581 http_encryption_t encryption
; /* Encryption to use */
582 http_addrlist_t
*addrlist
; /* Address list for server */
583 http_t
*http
; /* Connection to server */
586 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
));
589 * Range check input...
597 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
601 if (!resource
|| resourcesize
< 1)
603 resource
= tempresource
;
604 resourcesize
= sizeof(tempresource
);
608 * Grab the printer URI...
611 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
)) == NULL
)
613 if ((uri
= cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
)) == NULL
)
615 if ((uri
= cupsGetOption("device-uri", dest
->num_options
, dest
->options
)) != NULL
)
617 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
618 if (strstr(uri
, "._tcp"))
619 uri
= cups_dnssd_resolve(dest
, uri
, msec
, cancel
, cb
, user_data
);
620 #endif /* HAVE_DNSSD || HAVE_AVAHI */
625 uri
= _cupsCreateDest(dest
->name
, cupsGetOption("printer-info", dest
->num_options
, dest
->options
), NULL
, uri
, tempresource
, sizeof(tempresource
));
629 dest
->num_options
= cupsAddOption("printer-uri-supported", uri
, dest
->num_options
, &dest
->options
);
631 uri
= cupsGetOption("printer-uri-supported", dest
->num_options
, dest
->options
);
637 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
640 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
645 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
646 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
647 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
649 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
652 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
,
659 * Lookup the address for the server...
663 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
665 snprintf(portstr
, sizeof(portstr
), "%d", port
);
667 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portstr
)) == NULL
)
670 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
675 if (cancel
&& *cancel
)
677 httpAddrFreeList(addrlist
);
680 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CANCELED
, dest
);
686 * Create the HTTP object pointing to the server referenced by the URI...
689 if (!strcmp(scheme
, "ipps") || port
== 443)
690 encryption
= HTTP_ENCRYPTION_ALWAYS
;
692 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
694 http
= httpConnect2(hostname
, port
, addrlist
, AF_UNSPEC
, encryption
, 1, 0, NULL
);
695 httpAddrFreeList(addrlist
);
698 * Connect if requested...
701 if (flags
& CUPS_DEST_FLAGS_UNCONNECTED
)
704 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
, dest
);
709 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
711 if (!httpReconnect2(http
, msec
, cancel
) && cb
)
713 if (cancel
&& *cancel
)
714 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_CONNECTING
, dest
);
716 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
719 (*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, dest
);
728 * 'cupsConnectDestBlock()' - Connect to the server for a destination.
730 * Connect to the destination, returning a new http_t connection object and
731 * optionally the resource path to use for the destination. These calls will
732 * block until a connection is made, the timeout expires, the integer pointed
733 * to by "cancel" is non-zero, or the callback function (or block) returns 0,
734 * The caller is responsible for calling httpClose() on the returned object.
736 * @since CUPS 1.6/macOS 10.8@
739 http_t
* /* O - Connection to server or @code NULL@ */
740 cupsConnectDestBlock(
741 cups_dest_t
*dest
, /* I - Destination */
742 unsigned flags
, /* I - Connection flags */
743 int msec
, /* I - Timeout in milliseconds */
744 int *cancel
, /* I - Pointer to "cancel" variable */
745 char *resource
, /* I - Resource buffer */
746 size_t resourcesize
, /* I - Size of resource buffer */
747 cups_dest_block_t block
) /* I - Callback block */
749 return (cupsConnectDest(dest
, flags
, msec
, cancel
, resource
, resourcesize
,
750 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
752 #endif /* __BLOCKS__ */
756 * 'cupsCopyDest()' - Copy a destination.
758 * Make a copy of the destination to an array of destinations (or just a single
759 * copy) - for use with the cupsEnumDests* functions. The caller is responsible
760 * for calling cupsFreeDests() on the returned object(s).
762 * @since CUPS 1.6/macOS 10.8@
766 cupsCopyDest(cups_dest_t
*dest
,
770 int i
; /* Looping var */
771 cups_dest_t
*new_dest
; /* New destination pointer */
772 cups_option_t
*new_option
, /* Current destination option */
773 *option
; /* Current parent option */
777 * Range check input...
780 if (!dest
|| num_dests
< 0 || !dests
)
784 * See if the destination already exists...
787 if ((new_dest
= cupsGetDest(dest
->name
, dest
->instance
, num_dests
,
791 * Protect against copying destination to itself...
794 if (new_dest
== dest
)
798 * Otherwise, free the options...
801 cupsFreeOptions(new_dest
->num_options
, new_dest
->options
);
803 new_dest
->num_options
= 0;
804 new_dest
->options
= NULL
;
807 new_dest
= cups_add_dest(dest
->name
, dest
->instance
, &num_dests
, dests
);
811 if ((new_dest
->options
= calloc(sizeof(cups_option_t
), (size_t)dest
->num_options
)) == NULL
)
812 return (cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
, dests
));
814 new_dest
->num_options
= dest
->num_options
;
816 for (i
= dest
->num_options
, option
= dest
->options
,
817 new_option
= new_dest
->options
;
819 i
--, option
++, new_option
++)
821 new_option
->name
= _cupsStrRetain(option
->name
);
822 new_option
->value
= _cupsStrRetain(option
->value
);
831 * '_cupsCreateDest()' - Create a local (temporary) queue.
834 char * /* O - Printer URI or @code NULL@ on error */
835 _cupsCreateDest(const char *name
, /* I - Printer name */
836 const char *info
, /* I - Printer description of @code NULL@ */
837 const char *device_id
, /* I - 1284 Device ID or @code NULL@ */
838 const char *device_uri
, /* I - Device URI */
839 char *uri
, /* I - Printer URI buffer */
840 size_t urisize
) /* I - Size of URI buffer */
842 http_t
*http
; /* Connection to server */
843 ipp_t
*request
, /* CUPS-Create-Local-Printer request */
844 *response
; /* CUPS-Create-Local-Printer response */
845 ipp_attribute_t
*attr
; /* printer-uri-supported attribute */
846 ipp_pstate_t state
= IPP_PSTATE_STOPPED
;
847 /* printer-state value */
850 if (!name
|| !device_uri
|| !uri
|| urisize
< 32)
853 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, HTTP_ENCRYPTION_IF_REQUESTED
, 1, 30000, NULL
)) == NULL
)
856 request
= ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER
);
858 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, "ipp://localhost/");
859 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
861 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
, device_uri
);
862 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
864 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, info
);
866 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
868 response
= cupsDoRequest(http
, request
, "/");
870 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
871 strlcpy(uri
, ippGetString(attr
, 0, NULL
), urisize
);
879 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
880 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
882 while (state
== IPP_PSTATE_STOPPED
&& cupsLastError() == IPP_STATUS_OK
)
887 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
889 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
890 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name", NULL
, cupsUser());
891 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", NULL
, "printer-state");
893 response
= cupsDoRequest(http
, request
, "/");
895 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
896 state
= (ipp_pstate_t
)ippGetInteger(attr
, 0);
908 * 'cupsEnumDests()' - Enumerate available destinations with a callback function.
910 * Destinations are enumerated from one or more sources. The callback function
911 * receives the @code user_data@ pointer, destination name, instance, number of
912 * options, and options which can be used as input to the @link cupsAddDest@
913 * function. The function must return 1 to continue enumeration or 0 to stop.
915 * Enumeration happens on the current thread and does not return until all
916 * destinations have been enumerated or the callback function returns 0.
918 * @since CUPS 1.6/macOS 10.8@
921 int /* O - 1 on success, 0 on failure */
923 unsigned flags
, /* I - Enumeration flags */
924 int msec
, /* I - Timeout in milliseconds,
925 * -1 for indefinite */
926 int *cancel
, /* I - Pointer to "cancel" variable */
927 cups_ptype_t type
, /* I - Printer type bits */
928 cups_ptype_t mask
, /* I - Mask for printer type bits */
929 cups_dest_cb_t cb
, /* I - Callback function */
930 void *user_data
) /* I - User data */
932 int i
, /* Looping var */
933 num_dests
; /* Number of destinations */
934 cups_dest_t
*dests
= NULL
, /* Destinations */
935 *dest
; /* Current destination */
936 const char *defprinter
; /* Default printer */
937 char name
[1024], /* Copy of printer name */
938 *instance
, /* Pointer to instance name */
939 *user_default
; /* User default printer */
940 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
941 int count
, /* Number of queries started */
942 remaining
; /* Remainder of timeout */
943 _cups_dnssd_data_t data
; /* Data for callback */
944 _cups_dnssd_device_t
*device
; /* Current device */
946 int nfds
, /* Number of files responded */
947 main_fd
; /* File descriptor for lookups */
948 DNSServiceRef ipp_ref
, /* IPP browser */
949 local_ipp_ref
; /* Local IPP browser */
951 DNSServiceRef ipps_ref
, /* IPPS browser */
952 local_ipps_ref
; /* Local IPPS browser */
953 # endif /* HAVE_SSL */
955 struct pollfd pfd
; /* Polling data */
957 fd_set input
; /* Input set for select() */
958 struct timeval timeout
; /* Timeout for select() */
959 # endif /* HAVE_POLL */
960 # else /* HAVE_AVAHI */
961 int error
; /* Error value */
962 AvahiServiceBrowser
*ipp_ref
; /* IPP browser */
964 AvahiServiceBrowser
*ipps_ref
; /* IPPS browser */
965 # endif /* HAVE_SSL */
966 # endif /* HAVE_DNSSD */
967 #endif /* HAVE_DNSSD || HAVE_AVAHI */
970 * Range check input...
979 * Get the list of local printers and pass them to the callback function...
982 num_dests
= _cupsGetDests(CUPS_HTTP_DEFAULT
, IPP_OP_CUPS_GET_PRINTERS
, NULL
,
983 &dests
, type
, mask
| CUPS_PRINTER_3D
);
985 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
987 else if ((defprinter
= cupsGetDefault2(CUPS_HTTP_DEFAULT
)) != NULL
)
989 strlcpy(name
, defprinter
, sizeof(name
));
996 * Separate printer and instance name...
999 if ((instance
= strchr(name
, '/')) != NULL
)
1003 * Lookup the printer and instance and make it the default...
1006 if ((dest
= cupsGetDest(name
, instance
, num_dests
, dests
)) != NULL
)
1007 dest
->is_default
= 1;
1010 for (i
= num_dests
, dest
= dests
;
1011 i
> 0 && (!cancel
|| !*cancel
);
1013 if (!(*cb
)(user_data
, i
> 1 ? CUPS_DEST_FLAGS_MORE
: CUPS_DEST_FLAGS_NONE
,
1017 cupsFreeDests(num_dests
, dests
);
1019 if (i
> 0 || msec
== 0)
1022 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1024 * Get Bonjour-shared printers...
1030 data
.user_data
= user_data
;
1031 data
.devices
= cupsArrayNew3((cups_array_func_t
)cups_dnssd_compare_devices
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)cups_dnssd_free_device
);
1034 if (DNSServiceCreateConnection(&data
.main_ref
) != kDNSServiceErr_NoError
)
1037 main_fd
= DNSServiceRefSockFD(data
.main_ref
);
1039 ipp_ref
= data
.main_ref
;
1040 DNSServiceBrowse(&ipp_ref
, kDNSServiceFlagsShareConnection
, 0,
1042 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1044 local_ipp_ref
= data
.main_ref
;
1045 DNSServiceBrowse(&local_ipp_ref
, kDNSServiceFlagsShareConnection
,
1046 kDNSServiceInterfaceIndexLocalOnly
,
1048 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1051 ipps_ref
= data
.main_ref
;
1052 DNSServiceBrowse(&ipps_ref
, kDNSServiceFlagsShareConnection
, 0,
1054 (DNSServiceBrowseReply
)cups_dnssd_browse_cb
, &data
);
1056 local_ipps_ref
= data
.main_ref
;
1057 DNSServiceBrowse(&local_ipps_ref
, kDNSServiceFlagsShareConnection
,
1058 kDNSServiceInterfaceIndexLocalOnly
,
1060 (DNSServiceBrowseReply
)cups_dnssd_local_cb
, &data
);
1061 # endif /* HAVE_SSL */
1063 # else /* HAVE_AVAHI */
1064 if ((data
.simple_poll
= avahi_simple_poll_new()) == NULL
)
1066 DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object.");
1070 avahi_simple_poll_set_func(data
.simple_poll
, cups_dnssd_poll_cb
, &data
);
1072 data
.client
= avahi_client_new(avahi_simple_poll_get(data
.simple_poll
),
1073 0, cups_dnssd_client_cb
, &data
,
1077 DEBUG_puts("cupsEnumDests: Unable to create Avahi client.");
1078 avahi_simple_poll_free(data
.simple_poll
);
1082 ipp_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
,
1083 AVAHI_PROTO_UNSPEC
, "_ipp._tcp", NULL
,
1084 0, cups_dnssd_browse_cb
, &data
);
1086 ipps_ref
= avahi_service_browser_new(data
.client
, AVAHI_IF_UNSPEC
,
1087 AVAHI_PROTO_UNSPEC
, "_ipps._tcp", NULL
,
1088 0, cups_dnssd_browse_cb
, &data
);
1089 # endif /* HAVE_SSL */
1090 # endif /* HAVE_DNSSD */
1093 remaining
= INT_MAX
;
1097 while (remaining
> 0 && (!cancel
|| !*cancel
))
1100 * Check for input...
1106 pfd
.events
= POLLIN
;
1108 nfds
= poll(&pfd
, 1, remaining
> 250 ? 250 : remaining
);
1112 FD_SET(main_fd
, &input
);
1115 timeout
.tv_usec
= remaining
> 250 ? 250000 : remaining
* 1000;
1117 nfds
= select(main_fd
+ 1, &input
, NULL
, NULL
, &timeout
);
1118 # endif /* HAVE_POLL */
1121 DNSServiceProcessResult(data
.main_ref
);
1125 # else /* HAVE_AVAHI */
1128 if ((error
= avahi_simple_poll_iterate(data
.simple_poll
, 250)) > 0)
1131 * We've been told to exit the loop. Perhaps the connection to
1140 # endif /* HAVE_DNSSD */
1142 for (device
= (_cups_dnssd_device_t
*)cupsArrayFirst(data
.devices
),
1145 device
= (_cups_dnssd_device_t
*)cupsArrayNext(data
.devices
))
1150 if (!device
->ref
&& device
->state
== _CUPS_DNSSD_NEW
)
1152 DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device
->fullName
));
1155 device
->ref
= data
.main_ref
;
1157 if (DNSServiceQueryRecord(&(device
->ref
),
1158 kDNSServiceFlagsShareConnection
,
1159 0, device
->fullName
,
1160 kDNSServiceType_TXT
,
1161 kDNSServiceClass_IN
,
1162 (DNSServiceQueryRecordReply
)cups_dnssd_query_cb
,
1163 &data
) == kDNSServiceErr_NoError
)
1170 device
->state
= _CUPS_DNSSD_ERROR
;
1172 DEBUG_puts("1cupsEnumDests: Query failed.");
1175 # else /* HAVE_AVAHI */
1176 if ((device
->ref
= avahi_record_browser_new(data
.client
,
1183 cups_dnssd_query_cb
,
1190 device
->state
= _CUPS_DNSSD_ERROR
;
1192 DEBUG_printf(("1cupsEnumDests: Query failed: %s",
1193 avahi_strerror(avahi_client_errno(data
.client
))));
1195 # endif /* HAVE_DNSSD */
1197 else if (device
->ref
&& device
->state
== _CUPS_DNSSD_PENDING
)
1199 if ((device
->type
& mask
) == type
)
1201 if (!(*cb
)(user_data
, CUPS_DEST_FLAGS_NONE
, &device
->dest
))
1208 device
->state
= _CUPS_DNSSD_ACTIVE
;
1213 cupsArrayDelete(data
.devices
);
1216 DNSServiceRefDeallocate(ipp_ref
);
1217 DNSServiceRefDeallocate(local_ipp_ref
);
1220 DNSServiceRefDeallocate(ipp_ref
);
1221 DNSServiceRefDeallocate(local_ipp_ref
);
1222 # endif /* HAVE_SSL */
1224 DNSServiceRefDeallocate(data
.main_ref
);
1226 # else /* HAVE_AVAHI */
1227 avahi_service_browser_free(ipp_ref
);
1229 avahi_service_browser_free(ipps_ref
);
1230 # endif /* HAVE_SSL */
1232 avahi_client_free(data
.client
);
1233 avahi_simple_poll_free(data
.simple_poll
);
1234 # endif /* HAVE_DNSSD */
1235 #endif /* HAVE_DNSSD || HAVE_DNSSD */
1243 * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block.
1245 * Destinations are enumerated from one or more sources. The block receives the
1246 * destination name, instance, number of options, and options which can be used
1247 * as input to the @link cupsAddDest@ function. The block must return 1 to
1248 * continue enumeration or 0 to stop.
1250 * Enumeration happens on the current thread and does not return until all
1251 * destinations have been enumerated or the block returns 0.
1253 * @since CUPS 1.6/macOS 10.8@
1256 int /* O - 1 on success, 0 on failure */
1258 unsigned flags
, /* I - Enumeration flags */
1259 int timeout
, /* I - Timeout in milliseconds, 0 for indefinite */
1260 int *cancel
, /* I - Pointer to "cancel" variable */
1261 cups_ptype_t type
, /* I - Printer type bits */
1262 cups_ptype_t mask
, /* I - Mask for printer type bits */
1263 cups_dest_block_t block
) /* I - Block */
1265 return (cupsEnumDests(flags
, timeout
, cancel
, type
, mask
,
1266 (cups_dest_cb_t
)cups_block_cb
, (void *)block
));
1268 # endif /* __BLOCKS__ */
1272 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
1276 cupsFreeDests(int num_dests
, /* I - Number of destinations */
1277 cups_dest_t
*dests
) /* I - Destinations */
1279 int i
; /* Looping var */
1280 cups_dest_t
*dest
; /* Current destination */
1283 if (num_dests
== 0 || dests
== NULL
)
1286 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1288 _cupsStrFree(dest
->name
);
1289 _cupsStrFree(dest
->instance
);
1291 cupsFreeOptions(dest
->num_options
, dest
->options
);
1299 * 'cupsGetDest()' - Get the named destination from the list.
1301 * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
1302 * list of supported destinations for the current user.
1305 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
1306 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
1307 const char *instance
, /* I - Instance name or @code NULL@ */
1308 int num_dests
, /* I - Number of destinations */
1309 cups_dest_t
*dests
) /* I - Destinations */
1311 int diff
, /* Result of comparison */
1312 match
; /* Matching index */
1315 if (num_dests
<= 0 || !dests
)
1321 * NULL name for default printer.
1324 while (num_dests
> 0)
1326 if (dests
->is_default
)
1336 * Lookup name and optionally the instance...
1339 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
1342 return (dests
+ match
);
1350 * '_cupsGetDestResource()' - Get the resource path and URI for a destination.
1353 const char * /* O - Printer URI */
1354 _cupsGetDestResource(
1355 cups_dest_t
*dest
, /* I - Destination */
1356 char *resource
, /* I - Resource buffer */
1357 size_t resourcesize
) /* I - Size of resource buffer */
1359 const char *uri
; /* Printer URI */
1360 char scheme
[32], /* URI scheme */
1361 userpass
[256], /* Username and password (unused) */
1362 hostname
[256]; /* Hostname */
1363 int port
; /* Port number */
1367 * Range check input...
1370 if (!dest
|| !resource
|| resourcesize
< 1)
1375 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1380 * Grab the printer URI...
1383 if ((uri
= cupsGetOption("printer-uri-supported", dest
->num_options
,
1384 dest
->options
)) == NULL
)
1389 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOENT
), 0);
1395 if (strstr(uri
, "._tcp"))
1397 if ((uri
= cups_dnssd_resolve(dest
, uri
, 5000, NULL
, NULL
, NULL
)) == NULL
)
1400 #endif /* HAVE_DNSSD */
1402 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1403 userpass
, sizeof(userpass
), hostname
, sizeof(hostname
),
1404 &port
, resource
, (int)resourcesize
) < HTTP_URI_STATUS_OK
)
1406 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1416 * 'cupsGetDestWithURI()' - Get a destination associated with a URI.
1418 * "name" is the desired name for the printer. If @code NULL@, a name will be
1419 * created using the URI.
1421 * "uri" is the "ipp" or "ipps" URI for the printer.
1423 * @since CUPS 2.0/macOS 10.10@
1426 cups_dest_t
* /* O - Destination or @code NULL@ */
1427 cupsGetDestWithURI(const char *name
, /* I - Desired printer name or @code NULL@ */
1428 const char *uri
) /* I - URI for the printer */
1430 cups_dest_t
*dest
; /* New destination */
1431 char temp
[1024], /* Temporary string */
1432 scheme
[256], /* Scheme from URI */
1433 userpass
[256], /* Username:password from URI */
1434 hostname
[256], /* Hostname from URI */
1435 resource
[1024], /* Resource path from URI */
1436 *ptr
; /* Pointer into string */
1437 int port
; /* Port number from URI */
1441 * Range check input...
1446 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
1450 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
||
1451 (strncmp(uri
, "ipp://", 6) && strncmp(uri
, "ipps://", 7)))
1453 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad printer-uri."), 1);
1461 * Create the name from the URI...
1464 if (strstr(hostname
, "._tcp"))
1467 * Use the service instance name...
1470 if ((ptr
= strchr(hostname
, '.')) != NULL
)
1475 else if (!strncmp(resource
, "/classes/", 9))
1477 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 9, hostname
);
1480 else if (!strncmp(resource
, "/printers/", 10))
1482 snprintf(temp
, sizeof(temp
), "%s @ %s", resource
+ 10, hostname
);
1492 * Create the destination...
1495 if ((dest
= calloc(1, sizeof(cups_dest_t
))) == NULL
)
1497 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1501 dest
->name
= _cupsStrAlloc(name
);
1502 dest
->num_options
= cupsAddOption("device-uri", uri
, dest
->num_options
, &(dest
->options
));
1503 dest
->num_options
= cupsAddOption("printer-info", name
, dest
->num_options
, &(dest
->options
));
1510 * '_cupsGetDests()' - Get destinations from a server.
1512 * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT
1513 * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for
1516 * "name" is the name of an existing printer and is only used when "op" is
1517 * IPP_OP_GET_PRINTER_ATTRIBUTES.
1519 * "dest" is initialized to point to the array of destinations.
1521 * 0 is returned if there are no printers, no default printer, or the named
1522 * printer does not exist, respectively.
1524 * Free the memory used by the destination array using the @link cupsFreeDests@
1527 * Note: On macOS this function also gets the default paper from the system
1528 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
1529 * options array for each destination that supports it.
1532 int /* O - Number of destinations */
1533 _cupsGetDests(http_t
*http
, /* I - Connection to server or
1534 * @code CUPS_HTTP_DEFAULT@ */
1535 ipp_op_t op
, /* I - IPP operation */
1536 const char *name
, /* I - Name of destination */
1537 cups_dest_t
**dests
, /* IO - Destinations */
1538 cups_ptype_t type
, /* I - Printer type bits */
1539 cups_ptype_t mask
) /* I - Printer type mask */
1541 int num_dests
= 0; /* Number of destinations */
1542 cups_dest_t
*dest
; /* Current destination */
1543 ipp_t
*request
, /* IPP Request */
1544 *response
; /* IPP Response */
1545 ipp_attribute_t
*attr
; /* Current attribute */
1546 const char *printer_name
; /* printer-name attribute */
1547 char uri
[1024]; /* printer-uri value */
1548 int num_options
; /* Number of options */
1549 cups_option_t
*options
; /* Options */
1551 char media_default
[41]; /* Default paper size */
1552 #endif /* __APPLE__ */
1553 char optname
[1024], /* Option name */
1554 value
[2048], /* Option value */
1555 *ptr
; /* Pointer into name/value */
1556 static const char * const pattrs
[] = /* Attributes we're interested in */
1558 "auth-info-required",
1560 "job-sheets-default",
1561 "marker-change-time",
1563 "marker-high-levels",
1565 "marker-low-levels",
1571 #endif /* __APPLE__ */
1575 "printer-is-accepting-jobs",
1576 "printer-is-shared",
1578 "printer-make-and-model",
1579 "printer-mandatory-job-attributes",
1582 "printer-state-change-time",
1583 "printer-state-reasons",
1585 "printer-uri-supported"
1591 * Get the default paper size...
1594 appleGetPaperSize(media_default
, sizeof(media_default
));
1595 #endif /* __APPLE__ */
1598 * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which
1599 * require the following attributes:
1601 * attributes-charset
1602 * attributes-natural-language
1603 * requesting-user-name
1604 * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES]
1607 request
= ippNewRequest(op
);
1609 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1610 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1613 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1614 "requesting-user-name", NULL
, cupsUser());
1616 if (name
&& op
!= IPP_OP_CUPS_GET_DEFAULT
)
1618 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1619 "localhost", ippPort(), "/printers/%s", name
);
1620 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1625 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type", (int)type
);
1626 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
, "printer-type-mask", (int)mask
);
1630 * Do the request and get back a response...
1633 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1635 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1638 * Skip leading attributes until we hit a printer...
1641 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1648 * Pull the needed attributes from this printer...
1651 printer_name
= NULL
;
1655 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1657 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1658 attr
->value_tag
!= IPP_TAG_ENUM
&&
1659 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1660 attr
->value_tag
!= IPP_TAG_TEXT
&&
1661 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1662 attr
->value_tag
!= IPP_TAG_NAME
&&
1663 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1664 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1665 attr
->value_tag
!= IPP_TAG_RANGE
&&
1666 attr
->value_tag
!= IPP_TAG_URI
)
1669 if (!strcmp(attr
->name
, "auth-info-required") ||
1670 !strcmp(attr
->name
, "device-uri") ||
1671 !strcmp(attr
->name
, "marker-change-time") ||
1672 !strcmp(attr
->name
, "marker-colors") ||
1673 !strcmp(attr
->name
, "marker-high-levels") ||
1674 !strcmp(attr
->name
, "marker-levels") ||
1675 !strcmp(attr
->name
, "marker-low-levels") ||
1676 !strcmp(attr
->name
, "marker-message") ||
1677 !strcmp(attr
->name
, "marker-names") ||
1678 !strcmp(attr
->name
, "marker-types") ||
1679 !strcmp(attr
->name
, "printer-commands") ||
1680 !strcmp(attr
->name
, "printer-info") ||
1681 !strcmp(attr
->name
, "printer-is-shared") ||
1682 !strcmp(attr
->name
, "printer-make-and-model") ||
1683 !strcmp(attr
->name
, "printer-mandatory-job-attributes") ||
1684 !strcmp(attr
->name
, "printer-state") ||
1685 !strcmp(attr
->name
, "printer-state-change-time") ||
1686 !strcmp(attr
->name
, "printer-type") ||
1687 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1688 !strcmp(attr
->name
, "printer-location") ||
1689 !strcmp(attr
->name
, "printer-state-reasons") ||
1690 !strcmp(attr
->name
, "printer-uri-supported"))
1693 * Add a printer description attribute...
1696 num_options
= cupsAddOption(attr
->name
,
1697 cups_make_string(attr
, value
,
1699 num_options
, &options
);
1702 else if (!strcmp(attr
->name
, "media-supported"))
1705 * See if we can set a default media size...
1708 int i
; /* Looping var */
1710 for (i
= 0; i
< attr
->num_values
; i
++)
1711 if (!_cups_strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1713 num_options
= cupsAddOption("media", media_default
, num_options
,
1718 #endif /* __APPLE__ */
1719 else if (!strcmp(attr
->name
, "printer-name") &&
1720 attr
->value_tag
== IPP_TAG_NAME
)
1721 printer_name
= attr
->values
[0].string
.text
;
1722 else if (strncmp(attr
->name
, "notify-", 7) &&
1723 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1724 attr
->value_tag
== IPP_TAG_ENUM
||
1725 attr
->value_tag
== IPP_TAG_INTEGER
||
1726 attr
->value_tag
== IPP_TAG_KEYWORD
||
1727 attr
->value_tag
== IPP_TAG_NAME
||
1728 attr
->value_tag
== IPP_TAG_RANGE
) &&
1729 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1732 * Add a default option...
1735 strlcpy(optname
, attr
->name
, sizeof(optname
));
1736 optname
[ptr
- attr
->name
] = '\0';
1738 if (_cups_strcasecmp(optname
, "media") ||
1739 !cupsGetOption("media", num_options
, options
))
1740 num_options
= cupsAddOption(optname
,
1741 cups_make_string(attr
, value
,
1743 num_options
, &options
);
1748 * See if we have everything needed...
1753 cupsFreeOptions(num_options
, options
);
1761 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1763 dest
->num_options
= num_options
;
1764 dest
->options
= options
;
1767 cupsFreeOptions(num_options
, options
);
1773 ippDelete(response
);
1777 * Return the count...
1785 * 'cupsGetDests()' - Get the list of destinations from the default server.
1787 * Starting with CUPS 1.2, the returned list of destinations include the
1788 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1789 * printer-make-and-model, printer-state, printer-state-change-time,
1790 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1791 * adds the marker-change-time, marker-colors, marker-high-levels,
1792 * marker-levels, marker-low-levels, marker-message, marker-names,
1793 * marker-types, and printer-commands attributes as well.
1795 * Use the @link cupsFreeDests@ function to free the destination list and
1796 * the @link cupsGetDest@ function to find a particular destination.
1799 int /* O - Number of destinations */
1800 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
1802 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
1807 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
1809 * Starting with CUPS 1.2, the returned list of destinations include the
1810 * printer-info, printer-is-accepting-jobs, printer-is-shared,
1811 * printer-make-and-model, printer-state, printer-state-change-time,
1812 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
1813 * adds the marker-change-time, marker-colors, marker-high-levels,
1814 * marker-levels, marker-low-levels, marker-message, marker-names,
1815 * marker-types, and printer-commands attributes as well.
1817 * Use the @link cupsFreeDests@ function to free the destination list and
1818 * the @link cupsGetDest@ function to find a particular destination.
1820 * @since CUPS 1.1.21/macOS 10.4@
1823 int /* O - Number of destinations */
1824 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1825 cups_dest_t
**dests
) /* O - Destinations */
1827 int num_dests
; /* Number of destinations */
1828 cups_dest_t
*dest
; /* Destination pointer */
1829 const char *home
; /* HOME environment variable */
1830 char filename
[1024]; /* Local ~/.cups/lpoptions file */
1831 const char *defprinter
; /* Default printer */
1832 char name
[1024], /* Copy of printer name */
1833 *instance
, /* Pointer to instance name */
1834 *user_default
; /* User default printer */
1835 int num_reals
; /* Number of real queues */
1836 cups_dest_t
*reals
; /* Real queues */
1837 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1841 * Range check the input...
1846 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad NULL dests pointer"), 1);
1851 * Grab the printers and classes...
1854 *dests
= (cups_dest_t
*)0;
1855 num_dests
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, dests
, 0, CUPS_PRINTER_3D
);
1857 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
1859 cupsFreeDests(num_dests
, *dests
);
1860 *dests
= (cups_dest_t
*)0;
1865 * Make a copy of the "real" queues for a later sanity check...
1870 num_reals
= num_dests
;
1871 reals
= calloc((size_t)num_reals
, sizeof(cups_dest_t
));
1874 memcpy(reals
, *dests
, (size_t)num_reals
* sizeof(cups_dest_t
));
1885 * Grab the default destination...
1888 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
1890 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
1892 strlcpy(name
, defprinter
, sizeof(name
));
1899 * Separate printer and instance name...
1902 if ((instance
= strchr(name
, '/')) != NULL
)
1906 * Lookup the printer and instance and make it the default...
1909 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
1910 dest
->is_default
= 1;
1916 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
1919 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1920 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
1923 if ((home
= getenv("HOME")) != NULL
)
1925 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1927 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
1932 * Validate the current default destination - this prevents old
1933 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
1934 * pointing to a non-existent printer or class...
1940 * See if we have a default printer...
1943 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
1946 * Have a default; see if it is real...
1949 if (!cupsGetDest(dest
->name
, NULL
, num_reals
, reals
))
1952 * Remove the non-real printer from the list, since we don't want jobs
1953 * going to an unexpected printer... (<rdar://problem/14216472>)
1956 num_dests
= cupsRemoveDest(dest
->name
, dest
->instance
, num_dests
,
1969 * Return the number of destinations...
1973 _cupsSetError(IPP_STATUS_OK
, NULL
, 0);
1980 * 'cupsGetNamedDest()' - Get options for the named destination.
1982 * This function is optimized for retrieving a single destination and should
1983 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
1984 * know the name of the destination or want to print to the default destination.
1985 * If @code NULL@ is returned, the destination does not exist or there is no
1986 * default destination.
1988 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
1989 * server will be used.
1991 * If "name" is @code NULL@, the default printer for the current user will be
1994 * The returned destination must be freed using @link cupsFreeDests@ with a
1995 * "num_dests" value of 1.
1997 * @since CUPS 1.4/macOS 10.6@
2000 cups_dest_t
* /* O - Destination or @code NULL@ */
2001 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2002 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
2003 const char *instance
) /* I - Instance name or @code NULL@ */
2005 cups_dest_t
*dest
; /* Destination */
2006 char filename
[1024], /* Path to lpoptions */
2007 defname
[256]; /* Default printer name */
2008 const char *home
= getenv("HOME"); /* Home directory */
2009 int set_as_default
= 0; /* Set returned destination as default */
2010 ipp_op_t op
= IPP_OP_GET_PRINTER_ATTRIBUTES
;
2011 /* IPP operation to get server ops */
2012 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2016 * If "name" is NULL, find the default destination...
2022 name
= _cupsUserDefault(defname
, sizeof(defname
));
2026 char *ptr
; /* Temporary pointer... */
2028 if ((ptr
= strchr(defname
, '/')) != NULL
)
2039 * No default in the environment, try the user's lpoptions files...
2042 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2044 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2050 * Still not there? Try the system lpoptions file...
2053 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
2054 cg
->cups_serverroot
);
2055 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
2061 * No locally-set default destination, ask the server...
2064 op
= IPP_OP_CUPS_GET_DEFAULT
;
2069 * Get the printer's attributes...
2072 if (!_cupsGetDests(http
, op
, name
, &dest
, 0, CUPS_PRINTER_3D
))
2076 dest
->instance
= _cupsStrAlloc(instance
);
2079 dest
->is_default
= 1;
2082 * Then add local options...
2085 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2086 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2090 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2092 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
2096 * Return the result...
2104 * 'cupsRemoveDest()' - Remove a destination from the destination list.
2106 * Removing a destination/instance does not delete the class or printer
2107 * queue, merely the lpoptions for that destination/instance. Use the
2108 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
2109 * options for the user.
2111 * @since CUPS 1.3/macOS 10.5@
2114 int /* O - New number of destinations */
2115 cupsRemoveDest(const char *name
, /* I - Destination name */
2116 const char *instance
, /* I - Instance name or @code NULL@ */
2117 int num_dests
, /* I - Number of destinations */
2118 cups_dest_t
**dests
) /* IO - Destinations */
2120 int i
; /* Index into destinations */
2121 cups_dest_t
*dest
; /* Pointer to destination */
2125 * Find the destination...
2128 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
2135 _cupsStrFree(dest
->name
);
2136 _cupsStrFree(dest
->instance
);
2137 cupsFreeOptions(dest
->num_options
, dest
->options
);
2140 * Remove the destination from the array...
2145 i
= (int)(dest
- *dests
);
2148 memmove(dest
, dest
+ 1, (size_t)(num_dests
- i
) * sizeof(cups_dest_t
));
2155 * 'cupsSetDefaultDest()' - Set the default destination.
2157 * @since CUPS 1.3/macOS 10.5@
2162 const char *name
, /* I - Destination name */
2163 const char *instance
, /* I - Instance name or @code NULL@ */
2164 int num_dests
, /* I - Number of destinations */
2165 cups_dest_t
*dests
) /* I - Destinations */
2167 int i
; /* Looping var */
2168 cups_dest_t
*dest
; /* Current destination */
2172 * Range check input...
2175 if (!name
|| num_dests
<= 0 || !dests
)
2179 * Loop through the array and set the "is_default" flag for the matching
2183 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2184 dest
->is_default
= !_cups_strcasecmp(name
, dest
->name
) &&
2185 ((!instance
&& !dest
->instance
) ||
2186 (instance
&& dest
->instance
&&
2187 !_cups_strcasecmp(instance
, dest
->instance
)));
2192 * 'cupsSetDests()' - Save the list of destinations for the default server.
2194 * This function saves the destinations to /etc/cups/lpoptions when run
2195 * as root and ~/.cups/lpoptions when run as a normal user.
2199 cupsSetDests(int num_dests
, /* I - Number of destinations */
2200 cups_dest_t
*dests
) /* I - Destinations */
2202 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
2207 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
2209 * This function saves the destinations to /etc/cups/lpoptions when run
2210 * as root and ~/.cups/lpoptions when run as a normal user.
2212 * @since CUPS 1.1.21/macOS 10.4@
2215 int /* O - 0 on success, -1 on error */
2216 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
2217 int num_dests
, /* I - Number of destinations */
2218 cups_dest_t
*dests
) /* I - Destinations */
2220 int i
, j
; /* Looping vars */
2221 int wrote
; /* Wrote definition? */
2222 cups_dest_t
*dest
; /* Current destination */
2223 cups_option_t
*option
; /* Current option */
2224 _ipp_option_t
*match
; /* Matching attribute for option */
2225 FILE *fp
; /* File pointer */
2227 const char *home
; /* HOME environment variable */
2229 char filename
[1024]; /* lpoptions file */
2230 int num_temps
; /* Number of temporary destinations */
2231 cups_dest_t
*temps
= NULL
, /* Temporary destinations */
2232 *temp
; /* Current temporary dest */
2233 const char *val
; /* Value of temporary option */
2234 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
2238 * Range check the input...
2241 if (!num_dests
|| !dests
)
2245 * Get the server destinations...
2248 num_temps
= _cupsGetDests(http
, IPP_OP_CUPS_GET_PRINTERS
, NULL
, &temps
, 0, CUPS_PRINTER_3D
);
2250 if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE
)
2252 cupsFreeDests(num_temps
, temps
);
2257 * Figure out which file to write to...
2260 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
2266 * Merge in server defaults...
2269 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
2272 * Point to user defaults...
2275 if ((home
= getenv("HOME")) != NULL
)
2278 * Create ~/.cups subdirectory...
2281 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
2282 if (access(filename
, 0))
2283 mkdir(filename
, 0700);
2285 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
2291 * Try to open the file...
2294 if ((fp
= fopen(filename
, "w")) == NULL
)
2296 cupsFreeDests(num_temps
, temps
);
2302 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
2307 fchmod(fileno(fp
), 0644);
2311 * Write each printer; each line looks like:
2313 * Dest name[/instance] options
2314 * Default name[/instance] options
2317 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
2318 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
2320 if (dest
->is_default
)
2322 fprintf(fp
, "Default %s", dest
->name
);
2324 fprintf(fp
, "/%s", dest
->instance
);
2331 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
2332 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
2334 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
2337 * See if this option is a printer attribute; if so, skip it...
2340 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
2341 match
->group_tag
== IPP_TAG_PRINTER
)
2345 * See if the server/global options match these; if so, don't
2350 (val
= cupsGetOption(option
->name
, temp
->num_options
,
2351 temp
->options
)) != NULL
&&
2352 !_cups_strcasecmp(val
, option
->value
))
2356 * Options don't match, write to the file...
2361 fprintf(fp
, "Dest %s", dest
->name
);
2363 fprintf(fp
, "/%s", dest
->instance
);
2367 if (option
->value
[0])
2369 if (strchr(option
->value
, ' ') ||
2370 strchr(option
->value
, '\\') ||
2371 strchr(option
->value
, '\"') ||
2372 strchr(option
->value
, '\''))
2375 * Quote the value...
2378 fprintf(fp
, " %s=\"", option
->name
);
2380 for (val
= option
->value
; *val
; val
++)
2382 if (strchr("\"\'\\", *val
))
2393 * Store the literal value...
2396 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
2400 fprintf(fp
, " %s", option
->name
);
2408 * Free the temporary destinations and close the file...
2411 cupsFreeDests(num_temps
, temps
);
2417 * Set the default printer for this location - this allows command-line
2418 * and GUI applications to share the same default destination...
2421 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
2423 CFStringRef name
= CFStringCreateWithCString(kCFAllocatorDefault
,
2425 kCFStringEncodingUTF8
);
2426 /* Default printer name */
2430 _cupsAppleSetDefaultPrinter(name
);
2434 #endif /* __APPLE__ */
2436 #ifdef HAVE_NOTIFY_POST
2438 * Send a notification so that MacmacOS applications can know about the
2442 notify_post("com.apple.printerListChange");
2443 #endif /* HAVE_NOTIFY_POST */
2450 * '_cupsUserDefault()' - Get the user default printer from environment
2451 * variables and location information.
2454 char * /* O - Default printer or NULL */
2455 _cupsUserDefault(char *name
, /* I - Name buffer */
2456 size_t namesize
) /* I - Size of name buffer */
2458 const char *env
; /* LPDEST or PRINTER env variable */
2460 CFStringRef locprinter
; /* Last printer as this location */
2461 #endif /* __APPLE__ */
2464 if ((env
= getenv("LPDEST")) == NULL
)
2465 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
2470 strlcpy(name
, env
, namesize
);
2476 * Use location-based defaults if "use last printer" is selected in the
2477 * system preferences...
2480 if ((locprinter
= _cupsAppleCopyDefaultPrinter()) != NULL
)
2482 CFStringGetCString(locprinter
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
);
2483 CFRelease(locprinter
);
2488 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name
));
2490 return (*name
? name
: NULL
);
2494 * No location-based defaults on this platform...
2499 #endif /* __APPLE__ */
2503 #if _CUPS_LOCATION_DEFAULTS
2505 * 'appleCopyLocations()' - Copy the location history array.
2508 static CFArrayRef
/* O - Location array or NULL */
2509 appleCopyLocations(void)
2511 CFArrayRef locations
; /* Location array */
2515 * Look up the location array in the preferences...
2518 if ((locations
= CFPreferencesCopyAppValue(kLastUsedPrintersKey
,
2519 kCUPSPrintingPrefs
)) == NULL
)
2522 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
2524 CFRelease(locations
);
2533 * 'appleCopyNetwork()' - Get the network ID for the current location.
2536 static CFStringRef
/* O - Network ID */
2537 appleCopyNetwork(void)
2539 SCDynamicStoreRef dynamicStore
; /* System configuration data */
2540 CFStringRef key
; /* Current network configuration key */
2541 CFDictionaryRef ip_dict
; /* Network configuration data */
2542 CFStringRef network
= NULL
; /* Current network ID */
2545 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("libcups"), NULL
,
2549 * First use the IPv6 router address, if available, since that will generally
2550 * be a globally-unique link-local address.
2553 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2554 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv6
)) != NULL
)
2556 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2558 if ((network
= CFDictionaryGetValue(ip_dict
,
2559 kSCPropNetIPv6Router
)) != NULL
)
2569 * If that doesn't work, try the IPv4 router address. This isn't as unique
2570 * and will likely be a 10.x.y.z or 192.168.y.z address...
2575 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
2576 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
2578 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
2580 if ((network
= CFDictionaryGetValue(ip_dict
,
2581 kSCPropNetIPv4Router
)) != NULL
)
2591 CFRelease(dynamicStore
);
2596 #endif /* _CUPS_LOCATION_DEFAULTS */
2601 * 'appleGetPaperSize()' - Get the default paper size.
2604 static char * /* O - Default paper size */
2605 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
2606 size_t namesize
) /* I - Size of buffer */
2608 CFStringRef defaultPaperID
; /* Default paper ID */
2609 pwg_media_t
*pwgmedia
; /* PWG media size */
2612 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
2613 if (!defaultPaperID
||
2614 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
2615 !CFStringGetCString(defaultPaperID
, name
, (CFIndex
)namesize
, kCFStringEncodingUTF8
))
2617 else if ((pwgmedia
= pwgMediaForLegacy(name
)) != NULL
)
2618 strlcpy(name
, pwgmedia
->pwg
, namesize
);
2621 CFRelease(defaultPaperID
);
2625 #endif /* __APPLE__ */
2628 #if _CUPS_LOCATION_DEFAULTS
2630 * 'appleGetPrinter()' - Get a printer from the history array.
2633 static CFStringRef
/* O - Printer name or NULL */
2634 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
2635 CFStringRef network
, /* I - Network name */
2636 CFIndex
*locindex
) /* O - Index in array */
2638 CFIndex i
, /* Looping var */
2639 count
; /* Number of locations */
2640 CFDictionaryRef location
; /* Current location */
2641 CFStringRef locnetwork
, /* Current network */
2642 locprinter
; /* Current printer */
2645 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
2646 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
2647 CFGetTypeID(location
) == CFDictionaryGetTypeID())
2649 if ((locnetwork
= CFDictionaryGetValue(location
,
2650 kLocationNetworkKey
)) != NULL
&&
2651 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
2652 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
2653 (locprinter
= CFDictionaryGetValue(location
,
2654 kLocationPrinterIDKey
)) != NULL
&&
2655 CFGetTypeID(locprinter
) == CFStringGetTypeID())
2660 return (locprinter
);
2666 #endif /* _CUPS_LOCATION_DEFAULTS */
2670 * 'cups_add_dest()' - Add a destination to the array.
2672 * Unlike cupsAddDest(), this function does not check for duplicates.
2675 static cups_dest_t
* /* O - New destination */
2676 cups_add_dest(const char *name
, /* I - Name of destination */
2677 const char *instance
, /* I - Instance or NULL */
2678 int *num_dests
, /* IO - Number of destinations */
2679 cups_dest_t
**dests
) /* IO - Destinations */
2681 int insert
, /* Insertion point */
2682 diff
; /* Result of comparison */
2683 cups_dest_t
*dest
; /* Destination pointer */
2687 * Add new destination...
2690 if (*num_dests
== 0)
2691 dest
= malloc(sizeof(cups_dest_t
));
2693 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (size_t)(*num_dests
+ 1));
2701 * Find where to insert the destination...
2704 if (*num_dests
== 0)
2708 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
2716 * Move the array elements as needed...
2719 if (insert
< *num_dests
)
2720 memmove(*dests
+ insert
+ 1, *dests
+ insert
, (size_t)(*num_dests
- insert
) * sizeof(cups_dest_t
));
2725 * Initialize the destination...
2728 dest
= *dests
+ insert
;
2729 dest
->name
= _cupsStrAlloc(name
);
2730 dest
->instance
= _cupsStrAlloc(instance
);
2731 dest
->is_default
= 0;
2732 dest
->num_options
= 0;
2733 dest
->options
= (cups_option_t
*)0;
2741 * 'cups_block_cb()' - Enumeration callback for block API.
2744 static int /* O - 1 to continue, 0 to stop */
2746 cups_dest_block_t block
, /* I - Block */
2747 unsigned flags
, /* I - Destination flags */
2748 cups_dest_t
*dest
) /* I - Destination */
2750 return ((block
)(flags
, dest
));
2752 # endif /* __BLOCKS__ */
2756 * 'cups_compare_dests()' - Compare two destinations.
2759 static int /* O - Result of comparison */
2760 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
2761 cups_dest_t
*b
) /* I - Second destination */
2763 int diff
; /* Difference */
2766 if ((diff
= _cups_strcasecmp(a
->name
, b
->name
)) != 0)
2768 else if (a
->instance
&& b
->instance
)
2769 return (_cups_strcasecmp(a
->instance
, b
->instance
));
2771 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
2775 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
2778 * 'cups_dnssd_browse_cb()' - Browse for printers.
2782 cups_dnssd_browse_cb(
2783 DNSServiceRef sdRef
, /* I - Service reference */
2784 DNSServiceFlags flags
, /* I - Option flags */
2785 uint32_t interfaceIndex
, /* I - Interface number */
2786 DNSServiceErrorType errorCode
, /* I - Error, if any */
2787 const char *serviceName
, /* I - Name of service/device */
2788 const char *regtype
, /* I - Type of service */
2789 const char *replyDomain
, /* I - Service domain */
2790 void *context
) /* I - Enumeration data */
2792 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2793 /* Enumeration data */
2796 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
));
2799 * Don't do anything on error...
2802 if (errorCode
!= kDNSServiceErr_NoError
)
2809 cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
2813 # else /* HAVE_AVAHI */
2815 * 'cups_dnssd_browse_cb()' - Browse for printers.
2819 cups_dnssd_browse_cb(
2820 AvahiServiceBrowser
*browser
, /* I - Browser */
2821 AvahiIfIndex interface
, /* I - Interface index (unused) */
2822 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2823 AvahiBrowserEvent event
, /* I - What happened */
2824 const char *name
, /* I - Service name */
2825 const char *type
, /* I - Registration type */
2826 const char *domain
, /* I - Domain */
2827 AvahiLookupResultFlags flags
, /* I - Flags */
2828 void *context
) /* I - Devices array */
2831 AvahiClient
*client
= avahi_service_browser_get_client(browser
);
2832 /* Client information */
2834 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2835 /* Enumeration data */
2844 case AVAHI_BROWSER_FAILURE
:
2845 DEBUG_printf(("cups_dnssd_browse_cb: %s",
2846 avahi_strerror(avahi_client_errno(client
))));
2847 avahi_simple_poll_quit(data
->simple_poll
);
2850 case AVAHI_BROWSER_NEW
:
2852 * This object is new on the network.
2855 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
2858 * This comes from the local machine so ignore it.
2861 DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".",
2867 * Create a device entry for it if it doesn't yet exist.
2870 cups_dnssd_get_device(data
, name
, type
, domain
);
2874 case AVAHI_BROWSER_REMOVE
:
2875 case AVAHI_BROWSER_ALL_FOR_NOW
:
2876 case AVAHI_BROWSER_CACHE_EXHAUSTED
:
2883 * 'cups_dnssd_client_cb()' - Avahi client callback function.
2887 cups_dnssd_client_cb(
2888 AvahiClient
*client
, /* I - Client information (unused) */
2889 AvahiClientState state
, /* I - Current state */
2890 void *context
) /* I - User data (unused) */
2892 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
2893 /* Enumeration data */
2899 * If the connection drops, quit.
2902 if (state
== AVAHI_CLIENT_FAILURE
)
2904 DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed.");
2905 avahi_simple_poll_quit(data
->simple_poll
);
2908 # endif /* HAVE_DNSSD */
2912 * 'cups_dnssd_compare_device()' - Compare two devices.
2915 static int /* O - Result of comparison */
2916 cups_dnssd_compare_devices(
2917 _cups_dnssd_device_t
*a
, /* I - First device */
2918 _cups_dnssd_device_t
*b
) /* I - Second device */
2920 return (strcmp(a
->dest
.name
, b
->dest
.name
));
2925 * 'cups_dnssd_free_device()' - Free the memory used by a device.
2929 cups_dnssd_free_device(
2930 _cups_dnssd_device_t
*device
, /* I - Device */
2931 _cups_dnssd_data_t
*data
) /* I - Enumeration data */
2933 DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device
, device
->dest
.name
, (void *)data
));
2937 DNSServiceRefDeallocate(device
->ref
);
2938 # else /* HAVE_AVAHI */
2940 avahi_record_browser_free(device
->ref
);
2941 # endif /* HAVE_DNSSD */
2943 _cupsStrFree(device
->domain
);
2944 _cupsStrFree(device
->fullName
);
2945 _cupsStrFree(device
->regtype
);
2946 _cupsStrFree(device
->dest
.name
);
2948 cupsFreeOptions(device
->dest
.num_options
, device
->dest
.options
);
2955 * 'cups_dnssd_get_device()' - Lookup a device and create it as needed.
2958 static _cups_dnssd_device_t
* /* O - Device */
2959 cups_dnssd_get_device(
2960 _cups_dnssd_data_t
*data
, /* I - Enumeration data */
2961 const char *serviceName
, /* I - Service name */
2962 const char *regtype
, /* I - Registration type */
2963 const char *replyDomain
) /* I - Domain name */
2965 _cups_dnssd_device_t key
, /* Search key */
2966 *device
; /* Device */
2967 char fullName
[kDNSServiceMaxDomainName
];
2968 /* Full name for query */
2971 DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data
, serviceName
, regtype
, replyDomain
));
2974 * See if this is an existing device...
2977 key
.dest
.name
= (char *)serviceName
;
2979 if ((device
= cupsArrayFind(data
->devices
, &key
)) != NULL
)
2982 * Yes, see if we need to do anything with this...
2985 int update
= 0; /* Non-zero if we need to update */
2987 if (!_cups_strcasecmp(replyDomain
, "local.") &&
2988 _cups_strcasecmp(device
->domain
, replyDomain
))
2991 * Update the "global" listing to use the .local domain name instead.
2994 _cupsStrFree(device
->domain
);
2995 device
->domain
= _cupsStrAlloc(replyDomain
);
2997 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local "
2998 "domain.", device
->dest
.name
));
3003 if (!_cups_strcasecmp(regtype
, "_ipps._tcp") &&
3004 _cups_strcasecmp(device
->regtype
, regtype
))
3007 * Prefer IPPS over IPP.
3010 _cupsStrFree(device
->regtype
);
3011 device
->regtype
= _cupsStrAlloc(regtype
);
3013 DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.",
3014 device
->dest
.name
));
3021 DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.",
3022 device
->dest
.name
));
3029 * No, add the device...
3032 DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain "
3033 "'%s'.", serviceName
,
3034 !strcmp(regtype
, "_ipps._tcp") ? "IPPS" : "IPP",
3037 device
= calloc(sizeof(_cups_dnssd_device_t
), 1);
3038 device
->dest
.name
= _cupsStrAlloc(serviceName
);
3039 device
->domain
= _cupsStrAlloc(replyDomain
);
3040 device
->regtype
= _cupsStrAlloc(regtype
);
3042 cupsArrayAdd(data
->devices
, device
);
3046 * Set the "full name" of this service, which is used for queries...
3050 DNSServiceConstructFullName(fullName
, device
->dest
.name
, device
->regtype
,
3052 # else /* HAVE_AVAHI */
3053 avahi_service_name_join(fullName
, kDNSServiceMaxDomainName
, serviceName
,
3054 regtype
, replyDomain
);
3055 # endif /* HAVE_DNSSD */
3057 _cupsStrFree(device
->fullName
);
3058 device
->fullName
= _cupsStrAlloc(fullName
);
3063 DNSServiceRefDeallocate(device
->ref
);
3064 # else /* HAVE_AVAHI */
3065 avahi_record_browser_free(device
->ref
);
3066 # endif /* HAVE_DNSSD */
3071 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3073 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3074 device
->state
= _CUPS_DNSSD_NEW
;
3083 * 'cups_dnssd_local_cb()' - Browse for local printers.
3087 cups_dnssd_local_cb(
3088 DNSServiceRef sdRef
, /* I - Service reference */
3089 DNSServiceFlags flags
, /* I - Option flags */
3090 uint32_t interfaceIndex
, /* I - Interface number */
3091 DNSServiceErrorType errorCode
, /* I - Error, if any */
3092 const char *serviceName
, /* I - Name of service/device */
3093 const char *regtype
, /* I - Type of service */
3094 const char *replyDomain
, /* I - Service domain */
3095 void *context
) /* I - Devices array */
3097 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3098 /* Enumeration data */
3099 _cups_dnssd_device_t
*device
; /* Device */
3102 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
));
3105 * Only process "add" data...
3108 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3115 device
= cups_dnssd_get_device(data
, serviceName
, regtype
, replyDomain
);
3118 * Hide locally-registered devices...
3121 DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.",
3126 DNSServiceRefDeallocate(device
->ref
);
3130 if (device
->state
== _CUPS_DNSSD_ACTIVE
)
3131 (*data
->cb
)(data
->user_data
, CUPS_DEST_FLAGS_REMOVED
, &device
->dest
);
3133 device
->state
= _CUPS_DNSSD_LOCAL
;
3135 # endif /* HAVE_DNSSD */
3140 * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors.
3142 * Note: This function is needed because avahi_simple_poll_iterate is broken
3143 * and always uses a timeout of 0 (!) milliseconds.
3144 * (Avahi Ticket #364)
3147 static int /* O - Number of file descriptors matching */
3149 struct pollfd
*pollfds
, /* I - File descriptors */
3150 unsigned int num_pollfds
, /* I - Number of file descriptors */
3151 int timeout
, /* I - Timeout in milliseconds (unused) */
3152 void *context
) /* I - User data (unused) */
3154 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3155 /* Enumeration data */
3156 int val
; /* Return value */
3161 val
= poll(pollfds
, num_pollfds
, 250);
3165 DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno
)));
3172 # endif /* HAVE_AVAHI */
3176 * 'cups_dnssd_query_cb()' - Process query data.
3181 cups_dnssd_query_cb(
3182 DNSServiceRef sdRef
, /* I - Service reference */
3183 DNSServiceFlags flags
, /* I - Data flags */
3184 uint32_t interfaceIndex
, /* I - Interface */
3185 DNSServiceErrorType errorCode
, /* I - Error, if any */
3186 const char *fullName
, /* I - Full service name */
3187 uint16_t rrtype
, /* I - Record type */
3188 uint16_t rrclass
, /* I - Record class */
3189 uint16_t rdlen
, /* I - Length of record data */
3190 const void *rdata
, /* I - Record data */
3191 uint32_t ttl
, /* I - Time-to-live */
3192 void *context
) /* I - Enumeration data */
3194 # else /* HAVE_AVAHI */
3196 cups_dnssd_query_cb(
3197 AvahiRecordBrowser
*browser
, /* I - Record browser */
3198 AvahiIfIndex interfaceIndex
,
3199 /* I - Interface index (unused) */
3200 AvahiProtocol protocol
, /* I - Network protocol (unused) */
3201 AvahiBrowserEvent event
, /* I - What happened? */
3202 const char *fullName
, /* I - Service name */
3203 uint16_t rrclass
, /* I - Record class */
3204 uint16_t rrtype
, /* I - Record type */
3205 const void *rdata
, /* I - TXT record */
3206 size_t rdlen
, /* I - Length of TXT record */
3207 AvahiLookupResultFlags flags
, /* I - Flags */
3208 void *context
) /* I - Enumeration data */
3211 AvahiClient
*client
= avahi_record_browser_get_client(browser
);
3212 /* Client information */
3214 # endif /* HAVE_DNSSD */
3215 _cups_dnssd_data_t
*data
= (_cups_dnssd_data_t
*)context
;
3216 /* Enumeration data */
3217 char name
[1024], /* Service name */
3218 *ptr
; /* Pointer into string */
3219 _cups_dnssd_device_t dkey
, /* Search key */
3220 *device
; /* Device */
3224 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
));
3227 * Only process "add" data...
3230 if (errorCode
!= kDNSServiceErr_NoError
|| !(flags
& kDNSServiceFlagsAdd
))
3233 # else /* HAVE_AVAHI */
3234 DEBUG_printf(("5cups_dnssd_query_cb(browser=%p, interfaceIndex=%d, "
3235 "protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, "
3236 "rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)",
3237 browser
, interfaceIndex
, protocol
, event
, fullName
, rrclass
,
3238 rrtype
, rdata
, (unsigned)rdlen
, flags
, context
));
3241 * Only process "add" data...
3244 if (event
!= AVAHI_BROWSER_NEW
)
3246 if (event
== AVAHI_BROWSER_FAILURE
)
3247 DEBUG_printf(("cups_dnssd_query_cb: %s",
3248 avahi_strerror(avahi_client_errno(client
))));
3252 # endif /* HAVE_DNSSD */
3255 * Lookup the service in the devices array.
3258 dkey
.dest
.name
= name
;
3260 cups_dnssd_unquote(name
, fullName
, sizeof(name
));
3262 if ((ptr
= strstr(name
, "._")) != NULL
)
3265 if ((device
= cupsArrayFind(data
->devices
, &dkey
)) != NULL
)
3268 * Found it, pull out the make and model from the TXT record and save it...
3271 const uint8_t *txt
, /* Pointer into data */
3272 *txtnext
, /* Next key/value pair */
3273 *txtend
; /* End of entire TXT record */
3274 uint8_t txtlen
; /* Length of current key/value pair */
3275 char key
[256], /* Key string */
3276 value
[256], /* Value string */
3277 make_and_model
[512],
3278 /* Manufacturer and model */
3279 model
[256], /* Model */
3280 uriname
[1024], /* Name for URI */
3281 uri
[1024]; /* Printer URI */
3282 cups_ptype_t type
= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_BW
;
3284 int saw_printer_type
= 0;
3285 /* Did we see a printer-type key? */
3287 device
->state
= _CUPS_DNSSD_PENDING
;
3288 make_and_model
[0] = '\0';
3290 strlcpy(model
, "Unknown", sizeof(model
));
3292 for (txt
= rdata
, txtend
= txt
+ rdlen
;
3297 * Read a key/value pair starting with an 8-bit length. Since the
3298 * length is 8 bits and the size of the key/value buffers is 256, we
3299 * don't need to check for overflow...
3304 if (!txtlen
|| (txt
+ txtlen
) > txtend
)
3307 txtnext
= txt
+ txtlen
;
3309 for (ptr
= key
; txt
< txtnext
&& *txt
!= '='; txt
++)
3310 *ptr
++ = (char)*txt
;
3313 if (txt
< txtnext
&& *txt
== '=')
3318 memcpy(value
, txt
, (size_t)(txtnext
- txt
));
3319 value
[txtnext
- txt
] = '\0';
3321 DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key
, value
));
3325 DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key
));
3329 if (!_cups_strcasecmp(key
, "usb_MFG") ||
3330 !_cups_strcasecmp(key
, "usb_MANU") ||
3331 !_cups_strcasecmp(key
, "usb_MANUFACTURER"))
3332 strlcpy(make_and_model
, value
, sizeof(make_and_model
));
3333 else if (!_cups_strcasecmp(key
, "usb_MDL") ||
3334 !_cups_strcasecmp(key
, "usb_MODEL"))
3335 strlcpy(model
, value
, sizeof(model
));
3336 else if (!_cups_strcasecmp(key
, "product") && !strstr(value
, "Ghostscript"))
3338 if (value
[0] == '(')
3341 * Strip parenthesis...
3344 if ((ptr
= value
+ strlen(value
) - 1) > value
&& *ptr
== ')')
3347 strlcpy(model
, value
+ 1, sizeof(model
));
3350 strlcpy(model
, value
, sizeof(model
));
3352 else if (!_cups_strcasecmp(key
, "ty"))
3354 strlcpy(model
, value
, sizeof(model
));
3356 if ((ptr
= strchr(model
, ',')) != NULL
)
3359 else if (!_cups_strcasecmp(key
, "note"))
3360 device
->dest
.num_options
= cupsAddOption("printer-location", value
,
3361 device
->dest
.num_options
,
3362 &device
->dest
.options
);
3363 else if (!_cups_strcasecmp(key
, "pdl"))
3366 * Look for PDF-capable printers; only PDF-capable printers are shown.
3369 const char *start
, *next
; /* Pointer into value */
3370 int have_pdf
= 0, /* Have PDF? */
3371 have_raster
= 0;/* Have raster format support? */
3373 for (start
= value
; start
&& *start
; start
= next
)
3375 if (!_cups_strncasecmp(start
, "application/pdf", 15) && (!start
[15] || start
[15] == ','))
3380 else if ((!_cups_strncasecmp(start
, "image/pwg-raster", 16) && (!start
[16] || start
[16] == ',')) ||
3381 (!_cups_strncasecmp(start
, "image/urf", 9) && (!start
[9] || start
[9] == ',')))
3387 if ((next
= strchr(start
, ',')) != NULL
)
3391 if (!have_pdf
&& !have_raster
)
3392 device
->state
= _CUPS_DNSSD_INCOMPATIBLE
;
3394 else if (!_cups_strcasecmp(key
, "printer-type"))
3397 * Value is either NNNN or 0xXXXX
3400 saw_printer_type
= 1;
3401 type
= (cups_ptype_t
)strtol(value
, NULL
, 0);
3403 else if (!saw_printer_type
)
3405 if (!_cups_strcasecmp(key
, "air") &&
3406 !_cups_strcasecmp(value
, "t"))
3407 type
|= CUPS_PRINTER_AUTHENTICATED
;
3408 else if (!_cups_strcasecmp(key
, "bind") &&
3409 !_cups_strcasecmp(value
, "t"))
3410 type
|= CUPS_PRINTER_BIND
;
3411 else if (!_cups_strcasecmp(key
, "collate") &&
3412 !_cups_strcasecmp(value
, "t"))
3413 type
|= CUPS_PRINTER_COLLATE
;
3414 else if (!_cups_strcasecmp(key
, "color") &&
3415 !_cups_strcasecmp(value
, "t"))
3416 type
|= CUPS_PRINTER_COLOR
;
3417 else if (!_cups_strcasecmp(key
, "copies") &&
3418 !_cups_strcasecmp(value
, "t"))
3419 type
|= CUPS_PRINTER_COPIES
;
3420 else if (!_cups_strcasecmp(key
, "duplex") &&
3421 !_cups_strcasecmp(value
, "t"))
3422 type
|= CUPS_PRINTER_DUPLEX
;
3423 else if (!_cups_strcasecmp(key
, "fax") &&
3424 !_cups_strcasecmp(value
, "t"))
3425 type
|= CUPS_PRINTER_MFP
;
3426 else if (!_cups_strcasecmp(key
, "papercustom") &&
3427 !_cups_strcasecmp(value
, "t"))
3428 type
|= CUPS_PRINTER_VARIABLE
;
3429 else if (!_cups_strcasecmp(key
, "papermax"))
3431 if (!_cups_strcasecmp(value
, "legal-a4"))
3432 type
|= CUPS_PRINTER_SMALL
;
3433 else if (!_cups_strcasecmp(value
, "isoc-a2"))
3434 type
|= CUPS_PRINTER_MEDIUM
;
3435 else if (!_cups_strcasecmp(value
, ">isoc-a2"))
3436 type
|= CUPS_PRINTER_LARGE
;
3438 else if (!_cups_strcasecmp(key
, "punch") &&
3439 !_cups_strcasecmp(value
, "t"))
3440 type
|= CUPS_PRINTER_PUNCH
;
3441 else if (!_cups_strcasecmp(key
, "scan") &&
3442 !_cups_strcasecmp(value
, "t"))
3443 type
|= CUPS_PRINTER_MFP
;
3444 else if (!_cups_strcasecmp(key
, "sort") &&
3445 !_cups_strcasecmp(value
, "t"))
3446 type
|= CUPS_PRINTER_SORT
;
3447 else if (!_cups_strcasecmp(key
, "staple") &&
3448 !_cups_strcasecmp(value
, "t"))
3449 type
|= CUPS_PRINTER_STAPLE
;
3454 * Save the printer-xxx values...
3457 device
->dest
.num_options
= cupsAddOption("printer-info", name
, device
->dest
.num_options
, &device
->dest
.options
);
3459 if (make_and_model
[0])
3461 strlcat(make_and_model
, " ", sizeof(make_and_model
));
3462 strlcat(make_and_model
, model
, sizeof(make_and_model
));
3464 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", make_and_model
, device
->dest
.num_options
, &device
->dest
.options
);
3467 device
->dest
.num_options
= cupsAddOption("printer-make-and-model", model
, device
->dest
.num_options
, &device
->dest
.options
);
3469 device
->type
= type
;
3470 snprintf(value
, sizeof(value
), "%u", type
);
3471 device
->dest
.num_options
= cupsAddOption("printer-type", value
, device
->dest
.num_options
, &device
->dest
.options
);
3477 cups_dnssd_unquote(uriname
, device
->fullName
, sizeof(uriname
));
3478 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
),
3479 !strcmp(device
->regtype
, "_ipps._tcp") ? "ipps" : "ipp",
3480 NULL
, uriname
, 0, saw_printer_type
? "/cups" : "/");
3482 DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri
));
3484 device
->dest
.num_options
= cupsAddOption("device-uri", uri
, device
->dest
.num_options
, &device
->dest
.options
);
3487 DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.",
3493 * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI.
3496 static const char * /* O - Resolved URI or NULL */
3498 cups_dest_t
*dest
, /* I - Destination */
3499 const char *uri
, /* I - Current printer URI */
3500 int msec
, /* I - Time in milliseconds */
3501 int *cancel
, /* I - Pointer to "cancel" variable */
3502 cups_dest_cb_t cb
, /* I - Callback */
3503 void *user_data
) /* I - User data for callback */
3505 char tempuri
[1024]; /* Temporary URI buffer */
3506 _cups_dnssd_resolve_t resolve
; /* Resolve data */
3510 * Resolve the URI...
3513 resolve
.cancel
= cancel
;
3514 gettimeofday(&resolve
.end_time
, NULL
);
3517 resolve
.end_time
.tv_sec
+= msec
/ 1000;
3518 resolve
.end_time
.tv_usec
+= (msec
% 1000) * 1000;
3520 while (resolve
.end_time
.tv_usec
>= 1000000)
3522 resolve
.end_time
.tv_sec
++;
3523 resolve
.end_time
.tv_usec
-= 1000000;
3527 resolve
.end_time
.tv_sec
+= 75;
3530 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_RESOLVING
, dest
);
3532 if ((uri
= _httpResolveURI(uri
, tempuri
, sizeof(tempuri
), _HTTP_RESOLVE_DEFAULT
, cups_dnssd_resolve_cb
, &resolve
)) == NULL
)
3534 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to resolve printer-uri."), 1);
3537 (*cb
)(user_data
, CUPS_DEST_FLAGS_UNCONNECTED
| CUPS_DEST_FLAGS_ERROR
, dest
);
3543 * Save the resolved URI...
3546 dest
->num_options
= cupsAddOption("resolved-device-uri", uri
, dest
->num_options
, &dest
->options
);
3548 return (cupsGetOption("resolved-device-uri", dest
->num_options
, dest
->options
));
3553 * 'cups_dnssd_resolve_cb()' - See if we should continue resolving.
3556 static int /* O - 1 to continue, 0 to stop */
3557 cups_dnssd_resolve_cb(void *context
) /* I - Resolve data */
3559 _cups_dnssd_resolve_t
*resolve
= (_cups_dnssd_resolve_t
*)context
;
3561 struct timeval curtime
; /* Current time */
3565 * If the cancel variable is set, return immediately.
3568 if (resolve
->cancel
&& *(resolve
->cancel
))
3570 DEBUG_puts("4cups_dnssd_resolve_cb: Canceled.");
3575 * Otherwise check the end time...
3578 gettimeofday(&curtime
, NULL
);
3580 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
));
3582 return (curtime
.tv_sec
< resolve
->end_time
.tv_sec
||
3583 (curtime
.tv_sec
== resolve
->end_time
.tv_sec
&&
3584 curtime
.tv_usec
< resolve
->end_time
.tv_usec
));
3589 * 'cups_dnssd_unquote()' - Unquote a name string.
3593 cups_dnssd_unquote(char *dst
, /* I - Destination buffer */
3594 const char *src
, /* I - Source string */
3595 size_t dstsize
) /* I - Size of destination buffer */
3597 char *dstend
= dst
+ dstsize
- 1; /* End of destination buffer */
3600 while (*src
&& dst
< dstend
)
3605 if (isdigit(src
[0] & 255) && isdigit(src
[1] & 255) &&
3606 isdigit(src
[2] & 255))
3608 *dst
++ = ((((src
[0] - '0') * 10) + src
[1] - '0') * 10) + src
[2] - '0';
3620 #endif /* HAVE_DNSSD */
3624 * 'cups_find_dest()' - Find a destination using a binary search.
3627 static int /* O - Index of match */
3628 cups_find_dest(const char *name
, /* I - Destination name */
3629 const char *instance
, /* I - Instance or NULL */
3630 int num_dests
, /* I - Number of destinations */
3631 cups_dest_t
*dests
, /* I - Destinations */
3632 int prev
, /* I - Previous index */
3633 int *rdiff
) /* O - Difference of match */
3635 int left
, /* Low mark for binary search */
3636 right
, /* High mark for binary search */
3637 current
, /* Current index */
3638 diff
; /* Result of comparison */
3639 cups_dest_t key
; /* Search key */
3642 key
.name
= (char *)name
;
3643 key
.instance
= (char *)instance
;
3648 * Start search on either side of previous...
3651 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
3652 (diff
< 0 && prev
== 0) ||
3653 (diff
> 0 && prev
== (num_dests
- 1)))
3661 * Start with previous on right side...
3670 * Start wih previous on left side...
3674 right
= num_dests
- 1;
3680 * Start search in the middle...
3684 right
= num_dests
- 1;
3689 current
= (left
+ right
) / 2;
3690 diff
= cups_compare_dests(&key
, dests
+ current
);
3699 while ((right
- left
) > 1);
3704 * Check the last 1 or 2 elements...
3707 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
3711 diff
= cups_compare_dests(&key
, dests
+ right
);
3717 * Return the closest destination and the difference...
3727 * 'cups_get_default()' - Get the default destination from an lpoptions file.
3730 static char * /* O - Default destination or NULL */
3731 cups_get_default(const char *filename
, /* I - File to read */
3732 char *namebuf
, /* I - Name buffer */
3733 size_t namesize
, /* I - Size of name buffer */
3734 const char **instance
) /* I - Instance */
3736 cups_file_t
*fp
; /* lpoptions file */
3737 char line
[8192], /* Line from file */
3738 *value
, /* Value for line */
3739 *nameptr
; /* Pointer into name */
3740 int linenum
; /* Current line */
3745 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
3749 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
3751 if (!_cups_strcasecmp(line
, "default") && value
)
3753 strlcpy(namebuf
, value
, namesize
);
3755 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
3757 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
3760 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
3763 *instance
= nameptr
;
3771 return (*namebuf
? namebuf
: NULL
);
3776 * 'cups_get_dests()' - Get destinations from a file.
3779 static int /* O - Number of destinations */
3781 const char *filename
, /* I - File to read from */
3782 const char *match_name
, /* I - Destination name we want */
3783 const char *match_inst
, /* I - Instance name we want */
3784 int user_default_set
, /* I - User default printer set? */
3785 int num_dests
, /* I - Number of destinations */
3786 cups_dest_t
**dests
) /* IO - Destinations */
3788 int i
; /* Looping var */
3789 cups_dest_t
*dest
; /* Current destination */
3790 cups_file_t
*fp
; /* File pointer */
3791 char line
[8192], /* Line from file */
3792 *lineptr
, /* Pointer into line */
3793 *name
, /* Name of destination/option */
3794 *instance
; /* Instance of destination */
3795 int linenum
; /* Current line number */
3798 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
));
3801 * Try to open the file...
3804 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
3808 * Read each printer; each line looks like:
3810 * Dest name[/instance] options
3811 * Default name[/instance] options
3816 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
3819 * See what type of line it is...
3822 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
3823 linenum
, line
, lineptr
));
3825 if ((_cups_strcasecmp(line
, "dest") && _cups_strcasecmp(line
, "default")) || !lineptr
)
3827 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
3834 * Search for an instance...
3837 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
3840 if (*lineptr
== '/')
3843 * Found an instance...
3850 * Search for an instance...
3853 while (!isspace(*lineptr
& 255) && *lineptr
)
3862 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
3866 * See if the primary instance of the destination exists; if not,
3867 * ignore this entry and move on...
3872 if (_cups_strcasecmp(name
, match_name
) ||
3873 (!instance
&& match_inst
) ||
3874 (instance
&& !match_inst
) ||
3875 (instance
&& _cups_strcasecmp(instance
, match_inst
)))
3880 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
3882 DEBUG_puts("9cups_get_dests: Not found!");
3888 * Add the destination...
3891 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
3893 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
3899 DEBUG_puts("9cups_get_dests: Out of memory!");
3905 * Add options until we hit the end of the line...
3908 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
3912 * If we found what we were looking for, stop now...
3919 * Set this as default if needed...
3922 if (!user_default_set
&& !_cups_strcasecmp(line
, "default"))
3924 DEBUG_puts("9cups_get_dests: Setting as default...");
3926 for (i
= 0; i
< num_dests
; i
++)
3927 (*dests
)[i
].is_default
= 0;
3929 dest
->is_default
= 1;
3934 * Close the file and return...
3944 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
3948 static char * /* O - New string */
3950 ipp_attribute_t
*attr
, /* I - Attribute to convert */
3951 char *buffer
, /* I - Buffer */
3952 size_t bufsize
) /* I - Size of buffer */
3954 int i
; /* Looping var */
3955 char *ptr
, /* Pointer into buffer */
3956 *end
, /* Pointer to end of buffer */
3957 *valptr
; /* Pointer into string attribute */
3961 * Return quickly if we have a single string value...
3964 if (attr
->num_values
== 1 &&
3965 attr
->value_tag
!= IPP_TAG_INTEGER
&&
3966 attr
->value_tag
!= IPP_TAG_ENUM
&&
3967 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
3968 attr
->value_tag
!= IPP_TAG_RANGE
)
3969 return (attr
->values
[0].string
.text
);
3972 * Copy the values to the string, separating with commas and escaping strings
3976 end
= buffer
+ bufsize
- 1;
3978 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
3983 switch (attr
->value_tag
)
3985 case IPP_TAG_INTEGER
:
3987 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].integer
);
3990 case IPP_TAG_BOOLEAN
:
3991 if (attr
->values
[i
].boolean
)
3992 strlcpy(ptr
, "true", (size_t)(end
- ptr
+ 1));
3994 strlcpy(ptr
, "false", (size_t)(end
- ptr
+ 1));
3997 case IPP_TAG_RANGE
:
3998 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
3999 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d", attr
->values
[i
].range
.lower
);
4001 snprintf(ptr
, (size_t)(end
- ptr
+ 1), "%d-%d", attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
4005 for (valptr
= attr
->values
[i
].string
.text
;
4006 *valptr
&& ptr
< end
;)
4008 if (strchr(" \t\n\\\'\"", *valptr
))
4010 if (ptr
>= (end
- 1))