]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/util.c
eade62f081ddad7246f0d5343603e884ce243563
[thirdparty/cups.git] / cups / util.c
1 /*
2 * "$Id: util.c 7014 2007-10-10 21:57:43Z mike $"
3 *
4 * Printing utilities for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * cupsCancelJob() - Cancel a print job on the default server.
20 * cupsCancelJob2() - Cancel or purge a print job.
21 * cupsCreateJob() - Create an empty job.
22 * cupsFinishDocument() - Finish sending a document.
23 * cupsFreeJobs() - Free memory used by job data.
24 * cupsGetClasses() - Get a list of printer classes from the default
25 * server.
26 * cupsGetDefault() - Get the default printer or class for the default
27 * server.
28 * cupsGetDefault2() - Get the default printer or class for the specified
29 * server.
30 * cupsGetJobs() - Get the jobs from the default server.
31 * cupsGetJobs2() - Get the jobs from the specified server.
32 * cupsGetPPD() - Get the PPD file for a printer on the default
33 * server.
34 * cupsGetPPD2() - Get the PPD file for a printer from the specified
35 * server.
36 * cupsGetPPD3() - Get the PPD file for a printer on the specified
37 * server if it has changed.
38 * cupsGetPrinters() - Get a list of printers from the default server.
39 * cupsGetServerPPD() - Get an available PPD file from the server.
40 * cupsLastError() - Return the last IPP status code.
41 * cupsLastErrorString() - Return the last IPP status-message.
42 * cupsPrintFile() - Print a file to a printer or class on the default
43 * server.
44 * cupsPrintFile2() - Print a file to a printer or class on the
45 * specified server.
46 * cupsPrintFiles() - Print one or more files to a printer or class on
47 * the default server.
48 * cupsPrintFiles2() - Print one or more files to a printer or class on
49 * the specified server.
50 * cupsStartDocument() - Add a document to a job created with
51 * cupsCreateJob().
52 * _cupsConnect() - Get the default server connection...
53 * cups_get_printer_uri() - Get the printer-uri-supported attribute for the
54 * first printer in a class.
55 */
56
57 /*
58 * Include necessary headers...
59 */
60
61 #include "globals.h"
62 #include "debug.h"
63 #include <stdlib.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <sys/stat.h>
67 #if defined(WIN32) || defined(__EMX__)
68 # include <io.h>
69 #else
70 # include <unistd.h>
71 #endif /* WIN32 || __EMX__ */
72
73
74 /*
75 * Local functions...
76 */
77
78 static int cups_get_printer_uri(http_t *http, const char *name,
79 char *host, int hostsize, int *port,
80 char *resource, int resourcesize,
81 int depth);
82
83
84 /*
85 * 'cupsCancelJob()' - Cancel a print job on the default server.
86 *
87 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
88 * to cancel the current job on the named destination.
89 *
90 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
91 * the cause of any failure.
92 */
93
94 int /* O - 1 on success, 0 on failure */
95 cupsCancelJob(const char *name, /* I - Name of printer or class */
96 int job_id) /* I - Job ID */
97 {
98 return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0)
99 < IPP_REDIRECTION_OTHER_SITE);
100 }
101
102
103 /*
104 * 'cupsCancelJob2()' - Cancel or purge a print job.
105 *
106 * Canceled jobs remain in the job history while purged jobs are removed
107 * from the job history.
108 *
109 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
110 * to cancel the current job on the named destination.
111 *
112 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
113 * the cause of any failure.
114 *
115 * @since CUPS 1.4@
116 */
117
118 ipp_status_t /* O - IPP status */
119 cupsCancelJob2(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
120 const char *name, /* I - Name of printer or class */
121 int job_id, /* I - Job ID or 0 for the current job, -1 for all jobs */
122 int purge) /* I - 1 to purge, 0 to cancel */
123 {
124 char uri[HTTP_MAX_URI]; /* Job/printer URI */
125 ipp_t *request; /* IPP request */
126
127
128 /*
129 * Range check input...
130 */
131
132 if (job_id < -1 || (!name && job_id == 0))
133 {
134 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL));
135 return (0);
136 }
137
138 /*
139 * Connect to the default server as needed...
140 */
141
142 if (!http)
143 if ((http = _cupsConnect()) == NULL)
144 return (IPP_SERVICE_UNAVAILABLE);
145
146 /*
147 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
148 * attributes:
149 *
150 * attributes-charset
151 * attributes-natural-language
152 * job-uri or printer-uri + job-id
153 * requesting-user-name
154 * [purge-job] or [purge-jobs]
155 */
156
157 request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB);
158
159 if (name)
160 {
161 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
162 "localhost", ippPort(), "/printers/%s", name);
163
164 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
165 uri);
166 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
167 job_id);
168 }
169 else if (job_id > 0)
170 {
171 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
172
173 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
174 }
175
176 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
177 NULL, cupsUser());
178
179 if (purge && job_id >= 0)
180 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1);
181 else if (!purge && job_id < 0)
182 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0);
183
184 /*
185 * Do the request...
186 */
187
188 ippDelete(cupsDoRequest(http, request, "/jobs/"));
189
190 return (cupsLastError());
191 }
192
193
194 /*
195 * 'cupsCreateJob()' - Create an empty job.
196 *
197 * Submit files for printing to the job using the @link cupsStartDocument@,
198 * @link cupsWriteRequestData@, and @link cupsFinishDocument@ functions.
199 *
200 * @since CUPS 1.4@
201 */
202
203 int /* O - Job ID or 0 on error */
204 cupsCreateJob(
205 http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
206 const char *name, /* I - Printer or class name */
207 const char *title, /* I - Title of job */
208 int num_options, /* I - Number of options */
209 cups_option_t *options) /* I - Options */
210 {
211 char printer_uri[1024], /* Printer URI */
212 resource[1024]; /* Printer resource */
213 ipp_t *request, /* Create-Job request */
214 *response; /* Create-Job response */
215 ipp_attribute_t *attr; /* job-id attribute */
216 int job_id = 0; /* job-id value */
217
218
219 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
220 "num_options=%d, options=%p)\n",
221 http, name, title, num_options, options));
222
223 /*
224 * Range check input...
225 */
226
227 if (!name)
228 {
229 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
230 return (0);
231 }
232
233 /*
234 * Build a Create-Job request...
235 */
236
237 if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL)
238 {
239 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
240 return (0);
241 }
242
243 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
244 NULL, "localhost", ippPort(), "/printers/%s", name);
245 snprintf(resource, sizeof(resource), "/printers/%s", name);
246
247 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
248 NULL, printer_uri);
249 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
250 NULL, cupsUser());
251 if (title)
252 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
253 title);
254 cupsEncodeOptions(request, num_options, options);
255
256 /*
257 * Send the request and get the job-id...
258 */
259
260 response = cupsDoRequest(http, request, resource);
261
262 if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL)
263 job_id = attr->values[0].integer;
264
265 ippDelete(response);
266
267 /*
268 * Return it...
269 */
270
271 return (job_id);
272 }
273
274
275 /*
276 * 'cupsFinishDocument()' - Finish sending a document.
277 *
278 * The document must have been started using @link cupsStartDocument@.
279 *
280 * @since CUPS 1.4@
281 */
282
283 ipp_status_t /* O - Status of document submission */
284 cupsFinishDocument(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
285 const char *name) /* I - Printer or class name */
286 {
287 char resource[1024]; /* Printer resource */
288
289
290 snprintf(resource, sizeof(resource), "/printers/%s", name);
291
292 ippDelete(cupsGetResponse(http, resource));
293
294 return (cupsLastError());
295 }
296
297
298 /*
299 * 'cupsFreeJobs()' - Free memory used by job data.
300 */
301
302 void
303 cupsFreeJobs(int num_jobs, /* I - Number of jobs */
304 cups_job_t *jobs) /* I - Jobs */
305 {
306 int i; /* Looping var */
307 cups_job_t *job; /* Current job */
308
309
310 if (num_jobs <= 0 || !jobs)
311 return;
312
313 for (i = num_jobs, job = jobs; i > 0; i --, job ++)
314 {
315 _cupsStrFree(job->dest);
316 _cupsStrFree(job->user);
317 _cupsStrFree(job->format);
318 _cupsStrFree(job->title);
319 }
320
321 free(jobs);
322 }
323
324
325 /*
326 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
327 *
328 * This function is deprecated - use @link cupsGetDests@ instead.
329 *
330 * @deprecated@
331 */
332
333 int /* O - Number of classes */
334 cupsGetClasses(char ***classes) /* O - Classes */
335 {
336 int n; /* Number of classes */
337 ipp_t *request, /* IPP Request */
338 *response; /* IPP Response */
339 ipp_attribute_t *attr; /* Current attribute */
340 char **temp; /* Temporary pointer */
341 http_t *http; /* Connection to server */
342
343
344 if (!classes)
345 {
346 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
347
348 return (0);
349 }
350
351 *classes = NULL;
352
353 if ((http = _cupsConnect()) == NULL)
354 return (0);
355
356 /*
357 * Build a CUPS_GET_CLASSES request, which requires the following
358 * attributes:
359 *
360 * attributes-charset
361 * attributes-natural-language
362 * requested-attributes
363 */
364
365 request = ippNewRequest(CUPS_GET_CLASSES);
366
367 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
368 "requested-attributes", NULL, "printer-name");
369
370 /*
371 * Do the request and get back a response...
372 */
373
374 n = 0;
375
376 if ((response = cupsDoRequest(http, request, "/")) != NULL)
377 {
378 for (attr = response->attrs; attr != NULL; attr = attr->next)
379 if (attr->name != NULL &&
380 strcasecmp(attr->name, "printer-name") == 0 &&
381 attr->value_tag == IPP_TAG_NAME)
382 {
383 if (n == 0)
384 temp = malloc(sizeof(char *));
385 else
386 temp = realloc(*classes, sizeof(char *) * (n + 1));
387
388 if (temp == NULL)
389 {
390 /*
391 * Ran out of memory!
392 */
393
394 while (n > 0)
395 {
396 n --;
397 free((*classes)[n]);
398 }
399
400 free(*classes);
401 ippDelete(response);
402 return (0);
403 }
404
405 *classes = temp;
406 temp[n] = strdup(attr->values[0].string.text);
407 n ++;
408 }
409
410 ippDelete(response);
411 }
412
413 return (n);
414 }
415
416
417 /*
418 * 'cupsGetDefault()' - Get the default printer or class for the default server.
419 *
420 * This function returns the default printer or class as defined by
421 * the LPDEST or PRINTER environment variables. If these environment
422 * variables are not set, the server default destination is returned.
423 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
424 * functions to get the user-defined default printer, as this function does
425 * not support the lpoptions-defined default printer.
426 */
427
428 const char * /* O - Default printer or @code NULL@ */
429 cupsGetDefault(void)
430 {
431 /*
432 * Return the default printer...
433 */
434
435 return (cupsGetDefault2(CUPS_HTTP_DEFAULT));
436 }
437
438
439 /*
440 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
441 *
442 * This function returns the default printer or class as defined by
443 * the LPDEST or PRINTER environment variables. If these environment
444 * variables are not set, the server default destination is returned.
445 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
446 * functions to get the user-defined default printer, as this function does
447 * not support the lpoptions-defined default printer.
448 *
449 * @since CUPS 1.1.21@
450 */
451
452 const char * /* O - Default printer or @code NULL@ */
453 cupsGetDefault2(http_t *http) /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
454 {
455 ipp_t *request, /* IPP Request */
456 *response; /* IPP Response */
457 ipp_attribute_t *attr; /* Current attribute */
458 const char *var; /* Environment variable */
459 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
460
461
462 /*
463 * First see if the LPDEST or PRINTER environment variables are
464 * set... However, if PRINTER is set to "lp", ignore it to work
465 * around a "feature" in most Linux distributions - the default
466 * user login scripts set PRINTER to "lp"...
467 */
468
469 if ((var = getenv("LPDEST")) != NULL)
470 return (var);
471 else if ((var = getenv("PRINTER")) != NULL && strcmp(var, "lp") != 0)
472 return (var);
473
474 /*
475 * Connect to the server as needed...
476 */
477
478 if (!http)
479 if ((http = _cupsConnect()) == NULL)
480 return (NULL);
481
482 /*
483 * Build a CUPS_GET_DEFAULT request, which requires the following
484 * attributes:
485 *
486 * attributes-charset
487 * attributes-natural-language
488 */
489
490 request = ippNewRequest(CUPS_GET_DEFAULT);
491
492 /*
493 * Do the request and get back a response...
494 */
495
496 if ((response = cupsDoRequest(http, request, "/")) != NULL)
497 {
498 if ((attr = ippFindAttribute(response, "printer-name",
499 IPP_TAG_NAME)) != NULL)
500 {
501 strlcpy(cg->def_printer, attr->values[0].string.text,
502 sizeof(cg->def_printer));
503 ippDelete(response);
504 return (cg->def_printer);
505 }
506
507 ippDelete(response);
508 }
509
510 return (NULL);
511 }
512
513
514 /*
515 * 'cupsGetJobs()' - Get the jobs from the default server.
516 *
517 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
518 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
519 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
520 * jobs that are stopped, canceled, aborted, or completed.
521 */
522
523 int /* O - Number of jobs */
524 cupsGetJobs(cups_job_t **jobs, /* O - Job data */
525 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for mydest */
526 int myjobs, /* I - 0 = all users, 1 = mine */
527 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
528 {
529 /*
530 * Return the jobs...
531 */
532
533 return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs));
534 }
535
536
537
538 /*
539 * 'cupsGetJobs2()' - Get the jobs from the specified server.
540 *
541 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
542 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
543 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
544 * jobs that are stopped, canceled, aborted, or completed.
545 *
546 * @since CUPS 1.1.21@
547 */
548
549 int /* O - Number of jobs */
550 cupsGetJobs2(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
551 cups_job_t **jobs, /* O - Job data */
552 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for mydest */
553 int myjobs, /* I - 0 = all users, 1 = mine */
554 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
555 {
556 int n; /* Number of jobs */
557 ipp_t *request, /* IPP Request */
558 *response; /* IPP Response */
559 ipp_attribute_t *attr; /* Current attribute */
560 cups_job_t *temp; /* Temporary pointer */
561 int id, /* job-id */
562 priority, /* job-priority */
563 size; /* job-k-octets */
564 ipp_jstate_t state; /* job-state */
565 time_t completed_time, /* time-at-completed */
566 creation_time, /* time-at-creation */
567 processing_time; /* time-at-processing */
568 const char *dest, /* job-printer-uri */
569 *format, /* document-format */
570 *title, /* job-name */
571 *user; /* job-originating-user-name */
572 char uri[HTTP_MAX_URI]; /* URI for jobs */
573 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
574 static const char * const attrs[] = /* Requested attributes */
575 {
576 "job-id",
577 "job-priority",
578 "job-k-octets",
579 "job-state",
580 "time-at-completed",
581 "time-at-creation",
582 "time-at-processing",
583 "job-printer-uri",
584 "document-format",
585 "job-name",
586 "job-originating-user-name"
587 };
588
589
590 /*
591 * Range check input...
592 */
593
594 if (!jobs)
595 {
596 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
597
598 return (-1);
599 }
600
601 /*
602 * Get the right URI...
603 */
604
605 if (name)
606 {
607 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
608 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
609 {
610 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
611
612 return (-1);
613 }
614 }
615 else
616 strcpy(uri, "ipp://localhost/jobs");
617
618 if (!http)
619 if ((http = _cupsConnect()) == NULL)
620 return (-1);
621
622 /*
623 * Build an IPP_GET_JOBS request, which requires the following
624 * attributes:
625 *
626 * attributes-charset
627 * attributes-natural-language
628 * printer-uri
629 * requesting-user-name
630 * which-jobs
631 * my-jobs
632 * requested-attributes
633 */
634
635 request = ippNewRequest(IPP_GET_JOBS);
636
637 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
638 "printer-uri", NULL, uri);
639
640 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
641 "requesting-user-name", NULL, cupsUser());
642
643 if (myjobs)
644 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
645
646 if (whichjobs == CUPS_WHICHJOBS_COMPLETED)
647 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
648 "which-jobs", NULL, "completed");
649 else if (whichjobs == CUPS_WHICHJOBS_ALL)
650 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
651 "which-jobs", NULL, "all");
652
653 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
654 "requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
655 NULL, attrs);
656
657 /*
658 * Do the request and get back a response...
659 */
660
661 n = 0;
662 *jobs = NULL;
663
664 if ((response = cupsDoRequest(http, request, "/")) != NULL)
665 {
666 for (attr = response->attrs; attr; attr = attr->next)
667 {
668 /*
669 * Skip leading attributes until we hit a job...
670 */
671
672 while (attr && attr->group_tag != IPP_TAG_JOB)
673 attr = attr->next;
674
675 if (!attr)
676 break;
677
678 /*
679 * Pull the needed attributes from this job...
680 */
681
682 id = 0;
683 size = 0;
684 priority = 50;
685 state = IPP_JOB_PENDING;
686 user = "unknown";
687 dest = NULL;
688 format = "application/octet-stream";
689 title = "untitled";
690 creation_time = 0;
691 completed_time = 0;
692 processing_time = 0;
693
694 while (attr && attr->group_tag == IPP_TAG_JOB)
695 {
696 if (!strcmp(attr->name, "job-id") &&
697 attr->value_tag == IPP_TAG_INTEGER)
698 id = attr->values[0].integer;
699 else if (!strcmp(attr->name, "job-state") &&
700 attr->value_tag == IPP_TAG_ENUM)
701 state = (ipp_jstate_t)attr->values[0].integer;
702 else if (!strcmp(attr->name, "job-priority") &&
703 attr->value_tag == IPP_TAG_INTEGER)
704 priority = attr->values[0].integer;
705 else if (!strcmp(attr->name, "job-k-octets") &&
706 attr->value_tag == IPP_TAG_INTEGER)
707 size = attr->values[0].integer;
708 else if (!strcmp(attr->name, "time-at-completed") &&
709 attr->value_tag == IPP_TAG_INTEGER)
710 completed_time = attr->values[0].integer;
711 else if (!strcmp(attr->name, "time-at-creation") &&
712 attr->value_tag == IPP_TAG_INTEGER)
713 creation_time = attr->values[0].integer;
714 else if (!strcmp(attr->name, "time-at-processing") &&
715 attr->value_tag == IPP_TAG_INTEGER)
716 processing_time = attr->values[0].integer;
717 else if (!strcmp(attr->name, "job-printer-uri") &&
718 attr->value_tag == IPP_TAG_URI)
719 {
720 if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
721 dest ++;
722 }
723 else if (!strcmp(attr->name, "job-originating-user-name") &&
724 attr->value_tag == IPP_TAG_NAME)
725 user = attr->values[0].string.text;
726 else if (!strcmp(attr->name, "document-format") &&
727 attr->value_tag == IPP_TAG_MIMETYPE)
728 format = attr->values[0].string.text;
729 else if (!strcmp(attr->name, "job-name") &&
730 (attr->value_tag == IPP_TAG_TEXT ||
731 attr->value_tag == IPP_TAG_NAME))
732 title = attr->values[0].string.text;
733
734 attr = attr->next;
735 }
736
737 /*
738 * See if we have everything needed...
739 */
740
741 if (!dest || !id)
742 {
743 if (!attr)
744 break;
745 else
746 continue;
747 }
748
749 /*
750 * Allocate memory for the job...
751 */
752
753 if (n == 0)
754 temp = malloc(sizeof(cups_job_t));
755 else
756 temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1));
757
758 if (!temp)
759 {
760 /*
761 * Ran out of memory!
762 */
763
764 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
765
766 cupsFreeJobs(n, *jobs);
767 *jobs = NULL;
768
769 ippDelete(response);
770
771 return (-1);
772 }
773
774 *jobs = temp;
775 temp += n;
776 n ++;
777
778 /*
779 * Copy the data over...
780 */
781
782 temp->dest = _cupsStrAlloc(dest);
783 temp->user = _cupsStrAlloc(user);
784 temp->format = _cupsStrAlloc(format);
785 temp->title = _cupsStrAlloc(title);
786 temp->id = id;
787 temp->priority = priority;
788 temp->state = state;
789 temp->size = size;
790 temp->completed_time = completed_time;
791 temp->creation_time = creation_time;
792 temp->processing_time = processing_time;
793
794 if (!attr)
795 break;
796 }
797
798 ippDelete(response);
799 }
800
801 if (n == 0 && cg->last_error >= IPP_BAD_REQUEST)
802 return (-1);
803 else
804 return (n);
805 }
806
807
808 /*
809 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
810 *
811 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
812 * in the class.
813 */
814
815 const char * /* O - Filename for PPD file */
816 cupsGetPPD(const char *name) /* I - Printer name */
817 {
818 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
819 time_t modtime = 0; /* Modification time */
820
821
822 /*
823 * Return the PPD file...
824 */
825
826 cg->ppd_filename[0] = '\0';
827
828 if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename,
829 sizeof(cg->ppd_filename)) == HTTP_OK)
830 return (cg->ppd_filename);
831 else
832 return (NULL);
833 }
834
835
836 /*
837 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
838 *
839 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
840 * in the class.
841 *
842 * @since CUPS 1.1.21@
843 */
844
845 const char * /* O - Filename for PPD file */
846 cupsGetPPD2(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
847 const char *name) /* I - Printer name */
848 {
849 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
850 time_t modtime = 0; /* Modification time */
851
852
853 cg->ppd_filename[0] = '\0';
854
855 if (cupsGetPPD3(http, name, &modtime, cg->ppd_filename,
856 sizeof(cg->ppd_filename)) == HTTP_OK)
857 return (cg->ppd_filename);
858 else
859 return (NULL);
860 }
861
862
863 /*
864 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
865 * server if it has changed.
866 *
867 * The "modtime" parameter contains the modification time of any
868 * locally-cached content and is updated with the time from the PPD file on
869 * the server.
870 *
871 * The "buffer" parameter contains the local PPD filename. If it contains
872 * the empty string, a new temporary file is created, otherwise the existing
873 * file will be overwritten as needed.
874 *
875 * On success, @code HTTP_OK@ is returned for a new PPD file and
876 * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
877 * status is an error.
878 *
879 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
880 * in the class.
881 */
882
883 http_status_t /* O - HTTP status */
884 cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
885 const char *name, /* I - Printer name */
886 time_t *modtime, /* IO - Modification time */
887 char *buffer, /* I - Filename buffer */
888 size_t bufsize) /* I - Size of filename buffer */
889 {
890 int http_port; /* Port number */
891 char http_hostname[HTTP_MAX_HOST];
892 /* Hostname associated with connection */
893 http_t *http2; /* Alternate HTTP connection */
894 int fd; /* PPD file */
895 char localhost[HTTP_MAX_URI],/* Local hostname */
896 hostname[HTTP_MAX_URI], /* Hostname */
897 resource[HTTP_MAX_URI]; /* Resource name */
898 int port; /* Port number */
899 http_status_t status; /* HTTP status from server */
900 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
901
902
903 /*
904 * Range check input...
905 */
906
907 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
908 "bufsize=%d)\n", http, name ? name : "(null)", modtime,
909 modtime ? *modtime : 0, buffer, (int)bufsize));
910
911 if (!name)
912 {
913 _cupsSetError(IPP_INTERNAL_ERROR, "No printer name!");
914 return (HTTP_NOT_ACCEPTABLE);
915 }
916
917 if (!modtime)
918 {
919 _cupsSetError(IPP_INTERNAL_ERROR, "No modification time!");
920 return (HTTP_NOT_ACCEPTABLE);
921 }
922
923 if (!buffer || bufsize <= 1)
924 {
925 _cupsSetError(IPP_INTERNAL_ERROR, "Bad filename buffer!");
926 return (HTTP_NOT_ACCEPTABLE);
927 }
928
929 /*
930 * Try finding a printer URI for this printer...
931 */
932
933 if (!http)
934 http = _cupsConnect();
935
936 if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
937 resource, sizeof(resource), 0))
938 return (HTTP_NOT_FOUND);
939
940 DEBUG_printf(("Printer hostname=\"%s\", port=%d\n", hostname, port));
941
942 /*
943 * Remap local hostname to localhost...
944 */
945
946 httpGetHostname(NULL, localhost, sizeof(localhost));
947
948 DEBUG_printf(("Local hostname=\"%s\"\n", localhost));
949
950 if (!strcasecmp(localhost, hostname))
951 strcpy(hostname, "localhost");
952
953 /*
954 * Get the hostname and port number we are connected to...
955 */
956
957 httpGetHostname(http, http_hostname, sizeof(http_hostname));
958
959 #ifdef AF_INET6
960 if (http->hostaddr->addr.sa_family == AF_INET6)
961 http_port = ntohs(http->hostaddr->ipv6.sin6_port);
962 else
963 #endif /* AF_INET6 */
964 if (http->hostaddr->addr.sa_family == AF_INET)
965 http_port = ntohs(http->hostaddr->ipv4.sin_port);
966 else
967 http_port = ippPort();
968
969 DEBUG_printf(("Connection hostname=\"%s\", port=%d\n", http_hostname,
970 http_port));
971
972 /*
973 * Reconnect to the correct server as needed...
974 */
975
976 if (!strcasecmp(http_hostname, hostname) && port == http_port)
977 http2 = http;
978 else if ((http2 = httpConnectEncrypt(hostname, port,
979 cupsEncryption())) == NULL)
980 {
981 DEBUG_puts("Unable to connect to server!");
982
983 return (HTTP_SERVICE_UNAVAILABLE);
984 }
985
986 /*
987 * Get a temp file...
988 */
989
990 if (buffer[0])
991 fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600);
992 else
993 fd = cupsTempFd(buffer, bufsize);
994
995 if (fd < 0)
996 {
997 /*
998 * Can't open file; close the server connection and return NULL...
999 */
1000
1001 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1002
1003 if (http2 != http)
1004 httpClose(http2);
1005
1006 return (HTTP_SERVER_ERROR);
1007 }
1008
1009 /*
1010 * And send a request to the HTTP server...
1011 */
1012
1013 strlcat(resource, ".ppd", sizeof(resource));
1014
1015 if (*modtime > 0)
1016 httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE,
1017 httpGetDateString(*modtime));
1018
1019 status = cupsGetFd(http2, resource, fd);
1020
1021 close(fd);
1022
1023 /*
1024 * See if we actually got the file or an error...
1025 */
1026
1027 if (status == HTTP_OK)
1028 *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE));
1029 else if (status != HTTP_NOT_MODIFIED)
1030 {
1031 switch (status)
1032 {
1033 case HTTP_NOT_FOUND :
1034 _cupsSetError(IPP_NOT_FOUND, httpStatus(status));
1035 break;
1036
1037 case HTTP_UNAUTHORIZED :
1038 _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status));
1039 break;
1040
1041 default :
1042 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
1043 status));
1044 _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status));
1045 break;
1046 }
1047
1048 unlink(cg->ppd_filename);
1049 }
1050
1051 if (http2 != http)
1052 httpClose(http2);
1053
1054 /*
1055 * Return the PPD file...
1056 */
1057
1058 return (status);
1059 }
1060
1061
1062 /*
1063 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1064 *
1065 * This function is deprecated - use @link cupsGetDests@ instead.
1066 *
1067 * @deprecated@
1068 */
1069
1070 int /* O - Number of printers */
1071 cupsGetPrinters(char ***printers) /* O - Printers */
1072 {
1073 int n; /* Number of printers */
1074 ipp_t *request, /* IPP Request */
1075 *response; /* IPP Response */
1076 ipp_attribute_t *attr; /* Current attribute */
1077 char **temp; /* Temporary pointer */
1078 http_t *http; /* Connection to server */
1079
1080
1081 /*
1082 * Range check input...
1083 */
1084
1085 if (!printers)
1086 {
1087 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
1088
1089 return (0);
1090 }
1091
1092 *printers = NULL;
1093
1094 /*
1095 * Try to connect to the server...
1096 */
1097
1098 if ((http = _cupsConnect()) == NULL)
1099 return (0);
1100
1101 /*
1102 * Build a CUPS_GET_PRINTERS request, which requires the following
1103 * attributes:
1104 *
1105 * attributes-charset
1106 * attributes-natural-language
1107 * requested-attributes
1108 */
1109
1110 request = ippNewRequest(CUPS_GET_PRINTERS);
1111
1112 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1113 "requested-attributes", NULL, "printer-name");
1114
1115 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1116 "printer-type", 0);
1117
1118 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1119 "printer-type-mask", CUPS_PRINTER_CLASS);
1120
1121 /*
1122 * Do the request and get back a response...
1123 */
1124
1125 n = 0;
1126
1127 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1128 {
1129 for (attr = response->attrs; attr != NULL; attr = attr->next)
1130 if (attr->name != NULL &&
1131 strcasecmp(attr->name, "printer-name") == 0 &&
1132 attr->value_tag == IPP_TAG_NAME)
1133 {
1134 if (n == 0)
1135 temp = malloc(sizeof(char *));
1136 else
1137 temp = realloc(*printers, sizeof(char *) * (n + 1));
1138
1139 if (temp == NULL)
1140 {
1141 /*
1142 * Ran out of memory!
1143 */
1144
1145 while (n > 0)
1146 {
1147 n --;
1148 free((*printers)[n]);
1149 }
1150
1151 free(*printers);
1152 ippDelete(response);
1153 return (0);
1154 }
1155
1156 *printers = temp;
1157 temp[n] = strdup(attr->values[0].string.text);
1158 n ++;
1159 }
1160
1161 ippDelete(response);
1162 }
1163
1164 return (n);
1165 }
1166
1167
1168 /*
1169 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1170 *
1171 * This function returns the named PPD file from the server. The
1172 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1173 * operation.
1174 *
1175 * You must remove (unlink) the PPD file when you are finished with
1176 * it. The PPD filename is stored in a static location that will be
1177 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1178 * or @link cupsGetServerPPD@.
1179 *
1180 * @since CUPS 1.3@
1181 */
1182
1183 char * /* O - Name of PPD file or @code NULL@ on error */
1184 cupsGetServerPPD(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
1185 const char *name) /* I - Name of PPD file ("ppd-name") */
1186 {
1187 int fd; /* PPD file descriptor */
1188 ipp_t *request; /* IPP request */
1189 _cups_globals_t *cg = _cupsGlobals();
1190 /* Pointer to library globals */
1191
1192
1193 /*
1194 * Range check input...
1195 */
1196
1197 if (!name)
1198 {
1199 _cupsSetError(IPP_INTERNAL_ERROR, "No PPD name!");
1200
1201 return (NULL);
1202 }
1203
1204 if (!http)
1205 if ((http = _cupsConnect()) == NULL)
1206 return (NULL);
1207
1208 /*
1209 * Get a temp file...
1210 */
1211
1212 if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
1213 {
1214 /*
1215 * Can't open file; close the server connection and return NULL...
1216 */
1217
1218 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno));
1219
1220 return (NULL);
1221 }
1222
1223 /*
1224 * Get the PPD file...
1225 */
1226
1227 request = ippNewRequest(CUPS_GET_PPD);
1228 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
1229 name);
1230
1231 ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
1232
1233 close(fd);
1234
1235 if (cupsLastError() != IPP_OK)
1236 {
1237 unlink(cg->ppd_filename);
1238 return (NULL);
1239 }
1240 else
1241 return (cg->ppd_filename);
1242 }
1243
1244
1245 /*
1246 * 'cupsLastError()' - Return the last IPP status code.
1247 */
1248
1249 ipp_status_t /* O - IPP status code from last request */
1250 cupsLastError(void)
1251 {
1252 return (_cupsGlobals()->last_error);
1253 }
1254
1255
1256 /*
1257 * 'cupsLastErrorString()' - Return the last IPP status-message.
1258 *
1259 * @since CUPS 1.2@
1260 */
1261
1262 const char * /* O - status-message text from last request */
1263 cupsLastErrorString(void)
1264 {
1265 return (_cupsGlobals()->last_status_message);
1266 }
1267
1268
1269 /*
1270 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1271 */
1272
1273 int /* O - Job ID or 0 on error */
1274 cupsPrintFile(const char *name, /* I - Printer or class name */
1275 const char *filename, /* I - File to print */
1276 const char *title, /* I - Title of job */
1277 int num_options,/* I - Number of options */
1278 cups_option_t *options) /* I - Options */
1279 {
1280 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1281 "title=\"%s\", num_options=%d, options=%p)\n",
1282 name, filename, title, num_options, options));
1283
1284 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
1285 num_options, options));
1286 }
1287
1288
1289 /*
1290 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1291 * server.
1292 *
1293 * @since CUPS 1.1.21@
1294 */
1295
1296 int /* O - Job ID or 0 on error */
1297 cupsPrintFile2(
1298 http_t *http, /* I - HTTP connection */
1299 const char *name, /* I - Printer or class name */
1300 const char *filename, /* I - File to print */
1301 const char *title, /* I - Title of job */
1302 int num_options, /* I - Number of options */
1303 cups_option_t *options) /* I - Options */
1304 {
1305 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1306 "title=\"%s\", num_options=%d, options=%p)\n",
1307 http, name, filename, title, num_options, options));
1308
1309 return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
1310 options));
1311 }
1312
1313
1314 /*
1315 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1316 * default server.
1317 */
1318
1319 int /* O - Job ID or 0 on error */
1320 cupsPrintFiles(
1321 const char *name, /* I - Printer or class name */
1322 int num_files, /* I - Number of files */
1323 const char **files, /* I - File(s) to print */
1324 const char *title, /* I - Title of job */
1325 int num_options, /* I - Number of options */
1326 cups_option_t *options) /* I - Options */
1327 {
1328 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1329 "files=%p, title=\"%s\", num_options=%d, options=%p)\n",
1330 name, num_files, files, title, num_options, options));
1331
1332
1333 /*
1334 * Print the file(s)...
1335 */
1336
1337 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
1338 num_options, options));
1339 }
1340
1341
1342 /*
1343 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1344 * specified server.
1345 *
1346 * @since CUPS 1.1.21@
1347 */
1348
1349 int /* O - Job ID or 0 on error */
1350 cupsPrintFiles2(
1351 http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
1352 const char *name, /* I - Printer or class name */
1353 int num_files, /* I - Number of files */
1354 const char **files, /* I - File(s) to print */
1355 const char *title, /* I - Title of job */
1356 int num_options, /* I - Number of options */
1357 cups_option_t *options) /* I - Options */
1358 {
1359 int i; /* Looping var */
1360 int job_id; /* New job ID */
1361 const char *docname; /* Basename of current filename */
1362 const char *format; /* Document format */
1363 cups_file_t *fp; /* Current file */
1364 char buffer[8192]; /* Copy buffer */
1365 ssize_t bytes; /* Bytes in buffer */
1366 http_status_t status; /* Status of write */
1367
1368
1369 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1370 "files=%p, title=\"%s\", num_options=%d, options=%p)\n",
1371 http, name, num_files, files, title, num_options, options));
1372
1373 /*
1374 * Range check input...
1375 */
1376
1377 if (!name || num_files < 1 || !files)
1378 {
1379 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
1380
1381 return (0);
1382 }
1383
1384 /*
1385 * Create the print job...
1386 */
1387
1388 if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
1389 return (0);
1390
1391 /*
1392 * Send each of the files...
1393 */
1394
1395 if (cupsGetOption("raw", num_options, options))
1396 format = CUPS_FORMAT_RAW;
1397 else if ((format = cupsGetOption("document-format", num_options,
1398 options)) == NULL)
1399 format = CUPS_FORMAT_AUTO;
1400
1401 for (i = 0; i < num_files; i ++)
1402 {
1403 /*
1404 * Start the next file...
1405 */
1406
1407 if ((docname = strrchr(files[i], '/')) != NULL)
1408 docname ++;
1409 else
1410 docname = files[i];
1411
1412 if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
1413 {
1414 /*
1415 * Unable to open print file, cancel the job and return...
1416 */
1417
1418 cupsCancelJob2(http, name, job_id, 0);
1419 return (0);
1420 }
1421
1422 status = cupsStartDocument(http, name, job_id, docname, format,
1423 i == (num_files - 1));
1424
1425 while (status == HTTP_CONTINUE &&
1426 (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
1427 status = cupsWriteRequestData(http, buffer, bytes);
1428
1429 cupsFileClose(fp);
1430
1431 if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK)
1432 {
1433 /*
1434 * Unable to queue, cancel the job and return...
1435 */
1436
1437 cupsCancelJob2(http, name, job_id, 0);
1438 return (0);
1439 }
1440 }
1441
1442 return (job_id);
1443 }
1444
1445
1446 /*
1447 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1448 *
1449 * Use @link cupsWriteRequestData@ to write data for the document and
1450 * @link cupsFinishDocument@ to finish the document and get the submission status.
1451 *
1452 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1453 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1454 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1455 * any supported MIME type string can be supplied.
1456 *
1457 * @since CUPS 1.4@
1458 */
1459
1460 http_status_t /* O - HTTP status of request */
1461 cupsStartDocument(
1462 http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
1463 const char *name, /* I - Printer or class name */
1464 int job_id, /* I - Job ID from @link cupsCreateJob@ */
1465 const char *docname, /* I - Name of document */
1466 const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1467 int last_document) /* I - 1 for last document in job, 0 otherwise */
1468 {
1469 char resource[1024], /* Resource for destinatio */
1470 printer_uri[1024]; /* Printer URI */
1471 ipp_t *request; /* Send-Document request */
1472 http_status_t status; /* HTTP status */
1473
1474
1475 /*
1476 * Create a Send-Document request...
1477 */
1478
1479 if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL)
1480 {
1481 _cupsSetError(IPP_INTERNAL_ERROR, NULL);
1482 return (0);
1483 }
1484
1485 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
1486 NULL, "localhost", ippPort(), "/printers/%s", name);
1487 snprintf(resource, sizeof(resource), "/printers/%s", name);
1488
1489 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1490 NULL, printer_uri);
1491 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
1492 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1493 NULL, cupsUser());
1494 if (docname)
1495 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
1496 NULL, docname);
1497 if (format)
1498 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1499 "document-format", NULL, format);
1500 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document);
1501
1502 /*
1503 * Send and delete the request, then return the status...
1504 */
1505
1506 status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
1507
1508 ippDelete(request);
1509
1510 return (status);
1511 }
1512
1513
1514 /*
1515 * '_cupsConnect()' - Get the default server connection...
1516 */
1517
1518 http_t * /* O - HTTP connection */
1519 _cupsConnect(void)
1520 {
1521 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
1522
1523
1524 /*
1525 * See if we are connected to the same server...
1526 */
1527
1528 if (cg->http)
1529 {
1530 int port; /* Port for connection */
1531
1532
1533 /*
1534 * Get the port associated with the current connection...
1535 */
1536
1537 #ifdef AF_INET6
1538 if (cg->http->hostaddr->addr.sa_family == AF_INET6)
1539 port = ntohs(cg->http->hostaddr->ipv6.sin6_port);
1540 else
1541 #endif /* AF_INET6 */
1542 if (cg->http->hostaddr->addr.sa_family == AF_INET)
1543 port = ntohs(cg->http->hostaddr->ipv4.sin_port);
1544 else
1545 port = cg->ipp_port;
1546
1547 /*
1548 * Compare the connection hostname, port, and encryption settings to
1549 * the cached defaults; these were initialized the first time we
1550 * connected...
1551 */
1552
1553 if (strcmp(cg->http->hostname, cg->server) || cg->ipp_port != port ||
1554 (cg->http->encryption != cg->encryption &&
1555 cg->http->encryption == HTTP_ENCRYPT_NEVER))
1556 {
1557 /*
1558 * Need to close the current connection because something has changed...
1559 */
1560
1561 httpClose(cg->http);
1562 cg->http = NULL;
1563 }
1564 }
1565
1566 /*
1567 * (Re)connect as needed...
1568 */
1569
1570 if (!cg->http)
1571 {
1572 if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(),
1573 cupsEncryption())) == NULL)
1574 _cupsSetError(IPP_SERVICE_UNAVAILABLE,
1575 errno ? strerror(errno) : "Unable to connect to host.");
1576 }
1577
1578 /*
1579 * Return the cached connection...
1580 */
1581
1582 return (cg->http);
1583 }
1584
1585
1586 /*
1587 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the first printer in a class.
1588 */
1589
1590 static int /* O - 1 on success, 0 on failure */
1591 cups_get_printer_uri(
1592 http_t *http, /* I - HTTP connection */
1593 const char *name, /* I - Name of printer or class */
1594 char *host, /* I - Hostname buffer */
1595 int hostsize, /* I - Size of hostname buffer */
1596 int *port, /* O - Port number */
1597 char *resource, /* I - Resource buffer */
1598 int resourcesize, /* I - Size of resource buffer */
1599 int depth) /* I - Depth of query */
1600 {
1601 int i; /* Looping var */
1602 int http_port; /* Port number */
1603 http_t *http2; /* Alternate HTTP connection */
1604 ipp_t *request, /* IPP request */
1605 *response; /* IPP response */
1606 ipp_attribute_t *attr; /* Current attribute */
1607 char uri[HTTP_MAX_URI], /* printer-uri attribute */
1608 scheme[HTTP_MAX_URI], /* Scheme name */
1609 username[HTTP_MAX_URI], /* Username:password */
1610 classname[255], /* Temporary class name */
1611 http_hostname[HTTP_MAX_HOST];
1612 /* Hostname associated with connection */
1613 static const char * const requested_attrs[] =
1614 { /* Requested attributes */
1615 "printer-uri-supported",
1616 "printer-type",
1617 "member-uris"
1618 };
1619
1620
1621 DEBUG_printf(("cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1622 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)\n",
1623 http, name ? name : "(null)", host, hostsize,
1624 resource, resourcesize, depth));
1625
1626 /*
1627 * Setup the printer URI...
1628 */
1629
1630 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1631 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
1632 {
1633 _cupsSetError(IPP_INTERNAL_ERROR, "Unable to create printer-uri!");
1634
1635 *host = '\0';
1636 *resource = '\0';
1637
1638 return (0);
1639 }
1640
1641 DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", uri));
1642
1643 /*
1644 * Get the hostname and port number we are connected to...
1645 */
1646
1647 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1648
1649 #ifdef AF_INET6
1650 if (http->hostaddr->addr.sa_family == AF_INET6)
1651 http_port = ntohs(http->hostaddr->ipv6.sin6_port);
1652 else
1653 #endif /* AF_INET6 */
1654 if (http->hostaddr->addr.sa_family == AF_INET)
1655 http_port = ntohs(http->hostaddr->ipv4.sin_port);
1656 else
1657 http_port = ippPort();
1658
1659 /*
1660 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1661 * attributes:
1662 *
1663 * attributes-charset
1664 * attributes-natural-language
1665 * printer-uri
1666 * requested-attributes
1667 */
1668
1669 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1670
1671 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1672 NULL, uri);
1673
1674 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1675 "requested-attributes",
1676 sizeof(requested_attrs) / sizeof(requested_attrs[0]),
1677 NULL, requested_attrs);
1678
1679 /*
1680 * Do the request and get back a response...
1681 */
1682
1683 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1684 {
1685 if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
1686 {
1687 /*
1688 * Get the first actual printer name in the class...
1689 */
1690
1691 for (i = 0; i < attr->num_values; i ++)
1692 {
1693 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1694 scheme, sizeof(scheme), username, sizeof(username),
1695 host, hostsize, port, resource, resourcesize);
1696 if (!strncmp(resource, "/printers/", 10))
1697 {
1698 /*
1699 * Found a printer!
1700 */
1701
1702 ippDelete(response);
1703
1704 return (1);
1705 }
1706 }
1707
1708 /*
1709 * No printers in this class - try recursively looking for a printer,
1710 * but not more than 3 levels deep...
1711 */
1712
1713 if (depth < 3)
1714 {
1715 for (i = 0; i < attr->num_values; i ++)
1716 {
1717 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1718 scheme, sizeof(scheme), username, sizeof(username),
1719 host, hostsize, port, resource, resourcesize);
1720 if (!strncmp(resource, "/classes/", 9))
1721 {
1722 /*
1723 * Found a class! Connect to the right server...
1724 */
1725
1726 if (!strcasecmp(http_hostname, host) && *port == http_port)
1727 http2 = http;
1728 else if ((http2 = httpConnectEncrypt(host, *port,
1729 cupsEncryption())) == NULL)
1730 {
1731 DEBUG_puts("Unable to connect to server!");
1732
1733 continue;
1734 }
1735
1736 /*
1737 * Look up printers on that server...
1738 */
1739
1740 strlcpy(classname, resource + 9, sizeof(classname));
1741
1742 cups_get_printer_uri(http2, classname, host, hostsize, port,
1743 resource, resourcesize, depth + 1);
1744
1745 /*
1746 * Close the connection as needed...
1747 */
1748
1749 if (http2 != http)
1750 httpClose(http2);
1751
1752 if (*host)
1753 return (1);
1754 }
1755 }
1756 }
1757 }
1758 else if ((attr = ippFindAttribute(response, "printer-uri-supported",
1759 IPP_TAG_URI)) != NULL)
1760 {
1761 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text,
1762 scheme, sizeof(scheme), username, sizeof(username),
1763 host, hostsize, port, resource, resourcesize);
1764 ippDelete(response);
1765
1766 return (1);
1767 }
1768
1769 ippDelete(response);
1770 }
1771
1772 if (cupsLastError() != IPP_NOT_FOUND)
1773 _cupsSetError(IPP_INTERNAL_ERROR, "No printer-uri found!");
1774
1775 *host = '\0';
1776 *resource = '\0';
1777
1778 return (0);
1779 }
1780
1781
1782 /*
1783 * End of "$Id: util.c 7014 2007-10-10 21:57:43Z mike $".
1784 */