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