2 * IPP Everywhere printer application for CUPS.
4 * Copyright © 2010-2019 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more
9 * Note: This program began life as the "ippserver" sample code that first
10 * appeared in CUPS 1.4. The name has been changed in order to distinguish it
11 * from the PWG's much more ambitious "ippserver" program, which supports
12 * different kinds of IPP services and multiple services per instance - the
13 * "ippeveprinter" program exposes a single print service conforming to the
14 * current IPP Everywhere specification, thus the new name.
18 * Include necessary headers...
21 #include <cups/cups-private.h>
22 #include <cups/debug-private.h>
24 # include <cups/ppd-private.h>
25 #endif /* !CUPS_LITE */
34 # define WEXITSTATUS(s) (s)
35 # include <winsock2.h>
39 extern char **environ
;
41 # include <sys/fcntl.h>
42 # include <sys/wait.h>
48 #elif defined(HAVE_AVAHI)
49 # include <avahi-client/client.h>
50 # include <avahi-client/publish.h>
51 # include <avahi-common/error.h>
52 # include <avahi-common/thread-watch.h>
53 #endif /* HAVE_DNSSD */
54 #ifdef HAVE_SYS_MOUNT_H
55 # include <sys/mount.h>
56 #endif /* HAVE_SYS_MOUNT_H */
57 #ifdef HAVE_SYS_STATFS_H
58 # include <sys/statfs.h>
59 #endif /* HAVE_SYS_STATFS_H */
60 #ifdef HAVE_SYS_STATVFS_H
61 # include <sys/statvfs.h>
62 #endif /* HAVE_SYS_STATVFS_H */
65 #endif /* HAVE_SYS_VFS_H */
67 #include "printer-png.h"
74 enum ippeve_preason_e
/* printer-state-reasons bit values */
76 IPPEVE_PREASON_NONE
= 0x0000, /* none */
77 IPPEVE_PREASON_OTHER
= 0x0001, /* other */
78 IPPEVE_PREASON_COVER_OPEN
= 0x0002, /* cover-open */
79 IPPEVE_PREASON_INPUT_TRAY_MISSING
= 0x0004,
80 /* input-tray-missing */
81 IPPEVE_PREASON_MARKER_SUPPLY_EMPTY
= 0x0008,
82 /* marker-supply-empty */
83 IPPEVE_PREASON_MARKER_SUPPLY_LOW
= 0x0010,
84 /* marker-supply-low */
85 IPPEVE_PREASON_MARKER_WASTE_ALMOST_FULL
= 0x0020,
86 /* marker-waste-almost-full */
87 IPPEVE_PREASON_MARKER_WASTE_FULL
= 0x0040,
88 /* marker-waste-full */
89 IPPEVE_PREASON_MEDIA_EMPTY
= 0x0080, /* media-empty */
90 IPPEVE_PREASON_MEDIA_JAM
= 0x0100, /* media-jam */
91 IPPEVE_PREASON_MEDIA_LOW
= 0x0200, /* media-low */
92 IPPEVE_PREASON_MEDIA_NEEDED
= 0x0400, /* media-needed */
93 IPPEVE_PREASON_MOVING_TO_PAUSED
= 0x0800,
94 /* moving-to-paused */
95 IPPEVE_PREASON_PAUSED
= 0x1000, /* paused */
96 IPPEVE_PREASON_SPOOL_AREA_FULL
= 0x2000,/* spool-area-full */
97 IPPEVE_PREASON_TONER_EMPTY
= 0x4000, /* toner-empty */
98 IPPEVE_PREASON_TONER_LOW
= 0x8000 /* toner-low */
100 typedef unsigned int ippeve_preason_t
; /* Bitfield for printer-state-reasons */
101 static const char * const ippeve_preason_strings
[] =
102 { /* Strings for each bit */
103 /* "none" is implied for no bits set */
106 "input-tray-missing",
107 "marker-supply-empty",
109 "marker-waste-almost-full",
124 * URL scheme for web resources...
128 # define WEB_SCHEME "https"
130 # define WEB_SCHEME "http"
131 #endif /* HAVE_SSL */
139 typedef DNSServiceRef ippeve_srv_t
; /* Service reference */
140 typedef TXTRecordRef ippeve_txt_t
; /* TXT record */
142 #elif defined(HAVE_AVAHI)
143 typedef AvahiEntryGroup
*ippeve_srv_t
; /* Service reference */
144 typedef AvahiStringList
*ippeve_txt_t
; /* TXT record */
147 typedef void *ippeve_srv_t
; /* Service reference */
148 typedef void *ippeve_txt_t
; /* TXT record */
149 #endif /* HAVE_DNSSD */
151 typedef struct ippeve_filter_s
/**** Attribute filter ****/
153 cups_array_t
*ra
; /* Requested attributes */
154 ipp_tag_t group_tag
; /* Group to copy */
157 typedef struct ippeve_job_s ippeve_job_t
;
159 typedef struct ippeve_printer_s
/**** Printer data ****/
161 /* TODO: One IPv4 and one IPv6 listener are really not sufficient */
162 int ipv4
, /* IPv4 listener */
163 ipv6
; /* IPv6 listener */
164 ippeve_srv_t ipp_ref
, /* Bonjour IPP service */
165 ipps_ref
, /* Bonjour IPPS service */
166 http_ref
, /* Bonjour HTTP service */
167 printer_ref
; /* Bonjour LPD service */
168 char *dnssd_name
, /* printer-dnssd-name */
169 *name
, /* printer-name */
170 *icon
, /* Icon filename */
171 *directory
, /* Spool directory */
172 *hostname
, /* Hostname */
173 *uri
, /* printer-uri-supported */
174 *device_uri
, /* Device URI (if any) */
175 *output_format
, /* Output format */
177 *ppdfile
, /* PPD file (if any) */
178 #endif /* !CUPS_LITE */
179 *command
; /* Command to run with job file */
181 int web_forms
; /* Enable web interface forms? */
182 size_t urilen
; /* Length of printer URI */
183 ipp_t
*attrs
; /* Static attributes */
184 time_t start_time
; /* Startup time */
185 time_t config_time
; /* printer-config-change-time */
186 ipp_pstate_t state
; /* printer-state value */
187 ippeve_preason_t state_reasons
; /* printer-state-reasons values */
188 time_t state_time
; /* printer-state-change-time */
189 cups_array_t
*jobs
; /* Jobs */
190 ippeve_job_t
*active_job
; /* Current active/pending job */
191 int next_job_id
; /* Next job-id value */
192 _cups_rwlock_t rwlock
; /* Printer lock */
195 struct ippeve_job_s
/**** Job data ****/
198 const char *name
, /* job-name */
199 *username
, /* job-originating-user-name */
200 *format
; /* document-format */
201 ipp_jstate_t state
; /* job-state value */
202 char *message
; /* job-state-message value */
203 int msglevel
; /* job-state-message log level (0=error, 1=info) */
204 time_t created
, /* time-at-creation value */
205 processing
, /* time-at-processing value */
206 completed
; /* time-at-completed value */
207 int impressions
, /* job-impressions value */
208 impcompleted
; /* job-impressions-completed value */
209 ipp_t
*attrs
; /* Static attributes */
210 int cancel
; /* Non-zero when job canceled */
211 char *filename
; /* Print file name */
212 int fd
; /* Print file descriptor */
213 ippeve_printer_t
*printer
; /* Printer */
216 typedef struct ippeve_client_s
/**** Client data ****/
218 http_t
*http
; /* HTTP connection */
219 ipp_t
*request
, /* IPP request */
220 *response
; /* IPP response */
221 time_t start
; /* Request start time */
222 http_state_t operation
; /* Request operation */
223 ipp_op_t operation_id
; /* IPP operation-id */
224 char uri
[1024], /* Request URI */
225 *options
; /* URI options */
226 http_addr_t addr
; /* Client address */
227 char hostname
[256]; /* Client hostname */
228 ippeve_printer_t
*printer
; /* Printer */
229 ippeve_job_t
*job
; /* Current job, if any */
237 static void clean_jobs(ippeve_printer_t
*printer
);
238 static int compare_jobs(ippeve_job_t
*a
, ippeve_job_t
*b
);
239 static void copy_attributes(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
, ipp_tag_t group_tag
, int quickcopy
);
240 static void copy_job_attributes(ippeve_client_t
*client
, ippeve_job_t
*job
, cups_array_t
*ra
);
241 static ippeve_client_t
*create_client(ippeve_printer_t
*printer
, int sock
);
242 static ippeve_job_t
*create_job(ippeve_client_t
*client
);
243 static int create_job_file(ippeve_job_t
*job
, char *fname
, size_t fnamesize
, const char *dir
, const char *ext
);
244 static int create_listener(const char *name
, int port
, int family
);
245 static ipp_t
*create_media_col(const char *media
, const char *source
, const char *type
, int width
, int length
, int bottom
, int left
, int right
, int top
);
246 static ipp_t
*create_media_size(int width
, int length
);
247 static ippeve_printer_t
*create_printer(const char *servername
, int serverport
, const char *name
, const char *location
, const char *icon
, cups_array_t
*docformats
, const char *subtypes
, const char *directory
, const char *command
, const char *device_uri
, const char *output_format
, ipp_t
*attrs
);
248 static void debug_attributes(const char *title
, ipp_t
*ipp
, int response
);
249 static void delete_client(ippeve_client_t
*client
);
250 static void delete_job(ippeve_job_t
*job
);
251 static void delete_printer(ippeve_printer_t
*printer
);
253 static void DNSSD_API
dnssd_callback(DNSServiceRef sdRef
, DNSServiceFlags flags
, DNSServiceErrorType errorCode
, const char *name
, const char *regtype
, const char *domain
, ippeve_printer_t
*printer
);
254 #elif defined(HAVE_AVAHI)
255 static void dnssd_callback(AvahiEntryGroup
*p
, AvahiEntryGroupState state
, void *context
);
256 static void dnssd_client_cb(AvahiClient
*c
, AvahiClientState state
, void *userdata
);
257 #endif /* HAVE_DNSSD */
258 static void dnssd_init(void);
259 static int filter_cb(ippeve_filter_t
*filter
, ipp_t
*dst
, ipp_attribute_t
*attr
);
260 static ippeve_job_t
*find_job(ippeve_client_t
*client
);
261 static void finish_document_data(ippeve_client_t
*client
, ippeve_job_t
*job
);
262 static void finish_document_uri(ippeve_client_t
*client
, ippeve_job_t
*job
);
263 static void html_escape(ippeve_client_t
*client
, const char *s
, size_t slen
);
264 static void html_footer(ippeve_client_t
*client
);
265 static void html_header(ippeve_client_t
*client
, const char *title
, int refresh
);
266 static void html_printf(ippeve_client_t
*client
, const char *format
, ...) _CUPS_FORMAT(2, 3);
267 static void ipp_cancel_job(ippeve_client_t
*client
);
268 static void ipp_close_job(ippeve_client_t
*client
);
269 static void ipp_create_job(ippeve_client_t
*client
);
270 static void ipp_get_job_attributes(ippeve_client_t
*client
);
271 static void ipp_get_jobs(ippeve_client_t
*client
);
272 static void ipp_get_printer_attributes(ippeve_client_t
*client
);
273 static void ipp_identify_printer(ippeve_client_t
*client
);
274 static void ipp_print_job(ippeve_client_t
*client
);
275 static void ipp_print_uri(ippeve_client_t
*client
);
276 static void ipp_send_document(ippeve_client_t
*client
);
277 static void ipp_send_uri(ippeve_client_t
*client
);
278 static void ipp_validate_job(ippeve_client_t
*client
);
279 static ipp_t
*load_ippserver_attributes(const char *servername
, int serverport
, const char *filename
, cups_array_t
*docformats
);
280 static ipp_t
*load_legacy_attributes(const char *make
, const char *model
, int ppm
, int ppm_color
, int duplex
, cups_array_t
*docformats
);
282 static ipp_t
*load_ppd_attributes(const char *ppdfile
, cups_array_t
*docformats
);
283 #endif /* !CUPS_LITE */
284 static int parse_options(ippeve_client_t
*client
, cups_option_t
**options
);
285 static void process_attr_message(ippeve_job_t
*job
, char *message
);
286 static void *process_client(ippeve_client_t
*client
);
287 static int process_http(ippeve_client_t
*client
);
288 static int process_ipp(ippeve_client_t
*client
);
289 static void *process_job(ippeve_job_t
*job
);
290 static void process_state_message(ippeve_job_t
*job
, char *message
);
291 static int register_printer(ippeve_printer_t
*printer
, const char *subtypes
);
292 static int respond_http(ippeve_client_t
*client
, http_status_t code
, const char *content_coding
, const char *type
, size_t length
);
293 static void respond_ipp(ippeve_client_t
*client
, ipp_status_t status
, const char *message
, ...) _CUPS_FORMAT(3, 4);
294 static void respond_unsupported(ippeve_client_t
*client
, ipp_attribute_t
*attr
);
295 static void run_printer(ippeve_printer_t
*printer
);
296 static int show_media(ippeve_client_t
*client
);
297 static int show_status(ippeve_client_t
*client
);
298 static int show_supplies(ippeve_client_t
*client
);
299 static char *time_string(time_t tv
, char *buffer
, size_t bufsize
);
300 static void usage(int status
) _CUPS_NORETURN
;
301 static int valid_doc_attributes(ippeve_client_t
*client
);
302 static int valid_job_attributes(ippeve_client_t
*client
);
310 static DNSServiceRef DNSSDMaster
= NULL
;
311 #elif defined(HAVE_AVAHI)
312 static AvahiThreadedPoll
*DNSSDMaster
= NULL
;
313 static AvahiClient
*DNSSDClient
= NULL
;
314 #endif /* HAVE_DNSSD */
316 static int KeepFiles
= 0, /* Keep spooled job files? */
317 MaxVersion
= 20,/* Maximum IPP version (20 = 2.0, 11 = 1.1, etc.) */
318 Verbosity
= 0; /* Verbosity level */
322 * 'main()' - Main entry to the sample server.
325 int /* O - Exit status */
326 main(int argc
, /* I - Number of command-line args */
327 char *argv
[]) /* I - Command-line arguments */
329 int i
; /* Looping var */
330 const char *opt
, /* Current option character */
331 *attrfile
= NULL
, /* ippserver attributes file */
332 *command
= NULL
, /* Command to run with job files */
333 *device_uri
= NULL
, /* Device URI */
334 *output_format
= NULL
, /* Output format */
335 *icon
= NULL
, /* Icon file */
337 *keypath
= NULL
, /* Keychain path */
338 #endif /* HAVE_SSL */
339 *location
= "", /* Location of printer */
340 *make
= "Example", /* Manufacturer */
341 *model
= "Printer", /* Model */
342 *name
= NULL
, /* Printer name */
344 *ppdfile
= NULL
, /* PPD file */
345 #endif /* !CUPS_LITE */
346 *subtypes
= "_print"; /* DNS-SD service subtype */
347 int legacy
= 0, /* Legacy mode? */
348 duplex
= 0, /* Duplex mode */
349 ppm
= 10, /* Pages per minute for mono */
350 ppm_color
= 0, /* Pages per minute for color */
351 web_forms
= 1; /* Enable web site forms? */
352 ipp_t
*attrs
= NULL
; /* Printer attributes */
353 char directory
[1024] = ""; /* Spool directory */
354 cups_array_t
*docformats
= NULL
; /* Supported formats */
355 const char *servername
= NULL
; /* Server host name */
356 int serverport
= 0; /* Server port number (0 = auto) */
357 ippeve_printer_t
*printer
; /* Printer object */
361 * Parse command-line arguments...
364 for (i
= 1; i
< argc
; i
++)
366 if (!strcmp(argv
[i
], "--help"))
370 else if (!strcmp(argv
[i
], "--no-web-forms"))
374 else if (!strcmp(argv
[i
], "--version"))
379 else if (!strncmp(argv
[i
], "--", 2))
381 _cupsLangPrintf(stderr
, _("%s: Unknown option \"%s\"."), argv
[0], argv
[i
]);
384 else if (argv
[i
][0] == '-')
386 for (opt
= argv
[i
] + 1; *opt
; opt
++)
390 case '2' : /* -2 (enable 2-sided printing) */
395 case 'D' : /* -D device-uri */
400 device_uri
= argv
[i
];
403 case 'F' : /* -F output/format */
408 output_format
= argv
[i
];
412 case 'K' : /* -K keypath */
419 #endif /* HAVE_SSL */
421 case 'M' : /* -M manufacturer */
431 case 'P' : /* -P filename.ppd */
438 #endif /* !CUPS_LITE */
440 case 'V' : /* -V max-version */
445 if (!strcmp(argv
[i
], "2.0"))
447 else if (!strcmp(argv
[i
], "1.1"))
453 case 'a' : /* -a attributes-file */
461 case 'c' : /* -c command */
469 case 'd' : /* -d spool-directory */
474 strlcpy(directory
, argv
[i
], sizeof(directory
));
477 case 'f' : /* -f type/subtype[,...] */
482 docformats
= _cupsArrayNewStrings(argv
[i
], ',');
486 case 'i' : /* -i icon.png */
494 case 'k' : /* -k (keep files) */
498 case 'l' : /* -l location */
506 case 'm' : /* -m model */
515 case 'n' : /* -n hostname */
520 servername
= argv
[i
];
523 case 'p' : /* -p port */
525 if (i
>= argc
|| !isdigit(argv
[i
][0] & 255))
528 serverport
= atoi(argv
[i
]);
531 case 'r' : /* -r subtype */
539 case 's' : /* -s speed[,color-speed] */
544 if (sscanf(argv
[i
], "%d,%d", &ppm
, &ppm_color
) < 1)
550 case 'v' : /* -v (be verbose) */
554 default : /* Unknown */
555 _cupsLangPrintf(stderr
, _("%s: Unknown option \"-%c\"."), argv
[0], *opt
);
566 _cupsLangPrintf(stderr
, _("%s: Unknown option \"%s\"."), argv
[0], argv
[i
]);
575 if (attrfile
!= NULL
&& legacy
)
578 if (((ppdfile
!= NULL
) + (attrfile
!= NULL
) + legacy
) > 1)
580 #endif /* CUPS_LITE */
583 * Apply defaults as needed...
590 * Windows is almost always used as a single user system, so use a default
591 * port number of 8631.
598 * Use 8000 + UID mod 1000 for the default port number...
601 serverport
= 8000 + ((int)getuid() % 1000);
604 _cupsLangPrintf(stderr
, _("Listening on port %d."), serverport
);
609 const char *tmpdir
; /* Temporary directory */
612 if ((tmpdir
= getenv("TEMP")) == NULL
)
614 #elif defined(__APPLE__) && TARGET_OS_OSX
615 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
616 tmpdir
= "/private/tmp";
618 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
622 snprintf(directory
, sizeof(directory
), "%s/ippeveprinter.%d", tmpdir
, (int)getpid());
624 if (mkdir(directory
, 0755) && errno
!= EEXIST
)
626 _cupsLangPrintf(stderr
, _("Unable to create spool directory \"%s\": %s"), directory
, strerror(errno
));
631 _cupsLangPrintf(stderr
, _("Using spool directory \"%s\"."), directory
);
635 cupsSetServerCredentials(keypath
, servername
, 1);
636 #endif /* HAVE_SSL */
639 * Initialize DNS-SD...
645 * Create the printer...
649 docformats
= _cupsArrayNewStrings(ppm_color
> 0 ? "image/jpeg,image/pwg-raster,image/urf": "image/pwg-raster,image/urf", ',');
652 attrs
= load_ippserver_attributes(servername
, serverport
, attrfile
, docformats
);
656 attrs
= load_ppd_attributes(ppdfile
, docformats
);
659 command
= "ippeveps";
662 output_format
= "application/postscript";
664 #endif /* !CUPS_LITE */
666 attrs
= load_legacy_attributes(make
, model
, ppm
, ppm_color
, duplex
, docformats
);
668 if ((printer
= create_printer(servername
, serverport
, name
, location
, icon
, docformats
, subtypes
, directory
, command
, device_uri
, output_format
, attrs
)) == NULL
)
671 printer
->web_forms
= web_forms
;
675 printer
->ppdfile
= strdup(ppdfile
);
676 #endif /* !CUPS_LITE */
679 * Run the print service...
682 run_printer(printer
);
685 * Destroy the printer and exit...
688 delete_printer(printer
);
695 * 'clean_jobs()' - Clean out old (completed) jobs.
699 clean_jobs(ippeve_printer_t
*printer
) /* I - Printer */
701 ippeve_job_t
*job
; /* Current job */
702 time_t cleantime
; /* Clean time */
705 if (cupsArrayCount(printer
->jobs
) == 0)
708 cleantime
= time(NULL
) - 60;
710 _cupsRWLockWrite(&(printer
->rwlock
));
711 for (job
= (ippeve_job_t
*)cupsArrayFirst(printer
->jobs
);
713 job
= (ippeve_job_t
*)cupsArrayNext(printer
->jobs
))
714 if (job
->completed
&& job
->completed
< cleantime
)
716 cupsArrayRemove(printer
->jobs
, job
);
721 _cupsRWUnlock(&(printer
->rwlock
));
726 * 'compare_jobs()' - Compare two jobs.
729 static int /* O - Result of comparison */
730 compare_jobs(ippeve_job_t
*a
, /* I - First job */
731 ippeve_job_t
*b
) /* I - Second job */
733 return (b
->id
- a
->id
);
738 * 'copy_attributes()' - Copy attributes from one request to another.
742 copy_attributes(ipp_t
*to
, /* I - Destination request */
743 ipp_t
*from
, /* I - Source request */
744 cups_array_t
*ra
, /* I - Requested attributes */
745 ipp_tag_t group_tag
, /* I - Group to copy */
746 int quickcopy
) /* I - Do a quick copy? */
748 ippeve_filter_t filter
; /* Filter data */
752 filter
.group_tag
= group_tag
;
754 ippCopyAttributes(to
, from
, quickcopy
, (ipp_copycb_t
)filter_cb
, &filter
);
759 * 'copy_job_attrs()' - Copy job attributes to the response.
764 ippeve_client_t
*client
, /* I - Client */
765 ippeve_job_t
*job
, /* I - Job */
766 cups_array_t
*ra
) /* I - requested-attributes */
768 copy_attributes(client
->response
, job
->attrs
, ra
, IPP_TAG_JOB
, 0);
770 if (!ra
|| cupsArrayFind(ra
, "date-time-at-completed"))
773 ippAddDate(client
->response
, IPP_TAG_JOB
, "date-time-at-completed", ippTimeToDate(job
->completed
));
775 ippAddOutOfBand(client
->response
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-completed");
778 if (!ra
|| cupsArrayFind(ra
, "date-time-at-processing"))
781 ippAddDate(client
->response
, IPP_TAG_JOB
, "date-time-at-processing", ippTimeToDate(job
->processing
));
783 ippAddOutOfBand(client
->response
, IPP_TAG_JOB
, IPP_TAG_NOVALUE
, "date-time-at-processing");
786 if (!ra
|| cupsArrayFind(ra
, "job-impressions"))
787 ippAddInteger(client
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions", job
->impressions
);
789 if (!ra
|| cupsArrayFind(ra
, "job-impressions-completed"))
790 ippAddInteger(client
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-impressions-completed", job
->impcompleted
);
792 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
793 ippAddInteger(client
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-printer-up-time", (int)(time(NULL
) - client
->printer
->start_time
));
795 if (!ra
|| cupsArrayFind(ra
, "job-state"))
796 ippAddInteger(client
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "job-state", (int)job
->state
);
798 if (!ra
|| cupsArrayFind(ra
, "job-state-message"))
802 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_TAG_TEXT
, "job-state-message", NULL
, job
->message
);
808 case IPP_JSTATE_PENDING
:
809 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job pending.");
812 case IPP_JSTATE_HELD
:
814 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job incoming.");
815 else if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_ZERO
))
816 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job held.");
818 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job created.");
821 case IPP_JSTATE_PROCESSING
:
823 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job canceling.");
825 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job printing.");
828 case IPP_JSTATE_STOPPED
:
829 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job stopped.");
832 case IPP_JSTATE_CANCELED
:
833 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job canceled.");
836 case IPP_JSTATE_ABORTED
:
837 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job aborted.");
840 case IPP_JSTATE_COMPLETED
:
841 ippAddString(client
->response
, IPP_TAG_JOB
, IPP_CONST_TAG(IPP_TAG_TEXT
), "job-state-message", NULL
, "Job completed.");
847 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
851 case IPP_JSTATE_PENDING
:
852 ippAddString(client
->response
, IPP_TAG_JOB
,
853 IPP_CONST_TAG(IPP_TAG_KEYWORD
), "job-state-reasons",
857 case IPP_JSTATE_HELD
:
859 ippAddString(client
->response
, IPP_TAG_JOB
,
860 IPP_CONST_TAG(IPP_TAG_KEYWORD
),
861 "job-state-reasons", NULL
, "job-incoming");
862 else if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_ZERO
))
863 ippAddString(client
->response
, IPP_TAG_JOB
,
864 IPP_CONST_TAG(IPP_TAG_KEYWORD
),
865 "job-state-reasons", NULL
, "job-hold-until-specified");
867 ippAddString(client
->response
, IPP_TAG_JOB
,
868 IPP_CONST_TAG(IPP_TAG_KEYWORD
),
869 "job-state-reasons", NULL
, "job-data-insufficient");
872 case IPP_JSTATE_PROCESSING
:
874 ippAddString(client
->response
, IPP_TAG_JOB
,
875 IPP_CONST_TAG(IPP_TAG_KEYWORD
),
876 "job-state-reasons", NULL
, "processing-to-stop-point");
878 ippAddString(client
->response
, IPP_TAG_JOB
,
879 IPP_CONST_TAG(IPP_TAG_KEYWORD
),
880 "job-state-reasons", NULL
, "job-printing");
883 case IPP_JSTATE_STOPPED
:
884 ippAddString(client
->response
, IPP_TAG_JOB
,
885 IPP_CONST_TAG(IPP_TAG_KEYWORD
), "job-state-reasons",
886 NULL
, "job-stopped");
889 case IPP_JSTATE_CANCELED
:
890 ippAddString(client
->response
, IPP_TAG_JOB
,
891 IPP_CONST_TAG(IPP_TAG_KEYWORD
), "job-state-reasons",
892 NULL
, "job-canceled-by-user");
895 case IPP_JSTATE_ABORTED
:
896 ippAddString(client
->response
, IPP_TAG_JOB
,
897 IPP_CONST_TAG(IPP_TAG_KEYWORD
), "job-state-reasons",
898 NULL
, "aborted-by-system");
901 case IPP_JSTATE_COMPLETED
:
902 ippAddString(client
->response
, IPP_TAG_JOB
,
903 IPP_CONST_TAG(IPP_TAG_KEYWORD
), "job-state-reasons",
904 NULL
, "job-completed-successfully");
909 if (!ra
|| cupsArrayFind(ra
, "time-at-completed"))
910 ippAddInteger(client
->response
, IPP_TAG_JOB
,
911 job
->completed
? IPP_TAG_INTEGER
: IPP_TAG_NOVALUE
,
912 "time-at-completed", (int)(job
->completed
- client
->printer
->start_time
));
914 if (!ra
|| cupsArrayFind(ra
, "time-at-processing"))
915 ippAddInteger(client
->response
, IPP_TAG_JOB
,
916 job
->processing
? IPP_TAG_INTEGER
: IPP_TAG_NOVALUE
,
917 "time-at-processing", (int)(job
->processing
- client
->printer
->start_time
));
922 * 'create_client()' - Accept a new network connection and create a client
926 static ippeve_client_t
* /* O - Client */
927 create_client(ippeve_printer_t
*printer
, /* I - Printer */
928 int sock
) /* I - Listen socket */
930 ippeve_client_t
*client
; /* Client */
933 if ((client
= calloc(1, sizeof(ippeve_client_t
))) == NULL
)
935 perror("Unable to allocate memory for client");
939 client
->printer
= printer
;
942 * Accept the client and get the remote address...
945 if ((client
->http
= httpAcceptConnection(sock
, 1)) == NULL
)
947 perror("Unable to accept client connection");
954 httpGetHostname(client
->http
, client
->hostname
, sizeof(client
->hostname
));
957 fprintf(stderr
, "Accepted connection from %s\n", client
->hostname
);
964 * 'create_job()' - Create a new job object from a Print-Job or Create-Job
968 static ippeve_job_t
* /* O - Job */
969 create_job(ippeve_client_t
*client
) /* I - Client */
971 ippeve_job_t
*job
; /* Job */
972 ipp_attribute_t
*attr
; /* Job attribute */
973 char uri
[1024], /* job-uri value */
974 uuid
[64]; /* job-uuid value */
977 _cupsRWLockWrite(&(client
->printer
->rwlock
));
978 if (client
->printer
->active_job
&&
979 client
->printer
->active_job
->state
< IPP_JSTATE_CANCELED
)
982 * Only accept a single job at a time...
985 _cupsRWUnlock(&(client
->printer
->rwlock
));
990 * Allocate and initialize the job object...
993 if ((job
= calloc(1, sizeof(ippeve_job_t
))) == NULL
)
995 perror("Unable to allocate memory for job");
999 job
->printer
= client
->printer
;
1000 job
->attrs
= ippNew();
1001 job
->state
= IPP_JSTATE_HELD
;
1005 * Copy all of the job attributes...
1008 copy_attributes(job
->attrs
, client
->request
, NULL
, IPP_TAG_JOB
, 0);
1011 * Get the requesting-user-name, document format, and priority...
1014 if ((attr
= ippFindAttribute(client
->request
, "requesting-user-name", IPP_TAG_NAME
)) != NULL
)
1015 job
->username
= ippGetString(attr
, 0, NULL
);
1017 job
->username
= "anonymous";
1019 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-originating-user-name", NULL
, job
->username
);
1021 if (ippGetOperation(client
->request
) != IPP_OP_CREATE_JOB
)
1023 if ((attr
= ippFindAttribute(job
->attrs
, "document-format-detected", IPP_TAG_MIMETYPE
)) != NULL
)
1024 job
->format
= ippGetString(attr
, 0, NULL
);
1025 else if ((attr
= ippFindAttribute(job
->attrs
, "document-format-supplied", IPP_TAG_MIMETYPE
)) != NULL
)
1026 job
->format
= ippGetString(attr
, 0, NULL
);
1028 job
->format
= "application/octet-stream";
1031 if ((attr
= ippFindAttribute(client
->request
, "job-impressions", IPP_TAG_INTEGER
)) != NULL
)
1032 job
->impressions
= ippGetInteger(attr
, 0);
1034 if ((attr
= ippFindAttribute(client
->request
, "job-name", IPP_TAG_NAME
)) != NULL
)
1035 job
->name
= ippGetString(attr
, 0, NULL
);
1038 * Add job description attributes and add to the jobs array...
1041 job
->id
= client
->printer
->next_job_id
++;
1043 snprintf(uri
, sizeof(uri
), "%s/%d", client
->printer
->uri
, job
->id
);
1044 httpAssembleUUID(client
->printer
->hostname
, client
->printer
->port
, client
->printer
->name
, job
->id
, uuid
, sizeof(uuid
));
1046 ippAddDate(job
->attrs
, IPP_TAG_JOB
, "date-time-at-creation", ippTimeToDate(time(&job
->created
)));
1047 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1048 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1049 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uuid", NULL
, uuid
);
1050 if ((attr
= ippFindAttribute(client
->request
, "printer-uri", IPP_TAG_URI
)) != NULL
)
1051 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
, ippGetString(attr
, 0, NULL
));
1053 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
, client
->printer
->uri
);
1054 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation", (int)(job
->created
- client
->printer
->start_time
));
1056 cupsArrayAdd(client
->printer
->jobs
, job
);
1057 client
->printer
->active_job
= job
;
1059 _cupsRWUnlock(&(client
->printer
->rwlock
));
1066 * 'create_job_file()' - Create a file for the document in a job.
1069 static int /* O - File descriptor or -1 on error */
1071 ippeve_job_t
*job
, /* I - Job */
1072 char *fname
, /* I - Filename buffer */
1073 size_t fnamesize
, /* I - Size of filename buffer */
1074 const char *directory
, /* I - Directory to store in */
1075 const char *ext
) /* I - Extension (`NULL` for default) */
1077 char name
[256], /* "Safe" filename */
1078 *nameptr
; /* Pointer into filename */
1079 const char *job_name
; /* job-name value */
1083 * Make a name from the job-name attribute...
1086 if ((job_name
= ippGetString(ippFindAttribute(job
->attrs
, "job-name", IPP_TAG_NAME
), 0, NULL
)) == NULL
)
1087 job_name
= "untitled";
1089 for (nameptr
= name
; *job_name
&& nameptr
< (name
+ sizeof(name
) - 1); job_name
++)
1091 if (isalnum(*job_name
& 255) || *job_name
== '-')
1093 *nameptr
++ = (char)tolower(*job_name
& 255);
1099 while (job_name
[1] && !isalnum(job_name
[1] & 255) && job_name
[1] != '-')
1107 * Figure out the extension...
1112 if (!strcasecmp(job
->format
, "image/jpeg"))
1114 else if (!strcasecmp(job
->format
, "image/png"))
1116 else if (!strcasecmp(job
->format
, "image/pwg-raster"))
1118 else if (!strcasecmp(job
->format
, "image/urf"))
1120 else if (!strcasecmp(job
->format
, "application/pdf"))
1122 else if (!strcasecmp(job
->format
, "application/postscript"))
1124 else if (!strcasecmp(job
->format
, "application/vnd.hp-pcl"))
1131 * Create a filename with the job-id, job-name, and document-format (extension)...
1134 snprintf(fname
, fnamesize
, "%s/%d-%s.%s", directory
, job
->id
, name
, ext
);
1136 return (open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666));
1141 * 'create_listener()' - Create a listener socket.
1144 static int /* O - Listener socket or -1 on error */
1145 create_listener(const char *name
, /* I - Host name (`NULL` for any address) */
1146 int port
, /* I - Port number */
1147 int family
) /* I - Address family */
1149 int sock
; /* Listener socket */
1150 http_addrlist_t
*addrlist
; /* Listen address */
1151 char service
[255]; /* Service port */
1154 snprintf(service
, sizeof(service
), "%d", port
);
1155 if ((addrlist
= httpAddrGetList(name
, family
, service
)) == NULL
)
1158 sock
= httpAddrListen(&(addrlist
->addr
), port
);
1160 httpAddrFreeList(addrlist
);
1167 * 'create_media_col()' - Create a media-col value.
1170 static ipp_t
* /* O - media-col collection */
1171 create_media_col(const char *media
, /* I - Media name */
1172 const char *source
, /* I - Media source, if any */
1173 const char *type
, /* I - Media type, if any */
1174 int width
, /* I - x-dimension in 2540ths */
1175 int length
, /* I - y-dimension in 2540ths */
1176 int bottom
, /* I - Bottom margin in 2540ths */
1177 int left
, /* I - Left margin in 2540ths */
1178 int right
, /* I - Right margin in 2540ths */
1179 int top
) /* I - Top margin in 2540ths */
1181 ipp_t
*media_col
= ippNew(), /* media-col value */
1182 *media_size
= create_media_size(width
, length
);
1183 /* media-size value */
1184 char media_key
[256]; /* media-key value */
1185 const char *media_key_suffix
= ""; /* media-key suffix */
1188 if (bottom
== 0 && left
== 0 && right
== 0 && top
== 0)
1189 media_key_suffix
= "_borderless";
1192 snprintf(media_key
, sizeof(media_key
), "%s_%s_%s%s", media
, source
, type
, media_key_suffix
);
1194 snprintf(media_key
, sizeof(media_key
), "%s__%s%s", media
, type
, media_key_suffix
);
1196 snprintf(media_key
, sizeof(media_key
), "%s_%s%s", media
, source
, media_key_suffix
);
1198 snprintf(media_key
, sizeof(media_key
), "%s%s", media
, media_key_suffix
);
1200 ippAddString(media_col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-key", NULL
, media_key
);
1201 ippAddCollection(media_col
, IPP_TAG_PRINTER
, "media-size", media_size
);
1202 ippAddString(media_col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-size-name", NULL
, media
);
1204 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-bottom-margin", bottom
);
1206 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-left-margin", left
);
1208 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-right-margin", right
);
1210 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-top-margin", top
);
1212 ippAddString(media_col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-source", NULL
, source
);
1214 ippAddString(media_col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-type", NULL
, type
);
1216 ippDelete(media_size
);
1223 * 'create_media_size()' - Create a media-size value.
1226 static ipp_t
* /* O - media-col collection */
1227 create_media_size(int width
, /* I - x-dimension in 2540ths */
1228 int length
) /* I - y-dimension in 2540ths */
1230 ipp_t
*media_size
= ippNew(); /* media-size value */
1233 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "x-dimension", width
);
1234 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "y-dimension", length
);
1236 return (media_size
);
1241 * 'create_printer()' - Create, register, and listen for connections to a
1245 static ippeve_printer_t
* /* O - Printer */
1247 const char *servername
, /* I - Server hostname (NULL for default) */
1248 int serverport
, /* I - Server port */
1249 const char *name
, /* I - printer-name */
1250 const char *location
, /* I - printer-location */
1251 const char *icon
, /* I - printer-icons */
1252 cups_array_t
*docformats
, /* I - document-format-supported */
1253 const char *subtypes
, /* I - Bonjour service subtype(s) */
1254 const char *directory
, /* I - Spool directory */
1255 const char *command
, /* I - Command to run on job files, if any */
1256 const char *device_uri
, /* I - Output device, if any */
1257 const char *output_format
, /* I - Output format, if any */
1258 ipp_t
*attrs
) /* I - Capability attributes */
1260 ippeve_printer_t
*printer
; /* Printer */
1261 int i
; /* Looping var */
1263 char path
[1024]; /* Full path to command */
1264 #endif /* !_WIN32 */
1265 char uri
[1024], /* Printer URI */
1267 securi
[1024], /* Secure printer URI */
1268 *uris
[2], /* All URIs */
1269 #endif /* HAVE_SSL */
1270 icons
[1024], /* printer-icons URI */
1271 adminurl
[1024], /* printer-more-info URI */
1272 supplyurl
[1024],/* printer-supply-info-uri URI */
1273 uuid
[128]; /* printer-uuid */
1274 int k_supported
; /* Maximum file size supported */
1275 int num_formats
; /* Number of supported document formats */
1276 const char *formats
[100], /* Supported document formats */
1277 *format
; /* Current format */
1278 int num_sup_attrs
; /* Number of supported attributes */
1279 const char *sup_attrs
[100];/* Supported attributes */
1280 char xxx_supported
[256];
1281 /* Name of -supported attribute */
1282 _cups_globals_t
*cg
= _cupsGlobals();
1283 /* Global path values */
1285 struct statvfs spoolinfo
; /* FS info for spool directory */
1286 double spoolsize
; /* FS size */
1287 #elif defined(HAVE_STATFS)
1288 struct statfs spoolinfo
; /* FS info for spool directory */
1289 double spoolsize
; /* FS size */
1290 #endif /* HAVE_STATVFS */
1291 static const char * const versions
[] =/* ipp-versions-supported values */
1296 static const char * const features
[] =/* ipp-features-supported values */
1300 static const int ops
[] = /* operations-supported values */
1304 IPP_OP_VALIDATE_JOB
,
1306 IPP_OP_SEND_DOCUMENT
,
1309 IPP_OP_GET_JOB_ATTRIBUTES
,
1311 IPP_OP_GET_PRINTER_ATTRIBUTES
,
1312 IPP_OP_CANCEL_MY_JOBS
,
1314 IPP_OP_IDENTIFY_PRINTER
1316 static const char * const charsets
[] =/* charset-supported values */
1321 static const char * const compressions
[] =/* compression-supported values */
1326 #endif /* HAVE_LIBZ */
1329 static const char * const identify_actions
[] =
1334 static const char * const job_creation
[] =
1335 { /* job-creation-attributes-supported values */
1341 "document-metadata",
1343 "document-natural-language",
1344 "document-password",
1347 "ipp-attribute-fidelity",
1350 "job-accouunting-sheets",
1351 "job-accounting-user-id",
1352 "job-authorization-uri",
1356 "job-hold-until-time",
1357 "job-mandatory-attributes",
1358 "job-message-to-operator",
1360 "job-pages-per-set",
1362 "job-password-encryption",
1365 "job-recipient-name",
1367 "job-sheet-message",
1372 "multiple-document-handling",
1374 "orientation-requested",
1380 "presentation-direction-number-up",
1382 "print-content-optimize",
1384 "print-rendering-intent",
1386 "printer-resolution",
1392 "x-side1-image-shift",
1393 "x-side2-image-shift",
1396 "y-side1-image-shift",
1397 "y-side2-image-shift"
1399 static const char * const media_col_supported
[] =
1400 { /* media-col-supported values */
1401 "media-bottom-margin",
1402 "media-left-margin",
1403 "media-right-margin",
1410 static const char * const multiple_document_handling
[] =
1411 { /* multiple-document-handling-supported values */
1412 "separate-documents-uncollated-copies",
1413 "separate-documents-collated-copies"
1415 static const char * const reference_uri_schemes_supported
[] =
1416 { /* reference-uri-schemes-supported */
1422 #endif /* HAVE_SSL */
1425 static const char * const uri_authentication_supported
[] =
1426 { /* uri-authentication-supported values */
1430 static const char * const uri_security_supported
[] =
1431 { /* uri-security-supported values */
1435 #endif /* HAVE_SSL */
1436 static const char * const which_jobs
[] =
1437 { /* which-jobs-supported values */
1446 "processing-stopped"
1452 * If a command was specified, make sure it exists and is executable...
1457 if (*command
== '/' || !strncmp(command
, "./", 2))
1459 if (access(command
, X_OK
))
1461 _cupsLangPrintf(stderr
, _("Unable to execute command \"%s\": %s"), command
, strerror(errno
));
1467 snprintf(path
, sizeof(path
), "%s/command/%s", cg
->cups_serverbin
, command
);
1469 if (access(command
, X_OK
))
1471 _cupsLangPrintf(stderr
, _("Unable to execute command \"%s\": %s"), command
, strerror(errno
));
1478 #endif /* !_WIN32 */
1481 * Allocate memory for the printer...
1484 if ((printer
= calloc(1, sizeof(ippeve_printer_t
))) == NULL
)
1486 _cupsLangPrintError(NULL
, _("Unable to allocate memory for printer"));
1492 printer
->name
= strdup(name
);
1493 printer
->dnssd_name
= strdup(name
);
1494 printer
->command
= command
? strdup(command
) : NULL
;
1495 printer
->device_uri
= device_uri
? strdup(device_uri
) : NULL
;
1496 printer
->output_format
= output_format
? strdup(output_format
) : NULL
;
1497 printer
->directory
= strdup(directory
);
1498 printer
->icon
= icon
? strdup(icon
) : NULL
;
1499 printer
->port
= serverport
;
1500 printer
->start_time
= time(NULL
);
1501 printer
->config_time
= printer
->start_time
;
1502 printer
->state
= IPP_PSTATE_IDLE
;
1503 printer
->state_reasons
= IPPEVE_PREASON_NONE
;
1504 printer
->state_time
= printer
->start_time
;
1505 printer
->jobs
= cupsArrayNew((cups_array_func_t
)compare_jobs
, NULL
);
1506 printer
->next_job_id
= 1;
1510 printer
->hostname
= strdup(servername
);
1514 char temp
[1024]; /* Temporary string */
1516 printer
->hostname
= strdup(httpGetHostname(NULL
, temp
, sizeof(temp
)));
1519 _cupsRWInit(&(printer
->rwlock
));
1522 * Create the listener sockets...
1525 if ((printer
->ipv4
= create_listener(servername
, printer
->port
, AF_INET
)) < 0)
1527 perror("Unable to create IPv4 listener");
1531 if ((printer
->ipv6
= create_listener(servername
, printer
->port
, AF_INET6
)) < 0)
1533 perror("Unable to create IPv6 listener");
1538 * Prepare URI values for the printer attributes...
1541 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
, printer
->hostname
, printer
->port
, "/ipp/print");
1542 printer
->uri
= strdup(uri
);
1543 printer
->urilen
= strlen(uri
);
1546 httpAssembleURI(HTTP_URI_CODING_ALL
, securi
, sizeof(securi
), "ipps", NULL
, printer
->hostname
, printer
->port
, "/ipp/print");
1547 #endif /* HAVE_SSL */
1549 httpAssembleURI(HTTP_URI_CODING_ALL
, icons
, sizeof(icons
), WEB_SCHEME
, NULL
, printer
->hostname
, printer
->port
, "/icon.png");
1550 httpAssembleURI(HTTP_URI_CODING_ALL
, adminurl
, sizeof(adminurl
), WEB_SCHEME
, NULL
, printer
->hostname
, printer
->port
, "/");
1551 httpAssembleURI(HTTP_URI_CODING_ALL
, supplyurl
, sizeof(supplyurl
), WEB_SCHEME
, NULL
, printer
->hostname
, printer
->port
, "/supplies");
1552 httpAssembleUUID(printer
->hostname
, serverport
, name
, 0, uuid
, sizeof(uuid
));
1556 fprintf(stderr
, "printer-more-info=\"%s\"\n", adminurl
);
1557 fprintf(stderr
, "printer-supply-info-uri=\"%s\"\n", supplyurl
);
1559 fprintf(stderr
, "printer-uri=\"%s\",\"%s\"\n", uri
, securi
);
1561 fprintf(stderr
, "printer-uri=\"%s\"\n", uri
);
1562 #endif /* HAVE_SSL */
1566 * Get the maximum spool size based on the size of the filesystem used for
1567 * the spool directory. If the host OS doesn't support the statfs call
1568 * or the filesystem is larger than 2TiB, always report INT_MAX.
1572 if (statvfs(printer
->directory
, &spoolinfo
))
1573 k_supported
= INT_MAX
;
1574 else if ((spoolsize
= (double)spoolinfo
.f_frsize
*
1575 spoolinfo
.f_blocks
/ 1024) > INT_MAX
)
1576 k_supported
= INT_MAX
;
1578 k_supported
= (int)spoolsize
;
1580 #elif defined(HAVE_STATFS)
1581 if (statfs(printer
->directory
, &spoolinfo
))
1582 k_supported
= INT_MAX
;
1583 else if ((spoolsize
= (double)spoolinfo
.f_bsize
*
1584 spoolinfo
.f_blocks
/ 1024) > INT_MAX
)
1585 k_supported
= INT_MAX
;
1587 k_supported
= (int)spoolsize
;
1590 k_supported
= INT_MAX
;
1591 #endif /* HAVE_STATVFS */
1594 * Assemble the final list of document formats...
1597 if (!cupsArrayFind(docformats
, (void *)"application/octet-stream"))
1598 cupsArrayAdd(docformats
, (void *)"application/octet-stream");
1600 for (num_formats
= 0, format
= (const char *)cupsArrayFirst(docformats
); format
&& num_formats
< (int)(sizeof(formats
) / sizeof(formats
[0])); format
= (const char *)cupsArrayNext(docformats
))
1601 formats
[num_formats
++] = format
;
1604 * Get the list of attributes that can be used when creating a job...
1608 sup_attrs
[num_sup_attrs
++] = "document-access";
1609 sup_attrs
[num_sup_attrs
++] = "document-charset";
1610 sup_attrs
[num_sup_attrs
++] = "document-format";
1611 sup_attrs
[num_sup_attrs
++] = "document-message";
1612 sup_attrs
[num_sup_attrs
++] = "document-metadata";
1613 sup_attrs
[num_sup_attrs
++] = "document-name";
1614 sup_attrs
[num_sup_attrs
++] = "document-natural-language";
1615 sup_attrs
[num_sup_attrs
++] = "ipp-attribute-fidelity";
1616 sup_attrs
[num_sup_attrs
++] = "job-name";
1617 sup_attrs
[num_sup_attrs
++] = "job-priority";
1619 for (i
= 0; i
< (int)(sizeof(job_creation
) / sizeof(job_creation
[0])) && num_sup_attrs
< (int)(sizeof(sup_attrs
) / sizeof(sup_attrs
[0])); i
++)
1621 snprintf(xxx_supported
, sizeof(xxx_supported
), "%s-supported", job_creation
[i
]);
1622 if (ippFindAttribute(attrs
, xxx_supported
, IPP_TAG_ZERO
))
1623 sup_attrs
[num_sup_attrs
++] = job_creation
[i
];
1627 * Fill out the rest of the printer attributes.
1630 printer
->attrs
= attrs
;
1632 /* charset-configured */
1633 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_CHARSET
), "charset-configured", NULL
, "utf-8");
1635 /* charset-supported */
1636 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_CHARSET
), "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]), NULL
, charsets
);
1638 /* compression-supported */
1639 if (!ippFindAttribute(printer
->attrs
, "compression-supported", IPP_TAG_ZERO
))
1640 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "compression-supported", (int)(sizeof(compressions
) / sizeof(compressions
[0])), NULL
, compressions
);
1642 /* document-format-default */
1643 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_MIMETYPE
), "document-format-default", NULL
, "application/octet-stream");
1645 /* document-format-supported */
1646 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
, "document-format-supported", num_formats
, NULL
, formats
);
1648 /* generated-natural-language-supported */
1649 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_LANGUAGE
), "generated-natural-language-supported", NULL
, "en");
1651 /* identify-actions-default */
1652 ippAddString (printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "identify-actions-default", NULL
, "sound");
1654 /* identify-actions-supported */
1655 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "identify-actions-supported", sizeof(identify_actions
) / sizeof(identify_actions
[0]), NULL
, identify_actions
);
1657 /* ipp-features-supported */
1658 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "ipp-features-supported", sizeof(features
) / sizeof(features
[0]), NULL
, features
);
1660 /* ipp-versions-supported */
1661 if (MaxVersion
== 11)
1662 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "ipp-versions-supported", NULL
, "1.1");
1664 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "ipp-versions-supported", (int)(sizeof(versions
) / sizeof(versions
[0])), NULL
, versions
);
1666 /* job-creation-attributes-supported */
1667 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "job-creation-attributes-supported", num_sup_attrs
, NULL
, sup_attrs
);
1669 /* job-ids-supported */
1670 ippAddBoolean(printer
->attrs
, IPP_TAG_PRINTER
, "job-ids-supported", 1);
1672 /* job-k-octets-supported */
1673 ippAddRange(printer
->attrs
, IPP_TAG_PRINTER
, "job-k-octets-supported", 0, k_supported
);
1675 /* job-priority-default */
1676 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "job-priority-default", 50);
1678 /* job-priority-supported */
1679 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "job-priority-supported", 1);
1681 /* job-sheets-default */
1682 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-sheets-default", NULL
, "none");
1684 /* job-sheets-supported */
1685 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_NAME
), "job-sheets-supported", NULL
, "none");
1687 /* media-col-supported */
1688 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-col-supported", (int)(sizeof(media_col_supported
) / sizeof(media_col_supported
[0])), NULL
, media_col_supported
);
1690 /* multiple-document-handling-supported */
1691 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "multiple-document-handling-supported", sizeof(multiple_document_handling
) / sizeof(multiple_document_handling
[0]), NULL
, multiple_document_handling
);
1693 /* multiple-document-jobs-supported */
1694 ippAddBoolean(printer
->attrs
, IPP_TAG_PRINTER
, "multiple-document-jobs-supported", 0);
1696 /* multiple-operation-time-out */
1697 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "multiple-operation-time-out", 60);
1699 /* multiple-operation-time-out-action */
1700 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "multiple-operation-time-out-action", NULL
, "abort-job");
1702 /* natural-language-configured */
1703 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_LANGUAGE
), "natural-language-configured", NULL
, "en");
1705 /* operations-supported */
1706 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "operations-supported", sizeof(ops
) / sizeof(ops
[0]), ops
);
1708 /* pdl-override-supported */
1709 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pdl-override-supported", NULL
, "attempted");
1711 /* preferred-attributes-supported */
1712 ippAddBoolean(printer
->attrs
, IPP_TAG_PRINTER
, "preferred-attributes-supported", 0);
1714 /* printer-get-attributes-supported */
1715 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "printer-get-attributes-supported", NULL
, "document-format");
1717 /* printer-geo-location */
1718 ippAddOutOfBand(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_UNKNOWN
, "printer-geo-location");
1721 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-icons", NULL
, icons
);
1723 /* printer-is-accepting-jobs */
1724 ippAddBoolean(printer
->attrs
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1727 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
, name
);
1729 /* printer-location */
1730 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location", NULL
, location
);
1732 /* printer-more-info */
1733 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info", NULL
, adminurl
);
1736 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
, name
);
1738 /* printer-organization */
1739 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_TEXT
), "printer-organization", NULL
, "");
1741 /* printer-organizational-unit */
1742 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_TEXT
), "printer-organizational-unit", NULL
, "");
1744 /* printer-supply-info-uri */
1745 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-supply-info-uri", NULL
, supplyurl
);
1747 /* printer-uri-supported */
1752 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uri-supported", 2, NULL
, (const char **)uris
);
1755 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uri-supported", NULL
, uri
);
1756 #endif /* HAVE_SSL */
1759 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uuid", NULL
, uuid
);
1761 /* reference-uri-scheme-supported */
1762 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_URISCHEME
), "reference-uri-schemes-supported", (int)(sizeof(reference_uri_schemes_supported
) / sizeof(reference_uri_schemes_supported
[0])), NULL
, reference_uri_schemes_supported
);
1764 /* uri-authentication-supported */
1766 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "uri-authentication-supported", 2, NULL
, uri_authentication_supported
);
1768 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "uri-authentication-supported", NULL
, "none");
1769 #endif /* HAVE_SSL */
1771 /* uri-security-supported */
1773 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "uri-security-supported", 2, NULL
, uri_security_supported
);
1775 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "uri-security-supported", NULL
, "none");
1776 #endif /* HAVE_SSL */
1778 /* which-jobs-supported */
1779 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "which-jobs-supported", sizeof(which_jobs
) / sizeof(which_jobs
[0]), NULL
, which_jobs
);
1781 debug_attributes("Printer", printer
->attrs
, 0);
1784 * Register the printer with Bonjour...
1787 if (!register_printer(printer
, subtypes
))
1798 * If we get here we were unable to create the printer...
1803 delete_printer(printer
);
1810 * 'debug_attributes()' - Print attributes in a request or response.
1814 debug_attributes(const char *title
, /* I - Title */
1815 ipp_t
*ipp
, /* I - Request/response */
1816 int type
) /* I - 0 = object, 1 = request, 2 = response */
1818 ipp_tag_t group_tag
; /* Current group */
1819 ipp_attribute_t
*attr
; /* Current attribute */
1820 char buffer
[2048]; /* String buffer for value */
1821 int major
, minor
; /* Version */
1827 fprintf(stderr
, "%s:\n", title
);
1828 major
= ippGetVersion(ipp
, &minor
);
1829 fprintf(stderr
, " version=%d.%d\n", major
, minor
);
1831 fprintf(stderr
, " operation-id=%s(%04x)\n",
1832 ippOpString(ippGetOperation(ipp
)), ippGetOperation(ipp
));
1834 fprintf(stderr
, " status-code=%s(%04x)\n",
1835 ippErrorString(ippGetStatusCode(ipp
)), ippGetStatusCode(ipp
));
1836 fprintf(stderr
, " request-id=%d\n\n", ippGetRequestId(ipp
));
1838 for (attr
= ippFirstAttribute(ipp
), group_tag
= IPP_TAG_ZERO
;
1840 attr
= ippNextAttribute(ipp
))
1842 if (ippGetGroupTag(attr
) != group_tag
)
1844 group_tag
= ippGetGroupTag(attr
);
1845 fprintf(stderr
, " %s\n", ippTagString(group_tag
));
1848 if (ippGetName(attr
))
1850 ippAttributeString(attr
, buffer
, sizeof(buffer
));
1851 fprintf(stderr
, " %s (%s%s) %s\n", ippGetName(attr
),
1852 ippGetCount(attr
) > 1 ? "1setOf " : "",
1853 ippTagString(ippGetValueTag(attr
)), buffer
);
1860 * 'delete_client()' - Close the socket and free all memory used by a client
1865 delete_client(ippeve_client_t
*client
) /* I - Client */
1868 fprintf(stderr
, "Closing connection from %s\n", client
->hostname
);
1871 * Flush pending writes before closing...
1874 httpFlushWrite(client
->http
);
1880 httpClose(client
->http
);
1882 ippDelete(client
->request
);
1883 ippDelete(client
->response
);
1890 * 'delete_job()' - Remove from the printer and free all memory used by a job
1895 delete_job(ippeve_job_t
*job
) /* I - Job */
1898 fprintf(stderr
, "[Job %d] Removing job from history.\n", job
->id
);
1900 ippDelete(job
->attrs
);
1908 unlink(job
->filename
);
1910 free(job
->filename
);
1918 * 'delete_printer()' - Unregister, close listen sockets, and free all memory
1919 * used by a printer object.
1923 delete_printer(ippeve_printer_t
*printer
) /* I - Printer */
1925 if (printer
->ipv4
>= 0)
1926 close(printer
->ipv4
);
1928 if (printer
->ipv6
>= 0)
1929 close(printer
->ipv6
);
1932 if (printer
->printer_ref
)
1933 DNSServiceRefDeallocate(printer
->printer_ref
);
1934 if (printer
->ipp_ref
)
1935 DNSServiceRefDeallocate(printer
->ipp_ref
);
1936 if (printer
->ipps_ref
)
1937 DNSServiceRefDeallocate(printer
->ipps_ref
);
1938 if (printer
->http_ref
)
1939 DNSServiceRefDeallocate(printer
->http_ref
);
1940 #elif defined(HAVE_AVAHI)
1941 avahi_threaded_poll_lock(DNSSDMaster
);
1943 if (printer
->printer_ref
)
1944 avahi_entry_group_free(printer
->printer_ref
);
1945 if (printer
->ipp_ref
)
1946 avahi_entry_group_free(printer
->ipp_ref
);
1947 if (printer
->ipps_ref
)
1948 avahi_entry_group_free(printer
->ipps_ref
);
1949 if (printer
->http_ref
)
1950 avahi_entry_group_free(printer
->http_ref
);
1952 avahi_threaded_poll_unlock(DNSSDMaster
);
1953 #endif /* HAVE_DNSSD */
1955 if (printer
->dnssd_name
)
1956 free(printer
->dnssd_name
);
1958 free(printer
->name
);
1960 free(printer
->icon
);
1961 if (printer
->command
)
1962 free(printer
->command
);
1963 if (printer
->device_uri
)
1964 free(printer
->device_uri
);
1966 if (printer
->ppdfile
)
1967 free(printer
->ppdfile
);
1968 #endif /* !CUPS_LITE */
1969 if (printer
->directory
)
1970 free(printer
->directory
);
1971 if (printer
->hostname
)
1972 free(printer
->hostname
);
1976 ippDelete(printer
->attrs
);
1977 cupsArrayDelete(printer
->jobs
);
1985 * 'dnssd_callback()' - Handle Bonjour registration events.
1988 static void DNSSD_API
1990 DNSServiceRef sdRef
, /* I - Service reference */
1991 DNSServiceFlags flags
, /* I - Status flags */
1992 DNSServiceErrorType errorCode
, /* I - Error, if any */
1993 const char *name
, /* I - Service name */
1994 const char *regtype
, /* I - Service type */
1995 const char *domain
, /* I - Domain for service */
1996 ippeve_printer_t
*printer
) /* I - Printer */
2004 fprintf(stderr
, "DNSServiceRegister for %s failed with error %d.\n", regtype
, (int)errorCode
);
2007 else if (strcasecmp(name
, printer
->dnssd_name
))
2010 fprintf(stderr
, "Now using DNS-SD service name \"%s\".\n", name
);
2012 /* No lock needed since only the main thread accesses/changes this */
2013 free(printer
->dnssd_name
);
2014 printer
->dnssd_name
= strdup(name
);
2019 #elif defined(HAVE_AVAHI)
2021 * 'dnssd_callback()' - Handle Bonjour registration events.
2026 AvahiEntryGroup
*srv
, /* I - Service */
2027 AvahiEntryGroupState state
, /* I - Registration state */
2028 void *context
) /* I - Printer */
2037 * 'dnssd_client_cb()' - Client callback for Avahi.
2039 * Called whenever the client or server state changes...
2044 AvahiClient
*c
, /* I - Client */
2045 AvahiClientState state
, /* I - Current state */
2046 void *userdata
) /* I - User data (unused) */
2056 fprintf(stderr
, "Ignored Avahi state %d.\n", state
);
2059 case AVAHI_CLIENT_FAILURE
:
2060 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
)
2062 fputs("Avahi server crashed, exiting.\n", stderr
);
2068 #endif /* HAVE_DNSSD */
2072 * 'dnssd_init()' - Initialize the DNS-SD service connections...
2079 if (DNSServiceCreateConnection(&DNSSDMaster
) != kDNSServiceErr_NoError
)
2081 fputs("Error: Unable to initialize Bonjour.\n", stderr
);
2085 #elif defined(HAVE_AVAHI)
2086 int error
; /* Error code, if any */
2088 if ((DNSSDMaster
= avahi_threaded_poll_new()) == NULL
)
2090 fputs("Error: Unable to initialize Bonjour.\n", stderr
);
2094 if ((DNSSDClient
= avahi_client_new(avahi_threaded_poll_get(DNSSDMaster
), AVAHI_CLIENT_NO_FAIL
, dnssd_client_cb
, NULL
, &error
)) == NULL
)
2096 fputs("Error: Unable to initialize Bonjour.\n", stderr
);
2100 avahi_threaded_poll_start(DNSSDMaster
);
2101 #endif /* HAVE_DNSSD */
2106 * 'filter_cb()' - Filter printer attributes based on the requested array.
2109 static int /* O - 1 to copy, 0 to ignore */
2110 filter_cb(ippeve_filter_t
*filter
, /* I - Filter parameters */
2111 ipp_t
*dst
, /* I - Destination (unused) */
2112 ipp_attribute_t
*attr
) /* I - Source attribute */
2115 * Filter attributes as needed...
2118 #ifndef _WIN32 /* Avoid MS compiler bug */
2120 #endif /* !_WIN32 */
2122 ipp_tag_t group
= ippGetGroupTag(attr
);
2123 const char *name
= ippGetName(attr
);
2125 if ((filter
->group_tag
!= IPP_TAG_ZERO
&& group
!= filter
->group_tag
&& group
!= IPP_TAG_ZERO
) || !name
|| (!strcmp(name
, "media-col-database") && !cupsArrayFind(filter
->ra
, (void *)name
)))
2128 return (!filter
->ra
|| cupsArrayFind(filter
->ra
, (void *)name
) != NULL
);
2133 * 'find_job()' - Find a job specified in a request.
2136 static ippeve_job_t
* /* O - Job or NULL */
2137 find_job(ippeve_client_t
*client
) /* I - Client */
2139 ipp_attribute_t
*attr
; /* job-id or job-uri attribute */
2140 ippeve_job_t key
, /* Job search key */
2141 *job
; /* Matching job, if any */
2144 if ((attr
= ippFindAttribute(client
->request
, "job-uri", IPP_TAG_URI
)) != NULL
)
2146 const char *uri
= ippGetString(attr
, 0, NULL
);
2148 if (!strncmp(uri
, client
->printer
->uri
, client
->printer
->urilen
) &&
2149 uri
[client
->printer
->urilen
] == '/')
2150 key
.id
= atoi(uri
+ client
->printer
->urilen
+ 1);
2154 else if ((attr
= ippFindAttribute(client
->request
, "job-id", IPP_TAG_INTEGER
)) != NULL
)
2155 key
.id
= ippGetInteger(attr
, 0);
2157 _cupsRWLockRead(&(client
->printer
->rwlock
));
2158 job
= (ippeve_job_t
*)cupsArrayFind(client
->printer
->jobs
, &key
);
2159 _cupsRWUnlock(&(client
->printer
->rwlock
));
2166 * 'finish_document()' - Finish receiving a document file and start processing.
2170 finish_document_data(
2171 ippeve_client_t
*client
, /* I - Client */
2172 ippeve_job_t
*job
) /* I - Job */
2174 char filename
[1024], /* Filename buffer */
2175 buffer
[4096]; /* Copy buffer */
2176 ssize_t bytes
; /* Bytes read */
2177 cups_array_t
*ra
; /* Attributes to send in response */
2178 _cups_thread_t t
; /* Thread */
2182 * Create a file for the request data...
2184 * TODO: Update code to support piping large raster data to the print command.
2187 if ((job
->fd
= create_job_file(job
, filename
, sizeof(filename
), client
->printer
->directory
, NULL
)) < 0)
2189 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to create print file: %s", strerror(errno
));
2195 fprintf(stderr
, "Created job file \"%s\", format \"%s\".\n", filename
, job
->format
);
2197 while ((bytes
= httpRead2(client
->http
, buffer
, sizeof(buffer
))) > 0)
2199 if (write(job
->fd
, buffer
, (size_t)bytes
) < bytes
)
2201 int error
= errno
; /* Write error */
2208 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to write print file: %s", strerror(error
));
2217 * Got an error while reading the print data, so abort this job.
2225 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to read print file.");
2232 int error
= errno
; /* Write error */
2238 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to write print file: %s", strerror(error
));
2244 job
->filename
= strdup(filename
);
2245 job
->state
= IPP_JSTATE_PENDING
;
2248 * Process the job...
2251 t
= _cupsThreadCreate((_cups_thread_func_t
)process_job
, job
);
2255 _cupsThreadDetach(t
);
2259 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to process job.");
2264 * Return the job info...
2267 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
2269 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2270 cupsArrayAdd(ra
, "job-id");
2271 cupsArrayAdd(ra
, "job-state");
2272 cupsArrayAdd(ra
, "job-state-message");
2273 cupsArrayAdd(ra
, "job-state-reasons");
2274 cupsArrayAdd(ra
, "job-uri");
2276 copy_job_attributes(client
, job
, ra
);
2277 cupsArrayDelete(ra
);
2281 * If we get here we had to abort the job...
2286 job
->state
= IPP_JSTATE_ABORTED
;
2287 job
->completed
= time(NULL
);
2289 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2290 cupsArrayAdd(ra
, "job-id");
2291 cupsArrayAdd(ra
, "job-state");
2292 cupsArrayAdd(ra
, "job-state-reasons");
2293 cupsArrayAdd(ra
, "job-uri");
2295 copy_job_attributes(client
, job
, ra
);
2296 cupsArrayDelete(ra
);
2301 * 'finish_uri()' - Finish fetching a document URI and start processing.
2305 finish_document_uri(
2306 ippeve_client_t
*client
, /* I - Client */
2307 ippeve_job_t
*job
) /* I - Job */
2309 ipp_attribute_t
*uri
; /* document-uri */
2310 char scheme
[256], /* URI scheme */
2311 userpass
[256], /* Username and password info */
2312 hostname
[256], /* Hostname */
2313 resource
[1024]; /* Resource path */
2314 int port
; /* Port number */
2315 http_uri_status_t uri_status
; /* URI decode status */
2316 http_encryption_t encryption
; /* Encryption to use, if any */
2317 http_t
*http
; /* Connection for http/https URIs */
2318 http_status_t status
; /* Access status for http/https URIs */
2319 int infile
; /* Input file for local file URIs */
2320 char filename
[1024], /* Filename buffer */
2321 buffer
[4096]; /* Copy buffer */
2322 ssize_t bytes
; /* Bytes read */
2323 ipp_attribute_t
*attr
; /* Current attribute */
2324 cups_array_t
*ra
; /* Attributes to send in response */
2328 * Do we have a file to print?
2331 if (httpGetState(client
->http
) == HTTP_STATE_POST_RECV
)
2333 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "Unexpected document data following request.");
2339 * Do we have a document URI?
2342 if ((uri
= ippFindAttribute(client
->request
, "document-uri", IPP_TAG_URI
)) == NULL
)
2344 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "Missing document-uri.");
2349 if (ippGetCount(uri
) != 1)
2351 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "Too many document-uri values.");
2356 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, ippGetString(uri
, 0, NULL
),
2357 scheme
, sizeof(scheme
), userpass
,
2358 sizeof(userpass
), hostname
, sizeof(hostname
),
2359 &port
, resource
, sizeof(resource
));
2360 if (uri_status
< HTTP_URI_STATUS_OK
)
2362 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "Bad document-uri: %s", httpURIStatusString(uri_status
));
2367 if (strcmp(scheme
, "file") &&
2369 strcmp(scheme
, "https") &&
2370 #endif /* HAVE_SSL */
2371 strcmp(scheme
, "http"))
2373 respond_ipp(client
, IPP_STATUS_ERROR_URI_SCHEME
, "URI scheme \"%s\" not supported.", scheme
);
2378 if (!strcmp(scheme
, "file") && access(resource
, R_OK
))
2380 respond_ipp(client
, IPP_STATUS_ERROR_DOCUMENT_ACCESS
, "Unable to access URI: %s", strerror(errno
));
2386 * Get the document format for the job...
2389 _cupsRWLockWrite(&(client
->printer
->rwlock
));
2391 if ((attr
= ippFindAttribute(job
->attrs
, "document-format", IPP_TAG_MIMETYPE
)) != NULL
)
2392 job
->format
= ippGetString(attr
, 0, NULL
);
2394 job
->format
= "application/octet-stream";
2397 * Create a file for the request data...
2400 if ((job
->fd
= create_job_file(job
, filename
, sizeof(filename
), client
->printer
->directory
, NULL
)) < 0)
2402 _cupsRWUnlock(&(client
->printer
->rwlock
));
2404 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to create print file: %s", strerror(errno
));
2409 _cupsRWUnlock(&(client
->printer
->rwlock
));
2411 if (!strcmp(scheme
, "file"))
2413 if ((infile
= open(resource
, O_RDONLY
)) < 0)
2415 respond_ipp(client
, IPP_STATUS_ERROR_DOCUMENT_ACCESS
, "Unable to access URI: %s", strerror(errno
));
2422 if ((bytes
= read(infile
, buffer
, sizeof(buffer
))) < 0 &&
2423 (errno
== EAGAIN
|| errno
== EINTR
))
2427 else if (bytes
> 0 && write(job
->fd
, buffer
, (size_t)bytes
) < bytes
)
2429 int error
= errno
; /* Write error */
2437 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to write print file: %s", strerror(error
));
2449 if (port
== 443 || !strcmp(scheme
, "https"))
2450 encryption
= HTTP_ENCRYPTION_ALWAYS
;
2452 #endif /* HAVE_SSL */
2453 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
2455 if ((http
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 30000, NULL
)) == NULL
)
2457 respond_ipp(client
, IPP_STATUS_ERROR_DOCUMENT_ACCESS
, "Unable to connect to %s: %s", hostname
, cupsLastErrorString());
2467 httpClearFields(http
);
2468 httpSetField(http
, HTTP_FIELD_ACCEPT_LANGUAGE
, "en");
2469 if (httpGet(http
, resource
))
2471 respond_ipp(client
, IPP_STATUS_ERROR_DOCUMENT_ACCESS
, "Unable to GET URI: %s", strerror(errno
));
2482 while ((status
= httpUpdate(http
)) == HTTP_STATUS_CONTINUE
);
2484 if (status
!= HTTP_STATUS_OK
)
2486 respond_ipp(client
, IPP_STATUS_ERROR_DOCUMENT_ACCESS
, "Unable to GET URI: %s", httpStatus(status
));
2497 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
2499 if (write(job
->fd
, buffer
, (size_t)bytes
) < bytes
)
2501 int error
= errno
; /* Write error */
2509 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
,
2510 "Unable to write print file: %s", strerror(error
));
2521 int error
= errno
; /* Write error */
2527 respond_ipp(client
, IPP_STATUS_ERROR_INTERNAL
, "Unable to write print file: %s", strerror(error
));
2532 _cupsRWLockWrite(&(client
->printer
->rwlock
));
2535 job
->filename
= strdup(filename
);
2536 job
->state
= IPP_JSTATE_PENDING
;
2538 _cupsRWUnlock(&(client
->printer
->rwlock
));
2541 * Process the job...
2547 * Return the job info...
2550 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
2552 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2553 cupsArrayAdd(ra
, "job-id");
2554 cupsArrayAdd(ra
, "job-state");
2555 cupsArrayAdd(ra
, "job-state-reasons");
2556 cupsArrayAdd(ra
, "job-uri");
2558 copy_job_attributes(client
, job
, ra
);
2559 cupsArrayDelete(ra
);
2563 * If we get here we had to abort the job...
2568 job
->state
= IPP_JSTATE_ABORTED
;
2569 job
->completed
= time(NULL
);
2571 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2572 cupsArrayAdd(ra
, "job-id");
2573 cupsArrayAdd(ra
, "job-state");
2574 cupsArrayAdd(ra
, "job-state-reasons");
2575 cupsArrayAdd(ra
, "job-uri");
2577 copy_job_attributes(client
, job
, ra
);
2578 cupsArrayDelete(ra
);
2583 * 'html_escape()' - Write a HTML-safe string.
2587 html_escape(ippeve_client_t
*client
, /* I - Client */
2588 const char *s
, /* I - String to write */
2589 size_t slen
) /* I - Number of characters to write */
2591 const char *start
, /* Start of segment */
2592 *end
; /* End of string */
2596 end
= s
+ (slen
> 0 ? slen
: strlen(s
));
2598 while (*s
&& s
< end
)
2600 if (*s
== '&' || *s
== '<')
2603 httpWrite2(client
->http
, start
, (size_t)(s
- start
));
2606 httpWrite2(client
->http
, "&", 5);
2608 httpWrite2(client
->http
, "<", 4);
2617 httpWrite2(client
->http
, start
, (size_t)(s
- start
));
2622 * 'html_footer()' - Show the web interface footer.
2624 * This function also writes the trailing 0-length chunk.
2628 html_footer(ippeve_client_t
*client
) /* I - Client */
2634 httpWrite2(client
->http
, "", 0);
2639 * 'html_header()' - Show the web interface header and title.
2643 html_header(ippeve_client_t
*client
, /* I - Client */
2644 const char *title
, /* I - Title */
2645 int refresh
) /* I - Refresh timer, if any */
2651 "<title>%s</title>\n"
2652 "<link rel=\"shortcut icon\" href=\"/icon.png\" type=\"image/png\">\n"
2653 "<link rel=\"apple-touch-icon\" href=\"/icon.png\" type=\"image/png\">\n"
2654 "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\">\n", title
);
2656 html_printf(client
, "<meta http-equiv=\"refresh\" content=\"%d\">\n", refresh
);
2658 "<meta name=\"viewport\" content=\"width=device-width\">\n"
2660 "body { font-family: sans-serif; margin: 0; }\n"
2661 "div.body { padding: 0px 10px 10px; }\n"
2662 "span.badge { background: #090; border-radius: 5px; color: #fff; padding: 5px 10px; }\n"
2663 "span.bar { box-shadow: 0px 1px 5px #333; font-size: 75%%; }\n"
2664 "table.form { border-collapse: collapse; margin-left: auto; margin-right: auto; margin-top: 10px; width: auto; }\n"
2665 "table.form td, table.form th { padding: 5px 2px; }\n"
2666 "table.form td.meter { border-right: solid 1px #ccc; padding: 0px; width: 400px; }\n"
2667 "table.form th { text-align: right; }\n"
2668 "table.striped { border-bottom: solid thin black; border-collapse: collapse; width: 100%%; }\n"
2669 "table.striped tr:nth-child(even) { background: #fcfcfc; }\n"
2670 "table.striped tr:nth-child(odd) { background: #f0f0f0; }\n"
2671 "table.striped th { background: white; border-bottom: solid thin black; text-align: left; vertical-align: bottom; }\n"
2672 "table.striped td { margin: 0; padding: 5px; vertical-align: top; }\n"
2673 "table.nav { border-collapse: collapse; width: 100%%; }\n"
2674 "table.nav td { margin: 0; text-align: center; }\n"
2675 "td.nav a, td.nav a:active, td.nav a:hover, td.nav a:hover:link, td.nav a:hover:link:visited, td.nav a:link, td.nav a:link:visited, td.nav a:visited { background: inherit; color: inherit; font-size: 80%%; text-decoration: none; }\n"
2676 "td.nav { background: #333; color: #fff; padding: 4px 8px; width: 33%%; }\n"
2677 "td.nav.sel { background: #fff; color: #000; font-weight: bold; }\n"
2678 "td.nav:hover { background: #666; color: #fff; }\n"
2679 "td.nav:active { background: #000; color: #ff0; }\n"
2683 "<table class=\"nav\"><tr>"
2684 "<td class=\"nav%s\"><a href=\"/\">Status</a></td>"
2685 "<td class=\"nav%s\"><a href=\"/supplies\">Supplies</a></td>"
2686 "<td class=\"nav%s\"><a href=\"/media\">Media</a></td>"
2688 "<div class=\"body\">\n", !strcmp(client
->uri
, "/") ? " sel" : "", !strcmp(client
->uri
, "/supplies") ? " sel" : "", !strcmp(client
->uri
, "/media") ? " sel" : "");
2693 * 'html_printf()' - Send formatted text to the client, quoting as needed.
2697 html_printf(ippeve_client_t
*client
, /* I - Client */
2698 const char *format
, /* I - Printf-style format string */
2699 ...) /* I - Additional arguments as needed */
2701 va_list ap
; /* Pointer to arguments */
2702 const char *start
; /* Start of string */
2703 char size
, /* Size character (h, l, L) */
2704 type
; /* Format type character */
2705 int width
, /* Width of field */
2706 prec
; /* Number of characters of precision */
2707 char tformat
[100], /* Temporary format string for sprintf() */
2708 *tptr
, /* Pointer into temporary format */
2709 temp
[1024]; /* Buffer for formatted numbers */
2710 char *s
; /* Pointer to string */
2714 * Loop through the format string, formatting as needed...
2717 va_start(ap
, format
);
2725 httpWrite2(client
->http
, start
, (size_t)(format
- start
));
2728 *tptr
++ = *format
++;
2732 httpWrite2(client
->http
, "%", 1);
2737 else if (strchr(" -+#\'", *format
))
2738 *tptr
++ = *format
++;
2743 * Get width from argument...
2747 width
= va_arg(ap
, int);
2749 snprintf(tptr
, sizeof(tformat
) - (size_t)(tptr
- tformat
), "%d", width
);
2750 tptr
+= strlen(tptr
);
2756 while (isdigit(*format
& 255))
2758 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2761 width
= width
* 10 + *format
++ - '0';
2767 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2775 * Get precision from argument...
2779 prec
= va_arg(ap
, int);
2781 snprintf(tptr
, sizeof(tformat
) - (size_t)(tptr
- tformat
), "%d", prec
);
2782 tptr
+= strlen(tptr
);
2788 while (isdigit(*format
& 255))
2790 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2793 prec
= prec
* 10 + *format
++ - '0';
2798 if (*format
== 'l' && format
[1] == 'l')
2802 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
2810 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
2812 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2827 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2836 case 'E' : /* Floating point formats */
2841 if ((size_t)(width
+ 2) > sizeof(temp
))
2844 sprintf(temp
, tformat
, va_arg(ap
, double));
2846 httpWrite2(client
->http
, temp
, strlen(temp
));
2849 case 'B' : /* Integer formats */
2857 if ((size_t)(width
+ 2) > sizeof(temp
))
2860 # ifdef HAVE_LONG_LONG
2862 sprintf(temp
, tformat
, va_arg(ap
, long long));
2864 # endif /* HAVE_LONG_LONG */
2866 sprintf(temp
, tformat
, va_arg(ap
, long));
2868 sprintf(temp
, tformat
, va_arg(ap
, int));
2870 httpWrite2(client
->http
, temp
, strlen(temp
));
2873 case 'p' : /* Pointer value */
2874 if ((size_t)(width
+ 2) > sizeof(temp
))
2877 sprintf(temp
, tformat
, va_arg(ap
, void *));
2879 httpWrite2(client
->http
, temp
, strlen(temp
));
2882 case 'c' : /* Character or character array */
2885 temp
[0] = (char)va_arg(ap
, int);
2887 html_escape(client
, temp
, 1);
2890 html_escape(client
, va_arg(ap
, char *), (size_t)width
);
2893 case 's' : /* String */
2894 if ((s
= va_arg(ap
, char *)) == NULL
)
2897 html_escape(client
, s
, strlen(s
));
2906 httpWrite2(client
->http
, start
, (size_t)(format
- start
));
2913 * 'ipp_cancel_job()' - Cancel a job.
2917 ipp_cancel_job(ippeve_client_t
*client
) /* I - Client */
2919 ippeve_job_t
*job
; /* Job information */
2926 if ((job
= find_job(client
)) == NULL
)
2928 respond_ipp(client
, IPP_STATUS_ERROR_NOT_FOUND
, "Job does not exist.");
2933 * See if the job is already completed, canceled, or aborted; if so,
2934 * we can't cancel...
2939 case IPP_JSTATE_CANCELED
:
2940 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
,
2941 "Job #%d is already canceled - can\'t cancel.", job
->id
);
2944 case IPP_JSTATE_ABORTED
:
2945 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
,
2946 "Job #%d is already aborted - can\'t cancel.", job
->id
);
2949 case IPP_JSTATE_COMPLETED
:
2950 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
,
2951 "Job #%d is already completed - can\'t cancel.", job
->id
);
2959 _cupsRWLockWrite(&(client
->printer
->rwlock
));
2961 if (job
->state
== IPP_JSTATE_PROCESSING
||
2962 (job
->state
== IPP_JSTATE_HELD
&& job
->fd
>= 0))
2966 job
->state
= IPP_JSTATE_CANCELED
;
2967 job
->completed
= time(NULL
);
2970 _cupsRWUnlock(&(client
->printer
->rwlock
));
2972 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
2979 * 'ipp_close_job()' - Close an open job.
2983 ipp_close_job(ippeve_client_t
*client
) /* I - Client */
2985 ippeve_job_t
*job
; /* Job information */
2992 if ((job
= find_job(client
)) == NULL
)
2994 respond_ipp(client
, IPP_STATUS_ERROR_NOT_FOUND
, "Job does not exist.");
2999 * See if the job is already completed, canceled, or aborted; if so,
3000 * we can't cancel...
3005 case IPP_JSTATE_CANCELED
:
3006 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
,
3007 "Job #%d is canceled - can\'t close.", job
->id
);
3010 case IPP_JSTATE_ABORTED
:
3011 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
,
3012 "Job #%d is aborted - can\'t close.", job
->id
);
3015 case IPP_JSTATE_COMPLETED
:
3016 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
,
3017 "Job #%d is completed - can\'t close.", job
->id
);
3020 case IPP_JSTATE_PROCESSING
:
3021 case IPP_JSTATE_STOPPED
:
3022 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
,
3023 "Job #%d is already closed.", job
->id
);
3027 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
3034 * 'ipp_create_job()' - Create a job object.
3038 ipp_create_job(ippeve_client_t
*client
) /* I - Client */
3040 ippeve_job_t
*job
; /* New job */
3041 cups_array_t
*ra
; /* Attributes to send in response */
3045 * Validate print job attributes...
3048 if (!valid_job_attributes(client
))
3050 httpFlush(client
->http
);
3055 * Do we have a file to print?
3058 if (httpGetState(client
->http
) == HTTP_STATE_POST_RECV
)
3060 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
,
3061 "Unexpected document data following request.");
3069 if ((job
= create_job(client
)) == NULL
)
3071 respond_ipp(client
, IPP_STATUS_ERROR_BUSY
,
3072 "Currently printing another job.");
3077 * Return the job info...
3080 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
3082 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3083 cupsArrayAdd(ra
, "job-id");
3084 cupsArrayAdd(ra
, "job-state");
3085 cupsArrayAdd(ra
, "job-state-message");
3086 cupsArrayAdd(ra
, "job-state-reasons");
3087 cupsArrayAdd(ra
, "job-uri");
3089 copy_job_attributes(client
, job
, ra
);
3090 cupsArrayDelete(ra
);
3095 * 'ipp_get_job_attributes()' - Get the attributes for a job object.
3099 ipp_get_job_attributes(
3100 ippeve_client_t
*client
) /* I - Client */
3102 ippeve_job_t
*job
; /* Job */
3103 cups_array_t
*ra
; /* requested-attributes */
3106 if ((job
= find_job(client
)) == NULL
)
3108 respond_ipp(client
, IPP_STATUS_ERROR_NOT_FOUND
, "Job not found.");
3112 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
3114 ra
= ippCreateRequestedArray(client
->request
);
3115 copy_job_attributes(client
, job
, ra
);
3116 cupsArrayDelete(ra
);
3121 * 'ipp_get_jobs()' - Get a list of job objects.
3125 ipp_get_jobs(ippeve_client_t
*client
) /* I - Client */
3127 ipp_attribute_t
*attr
; /* Current attribute */
3128 const char *which_jobs
= NULL
;
3129 /* which-jobs values */
3130 int job_comparison
; /* Job comparison */
3131 ipp_jstate_t job_state
; /* job-state value */
3132 int first_job_id
, /* First job ID */
3133 limit
, /* Maximum number of jobs to return */
3134 count
; /* Number of jobs that match */
3135 const char *username
; /* Username */
3136 ippeve_job_t
*job
; /* Current job pointer */
3137 cups_array_t
*ra
; /* Requested attributes array */
3141 * See if the "which-jobs" attribute have been specified...
3144 if ((attr
= ippFindAttribute(client
->request
, "which-jobs",
3145 IPP_TAG_KEYWORD
)) != NULL
)
3147 which_jobs
= ippGetString(attr
, 0, NULL
);
3148 fprintf(stderr
, "%s Get-Jobs which-jobs=%s", client
->hostname
, which_jobs
);
3151 if (!which_jobs
|| !strcmp(which_jobs
, "not-completed"))
3153 job_comparison
= -1;
3154 job_state
= IPP_JSTATE_STOPPED
;
3156 else if (!strcmp(which_jobs
, "completed"))
3159 job_state
= IPP_JSTATE_CANCELED
;
3161 else if (!strcmp(which_jobs
, "aborted"))
3164 job_state
= IPP_JSTATE_ABORTED
;
3166 else if (!strcmp(which_jobs
, "all"))
3169 job_state
= IPP_JSTATE_PENDING
;
3171 else if (!strcmp(which_jobs
, "canceled"))
3174 job_state
= IPP_JSTATE_CANCELED
;
3176 else if (!strcmp(which_jobs
, "pending"))
3179 job_state
= IPP_JSTATE_PENDING
;
3181 else if (!strcmp(which_jobs
, "pending-held"))
3184 job_state
= IPP_JSTATE_HELD
;
3186 else if (!strcmp(which_jobs
, "processing"))
3189 job_state
= IPP_JSTATE_PROCESSING
;
3191 else if (!strcmp(which_jobs
, "processing-stopped"))
3194 job_state
= IPP_JSTATE_STOPPED
;
3198 respond_ipp(client
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
,
3199 "The which-jobs value \"%s\" is not supported.", which_jobs
);
3200 ippAddString(client
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
3201 "which-jobs", NULL
, which_jobs
);
3206 * See if they want to limit the number of jobs reported...
3209 if ((attr
= ippFindAttribute(client
->request
, "limit",
3210 IPP_TAG_INTEGER
)) != NULL
)
3212 limit
= ippGetInteger(attr
, 0);
3214 fprintf(stderr
, "%s Get-Jobs limit=%d", client
->hostname
, limit
);
3219 if ((attr
= ippFindAttribute(client
->request
, "first-job-id",
3220 IPP_TAG_INTEGER
)) != NULL
)
3222 first_job_id
= ippGetInteger(attr
, 0);
3224 fprintf(stderr
, "%s Get-Jobs first-job-id=%d", client
->hostname
, first_job_id
);
3230 * See if we only want to see jobs for a specific user...
3235 if ((attr
= ippFindAttribute(client
->request
, "my-jobs",
3236 IPP_TAG_BOOLEAN
)) != NULL
)
3238 int my_jobs
= ippGetBoolean(attr
, 0);
3240 fprintf(stderr
, "%s Get-Jobs my-jobs=%s\n", client
->hostname
, my_jobs
? "true" : "false");
3244 if ((attr
= ippFindAttribute(client
->request
, "requesting-user-name",
3245 IPP_TAG_NAME
)) == NULL
)
3247 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
,
3248 "Need requesting-user-name with my-jobs.");
3252 username
= ippGetString(attr
, 0, NULL
);
3254 fprintf(stderr
, "%s Get-Jobs requesting-user-name=\"%s\"\n", client
->hostname
, username
);
3259 * OK, build a list of jobs for this printer...
3262 ra
= ippCreateRequestedArray(client
->request
);
3264 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
3266 _cupsRWLockRead(&(client
->printer
->rwlock
));
3268 for (count
= 0, job
= (ippeve_job_t
*)cupsArrayFirst(client
->printer
->jobs
);
3269 (limit
<= 0 || count
< limit
) && job
;
3270 job
= (ippeve_job_t
*)cupsArrayNext(client
->printer
->jobs
))
3273 * Filter out jobs that don't match...
3276 if ((job_comparison
< 0 && job
->state
> job_state
) ||
3277 (job_comparison
== 0 && job
->state
!= job_state
) ||
3278 (job_comparison
> 0 && job
->state
< job_state
) ||
3279 job
->id
< first_job_id
||
3280 (username
&& job
->username
&&
3281 strcasecmp(username
, job
->username
)))
3285 ippAddSeparator(client
->response
);
3288 copy_job_attributes(client
, job
, ra
);
3291 cupsArrayDelete(ra
);
3293 _cupsRWUnlock(&(client
->printer
->rwlock
));
3298 * 'ipp_get_printer_attributes()' - Get the attributes for a printer object.
3302 ipp_get_printer_attributes(
3303 ippeve_client_t
*client
) /* I - Client */
3305 cups_array_t
*ra
; /* Requested attributes array */
3306 ippeve_printer_t
*printer
; /* Printer */
3310 * Send the attributes...
3313 ra
= ippCreateRequestedArray(client
->request
);
3314 printer
= client
->printer
;
3316 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
3318 _cupsRWLockRead(&(printer
->rwlock
));
3320 copy_attributes(client
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
,
3321 IPP_TAG_CUPS_CONST
);
3323 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-date-time"))
3324 ippAddDate(client
->response
, IPP_TAG_PRINTER
, "printer-config-change-date-time", ippTimeToDate(printer
->config_time
));
3326 if (!ra
|| cupsArrayFind(ra
, "printer-config-change-time"))
3327 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "printer-config-change-time", (int)(printer
->config_time
- printer
->start_time
));
3329 if (!ra
|| cupsArrayFind(ra
, "printer-current-time"))
3330 ippAddDate(client
->response
, IPP_TAG_PRINTER
, "printer-current-time", ippTimeToDate(time(NULL
)));
3333 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
3334 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state", (int)printer
->state
);
3336 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-date-time"))
3337 ippAddDate(client
->response
, IPP_TAG_PRINTER
, "printer-state-change-date-time", ippTimeToDate(printer
->state_time
));
3339 if (!ra
|| cupsArrayFind(ra
, "printer-state-change-time"))
3340 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "printer-state-change-time", (int)(printer
->state_time
- printer
->start_time
));
3342 if (!ra
|| cupsArrayFind(ra
, "printer-state-message"))
3344 static const char * const messages
[] = { "Idle.", "Printing.", "Stopped." };
3346 ippAddString(client
->response
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_TEXT
), "printer-state-message", NULL
, messages
[printer
->state
- IPP_PSTATE_IDLE
]);
3349 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
3351 if (printer
->state_reasons
== IPPEVE_PREASON_NONE
)
3353 ippAddString(client
->response
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "printer-state-reasons", NULL
, "none");
3357 ipp_attribute_t
*attr
= NULL
; /* printer-state-reasons */
3358 ippeve_preason_t bit
; /* Reason bit */
3359 int i
; /* Looping var */
3360 char reason
[32]; /* Reason string */
3362 for (i
= 0, bit
= 1; i
< (int)(sizeof(ippeve_preason_strings
) / sizeof(ippeve_preason_strings
[0])); i
++, bit
*= 2)
3364 if (printer
->state_reasons
& bit
)
3366 snprintf(reason
, sizeof(reason
), "%s-%s", ippeve_preason_strings
[i
], printer
->state
== IPP_PSTATE_IDLE
? "report" : printer
->state
== IPP_PSTATE_PROCESSING
? "warning" : "error");
3368 ippSetString(client
->response
, &attr
, ippGetCount(attr
), reason
);
3370 attr
= ippAddString(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
, reason
);
3376 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
3377 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "printer-up-time", (int)(time(NULL
) - printer
->start_time
));
3379 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
3380 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "queued-job-count", printer
->active_job
&& printer
->active_job
->state
< IPP_JSTATE_CANCELED
);
3382 _cupsRWUnlock(&(printer
->rwlock
));
3384 cupsArrayDelete(ra
);
3389 * 'ipp_identify_printer()' - Beep or display a message.
3393 ipp_identify_printer(
3394 ippeve_client_t
*client
) /* I - Client */
3396 ipp_attribute_t
*actions
, /* identify-actions */
3397 *message
; /* message */
3400 actions
= ippFindAttribute(client
->request
, "identify-actions", IPP_TAG_KEYWORD
);
3401 message
= ippFindAttribute(client
->request
, "message", IPP_TAG_TEXT
);
3403 if (!actions
|| ippContainsString(actions
, "sound"))
3409 if (ippContainsString(actions
, "display"))
3410 printf("IDENTIFY from %s: %s\n", client
->hostname
, message
? ippGetString(message
, 0, NULL
) : "No message supplied");
3412 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
3417 * 'ipp_print_job()' - Create a job object with an attached document.
3421 ipp_print_job(ippeve_client_t
*client
) /* I - Client */
3423 ippeve_job_t
*job
; /* New job */
3427 * Validate print job attributes...
3430 if (!valid_job_attributes(client
))
3432 httpFlush(client
->http
);
3437 * Do we have a file to print?
3440 if (httpGetState(client
->http
) == HTTP_STATE_POST_SEND
)
3442 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "No file in request.");
3450 if ((job
= create_job(client
)) == NULL
)
3452 respond_ipp(client
, IPP_STATUS_ERROR_BUSY
, "Currently printing another job.");
3457 * Then finish getting the document data and process things...
3460 finish_document_data(client
, job
);
3465 * 'ipp_print_uri()' - Create a job object with a referenced document.
3469 ipp_print_uri(ippeve_client_t
*client
) /* I - Client */
3471 ippeve_job_t
*job
; /* New job */
3475 * Validate print job attributes...
3478 if (!valid_job_attributes(client
))
3480 httpFlush(client
->http
);
3488 if ((job
= create_job(client
)) == NULL
)
3490 respond_ipp(client
, IPP_STATUS_ERROR_BUSY
, "Currently printing another job.");
3495 * Then finish getting the document data and process things...
3498 finish_document_uri(client
, job
);
3503 * 'ipp_send_document()' - Add an attached document to a job object created with
3509 ippeve_client_t
*client
) /* I - Client */
3511 ippeve_job_t
*job
; /* Job information */
3512 ipp_attribute_t
*attr
; /* Current attribute */
3519 if ((job
= find_job(client
)) == NULL
)
3521 respond_ipp(client
, IPP_STATUS_ERROR_NOT_FOUND
, "Job does not exist.");
3522 httpFlush(client
->http
);
3527 * See if we already have a document for this job or the job has already
3528 * in a non-pending state...
3531 if (job
->state
> IPP_JSTATE_HELD
)
3533 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
, "Job is not in a pending state.");
3534 httpFlush(client
->http
);
3537 else if (job
->filename
|| job
->fd
>= 0)
3539 respond_ipp(client
, IPP_STATUS_ERROR_MULTIPLE_JOBS_NOT_SUPPORTED
, "Multiple document jobs are not supported.");
3540 httpFlush(client
->http
);
3545 * Make sure we have the "last-document" operation attribute...
3548 if ((attr
= ippFindAttribute(client
->request
, "last-document", IPP_TAG_ZERO
)) == NULL
)
3550 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "Missing required last-document attribute.");
3551 httpFlush(client
->http
);
3554 else if (ippGetGroupTag(attr
) != IPP_TAG_OPERATION
)
3556 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "The last-document attribute is not in the operation group.");
3557 httpFlush(client
->http
);
3560 else if (ippGetValueTag(attr
) != IPP_TAG_BOOLEAN
|| ippGetCount(attr
) != 1 || !ippGetBoolean(attr
, 0))
3562 respond_unsupported(client
, attr
);
3563 httpFlush(client
->http
);
3568 * Validate document attributes...
3571 if (!valid_doc_attributes(client
))
3573 httpFlush(client
->http
);
3578 * Then finish getting the document data and process things...
3581 _cupsRWLockWrite(&(client
->printer
->rwlock
));
3583 copy_attributes(job
->attrs
, client
->request
, NULL
, IPP_TAG_JOB
, 0);
3585 if ((attr
= ippFindAttribute(job
->attrs
, "document-format-detected", IPP_TAG_MIMETYPE
)) != NULL
)
3586 job
->format
= ippGetString(attr
, 0, NULL
);
3587 else if ((attr
= ippFindAttribute(job
->attrs
, "document-format-supplied", IPP_TAG_MIMETYPE
)) != NULL
)
3588 job
->format
= ippGetString(attr
, 0, NULL
);
3590 job
->format
= "application/octet-stream";
3592 _cupsRWUnlock(&(client
->printer
->rwlock
));
3594 finish_document_data(client
, job
);
3599 * 'ipp_send_uri()' - Add a referenced document to a job object created with
3604 ipp_send_uri(ippeve_client_t
*client
) /* I - Client */
3606 ippeve_job_t
*job
; /* Job information */
3607 ipp_attribute_t
*attr
; /* Current attribute */
3614 if ((job
= find_job(client
)) == NULL
)
3616 respond_ipp(client
, IPP_STATUS_ERROR_NOT_FOUND
, "Job does not exist.");
3617 httpFlush(client
->http
);
3622 * See if we already have a document for this job or the job has already
3623 * in a non-pending state...
3626 if (job
->state
> IPP_JSTATE_HELD
)
3628 respond_ipp(client
, IPP_STATUS_ERROR_NOT_POSSIBLE
, "Job is not in a pending state.");
3629 httpFlush(client
->http
);
3632 else if (job
->filename
|| job
->fd
>= 0)
3634 respond_ipp(client
, IPP_STATUS_ERROR_MULTIPLE_JOBS_NOT_SUPPORTED
, "Multiple document jobs are not supported.");
3635 httpFlush(client
->http
);
3639 if ((attr
= ippFindAttribute(client
->request
, "last-document", IPP_TAG_ZERO
)) == NULL
)
3641 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "Missing required last-document attribute.");
3642 httpFlush(client
->http
);
3645 else if (ippGetGroupTag(attr
) != IPP_TAG_OPERATION
)
3647 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "The last-document attribute is not in the operation group.");
3648 httpFlush(client
->http
);
3651 else if (ippGetValueTag(attr
) != IPP_TAG_BOOLEAN
|| ippGetCount(attr
) != 1 || !ippGetBoolean(attr
, 0))
3653 respond_unsupported(client
, attr
);
3654 httpFlush(client
->http
);
3659 * Validate document attributes...
3662 if (!valid_doc_attributes(client
))
3664 httpFlush(client
->http
);
3669 * Then finish getting the document data and process things...
3672 _cupsRWLockWrite(&(client
->printer
->rwlock
));
3674 copy_attributes(job
->attrs
, client
->request
, NULL
, IPP_TAG_JOB
, 0);
3676 if ((attr
= ippFindAttribute(job
->attrs
, "document-format-detected", IPP_TAG_MIMETYPE
)) != NULL
)
3677 job
->format
= ippGetString(attr
, 0, NULL
);
3678 else if ((attr
= ippFindAttribute(job
->attrs
, "document-format-supplied", IPP_TAG_MIMETYPE
)) != NULL
)
3679 job
->format
= ippGetString(attr
, 0, NULL
);
3681 job
->format
= "application/octet-stream";
3683 _cupsRWUnlock(&(client
->printer
->rwlock
));
3685 finish_document_uri(client
, job
);
3690 * 'ipp_validate_job()' - Validate job creation attributes.
3694 ipp_validate_job(ippeve_client_t
*client
) /* I - Client */
3696 if (valid_job_attributes(client
))
3697 respond_ipp(client
, IPP_STATUS_OK
, NULL
);
3702 * 'ippserver_attr_cb()' - Determine whether an attribute should be loaded.
3705 static int /* O - 1 to use, 0 to ignore */
3707 _ipp_file_t
*f
, /* I - IPP file */
3708 void *user_data
, /* I - User data pointer (unused) */
3709 const char *attr
) /* I - Attribute name */
3711 int i
, /* Current element */
3712 result
; /* Result of comparison */
3713 static const char * const ignored
[] =
3714 { /* Ignored attributes */
3715 "attributes-charset",
3716 "attributes-natural-language",
3717 "charset-configured",
3718 "charset-supported",
3719 "device-service-count",
3721 "document-format-varying-attributes",
3722 "generated-natural-language-supported",
3723 "identify-actions-default",
3724 "identify-actions-supported",
3725 "ipp-features-supported",
3726 "ipp-versions-supproted",
3727 "ippget-event-life",
3728 "job-hold-until-supported",
3729 "job-hold-until-time-supported",
3730 "job-ids-supported",
3731 "job-k-octets-supported",
3732 "job-settable-attributes-supported",
3733 "multiple-document-jobs-supported",
3734 "multiple-operation-time-out",
3735 "multiple-operation-time-out-action",
3736 "natural-language-configured",
3737 "notify-attributes-supported",
3738 "notify-events-default",
3739 "notify-events-supported",
3740 "notify-lease-duration-default",
3741 "notify-lease-duration-supported",
3742 "notify-max-events-supported",
3743 "notify-pull-method-supported",
3744 "operations-supported",
3746 "printer-alert-description",
3747 "printer-camera-image-uri",
3748 "printer-charge-info",
3749 "printer-charge-info-uri",
3750 "printer-config-change-date-time",
3751 "printer-config-change-time",
3752 "printer-current-time",
3753 "printer-detailed-status-messages",
3754 "printer-dns-sd-name",
3755 "printer-fax-log-uri",
3756 "printer-get-attributes-supported",
3760 "printer-is-accepting-jobs",
3761 "printer-message-date-time",
3762 "printer-message-from-operator",
3763 "printer-message-time",
3764 "printer-more-info",
3765 "printer-service-type",
3766 "printer-settable-attributes-supported",
3768 "printer-state-message",
3769 "printer-state-reasons",
3770 "printer-static-resource-directory-uri",
3771 "printer-static-resource-k-octets-free",
3772 "printer-static-resource-k-octets-supported",
3773 "printer-strings-languages-supported",
3774 "printer-strings-uri",
3775 "printer-supply-info-uri",
3777 "printer-uri-supported",
3778 "printer-xri-supported",
3780 "reference-uri-scheme-supported",
3781 "uri-authentication-supported",
3782 "uri-security-supported",
3783 "which-jobs-supported",
3784 "xri-authentication-supported",
3785 "xri-security-supported",
3786 "xri-uri-scheme-supported"
3793 for (i
= 0, result
= 1; i
< (int)(sizeof(ignored
) / sizeof(ignored
[0])); i
++)
3795 if ((result
= strcmp(attr
, ignored
[i
])) <= 0)
3799 return (result
!= 0);
3804 * 'ippserver_error_cb()' - Log an error message.
3807 static int /* O - 1 to continue, 0 to stop */
3809 _ipp_file_t
*f
, /* I - IPP file data */
3810 void *user_data
, /* I - User data pointer (unused) */
3811 const char *error
) /* I - Error message */
3816 _cupsLangPrintf(stderr
, "%s\n", error
);
3823 * 'ippserver_token_cb()' - Process ippserver-specific config file tokens.
3826 static int /* O - 1 to continue, 0 to stop */
3828 _ipp_file_t
*f
, /* I - IPP file data */
3829 _ipp_vars_t
*vars
, /* I - IPP variables */
3830 void *user_data
, /* I - User data pointer (unused) */
3831 const char *token
) /* I - Current token */
3839 * NULL token means do the initial setup - create an empty IPP message and
3843 f
->attrs
= ippNew();
3844 f
->group_tag
= IPP_TAG_PRINTER
;
3848 _cupsLangPrintf(stderr
, _("Unknown directive \"%s\" on line %d of \"%s\" ignored."), token
, f
->linenum
, f
->filename
);
3856 * 'load_ippserver_attributes()' - Load IPP attributes from an ippserver file.
3859 static ipp_t
* /* O - IPP attributes or `NULL` on error */
3860 load_ippserver_attributes(
3861 const char *servername
, /* I - Server name or `NULL` for default */
3862 int serverport
, /* I - Server port number */
3863 const char *filename
, /* I - ippserver attribute filename */
3864 cups_array_t
*docformats
) /* I - document-format-supported values */
3866 ipp_t
*attrs
; /* IPP attributes */
3867 _ipp_vars_t vars
; /* IPP variables */
3868 char temp
[256]; /* Temporary string */
3871 (void)docformats
; /* for now */
3874 * Setup callbacks and variables for the printer configuration file...
3876 * The following additional variables are supported:
3878 * - SERVERNAME: The host name of the server.
3879 * - SERVERPORT: The default port of the server.
3882 _ippVarsInit(&vars
, (_ipp_fattr_cb_t
)ippserver_attr_cb
, (_ipp_ferror_cb_t
)ippserver_error_cb
, (_ipp_ftoken_cb_t
)ippserver_token_cb
);
3886 _ippVarsSet(&vars
, "SERVERNAME", servername
);
3890 httpGetHostname(NULL
, temp
, sizeof(temp
));
3891 _ippVarsSet(&vars
, "SERVERNAME", temp
);
3894 snprintf(temp
, sizeof(temp
), "%d", serverport
);
3895 _ippVarsSet(&vars
, "SERVERPORT", temp
);
3898 * Load attributes and values for the printer...
3901 attrs
= _ippFileParse(&vars
, filename
, NULL
);
3904 * Free memory and return...
3907 _ippVarsDeinit(&vars
);
3914 * 'load_legacy_attributes()' - Load IPP attributes using the old ippserver
3918 static ipp_t
* /* O - IPP attributes or `NULL` on error */
3919 load_legacy_attributes(
3920 const char *make
, /* I - Manufacturer name */
3921 const char *model
, /* I - Model name */
3922 int ppm
, /* I - pages-per-minute */
3923 int ppm_color
, /* I - pages-per-minute-color */
3924 int duplex
, /* I - Duplex support? */
3925 cups_array_t
*docformats
) /* I - document-format-supported values */
3927 int i
; /* Looping var */
3928 ipp_t
*attrs
, /* IPP attributes */
3929 *col
; /* Collection value */
3930 ipp_attribute_t
*attr
; /* Current attribute */
3931 char device_id
[1024],/* printer-device-id */
3932 *ptr
, /* Pointer into device ID */
3933 make_model
[128];/* printer-make-and-model */
3934 const char *format
, /* Current document format */
3935 *prefix
; /* Prefix for device ID */
3936 int num_media
; /* Number of media */
3937 const char * const *media
; /* List of media */
3938 int num_ready
; /* Number of loaded media */
3939 const char * const *ready
; /* List of loaded media */
3940 pwg_media_t
*pwg
; /* PWG media size information */
3941 static const char * const media_supported
[] =
3942 { /* media-supported values */
3943 "na_letter_8.5x11in", /* Letter */
3944 "na_legal_8.5x14in", /* Legal */
3945 "iso_a4_210x297mm", /* A4 */
3946 "na_number-10_4.125x9.5in", /* #10 Envelope */
3947 "iso_dl_110x220mm" /* DL Envelope */
3949 static const char * const media_supported_color
[] =
3950 { /* media-supported values */
3951 "na_letter_8.5x11in", /* Letter */
3952 "na_legal_8.5x14in", /* Legal */
3953 "iso_a4_210x297mm", /* A4 */
3954 "na_number-10_4.125x9.5in", /* #10 Envelope */
3955 "iso_dl_110x220mm", /* DL Envelope */
3956 "na_index-3x5_3x5in", /* Photo 3x5 */
3957 "oe_photo-l_3.5x5in", /* Photo L */
3958 "na_index-4x6_4x6in", /* Photo 4x6 */
3959 "iso_a6_105x148mm", /* A6 */
3960 "na_5x7_5x7in" /* Photo 5x7 aka 2L */
3961 "iso_a5_148x210mm", /* A5 */
3963 static const char * const media_ready
[] =
3964 { /* media-ready values */
3965 "na_letter_8.5x11in", /* Letter */
3966 "na_number-10_4.125x9.5in" /* #10 */
3968 static const char * const media_ready_color
[] =
3969 { /* media-ready values */
3970 "na_letter_8.5x11in", /* Letter */
3971 "na_index-4x6_4x6in" /* Photo 4x6 */
3973 static const char * const media_source_supported
[] =
3974 { /* media-source-supported values */
3978 "by-pass-tray" /* AKA multi-purpose tray */
3980 static const char * const media_source_supported_color
[] =
3981 { /* media-source-supported values */
3986 static const char * const media_type_supported
[] =
3987 { /* media-type-supported values */
3994 "stationery-letterhead",
3997 static const char * const media_type_supported_color
[] =
3998 { /* media-type-supported values */
4005 "stationery-letterhead",
4007 "photographic-glossy",
4008 "photographic-high-gloss",
4009 "photographic-matte",
4010 "photographic-satin",
4011 "photographic-semi-gloss"
4013 static const int media_bottom_margin_supported
[] =
4014 { /* media-bottom-margin-supported values */
4017 static const int media_bottom_margin_supported_color
[] =
4018 { /* media-bottom/top-margin-supported values */
4020 1168 /* 0.46" (common HP inkjet bottom margin) */
4022 static const int media_lr_margin_supported
[] =
4023 { /* media-left/right-margin-supported values */
4024 340, /* 3.4mm (historical HP PCL A4 margin) */
4027 static const int media_lr_margin_supported_color
[] =
4028 { /* media-left/right-margin-supported values */
4030 340, /* 3.4mm (historical HP PCL A4 margin) */
4033 static const int media_top_margin_supported
[] =
4034 { /* media-top-margin-supported values */
4037 static const int media_top_margin_supported_color
[] =
4038 { /* media-top/top-margin-supported values */
4040 102 /* 0.04" (common HP inkjet top margin */
4042 static const int orientation_requested_supported
[4] =
4043 { /* orientation-requested-supported values */
4044 IPP_ORIENT_PORTRAIT
,
4045 IPP_ORIENT_LANDSCAPE
,
4046 IPP_ORIENT_REVERSE_LANDSCAPE
,
4047 IPP_ORIENT_REVERSE_PORTRAIT
4049 static const char * const overrides_supported
[] =
4050 { /* overrides-supported values */
4054 "orientation-requested",
4057 static const char * const print_color_mode_supported
[] =
4058 { /* print-color-mode-supported values */
4061 static const char * const print_color_mode_supported_color
[] =
4062 { /* print-color-mode-supported values */
4067 static const int print_quality_supported
[] =
4068 { /* print-quality-supported values */
4073 static const char * const printer_input_tray
[] =
4074 { /* printer-input-tray values */
4075 "type=sheetFeedAutoRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=-2;level=-2;status=0;name=auto",
4076 "type=sheetFeedAutoRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=250;level=100;status=0;name=main",
4077 "type=sheetFeedManual;mediafeed=0;mediaxfeed=0;maxcapacity=1;level=-2;status=0;name=manual",
4078 "type=sheetFeedAutoNonRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=25;level=-2;status=0;name=by-pass-tray"
4080 static const char * const printer_input_tray_color
[] =
4081 { /* printer-input-tray values */
4082 "type=sheetFeedAutoRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=-2;level=-2;status=0;name=auto",
4083 "type=sheetFeedAutoRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=250;level=-2;status=0;name=main",
4084 "type=sheetFeedAutoRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=25;level=-2;status=0;name=photo"
4086 static const char * const printer_supply
[] =
4087 { /* printer-supply values */
4088 "index=1;class=receptacleThatIsFilled;type=wasteToner;unit=percent;"
4089 "maxcapacity=100;level=25;colorantname=unknown;",
4090 "index=2;class=supplyThatIsConsumed;type=toner;unit=percent;"
4091 "maxcapacity=100;level=75;colorantname=black;"
4093 static const char * const printer_supply_color
[] =
4094 { /* printer-supply values */
4095 "index=1;class=receptacleThatIsFilled;type=wasteInk;unit=percent;"
4096 "maxcapacity=100;level=25;colorantname=unknown;",
4097 "index=2;class=supplyThatIsConsumed;type=ink;unit=percent;"
4098 "maxcapacity=100;level=75;colorantname=black;",
4099 "index=3;class=supplyThatIsConsumed;type=ink;unit=percent;"
4100 "maxcapacity=100;level=50;colorantname=cyan;",
4101 "index=4;class=supplyThatIsConsumed;type=ink;unit=percent;"
4102 "maxcapacity=100;level=33;colorantname=magenta;",
4103 "index=5;class=supplyThatIsConsumed;type=ink;unit=percent;"
4104 "maxcapacity=100;level=67;colorantname=yellow;"
4106 static const char * const printer_supply_description
[] =
4107 { /* printer-supply-description values */
4111 static const char * const printer_supply_description_color
[] =
4112 { /* printer-supply-description values */
4119 static const int pwg_raster_document_resolution_supported
[] =
4124 static const char * const pwg_raster_document_type_supported
[] =
4129 static const char * const pwg_raster_document_type_supported_color
[] =
4136 static const char * const sides_supported
[] =
4137 { /* sides-supported values */
4139 "two-sided-long-edge",
4140 "two-sided-short-edge"
4142 static const char * const urf_supported
[] =
4143 { /* urf-supported values */
4151 static const char * const urf_supported_color
[] =
4152 { /* urf-supported values */
4155 "MT1-2-3-4-5-6-8-9-10-11-12-13",
4161 static const char * const urf_supported_color_duplex
[] =
4162 { /* urf-supported values */
4165 "MT1-2-3-4-5-6-8-9-10-11-12-13",
4172 static const char * const urf_supported_duplex
[] =
4173 { /* urf-supported values */
4188 num_media
= (int)(sizeof(media_supported_color
) / sizeof(media_supported_color
[0]));
4189 media
= media_supported_color
;
4190 num_ready
= (int)(sizeof(media_ready_color
) / sizeof(media_ready_color
[0]));
4191 ready
= media_ready_color
;
4195 num_media
= (int)(sizeof(media_supported
) / sizeof(media_supported
[0]));
4196 media
= media_supported
;
4197 num_ready
= (int)(sizeof(media_ready
) / sizeof(media_ready
[0]));
4198 ready
= media_ready
;
4201 /* color-supported */
4202 ippAddBoolean(attrs
, IPP_TAG_PRINTER
, "color-supported", ppm_color
> 0);
4204 /* copies-default */
4205 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default", 1);
4207 /* copies-supported */
4208 ippAddRange(attrs
, IPP_TAG_PRINTER
, "copies-supported", 1, (cupsArrayFind(docformats
, (void *)"application/pdf") != NULL
|| cupsArrayFind(docformats
, (void *)"image/jpeg") != NULL
) ? 999 : 1);
4210 /* document-password-supported */
4211 if (cupsArrayFind(docformats
, (void *)"application/pdf"))
4212 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "document-password-supported", 1023);
4214 /* finishings-default */
4215 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "finishings-default", IPP_FINISHINGS_NONE
);
4217 /* finishings-supported */
4218 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "finishings-supported", IPP_FINISHINGS_NONE
);
4220 /* media-bottom-margin-supported */
4222 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-bottom-margin-supported", (int)(sizeof(media_bottom_margin_supported
) / sizeof(media_bottom_margin_supported
[0])), media_bottom_margin_supported
);
4224 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-bottom-margin-supported", (int)(sizeof(media_bottom_margin_supported_color
) / sizeof(media_bottom_margin_supported_color
[0])), media_bottom_margin_supported_color
);
4226 /* media-col-database and media-col-default */
4227 attr
= ippAddCollections(attrs
, IPP_TAG_PRINTER
, "media-col-database", num_media
, NULL
);
4228 for (i
= 0; i
< num_media
; i
++)
4230 int bottom
, left
, /* media-xxx-margins */
4232 const char *source
; /* media-source, if any */
4234 pwg
= pwgMediaForPWG(media
[i
]);
4236 if (pwg
->width
< 21000 && pwg
->length
< 21000)
4238 source
= "photo"; /* Photo size media from photo tray */
4239 bottom
= /* Borderless margins */
4244 else if (pwg
->width
< 21000)
4246 source
= "by-pass-tray"; /* Envelopes from multi-purpose tray */
4247 bottom
= ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0];
4248 left
= /* Left/right margins are standard */
4249 right
= media_lr_margin_supported
[1];
4250 top
= ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0];
4252 else if (pwg
->width
== 21000)
4254 source
= NULL
; /* A4 from any tray */
4255 bottom
= ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0];
4256 left
= /* Left/right margins are reduced */
4257 right
= media_lr_margin_supported
[0];
4258 top
= ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0];
4262 source
= NULL
; /* Other size media from any tray */
4263 bottom
= ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0];
4264 left
= /* Left/right margins are standard */
4265 right
= media_lr_margin_supported
[1];
4266 top
= ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0];
4269 col
= create_media_col(media
[i
], source
, NULL
, pwg
->width
, pwg
->length
, bottom
, left
, right
, top
);
4270 ippSetCollection(attrs
, &attr
, i
, col
);
4275 /* media-col-default */
4276 pwg
= pwgMediaForPWG(ready
[0]);
4278 if (pwg
->width
== 21000)
4279 col
= create_media_col(ready
[0], "main", "stationery", pwg
->width
, pwg
->length
, ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0], media_lr_margin_supported
[0], media_lr_margin_supported
[0], ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0]);
4281 col
= create_media_col(ready
[0], "main", "stationery", pwg
->width
, pwg
->length
, ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0], media_lr_margin_supported
[1], media_lr_margin_supported
[1], ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0]);
4283 ippAddCollection(attrs
, IPP_TAG_PRINTER
, "media-col-default", col
);
4287 /* media-col-ready */
4288 attr
= ippAddCollections(attrs
, IPP_TAG_PRINTER
, "media-col-ready", num_ready
, NULL
);
4289 for (i
= 0; i
< num_ready
; i
++)
4291 int bottom
, left
, /* media-xxx-margins */
4293 const char *source
, /* media-source */
4294 *type
; /* media-type */
4296 pwg
= pwgMediaForPWG(ready
[i
]);
4298 if (pwg
->width
< 21000 && pwg
->length
< 21000)
4300 source
= "photo"; /* Photo size media from photo tray */
4301 type
= "photographic-glossy"; /* Glossy photo paper */
4302 bottom
= /* Borderless margins */
4307 else if (pwg
->width
< 21000)
4309 source
= "by-pass-tray"; /* Envelopes from multi-purpose tray */
4310 type
= "envelope"; /* Envelope */
4311 bottom
= ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0];
4312 left
= /* Left/right margins are standard */
4313 right
= media_lr_margin_supported
[1];
4314 top
= ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0];
4316 else if (pwg
->width
== 21000)
4318 source
= "main"; /* A4 from main tray */
4319 type
= "stationery"; /* Plain paper */
4320 bottom
= ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0];
4321 left
= /* Left/right margins are reduced */
4322 right
= media_lr_margin_supported
[0];
4323 top
= ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0];
4327 source
= "main"; /* A4 from main tray */
4328 type
= "stationery"; /* Plain paper */
4329 bottom
= ppm_color
> 0 ? media_bottom_margin_supported_color
[1] : media_bottom_margin_supported
[0];
4330 left
= /* Left/right margins are standard */
4331 right
= media_lr_margin_supported
[1];
4332 top
= ppm_color
> 0 ? media_top_margin_supported_color
[1] : media_top_margin_supported
[0];
4335 col
= create_media_col(ready
[i
], source
, type
, pwg
->width
, pwg
->length
, bottom
, left
, right
, top
);
4336 ippSetCollection(attrs
, &attr
, i
, col
);
4341 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-default", NULL
, media
[0]);
4343 /* media-left/right-margin-supported */
4346 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-left-margin-supported", (int)(sizeof(media_lr_margin_supported_color
) / sizeof(media_lr_margin_supported_color
[0])), media_lr_margin_supported_color
);
4347 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-right-margin-supported", (int)(sizeof(media_lr_margin_supported_color
) / sizeof(media_lr_margin_supported_color
[0])), media_lr_margin_supported_color
);
4351 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-left-margin-supported", (int)(sizeof(media_lr_margin_supported
) / sizeof(media_lr_margin_supported
[0])), media_lr_margin_supported
);
4352 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-right-margin-supported", (int)(sizeof(media_lr_margin_supported
) / sizeof(media_lr_margin_supported
[0])), media_lr_margin_supported
);
4356 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-ready", num_ready
, NULL
, ready
);
4358 /* media-supported */
4359 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-supported", num_media
, NULL
, media
);
4361 /* media-size-supported */
4362 attr
= ippAddCollections(attrs
, IPP_TAG_PRINTER
, "media-size-supported", num_media
, NULL
);
4363 for (i
= 0; i
< num_media
; i
++)
4365 pwg
= pwgMediaForPWG(media
[i
]);
4366 col
= create_media_size(pwg
->width
, pwg
->length
);
4368 ippSetCollection(attrs
, &attr
, i
, col
);
4372 /* media-source-supported */
4374 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-source-supported", (int)(sizeof(media_source_supported_color
) / sizeof(media_source_supported_color
[0])), NULL
, media_source_supported_color
);
4376 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-source-supported", (int)(sizeof(media_source_supported
) / sizeof(media_source_supported
[0])), NULL
, media_source_supported
);
4378 /* media-top-margin-supported */
4380 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-top-margin-supported", (int)(sizeof(media_top_margin_supported
) / sizeof(media_top_margin_supported
[0])), media_top_margin_supported
);
4382 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-top-margin-supported", (int)(sizeof(media_top_margin_supported_color
) / sizeof(media_top_margin_supported_color
[0])), media_top_margin_supported_color
);
4384 /* media-type-supported */
4386 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-type-supported", (int)(sizeof(media_type_supported_color
) / sizeof(media_type_supported_color
[0])), NULL
, media_type_supported_color
);
4388 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-type-supported", (int)(sizeof(media_type_supported
) / sizeof(media_type_supported
[0])), NULL
, media_type_supported
);
4390 /* orientation-requested-default */
4391 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "orientation-requested-default", IPP_ORIENT_PORTRAIT
);
4393 /* orientation-requested-supported */
4394 if (cupsArrayFind(docformats
, (void *)"application/pdf") || cupsArrayFind(docformats
, (void *)"image/jpeg"))
4395 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "orientation-requested-supported", (int)(sizeof(orientation_requested_supported
) / sizeof(orientation_requested_supported
[0])), orientation_requested_supported
);
4397 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "orientation-requested-supported", IPP_ORIENT_PORTRAIT
);
4399 /* output-bin-default */
4401 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "output-bin-default", NULL
, "face-up");
4403 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "output-bin-default", NULL
, "face-down");
4405 /* output-bin-supported */
4407 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "output-bin-supported", NULL
, "face-up");
4409 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "output-bin-supported", NULL
, "face-down");
4411 /* overrides-supported */
4412 if (cupsArrayFind(docformats
, (void *)"application/pdf"))
4413 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "overrides-supported", (int)(sizeof(overrides_supported
) / sizeof(overrides_supported
[0])), NULL
, overrides_supported
);
4415 /* page-ranges-supported */
4416 ippAddBoolean(attrs
, IPP_TAG_PRINTER
, "page-ranges-supported", cupsArrayFind(docformats
, (void *)"application/pdf") != NULL
);
4418 /* pages-per-minute */
4419 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "pages-per-minute", ppm
);
4421 /* pages-per-minute-color */
4423 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "pages-per-minute-color", ppm_color
);
4425 /* print-color-mode-default */
4426 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-color-mode-default", NULL
, ppm_color
> 0 ? "auto" : "monochrome");
4428 /* print-color-mode-supported */
4430 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-color-mode-supported", (int)(sizeof(print_color_mode_supported_color
) / sizeof(print_color_mode_supported_color
[0])), NULL
, print_color_mode_supported_color
);
4432 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-color-mode-supported", (int)(sizeof(print_color_mode_supported
) / sizeof(print_color_mode_supported
[0])), NULL
, print_color_mode_supported
);
4434 /* print-content-optimize-default */
4435 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-content-optimize-default", NULL
, "auto");
4437 /* print-content-optimize-supported */
4438 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-content-optimize-supported", NULL
, "auto");
4440 /* print-quality-default */
4441 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "print-quality-default", IPP_QUALITY_NORMAL
);
4443 /* print-quality-supported */
4444 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "print-quality-supported", (int)(sizeof(print_quality_supported
) / sizeof(print_quality_supported
[0])), print_quality_supported
);
4446 /* print-rendering-intent-default */
4447 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-rendering-intent-default", NULL
, "auto");
4449 /* print-rendering-intent-supported */
4450 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-rendering-intent-supported", NULL
, "auto");
4452 /* printer-device-id */
4453 snprintf(device_id
, sizeof(device_id
), "MFG:%s;MDL:%s;", make
, model
);
4454 ptr
= device_id
+ strlen(device_id
);
4456 for (format
= (const char *)cupsArrayFirst(docformats
); format
; format
= (const char *)cupsArrayNext(docformats
))
4458 if (!strcasecmp(format
, "application/pdf"))
4459 snprintf(ptr
, sizeof(device_id
) - (size_t)(ptr
- device_id
), "%sPDF", prefix
);
4460 else if (!strcasecmp(format
, "application/postscript"))
4461 snprintf(ptr
, sizeof(device_id
) - (size_t)(ptr
- device_id
), "%sPS", prefix
);
4462 else if (!strcasecmp(format
, "application/vnd.hp-PCL"))
4463 snprintf(ptr
, sizeof(device_id
) - (size_t)(ptr
- device_id
), "%sPCL", prefix
);
4464 else if (!strcasecmp(format
, "image/jpeg"))
4465 snprintf(ptr
, sizeof(device_id
) - (size_t)(ptr
- device_id
), "%sJPEG", prefix
);
4466 else if (!strcasecmp(format
, "image/png"))
4467 snprintf(ptr
, sizeof(device_id
) - (size_t)(ptr
- device_id
), "%sPNG", prefix
);
4468 else if (!strcasecmp(format
, "image/pwg-raster"))
4469 snprintf(ptr
, sizeof(device_id
) - (size_t)(ptr
- device_id
), "%sPWG", prefix
);
4470 else if (!strcasecmp(format
, "image/urf"))
4471 snprintf(ptr
, sizeof(device_id
) - (size_t)(ptr
- device_id
), "%sURF", prefix
);
4478 if (ptr
< (device_id
+ sizeof(device_id
) - 1))
4483 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
4485 /* printer-input-tray */
4488 attr
= ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-input-tray", printer_input_tray_color
[0], (int)strlen(printer_input_tray_color
[0]));
4489 for (i
= 1; i
< (int)(sizeof(printer_input_tray_color
) / sizeof(printer_input_tray_color
[0])); i
++)
4490 ippSetOctetString(attrs
, &attr
, i
, printer_input_tray_color
[i
], (int)strlen(printer_input_tray_color
[i
]));
4494 attr
= ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-input-tray", printer_input_tray
[0], (int)strlen(printer_input_tray
[0]));
4495 for (i
= 1; i
< (int)(sizeof(printer_input_tray
) / sizeof(printer_input_tray
[0])); i
++)
4496 ippSetOctetString(attrs
, &attr
, i
, printer_input_tray
[i
], (int)strlen(printer_input_tray
[i
]));
4499 /* printer-make-and-model */
4500 snprintf(make_model
, sizeof(make_model
), "%s %s", make
, model
);
4501 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-make-and-model", NULL
, make_model
);
4503 /* printer-resolution-default */
4504 ippAddResolution(attrs
, IPP_TAG_PRINTER
, "printer-resolution-default", IPP_RES_PER_INCH
, 600, 600);
4506 /* printer-resolution-supported */
4507 ippAddResolution(attrs
, IPP_TAG_PRINTER
, "printer-resolution-supported", IPP_RES_PER_INCH
, 600, 600);
4509 /* printer-supply and printer-supply-description */
4512 attr
= ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-supply", printer_supply_color
[0], (int)strlen(printer_supply_color
[0]));
4513 for (i
= 1; i
< (int)(sizeof(printer_supply_color
) / sizeof(printer_supply_color
[0])); i
++)
4514 ippSetOctetString(attrs
, &attr
, i
, printer_supply_color
[i
], (int)strlen(printer_supply_color
[i
]));
4516 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_TEXT
), "printer-supply-description", (int)(sizeof(printer_supply_description_color
) / sizeof(printer_supply_description_color
[0])), NULL
, printer_supply_description_color
);
4520 attr
= ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-supply", printer_supply
[0], (int)strlen(printer_supply
[0]));
4521 for (i
= 1; i
< (int)(sizeof(printer_supply
) / sizeof(printer_supply
[0])); i
++)
4522 ippSetOctetString(attrs
, &attr
, i
, printer_supply
[i
], (int)strlen(printer_supply
[i
]));
4524 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_TEXT
), "printer-supply-description", (int)(sizeof(printer_supply_description
) / sizeof(printer_supply_description
[0])), NULL
, printer_supply_description
);
4527 /* pwg-raster-document-xxx-supported */
4528 if (cupsArrayFind(docformats
, (void *)"image/pwg-raster"))
4530 ippAddResolutions(attrs
, IPP_TAG_PRINTER
, "pwg-raster-document-resolution-supported", (int)(sizeof(pwg_raster_document_resolution_supported
) / sizeof(pwg_raster_document_resolution_supported
[0])), IPP_RES_PER_INCH
, pwg_raster_document_resolution_supported
, pwg_raster_document_resolution_supported
);
4532 if (ppm_color
> 0 && duplex
)
4533 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pwg-raster-document-sheet-back", NULL
, "rotated");
4535 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pwg-raster-document-sheet-back", NULL
, "normal");
4538 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pwg-raster-document-type-supported", (int)(sizeof(pwg_raster_document_type_supported_color
) / sizeof(pwg_raster_document_type_supported_color
[0])), NULL
, pwg_raster_document_type_supported_color
);
4540 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pwg-raster-document-type-supported", (int)(sizeof(pwg_raster_document_type_supported
) / sizeof(pwg_raster_document_type_supported
[0])), NULL
, pwg_raster_document_type_supported
);
4544 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "sides-default", NULL
, "one-sided");
4546 /* sides-supported */
4548 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "sides-supported", (int)(sizeof(sides_supported
) / sizeof(sides_supported
[0])), NULL
, sides_supported
);
4550 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "sides-supported", NULL
, "one-sided");
4553 if (cupsArrayFind(docformats
, (void *)"image/urf"))
4558 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "urf-supported", (int)(sizeof(urf_supported_color_duplex
) / sizeof(urf_supported_color_duplex
[0])), NULL
, urf_supported_color_duplex
);
4560 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "urf-supported", (int)(sizeof(urf_supported_color
) / sizeof(urf_supported_color
[0])), NULL
, urf_supported_color
);
4564 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "urf-supported", (int)(sizeof(urf_supported_duplex
) / sizeof(urf_supported_duplex
[0])), NULL
, urf_supported_duplex
);
4568 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "urf-supported", (int)(sizeof(urf_supported
) / sizeof(urf_supported
[0])), NULL
, urf_supported
);
4578 * 'load_ppd_attributes()' - Load IPP attributes from a PPD file.
4581 static ipp_t
* /* O - IPP attributes or `NULL` on error */
4582 load_ppd_attributes(
4583 const char *ppdfile
, /* I - PPD filename */
4584 cups_array_t
*docformats
) /* I - document-format-supported values */
4586 int i
, j
; /* Looping vars */
4587 ipp_t
*attrs
; /* Attributes */
4588 ipp_attribute_t
*attr
; /* Current attribute */
4589 ipp_t
*col
; /* Current collection value */
4590 ppd_file_t
*ppd
; /* PPD data */
4591 ppd_attr_t
*ppd_attr
; /* PPD attribute */
4592 ppd_choice_t
*ppd_choice
; /* PPD choice */
4593 ppd_size_t
*ppd_size
; /* Default PPD size */
4594 pwg_size_t
*pwg_size
, /* Current PWG size */
4595 *default_size
= NULL
; /* Default PWG size */
4596 const char *default_source
= NULL
, /* Default media source */
4597 *default_type
= NULL
; /* Default media type */
4598 pwg_map_t
*pwg_map
; /* Mapping from PWG to PPD keywords */
4599 _ppd_cache_t
*pc
; /* PPD cache */
4600 _pwg_finishings_t
*finishings
; /* Current finishings value */
4601 const char *template; /* Current finishings-template value */
4602 int num_margins
; /* Number of media-xxx-margin-supported values */
4603 int margins
[10]; /* media-xxx-margin-supported values */
4604 int xres
, /* Default horizontal resolution */
4605 yres
; /* Default vertical resolution */
4606 int num_urf
; /* Number of urf-supported values */
4607 const char *urf
[10]; /* urf-supported values */
4608 char urf_rs
[32]; /* RS value */
4609 static const int orientation_requested_supported
[4] =
4610 { /* orientation-requested-supported values */
4611 IPP_ORIENT_PORTRAIT
,
4612 IPP_ORIENT_LANDSCAPE
,
4613 IPP_ORIENT_REVERSE_LANDSCAPE
,
4614 IPP_ORIENT_REVERSE_PORTRAIT
4616 static const char * const overrides_supported
[] =
4617 { /* overrides-supported */
4621 "orientation-requested",
4624 static const char * const print_color_mode_supported
[] =
4625 { /* print-color-mode-supported values */
4628 static const char * const print_color_mode_supported_color
[] =
4629 { /* print-color-mode-supported values */
4634 static const int print_quality_supported
[] =
4635 { /* print-quality-supported values */
4640 static const char * const printer_supply
[] =
4641 { /* printer-supply values */
4642 "index=1;class=receptacleThatIsFilled;type=wasteToner;unit=percent;"
4643 "maxcapacity=100;level=25;colorantname=unknown;",
4644 "index=2;class=supplyThatIsConsumed;type=toner;unit=percent;"
4645 "maxcapacity=100;level=75;colorantname=black;"
4647 static const char * const printer_supply_color
[] =
4648 { /* printer-supply values */
4649 "index=1;class=receptacleThatIsFilled;type=wasteInk;unit=percent;"
4650 "maxcapacity=100;level=25;colorantname=unknown;",
4651 "index=2;class=supplyThatIsConsumed;type=ink;unit=percent;"
4652 "maxcapacity=100;level=75;colorantname=black;",
4653 "index=3;class=supplyThatIsConsumed;type=ink;unit=percent;"
4654 "maxcapacity=100;level=50;colorantname=cyan;",
4655 "index=4;class=supplyThatIsConsumed;type=ink;unit=percent;"
4656 "maxcapacity=100;level=33;colorantname=magenta;",
4657 "index=5;class=supplyThatIsConsumed;type=ink;unit=percent;"
4658 "maxcapacity=100;level=67;colorantname=yellow;"
4660 static const char * const printer_supply_description
[] =
4661 { /* printer-supply-description values */
4665 static const char * const printer_supply_description_color
[] =
4666 { /* printer-supply-description values */
4673 static const char * const pwg_raster_document_type_supported
[] =
4678 static const char * const pwg_raster_document_type_supported_color
[] =
4685 static const char * const sides_supported
[] =
4686 { /* sides-supported values */
4688 "two-sided-long-edge",
4689 "two-sided-short-edge"
4694 * Open the PPD file...
4697 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
4699 ppd_status_t status
; /* Load error */
4701 status
= ppdLastError(&i
);
4702 _cupsLangPrintf(stderr
, _("ippeveprinter: Unable to open \"%s\": %s on line %d."), ppdfile
, ppdErrorString(status
), i
);
4706 ppdMarkDefaults(ppd
);
4708 pc
= _ppdCacheCreateWithPPD(ppd
);
4710 if ((ppd_size
= ppdPageSize(ppd
, NULL
)) != NULL
)
4713 * Look up default size...
4716 for (i
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
; i
++, pwg_size
++)
4718 if (!strcmp(pwg_size
->map
.ppd
, ppd_size
->name
))
4720 default_size
= pwg_size
;
4729 * Default to A4 or Letter...
4732 for (i
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
; i
++, pwg_size
++)
4734 if (!strcmp(pwg_size
->map
.ppd
, "Letter") || !strcmp(pwg_size
->map
.ppd
, "A4"))
4736 default_size
= pwg_size
;
4742 default_size
= pc
->sizes
; /* Last resort: first size */
4745 if ((ppd_choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
4746 default_source
= _ppdCacheGetSource(pc
, ppd_choice
->choice
);
4748 if ((ppd_choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
4749 default_source
= _ppdCacheGetType(pc
, ppd_choice
->choice
);
4751 if ((ppd_attr
= ppdFindAttr(ppd
, "DefaultResolution", NULL
)) != NULL
)
4754 * Use the PPD-defined default resolution...
4757 if ((i
= sscanf(ppd_attr
->value
, "%dx%d", &xres
, &yres
)) == 1)
4765 * Use default of 300dpi...
4771 snprintf(urf_rs
, sizeof(urf_rs
), "RS%d", yres
< xres
? yres
: xres
);
4774 urf
[num_urf
++] = "V1.4";
4775 urf
[num_urf
++] = "CP1";
4776 urf
[num_urf
++] = urf_rs
;
4777 urf
[num_urf
++] = "W8";
4778 if (pc
->sides_2sided_long
)
4779 urf
[num_urf
++] = "DM1";
4780 if (ppd
->color_device
)
4781 urf
[num_urf
++] = "SRGB24";
4784 * PostScript printers accept PDF via one of the CUPS PDF to PostScript
4785 * filters, along with PostScript (of course) and JPEG...
4788 cupsArrayAdd(docformats
, "application/pdf");
4789 cupsArrayAdd(docformats
, "application/postscript");
4790 cupsArrayAdd(docformats
, "image/jpeg");
4793 * Create the attributes...
4798 /* color-supported */
4799 ippAddBoolean(attrs
, IPP_TAG_PRINTER
, "color-supported", (char)ppd
->color_device
);
4801 /* copies-default */
4802 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default", 1);
4804 /* copies-supported */
4805 ippAddRange(attrs
, IPP_TAG_PRINTER
, "copies-supported", 1, 999);
4807 /* document-password-supported */
4808 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "document-password-supported", 127);
4810 /* finishing-template-supported */
4811 attr
= ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "finishing-template-supported", cupsArrayCount(pc
->templates
) + 1, NULL
, NULL
);
4812 ippSetString(attrs
, &attr
, 0, "none");
4813 for (i
= 1, template = (const char *)cupsArrayFirst(pc
->templates
); template; i
++, template = (const char *)cupsArrayNext(pc
->templates
))
4814 ippSetString(attrs
, &attr
, i
, template);
4816 /* finishings-col-database */
4817 attr
= ippAddCollections(attrs
, IPP_TAG_PRINTER
, "finishings-col-database", cupsArrayCount(pc
->templates
) + 1, NULL
);
4820 ippAddString(col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "finishing-template", NULL
, "none");
4821 ippSetCollection(attrs
, &attr
, 0, col
);
4824 for (i
= 1, template = (const char *)cupsArrayFirst(pc
->templates
); template; i
++, template = (const char *)cupsArrayNext(pc
->templates
))
4827 ippAddString(col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "finishing-template", NULL
, template);
4828 ippSetCollection(attrs
, &attr
, i
, col
);
4832 /* finishings-col-default */
4834 ippAddString(col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "finishing-template", NULL
, "none");
4835 ippAddCollection(attrs
, IPP_TAG_PRINTER
, "finishings-col-default", col
);
4838 /* finishings-col-ready */
4839 attr
= ippAddCollections(attrs
, IPP_TAG_PRINTER
, "finishings-col-ready", cupsArrayCount(pc
->templates
) + 1, NULL
);
4842 ippAddString(col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "finishing-template", NULL
, "none");
4843 ippSetCollection(attrs
, &attr
, 0, col
);
4846 for (i
= 1, template = (const char *)cupsArrayFirst(pc
->templates
); template; i
++, template = (const char *)cupsArrayNext(pc
->templates
))
4849 ippAddString(col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "finishing-template", NULL
, template);
4850 ippSetCollection(attrs
, &attr
, i
, col
);
4854 /* finishings-col-supported */
4855 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "finishings-col-supported", NULL
, "finishing-template");
4857 /* finishings-default */
4858 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "finishings-default", IPP_FINISHINGS_NONE
);
4860 /* finishings-ready */
4861 attr
= ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "finishings-ready", cupsArrayCount(pc
->finishings
) + 1, NULL
);
4862 ippSetInteger(attrs
, &attr
, 0, IPP_FINISHINGS_NONE
);
4863 for (i
= 1, finishings
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
); finishings
; i
++, finishings
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
4864 ippSetInteger(attrs
, &attr
, i
, (int)finishings
->value
);
4866 /* finishings-supported */
4867 attr
= ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "finishings-supported", cupsArrayCount(pc
->finishings
) + 1, NULL
);
4868 ippSetInteger(attrs
, &attr
, 0, IPP_FINISHINGS_NONE
);
4869 for (i
= 1, finishings
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
); finishings
; i
++, finishings
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
4870 ippSetInteger(attrs
, &attr
, i
, (int)finishings
->value
);
4872 /* media-bottom-margin-supported */
4873 for (i
= 0, num_margins
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
&& num_margins
< (int)(sizeof(margins
) / sizeof(margins
[0])); i
++, pwg_size
++)
4875 for (j
= 0; j
< num_margins
; j
++)
4877 if (margins
[j
] == pwg_size
->bottom
)
4881 if (j
>= num_margins
)
4882 margins
[num_margins
++] = pwg_size
->bottom
;
4885 for (i
= 0; i
< (num_margins
- 1); i
++)
4887 for (j
= i
+ 1; j
< num_margins
; j
++)
4889 if (margins
[i
] > margins
[j
])
4891 int mtemp
= margins
[i
];
4893 margins
[i
] = margins
[j
];
4899 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-bottom-margin-supported", num_margins
, margins
);
4901 /* media-col-database */
4902 attr
= ippAddCollections(attrs
, IPP_TAG_PRINTER
, "media-col-database", pc
->num_sizes
, NULL
);
4903 for (i
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
; i
++, pwg_size
++)
4905 col
= create_media_col(pwg_size
->map
.pwg
, NULL
, NULL
, pwg_size
->width
, pwg_size
->length
, pwg_size
->bottom
, pwg_size
->left
, pwg_size
->right
, pwg_size
->top
);
4906 ippSetCollection(attrs
, &attr
, i
, col
);
4910 /* media-col-default */
4911 col
= create_media_col(default_size
->map
.pwg
, default_source
, default_type
, default_size
->width
, default_size
->length
, default_size
->bottom
, default_size
->left
, default_size
->right
, default_size
->top
);
4912 ippAddCollection(attrs
, IPP_TAG_PRINTER
, "media-col-default", col
);
4915 /* media-col-ready */
4916 col
= create_media_col(default_size
->map
.pwg
, default_source
, default_type
, default_size
->width
, default_size
->length
, default_size
->bottom
, default_size
->left
, default_size
->right
, default_size
->top
);
4917 ippAddCollection(attrs
, IPP_TAG_PRINTER
, "media-col-ready", col
);
4921 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-default", NULL
, default_size
->map
.pwg
);
4923 /* media-left-margin-supported */
4924 for (i
= 0, num_margins
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
&& num_margins
< (int)(sizeof(margins
) / sizeof(margins
[0])); i
++, pwg_size
++)
4926 for (j
= 0; j
< num_margins
; j
++)
4928 if (margins
[j
] == pwg_size
->left
)
4932 if (j
>= num_margins
)
4933 margins
[num_margins
++] = pwg_size
->left
;
4936 for (i
= 0; i
< (num_margins
- 1); i
++)
4938 for (j
= i
+ 1; j
< num_margins
; j
++)
4940 if (margins
[i
] > margins
[j
])
4942 int mtemp
= margins
[i
];
4944 margins
[i
] = margins
[j
];
4950 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-left-margin-supported", num_margins
, margins
);
4953 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-ready", NULL
, default_size
->map
.pwg
);
4955 /* media-right-margin-supported */
4956 for (i
= 0, num_margins
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
&& num_margins
< (int)(sizeof(margins
) / sizeof(margins
[0])); i
++, pwg_size
++)
4958 for (j
= 0; j
< num_margins
; j
++)
4960 if (margins
[j
] == pwg_size
->right
)
4964 if (j
>= num_margins
)
4965 margins
[num_margins
++] = pwg_size
->right
;
4968 for (i
= 0; i
< (num_margins
- 1); i
++)
4970 for (j
= i
+ 1; j
< num_margins
; j
++)
4972 if (margins
[i
] > margins
[j
])
4974 int mtemp
= margins
[i
];
4976 margins
[i
] = margins
[j
];
4982 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-right-margin-supported", num_margins
, margins
);
4984 /* media-supported */
4985 attr
= ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-supported", pc
->num_sizes
, NULL
, NULL
);
4986 for (i
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
; i
++, pwg_size
++)
4987 ippSetString(attrs
, &attr
, i
, pwg_size
->map
.pwg
);
4989 /* media-size-supported */
4990 attr
= ippAddCollections(attrs
, IPP_TAG_PRINTER
, "media-size-supported", pc
->num_sizes
, NULL
);
4991 for (i
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
; i
++, pwg_size
++)
4993 col
= create_media_size(pwg_size
->width
, pwg_size
->length
);
4994 ippSetCollection(attrs
, &attr
, i
, col
);
4998 /* media-source-supported */
4999 if (pc
->num_sources
> 0)
5001 attr
= ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-source-supported", pc
->num_sources
, NULL
, NULL
);
5002 for (i
= 0, pwg_map
= pc
->sources
; i
< pc
->num_sources
; i
++, pwg_map
++)
5003 ippSetString(attrs
, &attr
, i
, pwg_map
->pwg
);
5007 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-source-supported", NULL
, "auto");
5010 /* media-top-margin-supported */
5011 for (i
= 0, num_margins
= 0, pwg_size
= pc
->sizes
; i
< pc
->num_sizes
&& num_margins
< (int)(sizeof(margins
) / sizeof(margins
[0])); i
++, pwg_size
++)
5013 for (j
= 0; j
< num_margins
; j
++)
5015 if (margins
[j
] == pwg_size
->top
)
5019 if (j
>= num_margins
)
5020 margins
[num_margins
++] = pwg_size
->top
;
5023 for (i
= 0; i
< (num_margins
- 1); i
++)
5025 for (j
= i
+ 1; j
< num_margins
; j
++)
5027 if (margins
[i
] > margins
[j
])
5029 int mtemp
= margins
[i
];
5031 margins
[i
] = margins
[j
];
5037 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "media-top-margin-supported", num_margins
, margins
);
5039 /* media-type-supported */
5040 if (pc
->num_types
> 0)
5042 attr
= ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-type-supported", pc
->num_types
, NULL
, NULL
);
5043 for (i
= 0, pwg_map
= pc
->types
; i
< pc
->num_types
; i
++, pwg_map
++)
5044 ippSetString(attrs
, &attr
, i
, pwg_map
->pwg
);
5048 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "media-type-supported", NULL
, "auto");
5051 /* orientation-requested-default */
5052 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "orientation-requested-default", IPP_ORIENT_PORTRAIT
);
5054 /* orientation-requested-supported */
5055 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "orientation-requested-supported", (int)(sizeof(orientation_requested_supported
) / sizeof(orientation_requested_supported
[0])), orientation_requested_supported
);
5057 /* output-bin-default */
5058 if (pc
->num_bins
> 0)
5059 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "output-bin-default", NULL
, pc
->bins
->pwg
);
5061 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "output-bin-default", NULL
, "face-down");
5063 /* output-bin-supported */
5064 if (pc
->num_bins
> 0)
5066 attr
= ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "output-bin-supported", pc
->num_bins
, NULL
, NULL
);
5067 for (i
= 0, pwg_map
= pc
->bins
; i
< pc
->num_bins
; i
++, pwg_map
++)
5068 ippSetString(attrs
, &attr
, i
, pwg_map
->pwg
);
5072 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "output-bin-supported", NULL
, "face-down");
5075 /* overrides-supported */
5076 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "overrides-supported", (int)(sizeof(overrides_supported
) / sizeof(overrides_supported
[0])), NULL
, overrides_supported
);
5078 /* page-ranges-supported */
5079 ippAddBoolean(attrs
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
5081 /* pages-per-minute */
5082 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "pages-per-minute", ppd
->throughput
);
5084 /* pages-per-minute-color */
5085 if (ppd
->color_device
)
5086 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "pages-per-minute-color", ppd
->throughput
);
5088 /* print-color-mode-default */
5089 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-color-mode-default", NULL
, ppd
->color_device
? "auto" : "monochrome");
5091 /* print-color-mode-supported */
5092 if (ppd
->color_device
)
5093 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-color-mode-supported", (int)(sizeof(print_color_mode_supported_color
) / sizeof(print_color_mode_supported_color
[0])), NULL
, print_color_mode_supported_color
);
5095 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-color-mode-supported", (int)(sizeof(print_color_mode_supported
) / sizeof(print_color_mode_supported
[0])), NULL
, print_color_mode_supported
);
5097 /* print-content-optimize-default */
5098 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-content-optimize-default", NULL
, "auto");
5100 /* print-content-optimize-supported */
5101 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-content-optimize-supported", NULL
, "auto");
5103 /* print-quality-default */
5104 ippAddInteger(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "print-quality-default", IPP_QUALITY_NORMAL
);
5106 /* print-quality-supported */
5107 ippAddIntegers(attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "print-quality-supported", (int)(sizeof(print_quality_supported
) / sizeof(print_quality_supported
[0])), print_quality_supported
);
5109 /* print-rendering-intent-default */
5110 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-rendering-intent-default", NULL
, "auto");
5112 /* print-rendering-intent-supported */
5113 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "print-rendering-intent-supported", NULL
, "auto");
5115 /* printer-device-id */
5116 if ((ppd_attr
= ppdFindAttr(ppd
, "1284DeviceId", NULL
)) != NULL
)
5119 * Use the device ID string from the PPD...
5122 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, ppd_attr
->value
);
5127 * Synthesize a device ID string...
5130 char device_id
[1024]; /* Device ID string */
5132 snprintf(device_id
, sizeof(device_id
), "MFG:%s;MDL:%s;CMD:PS;", ppd
->manufacturer
, ppd
->modelname
);
5134 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-device-id", NULL
, device_id
);
5137 /* printer-input-tray */
5138 if (pc
->num_sources
> 0)
5140 for (i
= 0, attr
= NULL
; i
< pc
->num_sources
; i
++)
5142 char input_tray
[1024]; /* printer-input-tray value */
5144 if (!strcmp(pc
->sources
[i
].pwg
, "manual") || strstr(pc
->sources
[i
].pwg
, "-man") != NULL
)
5145 snprintf(input_tray
, sizeof(input_tray
), "type=sheetFeedManual;mediafeed=0;mediaxfeed=0;maxcapacity=1;level=-2;status=0;name=%s", pc
->sources
[i
].pwg
);
5147 snprintf(input_tray
, sizeof(input_tray
), "type=sheetFeedAutoRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=250;level=125;status=0;name=%s", pc
->sources
[i
].pwg
);
5150 ippSetOctetString(attrs
, &attr
, i
, input_tray
, (int)strlen(input_tray
));
5152 attr
= ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-input-tray", input_tray
, (int)strlen(input_tray
));
5157 static const char *printer_input_tray
= "type=sheetFeedAutoRemovableTray;mediafeed=0;mediaxfeed=0;maxcapacity=-2;level=-2;status=0;name=auto";
5159 ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-input-tray", printer_input_tray
, (int)strlen(printer_input_tray
));
5162 /* printer-make-and-model */
5163 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-make-and-model", NULL
, ppd
->nickname
);
5165 /* printer-resolution-default */
5166 ippAddResolution(attrs
, IPP_TAG_PRINTER
, "printer-resolution-default", IPP_RES_PER_INCH
, xres
, yres
);
5168 /* printer-resolution-supported */
5169 ippAddResolution(attrs
, IPP_TAG_PRINTER
, "printer-resolution-supported", IPP_RES_PER_INCH
, xres
, yres
);
5171 /* printer-supply and printer-supply-description */
5172 if (ppd
->color_device
)
5174 attr
= ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-supply", printer_supply_color
[0], (int)strlen(printer_supply_color
[0]));
5175 for (i
= 1; i
< (int)(sizeof(printer_supply_color
) / sizeof(printer_supply_color
[0])); i
++)
5176 ippSetOctetString(attrs
, &attr
, i
, printer_supply_color
[i
], (int)strlen(printer_supply_color
[i
]));
5178 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_TEXT
), "printer-supply-description", (int)(sizeof(printer_supply_description_color
) / sizeof(printer_supply_description_color
[0])), NULL
, printer_supply_description_color
);
5182 attr
= ippAddOctetString(attrs
, IPP_TAG_PRINTER
, "printer-supply", printer_supply
[0], (int)strlen(printer_supply
[0]));
5183 for (i
= 1; i
< (int)(sizeof(printer_supply
) / sizeof(printer_supply
[0])); i
++)
5184 ippSetOctetString(attrs
, &attr
, i
, printer_supply
[i
], (int)strlen(printer_supply
[i
]));
5186 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_TEXT
), "printer-supply-description", (int)(sizeof(printer_supply_description
) / sizeof(printer_supply_description
[0])), NULL
, printer_supply_description
);
5189 /* pwg-raster-document-xxx-supported */
5190 if (cupsArrayFind(docformats
, (void *)"image/pwg-raster"))
5192 ippAddResolution(attrs
, IPP_TAG_PRINTER
, "pwg-raster-document-resolution-supported", IPP_RES_PER_INCH
, xres
, yres
);
5194 if (pc
->sides_2sided_long
)
5195 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pwg-raster-document-sheet-back", NULL
, "normal");
5197 if (ppd
->color_device
)
5198 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pwg-raster-document-type-supported", (int)(sizeof(pwg_raster_document_type_supported_color
) / sizeof(pwg_raster_document_type_supported_color
[0])), NULL
, pwg_raster_document_type_supported_color
);
5200 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "pwg-raster-document-type-supported", (int)(sizeof(pwg_raster_document_type_supported
) / sizeof(pwg_raster_document_type_supported
[0])), NULL
, pwg_raster_document_type_supported
);
5204 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "sides-default", NULL
, "one-sided");
5206 /* sides-supported */
5207 if (pc
->sides_2sided_long
)
5208 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "sides-supported", (int)(sizeof(sides_supported
) / sizeof(sides_supported
[0])), NULL
, sides_supported
);
5210 ippAddString(attrs
, IPP_TAG_PRINTER
, IPP_CONST_TAG(IPP_TAG_KEYWORD
), "sides-supported", NULL
, "one-sided");
5213 if (cupsArrayFind(docformats
, (void *)"image/urf"))
5214 ippAddStrings(attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "urf-supported", num_urf
, NULL
, urf
);
5217 * Free the PPD file and return the attributes...
5220 _ppdCacheDestroy(pc
);
5226 #endif /* !CUPS_LITE */
5230 * 'parse_options()' - Parse URL options into CUPS options.
5232 * The client->options string is destroyed by this function.
5235 static int /* O - Number of options */
5236 parse_options(ippeve_client_t
*client
, /* I - Client */
5237 cups_option_t
**options
)/* O - Options */
5239 char *name
, /* Name */
5241 *next
; /* Next name=value pair */
5242 int num_options
= 0; /* Number of options */
5247 for (name
= client
->options
; name
&& *name
; name
= next
)
5249 if ((value
= strchr(name
, '=')) == NULL
)
5253 if ((next
= strchr(value
, '&')) != NULL
)
5256 num_options
= cupsAddOption(name
, value
, num_options
, options
);
5259 return (num_options
);
5264 * 'process_attr_message()' - Process an ATTR: message from a command.
5268 process_attr_message(
5269 ippeve_job_t
*job
, /* I - Job */
5270 char *message
) /* I - Message */
5272 int i
, /* Looping var */
5273 num_options
= 0; /* Number of name=value pairs */
5274 cups_option_t
*options
= NULL
, /* name=value pairs from message */
5275 *option
; /* Current option */
5276 ipp_attribute_t
*attr
; /* Current attribute */
5280 * Grab attributes from the message line...
5283 num_options
= cupsParseOptions(message
+ 5, num_options
, &options
);
5286 * Loop through the options and record them in the printer or job objects...
5289 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
5291 if (!strcmp(option
->name
, "job-impressions"))
5294 * Update job-impressions attribute...
5297 job
->impressions
= atoi(option
->value
);
5299 else if (!strcmp(option
->name
, "job-impressions-completed"))
5302 * Update job-impressions-completed attribute...
5305 job
->impcompleted
= atoi(option
->value
);
5307 else if (!strncmp(option
->name
, "marker-", 7) || !strcmp(option
->name
, "printer-alert") || !strcmp(option
->name
, "printer-alert-description") || !strcmp(option
->name
, "printer-supply") || !strcmp(option
->name
, "printer-supply-description"))
5310 * Update Printer Status attribute...
5313 _cupsRWLockWrite(&job
->printer
->rwlock
);
5315 if ((attr
= ippFindAttribute(job
->printer
->attrs
, option
->name
, IPP_TAG_ZERO
)) != NULL
)
5316 ippDeleteAttribute(job
->printer
->attrs
, attr
);
5318 cupsEncodeOption(job
->printer
->attrs
, IPP_TAG_PRINTER
, option
->name
, option
->value
);
5320 _cupsRWUnlock(&job
->printer
->rwlock
);
5325 * Something else that isn't currently supported...
5328 fprintf(stderr
, "[Job %d] Ignoring update of attribute \"%s\" with value \"%s\".\n", job
->id
, option
->name
, option
->value
);
5332 cupsFreeOptions(num_options
, options
);
5337 * 'process_client()' - Process client requests on a thread.
5340 static void * /* O - Exit status */
5341 process_client(ippeve_client_t
*client
) /* I - Client */
5344 * Loop until we are out of requests or timeout (30 seconds)...
5348 int first_time
= 1; /* First time request? */
5349 #endif /* HAVE_SSL */
5351 while (httpWait(client
->http
, 30000))
5357 * See if we need to negotiate a TLS connection...
5360 char buf
[1]; /* First byte from client */
5362 if (recv(httpGetFd(client
->http
), buf
, 1, MSG_PEEK
) == 1 && (!buf
[0] || !strchr("DGHOPT", buf
[0])))
5364 fprintf(stderr
, "%s Starting HTTPS session.\n", client
->hostname
);
5366 if (httpEncryption(client
->http
, HTTP_ENCRYPTION_ALWAYS
))
5368 fprintf(stderr
, "%s Unable to encrypt connection: %s\n", client
->hostname
, cupsLastErrorString());
5372 fprintf(stderr
, "%s Connection now encrypted.\n", client
->hostname
);
5377 #endif /* HAVE_SSL */
5379 if (!process_http(client
))
5384 * Close the conection to the client and return...
5387 delete_client(client
);
5394 * 'process_http()' - Process a HTTP request.
5397 int /* O - 1 on success, 0 on failure */
5398 process_http(ippeve_client_t
*client
) /* I - Client connection */
5400 char uri
[1024]; /* URI */
5401 http_state_t http_state
; /* HTTP state */
5402 http_status_t http_status
; /* HTTP status */
5403 ipp_state_t ipp_state
; /* State of IPP transfer */
5404 char scheme
[32], /* Method/scheme */
5405 userpass
[128], /* Username:password */
5406 hostname
[HTTP_MAX_HOST
];
5408 int port
; /* Port number */
5409 static const char * const http_states
[] =
5410 { /* Strings for logging HTTP method */
5431 * Clear state variables...
5434 ippDelete(client
->request
);
5435 ippDelete(client
->response
);
5437 client
->request
= NULL
;
5438 client
->response
= NULL
;
5439 client
->operation
= HTTP_STATE_WAITING
;
5442 * Read a request from the connection...
5445 while ((http_state
= httpReadRequest(client
->http
, uri
,
5446 sizeof(uri
))) == HTTP_STATE_WAITING
)
5450 * Parse the request line...
5453 if (http_state
== HTTP_STATE_ERROR
)
5455 if (httpError(client
->http
) == EPIPE
)
5456 fprintf(stderr
, "%s Client closed connection.\n", client
->hostname
);
5458 fprintf(stderr
, "%s Bad request line (%s).\n", client
->hostname
, strerror(httpError(client
->http
)));
5462 else if (http_state
== HTTP_STATE_UNKNOWN_METHOD
)
5464 fprintf(stderr
, "%s Bad/unknown operation.\n", client
->hostname
);
5465 respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0);
5468 else if (http_state
== HTTP_STATE_UNKNOWN_VERSION
)
5470 fprintf(stderr
, "%s Bad HTTP version.\n", client
->hostname
);
5471 respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0);
5475 fprintf(stderr
, "%s %s %s\n", client
->hostname
, http_states
[http_state
], uri
);
5478 * Separate the URI into its components...
5481 if (httpSeparateURI(HTTP_URI_CODING_MOST
, uri
, scheme
, sizeof(scheme
),
5482 userpass
, sizeof(userpass
),
5483 hostname
, sizeof(hostname
), &port
,
5484 client
->uri
, sizeof(client
->uri
)) < HTTP_URI_STATUS_OK
&&
5485 (http_state
!= HTTP_STATE_OPTIONS
|| strcmp(uri
, "*")))
5487 fprintf(stderr
, "%s Bad URI \"%s\".\n", client
->hostname
, uri
);
5488 respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0);
5492 if ((client
->options
= strchr(client
->uri
, '?')) != NULL
)
5493 *(client
->options
)++ = '\0';
5496 * Process the request...
5499 client
->start
= time(NULL
);
5500 client
->operation
= httpGetState(client
->http
);
5503 * Parse incoming parameters until the status changes...
5506 while ((http_status
= httpUpdate(client
->http
)) == HTTP_STATUS_CONTINUE
);
5508 if (http_status
!= HTTP_STATUS_OK
)
5510 respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0);
5514 if (!httpGetField(client
->http
, HTTP_FIELD_HOST
)[0] &&
5515 httpGetVersion(client
->http
) >= HTTP_VERSION_1_1
)
5518 * HTTP/1.1 and higher require the "Host:" field...
5521 respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0);
5526 * Handle HTTP Upgrade...
5529 if (!strcasecmp(httpGetField(client
->http
, HTTP_FIELD_CONNECTION
),
5533 if (strstr(httpGetField(client
->http
, HTTP_FIELD_UPGRADE
), "TLS/") != NULL
&& !httpIsEncrypted(client
->http
))
5535 if (!respond_http(client
, HTTP_STATUS_SWITCHING_PROTOCOLS
, NULL
, NULL
, 0))
5538 fprintf(stderr
, "%s Upgrading to encrypted connection.\n", client
->hostname
);
5540 if (httpEncryption(client
->http
, HTTP_ENCRYPTION_REQUIRED
))
5542 fprintf(stderr
, "%s Unable to encrypt connection: %s\n", client
->hostname
, cupsLastErrorString());
5546 fprintf(stderr
, "%s Connection now encrypted.\n", client
->hostname
);
5549 #endif /* HAVE_SSL */
5551 if (!respond_http(client
, HTTP_STATUS_NOT_IMPLEMENTED
, NULL
, NULL
, 0))
5556 * Handle HTTP Expect...
5559 if (httpGetExpect(client
->http
) &&
5560 (client
->operation
== HTTP_STATE_POST
||
5561 client
->operation
== HTTP_STATE_PUT
))
5563 if (httpGetExpect(client
->http
) == HTTP_STATUS_CONTINUE
)
5566 * Send 100-continue header...
5569 if (!respond_http(client
, HTTP_STATUS_CONTINUE
, NULL
, NULL
, 0))
5575 * Send 417-expectation-failed header...
5578 if (!respond_http(client
, HTTP_STATUS_EXPECTATION_FAILED
, NULL
, NULL
, 0))
5584 * Handle new transfers...
5587 switch (client
->operation
)
5589 case HTTP_STATE_OPTIONS
:
5591 * Do OPTIONS command...
5594 return (respond_http(client
, HTTP_STATUS_OK
, NULL
, NULL
, 0));
5596 case HTTP_STATE_HEAD
:
5597 if (!strcmp(client
->uri
, "/icon.png"))
5598 return (respond_http(client
, HTTP_STATUS_OK
, NULL
, "image/png", 0));
5599 else if (!strcmp(client
->uri
, "/") || !strcmp(client
->uri
, "/media") || !strcmp(client
->uri
, "/supplies"))
5600 return (respond_http(client
, HTTP_STATUS_OK
, NULL
, "text/html", 0));
5602 return (respond_http(client
, HTTP_STATUS_NOT_FOUND
, NULL
, NULL
, 0));
5604 case HTTP_STATE_GET
:
5605 if (!strcmp(client
->uri
, "/icon.png"))
5608 * Send PNG icon file.
5611 if (client
->printer
->icon
)
5613 int fd
; /* Icon file */
5614 struct stat fileinfo
; /* Icon file information */
5615 char buffer
[4096]; /* Copy buffer */
5616 ssize_t bytes
; /* Bytes */
5618 fprintf(stderr
, "Icon file is \"%s\".\n", client
->printer
->icon
);
5620 if (!stat(client
->printer
->icon
, &fileinfo
) && (fd
= open(client
->printer
->icon
, O_RDONLY
)) >= 0)
5622 if (!respond_http(client
, HTTP_STATUS_OK
, NULL
, "image/png", (size_t)fileinfo
.st_size
))
5628 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
5629 httpWrite2(client
->http
, buffer
, (size_t)bytes
);
5631 httpFlushWrite(client
->http
);
5636 return (respond_http(client
, HTTP_STATUS_NOT_FOUND
, NULL
, NULL
, 0));
5640 fputs("Icon file is internal printer.png.\n", stderr
);
5642 if (!respond_http(client
, HTTP_STATUS_OK
, NULL
, "image/png", sizeof(printer_png
)))
5645 httpWrite2(client
->http
, (const char *)printer_png
, sizeof(printer_png
));
5646 httpFlushWrite(client
->http
);
5649 else if (!strcmp(client
->uri
, "/"))
5652 * Show web status page...
5655 return (show_status(client
));
5657 else if (!strcmp(client
->uri
, "/media"))
5660 * Show web media page...
5663 return (show_media(client
));
5665 else if (!strcmp(client
->uri
, "/supplies"))
5668 * Show web supplies page...
5671 return (show_supplies(client
));
5674 return (respond_http(client
, HTTP_STATUS_NOT_FOUND
, NULL
, NULL
, 0));
5677 case HTTP_STATE_POST
:
5678 if (strcmp(httpGetField(client
->http
, HTTP_FIELD_CONTENT_TYPE
),
5682 * Not an IPP request...
5685 return (respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0));
5689 * Read the IPP request...
5692 client
->request
= ippNew();
5694 while ((ipp_state
= ippRead(client
->http
,
5695 client
->request
)) != IPP_STATE_DATA
)
5697 if (ipp_state
== IPP_STATE_ERROR
)
5699 fprintf(stderr
, "%s IPP read error (%s).\n", client
->hostname
, cupsLastErrorString());
5700 respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0);
5706 * Now that we have the IPP request, process the request...
5709 return (process_ipp(client
));
5712 break; /* Anti-compiler-warning-code */
5720 * 'process_ipp()' - Process an IPP request.
5723 static int /* O - 1 on success, 0 on error */
5724 process_ipp(ippeve_client_t
*client
) /* I - Client */
5726 ipp_tag_t group
; /* Current group tag */
5727 ipp_attribute_t
*attr
; /* Current attribute */
5728 ipp_attribute_t
*charset
; /* Character set attribute */
5729 ipp_attribute_t
*language
; /* Language attribute */
5730 ipp_attribute_t
*uri
; /* Printer URI attribute */
5731 int major
, minor
; /* Version number */
5732 const char *name
; /* Name of attribute */
5735 debug_attributes("Request", client
->request
, 1);
5738 * First build an empty response message for this request...
5741 client
->operation_id
= ippGetOperation(client
->request
);
5742 client
->response
= ippNewResponse(client
->request
);
5745 * Then validate the request header and required attributes...
5748 major
= ippGetVersion(client
->request
, &minor
);
5750 if (major
< 1 || major
> 2)
5753 * Return an error, since we only support IPP 1.x and 2.x.
5756 respond_ipp(client
, IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, "Bad request version number %d.%d.", major
, minor
);
5758 else if ((major
* 10 + minor
) > MaxVersion
)
5760 if (httpGetState(client
->http
) != HTTP_STATE_POST_SEND
)
5761 httpFlush(client
->http
); /* Flush trailing (junk) data */
5763 respond_http(client
, HTTP_STATUS_BAD_REQUEST
, NULL
, NULL
, 0);
5766 else if (ippGetRequestId(client
->request
) <= 0)
5768 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "Bad request-id %d.", ippGetRequestId(client
->request
));
5770 else if (!ippFirstAttribute(client
->request
))
5772 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
, "No attributes in request.");
5777 * Make sure that the attributes are provided in the correct order and
5778 * don't repeat groups...
5781 for (attr
= ippFirstAttribute(client
->request
),
5782 group
= ippGetGroupTag(attr
);
5784 attr
= ippNextAttribute(client
->request
))
5786 if (ippGetGroupTag(attr
) < group
&& ippGetGroupTag(attr
) != IPP_TAG_ZERO
)
5789 * Out of order; return an error...
5792 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
,
5793 "Attribute groups are out of order (%x < %x).",
5794 ippGetGroupTag(attr
), group
);
5798 group
= ippGetGroupTag(attr
);
5804 * Then make sure that the first three attributes are:
5806 * attributes-charset
5807 * attributes-natural-language
5808 * printer-uri/job-uri
5811 attr
= ippFirstAttribute(client
->request
);
5812 name
= ippGetName(attr
);
5813 if (attr
&& name
&& !strcmp(name
, "attributes-charset") &&
5814 ippGetValueTag(attr
) == IPP_TAG_CHARSET
)
5819 attr
= ippNextAttribute(client
->request
);
5820 name
= ippGetName(attr
);
5822 if (attr
&& name
&& !strcmp(name
, "attributes-natural-language") &&
5823 ippGetValueTag(attr
) == IPP_TAG_LANGUAGE
)
5828 if ((attr
= ippFindAttribute(client
->request
, "printer-uri",
5829 IPP_TAG_URI
)) != NULL
)
5831 else if ((attr
= ippFindAttribute(client
->request
, "job-uri",
5832 IPP_TAG_URI
)) != NULL
)
5838 strcasecmp(ippGetString(charset
, 0, NULL
), "us-ascii") &&
5839 strcasecmp(ippGetString(charset
, 0, NULL
), "utf-8"))
5842 * Bad character set...
5845 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
,
5846 "Unsupported character set \"%s\".",
5847 ippGetString(charset
, 0, NULL
));
5849 else if (!charset
|| !language
|| !uri
)
5852 * Return an error, since attributes-charset,
5853 * attributes-natural-language, and printer-uri/job-uri are required
5854 * for all operations.
5857 respond_ipp(client
, IPP_STATUS_ERROR_BAD_REQUEST
,
5858 "Missing required attributes.");
5862 char scheme
[32], /* URI scheme */
5863 userpass
[32], /* Username/password in URI */
5864 host
[256], /* Host name in URI */
5865 resource
[256]; /* Resource path in URI */
5866 int port
; /* Port number in URI */
5868 name
= ippGetName(uri
);
5870 if (httpSeparateURI(HTTP_URI_CODING_ALL
, ippGetString(uri
, 0, NULL
),
5871 scheme
, sizeof(scheme
),
5872 userpass
, sizeof(userpass
),
5873 host
, sizeof(host
), &port
,
5874 resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
5875 respond_ipp(client
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
,
5876 "Bad %s value '%s'.", name
, ippGetString(uri
, 0, NULL
));
5877 else if ((!strcmp(name
, "job-uri") &&
5878 strncmp(resource
, "/ipp/print/", 11)) ||
5879 (!strcmp(name
, "printer-uri") &&
5880 strcmp(resource
, "/ipp/print")))
5881 respond_ipp(client
, IPP_STATUS_ERROR_NOT_FOUND
, "%s %s not found.",
5882 name
, ippGetString(uri
, 0, NULL
));
5886 * Try processing the operation...
5889 switch (ippGetOperation(client
->request
))
5891 case IPP_OP_PRINT_JOB
:
5892 ipp_print_job(client
);
5895 case IPP_OP_PRINT_URI
:
5896 ipp_print_uri(client
);
5899 case IPP_OP_VALIDATE_JOB
:
5900 ipp_validate_job(client
);
5903 case IPP_OP_CREATE_JOB
:
5904 ipp_create_job(client
);
5907 case IPP_OP_SEND_DOCUMENT
:
5908 ipp_send_document(client
);
5911 case IPP_OP_SEND_URI
:
5912 ipp_send_uri(client
);
5915 case IPP_OP_CANCEL_JOB
:
5916 ipp_cancel_job(client
);
5919 case IPP_OP_GET_JOB_ATTRIBUTES
:
5920 ipp_get_job_attributes(client
);
5923 case IPP_OP_GET_JOBS
:
5924 ipp_get_jobs(client
);
5927 case IPP_OP_GET_PRINTER_ATTRIBUTES
:
5928 ipp_get_printer_attributes(client
);
5931 case IPP_OP_CLOSE_JOB
:
5932 ipp_close_job(client
);
5935 case IPP_OP_IDENTIFY_PRINTER
:
5936 ipp_identify_printer(client
);
5940 respond_ipp(client
, IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
,
5941 "Operation not supported.");
5950 * Send the HTTP header and return...
5953 if (httpGetState(client
->http
) != HTTP_STATE_POST_SEND
)
5954 httpFlush(client
->http
); /* Flush trailing (junk) data */
5956 return (respond_http(client
, HTTP_STATUS_OK
, NULL
, "application/ipp",
5957 ippLength(client
->response
)));
5962 * 'process_job()' - Process a print job.
5965 static void * /* O - Thread exit status */
5966 process_job(ippeve_job_t
*job
) /* I - Job */
5968 job
->state
= IPP_JSTATE_PROCESSING
;
5969 job
->printer
->state
= IPP_PSTATE_PROCESSING
;
5970 job
->processing
= time(NULL
);
5972 while (job
->printer
->state_reasons
& IPPEVE_PREASON_MEDIA_EMPTY
)
5974 job
->printer
->state_reasons
|= IPPEVE_PREASON_MEDIA_NEEDED
;
5979 job
->printer
->state_reasons
&= (ippeve_preason_t
)~IPPEVE_PREASON_MEDIA_NEEDED
;
5981 if (job
->printer
->command
)
5984 * Execute a command with the job spool file and wait for it to complete...
5987 int pid
, /* Process ID */
5988 status
; /* Exit status */
5989 struct timeval start
, /* Start time */
5991 char *myargv
[3], /* Command-line arguments */
5992 *myenvp
[400]; /* Environment variables */
5993 int myenvc
; /* Number of environment variables */
5994 ipp_attribute_t
*attr
; /* Job attribute */
5995 char val
[1280], /* IPP_NAME=value */
5996 *valptr
; /* Pointer into string */
5998 int mystdout
= -1; /* File for stdout */
5999 int mypipe
[2]; /* Pipe for stderr */
6000 char line
[2048], /* Line from stderr */
6001 *ptr
, /* Pointer into line */
6002 *endptr
; /* End of line */
6003 ssize_t bytes
; /* Bytes read */
6004 #endif /* !_WIN32 */
6006 fprintf(stderr
, "[Job %d] Running command \"%s %s\".\n", job
->id
, job
->printer
->command
, job
->filename
);
6007 gettimeofday(&start
, NULL
);
6010 * Setup the command-line arguments...
6013 myargv
[0] = job
->printer
->command
;
6014 myargv
[1] = job
->filename
;
6018 * Copy the current environment, then add environment variables for every
6019 * Job attribute and Printer -default attributes...
6022 for (myenvc
= 0; environ
[myenvc
] && myenvc
< (int)(sizeof(myenvp
) / sizeof(myenvp
[0]) - 1); myenvc
++)
6023 myenvp
[myenvc
] = strdup(environ
[myenvc
]);
6025 if (myenvc
> (int)(sizeof(myenvp
) / sizeof(myenvp
[0]) - 32))
6027 fprintf(stderr
, "[Job %d] Too many environment variables to process job.\n", job
->id
);
6028 job
->state
= IPP_JSTATE_ABORTED
;
6032 snprintf(val
, sizeof(val
), "CONTENT_TYPE=%s", job
->format
);
6033 myenvp
[myenvc
++] = strdup(val
);
6035 if (job
->printer
->device_uri
)
6037 snprintf(val
, sizeof(val
), "DEVICE_URI=%s", job
->printer
->device_uri
);
6038 myenvp
[myenvc
++] = strdup(val
);
6041 if (job
->printer
->output_format
)
6043 snprintf(val
, sizeof(val
), "OUTPUT_TYPE=%s", job
->printer
->output_format
);
6044 myenvp
[myenvc
++] = strdup(val
);
6048 if (job
->printer
->ppdfile
)
6050 snprintf(val
, sizeof(val
), "PPD=%s", job
->printer
->ppdfile
);
6051 myenvp
[myenvc
++] = strdup(val
);
6053 #endif /* !CUPS_LITE */
6055 for (attr
= ippFirstAttribute(job
->printer
->attrs
); attr
&& myenvc
< (int)(sizeof(myenvp
) / sizeof(myenvp
[0]) - 1); attr
= ippNextAttribute(job
->printer
->attrs
))
6058 * Convert "attribute-name-default" to "IPP_ATTRIBUTE_NAME_DEFAULT=" and
6059 * "pwg-xxx" to "IPP_PWG_XXX", then add the value(s) from the attribute.
6062 const char *name
= ippGetName(attr
),
6063 /* Attribute name */
6064 *suffix
= strstr(name
, "-default");
6065 /* Suffix on attribute name */
6067 if (strncmp(name
, "pwg-", 4) && (!suffix
|| suffix
[8]))
6075 while (*name
&& valptr
< (val
+ sizeof(val
) - 2))
6080 *valptr
++ = (char)toupper(*name
& 255);
6085 ippAttributeString(attr
, valptr
, sizeof(val
) - (size_t)(valptr
- val
));
6087 myenvp
[myenvc
++] = strdup(val
);
6090 for (attr
= ippFirstAttribute(job
->attrs
); attr
&& myenvc
< (int)(sizeof(myenvp
) / sizeof(myenvp
[0]) - 1); attr
= ippNextAttribute(job
->attrs
))
6093 * Convert "attribute-name" to "IPP_ATTRIBUTE_NAME=" and then add the
6094 * value(s) from the attribute.
6097 const char *name
= ippGetName(attr
);
6098 /* Attribute name */
6108 while (*name
&& valptr
< (val
+ sizeof(val
) - 2))
6113 *valptr
++ = (char)toupper(*name
& 255);
6118 ippAttributeString(attr
, valptr
, sizeof(val
) - (size_t)(valptr
- val
));
6120 myenvp
[myenvc
++] = strdup(val
);
6125 fprintf(stderr
, "[Job %d] Too many environment variables to process job.\n", job
->id
);
6126 job
->state
= IPP_JSTATE_ABORTED
;
6130 myenvp
[myenvc
] = NULL
;
6133 * Now run the program...
6137 status
= _spawnvpe(_P_WAIT
, job
->printer
->command
, myargv
, myenvp
);
6140 if (job
->printer
->device_uri
)
6142 char scheme
[32], /* URI scheme */
6143 userpass
[256], /* username:password (unused) */
6144 host
[256], /* Hostname or IP address */
6145 resource
[256]; /* Resource path */
6146 int port
; /* Port number */
6149 if (httpSeparateURI(HTTP_URI_CODING_ALL
, job
->printer
->device_uri
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, sizeof(resource
)) < HTTP_URI_STATUS_OK
)
6151 fprintf(stderr
, "[Job %d] Bad device URI \"%s\".\n", job
->id
, job
->printer
->device_uri
);
6153 else if (!strcmp(scheme
, "file"))
6155 struct stat fileinfo
; /* See if this is a file or directory... */
6157 if (stat(resource
, &fileinfo
))
6159 if (errno
== ENOENT
)
6161 if ((mystdout
= open(resource
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) >= 0)
6162 fprintf(stderr
, "[Job %d] Saving print command output to \"%s\".\n", job
->id
, resource
);
6164 fprintf(stderr
, "[Job %d] Unable to create \"%s\": %s\n", job
->id
, resource
, strerror(errno
));
6167 fprintf(stderr
, "[Job %d] Unable to access \"%s\": %s\n", job
->id
, resource
, strerror(errno
));
6169 else if (S_ISDIR(fileinfo
.st_mode
))
6171 if ((mystdout
= create_job_file(job
, line
, sizeof(line
), resource
, "prn")) >= 0)
6172 fprintf(stderr
, "[Job %d] Saving print command output to \"%s\".\n", job
->id
, line
);
6174 fprintf(stderr
, "[Job %d] Unable to create \"%s\": %s\n", job
->id
, line
, strerror(errno
));
6176 else if (!S_ISREG(fileinfo
.st_mode
))
6178 if ((mystdout
= open(resource
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) >= 0)
6179 fprintf(stderr
, "[Job %d] Saving print command output to \"%s\".\n", job
->id
, resource
);
6181 fprintf(stderr
, "[Job %d] Unable to create \"%s\": %s\n", job
->id
, resource
, strerror(errno
));
6183 else if ((mystdout
= open(resource
, O_WRONLY
)) >= 0)
6184 fprintf(stderr
, "[Job %d] Saving print command output to \"%s\".\n", job
->id
, resource
);
6186 fprintf(stderr
, "[Job %d] Unable to open \"%s\": %s\n", job
->id
, resource
, strerror(errno
));
6188 else if (!strcmp(scheme
, "socket"))
6190 http_addrlist_t
*addrlist
; /* List of addresses */
6191 char service
[32]; /* Service number */
6193 snprintf(service
, sizeof(service
), "%d", port
);
6195 if ((addrlist
= httpAddrGetList(host
, AF_UNSPEC
, service
)) == NULL
)
6196 fprintf(stderr
, "[Job %d] Unable to find \"%s\": %s\n", job
->id
, host
, cupsLastErrorString());
6197 else if (!httpAddrConnect2(addrlist
, &mystdout
, 30000, &(job
->cancel
)))
6198 fprintf(stderr
, "[Job %d] Unable to connect to \"%s\": %s\n", job
->id
, host
, cupsLastErrorString());
6200 httpAddrFreeList(addrlist
);
6204 fprintf(stderr
, "[Job %d] Unsupported device URI scheme \"%s\".\n", job
->id
, scheme
);
6207 else if ((mystdout
= create_job_file(job
, line
, sizeof(line
), job
->printer
->directory
, "prn")) >= 0)
6209 fprintf(stderr
, "[Job %d] Saving print command output to \"%s\".\n", job
->id
, line
);
6213 mystdout
= open("/dev/null", O_WRONLY
);
6217 fprintf(stderr
, "[Job %d] Unable to create pipe for stderr: %s\n", job
->id
, strerror(errno
));
6218 mypipe
[0] = mypipe
[1] = -1;
6221 if ((pid
= fork()) == 0)
6224 * Child comes here...
6236 execve(job
->printer
->command
, myargv
, myenvp
);
6242 * Unable to fork process...
6245 fprintf(stderr
, "[Job %d] Unable to start job processing command: %s\n", job
->id
, strerror(errno
));
6253 * Free memory used for environment...
6257 free(myenvp
[-- myenvc
]);
6262 * Free memory used for environment...
6266 free(myenvp
[-- myenvc
]);
6269 * Close the output file in the parent process...
6275 * If the pipe exists, read from it until EOF...
6283 while ((bytes
= read(mypipe
[0], endptr
, sizeof(line
) - (size_t)(endptr
- line
) - 1)) > 0)
6288 while ((ptr
= strchr(line
, '\n')) != NULL
)
6290 int level
= 3; /* Message log level */
6294 if (!strncmp(line
, "ATTR:", 5))
6297 * Process job/printer attribute updates.
6300 process_attr_message(job
, line
);
6302 else if (!strncmp(line
, "DEBUG:", 6))
6310 else if (!strncmp(line
, "ERROR:", 6))
6317 job
->message
= strdup(line
+ 6);
6320 else if (!strncmp(line
, "INFO:", 5))
6323 * Informational/progress message...
6329 job
->message
= strdup(line
+ 5);
6333 else if (!strncmp(line
, "STATE:", 6))
6336 * Process printer-state-reasons keywords.
6339 process_state_message(job
, line
);
6342 if (Verbosity
>= level
)
6343 fprintf(stderr
, "[Job %d] Command - %s\n", job
->id
, line
);
6347 memmove(line
, ptr
, (size_t)(endptr
- ptr
));
6357 * Wait for child to complete...
6360 # ifdef HAVE_WAITPID
6361 while (waitpid(pid
, &status
, 0) < 0);
6363 while (wait(&status
) < 0);
6364 # endif /* HAVE_WAITPID */
6371 if (WIFEXITED(status
))
6372 #endif /* !_WIN32 */
6373 fprintf(stderr
, "[Job %d] Command \"%s\" exited with status %d.\n", job
->id
, job
->printer
->command
, WEXITSTATUS(status
));
6376 fprintf(stderr
, "[Job %d] Command \"%s\" terminated with signal %d.\n", job
->id
, job
->printer
->command
, WTERMSIG(status
));
6377 #endif /* !_WIN32 */
6378 job
->state
= IPP_JSTATE_ABORTED
;
6380 else if (status
< 0)
6381 job
->state
= IPP_JSTATE_ABORTED
;
6383 fprintf(stderr
, "[Job %d] Command \"%s\" completed successfully.\n", job
->id
, job
->printer
->command
);
6386 * Report the total processing time...
6389 gettimeofday(&end
, NULL
);
6391 fprintf(stderr
, "[Job %d] Processing time was %.3f seconds.\n", job
->id
, end
.tv_sec
- start
.tv_sec
+ 0.000001 * (end
.tv_usec
- start
.tv_usec
));
6396 * Sleep for a random amount of time to simulate job processing.
6399 sleep((unsigned)(5 + (CUPS_RAND() % 11)));
6403 job
->state
= IPP_JSTATE_CANCELED
;
6404 else if (job
->state
== IPP_JSTATE_PROCESSING
)
6405 job
->state
= IPP_JSTATE_COMPLETED
;
6409 job
->completed
= time(NULL
);
6410 job
->printer
->state
= IPP_PSTATE_IDLE
;
6411 job
->printer
->active_job
= NULL
;
6418 * 'process_state_message()' - Process a STATE: message from a command.
6422 process_state_message(
6423 ippeve_job_t
*job
, /* I - Job */
6424 char *message
) /* I - Message */
6426 int i
; /* Looping var */
6427 ippeve_preason_t state_reasons
, /* printer-state-reasons values */
6428 bit
; /* Current reason bit */
6429 char *ptr
, /* Pointer into message */
6430 *next
; /* Next keyword in message */
6431 int remove
; /* Non-zero if we are removing keywords */
6435 * Skip leading "STATE:" and any whitespace...
6438 for (message
+= 6; *message
; message
++)
6439 if (*message
!= ' ' && *message
!= '\t')
6443 * Support the following forms of message:
6445 * "keyword[,keyword,...]" to set the printer-state-reasons value(s).
6447 * "-keyword[,keyword,...]" to remove keywords.
6449 * "+keyword[,keyword,...]" to add keywords.
6451 * Keywords may or may not have a suffix (-report, -warning, -error) per
6455 if (*message
== '-')
6458 state_reasons
= job
->printer
->state_reasons
;
6461 else if (*message
== '+')
6464 state_reasons
= job
->printer
->state_reasons
;
6470 state_reasons
= IPPEVE_PREASON_NONE
;
6475 if ((next
= strchr(message
, ',')) != NULL
)
6478 if ((ptr
= strstr(message
, "-error")) != NULL
)
6480 else if ((ptr
= strstr(message
, "-report")) != NULL
)
6482 else if ((ptr
= strstr(message
, "-warning")) != NULL
)
6485 for (i
= 0, bit
= 1; i
< (int)(sizeof(ippeve_preason_strings
) / sizeof(ippeve_preason_strings
[0])); i
++, bit
*= 2)
6487 if (!strcmp(message
, ippeve_preason_strings
[i
]))
6490 state_reasons
&= ~bit
;
6492 state_reasons
|= bit
;
6502 job
->printer
->state_reasons
= state_reasons
;
6507 * 'register_printer()' - Register a printer object via Bonjour.
6510 static int /* O - 1 on success, 0 on error */
6512 ippeve_printer_t
*printer
, /* I - Printer */
6513 const char *subtypes
) /* I - Service subtype(s) */
6515 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
6516 ippeve_txt_t ipp_txt
; /* Bonjour IPP TXT record */
6517 int i
, /* Looping var */
6518 count
; /* Number of values */
6519 ipp_attribute_t
*color_supported
,
6520 *document_format_supported
,
6522 *printer_make_and_model
,
6526 *urf_supported
; /* Printer attributes */
6527 const char *value
; /* Value string */
6528 char formats
[252], /* List of supported formats */
6529 urf
[252], /* List of supported URF values */
6530 *ptr
; /* Pointer into string */
6532 color_supported
= ippFindAttribute(printer
->attrs
, "color-supported", IPP_TAG_BOOLEAN
);
6533 document_format_supported
= ippFindAttribute(printer
->attrs
, "document-format-supported", IPP_TAG_MIMETYPE
);
6534 printer_location
= ippFindAttribute(printer
->attrs
, "printer-location", IPP_TAG_TEXT
);
6535 printer_make_and_model
= ippFindAttribute(printer
->attrs
, "printer-make-and-model", IPP_TAG_TEXT
);
6536 printer_more_info
= ippFindAttribute(printer
->attrs
, "printer-more-info", IPP_TAG_URI
);
6537 printer_uuid
= ippFindAttribute(printer
->attrs
, "printer-uuid", IPP_TAG_URI
);
6538 sides_supported
= ippFindAttribute(printer
->attrs
, "sides-supported", IPP_TAG_KEYWORD
);
6539 urf_supported
= ippFindAttribute(printer
->attrs
, "urf-supported", IPP_TAG_KEYWORD
);
6541 for (i
= 0, count
= ippGetCount(document_format_supported
), ptr
= formats
; i
< count
; i
++)
6543 value
= ippGetString(document_format_supported
, i
, NULL
);
6545 if (!strcasecmp(value
, "application/octet-stream"))
6548 if (ptr
> formats
&& ptr
< (formats
+ sizeof(formats
) - 1))
6551 strlcpy(ptr
, value
, sizeof(formats
) - (size_t)(ptr
- formats
));
6554 if (ptr
>= (formats
+ sizeof(formats
) - 1))
6559 for (i
= 0, count
= ippGetCount(urf_supported
), ptr
= urf
; i
< count
; i
++)
6561 value
= ippGetString(urf_supported
, i
, NULL
);
6563 if (ptr
> urf
&& ptr
< (urf
+ sizeof(urf
) - 1))
6566 strlcpy(ptr
, value
, sizeof(urf
) - (size_t)(ptr
- urf
));
6569 if (ptr
>= (urf
+ sizeof(urf
) - 1))
6573 #endif /* HAVE_DNSSD || HAVE_AVAHI */
6575 DNSServiceErrorType error
; /* Error from Bonjour */
6576 char regtype
[256]; /* Bonjour service type */
6580 * Build the TXT record for IPP...
6583 TXTRecordCreate(&ipp_txt
, 1024, NULL
);
6584 TXTRecordSetValue(&ipp_txt
, "rp", 9, "ipp/print");
6585 if ((value
= ippGetString(printer_make_and_model
, 0, NULL
)) != NULL
)
6586 TXTRecordSetValue(&ipp_txt
, "ty", (uint8_t)strlen(value
), value
);
6587 if ((value
= ippGetString(printer_more_info
, 0, NULL
)) != NULL
)
6588 TXTRecordSetValue(&ipp_txt
, "adminurl", (uint8_t)strlen(value
), value
);
6589 if ((value
= ippGetString(printer_location
, 0, NULL
)) != NULL
)
6590 TXTRecordSetValue(&ipp_txt
, "note", (uint8_t)strlen(value
), value
);
6591 TXTRecordSetValue(&ipp_txt
, "pdl", (uint8_t)strlen(formats
), formats
);
6592 TXTRecordSetValue(&ipp_txt
, "Color", 1, ippGetBoolean(color_supported
, 0) ? "T" : "F");
6593 TXTRecordSetValue(&ipp_txt
, "Duplex", 1, ippGetCount(sides_supported
) > 1 ? "T" : "F");
6594 if ((value
= ippGetString(printer_uuid
, 0, NULL
)) != NULL
)
6595 TXTRecordSetValue(&ipp_txt
, "UUID", (uint8_t)strlen(value
) - 9, value
+ 9);
6597 TXTRecordSetValue(&ipp_txt
, "TLS", 3, "1.2");
6598 # endif /* HAVE_SSL */
6600 TXTRecordSetValue(&ipp_txt
, "URF", (uint8_t)strlen(urf
), urf
);
6601 TXTRecordSetValue(&ipp_txt
, "txtvers", 1, "1");
6602 TXTRecordSetValue(&ipp_txt
, "qtotal", 1, "1");
6605 * Register the _printer._tcp (LPD) service type with a port number of 0 to
6606 * defend our service name but not actually support LPD...
6609 printer
->printer_ref
= DNSSDMaster
;
6611 if ((error
= DNSServiceRegister(&(printer
->printer_ref
), kDNSServiceFlagsShareConnection
, 0 /* interfaceIndex */, printer
->dnssd_name
, "_printer._tcp", NULL
/* domain */, NULL
/* host */, 0 /* port */, 0 /* txtLen */, NULL
/* txtRecord */, (DNSServiceRegisterReply
)dnssd_callback
, printer
)) != kDNSServiceErr_NoError
)
6613 _cupsLangPrintf(stderr
, _("Unable to register \"%s.%s\": %d"), printer
->dnssd_name
, "_printer._tcp", error
);
6618 * Then register the _ipp._tcp (IPP) service type with the real port number to
6619 * advertise our IPP printer...
6622 printer
->ipp_ref
= DNSSDMaster
;
6624 if (subtypes
&& *subtypes
)
6625 snprintf(regtype
, sizeof(regtype
), "_ipp._tcp,%s", subtypes
);
6627 strlcpy(regtype
, "_ipp._tcp", sizeof(regtype
));
6629 if ((error
= DNSServiceRegister(&(printer
->ipp_ref
), kDNSServiceFlagsShareConnection
, 0 /* interfaceIndex */, printer
->dnssd_name
, regtype
, NULL
/* domain */, NULL
/* host */, htons(printer
->port
), TXTRecordGetLength(&ipp_txt
), TXTRecordGetBytesPtr(&ipp_txt
), (DNSServiceRegisterReply
)dnssd_callback
, printer
)) != kDNSServiceErr_NoError
)
6631 _cupsLangPrintf(stderr
, _("Unable to register \"%s.%s\": %d"), printer
->dnssd_name
, regtype
, error
);
6637 * Then register the _ipps._tcp (IPP) service type with the real port number to
6638 * advertise our IPPS printer...
6641 printer
->ipps_ref
= DNSSDMaster
;
6643 if (subtypes
&& *subtypes
)
6644 snprintf(regtype
, sizeof(regtype
), "_ipps._tcp,%s", subtypes
);
6646 strlcpy(regtype
, "_ipps._tcp", sizeof(regtype
));
6648 if ((error
= DNSServiceRegister(&(printer
->ipps_ref
), kDNSServiceFlagsShareConnection
, 0 /* interfaceIndex */, printer
->dnssd_name
, regtype
, NULL
/* domain */, NULL
/* host */, htons(printer
->port
), TXTRecordGetLength(&ipp_txt
), TXTRecordGetBytesPtr(&ipp_txt
), (DNSServiceRegisterReply
)dnssd_callback
, printer
)) != kDNSServiceErr_NoError
)
6650 _cupsLangPrintf(stderr
, _("Unable to register \"%s.%s\": %d"), printer
->dnssd_name
, regtype
, error
);
6653 # endif /* HAVE_SSL */
6656 * Similarly, register the _http._tcp,_printer (HTTP) service type with the
6657 * real port number to advertise our IPP printer...
6660 printer
->http_ref
= DNSSDMaster
;
6662 if ((error
= DNSServiceRegister(&(printer
->http_ref
), kDNSServiceFlagsShareConnection
, 0 /* interfaceIndex */, printer
->dnssd_name
, "_http._tcp,_printer", NULL
/* domain */, NULL
/* host */, htons(printer
->port
), 0 /* txtLen */, NULL
/* txtRecord */, (DNSServiceRegisterReply
)dnssd_callback
, printer
)) != kDNSServiceErr_NoError
)
6664 _cupsLangPrintf(stderr
, _("Unable to register \"%s.%s\": %d"), printer
->dnssd_name
, "_http._tcp,_printer", error
);
6668 TXTRecordDeallocate(&ipp_txt
);
6670 #elif defined(HAVE_AVAHI)
6671 char temp
[256]; /* Subtype service string */
6674 * Create the TXT record...
6678 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "rp=ipp/print");
6679 if ((value
= ippGetString(printer_make_and_model
, 0, NULL
)) != NULL
)
6680 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "ty=%s", value
);
6681 if ((value
= ippGetString(printer_more_info
, 0, NULL
)) != NULL
)
6682 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "adminurl=%s", value
);
6683 if ((value
= ippGetString(printer_location
, 0, NULL
)) != NULL
)
6684 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "note=%s", value
);
6685 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "pdl=%s", formats
);
6686 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "Color=%s", ippGetBoolean(color_supported
, 0) ? "T" : "F");
6687 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "Duplex=%s", ippGetCount(sides_supported
) > 1 ? "T" : "F");
6688 if ((value
= ippGetString(printer_uuid
, 0, NULL
)) != NULL
)
6689 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "UUID=%s", value
+ 9);
6691 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "TLS=1.2");
6692 # endif /* HAVE_SSL */
6694 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "URF=%s", urf
);
6695 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "txtvers=1");
6696 ipp_txt
= avahi_string_list_add_printf(ipp_txt
, "qtotal=1");
6699 * Register _printer._tcp (LPD) with port 0 to reserve the service name...
6702 avahi_threaded_poll_lock(DNSSDMaster
);
6704 printer
->ipp_ref
= avahi_entry_group_new(DNSSDClient
, dnssd_callback
, NULL
);
6706 avahi_entry_group_add_service_strlst(printer
->ipp_ref
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, printer
->dnssd_name
, "_printer._tcp", NULL
, NULL
, 0, NULL
);
6709 * Then register the _ipp._tcp (IPP)...
6712 avahi_entry_group_add_service_strlst(printer
->ipp_ref
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, printer
->dnssd_name
, "_ipp._tcp", NULL
, NULL
, printer
->port
, ipp_txt
);
6713 if (subtypes
&& *subtypes
)
6715 char *temptypes
= strdup(subtypes
), *start
, *end
;
6717 for (start
= temptypes
; *start
; start
= end
)
6719 if ((end
= strchr(start
, ',')) != NULL
)
6722 end
= start
+ strlen(start
);
6724 snprintf(temp
, sizeof(temp
), "%s._sub._ipp._tcp", start
);
6725 avahi_entry_group_add_service_subtype(printer
->ipp_ref
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, printer
->dnssd_name
, "_ipp._tcp", NULL
, temp
);
6733 * _ipps._tcp (IPPS) for secure printing...
6736 avahi_entry_group_add_service_strlst(printer
->ipp_ref
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, printer
->dnssd_name
, "_ipps._tcp", NULL
, NULL
, printer
->port
, ipp_txt
);
6737 if (subtypes
&& *subtypes
)
6739 char *temptypes
= strdup(subtypes
), *start
, *end
;
6741 for (start
= temptypes
; *start
; start
= end
)
6743 if ((end
= strchr(start
, ',')) != NULL
)
6746 end
= start
+ strlen(start
);
6748 snprintf(temp
, sizeof(temp
), "%s._sub._ipps._tcp", start
);
6749 avahi_entry_group_add_service_subtype(printer
->ipp_ref
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, printer
->dnssd_name
, "_ipps._tcp", NULL
, temp
);
6754 #endif /* HAVE_SSL */
6757 * Finally _http.tcp (HTTP) for the web interface...
6760 avahi_entry_group_add_service_strlst(printer
->ipp_ref
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, printer
->dnssd_name
, "_http._tcp", NULL
, NULL
, printer
->port
, NULL
);
6761 avahi_entry_group_add_service_subtype(printer
->ipp_ref
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, printer
->dnssd_name
, "_http._tcp", NULL
, "_printer._sub._http._tcp");
6767 avahi_entry_group_commit(printer
->ipp_ref
);
6768 avahi_threaded_poll_unlock(DNSSDMaster
);
6770 avahi_string_list_free(ipp_txt
);
6771 #endif /* HAVE_DNSSD */
6778 * 'respond_http()' - Send a HTTP response.
6781 int /* O - 1 on success, 0 on failure */
6783 ippeve_client_t
*client
, /* I - Client */
6784 http_status_t code
, /* I - HTTP status of response */
6785 const char *content_encoding
, /* I - Content-Encoding of response */
6786 const char *type
, /* I - MIME media type of response */
6787 size_t length
) /* I - Length of response */
6789 char message
[1024]; /* Text message */
6792 fprintf(stderr
, "%s %s\n", client
->hostname
, httpStatus(code
));
6794 if (code
== HTTP_STATUS_CONTINUE
)
6797 * 100-continue doesn't send any headers...
6800 return (httpWriteResponse(client
->http
, HTTP_STATUS_CONTINUE
) == 0);
6804 * Format an error message...
6807 if (!type
&& !length
&& code
!= HTTP_STATUS_OK
&& code
!= HTTP_STATUS_SWITCHING_PROTOCOLS
)
6809 snprintf(message
, sizeof(message
), "%d - %s\n", code
, httpStatus(code
));
6811 type
= "text/plain";
6812 length
= strlen(message
);
6818 * Send the HTTP response header...
6821 httpClearFields(client
->http
);
6823 if (code
== HTTP_STATUS_METHOD_NOT_ALLOWED
||
6824 client
->operation
== HTTP_STATE_OPTIONS
)
6825 httpSetField(client
->http
, HTTP_FIELD_ALLOW
, "GET, HEAD, OPTIONS, POST");
6829 if (!strcmp(type
, "text/html"))
6830 httpSetField(client
->http
, HTTP_FIELD_CONTENT_TYPE
,
6831 "text/html; charset=utf-8");
6833 httpSetField(client
->http
, HTTP_FIELD_CONTENT_TYPE
, type
);
6835 if (content_encoding
)
6836 httpSetField(client
->http
, HTTP_FIELD_CONTENT_ENCODING
, content_encoding
);
6839 httpSetLength(client
->http
, length
);
6841 if (httpWriteResponse(client
->http
, code
) < 0)
6845 * Send the response data...
6851 * Send a plain text message.
6854 if (httpPrintf(client
->http
, "%s", message
) < 0)
6857 if (httpWrite2(client
->http
, "", 0) < 0)
6860 else if (client
->response
)
6863 * Send an IPP response...
6866 debug_attributes("Response", client
->response
, 2);
6868 ippSetState(client
->response
, IPP_STATE_IDLE
);
6870 if (ippWrite(client
->http
, client
->response
) != IPP_STATE_DATA
)
6879 * 'respond_ipp()' - Send an IPP response.
6883 respond_ipp(ippeve_client_t
*client
, /* I - Client */
6884 ipp_status_t status
, /* I - status-code */
6885 const char *message
, /* I - printf-style status-message */
6886 ...) /* I - Additional args as needed */
6888 const char *formatted
= NULL
; /* Formatted message */
6891 ippSetStatusCode(client
->response
, status
);
6895 va_list ap
; /* Pointer to additional args */
6896 ipp_attribute_t
*attr
; /* New status-message attribute */
6898 va_start(ap
, message
);
6899 if ((attr
= ippFindAttribute(client
->response
, "status-message", IPP_TAG_TEXT
)) != NULL
)
6900 ippSetStringfv(client
->response
, &attr
, 0, message
, ap
);
6902 attr
= ippAddStringfv(client
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
, "status-message", NULL
, message
, ap
);
6905 formatted
= ippGetString(attr
, 0, NULL
);
6909 fprintf(stderr
, "%s %s %s (%s)\n", client
->hostname
, ippOpString(client
->operation_id
), ippErrorString(status
), formatted
);
6911 fprintf(stderr
, "%s %s %s\n", client
->hostname
, ippOpString(client
->operation_id
), ippErrorString(status
));
6916 * 'respond_unsupported()' - Respond with an unsupported attribute.
6920 respond_unsupported(
6921 ippeve_client_t
*client
, /* I - Client */
6922 ipp_attribute_t
*attr
) /* I - Atribute */
6924 ipp_attribute_t
*temp
; /* Copy of attribute */
6927 respond_ipp(client
, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES
, "Unsupported %s %s%s value.", ippGetName(attr
), ippGetCount(attr
) > 1 ? "1setOf " : "", ippTagString(ippGetValueTag(attr
)));
6929 temp
= ippCopyAttribute(client
->response
, attr
, 0);
6930 ippSetGroupTag(client
->response
, &temp
, IPP_TAG_UNSUPPORTED_GROUP
);
6935 * 'run_printer()' - Run the printer service.
6939 run_printer(ippeve_printer_t
*printer
) /* I - Printer */
6941 int num_fds
; /* Number of file descriptors */
6942 struct pollfd polldata
[3]; /* poll() data */
6943 int timeout
; /* Timeout for poll() */
6944 ippeve_client_t
*client
; /* New client */
6948 * Setup poll() data for the Bonjour service socket and IPv4/6 listeners...
6951 polldata
[0].fd
= printer
->ipv4
;
6952 polldata
[0].events
= POLLIN
;
6954 polldata
[1].fd
= printer
->ipv6
;
6955 polldata
[1].events
= POLLIN
;
6960 polldata
[num_fds
].fd
= DNSServiceRefSockFD(DNSSDMaster
);
6961 polldata
[num_fds
++].events
= POLLIN
;
6962 #endif /* HAVE_DNSSD */
6965 * Loop until we are killed or have a hard error...
6970 if (cupsArrayCount(printer
->jobs
))
6975 if (poll(polldata
, (nfds_t
)num_fds
, timeout
) < 0 && errno
!= EINTR
)
6977 perror("poll() failed");
6981 if (polldata
[0].revents
& POLLIN
)
6983 if ((client
= create_client(printer
, printer
->ipv4
)) != NULL
)
6985 _cups_thread_t t
= _cupsThreadCreate((_cups_thread_func_t
)process_client
, client
);
6989 _cupsThreadDetach(t
);
6993 perror("Unable to create client thread");
6994 delete_client(client
);
6999 if (polldata
[1].revents
& POLLIN
)
7001 if ((client
= create_client(printer
, printer
->ipv6
)) != NULL
)
7003 _cups_thread_t t
= _cupsThreadCreate((_cups_thread_func_t
)process_client
, client
);
7007 _cupsThreadDetach(t
);
7011 perror("Unable to create client thread");
7012 delete_client(client
);
7018 if (polldata
[2].revents
& POLLIN
)
7019 DNSServiceProcessResult(DNSSDMaster
);
7020 #endif /* HAVE_DNSSD */
7023 * Clean out old jobs...
7026 clean_jobs(printer
);
7032 * 'show_media()' - Show media load state.
7035 static int /* O - 1 on success, 0 on failure */
7036 show_media(ippeve_client_t
*client
) /* I - Client connection */
7038 ippeve_printer_t
*printer
= client
->printer
;
7040 int i
, j
, /* Looping vars */
7041 num_ready
, /* Number of ready media */
7042 num_sizes
, /* Number of media sizes */
7043 num_sources
, /* Number of media sources */
7044 num_types
; /* Number of media types */
7045 ipp_attribute_t
*media_col_ready
,/* media-col-ready attribute */
7046 *media_ready
, /* media-ready attribute */
7047 *media_sizes
, /* media-supported attribute */
7048 *media_sources
, /* media-source-supported attribute */
7049 *media_types
, /* media-type-supported attribute */
7050 *input_tray
; /* printer-input-tray attribute */
7051 ipp_t
*media_col
; /* media-col value */
7052 const char *media_size
, /* media value */
7053 *media_source
, /* media-source value */
7054 *media_type
, /* media-type value */
7055 *ready_size
, /* media-col-ready media-size[-name] value */
7056 *ready_source
, /* media-col-ready media-source value */
7057 *ready_tray
, /* printer-input-tray value */
7058 *ready_type
; /* media-col-ready media-type value */
7059 char tray_str
[1024], /* printer-input-tray string value */
7060 *tray_ptr
; /* Pointer into value */
7061 int tray_len
; /* Length of printer-input-tray value */
7062 int ready_sheets
; /* printer-input-tray sheets value */
7063 int num_options
= 0;/* Number of form options */
7064 cups_option_t
*options
= NULL
;/* Form options */
7065 static const int sheets
[] = /* Number of sheets */
7077 if (!respond_http(client
, HTTP_STATUS_OK
, NULL
, "text/html", 0))
7080 html_header(client
, printer
->name
, 0);
7082 if ((media_col_ready
= ippFindAttribute(printer
->attrs
, "media-col-ready", IPP_TAG_BEGIN_COLLECTION
)) == NULL
)
7084 html_printf(client
, "<p>Error: No media-col-ready defined for printer.</p>\n");
7085 html_footer(client
);
7089 media_ready
= ippFindAttribute(printer
->attrs
, "media-ready", IPP_TAG_ZERO
);
7091 if ((media_sizes
= ippFindAttribute(printer
->attrs
, "media-supported", IPP_TAG_ZERO
)) == NULL
)
7093 html_printf(client
, "<p>Error: No media-supported defined for printer.</p>\n");
7094 html_footer(client
);
7098 if ((media_sources
= ippFindAttribute(printer
->attrs
, "media-source-supported", IPP_TAG_ZERO
)) == NULL
)
7100 html_printf(client
, "<p>Error: No media-source-supported defined for printer.</p>\n");
7101 html_footer(client
);
7105 if ((media_types
= ippFindAttribute(printer
->attrs
, "media-type-supported", IPP_TAG_ZERO
)) == NULL
)
7107 html_printf(client
, "<p>Error: No media-type-supported defined for printer.</p>\n");
7108 html_footer(client
);
7112 if ((input_tray
= ippFindAttribute(printer
->attrs
, "printer-input-tray", IPP_TAG_STRING
)) == NULL
)
7114 html_printf(client
, "<p>Error: No printer-input-tray defined for printer.</p>\n");
7115 html_footer(client
);
7119 num_ready
= ippGetCount(media_col_ready
);
7120 num_sizes
= ippGetCount(media_sizes
);
7121 num_sources
= ippGetCount(media_sources
);
7122 num_types
= ippGetCount(media_types
);
7124 if (num_sources
!= ippGetCount(input_tray
))
7126 html_printf(client
, "<p>Error: Different number of trays in media-source-supported and printer-input-tray defined for printer.</p>\n");
7127 html_footer(client
);
7132 * Process form data if present...
7135 if (printer
->web_forms
)
7136 num_options
= parse_options(client
, &options
);
7138 if (num_options
> 0)
7141 * WARNING: A real printer/server implementation MUST NOT implement
7142 * media updates via a GET request - GET requests are supposed to be
7143 * idempotent (without side-effects) and we obviously are not
7144 * authenticating access here. This form is provided solely to
7145 * enable testing and development!
7148 char name
[255]; /* Form name */
7149 const char *val
; /* Form value */
7150 pwg_media_t
*media
; /* Media info */
7152 _cupsRWLockWrite(&printer
->rwlock
);
7154 ippDeleteAttribute(printer
->attrs
, media_col_ready
);
7155 media_col_ready
= NULL
;
7159 ippDeleteAttribute(printer
->attrs
, media_ready
);
7163 printer
->state_reasons
&= (ippeve_preason_t
)~(IPPEVE_PREASON_MEDIA_LOW
| IPPEVE_PREASON_MEDIA_EMPTY
| IPPEVE_PREASON_MEDIA_NEEDED
);
7165 for (i
= 0; i
< num_sources
; i
++)
7167 media_source
= ippGetString(media_sources
, i
, NULL
);
7169 if (!strcmp(media_source
, "auto") || !strcmp(media_source
, "manual") || strstr(media_source
, "-man") != NULL
)
7172 snprintf(name
, sizeof(name
), "size%d", i
);
7173 if ((media_size
= cupsGetOption(name
, num_options
, options
)) != NULL
&& (media
= pwgMediaForPWG(media_size
)) != NULL
)
7175 snprintf(name
, sizeof(name
), "type%d", i
);
7176 if ((media_type
= cupsGetOption(name
, num_options
, options
)) != NULL
&& !*media_type
)
7180 ippSetString(printer
->attrs
, &media_ready
, ippGetCount(media_ready
), media_size
);
7182 media_ready
= ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-ready", NULL
, media_size
);
7184 media_col
= create_media_col(media_size
, media_source
, media_type
, media
->width
, media
->length
, -1, -1, -1, -1);
7186 if (media_col_ready
)
7187 ippSetCollection(printer
->attrs
, &media_col_ready
, ippGetCount(media_col_ready
), media_col
);
7189 media_col_ready
= ippAddCollection(printer
->attrs
, IPP_TAG_PRINTER
, "media-col-ready", media_col
);
7190 ippDelete(media_col
);
7195 snprintf(name
, sizeof(name
), "level%d", i
);
7196 if ((val
= cupsGetOption(name
, num_options
, options
)) != NULL
)
7197 ready_sheets
= atoi(val
);
7201 snprintf(tray_str
, sizeof(tray_str
), "type=sheetFeedAuto%sRemovableTray;mediafeed=%d;mediaxfeed=%d;maxcapacity=%d;level=%d;status=0;name=%s;", !strcmp(media_source
, "by-pass-tray") ? "Non" : "", media
? media
->length
: 0, media
? media
->width
: 0, strcmp(media_source
, "by-pass-tray") ? 250 : 25, ready_sheets
, media_source
);
7203 ippSetOctetString(printer
->attrs
, &input_tray
, i
, tray_str
, (int)strlen(tray_str
));
7205 if (ready_sheets
== 0)
7207 printer
->state_reasons
|= IPPEVE_PREASON_MEDIA_EMPTY
;
7208 if (printer
->active_job
)
7209 printer
->state_reasons
|= IPPEVE_PREASON_MEDIA_NEEDED
;
7211 else if (ready_sheets
< 25 && ready_sheets
> 0)
7212 printer
->state_reasons
|= IPPEVE_PREASON_MEDIA_LOW
;
7215 if (!media_col_ready
)
7216 media_col_ready
= ippAddOutOfBand(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
, "media-col-ready");
7219 media_ready
= ippAddOutOfBand(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
, "media-ready");
7221 _cupsRWUnlock(&printer
->rwlock
);
7224 if (printer
->web_forms
)
7225 html_printf(client
, "<form method=\"GET\" action=\"/media\">\n");
7227 html_printf(client
, "<table class=\"form\" summary=\"Media\">\n");
7228 for (i
= 0; i
< num_sources
; i
++)
7230 media_source
= ippGetString(media_sources
, i
, NULL
);
7232 if (!strcmp(media_source
, "auto") || !strcmp(media_source
, "manual") || strstr(media_source
, "-man") != NULL
)
7235 for (j
= 0, ready_size
= NULL
, ready_type
= NULL
; j
< num_ready
; j
++)
7237 media_col
= ippGetCollection(media_col_ready
, j
);
7238 ready_size
= ippGetString(ippFindAttribute(media_col
, "media-size-name", IPP_TAG_ZERO
), 0, NULL
);
7239 ready_source
= ippGetString(ippFindAttribute(media_col
, "media-source", IPP_TAG_ZERO
), 0, NULL
);
7240 ready_type
= ippGetString(ippFindAttribute(media_col
, "media-type", IPP_TAG_ZERO
), 0, NULL
);
7242 if (ready_source
&& !strcmp(ready_source
, media_source
))
7245 ready_source
= NULL
;
7250 html_printf(client
, "<tr><th>%s:</th>", media_source
);
7256 if (printer
->web_forms
)
7258 html_printf(client
, "<td><select name=\"size%d\"><option value=\"\">None</option>", i
);
7259 for (j
= 0; j
< num_sizes
; j
++)
7261 media_size
= ippGetString(media_sizes
, j
, NULL
);
7263 html_printf(client
, "<option%s>%s</option>", (ready_size
&& !strcmp(ready_size
, media_size
)) ? " selected" : "", media_size
);
7265 html_printf(client
, "</select>");
7268 html_printf(client
, "<td>%s", ready_size
);
7274 if (printer
->web_forms
)
7276 html_printf(client
, " <select name=\"type%d\"><option value=\"\">None</option>", i
);
7277 for (j
= 0; j
< num_types
; j
++)
7279 media_type
= ippGetString(media_types
, j
, NULL
);
7281 html_printf(client
, "<option%s>%s</option>", (ready_type
&& !strcmp(ready_type
, media_type
)) ? " selected" : "", media_type
);
7283 html_printf(client
, "</select>");
7285 else if (ready_type
)
7286 html_printf(client
, ", %s", ready_type
);
7289 * Level/sheets loaded...
7292 if ((ready_tray
= ippGetOctetString(input_tray
, i
, &tray_len
)) != NULL
)
7294 if (tray_len
> (int)(sizeof(tray_str
) - 1))
7295 tray_len
= (int)sizeof(tray_str
) - 1;
7296 memcpy(tray_str
, ready_tray
, (size_t)tray_len
);
7297 tray_str
[tray_len
] = '\0';
7299 if ((tray_ptr
= strstr(tray_str
, "level=")) != NULL
)
7300 ready_sheets
= atoi(tray_ptr
+ 6);
7307 if (printer
->web_forms
)
7309 html_printf(client
, " <select name=\"level%d\">", i
);
7310 for (j
= 0; j
< (int)(sizeof(sheets
) / sizeof(sheets
[0])); j
++)
7312 if (!strcmp(media_source
, "by-pass-tray") && sheets
[j
] > 25)
7316 html_printf(client
, "<option value=\"%d\"%s>Unknown</option>", sheets
[j
], sheets
[j
] == ready_sheets
? " selected" : "");
7318 html_printf(client
, "<option value=\"%d\"%s>%d sheets</option>", sheets
[j
], sheets
[j
] == ready_sheets
? " selected" : "", sheets
[j
]);
7320 html_printf(client
, "</select></td></tr>\n");
7322 else if (ready_sheets
== 1)
7323 html_printf(client
, ", 1 sheet</td></tr>\n");
7324 else if (ready_sheets
> 0)
7325 html_printf(client
, ", %d sheets</td></tr>\n", ready_sheets
);
7327 html_printf(client
, "</td></tr>\n");
7330 if (printer
->web_forms
)
7332 html_printf(client
, "<tr><td></td><td><input type=\"submit\" value=\"Update Media\">");
7333 if (num_options
> 0)
7334 html_printf(client
, " <span class=\"badge\" id=\"status\">Media updated.</span>\n");
7335 html_printf(client
, "</td></tr></table></form>\n");
7337 if (num_options
> 0)
7338 html_printf(client
, "<script>\n"
7339 "setTimeout(hide_status, 3000);\n"
7340 "function hide_status() {\n"
7341 " var status = document.getElementById('status');\n"
7342 " status.style.display = 'none';\n"
7347 html_printf(client
, "</table>\n");
7349 html_footer(client
);
7356 * 'show_status()' - Show printer/system state.
7359 static int /* O - 1 on success, 0 on failure */
7360 show_status(ippeve_client_t
*client
) /* I - Client connection */
7362 ippeve_printer_t
*printer
= client
->printer
;
7364 ippeve_job_t
*job
; /* Current job */
7365 int i
; /* Looping var */
7366 ippeve_preason_t reason
; /* Current reason */
7367 static const char * const reasons
[] = /* Reason strings */
7371 "Input Tray Missing",
7372 "Marker Supply Empty",
7373 "Marker Supply Low",
7374 "Marker Waste Almost Full",
7375 "Marker Waste Full",
7386 static const char * const state_colors
[] =
7387 { /* State colors */
7389 "#EE0", /* Processing */
7390 "#C00" /* Stopped */
7394 if (!respond_http(client
, HTTP_STATUS_OK
, NULL
, "text/html", 0))
7397 html_header(client
, printer
->name
, printer
->state
== IPP_PSTATE_PROCESSING
? 5 : 15);
7398 html_printf(client
, "<h1><img style=\"background: %s; border-radius: 10px; float: left; margin-right: 10px; padding: 10px;\" src=\"/icon.png\" width=\"64\" height=\"64\">%s Jobs</h1>\n", state_colors
[printer
->state
- IPP_PSTATE_IDLE
], printer
->name
);
7399 html_printf(client
, "<p>%s, %d job(s).", printer
->state
== IPP_PSTATE_IDLE
? "Idle" : printer
->state
== IPP_PSTATE_PROCESSING
? "Printing" : "Stopped", cupsArrayCount(printer
->jobs
));
7400 for (i
= 0, reason
= 1; i
< (int)(sizeof(reasons
) / sizeof(reasons
[0])); i
++, reason
<<= 1)
7401 if (printer
->state_reasons
& reason
)
7402 html_printf(client
, "\n<br> %s", reasons
[i
]);
7403 html_printf(client
, "</p>\n");
7405 if (cupsArrayCount(printer
->jobs
) > 0)
7407 _cupsRWLockRead(&(printer
->rwlock
));
7409 html_printf(client
, "<table class=\"striped\" summary=\"Jobs\"><thead><tr><th>Job #</th><th>Name</th><th>Owner</th><th>Status</th></tr></thead><tbody>\n");
7410 for (job
= (ippeve_job_t
*)cupsArrayFirst(printer
->jobs
); job
; job
= (ippeve_job_t
*)cupsArrayNext(printer
->jobs
))
7412 char when
[256], /* When job queued/started/finished */
7413 hhmmss
[64]; /* Time HH:MM:SS */
7417 case IPP_JSTATE_PENDING
:
7418 case IPP_JSTATE_HELD
:
7419 snprintf(when
, sizeof(when
), "Queued at %s", time_string(job
->created
, hhmmss
, sizeof(hhmmss
)));
7421 case IPP_JSTATE_PROCESSING
:
7422 case IPP_JSTATE_STOPPED
:
7423 snprintf(when
, sizeof(when
), "Started at %s", time_string(job
->processing
, hhmmss
, sizeof(hhmmss
)));
7425 case IPP_JSTATE_ABORTED
:
7426 snprintf(when
, sizeof(when
), "Aborted at %s", time_string(job
->completed
, hhmmss
, sizeof(hhmmss
)));
7428 case IPP_JSTATE_CANCELED
:
7429 snprintf(when
, sizeof(when
), "Canceled at %s", time_string(job
->completed
, hhmmss
, sizeof(hhmmss
)));
7431 case IPP_JSTATE_COMPLETED
:
7432 snprintf(when
, sizeof(when
), "Completed at %s", time_string(job
->completed
, hhmmss
, sizeof(hhmmss
)));
7436 html_printf(client
, "<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", job
->id
, job
->name
, job
->username
, when
);
7438 html_printf(client
, "</tbody></table>\n");
7440 _cupsRWUnlock(&(printer
->rwlock
));
7443 html_footer(client
);
7450 * 'show_supplies()' - Show printer supplies.
7453 static int /* O - 1 on success, 0 on failure */
7455 ippeve_client_t
*client
) /* I - Client connection */
7457 ippeve_printer_t
*printer
= client
->printer
;
7459 int i
, /* Looping var */
7460 num_supply
; /* Number of supplies */
7461 ipp_attribute_t
*supply
, /* printer-supply attribute */
7462 *supply_desc
; /* printer-supply-description attribute */
7463 int num_options
= 0; /* Number of form options */
7464 cups_option_t
*options
= NULL
; /* Form options */
7465 int supply_len
, /* Length of supply value */
7466 level
; /* Supply level */
7467 const char *supply_value
; /* Supply value */
7468 char supply_text
[1024], /* Supply string */
7469 *supply_ptr
; /* Pointer into supply string */
7470 static const char * const printer_supply
[] =
7471 { /* printer-supply values */
7472 "index=1;class=receptacleThatIsFilled;type=wasteToner;unit=percent;"
7473 "maxcapacity=100;level=%d;colorantname=unknown;",
7474 "index=2;class=supplyThatIsConsumed;type=toner;unit=percent;"
7475 "maxcapacity=100;level=%d;colorantname=black;",
7476 "index=3;class=supplyThatIsConsumed;type=toner;unit=percent;"
7477 "maxcapacity=100;level=%d;colorantname=cyan;",
7478 "index=4;class=supplyThatIsConsumed;type=toner;unit=percent;"
7479 "maxcapacity=100;level=%d;colorantname=magenta;",
7480 "index=5;class=supplyThatIsConsumed;type=toner;unit=percent;"
7481 "maxcapacity=100;level=%d;colorantname=yellow;"
7483 static const char * const backgrounds
[] =
7484 { /* Background colors for the supply-level bars */
7485 "#777 linear-gradient(#333,#777)",
7486 "#000 linear-gradient(#666,#000)",
7487 "#0FF linear-gradient(#6FF,#0FF)",
7488 "#F0F linear-gradient(#F6F,#F0F)",
7489 "#CC0 linear-gradient(#EE6,#EE0)"
7491 static const char * const colors
[] = /* Text colors for the supply-level bars */
7501 if (!respond_http(client
, HTTP_STATUS_OK
, NULL
, "text/html", 0))
7504 html_header(client
, printer
->name
, 0);
7506 if ((supply
= ippFindAttribute(printer
->attrs
, "printer-supply", IPP_TAG_STRING
)) == NULL
)
7508 html_printf(client
, "<p>Error: No printer-supply defined for printer.</p>\n");
7509 html_footer(client
);
7513 num_supply
= ippGetCount(supply
);
7515 if ((supply_desc
= ippFindAttribute(printer
->attrs
, "printer-supply-description", IPP_TAG_TEXT
)) == NULL
)
7517 html_printf(client
, "<p>Error: No printer-supply-description defined for printer.</p>\n");
7518 html_footer(client
);
7522 if (num_supply
!= ippGetCount(supply_desc
))
7524 html_printf(client
, "<p>Error: Different number of values for printer-supply and printer-supply-description defined for printer.</p>\n");
7525 html_footer(client
);
7529 if (printer
->web_forms
)
7530 num_options
= parse_options(client
, &options
);
7532 if (num_options
> 0)
7535 * WARNING: A real printer/server implementation MUST NOT implement
7536 * supply updates via a GET request - GET requests are supposed to be
7537 * idempotent (without side-effects) and we obviously are not
7538 * authenticating access here. This form is provided solely to
7539 * enable testing and development!
7542 char name
[64]; /* Form field */
7543 const char *val
; /* Form value */
7545 _cupsRWLockWrite(&printer
->rwlock
);
7547 ippDeleteAttribute(printer
->attrs
, supply
);
7550 printer
->state_reasons
&= (ippeve_preason_t
)~(IPPEVE_PREASON_MARKER_SUPPLY_EMPTY
| IPPEVE_PREASON_MARKER_SUPPLY_LOW
| IPPEVE_PREASON_MARKER_WASTE_ALMOST_FULL
| IPPEVE_PREASON_MARKER_WASTE_FULL
| IPPEVE_PREASON_TONER_EMPTY
| IPPEVE_PREASON_TONER_LOW
);
7552 for (i
= 0; i
< num_supply
; i
++)
7554 snprintf(name
, sizeof(name
), "supply%d", i
);
7555 if ((val
= cupsGetOption(name
, num_options
, options
)) != NULL
)
7557 level
= atoi(val
); /* New level */
7559 snprintf(supply_text
, sizeof(supply_text
), printer_supply
[i
], level
);
7561 ippSetOctetString(printer
->attrs
, &supply
, ippGetCount(supply
), supply_text
, (int)strlen(supply_text
));
7563 supply
= ippAddOctetString(printer
->attrs
, IPP_TAG_PRINTER
, "printer-supply", supply_text
, (int)strlen(supply_text
));
7568 printer
->state_reasons
|= IPPEVE_PREASON_MARKER_WASTE_FULL
;
7569 else if (level
> 90)
7570 printer
->state_reasons
|= IPPEVE_PREASON_MARKER_WASTE_ALMOST_FULL
;
7575 printer
->state_reasons
|= IPPEVE_PREASON_TONER_EMPTY
;
7576 else if (level
< 10)
7577 printer
->state_reasons
|= IPPEVE_PREASON_TONER_LOW
;
7582 _cupsRWUnlock(&printer
->rwlock
);
7585 if (printer
->web_forms
)
7586 html_printf(client
, "<form method=\"GET\" action=\"/supplies\">\n");
7588 html_printf(client
, "<table class=\"form\" summary=\"Supplies\">\n");
7589 for (i
= 0; i
< num_supply
; i
++)
7591 supply_value
= ippGetOctetString(supply
, i
, &supply_len
);
7592 if (supply_len
> (int)(sizeof(supply_text
) - 1))
7593 supply_len
= (int)sizeof(supply_text
) - 1;
7595 memcpy(supply_text
, supply_value
, (size_t)supply_len
);
7596 supply_text
[supply_len
] = '\0';
7598 if ((supply_ptr
= strstr(supply_text
, "level=")) != NULL
)
7599 level
= atoi(supply_ptr
+ 6);
7603 if (printer
->web_forms
)
7604 html_printf(client
, "<tr><th>%s:</th><td><input name=\"supply%d\" size=\"3\" value=\"%d\"></td>", ippGetString(supply_desc
, i
, NULL
), i
, level
);
7606 html_printf(client
, "<tr><th>%s:</th>", ippGetString(supply_desc
, i
, NULL
));
7609 html_printf(client
, "<td class=\"meter\"><span class=\"bar\" style=\"background: %s; padding: 5px %dpx;\"></span> %d%%</td></tr>\n", backgrounds
[i
], level
* 2, level
);
7611 html_printf(client
, "<td class=\"meter\"><span class=\"bar\" style=\"background: %s; color: %s; padding: 5px %dpx;\">%d%%</span></td></tr>\n", backgrounds
[i
], colors
[i
], level
* 2, level
);
7614 if (printer
->web_forms
)
7616 html_printf(client
, "<tr><td></td><td colspan=\"2\"><input type=\"submit\" value=\"Update Supplies\">");
7617 if (num_options
> 0)
7618 html_printf(client
, " <span class=\"badge\" id=\"status\">Supplies updated.</span>\n");
7619 html_printf(client
, "</td></tr>\n</table>\n</form>\n");
7621 if (num_options
> 0)
7622 html_printf(client
, "<script>\n"
7623 "setTimeout(hide_status, 3000);\n"
7624 "function hide_status() {\n"
7625 " var status = document.getElementById('status');\n"
7626 " status.style.display = 'none';\n"
7631 html_printf(client
, "</table>\n");
7633 html_footer(client
);
7640 * 'time_string()' - Return the local time in hours, minutes, and seconds.
7644 time_string(time_t tv
, /* I - Time value */
7645 char *buffer
, /* I - Buffer */
7646 size_t bufsize
) /* I - Size of buffer */
7648 struct tm
*curtime
= localtime(&tv
);
7651 strftime(buffer
, bufsize
, "%X", curtime
);
7657 * 'usage()' - Show program usage.
7661 usage(int status
) /* O - Exit status */
7663 _cupsLangPuts(stdout
, _("Usage: ippeveprinter [options] \"name\""));
7664 _cupsLangPuts(stdout
, _("Options:"));
7665 _cupsLangPuts(stderr
, _("--help Show program help"));
7666 _cupsLangPuts(stderr
, _("--no-web-forms Disable web forms for media and supplies"));
7667 _cupsLangPuts(stderr
, _("--version Show program version"));
7668 _cupsLangPuts(stdout
, _("-2 Set 2-sided printing support (default=1-sided)"));
7669 _cupsLangPuts(stdout
, _("-D device-uri Set the device URI for the printer"));
7670 _cupsLangPuts(stdout
, _("-F output-type/subtype Set the output format for the printer"));
7672 _cupsLangPuts(stdout
, _("-K keypath Set location of server X.509 certificates and keys."));
7673 #endif /* HAVE_SSL */
7674 _cupsLangPuts(stdout
, _("-M manufacturer Set manufacturer name (default=Test)"));
7675 _cupsLangPuts(stdout
, _("-P filename.ppd Load printer attributes from PPD file"));
7676 _cupsLangPuts(stdout
, _("-V version Set default IPP version"));
7677 _cupsLangPuts(stdout
, _("-a filename.conf Load printer attributes from conf file"));
7678 _cupsLangPuts(stdout
, _("-c command Set print command"));
7679 _cupsLangPuts(stdout
, _("-d spool-directory Set spool directory"));
7680 _cupsLangPuts(stdout
, _("-f type/subtype[,...] Set supported file types"));
7681 _cupsLangPuts(stdout
, _("-i iconfile.png Set icon file"));
7682 _cupsLangPuts(stdout
, _("-k Keep job spool files"));
7683 _cupsLangPuts(stdout
, _("-l location Set location of printer"));
7684 _cupsLangPuts(stdout
, _("-m model Set model name (default=Printer)"));
7685 _cupsLangPuts(stdout
, _("-n hostname Set hostname for printer"));
7686 _cupsLangPuts(stdout
, _("-p port Set port number for printer"));
7687 _cupsLangPuts(stdout
, _("-r subtype,[subtype] Set DNS-SD service subtype"));
7688 _cupsLangPuts(stdout
, _("-s speed[,color-speed] Set speed in pages per minute"));
7689 _cupsLangPuts(stderr
, _("-v Be verbose"));
7696 * 'valid_doc_attributes()' - Determine whether the document attributes are
7699 * When one or more document attributes are invalid, this function adds a
7700 * suitable response and attributes to the unsupported group.
7703 static int /* O - 1 if valid, 0 if not */
7704 valid_doc_attributes(
7705 ippeve_client_t
*client
) /* I - Client */
7707 int valid
= 1; /* Valid attributes? */
7708 ipp_op_t op
= ippGetOperation(client
->request
);
7710 const char *op_name
= ippOpString(op
);
7711 /* IPP operation name */
7712 ipp_attribute_t
*attr
, /* Current attribute */
7713 *supported
; /* xxx-supported attribute */
7714 const char *compression
= NULL
,
7715 /* compression value */
7716 *format
= NULL
; /* document-format value */
7720 * Check operation attributes...
7723 if ((attr
= ippFindAttribute(client
->request
, "compression", IPP_TAG_ZERO
)) != NULL
)
7726 * If compression is specified, only accept a supported value in a Print-Job
7727 * or Send-Document request...
7730 compression
= ippGetString(attr
, 0, NULL
);
7731 supported
= ippFindAttribute(client
->printer
->attrs
,
7732 "compression-supported", IPP_TAG_KEYWORD
);
7734 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_KEYWORD
||
7735 ippGetGroupTag(attr
) != IPP_TAG_OPERATION
||
7736 (op
!= IPP_OP_PRINT_JOB
&& op
!= IPP_OP_SEND_DOCUMENT
&&
7737 op
!= IPP_OP_VALIDATE_JOB
) ||
7738 !ippContainsString(supported
, compression
))
7740 respond_unsupported(client
, attr
);
7745 fprintf(stderr
, "%s %s compression=\"%s\"\n", client
->hostname
, op_name
, compression
);
7747 ippAddString(client
->request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "compression-supplied", NULL
, compression
);
7749 if (strcmp(compression
, "none"))
7752 fprintf(stderr
, "Receiving job file with \"%s\" compression.\n", compression
);
7753 httpSetField(client
->http
, HTTP_FIELD_CONTENT_ENCODING
, compression
);
7759 * Is it a format we support?
7762 if ((attr
= ippFindAttribute(client
->request
, "document-format", IPP_TAG_ZERO
)) != NULL
)
7764 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_MIMETYPE
||
7765 ippGetGroupTag(attr
) != IPP_TAG_OPERATION
)
7767 respond_unsupported(client
, attr
);
7772 format
= ippGetString(attr
, 0, NULL
);
7774 fprintf(stderr
, "%s %s document-format=\"%s\"\n", client
->hostname
, op_name
, format
);
7776 ippAddString(client
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-supplied", NULL
, format
);
7781 format
= ippGetString(ippFindAttribute(client
->printer
->attrs
, "document-format-default", IPP_TAG_MIMETYPE
), 0, NULL
);
7783 format
= "application/octet-stream"; /* Should never happen */
7785 attr
= ippAddString(client
->request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format", NULL
, format
);
7788 if (format
&& !strcmp(format
, "application/octet-stream") && (ippGetOperation(client
->request
) == IPP_OP_PRINT_JOB
|| ippGetOperation(client
->request
) == IPP_OP_SEND_DOCUMENT
))
7791 * Auto-type the file using the first 8 bytes of the file...
7794 unsigned char header
[8]; /* First 8 bytes of file */
7796 memset(header
, 0, sizeof(header
));
7797 httpPeek(client
->http
, (char *)header
, sizeof(header
));
7799 if (!memcmp(header
, "%PDF", 4))
7800 format
= "application/pdf";
7801 else if (!memcmp(header
, "%!", 2))
7802 format
= "application/postscript";
7803 else if (!memcmp(header
, "\377\330\377", 3) && header
[3] >= 0xe0 && header
[3] <= 0xef)
7804 format
= "image/jpeg";
7805 else if (!memcmp(header
, "\211PNG", 4))
7806 format
= "image/png";
7807 else if (!memcmp(header
, "RAS2", 4))
7808 format
= "image/pwg-raster";
7809 else if (!memcmp(header
, "UNIRAST", 8))
7810 format
= "image/urf";
7816 fprintf(stderr
, "%s %s Auto-typed document-format=\"%s\"\n", client
->hostname
, op_name
, format
);
7818 ippAddString(client
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format-detected", NULL
, format
);
7822 if (op
!= IPP_OP_CREATE_JOB
&& (supported
= ippFindAttribute(client
->printer
->attrs
, "document-format-supported", IPP_TAG_MIMETYPE
)) != NULL
&& !ippContainsString(supported
, format
))
7824 respond_unsupported(client
, attr
);
7832 if ((attr
= ippFindAttribute(client
->request
, "document-name", IPP_TAG_NAME
)) != NULL
)
7833 ippAddString(client
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "document-name-supplied", NULL
, ippGetString(attr
, 0, NULL
));
7840 * 'valid_job_attributes()' - Determine whether the job attributes are valid.
7842 * When one or more job attributes are invalid, this function adds a suitable
7843 * response and attributes to the unsupported group.
7846 static int /* O - 1 if valid, 0 if not */
7847 valid_job_attributes(
7848 ippeve_client_t
*client
) /* I - Client */
7850 int i
, /* Looping var */
7851 count
, /* Number of values */
7852 valid
= 1; /* Valid attributes? */
7853 ipp_attribute_t
*attr
, /* Current attribute */
7854 *supported
; /* xxx-supported attribute */
7858 * Check operation attributes...
7861 valid
= valid_doc_attributes(client
);
7864 * Check the various job template attributes...
7867 if ((attr
= ippFindAttribute(client
->request
, "copies", IPP_TAG_ZERO
)) != NULL
)
7869 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_INTEGER
||
7870 ippGetInteger(attr
, 0) < 1 || ippGetInteger(attr
, 0) > 999)
7872 respond_unsupported(client
, attr
);
7877 if ((attr
= ippFindAttribute(client
->request
, "ipp-attribute-fidelity", IPP_TAG_ZERO
)) != NULL
)
7879 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_BOOLEAN
)
7881 respond_unsupported(client
, attr
);
7886 if ((attr
= ippFindAttribute(client
->request
, "job-hold-until", IPP_TAG_ZERO
)) != NULL
)
7888 if (ippGetCount(attr
) != 1 ||
7889 (ippGetValueTag(attr
) != IPP_TAG_NAME
&&
7890 ippGetValueTag(attr
) != IPP_TAG_NAMELANG
&&
7891 ippGetValueTag(attr
) != IPP_TAG_KEYWORD
) ||
7892 strcmp(ippGetString(attr
, 0, NULL
), "no-hold"))
7894 respond_unsupported(client
, attr
);
7899 if ((attr
= ippFindAttribute(client
->request
, "job-impressions", IPP_TAG_ZERO
)) != NULL
)
7901 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_INTEGER
|| ippGetInteger(attr
, 0) < 0)
7903 respond_unsupported(client
, attr
);
7908 if ((attr
= ippFindAttribute(client
->request
, "job-name", IPP_TAG_ZERO
)) != NULL
)
7910 if (ippGetCount(attr
) != 1 ||
7911 (ippGetValueTag(attr
) != IPP_TAG_NAME
&&
7912 ippGetValueTag(attr
) != IPP_TAG_NAMELANG
))
7914 respond_unsupported(client
, attr
);
7918 ippSetGroupTag(client
->request
, &attr
, IPP_TAG_JOB
);
7921 ippAddString(client
->request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-name", NULL
, "Untitled");
7923 if ((attr
= ippFindAttribute(client
->request
, "job-priority", IPP_TAG_ZERO
)) != NULL
)
7925 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_INTEGER
||
7926 ippGetInteger(attr
, 0) < 1 || ippGetInteger(attr
, 0) > 100)
7928 respond_unsupported(client
, attr
);
7933 if ((attr
= ippFindAttribute(client
->request
, "job-sheets", IPP_TAG_ZERO
)) != NULL
)
7935 if (ippGetCount(attr
) != 1 ||
7936 (ippGetValueTag(attr
) != IPP_TAG_NAME
&&
7937 ippGetValueTag(attr
) != IPP_TAG_NAMELANG
&&
7938 ippGetValueTag(attr
) != IPP_TAG_KEYWORD
) ||
7939 strcmp(ippGetString(attr
, 0, NULL
), "none"))
7941 respond_unsupported(client
, attr
);
7946 if ((attr
= ippFindAttribute(client
->request
, "media", IPP_TAG_ZERO
)) != NULL
)
7948 if (ippGetCount(attr
) != 1 ||
7949 (ippGetValueTag(attr
) != IPP_TAG_NAME
&&
7950 ippGetValueTag(attr
) != IPP_TAG_NAMELANG
&&
7951 ippGetValueTag(attr
) != IPP_TAG_KEYWORD
))
7953 respond_unsupported(client
, attr
);
7958 supported
= ippFindAttribute(client
->printer
->attrs
, "media-supported", IPP_TAG_KEYWORD
);
7960 if (!ippContainsString(supported
, ippGetString(attr
, 0, NULL
)))
7962 respond_unsupported(client
, attr
);
7968 if ((attr
= ippFindAttribute(client
->request
, "media-col", IPP_TAG_ZERO
)) != NULL
)
7970 ipp_t
*col
, /* media-col collection */
7971 *size
; /* media-size collection */
7972 ipp_attribute_t
*member
, /* Member attribute */
7973 *x_dim
, /* x-dimension */
7974 *y_dim
; /* y-dimension */
7975 int x_value
, /* y-dimension value */
7976 y_value
; /* x-dimension value */
7978 if (ippGetCount(attr
) != 1 ||
7979 ippGetValueTag(attr
) != IPP_TAG_BEGIN_COLLECTION
)
7981 respond_unsupported(client
, attr
);
7985 col
= ippGetCollection(attr
, 0);
7987 if ((member
= ippFindAttribute(col
, "media-size-name", IPP_TAG_ZERO
)) != NULL
)
7989 if (ippGetCount(member
) != 1 ||
7990 (ippGetValueTag(member
) != IPP_TAG_NAME
&&
7991 ippGetValueTag(member
) != IPP_TAG_NAMELANG
&&
7992 ippGetValueTag(member
) != IPP_TAG_KEYWORD
))
7994 respond_unsupported(client
, attr
);
7999 supported
= ippFindAttribute(client
->printer
->attrs
, "media-supported", IPP_TAG_KEYWORD
);
8001 if (!ippContainsString(supported
, ippGetString(member
, 0, NULL
)))
8003 respond_unsupported(client
, attr
);
8008 else if ((member
= ippFindAttribute(col
, "media-size", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
8010 if (ippGetCount(member
) != 1)
8012 respond_unsupported(client
, attr
);
8017 size
= ippGetCollection(member
, 0);
8019 if ((x_dim
= ippFindAttribute(size
, "x-dimension", IPP_TAG_INTEGER
)) == NULL
|| ippGetCount(x_dim
) != 1 ||
8020 (y_dim
= ippFindAttribute(size
, "y-dimension", IPP_TAG_INTEGER
)) == NULL
|| ippGetCount(y_dim
) != 1)
8022 respond_unsupported(client
, attr
);
8027 x_value
= ippGetInteger(x_dim
, 0);
8028 y_value
= ippGetInteger(y_dim
, 0);
8029 supported
= ippFindAttribute(client
->printer
->attrs
, "media-size-supported", IPP_TAG_BEGIN_COLLECTION
);
8030 count
= ippGetCount(supported
);
8032 for (i
= 0; i
< count
; i
++)
8034 size
= ippGetCollection(supported
, i
);
8035 x_dim
= ippFindAttribute(size
, "x-dimension", IPP_TAG_ZERO
);
8036 y_dim
= ippFindAttribute(size
, "y-dimension", IPP_TAG_ZERO
);
8038 if (ippContainsInteger(x_dim
, x_value
) && ippContainsInteger(y_dim
, y_value
))
8044 respond_unsupported(client
, attr
);
8052 if ((attr
= ippFindAttribute(client
->request
, "multiple-document-handling", IPP_TAG_ZERO
)) != NULL
)
8054 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_KEYWORD
||
8055 (strcmp(ippGetString(attr
, 0, NULL
),
8056 "separate-documents-uncollated-copies") &&
8057 strcmp(ippGetString(attr
, 0, NULL
),
8058 "separate-documents-collated-copies")))
8060 respond_unsupported(client
, attr
);
8065 if ((attr
= ippFindAttribute(client
->request
, "orientation-requested", IPP_TAG_ZERO
)) != NULL
)
8067 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_ENUM
||
8068 ippGetInteger(attr
, 0) < IPP_ORIENT_PORTRAIT
||
8069 ippGetInteger(attr
, 0) > IPP_ORIENT_REVERSE_PORTRAIT
)
8071 respond_unsupported(client
, attr
);
8076 if ((attr
= ippFindAttribute(client
->request
, "page-ranges", IPP_TAG_ZERO
)) != NULL
)
8078 if (ippGetValueTag(attr
) != IPP_TAG_RANGE
)
8080 respond_unsupported(client
, attr
);
8085 if ((attr
= ippFindAttribute(client
->request
, "print-quality", IPP_TAG_ZERO
)) != NULL
)
8087 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_ENUM
||
8088 ippGetInteger(attr
, 0) < IPP_QUALITY_DRAFT
||
8089 ippGetInteger(attr
, 0) > IPP_QUALITY_HIGH
)
8091 respond_unsupported(client
, attr
);
8096 if ((attr
= ippFindAttribute(client
->request
, "printer-resolution", IPP_TAG_ZERO
)) != NULL
)
8098 supported
= ippFindAttribute(client
->printer
->attrs
, "printer-resolution-supported", IPP_TAG_RESOLUTION
);
8100 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_RESOLUTION
||
8103 respond_unsupported(client
, attr
);
8108 int xdpi
, /* Horizontal resolution for job template attribute */
8109 ydpi
, /* Vertical resolution for job template attribute */
8110 sydpi
; /* Vertical resolution for supported value */
8111 ipp_res_t units
, /* Units for job template attribute */
8112 sunits
; /* Units for supported value */
8114 xdpi
= ippGetResolution(attr
, 0, &ydpi
, &units
);
8115 count
= ippGetCount(supported
);
8117 for (i
= 0; i
< count
; i
++)
8119 if (xdpi
== ippGetResolution(supported
, i
, &sydpi
, &sunits
) && ydpi
== sydpi
&& units
== sunits
)
8125 respond_unsupported(client
, attr
);
8131 if ((attr
= ippFindAttribute(client
->request
, "sides", IPP_TAG_ZERO
)) != NULL
)
8133 const char *sides
= ippGetString(attr
, 0, NULL
);
8134 /* "sides" value... */
8136 if (ippGetCount(attr
) != 1 || ippGetValueTag(attr
) != IPP_TAG_KEYWORD
)
8138 respond_unsupported(client
, attr
);
8141 else if ((supported
= ippFindAttribute(client
->printer
->attrs
, "sides-supported", IPP_TAG_KEYWORD
)) != NULL
)
8143 if (!ippContainsString(supported
, sides
))
8145 respond_unsupported(client
, attr
);
8149 else if (strcmp(sides
, "one-sided"))
8151 respond_unsupported(client
, attr
);