]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2e4ff8af | 2 | * "$Id: util.c 7014 2007-10-10 21:57:43Z mike $" |
ef416fc2 | 3 | * |
4 | * Printing utilities for the Common UNIX Printing System (CUPS). | |
5 | * | |
3d052e43 | 6 | * Copyright 2007-2008 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
fa73b229 | 19 | * cupsCancelJob() - Cancel a print job on the default server. |
3d052e43 MS |
20 | * cupsCancelJob2() - Cancel or purge a print job. |
21 | * cupsCreateJob() - Create an empty job. | |
22 | * cupsFinishDocument() - Finish sending a document. | |
fa73b229 | 23 | * cupsFreeJobs() - Free memory used by job data. |
24 | * cupsGetClasses() - Get a list of printer classes from the default | |
25 | * server. | |
3d052e43 MS |
26 | * cupsGetDefault() - Get the default printer or class for the default |
27 | * server. | |
28 | * cupsGetDefault2() - Get the default printer or class for the specified | |
fa73b229 | 29 | * server. |
fa73b229 | 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. | |
3d052e43 | 34 | * cupsGetPPD2() - Get the PPD file for a printer from the specified |
fa73b229 | 35 | * server. |
2e4ff8af MS |
36 | * cupsGetPPD3() - Get the PPD file for a printer on the specified |
37 | * server if it has changed. | |
fa73b229 | 38 | * cupsGetPrinters() - Get a list of printers from the default server. |
b94498cf | 39 | * cupsGetServerPPD() - Get an available PPD file from the server. |
fa73b229 | 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. | |
3d052e43 MS |
50 | * cupsStartDocument() - Add a document to a job created with |
51 | * cupsCreateJob(). | |
52 | * _cupsConnect() - Get the default server connection... | |
fa73b229 | 53 | * cups_get_printer_uri() - Get the printer-uri-supported attribute for the |
54 | * first printer in a class. | |
ef416fc2 | 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 | ||
fa73b229 | 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); | |
ef416fc2 | 82 | |
83 | ||
84 | /* | |
85 | * 'cupsCancelJob()' - Cancel a print job on the default server. | |
86 | * | |
5a738aea MS |
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 | |
ef416fc2 | 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 */ | |
3d052e43 | 96 | int job_id) /* I - Job ID */ |
ef416fc2 | 97 | { |
5a738aea | 98 | return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) |
3d052e43 MS |
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 | * | |
5a738aea MS |
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 | |
3d052e43 MS |
113 | * the cause of any failure. |
114 | * | |
115 | * @since CUPS 1.4@ | |
116 | */ | |
117 | ||
118 | ipp_status_t /* O - IPP status */ | |
5a738aea MS |
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 */ | |
3d052e43 | 123 | { |
5a738aea | 124 | char uri[HTTP_MAX_URI]; /* Job/printer URI */ |
3d052e43 | 125 | ipp_t *request; /* IPP request */ |
ef416fc2 | 126 | |
127 | ||
128 | /* | |
3d052e43 | 129 | * Range check input... |
ef416fc2 | 130 | */ |
131 | ||
5a738aea | 132 | if (job_id < -1 || (!name && job_id == 0)) |
ef416fc2 | 133 | { |
3d052e43 | 134 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL)); |
ef416fc2 | 135 | return (0); |
136 | } | |
137 | ||
138 | /* | |
3d052e43 | 139 | * Connect to the default server as needed... |
ef416fc2 | 140 | */ |
141 | ||
3d052e43 MS |
142 | if (!http) |
143 | if ((http = _cupsConnect()) == NULL) | |
144 | return (IPP_SERVICE_UNAVAILABLE); | |
ef416fc2 | 145 | |
146 | /* | |
5a738aea | 147 | * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following |
ef416fc2 | 148 | * attributes: |
149 | * | |
150 | * attributes-charset | |
151 | * attributes-natural-language | |
5a738aea | 152 | * job-uri or printer-uri + job-id |
3d052e43 | 153 | * requesting-user-name |
5a738aea | 154 | * [purge-job] or [purge-jobs] |
ef416fc2 | 155 | */ |
156 | ||
5a738aea | 157 | request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB); |
ef416fc2 | 158 | |
5a738aea MS |
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 | } | |
ef416fc2 | 175 | |
176 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
177 | NULL, cupsUser()); | |
5a738aea MS |
178 | |
179 | if (purge && job_id >= 0) | |
3d052e43 | 180 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1); |
5a738aea MS |
181 | else if (!purge && job_id < 0) |
182 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0); | |
ef416fc2 | 183 | |
184 | /* | |
185 | * Do the request... | |
186 | */ | |
187 | ||
3d052e43 MS |
188 | ippDelete(cupsDoRequest(http, request, "/jobs/")); |
189 | ||
190 | return (cupsLastError()); | |
191 | } | |
192 | ||
193 | ||
194 | /* | |
195 | * 'cupsCreateJob()' - Create an empty job. | |
196 | * | |
5a738aea MS |
197 | * Submit files for printing to the job using the @link cupsStartDocument@, |
198 | * @link cupsWriteRequestData@, and @link cupsFinishDocument@ functions. | |
3d052e43 MS |
199 | * |
200 | * @since CUPS 1.4@ | |
201 | */ | |
202 | ||
203 | int /* O - Job ID or 0 on error */ | |
204 | cupsCreateJob( | |
5a738aea | 205 | http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
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 | * | |
5a738aea MS |
278 | * The document must have been started using @link cupsStartDocument@. |
279 | * | |
3d052e43 MS |
280 | * @since CUPS 1.4@ |
281 | */ | |
282 | ||
283 | ipp_status_t /* O - Status of document submission */ | |
5a738aea | 284 | cupsFinishDocument(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
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); | |
ef416fc2 | 291 | |
3d052e43 MS |
292 | ippDelete(cupsGetResponse(http, resource)); |
293 | ||
294 | return (cupsLastError()); | |
ef416fc2 | 295 | } |
296 | ||
297 | ||
ef416fc2 | 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 | { | |
3d052e43 MS |
306 | int i; /* Looping var */ |
307 | cups_job_t *job; /* Current job */ | |
ef416fc2 | 308 | |
309 | ||
3d052e43 | 310 | if (num_jobs <= 0 || !jobs) |
ef416fc2 | 311 | return; |
312 | ||
3d052e43 | 313 | for (i = num_jobs, job = jobs; i > 0; i --, job ++) |
ef416fc2 | 314 | { |
3d052e43 MS |
315 | _cupsStrFree(job->dest); |
316 | _cupsStrFree(job->user); | |
317 | _cupsStrFree(job->format); | |
318 | _cupsStrFree(job->title); | |
ef416fc2 | 319 | } |
320 | ||
321 | free(jobs); | |
322 | } | |
323 | ||
324 | ||
325 | /* | |
326 | * 'cupsGetClasses()' - Get a list of printer classes from the default server. | |
327 | * | |
5a738aea | 328 | * This function is deprecated - use @link cupsGetDests@ instead. |
ef416fc2 | 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 */ | |
ef416fc2 | 340 | char **temp; /* Temporary pointer */ |
3d052e43 | 341 | http_t *http; /* Connection to server */ |
ef416fc2 | 342 | |
343 | ||
3d052e43 | 344 | if (!classes) |
ef416fc2 | 345 | { |
ecdc0628 | 346 | _cupsSetError(IPP_INTERNAL_ERROR, NULL); |
ef416fc2 | 347 | |
348 | return (0); | |
349 | } | |
350 | ||
3d052e43 | 351 | *classes = NULL; |
ef416fc2 | 352 | |
3d052e43 | 353 | if ((http = _cupsConnect()) == NULL) |
ef416fc2 | 354 | return (0); |
ef416fc2 | 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 | ||
2e4ff8af | 365 | request = ippNewRequest(CUPS_GET_CLASSES); |
ef416fc2 | 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 | ||
3d052e43 | 374 | n = 0; |
ef416fc2 | 375 | |
3d052e43 | 376 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
ef416fc2 | 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. | |
5a738aea MS |
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. | |
ef416fc2 | 426 | */ |
427 | ||
5a738aea | 428 | const char * /* O - Default printer or @code NULL@ */ |
ef416fc2 | 429 | cupsGetDefault(void) |
430 | { | |
ef416fc2 | 431 | /* |
432 | * Return the default printer... | |
433 | */ | |
434 | ||
3d052e43 | 435 | return (cupsGetDefault2(CUPS_HTTP_DEFAULT)); |
ef416fc2 | 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. | |
5a738aea MS |
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. | |
ef416fc2 | 448 | * |
449 | * @since CUPS 1.1.21@ | |
450 | */ | |
451 | ||
5a738aea MS |
452 | const char * /* O - Default printer or @code NULL@ */ |
453 | cupsGetDefault2(http_t *http) /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ | |
ef416fc2 | 454 | { |
455 | ipp_t *request, /* IPP Request */ | |
456 | *response; /* IPP Response */ | |
457 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 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 | /* | |
3d052e43 | 475 | * Connect to the server as needed... |
ef416fc2 | 476 | */ |
477 | ||
478 | if (!http) | |
3d052e43 MS |
479 | if ((http = _cupsConnect()) == NULL) |
480 | return (NULL); | |
ef416fc2 | 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 | ||
2e4ff8af | 490 | request = ippNewRequest(CUPS_GET_DEFAULT); |
ef416fc2 | 491 | |
492 | /* | |
493 | * Do the request and get back a response... | |
494 | */ | |
495 | ||
496 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
497 | { | |
3d052e43 MS |
498 | if ((attr = ippFindAttribute(response, "printer-name", |
499 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 500 | { |
3d052e43 MS |
501 | strlcpy(cg->def_printer, attr->values[0].string.text, |
502 | sizeof(cg->def_printer)); | |
ef416fc2 | 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. | |
5a738aea MS |
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. | |
ef416fc2 | 521 | */ |
522 | ||
523 | int /* O - Number of jobs */ | |
524 | cupsGetJobs(cups_job_t **jobs, /* O - Job data */ | |
5a738aea | 525 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for mydest */ |
ecdc0628 | 526 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 527 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 528 | { |
ef416fc2 | 529 | /* |
530 | * Return the jobs... | |
531 | */ | |
532 | ||
5a738aea | 533 | return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs)); |
ef416fc2 | 534 | } |
535 | ||
536 | ||
537 | ||
538 | /* | |
539 | * 'cupsGetJobs2()' - Get the jobs from the specified server. | |
540 | * | |
5a738aea MS |
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 | * | |
ef416fc2 | 546 | * @since CUPS 1.1.21@ |
547 | */ | |
548 | ||
549 | int /* O - Number of jobs */ | |
5a738aea | 550 | cupsGetJobs2(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 551 | cups_job_t **jobs, /* O - Job data */ |
5a738aea | 552 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for mydest */ |
ecdc0628 | 553 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 554 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 555 | { |
556 | int n; /* Number of jobs */ | |
557 | ipp_t *request, /* IPP Request */ | |
558 | *response; /* IPP Response */ | |
559 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 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 | ||
3d052e43 | 594 | if (!jobs) |
ef416fc2 | 595 | { |
ecdc0628 | 596 | _cupsSetError(IPP_INTERNAL_ERROR, NULL); |
ef416fc2 | 597 | |
598 | return (-1); | |
599 | } | |
600 | ||
601 | /* | |
602 | * Get the right URI... | |
603 | */ | |
604 | ||
5a738aea | 605 | if (name) |
ef416fc2 | 606 | { |
a4d04587 | 607 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
5a738aea | 608 | "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) |
ef416fc2 | 609 | { |
ecdc0628 | 610 | _cupsSetError(IPP_INTERNAL_ERROR, NULL); |
ef416fc2 | 611 | |
612 | return (-1); | |
613 | } | |
614 | } | |
615 | else | |
616 | strcpy(uri, "ipp://localhost/jobs"); | |
617 | ||
3d052e43 MS |
618 | if (!http) |
619 | if ((http = _cupsConnect()) == NULL) | |
620 | return (-1); | |
ef416fc2 | 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 | ||
2e4ff8af | 635 | request = ippNewRequest(IPP_GET_JOBS); |
ef416fc2 | 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 | ||
5a738aea | 646 | if (whichjobs == CUPS_WHICHJOBS_COMPLETED) |
ef416fc2 | 647 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
648 | "which-jobs", NULL, "completed"); | |
5a738aea | 649 | else if (whichjobs == CUPS_WHICHJOBS_ALL) |
ecdc0628 | 650 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
651 | "which-jobs", NULL, "all"); | |
ef416fc2 | 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 | { | |
3d052e43 | 666 | for (attr = response->attrs; attr; attr = attr->next) |
ef416fc2 | 667 | { |
668 | /* | |
669 | * Skip leading attributes until we hit a job... | |
670 | */ | |
671 | ||
3d052e43 | 672 | while (attr && attr->group_tag != IPP_TAG_JOB) |
ef416fc2 | 673 | attr = attr->next; |
674 | ||
3d052e43 | 675 | if (!attr) |
ef416fc2 | 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 | ||
3d052e43 | 694 | while (attr && attr->group_tag == IPP_TAG_JOB) |
ef416fc2 | 695 | { |
3d052e43 | 696 | if (!strcmp(attr->name, "job-id") && |
ef416fc2 | 697 | attr->value_tag == IPP_TAG_INTEGER) |
698 | id = attr->values[0].integer; | |
3d052e43 | 699 | else if (!strcmp(attr->name, "job-state") && |
ef416fc2 | 700 | attr->value_tag == IPP_TAG_ENUM) |
701 | state = (ipp_jstate_t)attr->values[0].integer; | |
3d052e43 | 702 | else if (!strcmp(attr->name, "job-priority") && |
ef416fc2 | 703 | attr->value_tag == IPP_TAG_INTEGER) |
704 | priority = attr->values[0].integer; | |
3d052e43 | 705 | else if (!strcmp(attr->name, "job-k-octets") && |
ef416fc2 | 706 | attr->value_tag == IPP_TAG_INTEGER) |
707 | size = attr->values[0].integer; | |
3d052e43 | 708 | else if (!strcmp(attr->name, "time-at-completed") && |
ef416fc2 | 709 | attr->value_tag == IPP_TAG_INTEGER) |
710 | completed_time = attr->values[0].integer; | |
3d052e43 | 711 | else if (!strcmp(attr->name, "time-at-creation") && |
ef416fc2 | 712 | attr->value_tag == IPP_TAG_INTEGER) |
713 | creation_time = attr->values[0].integer; | |
3d052e43 | 714 | else if (!strcmp(attr->name, "time-at-processing") && |
ef416fc2 | 715 | attr->value_tag == IPP_TAG_INTEGER) |
716 | processing_time = attr->values[0].integer; | |
3d052e43 | 717 | else if (!strcmp(attr->name, "job-printer-uri") && |
ef416fc2 | 718 | attr->value_tag == IPP_TAG_URI) |
719 | { | |
720 | if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL) | |
721 | dest ++; | |
722 | } | |
3d052e43 | 723 | else if (!strcmp(attr->name, "job-originating-user-name") && |
ef416fc2 | 724 | attr->value_tag == IPP_TAG_NAME) |
725 | user = attr->values[0].string.text; | |
3d052e43 | 726 | else if (!strcmp(attr->name, "document-format") && |
ef416fc2 | 727 | attr->value_tag == IPP_TAG_MIMETYPE) |
728 | format = attr->values[0].string.text; | |
3d052e43 | 729 | else if (!strcmp(attr->name, "job-name") && |
ef416fc2 | 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 | ||
3d052e43 | 741 | if (!dest || !id) |
ef416fc2 | 742 | { |
3d052e43 | 743 | if (!attr) |
ef416fc2 | 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 | ||
3d052e43 | 758 | if (!temp) |
ef416fc2 | 759 | { |
760 | /* | |
761 | * Ran out of memory! | |
762 | */ | |
763 | ||
3d052e43 MS |
764 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno)); |
765 | ||
ef416fc2 | 766 | cupsFreeJobs(n, *jobs); |
767 | *jobs = NULL; | |
768 | ||
769 | ippDelete(response); | |
3d052e43 MS |
770 | |
771 | return (-1); | |
ef416fc2 | 772 | } |
773 | ||
774 | *jobs = temp; | |
775 | temp += n; | |
776 | n ++; | |
777 | ||
778 | /* | |
779 | * Copy the data over... | |
780 | */ | |
781 | ||
3d052e43 MS |
782 | temp->dest = _cupsStrAlloc(dest); |
783 | temp->user = _cupsStrAlloc(user); | |
784 | temp->format = _cupsStrAlloc(format); | |
785 | temp->title = _cupsStrAlloc(title); | |
ef416fc2 | 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 | ||
3d052e43 | 794 | if (!attr) |
ef416fc2 | 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 | * | |
5a738aea | 811 | * For classes, @code cupsGetPPD@ returns the PPD file for the first printer |
ef416fc2 | 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 */ | |
2e4ff8af MS |
819 | time_t modtime = 0; /* Modification time */ |
820 | ||
ef416fc2 | 821 | |
ef416fc2 | 822 | /* |
823 | * Return the PPD file... | |
824 | */ | |
825 | ||
2e4ff8af MS |
826 | cg->ppd_filename[0] = '\0'; |
827 | ||
3d052e43 | 828 | if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename, |
2e4ff8af MS |
829 | sizeof(cg->ppd_filename)) == HTTP_OK) |
830 | return (cg->ppd_filename); | |
831 | else | |
832 | return (NULL); | |
ef416fc2 | 833 | } |
834 | ||
835 | ||
836 | /* | |
837 | * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server. | |
838 | * | |
5a738aea | 839 | * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer |
ef416fc2 | 840 | * in the class. |
841 | * | |
842 | * @since CUPS 1.1.21@ | |
843 | */ | |
844 | ||
845 | const char * /* O - Filename for PPD file */ | |
5a738aea | 846 | cupsGetPPD2(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 847 | const char *name) /* I - Printer name */ |
2e4ff8af MS |
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 | * | |
5a738aea MS |
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. | |
2e4ff8af MS |
881 | */ |
882 | ||
883 | http_status_t /* O - HTTP status */ | |
5a738aea | 884 | cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
2e4ff8af MS |
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 */ | |
ef416fc2 | 889 | { |
ef416fc2 | 890 | int http_port; /* Port number */ |
ed486911 | 891 | char http_hostname[HTTP_MAX_HOST]; |
892 | /* Hostname associated with connection */ | |
ef416fc2 | 893 | http_t *http2; /* Alternate HTTP connection */ |
ef416fc2 | 894 | int fd; /* PPD file */ |
fa73b229 | 895 | char localhost[HTTP_MAX_URI],/* Local hostname */ |
ef416fc2 | 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 */ | |
ef416fc2 | 901 | |
902 | ||
903 | /* | |
904 | * Range check input... | |
905 | */ | |
906 | ||
2e4ff8af MS |
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)); | |
ef416fc2 | 910 | |
2e4ff8af MS |
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); | |
ef416fc2 | 927 | } |
928 | ||
929 | /* | |
fa73b229 | 930 | * Try finding a printer URI for this printer... |
ef416fc2 | 931 | */ |
932 | ||
3d052e43 MS |
933 | if (!http) |
934 | http = _cupsConnect(); | |
935 | ||
fa73b229 | 936 | if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, |
937 | resource, sizeof(resource), 0)) | |
2e4ff8af | 938 | return (HTTP_NOT_FOUND); |
ef416fc2 | 939 | |
8ca02f3c | 940 | DEBUG_printf(("Printer hostname=\"%s\", port=%d\n", hostname, port)); |
941 | ||
ef416fc2 | 942 | /* |
fa73b229 | 943 | * Remap local hostname to localhost... |
944 | */ | |
945 | ||
757d2cad | 946 | httpGetHostname(NULL, localhost, sizeof(localhost)); |
fa73b229 | 947 | |
8ca02f3c | 948 | DEBUG_printf(("Local hostname=\"%s\"\n", localhost)); |
949 | ||
fa73b229 | 950 | if (!strcasecmp(localhost, hostname)) |
951 | strcpy(hostname, "localhost"); | |
952 | ||
953 | /* | |
ed486911 | 954 | * Get the hostname and port number we are connected to... |
ef416fc2 | 955 | */ |
956 | ||
ed486911 | 957 | httpGetHostname(http, http_hostname, sizeof(http_hostname)); |
958 | ||
ef416fc2 | 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 | ||
8ca02f3c | 969 | DEBUG_printf(("Connection hostname=\"%s\", port=%d\n", http_hostname, |
970 | http_port)); | |
971 | ||
ef416fc2 | 972 | /* |
973 | * Reconnect to the correct server as needed... | |
974 | */ | |
975 | ||
ed486911 | 976 | if (!strcasecmp(http_hostname, hostname) && port == http_port) |
ef416fc2 | 977 | http2 = http; |
978 | else if ((http2 = httpConnectEncrypt(hostname, port, | |
979 | cupsEncryption())) == NULL) | |
980 | { | |
981 | DEBUG_puts("Unable to connect to server!"); | |
982 | ||
2e4ff8af | 983 | return (HTTP_SERVICE_UNAVAILABLE); |
ef416fc2 | 984 | } |
985 | ||
986 | /* | |
987 | * Get a temp file... | |
988 | */ | |
989 | ||
2e4ff8af MS |
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) | |
ef416fc2 | 996 | { |
997 | /* | |
998 | * Can't open file; close the server connection and return NULL... | |
999 | */ | |
1000 | ||
ecdc0628 | 1001 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno)); |
ef416fc2 | 1002 | |
1003 | if (http2 != http) | |
1004 | httpClose(http2); | |
1005 | ||
2e4ff8af | 1006 | return (HTTP_SERVER_ERROR); |
ef416fc2 | 1007 | } |
1008 | ||
1009 | /* | |
1010 | * And send a request to the HTTP server... | |
1011 | */ | |
1012 | ||
fa73b229 | 1013 | strlcat(resource, ".ppd", sizeof(resource)); |
ef416fc2 | 1014 | |
2e4ff8af MS |
1015 | if (*modtime > 0) |
1016 | httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE, | |
1017 | httpGetDateString(*modtime)); | |
1018 | ||
ef416fc2 | 1019 | status = cupsGetFd(http2, resource, fd); |
1020 | ||
1021 | close(fd); | |
1022 | ||
ef416fc2 | 1023 | /* |
1024 | * See if we actually got the file or an error... | |
1025 | */ | |
1026 | ||
2e4ff8af MS |
1027 | if (status == HTTP_OK) |
1028 | *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE)); | |
1029 | else if (status != HTTP_NOT_MODIFIED) | |
ef416fc2 | 1030 | { |
1031 | switch (status) | |
1032 | { | |
1033 | case HTTP_NOT_FOUND : | |
ecdc0628 | 1034 | _cupsSetError(IPP_NOT_FOUND, httpStatus(status)); |
ef416fc2 | 1035 | break; |
1036 | ||
1037 | case HTTP_UNAUTHORIZED : | |
ecdc0628 | 1038 | _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status)); |
ef416fc2 | 1039 | break; |
1040 | ||
1041 | default : | |
1042 | DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n", | |
1043 | status)); | |
ecdc0628 | 1044 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status)); |
ef416fc2 | 1045 | break; |
1046 | } | |
1047 | ||
1048 | unlink(cg->ppd_filename); | |
ef416fc2 | 1049 | } |
1050 | ||
91c84a35 MS |
1051 | if (http2 != http) |
1052 | httpClose(http2); | |
1053 | ||
ef416fc2 | 1054 | /* |
1055 | * Return the PPD file... | |
1056 | */ | |
1057 | ||
2e4ff8af | 1058 | return (status); |
ef416fc2 | 1059 | } |
1060 | ||
1061 | ||
1062 | /* | |
1063 | * 'cupsGetPrinters()' - Get a list of printers from the default server. | |
1064 | * | |
5a738aea | 1065 | * This function is deprecated - use @link cupsGetDests@ instead. |
ef416fc2 | 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 */ | |
ef416fc2 | 1077 | char **temp; /* Temporary pointer */ |
3d052e43 MS |
1078 | http_t *http; /* Connection to server */ |
1079 | ||
ef416fc2 | 1080 | |
3d052e43 MS |
1081 | /* |
1082 | * Range check input... | |
1083 | */ | |
ef416fc2 | 1084 | |
3d052e43 | 1085 | if (!printers) |
ef416fc2 | 1086 | { |
ecdc0628 | 1087 | _cupsSetError(IPP_INTERNAL_ERROR, NULL); |
ef416fc2 | 1088 | |
1089 | return (0); | |
1090 | } | |
1091 | ||
3d052e43 MS |
1092 | *printers = NULL; |
1093 | ||
ef416fc2 | 1094 | /* |
1095 | * Try to connect to the server... | |
1096 | */ | |
1097 | ||
3d052e43 | 1098 | if ((http = _cupsConnect()) == NULL) |
ef416fc2 | 1099 | return (0); |
ef416fc2 | 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 | ||
2e4ff8af | 1110 | request = ippNewRequest(CUPS_GET_PRINTERS); |
ef416fc2 | 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 | ||
3d052e43 | 1125 | n = 0; |
ef416fc2 | 1126 | |
3d052e43 | 1127 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
ef416fc2 | 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 | ||
b94498cf | 1168 | /* |
1169 | * 'cupsGetServerPPD()' - Get an available PPD file from the server. | |
1170 | * | |
1171 | * This function returns the named PPD file from the server. The | |
5a738aea | 1172 | * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@ |
b94498cf | 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 | |
5a738aea MS |
1177 | * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@, |
1178 | * or @link cupsGetServerPPD@. | |
b94498cf | 1179 | * |
1180 | * @since CUPS 1.3@ | |
1181 | */ | |
1182 | ||
5a738aea MS |
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@ */ | |
b94498cf | 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 | ||
3d052e43 | 1197 | if (!name) |
b94498cf | 1198 | { |
3d052e43 | 1199 | _cupsSetError(IPP_INTERNAL_ERROR, "No PPD name!"); |
b94498cf | 1200 | |
1201 | return (NULL); | |
1202 | } | |
1203 | ||
3d052e43 MS |
1204 | if (!http) |
1205 | if ((http = _cupsConnect()) == NULL) | |
1206 | return (NULL); | |
1207 | ||
b94498cf | 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 | ||
ef416fc2 | 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 | ||
5a738aea | 1273 | int /* O - Job ID or 0 on error */ |
ef416fc2 | 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 | ||
3d052e43 MS |
1284 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, |
1285 | num_options, options)); | |
ef416fc2 | 1286 | } |
1287 | ||
1288 | ||
1289 | /* | |
5a738aea MS |
1290 | * 'cupsPrintFile2()' - Print a file to a printer or class on the specified |
1291 | * server. | |
ef416fc2 | 1292 | * |
1293 | * @since CUPS 1.1.21@ | |
1294 | */ | |
1295 | ||
5a738aea | 1296 | int /* O - Job ID or 0 on error */ |
3d052e43 MS |
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 */ | |
ef416fc2 | 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 | ||
3d052e43 MS |
1309 | return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, |
1310 | options)); | |
ef416fc2 | 1311 | } |
1312 | ||
1313 | ||
1314 | /* | |
fa73b229 | 1315 | * 'cupsPrintFiles()' - Print one or more files to a printer or class on the |
1316 | * default server. | |
ef416fc2 | 1317 | */ |
1318 | ||
5a738aea | 1319 | int /* O - Job ID or 0 on error */ |
3d052e43 MS |
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 */ | |
ef416fc2 | 1327 | { |
ef416fc2 | 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 | ||
ef416fc2 | 1333 | /* |
1334 | * Print the file(s)... | |
1335 | */ | |
1336 | ||
3d052e43 | 1337 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title, |
ef416fc2 | 1338 | num_options, options)); |
1339 | } | |
1340 | ||
1341 | ||
ef416fc2 | 1342 | /* |
fa73b229 | 1343 | * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the |
1344 | * specified server. | |
ef416fc2 | 1345 | * |
1346 | * @since CUPS 1.1.21@ | |
1347 | */ | |
1348 | ||
5a738aea | 1349 | int /* O - Job ID or 0 on error */ |
3d052e43 | 1350 | cupsPrintFiles2( |
5a738aea | 1351 | http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
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 */ | |
ef416fc2 | 1358 | { |
1359 | int i; /* Looping var */ | |
3d052e43 MS |
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 */ | |
ef416fc2 | 1367 | |
1368 | ||
3d052e43 | 1369 | DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, " |
ef416fc2 | 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 | ||
3d052e43 | 1377 | if (!name || num_files < 1 || !files) |
ef416fc2 | 1378 | { |
ecdc0628 | 1379 | _cupsSetError(IPP_INTERNAL_ERROR, NULL); |
ef416fc2 | 1380 | |
1381 | return (0); | |
1382 | } | |
1383 | ||
1384 | /* | |
3d052e43 | 1385 | * Create the print job... |
ef416fc2 | 1386 | */ |
1387 | ||
3d052e43 | 1388 | if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0) |
ef416fc2 | 1389 | return (0); |
ef416fc2 | 1390 | |
1391 | /* | |
3d052e43 | 1392 | * Send each of the files... |
ef416fc2 | 1393 | */ |
1394 | ||
3d052e43 MS |
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; | |
ef416fc2 | 1400 | |
3d052e43 MS |
1401 | for (i = 0; i < num_files; i ++) |
1402 | { | |
1403 | /* | |
1404 | * Start the next file... | |
1405 | */ | |
ef416fc2 | 1406 | |
3d052e43 MS |
1407 | if ((docname = strrchr(files[i], '/')) != NULL) |
1408 | docname ++; | |
1409 | else | |
1410 | docname = files[i]; | |
ef416fc2 | 1411 | |
3d052e43 MS |
1412 | if ((fp = cupsFileOpen(files[i], "rb")) == NULL) |
1413 | { | |
1414 | /* | |
1415 | * Unable to open print file, cancel the job and return... | |
1416 | */ | |
ef416fc2 | 1417 | |
5a738aea | 1418 | cupsCancelJob2(http, name, job_id, 0); |
3d052e43 MS |
1419 | return (0); |
1420 | } | |
ef416fc2 | 1421 | |
3d052e43 MS |
1422 | status = cupsStartDocument(http, name, job_id, docname, format, |
1423 | i == (num_files - 1)); | |
ef416fc2 | 1424 | |
3d052e43 MS |
1425 | while (status == HTTP_CONTINUE && |
1426 | (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) | |
1427 | status = cupsWriteRequestData(http, buffer, bytes); | |
ef416fc2 | 1428 | |
3d052e43 | 1429 | cupsFileClose(fp); |
ef416fc2 | 1430 | |
3d052e43 | 1431 | if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK) |
ef416fc2 | 1432 | { |
1433 | /* | |
3d052e43 | 1434 | * Unable to queue, cancel the job and return... |
ef416fc2 | 1435 | */ |
1436 | ||
5a738aea | 1437 | cupsCancelJob2(http, name, job_id, 0); |
3d052e43 MS |
1438 | return (0); |
1439 | } | |
1440 | } | |
ef416fc2 | 1441 | |
3d052e43 MS |
1442 | return (job_id); |
1443 | } | |
ef416fc2 | 1444 | |
ef416fc2 | 1445 | |
3d052e43 MS |
1446 | /* |
1447 | * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob(). | |
1448 | * | |
5a738aea MS |
1449 | * Use @link cupsWriteRequestData@ to write data for the document and |
1450 | * @link cupsFinishDocument@ to finish the document and get the submission status. | |
3d052e43 | 1451 | * |
5a738aea MS |
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. | |
3d052e43 MS |
1456 | * |
1457 | * @since CUPS 1.4@ | |
1458 | */ | |
ef416fc2 | 1459 | |
3d052e43 MS |
1460 | http_status_t /* O - HTTP status of request */ |
1461 | cupsStartDocument( | |
5a738aea | 1462 | http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 | 1463 | const char *name, /* I - Printer or class name */ |
5a738aea | 1464 | int job_id, /* I - Job ID from @link cupsCreateJob@ */ |
3d052e43 | 1465 | const char *docname, /* I - Name of document */ |
5a738aea | 1466 | const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */ |
3d052e43 MS |
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 */ | |
ef416fc2 | 1473 | |
bd7854cb | 1474 | |
3d052e43 MS |
1475 | /* |
1476 | * Create a Send-Document request... | |
1477 | */ | |
bd7854cb | 1478 | |
3d052e43 MS |
1479 | if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL) |
1480 | { | |
1481 | _cupsSetError(IPP_INTERNAL_ERROR, NULL); | |
1482 | return (0); | |
1483 | } | |
bd7854cb | 1484 | |
3d052e43 MS |
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); | |
ef416fc2 | 1488 | |
3d052e43 MS |
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); | |
ef416fc2 | 1501 | |
3d052e43 MS |
1502 | /* |
1503 | * Send and delete the request, then return the status... | |
1504 | */ | |
ef416fc2 | 1505 | |
3d052e43 | 1506 | status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE); |
ef416fc2 | 1507 | |
3d052e43 | 1508 | ippDelete(request); |
ef416fc2 | 1509 | |
3d052e43 | 1510 | return (status); |
ef416fc2 | 1511 | } |
1512 | ||
1513 | ||
1514 | /* | |
3d052e43 | 1515 | * '_cupsConnect()' - Get the default server connection... |
ef416fc2 | 1516 | */ |
1517 | ||
3d052e43 MS |
1518 | http_t * /* O - HTTP connection */ |
1519 | _cupsConnect(void) | |
ef416fc2 | 1520 | { |
3d052e43 | 1521 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
ef416fc2 | 1522 | |
ef416fc2 | 1523 | |
1524 | /* | |
3d052e43 | 1525 | * See if we are connected to the same server... |
ef416fc2 | 1526 | */ |
1527 | ||
3d052e43 MS |
1528 | if (cg->http) |
1529 | { | |
1530 | int port; /* Port for connection */ | |
ef416fc2 | 1531 | |
ed486911 | 1532 | |
3d052e43 MS |
1533 | /* |
1534 | * Get the port associated with the current connection... | |
1535 | */ | |
ef416fc2 | 1536 | |
3d052e43 MS |
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; | |
ef416fc2 | 1546 | |
3d052e43 MS |
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 | */ | |
ef416fc2 | 1560 | |
3d052e43 MS |
1561 | httpClose(cg->http); |
1562 | cg->http = NULL; | |
1563 | } | |
ef416fc2 | 1564 | } |
1565 | ||
3d052e43 MS |
1566 | /* |
1567 | * (Re)connect as needed... | |
1568 | */ | |
ef416fc2 | 1569 | |
3d052e43 | 1570 | if (!cg->http) |
ef416fc2 | 1571 | { |
3d052e43 MS |
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 | } | |
ef416fc2 | 1577 | |
3d052e43 MS |
1578 | /* |
1579 | * Return the cached connection... | |
1580 | */ | |
ef416fc2 | 1581 | |
3d052e43 | 1582 | return (cg->http); |
ef416fc2 | 1583 | } |
1584 | ||
1585 | ||
fa73b229 | 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 */ | |
ed486911 | 1610 | classname[255], /* Temporary class name */ |
1611 | http_hostname[HTTP_MAX_HOST]; | |
1612 | /* Hostname associated with connection */ | |
fa73b229 | 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 | ||
a4d04587 | 1630 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
1631 | "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) | |
fa73b229 | 1632 | { |
b423cd4c | 1633 | _cupsSetError(IPP_INTERNAL_ERROR, "Unable to create printer-uri!"); |
fa73b229 | 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 | /* | |
ed486911 | 1644 | * Get the hostname and port number we are connected to... |
fa73b229 | 1645 | */ |
1646 | ||
ed486911 | 1647 | httpGetHostname(http, http_hostname, sizeof(http_hostname)); |
1648 | ||
fa73b229 | 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 | { | |
a4d04587 | 1693 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, |
1694 | scheme, sizeof(scheme), username, sizeof(username), | |
1695 | host, hostsize, port, resource, resourcesize); | |
fa73b229 | 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 | { | |
a4d04587 | 1717 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, |
1718 | scheme, sizeof(scheme), username, sizeof(username), | |
1719 | host, hostsize, port, resource, resourcesize); | |
fa73b229 | 1720 | if (!strncmp(resource, "/classes/", 9)) |
1721 | { | |
1722 | /* | |
1723 | * Found a class! Connect to the right server... | |
1724 | */ | |
1725 | ||
ed486911 | 1726 | if (!strcasecmp(http_hostname, host) && *port == http_port) |
fa73b229 | 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 | { | |
a4d04587 | 1761 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, |
1762 | scheme, sizeof(scheme), username, sizeof(username), | |
1763 | host, hostsize, port, resource, resourcesize); | |
fa73b229 | 1764 | ippDelete(response); |
1765 | ||
1766 | return (1); | |
1767 | } | |
1768 | ||
1769 | ippDelete(response); | |
1770 | } | |
1771 | ||
b86bc4cf | 1772 | if (cupsLastError() != IPP_NOT_FOUND) |
1773 | _cupsSetError(IPP_INTERNAL_ERROR, "No printer-uri found!"); | |
b423cd4c | 1774 | |
fa73b229 | 1775 | *host = '\0'; |
1776 | *resource = '\0'; | |
1777 | ||
1778 | return (0); | |
1779 | } | |
1780 | ||
1781 | ||
ef416fc2 | 1782 | /* |
2e4ff8af | 1783 | * End of "$Id: util.c 7014 2007-10-10 21:57:43Z mike $". |
ef416fc2 | 1784 | */ |