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