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