+/*
+ * '_httpResolveURI()' - Resolve a DNS-SD URI.
+ */
+
+const char * /* O - Resolved URI */
+_httpResolveURI(
+ const char *uri, /* I - DNS-SD URI */
+ char *resolved_uri, /* I - Buffer for resolved URI */
+ size_t resolved_size, /* I - Size of URI buffer */
+ int options, /* I - Resolve options */
+ int (*cb)(void *context), /* I - Continue callback function */
+ void *context) /* I - Context pointer for callback */
+{
+ char scheme[32], /* URI components... */
+ userpass[256],
+ hostname[1024],
+ resource[1024];
+ int port;
+#ifdef DEBUG
+ http_uri_status_t status; /* URI decode status */
+#endif /* DEBUG */
+
+
+ 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));
+
+ /*
+ * Get the device URI...
+ */
+
+#ifdef DEBUG
+ if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
+ sizeof(scheme), userpass, sizeof(userpass),
+ hostname, sizeof(hostname), &port, resource,
+ sizeof(resource))) < HTTP_URI_STATUS_OK)
+#else
+ if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme,
+ sizeof(scheme), userpass, sizeof(userpass),
+ hostname, sizeof(hostname), &port, resource,
+ sizeof(resource)) < HTTP_URI_STATUS_OK)
+#endif /* DEBUG */
+ {
+ if (options & _HTTP_RESOLVE_STDERR)
+ _cupsLangPrintFilter(stderr, "ERROR", _("Bad device-uri \"%s\"."), uri);
+
+ DEBUG_printf(("2_httpResolveURI: httpSeparateURI returned %d!", status));
+ DEBUG_puts("2_httpResolveURI: Returning NULL");
+ return (NULL);
+ }
+
+ /*
+ * Resolve it as needed...
+ */
+
+ if (strstr(hostname, "._tcp"))
+ {
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+ char *regtype, /* Pointer to type in hostname */
+ *domain, /* Pointer to domain in hostname */
+ *uuid, /* Pointer to UUID in URI */
+ *uuidend; /* Pointer to end of UUID in URI */
+ _http_uribuf_t uribuf; /* URI buffer */
+ int offline = 0; /* offline-report state set? */
+# ifdef HAVE_DNSSD
+# ifdef WIN32
+# pragma comment(lib, "dnssd.lib")
+# endif /* WIN32 */
+ DNSServiceRef ref, /* DNS-SD master service reference */
+ domainref = NULL,/* DNS-SD service reference for domain */
+ ippref = NULL, /* DNS-SD service reference for network IPP */
+ ippsref = NULL, /* DNS-SD service reference for network IPPS */
+ localref; /* DNS-SD service reference for .local */
+ int extrasent = 0; /* Send the domain/IPP/IPPS resolves? */
+# ifdef HAVE_POLL
+ struct pollfd polldata; /* Polling data */
+# else /* select() */
+ fd_set input_set; /* Input set for select() */
+ struct timeval stimeout; /* Timeout value for select() */
+# endif /* HAVE_POLL */
+# elif defined(HAVE_AVAHI)
+ AvahiClient *client; /* Client information */
+ int error; /* Status */
+# endif /* HAVE_DNSSD */
+
+ if (options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr, "DEBUG: Resolving \"%s\"...\n", hostname);
+
+ /*
+ * Separate the hostname into service name, registration type, and domain...
+ */
+
+ for (regtype = strstr(hostname, "._tcp") - 2;
+ regtype > hostname;
+ regtype --)
+ if (regtype[0] == '.' && regtype[1] == '_')
+ {
+ /*
+ * Found ._servicetype in front of ._tcp...
+ */
+
+ *regtype++ = '\0';
+ break;
+ }
+
+ if (regtype <= hostname)
+ {
+ DEBUG_puts("2_httpResolveURI: Bad hostname, returning NULL");
+ return (NULL);
+ }
+
+ for (domain = strchr(regtype, '.');
+ domain;
+ domain = strchr(domain + 1, '.'))
+ if (domain[1] != '_')
+ break;
+
+ if (domain)
+ *domain++ = '\0';
+
+ if ((uuid = strstr(resource, "?uuid=")) != NULL)
+ {
+ *uuid = '\0';
+ uuid += 6;
+ if ((uuidend = strchr(uuid, '&')) != NULL)
+ *uuidend = '\0';
+ }
+
+ resolved_uri[0] = '\0';
+
+ uribuf.buffer = resolved_uri;
+ uribuf.bufsize = resolved_size;
+ uribuf.options = options;
+ uribuf.resource = resource;
+ uribuf.uuid = uuid;
+
+ DEBUG_printf(("2_httpResolveURI: Resolving hostname=\"%s\", regtype=\"%s\", "
+ "domain=\"%s\"\n", hostname, regtype, domain));
+ if (options & _HTTP_RESOLVE_STDERR)
+ {
+ fputs("STATE: +connecting-to-device\n", stderr);
+ fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"%s\", "
+ "domain=\"local.\"...\n", hostname, regtype);
+ }
+
+ uri = NULL;
+
+# ifdef HAVE_DNSSD
+ if (DNSServiceCreateConnection(&ref) == kDNSServiceErr_NoError)
+ {
+ uint32_t myinterface = kDNSServiceInterfaceIndexAny;
+ /* Lookup on any interface */
+
+ if (!strcmp(scheme, "ippusb"))
+ myinterface = kDNSServiceInterfaceIndexLocalOnly;
+
+ localref = ref;
+ if (DNSServiceResolve(&localref,
+ kDNSServiceFlagsShareConnection, myinterface,
+ hostname, regtype, "local.", http_resolve_cb,
+ &uribuf) == kDNSServiceErr_NoError)
+ {
+ int fds; /* Number of ready descriptors */
+ time_t timeout, /* Poll timeout */
+ start_time = time(NULL),/* Start time */
+ end_time = start_time + 90;
+ /* End time */
+
+ while (time(NULL) < end_time)
+ {
+ if (options & _HTTP_RESOLVE_STDERR)
+ _cupsLangPrintFilter(stderr, "INFO", _("Looking for printer."));
+
+ if (cb && !(*cb)(context))
+ {
+ DEBUG_puts("2_httpResolveURI: callback returned 0 (stop)");
+ break;
+ }
+
+ /*
+ * Wakeup every 2 seconds to emit a "looking for printer" message...
+ */
+
+ if ((timeout = end_time - time(NULL)) > 2)
+ timeout = 2;
+
+# ifdef HAVE_POLL
+ polldata.fd = DNSServiceRefSockFD(ref);
+ polldata.events = POLLIN;
+
+ fds = poll(&polldata, 1, (int)(1000 * timeout));
+
+# else /* select() */
+ FD_ZERO(&input_set);
+ FD_SET(DNSServiceRefSockFD(ref), &input_set);
+
+# ifdef WIN32
+ stimeout.tv_sec = (long)timeout;
+# else
+ stimeout.tv_sec = timeout;
+# endif /* WIN32 */
+ stimeout.tv_usec = 0;
+
+ fds = select(DNSServiceRefSockFD(ref)+1, &input_set, NULL, NULL,
+ &stimeout);
+# endif /* HAVE_POLL */
+
+ if (fds < 0)
+ {
+ if (errno != EINTR && errno != EAGAIN)
+ {
+ DEBUG_printf(("2_httpResolveURI: poll error: %s", strerror(errno)));
+ break;
+ }
+ }
+ else if (fds == 0)
+ {
+ /*
+ * Wait 2 seconds for a response to the local resolve; if nothing
+ * comes in, do an additional domain resolution...
+ */
+
+ if (extrasent == 0 && domain && _cups_strcasecmp(domain, "local."))
+ {
+ if (options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr,
+ "DEBUG: Resolving \"%s\", regtype=\"%s\", "
+ "domain=\"%s\"...\n", hostname, regtype,
+ domain ? domain : "");
+
+ domainref = ref;
+ if (DNSServiceResolve(&domainref,
+ kDNSServiceFlagsShareConnection,
+ myinterface, hostname, regtype, domain,
+ http_resolve_cb,
+ &uribuf) == kDNSServiceErr_NoError)
+ extrasent = 1;
+ }
+ else if (extrasent == 0 && !strcmp(scheme, "ippusb"))
+ {
+ if (options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"_ipps._tcp\", domain=\"local.\"...\n", hostname);
+
+ ippsref = ref;
+ if (DNSServiceResolve(&ippsref,
+ kDNSServiceFlagsShareConnection,
+ kDNSServiceInterfaceIndexAny, hostname,
+ "_ipps._tcp", domain, http_resolve_cb,
+ &uribuf) == kDNSServiceErr_NoError)
+ extrasent = 1;
+ }
+ else if (extrasent == 1 && !strcmp(scheme, "ippusb"))
+ {
+ if (options & _HTTP_RESOLVE_STDERR)
+ fprintf(stderr, "DEBUG: Resolving \"%s\", regtype=\"_ipp._tcp\", domain=\"local.\"...\n", hostname);
+
+ ippref = ref;
+ if (DNSServiceResolve(&ippref,
+ kDNSServiceFlagsShareConnection,
+ kDNSServiceInterfaceIndexAny, hostname,
+ "_ipp._tcp", domain, http_resolve_cb,
+ &uribuf) == kDNSServiceErr_NoError)
+ extrasent = 2;
+ }
+
+ /*
+ * If it hasn't resolved within 5 seconds set the offline-report
+ * printer-state-reason...
+ */
+
+ if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 &&
+ time(NULL) > (start_time + 5))
+ {
+ fputs("STATE: +offline-report\n", stderr);
+ offline = 1;
+ }
+ }
+ else
+ {
+ if (DNSServiceProcessResult(ref) == kDNSServiceErr_NoError &&
+ resolved_uri[0])
+ {
+ uri = resolved_uri;
+ break;
+ }
+ }
+ }
+
+ if (extrasent)
+ {
+ if (domainref)
+ DNSServiceRefDeallocate(domainref);
+ if (ippref)
+ DNSServiceRefDeallocate(ippref);
+ if (ippsref)
+ DNSServiceRefDeallocate(ippsref);
+ }
+
+ DNSServiceRefDeallocate(localref);
+ }
+
+ DNSServiceRefDeallocate(ref);
+ }
+# else /* HAVE_AVAHI */
+ if ((uribuf.poll = avahi_simple_poll_new()) != NULL)
+ {
+ avahi_simple_poll_set_func(uribuf.poll, http_poll_cb, NULL);
+
+ if ((client = avahi_client_new(avahi_simple_poll_get(uribuf.poll),
+ 0, http_client_cb,
+ &uribuf, &error)) != NULL)
+ {
+ if (avahi_service_resolver_new(client, AVAHI_IF_UNSPEC,
+ AVAHI_PROTO_UNSPEC, hostname,
+ regtype, "local.", AVAHI_PROTO_UNSPEC, 0,
+ http_resolve_cb, &uribuf) != NULL)
+ {
+ time_t start_time = time(NULL),
+ /* Start time */
+ end_time = start_time + 90;
+ /* End time */
+ int pstatus; /* Poll status */
+
+ pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000);
+
+ if (pstatus == 0 && !resolved_uri[0] && domain &&
+ _cups_strcasecmp(domain, "local."))
+ {
+ /*
+ * Resolve for .local hasn't returned anything, try the listed
+ * domain...
+ */
+
+ avahi_service_resolver_new(client, AVAHI_IF_UNSPEC,
+ AVAHI_PROTO_UNSPEC, hostname,
+ regtype, domain, AVAHI_PROTO_UNSPEC, 0,
+ http_resolve_cb, &uribuf);
+ }
+
+ while (!pstatus && !resolved_uri[0] && time(NULL) < end_time)
+ {
+ if ((pstatus = avahi_simple_poll_iterate(uribuf.poll, 2000)) != 0)
+ break;
+
+ /*
+ * If it hasn't resolved within 5 seconds set the offline-report
+ * printer-state-reason...
+ */
+
+ if ((options & _HTTP_RESOLVE_STDERR) && offline == 0 &&
+ time(NULL) > (start_time + 5))
+ {
+ fputs("STATE: +offline-report\n", stderr);
+ offline = 1;
+ }
+ }
+
+ /*
+ * Collect the result (if we got one).
+ */
+
+ if (resolved_uri[0])
+ uri = resolved_uri;
+ }
+
+ avahi_client_free(client);
+ }
+
+ avahi_simple_poll_free(uribuf.poll);
+ }
+# endif /* HAVE_DNSSD */
+
+ if (options & _HTTP_RESOLVE_STDERR)
+ {
+ if (uri)
+ {
+ fprintf(stderr, "DEBUG: Resolved as \"%s\"...\n", uri);
+ fputs("STATE: -connecting-to-device,offline-report\n", stderr);
+ }
+ else
+ {
+ fputs("DEBUG: Unable to resolve URI\n", stderr);
+ fputs("STATE: -connecting-to-device\n", stderr);
+ }
+ }
+
+#else /* HAVE_DNSSD || HAVE_AVAHI */
+ /*
+ * No DNS-SD support...
+ */
+
+ uri = NULL;
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
+
+ if ((options & _HTTP_RESOLVE_STDERR) && !uri)
+ _cupsLangPrintFilter(stderr, "INFO", _("Unable to find printer."));
+ }
+ else
+ {
+ /*
+ * Nothing more to do...
+ */
+
+ strlcpy(resolved_uri, uri, resolved_size);
+ uri = resolved_uri;
+ }
+
+ DEBUG_printf(("2_httpResolveURI: Returning \"%s\"", uri));
+
+ return (uri);
+}
+
+
+#ifdef HAVE_AVAHI
+/*
+ * 'http_client_cb()' - Client callback for resolving URI.
+ */
+
+static void
+http_client_cb(
+ AvahiClient *client, /* I - Client information */
+ AvahiClientState state, /* I - Current state */
+ void *context) /* I - Pointer to URI buffer */
+{
+ DEBUG_printf(("7http_client_cb(client=%p, state=%d, context=%p)", client,
+ state, context));
+
+ /*
+ * If the connection drops, quit.
+ */
+
+ if (state == AVAHI_CLIENT_FAILURE)
+ {
+ _http_uribuf_t *uribuf = (_http_uribuf_t *)context;
+ /* URI buffer */
+
+ avahi_simple_poll_quit(uribuf->poll);
+ }
+}
+#endif /* HAVE_AVAHI */
+
+