2 * HTTP support routines for CUPS.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
25 # elif defined(HAVE_POLL)
28 # include <sys/select.h>
30 #elif defined(HAVE_AVAHI)
31 # include <avahi-client/client.h>
32 # include <avahi-client/lookup.h>
33 # include <avahi-common/simple-watch.h>
34 #endif /* HAVE_DNSSD */
41 typedef struct _http_uribuf_s
/* URI buffer */
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 */
49 const char *resource
; /* Resource from URI */
50 const char *uuid
; /* UUID from URI */
58 static const char * const http_days
[7] =/* Days of the week */
68 static const char * const http_months
[12] =
69 { /* Months of the year */
83 static const char * const http_states
[] =
84 { /* HTTP state strings */
89 "HTTP_STATE_GET_SEND",
92 "HTTP_STATE_POST_RECV",
93 "HTTP_STATE_POST_SEND",
95 "HTTP_STATE_PUT_RECV",
100 "HTTP_STATE_UNKNOWN_METHOD",
101 "HTTP_STATE_UNKNOWN_VERSION"
109 static const char *http_copy_decode(char *dst
, const char *src
,
110 int dstsize
, const char *term
,
112 static char *http_copy_encode(char *dst
, const char *src
,
113 char *dstend
, const char *reserved
,
114 const char *term
, int encode
);
116 static 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
,
125 #endif /* HAVE_DNSSD */
128 static void http_client_cb(AvahiClient
*client
,
129 AvahiClientState state
, void *simple_poll
);
130 static int http_poll_cb(struct pollfd
*pollfds
, unsigned int num_pollfds
,
131 int timeout
, void *context
);
132 static 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 */
145 * 'httpAssembleURI()' - Assemble a uniform resource identifier from its
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
153 * @since CUPS 1.2/macOS 10.5@
156 http_uri_status_t
/* O - URI status */
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 */
167 char *ptr
, /* Pointer into URI buffer */
168 *end
; /* End of URI buffer */
172 * Range check input...
175 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0)
180 return (HTTP_URI_STATUS_BAD_ARGUMENTS
);
184 * Assemble the URI starting with the scheme...
187 end
= uri
+ urilen
- 1;
188 ptr
= http_copy_encode(uri
, scheme
, end
, NULL
, NULL
, 0);
191 goto assemble_overflow
;
193 if (!strcmp(scheme
, "geo") || !strcmp(scheme
, "mailto") || !strcmp(scheme
, "tel"))
196 * geo:, mailto:, and tel: only have :, no //...
202 goto assemble_overflow
;
207 * Schemes other than geo:, mailto:, and tel: typically have //...
217 goto assemble_overflow
;
221 * Next the username and hostname, if any...
226 const char *hostptr
; /* Pointer into hostname */
227 int have_ipv6
; /* Do we have an IPv6 address? */
229 if (username
&& *username
)
232 * Add username@ first...
235 ptr
= http_copy_encode(ptr
, username
, end
, "/?#[]@", NULL
,
236 encoding
& HTTP_URI_CODING_USERNAME
);
239 goto assemble_overflow
;
244 goto assemble_overflow
;
248 * Then add the hostname. Since IPv6 is a particular pain to deal
249 * with, we have several special cases to deal with. If we get
250 * an IPv6 address with brackets around it, assume it is already in
251 * URI format. Since DNS-SD service names can sometimes look like
252 * raw IPv6 addresses, we specifically look for "._tcp" in the name,
257 have_ipv6
= strchr(host
, ':') && !strstr(host
, "._tcp");
258 *hostptr
&& have_ipv6
;
260 if (*hostptr
!= ':' && !isxdigit(*hostptr
& 255))
262 have_ipv6
= *hostptr
== '%';
269 * We have a raw IPv6 address...
272 if (strchr(host
, '%') && !(encoding
& HTTP_URI_CODING_RFC6874
))
275 * We have a link-local address, add "[v1." prefix...
286 goto assemble_overflow
;
291 * We have a normal (or RFC 6874 link-local) address, add "[" prefix...
297 goto assemble_overflow
;
301 * Copy the rest of the IPv6 address, and terminate with "]".
304 while (ptr
< end
&& *host
)
309 * Convert/encode zone separator
312 if (encoding
& HTTP_URI_CODING_RFC6874
)
314 if (ptr
>= (end
- 2))
315 goto assemble_overflow
;
331 goto assemble_overflow
;
336 goto assemble_overflow
;
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
346 ptr
= http_copy_encode(ptr
, host
, end
, "\"#/:<>?@[\\]^`{|}", NULL
,
347 encoding
& HTTP_URI_CODING_HOSTNAME
);
350 goto assemble_overflow
;
354 * Finish things off with the port number...
359 snprintf(ptr
, (size_t)(end
- ptr
+ 1), ":%d", port
);
363 goto assemble_overflow
;
368 * Last but not least, add the resource string...
373 char *query
; /* Pointer to query string */
377 * Copy the resource string up to the query string if present...
380 query
= strchr(resource
, '?');
381 ptr
= http_copy_encode(ptr
, resource
, end
, NULL
, "?",
382 encoding
& HTTP_URI_CODING_RESOURCE
);
384 goto assemble_overflow
;
389 * Copy query string without encoding...
392 ptr
= http_copy_encode(ptr
, query
, end
, NULL
, NULL
,
393 encoding
& HTTP_URI_CODING_QUERY
);
395 goto assemble_overflow
;
401 goto assemble_overflow
;
404 * Nul-terminate the URI buffer and return with no errors...
409 return (HTTP_URI_STATUS_OK
);
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...
419 return (HTTP_URI_STATUS_OVERFLOW
);
424 * 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
425 * components with a formatted resource.
427 * This function creates a formatted version of the resource string
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.
433 * @since CUPS 1.2/macOS 10.5@
436 http_uri_status_t
/* O - URI status */
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 */
448 va_list ap
; /* Pointer to additional arguments */
449 char resource
[1024]; /* Formatted resource string */
450 int bytes
; /* Bytes in formatted string */
454 * Range check input...
457 if (!uri
|| urilen
< 1 || !scheme
|| port
< 0 || !resourcef
)
462 return (HTTP_URI_STATUS_BAD_ARGUMENTS
);
466 * Format the resource string and assemble the URI...
469 va_start(ap
, resourcef
);
470 bytes
= vsnprintf(resource
, sizeof(resource
), resourcef
, ap
);
473 if ((size_t)bytes
>= sizeof(resource
))
476 return (HTTP_URI_STATUS_OVERFLOW
);
479 return (httpAssembleURI(encoding
, uri
, urilen
, scheme
, username
, host
,
485 * 'httpAssembleUUID()' - Assemble a name-based UUID URN conforming to RFC 4122.
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.
491 * The buffer needs to be at least 46 bytes in size.
493 * @since CUPS 1.7/macOS 10.9@
496 char * /* I - UUID string */
497 httpAssembleUUID(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 */
504 char data
[1024]; /* Source string for MD5 */
505 unsigned char md5sum
[16]; /* MD5 digest/sum */
509 * Build a version 3 UUID conforming to RFC 4122.
511 * Start with the MD5 sum of the server, port, object name and
512 * number, and some random data on the end.
515 snprintf(data
, sizeof(data
), "%s:%d:%s:%d:%04x:%04x", server
,
516 port
, name
? name
: server
, number
,
517 (unsigned)CUPS_RAND() & 0xffff, (unsigned)CUPS_RAND() & 0xffff);
519 cupsHashData("md5", (unsigned char *)data
, strlen(data
), md5sum
, sizeof(md5sum
));
522 * Generate the UUID from the MD5...
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]);
538 * 'httpDecode64()' - Base64-decode a string.
540 * This function is deprecated. Use the httpDecode64_2() function instead
541 * which provides buffer length arguments.
543 * @deprecated@ @exclude all@
546 char * /* O - Decoded string */
547 httpDecode64(char *out
, /* I - String to write to */
548 const char *in
) /* I - String to read from */
550 int outlen
; /* Output buffer length */
554 * Use the old maximum buffer size for binary compatibility...
559 return (httpDecode64_2(out
, &outlen
, in
));
564 * 'httpDecode64_2()' - Base64-decode a string.
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.
570 * @since CUPS 1.1.21/macOS 10.4@
573 char * /* O - Decoded string */
574 httpDecode64_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 */
578 int pos
; /* Bit position */
579 unsigned base64
; /* Value of this character */
580 char *outptr
, /* Output pointer */
581 *outend
; /* End of output buffer */
585 * Range check input...
588 if (!out
|| !outlen
|| *outlen
< 1 || !in
)
600 * Convert from base-64 to bytes...
603 for (outptr
= out
, outend
= out
+ *outlen
- 1, pos
= 0; *in
!= '\0'; in
++)
606 * Decode this character into a number from 0 to 63...
609 if (*in
>= 'A' && *in
<= 'Z')
610 base64
= (unsigned)(*in
- 'A');
611 else if (*in
>= 'a' && *in
<= 'z')
612 base64
= (unsigned)(*in
- 'a' + 26);
613 else if (*in
>= '0' && *in
<= '9')
614 base64
= (unsigned)(*in
- '0' + 52);
625 * Store the result in the appropriate chars...
632 *outptr
= (char)(base64
<< 2);
637 *outptr
++ |= (char)((base64
>> 4) & 3);
639 *outptr
= (char)((base64
<< 4) & 255);
644 *outptr
++ |= (char)((base64
>> 2) & 15);
646 *outptr
= (char)((base64
<< 6) & 255);
651 *outptr
++ |= (char)base64
;
660 * Return the decoded string and size...
663 *outlen
= (int)(outptr
- out
);
670 * 'httpEncode64()' - Base64-encode a string.
672 * This function is deprecated. Use the httpEncode64_2() function instead
673 * which provides buffer length arguments.
675 * @deprecated@ @exclude all@
678 char * /* O - Encoded string */
679 httpEncode64(char *out
, /* I - String to write to */
680 const char *in
) /* I - String to read from */
682 return (httpEncode64_2(out
, 512, in
, (int)strlen(in
)));
687 * 'httpEncode64_2()' - Base64-encode a string.
689 * @since CUPS 1.1.21/macOS 10.4@
692 char * /* O - Encoded string */
693 httpEncode64_2(char *out
, /* I - String to write to */
694 int outlen
, /* I - Maximum size of output string */
695 const char *in
, /* I - String to read from */
696 int inlen
) /* I - Size of input string */
698 char *outptr
, /* Output pointer */
699 *outend
; /* End of output buffer */
700 static const char base64
[] = /* Base64 characters... */
702 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
703 "abcdefghijklmnopqrstuvwxyz"
710 * Range check input...
713 if (!out
|| outlen
< 1 || !in
)
717 * Convert bytes to base-64...
720 for (outptr
= out
, outend
= out
+ outlen
- 1; inlen
> 0; in
++, inlen
--)
723 * Encode the up to 3 characters as 4 Base64 numbers...
727 *outptr
++ = base64
[(in
[0] & 255) >> 2];
732 *outptr
++ = base64
[(((in
[0] & 255) << 4) | ((in
[1] & 255) >> 4)) & 63];
734 *outptr
++ = base64
[((in
[0] & 255) << 4) & 63];
751 *outptr
++ = base64
[(((in
[0] & 255) << 2) | ((in
[1] & 255) >> 6)) & 63];
753 *outptr
++ = base64
[((in
[0] & 255) << 2) & 63];
766 *outptr
++ = base64
[in
[0] & 63];
772 * Return the encoded string...
780 * 'httpGetDateString()' - Get a formatted date/time string from a time value.
782 * @deprecated@ @exclude all@
785 const char * /* O - Date/time string */
786 httpGetDateString(time_t t
) /* I - Time in seconds */
788 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
791 return (httpGetDateString2(t
, cg
->http_date
, sizeof(cg
->http_date
)));
796 * 'httpGetDateString2()' - Get a formatted date/time string from a time value.
798 * @since CUPS 1.2/macOS 10.5@
801 const char * /* O - Date/time string */
802 httpGetDateString2(time_t t
, /* I - Time in seconds */
803 char *s
, /* I - String buffer */
804 int slen
) /* I - Size of string buffer */
806 struct tm
*tdate
; /* UNIX date/time data */
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
);
820 * 'httpGetDateTime()' - Get a time value from a formatted date/time string.
823 time_t /* O - Time in seconds */
824 httpGetDateTime(const char *s
) /* I - Date/time string */
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 };
837 DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s
));
840 * Extract the date and time from the formatted string...
843 if (sscanf(s
, "%*s%d%15s%d%d:%d:%d", &day
, mon
, &year
, &hour
, &min
, &sec
) < 6)
846 DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
847 "min=%d, sec=%d", day
, mon
, year
, hour
, min
, sec
));
850 * Convert the month name to a number from 0 to 11.
853 for (i
= 0; i
< 12; i
++)
854 if (!_cups_strcasecmp(mon
, http_months
[i
]))
860 DEBUG_printf(("4httpGetDateTime: i=%d", i
));
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.
868 if ((year
& 3) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
869 days
= leap_days
[i
] + day
- 1;
871 days
= normal_days
[i
] + day
- 1;
873 DEBUG_printf(("4httpGetDateTime: days=%d", days
));
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 */
880 DEBUG_printf(("4httpGetDateTime: days=%d\n", days
));
882 return (days
* 86400 + hour
* 3600 + min
* 60 + sec
);
887 * 'httpSeparate()' - Separate a Universal Resource Identifier into its
890 * This function is deprecated; use the httpSeparateURI() function instead.
892 * @deprecated@ @exclude all@
896 httpSeparate(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] */
903 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, 32, username
,
904 HTTP_MAX_URI
, host
, HTTP_MAX_URI
, port
, resource
,
910 * 'httpSeparate2()' - Separate a Universal Resource Identifier into its
913 * This function is deprecated; use the httpSeparateURI() function instead.
915 * @since CUPS 1.1.21/macOS 10.4@
916 * @deprecated@ @exclude all@
920 httpSeparate2(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 */
931 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, schemelen
, username
,
932 usernamelen
, host
, hostlen
, port
, resource
, resourcelen
);
937 * 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
940 * @since CUPS 1.2/macOS 10.5@
943 http_uri_status_t
/* O - Result of separation */
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 */
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 */
964 * Initialize everything to blank...
967 if (scheme
&& schemelen
> 0)
970 if (username
&& usernamelen
> 0)
973 if (host
&& hostlen
> 0)
979 if (resource
&& resourcelen
> 0)
983 * Range check input...
986 if (!uri
|| !port
|| !scheme
|| schemelen
<= 0 || !username
||
987 usernamelen
<= 0 || !host
|| hostlen
<= 0 || !resource
||
989 return (HTTP_URI_STATUS_BAD_ARGUMENTS
);
992 return (HTTP_URI_STATUS_BAD_URI
);
995 * Grab the scheme portion of the URI...
998 status
= HTTP_URI_STATUS_OK
;
1000 if (!strncmp(uri
, "//", 2))
1003 * Workaround for HP IPP client bug...
1006 strlcpy(scheme
, "ipp", (size_t)schemelen
);
1007 status
= HTTP_URI_STATUS_MISSING_SCHEME
;
1009 else if (*uri
== '/')
1015 strlcpy(scheme
, "file", (size_t)schemelen
);
1016 status
= HTTP_URI_STATUS_MISSING_SCHEME
;
1021 * Standard URI with scheme...
1024 for (ptr
= scheme
, end
= scheme
+ schemelen
- 1;
1025 *uri
&& *uri
!= ':' && ptr
< end
;)
1026 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1027 "abcdefghijklmnopqrstuvwxyz"
1028 "0123456789-+.", *uri
) != NULL
)
1035 if (*uri
!= ':' || *scheme
== '.' || !*scheme
)
1038 return (HTTP_URI_STATUS_BAD_SCHEME
);
1045 * Set the default port number...
1048 if (!strcmp(scheme
, "http"))
1050 else if (!strcmp(scheme
, "https"))
1052 else if (!strcmp(scheme
, "ipp") || !strcmp(scheme
, "ipps"))
1054 else if (!_cups_strcasecmp(scheme
, "lpd"))
1056 else if (!strcmp(scheme
, "socket")) /* Not yet registered with IANA... */
1058 else if (strcmp(scheme
, "file") && strcmp(scheme
, "mailto") && strcmp(scheme
, "tel"))
1059 status
= HTTP_URI_STATUS_UNKNOWN_SCHEME
;
1062 * Now see if we have a hostname...
1065 if (!strncmp(uri
, "//", 2))
1068 * Yes, extract it...
1074 * Grab the username, if any...
1077 if ((sep
= strpbrk(uri
, "@/")) != NULL
&& *sep
== '@')
1080 * Get a username:password combo...
1083 uri
= http_copy_decode(username
, uri
, usernamelen
, "@",
1084 decoding
& HTTP_URI_CODING_USERNAME
);
1089 return (HTTP_URI_STATUS_BAD_USERNAME
);
1096 * Then the hostname/IP address...
1102 * Grab IPv6 address...
1109 * Skip IPvFuture ("vXXXX.") prefix...
1114 while (isxdigit(*uri
& 255))
1120 return (HTTP_URI_STATUS_BAD_HOSTNAME
);
1126 uri
= http_copy_decode(host
, uri
, hostlen
, "]",
1127 decoding
& HTTP_URI_CODING_HOSTNAME
);
1132 return (HTTP_URI_STATUS_BAD_HOSTNAME
);
1142 return (HTTP_URI_STATUS_BAD_HOSTNAME
);
1147 for (ptr
= host
; *ptr
; ptr
++)
1151 * Convert zone separator to % and stop here...
1157 else if (*ptr
== '%')
1160 * Stop at zone separator (RFC 6874)
1165 else if (*ptr
!= ':' && *ptr
!= '.' && !isxdigit(*ptr
& 255))
1168 return (HTTP_URI_STATUS_BAD_HOSTNAME
);
1174 * Validate the hostname or IPv4 address first...
1177 for (ptr
= (char *)uri
; *ptr
; ptr
++)
1178 if (strchr(":?/", *ptr
))
1180 else if (!strchr("abcdefghijklmnopqrstuvwxyz" /* unreserved */
1181 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* unreserved */
1182 "0123456789" /* unreserved */
1183 "-._~" /* unreserved */
1184 "%" /* pct-encoded */
1185 "!$&'()*+,;=" /* sub-delims */
1186 "\\", *ptr
)) /* SMB domain */
1189 return (HTTP_URI_STATUS_BAD_HOSTNAME
);
1193 * Then copy the hostname or IPv4 address to the buffer...
1196 uri
= http_copy_decode(host
, uri
, hostlen
, ":?/",
1197 decoding
& HTTP_URI_CODING_HOSTNAME
);
1202 return (HTTP_URI_STATUS_BAD_HOSTNAME
);
1207 * Validate hostname for file scheme - only empty and localhost are
1211 if (!strcmp(scheme
, "file") && strcmp(host
, "localhost") && host
[0])
1214 return (HTTP_URI_STATUS_BAD_HOSTNAME
);
1218 * See if we have a port number...
1224 * Yes, collect the port number...
1227 if (!isdigit(uri
[1] & 255))
1230 return (HTTP_URI_STATUS_BAD_PORT
);
1233 *port
= (int)strtol(uri
+ 1, (char **)&uri
, 10);
1235 if (*port
<= 0 || *port
> 65535)
1238 return (HTTP_URI_STATUS_BAD_PORT
);
1241 if (*uri
!= '/' && *uri
)
1244 return (HTTP_URI_STATUS_BAD_PORT
);
1250 * The remaining portion is the resource string...
1253 if (*uri
== '?' || !*uri
)
1256 * Hostname but no path...
1259 status
= HTTP_URI_STATUS_MISSING_RESOURCE
;
1263 * Copy any query string...
1267 uri
= http_copy_decode(resource
+ 1, uri
, resourcelen
- 1, NULL
,
1268 decoding
& HTTP_URI_CODING_QUERY
);
1274 uri
= http_copy_decode(resource
, uri
, resourcelen
, "?",
1275 decoding
& HTTP_URI_CODING_RESOURCE
);
1277 if (uri
&& *uri
== '?')
1280 * Concatenate any query string...
1283 char *resptr
= resource
+ strlen(resource
);
1285 uri
= http_copy_decode(resptr
, uri
,
1286 resourcelen
- (int)(resptr
- resource
), NULL
,
1287 decoding
& HTTP_URI_CODING_QUERY
);
1294 return (HTTP_URI_STATUS_BAD_RESOURCE
);
1298 * Return the URI separation status...
1306 * '_httpSetDigestAuthString()' - Calculate a Digest authentication response
1307 * using the appropriate RFC 2068/2617/7616
1311 int /* 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 */
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 */
1330 DEBUG_printf(("2_httpSetDigestAuthString(http=%p, nonce=\"%s\", method=\"%s\", resource=\"%s\")", http
, nonce
, method
, resource
));
1332 if (nonce
&& *nonce
&& strcmp(nonce
, http
->nonce
))
1334 strlcpy(http
->nonce
, nonce
, sizeof(http
->nonce
));
1336 if (nonce
== http
->nextnonce
)
1337 http
->nextnonce
[0] = '\0';
1339 http
->nonce_count
= 1;
1342 http
->nonce_count
++;
1344 strlcpy(username
, http
->userpass
, sizeof(username
));
1345 if ((password
= strchr(username
, ':')) != NULL
)
1350 if (http
->algorithm
[0])
1353 * Follow RFC 2617/7616...
1356 int i
; /* Looping var */
1357 char cnonce
[65]; /* cnonce value */
1358 const char *hashalg
; /* Hashing algorithm */
1360 for (i
= 0; i
< 64; i
++)
1361 cnonce
[i
] = "0123456789ABCDEF"[CUPS_RAND() & 15];
1364 if (!_cups_strcasecmp(http
->algorithm
, "MD5"))
1367 * RFC 2617 Digest with MD5
1372 else if (!_cups_strcasecmp(http
->algorithm
, "SHA-256"))
1375 * RFC 7616 Digest with SHA-256
1378 hashalg
= "sha2-256";
1383 * Some other algorithm we don't support, skip this one...
1390 * Calculate digest value...
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
));
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
));
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
));
1409 * Pass the RFC 2617/7616 WWW-Authenticate header...
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
);
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
);
1420 * Use old RFC 2069 Digest method...
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
));
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
));
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
));
1439 * Pass the old RFC 2069 WWW-Authenticate header...
1442 snprintf(digest
, sizeof(digest
), "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", username
, http
->realm
, http
->nonce
, resource
, kd
);
1445 httpSetAuthString(http
, "Digest", digest
);
1452 * 'httpStateString()' - Return the string describing a HTTP state value.
1454 * @since CUPS 2.0/OS 10.10@
1457 const char * /* O - State string */
1458 httpStateString(http_state_t state
) /* I - HTTP state value */
1460 if (state
< HTTP_STATE_ERROR
|| state
> HTTP_STATE_UNKNOWN_VERSION
)
1461 return ("HTTP_STATE_???");
1463 return (http_states
[state
- HTTP_STATE_ERROR
]);
1468 * '_httpStatus()' - Return the localized string describing a HTTP status code.
1470 * The returned string is localized using the passed message catalog.
1473 const char * /* O - Localized status string */
1474 _httpStatus(cups_lang_t
*lang
, /* I - Language */
1475 http_status_t status
) /* I - HTTP status code */
1477 const char *s
; /* Status string */
1482 case HTTP_STATUS_ERROR
:
1483 s
= strerror(errno
);
1485 case HTTP_STATUS_CONTINUE
:
1488 case HTTP_STATUS_SWITCHING_PROTOCOLS
:
1489 s
= _("Switching Protocols");
1491 case HTTP_STATUS_OK
:
1494 case HTTP_STATUS_CREATED
:
1497 case HTTP_STATUS_ACCEPTED
:
1500 case HTTP_STATUS_NO_CONTENT
:
1501 s
= _("No Content");
1503 case HTTP_STATUS_MOVED_PERMANENTLY
:
1504 s
= _("Moved Permanently");
1506 case HTTP_STATUS_FOUND
:
1509 case HTTP_STATUS_SEE_OTHER
:
1512 case HTTP_STATUS_NOT_MODIFIED
:
1513 s
= _("Not Modified");
1515 case HTTP_STATUS_BAD_REQUEST
:
1516 s
= _("Bad Request");
1518 case HTTP_STATUS_UNAUTHORIZED
:
1519 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
:
1520 s
= _("Unauthorized");
1522 case HTTP_STATUS_FORBIDDEN
:
1525 case HTTP_STATUS_NOT_FOUND
:
1528 case HTTP_STATUS_REQUEST_TOO_LARGE
:
1529 s
= _("Request Entity Too Large");
1531 case HTTP_STATUS_URI_TOO_LONG
:
1532 s
= _("URI Too Long");
1534 case HTTP_STATUS_UPGRADE_REQUIRED
:
1535 s
= _("Upgrade Required");
1537 case HTTP_STATUS_NOT_IMPLEMENTED
:
1538 s
= _("Not Implemented");
1540 case HTTP_STATUS_NOT_SUPPORTED
:
1541 s
= _("Not Supported");
1543 case HTTP_STATUS_EXPECTATION_FAILED
:
1544 s
= _("Expectation Failed");
1546 case HTTP_STATUS_SERVICE_UNAVAILABLE
:
1547 s
= _("Service Unavailable");
1549 case HTTP_STATUS_SERVER_ERROR
:
1550 s
= _("Internal Server Error");
1552 case HTTP_STATUS_CUPS_PKI_ERROR
:
1553 s
= _("SSL/TLS Negotiation Error");
1555 case HTTP_STATUS_CUPS_WEBIF_DISABLED
:
1556 s
= _("Web Interface is Disabled");
1564 return (_cupsLangString(lang
, s
));
1569 * 'httpStatus()' - Return a short string describing a HTTP status code.
1571 * The returned string is localized to the current POSIX locale and is based
1572 * on the status strings defined in RFC 7231.
1575 const char * /* O - Localized status string */
1576 httpStatus(http_status_t status
) /* I - HTTP status code */
1578 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1581 if (!cg
->lang_default
)
1582 cg
->lang_default
= cupsLangDefault();
1584 return (_httpStatus(cg
->lang_default
, status
));
1588 * 'httpURIStatusString()' - Return a string describing a URI status code.
1590 * @since CUPS 2.0/OS 10.10@
1593 const char * /* O - Localized status string */
1594 httpURIStatusString(
1595 http_uri_status_t status
) /* I - URI status code */
1597 const char *s
; /* Status string */
1598 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1601 if (!cg
->lang_default
)
1602 cg
->lang_default
= cupsLangDefault();
1606 case HTTP_URI_STATUS_OVERFLOW
:
1607 s
= _("URI too large");
1609 case HTTP_URI_STATUS_BAD_ARGUMENTS
:
1610 s
= _("Bad arguments to function");
1612 case HTTP_URI_STATUS_BAD_RESOURCE
:
1613 s
= _("Bad resource in URI");
1615 case HTTP_URI_STATUS_BAD_PORT
:
1616 s
= _("Bad port number in URI");
1618 case HTTP_URI_STATUS_BAD_HOSTNAME
:
1619 s
= _("Bad hostname/address in URI");
1621 case HTTP_URI_STATUS_BAD_USERNAME
:
1622 s
= _("Bad username in URI");
1624 case HTTP_URI_STATUS_BAD_SCHEME
:
1625 s
= _("Bad scheme in URI");
1627 case HTTP_URI_STATUS_BAD_URI
:
1628 s
= _("Bad/empty URI");
1630 case HTTP_URI_STATUS_OK
:
1633 case HTTP_URI_STATUS_MISSING_SCHEME
:
1634 s
= _("Missing scheme in URI");
1636 case HTTP_URI_STATUS_UNKNOWN_SCHEME
:
1637 s
= _("Unknown scheme in URI");
1639 case HTTP_URI_STATUS_MISSING_RESOURCE
:
1640 s
= _("Missing resource in URI");
1648 return (_cupsLangString(cg
->lang_default
, s
));
1652 #ifndef HAVE_HSTRERROR
1654 * '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others.
1657 const char * /* O - Error string */
1658 _cups_hstrerror(int error
) /* I - Error number */
1660 static const char * const errors
[] = /* Error strings */
1665 "Unrecoverable lookup error.",
1666 "No data associated with name."
1670 if (error
< 0 || error
> 4)
1671 return ("Unknown hostname lookup error.");
1673 return (errors
[error
]);
1675 #endif /* !HAVE_HSTRERROR */
1679 * '_httpDecodeURI()' - Percent-decode a HTTP request URI.
1682 char * /* 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 */
1687 if (http_copy_decode(dst
, src
, (int)dstsize
, NULL
, 1))
1695 * '_httpEncodeURI()' - Percent-encode a HTTP request URI.
1698 char * /* 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 */
1703 http_copy_encode(dst
, src
, dst
+ dstsize
- 1, NULL
, NULL
, 1);
1709 * '_httpResolveURI()' - Resolve a DNS-SD URI.
1712 const char * /* O - Resolved URI */
1714 const char *uri
, /* I - DNS-SD URI */
1715 char *resolved_uri
, /* I - Buffer for resolved URI */
1716 size_t resolved_size
, /* I - Size of URI buffer */
1717 int options
, /* I - Resolve options */
1718 int (*cb
)(void *context
), /* I - Continue callback function */
1719 void *context
) /* I - Context pointer for callback */
1721 char scheme
[32], /* URI components... */
1727 http_uri_status_t status
; /* URI decode status */
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
));
1734 * Get the device URI...
1738 if ((status
= httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1739 sizeof(scheme
), userpass
, sizeof(userpass
),
1740 hostname
, sizeof(hostname
), &port
, resource
,
1741 sizeof(resource
))) < HTTP_URI_STATUS_OK
)
1743 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
,
1744 sizeof(scheme
), userpass
, sizeof(userpass
),
1745 hostname
, sizeof(hostname
), &port
, resource
,
1746 sizeof(resource
)) < HTTP_URI_STATUS_OK
)
1749 if (options
& _HTTP_RESOLVE_STDERR
)
1750 _cupsLangPrintFilter(stderr
, "ERROR", _("Bad device-uri \"%s\"."), uri
);
1752 DEBUG_printf(("2_httpResolveURI: httpSeparateURI returned %d!", status
));
1753 DEBUG_puts("2_httpResolveURI: Returning NULL");
1758 * Resolve it as needed...
1761 if (strstr(hostname
, "._tcp"))
1763 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
1764 char *regtype
, /* Pointer to type in hostname */
1765 *domain
, /* Pointer to domain in hostname */
1766 *uuid
, /* Pointer to UUID in URI */
1767 *uuidend
; /* Pointer to end of UUID in URI */
1768 _http_uribuf_t uribuf
; /* URI buffer */
1769 int offline
= 0; /* offline-report state set? */
1771 DNSServiceRef ref
, /* DNS-SD master service reference */
1772 domainref
= NULL
,/* DNS-SD service reference for domain */
1773 ippref
= NULL
, /* DNS-SD service reference for network IPP */
1774 ippsref
= NULL
, /* DNS-SD service reference for network IPPS */
1775 localref
; /* DNS-SD service reference for .local */
1776 int extrasent
= 0; /* Send the domain/IPP/IPPS resolves? */
1778 struct pollfd polldata
; /* Polling data */
1779 # else /* select() */
1780 fd_set input_set
; /* Input set for select() */
1781 struct timeval stimeout
; /* Timeout value for select() */
1782 # endif /* HAVE_POLL */
1783 # elif defined(HAVE_AVAHI)
1784 AvahiClient
*client
; /* Client information */
1785 int error
; /* Status */
1786 # endif /* HAVE_DNSSD */
1788 if (options
& _HTTP_RESOLVE_STDERR
)
1789 fprintf(stderr
, "DEBUG: Resolving \"%s\"...\n", hostname
);
1792 * Separate the hostname into service name, registration type, and domain...
1795 for (regtype
= strstr(hostname
, "._tcp") - 2;
1798 if (regtype
[0] == '.' && regtype
[1] == '_')
1801 * Found ._servicetype in front of ._tcp...
1808 if (regtype
<= hostname
)
1810 DEBUG_puts("2_httpResolveURI: Bad hostname, returning NULL");
1814 for (domain
= strchr(regtype
, '.');
1816 domain
= strchr(domain
+ 1, '.'))
1817 if (domain
[1] != '_')
1823 if ((uuid
= strstr(resource
, "?uuid=")) != NULL
)
1827 if ((uuidend
= strchr(uuid
, '&')) != NULL
)
1831 resolved_uri
[0] = '\0';
1833 uribuf
.buffer
= resolved_uri
;
1834 uribuf
.bufsize
= resolved_size
;
1835 uribuf
.options
= options
;
1836 uribuf
.resource
= resource
;
1839 DEBUG_printf(("2_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
1840 "domain=\"%s\"\n", hostname
, regtype
, domain
));
1841 if (options
& _HTTP_RESOLVE_STDERR
)
1843 fputs("STATE: +connecting-to-device\n", stderr
);
1844 fprintf(stderr
, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1845 "domain=\"local.\"...\n", hostname
, regtype
);
1851 if (DNSServiceCreateConnection(&ref
) == kDNSServiceErr_NoError
)
1853 uint32_t myinterface
= kDNSServiceInterfaceIndexAny
;
1854 /* Lookup on any interface */
1856 if (!strcmp(scheme
, "ippusb"))
1857 myinterface
= kDNSServiceInterfaceIndexLocalOnly
;
1860 if (DNSServiceResolve(&localref
,
1861 kDNSServiceFlagsShareConnection
, myinterface
,
1862 hostname
, regtype
, "local.", http_resolve_cb
,
1863 &uribuf
) == kDNSServiceErr_NoError
)
1865 int fds
; /* Number of ready descriptors */
1866 time_t timeout
, /* Poll timeout */
1867 start_time
= time(NULL
),/* Start time */
1868 end_time
= start_time
+ 90;
1871 while (time(NULL
) < end_time
)
1873 if (options
& _HTTP_RESOLVE_STDERR
)
1874 _cupsLangPrintFilter(stderr
, "INFO", _("Looking for printer."));
1876 if (cb
&& !(*cb
)(context
))
1878 DEBUG_puts("2_httpResolveURI: callback returned 0 (stop)");
1883 * Wakeup every 2 seconds to emit a "looking for printer" message...
1886 if ((timeout
= end_time
- time(NULL
)) > 2)
1890 polldata
.fd
= DNSServiceRefSockFD(ref
);
1891 polldata
.events
= POLLIN
;
1893 fds
= poll(&polldata
, 1, (int)(1000 * timeout
));
1895 # else /* select() */
1896 FD_ZERO(&input_set
);
1897 FD_SET(DNSServiceRefSockFD(ref
), &input_set
);
1900 stimeout
.tv_sec
= (long)timeout
;
1902 stimeout
.tv_sec
= timeout
;
1903 # endif /* _WIN32 */
1904 stimeout
.tv_usec
= 0;
1906 fds
= select(DNSServiceRefSockFD(ref
)+1, &input_set
, NULL
, NULL
,
1908 # endif /* HAVE_POLL */
1912 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1914 DEBUG_printf(("2_httpResolveURI: poll error: %s", strerror(errno
)));
1921 * Wait 2 seconds for a response to the local resolve; if nothing
1922 * comes in, do an additional domain resolution...
1925 if (extrasent
== 0 && domain
&& _cups_strcasecmp(domain
, "local."))
1927 if (options
& _HTTP_RESOLVE_STDERR
)
1929 "DEBUG: Resolving \"%s\", regtype=\"%s\", "
1930 "domain=\"%s\"...\n", hostname
, regtype
,
1931 domain
? domain
: "");
1934 if (DNSServiceResolve(&domainref
,
1935 kDNSServiceFlagsShareConnection
,
1936 myinterface
, hostname
, regtype
, domain
,
1938 &uribuf
) == kDNSServiceErr_NoError
)
1941 else if (extrasent
== 0 && !strcmp(scheme
, "ippusb"))
1943 if (options
& _HTTP_RESOLVE_STDERR
)
1944 fprintf(stderr
, "DEBUG: Resolving \"%s\", regtype=\"_ipps._tcp\", domain=\"local.\"...\n", hostname
);
1947 if (DNSServiceResolve(&ippsref
,
1948 kDNSServiceFlagsShareConnection
,
1949 kDNSServiceInterfaceIndexAny
, hostname
,
1950 "_ipps._tcp", domain
, http_resolve_cb
,
1951 &uribuf
) == kDNSServiceErr_NoError
)
1954 else if (extrasent
== 1 && !strcmp(scheme
, "ippusb"))
1956 if (options
& _HTTP_RESOLVE_STDERR
)
1957 fprintf(stderr
, "DEBUG: Resolving \"%s\", regtype=\"_ipp._tcp\", domain=\"local.\"...\n", hostname
);
1960 if (DNSServiceResolve(&ippref
,
1961 kDNSServiceFlagsShareConnection
,
1962 kDNSServiceInterfaceIndexAny
, hostname
,
1963 "_ipp._tcp", domain
, http_resolve_cb
,
1964 &uribuf
) == kDNSServiceErr_NoError
)
1969 * If it hasn't resolved within 5 seconds set the offline-report
1970 * printer-state-reason...
1973 if ((options
& _HTTP_RESOLVE_STDERR
) && offline
== 0 &&
1974 time(NULL
) > (start_time
+ 5))
1976 fputs("STATE: +offline-report\n", stderr
);
1982 if (DNSServiceProcessResult(ref
) == kDNSServiceErr_NoError
&&
1994 DNSServiceRefDeallocate(domainref
);
1996 DNSServiceRefDeallocate(ippref
);
1998 DNSServiceRefDeallocate(ippsref
);
2001 DNSServiceRefDeallocate(localref
);
2004 DNSServiceRefDeallocate(ref
);
2006 # else /* HAVE_AVAHI */
2007 if ((uribuf
.poll
= avahi_simple_poll_new()) != NULL
)
2009 avahi_simple_poll_set_func(uribuf
.poll
, http_poll_cb
, NULL
);
2011 if ((client
= avahi_client_new(avahi_simple_poll_get(uribuf
.poll
),
2013 &uribuf
, &error
)) != NULL
)
2015 if (avahi_service_resolver_new(client
, AVAHI_IF_UNSPEC
,
2016 AVAHI_PROTO_UNSPEC
, hostname
,
2017 regtype
, "local.", AVAHI_PROTO_UNSPEC
, 0,
2018 http_resolve_cb
, &uribuf
) != NULL
)
2020 time_t start_time
= time(NULL
),
2022 end_time
= start_time
+ 90;
2024 int pstatus
; /* Poll status */
2026 pstatus
= avahi_simple_poll_iterate(uribuf
.poll
, 2000);
2028 if (pstatus
== 0 && !resolved_uri
[0] && domain
&&
2029 _cups_strcasecmp(domain
, "local."))
2032 * Resolve for .local hasn't returned anything, try the listed
2036 avahi_service_resolver_new(client
, AVAHI_IF_UNSPEC
,
2037 AVAHI_PROTO_UNSPEC
, hostname
,
2038 regtype
, domain
, AVAHI_PROTO_UNSPEC
, 0,
2039 http_resolve_cb
, &uribuf
);
2042 while (!pstatus
&& !resolved_uri
[0] && time(NULL
) < end_time
)
2044 if ((pstatus
= avahi_simple_poll_iterate(uribuf
.poll
, 2000)) != 0)
2048 * If it hasn't resolved within 5 seconds set the offline-report
2049 * printer-state-reason...
2052 if ((options
& _HTTP_RESOLVE_STDERR
) && offline
== 0 &&
2053 time(NULL
) > (start_time
+ 5))
2055 fputs("STATE: +offline-report\n", stderr
);
2061 * Collect the result (if we got one).
2064 if (resolved_uri
[0])
2068 avahi_client_free(client
);
2071 avahi_simple_poll_free(uribuf
.poll
);
2073 # endif /* HAVE_DNSSD */
2075 if (options
& _HTTP_RESOLVE_STDERR
)
2079 fprintf(stderr
, "DEBUG: Resolved as \"%s\"...\n", uri
);
2080 fputs("STATE: -connecting-to-device,offline-report\n", stderr
);
2084 fputs("DEBUG: Unable to resolve URI\n", stderr
);
2085 fputs("STATE: -connecting-to-device\n", stderr
);
2089 #else /* HAVE_DNSSD || HAVE_AVAHI */
2091 * No DNS-SD support...
2095 #endif /* HAVE_DNSSD || HAVE_AVAHI */
2097 if ((options
& _HTTP_RESOLVE_STDERR
) && !uri
)
2098 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to find printer."));
2103 * Nothing more to do...
2106 strlcpy(resolved_uri
, uri
, resolved_size
);
2110 DEBUG_printf(("2_httpResolveURI: Returning \"%s\"", uri
));
2118 * 'http_client_cb()' - Client callback for resolving URI.
2123 AvahiClient
*client
, /* I - Client information */
2124 AvahiClientState state
, /* I - Current state */
2125 void *context
) /* I - Pointer to URI buffer */
2127 DEBUG_printf(("7http_client_cb(client=%p, state=%d, context=%p)", client
,
2131 * If the connection drops, quit.
2134 if (state
== AVAHI_CLIENT_FAILURE
)
2136 _http_uribuf_t
*uribuf
= (_http_uribuf_t
*)context
;
2139 avahi_simple_poll_quit(uribuf
->poll
);
2142 #endif /* HAVE_AVAHI */
2146 * 'http_copy_decode()' - Copy and decode a URI.
2149 static const char * /* O - New source pointer or NULL on error */
2150 http_copy_decode(char *dst
, /* O - Destination buffer */
2151 const char *src
, /* I - Source pointer */
2152 int dstsize
, /* I - Destination size */
2153 const char *term
, /* I - Terminating characters */
2154 int decode
) /* I - Decode %-encoded values */
2156 char *ptr
, /* Pointer into buffer */
2157 *end
; /* End of buffer */
2158 int quoted
; /* Quoted character */
2162 * Copy the src to the destination until we hit a terminating character
2163 * or the end of the string.
2166 for (ptr
= dst
, end
= dst
+ dstsize
- 1;
2167 *src
&& (!term
|| !strchr(term
, *src
));
2171 if (*src
== '%' && decode
)
2173 if (isxdigit(src
[1] & 255) && isxdigit(src
[2] & 255))
2176 * Grab a hex-encoded character...
2181 quoted
= (tolower(*src
) - 'a' + 10) << 4;
2183 quoted
= (*src
- '0') << 4;
2187 quoted
|= tolower(*src
) - 'a' + 10;
2189 quoted
|= *src
- '0';
2191 *ptr
++ = (char)quoted
;
2196 * Bad hex-encoded character...
2203 else if ((*src
& 255) <= 0x20 || (*src
& 255) >= 0x7f)
2219 * 'http_copy_encode()' - Copy and encode a URI.
2222 static char * /* O - End of current URI */
2223 http_copy_encode(char *dst
, /* O - Destination buffer */
2224 const char *src
, /* I - Source pointer */
2225 char *dstend
, /* I - End of destination buffer */
2226 const char *reserved
, /* I - Extra reserved characters */
2227 const char *term
, /* I - Terminating characters */
2228 int encode
) /* I - %-encode reserved chars? */
2230 static const char hex
[] = "0123456789ABCDEF";
2233 while (*src
&& dst
< dstend
)
2235 if (term
&& *src
== *term
)
2238 if (encode
&& (*src
== '%' || *src
<= ' ' || *src
& 128 ||
2239 (reserved
&& strchr(reserved
, *src
))))
2242 * Hex encode reserved characters...
2245 if ((dst
+ 2) >= dstend
)
2249 *dst
++ = hex
[(*src
>> 4) & 15];
2250 *dst
++ = hex
[*src
& 15];
2269 * 'http_resolve_cb()' - Build a device URI for the given service name.
2272 static void DNSSD_API
2274 DNSServiceRef sdRef
, /* I - Service reference */
2275 DNSServiceFlags flags
, /* I - Results flags */
2276 uint32_t interfaceIndex
, /* I - Interface number */
2277 DNSServiceErrorType errorCode
, /* I - Error, if any */
2278 const char *fullName
, /* I - Full service name */
2279 const char *hostTarget
, /* I - Hostname */
2280 uint16_t port
, /* I - Port number */
2281 uint16_t txtLen
, /* I - Length of TXT record */
2282 const unsigned char *txtRecord
, /* I - TXT record data */
2283 void *context
) /* I - Pointer to URI buffer */
2285 _http_uribuf_t
*uribuf
= (_http_uribuf_t
*)context
;
2287 const char *scheme
, /* URI scheme */
2288 *hostptr
, /* Pointer into hostTarget */
2289 *reskey
, /* "rp" or "rfo" */
2290 *resdefault
; /* Default path */
2291 char resource
[257], /* Remote path */
2292 fqdn
[256]; /* FQDN of the .local name */
2293 const void *value
; /* Value from TXT record */
2294 uint8_t valueLen
; /* Length of value */
2297 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
));
2300 * If we have a UUID, compare it...
2304 (value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, "UUID",
2305 &valueLen
)) != NULL
)
2307 char uuid
[256]; /* UUID value */
2309 memcpy(uuid
, value
, valueLen
);
2310 uuid
[valueLen
] = '\0';
2312 if (_cups_strcasecmp(uuid
, uribuf
->uuid
))
2314 if (uribuf
->options
& _HTTP_RESOLVE_STDERR
)
2315 fprintf(stderr
, "DEBUG: Found UUID %s, looking for %s.", uuid
,
2318 DEBUG_printf(("5http_resolve_cb: Found UUID %s, looking for %s.", uuid
,
2325 * Figure out the scheme from the full name...
2328 if (strstr(fullName
, "._ipps") || strstr(fullName
, "._ipp-tls"))
2330 else if (strstr(fullName
, "._ipp") || strstr(fullName
, "._fax-ipp"))
2332 else if (strstr(fullName
, "._http."))
2334 else if (strstr(fullName
, "._https."))
2336 else if (strstr(fullName
, "._printer."))
2338 else if (strstr(fullName
, "._pdl-datastream."))
2341 scheme
= "riousbprint";
2344 * Extract the "remote printer" key from the TXT record...
2347 if ((uribuf
->options
& _HTTP_RESOLVE_FAXOUT
) &&
2348 (!strcmp(scheme
, "ipp") || !strcmp(scheme
, "ipps")) &&
2349 !TXTRecordGetValuePtr(txtLen
, txtRecord
, "printer-type", &valueLen
))
2352 resdefault
= "/ipp/faxout";
2360 if ((value
= TXTRecordGetValuePtr(txtLen
, txtRecord
, reskey
,
2361 &valueLen
)) != NULL
)
2363 if (((char *)value
)[0] == '/')
2366 * Value (incorrectly) has a leading slash already...
2369 memcpy(resource
, value
, valueLen
);
2370 resource
[valueLen
] = '\0';
2375 * Convert to resource by concatenating with a leading "/"...
2379 memcpy(resource
+ 1, value
, valueLen
);
2380 resource
[valueLen
+ 1] = '\0';
2386 * Use the default value...
2389 strlcpy(resource
, resdefault
, sizeof(resource
));
2393 * Lookup the FQDN if needed...
2396 if ((uribuf
->options
& _HTTP_RESOLVE_FQDN
) &&
2397 (hostptr
= hostTarget
+ strlen(hostTarget
) - 7) > hostTarget
&&
2398 !_cups_strcasecmp(hostptr
, ".local."))
2401 * OK, we got a .local name but the caller needs a real domain. Start by
2402 * getting the IP address of the .local name and then do reverse-lookups...
2405 http_addrlist_t
*addrlist
, /* List of addresses */
2406 *addr
; /* Current address */
2408 DEBUG_printf(("5http_resolve_cb: Looking up \"%s\".", hostTarget
));
2410 snprintf(fqdn
, sizeof(fqdn
), "%d", ntohs(port
));
2411 if ((addrlist
= httpAddrGetList(hostTarget
, AF_UNSPEC
, fqdn
)) != NULL
)
2413 for (addr
= addrlist
; addr
; addr
= addr
->next
)
2415 int error
= getnameinfo(&(addr
->addr
.addr
), (socklen_t
)httpAddrLength(&(addr
->addr
)), fqdn
, sizeof(fqdn
), NULL
, 0, NI_NAMEREQD
);
2419 DEBUG_printf(("5http_resolve_cb: Found \"%s\".", fqdn
));
2421 if ((hostptr
= fqdn
+ strlen(fqdn
) - 6) <= fqdn
||
2422 _cups_strcasecmp(hostptr
, ".local"))
2430 DEBUG_printf(("5http_resolve_cb: \"%s\" did not resolve: %d",
2431 httpAddrString(&(addr
->addr
), fqdn
, sizeof(fqdn
)),
2436 httpAddrFreeList(addrlist
);
2441 * Assemble the final device URI...
2444 if ((!strcmp(scheme
, "ipp") || !strcmp(scheme
, "ipps")) &&
2445 !strcmp(uribuf
->resource
, "/cups"))
2446 httpAssembleURIf(HTTP_URI_CODING_ALL
, uribuf
->buffer
, (int)uribuf
->bufsize
, scheme
, NULL
, hostTarget
, ntohs(port
), "%s?snmp=false", resource
);
2448 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, (int)uribuf
->bufsize
, scheme
, NULL
, hostTarget
, ntohs(port
), resource
);
2450 DEBUG_printf(("5http_resolve_cb: Resolved URI is \"%s\"...", uribuf
->buffer
));
2453 #elif defined(HAVE_AVAHI)
2455 * 'http_poll_cb()' - Wait for input on the specified file descriptors.
2457 * Note: This function is needed because avahi_simple_poll_iterate is broken
2458 * and always uses a timeout of 0 (!) milliseconds.
2459 * (Avahi Ticket #364)
2464 static int /* O - Number of file descriptors matching */
2466 struct pollfd
*pollfds
, /* I - File descriptors */
2467 unsigned int num_pollfds
, /* I - Number of file descriptors */
2468 int timeout
, /* I - Timeout in milliseconds (used) */
2469 void *context
) /* I - User data (unused) */
2474 return (poll(pollfds
, num_pollfds
, 2000));
2479 * 'http_resolve_cb()' - Build a device URI for the given service name.
2484 AvahiServiceResolver
*resolver
, /* I - Resolver (unused) */
2485 AvahiIfIndex interface
, /* I - Interface index (unused) */
2486 AvahiProtocol protocol
, /* I - Network protocol (unused) */
2487 AvahiResolverEvent event
, /* I - Event (found, etc.) */
2488 const char *name
, /* I - Service name */
2489 const char *type
, /* I - Registration type */
2490 const char *domain
, /* I - Domain (unused) */
2491 const char *hostTarget
, /* I - Hostname */
2492 const AvahiAddress
*address
, /* I - Address (unused) */
2493 uint16_t port
, /* I - Port number */
2494 AvahiStringList
*txt
, /* I - TXT record */
2495 AvahiLookupResultFlags flags
, /* I - Lookup flags (unused) */
2496 void *context
) /* I - Pointer to URI buffer */
2498 _http_uribuf_t
*uribuf
= (_http_uribuf_t
*)context
;
2500 const char *scheme
, /* URI scheme */
2501 *hostptr
, /* Pointer into hostTarget */
2502 *reskey
, /* "rp" or "rfo" */
2503 *resdefault
; /* Default path */
2504 char resource
[257], /* Remote path */
2505 fqdn
[256]; /* FQDN of the .local name */
2506 AvahiStringList
*pair
; /* Current TXT record key/value pair */
2507 char *value
; /* Value for "rp" key */
2508 size_t valueLen
= 0; /* Length of "rp" key */
2511 DEBUG_printf(("4http_resolve_cb(resolver=%p, "
2512 "interface=%d, protocol=%d, event=%d, name=\"%s\", "
2513 "type=\"%s\", domain=\"%s\", hostTarget=\"%s\", address=%p, "
2514 "port=%d, txt=%p, flags=%d, context=%p)",
2515 resolver
, interface
, protocol
, event
, name
, type
, domain
,
2516 hostTarget
, address
, port
, txt
, flags
, context
));
2518 if (event
!= AVAHI_RESOLVER_FOUND
)
2520 avahi_service_resolver_free(resolver
);
2521 avahi_simple_poll_quit(uribuf
->poll
);
2526 * If we have a UUID, compare it...
2529 if (uribuf
->uuid
&& (pair
= avahi_string_list_find(txt
, "UUID")) != NULL
)
2531 char uuid
[256]; /* UUID value */
2533 avahi_string_list_get_pair(pair
, NULL
, &value
, &valueLen
);
2535 memcpy(uuid
, value
, valueLen
);
2536 uuid
[valueLen
] = '\0';
2538 if (_cups_strcasecmp(uuid
, uribuf
->uuid
))
2540 if (uribuf
->options
& _HTTP_RESOLVE_STDERR
)
2541 fprintf(stderr
, "DEBUG: Found UUID %s, looking for %s.", uuid
,
2544 DEBUG_printf(("5http_resolve_cb: Found UUID %s, looking for %s.", uuid
,
2551 * Figure out the scheme from the full name...
2554 if (strstr(type
, "_ipp."))
2556 else if (strstr(type
, "_printer."))
2558 else if (strstr(type
, "_pdl-datastream."))
2561 scheme
= "riousbprint";
2563 if (!strncmp(type
, "_ipps.", 6) || !strncmp(type
, "_ipp-tls.", 9))
2565 else if (!strncmp(type
, "_ipp.", 5) || !strncmp(type
, "_fax-ipp.", 9))
2567 else if (!strncmp(type
, "_http.", 6))
2569 else if (!strncmp(type
, "_https.", 7))
2571 else if (!strncmp(type
, "_printer.", 9))
2573 else if (!strncmp(type
, "_pdl-datastream.", 16))
2577 avahi_service_resolver_free(resolver
);
2578 avahi_simple_poll_quit(uribuf
->poll
);
2583 * Extract the remote resource key from the TXT record...
2586 if ((uribuf
->options
& _HTTP_RESOLVE_FAXOUT
) &&
2587 (!strcmp(scheme
, "ipp") || !strcmp(scheme
, "ipps")) &&
2588 !avahi_string_list_find(txt
, "printer-type"))
2591 resdefault
= "/ipp/faxout";
2599 if ((pair
= avahi_string_list_find(txt
, reskey
)) != NULL
)
2601 avahi_string_list_get_pair(pair
, NULL
, &value
, &valueLen
);
2603 if (value
[0] == '/')
2606 * Value (incorrectly) has a leading slash already...
2609 memcpy(resource
, value
, valueLen
);
2610 resource
[valueLen
] = '\0';
2615 * Convert to resource by concatenating with a leading "/"...
2619 memcpy(resource
+ 1, value
, valueLen
);
2620 resource
[valueLen
+ 1] = '\0';
2626 * Use the default value...
2629 strlcpy(resource
, resdefault
, sizeof(resource
));
2633 * Lookup the FQDN if needed...
2636 if ((uribuf
->options
& _HTTP_RESOLVE_FQDN
) &&
2637 (hostptr
= hostTarget
+ strlen(hostTarget
) - 6) > hostTarget
&&
2638 !_cups_strcasecmp(hostptr
, ".local"))
2641 * OK, we got a .local name but the caller needs a real domain. Start by
2642 * getting the IP address of the .local name and then do reverse-lookups...
2645 http_addrlist_t
*addrlist
, /* List of addresses */
2646 *addr
; /* Current address */
2648 DEBUG_printf(("5http_resolve_cb: Looking up \"%s\".", hostTarget
));
2650 snprintf(fqdn
, sizeof(fqdn
), "%d", ntohs(port
));
2651 if ((addrlist
= httpAddrGetList(hostTarget
, AF_UNSPEC
, fqdn
)) != NULL
)
2653 for (addr
= addrlist
; addr
; addr
= addr
->next
)
2655 int error
= getnameinfo(&(addr
->addr
.addr
), (socklen_t
)httpAddrLength(&(addr
->addr
)), fqdn
, sizeof(fqdn
), NULL
, 0, NI_NAMEREQD
);
2659 DEBUG_printf(("5http_resolve_cb: Found \"%s\".", fqdn
));
2661 if ((hostptr
= fqdn
+ strlen(fqdn
) - 6) <= fqdn
||
2662 _cups_strcasecmp(hostptr
, ".local"))
2670 DEBUG_printf(("5http_resolve_cb: \"%s\" did not resolve: %d",
2671 httpAddrString(&(addr
->addr
), fqdn
, sizeof(fqdn
)),
2676 httpAddrFreeList(addrlist
);
2681 * Assemble the final device URI using the resolved hostname...
2684 httpAssembleURI(HTTP_URI_CODING_ALL
, uribuf
->buffer
, (int)uribuf
->bufsize
, scheme
,
2685 NULL
, hostTarget
, port
, resource
);
2686 DEBUG_printf(("5http_resolve_cb: Resolved URI is \"%s\".", uribuf
->buffer
));
2688 avahi_simple_poll_quit(uribuf
->poll
);
2690 #endif /* HAVE_DNSSD */