]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * User-defined destination (and option) support for CUPS. |
ef416fc2 | 3 | * |
7536de1a | 4 | * Copyright 2007-2017 by Apple Inc. |
7e86f2f6 | 5 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 6 | * |
7e86f2f6 MS |
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 | |
57b7b66b | 11 | * missing or damaged, see the license at "http://www.cups.org/". |
ef416fc2 | 12 | * |
7e86f2f6 | 13 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include "cups-private.h" |
b423cd4c | 21 | #include <sys/stat.h> |
ef416fc2 | 22 | |
fa73b229 | 23 | #ifdef HAVE_NOTIFY_H |
24 | # include <notify.h> | |
25 | #endif /* HAVE_NOTIFY_H */ | |
26 | ||
dcb445bc MS |
27 | #ifdef HAVE_POLL |
28 | # include <poll.h> | |
29 | #endif /* HAVE_POLL */ | |
30 | ||
31 | #ifdef HAVE_DNSSD | |
32 | # include <dns_sd.h> | |
33 | #endif /* HAVE_DNSSD */ | |
34 | ||
a29fd7dd MS |
35 | #ifdef HAVE_AVAHI |
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 */ | |
44 | ||
dcb445bc MS |
45 | |
46 | /* | |
47 | * Constants... | |
48 | */ | |
49 | ||
080811b1 | 50 | #ifdef __APPLE__ |
9688061e MS |
51 | # if !TARGET_OS_IOS |
52 | # include <SystemConfiguration/SystemConfiguration.h> | |
53 | # define _CUPS_LOCATION_DEFAULTS 1 | |
54 | # endif /* !TARGET_OS_IOS */ | |
f14324a7 MS |
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") | |
080811b1 MS |
61 | #endif /* __APPLE__ */ |
62 | ||
3fae3b33 MS |
63 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
64 | # define _CUPS_DNSSD_MAXTIME 500 /* Milliseconds for maximum quantum of time */ | |
65 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ | |
66 | ||
ef416fc2 | 67 | |
dcb445bc MS |
68 | /* |
69 | * Types... | |
70 | */ | |
71 | ||
a29fd7dd | 72 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
dcb445bc MS |
73 | typedef enum _cups_dnssd_state_e /* Enumerated device state */ |
74 | { | |
75 | _CUPS_DNSSD_NEW, | |
76 | _CUPS_DNSSD_QUERY, | |
77 | _CUPS_DNSSD_PENDING, | |
78 | _CUPS_DNSSD_ACTIVE, | |
79 | _CUPS_DNSSD_LOCAL, | |
a29fd7dd | 80 | _CUPS_DNSSD_INCOMPATIBLE, |
dcb445bc MS |
81 | _CUPS_DNSSD_ERROR |
82 | } _cups_dnssd_state_t; | |
83 | ||
84 | typedef struct _cups_dnssd_data_s /* Enumeration data */ | |
85 | { | |
a29fd7dd | 86 | # ifdef HAVE_DNSSD |
dcb445bc | 87 | DNSServiceRef main_ref; /* Main service reference */ |
a29fd7dd MS |
88 | # else /* HAVE_AVAHI */ |
89 | AvahiSimplePoll *simple_poll; /* Polling interface */ | |
90 | AvahiClient *client; /* Client information */ | |
91 | int got_data; /* Did we get data? */ | |
657c5b5f | 92 | int browsers; /* How many browsers are running? */ |
a29fd7dd | 93 | # endif /* HAVE_DNSSD */ |
dcb445bc MS |
94 | cups_dest_cb_t cb; /* Callback */ |
95 | void *user_data; /* User data pointer */ | |
96 | cups_ptype_t type, /* Printer type filter */ | |
97 | mask; /* Printer type mask */ | |
98 | cups_array_t *devices; /* Devices found so far */ | |
99 | } _cups_dnssd_data_t; | |
100 | ||
101 | typedef struct _cups_dnssd_device_s /* Enumerated device */ | |
102 | { | |
103 | _cups_dnssd_state_t state; /* State of device listing */ | |
a29fd7dd | 104 | # ifdef HAVE_DNSSD |
dcb445bc | 105 | DNSServiceRef ref; /* Service reference for query */ |
a29fd7dd MS |
106 | # else /* HAVE_AVAHI */ |
107 | AvahiRecordBrowser *ref; /* Browser for query */ | |
108 | # endif /* HAVE_DNSSD */ | |
a2187a63 | 109 | char *fullName, /* Full name */ |
a2187a63 MS |
110 | *regtype, /* Registration type */ |
111 | *domain; /* Domain name */ | |
dcb445bc MS |
112 | cups_ptype_t type; /* Device registration type */ |
113 | cups_dest_t dest; /* Destination record */ | |
114 | } _cups_dnssd_device_t; | |
115 | ||
116 | typedef struct _cups_dnssd_resolve_s /* Data for resolving URI */ | |
117 | { | |
118 | int *cancel; /* Pointer to "cancel" variable */ | |
119 | struct timeval end_time; /* Ending time */ | |
120 | } _cups_dnssd_resolve_t; | |
121 | #endif /* HAVE_DNSSD */ | |
122 | ||
7536de1a MS |
123 | typedef struct _cups_getdata_s |
124 | { | |
125 | int num_dests; /* Number of destinations */ | |
126 | cups_dest_t *dests; /* Destinations */ | |
127 | } _cups_getdata_t; | |
128 | ||
4a366251 MS |
129 | typedef struct _cups_namedata_s |
130 | { | |
131 | const char *name; /* Named destination */ | |
132 | cups_dest_t *dest; /* Destination */ | |
133 | } _cups_namedata_t; | |
134 | ||
7536de1a | 135 | |
ef416fc2 | 136 | /* |
137 | * Local functions... | |
138 | */ | |
139 | ||
9688061e | 140 | #if _CUPS_LOCATION_DEFAULTS |
9c80ffa2 MS |
141 | static CFArrayRef appleCopyLocations(void); |
142 | static CFStringRef appleCopyNetwork(void); | |
9688061e MS |
143 | #endif /* _CUPS_LOCATION_DEFAULTS */ |
144 | #ifdef __APPLE__ | |
d649be59 | 145 | static char *appleGetPaperSize(char *name, size_t namesize); |
9688061e MS |
146 | #endif /* __APPLE__ */ |
147 | #if _CUPS_LOCATION_DEFAULTS | |
a29fd7dd MS |
148 | static CFStringRef appleGetPrinter(CFArrayRef locations, |
149 | CFStringRef network, CFIndex *locindex); | |
9688061e | 150 | #endif /* _CUPS_LOCATION_DEFAULTS */ |
9c80ffa2 MS |
151 | static cups_dest_t *cups_add_dest(const char *name, const char *instance, |
152 | int *num_dests, cups_dest_t **dests); | |
153 | #ifdef __BLOCKS__ | |
dcb445bc MS |
154 | static int cups_block_cb(cups_dest_block_t block, unsigned flags, |
155 | cups_dest_t *dest); | |
9c80ffa2 MS |
156 | #endif /* __BLOCKS__ */ |
157 | static int cups_compare_dests(cups_dest_t *a, cups_dest_t *b); | |
a29fd7dd MS |
158 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
159 | # ifdef HAVE_DNSSD | |
dcb445bc MS |
160 | static void cups_dnssd_browse_cb(DNSServiceRef sdRef, |
161 | DNSServiceFlags flags, | |
162 | uint32_t interfaceIndex, | |
163 | DNSServiceErrorType errorCode, | |
164 | const char *serviceName, | |
165 | const char *regtype, | |
166 | const char *replyDomain, | |
167 | void *context); | |
a29fd7dd MS |
168 | # else /* HAVE_AVAHI */ |
169 | static void cups_dnssd_browse_cb(AvahiServiceBrowser *browser, | |
170 | AvahiIfIndex interface, | |
171 | AvahiProtocol protocol, | |
172 | AvahiBrowserEvent event, | |
173 | const char *serviceName, | |
174 | const char *regtype, | |
175 | const char *replyDomain, | |
176 | AvahiLookupResultFlags flags, | |
177 | void *context); | |
178 | static void cups_dnssd_client_cb(AvahiClient *client, | |
179 | AvahiClientState state, | |
180 | void *context); | |
181 | # endif /* HAVE_DNSSD */ | |
dcb445bc MS |
182 | static int cups_dnssd_compare_devices(_cups_dnssd_device_t *a, |
183 | _cups_dnssd_device_t *b); | |
184 | static void cups_dnssd_free_device(_cups_dnssd_device_t *device, | |
185 | _cups_dnssd_data_t *data); | |
186 | static _cups_dnssd_device_t * | |
187 | cups_dnssd_get_device(_cups_dnssd_data_t *data, | |
188 | const char *serviceName, | |
189 | const char *regtype, | |
190 | const char *replyDomain); | |
a29fd7dd | 191 | # ifdef HAVE_DNSSD |
dcb445bc MS |
192 | static void cups_dnssd_local_cb(DNSServiceRef sdRef, |
193 | DNSServiceFlags flags, | |
194 | uint32_t interfaceIndex, | |
195 | DNSServiceErrorType errorCode, | |
196 | const char *serviceName, | |
197 | const char *regtype, | |
198 | const char *replyDomain, | |
199 | void *context); | |
200 | static void cups_dnssd_query_cb(DNSServiceRef sdRef, | |
201 | DNSServiceFlags flags, | |
202 | uint32_t interfaceIndex, | |
203 | DNSServiceErrorType errorCode, | |
204 | const char *fullName, | |
205 | uint16_t rrtype, uint16_t rrclass, | |
206 | uint16_t rdlen, const void *rdata, | |
207 | uint32_t ttl, void *context); | |
a29fd7dd MS |
208 | # else /* HAVE_AVAHI */ |
209 | static int cups_dnssd_poll_cb(struct pollfd *pollfds, | |
210 | unsigned int num_pollfds, | |
211 | int timeout, void *context); | |
212 | static void cups_dnssd_query_cb(AvahiRecordBrowser *browser, | |
213 | AvahiIfIndex interface, | |
214 | AvahiProtocol protocol, | |
215 | AvahiBrowserEvent event, | |
216 | const char *name, uint16_t rrclass, | |
217 | uint16_t rrtype, const void *rdata, | |
218 | size_t rdlen, | |
219 | AvahiLookupResultFlags flags, | |
220 | void *context); | |
221 | # endif /* HAVE_DNSSD */ | |
dcb445bc MS |
222 | static const char *cups_dnssd_resolve(cups_dest_t *dest, const char *uri, |
223 | int msec, int *cancel, | |
224 | cups_dest_cb_t cb, void *user_data); | |
225 | static int cups_dnssd_resolve_cb(void *context); | |
226 | static void cups_dnssd_unquote(char *dst, const char *src, | |
227 | size_t dstsize); | |
3fae3b33 | 228 | static int cups_elapsed(struct timeval *t); |
f34c1c99 | 229 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
9c80ffa2 MS |
230 | static int cups_find_dest(const char *name, const char *instance, |
231 | int num_dests, cups_dest_t *dests, int prev, | |
232 | int *rdiff); | |
7536de1a | 233 | static int cups_get_cb(_cups_getdata_t *data, unsigned flags, cups_dest_t *dest); |
9c80ffa2 MS |
234 | static char *cups_get_default(const char *filename, char *namebuf, |
235 | size_t namesize, const char **instance); | |
236 | static int cups_get_dests(const char *filename, const char *match_name, | |
237 | const char *match_inst, int user_default_set, | |
238 | int num_dests, cups_dest_t **dests); | |
239 | static char *cups_make_string(ipp_attribute_t *attr, char *buffer, | |
240 | size_t bufsize); | |
4a366251 | 241 | static int cups_name_cb(_cups_namedata_t *data, unsigned flags, cups_dest_t *dest); |
efa72f61 | 242 | static void cups_queue_name(char *name, const char *serviceName, size_t namesize); |
ef416fc2 | 243 | |
244 | ||
245 | /* | |
246 | * 'cupsAddDest()' - Add a destination to the list of destinations. | |
247 | * | |
2abf387c | 248 | * This function cannot be used to add a new class or printer queue, |
249 | * it only adds a new container of saved options for the named | |
250 | * destination or instance. | |
251 | * | |
252 | * If the named destination already exists, the destination list is | |
253 | * returned unchanged. Adding a new instance of a destination creates | |
254 | * a copy of that destination's options. | |
255 | * | |
5a738aea | 256 | * Use the @link cupsSaveDests@ function to save the updated list of |
2abf387c | 257 | * destinations to the user's lpoptions file. |
ef416fc2 | 258 | */ |
259 | ||
260 | int /* O - New number of destinations */ | |
2abf387c | 261 | cupsAddDest(const char *name, /* I - Destination name */ |
5a738aea | 262 | const char *instance, /* I - Instance name or @code NULL@ for none/primary */ |
ef416fc2 | 263 | int num_dests, /* I - Number of destinations */ |
264 | cups_dest_t **dests) /* IO - Destinations */ | |
265 | { | |
266 | int i; /* Looping var */ | |
267 | cups_dest_t *dest; /* Destination pointer */ | |
bf3816c7 | 268 | cups_dest_t *parent = NULL; /* Parent destination */ |
426c6a59 MS |
269 | cups_option_t *doption, /* Current destination option */ |
270 | *poption; /* Current parent option */ | |
ef416fc2 | 271 | |
272 | ||
2abf387c | 273 | if (!name || !dests) |
ef416fc2 | 274 | return (0); |
275 | ||
426c6a59 MS |
276 | if (!cupsGetDest(name, instance, num_dests, *dests)) |
277 | { | |
41681883 | 278 | if (instance && !cupsGetDest(name, NULL, num_dests, *dests)) |
426c6a59 | 279 | return (num_dests); |
ef416fc2 | 280 | |
5a9febac MS |
281 | if ((dest = cups_add_dest(name, instance, &num_dests, dests)) == NULL) |
282 | return (num_dests); | |
2abf387c | 283 | |
5a662dc0 MS |
284 | /* |
285 | * Find the base dest again now the array has been realloc'd. | |
286 | */ | |
287 | ||
288 | parent = cupsGetDest(name, NULL, num_dests, *dests); | |
289 | ||
426c6a59 MS |
290 | if (instance && parent && parent->num_options > 0) |
291 | { | |
292 | /* | |
293 | * Copy options from parent... | |
294 | */ | |
ef416fc2 | 295 | |
7e86f2f6 | 296 | dest->options = calloc(sizeof(cups_option_t), (size_t)parent->num_options); |
2abf387c | 297 | |
426c6a59 MS |
298 | if (dest->options) |
299 | { | |
300 | dest->num_options = parent->num_options; | |
ef416fc2 | 301 | |
426c6a59 MS |
302 | for (i = dest->num_options, doption = dest->options, |
303 | poption = parent->options; | |
304 | i > 0; | |
305 | i --, doption ++, poption ++) | |
306 | { | |
307 | doption->name = _cupsStrRetain(poption->name); | |
308 | doption->value = _cupsStrRetain(poption->value); | |
309 | } | |
310 | } | |
2abf387c | 311 | } |
312 | } | |
313 | ||
426c6a59 | 314 | return (num_dests); |
ef416fc2 | 315 | } |
316 | ||
317 | ||
c8fef167 MS |
318 | #ifdef __APPLE__ |
319 | /* | |
320 | * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID. | |
321 | */ | |
322 | ||
323 | CFStringRef /* O - Default paper ID */ | |
324 | _cupsAppleCopyDefaultPaperID(void) | |
325 | { | |
f14324a7 MS |
326 | return (CFPreferencesCopyAppValue(kDefaultPaperIDKey, |
327 | kCUPSPrintingPrefs)); | |
328 | } | |
329 | ||
330 | ||
331 | /* | |
332 | * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location. | |
333 | */ | |
334 | ||
335 | CFStringRef /* O - Default printer name */ | |
336 | _cupsAppleCopyDefaultPrinter(void) | |
337 | { | |
9688061e | 338 | # if _CUPS_LOCATION_DEFAULTS |
f14324a7 MS |
339 | CFStringRef network; /* Network location */ |
340 | CFArrayRef locations; /* Location array */ | |
341 | CFStringRef locprinter; /* Current printer */ | |
c8fef167 MS |
342 | |
343 | ||
f14324a7 MS |
344 | /* |
345 | * Use location-based defaults only if "use last printer" is selected in the | |
346 | * system preferences... | |
347 | */ | |
c8fef167 | 348 | |
f14324a7 MS |
349 | if (!_cupsAppleGetUseLastPrinter()) |
350 | { | |
351 | DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as " | |
352 | "default."); | |
353 | return (NULL); | |
354 | } | |
c8fef167 | 355 | |
f14324a7 MS |
356 | /* |
357 | * Get the current location... | |
358 | */ | |
359 | ||
360 | if ((network = appleCopyNetwork()) == NULL) | |
361 | { | |
362 | DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current " | |
363 | "network."); | |
364 | return (NULL); | |
365 | } | |
366 | ||
f14324a7 MS |
367 | /* |
368 | * Lookup the network in the preferences... | |
369 | */ | |
370 | ||
371 | if ((locations = appleCopyLocations()) == NULL) | |
372 | { | |
373 | /* | |
374 | * Missing or bad location array, so no location-based default... | |
375 | */ | |
376 | ||
377 | DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used " | |
378 | "printer array."); | |
379 | ||
380 | CFRelease(network); | |
381 | ||
382 | return (NULL); | |
383 | } | |
88f9aafc | 384 | |
f14324a7 MS |
385 | DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.", |
386 | (int)CFArrayGetCount(locations))); | |
387 | ||
388 | if ((locprinter = appleGetPrinter(locations, network, NULL)) != NULL) | |
389 | CFRetain(locprinter); | |
390 | ||
391 | CFRelease(network); | |
392 | CFRelease(locations); | |
393 | ||
394 | return (locprinter); | |
9688061e MS |
395 | |
396 | # else | |
397 | return (NULL); | |
398 | # endif /* _CUPS_LOCATION_DEFAULTS */ | |
c8fef167 MS |
399 | } |
400 | ||
401 | ||
402 | /* | |
403 | * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer. | |
404 | */ | |
405 | ||
406 | int /* O - 1 to use last printer, 0 otherwise */ | |
407 | _cupsAppleGetUseLastPrinter(void) | |
408 | { | |
409 | Boolean uselast, /* Use last printer preference value */ | |
410 | uselast_set; /* Valid is set? */ | |
411 | ||
412 | ||
413 | if (getenv("CUPS_DISABLE_APPLE_DEFAULT")) | |
414 | return (0); | |
415 | ||
f14324a7 MS |
416 | uselast = CFPreferencesGetAppBooleanValue(kUseLastPrinter, |
417 | kCUPSPrintingPrefs, | |
c8fef167 | 418 | &uselast_set); |
c8fef167 MS |
419 | if (!uselast_set) |
420 | return (1); | |
421 | else | |
422 | return (uselast); | |
423 | } | |
424 | ||
425 | ||
426 | /* | |
427 | * '_cupsAppleSetDefaultPaperID()' - Set the default paper id. | |
428 | */ | |
429 | ||
430 | void | |
431 | _cupsAppleSetDefaultPaperID( | |
432 | CFStringRef name) /* I - New paper ID */ | |
433 | { | |
f14324a7 MS |
434 | CFPreferencesSetAppValue(kDefaultPaperIDKey, name, kCUPSPrintingPrefs); |
435 | CFPreferencesAppSynchronize(kCUPSPrintingPrefs); | |
684d1464 MS |
436 | |
437 | # ifdef HAVE_NOTIFY_POST | |
f14324a7 | 438 | notify_post("com.apple.printerPrefsChange"); |
684d1464 | 439 | # endif /* HAVE_NOTIFY_POST */ |
f14324a7 MS |
440 | } |
441 | ||
442 | ||
443 | /* | |
444 | * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location. | |
445 | */ | |
446 | ||
447 | void | |
448 | _cupsAppleSetDefaultPrinter( | |
449 | CFStringRef name) /* I - Default printer/class name */ | |
450 | { | |
9688061e | 451 | # if _CUPS_LOCATION_DEFAULTS |
f14324a7 MS |
452 | CFStringRef network; /* Current network */ |
453 | CFArrayRef locations; /* Old locations array */ | |
454 | CFIndex locindex; /* Index in locations array */ | |
455 | CFStringRef locprinter; /* Current printer */ | |
456 | CFMutableArrayRef newlocations; /* New locations array */ | |
457 | CFMutableDictionaryRef newlocation; /* New location */ | |
458 | ||
459 | ||
460 | /* | |
461 | * Get the current location... | |
462 | */ | |
463 | ||
464 | if ((network = appleCopyNetwork()) == NULL) | |
465 | { | |
466 | DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network..."); | |
467 | return; | |
468 | } | |
469 | ||
470 | /* | |
471 | * Lookup the network in the preferences... | |
472 | */ | |
473 | ||
474 | if ((locations = appleCopyLocations()) != NULL) | |
475 | locprinter = appleGetPrinter(locations, network, &locindex); | |
476 | else | |
477 | { | |
478 | locprinter = NULL; | |
479 | locindex = -1; | |
480 | } | |
481 | ||
482 | if (!locprinter || CFStringCompare(locprinter, name, 0) != kCFCompareEqualTo) | |
483 | { | |
484 | /* | |
485 | * Need to change the locations array... | |
486 | */ | |
487 | ||
488 | if (locations) | |
489 | { | |
490 | newlocations = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, | |
491 | locations); | |
492 | ||
493 | if (locprinter) | |
494 | CFArrayRemoveValueAtIndex(newlocations, locindex); | |
495 | } | |
496 | else | |
497 | newlocations = CFArrayCreateMutable(kCFAllocatorDefault, 0, | |
498 | &kCFTypeArrayCallBacks); | |
499 | ||
500 | newlocation = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
501 | &kCFTypeDictionaryKeyCallBacks, | |
502 | &kCFTypeDictionaryValueCallBacks); | |
503 | ||
504 | if (newlocation && newlocations) | |
505 | { | |
506 | /* | |
507 | * Put the new location at the front of the array... | |
508 | */ | |
509 | ||
510 | CFDictionaryAddValue(newlocation, kLocationNetworkKey, network); | |
511 | CFDictionaryAddValue(newlocation, kLocationPrinterIDKey, name); | |
512 | CFArrayInsertValueAtIndex(newlocations, 0, newlocation); | |
513 | ||
514 | /* | |
515 | * Limit the number of locations to 10... | |
516 | */ | |
517 | ||
518 | while (CFArrayGetCount(newlocations) > 10) | |
519 | CFArrayRemoveValueAtIndex(newlocations, 10); | |
520 | ||
521 | /* | |
522 | * Push the changes out... | |
523 | */ | |
524 | ||
525 | CFPreferencesSetAppValue(kLastUsedPrintersKey, newlocations, | |
526 | kCUPSPrintingPrefs); | |
527 | CFPreferencesAppSynchronize(kCUPSPrintingPrefs); | |
684d1464 MS |
528 | |
529 | # ifdef HAVE_NOTIFY_POST | |
f14324a7 | 530 | notify_post("com.apple.printerPrefsChange"); |
684d1464 | 531 | # endif /* HAVE_NOTIFY_POST */ |
f14324a7 MS |
532 | } |
533 | ||
534 | if (newlocations) | |
535 | CFRelease(newlocations); | |
536 | ||
537 | if (newlocation) | |
538 | CFRelease(newlocation); | |
539 | } | |
540 | ||
541 | if (locations) | |
542 | CFRelease(locations); | |
543 | ||
544 | CFRelease(network); | |
9688061e MS |
545 | |
546 | # else | |
547 | (void)name; | |
548 | # endif /* _CUPS_LOCATION_DEFAULTS */ | |
c8fef167 MS |
549 | } |
550 | ||
551 | ||
552 | /* | |
553 | * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer. | |
554 | */ | |
88f9aafc MS |
555 | |
556 | void | |
c8fef167 MS |
557 | _cupsAppleSetUseLastPrinter( |
558 | int uselast) /* O - 1 to use last printer, 0 otherwise */ | |
559 | { | |
88f9aafc | 560 | CFPreferencesSetAppValue(kUseLastPrinter, |
c8fef167 | 561 | uselast ? kCFBooleanTrue : kCFBooleanFalse, |
f14324a7 MS |
562 | kCUPSPrintingPrefs); |
563 | CFPreferencesAppSynchronize(kCUPSPrintingPrefs); | |
684d1464 MS |
564 | |
565 | # ifdef HAVE_NOTIFY_POST | |
f14324a7 | 566 | notify_post("com.apple.printerPrefsChange"); |
684d1464 | 567 | # endif /* HAVE_NOTIFY_POST */ |
c8fef167 MS |
568 | } |
569 | #endif /* __APPLE__ */ | |
570 | ||
571 | ||
9c80ffa2 | 572 | /* |
98d88c8d | 573 | * 'cupsConnectDest()' - Open a conection to the destination. |
9c80ffa2 | 574 | * |
98d88c8d MS |
575 | * Connect to the destination, returning a new @code http_t@ connection object |
576 | * and optionally the resource path to use for the destination. These calls | |
577 | * will block until a connection is made, the timeout expires, the integer | |
578 | * pointed to by "cancel" is non-zero, or the callback function (or block) | |
579 | * returns 0. The caller is responsible for calling @link httpClose@ on the | |
580 | * returned connection. | |
dcb445bc | 581 | * |
90c67342 MS |
582 | * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@ |
583 | * for the "flags" argument to connect directly to the device associated with | |
584 | * the destination. Otherwise, the connection is made to the CUPS scheduler | |
585 | * associated with the destination. | |
586 | * | |
8072030b | 587 | * @since CUPS 1.6/macOS 10.8@ |
9c80ffa2 MS |
588 | */ |
589 | ||
98d88c8d | 590 | http_t * /* O - Connection to destination or @code NULL@ */ |
dcb445bc MS |
591 | cupsConnectDest( |
592 | cups_dest_t *dest, /* I - Destination */ | |
593 | unsigned flags, /* I - Connection flags */ | |
594 | int msec, /* I - Timeout in milliseconds */ | |
595 | int *cancel, /* I - Pointer to "cancel" variable */ | |
596 | char *resource, /* I - Resource buffer */ | |
597 | size_t resourcesize, /* I - Size of resource buffer */ | |
598 | cups_dest_cb_t cb, /* I - Callback function */ | |
599 | void *user_data) /* I - User data pointer */ | |
9c80ffa2 | 600 | { |
dcb445bc MS |
601 | const char *uri; /* Printer URI */ |
602 | char scheme[32], /* URI scheme */ | |
603 | userpass[256], /* Username and password (unused) */ | |
604 | hostname[256], /* Hostname */ | |
605 | tempresource[1024]; /* Temporary resource buffer */ | |
606 | int port; /* Port number */ | |
607 | char portstr[16]; /* Port number string */ | |
12f89d24 | 608 | http_encryption_t encryption; /* Encryption to use */ |
dcb445bc MS |
609 | http_addrlist_t *addrlist; /* Address list for server */ |
610 | http_t *http; /* Connection to server */ | |
611 | ||
612 | ||
807315e6 | 613 | 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)); |
4db7fcee | 614 | |
dcb445bc MS |
615 | /* |
616 | * Range check input... | |
617 | */ | |
618 | ||
619 | if (!dest) | |
620 | { | |
621 | if (resource) | |
622 | *resource = '\0'; | |
623 | ||
cb7f98ee | 624 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
dcb445bc MS |
625 | return (NULL); |
626 | } | |
627 | ||
628 | if (!resource || resourcesize < 1) | |
629 | { | |
630 | resource = tempresource; | |
631 | resourcesize = sizeof(tempresource); | |
632 | } | |
633 | ||
634 | /* | |
635 | * Grab the printer URI... | |
636 | */ | |
637 | ||
90c67342 MS |
638 | if (flags & CUPS_DEST_FLAGS_DEVICE) |
639 | { | |
c9a0ff62 | 640 | if ((uri = cupsGetOption("device-uri", dest->num_options, dest->options)) != NULL) |
90c67342 | 641 | { |
90c67342 | 642 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
c9a0ff62 MS |
643 | if (strstr(uri, "._tcp")) |
644 | uri = cups_dnssd_resolve(dest, uri, msec, cancel, cb, user_data); | |
90c67342 | 645 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
90c67342 MS |
646 | } |
647 | } | |
648 | else if ((uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options)) == NULL) | |
dcb445bc | 649 | { |
c9a0ff62 | 650 | if ((uri = cupsGetOption("device-uri", dest->num_options, dest->options)) != NULL) |
46385a1a | 651 | { |
46385a1a | 652 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
c9a0ff62 MS |
653 | if (strstr(uri, "._tcp")) |
654 | uri = cups_dnssd_resolve(dest, uri, msec, cancel, cb, user_data); | |
46385a1a MS |
655 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
656 | } | |
dcb445bc | 657 | |
46385a1a MS |
658 | if (uri) |
659 | uri = _cupsCreateDest(dest->name, cupsGetOption("printer-info", dest->num_options, dest->options), NULL, uri, tempresource, sizeof(tempresource)); | |
dcb445bc | 660 | |
46385a1a MS |
661 | if (uri) |
662 | { | |
663 | dest->num_options = cupsAddOption("printer-uri-supported", uri, dest->num_options, &dest->options); | |
664 | ||
665 | uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options); | |
666 | } | |
dcb445bc MS |
667 | } |
668 | ||
46385a1a | 669 | if (!uri) |
dcb445bc | 670 | { |
46385a1a MS |
671 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0); |
672 | ||
673 | if (cb) | |
674 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest); | |
675 | ||
676 | return (NULL); | |
dcb445bc | 677 | } |
dcb445bc MS |
678 | |
679 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), | |
680 | userpass, sizeof(userpass), hostname, sizeof(hostname), | |
7e86f2f6 | 681 | &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK) |
dcb445bc | 682 | { |
f5f2e19e | 683 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1); |
dcb445bc MS |
684 | |
685 | if (cb) | |
686 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, | |
687 | dest); | |
688 | ||
689 | return (NULL); | |
690 | } | |
691 | ||
692 | /* | |
693 | * Lookup the address for the server... | |
694 | */ | |
695 | ||
696 | if (cb) | |
46385a1a | 697 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, dest); |
dcb445bc MS |
698 | |
699 | snprintf(portstr, sizeof(portstr), "%d", port); | |
700 | ||
701 | if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portstr)) == NULL) | |
702 | { | |
703 | if (cb) | |
46385a1a | 704 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest); |
dcb445bc MS |
705 | |
706 | return (NULL); | |
707 | } | |
708 | ||
709 | if (cancel && *cancel) | |
710 | { | |
711 | httpAddrFreeList(addrlist); | |
712 | ||
713 | if (cb) | |
46385a1a | 714 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CANCELED, dest); |
dcb445bc MS |
715 | |
716 | return (NULL); | |
717 | } | |
718 | ||
719 | /* | |
720 | * Create the HTTP object pointing to the server referenced by the URI... | |
721 | */ | |
722 | ||
723 | if (!strcmp(scheme, "ipps") || port == 443) | |
cb7f98ee | 724 | encryption = HTTP_ENCRYPTION_ALWAYS; |
dcb445bc | 725 | else |
cb7f98ee | 726 | encryption = HTTP_ENCRYPTION_IF_REQUESTED; |
dcb445bc | 727 | |
46385a1a | 728 | http = httpConnect2(hostname, port, addrlist, AF_UNSPEC, encryption, 1, 0, NULL); |
520fe1de | 729 | httpAddrFreeList(addrlist); |
dcb445bc MS |
730 | |
731 | /* | |
732 | * Connect if requested... | |
733 | */ | |
734 | ||
735 | if (flags & CUPS_DEST_FLAGS_UNCONNECTED) | |
736 | { | |
737 | if (cb) | |
738 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED, dest); | |
739 | } | |
740 | else | |
741 | { | |
742 | if (cb) | |
46385a1a | 743 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CONNECTING, dest); |
dcb445bc MS |
744 | |
745 | if (!httpReconnect2(http, msec, cancel) && cb) | |
746 | { | |
747 | if (cancel && *cancel) | |
46385a1a | 748 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_CONNECTING, dest); |
dcb445bc | 749 | else |
46385a1a | 750 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest); |
dcb445bc MS |
751 | } |
752 | else if (cb) | |
753 | (*cb)(user_data, CUPS_DEST_FLAGS_NONE, dest); | |
754 | } | |
755 | ||
756 | return (http); | |
757 | } | |
758 | ||
759 | ||
760 | #ifdef __BLOCKS__ | |
761 | /* | |
98d88c8d | 762 | * 'cupsConnectDestBlock()' - Open a connection to the destination. |
dcb445bc | 763 | * |
98d88c8d MS |
764 | * Connect to the destination, returning a new @code http_t@ connection object |
765 | * and optionally the resource path to use for the destination. These calls | |
766 | * will block until a connection is made, the timeout expires, the integer | |
767 | * pointed to by "cancel" is non-zero, or the block returns 0. The caller is | |
768 | * responsible for calling @link httpClose@ on the returned connection. | |
dcb445bc | 769 | * |
90c67342 MS |
770 | * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@ |
771 | * for the "flags" argument to connect directly to the device associated with | |
772 | * the destination. Otherwise, the connection is made to the CUPS scheduler | |
773 | * associated with the destination. | |
774 | * | |
53af7f21 | 775 | * @since CUPS 1.6/macOS 10.8@ @exclude all@ |
dcb445bc MS |
776 | */ |
777 | ||
98d88c8d | 778 | http_t * /* O - Connection to destination or @code NULL@ */ |
dcb445bc MS |
779 | cupsConnectDestBlock( |
780 | cups_dest_t *dest, /* I - Destination */ | |
781 | unsigned flags, /* I - Connection flags */ | |
782 | int msec, /* I - Timeout in milliseconds */ | |
783 | int *cancel, /* I - Pointer to "cancel" variable */ | |
784 | char *resource, /* I - Resource buffer */ | |
785 | size_t resourcesize, /* I - Size of resource buffer */ | |
786 | cups_dest_block_t block) /* I - Callback block */ | |
787 | { | |
788 | return (cupsConnectDest(dest, flags, msec, cancel, resource, resourcesize, | |
789 | (cups_dest_cb_t)cups_block_cb, (void *)block)); | |
790 | } | |
791 | #endif /* __BLOCKS__ */ | |
792 | ||
793 | ||
794 | /* | |
795 | * 'cupsCopyDest()' - Copy a destination. | |
796 | * | |
797 | * Make a copy of the destination to an array of destinations (or just a single | |
798 | * copy) - for use with the cupsEnumDests* functions. The caller is responsible | |
799 | * for calling cupsFreeDests() on the returned object(s). | |
800 | * | |
8072030b | 801 | * @since CUPS 1.6/macOS 10.8@ |
dcb445bc MS |
802 | */ |
803 | ||
798d6e29 MS |
804 | int /* O - New number of destinations */ |
805 | cupsCopyDest(cups_dest_t *dest, /* I - Destination to copy */ | |
806 | int num_dests, /* I - Number of destinations */ | |
807 | cups_dest_t **dests) /* IO - Destination array */ | |
dcb445bc MS |
808 | { |
809 | int i; /* Looping var */ | |
810 | cups_dest_t *new_dest; /* New destination pointer */ | |
811 | cups_option_t *new_option, /* Current destination option */ | |
812 | *option; /* Current parent option */ | |
813 | ||
814 | ||
815 | /* | |
816 | * Range check input... | |
817 | */ | |
818 | ||
819 | if (!dest || num_dests < 0 || !dests) | |
820 | return (num_dests); | |
821 | ||
822 | /* | |
823 | * See if the destination already exists... | |
824 | */ | |
825 | ||
826 | if ((new_dest = cupsGetDest(dest->name, dest->instance, num_dests, | |
827 | *dests)) != NULL) | |
828 | { | |
829 | /* | |
830 | * Protect against copying destination to itself... | |
831 | */ | |
832 | ||
833 | if (new_dest == dest) | |
834 | return (num_dests); | |
835 | ||
836 | /* | |
837 | * Otherwise, free the options... | |
838 | */ | |
839 | ||
840 | cupsFreeOptions(new_dest->num_options, new_dest->options); | |
841 | ||
842 | new_dest->num_options = 0; | |
843 | new_dest->options = NULL; | |
844 | } | |
845 | else | |
846 | new_dest = cups_add_dest(dest->name, dest->instance, &num_dests, dests); | |
847 | ||
848 | if (new_dest) | |
849 | { | |
7e86f2f6 | 850 | if ((new_dest->options = calloc(sizeof(cups_option_t), (size_t)dest->num_options)) == NULL) |
dcb445bc MS |
851 | return (cupsRemoveDest(dest->name, dest->instance, num_dests, dests)); |
852 | ||
853 | new_dest->num_options = dest->num_options; | |
854 | ||
855 | for (i = dest->num_options, option = dest->options, | |
856 | new_option = new_dest->options; | |
857 | i > 0; | |
858 | i --, option ++, new_option ++) | |
859 | { | |
860 | new_option->name = _cupsStrRetain(option->name); | |
861 | new_option->value = _cupsStrRetain(option->value); | |
862 | } | |
863 | } | |
864 | ||
865 | return (num_dests); | |
9c80ffa2 MS |
866 | } |
867 | ||
868 | ||
7ae00c35 MS |
869 | /* |
870 | * '_cupsCreateDest()' - Create a local (temporary) queue. | |
871 | */ | |
872 | ||
873 | char * /* O - Printer URI or @code NULL@ on error */ | |
874 | _cupsCreateDest(const char *name, /* I - Printer name */ | |
875 | const char *info, /* I - Printer description of @code NULL@ */ | |
876 | const char *device_id, /* I - 1284 Device ID or @code NULL@ */ | |
877 | const char *device_uri, /* I - Device URI */ | |
878 | char *uri, /* I - Printer URI buffer */ | |
879 | size_t urisize) /* I - Size of URI buffer */ | |
880 | { | |
881 | http_t *http; /* Connection to server */ | |
882 | ipp_t *request, /* CUPS-Create-Local-Printer request */ | |
883 | *response; /* CUPS-Create-Local-Printer response */ | |
884 | ipp_attribute_t *attr; /* printer-uri-supported attribute */ | |
c585706d MS |
885 | ipp_pstate_t state = IPP_PSTATE_STOPPED; |
886 | /* printer-state value */ | |
7ae00c35 MS |
887 | |
888 | ||
7ae00c35 MS |
889 | if (!name || !device_uri || !uri || urisize < 32) |
890 | return (NULL); | |
891 | ||
892 | if ((http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL)) == NULL) | |
893 | return (NULL); | |
894 | ||
895 | request = ippNewRequest(IPP_OP_CUPS_CREATE_LOCAL_PRINTER); | |
896 | ||
4db7fcee | 897 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, "ipp://localhost/"); |
7ae00c35 MS |
898 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); |
899 | ||
900 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL, device_uri); | |
901 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL, name); | |
902 | if (info) | |
903 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", NULL, info); | |
904 | if (device_id) | |
905 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-device-id", NULL, device_id); | |
906 | ||
907 | response = cupsDoRequest(http, request, "/"); | |
908 | ||
7ae00c35 MS |
909 | if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) |
910 | strlcpy(uri, ippGetString(attr, 0, NULL), urisize); | |
c585706d MS |
911 | else |
912 | { | |
913 | ippDelete(response); | |
914 | httpClose(http); | |
915 | return (NULL); | |
916 | } | |
917 | ||
918 | if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) | |
919 | state = (ipp_pstate_t)ippGetInteger(attr, 0); | |
920 | ||
921 | while (state == IPP_PSTATE_STOPPED && cupsLastError() == IPP_STATUS_OK) | |
922 | { | |
923 | sleep(1); | |
924 | ippDelete(response); | |
925 | ||
926 | request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES); | |
927 | ||
928 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); | |
929 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser()); | |
930 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "printer-state"); | |
931 | ||
932 | response = cupsDoRequest(http, request, "/"); | |
933 | ||
934 | if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) | |
935 | state = (ipp_pstate_t)ippGetInteger(attr, 0); | |
936 | } | |
7ae00c35 MS |
937 | |
938 | ippDelete(response); | |
939 | ||
c585706d MS |
940 | httpClose(http); |
941 | ||
942 | return (uri); | |
7ae00c35 MS |
943 | } |
944 | ||
945 | ||
9c80ffa2 MS |
946 | /* |
947 | * 'cupsEnumDests()' - Enumerate available destinations with a callback function. | |
948 | * | |
f50db552 MS |
949 | * Destinations are enumerated from one or more sources. The callback function |
950 | * receives the @code user_data@ pointer and the destination pointer which can | |
951 | * be used as input to the @link cupsCopyDest@ function. The function must | |
952 | * return 1 to continue enumeration or 0 to stop. | |
953 | * | |
954 | * The @code type@ and @code mask@ arguments allow the caller to filter the | |
955 | * destinations that are enumerated. Passing 0 for both will enumerate all | |
956 | * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on | |
957 | * destinations that are available but have not yet been added locally. | |
9c80ffa2 | 958 | * |
dcb445bc MS |
959 | * Enumeration happens on the current thread and does not return until all |
960 | * destinations have been enumerated or the callback function returns 0. | |
9c80ffa2 | 961 | * |
500fca27 MS |
962 | * Note: The callback function will likely receive multiple updates for the same |
963 | * destinations - it is up to the caller to suppress any duplicate destinations. | |
964 | * | |
8072030b | 965 | * @since CUPS 1.6/macOS 10.8@ |
9c80ffa2 MS |
966 | */ |
967 | ||
968 | int /* O - 1 on success, 0 on failure */ | |
dcb445bc | 969 | cupsEnumDests( |
a2187a63 MS |
970 | unsigned flags, /* I - Enumeration flags */ |
971 | int msec, /* I - Timeout in milliseconds, -1 for indefinite */ | |
972 | int *cancel, /* I - Pointer to "cancel" variable */ | |
973 | cups_ptype_t type, /* I - Printer type bits */ | |
974 | cups_ptype_t mask, /* I - Mask for printer type bits */ | |
975 | cups_dest_cb_t cb, /* I - Callback function */ | |
976 | void *user_data) /* I - User data */ | |
9c80ffa2 | 977 | { |
dcb445bc MS |
978 | int i, /* Looping var */ |
979 | num_dests; /* Number of destinations */ | |
82cc1f9a | 980 | cups_dest_t *dests = NULL, /* Destinations */ |
dcb445bc | 981 | *dest; /* Current destination */ |
4db41741 MS |
982 | const char *defprinter; /* Default printer */ |
983 | char name[1024], /* Copy of printer name */ | |
984 | *instance, /* Pointer to instance name */ | |
985 | *user_default; /* User default printer */ | |
a29fd7dd MS |
986 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
987 | int count, /* Number of queries started */ | |
a2187a63 | 988 | completed, /* Number of completed queries */ |
dcb445bc | 989 | remaining; /* Remainder of timeout */ |
3fae3b33 | 990 | struct timeval curtime; /* Current time */ |
dcb445bc MS |
991 | _cups_dnssd_data_t data; /* Data for callback */ |
992 | _cups_dnssd_device_t *device; /* Current device */ | |
a29fd7dd MS |
993 | # ifdef HAVE_DNSSD |
994 | int nfds, /* Number of files responded */ | |
995 | main_fd; /* File descriptor for lookups */ | |
7f500d89 MS |
996 | DNSServiceRef ipp_ref = NULL, /* IPP browser */ |
997 | local_ipp_ref = NULL; /* Local IPP browser */ | |
a29fd7dd | 998 | # ifdef HAVE_SSL |
7f500d89 MS |
999 | DNSServiceRef ipps_ref = NULL,/* IPPS browser */ |
1000 | local_ipps_ref = NULL; /* Local IPPS browser */ | |
a29fd7dd MS |
1001 | # endif /* HAVE_SSL */ |
1002 | # ifdef HAVE_POLL | |
dcb445bc | 1003 | struct pollfd pfd; /* Polling data */ |
a29fd7dd | 1004 | # else |
dcb445bc MS |
1005 | fd_set input; /* Input set for select() */ |
1006 | struct timeval timeout; /* Timeout for select() */ | |
a29fd7dd MS |
1007 | # endif /* HAVE_POLL */ |
1008 | # else /* HAVE_AVAHI */ | |
1009 | int error; /* Error value */ | |
7f500d89 | 1010 | AvahiServiceBrowser *ipp_ref = NULL;/* IPP browser */ |
a29fd7dd | 1011 | # ifdef HAVE_SSL |
7f500d89 | 1012 | AvahiServiceBrowser *ipps_ref = NULL; /* IPPS browser */ |
a29fd7dd MS |
1013 | # endif /* HAVE_SSL */ |
1014 | # endif /* HAVE_DNSSD */ | |
1015 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ | |
dcb445bc | 1016 | |
7f500d89 | 1017 | |
dcb445bc MS |
1018 | /* |
1019 | * Range check input... | |
1020 | */ | |
1021 | ||
f3c17241 MS |
1022 | (void)flags; |
1023 | ||
dcb445bc MS |
1024 | if (!cb) |
1025 | return (0); | |
1026 | ||
1027 | /* | |
f50db552 | 1028 | * Get ready to enumerate... |
dcb445bc MS |
1029 | */ |
1030 | ||
a2187a63 | 1031 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
7f500d89 MS |
1032 | memset(&data, 0, sizeof(data)); |
1033 | ||
a2187a63 MS |
1034 | data.type = type; |
1035 | data.mask = mask; | |
1036 | data.cb = cb; | |
1037 | data.user_data = user_data; | |
1038 | data.devices = cupsArrayNew3((cups_array_func_t)cups_dnssd_compare_devices, NULL, NULL, 0, NULL, (cups_afree_func_t)cups_dnssd_free_device); | |
1039 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ | |
1040 | ||
f50db552 | 1041 | if (!(mask & CUPS_PRINTER_DISCOVERED) || !(type & CUPS_PRINTER_DISCOVERED)) |
a2187a63 | 1042 | { |
f50db552 MS |
1043 | /* |
1044 | * Get the list of local printers and pass them to the callback function... | |
1045 | */ | |
a2187a63 | 1046 | |
f50db552 MS |
1047 | num_dests = _cupsGetDests(CUPS_HTTP_DEFAULT, IPP_OP_CUPS_GET_PRINTERS, NULL, |
1048 | &dests, type, mask); | |
dcb445bc | 1049 | |
f50db552 MS |
1050 | if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL) |
1051 | defprinter = name; | |
1052 | else if ((defprinter = cupsGetDefault2(CUPS_HTTP_DEFAULT)) != NULL) | |
1053 | { | |
1054 | strlcpy(name, defprinter, sizeof(name)); | |
1055 | defprinter = name; | |
1056 | } | |
1057 | ||
1058 | if (defprinter) | |
a2187a63 MS |
1059 | { |
1060 | /* | |
f50db552 | 1061 | * Separate printer and instance name... |
a2187a63 MS |
1062 | */ |
1063 | ||
f50db552 MS |
1064 | if ((instance = strchr(name, '/')) != NULL) |
1065 | *instance++ = '\0'; | |
a2187a63 | 1066 | |
f50db552 MS |
1067 | /* |
1068 | * Lookup the printer and instance and make it the default... | |
1069 | */ | |
a2187a63 | 1070 | |
f50db552 MS |
1071 | if ((dest = cupsGetDest(name, instance, num_dests, dests)) != NULL) |
1072 | dest->is_default = 1; | |
a2187a63 | 1073 | } |
f50db552 MS |
1074 | |
1075 | for (i = num_dests, dest = dests; | |
1076 | i > 0 && (!cancel || !*cancel); | |
1077 | i --, dest ++) | |
1078 | { | |
1079 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) | |
1080 | const char *device_uri; /* Device URI */ | |
657c5b5f | 1081 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
a2187a63 | 1082 | |
f50db552 MS |
1083 | if (!(*cb)(user_data, i > 1 ? CUPS_DEST_FLAGS_MORE : CUPS_DEST_FLAGS_NONE, |
1084 | dest)) | |
1085 | break; | |
dcb445bc | 1086 | |
657c5b5f | 1087 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
f50db552 MS |
1088 | if (!dest->instance && (device_uri = cupsGetOption("device-uri", dest->num_options, dest->options)) != NULL && !strncmp(device_uri, "dnssd://", 8)) |
1089 | { | |
1090 | /* | |
1091 | * Add existing queue using service name, etc. so we don't list it again... | |
1092 | */ | |
1093 | ||
1094 | char scheme[32], /* URI scheme */ | |
7f500d89 MS |
1095 | userpass[32], /* Username:password */ |
1096 | serviceName[256], /* Service name (host field) */ | |
1097 | resource[256], /* Resource (options) */ | |
1098 | *regtype, /* Registration type */ | |
1099 | *replyDomain; /* Registration domain */ | |
f50db552 MS |
1100 | int port; /* Port number (not used) */ |
1101 | ||
1102 | if (httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), userpass, sizeof(userpass), serviceName, sizeof(serviceName), &port, resource, sizeof(resource)) >= HTTP_URI_STATUS_OK) | |
1103 | { | |
1104 | if ((regtype = strstr(serviceName, "._ipp")) != NULL) | |
1105 | { | |
1106 | *regtype++ = '\0'; | |
1107 | ||
1108 | if ((replyDomain = strstr(regtype, "._tcp.")) != NULL) | |
1109 | { | |
1110 | replyDomain[5] = '\0'; | |
1111 | replyDomain += 6; | |
1112 | ||
1113 | if ((device = cups_dnssd_get_device(&data, serviceName, regtype, replyDomain)) != NULL) | |
1114 | device->state = _CUPS_DNSSD_ACTIVE; | |
1115 | } | |
1116 | } | |
1117 | } | |
1118 | } | |
657c5b5f | 1119 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
f50db552 | 1120 | } |
657c5b5f | 1121 | |
f50db552 MS |
1122 | cupsFreeDests(num_dests, dests); |
1123 | ||
1124 | if (i > 0 || msec == 0) | |
1125 | goto enum_finished; | |
a2187a63 | 1126 | } |
dcb445bc | 1127 | |
f50db552 MS |
1128 | /* |
1129 | * Return early if the caller doesn't want to do discovery... | |
1130 | */ | |
1131 | ||
1132 | if ((mask & CUPS_PRINTER_DISCOVERED) && !(type & CUPS_PRINTER_DISCOVERED)) | |
1133 | goto enum_finished; | |
1134 | ||
a29fd7dd | 1135 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
dcb445bc MS |
1136 | /* |
1137 | * Get Bonjour-shared printers... | |
1138 | */ | |
1139 | ||
2a75f21b MS |
1140 | gettimeofday(&curtime, NULL); |
1141 | ||
a29fd7dd | 1142 | # ifdef HAVE_DNSSD |
dcb445bc MS |
1143 | if (DNSServiceCreateConnection(&data.main_ref) != kDNSServiceErr_NoError) |
1144 | return (0); | |
1145 | ||
1146 | main_fd = DNSServiceRefSockFD(data.main_ref); | |
1147 | ||
1148 | ipp_ref = data.main_ref; | |
1149 | DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0, | |
a29fd7dd | 1150 | "_ipp._tcp", NULL, |
12f89d24 | 1151 | (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data); |
dcb445bc MS |
1152 | |
1153 | local_ipp_ref = data.main_ref; | |
1154 | DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection, | |
1155 | kDNSServiceInterfaceIndexLocalOnly, | |
a29fd7dd | 1156 | "_ipp._tcp", NULL, |
12f89d24 | 1157 | (DNSServiceBrowseReply)cups_dnssd_local_cb, &data); |
dcb445bc | 1158 | |
a29fd7dd | 1159 | # ifdef HAVE_SSL |
dcb445bc MS |
1160 | ipps_ref = data.main_ref; |
1161 | DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0, | |
a29fd7dd | 1162 | "_ipps._tcp", NULL, |
12f89d24 | 1163 | (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data); |
dcb445bc MS |
1164 | |
1165 | local_ipps_ref = data.main_ref; | |
1166 | DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection, | |
1167 | kDNSServiceInterfaceIndexLocalOnly, | |
a29fd7dd | 1168 | "_ipps._tcp", NULL, |
12f89d24 | 1169 | (DNSServiceBrowseReply)cups_dnssd_local_cb, &data); |
a29fd7dd MS |
1170 | # endif /* HAVE_SSL */ |
1171 | ||
1172 | # else /* HAVE_AVAHI */ | |
1173 | if ((data.simple_poll = avahi_simple_poll_new()) == NULL) | |
1174 | { | |
1175 | DEBUG_puts("cupsEnumDests: Unable to create Avahi simple poll object."); | |
1176 | return (1); | |
1177 | } | |
1178 | ||
1179 | avahi_simple_poll_set_func(data.simple_poll, cups_dnssd_poll_cb, &data); | |
1180 | ||
1181 | data.client = avahi_client_new(avahi_simple_poll_get(data.simple_poll), | |
1182 | 0, cups_dnssd_client_cb, &data, | |
1183 | &error); | |
1184 | if (!data.client) | |
1185 | { | |
1186 | DEBUG_puts("cupsEnumDests: Unable to create Avahi client."); | |
1187 | avahi_simple_poll_free(data.simple_poll); | |
1188 | return (1); | |
1189 | } | |
1190 | ||
3fae3b33 MS |
1191 | data.browsers = 1; |
1192 | ipp_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL, 0, cups_dnssd_browse_cb, &data); | |
1193 | ||
a29fd7dd | 1194 | # ifdef HAVE_SSL |
657c5b5f | 1195 | data.browsers ++; |
3fae3b33 | 1196 | ipps_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipps._tcp", NULL, 0, cups_dnssd_browse_cb, &data); |
a29fd7dd MS |
1197 | # endif /* HAVE_SSL */ |
1198 | # endif /* HAVE_DNSSD */ | |
dcb445bc MS |
1199 | |
1200 | if (msec < 0) | |
1201 | remaining = INT_MAX; | |
1202 | else | |
1203 | remaining = msec; | |
1204 | ||
1205 | while (remaining > 0 && (!cancel || !*cancel)) | |
1206 | { | |
1207 | /* | |
1208 | * Check for input... | |
1209 | */ | |
1210 | ||
3fae3b33 MS |
1211 | DEBUG_printf(("1cupsEnumDests: remaining=%d", remaining)); |
1212 | ||
1213 | cups_elapsed(&curtime); | |
1214 | ||
a29fd7dd MS |
1215 | # ifdef HAVE_DNSSD |
1216 | # ifdef HAVE_POLL | |
dcb445bc MS |
1217 | pfd.fd = main_fd; |
1218 | pfd.events = POLLIN; | |
1219 | ||
3fae3b33 | 1220 | nfds = poll(&pfd, 1, remaining > _CUPS_DNSSD_MAXTIME ? _CUPS_DNSSD_MAXTIME : remaining); |
dcb445bc | 1221 | |
a29fd7dd | 1222 | # else |
dcb445bc MS |
1223 | FD_ZERO(&input); |
1224 | FD_SET(main_fd, &input); | |
1225 | ||
1226 | timeout.tv_sec = 0; | |
3fae3b33 | 1227 | timeout.tv_usec = 1000 * (remaining > _CUPS_DNSSD_MAXTIME ? _CUPS_DNSSD_MAXTIME : remaining); |
dcb445bc MS |
1228 | |
1229 | nfds = select(main_fd + 1, &input, NULL, NULL, &timeout); | |
a29fd7dd | 1230 | # endif /* HAVE_POLL */ |
dcb445bc MS |
1231 | |
1232 | if (nfds > 0) | |
1233 | DNSServiceProcessResult(data.main_ref); | |
dcb445bc | 1234 | |
a29fd7dd MS |
1235 | # else /* HAVE_AVAHI */ |
1236 | data.got_data = 0; | |
1237 | ||
3fae3b33 | 1238 | if ((error = avahi_simple_poll_iterate(data.simple_poll, _CUPS_DNSSD_MAXTIME)) > 0) |
a29fd7dd MS |
1239 | { |
1240 | /* | |
1241 | * We've been told to exit the loop. Perhaps the connection to | |
1242 | * Avahi failed. | |
1243 | */ | |
1244 | ||
1245 | break; | |
1246 | } | |
1247 | ||
657c5b5f | 1248 | DEBUG_printf(("1cupsEnumDests: got_data=%d", data.got_data)); |
a29fd7dd MS |
1249 | # endif /* HAVE_DNSSD */ |
1250 | ||
3fae3b33 | 1251 | remaining -= cups_elapsed(&curtime); |
a2187a63 | 1252 | |
dcb445bc | 1253 | for (device = (_cups_dnssd_device_t *)cupsArrayFirst(data.devices), |
a2187a63 | 1254 | count = 0, completed = 0; |
dcb445bc MS |
1255 | device; |
1256 | device = (_cups_dnssd_device_t *)cupsArrayNext(data.devices)) | |
1257 | { | |
1258 | if (device->ref) | |
1259 | count ++; | |
1260 | ||
a2187a63 MS |
1261 | if (device->state == _CUPS_DNSSD_ACTIVE) |
1262 | completed ++; | |
1263 | ||
dcb445bc MS |
1264 | if (!device->ref && device->state == _CUPS_DNSSD_NEW) |
1265 | { | |
dcb445bc MS |
1266 | DEBUG_printf(("1cupsEnumDests: Querying '%s'.", device->fullName)); |
1267 | ||
a29fd7dd MS |
1268 | # ifdef HAVE_DNSSD |
1269 | device->ref = data.main_ref; | |
1270 | ||
dcb445bc MS |
1271 | if (DNSServiceQueryRecord(&(device->ref), |
1272 | kDNSServiceFlagsShareConnection, | |
1273 | 0, device->fullName, | |
1274 | kDNSServiceType_TXT, | |
12f89d24 MS |
1275 | kDNSServiceClass_IN, |
1276 | (DNSServiceQueryRecordReply)cups_dnssd_query_cb, | |
dcb445bc MS |
1277 | &data) == kDNSServiceErr_NoError) |
1278 | { | |
1279 | count ++; | |
1280 | } | |
1281 | else | |
1282 | { | |
1283 | device->ref = 0; | |
1284 | device->state = _CUPS_DNSSD_ERROR; | |
1285 | ||
1286 | DEBUG_puts("1cupsEnumDests: Query failed."); | |
1287 | } | |
a29fd7dd MS |
1288 | |
1289 | # else /* HAVE_AVAHI */ | |
3fae3b33 | 1290 | if ((device->ref = avahi_record_browser_new(data.client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, device->fullName, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, 0, cups_dnssd_query_cb, &data)) != NULL) |
a29fd7dd | 1291 | { |
3fae3b33 | 1292 | DEBUG_printf(("1cupsEnumDests: Query ref=%p", device->ref)); |
a29fd7dd MS |
1293 | count ++; |
1294 | } | |
1295 | else | |
1296 | { | |
1297 | device->state = _CUPS_DNSSD_ERROR; | |
1298 | ||
657c5b5f | 1299 | DEBUG_printf(("1cupsEnumDests: Query failed: %s", avahi_strerror(avahi_client_errno(data.client)))); |
a29fd7dd MS |
1300 | } |
1301 | # endif /* HAVE_DNSSD */ | |
dcb445bc MS |
1302 | } |
1303 | else if (device->ref && device->state == _CUPS_DNSSD_PENDING) | |
1304 | { | |
a2187a63 MS |
1305 | completed ++; |
1306 | ||
3fae3b33 MS |
1307 | DEBUG_printf(("1cupsEnumDests: Query for \"%s\" is complete.", device->fullName)); |
1308 | ||
a29fd7dd MS |
1309 | if ((device->type & mask) == type) |
1310 | { | |
a2187a63 | 1311 | DEBUG_printf(("1cupsEnumDests: Add callback for \"%s\".", device->dest.name)); |
a29fd7dd MS |
1312 | if (!(*cb)(user_data, CUPS_DEST_FLAGS_NONE, &device->dest)) |
1313 | { | |
1314 | remaining = -1; | |
1315 | break; | |
1316 | } | |
1317 | } | |
dcb445bc MS |
1318 | |
1319 | device->state = _CUPS_DNSSD_ACTIVE; | |
1320 | } | |
1321 | } | |
a2187a63 | 1322 | |
657c5b5f | 1323 | # ifdef HAVE_AVAHI |
3fae3b33 | 1324 | DEBUG_printf(("1cupsEnumDests: remaining=%d, browsers=%d, completed=%d, count=%d, devices count=%d", remaining, data.browsers, completed, count, cupsArrayCount(data.devices))); |
657c5b5f MS |
1325 | |
1326 | if (data.browsers == 0 && completed == cupsArrayCount(data.devices)) | |
1327 | break; | |
1328 | # else | |
3fae3b33 | 1329 | DEBUG_printf(("1cupsEnumDests: remaining=%d, completed=%d, count=%d, devices count=%d", remaining, completed, count, cupsArrayCount(data.devices))); |
657c5b5f | 1330 | |
a2187a63 MS |
1331 | if (completed == cupsArrayCount(data.devices)) |
1332 | break; | |
657c5b5f | 1333 | # endif /* HAVE_AVAHI */ |
dcb445bc | 1334 | } |
7f500d89 MS |
1335 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
1336 | ||
1337 | /* | |
1338 | * Return... | |
1339 | */ | |
1340 | ||
1341 | enum_finished: | |
dcb445bc | 1342 | |
7f500d89 | 1343 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
de4966cb MS |
1344 | cupsArrayDelete(data.devices); |
1345 | ||
a29fd7dd | 1346 | # ifdef HAVE_DNSSD |
de4966cb MS |
1347 | if (ipp_ref) |
1348 | DNSServiceRefDeallocate(ipp_ref); | |
1349 | if (local_ipp_ref) | |
1350 | DNSServiceRefDeallocate(local_ipp_ref); | |
dcb445bc | 1351 | |
a29fd7dd | 1352 | # ifdef HAVE_SSL |
de4966cb MS |
1353 | if (ipps_ref) |
1354 | DNSServiceRefDeallocate(ipps_ref); | |
1355 | if (local_ipps_ref) | |
1356 | DNSServiceRefDeallocate(local_ipps_ref); | |
a29fd7dd | 1357 | # endif /* HAVE_SSL */ |
dcb445bc | 1358 | |
de4966cb MS |
1359 | if (data.main_ref) |
1360 | DNSServiceRefDeallocate(data.main_ref); | |
a29fd7dd MS |
1361 | |
1362 | # else /* HAVE_AVAHI */ | |
de4966cb MS |
1363 | if (ipp_ref) |
1364 | avahi_service_browser_free(ipp_ref); | |
a29fd7dd | 1365 | # ifdef HAVE_SSL |
de4966cb MS |
1366 | if (ipps_ref) |
1367 | avahi_service_browser_free(ipps_ref); | |
a29fd7dd MS |
1368 | # endif /* HAVE_SSL */ |
1369 | ||
de4966cb MS |
1370 | if (data.client) |
1371 | avahi_client_free(data.client); | |
1372 | if (data.simple_poll) | |
1373 | avahi_simple_poll_free(data.simple_poll); | |
a29fd7dd | 1374 | # endif /* HAVE_DNSSD */ |
f50db552 MS |
1375 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
1376 | ||
dcb445bc | 1377 | return (1); |
9c80ffa2 MS |
1378 | } |
1379 | ||
1380 | ||
1381 | # ifdef __BLOCKS__ | |
1382 | /* | |
1383 | * 'cupsEnumDestsBlock()' - Enumerate available destinations with a block. | |
1384 | * | |
f50db552 MS |
1385 | * Destinations are enumerated from one or more sources. The block receives the |
1386 | * @code user_data@ pointer and the destination pointer which can be used as | |
1387 | * input to the @link cupsCopyDest@ function. The block must return 1 to | |
dcb445bc | 1388 | * continue enumeration or 0 to stop. |
9c80ffa2 | 1389 | * |
f50db552 MS |
1390 | * The @code type@ and @code mask@ arguments allow the caller to filter the |
1391 | * destinations that are enumerated. Passing 0 for both will enumerate all | |
1392 | * printers. The constant @code CUPS_PRINTER_DISCOVERED@ is used to filter on | |
1393 | * destinations that are available but have not yet been added locally. | |
1394 | * | |
dcb445bc MS |
1395 | * Enumeration happens on the current thread and does not return until all |
1396 | * destinations have been enumerated or the block returns 0. | |
9c80ffa2 | 1397 | * |
500fca27 MS |
1398 | * Note: The block will likely receive multiple updates for the same |
1399 | * destinations - it is up to the caller to suppress any duplicate destinations. | |
1400 | * | |
53af7f21 | 1401 | * @since CUPS 1.6/macOS 10.8@ @exclude all@ |
9c80ffa2 MS |
1402 | */ |
1403 | ||
1404 | int /* O - 1 on success, 0 on failure */ | |
1405 | cupsEnumDestsBlock( | |
dcb445bc MS |
1406 | unsigned flags, /* I - Enumeration flags */ |
1407 | int timeout, /* I - Timeout in milliseconds, 0 for indefinite */ | |
1408 | int *cancel, /* I - Pointer to "cancel" variable */ | |
1409 | cups_ptype_t type, /* I - Printer type bits */ | |
1410 | cups_ptype_t mask, /* I - Mask for printer type bits */ | |
9c80ffa2 MS |
1411 | cups_dest_block_t block) /* I - Block */ |
1412 | { | |
dcb445bc MS |
1413 | return (cupsEnumDests(flags, timeout, cancel, type, mask, |
1414 | (cups_dest_cb_t)cups_block_cb, (void *)block)); | |
9c80ffa2 MS |
1415 | } |
1416 | # endif /* __BLOCKS__ */ | |
1417 | ||
1418 | ||
ef416fc2 | 1419 | /* |
1420 | * 'cupsFreeDests()' - Free the memory used by the list of destinations. | |
1421 | */ | |
1422 | ||
1423 | void | |
1424 | cupsFreeDests(int num_dests, /* I - Number of destinations */ | |
1425 | cups_dest_t *dests) /* I - Destinations */ | |
1426 | { | |
1427 | int i; /* Looping var */ | |
1428 | cups_dest_t *dest; /* Current destination */ | |
1429 | ||
1430 | ||
1431 | if (num_dests == 0 || dests == NULL) | |
1432 | return; | |
1433 | ||
1434 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
1435 | { | |
2e4ff8af MS |
1436 | _cupsStrFree(dest->name); |
1437 | _cupsStrFree(dest->instance); | |
ef416fc2 | 1438 | |
1439 | cupsFreeOptions(dest->num_options, dest->options); | |
1440 | } | |
1441 | ||
1442 | free(dests); | |
1443 | } | |
1444 | ||
1445 | ||
1446 | /* | |
1447 | * 'cupsGetDest()' - Get the named destination from the list. | |
1448 | * | |
98d88c8d | 1449 | * Use the @link cupsEnumDests@ or @link cupsGetDests2@ functions to get a |
ef416fc2 | 1450 | * list of supported destinations for the current user. |
1451 | */ | |
1452 | ||
5a738aea MS |
1453 | cups_dest_t * /* O - Destination pointer or @code NULL@ */ |
1454 | cupsGetDest(const char *name, /* I - Destination name or @code NULL@ for the default destination */ | |
1455 | const char *instance, /* I - Instance name or @code NULL@ */ | |
ef416fc2 | 1456 | int num_dests, /* I - Number of destinations */ |
1457 | cups_dest_t *dests) /* I - Destinations */ | |
1458 | { | |
426c6a59 MS |
1459 | int diff, /* Result of comparison */ |
1460 | match; /* Matching index */ | |
ef416fc2 | 1461 | |
1462 | ||
2abf387c | 1463 | if (num_dests <= 0 || !dests) |
ef416fc2 | 1464 | return (NULL); |
1465 | ||
2abf387c | 1466 | if (!name) |
ef416fc2 | 1467 | { |
1468 | /* | |
1469 | * NULL name for default printer. | |
1470 | */ | |
1471 | ||
1472 | while (num_dests > 0) | |
1473 | { | |
1474 | if (dests->is_default) | |
1475 | return (dests); | |
1476 | ||
1477 | num_dests --; | |
1478 | dests ++; | |
1479 | } | |
1480 | } | |
1481 | else | |
1482 | { | |
1483 | /* | |
1484 | * Lookup name and optionally the instance... | |
1485 | */ | |
1486 | ||
426c6a59 | 1487 | match = cups_find_dest(name, instance, num_dests, dests, -1, &diff); |
ef416fc2 | 1488 | |
426c6a59 MS |
1489 | if (!diff) |
1490 | return (dests + match); | |
ef416fc2 | 1491 | } |
1492 | ||
1493 | return (NULL); | |
1494 | } | |
1495 | ||
1496 | ||
dcb445bc MS |
1497 | /* |
1498 | * '_cupsGetDestResource()' - Get the resource path and URI for a destination. | |
1499 | */ | |
1500 | ||
1501 | const char * /* O - Printer URI */ | |
1502 | _cupsGetDestResource( | |
1503 | cups_dest_t *dest, /* I - Destination */ | |
1504 | char *resource, /* I - Resource buffer */ | |
1505 | size_t resourcesize) /* I - Size of resource buffer */ | |
1506 | { | |
1507 | const char *uri; /* Printer URI */ | |
1508 | char scheme[32], /* URI scheme */ | |
1509 | userpass[256], /* Username and password (unused) */ | |
1510 | hostname[256]; /* Hostname */ | |
1511 | int port; /* Port number */ | |
1512 | ||
1513 | ||
2a75f21b | 1514 | DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", (void *)dest, dest->name, (void *)resource, (int)resourcesize)); |
7536de1a | 1515 | |
dcb445bc MS |
1516 | /* |
1517 | * Range check input... | |
1518 | */ | |
1519 | ||
1520 | if (!dest || !resource || resourcesize < 1) | |
1521 | { | |
1522 | if (resource) | |
1523 | *resource = '\0'; | |
1524 | ||
cb7f98ee | 1525 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
dcb445bc MS |
1526 | return (NULL); |
1527 | } | |
1528 | ||
1529 | /* | |
1530 | * Grab the printer URI... | |
1531 | */ | |
1532 | ||
4a366251 | 1533 | if ((uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options)) == NULL) |
dcb445bc | 1534 | { |
c9a0ff62 | 1535 | if ((uri = cupsGetOption("device-uri", dest->num_options, dest->options)) != NULL) |
4a366251 | 1536 | { |
4a366251 | 1537 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
c9a0ff62 MS |
1538 | if (strstr(uri, "._tcp")) |
1539 | uri = cups_dnssd_resolve(dest, uri, 5000, NULL, NULL, NULL); | |
4a366251 | 1540 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
4a366251 | 1541 | } |
7536de1a | 1542 | |
4a366251 MS |
1543 | if (uri) |
1544 | { | |
1545 | DEBUG_printf(("1_cupsGetDestResource: Resolved printer-uri-supported=\"%s\"", uri)); | |
dcb445bc | 1546 | |
4a366251 MS |
1547 | uri = _cupsCreateDest(dest->name, cupsGetOption("printer-info", dest->num_options, dest->options), NULL, uri, resource, resourcesize); |
1548 | } | |
dcb445bc | 1549 | |
4a366251 MS |
1550 | if (uri) |
1551 | { | |
1552 | DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri)); | |
dcb445bc | 1553 | |
4a366251 MS |
1554 | dest->num_options = cupsAddOption("printer-uri-supported", uri, dest->num_options, &dest->options); |
1555 | ||
1556 | uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options); | |
1557 | } | |
1558 | else | |
1559 | { | |
1560 | DEBUG_puts("1_cupsGetDestResource: No printer-uri-supported found."); | |
1561 | ||
1562 | if (resource) | |
1563 | *resource = '\0'; | |
1564 | ||
1565 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0); | |
7536de1a | 1566 | |
dcb445bc | 1567 | return (NULL); |
4a366251 | 1568 | } |
dcb445bc | 1569 | } |
4a366251 | 1570 | else |
dcb445bc | 1571 | { |
4a366251 | 1572 | DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri)); |
dcb445bc | 1573 | |
4a366251 MS |
1574 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), |
1575 | userpass, sizeof(userpass), hostname, sizeof(hostname), | |
1576 | &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK) | |
1577 | { | |
1578 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1); | |
1579 | ||
1580 | return (NULL); | |
1581 | } | |
dcb445bc MS |
1582 | } |
1583 | ||
7536de1a MS |
1584 | DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource)); |
1585 | ||
dcb445bc MS |
1586 | return (uri); |
1587 | } | |
1588 | ||
1589 | ||
104fd4ae MS |
1590 | /* |
1591 | * 'cupsGetDestWithURI()' - Get a destination associated with a URI. | |
1592 | * | |
1593 | * "name" is the desired name for the printer. If @code NULL@, a name will be | |
1594 | * created using the URI. | |
1595 | * | |
1596 | * "uri" is the "ipp" or "ipps" URI for the printer. | |
1597 | * | |
8072030b | 1598 | * @since CUPS 2.0/macOS 10.10@ |
104fd4ae MS |
1599 | */ |
1600 | ||
1601 | cups_dest_t * /* O - Destination or @code NULL@ */ | |
1602 | cupsGetDestWithURI(const char *name, /* I - Desired printer name or @code NULL@ */ | |
1603 | const char *uri) /* I - URI for the printer */ | |
1604 | { | |
1605 | cups_dest_t *dest; /* New destination */ | |
1606 | char temp[1024], /* Temporary string */ | |
1607 | scheme[256], /* Scheme from URI */ | |
1608 | userpass[256], /* Username:password from URI */ | |
1609 | hostname[256], /* Hostname from URI */ | |
1610 | resource[1024], /* Resource path from URI */ | |
1611 | *ptr; /* Pointer into string */ | |
efa72f61 | 1612 | const char *info; /* printer-info string */ |
104fd4ae MS |
1613 | int port; /* Port number from URI */ |
1614 | ||
1615 | ||
1616 | /* | |
1617 | * Range check input... | |
1618 | */ | |
1619 | ||
1620 | if (!uri) | |
1621 | { | |
1622 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); | |
1623 | return (NULL); | |
1624 | } | |
1625 | ||
1626 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK || | |
1627 | (strncmp(uri, "ipp://", 6) && strncmp(uri, "ipps://", 7))) | |
1628 | { | |
f5f2e19e | 1629 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad printer-uri."), 1); |
104fd4ae MS |
1630 | |
1631 | return (NULL); | |
1632 | } | |
1633 | ||
efa72f61 MS |
1634 | if (name) |
1635 | { | |
1636 | info = name; | |
1637 | } | |
1638 | else | |
104fd4ae MS |
1639 | { |
1640 | /* | |
1641 | * Create the name from the URI... | |
1642 | */ | |
1643 | ||
1644 | if (strstr(hostname, "._tcp")) | |
1645 | { | |
1646 | /* | |
1647 | * Use the service instance name... | |
1648 | */ | |
1649 | ||
efa72f61 | 1650 | if ((ptr = strstr(hostname, "._")) != NULL) |
104fd4ae MS |
1651 | *ptr = '\0'; |
1652 | ||
efa72f61 MS |
1653 | cups_queue_name(temp, hostname, sizeof(temp)); |
1654 | name = temp; | |
1655 | info = hostname; | |
104fd4ae MS |
1656 | } |
1657 | else if (!strncmp(resource, "/classes/", 9)) | |
1658 | { | |
1659 | snprintf(temp, sizeof(temp), "%s @ %s", resource + 9, hostname); | |
efa72f61 MS |
1660 | name = resource + 9; |
1661 | info = temp; | |
104fd4ae MS |
1662 | } |
1663 | else if (!strncmp(resource, "/printers/", 10)) | |
1664 | { | |
1665 | snprintf(temp, sizeof(temp), "%s @ %s", resource + 10, hostname); | |
efa72f61 MS |
1666 | name = resource + 10; |
1667 | info = temp; | |
104fd4ae MS |
1668 | } |
1669 | else | |
1670 | { | |
1671 | name = hostname; | |
efa72f61 | 1672 | info = hostname; |
104fd4ae MS |
1673 | } |
1674 | } | |
1675 | ||
1676 | /* | |
1677 | * Create the destination... | |
1678 | */ | |
1679 | ||
1680 | if ((dest = calloc(1, sizeof(cups_dest_t))) == NULL) | |
1681 | { | |
1682 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); | |
1683 | return (NULL); | |
1684 | } | |
1685 | ||
1686 | dest->name = _cupsStrAlloc(name); | |
4db7fcee | 1687 | dest->num_options = cupsAddOption("device-uri", uri, dest->num_options, &(dest->options)); |
efa72f61 | 1688 | dest->num_options = cupsAddOption("printer-info", info, dest->num_options, &(dest->options)); |
104fd4ae MS |
1689 | |
1690 | return (dest); | |
1691 | } | |
1692 | ||
1693 | ||
c8fef167 MS |
1694 | /* |
1695 | * '_cupsGetDests()' - Get destinations from a server. | |
1696 | * | |
5489ad43 MS |
1697 | * "op" is IPP_OP_CUPS_GET_PRINTERS to get a full list, IPP_OP_CUPS_GET_DEFAULT |
1698 | * to get the system-wide default printer, or IPP_OP_GET_PRINTER_ATTRIBUTES for | |
1699 | * a known printer. | |
c8fef167 MS |
1700 | * |
1701 | * "name" is the name of an existing printer and is only used when "op" is | |
cb7f98ee | 1702 | * IPP_OP_GET_PRINTER_ATTRIBUTES. |
c8fef167 MS |
1703 | * |
1704 | * "dest" is initialized to point to the array of destinations. | |
1705 | * | |
1706 | * 0 is returned if there are no printers, no default printer, or the named | |
1707 | * printer does not exist, respectively. | |
1708 | * | |
1709 | * Free the memory used by the destination array using the @link cupsFreeDests@ | |
1710 | * function. | |
1711 | * | |
8072030b | 1712 | * Note: On macOS this function also gets the default paper from the system |
c8fef167 MS |
1713 | * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the |
1714 | * options array for each destination that supports it. | |
1715 | */ | |
1716 | ||
dcb445bc MS |
1717 | int /* O - Number of destinations */ |
1718 | _cupsGetDests(http_t *http, /* I - Connection to server or | |
1719 | * @code CUPS_HTTP_DEFAULT@ */ | |
1720 | ipp_op_t op, /* I - IPP operation */ | |
1721 | const char *name, /* I - Name of destination */ | |
1722 | cups_dest_t **dests, /* IO - Destinations */ | |
1723 | cups_ptype_t type, /* I - Printer type bits */ | |
1724 | cups_ptype_t mask) /* I - Printer type mask */ | |
c8fef167 MS |
1725 | { |
1726 | int num_dests = 0; /* Number of destinations */ | |
1727 | cups_dest_t *dest; /* Current destination */ | |
1728 | ipp_t *request, /* IPP Request */ | |
1729 | *response; /* IPP Response */ | |
1730 | ipp_attribute_t *attr; /* Current attribute */ | |
1731 | const char *printer_name; /* printer-name attribute */ | |
1732 | char uri[1024]; /* printer-uri value */ | |
1733 | int num_options; /* Number of options */ | |
1734 | cups_option_t *options; /* Options */ | |
1735 | #ifdef __APPLE__ | |
1736 | char media_default[41]; /* Default paper size */ | |
1737 | #endif /* __APPLE__ */ | |
1738 | char optname[1024], /* Option name */ | |
1739 | value[2048], /* Option value */ | |
1740 | *ptr; /* Pointer into name/value */ | |
1741 | static const char * const pattrs[] = /* Attributes we're interested in */ | |
1742 | { | |
1743 | "auth-info-required", | |
1744 | "device-uri", | |
1745 | "job-sheets-default", | |
1746 | "marker-change-time", | |
1747 | "marker-colors", | |
1748 | "marker-high-levels", | |
1749 | "marker-levels", | |
1750 | "marker-low-levels", | |
1751 | "marker-message", | |
1752 | "marker-names", | |
1753 | "marker-types", | |
1754 | #ifdef __APPLE__ | |
1755 | "media-supported", | |
1756 | #endif /* __APPLE__ */ | |
1757 | "printer-commands", | |
1758 | "printer-defaults", | |
1759 | "printer-info", | |
1760 | "printer-is-accepting-jobs", | |
1761 | "printer-is-shared", | |
12b798a0 | 1762 | "printer-is-temporary", |
c8fef167 MS |
1763 | "printer-location", |
1764 | "printer-make-and-model", | |
5a9febac | 1765 | "printer-mandatory-job-attributes", |
c8fef167 MS |
1766 | "printer-name", |
1767 | "printer-state", | |
1768 | "printer-state-change-time", | |
1769 | "printer-state-reasons", | |
1770 | "printer-type", | |
1771 | "printer-uri-supported" | |
1772 | }; | |
1773 | ||
1774 | ||
2a75f21b | 1775 | DEBUG_printf(("_cupsGetDests(http=%p, op=%x(%s), name=\"%s\", dests=%p, type=%x, mask=%x)", (void *)http, op, ippOpString(op), name, (void *)dests, type, mask)); |
4a366251 | 1776 | |
c8fef167 MS |
1777 | #ifdef __APPLE__ |
1778 | /* | |
1779 | * Get the default paper size... | |
1780 | */ | |
1781 | ||
1782 | appleGetPaperSize(media_default, sizeof(media_default)); | |
1783 | #endif /* __APPLE__ */ | |
1784 | ||
0cb67df3 | 1785 | /* |
cb7f98ee | 1786 | * Build a IPP_OP_CUPS_GET_PRINTERS or IPP_OP_GET_PRINTER_ATTRIBUTES request, which |
0cb67df3 MS |
1787 | * require the following attributes: |
1788 | * | |
1789 | * attributes-charset | |
1790 | * attributes-natural-language | |
1791 | * requesting-user-name | |
cb7f98ee | 1792 | * printer-uri [for IPP_OP_GET_PRINTER_ATTRIBUTES] |
0cb67df3 | 1793 | */ |
c1420c87 | 1794 | |
0cb67df3 | 1795 | request = ippNewRequest(op); |
c1420c87 | 1796 | |
0cb67df3 MS |
1797 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
1798 | "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), | |
1799 | NULL, pattrs); | |
c1420c87 | 1800 | |
0cb67df3 MS |
1801 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
1802 | "requesting-user-name", NULL, cupsUser()); | |
c1420c87 | 1803 | |
cb7f98ee | 1804 | if (name && op != IPP_OP_CUPS_GET_DEFAULT) |
0cb67df3 MS |
1805 | { |
1806 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
1807 | "localhost", ippPort(), "/printers/%s", name); | |
1808 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
1809 | uri); | |
1810 | } | |
1811 | else if (mask) | |
1812 | { | |
7e86f2f6 MS |
1813 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type", (int)type); |
1814 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, "printer-type-mask", (int)mask); | |
c1420c87 | 1815 | } |
c8fef167 | 1816 | |
0cb67df3 MS |
1817 | /* |
1818 | * Do the request and get back a response... | |
1819 | */ | |
1820 | ||
1821 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
c8fef167 MS |
1822 | { |
1823 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
1824 | { | |
1825 | /* | |
1826 | * Skip leading attributes until we hit a printer... | |
1827 | */ | |
1828 | ||
1829 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
1830 | attr = attr->next; | |
1831 | ||
1832 | if (attr == NULL) | |
1833 | break; | |
1834 | ||
1835 | /* | |
1836 | * Pull the needed attributes from this printer... | |
1837 | */ | |
1838 | ||
1839 | printer_name = NULL; | |
1840 | num_options = 0; | |
1841 | options = NULL; | |
1842 | ||
1843 | for (; attr && attr->group_tag == IPP_TAG_PRINTER; attr = attr->next) | |
1844 | { | |
1845 | if (attr->value_tag != IPP_TAG_INTEGER && | |
1846 | attr->value_tag != IPP_TAG_ENUM && | |
1847 | attr->value_tag != IPP_TAG_BOOLEAN && | |
1848 | attr->value_tag != IPP_TAG_TEXT && | |
1849 | attr->value_tag != IPP_TAG_TEXTLANG && | |
1850 | attr->value_tag != IPP_TAG_NAME && | |
1851 | attr->value_tag != IPP_TAG_NAMELANG && | |
1852 | attr->value_tag != IPP_TAG_KEYWORD && | |
1853 | attr->value_tag != IPP_TAG_RANGE && | |
1854 | attr->value_tag != IPP_TAG_URI) | |
1855 | continue; | |
1856 | ||
1857 | if (!strcmp(attr->name, "auth-info-required") || | |
1858 | !strcmp(attr->name, "device-uri") || | |
1859 | !strcmp(attr->name, "marker-change-time") || | |
1860 | !strcmp(attr->name, "marker-colors") || | |
1861 | !strcmp(attr->name, "marker-high-levels") || | |
1862 | !strcmp(attr->name, "marker-levels") || | |
1863 | !strcmp(attr->name, "marker-low-levels") || | |
1864 | !strcmp(attr->name, "marker-message") || | |
1865 | !strcmp(attr->name, "marker-names") || | |
1866 | !strcmp(attr->name, "marker-types") || | |
1867 | !strcmp(attr->name, "printer-commands") || | |
1868 | !strcmp(attr->name, "printer-info") || | |
12b798a0 MS |
1869 | !strcmp(attr->name, "printer-is-shared") || |
1870 | !strcmp(attr->name, "printer-is-temporary") || | |
c8fef167 | 1871 | !strcmp(attr->name, "printer-make-and-model") || |
5a9febac | 1872 | !strcmp(attr->name, "printer-mandatory-job-attributes") || |
c8fef167 MS |
1873 | !strcmp(attr->name, "printer-state") || |
1874 | !strcmp(attr->name, "printer-state-change-time") || | |
1875 | !strcmp(attr->name, "printer-type") || | |
1876 | !strcmp(attr->name, "printer-is-accepting-jobs") || | |
1877 | !strcmp(attr->name, "printer-location") || | |
1878 | !strcmp(attr->name, "printer-state-reasons") || | |
1879 | !strcmp(attr->name, "printer-uri-supported")) | |
1880 | { | |
1881 | /* | |
1882 | * Add a printer description attribute... | |
1883 | */ | |
1884 | ||
1885 | num_options = cupsAddOption(attr->name, | |
1886 | cups_make_string(attr, value, | |
1887 | sizeof(value)), | |
1888 | num_options, &options); | |
1889 | } | |
1890 | #ifdef __APPLE__ | |
1891 | else if (!strcmp(attr->name, "media-supported")) | |
1892 | { | |
1893 | /* | |
1894 | * See if we can set a default media size... | |
1895 | */ | |
1896 | ||
1897 | int i; /* Looping var */ | |
1898 | ||
1899 | for (i = 0; i < attr->num_values; i ++) | |
88f9aafc | 1900 | if (!_cups_strcasecmp(media_default, attr->values[i].string.text)) |
c8fef167 MS |
1901 | { |
1902 | num_options = cupsAddOption("media", media_default, num_options, | |
1903 | &options); | |
1904 | break; | |
1905 | } | |
1906 | } | |
1907 | #endif /* __APPLE__ */ | |
1908 | else if (!strcmp(attr->name, "printer-name") && | |
1909 | attr->value_tag == IPP_TAG_NAME) | |
1910 | printer_name = attr->values[0].string.text; | |
1911 | else if (strncmp(attr->name, "notify-", 7) && | |
1912 | (attr->value_tag == IPP_TAG_BOOLEAN || | |
1913 | attr->value_tag == IPP_TAG_ENUM || | |
1914 | attr->value_tag == IPP_TAG_INTEGER || | |
1915 | attr->value_tag == IPP_TAG_KEYWORD || | |
1916 | attr->value_tag == IPP_TAG_NAME || | |
1917 | attr->value_tag == IPP_TAG_RANGE) && | |
1918 | (ptr = strstr(attr->name, "-default")) != NULL) | |
1919 | { | |
1920 | /* | |
1921 | * Add a default option... | |
1922 | */ | |
1923 | ||
1924 | strlcpy(optname, attr->name, sizeof(optname)); | |
1925 | optname[ptr - attr->name] = '\0'; | |
1926 | ||
88f9aafc | 1927 | if (_cups_strcasecmp(optname, "media") || |
c8fef167 MS |
1928 | !cupsGetOption("media", num_options, options)) |
1929 | num_options = cupsAddOption(optname, | |
1930 | cups_make_string(attr, value, | |
1931 | sizeof(value)), | |
1932 | num_options, &options); | |
1933 | } | |
1934 | } | |
1935 | ||
1936 | /* | |
1937 | * See if we have everything needed... | |
1938 | */ | |
1939 | ||
1940 | if (!printer_name) | |
1941 | { | |
1942 | cupsFreeOptions(num_options, options); | |
1943 | ||
1944 | if (attr == NULL) | |
1945 | break; | |
1946 | else | |
1947 | continue; | |
1948 | } | |
1949 | ||
1950 | if ((dest = cups_add_dest(printer_name, NULL, &num_dests, dests)) != NULL) | |
1951 | { | |
1952 | dest->num_options = num_options; | |
1953 | dest->options = options; | |
1954 | } | |
1955 | else | |
1956 | cupsFreeOptions(num_options, options); | |
1957 | ||
1958 | if (attr == NULL) | |
1959 | break; | |
1960 | } | |
1961 | ||
1962 | ippDelete(response); | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Return the count... | |
1967 | */ | |
1968 | ||
1969 | return (num_dests); | |
1970 | } | |
1971 | ||
1972 | ||
ef416fc2 | 1973 | /* |
1974 | * 'cupsGetDests()' - Get the list of destinations from the default server. | |
ecdc0628 | 1975 | * |
1976 | * Starting with CUPS 1.2, the returned list of destinations include the | |
98d88c8d MS |
1977 | * "printer-info", "printer-is-accepting-jobs", "printer-is-shared", |
1978 | * "printer-make-and-model", "printer-state", "printer-state-change-time", | |
1979 | * "printer-state-reasons", "printer-type", and "printer-uri-supported" | |
1980 | * attributes as options. | |
1981 | * | |
1982 | * CUPS 1.4 adds the "marker-change-time", "marker-colors", | |
1983 | * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message", | |
1984 | * "marker-names", "marker-types", and "printer-commands" attributes as options. | |
1985 | * | |
1986 | * CUPS 2.2 adds accessible IPP printers to the list of destinations that can | |
1987 | * be used. The "printer-uri-supported" option will be present for those IPP | |
1988 | * printers that have been recently used. | |
2abf387c | 1989 | * |
5a738aea MS |
1990 | * Use the @link cupsFreeDests@ function to free the destination list and |
1991 | * the @link cupsGetDest@ function to find a particular destination. | |
53af7f21 MS |
1992 | * |
1993 | * @exclude all@ | |
ef416fc2 | 1994 | */ |
1995 | ||
1996 | int /* O - Number of destinations */ | |
1997 | cupsGetDests(cups_dest_t **dests) /* O - Destinations */ | |
1998 | { | |
3d052e43 | 1999 | return (cupsGetDests2(CUPS_HTTP_DEFAULT, dests)); |
ef416fc2 | 2000 | } |
2001 | ||
2002 | ||
2003 | /* | |
2004 | * 'cupsGetDests2()' - Get the list of destinations from the specified server. | |
2005 | * | |
ecdc0628 | 2006 | * Starting with CUPS 1.2, the returned list of destinations include the |
98d88c8d MS |
2007 | * "printer-info", "printer-is-accepting-jobs", "printer-is-shared", |
2008 | * "printer-make-and-model", "printer-state", "printer-state-change-time", | |
2009 | * "printer-state-reasons", "printer-type", and "printer-uri-supported" | |
2010 | * attributes as options. | |
2011 | * | |
2012 | * CUPS 1.4 adds the "marker-change-time", "marker-colors", | |
2013 | * "marker-high-levels", "marker-levels", "marker-low-levels", "marker-message", | |
2014 | * "marker-names", "marker-types", and "printer-commands" attributes as options. | |
2015 | * | |
2016 | * CUPS 2.2 adds accessible IPP printers to the list of destinations that can | |
2017 | * be used. The "printer-uri-supported" option will be present for those IPP | |
2018 | * printers that have been recently used. | |
ecdc0628 | 2019 | * |
5a738aea MS |
2020 | * Use the @link cupsFreeDests@ function to free the destination list and |
2021 | * the @link cupsGetDest@ function to find a particular destination. | |
2abf387c | 2022 | * |
98d88c8d | 2023 | * @since CUPS 1.1.21/macOS 10.4@ |
ef416fc2 | 2024 | */ |
2025 | ||
2026 | int /* O - Number of destinations */ | |
568fa3fa | 2027 | cupsGetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 2028 | cups_dest_t **dests) /* O - Destinations */ |
2029 | { | |
7536de1a MS |
2030 | _cups_getdata_t data; /* Enumeration data */ |
2031 | cups_dest_t *dest; /* Current destination */ | |
ef416fc2 | 2032 | const char *home; /* HOME environment variable */ |
b423cd4c | 2033 | char filename[1024]; /* Local ~/.cups/lpoptions file */ |
ef416fc2 | 2034 | const char *defprinter; /* Default printer */ |
2035 | char name[1024], /* Copy of printer name */ | |
38e73f87 MS |
2036 | *instance, /* Pointer to instance name */ |
2037 | *user_default; /* User default printer */ | |
ef416fc2 | 2038 | int num_reals; /* Number of real queues */ |
2039 | cups_dest_t *reals; /* Real queues */ | |
3d052e43 | 2040 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
ef416fc2 | 2041 | |
2042 | ||
2043 | /* | |
2044 | * Range check the input... | |
2045 | */ | |
2046 | ||
3d052e43 | 2047 | if (!dests) |
1ff0402e | 2048 | { |
cb7f98ee | 2049 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad NULL dests pointer"), 1); |
ef416fc2 | 2050 | return (0); |
c8fef167 | 2051 | } |
ef416fc2 | 2052 | |
2053 | /* | |
2054 | * Grab the printers and classes... | |
2055 | */ | |
2056 | ||
7536de1a MS |
2057 | data.num_dests = 0; |
2058 | data.dests = NULL; | |
2059 | ||
f50db552 | 2060 | cupsEnumDests(0, 1000, NULL, 0, 0, (cups_dest_cb_t)cups_get_cb, &data); |
b19ccc9e | 2061 | |
cb7f98ee | 2062 | if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE) |
b19ccc9e | 2063 | { |
7536de1a MS |
2064 | cupsFreeDests(data.num_dests, data.dests); |
2065 | ||
b19ccc9e | 2066 | *dests = (cups_dest_t *)0; |
7536de1a | 2067 | |
b19ccc9e MS |
2068 | return (0); |
2069 | } | |
ef416fc2 | 2070 | |
2071 | /* | |
2072 | * Make a copy of the "real" queues for a later sanity check... | |
2073 | */ | |
2074 | ||
7536de1a | 2075 | if (data.num_dests > 0) |
ef416fc2 | 2076 | { |
7536de1a | 2077 | num_reals = data.num_dests; |
7e86f2f6 | 2078 | reals = calloc((size_t)num_reals, sizeof(cups_dest_t)); |
ef416fc2 | 2079 | |
2080 | if (reals) | |
7536de1a | 2081 | memcpy(reals, data.dests, (size_t)num_reals * sizeof(cups_dest_t)); |
ef416fc2 | 2082 | else |
2083 | num_reals = 0; | |
2084 | } | |
2085 | else | |
2086 | { | |
2087 | num_reals = 0; | |
2088 | reals = NULL; | |
2089 | } | |
2090 | ||
2091 | /* | |
2092 | * Grab the default destination... | |
2093 | */ | |
2094 | ||
38e73f87 MS |
2095 | if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL) |
2096 | defprinter = name; | |
2097 | else if ((defprinter = cupsGetDefault2(http)) != NULL) | |
2098 | { | |
2099 | strlcpy(name, defprinter, sizeof(name)); | |
2100 | defprinter = name; | |
2101 | } | |
080811b1 MS |
2102 | |
2103 | if (defprinter) | |
ef416fc2 | 2104 | { |
2105 | /* | |
38e73f87 | 2106 | * Separate printer and instance name... |
ef416fc2 | 2107 | */ |
2108 | ||
ef416fc2 | 2109 | if ((instance = strchr(name, '/')) != NULL) |
2110 | *instance++ = '\0'; | |
2111 | ||
2112 | /* | |
2113 | * Lookup the printer and instance and make it the default... | |
2114 | */ | |
2115 | ||
7536de1a | 2116 | if ((dest = cupsGetDest(name, instance, data.num_dests, data.dests)) != NULL) |
ef416fc2 | 2117 | dest->is_default = 1; |
2118 | } | |
2119 | else | |
ef416fc2 | 2120 | instance = NULL; |
ef416fc2 | 2121 | |
2122 | /* | |
b423cd4c | 2123 | * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files... |
ef416fc2 | 2124 | */ |
2125 | ||
2126 | snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); | |
7536de1a | 2127 | data.num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL, data.num_dests, &data.dests); |
ef416fc2 | 2128 | |
2129 | if ((home = getenv("HOME")) != NULL) | |
2130 | { | |
b423cd4c | 2131 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); |
b423cd4c | 2132 | |
7536de1a | 2133 | data.num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL, data.num_dests, &data.dests); |
ef416fc2 | 2134 | } |
2135 | ||
2136 | /* | |
2137 | * Validate the current default destination - this prevents old | |
b423cd4c | 2138 | * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from |
ef416fc2 | 2139 | * pointing to a non-existent printer or class... |
2140 | */ | |
2141 | ||
2142 | if (num_reals) | |
2143 | { | |
2144 | /* | |
2145 | * See if we have a default printer... | |
2146 | */ | |
2147 | ||
7536de1a | 2148 | if ((dest = cupsGetDest(NULL, NULL, data.num_dests, data.dests)) != NULL) |
ef416fc2 | 2149 | { |
2150 | /* | |
2151 | * Have a default; see if it is real... | |
2152 | */ | |
2153 | ||
5489ad43 MS |
2154 | if (!cupsGetDest(dest->name, NULL, num_reals, reals)) |
2155 | { | |
2156 | /* | |
2157 | * Remove the non-real printer from the list, since we don't want jobs | |
2158 | * going to an unexpected printer... (<rdar://problem/14216472>) | |
2159 | */ | |
ef416fc2 | 2160 | |
7536de1a | 2161 | data.num_dests = cupsRemoveDest(dest->name, dest->instance, data.num_dests, &data.dests); |
5489ad43 | 2162 | } |
ef416fc2 | 2163 | } |
2164 | ||
2165 | /* | |
2166 | * Free memory... | |
2167 | */ | |
2168 | ||
2169 | free(reals); | |
2170 | } | |
2171 | ||
2172 | /* | |
2173 | * Return the number of destinations... | |
2174 | */ | |
2175 | ||
7536de1a MS |
2176 | *dests = data.dests; |
2177 | ||
2178 | if (data.num_dests > 0) | |
cb7f98ee | 2179 | _cupsSetError(IPP_STATUS_OK, NULL, 0); |
1ff0402e | 2180 | |
7536de1a | 2181 | return (data.num_dests); |
ef416fc2 | 2182 | } |
2183 | ||
2184 | ||
a4924f6c MS |
2185 | /* |
2186 | * 'cupsGetNamedDest()' - Get options for the named destination. | |
2187 | * | |
2188 | * This function is optimized for retrieving a single destination and should | |
98d88c8d MS |
2189 | * be used instead of @link cupsGetDests2@ and @link cupsGetDest@ when you |
2190 | * either know the name of the destination or want to print to the default | |
2191 | * destination. If @code NULL@ is returned, the destination does not exist or | |
2192 | * there is no default destination. | |
a4924f6c | 2193 | * |
5a738aea MS |
2194 | * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print |
2195 | * server will be used. | |
a4924f6c | 2196 | * |
5a738aea MS |
2197 | * If "name" is @code NULL@, the default printer for the current user will be |
2198 | * returned. | |
a4924f6c | 2199 | * |
5a738aea MS |
2200 | * The returned destination must be freed using @link cupsFreeDests@ with a |
2201 | * "num_dests" value of 1. | |
a4924f6c | 2202 | * |
8072030b | 2203 | * @since CUPS 1.4/macOS 10.6@ |
a4924f6c MS |
2204 | */ |
2205 | ||
5a738aea | 2206 | cups_dest_t * /* O - Destination or @code NULL@ */ |
568fa3fa MS |
2207 | cupsGetNamedDest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
2208 | const char *name, /* I - Destination name or @code NULL@ for the default destination */ | |
5a738aea | 2209 | const char *instance) /* I - Instance name or @code NULL@ */ |
a4924f6c | 2210 | { |
89c924d5 | 2211 | const char *dest_name; /* Working destination name */ |
a4924f6c MS |
2212 | cups_dest_t *dest; /* Destination */ |
2213 | char filename[1024], /* Path to lpoptions */ | |
2214 | defname[256]; /* Default printer name */ | |
2215 | const char *home = getenv("HOME"); /* Home directory */ | |
38e73f87 | 2216 | int set_as_default = 0; /* Set returned destination as default */ |
cb7f98ee | 2217 | ipp_op_t op = IPP_OP_GET_PRINTER_ATTRIBUTES; |
a4924f6c MS |
2218 | /* IPP operation to get server ops */ |
2219 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
2220 | ||
2221 | ||
2a75f21b | 2222 | DEBUG_printf(("cupsGetNamedDest(http=%p, name=\"%s\", instance=\"%s\")", (void *)http, name, instance)); |
4a366251 | 2223 | |
a4924f6c MS |
2224 | /* |
2225 | * If "name" is NULL, find the default destination... | |
2226 | */ | |
2227 | ||
89c924d5 MS |
2228 | dest_name = name; |
2229 | ||
2230 | if (!dest_name) | |
a4924f6c | 2231 | { |
38e73f87 | 2232 | set_as_default = 1; |
89c924d5 | 2233 | dest_name = _cupsUserDefault(defname, sizeof(defname)); |
a4924f6c | 2234 | |
89c924d5 | 2235 | if (dest_name) |
7cf5915e MS |
2236 | { |
2237 | char *ptr; /* Temporary pointer... */ | |
2238 | ||
2239 | if ((ptr = strchr(defname, '/')) != NULL) | |
2240 | { | |
2241 | *ptr++ = '\0'; | |
2242 | instance = ptr; | |
2243 | } | |
2244 | else | |
2245 | instance = NULL; | |
2246 | } | |
2247 | else if (home) | |
a4924f6c MS |
2248 | { |
2249 | /* | |
2250 | * No default in the environment, try the user's lpoptions files... | |
2251 | */ | |
2252 | ||
2253 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); | |
2254 | ||
89c924d5 | 2255 | dest_name = cups_get_default(filename, defname, sizeof(defname), &instance); |
a4924f6c MS |
2256 | } |
2257 | ||
2258 | if (!name) | |
2259 | { | |
2260 | /* | |
2261 | * Still not there? Try the system lpoptions file... | |
2262 | */ | |
2263 | ||
89c924d5 MS |
2264 | snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); |
2265 | dest_name = cups_get_default(filename, defname, sizeof(defname), &instance); | |
a4924f6c MS |
2266 | } |
2267 | ||
89c924d5 | 2268 | if (!dest_name) |
a4924f6c MS |
2269 | { |
2270 | /* | |
2271 | * No locally-set default destination, ask the server... | |
2272 | */ | |
2273 | ||
cb7f98ee | 2274 | op = IPP_OP_CUPS_GET_DEFAULT; |
4a366251 MS |
2275 | |
2276 | DEBUG_puts("1cupsGetNamedDest: Asking server for default printer..."); | |
a4924f6c | 2277 | } |
4a366251 MS |
2278 | else |
2279 | DEBUG_printf(("1cupsGetNamedDest: Using name=\"%s\"...", name)); | |
a4924f6c MS |
2280 | } |
2281 | ||
2282 | /* | |
2283 | * Get the printer's attributes... | |
2284 | */ | |
2285 | ||
89c924d5 | 2286 | if (!_cupsGetDests(http, op, dest_name, &dest, 0, 0)) |
4a366251 MS |
2287 | { |
2288 | if (name) | |
2289 | { | |
2290 | _cups_namedata_t data; /* Callback data */ | |
2291 | ||
2292 | DEBUG_puts("1cupsGetNamedDest: No queue found for printer, looking on network..."); | |
2293 | ||
2294 | data.name = name; | |
2295 | data.dest = NULL; | |
2296 | ||
f50db552 | 2297 | cupsEnumDests(0, 1000, NULL, 0, 0, (cups_dest_cb_t)cups_name_cb, &data); |
4a366251 MS |
2298 | |
2299 | if (!data.dest) | |
2300 | return (NULL); | |
2301 | ||
2302 | dest = data.dest; | |
2303 | } | |
2304 | else | |
2305 | return (NULL); | |
2306 | } | |
2307 | ||
2a75f21b | 2308 | DEBUG_printf(("1cupsGetNamedDest: Got dest=%p", (void *)dest)); |
a4924f6c MS |
2309 | |
2310 | if (instance) | |
2311 | dest->instance = _cupsStrAlloc(instance); | |
2312 | ||
38e73f87 MS |
2313 | if (set_as_default) |
2314 | dest->is_default = 1; | |
2315 | ||
a4924f6c MS |
2316 | /* |
2317 | * Then add local options... | |
2318 | */ | |
2319 | ||
2320 | snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); | |
89c924d5 | 2321 | cups_get_dests(filename, dest_name, instance, 1, 1, &dest); |
a4924f6c MS |
2322 | |
2323 | if (home) | |
2324 | { | |
2325 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); | |
2326 | ||
89c924d5 | 2327 | cups_get_dests(filename, dest_name, instance, 1, 1, &dest); |
a4924f6c MS |
2328 | } |
2329 | ||
2330 | /* | |
2331 | * Return the result... | |
2332 | */ | |
2333 | ||
2334 | return (dest); | |
2335 | } | |
2336 | ||
2337 | ||
f7deaa1a | 2338 | /* |
2339 | * 'cupsRemoveDest()' - Remove a destination from the destination list. | |
2340 | * | |
2341 | * Removing a destination/instance does not delete the class or printer | |
2342 | * queue, merely the lpoptions for that destination/instance. Use the | |
5a738aea MS |
2343 | * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new |
2344 | * options for the user. | |
f7deaa1a | 2345 | * |
8072030b | 2346 | * @since CUPS 1.3/macOS 10.5@ |
f7deaa1a | 2347 | */ |
2348 | ||
2349 | int /* O - New number of destinations */ | |
2350 | cupsRemoveDest(const char *name, /* I - Destination name */ | |
5a738aea | 2351 | const char *instance, /* I - Instance name or @code NULL@ */ |
f7deaa1a | 2352 | int num_dests, /* I - Number of destinations */ |
2353 | cups_dest_t **dests) /* IO - Destinations */ | |
2354 | { | |
2355 | int i; /* Index into destinations */ | |
2356 | cups_dest_t *dest; /* Pointer to destination */ | |
2357 | ||
2358 | ||
2359 | /* | |
2360 | * Find the destination... | |
2361 | */ | |
2362 | ||
2363 | if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL) | |
2364 | return (num_dests); | |
2365 | ||
2366 | /* | |
2367 | * Free memory... | |
2368 | */ | |
2369 | ||
2e4ff8af MS |
2370 | _cupsStrFree(dest->name); |
2371 | _cupsStrFree(dest->instance); | |
f7deaa1a | 2372 | cupsFreeOptions(dest->num_options, dest->options); |
2373 | ||
2374 | /* | |
2375 | * Remove the destination from the array... | |
2376 | */ | |
2377 | ||
2378 | num_dests --; | |
2379 | ||
7e86f2f6 | 2380 | i = (int)(dest - *dests); |
f7deaa1a | 2381 | |
2382 | if (i < num_dests) | |
7e86f2f6 | 2383 | memmove(dest, dest + 1, (size_t)(num_dests - i) * sizeof(cups_dest_t)); |
f7deaa1a | 2384 | |
2385 | return (num_dests); | |
2386 | } | |
2387 | ||
2388 | ||
2389 | /* | |
3d052e43 | 2390 | * 'cupsSetDefaultDest()' - Set the default destination. |
f7deaa1a | 2391 | * |
8072030b | 2392 | * @since CUPS 1.3/macOS 10.5@ |
f7deaa1a | 2393 | */ |
2394 | ||
2395 | void | |
2396 | cupsSetDefaultDest( | |
2397 | const char *name, /* I - Destination name */ | |
5a738aea | 2398 | const char *instance, /* I - Instance name or @code NULL@ */ |
f7deaa1a | 2399 | int num_dests, /* I - Number of destinations */ |
2400 | cups_dest_t *dests) /* I - Destinations */ | |
2401 | { | |
2402 | int i; /* Looping var */ | |
2403 | cups_dest_t *dest; /* Current destination */ | |
2404 | ||
2405 | ||
2406 | /* | |
2407 | * Range check input... | |
2408 | */ | |
2409 | ||
2410 | if (!name || num_dests <= 0 || !dests) | |
2411 | return; | |
2412 | ||
2413 | /* | |
2414 | * Loop through the array and set the "is_default" flag for the matching | |
2415 | * destination... | |
2416 | */ | |
2417 | ||
2418 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
88f9aafc | 2419 | dest->is_default = !_cups_strcasecmp(name, dest->name) && |
f7deaa1a | 2420 | ((!instance && !dest->instance) || |
2421 | (instance && dest->instance && | |
88f9aafc | 2422 | !_cups_strcasecmp(instance, dest->instance))); |
f7deaa1a | 2423 | } |
2424 | ||
2425 | ||
ef416fc2 | 2426 | /* |
2427 | * 'cupsSetDests()' - Save the list of destinations for the default server. | |
2428 | * | |
2429 | * This function saves the destinations to /etc/cups/lpoptions when run | |
b423cd4c | 2430 | * as root and ~/.cups/lpoptions when run as a normal user. |
98d88c8d MS |
2431 | * |
2432 | * @exclude all@ | |
ef416fc2 | 2433 | */ |
2434 | ||
2435 | void | |
2436 | cupsSetDests(int num_dests, /* I - Number of destinations */ | |
2437 | cups_dest_t *dests) /* I - Destinations */ | |
2438 | { | |
3d052e43 | 2439 | cupsSetDests2(CUPS_HTTP_DEFAULT, num_dests, dests); |
ef416fc2 | 2440 | } |
2441 | ||
2442 | ||
2443 | /* | |
2444 | * 'cupsSetDests2()' - Save the list of destinations for the specified server. | |
2445 | * | |
2446 | * This function saves the destinations to /etc/cups/lpoptions when run | |
b423cd4c | 2447 | * as root and ~/.cups/lpoptions when run as a normal user. |
ef416fc2 | 2448 | * |
8072030b | 2449 | * @since CUPS 1.1.21/macOS 10.4@ |
ef416fc2 | 2450 | */ |
2451 | ||
2452 | int /* O - 0 on success, -1 on error */ | |
568fa3fa | 2453 | cupsSetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 2454 | int num_dests, /* I - Number of destinations */ |
2455 | cups_dest_t *dests) /* I - Destinations */ | |
2456 | { | |
2457 | int i, j; /* Looping vars */ | |
2458 | int wrote; /* Wrote definition? */ | |
2459 | cups_dest_t *dest; /* Current destination */ | |
2460 | cups_option_t *option; /* Current option */ | |
8ca02f3c | 2461 | _ipp_option_t *match; /* Matching attribute for option */ |
ef416fc2 | 2462 | FILE *fp; /* File pointer */ |
b86bc4cf | 2463 | #ifndef WIN32 |
ef416fc2 | 2464 | const char *home; /* HOME environment variable */ |
b86bc4cf | 2465 | #endif /* WIN32 */ |
ef416fc2 | 2466 | char filename[1024]; /* lpoptions file */ |
2467 | int num_temps; /* Number of temporary destinations */ | |
82cc1f9a | 2468 | cups_dest_t *temps = NULL, /* Temporary destinations */ |
ef416fc2 | 2469 | *temp; /* Current temporary dest */ |
2470 | const char *val; /* Value of temporary option */ | |
3d052e43 | 2471 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
ef416fc2 | 2472 | |
2473 | ||
2474 | /* | |
2475 | * Range check the input... | |
2476 | */ | |
2477 | ||
3d052e43 | 2478 | if (!num_dests || !dests) |
ef416fc2 | 2479 | return (-1); |
2480 | ||
2481 | /* | |
2482 | * Get the server destinations... | |
2483 | */ | |
2484 | ||
f50db552 | 2485 | num_temps = _cupsGetDests(http, IPP_OP_CUPS_GET_PRINTERS, NULL, &temps, 0, 0); |
426c6a59 | 2486 | |
cb7f98ee | 2487 | if (cupsLastError() >= IPP_STATUS_REDIRECTION_OTHER_SITE) |
426c6a59 MS |
2488 | { |
2489 | cupsFreeDests(num_temps, temps); | |
2490 | return (-1); | |
2491 | } | |
ef416fc2 | 2492 | |
2493 | /* | |
2494 | * Figure out which file to write to... | |
2495 | */ | |
2496 | ||
2497 | snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); | |
2498 | ||
2499 | #ifndef WIN32 | |
2500 | if (getuid()) | |
2501 | { | |
2502 | /* | |
2503 | * Merge in server defaults... | |
2504 | */ | |
2505 | ||
38e73f87 | 2506 | num_temps = cups_get_dests(filename, NULL, NULL, 0, num_temps, &temps); |
ef416fc2 | 2507 | |
2508 | /* | |
2509 | * Point to user defaults... | |
2510 | */ | |
2511 | ||
2512 | if ((home = getenv("HOME")) != NULL) | |
b423cd4c | 2513 | { |
b423cd4c | 2514 | /* |
2515 | * Create ~/.cups subdirectory... | |
2516 | */ | |
2517 | ||
2518 | snprintf(filename, sizeof(filename), "%s/.cups", home); | |
2519 | if (access(filename, 0)) | |
2520 | mkdir(filename, 0700); | |
2521 | ||
2522 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); | |
2523 | } | |
ef416fc2 | 2524 | } |
2525 | #endif /* !WIN32 */ | |
2526 | ||
2527 | /* | |
2528 | * Try to open the file... | |
2529 | */ | |
2530 | ||
2531 | if ((fp = fopen(filename, "w")) == NULL) | |
2532 | { | |
2533 | cupsFreeDests(num_temps, temps); | |
2534 | return (-1); | |
2535 | } | |
2536 | ||
d6ae789d | 2537 | #ifndef WIN32 |
2538 | /* | |
2539 | * Set the permissions to 0644 when saving to the /etc/cups/lpoptions | |
2540 | * file... | |
2541 | */ | |
2542 | ||
2543 | if (!getuid()) | |
2544 | fchmod(fileno(fp), 0644); | |
2545 | #endif /* !WIN32 */ | |
2546 | ||
ef416fc2 | 2547 | /* |
2548 | * Write each printer; each line looks like: | |
2549 | * | |
2550 | * Dest name[/instance] options | |
2551 | * Default name[/instance] options | |
2552 | */ | |
2553 | ||
2554 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
2555 | if (dest->instance != NULL || dest->num_options != 0 || dest->is_default) | |
2556 | { | |
2557 | if (dest->is_default) | |
2558 | { | |
2559 | fprintf(fp, "Default %s", dest->name); | |
2560 | if (dest->instance) | |
2561 | fprintf(fp, "/%s", dest->instance); | |
2562 | ||
2563 | wrote = 1; | |
2564 | } | |
2565 | else | |
2566 | wrote = 0; | |
2567 | ||
2568 | if ((temp = cupsGetDest(dest->name, dest->instance, num_temps, temps)) == NULL) | |
2569 | temp = cupsGetDest(dest->name, NULL, num_temps, temps); | |
2570 | ||
2571 | for (j = dest->num_options, option = dest->options; j > 0; j --, option ++) | |
2572 | { | |
8ca02f3c | 2573 | /* |
2574 | * See if this option is a printer attribute; if so, skip it... | |
2575 | */ | |
2576 | ||
2577 | if ((match = _ippFindOption(option->name)) != NULL && | |
2578 | match->group_tag == IPP_TAG_PRINTER) | |
2579 | continue; | |
2580 | ||
ef416fc2 | 2581 | /* |
2582 | * See if the server/global options match these; if so, don't | |
2583 | * write 'em. | |
2584 | */ | |
2585 | ||
8ca02f3c | 2586 | if (temp && |
2587 | (val = cupsGetOption(option->name, temp->num_options, | |
2588 | temp->options)) != NULL && | |
88f9aafc | 2589 | !_cups_strcasecmp(val, option->value)) |
8ca02f3c | 2590 | continue; |
ef416fc2 | 2591 | |
2592 | /* | |
2593 | * Options don't match, write to the file... | |
2594 | */ | |
2595 | ||
2596 | if (!wrote) | |
2597 | { | |
2598 | fprintf(fp, "Dest %s", dest->name); | |
2599 | if (dest->instance) | |
2600 | fprintf(fp, "/%s", dest->instance); | |
2601 | wrote = 1; | |
2602 | } | |
88f9aafc | 2603 | |
ef416fc2 | 2604 | if (option->value[0]) |
2605 | { | |
8ca02f3c | 2606 | if (strchr(option->value, ' ') || |
2607 | strchr(option->value, '\\') || | |
2608 | strchr(option->value, '\"') || | |
2609 | strchr(option->value, '\'')) | |
2610 | { | |
2611 | /* | |
2612 | * Quote the value... | |
2613 | */ | |
2614 | ||
2615 | fprintf(fp, " %s=\"", option->name); | |
2616 | ||
2617 | for (val = option->value; *val; val ++) | |
2618 | { | |
2619 | if (strchr("\"\'\\", *val)) | |
2620 | putc('\\', fp); | |
2621 | ||
2622 | putc(*val, fp); | |
2623 | } | |
2624 | ||
2625 | putc('\"', fp); | |
2626 | } | |
2627 | else | |
2628 | { | |
2629 | /* | |
2630 | * Store the literal value... | |
2631 | */ | |
2632 | ||
ef416fc2 | 2633 | fprintf(fp, " %s=%s", option->name, option->value); |
8ca02f3c | 2634 | } |
ef416fc2 | 2635 | } |
2636 | else | |
2637 | fprintf(fp, " %s", option->name); | |
2638 | } | |
2639 | ||
2640 | if (wrote) | |
2641 | fputs("\n", fp); | |
2642 | } | |
2643 | ||
2644 | /* | |
fa73b229 | 2645 | * Free the temporary destinations and close the file... |
ef416fc2 | 2646 | */ |
2647 | ||
2648 | cupsFreeDests(num_temps, temps); | |
2649 | ||
fa73b229 | 2650 | fclose(fp); |
2651 | ||
080811b1 MS |
2652 | #ifdef __APPLE__ |
2653 | /* | |
2654 | * Set the default printer for this location - this allows command-line | |
2655 | * and GUI applications to share the same default destination... | |
2656 | */ | |
2657 | ||
2658 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL) | |
f14324a7 MS |
2659 | { |
2660 | CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, | |
2661 | dest->name, | |
2662 | kCFStringEncodingUTF8); | |
2663 | /* Default printer name */ | |
2664 | ||
2665 | if (name) | |
2666 | { | |
2667 | _cupsAppleSetDefaultPrinter(name); | |
2668 | CFRelease(name); | |
2669 | } | |
2670 | } | |
080811b1 MS |
2671 | #endif /* __APPLE__ */ |
2672 | ||
fa73b229 | 2673 | #ifdef HAVE_NOTIFY_POST |
ef416fc2 | 2674 | /* |
d4874933 | 2675 | * Send a notification so that macOS applications can know about the |
fa73b229 | 2676 | * change, too. |
ef416fc2 | 2677 | */ |
2678 | ||
fa73b229 | 2679 | notify_post("com.apple.printerListChange"); |
2680 | #endif /* HAVE_NOTIFY_POST */ | |
ef416fc2 | 2681 | |
2682 | return (0); | |
2683 | } | |
2684 | ||
2685 | ||
38e73f87 MS |
2686 | /* |
2687 | * '_cupsUserDefault()' - Get the user default printer from environment | |
2688 | * variables and location information. | |
2689 | */ | |
2690 | ||
2691 | char * /* O - Default printer or NULL */ | |
2692 | _cupsUserDefault(char *name, /* I - Name buffer */ | |
2693 | size_t namesize) /* I - Size of name buffer */ | |
2694 | { | |
2695 | const char *env; /* LPDEST or PRINTER env variable */ | |
2696 | #ifdef __APPLE__ | |
f14324a7 | 2697 | CFStringRef locprinter; /* Last printer as this location */ |
38e73f87 MS |
2698 | #endif /* __APPLE__ */ |
2699 | ||
2700 | ||
2701 | if ((env = getenv("LPDEST")) == NULL) | |
2702 | if ((env = getenv("PRINTER")) != NULL && !strcmp(env, "lp")) | |
2703 | env = NULL; | |
2704 | ||
2705 | if (env) | |
2706 | { | |
2707 | strlcpy(name, env, namesize); | |
2708 | return (name); | |
2709 | } | |
2710 | ||
2711 | #ifdef __APPLE__ | |
2712 | /* | |
2713 | * Use location-based defaults if "use last printer" is selected in the | |
2714 | * system preferences... | |
2715 | */ | |
2716 | ||
f14324a7 | 2717 | if ((locprinter = _cupsAppleCopyDefaultPrinter()) != NULL) |
38e73f87 | 2718 | { |
7e86f2f6 | 2719 | CFStringGetCString(locprinter, name, (CFIndex)namesize, kCFStringEncodingUTF8); |
f14324a7 MS |
2720 | CFRelease(locprinter); |
2721 | } | |
38e73f87 MS |
2722 | else |
2723 | name[0] = '\0'; | |
2724 | ||
f14324a7 | 2725 | DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name)); |
38e73f87 MS |
2726 | |
2727 | return (*name ? name : NULL); | |
2728 | ||
2729 | #else | |
2730 | /* | |
2731 | * No location-based defaults on this platform... | |
2732 | */ | |
2733 | ||
2734 | name[0] = '\0'; | |
2735 | return (NULL); | |
2736 | #endif /* __APPLE__ */ | |
2737 | } | |
2738 | ||
2739 | ||
9688061e | 2740 | #if _CUPS_LOCATION_DEFAULTS |
06d4e77b MS |
2741 | /* |
2742 | * 'appleCopyLocations()' - Copy the location history array. | |
2743 | */ | |
2744 | ||
2745 | static CFArrayRef /* O - Location array or NULL */ | |
2746 | appleCopyLocations(void) | |
2747 | { | |
2748 | CFArrayRef locations; /* Location array */ | |
2749 | ||
2750 | ||
2751 | /* | |
2752 | * Look up the location array in the preferences... | |
2753 | */ | |
2754 | ||
f14324a7 MS |
2755 | if ((locations = CFPreferencesCopyAppValue(kLastUsedPrintersKey, |
2756 | kCUPSPrintingPrefs)) == NULL) | |
06d4e77b MS |
2757 | return (NULL); |
2758 | ||
2759 | if (CFGetTypeID(locations) != CFArrayGetTypeID()) | |
2760 | { | |
2761 | CFRelease(locations); | |
2762 | return (NULL); | |
2763 | } | |
2764 | ||
2765 | return (locations); | |
2766 | } | |
2767 | ||
2768 | ||
749b1e90 MS |
2769 | /* |
2770 | * 'appleCopyNetwork()' - Get the network ID for the current location. | |
2771 | */ | |
2772 | ||
2773 | static CFStringRef /* O - Network ID */ | |
2774 | appleCopyNetwork(void) | |
2775 | { | |
2776 | SCDynamicStoreRef dynamicStore; /* System configuration data */ | |
2777 | CFStringRef key; /* Current network configuration key */ | |
2778 | CFDictionaryRef ip_dict; /* Network configuration data */ | |
2779 | CFStringRef network = NULL; /* Current network ID */ | |
88f9aafc | 2780 | |
749b1e90 | 2781 | |
f14324a7 | 2782 | if ((dynamicStore = SCDynamicStoreCreate(NULL, CFSTR("libcups"), NULL, |
749b1e90 MS |
2783 | NULL)) != NULL) |
2784 | { | |
f14324a7 MS |
2785 | /* |
2786 | * First use the IPv6 router address, if available, since that will generally | |
2787 | * be a globally-unique link-local address. | |
2788 | */ | |
2789 | ||
749b1e90 | 2790 | if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity( |
f14324a7 | 2791 | NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)) != NULL) |
749b1e90 MS |
2792 | { |
2793 | if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL) | |
2794 | { | |
2795 | if ((network = CFDictionaryGetValue(ip_dict, | |
f14324a7 | 2796 | kSCPropNetIPv6Router)) != NULL) |
749b1e90 MS |
2797 | CFRetain(network); |
2798 | ||
2799 | CFRelease(ip_dict); | |
2800 | } | |
2801 | ||
2802 | CFRelease(key); | |
2803 | } | |
2804 | ||
f14324a7 MS |
2805 | /* |
2806 | * If that doesn't work, try the IPv4 router address. This isn't as unique | |
2807 | * and will likely be a 10.x.y.z or 192.168.y.z address... | |
2808 | */ | |
2809 | ||
2810 | if (!network) | |
2811 | { | |
2812 | if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity( | |
2813 | NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)) != NULL) | |
2814 | { | |
2815 | if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL) | |
2816 | { | |
2817 | if ((network = CFDictionaryGetValue(ip_dict, | |
2818 | kSCPropNetIPv4Router)) != NULL) | |
2819 | CFRetain(network); | |
2820 | ||
2821 | CFRelease(ip_dict); | |
2822 | } | |
2823 | ||
2824 | CFRelease(key); | |
2825 | } | |
2826 | } | |
2827 | ||
749b1e90 MS |
2828 | CFRelease(dynamicStore); |
2829 | } | |
2830 | ||
2831 | return (network); | |
2832 | } | |
9688061e | 2833 | #endif /* _CUPS_LOCATION_DEFAULTS */ |
749b1e90 MS |
2834 | |
2835 | ||
9688061e | 2836 | #ifdef __APPLE__ |
426c6a59 MS |
2837 | /* |
2838 | * 'appleGetPaperSize()' - Get the default paper size. | |
2839 | */ | |
2840 | ||
5a9febac | 2841 | static char * /* O - Default paper size */ |
d649be59 MS |
2842 | appleGetPaperSize(char *name, /* I - Paper size name buffer */ |
2843 | size_t namesize) /* I - Size of buffer */ | |
426c6a59 | 2844 | { |
54afec33 | 2845 | CFStringRef defaultPaperID; /* Default paper ID */ |
6961465f | 2846 | pwg_media_t *pwgmedia; /* PWG media size */ |
426c6a59 MS |
2847 | |
2848 | ||
c8fef167 | 2849 | defaultPaperID = _cupsAppleCopyDefaultPaperID(); |
426c6a59 | 2850 | if (!defaultPaperID || |
557dde9f | 2851 | CFGetTypeID(defaultPaperID) != CFStringGetTypeID() || |
d649be59 | 2852 | !CFStringGetCString(defaultPaperID, name, (CFIndex)namesize, kCFStringEncodingUTF8)) |
426c6a59 | 2853 | name[0] = '\0'; |
6961465f | 2854 | else if ((pwgmedia = pwgMediaForLegacy(name)) != NULL) |
c168a833 | 2855 | strlcpy(name, pwgmedia->pwg, namesize); |
426c6a59 MS |
2856 | |
2857 | if (defaultPaperID) | |
2858 | CFRelease(defaultPaperID); | |
2859 | ||
2860 | return (name); | |
2861 | } | |
9688061e | 2862 | #endif /* __APPLE__ */ |
426c6a59 MS |
2863 | |
2864 | ||
9688061e | 2865 | #if _CUPS_LOCATION_DEFAULTS |
080811b1 MS |
2866 | /* |
2867 | * 'appleGetPrinter()' - Get a printer from the history array. | |
2868 | */ | |
2869 | ||
2870 | static CFStringRef /* O - Printer name or NULL */ | |
2871 | appleGetPrinter(CFArrayRef locations, /* I - Location array */ | |
2872 | CFStringRef network, /* I - Network name */ | |
2873 | CFIndex *locindex) /* O - Index in array */ | |
2874 | { | |
2875 | CFIndex i, /* Looping var */ | |
2876 | count; /* Number of locations */ | |
2877 | CFDictionaryRef location; /* Current location */ | |
2878 | CFStringRef locnetwork, /* Current network */ | |
2879 | locprinter; /* Current printer */ | |
2880 | ||
2881 | ||
2882 | for (i = 0, count = CFArrayGetCount(locations); i < count; i ++) | |
2883 | if ((location = CFArrayGetValueAtIndex(locations, i)) != NULL && | |
2884 | CFGetTypeID(location) == CFDictionaryGetTypeID()) | |
2885 | { | |
2886 | if ((locnetwork = CFDictionaryGetValue(location, | |
2887 | kLocationNetworkKey)) != NULL && | |
2888 | CFGetTypeID(locnetwork) == CFStringGetTypeID() && | |
2889 | CFStringCompare(network, locnetwork, 0) == kCFCompareEqualTo && | |
2890 | (locprinter = CFDictionaryGetValue(location, | |
2891 | kLocationPrinterIDKey)) != NULL && | |
2892 | CFGetTypeID(locprinter) == CFStringGetTypeID()) | |
2893 | { | |
2894 | if (locindex) | |
2895 | *locindex = i; | |
2896 | ||
2897 | return (locprinter); | |
2898 | } | |
2899 | } | |
2900 | ||
2901 | return (NULL); | |
2902 | } | |
9688061e | 2903 | #endif /* _CUPS_LOCATION_DEFAULTS */ |
080811b1 MS |
2904 | |
2905 | ||
426c6a59 MS |
2906 | /* |
2907 | * 'cups_add_dest()' - Add a destination to the array. | |
2908 | * | |
2909 | * Unlike cupsAddDest(), this function does not check for duplicates. | |
2910 | */ | |
2911 | ||
2912 | static cups_dest_t * /* O - New destination */ | |
2913 | cups_add_dest(const char *name, /* I - Name of destination */ | |
2914 | const char *instance, /* I - Instance or NULL */ | |
2915 | int *num_dests, /* IO - Number of destinations */ | |
2916 | cups_dest_t **dests) /* IO - Destinations */ | |
2917 | { | |
2918 | int insert, /* Insertion point */ | |
2919 | diff; /* Result of comparison */ | |
2920 | cups_dest_t *dest; /* Destination pointer */ | |
2921 | ||
2922 | ||
2923 | /* | |
2924 | * Add new destination... | |
2925 | */ | |
2926 | ||
2927 | if (*num_dests == 0) | |
2928 | dest = malloc(sizeof(cups_dest_t)); | |
2929 | else | |
7e86f2f6 | 2930 | dest = realloc(*dests, sizeof(cups_dest_t) * (size_t)(*num_dests + 1)); |
426c6a59 MS |
2931 | |
2932 | if (!dest) | |
2933 | return (NULL); | |
2934 | ||
2935 | *dests = dest; | |
2936 | ||
2937 | /* | |
2938 | * Find where to insert the destination... | |
2939 | */ | |
2940 | ||
2941 | if (*num_dests == 0) | |
2942 | insert = 0; | |
2943 | else | |
2944 | { | |
2945 | insert = cups_find_dest(name, instance, *num_dests, *dests, *num_dests - 1, | |
2946 | &diff); | |
2947 | ||
2948 | if (diff > 0) | |
2949 | insert ++; | |
2950 | } | |
2951 | ||
2952 | /* | |
2953 | * Move the array elements as needed... | |
2954 | */ | |
2955 | ||
2956 | if (insert < *num_dests) | |
7e86f2f6 | 2957 | memmove(*dests + insert + 1, *dests + insert, (size_t)(*num_dests - insert) * sizeof(cups_dest_t)); |
426c6a59 MS |
2958 | |
2959 | (*num_dests) ++; | |
2960 | ||
2961 | /* | |
2962 | * Initialize the destination... | |
2963 | */ | |
2964 | ||
2965 | dest = *dests + insert; | |
2966 | dest->name = _cupsStrAlloc(name); | |
2967 | dest->instance = _cupsStrAlloc(instance); | |
2968 | dest->is_default = 0; | |
2969 | dest->num_options = 0; | |
2970 | dest->options = (cups_option_t *)0; | |
2971 | ||
2972 | return (dest); | |
2973 | } | |
2974 | ||
2975 | ||
9c80ffa2 MS |
2976 | # ifdef __BLOCKS__ |
2977 | /* | |
2978 | * 'cups_block_cb()' - Enumeration callback for block API. | |
2979 | */ | |
2980 | ||
2981 | static int /* O - 1 to continue, 0 to stop */ | |
2982 | cups_block_cb( | |
2983 | cups_dest_block_t block, /* I - Block */ | |
dcb445bc MS |
2984 | unsigned flags, /* I - Destination flags */ |
2985 | cups_dest_t *dest) /* I - Destination */ | |
9c80ffa2 | 2986 | { |
dcb445bc | 2987 | return ((block)(flags, dest)); |
9c80ffa2 MS |
2988 | } |
2989 | # endif /* __BLOCKS__ */ | |
2990 | ||
2991 | ||
426c6a59 MS |
2992 | /* |
2993 | * 'cups_compare_dests()' - Compare two destinations. | |
2994 | */ | |
2995 | ||
2996 | static int /* O - Result of comparison */ | |
2997 | cups_compare_dests(cups_dest_t *a, /* I - First destination */ | |
2998 | cups_dest_t *b) /* I - Second destination */ | |
2999 | { | |
3000 | int diff; /* Difference */ | |
3001 | ||
3002 | ||
88f9aafc | 3003 | if ((diff = _cups_strcasecmp(a->name, b->name)) != 0) |
426c6a59 MS |
3004 | return (diff); |
3005 | else if (a->instance && b->instance) | |
88f9aafc | 3006 | return (_cups_strcasecmp(a->instance, b->instance)); |
426c6a59 MS |
3007 | else |
3008 | return ((a->instance && !b->instance) - (!a->instance && b->instance)); | |
3009 | } | |
3010 | ||
3011 | ||
a29fd7dd MS |
3012 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
3013 | # ifdef HAVE_DNSSD | |
dcb445bc MS |
3014 | /* |
3015 | * 'cups_dnssd_browse_cb()' - Browse for printers. | |
3016 | */ | |
3017 | ||
3018 | static void | |
3019 | cups_dnssd_browse_cb( | |
3020 | DNSServiceRef sdRef, /* I - Service reference */ | |
3021 | DNSServiceFlags flags, /* I - Option flags */ | |
3022 | uint32_t interfaceIndex, /* I - Interface number */ | |
3023 | DNSServiceErrorType errorCode, /* I - Error, if any */ | |
3024 | const char *serviceName, /* I - Name of service/device */ | |
3025 | const char *regtype, /* I - Type of service */ | |
3026 | const char *replyDomain, /* I - Service domain */ | |
3027 | void *context) /* I - Enumeration data */ | |
3028 | { | |
3029 | _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; | |
3030 | /* Enumeration data */ | |
3031 | ||
3032 | ||
807315e6 | 3033 | 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)); |
dcb445bc MS |
3034 | |
3035 | /* | |
3036 | * Don't do anything on error... | |
3037 | */ | |
3038 | ||
3039 | if (errorCode != kDNSServiceErr_NoError) | |
3040 | return; | |
3041 | ||
3042 | /* | |
3043 | * Get the device... | |
3044 | */ | |
3045 | ||
3046 | cups_dnssd_get_device(data, serviceName, regtype, replyDomain); | |
3047 | } | |
3048 | ||
3049 | ||
a29fd7dd MS |
3050 | # else /* HAVE_AVAHI */ |
3051 | /* | |
3052 | * 'cups_dnssd_browse_cb()' - Browse for printers. | |
3053 | */ | |
3054 | ||
3055 | static void | |
3056 | cups_dnssd_browse_cb( | |
3057 | AvahiServiceBrowser *browser, /* I - Browser */ | |
3058 | AvahiIfIndex interface, /* I - Interface index (unused) */ | |
3059 | AvahiProtocol protocol, /* I - Network protocol (unused) */ | |
3060 | AvahiBrowserEvent event, /* I - What happened */ | |
3061 | const char *name, /* I - Service name */ | |
3062 | const char *type, /* I - Registration type */ | |
3063 | const char *domain, /* I - Domain */ | |
3064 | AvahiLookupResultFlags flags, /* I - Flags */ | |
3065 | void *context) /* I - Devices array */ | |
3066 | { | |
37e7e6e0 | 3067 | #ifdef DEBUG |
a29fd7dd MS |
3068 | AvahiClient *client = avahi_service_browser_get_client(browser); |
3069 | /* Client information */ | |
37e7e6e0 | 3070 | #endif /* DEBUG */ |
a29fd7dd MS |
3071 | _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; |
3072 | /* Enumeration data */ | |
3073 | ||
3074 | ||
3075 | (void)interface; | |
3076 | (void)protocol; | |
3077 | (void)context; | |
3078 | ||
657c5b5f MS |
3079 | DEBUG_printf(("cups_dnssd_browse_cb(..., name=\"%s\", type=\"%s\", domain=\"%s\", ...);", name, type, domain)); |
3080 | ||
a29fd7dd MS |
3081 | switch (event) |
3082 | { | |
3083 | case AVAHI_BROWSER_FAILURE: | |
657c5b5f | 3084 | DEBUG_printf(("cups_dnssd_browse_cb: %s", avahi_strerror(avahi_client_errno(client)))); |
a29fd7dd MS |
3085 | avahi_simple_poll_quit(data->simple_poll); |
3086 | break; | |
3087 | ||
3088 | case AVAHI_BROWSER_NEW: | |
3089 | /* | |
3090 | * This object is new on the network. | |
3091 | */ | |
3092 | ||
3093 | if (flags & AVAHI_LOOKUP_RESULT_LOCAL) | |
3094 | { | |
3095 | /* | |
3096 | * This comes from the local machine so ignore it. | |
3097 | */ | |
3098 | ||
657c5b5f | 3099 | DEBUG_printf(("cups_dnssd_browse_cb: Ignoring local service \"%s\".", name)); |
a29fd7dd MS |
3100 | } |
3101 | else | |
3102 | { | |
3103 | /* | |
3104 | * Create a device entry for it if it doesn't yet exist. | |
3105 | */ | |
3106 | ||
3107 | cups_dnssd_get_device(data, name, type, domain); | |
3108 | } | |
3109 | break; | |
3110 | ||
657c5b5f MS |
3111 | case AVAHI_BROWSER_REMOVE : |
3112 | case AVAHI_BROWSER_CACHE_EXHAUSTED : | |
3113 | break; | |
3114 | ||
3115 | case AVAHI_BROWSER_ALL_FOR_NOW : | |
3116 | DEBUG_puts("cups_dnssd_browse_cb: ALL_FOR_NOW"); | |
3117 | data->browsers --; | |
a29fd7dd MS |
3118 | break; |
3119 | } | |
3120 | } | |
3121 | ||
3122 | ||
3123 | /* | |
3124 | * 'cups_dnssd_client_cb()' - Avahi client callback function. | |
3125 | */ | |
3126 | ||
3127 | static void | |
3128 | cups_dnssd_client_cb( | |
3129 | AvahiClient *client, /* I - Client information (unused) */ | |
3130 | AvahiClientState state, /* I - Current state */ | |
3131 | void *context) /* I - User data (unused) */ | |
3132 | { | |
3133 | _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; | |
3134 | /* Enumeration data */ | |
3135 | ||
3136 | ||
3137 | (void)client; | |
3138 | ||
657c5b5f MS |
3139 | DEBUG_printf(("cups_dnssd_client_cb(client=%p, state=%d, context=%p)", client, state, context)); |
3140 | ||
a29fd7dd MS |
3141 | /* |
3142 | * If the connection drops, quit. | |
3143 | */ | |
3144 | ||
3145 | if (state == AVAHI_CLIENT_FAILURE) | |
3146 | { | |
3147 | DEBUG_puts("cups_dnssd_client_cb: Avahi connection failed."); | |
3148 | avahi_simple_poll_quit(data->simple_poll); | |
3149 | } | |
3150 | } | |
3151 | # endif /* HAVE_DNSSD */ | |
3152 | ||
3153 | ||
dcb445bc MS |
3154 | /* |
3155 | * 'cups_dnssd_compare_device()' - Compare two devices. | |
3156 | */ | |
3157 | ||
3158 | static int /* O - Result of comparison */ | |
3159 | cups_dnssd_compare_devices( | |
3160 | _cups_dnssd_device_t *a, /* I - First device */ | |
3161 | _cups_dnssd_device_t *b) /* I - Second device */ | |
3162 | { | |
3163 | return (strcmp(a->dest.name, b->dest.name)); | |
3164 | } | |
3165 | ||
3166 | ||
3167 | /* | |
3168 | * 'cups_dnssd_free_device()' - Free the memory used by a device. | |
3169 | */ | |
3170 | ||
3171 | static void | |
3172 | cups_dnssd_free_device( | |
3173 | _cups_dnssd_device_t *device, /* I - Device */ | |
3174 | _cups_dnssd_data_t *data) /* I - Enumeration data */ | |
3175 | { | |
807315e6 | 3176 | DEBUG_printf(("5cups_dnssd_free_device(device=%p(%s), data=%p)", (void *)device, device->dest.name, (void *)data)); |
dcb445bc | 3177 | |
a29fd7dd | 3178 | # ifdef HAVE_DNSSD |
dcb445bc MS |
3179 | if (device->ref) |
3180 | DNSServiceRefDeallocate(device->ref); | |
a29fd7dd MS |
3181 | # else /* HAVE_AVAHI */ |
3182 | if (device->ref) | |
3183 | avahi_record_browser_free(device->ref); | |
3184 | # endif /* HAVE_DNSSD */ | |
dcb445bc MS |
3185 | |
3186 | _cupsStrFree(device->domain); | |
3187 | _cupsStrFree(device->fullName); | |
3188 | _cupsStrFree(device->regtype); | |
3189 | _cupsStrFree(device->dest.name); | |
3190 | ||
3191 | cupsFreeOptions(device->dest.num_options, device->dest.options); | |
3192 | ||
3193 | free(device); | |
3194 | } | |
3195 | ||
3196 | ||
3197 | /* | |
3198 | * 'cups_dnssd_get_device()' - Lookup a device and create it as needed. | |
3199 | */ | |
3200 | ||
3201 | static _cups_dnssd_device_t * /* O - Device */ | |
3202 | cups_dnssd_get_device( | |
3203 | _cups_dnssd_data_t *data, /* I - Enumeration data */ | |
3204 | const char *serviceName, /* I - Service name */ | |
3205 | const char *regtype, /* I - Registration type */ | |
3206 | const char *replyDomain) /* I - Domain name */ | |
3207 | { | |
3208 | _cups_dnssd_device_t key, /* Search key */ | |
3209 | *device; /* Device */ | |
a2187a63 | 3210 | char fullName[kDNSServiceMaxDomainName], |
dcb445bc | 3211 | /* Full name for query */ |
a2187a63 | 3212 | name[128]; /* Queue name */ |
dcb445bc MS |
3213 | |
3214 | ||
807315e6 | 3215 | DEBUG_printf(("5cups_dnssd_get_device(data=%p, serviceName=\"%s\", regtype=\"%s\", replyDomain=\"%s\")", (void *)data, serviceName, regtype, replyDomain)); |
dcb445bc MS |
3216 | |
3217 | /* | |
3218 | * See if this is an existing device... | |
3219 | */ | |
3220 | ||
efa72f61 | 3221 | cups_queue_name(name, serviceName, sizeof(name)); |
a2187a63 MS |
3222 | |
3223 | key.dest.name = name; | |
dcb445bc MS |
3224 | |
3225 | if ((device = cupsArrayFind(data->devices, &key)) != NULL) | |
3226 | { | |
3227 | /* | |
3228 | * Yes, see if we need to do anything with this... | |
3229 | */ | |
3230 | ||
3231 | int update = 0; /* Non-zero if we need to update */ | |
3232 | ||
3233 | if (!_cups_strcasecmp(replyDomain, "local.") && | |
3234 | _cups_strcasecmp(device->domain, replyDomain)) | |
3235 | { | |
3236 | /* | |
3237 | * Update the "global" listing to use the .local domain name instead. | |
3238 | */ | |
3239 | ||
3240 | _cupsStrFree(device->domain); | |
3241 | device->domain = _cupsStrAlloc(replyDomain); | |
3242 | ||
3243 | DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use local " | |
3244 | "domain.", device->dest.name)); | |
3245 | ||
3246 | update = 1; | |
3247 | } | |
3248 | ||
3249 | if (!_cups_strcasecmp(regtype, "_ipps._tcp") && | |
3250 | _cups_strcasecmp(device->regtype, regtype)) | |
3251 | { | |
3252 | /* | |
3253 | * Prefer IPPS over IPP. | |
3254 | */ | |
3255 | ||
3256 | _cupsStrFree(device->regtype); | |
3257 | device->regtype = _cupsStrAlloc(regtype); | |
3258 | ||
3259 | DEBUG_printf(("6cups_dnssd_get_device: Updating '%s' to use IPPS.", | |
3260 | device->dest.name)); | |
3261 | ||
3262 | update = 1; | |
3263 | } | |
3264 | ||
3265 | if (!update) | |
3266 | { | |
3267 | DEBUG_printf(("6cups_dnssd_get_device: No changes to '%s'.", | |
3268 | device->dest.name)); | |
3269 | return (device); | |
3270 | } | |
3271 | } | |
3272 | else | |
3273 | { | |
3274 | /* | |
3275 | * No, add the device... | |
3276 | */ | |
3277 | ||
3278 | DEBUG_printf(("6cups_dnssd_get_device: Adding '%s' for %s with domain " | |
3279 | "'%s'.", serviceName, | |
3280 | !strcmp(regtype, "_ipps._tcp") ? "IPPS" : "IPP", | |
3281 | replyDomain)); | |
3282 | ||
3283 | device = calloc(sizeof(_cups_dnssd_device_t), 1); | |
a2187a63 | 3284 | device->dest.name = _cupsStrAlloc(name); |
dcb445bc MS |
3285 | device->domain = _cupsStrAlloc(replyDomain); |
3286 | device->regtype = _cupsStrAlloc(regtype); | |
3287 | ||
a2187a63 MS |
3288 | device->dest.num_options = cupsAddOption("printer-info", serviceName, 0, &device->dest.options); |
3289 | ||
dcb445bc MS |
3290 | cupsArrayAdd(data->devices, device); |
3291 | } | |
3292 | ||
3293 | /* | |
3294 | * Set the "full name" of this service, which is used for queries... | |
3295 | */ | |
3296 | ||
a29fd7dd | 3297 | # ifdef HAVE_DNSSD |
a2187a63 | 3298 | DNSServiceConstructFullName(fullName, serviceName, regtype, replyDomain); |
a29fd7dd | 3299 | # else /* HAVE_AVAHI */ |
a2187a63 | 3300 | avahi_service_name_join(fullName, kDNSServiceMaxDomainName, serviceName, regtype, replyDomain); |
a29fd7dd MS |
3301 | # endif /* HAVE_DNSSD */ |
3302 | ||
dcb445bc MS |
3303 | _cupsStrFree(device->fullName); |
3304 | device->fullName = _cupsStrAlloc(fullName); | |
3305 | ||
3306 | if (device->ref) | |
3307 | { | |
a29fd7dd | 3308 | # ifdef HAVE_DNSSD |
dcb445bc | 3309 | DNSServiceRefDeallocate(device->ref); |
a29fd7dd MS |
3310 | # else /* HAVE_AVAHI */ |
3311 | avahi_record_browser_free(device->ref); | |
3312 | # endif /* HAVE_DNSSD */ | |
3313 | ||
dcb445bc MS |
3314 | device->ref = 0; |
3315 | } | |
3316 | ||
3317 | if (device->state == _CUPS_DNSSD_ACTIVE) | |
3318 | { | |
a2187a63 MS |
3319 | DEBUG_printf(("6cups_dnssd_get_device: Remove callback for \"%s\".", device->dest.name)); |
3320 | ||
dcb445bc MS |
3321 | (*data->cb)(data->user_data, CUPS_DEST_FLAGS_REMOVED, &device->dest); |
3322 | device->state = _CUPS_DNSSD_NEW; | |
3323 | } | |
3324 | ||
3325 | return (device); | |
3326 | } | |
3327 | ||
3328 | ||
a29fd7dd | 3329 | # ifdef HAVE_DNSSD |
dcb445bc MS |
3330 | /* |
3331 | * 'cups_dnssd_local_cb()' - Browse for local printers. | |
3332 | */ | |
3333 | ||
3334 | static void | |
3335 | cups_dnssd_local_cb( | |
3336 | DNSServiceRef sdRef, /* I - Service reference */ | |
3337 | DNSServiceFlags flags, /* I - Option flags */ | |
3338 | uint32_t interfaceIndex, /* I - Interface number */ | |
3339 | DNSServiceErrorType errorCode, /* I - Error, if any */ | |
3340 | const char *serviceName, /* I - Name of service/device */ | |
3341 | const char *regtype, /* I - Type of service */ | |
3342 | const char *replyDomain, /* I - Service domain */ | |
3343 | void *context) /* I - Devices array */ | |
3344 | { | |
3345 | _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; | |
3346 | /* Enumeration data */ | |
3347 | _cups_dnssd_device_t *device; /* Device */ | |
3348 | ||
3349 | ||
807315e6 | 3350 | 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)); |
dcb445bc MS |
3351 | |
3352 | /* | |
3353 | * Only process "add" data... | |
3354 | */ | |
3355 | ||
3356 | if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd)) | |
3357 | return; | |
3358 | ||
3359 | /* | |
3360 | * Get the device... | |
3361 | */ | |
3362 | ||
3363 | device = cups_dnssd_get_device(data, serviceName, regtype, replyDomain); | |
3364 | ||
3365 | /* | |
3366 | * Hide locally-registered devices... | |
3367 | */ | |
3368 | ||
3369 | DEBUG_printf(("6cups_dnssd_local_cb: Hiding local printer '%s'.", | |
3370 | serviceName)); | |
3371 | ||
3372 | if (device->ref) | |
3373 | { | |
3374 | DNSServiceRefDeallocate(device->ref); | |
3375 | device->ref = 0; | |
3376 | } | |
3377 | ||
3378 | if (device->state == _CUPS_DNSSD_ACTIVE) | |
a2187a63 MS |
3379 | { |
3380 | DEBUG_printf(("6cups_dnssd_local_cb: Remove callback for \"%s\".", device->dest.name)); | |
dcb445bc | 3381 | (*data->cb)(data->user_data, CUPS_DEST_FLAGS_REMOVED, &device->dest); |
a2187a63 | 3382 | } |
dcb445bc MS |
3383 | |
3384 | device->state = _CUPS_DNSSD_LOCAL; | |
3385 | } | |
a29fd7dd MS |
3386 | # endif /* HAVE_DNSSD */ |
3387 | ||
3388 | ||
3389 | # ifdef HAVE_AVAHI | |
3390 | /* | |
3391 | * 'cups_dnssd_poll_cb()' - Wait for input on the specified file descriptors. | |
3392 | * | |
3393 | * Note: This function is needed because avahi_simple_poll_iterate is broken | |
3394 | * and always uses a timeout of 0 (!) milliseconds. | |
ac032efa | 3395 | * (https://github.com/lathiat/avahi/issues/127) |
53af7f21 MS |
3396 | * |
3397 | * @private@ | |
a29fd7dd MS |
3398 | */ |
3399 | ||
3400 | static int /* O - Number of file descriptors matching */ | |
3401 | cups_dnssd_poll_cb( | |
3402 | struct pollfd *pollfds, /* I - File descriptors */ | |
3403 | unsigned int num_pollfds, /* I - Number of file descriptors */ | |
3404 | int timeout, /* I - Timeout in milliseconds (unused) */ | |
3405 | void *context) /* I - User data (unused) */ | |
3406 | { | |
3407 | _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; | |
3408 | /* Enumeration data */ | |
3409 | int val; /* Return value */ | |
3410 | ||
3411 | ||
657c5b5f MS |
3412 | DEBUG_printf(("cups_dnssd_poll_cb(pollfds=%p, num_pollfds=%d, timeout=%d, context=%p)", pollfds, num_pollfds, timeout, context)); |
3413 | ||
a29fd7dd MS |
3414 | (void)timeout; |
3415 | ||
3fae3b33 | 3416 | val = poll(pollfds, num_pollfds, _CUPS_DNSSD_MAXTIME); |
657c5b5f MS |
3417 | |
3418 | DEBUG_printf(("cups_dnssd_poll_cb: poll() returned %d", val)); | |
a29fd7dd MS |
3419 | |
3420 | if (val < 0) | |
3421 | { | |
3422 | DEBUG_printf(("cups_dnssd_poll_cb: %s", strerror(errno))); | |
3423 | } | |
3424 | else if (val > 0) | |
657c5b5f | 3425 | { |
a29fd7dd | 3426 | data->got_data = 1; |
657c5b5f | 3427 | } |
a29fd7dd MS |
3428 | |
3429 | return (val); | |
3430 | } | |
3431 | # endif /* HAVE_AVAHI */ | |
dcb445bc MS |
3432 | |
3433 | ||
3434 | /* | |
3435 | * 'cups_dnssd_query_cb()' - Process query data. | |
3436 | */ | |
3437 | ||
a29fd7dd | 3438 | # ifdef HAVE_DNSSD |
dcb445bc MS |
3439 | static void |
3440 | cups_dnssd_query_cb( | |
3441 | DNSServiceRef sdRef, /* I - Service reference */ | |
3442 | DNSServiceFlags flags, /* I - Data flags */ | |
3443 | uint32_t interfaceIndex, /* I - Interface */ | |
3444 | DNSServiceErrorType errorCode, /* I - Error, if any */ | |
3445 | const char *fullName, /* I - Full service name */ | |
3446 | uint16_t rrtype, /* I - Record type */ | |
3447 | uint16_t rrclass, /* I - Record class */ | |
3448 | uint16_t rdlen, /* I - Length of record data */ | |
3449 | const void *rdata, /* I - Record data */ | |
3450 | uint32_t ttl, /* I - Time-to-live */ | |
3451 | void *context) /* I - Enumeration data */ | |
3452 | { | |
a29fd7dd MS |
3453 | # else /* HAVE_AVAHI */ |
3454 | static void | |
3455 | cups_dnssd_query_cb( | |
3456 | AvahiRecordBrowser *browser, /* I - Record browser */ | |
3457 | AvahiIfIndex interfaceIndex, | |
3458 | /* I - Interface index (unused) */ | |
3459 | AvahiProtocol protocol, /* I - Network protocol (unused) */ | |
3460 | AvahiBrowserEvent event, /* I - What happened? */ | |
3461 | const char *fullName, /* I - Service name */ | |
3462 | uint16_t rrclass, /* I - Record class */ | |
3463 | uint16_t rrtype, /* I - Record type */ | |
3464 | const void *rdata, /* I - TXT record */ | |
3465 | size_t rdlen, /* I - Length of TXT record */ | |
3466 | AvahiLookupResultFlags flags, /* I - Flags */ | |
3467 | void *context) /* I - Enumeration data */ | |
3468 | { | |
37e7e6e0 | 3469 | # ifdef DEBUG |
a29fd7dd MS |
3470 | AvahiClient *client = avahi_record_browser_get_client(browser); |
3471 | /* Client information */ | |
37e7e6e0 | 3472 | # endif /* DEBUG */ |
a29fd7dd | 3473 | # endif /* HAVE_DNSSD */ |
dcb445bc MS |
3474 | _cups_dnssd_data_t *data = (_cups_dnssd_data_t *)context; |
3475 | /* Enumeration data */ | |
a2187a63 MS |
3476 | char serviceName[256],/* Service name */ |
3477 | name[128], /* Queue name */ | |
dcb445bc MS |
3478 | *ptr; /* Pointer into string */ |
3479 | _cups_dnssd_device_t dkey, /* Search key */ | |
3480 | *device; /* Device */ | |
3481 | ||
3482 | ||
a29fd7dd | 3483 | # ifdef HAVE_DNSSD |
807315e6 | 3484 | 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)); |
dcb445bc MS |
3485 | |
3486 | /* | |
3487 | * Only process "add" data... | |
3488 | */ | |
3489 | ||
3490 | if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd)) | |
3491 | return; | |
3492 | ||
a29fd7dd | 3493 | # else /* HAVE_AVAHI */ |
657c5b5f | 3494 | DEBUG_printf(("cups_dnssd_query_cb(browser=%p, interfaceIndex=%d, protocol=%d, event=%d, fullName=\"%s\", rrclass=%u, rrtype=%u, rdata=%p, rdlen=%u, flags=%x, context=%p)", browser, interfaceIndex, protocol, event, fullName, rrclass, rrtype, rdata, (unsigned)rdlen, flags, context)); |
a29fd7dd MS |
3495 | |
3496 | /* | |
3497 | * Only process "add" data... | |
3498 | */ | |
3499 | ||
3500 | if (event != AVAHI_BROWSER_NEW) | |
3501 | { | |
3502 | if (event == AVAHI_BROWSER_FAILURE) | |
657c5b5f | 3503 | DEBUG_printf(("cups_dnssd_query_cb: %s", avahi_strerror(avahi_client_errno(client)))); |
a29fd7dd MS |
3504 | |
3505 | return; | |
3506 | } | |
3507 | # endif /* HAVE_DNSSD */ | |
3508 | ||
dcb445bc MS |
3509 | /* |
3510 | * Lookup the service in the devices array. | |
3511 | */ | |
3512 | ||
a2187a63 | 3513 | cups_dnssd_unquote(serviceName, fullName, sizeof(serviceName)); |
dcb445bc | 3514 | |
a2187a63 | 3515 | if ((ptr = strstr(serviceName, "._")) != NULL) |
dcb445bc MS |
3516 | *ptr = '\0'; |
3517 | ||
efa72f61 | 3518 | cups_queue_name(name, serviceName, sizeof(name)); |
a2187a63 MS |
3519 | |
3520 | dkey.dest.name = name; | |
3521 | ||
3522 | if ((device = cupsArrayFind(data->devices, &dkey)) != NULL && device->state == _CUPS_DNSSD_NEW) | |
dcb445bc MS |
3523 | { |
3524 | /* | |
a29fd7dd | 3525 | * Found it, pull out the make and model from the TXT record and save it... |
dcb445bc MS |
3526 | */ |
3527 | ||
3528 | const uint8_t *txt, /* Pointer into data */ | |
3529 | *txtnext, /* Next key/value pair */ | |
3530 | *txtend; /* End of entire TXT record */ | |
3531 | uint8_t txtlen; /* Length of current key/value pair */ | |
3532 | char key[256], /* Key string */ | |
3533 | value[256], /* Value string */ | |
3534 | make_and_model[512], | |
3535 | /* Manufacturer and model */ | |
3536 | model[256], /* Model */ | |
3537 | uriname[1024], /* Name for URI */ | |
3538 | uri[1024]; /* Printer URI */ | |
f50db552 | 3539 | cups_ptype_t type = CUPS_PRINTER_DISCOVERED | CUPS_PRINTER_BW; |
a29fd7dd MS |
3540 | /* Printer type */ |
3541 | int saw_printer_type = 0; | |
3542 | /* Did we see a printer-type key? */ | |
dcb445bc MS |
3543 | |
3544 | device->state = _CUPS_DNSSD_PENDING; | |
3545 | make_and_model[0] = '\0'; | |
dcb445bc | 3546 | |
5a9febac | 3547 | strlcpy(model, "Unknown", sizeof(model)); |
dcb445bc MS |
3548 | |
3549 | for (txt = rdata, txtend = txt + rdlen; | |
3550 | txt < txtend; | |
3551 | txt = txtnext) | |
3552 | { | |
3553 | /* | |
3554 | * Read a key/value pair starting with an 8-bit length. Since the | |
3555 | * length is 8 bits and the size of the key/value buffers is 256, we | |
3556 | * don't need to check for overflow... | |
3557 | */ | |
3558 | ||
3559 | txtlen = *txt++; | |
3560 | ||
3561 | if (!txtlen || (txt + txtlen) > txtend) | |
3562 | break; | |
3563 | ||
3564 | txtnext = txt + txtlen; | |
3565 | ||
3566 | for (ptr = key; txt < txtnext && *txt != '='; txt ++) | |
7e86f2f6 | 3567 | *ptr++ = (char)*txt; |
dcb445bc MS |
3568 | *ptr = '\0'; |
3569 | ||
3570 | if (txt < txtnext && *txt == '=') | |
3571 | { | |
3572 | txt ++; | |
3573 | ||
3574 | if (txt < txtnext) | |
07623986 | 3575 | memcpy(value, txt, (size_t)(txtnext - txt)); |
dcb445bc MS |
3576 | value[txtnext - txt] = '\0'; |
3577 | ||
3578 | DEBUG_printf(("6cups_dnssd_query_cb: %s=%s", key, value)); | |
3579 | } | |
3580 | else | |
3581 | { | |
3582 | DEBUG_printf(("6cups_dnssd_query_cb: '%s' with no value.", key)); | |
3583 | continue; | |
3584 | } | |
3585 | ||
3586 | if (!_cups_strcasecmp(key, "usb_MFG") || | |
3587 | !_cups_strcasecmp(key, "usb_MANU") || | |
3588 | !_cups_strcasecmp(key, "usb_MANUFACTURER")) | |
5a9febac | 3589 | strlcpy(make_and_model, value, sizeof(make_and_model)); |
dcb445bc MS |
3590 | else if (!_cups_strcasecmp(key, "usb_MDL") || |
3591 | !_cups_strcasecmp(key, "usb_MODEL")) | |
5a9febac | 3592 | strlcpy(model, value, sizeof(model)); |
dcb445bc MS |
3593 | else if (!_cups_strcasecmp(key, "product") && !strstr(value, "Ghostscript")) |
3594 | { | |
3595 | if (value[0] == '(') | |
3596 | { | |
3597 | /* | |
3598 | * Strip parenthesis... | |
3599 | */ | |
3600 | ||
3601 | if ((ptr = value + strlen(value) - 1) > value && *ptr == ')') | |
3602 | *ptr = '\0'; | |
3603 | ||
5a9febac | 3604 | strlcpy(model, value + 1, sizeof(model)); |
dcb445bc MS |
3605 | } |
3606 | else | |
5a9febac | 3607 | strlcpy(model, value, sizeof(model)); |
dcb445bc MS |
3608 | } |
3609 | else if (!_cups_strcasecmp(key, "ty")) | |
3610 | { | |
5a9febac | 3611 | strlcpy(model, value, sizeof(model)); |
dcb445bc MS |
3612 | |
3613 | if ((ptr = strchr(model, ',')) != NULL) | |
3614 | *ptr = '\0'; | |
3615 | } | |
a29fd7dd MS |
3616 | else if (!_cups_strcasecmp(key, "note")) |
3617 | device->dest.num_options = cupsAddOption("printer-location", value, | |
3618 | device->dest.num_options, | |
3619 | &device->dest.options); | |
3620 | else if (!_cups_strcasecmp(key, "pdl")) | |
3621 | { | |
3622 | /* | |
3623 | * Look for PDF-capable printers; only PDF-capable printers are shown. | |
3624 | */ | |
3625 | ||
3626 | const char *start, *next; /* Pointer into value */ | |
46385a1a MS |
3627 | int have_pdf = 0, /* Have PDF? */ |
3628 | have_raster = 0;/* Have raster format support? */ | |
a29fd7dd MS |
3629 | |
3630 | for (start = value; start && *start; start = next) | |
3631 | { | |
46385a1a | 3632 | if (!_cups_strncasecmp(start, "application/pdf", 15) && (!start[15] || start[15] == ',')) |
a29fd7dd MS |
3633 | { |
3634 | have_pdf = 1; | |
3635 | break; | |
3636 | } | |
46385a1a MS |
3637 | else if ((!_cups_strncasecmp(start, "image/pwg-raster", 16) && (!start[16] || start[16] == ',')) || |
3638 | (!_cups_strncasecmp(start, "image/urf", 9) && (!start[9] || start[9] == ','))) | |
3639 | { | |
3640 | have_raster = 1; | |
3641 | break; | |
3642 | } | |
a29fd7dd MS |
3643 | |
3644 | if ((next = strchr(start, ',')) != NULL) | |
3645 | next ++; | |
3646 | } | |
3647 | ||
46385a1a | 3648 | if (!have_pdf && !have_raster) |
a29fd7dd MS |
3649 | device->state = _CUPS_DNSSD_INCOMPATIBLE; |
3650 | } | |
dcb445bc MS |
3651 | else if (!_cups_strcasecmp(key, "printer-type")) |
3652 | { | |
a29fd7dd MS |
3653 | /* |
3654 | * Value is either NNNN or 0xXXXX | |
3655 | */ | |
3656 | ||
3657 | saw_printer_type = 1; | |
f50db552 | 3658 | type = (cups_ptype_t)strtol(value, NULL, 0) | CUPS_PRINTER_DISCOVERED; |
a29fd7dd MS |
3659 | } |
3660 | else if (!saw_printer_type) | |
3661 | { | |
3662 | if (!_cups_strcasecmp(key, "air") && | |
3663 | !_cups_strcasecmp(value, "t")) | |
3664 | type |= CUPS_PRINTER_AUTHENTICATED; | |
3665 | else if (!_cups_strcasecmp(key, "bind") && | |
3666 | !_cups_strcasecmp(value, "t")) | |
3667 | type |= CUPS_PRINTER_BIND; | |
3668 | else if (!_cups_strcasecmp(key, "collate") && | |
3669 | !_cups_strcasecmp(value, "t")) | |
3670 | type |= CUPS_PRINTER_COLLATE; | |
3671 | else if (!_cups_strcasecmp(key, "color") && | |
3672 | !_cups_strcasecmp(value, "t")) | |
3673 | type |= CUPS_PRINTER_COLOR; | |
3674 | else if (!_cups_strcasecmp(key, "copies") && | |
3675 | !_cups_strcasecmp(value, "t")) | |
3676 | type |= CUPS_PRINTER_COPIES; | |
3677 | else if (!_cups_strcasecmp(key, "duplex") && | |
3678 | !_cups_strcasecmp(value, "t")) | |
3679 | type |= CUPS_PRINTER_DUPLEX; | |
3680 | else if (!_cups_strcasecmp(key, "fax") && | |
3681 | !_cups_strcasecmp(value, "t")) | |
3682 | type |= CUPS_PRINTER_MFP; | |
3683 | else if (!_cups_strcasecmp(key, "papercustom") && | |
3684 | !_cups_strcasecmp(value, "t")) | |
3685 | type |= CUPS_PRINTER_VARIABLE; | |
3686 | else if (!_cups_strcasecmp(key, "papermax")) | |
3687 | { | |
3688 | if (!_cups_strcasecmp(value, "legal-a4")) | |
3689 | type |= CUPS_PRINTER_SMALL; | |
3690 | else if (!_cups_strcasecmp(value, "isoc-a2")) | |
3691 | type |= CUPS_PRINTER_MEDIUM; | |
3692 | else if (!_cups_strcasecmp(value, ">isoc-a2")) | |
3693 | type |= CUPS_PRINTER_LARGE; | |
3694 | } | |
3695 | else if (!_cups_strcasecmp(key, "punch") && | |
3696 | !_cups_strcasecmp(value, "t")) | |
3697 | type |= CUPS_PRINTER_PUNCH; | |
3698 | else if (!_cups_strcasecmp(key, "scan") && | |
3699 | !_cups_strcasecmp(value, "t")) | |
3700 | type |= CUPS_PRINTER_MFP; | |
3701 | else if (!_cups_strcasecmp(key, "sort") && | |
3702 | !_cups_strcasecmp(value, "t")) | |
3703 | type |= CUPS_PRINTER_SORT; | |
3704 | else if (!_cups_strcasecmp(key, "staple") && | |
3705 | !_cups_strcasecmp(value, "t")) | |
3706 | type |= CUPS_PRINTER_STAPLE; | |
dcb445bc MS |
3707 | } |
3708 | } | |
3709 | ||
3710 | /* | |
a29fd7dd | 3711 | * Save the printer-xxx values... |
dcb445bc MS |
3712 | */ |
3713 | ||
3714 | if (make_and_model[0]) | |
3715 | { | |
3716 | strlcat(make_and_model, " ", sizeof(make_and_model)); | |
3717 | strlcat(make_and_model, model, sizeof(make_and_model)); | |
3718 | ||
46385a1a | 3719 | device->dest.num_options = cupsAddOption("printer-make-and-model", make_and_model, device->dest.num_options, &device->dest.options); |
dcb445bc MS |
3720 | } |
3721 | else | |
46385a1a | 3722 | device->dest.num_options = cupsAddOption("printer-make-and-model", model, device->dest.num_options, &device->dest.options); |
dcb445bc | 3723 | |
a29fd7dd MS |
3724 | device->type = type; |
3725 | snprintf(value, sizeof(value), "%u", type); | |
46385a1a | 3726 | device->dest.num_options = cupsAddOption("printer-type", value, device->dest.num_options, &device->dest.options); |
a29fd7dd | 3727 | |
dcb445bc MS |
3728 | /* |
3729 | * Save the URI... | |
3730 | */ | |
3731 | ||
3732 | cups_dnssd_unquote(uriname, device->fullName, sizeof(uriname)); | |
3733 | httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), | |
3734 | !strcmp(device->regtype, "_ipps._tcp") ? "ipps" : "ipp", | |
a29fd7dd | 3735 | NULL, uriname, 0, saw_printer_type ? "/cups" : "/"); |
dcb445bc | 3736 | |
46385a1a | 3737 | DEBUG_printf(("6cups_dnssd_query: device-uri=\"%s\"", uri)); |
dcb445bc | 3738 | |
46385a1a | 3739 | device->dest.num_options = cupsAddOption("device-uri", uri, device->dest.num_options, &device->dest.options); |
dcb445bc MS |
3740 | } |
3741 | else | |
3742 | DEBUG_printf(("6cups_dnssd_query: Ignoring TXT record for '%s'.", | |
3743 | fullName)); | |
3744 | } | |
3745 | ||
3746 | ||
3747 | /* | |
3748 | * 'cups_dnssd_resolve()' - Resolve a Bonjour printer URI. | |
3749 | */ | |
3750 | ||
3751 | static const char * /* O - Resolved URI or NULL */ | |
3752 | cups_dnssd_resolve( | |
3753 | cups_dest_t *dest, /* I - Destination */ | |
3754 | const char *uri, /* I - Current printer URI */ | |
3755 | int msec, /* I - Time in milliseconds */ | |
3756 | int *cancel, /* I - Pointer to "cancel" variable */ | |
3757 | cups_dest_cb_t cb, /* I - Callback */ | |
3758 | void *user_data) /* I - User data for callback */ | |
3759 | { | |
3760 | char tempuri[1024]; /* Temporary URI buffer */ | |
3761 | _cups_dnssd_resolve_t resolve; /* Resolve data */ | |
3762 | ||
3763 | ||
3764 | /* | |
3765 | * Resolve the URI... | |
3766 | */ | |
3767 | ||
3768 | resolve.cancel = cancel; | |
3769 | gettimeofday(&resolve.end_time, NULL); | |
3770 | if (msec > 0) | |
3771 | { | |
3772 | resolve.end_time.tv_sec += msec / 1000; | |
3773 | resolve.end_time.tv_usec += (msec % 1000) * 1000; | |
3774 | ||
3775 | while (resolve.end_time.tv_usec >= 1000000) | |
3776 | { | |
3777 | resolve.end_time.tv_sec ++; | |
3778 | resolve.end_time.tv_usec -= 1000000; | |
3779 | } | |
3780 | } | |
3781 | else | |
3782 | resolve.end_time.tv_sec += 75; | |
3783 | ||
3784 | if (cb) | |
4db7fcee | 3785 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, dest); |
dcb445bc | 3786 | |
c585706d | 3787 | if ((uri = _httpResolveURI(uri, tempuri, sizeof(tempuri), _HTTP_RESOLVE_DEFAULT, cups_dnssd_resolve_cb, &resolve)) == NULL) |
dcb445bc | 3788 | { |
f5f2e19e | 3789 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to resolve printer-uri."), 1); |
dcb445bc MS |
3790 | |
3791 | if (cb) | |
46385a1a | 3792 | (*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_ERROR, dest); |
dcb445bc MS |
3793 | |
3794 | return (NULL); | |
3795 | } | |
3796 | ||
3797 | /* | |
3798 | * Save the resolved URI... | |
3799 | */ | |
3800 | ||
c9a0ff62 | 3801 | dest->num_options = cupsAddOption("device-uri", uri, dest->num_options, &dest->options); |
dcb445bc | 3802 | |
c9a0ff62 | 3803 | return (cupsGetOption("device-uri", dest->num_options, dest->options)); |
dcb445bc MS |
3804 | } |
3805 | ||
3806 | ||
3807 | /* | |
3808 | * 'cups_dnssd_resolve_cb()' - See if we should continue resolving. | |
3809 | */ | |
3810 | ||
3811 | static int /* O - 1 to continue, 0 to stop */ | |
3812 | cups_dnssd_resolve_cb(void *context) /* I - Resolve data */ | |
3813 | { | |
3814 | _cups_dnssd_resolve_t *resolve = (_cups_dnssd_resolve_t *)context; | |
3815 | /* Resolve data */ | |
3816 | struct timeval curtime; /* Current time */ | |
3817 | ||
3818 | ||
3819 | /* | |
3820 | * If the cancel variable is set, return immediately. | |
3821 | */ | |
3822 | ||
4db7fcee MS |
3823 | if (resolve->cancel && *(resolve->cancel)) |
3824 | { | |
3825 | DEBUG_puts("4cups_dnssd_resolve_cb: Canceled."); | |
dcb445bc | 3826 | return (0); |
4db7fcee | 3827 | } |
dcb445bc MS |
3828 | |
3829 | /* | |
3830 | * Otherwise check the end time... | |
3831 | */ | |
3832 | ||
3833 | gettimeofday(&curtime, NULL); | |
3834 | ||
8ddb0f60 | 3835 | 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)); |
4db7fcee MS |
3836 | |
3837 | return (curtime.tv_sec < resolve->end_time.tv_sec || | |
dcb445bc | 3838 | (curtime.tv_sec == resolve->end_time.tv_sec && |
4db7fcee | 3839 | curtime.tv_usec < resolve->end_time.tv_usec)); |
dcb445bc MS |
3840 | } |
3841 | ||
3842 | ||
3843 | /* | |
3844 | * 'cups_dnssd_unquote()' - Unquote a name string. | |
3845 | */ | |
3846 | ||
3847 | static void | |
3848 | cups_dnssd_unquote(char *dst, /* I - Destination buffer */ | |
3849 | const char *src, /* I - Source string */ | |
3850 | size_t dstsize) /* I - Size of destination buffer */ | |
3851 | { | |
3852 | char *dstend = dst + dstsize - 1; /* End of destination buffer */ | |
3853 | ||
3854 | ||
3855 | while (*src && dst < dstend) | |
3856 | { | |
3857 | if (*src == '\\') | |
3858 | { | |
3859 | src ++; | |
3860 | if (isdigit(src[0] & 255) && isdigit(src[1] & 255) && | |
3861 | isdigit(src[2] & 255)) | |
3862 | { | |
3863 | *dst++ = ((((src[0] - '0') * 10) + src[1] - '0') * 10) + src[2] - '0'; | |
3864 | src += 3; | |
3865 | } | |
3866 | else | |
3867 | *dst++ = *src++; | |
3868 | } | |
3869 | else | |
3870 | *dst++ = *src ++; | |
3871 | } | |
3872 | ||
3873 | *dst = '\0'; | |
3874 | } | |
3875 | #endif /* HAVE_DNSSD */ | |
3876 | ||
3877 | ||
f34c1c99 | 3878 | #if defined(HAVE_AVAHI) || defined(HAVE_DNSSD) |
3fae3b33 MS |
3879 | /* |
3880 | * 'cups_elapsed()' - Return the elapsed time in milliseconds. | |
3881 | */ | |
3882 | ||
3883 | static int /* O - Elapsed time in milliseconds */ | |
3884 | cups_elapsed(struct timeval *t) /* IO - Previous time */ | |
3885 | { | |
3886 | int msecs; /* Milliseconds */ | |
3887 | struct timeval nt; /* New time */ | |
3888 | ||
3889 | ||
3890 | gettimeofday(&nt, NULL); | |
3891 | ||
2a75f21b | 3892 | msecs = (int)(1000 * (nt.tv_sec - t->tv_sec) + (nt.tv_usec - t->tv_usec) / 1000); |
3fae3b33 MS |
3893 | |
3894 | *t = nt; | |
3895 | ||
3896 | return (msecs); | |
3897 | } | |
f34c1c99 | 3898 | #endif /* HAVE_AVAHI || HAVE_DNSSD */ |
3fae3b33 | 3899 | |
798d6e29 | 3900 | |
426c6a59 MS |
3901 | /* |
3902 | * 'cups_find_dest()' - Find a destination using a binary search. | |
3903 | */ | |
3904 | ||
3905 | static int /* O - Index of match */ | |
3906 | cups_find_dest(const char *name, /* I - Destination name */ | |
3907 | const char *instance, /* I - Instance or NULL */ | |
3908 | int num_dests, /* I - Number of destinations */ | |
3909 | cups_dest_t *dests, /* I - Destinations */ | |
3910 | int prev, /* I - Previous index */ | |
3911 | int *rdiff) /* O - Difference of match */ | |
3912 | { | |
3913 | int left, /* Low mark for binary search */ | |
3914 | right, /* High mark for binary search */ | |
3915 | current, /* Current index */ | |
3916 | diff; /* Result of comparison */ | |
3917 | cups_dest_t key; /* Search key */ | |
3918 | ||
3919 | ||
3920 | key.name = (char *)name; | |
3921 | key.instance = (char *)instance; | |
3922 | ||
3923 | if (prev >= 0) | |
3924 | { | |
3925 | /* | |
3926 | * Start search on either side of previous... | |
3927 | */ | |
3928 | ||
3929 | if ((diff = cups_compare_dests(&key, dests + prev)) == 0 || | |
3930 | (diff < 0 && prev == 0) || | |
3931 | (diff > 0 && prev == (num_dests - 1))) | |
3932 | { | |
3933 | *rdiff = diff; | |
3934 | return (prev); | |
3935 | } | |
3936 | else if (diff < 0) | |
3937 | { | |
3938 | /* | |
3939 | * Start with previous on right side... | |
3940 | */ | |
3941 | ||
3942 | left = 0; | |
3943 | right = prev; | |
3944 | } | |
3945 | else | |
3946 | { | |
3947 | /* | |
3948 | * Start wih previous on left side... | |
3949 | */ | |
3950 | ||
3951 | left = prev; | |
3952 | right = num_dests - 1; | |
3953 | } | |
3954 | } | |
3955 | else | |
3956 | { | |
3957 | /* | |
3958 | * Start search in the middle... | |
3959 | */ | |
3960 | ||
3961 | left = 0; | |
3962 | right = num_dests - 1; | |
3963 | } | |
3964 | ||
3965 | do | |
3966 | { | |
3967 | current = (left + right) / 2; | |
3968 | diff = cups_compare_dests(&key, dests + current); | |
3969 | ||
3970 | if (diff == 0) | |
3971 | break; | |
3972 | else if (diff < 0) | |
3973 | right = current; | |
3974 | else | |
3975 | left = current; | |
3976 | } | |
3977 | while ((right - left) > 1); | |
3978 | ||
3979 | if (diff != 0) | |
3980 | { | |
3981 | /* | |
3982 | * Check the last 1 or 2 elements... | |
3983 | */ | |
3984 | ||
3985 | if ((diff = cups_compare_dests(&key, dests + left)) <= 0) | |
3986 | current = left; | |
3987 | else | |
3988 | { | |
3989 | diff = cups_compare_dests(&key, dests + right); | |
3990 | current = right; | |
3991 | } | |
3992 | } | |
3993 | ||
3994 | /* | |
3995 | * Return the closest destination and the difference... | |
3996 | */ | |
3997 | ||
3998 | *rdiff = diff; | |
3999 | ||
4000 | return (current); | |
4001 | } | |
4002 | ||
4003 | ||
7536de1a MS |
4004 | /* |
4005 | * 'cups_get_cb()' - Collect enumerated destinations. | |
4006 | */ | |
4007 | ||
4008 | static int /* O - 1 to continue, 0 to stop */ | |
4009 | cups_get_cb(_cups_getdata_t *data, /* I - Data from cupsGetDests */ | |
4010 | unsigned flags, /* I - Enumeration flags */ | |
4011 | cups_dest_t *dest) /* I - Destination */ | |
4012 | { | |
4013 | if (flags & CUPS_DEST_FLAGS_REMOVED) | |
4014 | { | |
4015 | /* | |
4016 | * Remove destination from array... | |
4017 | */ | |
4018 | ||
4019 | data->num_dests = cupsRemoveDest(dest->name, dest->instance, data->num_dests, &data->dests); | |
4020 | } | |
4021 | else | |
4022 | { | |
4023 | /* | |
4024 | * Add destination to array... | |
4025 | */ | |
4026 | ||
4027 | data->num_dests = cupsCopyDest(dest, data->num_dests, &data->dests); | |
4028 | } | |
4029 | ||
4030 | return (1); | |
4031 | } | |
4032 | ||
4033 | ||
a4924f6c MS |
4034 | /* |
4035 | * 'cups_get_default()' - Get the default destination from an lpoptions file. | |
4036 | */ | |
4037 | ||
080811b1 | 4038 | static char * /* O - Default destination or NULL */ |
a4924f6c MS |
4039 | cups_get_default(const char *filename, /* I - File to read */ |
4040 | char *namebuf, /* I - Name buffer */ | |
4041 | size_t namesize, /* I - Size of name buffer */ | |
4042 | const char **instance) /* I - Instance */ | |
4043 | { | |
4044 | cups_file_t *fp; /* lpoptions file */ | |
4045 | char line[8192], /* Line from file */ | |
4046 | *value, /* Value for line */ | |
4047 | *nameptr; /* Pointer into name */ | |
88f9aafc | 4048 | int linenum; /* Current line */ |
a4924f6c MS |
4049 | |
4050 | ||
4051 | *namebuf = '\0'; | |
4052 | ||
4053 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
4054 | { | |
4055 | linenum = 0; | |
4056 | ||
4057 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
4058 | { | |
88f9aafc | 4059 | if (!_cups_strcasecmp(line, "default") && value) |
a4924f6c MS |
4060 | { |
4061 | strlcpy(namebuf, value, namesize); | |
4062 | ||
4063 | if ((nameptr = strchr(namebuf, ' ')) != NULL) | |
4064 | *nameptr = '\0'; | |
4065 | if ((nameptr = strchr(namebuf, '\t')) != NULL) | |
4066 | *nameptr = '\0'; | |
4067 | ||
4068 | if ((nameptr = strchr(namebuf, '/')) != NULL) | |
4069 | *nameptr++ = '\0'; | |
4070 | ||
4071 | *instance = nameptr; | |
4072 | break; | |
4073 | } | |
4074 | } | |
4075 | ||
4076 | cupsFileClose(fp); | |
4077 | } | |
4078 | ||
4079 | return (*namebuf ? namebuf : NULL); | |
4080 | } | |
4081 | ||
4082 | ||
ef416fc2 | 4083 | /* |
4084 | * 'cups_get_dests()' - Get destinations from a file. | |
4085 | */ | |
4086 | ||
4087 | static int /* O - Number of destinations */ | |
38e73f87 MS |
4088 | cups_get_dests( |
4089 | const char *filename, /* I - File to read from */ | |
4090 | const char *match_name, /* I - Destination name we want */ | |
4091 | const char *match_inst, /* I - Instance name we want */ | |
4092 | int user_default_set, /* I - User default printer set? */ | |
4093 | int num_dests, /* I - Number of destinations */ | |
4094 | cups_dest_t **dests) /* IO - Destinations */ | |
ef416fc2 | 4095 | { |
4096 | int i; /* Looping var */ | |
4097 | cups_dest_t *dest; /* Current destination */ | |
a4924f6c | 4098 | cups_file_t *fp; /* File pointer */ |
ef416fc2 | 4099 | char line[8192], /* Line from file */ |
4100 | *lineptr, /* Pointer into line */ | |
4101 | *name, /* Name of destination/option */ | |
4102 | *instance; /* Instance of destination */ | |
a4924f6c | 4103 | int linenum; /* Current line number */ |
ef416fc2 | 4104 | |
4105 | ||
807315e6 | 4106 | 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)); |
a4924f6c MS |
4107 | |
4108 | /* | |
4109 | * Try to open the file... | |
4110 | */ | |
4111 | ||
4112 | if ((fp = cupsFileOpen(filename, "r")) == NULL) | |
4113 | return (num_dests); | |
4114 | ||
ef416fc2 | 4115 | /* |
4116 | * Read each printer; each line looks like: | |
4117 | * | |
4118 | * Dest name[/instance] options | |
4119 | * Default name[/instance] options | |
4120 | */ | |
4121 | ||
a4924f6c MS |
4122 | linenum = 0; |
4123 | ||
4124 | while (cupsFileGetConf(fp, line, sizeof(line), &lineptr, &linenum)) | |
ef416fc2 | 4125 | { |
4126 | /* | |
4127 | * See what type of line it is... | |
4128 | */ | |
4129 | ||
e07d4801 | 4130 | DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"", |
38e73f87 | 4131 | linenum, line, lineptr)); |
ef416fc2 | 4132 | |
88f9aafc | 4133 | if ((_cups_strcasecmp(line, "dest") && _cups_strcasecmp(line, "default")) || !lineptr) |
a4924f6c | 4134 | { |
e07d4801 | 4135 | DEBUG_puts("9cups_get_dests: Not a dest or default line..."); |
ef416fc2 | 4136 | continue; |
a4924f6c | 4137 | } |
ef416fc2 | 4138 | |
4139 | name = lineptr; | |
4140 | ||
4141 | /* | |
4142 | * Search for an instance... | |
4143 | */ | |
4144 | ||
4145 | while (!isspace(*lineptr & 255) && *lineptr && *lineptr != '/') | |
4146 | lineptr ++; | |
4147 | ||
ef416fc2 | 4148 | if (*lineptr == '/') |
4149 | { | |
4150 | /* | |
4151 | * Found an instance... | |
4152 | */ | |
4153 | ||
4154 | *lineptr++ = '\0'; | |
4155 | instance = lineptr; | |
4156 | ||
4157 | /* | |
4158 | * Search for an instance... | |
4159 | */ | |
4160 | ||
4161 | while (!isspace(*lineptr & 255) && *lineptr) | |
4162 | lineptr ++; | |
4163 | } | |
4164 | else | |
4165 | instance = NULL; | |
4166 | ||
a4924f6c MS |
4167 | if (*lineptr) |
4168 | *lineptr++ = '\0'; | |
4169 | ||
e07d4801 | 4170 | DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name, |
a4924f6c | 4171 | instance)); |
ef416fc2 | 4172 | |
4173 | /* | |
4174 | * See if the primary instance of the destination exists; if not, | |
4175 | * ignore this entry and move on... | |
4176 | */ | |
4177 | ||
a4924f6c MS |
4178 | if (match_name) |
4179 | { | |
88f9aafc | 4180 | if (_cups_strcasecmp(name, match_name) || |
a4924f6c MS |
4181 | (!instance && match_inst) || |
4182 | (instance && !match_inst) || | |
88f9aafc | 4183 | (instance && _cups_strcasecmp(instance, match_inst))) |
a4924f6c | 4184 | continue; |
ef416fc2 | 4185 | |
a4924f6c MS |
4186 | dest = *dests; |
4187 | } | |
4188 | else if (cupsGetDest(name, NULL, num_dests, *dests) == NULL) | |
4189 | { | |
e07d4801 | 4190 | DEBUG_puts("9cups_get_dests: Not found!"); |
a4924f6c MS |
4191 | continue; |
4192 | } | |
4193 | else | |
ef416fc2 | 4194 | { |
4195 | /* | |
a4924f6c | 4196 | * Add the destination... |
ef416fc2 | 4197 | */ |
4198 | ||
a4924f6c MS |
4199 | num_dests = cupsAddDest(name, instance, num_dests, dests); |
4200 | ||
4201 | if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL) | |
4202 | { | |
4203 | /* | |
4204 | * Out of memory! | |
4205 | */ | |
4206 | ||
e07d4801 | 4207 | DEBUG_puts("9cups_get_dests: Out of memory!"); |
a4924f6c MS |
4208 | break; |
4209 | } | |
ef416fc2 | 4210 | } |
4211 | ||
4212 | /* | |
4213 | * Add options until we hit the end of the line... | |
4214 | */ | |
4215 | ||
4216 | dest->num_options = cupsParseOptions(lineptr, dest->num_options, | |
4217 | &(dest->options)); | |
4218 | ||
a4924f6c MS |
4219 | /* |
4220 | * If we found what we were looking for, stop now... | |
4221 | */ | |
4222 | ||
4223 | if (match_name) | |
4224 | break; | |
4225 | ||
ef416fc2 | 4226 | /* |
4227 | * Set this as default if needed... | |
4228 | */ | |
4229 | ||
88f9aafc | 4230 | if (!user_default_set && !_cups_strcasecmp(line, "default")) |
ef416fc2 | 4231 | { |
e07d4801 | 4232 | DEBUG_puts("9cups_get_dests: Setting as default..."); |
a4924f6c | 4233 | |
ef416fc2 | 4234 | for (i = 0; i < num_dests; i ++) |
4235 | (*dests)[i].is_default = 0; | |
4236 | ||
4237 | dest->is_default = 1; | |
4238 | } | |
4239 | } | |
4240 | ||
4241 | /* | |
4242 | * Close the file and return... | |
4243 | */ | |
4244 | ||
88f9aafc | 4245 | cupsFileClose(fp); |
ef416fc2 | 4246 | |
4247 | return (num_dests); | |
4248 | } | |
4249 | ||
4250 | ||
426c6a59 MS |
4251 | /* |
4252 | * 'cups_make_string()' - Make a comma-separated string of values from an IPP | |
4253 | * attribute. | |
4254 | */ | |
4255 | ||
4256 | static char * /* O - New string */ | |
4257 | cups_make_string( | |
4258 | ipp_attribute_t *attr, /* I - Attribute to convert */ | |
4259 | char *buffer, /* I - Buffer */ | |
4260 | size_t bufsize) /* I - Size of buffer */ | |
4261 | { | |
4262 | int i; /* Looping var */ | |
4263 | char *ptr, /* Pointer into buffer */ | |
4264 | *end, /* Pointer to end of buffer */ | |
4265 | *valptr; /* Pointer into string attribute */ | |
4266 | ||
4267 | ||
4268 | /* | |
4269 | * Return quickly if we have a single string value... | |
4270 | */ | |
4271 | ||
4272 | if (attr->num_values == 1 && | |
4273 | attr->value_tag != IPP_TAG_INTEGER && | |
4274 | attr->value_tag != IPP_TAG_ENUM && | |
4275 | attr->value_tag != IPP_TAG_BOOLEAN && | |
4276 | attr->value_tag != IPP_TAG_RANGE) | |
4277 | return (attr->values[0].string.text); | |
4278 | ||
4279 | /* | |
4280 | * Copy the values to the string, separating with commas and escaping strings | |
4281 | * as needed... | |
4282 | */ | |
4283 | ||
4284 | end = buffer + bufsize - 1; | |
4285 | ||
4286 | for (i = 0, ptr = buffer; i < attr->num_values && ptr < end; i ++) | |
4287 | { | |
4288 | if (i) | |
4289 | *ptr++ = ','; | |
4290 | ||
4291 | switch (attr->value_tag) | |
4292 | { | |
4293 | case IPP_TAG_INTEGER : | |
4294 | case IPP_TAG_ENUM : | |
07623986 | 4295 | snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].integer); |
426c6a59 MS |
4296 | break; |
4297 | ||
4298 | case IPP_TAG_BOOLEAN : | |
4299 | if (attr->values[i].boolean) | |
07623986 | 4300 | strlcpy(ptr, "true", (size_t)(end - ptr + 1)); |
426c6a59 | 4301 | else |
07623986 | 4302 | strlcpy(ptr, "false", (size_t)(end - ptr + 1)); |
426c6a59 MS |
4303 | break; |
4304 | ||
4305 | case IPP_TAG_RANGE : | |
4306 | if (attr->values[i].range.lower == attr->values[i].range.upper) | |
07623986 | 4307 | snprintf(ptr, (size_t)(end - ptr + 1), "%d", attr->values[i].range.lower); |
426c6a59 | 4308 | else |
07623986 | 4309 | snprintf(ptr, (size_t)(end - ptr + 1), "%d-%d", attr->values[i].range.lower, attr->values[i].range.upper); |
426c6a59 MS |
4310 | break; |
4311 | ||
4312 | default : | |
4313 | for (valptr = attr->values[i].string.text; | |
4314 | *valptr && ptr < end;) | |
4315 | { | |
4316 | if (strchr(" \t\n\\\'\"", *valptr)) | |
4317 | { | |
4318 | if (ptr >= (end - 1)) | |
4319 | break; | |
4320 | ||
4321 | *ptr++ = '\\'; | |
4322 | } | |
4323 | ||
4324 | *ptr++ = *valptr++; | |
4325 | } | |
4326 | ||
4327 | *ptr = '\0'; | |
4328 | break; | |
4329 | } | |
4330 | ||
4331 | ptr += strlen(ptr); | |
4332 | } | |
4333 | ||
4334 | *ptr = '\0'; | |
4335 | ||
4336 | return (buffer); | |
4337 | } | |
efa72f61 MS |
4338 | |
4339 | ||
4a366251 MS |
4340 | /* |
4341 | * 'cups_name_cb()' - Find an enumerated destination. | |
4342 | */ | |
4343 | ||
4344 | static int /* O - 1 to continue, 0 to stop */ | |
4345 | cups_name_cb(_cups_namedata_t *data, /* I - Data from cupsGetNamedDest */ | |
4346 | unsigned flags, /* I - Enumeration flags */ | |
4347 | cups_dest_t *dest) /* I - Destination */ | |
4348 | { | |
2a75f21b | 4349 | DEBUG_printf(("2cups_name_cb(data=%p(%s), flags=%x, dest=%p(%s)", (void *)data, data->name, flags, (void *)dest, dest->name)); |
4a366251 MS |
4350 | |
4351 | if (!(flags & CUPS_DEST_FLAGS_REMOVED) && !dest->instance && !strcasecmp(data->name, dest->name)) | |
4352 | { | |
4353 | /* | |
4354 | * Copy destination and stop enumeration... | |
4355 | */ | |
4356 | ||
4357 | cupsCopyDest(dest, 0, &data->dest); | |
4358 | return (0); | |
4359 | } | |
4360 | ||
4361 | return (1); | |
4362 | } | |
4363 | ||
4364 | ||
efa72f61 MS |
4365 | /* |
4366 | * 'cups_queue_name()' - Create a local queue name based on the service name. | |
4367 | */ | |
4368 | ||
4369 | static void | |
4370 | cups_queue_name( | |
4371 | char *name, /* I - Name buffer */ | |
4372 | const char *serviceName, /* I - Service name */ | |
4373 | size_t namesize) /* I - Size of name buffer */ | |
4374 | { | |
4375 | const char *ptr; /* Pointer into serviceName */ | |
4376 | char *nameptr; /* Pointer into name */ | |
4377 | ||
4378 | ||
4379 | for (nameptr = name, ptr = serviceName; *ptr && nameptr < (name + namesize - 1); ptr ++) | |
4380 | { | |
4381 | /* | |
4382 | * Sanitize the printer name... | |
4383 | */ | |
4384 | ||
4385 | if (_cups_isalnum(*ptr)) | |
4386 | *nameptr++ = *ptr; | |
4387 | else if (nameptr == name || nameptr[-1] != '_') | |
4388 | *nameptr++ = '_'; | |
4389 | } | |
4390 | ||
4391 | *nameptr = '\0'; | |
4392 | } |