]>
Commit | Line | Data |
---|---|---|
7a14d768 MS |
1 | /* |
2 | * "$Id$" | |
3 | * | |
4 | * DNS-SD discovery backend for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2008 by Apple Inc. | |
7a14d768 MS |
7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Apple Inc. and are protected by Federal copyright | |
10 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
11 | * "LICENSE" which should have been included with this file. If this | |
12 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
13 | * | |
14 | * This file is subject to the Apple OS-Developed Software exception. | |
15 | * | |
16 | * Contents: | |
17 | * | |
18 | * main() - Browse for printers. | |
19 | * browse_callback() - Browse devices. | |
20 | * browse_local_callback() - Browse local devices. | |
21 | * compare_devices() - Compare two devices. | |
22 | * get_device() - Create or update a device. | |
23 | * query_callback() - Process query data. | |
24 | * unquote() - Unquote a name string. | |
25 | */ | |
26 | ||
27 | /* | |
28 | * Include necessary headers. | |
29 | */ | |
30 | ||
31 | #include "backend-private.h" | |
32 | #include <cups/array.h> | |
33 | #include <dns_sd.h> | |
34 | ||
35 | ||
36 | /* | |
37 | * Device structure... | |
38 | */ | |
39 | ||
40 | typedef enum | |
41 | { | |
42 | CUPS_DEVICE_PRINTER = 0, /* lpd://... */ | |
43 | CUPS_DEVICE_IPP, /* ipp://... */ | |
44 | CUPS_DEVICE_FAX_IPP, /* ipp://... */ | |
45 | CUPS_DEVICE_PDL_DATASTREAM, /* socket://... */ | |
46 | CUPS_DEVICE_RIOUSBPRINT /* riousbprint://... */ | |
47 | } cups_devtype_t; | |
48 | ||
49 | ||
50 | typedef struct | |
51 | { | |
52 | DNSServiceRef ref; /* Service reference for resolve */ | |
53 | char *name, /* Service name */ | |
54 | *domain, /* Domain name */ | |
55 | *fullName, /* Full name */ | |
56 | *make_and_model; /* Make and model from TXT record */ | |
57 | cups_devtype_t type; /* Device registration type */ | |
1f0275e3 MS |
58 | int priority, /* Priority associated with type */ |
59 | cups_shared, /* CUPS shared printer? */ | |
7a14d768 MS |
60 | sent; /* Did we list the device? */ |
61 | } cups_device_t; | |
62 | ||
63 | ||
64 | /* | |
65 | * Local functions... | |
66 | */ | |
67 | ||
68 | static void browse_callback(DNSServiceRef sdRef, | |
69 | DNSServiceFlags flags, | |
70 | uint32_t interfaceIndex, | |
71 | DNSServiceErrorType errorCode, | |
72 | const char *serviceName, | |
73 | const char *regtype, | |
74 | const char *replyDomain, void *context); | |
75 | static void browse_local_callback(DNSServiceRef sdRef, | |
76 | DNSServiceFlags flags, | |
77 | uint32_t interfaceIndex, | |
78 | DNSServiceErrorType errorCode, | |
79 | const char *serviceName, | |
80 | const char *regtype, | |
81 | const char *replyDomain, | |
82 | void *context); | |
83 | static int compare_devices(cups_device_t *a, cups_device_t *b); | |
84 | static void exec_backend(char **argv); | |
85 | static cups_device_t *get_device(cups_array_t *devices, | |
86 | const char *serviceName, | |
87 | const char *regtype, | |
88 | const char *replyDomain); | |
89 | static void query_callback(DNSServiceRef sdRef, | |
90 | DNSServiceFlags flags, | |
91 | uint32_t interfaceIndex, | |
92 | DNSServiceErrorType errorCode, | |
93 | const char *fullName, uint16_t rrtype, | |
94 | uint16_t rrclass, uint16_t rdlen, | |
95 | const void *rdata, uint32_t ttl, | |
96 | void *context); | |
97 | static void unquote(char *dst, const char *src, size_t dstsize); | |
98 | ||
99 | ||
100 | /* | |
101 | * 'main()' - Browse for printers. | |
102 | */ | |
103 | ||
104 | int /* O - Exit status */ | |
105 | main(int argc, /* I - Number of command-line args */ | |
106 | char *argv[]) /* I - Command-line arguments */ | |
107 | { | |
c5571a1d | 108 | const char *name; /* Backend name */ |
7a14d768 MS |
109 | DNSServiceRef main_ref, /* Main service reference */ |
110 | fax_ipp_ref, /* IPP fax service reference */ | |
111 | ipp_ref, /* IPP service reference */ | |
112 | ipp_tls_ref, /* IPP w/TLS service reference */ | |
113 | local_fax_ipp_ref, /* Local IPP fax service reference */ | |
114 | local_ipp_ref, /* Local IPP service reference */ | |
115 | local_ipp_tls_ref, /* Local IPP w/TLS service reference */ | |
116 | local_printer_ref, /* Local LPD service reference */ | |
117 | pdl_datastream_ref, /* AppSocket service reference */ | |
118 | printer_ref, /* LPD service reference */ | |
119 | riousbprint_ref; /* Remote IO service reference */ | |
120 | int fd; /* Main file descriptor */ | |
121 | fd_set input; /* Input set for select() */ | |
122 | struct timeval timeout; /* Timeout for select() */ | |
123 | cups_array_t *devices; /* Device array */ | |
124 | cups_device_t *device; /* Current device */ | |
125 | ||
126 | ||
127 | /* | |
128 | * Check command-line... | |
129 | */ | |
130 | ||
131 | setbuf(stderr, NULL); | |
132 | ||
133 | if (argc >= 6) | |
134 | exec_backend(argv); | |
135 | else if (argc != 1) | |
136 | { | |
c5571a1d MS |
137 | fprintf(stderr, "Usage: %s job user title copies options [filename(s)]\n", |
138 | argv[0]); | |
7a14d768 MS |
139 | return (1); |
140 | } | |
141 | ||
c5571a1d MS |
142 | /* |
143 | * Only do discovery when run as "dnssd"... | |
144 | */ | |
145 | ||
146 | if ((name = strrchr(argv[0], '/')) != NULL) | |
147 | name ++; | |
148 | else | |
149 | name = argv[0]; | |
150 | ||
151 | if (strcmp(name, "dnssd")) | |
152 | return (0); | |
153 | ||
7a14d768 MS |
154 | /* |
155 | * Create an array to track devices... | |
156 | */ | |
157 | ||
158 | devices = cupsArrayNew((cups_array_func_t)compare_devices, NULL); | |
159 | ||
160 | /* | |
161 | * Browse for different kinds of printers... | |
162 | */ | |
163 | ||
164 | if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError) | |
165 | { | |
166 | perror("ERROR: Unable to create service connection"); | |
167 | return (1); | |
168 | } | |
169 | ||
170 | fd = DNSServiceRefSockFD(main_ref); | |
171 | ||
172 | fax_ipp_ref = main_ref; | |
173 | DNSServiceBrowse(&fax_ipp_ref, kDNSServiceFlagsShareConnection, 0, | |
174 | "_fax-ipp._tcp", NULL, browse_callback, devices); | |
175 | ||
176 | ipp_ref = main_ref; | |
177 | DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0, | |
178 | "_ipp._tcp", NULL, browse_callback, devices); | |
179 | ||
180 | ipp_tls_ref = main_ref; | |
181 | DNSServiceBrowse(&ipp_tls_ref, kDNSServiceFlagsShareConnection, 0, | |
182 | "_ipp-tls._tcp", NULL, browse_callback, devices); | |
183 | ||
184 | local_fax_ipp_ref = main_ref; | |
185 | DNSServiceBrowse(&local_fax_ipp_ref, kDNSServiceFlagsShareConnection, | |
186 | kDNSServiceInterfaceIndexLocalOnly, | |
187 | "_fax-ipp._tcp", NULL, browse_local_callback, devices); | |
188 | ||
189 | local_ipp_ref = main_ref; | |
190 | DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection, | |
191 | kDNSServiceInterfaceIndexLocalOnly, | |
192 | "_ipp._tcp", NULL, browse_local_callback, devices); | |
193 | ||
194 | local_ipp_tls_ref = main_ref; | |
195 | DNSServiceBrowse(&local_ipp_tls_ref, kDNSServiceFlagsShareConnection, | |
196 | kDNSServiceInterfaceIndexLocalOnly, | |
197 | "_ipp-tls._tcp", NULL, browse_local_callback, devices); | |
198 | ||
199 | local_printer_ref = main_ref; | |
200 | DNSServiceBrowse(&local_printer_ref, kDNSServiceFlagsShareConnection, | |
201 | kDNSServiceInterfaceIndexLocalOnly, | |
202 | "_printer._tcp", NULL, browse_local_callback, devices); | |
203 | ||
204 | pdl_datastream_ref = main_ref; | |
205 | DNSServiceBrowse(&pdl_datastream_ref, kDNSServiceFlagsShareConnection, 0, | |
206 | "_pdl-datastream._tcp", NULL, browse_callback, devices); | |
207 | ||
208 | printer_ref = main_ref; | |
209 | DNSServiceBrowse(&printer_ref, kDNSServiceFlagsShareConnection, 0, | |
210 | "_printer._tcp", NULL, browse_callback, devices); | |
211 | ||
212 | riousbprint_ref = main_ref; | |
213 | DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0, | |
214 | "_riousbprint._tcp", NULL, browse_callback, devices); | |
215 | ||
216 | /* | |
217 | * Loop until we are killed... | |
218 | */ | |
219 | ||
220 | for (;;) | |
221 | { | |
222 | FD_ZERO(&input); | |
223 | FD_SET(fd, &input); | |
224 | ||
225 | timeout.tv_sec = 1; | |
226 | timeout.tv_usec = 0; | |
227 | ||
228 | if (select(fd + 1, &input, NULL, NULL, &timeout) < 0) | |
229 | continue; | |
230 | ||
231 | if (FD_ISSET(fd, &input)) | |
232 | { | |
233 | /* | |
234 | * Process results of our browsing... | |
235 | */ | |
236 | ||
237 | DNSServiceProcessResult(main_ref); | |
238 | } | |
239 | else | |
240 | { | |
241 | /* | |
242 | * Announce any devices we've found... | |
243 | */ | |
244 | ||
1f0275e3 | 245 | cups_device_t *best; /* Best matching device */ |
7a14d768 | 246 | char device_uri[1024]; /* Device URI */ |
5eb9da71 | 247 | int count; /* Number of queries */ |
7a14d768 MS |
248 | static const char * const schemes[] = |
249 | { "lpd", "ipp", "ipp", "socket", "riousbprint" }; | |
250 | /* URI schemes for devices */ | |
251 | ||
252 | ||
1f0275e3 MS |
253 | for (device = (cups_device_t *)cupsArrayFirst(devices), |
254 | best = NULL, count = 0; | |
7a14d768 MS |
255 | device; |
256 | device = (cups_device_t *)cupsArrayNext(devices)) | |
257 | if (!device->ref && !device->sent) | |
258 | { | |
259 | /* | |
260 | * Found the device, now get the TXT record(s) for it... | |
261 | */ | |
262 | ||
5eb9da71 MS |
263 | if (count < 10) |
264 | { | |
265 | device->ref = main_ref; | |
266 | ||
267 | fprintf(stderr, "DEBUG: Querying \"%s\"...\n", device->fullName); | |
268 | ||
269 | if (DNSServiceQueryRecord(&(device->ref), | |
270 | kDNSServiceFlagsShareConnection, | |
271 | 0, device->fullName, kDNSServiceType_TXT, | |
272 | kDNSServiceClass_IN, query_callback, | |
273 | devices) != kDNSServiceErr_NoError) | |
274 | fputs("ERROR: Unable to query for TXT records!\n", stderr); | |
275 | else | |
276 | count ++; | |
277 | } | |
7a14d768 MS |
278 | } |
279 | else if (!device->sent) | |
280 | { | |
281 | /* | |
282 | * Got the TXT records, now report the device... | |
283 | */ | |
284 | ||
285 | DNSServiceRefDeallocate(device->ref); | |
286 | device->ref = 0; | |
287 | ||
1f0275e3 MS |
288 | if (!best) |
289 | best = device; | |
290 | else if (strcasecmp(best->name, device->name) || | |
291 | strcasecmp(best->domain, device->domain)) | |
292 | { | |
293 | httpAssembleURI(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri), | |
294 | schemes[best->type], NULL, best->fullName, 0, | |
295 | best->cups_shared ? "/cups" : "/"); | |
296 | ||
749b1e90 MS |
297 | cupsBackendReport("network", device_uri, best->make_and_model, |
298 | best->name, NULL, NULL); | |
1f0275e3 MS |
299 | best->sent = 1; |
300 | best = device; | |
301 | } | |
302 | else if (best->priority > device->priority || | |
303 | (best->priority == device->priority && | |
304 | best->type < device->type)) | |
305 | { | |
306 | best->sent = 1; | |
307 | best = device; | |
308 | } | |
309 | else | |
310 | device->sent = 1; | |
311 | } | |
7a14d768 | 312 | |
1f0275e3 MS |
313 | if (best) |
314 | { | |
315 | httpAssembleURI(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri), | |
316 | schemes[best->type], NULL, best->fullName, 0, | |
317 | best->cups_shared ? "/cups" : "/"); | |
7a14d768 | 318 | |
749b1e90 MS |
319 | cupsBackendReport("network", device_uri, best->make_and_model, |
320 | best->name, NULL, NULL); | |
1f0275e3 MS |
321 | best->sent = 1; |
322 | } | |
7a14d768 MS |
323 | } |
324 | } | |
325 | } | |
326 | ||
327 | ||
328 | /* | |
329 | * 'browse_callback()' - Browse devices. | |
330 | */ | |
331 | ||
332 | static void | |
333 | browse_callback( | |
334 | DNSServiceRef sdRef, /* I - Service reference */ | |
335 | DNSServiceFlags flags, /* I - Option flags */ | |
336 | uint32_t interfaceIndex, /* I - Interface number */ | |
337 | DNSServiceErrorType errorCode, /* I - Error, if any */ | |
338 | const char *serviceName, /* I - Name of service/device */ | |
339 | const char *regtype, /* I - Type of service */ | |
340 | const char *replyDomain, /* I - Service domain */ | |
341 | void *context) /* I - Devices array */ | |
342 | { | |
343 | fprintf(stderr, "DEBUG2: browse_callback(sdRef=%p, flags=%x, " | |
344 | "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", " | |
345 | "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n", | |
346 | sdRef, flags, interfaceIndex, errorCode, | |
347 | serviceName ? serviceName : "(null)", | |
348 | regtype ? regtype : "(null)", | |
349 | replyDomain ? replyDomain : "(null)", | |
350 | context); | |
351 | ||
352 | /* | |
353 | * Only process "add" data... | |
354 | */ | |
355 | ||
356 | if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd)) | |
357 | return; | |
358 | ||
359 | /* | |
360 | * Get the device... | |
361 | */ | |
362 | ||
363 | get_device((cups_array_t *)context, serviceName, regtype, replyDomain); | |
364 | } | |
365 | ||
366 | ||
367 | /* | |
368 | * 'browse_local_callback()' - Browse local devices. | |
369 | */ | |
370 | ||
371 | static void | |
372 | browse_local_callback( | |
373 | DNSServiceRef sdRef, /* I - Service reference */ | |
374 | DNSServiceFlags flags, /* I - Option flags */ | |
375 | uint32_t interfaceIndex, /* I - Interface number */ | |
376 | DNSServiceErrorType errorCode, /* I - Error, if any */ | |
377 | const char *serviceName, /* I - Name of service/device */ | |
378 | const char *regtype, /* I - Type of service */ | |
379 | const char *replyDomain, /* I - Service domain */ | |
380 | void *context) /* I - Devices array */ | |
381 | { | |
382 | cups_device_t *device; /* Device */ | |
383 | ||
384 | ||
385 | fprintf(stderr, "DEBUG2: browse_local_callback(sdRef=%p, flags=%x, " | |
386 | "interfaceIndex=%d, errorCode=%d, serviceName=\"%s\", " | |
387 | "regtype=\"%s\", replyDomain=\"%s\", context=%p)\n", | |
388 | sdRef, flags, interfaceIndex, errorCode, | |
389 | serviceName ? serviceName : "(null)", | |
390 | regtype ? regtype : "(null)", | |
391 | replyDomain ? replyDomain : "(null)", | |
392 | context); | |
393 | ||
394 | /* | |
395 | * Only process "add" data... | |
396 | */ | |
397 | ||
398 | if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd)) | |
399 | return; | |
400 | ||
401 | /* | |
402 | * Get the device... | |
403 | */ | |
404 | ||
405 | device = get_device((cups_array_t *)context, serviceName, regtype, | |
406 | replyDomain); | |
407 | ||
408 | /* | |
409 | * Hide locally-registered devices... | |
410 | */ | |
411 | ||
412 | fprintf(stderr, "DEBUG: Hiding local printer \"%s\"...\n", | |
413 | device->fullName); | |
414 | device->sent = 1; | |
415 | } | |
416 | ||
417 | ||
418 | /* | |
419 | * 'compare_devices()' - Compare two devices. | |
420 | */ | |
421 | ||
422 | static int /* O - Result of comparison */ | |
423 | compare_devices(cups_device_t *a, /* I - First device */ | |
424 | cups_device_t *b) /* I - Second device */ | |
425 | { | |
426 | int result = strcmp(a->name, b->name); | |
427 | ||
428 | if (result) | |
429 | return (result); | |
430 | else | |
431 | return (strcmp(a->domain, b->domain)); | |
432 | } | |
433 | ||
434 | ||
435 | /* | |
436 | * 'exec_backend()' - Execute the backend that corresponds to the | |
437 | * resolved service name. | |
438 | */ | |
439 | ||
440 | static void | |
441 | exec_backend(char **argv) /* I - Command-line arguments */ | |
442 | { | |
443 | const char *resolved_uri, /* Resolved device URI */ | |
444 | *cups_serverbin; /* Location of programs */ | |
445 | char scheme[1024], /* Scheme from URI */ | |
446 | *ptr, /* Pointer into scheme */ | |
447 | filename[1024]; /* Backend filename */ | |
448 | ||
449 | ||
450 | /* | |
451 | * Resolve the device URI... | |
452 | */ | |
453 | ||
1f0275e3 MS |
454 | if ((resolved_uri = cupsBackendDeviceURI(argv)) == NULL) |
455 | exit(CUPS_BACKEND_FAILED); | |
7a14d768 MS |
456 | |
457 | /* | |
458 | * Extract the scheme from the URI... | |
459 | */ | |
460 | ||
461 | strlcpy(scheme, resolved_uri, sizeof(scheme)); | |
462 | if ((ptr = strchr(scheme, ':')) != NULL) | |
463 | *ptr = '\0'; | |
464 | ||
465 | /* | |
466 | * Get the filename of the backend... | |
467 | */ | |
468 | ||
469 | if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) | |
470 | cups_serverbin = CUPS_SERVERBIN; | |
471 | ||
472 | snprintf(filename, sizeof(filename), "%s/backend/%s", cups_serverbin, scheme); | |
473 | ||
474 | /* | |
475 | * Overwrite the device URIs and run the new backend... | |
476 | */ | |
477 | ||
478 | setenv("DEVICE_URI", resolved_uri, 1); | |
479 | ||
480 | argv[0] = (char *)resolved_uri; | |
481 | ||
482 | fprintf(stderr, "DEBUG: Executing backend \"%s\"...\n", filename); | |
483 | ||
484 | execv(filename, argv); | |
485 | ||
486 | fprintf(stderr, "ERROR: Unable to execute backend \"%s\": %s\n", filename, | |
487 | strerror(errno)); | |
488 | exit(CUPS_BACKEND_STOP); | |
489 | } | |
490 | ||
491 | ||
492 | /* | |
493 | * 'get_device()' - Create or update a device. | |
494 | */ | |
495 | ||
496 | static cups_device_t * /* O - Device */ | |
497 | get_device(cups_array_t *devices, /* I - Device array */ | |
498 | const char *serviceName, /* I - Name of service/device */ | |
499 | const char *regtype, /* I - Type of service */ | |
500 | const char *replyDomain) /* I - Service domain */ | |
501 | { | |
502 | cups_device_t key, /* Search key */ | |
503 | *device; /* Device */ | |
504 | char fullName[1024]; /* Full name for query */ | |
505 | ||
506 | ||
507 | /* | |
508 | * See if this is a new device... | |
509 | */ | |
510 | ||
511 | key.name = (char *)serviceName; | |
512 | key.domain = (char *)replyDomain; | |
513 | ||
514 | if (!strcmp(regtype, "_ipp._tcp.") || | |
515 | !strcmp(regtype, "_ipp-tls._tcp.")) | |
516 | key.type = CUPS_DEVICE_IPP; | |
517 | else if (!strcmp(regtype, "_fax-ipp._tcp.")) | |
518 | key.type = CUPS_DEVICE_FAX_IPP; | |
519 | else if (!strcmp(regtype, "_printer._tcp.")) | |
520 | key.type = CUPS_DEVICE_PRINTER; | |
521 | else if (!strcmp(regtype, "_pdl-datastream._tcp.")) | |
522 | key.type = CUPS_DEVICE_PDL_DATASTREAM; | |
523 | else | |
524 | key.type = CUPS_DEVICE_RIOUSBPRINT; | |
525 | ||
1f0275e3 MS |
526 | for (device = cupsArrayFind(devices, &key); |
527 | device; | |
528 | device = cupsArrayNext(devices)) | |
529 | if (strcasecmp(device->name, key.name) || | |
530 | strcasecmp(device->domain, key.domain)) | |
531 | break; | |
532 | else if (device->type == key.type) | |
533 | return (device); | |
7a14d768 | 534 | |
1f0275e3 MS |
535 | /* |
536 | * Yes, add the device... | |
537 | */ | |
7a14d768 | 538 | |
1f0275e3 MS |
539 | fprintf(stderr, "DEBUG: Found \"%s.%s%s\"...\n", serviceName, regtype, |
540 | replyDomain); | |
7a14d768 | 541 | |
1f0275e3 MS |
542 | device = calloc(sizeof(cups_device_t), 1); |
543 | device->name = strdup(serviceName); | |
544 | device->domain = strdup(replyDomain); | |
545 | device->type = key.type; | |
546 | device->priority = 50; | |
7a14d768 | 547 | |
1f0275e3 | 548 | cupsArrayAdd(devices, device); |
7a14d768 MS |
549 | |
550 | /* | |
1f0275e3 | 551 | * Set the "full name" of this service, which is used for queries... |
7a14d768 MS |
552 | */ |
553 | ||
554 | snprintf(fullName, sizeof(fullName), "%s.%s%s", serviceName, regtype, | |
555 | replyDomain); | |
7a14d768 MS |
556 | device->fullName = strdup(fullName); |
557 | ||
558 | return (device); | |
559 | } | |
560 | ||
561 | ||
562 | /* | |
563 | * 'query_callback()' - Process query data. | |
564 | */ | |
565 | ||
566 | static void | |
567 | query_callback( | |
568 | DNSServiceRef sdRef, /* I - Service reference */ | |
569 | DNSServiceFlags flags, /* I - Data flags */ | |
570 | uint32_t interfaceIndex, /* I - Interface */ | |
571 | DNSServiceErrorType errorCode, /* I - Error, if any */ | |
572 | const char *fullName, /* I - Full service name */ | |
573 | uint16_t rrtype, /* I - Record type */ | |
574 | uint16_t rrclass, /* I - Record class */ | |
575 | uint16_t rdlen, /* I - Length of record data */ | |
576 | const void *rdata, /* I - Record data */ | |
577 | uint32_t ttl, /* I - Time-to-live */ | |
578 | void *context) /* I - Devices array */ | |
579 | { | |
580 | cups_array_t *devices; /* Device array */ | |
581 | char name[1024], /* Service name */ | |
582 | *ptr; /* Pointer into name */ | |
583 | cups_device_t key, /* Search key */ | |
584 | *device; /* Device */ | |
585 | ||
586 | ||
587 | fprintf(stderr, "DEBUG2: query_callback(sdRef=%p, flags=%x, " | |
588 | "interfaceIndex=%d, errorCode=%d, fullName=\"%s\", " | |
589 | "rrtype=%u, rrclass=%u, rdlen=%u, rdata=%p, ttl=%u, " | |
590 | "context=%p)\n", | |
591 | sdRef, flags, interfaceIndex, errorCode, | |
592 | fullName ? fullName : "(null)", rrtype, rrclass, rdlen, rdata, ttl, | |
593 | context); | |
594 | ||
595 | /* | |
596 | * Only process "add" data... | |
597 | */ | |
598 | ||
599 | if (errorCode != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd)) | |
600 | return; | |
601 | ||
602 | /* | |
603 | * Lookup the service in the devices array. | |
604 | */ | |
605 | ||
606 | devices = (cups_array_t *)context; | |
607 | key.name = name; | |
608 | ||
609 | unquote(name, fullName, sizeof(name)); | |
610 | ||
611 | if ((key.domain = strstr(name, "._tcp.")) != NULL) | |
612 | key.domain += 6; | |
613 | else | |
614 | key.domain = (char *)"local."; | |
615 | ||
616 | if ((ptr = strstr(name, "._")) != NULL) | |
617 | *ptr = '\0'; | |
618 | ||
1f0275e3 MS |
619 | if (strstr(fullName, "_ipp._tcp.") || |
620 | strstr(fullName, "_ipp-tls._tcp.")) | |
621 | key.type = CUPS_DEVICE_IPP; | |
622 | else if (strstr(fullName, "_fax-ipp._tcp.")) | |
623 | key.type = CUPS_DEVICE_FAX_IPP; | |
624 | else if (strstr(fullName, "_printer._tcp.")) | |
625 | key.type = CUPS_DEVICE_PRINTER; | |
626 | else if (strstr(fullName, "_pdl-datastream._tcp.")) | |
627 | key.type = CUPS_DEVICE_PDL_DATASTREAM; | |
628 | else | |
629 | key.type = CUPS_DEVICE_RIOUSBPRINT; | |
630 | ||
631 | for (device = cupsArrayFind(devices, &key); | |
632 | device; | |
633 | device = cupsArrayNext(devices)) | |
7a14d768 | 634 | { |
1f0275e3 MS |
635 | if (strcasecmp(device->name, key.name) || |
636 | strcasecmp(device->domain, key.domain)) | |
637 | { | |
638 | device = NULL; | |
639 | break; | |
640 | } | |
641 | else if (device->type == key.type) | |
642 | { | |
643 | /* | |
644 | * Found it, pull out the priority and make and model from the TXT | |
645 | * record and save it... | |
646 | */ | |
7a14d768 | 647 | |
1f0275e3 MS |
648 | const void *value; /* Pointer to value */ |
649 | uint8_t valueLen; /* Length of value (max 255) */ | |
650 | char make_and_model[512], /* Manufacturer and model */ | |
651 | model[256], /* Model */ | |
652 | priority[256]; /* Priority */ | |
7a14d768 MS |
653 | |
654 | ||
1f0275e3 | 655 | value = TXTRecordGetValuePtr(rdlen, rdata, "priority", &valueLen); |
7a14d768 | 656 | |
1f0275e3 MS |
657 | if (value && valueLen) |
658 | { | |
659 | memcpy(priority, value, valueLen); | |
660 | priority[valueLen] = '\0'; | |
661 | device->priority = atoi(priority); | |
662 | } | |
7a14d768 | 663 | |
1f0275e3 MS |
664 | if ((value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MFG", |
665 | &valueLen)) == NULL) | |
666 | value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MANUFACTURER", | |
667 | &valueLen); | |
7a14d768 | 668 | |
1f0275e3 | 669 | if (value && valueLen) |
5eb9da71 | 670 | { |
1f0275e3 MS |
671 | memcpy(make_and_model, value, valueLen); |
672 | make_and_model[valueLen] = '\0'; | |
5eb9da71 MS |
673 | } |
674 | else | |
1f0275e3 MS |
675 | make_and_model[0] = '\0'; |
676 | ||
677 | if ((value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MDL", | |
678 | &valueLen)) == NULL) | |
679 | value = TXTRecordGetValuePtr(rdlen, rdata, "usb_MODEL", &valueLen); | |
680 | ||
681 | if (value && valueLen) | |
5eb9da71 MS |
682 | { |
683 | memcpy(model, value, valueLen); | |
684 | model[valueLen] = '\0'; | |
685 | } | |
1f0275e3 MS |
686 | else if ((value = TXTRecordGetValuePtr(rdlen, rdata, "product", |
687 | &valueLen)) != NULL && valueLen > 2) | |
7a14d768 | 688 | { |
1f0275e3 MS |
689 | if (((char *)value)[0] == '(') |
690 | { | |
691 | /* | |
692 | * Strip parenthesis... | |
693 | */ | |
694 | ||
695 | memcpy(model, value + 1, valueLen - 2); | |
696 | model[valueLen - 2] = '\0'; | |
697 | } | |
698 | else | |
699 | { | |
7a14d768 MS |
700 | memcpy(model, value, valueLen); |
701 | model[valueLen] = '\0'; | |
1f0275e3 | 702 | } |
7a14d768 | 703 | |
1f0275e3 MS |
704 | if (!strcasecmp(model, "GPL Ghostscript") || |
705 | !strcasecmp(model, "GNU Ghostscript") || | |
706 | !strcasecmp(model, "ESP Ghostscript")) | |
707 | { | |
708 | if ((value = TXTRecordGetValuePtr(rdlen, rdata, "ty", | |
709 | &valueLen)) != NULL) | |
710 | { | |
711 | memcpy(model, value, valueLen); | |
712 | model[valueLen] = '\0'; | |
713 | ||
714 | if ((ptr = strchr(model, ',')) != NULL) | |
715 | *ptr = '\0'; | |
716 | } | |
717 | else | |
718 | strcpy(model, "Unknown"); | |
7a14d768 | 719 | } |
7a14d768 | 720 | } |
1f0275e3 MS |
721 | else |
722 | strcpy(model, "Unknown"); | |
7a14d768 | 723 | |
1f0275e3 MS |
724 | if (device->make_and_model) |
725 | free(device->make_and_model); | |
7a14d768 | 726 | |
1f0275e3 MS |
727 | if (make_and_model[0]) |
728 | { | |
729 | strlcat(make_and_model, " ", sizeof(make_and_model)); | |
730 | strlcat(make_and_model, model, sizeof(make_and_model)); | |
731 | device->make_and_model = strdup(make_and_model); | |
732 | } | |
733 | else | |
734 | device->make_and_model = strdup(model); | |
7a14d768 | 735 | |
1f0275e3 MS |
736 | if ((device->type == CUPS_DEVICE_IPP || |
737 | device->type == CUPS_DEVICE_PRINTER) && | |
738 | TXTRecordGetValuePtr(rdlen, rdata, "printer-type", &valueLen)) | |
739 | { | |
740 | /* | |
741 | * This is a CUPS printer! | |
742 | */ | |
7a14d768 | 743 | |
1f0275e3 | 744 | device->cups_shared = 1; |
a0f6818e | 745 | |
1f0275e3 MS |
746 | if (device->type == CUPS_DEVICE_PRINTER) |
747 | device->sent = 1; | |
748 | } | |
749 | ||
750 | break; | |
7a14d768 MS |
751 | } |
752 | } | |
1f0275e3 MS |
753 | |
754 | if (!device) | |
7a14d768 MS |
755 | fprintf(stderr, "DEBUG: Ignoring TXT record for \"%s\"...\n", fullName); |
756 | } | |
757 | ||
758 | ||
759 | /* | |
760 | * 'unquote()' - Unquote a name string. | |
761 | */ | |
762 | ||
763 | static void | |
764 | unquote(char *dst, /* I - Destination buffer */ | |
765 | const char *src, /* I - Source string */ | |
766 | size_t dstsize) /* I - Size of destination buffer */ | |
767 | { | |
768 | char *dstend = dst + dstsize - 1; /* End of destination buffer */ | |
769 | ||
770 | ||
771 | while (*src && dst < dstend) | |
772 | { | |
773 | if (*src == '\\') | |
774 | { | |
775 | src ++; | |
776 | if (isdigit(src[0] & 255) && isdigit(src[1] & 255) && | |
777 | isdigit(src[2] & 255)) | |
778 | { | |
779 | *dst++ = ((((src[0] - '0') * 10) + src[1] - '0') * 10) + src[2] - '0'; | |
780 | src += 3; | |
781 | } | |
782 | else | |
783 | *dst++ = *src++; | |
784 | } | |
785 | else | |
786 | *dst++ = *src ++; | |
787 | } | |
788 | ||
789 | *dst = '\0'; | |
790 | } | |
791 | ||
792 | ||
793 | /* | |
794 | * End of "$Id$". | |
795 | */ |