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