#include "globals.h"
#include "debug.h"
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#if defined(WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
#endif
static char *cups_connect(const char *name, char *printer, char *hostname);
static int cups_get_printer_uri(http_t *http, const char *name,
char *host, int hostsize, int *port,
char *resource, int resourcesize,
int depth);
int
cupsCancelJob(const char *name,
int job)
{
char printer[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
uri[HTTP_MAX_URI];
ipp_t *request,
*response;
_cups_globals_t *cg = _cupsGlobals();
if (!cups_connect(name, printer, hostname))
{
DEBUG_puts("Unable to connect to server!");
return (0);
}
if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", printer) != HTTP_URI_OK)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (0);
}
request = ippNewRequest(IPP_CANCEL_JOB);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if ((response = cupsDoRequest(cg->http, request, "/jobs/")) != NULL)
ippDelete(response);
return (cg->last_error < IPP_REDIRECTION_OTHER_SITE);
}
void
cupsFreeJobs(int num_jobs,
cups_job_t *jobs)
{
int i;
if (num_jobs <= 0 || jobs == NULL)
return;
for (i = 0; i < num_jobs; i ++)
{
free(jobs[i].dest);
free(jobs[i].user);
free(jobs[i].format);
free(jobs[i].title);
}
free(jobs);
}
int
cupsGetClasses(char ***classes)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
char **temp;
_cups_globals_t *cg = _cupsGlobals();
if (classes == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (0);
}
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
return (0);
}
request = ippNewRequest(CUPS_GET_CLASSES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", NULL, "printer-name");
n = 0;
*classes = NULL;
if ((response = cupsDoRequest(cg->http, request, "/")) != NULL)
{
for (attr = response->attrs; attr != NULL; attr = attr->next)
if (attr->name != NULL &&
strcasecmp(attr->name, "printer-name") == 0 &&
attr->value_tag == IPP_TAG_NAME)
{
if (n == 0)
temp = malloc(sizeof(char *));
else
temp = realloc(*classes, sizeof(char *) * (n + 1));
if (temp == NULL)
{
while (n > 0)
{
n --;
free((*classes)[n]);
}
free(*classes);
ippDelete(response);
return (0);
}
*classes = temp;
temp[n] = strdup(attr->values[0].string.text);
n ++;
}
ippDelete(response);
}
return (n);
}
const char *
cupsGetDefault(void)
{
const char *var;
_cups_globals_t *cg = _cupsGlobals();
if ((var = getenv("LPDEST")) != NULL)
return (var);
else if ((var = getenv("PRINTER")) != NULL && strcmp(var, "lp") != 0)
return (var);
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
return (NULL);
}
return (cupsGetDefault2(cg->http));
}
const char *
cupsGetDefault2(http_t *http)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
const char *var;
_cups_globals_t *cg = _cupsGlobals();
if ((var = getenv("LPDEST")) != NULL)
return (var);
else if ((var = getenv("PRINTER")) != NULL && strcmp(var, "lp") != 0)
return (var);
if (!http)
return (NULL);
request = ippNewRequest(CUPS_GET_DEFAULT);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
{
strlcpy(cg->def_printer, attr->values[0].string.text, sizeof(cg->def_printer));
ippDelete(response);
return (cg->def_printer);
}
ippDelete(response);
}
return (NULL);
}
int
cupsGetJobs(cups_job_t **jobs,
const char *mydest,
int myjobs,
int completed)
{
_cups_globals_t *cg = _cupsGlobals();
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
return (-1);
}
return (cupsGetJobs2(cg->http, jobs, mydest, myjobs, completed));
}
int
cupsGetJobs2(http_t *http,
cups_job_t **jobs,
const char *mydest,
int myjobs,
int completed)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_job_t *temp;
int id,
priority,
size;
ipp_jstate_t state;
time_t completed_time,
creation_time,
processing_time;
const char *dest,
*format,
*title,
*user;
char uri[HTTP_MAX_URI];
_cups_globals_t *cg = _cupsGlobals();
static const char * const attrs[] =
{
"job-id",
"job-priority",
"job-k-octets",
"job-state",
"time-at-completed",
"time-at-creation",
"time-at-processing",
"job-printer-uri",
"document-format",
"job-name",
"job-originating-user-name"
};
if (!http || !jobs)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (-1);
}
if (mydest)
{
if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", mydest) != HTTP_URI_OK)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (-1);
}
}
else
strcpy(uri, "ipp://localhost/jobs");
request = ippNewRequest(IPP_GET_JOBS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if (myjobs)
ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
if (completed > 0)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"which-jobs", NULL, "completed");
else if (completed < 0)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"which-jobs", NULL, "all");
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
NULL, attrs);
n = 0;
*jobs = NULL;
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
for (attr = response->attrs; attr != NULL; attr = attr->next)
{
while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
attr = attr->next;
if (attr == NULL)
break;
id = 0;
size = 0;
priority = 50;
state = IPP_JOB_PENDING;
user = "unknown";
dest = NULL;
format = "application/octet-stream";
title = "untitled";
creation_time = 0;
completed_time = 0;
processing_time = 0;
while (attr != NULL && attr->group_tag == IPP_TAG_JOB)
{
if (strcmp(attr->name, "job-id") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
id = attr->values[0].integer;
else if (strcmp(attr->name, "job-state") == 0 &&
attr->value_tag == IPP_TAG_ENUM)
state = (ipp_jstate_t)attr->values[0].integer;
else if (strcmp(attr->name, "job-priority") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
priority = attr->values[0].integer;
else if (strcmp(attr->name, "job-k-octets") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
size = attr->values[0].integer;
else if (strcmp(attr->name, "time-at-completed") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
completed_time = attr->values[0].integer;
else if (strcmp(attr->name, "time-at-creation") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
creation_time = attr->values[0].integer;
else if (strcmp(attr->name, "time-at-processing") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
processing_time = attr->values[0].integer;
else if (strcmp(attr->name, "job-printer-uri") == 0 &&
attr->value_tag == IPP_TAG_URI)
{
if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
dest ++;
}
else if (strcmp(attr->name, "job-originating-user-name") == 0 &&
attr->value_tag == IPP_TAG_NAME)
user = attr->values[0].string.text;
else if (strcmp(attr->name, "document-format") == 0 &&
attr->value_tag == IPP_TAG_MIMETYPE)
format = attr->values[0].string.text;
else if (strcmp(attr->name, "job-name") == 0 &&
(attr->value_tag == IPP_TAG_TEXT ||
attr->value_tag == IPP_TAG_NAME))
title = attr->values[0].string.text;
attr = attr->next;
}
if (dest == NULL || id == 0)
{
if (attr == NULL)
break;
else
continue;
}
if (n == 0)
temp = malloc(sizeof(cups_job_t));
else
temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1));
if (temp == NULL)
{
cupsFreeJobs(n, *jobs);
*jobs = NULL;
ippDelete(response);
return (0);
}
*jobs = temp;
temp += n;
n ++;
temp->dest = strdup(dest);
temp->user = strdup(user);
temp->format = strdup(format);
temp->title = strdup(title);
temp->id = id;
temp->priority = priority;
temp->state = state;
temp->size = size;
temp->completed_time = completed_time;
temp->creation_time = creation_time;
temp->processing_time = processing_time;
if (attr == NULL)
break;
}
ippDelete(response);
}
if (n == 0 && cg->last_error >= IPP_BAD_REQUEST)
return (-1);
else
return (n);
}
const char *
cupsGetPPD(const char *name)
{
_cups_globals_t *cg = _cupsGlobals();
if (!cups_connect(name, NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
return (NULL);
}
return (cupsGetPPD2(cg->http, name));
}
const char *
cupsGetPPD2(http_t *http,
const char *name)
{
int http_port;
char http_hostname[HTTP_MAX_HOST];
http_t *http2;
int fd;
char localhost[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
http_status_t status;
_cups_globals_t *cg = _cupsGlobals();
DEBUG_printf(("cupsGetPPD2(http=%p, name=\"%s\")\n", http,
name ? name : "(null)"));
if (!http || !name)
{
if (!http)
_cupsSetError(IPP_INTERNAL_ERROR, "No HTTP connection!");
else
_cupsSetError(IPP_INTERNAL_ERROR, "No printer name!");
return (NULL);
}
if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
resource, sizeof(resource), 0))
return (NULL);
DEBUG_printf(("Printer hostname=\"%s\", port=%d\n", hostname, port));
httpGetHostname(NULL, localhost, sizeof(localhost));
DEBUG_printf(("Local hostname=\"%s\"\n", localhost));
if (!strcasecmp(localhost, hostname))
strcpy(hostname, "localhost");
httpGetHostname(http, http_hostname, sizeof(http_hostname));
#ifdef AF_INET6
if (http->hostaddr->addr.sa_family == AF_INET6)
http_port = ntohs(http->hostaddr->ipv6.sin6_port);
else
#endif
if (http->hostaddr->addr.sa_family == AF_INET)
http_port = ntohs(http->hostaddr->ipv4.sin_port);
else
http_port = ippPort();
DEBUG_printf(("Connection hostname=\"%s\", port=%d\n", http_hostname,
http_port));
if (!strcasecmp(http_hostname, hostname) && port == http_port)
http2 = http;
else if ((http2 = httpConnectEncrypt(hostname, port,
cupsEncryption())) == NULL)
{
DEBUG_puts("Unable to connect to server!");
return (NULL);
}
if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
if (http2 != http)
httpClose(http2);
return (NULL);
}
strlcat(resource, ".ppd", sizeof(resource));
status = cupsGetFd(http2, resource, fd);
close(fd);
if (http2 != http)
httpClose(http2);
if (status != HTTP_OK)
{
switch (status)
{
case HTTP_NOT_FOUND :
_cupsSetError(IPP_NOT_FOUND, httpStatus(status));
break;
case HTTP_UNAUTHORIZED :
_cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status));
break;
default :
DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
status));
_cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status));
break;
}
unlink(cg->ppd_filename);
return (NULL);
}
return (cg->ppd_filename);
}
int
cupsGetPrinters(char ***printers)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
char **temp;
_cups_globals_t *cg = _cupsGlobals();
if (printers == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (0);
}
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
return (0);
}
request = ippNewRequest(CUPS_GET_PRINTERS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", NULL, "printer-name");
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
"printer-type", 0);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
"printer-type-mask", CUPS_PRINTER_CLASS);
n = 0;
*printers = NULL;
if ((response = cupsDoRequest(cg->http, request, "/")) != NULL)
{
for (attr = response->attrs; attr != NULL; attr = attr->next)
if (attr->name != NULL &&
strcasecmp(attr->name, "printer-name") == 0 &&
attr->value_tag == IPP_TAG_NAME)
{
if (n == 0)
temp = malloc(sizeof(char *));
else
temp = realloc(*printers, sizeof(char *) * (n + 1));
if (temp == NULL)
{
while (n > 0)
{
n --;
free((*printers)[n]);
}
free(*printers);
ippDelete(response);
return (0);
}
*printers = temp;
temp[n] = strdup(attr->values[0].string.text);
n ++;
}
ippDelete(response);
}
return (n);
}
char *
cupsGetServerPPD(http_t *http,
const char *name)
{
int fd;
ipp_t *request;
_cups_globals_t *cg = _cupsGlobals();
if (!http || !name)
{
if (!http)
_cupsSetError(IPP_INTERNAL_ERROR, "No HTTP connection!");
else
_cupsSetError(IPP_INTERNAL_ERROR, "No PPD name!");
return (NULL);
}
if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
return (NULL);
}
request = ippNewRequest(CUPS_GET_PPD);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
name);
ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
close(fd);
if (cupsLastError() != IPP_OK)
{
unlink(cg->ppd_filename);
return (NULL);
}
else
return (cg->ppd_filename);
}
ipp_status_t
cupsLastError(void)
{
return (_cupsGlobals()->last_error);
}
const char *
cupsLastErrorString(void)
{
return (_cupsGlobals()->last_status_message);
}
int
cupsPrintFile(const char *name,
const char *filename,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
"title=\"%s\", num_options=%d, options=%p)\n",
name, filename, title, num_options, options));
return (cupsPrintFiles(name, 1, &filename, title, num_options, options));
}
int
cupsPrintFile2(http_t *http,
const char *name,
const char *filename,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
"title=\"%s\", num_options=%d, options=%p)\n",
http, name, filename, title, num_options, options));
return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, options));
}
int
cupsPrintFiles(const char *name,
int num_files,
const char **files,
const char *title,
int num_options,
cups_option_t *options)
{
_cups_globals_t *cg = _cupsGlobals();
DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
"files=%p, title=\"%s\", num_options=%d, options=%p)\n",
name, num_files, files, title, num_options, options));
if (!cups_connect(name, NULL, NULL))
{
DEBUG_printf(("cupsPrintFiles: Unable to open connection - %s.\n",
strerror(errno)));
DEBUG_puts("Unable to connect to server!");
return (0);
}
return (cupsPrintFiles2(cg->http, name, num_files, files, title,
num_options, options));
}
int
cupsPrintFiles2(http_t *http,
const char *name,
int num_files,
const char **files,
const char *title,
int num_options,
cups_option_t *options)
{
int i;
const char *val;
ipp_t *request;
ipp_t *response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI];
int jobid;
const char *base;
DEBUG_printf(("cupsPrintFiles(http=%p, name=\"%s\", num_files=%d, "
"files=%p, title=\"%s\", num_options=%d, options=%p)\n",
http, name, num_files, files, title, num_options, options));
if (!http || !name || num_files < 1 || files == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (0);
}
if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (0);
}
if ((request = ippNewRequest(num_files == 1 ? IPP_PRINT_JOB :
IPP_CREATE_JOB)) == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
return (0);
}
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if (title)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
title);
cupsEncodeOptions(request, num_options, options);
snprintf(uri, sizeof(uri), "/printers/%s", name);
if (num_files == 1)
response = cupsDoFileRequest(http, request, uri, *files);
else
response = cupsDoRequest(http, request, uri);
if (response == NULL)
jobid = 0;
else if (response->request.status.status_code > IPP_OK_CONFLICT)
{
DEBUG_printf(("IPP response code was 0x%x!\n",
response->request.status.status_code));
jobid = 0;
}
else if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL)
{
DEBUG_puts("No job ID!");
_cupsSetError(IPP_INTERNAL_ERROR, NULL);
jobid = 0;
}
else
jobid = attr->values[0].integer;
if (response != NULL)
ippDelete(response);
if (jobid > 0 && num_files > 1)
for (i = 0; i < num_files; i ++)
{
if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL)
return (0);
snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", jobid);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
NULL, uri);
if (cupsGetOption("raw", num_options, options))
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
"document-format", NULL, "application/vnd.cups-raw");
else if ((val = cupsGetOption("document-format", num_options,
options)) != NULL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
"document-format", NULL, val);
else
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
"document-format", NULL, "application/octet-stream");
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if ((base = strrchr(files[i], '/')) != NULL)
base ++;
else
base = files[i];
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
NULL, base);
if (i == (num_files - 1))
ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1);
snprintf(uri, sizeof(uri), "/printers/%s", name);
if ((response = cupsDoFileRequest(http, request, uri,
files[i])) != NULL)
ippDelete(response);
}
return (jobid);
}
static char *
cups_connect(const char *name,
char *printer,
char *hostname)
{
char hostbuf[HTTP_MAX_URI],
http_hostname[HTTP_MAX_HOST];
_cups_globals_t *cg = _cupsGlobals();
DEBUG_printf(("cups_connect(\"%s\", %p, %p)\n", name, printer, hostname));
if (name == NULL)
{
_cupsSetError(IPP_BAD_REQUEST, NULL);
return (NULL);
}
strlcpy(hostbuf, cupsServer(), sizeof(hostbuf));
httpGetHostname(cg->http, http_hostname, sizeof(http_hostname));
if (hostname != NULL)
strlcpy(hostname, hostbuf, HTTP_MAX_URI);
else
hostname = hostbuf;
if (printer != NULL)
strlcpy(printer, name, HTTP_MAX_URI);
else
printer = (char *)name;
if (cg->http != NULL)
{
if (!strcasecmp(http_hostname, hostname))
return (printer);
httpClose(cg->http);
}
DEBUG_printf(("connecting to %s on port %d...\n", hostname, ippPort()));
if ((cg->http = httpConnectEncrypt(hostname, ippPort(),
cupsEncryption())) == NULL)
{
DEBUG_puts("Unable to connect to server!");
_cupsSetError(IPP_SERVICE_UNAVAILABLE, strerror(errno));
return (NULL);
}
else
return (printer);
}
static int
cups_get_printer_uri(
http_t *http,
const char *name,
char *host,
int hostsize,
int *port,
char *resource,
int resourcesize,
int depth)
{
int i;
int http_port;
http_t *http2;
ipp_t *request,
*response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI],
scheme[HTTP_MAX_URI],
username[HTTP_MAX_URI],
classname[255],
http_hostname[HTTP_MAX_HOST];
static const char * const requested_attrs[] =
{
"printer-uri-supported",
"printer-type",
"member-uris"
};
DEBUG_printf(("cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
"hostsize=%d, resource=%p, resourcesize=%d, depth=%d)\n",
http, name ? name : "(null)", host, hostsize,
resource, resourcesize, depth));
if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
{
_cupsSetError(IPP_INTERNAL_ERROR, "Unable to create printer-uri!");
*host = '\0';
*resource = '\0';
return (0);
}
DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", uri));
httpGetHostname(http, http_hostname, sizeof(http_hostname));
#ifdef AF_INET6
if (http->hostaddr->addr.sa_family == AF_INET6)
http_port = ntohs(http->hostaddr->ipv6.sin6_port);
else
#endif
if (http->hostaddr->addr.sa_family == AF_INET)
http_port = ntohs(http->hostaddr->ipv4.sin_port);
else
http_port = ippPort();
request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requested-attributes",
sizeof(requested_attrs) / sizeof(requested_attrs[0]),
NULL, requested_attrs);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
{
for (i = 0; i < attr->num_values; i ++)
{
httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
scheme, sizeof(scheme), username, sizeof(username),
host, hostsize, port, resource, resourcesize);
if (!strncmp(resource, "/printers/", 10))
{
ippDelete(response);
return (1);
}
}
if (depth < 3)
{
for (i = 0; i < attr->num_values; i ++)
{
httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
scheme, sizeof(scheme), username, sizeof(username),
host, hostsize, port, resource, resourcesize);
if (!strncmp(resource, "/classes/", 9))
{
if (!strcasecmp(http_hostname, host) && *port == http_port)
http2 = http;
else if ((http2 = httpConnectEncrypt(host, *port,
cupsEncryption())) == NULL)
{
DEBUG_puts("Unable to connect to server!");
continue;
}
strlcpy(classname, resource + 9, sizeof(classname));
cups_get_printer_uri(http2, classname, host, hostsize, port,
resource, resourcesize, depth + 1);
if (http2 != http)
httpClose(http2);
if (*host)
return (1);
}
}
}
}
else if ((attr = ippFindAttribute(response, "printer-uri-supported",
IPP_TAG_URI)) != NULL)
{
httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
scheme, sizeof(scheme), username, sizeof(username),
host, hostsize, port, resource, resourcesize);
ippDelete(response);
return (1);
}
ippDelete(response);
}
if (cupsLastError() != IPP_NOT_FOUND)
_cupsSetError(IPP_INTERNAL_ERROR, "No printer-uri found!");
*host = '\0';
*resource = '\0';
return (0);
}