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