]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/http-support.c
Localize HTTP status codes based on Accept-Lanaguage (<rdar://problem/14201195>)
[thirdparty/cups.git] / cups / http-support.c
1 /*
2 * "$Id$"
3 *
4 * HTTP support routines for CUPS.
5 *
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * httpAssembleURI() - Assemble a uniform resource identifier from its
20 * components.
21 * httpAssembleURIf() - Assemble a uniform resource identifier from its
22 * components with a formatted resource.
23 * httpAssembleUUID() - Assemble a name-based UUID URN conforming to RFC
24 * 4122.
25 * httpDecode64() - Base64-decode a string.
26 * httpDecode64_2() - Base64-decode a string.
27 * httpEncode64() - Base64-encode a string.
28 * httpEncode64_2() - Base64-encode a string.
29 * httpGetDateString() - Get a formatted date/time string from a time value.
30 * httpGetDateString2() - Get a formatted date/time string from a time value.
31 * httpGetDateTime() - Get a time value from a formatted date/time string.
32 * httpSeparate() - Separate a Universal Resource Identifier into its
33 * components.
34 * httpSeparate2() - Separate a Universal Resource Identifier into its
35 * components.
36 * httpSeparateURI() - Separate a Universal Resource Identifier into its
37 * components.
38 * _httpStatus() - Return the localized string describing a HTTP
39 * status code.
40 * httpStatus() - Return a short string describing a HTTP status
41 * code.
42 * _cups_hstrerror() - hstrerror() emulation function for Solaris and
43 * others.
44 * _httpDecodeURI() - Percent-decode a HTTP request URI.
45 * _httpEncodeURI() - Percent-encode a HTTP request URI.
46 * _httpResolveURI() - Resolve a DNS-SD URI.
47 * http_client_cb() - Client callback for resolving URI.
48 * http_copy_decode() - Copy and decode a URI.
49 * http_copy_encode() - Copy and encode a URI.
50 * http_poll_cb() - Wait for input on the specified file descriptors.
51 * http_resolve_cb() - Build a device URI for the given service name.
52 * http_resolve_cb() - Build a device URI for the given service name.
53 */
54
55 /*
56 * Include necessary headers...
57 */
58
59 #include "cups-private.h"
60 #ifdef HAVE_DNSSD
61 # include <dns_sd.h>
62 # ifdef WIN32
63 # include <io.h>
64 # elif defined(HAVE_POLL)
65 # include <poll.h>
66 # else
67 # include <sys/select.h>
68 # endif /* WIN32 */
69 #elif defined(HAVE_AVAHI)
70 # include <avahi-client/client.h>
71 # include <avahi-client/lookup.h>
72 # include <avahi-common/simple-watch.h>
73 #endif /* HAVE_DNSSD */
74
75
76 /*
77 * Local types...
78 */
79
80 typedef struct _http_uribuf_s /* URI buffer */
81 {
82 #ifdef HAVE_AVAHI
83 AvahiSimplePoll *poll; /* Poll state */
84 #endif /* HAVE_AVAHI */
85 char *buffer; /* Pointer to buffer */
86 size_t bufsize; /* Size of buffer */
87 int options; /* Options passed to _httpResolveURI */
88 const char *resource; /* Resource from URI */
89 } _http_uribuf_t;
90
91
92 /*
93 * Local globals...
94 */
95
96 static const char * const http_days[7] =
97 {
98 "Sun",
99 "Mon",
100 "Tue",
101 "Wed",
102 "Thu",
103 "Fri",
104 "Sat"
105 };
106 static const char * const http_months[12] =
107 {
108 "Jan",
109 "Feb",
110 "Mar",
111 "Apr",
112 "May",
113 "Jun",
114 "Jul",
115 "Aug",
116 "Sep",
117 "Oct",
118 "Nov",
119 "Dec"
120 };
121
122
123 /*
124 * Local functions...
125 */
126
127 static const char *http_copy_decode(char *dst, const char *src,
128 int dstsize, const char *term,
129 int decode);
130 static char *http_copy_encode(char *dst, const char *src,
131 char *dstend, const char *reserved,
132 const char *term, int encode);
133 #ifdef HAVE_DNSSD
134 static void DNSSD_API http_resolve_cb(DNSServiceRef sdRef,
135 DNSServiceFlags flags,
136 uint32_t interfaceIndex,
137 DNSServiceErrorType errorCode,
138 const char *fullName,
139 const char *hostTarget,
140 uint16_t port, uint16_t txtLen,
141 const unsigned char *txtRecord,
142 void *context);
143 #endif /* HAVE_DNSSD */
144
145 #ifdef HAVE_AVAHI
146 static void http_client_cb(AvahiClient *client,
147 AvahiClientState state, void *simple_poll);
148 static int http_poll_cb(struct pollfd *pollfds, unsigned int num_pollfds,
149 int timeout, void *context);
150 static void http_resolve_cb(AvahiServiceResolver *resolver,
151 AvahiIfIndex interface,
152 AvahiProtocol protocol,
153 AvahiResolverEvent event,
154 const char *name, const char *type,
155 const char *domain, const char *host_name,
156 const AvahiAddress *address, uint16_t port,
157 AvahiStringList *txt,
158 AvahiLookupResultFlags flags, void *context);
159 #endif /* HAVE_AVAHI */
160
161
162 /*
163 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
164 * components.
165 *
166 * This function escapes reserved characters in the URI depending on the
167 * value of the "encoding" argument. You should use this function in
168 * place of traditional string functions whenever you need to create a
169 * URI string.
170 *
171 * @since CUPS 1.2/OS X 10.5@
172 */
173
174 http_uri_status_t /* O - URI status */
175 httpAssembleURI(
176 http_uri_coding_t encoding, /* I - Encoding flags */
177 char *uri, /* I - URI buffer */
178 int urilen, /* I - Size of URI buffer */
179 const char *scheme, /* I - Scheme name */
180 const char *username, /* I - Username */
181 const char *host, /* I - Hostname or address */
182 int port, /* I - Port number */
183 const char *resource) /* I - Resource */
184 {
185 char *ptr, /* Pointer into URI buffer */
186 *end; /* End of URI buffer */
187
188
189 /*
190 * Range check input...
191 */
192
193 if (!uri || urilen < 1 || !scheme || port < 0)
194 {
195 if (uri)
196 *uri = '\0';
197
198 return (HTTP_URI_STATUS_BAD_ARGUMENTS);
199 }
200
201 /*
202 * Assemble the URI starting with the scheme...
203 */
204
205 end = uri + urilen - 1;
206 ptr = http_copy_encode(uri, scheme, end, NULL, NULL, 0);
207
208 if (!ptr)
209 goto assemble_overflow;
210
211 if (!strcmp(scheme, "mailto"))
212 {
213 /*
214 * mailto: only has :, no //...
215 */
216
217 if (ptr < end)
218 *ptr++ = ':';
219 else
220 goto assemble_overflow;
221 }
222 else
223 {
224 /*
225 * Schemes other than mailto: all have //...
226 */
227
228 if ((ptr + 2) < end)
229 {
230 *ptr++ = ':';
231 *ptr++ = '/';
232 *ptr++ = '/';
233 }
234 else
235 goto assemble_overflow;
236 }
237
238 /*
239 * Next the username and hostname, if any...
240 */
241
242 if (host)
243 {
244 const char *hostptr; /* Pointer into hostname */
245 int have_ipv6; /* Do we have an IPv6 address? */
246
247 if (username && *username)
248 {
249 /*
250 * Add username@ first...
251 */
252
253 ptr = http_copy_encode(ptr, username, end, "/?#[]@", NULL,
254 encoding & HTTP_URI_CODING_USERNAME);
255
256 if (!ptr)
257 goto assemble_overflow;
258
259 if (ptr < end)
260 *ptr++ = '@';
261 else
262 goto assemble_overflow;
263 }
264
265 /*
266 * Then add the hostname. Since IPv6 is a particular pain to deal
267 * with, we have several special cases to deal with. If we get
268 * an IPv6 address with brackets around it, assume it is already in
269 * URI format. Since DNS-SD service names can sometimes look like
270 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
271 * too...
272 */
273
274 for (hostptr = host,
275 have_ipv6 = strchr(host, ':') && !strstr(host, "._tcp");
276 *hostptr && have_ipv6;
277 hostptr ++)
278 if (*hostptr != ':' && !isxdigit(*hostptr & 255))
279 {
280 have_ipv6 = *hostptr == '%';
281 break;
282 }
283
284 if (have_ipv6)
285 {
286 /*
287 * We have a raw IPv6 address...
288 */
289
290 if (strchr(host, '%') && !(encoding & HTTP_URI_CODING_RFC6874))
291 {
292 /*
293 * We have a link-local address, add "[v1." prefix...
294 */
295
296 if ((ptr + 4) < end)
297 {
298 *ptr++ = '[';
299 *ptr++ = 'v';
300 *ptr++ = '1';
301 *ptr++ = '.';
302 }
303 else
304 goto assemble_overflow;
305 }
306 else
307 {
308 /*
309 * We have a normal (or RFC 6874 link-local) address, add "[" prefix...
310 */
311
312 if (ptr < end)
313 *ptr++ = '[';
314 else
315 goto assemble_overflow;
316 }
317
318 /*
319 * Copy the rest of the IPv6 address, and terminate with "]".
320 */
321
322 while (ptr < end && *host)
323 {
324 if (*host == '%')
325 {
326 /*
327 * Convert/encode zone separator
328 */
329
330 if (encoding & HTTP_URI_CODING_RFC6874)
331 {
332 if (ptr >= (end - 2))
333 goto assemble_overflow;
334
335 *ptr++ = '%';
336 *ptr++ = '2';
337 *ptr++ = '5';
338 }
339 else
340 *ptr++ = '+';
341
342 host ++;
343 }
344 else
345 *ptr++ = *host++;
346 }
347
348 if (*host)
349 goto assemble_overflow;
350
351 if (ptr < end)
352 *ptr++ = ']';
353 else
354 goto assemble_overflow;
355 }
356 else
357 {
358 /*
359 * Otherwise, just copy the host string (the extra chars are not in the
360 * "reg-name" ABNF rule; anything <= SP or >= DEL plus % gets automatically
361 * percent-encoded.
362 */
363
364 ptr = http_copy_encode(ptr, host, end, "\"#/:<>?@[\\]^`{|}", NULL,
365 encoding & HTTP_URI_CODING_HOSTNAME);
366
367 if (!ptr)
368 goto assemble_overflow;
369 }
370
371 /*
372 * Finish things off with the port number...
373 */
374
375 if (port > 0)
376 {
377 snprintf(ptr, end - ptr + 1, ":%d", port);
378 ptr += strlen(ptr);
379
380 if (ptr >= end)
381 goto assemble_overflow;
382 }
383 }
384
385 /*
386 * Last but not least, add the resource string...
387 */
388
389 if (resource)
390 {
391 char *query; /* Pointer to query string */
392
393
394 /*
395 * Copy the resource string up to the query string if present...
396 */
397
398 query = strchr(resource, '?');
399 ptr = http_copy_encode(ptr, resource, end, NULL, "?",
400 encoding & HTTP_URI_CODING_RESOURCE);
401 if (!ptr)
402 goto assemble_overflow;
403
404 if (query)
405 {
406 /*
407 * Copy query string without encoding...
408 */
409
410 ptr = http_copy_encode(ptr, query, end, NULL, NULL,
411 encoding & HTTP_URI_CODING_QUERY);
412 if (!ptr)
413 goto assemble_overflow;
414 }
415 }
416 else if (ptr < end)
417 *ptr++ = '/';
418 else
419 goto assemble_overflow;
420
421 /*
422 * Nul-terminate the URI buffer and return with no errors...
423 */
424
425 *ptr = '\0';
426
427 return (HTTP_URI_STATUS_OK);
428
429 /*
430 * Clear the URI string and return an overflow error; I don't usually
431 * like goto's, but in this case it makes sense...
432 */
433
434 assemble_overflow:
435
436 *uri = '\0';
437 return (HTTP_URI_STATUS_OVERFLOW);
438 }
439
440
441 /*
442 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
443 * components with a formatted resource.
444 *
445 * This function creates a formatted version of the resource string
446 * argument "resourcef" and escapes reserved characters in the URI
447 * depending on the value of the "encoding" argument. You should use
448 * this function in place of traditional string functions whenever
449 * you need to create a URI string.
450 *
451 * @since CUPS 1.2/OS X 10.5@
452 */
453
454 http_uri_status_t /* O - URI status */
455 httpAssembleURIf(
456 http_uri_coding_t encoding, /* I - Encoding flags */
457 char *uri, /* I - URI buffer */
458 int urilen, /* I - Size of URI buffer */
459 const char *scheme, /* I - Scheme name */
460 const char *username, /* I - Username */
461 const char *host, /* I - Hostname or address */
462 int port, /* I - Port number */
463 const char *resourcef, /* I - Printf-style resource */
464 ...) /* I - Additional arguments as needed */
465 {
466 va_list ap; /* Pointer to additional arguments */
467 char resource[1024]; /* Formatted resource string */
468 int bytes; /* Bytes in formatted string */
469
470
471 /*
472 * Range check input...
473 */
474
475 if (!uri || urilen < 1 || !scheme || port < 0 || !resourcef)
476 {
477 if (uri)
478 *uri = '\0';
479
480 return (HTTP_URI_STATUS_BAD_ARGUMENTS);
481 }
482
483 /*
484 * Format the resource string and assemble the URI...
485 */
486
487 va_start(ap, resourcef);
488 bytes = vsnprintf(resource, sizeof(resource), resourcef, ap);
489 va_end(ap);
490
491 if (bytes >= sizeof(resource))
492 {
493 *uri = '\0';
494 return (HTTP_URI_STATUS_OVERFLOW);
495 }
496 else
497 return (httpAssembleURI(encoding, uri, urilen, scheme, username, host,
498 port, resource));
499 }
500
501
502 /*
503 * 'httpAssembleUUID()' - Assemble a name-based UUID URN conforming to RFC 4122.
504 *
505 * This function creates a unique 128-bit identifying number using the server
506 * name, port number, random data, and optionally an object name and/or object
507 * number. The result is formatted as a UUID URN as defined in RFC 4122.
508 *
509 * The buffer needs to be at least 46 bytes in size.
510 *
511 * @since CUPS 1.7/OS X 10.9@
512 */
513
514 char * /* I - UUID string */
515 httpAssembleUUID(const char *server, /* I - Server name */
516 int port, /* I - Port number */
517 const char *name, /* I - Object name or NULL */
518 int number, /* I - Object number or 0 */
519 char *buffer, /* I - String buffer */
520 size_t bufsize) /* I - Size of buffer */
521 {
522 char data[1024]; /* Source string for MD5 */
523 _cups_md5_state_t md5state; /* MD5 state */
524 unsigned char md5sum[16]; /* MD5 digest/sum */
525
526
527 /*
528 * Build a version 3 UUID conforming to RFC 4122.
529 *
530 * Start with the MD5 sum of the server, port, object name and
531 * number, and some random data on the end.
532 */
533
534 snprintf(data, sizeof(data), "%s:%d:%s:%d:%04x:%04x", server,
535 port, name ? name : server, number,
536 (unsigned)CUPS_RAND() & 0xffff, (unsigned)CUPS_RAND() & 0xffff);
537
538 _cupsMD5Init(&md5state);
539 _cupsMD5Append(&md5state, (unsigned char *)data, strlen(data));
540 _cupsMD5Finish(&md5state, md5sum);
541
542 /*
543 * Generate the UUID from the MD5...
544 */
545
546 snprintf(buffer, bufsize,
547 "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
548 "%02x%02x%02x%02x%02x%02x",
549 md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5],
550 (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40,
551 md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13],
552 md5sum[14], md5sum[15]);
553
554 return (buffer);
555 }
556
557 /* For OS X 10.8 and earlier */
558 char *_httpAssembleUUID(const char *server, int port, const char *name,
559 int number, char *buffer, size_t bufsize)
560 {
561 return (httpAssembleUUID(server, port, name, number, buffer, bufsize));
562 }
563
564
565 /*
566 * 'httpDecode64()' - Base64-decode a string.
567 *
568 * This function is deprecated. Use the httpDecode64_2() function instead
569 * which provides buffer length arguments.
570 *
571 * @deprecated@
572 */
573
574 char * /* O - Decoded string */
575 httpDecode64(char *out, /* I - String to write to */
576 const char *in) /* I - String to read from */
577 {
578 int outlen; /* Output buffer length */
579
580
581 /*
582 * Use the old maximum buffer size for binary compatibility...
583 */
584
585 outlen = 512;
586
587 return (httpDecode64_2(out, &outlen, in));
588 }
589
590
591 /*
592 * 'httpDecode64_2()' - Base64-decode a string.
593 *
594 * @since CUPS 1.1.21/OS X 10.4@
595 */
596
597 char * /* O - Decoded string */
598 httpDecode64_2(char *out, /* I - String to write to */
599 int *outlen, /* IO - Size of output string */
600 const char *in) /* I - String to read from */
601 {
602 int pos, /* Bit position */
603 base64; /* Value of this character */
604 char *outptr, /* Output pointer */
605 *outend; /* End of output buffer */
606
607
608 /*
609 * Range check input...
610 */
611
612 if (!out || !outlen || *outlen < 1 || !in)
613 return (NULL);
614
615 if (!*in)
616 {
617 *out = '\0';
618 *outlen = 0;
619
620 return (out);
621 }
622
623 /*
624 * Convert from base-64 to bytes...
625 */
626
627 for (outptr = out, outend = out + *outlen - 1, pos = 0; *in != '\0'; in ++)
628 {
629 /*
630 * Decode this character into a number from 0 to 63...
631 */
632
633 if (*in >= 'A' && *in <= 'Z')
634 base64 = *in - 'A';
635 else if (*in >= 'a' && *in <= 'z')
636 base64 = *in - 'a' + 26;
637 else if (*in >= '0' && *in <= '9')
638 base64 = *in - '0' + 52;
639 else if (*in == '+')
640 base64 = 62;
641 else if (*in == '/')
642 base64 = 63;
643 else if (*in == '=')
644 break;
645 else
646 continue;
647
648 /*
649 * Store the result in the appropriate chars...
650 */
651
652 switch (pos)
653 {
654 case 0 :
655 if (outptr < outend)
656 *outptr = base64 << 2;
657 pos ++;
658 break;
659 case 1 :
660 if (outptr < outend)
661 *outptr++ |= (base64 >> 4) & 3;
662 if (outptr < outend)
663 *outptr = (base64 << 4) & 255;
664 pos ++;
665 break;
666 case 2 :
667 if (outptr < outend)
668 *outptr++ |= (base64 >> 2) & 15;
669 if (outptr < outend)
670 *outptr = (base64 << 6) & 255;
671 pos ++;
672 break;
673 case 3 :
674 if (outptr < outend)
675 *outptr++ |= base64;
676 pos = 0;
677 break;
678 }
679 }
680
681 *outptr = '\0';
682
683 /*
684 * Return the decoded string and size...
685 */
686
687 *outlen = (int)(outptr - out);
688
689 return (out);
690 }
691
692
693 /*
694 * 'httpEncode64()' - Base64-encode a string.
695 *
696 * This function is deprecated. Use the httpEncode64_2() function instead
697 * which provides buffer length arguments.
698 *
699 * @deprecated@
700 */
701
702 char * /* O - Encoded string */
703 httpEncode64(char *out, /* I - String to write to */
704 const char *in) /* I - String to read from */
705 {
706 return (httpEncode64_2(out, 512, in, (int)strlen(in)));
707 }
708
709
710 /*
711 * 'httpEncode64_2()' - Base64-encode a string.
712 *
713 * @since CUPS 1.1.21/OS X 10.4@
714 */
715
716 char * /* O - Encoded string */
717 httpEncode64_2(char *out, /* I - String to write to */
718 int outlen, /* I - Size of output string */
719 const char *in, /* I - String to read from */
720 int inlen) /* I - Size of input string */
721 {
722 char *outptr, /* Output pointer */
723 *outend; /* End of output buffer */
724 static const char base64[] = /* Base64 characters... */
725 {
726 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
727 "abcdefghijklmnopqrstuvwxyz"
728 "0123456789"
729 "+/"
730 };
731
732
733 /*
734 * Range check input...
735 */
736
737 if (!out || outlen < 1 || !in)
738 return (NULL);
739
740 /*
741 * Convert bytes to base-64...
742 */
743
744 for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --)
745 {
746 /*
747 * Encode the up to 3 characters as 4 Base64 numbers...
748 */
749
750 if (outptr < outend)
751 *outptr ++ = base64[(in[0] & 255) >> 2];
752
753 if (outptr < outend)
754 {
755 if (inlen > 1)
756 *outptr ++ = base64[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63];
757 else
758 *outptr ++ = base64[((in[0] & 255) << 4) & 63];
759 }
760
761 in ++;
762 inlen --;
763 if (inlen <= 0)
764 {
765 if (outptr < outend)
766 *outptr ++ = '=';
767 if (outptr < outend)
768 *outptr ++ = '=';
769 break;
770 }
771
772 if (outptr < outend)
773 {
774 if (inlen > 1)
775 *outptr ++ = base64[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63];
776 else
777 *outptr ++ = base64[((in[0] & 255) << 2) & 63];
778 }
779
780 in ++;
781 inlen --;
782 if (inlen <= 0)
783 {
784 if (outptr < outend)
785 *outptr ++ = '=';
786 break;
787 }
788
789 if (outptr < outend)
790 *outptr ++ = base64[in[0] & 63];
791 }
792
793 *outptr = '\0';
794
795 /*
796 * Return the encoded string...
797 */
798
799 return (out);
800 }
801
802
803 /*
804 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
805 *
806 * @deprecated@
807 */
808
809 const char * /* O - Date/time string */
810 httpGetDateString(time_t t) /* I - UNIX time */
811 {
812 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
813
814
815 return (httpGetDateString2(t, cg->http_date, sizeof(cg->http_date)));
816 }
817
818
819 /*
820 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
821 *
822 * @since CUPS 1.2/OS X 10.5@
823 */
824
825 const char * /* O - Date/time string */
826 httpGetDateString2(time_t t, /* I - UNIX time */
827 char *s, /* I - String buffer */
828 int slen) /* I - Size of string buffer */
829 {
830 struct tm *tdate; /* UNIX date/time data */
831
832
833 tdate = gmtime(&t);
834 if (tdate)
835 snprintf(s, slen, "%s, %02d %s %d %02d:%02d:%02d GMT",
836 http_days[tdate->tm_wday], tdate->tm_mday,
837 http_months[tdate->tm_mon], tdate->tm_year + 1900,
838 tdate->tm_hour, tdate->tm_min, tdate->tm_sec);
839 else
840 s[0] = '\0';
841
842 return (s);
843 }
844
845
846 /*
847 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
848 */
849
850 time_t /* O - UNIX time */
851 httpGetDateTime(const char *s) /* I - Date/time string */
852 {
853 int i; /* Looping var */
854 char mon[16]; /* Abbreviated month name */
855 int day, year; /* Day of month and year */
856 int hour, min, sec; /* Time */
857 int days; /* Number of days since 1970 */
858 static const int normal_days[] = /* Days to a month, normal years */
859 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
860 static const int leap_days[] = /* Days to a month, leap years */
861 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
862
863
864 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s));
865
866 /*
867 * Extract the date and time from the formatted string...
868 */
869
870 if (sscanf(s, "%*s%d%15s%d%d:%d:%d", &day, mon, &year, &hour, &min, &sec) < 6)
871 return (0);
872
873 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
874 "min=%d, sec=%d", day, mon, year, hour, min, sec));
875
876 /*
877 * Convert the month name to a number from 0 to 11.
878 */
879
880 for (i = 0; i < 12; i ++)
881 if (!_cups_strcasecmp(mon, http_months[i]))
882 break;
883
884 if (i >= 12)
885 return (0);
886
887 DEBUG_printf(("4httpGetDateTime: i=%d", i));
888
889 /*
890 * Now convert the date and time to a UNIX time value in seconds since
891 * 1970. We can't use mktime() since the timezone may not be UTC but
892 * the date/time string *is* UTC.
893 */
894
895 if ((year & 3) == 0 && ((year % 100) != 0 || (year % 400) == 0))
896 days = leap_days[i] + day - 1;
897 else
898 days = normal_days[i] + day - 1;
899
900 DEBUG_printf(("4httpGetDateTime: days=%d", days));
901
902 days += (year - 1970) * 365 + /* 365 days per year (normally) */
903 ((year - 1) / 4 - 492) - /* + leap days */
904 ((year - 1) / 100 - 19) + /* - 100 year days */
905 ((year - 1) / 400 - 4); /* + 400 year days */
906
907 DEBUG_printf(("4httpGetDateTime: days=%d\n", days));
908
909 return (days * 86400 + hour * 3600 + min * 60 + sec);
910 }
911
912
913 /*
914 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
915 * components.
916 *
917 * This function is deprecated; use the httpSeparateURI() function instead.
918 *
919 * @deprecated@
920 */
921
922 void
923 httpSeparate(const char *uri, /* I - Universal Resource Identifier */
924 char *scheme, /* O - Scheme [32] (http, https, etc.) */
925 char *username, /* O - Username [1024] */
926 char *host, /* O - Hostname [1024] */
927 int *port, /* O - Port number to use */
928 char *resource) /* O - Resource/filename [1024] */
929 {
930 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, 32, username,
931 HTTP_MAX_URI, host, HTTP_MAX_URI, port, resource,
932 HTTP_MAX_URI);
933 }
934
935
936 /*
937 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
938 * components.
939 *
940 * This function is deprecated; use the httpSeparateURI() function instead.
941 *
942 * @since CUPS 1.1.21/OS X 10.4@
943 * @deprecated@
944 */
945
946 void
947 httpSeparate2(const char *uri, /* I - Universal Resource Identifier */
948 char *scheme, /* O - Scheme (http, https, etc.) */
949 int schemelen, /* I - Size of scheme buffer */
950 char *username, /* O - Username */
951 int usernamelen, /* I - Size of username buffer */
952 char *host, /* O - Hostname */
953 int hostlen, /* I - Size of hostname buffer */
954 int *port, /* O - Port number to use */
955 char *resource, /* O - Resource/filename */
956 int resourcelen) /* I - Size of resource buffer */
957 {
958 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, schemelen, username,
959 usernamelen, host, hostlen, port, resource, resourcelen);
960 }
961
962
963 /*
964 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
965 * components.
966 *
967 * @since CUPS 1.2/OS X 10.5@
968 */
969
970 http_uri_status_t /* O - Result of separation */
971 httpSeparateURI(
972 http_uri_coding_t decoding, /* I - Decoding flags */
973 const char *uri, /* I - Universal Resource Identifier */
974 char *scheme, /* O - Scheme (http, https, etc.) */
975 int schemelen, /* I - Size of scheme buffer */
976 char *username, /* O - Username */
977 int usernamelen, /* I - Size of username buffer */
978 char *host, /* O - Hostname */
979 int hostlen, /* I - Size of hostname buffer */
980 int *port, /* O - Port number to use */
981 char *resource, /* O - Resource/filename */
982 int resourcelen) /* I - Size of resource buffer */
983 {
984 char *ptr, /* Pointer into string... */
985 *end; /* End of string */
986 const char *sep; /* Separator character */
987 http_uri_status_t status; /* Result of separation */
988
989
990 /*
991 * Initialize everything to blank...
992 */
993
994 if (scheme && schemelen > 0)
995 *scheme = '\0';
996
997 if (username && usernamelen > 0)
998 *username = '\0';
999
1000 if (host && hostlen > 0)
1001 *host = '\0';
1002
1003 if (port)
1004 *port = 0;
1005
1006 if (resource && resourcelen > 0)
1007 *resource = '\0';
1008
1009 /*
1010 * Range check input...
1011 */
1012
1013 if (!uri || !port || !scheme || schemelen <= 0 || !username ||
1014 usernamelen <= 0 || !host || hostlen <= 0 || !resource ||
1015 resourcelen <= 0)
1016 return (HTTP_URI_STATUS_BAD_ARGUMENTS);
1017
1018 if (!*uri)
1019 return (HTTP_URI_STATUS_BAD_URI);
1020
1021 /*
1022 * Grab the scheme portion of the URI...
1023 */
1024
1025 status = HTTP_URI_STATUS_OK;
1026
1027 if (!strncmp(uri, "//", 2))
1028 {
1029 /*
1030 * Workaround for HP IPP client bug...
1031 */
1032
1033 strlcpy(scheme, "ipp", schemelen);
1034 status = HTTP_URI_STATUS_MISSING_SCHEME;
1035 }
1036 else if (*uri == '/')
1037 {
1038 /*
1039 * Filename...
1040 */
1041
1042 strlcpy(scheme, "file", schemelen);
1043 status = HTTP_URI_STATUS_MISSING_SCHEME;
1044 }
1045 else
1046 {
1047 /*
1048 * Standard URI with scheme...
1049 */
1050
1051 for (ptr = scheme, end = scheme + schemelen - 1;
1052 *uri && *uri != ':' && ptr < end;)
1053 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1054 "abcdefghijklmnopqrstuvwxyz"
1055 "0123456789-+.", *uri) != NULL)
1056 *ptr++ = *uri++;
1057 else
1058 break;
1059
1060 *ptr = '\0';
1061
1062 if (*uri != ':')
1063 {
1064 *scheme = '\0';
1065 return (HTTP_URI_STATUS_BAD_SCHEME);
1066 }
1067
1068 uri ++;
1069 }
1070
1071 /*
1072 * Set the default port number...
1073 */
1074
1075 if (!strcmp(scheme, "http"))
1076 *port = 80;
1077 else if (!strcmp(scheme, "https"))
1078 *port = 443;
1079 else if (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps"))
1080 *port = 631;
1081 else if (!_cups_strcasecmp(scheme, "lpd"))
1082 *port = 515;
1083 else if (!strcmp(scheme, "socket")) /* Not yet registered with IANA... */
1084 *port = 9100;
1085 else if (strcmp(scheme, "file") && strcmp(scheme, "mailto"))
1086 status = HTTP_URI_STATUS_UNKNOWN_SCHEME;
1087
1088 /*
1089 * Now see if we have a hostname...
1090 */
1091
1092 if (!strncmp(uri, "//", 2))
1093 {
1094 /*
1095 * Yes, extract it...
1096 */
1097
1098 uri += 2;
1099
1100 /*
1101 * Grab the username, if any...
1102 */
1103
1104 if ((sep = strpbrk(uri, "@/")) != NULL && *sep == '@')
1105 {
1106 /*
1107 * Get a username:password combo...
1108 */
1109
1110 uri = http_copy_decode(username, uri, usernamelen, "@",
1111 decoding & HTTP_URI_CODING_USERNAME);
1112
1113 if (!uri)
1114 {
1115 *username = '\0';
1116 return (HTTP_URI_STATUS_BAD_USERNAME);
1117 }
1118
1119 uri ++;
1120 }
1121
1122 /*
1123 * Then the hostname/IP address...
1124 */
1125
1126 if (*uri == '[')
1127 {
1128 /*
1129 * Grab IPv6 address...
1130 */
1131
1132 uri ++;
1133 if (*uri == 'v')
1134 {
1135 /*
1136 * Skip IPvFuture ("vXXXX.") prefix...
1137 */
1138
1139 uri ++;
1140
1141 while (isxdigit(*uri & 255))
1142 uri ++;
1143
1144 if (*uri != '.')
1145 {
1146 *host = '\0';
1147 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1148 }
1149
1150 uri ++;
1151 }
1152
1153 uri = http_copy_decode(host, uri, hostlen, "]",
1154 decoding & HTTP_URI_CODING_HOSTNAME);
1155
1156 if (!uri)
1157 {
1158 *host = '\0';
1159 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1160 }
1161
1162 /*
1163 * Validate value...
1164 */
1165
1166 if (*uri != ']')
1167 {
1168 *host = '\0';
1169 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1170 }
1171
1172 uri ++;
1173
1174 for (ptr = host; *ptr; ptr ++)
1175 if (*ptr == '+')
1176 {
1177 /*
1178 * Convert zone separator to % and stop here...
1179 */
1180
1181 *ptr = '%';
1182 break;
1183 }
1184 else if (*ptr == '%')
1185 {
1186 /*
1187 * Stop at zone separator (RFC 6874)
1188 */
1189
1190 break;
1191 }
1192 else if (*ptr != ':' && *ptr != '.' && !isxdigit(*ptr & 255))
1193 {
1194 *host = '\0';
1195 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1196 }
1197 }
1198 else
1199 {
1200 /*
1201 * Validate the hostname or IPv4 address first...
1202 */
1203
1204 for (ptr = (char *)uri; *ptr; ptr ++)
1205 if (strchr(":?/", *ptr))
1206 break;
1207 else if (!strchr("abcdefghijklmnopqrstuvwxyz" /* unreserved */
1208 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* unreserved */
1209 "0123456789" /* unreserved */
1210 "-._~" /* unreserved */
1211 "%" /* pct-encoded */
1212 "!$&'()*+,;=" /* sub-delims */
1213 "\\", *ptr)) /* SMB domain */
1214 {
1215 *host = '\0';
1216 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1217 }
1218
1219 /*
1220 * Then copy the hostname or IPv4 address to the buffer...
1221 */
1222
1223 uri = http_copy_decode(host, uri, hostlen, ":?/",
1224 decoding & HTTP_URI_CODING_HOSTNAME);
1225
1226 if (!uri)
1227 {
1228 *host = '\0';
1229 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1230 }
1231 }
1232
1233 /*
1234 * Validate hostname for file scheme - only empty and localhost are
1235 * acceptable.
1236 */
1237
1238 if (!strcmp(scheme, "file") && strcmp(host, "localhost") && host[0])
1239 {
1240 *host = '\0';
1241 return (HTTP_URI_STATUS_BAD_HOSTNAME);
1242 }
1243
1244 /*
1245 * See if we have a port number...
1246 */
1247
1248 if (*uri == ':')
1249 {
1250 /*
1251 * Yes, collect the port number...
1252 */
1253
1254 if (!isdigit(uri[1] & 255))
1255 {
1256 *port = 0;
1257 return (HTTP_URI_STATUS_BAD_PORT);
1258 }
1259
1260 *port = strtol(uri + 1, (char **)&uri, 10);
1261
1262 if (*uri != '/' && *uri)
1263 {
1264 *port = 0;
1265 return (HTTP_URI_STATUS_BAD_PORT);
1266 }
1267 }
1268 }
1269
1270 /*
1271 * The remaining portion is the resource string...
1272 */
1273
1274 if (*uri == '?' || !*uri)
1275 {
1276 /*
1277 * Hostname but no path...
1278 */
1279
1280 status = HTTP_URI_STATUS_MISSING_RESOURCE;
1281 *resource = '/';
1282
1283 /*
1284 * Copy any query string...
1285 */
1286
1287 if (*uri == '?')
1288 uri = http_copy_decode(resource + 1, uri, resourcelen - 1, NULL,
1289 decoding & HTTP_URI_CODING_QUERY);
1290 else
1291 resource[1] = '\0';
1292 }
1293 else
1294 {
1295 uri = http_copy_decode(resource, uri, resourcelen, "?",
1296 decoding & HTTP_URI_CODING_RESOURCE);
1297
1298 if (uri && *uri == '?')
1299 {
1300 /*
1301 * Concatenate any query string...
1302 */
1303
1304 char *resptr = resource + strlen(resource);
1305
1306 uri = http_copy_decode(resptr, uri,
1307 resourcelen - (int)(resptr - resource), NULL,
1308 decoding & HTTP_URI_CODING_QUERY);
1309 }
1310 }
1311
1312 if (!uri)
1313 {
1314 *resource = '\0';
1315 return (HTTP_URI_STATUS_BAD_RESOURCE);
1316 }
1317
1318 /*
1319 * Return the URI separation status...
1320 */
1321
1322 return (status);
1323 }
1324
1325
1326 /*
1327 * '_httpStatus()' - Return the localized string describing a HTTP status code.
1328 *
1329 * The returned string is localized using the passed message catalog.
1330 */
1331
1332 const char * /* O - Localized status string */
1333 _httpStatus(cups_lang_t *lang, /* I - Language */
1334 http_status_t status) /* I - HTTP status code */
1335 {
1336 const char *s; /* Status string */
1337
1338
1339 switch (status)
1340 {
1341 case HTTP_STATUS_ERROR :
1342 s = strerror(errno);
1343 break;
1344 case HTTP_STATUS_CONTINUE :
1345 s = _("Continue");
1346 break;
1347 case HTTP_STATUS_SWITCHING_PROTOCOLS :
1348 s = _("Switching Protocols");
1349 break;
1350 case HTTP_STATUS_OK :
1351 s = _("OK");
1352 break;
1353 case HTTP_STATUS_CREATED :
1354 s = _("Created");
1355 break;
1356 case HTTP_STATUS_ACCEPTED :
1357 s = _("Accepted");
1358 break;
1359 case HTTP_STATUS_NO_CONTENT :
1360 s = _("No Content");
1361 break;
1362 case HTTP_STATUS_MOVED_PERMANENTLY :
1363 s = _("Moved Permanently");
1364 break;
1365 case HTTP_STATUS_SEE_OTHER :
1366 s = _("See Other");
1367 break;
1368 case HTTP_STATUS_NOT_MODIFIED :
1369 s = _("Not Modified");
1370 break;
1371 case HTTP_STATUS_BAD_REQUEST :
1372 s = _("Bad Request");
1373 break;
1374 case HTTP_STATUS_UNAUTHORIZED :
1375 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED :
1376 s = _("Unauthorized");
1377 break;
1378 case HTTP_STATUS_FORBIDDEN :
1379 s = _("Forbidden");
1380 break;
1381 case HTTP_STATUS_NOT_FOUND :
1382 s = _("Not Found");
1383 break;
1384 case HTTP_STATUS_REQUEST_TOO_LARGE :
1385 s = _("Request Entity Too Large");
1386 break;
1387 case HTTP_STATUS_URI_TOO_LONG :
1388 s = _("URI Too Long");
1389 break;
1390 case HTTP_STATUS_UPGRADE_REQUIRED :
1391 s = _("Upgrade Required");
1392 break;
1393 case HTTP_STATUS_NOT_IMPLEMENTED :
1394 s = _("Not Implemented");
1395 break;
1396 case HTTP_STATUS_NOT_SUPPORTED :
1397 s = _("Not Supported");
1398 break;
1399 case HTTP_STATUS_EXPECTATION_FAILED :
1400 s = _("Expectation Failed");
1401 break;
1402 case HTTP_STATUS_SERVICE_UNAVAILABLE :
1403 s = _("Service Unavailable");
1404 break;
1405 case HTTP_STATUS_SERVER_ERROR :
1406 s = _("Internal Server Error");
1407 break;
1408 case HTTP_STATUS_CUPS_PKI_ERROR :
1409 s = _("SSL/TLS Negotiation Error");
1410 break;
1411 case HTTP_STATUS_CUPS_WEBIF_DISABLED :
1412 s = _("Web Interface is Disabled");
1413 break;
1414
1415 default :
1416 s = _("Unknown");
1417 break;
1418 }
1419
1420 return (_cupsLangString(lang, s));
1421 }
1422
1423
1424 /*
1425 * 'httpStatus()' - Return a short string describing a HTTP status code.
1426 *
1427 * The returned string is localized to the current POSIX locale and is based
1428 * on the status strings defined in RFC 2616.
1429 */
1430
1431 const char * /* O - Localized status string */
1432 httpStatus(http_status_t status) /* I - HTTP status code */
1433 {
1434 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1435
1436
1437 if (!cg->lang_default)
1438 cg->lang_default = cupsLangDefault();
1439
1440 return (_httpStatus(cg->lang_default, status));
1441 }
1442
1443
1444 #ifndef HAVE_HSTRERROR
1445 /*
1446 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others.
1447 */
1448
1449 const char * /* O - Error string */
1450 _cups_hstrerror(int error) /* I - Error number */
1451 {
1452 static const char * const errors[] = /* Error strings */
1453 {
1454 "OK",
1455 "Host not found.",
1456 "Try again.",
1457 "Unrecoverable lookup error.",
1458 "No data associated with name."
1459 };
1460
1461
1462 if (error < 0 || error > 4)
1463 return ("Unknown hostname lookup error.");
1464 else
1465 return (errors[error]);
1466 }
1467 #endif /* !HAVE_HSTRERROR */
1468
1469
1470 /*
1471 * '_httpDecodeURI()' - Percent-decode a HTTP request URI.
1472 */
1473
1474 char * /* O - Decoded URI or NULL on error */
1475 _httpDecodeURI(char *dst, /* I - Destination buffer */
1476 const char *src, /* I - Source URI */
1477 size_t dstsize) /* I - Size of destination buffer */
1478 {
1479 if (http_copy_decode(dst, src, (int)dstsize, NULL, 1))
1480 return (dst);
1481 else
1482 return (NULL);
1483 }
1484
1485
1486 /*
1487 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1488 */
1489
1490 char * /* O - Encoded URI */
1491 _httpEncodeURI(char *dst, /* I - Destination buffer */
1492 const char *src, /* I - Source URI */
1493 size_t dstsize) /* I - Size of destination buffer */
1494 {
1495 http_copy_encode(dst, src, dst + dstsize - 1, NULL, NULL, 1);
1496 return (dst);
1497 }
1498
1499
1500 /*
1501 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1502 */
1503
1504 const char * /* O - Resolved URI */
1505 _httpResolveURI(
1506 const char *uri, /* I - DNS-SD URI */
1507 char *resolved_uri, /* I - Buffer for resolved URI */
1508 size_t resolved_size, /* I - Size of URI buffer */
1509 int options, /* I - Resolve options */
1510 int (*cb)(void *context), /* I - Continue callback function */
1511 void *context) /* I - Context pointer for callback */
1512 {
1513 char scheme[32], /* URI components... */
1514 userpass[256],
1515 hostname[1024],
1516 resource[1024];
1517 int port;
1518 #ifdef DEBUG
1519 http_uri_status_t status; /* URI decode status */
1520 #endif /* DEBUG */
1521
1522
1523 DEBUG_printf(("4_httpResolveURI(uri=\"%s\", resolved_uri=%p, "
1524 "resolved_size=" CUPS_LLFMT ")", uri, resolved_uri,
1525 CUPS_LLCAST resolved_size));
1526
1527 /*
1528 * Get the device URI...
1529 */
1530
1531 #ifdef DEBUG
1532 if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
1533 sizeof(scheme), userpass, sizeof(userpass),
1534 hostname, sizeof(hostname), &port, resource,
1535 sizeof(resource))) < HTTP_URI_STATUS_OK)
1536 #else
1537 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
1538 sizeof(scheme), userpass, sizeof(userpass),
1539 hostname, sizeof(hostname), &port, resource,
1540 sizeof(resource)) < HTTP_URI_STATUS_OK)
1541 #endif /* DEBUG */
1542 {
1543 if (options & _HTTP_RESOLVE_STDERR)
1544 _cupsLangPrintFilter(stderr, "ERROR", _("Bad device-uri \"%s\"."), uri);
1545
1546 DEBUG_printf(("6_httpResolveURI: httpSeparateURI returned %d!", status));
1547 DEBUG_puts("5_httpResolveURI: Returning NULL");
1548 return (NULL);
1549 }
1550
1551 /*
1552 * Resolve it as needed...
1553 */
1554
1555 if (strstr(hostname, "._tcp"))
1556 {
1557 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1558 char *regtype, /* Pointer to type in hostname */
1559 *domain; /* Pointer to domain in hostname */
1560 _http_uribuf_t uribuf; /* URI buffer */
1561 int offline = 0; /* offline-report state set? */
1562 # ifdef HAVE_DNSSD
1563 # ifdef WIN32
1564 # pragma comment(lib, "dnssd.lib")
1565 # endif /* WIN32 */
1566 DNSServiceRef ref, /* DNS-SD master service reference */
1567 domainref, /* DNS-SD service reference for domain */
1568 localref; /* DNS-SD service reference for .local */
1569 int domainsent = 0; /* Send the domain resolve? */
1570 # ifdef HAVE_POLL
1571 struct pollfd polldata; /* Polling data */
1572 # else /* select() */
1573 fd_set input_set; /* Input set for select() */
1574 struct timeval stimeout; /* Timeout value for select() */
1575 # endif /* HAVE_POLL */
1576 # elif defined(HAVE_AVAHI)
1577 AvahiClient *client; /* Client information */
1578 int error; /* Status */
1579 # endif /* HAVE_DNSSD */
1580
1581 if (options & _HTTP_RESOLVE_STDERR)
1582 fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
1583
1584 /*
1585 * Separate the hostname into service name, registration type, and domain...
1586 */
1587
1588 for (regtype = strstr(hostname, "._tcp") - 2;
1589 regtype > hostname;
1590 regtype --)
1591 if (regtype[0] == '.' && regtype[1] == '_')
1592 {
1593 /*
1594 * Found ._servicetype in front of ._tcp...
1595 */
1596
1597 *regtype++ = '\0';
1598 break;
1599 }
1600
1601 if (regtype <= hostname)
1602 {
1603 DEBUG_puts("5_httpResolveURI: Bad hostname, returning NULL");
1604 return (NULL);
1605 }
1606
1607 for (domain = strchr(regtype, '.');
1608 domain;
1609 domain = strchr(domain + 1, '.'))
1610 if (domain[1] != '_')
1611 break;
1612
1613 if (domain)
1614 *domain++ = '\0';
1615
1616 uribuf.buffer = resolved_uri;
1617 uribuf.bufsize = resolved_size;
1618 uribuf.options = options;
1619 uribuf.resource = resource;
1620
1621 resolved_uri[0] = '\0';
1622
1623 DEBUG_printf(("6_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1624 "domain=\"%s\"\n", hostname, regtype, domain));
1625 if (options & _HTTP_RESOLVE_STDERR)
1626 {
1627 fputs("STATE: +connecting-to-device\n", stderr);
1628 fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1629 "domain=\"local.\"...\n", hostname, regtype);
1630 }
1631
1632 uri = NULL;
1633
1634 # ifdef HAVE_DNSSD
1635 if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
1636 {
1637 int myinterface = kDNSServiceInterfaceIndexAny;
1638 /* Lookup on any interface */
1639
1640 if (!strcmp(scheme, "ippusb"))
1641 myinterface = kDNSServiceInterfaceIndexLocalOnly;
1642
1643 localref = ref;
1644 if (DNSServiceResolve(&localref,
1645 kDNSServiceFlagsShareConnection, myinterface,
1646 hostname, regtype, "local.", http_resolve_cb,
1647 &uribuf) == kDNSServiceErr_NoError)
1648 {
1649 int fds; /* Number of ready descriptors */
1650 time_t timeout, /* Poll timeout */
1651 start_time = time(NULL),/* Start time */
1652 end_time = start_time + 90;
1653 /* End time */
1654
1655 while (time(NULL) < end_time)
1656 {
1657 if (options & _HTTP_RESOLVE_STDERR)
1658 _cupsLangPrintFilter(stderr, "INFO", _("Looking for printer."));
1659
1660 if (cb && !(*cb)(context))
1661 {
1662 DEBUG_puts("5_httpResolveURI: callback returned 0 (stop)");
1663 break;
1664 }
1665
1666 /*
1667 * Wakeup every 2 seconds to emit a "looking for printer" message...
1668 */
1669
1670 if ((timeout = end_time - time(NULL)) > 2)
1671 timeout = 2;
1672
1673 # ifdef HAVE_POLL
1674 polldata.fd = DNSServiceRefSockFD(ref);
1675 polldata.events = POLLIN;
1676
1677 fds = poll(&polldata, 1, 1000 * timeout);
1678
1679 # else /* select() */
1680 FD_ZERO(&input_set);
1681 FD_SET(DNSServiceRefSockFD(ref), &input_set);
1682
1683 # ifdef WIN32
1684 stimeout.tv_sec = (long)timeout;
1685 # else
1686 stimeout.tv_sec = timeout;
1687 # endif /* WIN32 */
1688 stimeout.tv_usec = 0;
1689
1690 fds = select(DNSServiceRefSockFD(ref)+1, &input_set, NULL, NULL,
1691 &stimeout);
1692 # endif /* HAVE_POLL */
1693
1694 if (fds < 0)
1695 {
1696 if (errno != EINTR && errno != EAGAIN)
1697 {
1698 DEBUG_printf(("5_httpResolveURI: poll error: %s", strerror(errno)));
1699 break;
1700 }
1701 }
1702 else if (fds == 0)
1703 {
1704 /*
1705 * Wait 2 seconds for a response to the local resolve; if nothing
1706 * comes in, do an additional domain resolution...
1707 */
1708
1709 if (domainsent == 0 && domain && _cups_strcasecmp(domain, "local."))
1710 {
1711 if (options & _HTTP_RESOLVE_STDERR)
1712 fprintf(stderr,
1713 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1714 "domain=\"%s\"...\n", hostname, regtype,
1715 domain ? domain : "");
1716
1717 domainref = ref;
1718 if (DNSServiceResolve(&domainref,
1719 kDNSServiceFlagsShareConnection,
1720 myinterface, hostname, regtype, domain,
1721 http_resolve_cb,
1722 &uribuf) == kDNSServiceErr_NoError)
1723 domainsent = 1;
1724 }
1725
1726 /*
1727 * If it hasn't resolved within 5 seconds set the offline-report
1728 * printer-state-reason...
1729 */
1730
1731 if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 &&
1732 time(NULL) > (start_time + 5))
1733 {
1734 fputs("STATE: +offline-report\n", stderr);
1735 offline = 1;
1736 }
1737 }
1738 else
1739 {
1740 if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError)
1741 {
1742 uri = resolved_uri;
1743 break;
1744 }
1745 }
1746 }
1747
1748 if (domainsent)
1749 DNSServiceRefDeallocate(domainref);
1750
1751 DNSServiceRefDeallocate(localref);
1752 }
1753
1754 DNSServiceRefDeallocate(ref);
1755 }
1756 # else /* HAVE_AVAHI */
1757 if ((uribuf.poll = avahi_simple_poll_new()) != NULL)
1758 {
1759 avahi_simple_poll_set_func(uribuf.poll, http_poll_cb, NULL);
1760
1761 if ((client = avahi_client_new(avahi_simple_poll_get(uribuf.poll),
1762 0, http_client_cb,
1763 &uribuf, &error)) != NULL)
1764 {
1765 if (avahi_service_resolver_new(client, AVAHI_IF_UNSPEC,
1766 AVAHI_PROTO_UNSPEC, hostname,
1767 regtype, "local.", AVAHI_PROTO_UNSPEC, 0,
1768 http_resolve_cb, &uribuf) != NULL)
1769 {
1770 time_t start_time = time(NULL),
1771 /* Start time */
1772 end_time = start_time + 90;
1773 /* End time */
1774 int pstatus; /* Poll status */
1775
1776 pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000);
1777
1778 if (pstatus == 0 && !resolved_uri[0] && domain &&
1779 _cups_strcasecmp(domain, "local."))
1780 {
1781 /*
1782 * Resolve for .local hasn't returned anything, try the listed
1783 * domain...
1784 */
1785
1786 avahi_service_resolver_new(client, AVAHI_IF_UNSPEC,
1787 AVAHI_PROTO_UNSPEC, hostname,
1788 regtype, domain, AVAHI_PROTO_UNSPEC, 0,
1789 http_resolve_cb, &uribuf);
1790 }
1791
1792 while (!pstatus && !resolved_uri[0] && time(NULL) < end_time)
1793 {
1794 if ((pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000)) != 0)
1795 break;
1796
1797 /*
1798 * If it hasn't resolved within 5 seconds set the offline-report
1799 * printer-state-reason...
1800 */
1801
1802 if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 &&
1803 time(NULL) > (start_time + 5))
1804 {
1805 fputs("STATE: +offline-report\n", stderr);
1806 offline = 1;
1807 }
1808 }
1809
1810 /*
1811 * Collect the result (if we got one).
1812 */
1813
1814 if (resolved_uri[0])
1815 uri = resolved_uri;
1816 }
1817
1818 avahi_client_free(client);
1819 }
1820
1821 avahi_simple_poll_free(uribuf.poll);
1822 }
1823 # endif /* HAVE_DNSSD */
1824
1825 if (options & _HTTP_RESOLVE_STDERR)
1826 {
1827 if (uri)
1828 {
1829 fprintf(stderr, "DEBUG: Resolved as \"%s\"...\n", uri);
1830 fputs("STATE: -connecting-to-device,offline-report\n", stderr);
1831 }
1832 else
1833 {
1834 fputs("DEBUG: Unable to resolve URI\n", stderr);
1835 fputs("STATE: -connecting-to-device\n", stderr);
1836 }
1837 }
1838
1839 #else /* HAVE_DNSSD || HAVE_AVAHI */
1840 /*
1841 * No DNS-SD support...
1842 */
1843
1844 uri = NULL;
1845 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1846
1847 if ((options & _HTTP_RESOLVE_STDERR) && !uri)
1848 _cupsLangPrintFilter(stderr, "INFO", _("Unable to find printer."));
1849 }
1850 else
1851 {
1852 /*
1853 * Nothing more to do...
1854 */
1855
1856 strlcpy(resolved_uri, uri, resolved_size);
1857 uri = resolved_uri;
1858 }
1859
1860 DEBUG_printf(("5_httpResolveURI: Returning \"%s\"", uri));
1861
1862 return (uri);
1863 }
1864
1865
1866 #ifdef HAVE_AVAHI
1867 /*
1868 * 'http_client_cb()' - Client callback for resolving URI.
1869 */
1870
1871 static void
1872 http_client_cb(
1873 AvahiClient *client, /* I - Client information */
1874 AvahiClientState state, /* I - Current state */
1875 void *context) /* I - Pointer to URI buffer */
1876 {
1877 DEBUG_printf(("7http_client_cb(client=%p, state=%d, context=%p)", client,
1878 state, context));
1879
1880 /*
1881 * If the connection drops, quit.
1882 */
1883
1884 if (state == AVAHI_CLIENT_FAILURE)
1885 {
1886 _http_uribuf_t *uribuf = (_http_uribuf_t *)context;
1887 /* URI buffer */
1888
1889 avahi_simple_poll_quit(uribuf->poll);
1890 }
1891 }
1892 #endif /* HAVE_AVAHI */
1893
1894
1895 /*
1896 * 'http_copy_decode()' - Copy and decode a URI.
1897 */
1898
1899 static const char * /* O - New source pointer or NULL on error */
1900 http_copy_decode(char *dst, /* O - Destination buffer */
1901 const char *src, /* I - Source pointer */
1902 int dstsize, /* I - Destination size */
1903 const char *term, /* I - Terminating characters */
1904 int decode) /* I - Decode %-encoded values */
1905 {
1906 char *ptr, /* Pointer into buffer */
1907 *end; /* End of buffer */
1908 int quoted; /* Quoted character */
1909
1910
1911 /*
1912 * Copy the src to the destination until we hit a terminating character
1913 * or the end of the string.
1914 */
1915
1916 for (ptr = dst, end = dst + dstsize - 1;
1917 *src && (!term || !strchr(term, *src));
1918 src ++)
1919 if (ptr < end)
1920 {
1921 if (*src == '%' && decode)
1922 {
1923 if (isxdigit(src[1] & 255) && isxdigit(src[2] & 255))
1924 {
1925 /*
1926 * Grab a hex-encoded character...
1927 */
1928
1929 src ++;
1930 if (isalpha(*src))
1931 quoted = (tolower(*src) - 'a' + 10) << 4;
1932 else
1933 quoted = (*src - '0') << 4;
1934
1935 src ++;
1936 if (isalpha(*src))
1937 quoted |= tolower(*src) - 'a' + 10;
1938 else
1939 quoted |= *src - '0';
1940
1941 *ptr++ = quoted;
1942 }
1943 else
1944 {
1945 /*
1946 * Bad hex-encoded character...
1947 */
1948
1949 *ptr = '\0';
1950 return (NULL);
1951 }
1952 }
1953 else if ((*src & 255) <= 0x20 || (*src & 255) >= 0x7f)
1954 {
1955 *ptr = '\0';
1956 return (NULL);
1957 }
1958 else
1959 *ptr++ = *src;
1960 }
1961
1962 *ptr = '\0';
1963
1964 return (src);
1965 }
1966
1967
1968 /*
1969 * 'http_copy_encode()' - Copy and encode a URI.
1970 */
1971
1972 static char * /* O - End of current URI */
1973 http_copy_encode(char *dst, /* O - Destination buffer */
1974 const char *src, /* I - Source pointer */
1975 char *dstend, /* I - End of destination buffer */
1976 const char *reserved, /* I - Extra reserved characters */
1977 const char *term, /* I - Terminating characters */
1978 int encode) /* I - %-encode reserved chars? */
1979 {
1980 static const char hex[] = "0123456789ABCDEF";
1981
1982
1983 while (*src && dst < dstend)
1984 {
1985 if (term && *src == *term)
1986 return (dst);
1987
1988 if (encode && (*src == '%' || *src <= ' ' || *src & 128 ||
1989 (reserved && strchr(reserved, *src))))
1990 {
1991 /*
1992 * Hex encode reserved characters...
1993 */
1994
1995 if ((dst + 2) >= dstend)
1996 break;
1997
1998 *dst++ = '%';
1999 *dst++ = hex[(*src >> 4) & 15];
2000 *dst++ = hex[*src & 15];
2001
2002 src ++;
2003 }
2004 else
2005 *dst++ = *src++;
2006 }
2007
2008 *dst = '\0';
2009
2010 if (*src)
2011 return (NULL);
2012 else
2013 return (dst);
2014 }
2015
2016
2017 #ifdef HAVE_DNSSD
2018 /*
2019 * 'http_resolve_cb()' - Build a device URI for the given service name.
2020 */
2021
2022 static void DNSSD_API
2023 http_resolve_cb(
2024 DNSServiceRef sdRef, /* I - Service reference */
2025 DNSServiceFlags flags, /* I - Results flags */
2026 uint32_t interfaceIndex, /* I - Interface number */
2027 DNSServiceErrorType errorCode, /* I - Error, if any */
2028 const char *fullName, /* I - Full service name */
2029 const char *hostTarget, /* I - Hostname */
2030 uint16_t port, /* I - Port number */
2031 uint16_t txtLen, /* I - Length of TXT record */
2032 const unsigned char *txtRecord, /* I - TXT record data */
2033 void *context) /* I - Pointer to URI buffer */
2034 {
2035 _http_uribuf_t *uribuf = (_http_uribuf_t *)context;
2036 /* URI buffer */
2037 const char *scheme, /* URI scheme */
2038 *hostptr, /* Pointer into hostTarget */
2039 *reskey, /* "rp" or "rfo" */
2040 *resdefault; /* Default path */
2041 char resource[257], /* Remote path */
2042 fqdn[256]; /* FQDN of the .local name */
2043 const void *value; /* Value from TXT record */
2044 uint8_t valueLen; /* Length of value */
2045
2046
2047 DEBUG_printf(("7http_resolve_cb(sdRef=%p, flags=%x, interfaceIndex=%u, "
2048 "errorCode=%d, fullName=\"%s\", hostTarget=\"%s\", port=%u, "
2049 "txtLen=%u, txtRecord=%p, context=%p)", sdRef, flags,
2050 interfaceIndex, errorCode, fullName, hostTarget, port, txtLen,
2051 txtRecord, context));
2052
2053 /*
2054 * Figure out the scheme from the full name...
2055 */
2056
2057 if (strstr(fullName, "._ipps") || strstr(fullName, "._ipp-tls"))
2058 scheme = "ipps";
2059 else if (strstr(fullName, "._ipp") || strstr(fullName, "._fax-ipp"))
2060 scheme = "ipp";
2061 else if (strstr(fullName, "._http."))
2062 scheme = "http";
2063 else if (strstr(fullName, "._https."))
2064 scheme = "https";
2065 else if (strstr(fullName, "._printer."))
2066 scheme = "lpd";
2067 else if (strstr(fullName, "._pdl-datastream."))
2068 scheme = "socket";
2069 else
2070 scheme = "riousbprint";
2071
2072 /*
2073 * Extract the "remote printer" key from the TXT record...
2074 */
2075
2076 if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) &&
2077 (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
2078 !TXTRecordGetValuePtr(txtLen, txtRecord, "printer-type", &valueLen))
2079 {
2080 reskey = "rfo";
2081 resdefault = "/ipp/faxout";
2082 }
2083 else
2084 {
2085 reskey = "rp";
2086 resdefault = "/";
2087 }
2088
2089 if ((value = TXTRecordGetValuePtr(txtLen, txtRecord, reskey,
2090 &valueLen)) != NULL)
2091 {
2092 if (((char *)value)[0] == '/')
2093 {
2094 /*
2095 * Value (incorrectly) has a leading slash already...
2096 */
2097
2098 memcpy(resource, value, valueLen);
2099 resource[valueLen] = '\0';
2100 }
2101 else
2102 {
2103 /*
2104 * Convert to resource by concatenating with a leading "/"...
2105 */
2106
2107 resource[0] = '/';
2108 memcpy(resource + 1, value, valueLen);
2109 resource[valueLen + 1] = '\0';
2110 }
2111 }
2112 else
2113 {
2114 /*
2115 * Use the default value...
2116 */
2117
2118 strlcpy(resource, resdefault, sizeof(resource));
2119 }
2120
2121 /*
2122 * Lookup the FQDN if needed...
2123 */
2124
2125 if ((uribuf->options & _HTTP_RESOLVE_FQDN) &&
2126 (hostptr = hostTarget + strlen(hostTarget) - 7) > hostTarget &&
2127 !_cups_strcasecmp(hostptr, ".local."))
2128 {
2129 /*
2130 * OK, we got a .local name but the caller needs a real domain. Start by
2131 * getting the IP address of the .local name and then do reverse-lookups...
2132 */
2133
2134 http_addrlist_t *addrlist, /* List of addresses */
2135 *addr; /* Current address */
2136
2137 DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget));
2138
2139 snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port));
2140 if ((addrlist = httpAddrGetList(hostTarget, AF_UNSPEC, fqdn)) != NULL)
2141 {
2142 for (addr = addrlist; addr; addr = addr->next)
2143 {
2144 int error = getnameinfo(&(addr->addr.addr),
2145 httpAddrLength(&(addr->addr)),
2146 fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
2147
2148 if (!error)
2149 {
2150 DEBUG_printf(("8http_resolve_cb: Found \"%s\".", fqdn));
2151
2152 if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn ||
2153 _cups_strcasecmp(hostptr, ".local"))
2154 {
2155 hostTarget = fqdn;
2156 break;
2157 }
2158 }
2159 #ifdef DEBUG
2160 else
2161 DEBUG_printf(("8http_resolve_cb: \"%s\" did not resolve: %d",
2162 httpAddrString(&(addr->addr), fqdn, sizeof(fqdn)),
2163 error));
2164 #endif /* DEBUG */
2165 }
2166
2167 httpAddrFreeList(addrlist);
2168 }
2169 }
2170
2171 /*
2172 * Assemble the final device URI...
2173 */
2174
2175 if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) &&
2176 !strcmp(uribuf->resource, "/cups"))
2177 httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize,
2178 scheme, NULL, hostTarget, ntohs(port), "%s?snmp=false",
2179 resource);
2180 else
2181 httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize,
2182 scheme, NULL, hostTarget, ntohs(port), resource);
2183
2184 DEBUG_printf(("8http_resolve_cb: Resolved URI is \"%s\"...", uribuf->buffer));
2185 }
2186
2187 #elif defined(HAVE_AVAHI)
2188 /*
2189 * 'http_poll_cb()' - Wait for input on the specified file descriptors.
2190 *
2191 * Note: This function is needed because avahi_simple_poll_iterate is broken
2192 * and always uses a timeout of 0 (!) milliseconds.
2193 * (Avahi Ticket #364)
2194 */
2195
2196 static int /* O - Number of file descriptors matching */
2197 http_poll_cb(
2198 struct pollfd *pollfds, /* I - File descriptors */
2199 unsigned int num_pollfds, /* I - Number of file descriptors */
2200 int timeout, /* I - Timeout in milliseconds (used) */
2201 void *context) /* I - User data (unused) */
2202 {
2203 (void)timeout;
2204 (void)context;
2205
2206 return (poll(pollfds, num_pollfds, 2000));
2207 }
2208
2209
2210 /*
2211 * 'http_resolve_cb()' - Build a device URI for the given service name.
2212 */
2213
2214 static void
2215 http_resolve_cb(
2216 AvahiServiceResolver *resolver, /* I - Resolver (unused) */
2217 AvahiIfIndex interface, /* I - Interface index (unused) */
2218 AvahiProtocol protocol, /* I - Network protocol (unused) */
2219 AvahiResolverEvent event, /* I - Event (found, etc.) */
2220 const char *name, /* I - Service name */
2221 const char *type, /* I - Registration type */
2222 const char *domain, /* I - Domain (unused) */
2223 const char *hostTarget, /* I - Hostname */
2224 const AvahiAddress *address, /* I - Address (unused) */
2225 uint16_t port, /* I - Port number */
2226 AvahiStringList *txt, /* I - TXT record */
2227 AvahiLookupResultFlags flags, /* I - Lookup flags (unused) */
2228 void *context) /* I - Pointer to URI buffer */
2229 {
2230 _http_uribuf_t *uribuf = (_http_uribuf_t *)context;
2231 /* URI buffer */
2232 const char *scheme, /* URI scheme */
2233 *hostptr, /* Pointer into hostTarget */
2234 *reskey, /* "rp" or "rfo" */
2235 *resdefault; /* Default path */
2236 char resource[257], /* Remote path */
2237 fqdn[256]; /* FQDN of the .local name */
2238 AvahiStringList *pair; /* Current TXT record key/value pair */
2239 char *value; /* Value for "rp" key */
2240 size_t valueLen = 0; /* Length of "rp" key */
2241
2242
2243 DEBUG_printf(("7http_resolve_cb(resolver=%p, "
2244 "interface=%d, protocol=%d, event=%d, name=\"%s\", "
2245 "type=\"%s\", domain=\"%s\", hostTarget=\"%s\", address=%p, "
2246 "port=%d, txt=%p, flags=%d, context=%p)",
2247 resolver, interface, protocol, event, name, type, domain,
2248 hostTarget, address, port, txt, flags, context));
2249
2250 if (event != AVAHI_RESOLVER_FOUND)
2251 {
2252 avahi_service_resolver_free(resolver);
2253 avahi_simple_poll_quit(uribuf->poll);
2254 return;
2255 }
2256
2257 /*
2258 * Figure out the scheme from the full name...
2259 */
2260
2261 if (strstr(type, "_ipp."))
2262 scheme = "ipp";
2263 else if (strstr(type, "_printer."))
2264 scheme = "lpd";
2265 else if (strstr(type, "_pdl-datastream."))
2266 scheme = "socket";
2267 else
2268 scheme = "riousbprint";
2269
2270 if (!strncmp(type, "_ipps.", 6) || !strncmp(type, "_ipp-tls.", 9))
2271 scheme = "ipps";
2272 else if (!strncmp(type, "_ipp.", 5) || !strncmp(type, "_fax-ipp.", 9))
2273 scheme = "ipp";
2274 else if (!strncmp(type, "_http.", 6))
2275 scheme = "http";
2276 else if (!strncmp(type, "_https.", 7))
2277 scheme = "https";
2278 else if (!strncmp(type, "_printer.", 9))
2279 scheme = "lpd";
2280 else if (!strncmp(type, "_pdl-datastream.", 16))
2281 scheme = "socket";
2282 else
2283 {
2284 avahi_service_resolver_free(resolver);
2285 avahi_simple_poll_quit(uribuf->poll);
2286 return;
2287 }
2288
2289 /*
2290 * Extract the remote resource key from the TXT record...
2291 */
2292
2293 if ((uribuf->options & _HTTP_RESOLVE_FAXOUT) &&
2294 (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")))
2295 {
2296 reskey = "rfo";
2297 resdefault = "/ipp/faxout";
2298 }
2299 else
2300 {
2301 reskey = "rp";
2302 resdefault = "/";
2303 }
2304
2305 if ((pair = avahi_string_list_find(txt, reskey)) != NULL)
2306 {
2307 avahi_string_list_get_pair(pair, NULL, &value, &valueLen);
2308
2309 if (value[0] == '/')
2310 {
2311 /*
2312 * Value (incorrectly) has a leading slash already...
2313 */
2314
2315 memcpy(resource, value, valueLen);
2316 resource[valueLen] = '\0';
2317 }
2318 else
2319 {
2320 /*
2321 * Convert to resource by concatenating with a leading "/"...
2322 */
2323
2324 resource[0] = '/';
2325 memcpy(resource + 1, value, valueLen);
2326 resource[valueLen + 1] = '\0';
2327 }
2328 }
2329 else
2330 {
2331 /*
2332 * Use the default value...
2333 */
2334
2335 strlcpy(resource, resdefault, sizeof(resource));
2336 }
2337
2338 /*
2339 * Lookup the FQDN if needed...
2340 */
2341
2342 if ((uribuf->options & _HTTP_RESOLVE_FQDN) &&
2343 (hostptr = hostTarget + strlen(hostTarget) - 6) > hostTarget &&
2344 !_cups_strcasecmp(hostptr, ".local"))
2345 {
2346 /*
2347 * OK, we got a .local name but the caller needs a real domain. Start by
2348 * getting the IP address of the .local name and then do reverse-lookups...
2349 */
2350
2351 http_addrlist_t *addrlist, /* List of addresses */
2352 *addr; /* Current address */
2353
2354 DEBUG_printf(("8http_resolve_cb: Looking up \"%s\".", hostTarget));
2355
2356 snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port));
2357 if ((addrlist = httpAddrGetList(hostTarget, AF_UNSPEC, fqdn)) != NULL)
2358 {
2359 for (addr = addrlist; addr; addr = addr->next)
2360 {
2361 int error = getnameinfo(&(addr->addr.addr),
2362 httpAddrLength(&(addr->addr)),
2363 fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD);
2364
2365 if (!error)
2366 {
2367 DEBUG_printf(("8http_resolve_cb: Found \"%s\".", fqdn));
2368
2369 if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn ||
2370 _cups_strcasecmp(hostptr, ".local"))
2371 {
2372 hostTarget = fqdn;
2373 break;
2374 }
2375 }
2376 #ifdef DEBUG
2377 else
2378 DEBUG_printf(("8http_resolve_cb: \"%s\" did not resolve: %d",
2379 httpAddrString(&(addr->addr), fqdn, sizeof(fqdn)),
2380 error));
2381 #endif /* DEBUG */
2382 }
2383
2384 httpAddrFreeList(addrlist);
2385 }
2386 }
2387
2388 /*
2389 * Assemble the final device URI using the resolved hostname...
2390 */
2391
2392 httpAssembleURI(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme,
2393 NULL, hostTarget, port, resource);
2394 DEBUG_printf(("8http_resolve_cb: Resolved URI is \"%s\".", uribuf->buffer));
2395
2396 avahi_simple_poll_quit(uribuf->poll);
2397 }
2398 #endif /* HAVE_DNSSD */
2399
2400
2401 /*
2402 * End of "$Id$".
2403 */