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