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