4 * Sample IPP/2.0 server for CUPS.
6 * Copyright 2010-2011 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * main() - Main entry to the sample server.
19 * clean_jobs() - Clean out old (completed) jobs.
20 * compare_jobs() - Compare two jobs.
21 * copy_attribute() - Copy a single attribute.
22 * copy_attributes() - Copy attributes from one request to another.
23 * copy_job_attrs() - Copy job attributes to the response.
24 * create_client() - Accept a new network connection and create a
26 * create_job() - Create a new job object from a Print-Job or
28 * create_listener() - Create a listener socket.
29 * create_media_col() - Create a media-col value.
30 * create_printer() - Create, register, and listen for connections
31 * to a printer object.
32 * create_requested_array() - Create an array for requested-attributes.
33 * debug_attributes() - Print attributes in a request or response.
34 * delete_client() - Close the socket and free all memory used by
36 * delete_job() - Remove from the printer and free all memory
37 * used by a job object.
38 * delete_printer() - Unregister, close listen sockets, and free
39 * all memory used by a printer object.
40 * dnssd_callback() - Handle Bonjour registration events.
41 * find_job() - Find a job specified in a request.
42 * html_escape() - Write a HTML-safe string.
43 * html_printf() - Send formatted text to the client, quoting
45 * ipp_cancel_job() - Cancel a job.
46 * ipp_create_job() - Create a job object.
47 * ipp_get_job_attributes() - Get the attributes for a job object.
48 * ipp_get_jobs() - Get a list of job objects.
49 * ipp_get_printer_attributes() - Get the attributes for a printer object.
50 * ipp_print_job() - Create a job object with an attached
52 * ipp_send_document() - Add an attached document to a job object
53 * created with Create-Job.
54 * ipp_validate_job() - Validate job creation attributes.
55 * process_client() - Process client requests on a thread.
56 * process_http() - Process a HTTP request.
57 * process_ipp() - Process an IPP request.
58 * process_job() - Process a print job.
59 * register_printer() - Register a printer object via Bonjour.
60 * respond_http() - Send a HTTP response.
61 * respond_ipp() - Send an IPP response.
62 * run_printer() - Run the printer service.
63 * usage() - Show program usage.
64 * valid_job_attributes() - Determine whether the job attributes are
69 * Include necessary headers...
72 #include <cups/cups-private.h>
75 #endif /* HAVE_DNSSD */
78 #ifdef HAVE_SYS_MOUNT_H
79 # include <sys/mount.h>
80 #endif /* HAVE_SYS_MOUNT_H */
81 #ifdef HAVE_SYS_STATFS_H
82 # include <sys/statfs.h>
83 #endif /* HAVE_SYS_STATFS_H */
84 #ifdef HAVE_SYS_STATVFS_H
85 # include <sys/statvfs.h>
86 #endif /* HAVE_SYS_STATVFS_H */
89 #endif /* HAVE_SYS_VFS_H */
96 enum _ipp_preasons_e
/* printer-state-reasons bit values */
98 _IPP_PRINTER_NONE
= 0x0000, /* none */
99 _IPP_PRINTER_OTHER
= 0x0001, /* other */
100 _IPP_PRINTER_COVER_OPEN
= 0x0002, /* cover-open */
101 _IPP_PRINTER_INPUT_TRAY_MISSING
= 0x0004,
102 /* input-tray-missing */
103 _IPP_PRINTER_MARKER_SUPPLY_EMPTY
= 0x0008,
104 /* marker-supply-empty */
105 _IPP_PRINTER_MARKER_SUPPLY_LOW
= 0x0010,
106 /* marker-suply-low */
107 _IPP_PRINTER_MARKER_WASTE_ALMOST_FULL
= 0x0020,
108 /* marker-waste-almost-full */
109 _IPP_PRINTER_MARKER_WASTE_FULL
= 0x0040,
110 /* marker-waste-full */
111 _IPP_PRINTER_MEDIA_EMPTY
= 0x0080, /* media-empty */
112 _IPP_PRINTER_MEDIA_JAM
= 0x0100, /* media-jam */
113 _IPP_PRINTER_MEDIA_LOW
= 0x0200, /* media-low */
114 _IPP_PRINTER_MEDIA_NEEDED
= 0x0400, /* media-needed */
115 _IPP_PRINTER_MOVING_TO_PAUSED
= 0x0800,
116 /* moving-to-paused */
117 _IPP_PRINTER_PAUSED
= 0x1000, /* paused */
118 _IPP_PRINTER_SPOOL_AREA_FULL
= 0x2000,/* spool-area-full */
119 _IPP_PRINTER_TONER_EMPTY
= 0x4000, /* toner-empty */
120 _IPP_PRINTER_TONER_LOW
= 0x8000 /* toner-low */
122 typedef unsigned int _ipp_preasons_t
; /* Bitfield for printer-state-reasons */
124 typedef enum _ipp_media_class_e
126 _IPP_GENERAL
, /* General-purpose size */
127 _IPP_PHOTO_ONLY
, /* Photo-only size */
128 _IPP_ENV_ONLY
/* Envelope-only size */
129 } _ipp_media_class_t
;
131 static const char * const media_supported
[] =
132 { /* media-supported values */
133 "iso_a4_210x297mm", /* A4 */
134 "iso_a5_148x210mm", /* A5 */
135 "iso_a6_105x148mm", /* A6 */
136 "iso_dl_110x220mm", /* DL */
137 "na_legal_8.5x14in", /* Legal */
138 "na_letter_8.5x11in", /* Letter */
139 "na_number-10_4.125x9.5in", /* #10 */
140 "na_index-3x5_3x5in", /* 3x5 */
141 "oe_photo-l_3.5x5in", /* L */
142 "na_index-4x6_4x6in", /* 4x6 */
143 "na_5x7_5x7in" /* 5x7 */
145 static const int media_col_sizes
[][3] =
146 { /* media-col-database sizes */
147 { 21000, 29700, _IPP_GENERAL
}, /* A4 */
148 { 14800, 21000, _IPP_PHOTO_ONLY
}, /* A5 */
149 { 10500, 14800, _IPP_PHOTO_ONLY
}, /* A6 */
150 { 11000, 22000, _IPP_ENV_ONLY
}, /* DL */
151 { 21590, 35560, _IPP_GENERAL
}, /* Legal */
152 { 21590, 27940, _IPP_GENERAL
}, /* Letter */
153 { 10477, 24130, _IPP_ENV_ONLY
}, /* #10 */
154 { 7630, 12700, _IPP_PHOTO_ONLY
}, /* 3x5 */
155 { 8890, 12700, _IPP_PHOTO_ONLY
}, /* L */
156 { 10160, 15240, _IPP_PHOTO_ONLY
}, /* 4x6 */
157 { 12700, 17780, _IPP_PHOTO_ONLY
} /* 5x7 */
159 static const char * const media_type_supported
[] =
160 /* media-type-supported values */
167 "photographic-glossy",
168 "photographic-high-gloss",
169 "photographic-matte",
170 "photographic-satin",
171 "photographic-semi-gloss",
173 "stationery-letterhead",
182 typedef struct _ipp_job_s _ipp_job_t
;
184 typedef struct _ipp_printer_s
/**** Printer data ****/
186 int ipv4
, /* IPv4 listener */
187 ipv6
; /* IPv6 listener */
189 DNSServiceRef common_ref
, /* Shared service connection */
190 ipp_ref
, /* Bonjour IPP service */
191 http_ref
, /* Bonjour HTTP service */
192 printer_ref
; /* Bonjour LPD service */
193 TXTRecordRef ipp_txt
; /* Bonjour IPP TXT record */
194 char *dnssd_name
; /* printer-dnssd-name */
195 #endif /* HAVE_DNSSD */
196 char *name
, /* printer-name */
197 *icon
, /* Icon filename */
198 *directory
, /* Spool directory */
199 *hostname
, /* Hostname */
200 *uri
; /* printer-uri-supported */
202 size_t urilen
; /* Length of printer URI */
203 ipp_t
*attrs
; /* Static attributes */
204 ipp_pstate_t state
; /* printer-state value */
205 _ipp_preasons_t state_reasons
; /* printer-state-reasons values */
206 cups_array_t
*jobs
; /* Jobs */
207 _ipp_job_t
*active_job
; /* Current active/pending job */
208 int next_job_id
; /* Next job-id value */
209 _cups_rwlock_t rwlock
; /* Printer lock */
212 struct _ipp_job_s
/**** Job data ****/
215 char *name
, /* job-name */
216 *username
, /* job-originating-user-name */
217 *format
; /* document-format */
218 ipp_jstate_t state
; /* job-state value */
219 time_t processing
, /* time-at-processing value */
220 completed
; /* time-at-completed value */
221 ipp_t
*attrs
; /* Static attributes */
222 int cancel
; /* Non-zero when job canceled */
223 char *filename
; /* Print file name */
224 int fd
; /* Print file descriptor */
225 _ipp_printer_t
*printer
; /* Printer */
228 typedef struct _ipp_client_s
/**** Client data ****/
230 http_t http
; /* HTTP connection */
231 ipp_t
*request
, /* IPP request */
232 *response
; /* IPP response */
233 time_t start
; /* Request start time */
234 http_state_t operation
; /* Request operation */
235 ipp_op_t operation_id
; /* IPP operation-id */
236 char uri
[1024]; /* Request URI */
237 http_addr_t addr
; /* Client address */
238 _ipp_printer_t
*printer
; /* Printer */
239 _ipp_job_t
*job
; /* Current job, if any */
247 static void clean_jobs(_ipp_printer_t
*printer
);
248 static int compare_jobs(_ipp_job_t
*a
, _ipp_job_t
*b
);
249 static ipp_attribute_t
*copy_attribute(ipp_t
*to
, ipp_attribute_t
*attr
,
250 ipp_tag_t group_tag
, int quickcopy
);
251 static void copy_attributes(ipp_t
*to
, ipp_t
*from
, cups_array_t
*ra
,
252 ipp_tag_t group_tag
, int quickcopy
);
253 static void copy_job_attributes(_ipp_client_t
*client
,
254 _ipp_job_t
*job
, cups_array_t
*ra
);
255 static _ipp_client_t
*create_client(_ipp_printer_t
*printer
, int sock
);
256 static _ipp_job_t
*create_job(_ipp_client_t
*client
);
257 static int create_listener(int family
, int *port
);
258 static ipp_t
*create_media_col(const char *media
, const char *type
,
259 int width
, int length
, int margins
);
260 static _ipp_printer_t
*create_printer(const char *servername
,
261 const char *name
, const char *location
,
262 const char *make
, const char *model
,
264 const char *docformats
, int ppm
,
265 int ppm_color
, int duplex
, int port
,
268 #endif /* HAVE_DNSSD */
269 const char *directory
);
270 static cups_array_t
*create_requested_array(_ipp_client_t
*client
);
271 static void debug_attributes(const char *title
, ipp_t
*ipp
);
272 static void delete_client(_ipp_client_t
*client
);
273 static void delete_job(_ipp_job_t
*job
);
274 static void delete_printer(_ipp_printer_t
*printer
);
276 static void dnssd_callback(DNSServiceRef sdRef
,
277 DNSServiceFlags flags
,
278 DNSServiceErrorType errorCode
,
282 _ipp_printer_t
*printer
);
283 #endif /* HAVE_DNSSD */
284 static _ipp_job_t
*find_job(_ipp_client_t
*client
);
285 static void html_escape(_ipp_client_t
*client
, const char *s
,
287 static void html_printf(_ipp_client_t
*client
, const char *format
,
290 __attribute__ ((__format__ (__printf__
, 2, 3)))
291 # endif /* __GNUC__ */
293 static void ipp_cancel_job(_ipp_client_t
*client
);
295 static void ipp_create_job(_ipp_client_t
*client
);
297 static void ipp_get_job_attributes(_ipp_client_t
*client
);
298 static void ipp_get_jobs(_ipp_client_t
*client
);
299 static void ipp_get_printer_attributes(_ipp_client_t
*client
);
300 static void ipp_print_job(_ipp_client_t
*client
);
302 static void ipp_send_document(_ipp_client_t
*client
);
304 static void ipp_validate_job(_ipp_client_t
*client
);
305 static void *process_client(_ipp_client_t
*client
);
306 static int process_http(_ipp_client_t
*client
);
307 static int process_ipp(_ipp_client_t
*client
);
308 static void *process_job(_ipp_job_t
*job
);
310 static int register_printer(_ipp_printer_t
*printer
,
311 const char *location
, const char *make
,
312 const char *model
, const char *formats
,
313 const char *adminurl
, int color
,
314 int duplex
, const char *regtype
);
315 #endif /* HAVE_DNSSD */
316 static int respond_http(_ipp_client_t
*client
, http_status_t code
,
317 const char *type
, size_t length
);
318 static void respond_ipp(_ipp_client_t
*client
, ipp_status_t status
,
319 const char *message
, ...)
321 __attribute__ ((__format__ (__printf__
, 3, 4)))
322 #endif /* __GNUC__ */
324 static void run_printer(_ipp_printer_t
*printer
);
325 static void usage(int status
);
326 static int valid_job_attributes(_ipp_client_t
*client
);
333 static int KeepFiles
= 0,
338 * 'main()' - Main entry to the sample server.
341 int /* O - Exit status */
342 main(int argc
, /* I - Number of command-line args */
343 char *argv
[]) /* I - Command-line arguments */
345 int i
; /* Looping var */
346 const char *opt
, /* Current option character */
347 *servername
= NULL
, /* Server host name */
348 *name
= NULL
, /* Printer name */
349 *location
= "", /* Location of printer */
350 *make
= "Test", /* Manufacturer */
351 *model
= "Printer", /* Model */
352 *icon
= "printer.png", /* Icon file */
353 *formats
= "application/pdf,image/jpeg";
354 /* Supported formats */
356 const char *regtype
= "_ipp._tcp"; /* Bonjour service type */
357 #endif /* HAVE_DNSSD */
358 int port
= 8631, /* Port number (0 = auto) TODO: FIX */
359 duplex
= 0, /* Duplex mode */
360 ppm
= 10, /* Pages per minute for mono */
361 ppm_color
= 0; /* Pages per minute for color */
362 char directory
[1024] = ""; /* Spool directory */
363 _ipp_printer_t
*printer
; /* Printer object */
367 * Parse command-line arguments...
370 for (i
= 1; i
< argc
; i
++)
371 if (argv
[i
][0] == '-')
373 for (opt
= argv
[i
] + 1; *opt
; opt
++)
376 case '2' : /* -2 (enable 2-sided printing) */
380 case 'M' : /* -M manufacturer */
387 case 'd' : /* -d spool-directory */
391 strlcpy(directory
, argv
[i
], sizeof(directory
));
394 case 'f' : /* -f type/subtype[,...] */
401 case 'h' : /* -h (show help) */
405 case 'i' : /* -i icon.png */
412 case 'k' : /* -k (keep files) */
416 case 'l' : /* -l location */
423 case 'm' : /* -m model */
430 case 'n' : /* -n hostname */
434 servername
= argv
[i
];
437 case 'p' : /* -p port */
439 if (i
>= argc
|| !isdigit(argv
[i
][0] & 255))
441 port
= atoi(argv
[i
]);
445 case 'r' : /* -r regtype */
451 #endif /* HAVE_DNSSD */
453 case 's' : /* -s speed[,color-speed] */
457 if (sscanf(argv
[i
], "%d,%d", &ppm
, &ppm_color
) < 1)
461 case 'v' : /* -v (be verbose) */
465 default : /* Unknown */
466 fprintf(stderr
, "Unknown option \"-%c\".\n", *opt
);
477 fprintf(stderr
, "Unexpected command-line argument \"%s\"\n", argv
[i
]);
485 * Apply defaults as needed...
490 snprintf(directory
, sizeof(directory
), "/tmp/ippserver.%d", (int)getpid());
492 if (mkdir(directory
, 0777) && errno
!= EEXIST
)
494 fprintf(stderr
, "Unable to create spool directory \"%s\": %s\n",
495 directory
, strerror(errno
));
500 fprintf(stderr
, "Using spool directory \"%s\".\n", directory
);
504 * Create the printer...
507 if ((printer
= create_printer(servername
, name
, location
, make
, model
, icon
,
508 formats
, ppm
, ppm_color
, duplex
, port
,
511 #endif /* HAVE_DNSSD */
516 * Run the print service...
519 run_printer(printer
);
522 * Destroy the printer and exit...
525 delete_printer(printer
);
532 * 'clean_jobs()' - Clean out old (completed) jobs.
536 clean_jobs(_ipp_printer_t
*printer
) /* I - Printer */
538 _ipp_job_t
*job
; /* Current job */
539 time_t cleantime
; /* Clean time */
542 if (cupsArrayCount(printer
->jobs
) == 0)
545 cleantime
= time(NULL
) - 60;
547 _cupsRWLockWrite(&(printer
->rwlock
));
548 for (job
= (_ipp_job_t
*)cupsArrayFirst(printer
->jobs
);
550 job
= (_ipp_job_t
*)cupsArrayNext(printer
->jobs
))
551 if (job
->completed
&& job
->completed
< cleantime
)
553 cupsArrayRemove(printer
->jobs
, job
);
558 _cupsRWUnlock(&(printer
->rwlock
));
563 * 'compare_jobs()' - Compare two jobs.
566 static int /* O - Result of comparison */
567 compare_jobs(_ipp_job_t
*a
, /* I - First job */
568 _ipp_job_t
*b
) /* I - Second job */
570 return (b
->id
- a
->id
);
575 * 'copy_attribute()' - Copy a single attribute.
578 static ipp_attribute_t
* /* O - New attribute */
580 ipp_t
*to
, /* O - Destination request/response */
581 ipp_attribute_t
*attr
, /* I - Attribute to copy */
582 ipp_tag_t group_tag
, /* I - Group to put the copy in */
583 int quickcopy
) /* I - Do a quick copy? */
585 int i
; /* Looping var */
586 ipp_attribute_t
*toattr
; /* Destination attribute */
589 if (Verbosity
&& attr
->name
)
591 char buffer
[2048]; /* Attribute value */
593 _ippAttrString(attr
, buffer
, sizeof(buffer
));
595 fprintf(stderr
, "Copying %s (%s%s) %s\n", attr
->name
,
596 attr
->num_values
> 1 ? "1setOf " : "",
597 ippTagString(attr
->value_tag
& ~IPP_TAG_COPY
), buffer
);
600 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
603 toattr
= ippAddSeparator(to
);
606 case IPP_TAG_INTEGER
:
608 toattr
= ippAddIntegers(to
, group_tag
, attr
->value_tag
,
609 attr
->name
, attr
->num_values
, NULL
);
611 for (i
= 0; i
< attr
->num_values
; i
++)
612 toattr
->values
[i
].integer
= attr
->values
[i
].integer
;
615 case IPP_TAG_BOOLEAN
:
616 toattr
= ippAddBooleans(to
, group_tag
, attr
->name
,
617 attr
->num_values
, NULL
);
619 for (i
= 0; i
< attr
->num_values
; i
++)
620 toattr
->values
[i
].boolean
= attr
->values
[i
].boolean
;
625 case IPP_TAG_KEYWORD
:
627 case IPP_TAG_URISCHEME
:
628 case IPP_TAG_CHARSET
:
629 case IPP_TAG_LANGUAGE
:
630 case IPP_TAG_MIMETYPE
:
631 toattr
= ippAddStrings(to
, group_tag
,
632 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
633 attr
->name
, attr
->num_values
, NULL
, NULL
);
637 for (i
= 0; i
< attr
->num_values
; i
++)
638 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
642 for (i
= 0; i
< attr
->num_values
; i
++)
643 toattr
->values
[i
].string
.text
=
644 _cupsStrAlloc(attr
->values
[i
].string
.text
);
649 toattr
= ippAddDate(to
, group_tag
, attr
->name
,
650 attr
->values
[0].date
);
653 case IPP_TAG_RESOLUTION
:
654 toattr
= ippAddResolutions(to
, group_tag
, attr
->name
,
655 attr
->num_values
, IPP_RES_PER_INCH
,
658 for (i
= 0; i
< attr
->num_values
; i
++)
660 toattr
->values
[i
].resolution
.xres
= attr
->values
[i
].resolution
.xres
;
661 toattr
->values
[i
].resolution
.yres
= attr
->values
[i
].resolution
.yres
;
662 toattr
->values
[i
].resolution
.units
= attr
->values
[i
].resolution
.units
;
667 toattr
= ippAddRanges(to
, group_tag
, attr
->name
,
668 attr
->num_values
, NULL
, NULL
);
670 for (i
= 0; i
< attr
->num_values
; i
++)
672 toattr
->values
[i
].range
.lower
= attr
->values
[i
].range
.lower
;
673 toattr
->values
[i
].range
.upper
= attr
->values
[i
].range
.upper
;
677 case IPP_TAG_TEXTLANG
:
678 case IPP_TAG_NAMELANG
:
679 toattr
= ippAddStrings(to
, group_tag
,
680 (ipp_tag_t
)(attr
->value_tag
| quickcopy
),
681 attr
->name
, attr
->num_values
, NULL
, NULL
);
685 for (i
= 0; i
< attr
->num_values
; i
++)
687 toattr
->values
[i
].string
.charset
= attr
->values
[i
].string
.charset
;
688 toattr
->values
[i
].string
.text
= attr
->values
[i
].string
.text
;
693 for (i
= 0; i
< attr
->num_values
; i
++)
696 toattr
->values
[i
].string
.charset
=
697 _cupsStrAlloc(attr
->values
[i
].string
.charset
);
699 toattr
->values
[i
].string
.charset
=
700 toattr
->values
[0].string
.charset
;
702 toattr
->values
[i
].string
.text
=
703 _cupsStrAlloc(attr
->values
[i
].string
.text
);
708 case IPP_TAG_BEGIN_COLLECTION
:
709 toattr
= ippAddCollections(to
, group_tag
, attr
->name
,
710 attr
->num_values
, NULL
);
712 for (i
= 0; i
< attr
->num_values
; i
++)
714 toattr
->values
[i
].collection
= attr
->values
[i
].collection
;
715 attr
->values
[i
].collection
->use
++;
719 case IPP_TAG_STRING
:
722 toattr
= ippAddOctetString(to
, group_tag
, attr
->name
, NULL
, 0);
723 toattr
->value_tag
|= quickcopy
;
724 toattr
->values
[0].unknown
.data
= attr
->values
[0].unknown
.data
;
725 toattr
->values
[0].unknown
.length
= attr
->values
[0].unknown
.length
;
728 toattr
= ippAddOctetString(to
, attr
->group_tag
, attr
->name
,
729 attr
->values
[0].unknown
.data
,
730 attr
->values
[0].unknown
.length
);
734 toattr
= ippAddIntegers(to
, group_tag
, attr
->value_tag
,
735 attr
->name
, attr
->num_values
, NULL
);
737 for (i
= 0; i
< attr
->num_values
; i
++)
739 toattr
->values
[i
].unknown
.length
= attr
->values
[i
].unknown
.length
;
741 if (toattr
->values
[i
].unknown
.length
> 0)
743 if ((toattr
->values
[i
].unknown
.data
=
744 malloc(toattr
->values
[i
].unknown
.length
)) == NULL
)
745 toattr
->values
[i
].unknown
.length
= 0;
747 memcpy(toattr
->values
[i
].unknown
.data
,
748 attr
->values
[i
].unknown
.data
,
749 toattr
->values
[i
].unknown
.length
);
752 break; /* anti-compiler-warning-code */
760 * 'copy_attributes()' - Copy attributes from one request to another.
764 copy_attributes(ipp_t
*to
, /* I - Destination request */
765 ipp_t
*from
, /* I - Source request */
766 cups_array_t
*ra
, /* I - Requested attributes */
767 ipp_tag_t group_tag
, /* I - Group to copy */
768 int quickcopy
) /* I - Do a quick copy? */
770 ipp_attribute_t
*fromattr
; /* Source attribute */
776 for (fromattr
= from
->attrs
; fromattr
; fromattr
= fromattr
->next
)
779 * Filter attributes as needed...
782 if ((group_tag
!= IPP_TAG_ZERO
&& fromattr
->group_tag
!= group_tag
&&
783 fromattr
->group_tag
!= IPP_TAG_ZERO
) || !fromattr
->name
)
786 if (!ra
|| cupsArrayFind(ra
, fromattr
->name
))
787 copy_attribute(to
, fromattr
, fromattr
->group_tag
, quickcopy
);
793 * 'copy_job_attrs()' - Copy job attributes to the response.
798 _ipp_client_t
*client
, /* I - Client */
799 _ipp_job_t
*job
, /* I - Job */
800 cups_array_t
*ra
) /* I - requested-attributes */
802 copy_attributes(client
->response
, job
->attrs
, ra
, IPP_TAG_ZERO
, 0);
804 if (!ra
|| cupsArrayFind(ra
, "job-printer-up-time"))
805 ippAddInteger(client
->response
, IPP_TAG_JOB
, IPP_TAG_INTEGER
,
806 "job-printer-up-time", (int)time(NULL
));
808 if (!ra
|| cupsArrayFind(ra
, "job-state"))
809 ippAddInteger(client
->response
, IPP_TAG_JOB
, IPP_TAG_ENUM
,
810 "job-state", job
->state
);
812 if (!ra
|| cupsArrayFind(ra
, "job-state-reasons"))
816 case IPP_JOB_PENDING
:
817 ippAddString(client
->response
, IPP_TAG_JOB
,
818 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
824 ippAddString(client
->response
, IPP_TAG_JOB
,
825 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
826 NULL
, "job-incoming");
827 else if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_ZERO
))
828 ippAddString(client
->response
, IPP_TAG_JOB
,
829 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
830 NULL
, "job-hold-until-specified");
833 case IPP_JOB_PROCESSING
:
835 ippAddString(client
->response
, IPP_TAG_JOB
,
836 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
837 NULL
, "processing-to-stop-point");
839 ippAddString(client
->response
, IPP_TAG_JOB
,
840 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
841 NULL
, "job-printing");
844 case IPP_JOB_STOPPED
:
845 ippAddString(client
->response
, IPP_TAG_JOB
,
846 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
847 NULL
, "job-stopped");
850 case IPP_JOB_CANCELED
:
851 ippAddString(client
->response
, IPP_TAG_JOB
,
852 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
853 NULL
, "job-canceled-by-user");
856 case IPP_JOB_ABORTED
:
857 ippAddString(client
->response
, IPP_TAG_JOB
,
858 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
859 NULL
, "aborted-by-system");
862 case IPP_JOB_COMPLETED
:
863 ippAddString(client
->response
, IPP_TAG_JOB
,
864 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "job-state-reasons",
865 NULL
, "job-completed-successfully");
870 if (!ra
|| cupsArrayFind(ra
, "time-at-completed"))
871 ippAddInteger(client
->response
, IPP_TAG_JOB
,
872 job
->completed
? IPP_TAG_INTEGER
: IPP_TAG_NOVALUE
,
873 "time-at-completed", job
->completed
);
875 if (!ra
|| cupsArrayFind(ra
, "time-at-processing"))
876 ippAddInteger(client
->response
, IPP_TAG_JOB
,
877 job
->processing
? IPP_TAG_INTEGER
: IPP_TAG_NOVALUE
,
878 "time-at-processing", job
->processing
);
883 * 'create_client()' - Accept a new network connection and create a client
887 static _ipp_client_t
* /* O - Client */
888 create_client(_ipp_printer_t
*printer
, /* I - Printer */
889 int sock
) /* I - Listen socket */
891 _ipp_client_t
*client
; /* Client */
892 int val
; /* Parameter value */
893 socklen_t addrlen
; /* Length of address */
896 if ((client
= calloc(1, sizeof(_ipp_client_t
))) == NULL
)
898 perror("Unable to allocate memory for client");
902 client
->printer
= printer
;
903 client
->http
.activity
= time(NULL
);
904 client
->http
.hostaddr
= &(client
->addr
);
905 client
->http
.blocking
= 1;
908 * Accept the client and get the remote address...
911 addrlen
= sizeof(http_addr_t
);
913 if ((client
->http
.fd
= accept(sock
, (struct sockaddr
*)&(client
->addr
),
916 perror("Unable to accept client connection");
923 httpAddrString(&(client
->addr
), client
->http
.hostname
,
924 sizeof(client
->http
.hostname
));
927 fprintf(stderr
, "Accepted connection from %s (%s)\n", client
->http
.hostname
,
928 client
->http
.hostaddr
->addr
.sa_family
== AF_INET
? "IPv4" : "IPv6");
931 * Using TCP_NODELAY improves responsiveness, especially on systems
932 * with a slow loopback interface. Since we write large buffers
933 * when sending print files and requests, there shouldn't be any
934 * performance penalty for this...
938 setsockopt(client
->http
.fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
,
946 * 'create_job()' - Create a new job object from a Print-Job or Create-Job
950 static _ipp_job_t
* /* O - Job */
951 create_job(_ipp_client_t
*client
) /* I - Client */
953 _ipp_job_t
*job
; /* Job */
954 ipp_attribute_t
*attr
; /* Job attribute */
955 char uri
[1024]; /* job-uri value */
958 _cupsRWLockWrite(&(client
->printer
->rwlock
));
959 if (client
->printer
->active_job
&&
960 client
->printer
->active_job
->state
< IPP_JOB_CANCELED
)
963 * Only accept a single job at a time...
966 _cupsRWLockWrite(&(client
->printer
->rwlock
));
971 * Allocate and initialize the job object...
974 if ((job
= calloc(1, sizeof(_ipp_job_t
))) == NULL
)
976 perror("Unable to allocate memory for job");
980 job
->printer
= client
->printer
;
981 job
->attrs
= client
->request
;
982 job
->state
= IPP_JOB_HELD
;
984 client
->request
= NULL
;
987 * Set all but the first two attributes to the job attributes group...
990 for (attr
= job
->attrs
->attrs
->next
->next
; attr
; attr
= attr
->next
)
991 attr
->group_tag
= IPP_TAG_JOB
;
994 * Get the requesting-user-name, document format, and priority...
997 if ((attr
= ippFindAttribute(job
->attrs
, "requesting-user-name",
998 IPP_TAG_NAME
)) != NULL
)
1000 _cupsStrFree(attr
->name
);
1001 attr
->name
= _cupsStrAlloc("job-originating-user-name");
1004 attr
= ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_NAME
| IPP_TAG_COPY
,
1005 "job-originating-user-name", NULL
, "anonymous");
1008 job
->username
= attr
->values
[0].string
.text
;
1010 job
->username
= "anonymous";
1012 if ((attr
= ippFindAttribute(job
->attrs
, "document-format",
1013 IPP_TAG_MIMETYPE
)) != NULL
)
1014 job
->format
= attr
->values
[0].string
.text
;
1016 job
->format
= "application/octet-stream";
1019 * Add job description attributes and add to the jobs array...
1022 job
->id
= client
->printer
->next_job_id
++;
1024 snprintf(uri
, sizeof(uri
), "%s/%d", client
->printer
->uri
, job
->id
);
1026 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-id", job
->id
);
1027 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1028 ippAddString(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_URI
, "job-printer-uri", NULL
,
1029 client
->printer
->uri
);
1030 ippAddInteger(job
->attrs
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "time-at-creation",
1033 cupsArrayAdd(client
->printer
->jobs
, job
);
1034 client
->printer
->active_job
= job
;
1036 _cupsRWUnlock(&(client
->printer
->rwlock
));
1043 * 'create_listener()' - Create a listener socket.
1046 static int /* O - Listener socket or -1 on error */
1047 create_listener(int family
, /* I - Address family */
1048 int *port
) /* IO - Port number */
1050 int sock
, /* Listener socket */
1051 val
; /* Socket value */
1052 http_addr_t address
; /* Listen address */
1053 socklen_t addrlen
; /* Length of listen address */
1056 if ((sock
= socket(family
, SOCK_STREAM
, 0)) < 0)
1060 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
1063 if (family
== AF_INET6
)
1064 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof(val
));
1065 #endif /* IPV6_V6ONLY */
1070 * Get the auto-assigned port number for the IPv4 socket...
1073 /* TODO: This code does not appear to work - port is always 0... */
1074 addrlen
= sizeof(address
);
1075 if (getsockname(sock
, (struct sockaddr
*)&address
, &addrlen
))
1077 perror("getsockname() failed");
1081 *port
= _httpAddrPort(&address
);
1083 fprintf(stderr
, "Listening on port %d.\n", *port
);
1086 memset(&address
, 0, sizeof(address
));
1087 address
.addr
.sa_family
= family
;
1088 _httpAddrSetPort(&address
, *port
);
1090 if (bind(sock
, (struct sockaddr
*)&address
, httpAddrLength(&address
)))
1096 if (listen(sock
, 5))
1107 * 'create_media_col()' - Create a media-col value.
1110 static ipp_t
* /* O - media-col collection */
1111 create_media_col(const char *media
, /* I - Media name */
1112 const char *type
, /* I - Nedua type */
1113 int width
, /* I - x-dimension in 2540ths */
1114 int length
, /* I - y-dimension in 2540ths */
1115 int margins
) /* I - Value for margins */
1117 ipp_t
*media_col
= ippNew(), /* media-col value */
1118 *media_size
= ippNew(); /* media-size value */
1119 char media_key
[256]; /* media-key value */
1122 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "x-dimension",
1124 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "y-dimension",
1127 snprintf(media_key
, sizeof(media_key
), "%s_%s%s", media
, type
,
1128 margins
== 0 ? "_borderless" : "");
1130 ippAddString(media_col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-key", NULL
,
1132 ippAddCollection(media_col
, IPP_TAG_PRINTER
, "media-size", media_size
);
1133 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1134 "media-bottom-margin", margins
);
1135 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1136 "media-left-margin", margins
);
1137 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1138 "media-right-margin", margins
);
1139 ippAddInteger(media_col
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1140 "media-top-margin", margins
);
1141 ippAddString(media_col
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-type",
1144 ippDelete(media_size
);
1151 * 'create_printer()' - Create, register, and listen for connections to a
1155 static _ipp_printer_t
* /* O - Printer */
1156 create_printer(const char *servername
, /* I - Server hostname (NULL for default) */
1157 const char *name
, /* I - printer-name */
1158 const char *location
, /* I - printer-location */
1159 const char *make
, /* I - printer-make-and-model */
1160 const char *model
, /* I - printer-make-and-model */
1161 const char *icon
, /* I - printer-icons */
1162 const char *docformats
, /* I - document-format-supported */
1163 int ppm
, /* I - Pages per minute in grayscale */
1164 int ppm_color
, /* I - Pages per minute in color (0 for gray) */
1165 int duplex
, /* I - 1 = duplex, 0 = simplex */
1166 int port
, /* I - Port for listeners or 0 for auto */
1168 const char *regtype
, /* I - Bonjour service type */
1169 #endif /* HAVE_DNSSD */
1170 const char *directory
) /* I - Spool directory */
1172 int i
, j
; /* Looping vars */
1173 _ipp_printer_t
*printer
; /* Printer */
1174 char hostname
[256], /* Hostname */
1175 uri
[1024], /* Printer URI */
1176 icons
[1024], /* printer-icons URI */
1177 adminurl
[1024], /* printer-more-info URI */
1178 device_id
[1024],/* printer-device-id */
1179 make_model
[128];/* printer-make-and-model */
1180 int num_formats
; /* Number of document-format-supported values */
1181 char *defformat
, /* document-format-default value */
1182 *formats
[100], /* document-format-supported values */
1183 *ptr
; /* Pointer into string */
1184 const char *prefix
; /* Prefix string */
1185 int num_database
; /* Number of database values */
1186 ipp_attribute_t
*media_col_database
;
1187 /* media-col-database value */
1188 ipp_t
*media_col_default
;
1189 /* media-col-default value */
1190 ipp_value_t
*media_col_value
;
1191 /* Current media-col-database value */
1192 int k_supported
; /* Maximum file size supported */
1194 struct statvfs spoolinfo
; /* FS info for spool directory */
1195 double spoolsize
; /* FS size */
1196 #elif defined(HAVE_STATFS)
1197 struct statfs spoolinfo
; /* FS info for spool directory */
1198 double spoolsize
; /* FS size */
1199 #endif /* HAVE_STATVFS */
1200 static const int orients
[4] = /* orientation-requested-supported values */
1204 IPP_REVERSE_LANDSCAPE
,
1205 IPP_REVERSE_PORTRAIT
1207 static const char * const versions
[] =/* ipp-versions-supported values */
1213 static const int ops
[] = /* operations-supported values */
1220 IPP_GET_JOB_ATTRIBUTES
,
1222 IPP_GET_PRINTER_ATTRIBUTES
1224 static const char * const charsets
[] =/* charset-supported values */
1229 static const char * const job_creation
[] =
1230 { /* job-creation-attributes-supported values */
1232 "ipp-attribute-fidelity",
1237 "multiple-document-handling",
1238 "orientation-requested",
1242 static const char * const media_col_supported
[] =
1243 { /* media-col-supported values */
1244 "media-bottom-margin",
1245 "media-left-margin",
1246 "media-right-margin",
1251 static const int media_xxx_margin_supported
[] =
1252 { /* media-xxx-margin-supported values */
1256 static const char * const multiple_document_handling
[] =
1257 { /* multiple-document-handling-supported values */
1258 "separate-documents-uncollated-copies",
1259 "separate-documents-collated-copies"
1261 static const int print_quality_supported
[] =
1262 { /* print-quality-supported values */
1267 static const char * const sides_supported
[] =
1268 { /* sides-supported values */
1270 "two-sided-long-edge",
1271 "two-sided-short-edge"
1273 static const char * const which_jobs
[] =
1274 { /* which-jobs-supported values */
1283 "processing-stopped"
1288 * Allocate memory for the printer...
1291 if ((printer
= calloc(1, sizeof(_ipp_printer_t
))) == NULL
)
1293 perror("Unable to allocate memory for printer");
1299 printer
->name
= _cupsStrAlloc(name
);
1301 printer
->dnssd_name
= _cupsStrRetain(printer
->name
);
1302 #endif /* HAVE_DNSSD */
1303 printer
->directory
= _cupsStrAlloc(directory
);
1304 printer
->hostname
= _cupsStrAlloc(servername
? servername
:
1305 httpGetHostname(NULL
, hostname
,
1307 printer
->port
= port
;
1308 printer
->state
= IPP_PRINTER_IDLE
;
1309 printer
->state_reasons
= _IPP_PRINTER_NONE
;
1310 printer
->jobs
= cupsArrayNew((cups_array_func_t
)compare_jobs
, NULL
);
1311 printer
->next_job_id
= 1;
1313 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1314 printer
->hostname
, printer
->port
, "/ipp");
1315 printer
->uri
= _cupsStrAlloc(uri
);
1316 printer
->urilen
= strlen(uri
);
1318 _cupsRWInit(&(printer
->rwlock
));
1321 * Create the listener sockets...
1324 if ((printer
->ipv4
= create_listener(AF_INET
, &(printer
->port
))) < 0)
1326 perror("Unable to create IPv4 listener");
1330 if ((printer
->ipv6
= create_listener(AF_INET6
, &(printer
->port
))) < 0)
1332 perror("Unable to create IPv6 listener");
1337 * Prepare values for the printer attributes...
1340 httpAssembleURI(HTTP_URI_CODING_ALL
, icons
, sizeof(icons
), "http", NULL
,
1341 printer
->hostname
, printer
->port
, "/icon.png");
1342 httpAssembleURI(HTTP_URI_CODING_ALL
, adminurl
, sizeof(adminurl
), "http", NULL
,
1343 printer
->hostname
, printer
->port
, "/");
1347 fprintf(stderr
, "printer-more-info=\"%s\"\n", adminurl
);
1348 fprintf(stderr
, "printer-uri=\"%s\"\n", uri
);
1351 snprintf(make_model
, sizeof(make_model
), "%s %s", make
, model
);
1354 formats
[0] = strdup(docformats
);
1355 defformat
= formats
[0];
1356 for (ptr
= strchr(formats
[0], ','); ptr
; ptr
= strchr(ptr
, ','))
1359 formats
[num_formats
++] = ptr
;
1361 if (!_cups_strcasecmp(ptr
, "application/octet-stream"))
1365 snprintf(device_id
, sizeof(device_id
), "MFG:%s;MDL:%s;", make
, model
);
1366 ptr
= device_id
+ strlen(device_id
);
1368 for (i
= 0; i
< num_formats
; i
++)
1370 if (!_cups_strcasecmp(formats
[i
], "application/pdf"))
1371 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%sPDF", prefix
);
1372 else if (!_cups_strcasecmp(formats
[i
], "application/postscript"))
1373 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%sPS", prefix
);
1374 else if (!_cups_strcasecmp(formats
[i
], "application/vnd.hp-PCL"))
1375 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%sPCL", prefix
);
1376 else if (!_cups_strcasecmp(formats
[i
], "image/jpeg"))
1377 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%sJPEG", prefix
);
1378 else if (!_cups_strcasecmp(formats
[i
], "image/png"))
1379 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%sPNG", prefix
);
1380 else if (_cups_strcasecmp(formats
[i
], "application/octet-stream"))
1381 snprintf(ptr
, sizeof(device_id
) - (ptr
- device_id
), "%s%s", prefix
,
1387 strlcat(device_id
, ";", sizeof(device_id
));
1390 * Get the maximum spool size based on the size of the filesystem used for
1391 * the spool directory. If the host OS doesn't support the statfs call
1392 * or the filesystem is larger than 2TiB, always report INT_MAX.
1396 if (statvfs(printer
->directory
, &spoolinfo
))
1397 k_supported
= INT_MAX
;
1398 else if ((spoolsize
= (double)spoolinfo
.f_frsize
*
1399 spoolinfo
.f_blocks
/ 1024) > INT_MAX
)
1400 k_supported
= INT_MAX
;
1402 k_supported
= (int)spoolsize
;
1404 #elif defined(HAVE_STATFS)
1405 if (statfs(printer
->directory
, &spoolinfo
))
1406 k_supported
= INT_MAX
;
1407 else if ((spoolsize
= (double)spoolinfo
.f_bsize
*
1408 spoolinfo
.f_blocks
/ 1024) > INT_MAX
)
1409 k_supported
= INT_MAX
;
1411 k_supported
= (int)spoolsize
;
1414 k_supported
= INT_MAX
;
1415 #endif /* HAVE_STATVFS */
1418 * Create the printer attributes. This list of attributes is sorted to improve
1419 * performance when the client provides a requested-attributes attribute...
1422 printer
->attrs
= ippNew();
1424 /* charset-configured */
1425 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
1426 "charset-configured", NULL
, "utf-8");
1428 /* charset-supported */
1429 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
1430 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
1433 /* color-supported */
1434 ippAddBoolean(printer
->attrs
, IPP_TAG_PRINTER
, "color-supported",
1437 /* compression-supported */
1438 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1439 "compression-supported", NULL
, "none");
1441 /* copies-default */
1442 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1443 "copies-default", 1);
1445 /* copies-supported */
1446 ippAddRange(printer
->attrs
, IPP_TAG_PRINTER
, "copies-supported", 1, 999);
1448 /* document-format-default */
1449 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
1450 "document-format-default", NULL
, defformat
);
1452 /* document-format-supported */
1453 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
1454 "document-format-supported", num_formats
, NULL
,
1455 (const char * const *)formats
);
1457 /* finishings-default */
1458 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1459 "finishings-default", IPP_FINISHINGS_NONE
);
1461 /* finishings-supported */
1462 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1463 "finishings-supported", IPP_FINISHINGS_NONE
);
1465 /* generated-natural-language-supported */
1466 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
1467 "generated-natural-language-supported", NULL
, "en");
1469 /* ipp-versions-supported */
1470 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1471 "ipp-versions-supported",
1472 sizeof(versions
) / sizeof(versions
[0]), NULL
, versions
);
1474 /* job-creation-attributes-supported */
1475 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1476 "job-creation-attributes-supported",
1477 sizeof(job_creation
) / sizeof(job_creation
[0]),
1478 NULL
, job_creation
);
1480 /* job-k-octets-supported */
1481 ippAddRange(printer
->attrs
, IPP_TAG_PRINTER
, "job-k-octets-supported", 0,
1484 /* job-priority-default */
1485 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1486 "job-priority-default", 50);
1488 /* job-priority-supported */
1489 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1490 "job-priority-supported", 100);
1492 /* job-sheets-default */
1493 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
1494 "job-sheets-default", NULL
, "none");
1496 /* job-sheets-supported */
1497 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
1498 "job-sheets-supported", NULL
, "none");
1500 /* media-bottom-margin-supported */
1501 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1502 "media-bottom-margin-supported",
1503 (int)(sizeof(media_xxx_margin_supported
) /
1504 sizeof(media_xxx_margin_supported
[0])),
1505 media_xxx_margin_supported
);
1507 /* media-col-database */
1508 for (num_database
= 0, i
= 0;
1509 i
< (int)(sizeof(media_col_sizes
) / sizeof(media_col_sizes
[0]));
1512 if (media_col_sizes
[i
][2] == _IPP_ENV_ONLY
)
1513 num_database
+= 2; /* auto + envelope */
1514 else if (media_col_sizes
[i
][2] == _IPP_PHOTO_ONLY
)
1515 num_database
+= 12; /* auto + photographic-* + borderless */
1517 num_database
+= (int)(sizeof(media_type_supported
) /
1518 sizeof(media_type_supported
[0])) + 6;
1519 /* All types + borderless */
1522 media_col_database
= ippAddCollections(printer
->attrs
, IPP_TAG_PRINTER
,
1523 "media-col-database", num_database
,
1525 for (media_col_value
= media_col_database
->values
, i
= 0;
1526 i
< (int)(sizeof(media_col_sizes
) / sizeof(media_col_sizes
[0]));
1530 j
< (int)(sizeof(media_type_supported
) /
1531 sizeof(media_type_supported
[0]));
1534 if (media_col_sizes
[i
][2] == _IPP_ENV_ONLY
&&
1535 strcmp(media_type_supported
[j
], "auto") &&
1536 strcmp(media_type_supported
[j
], "envelope"))
1538 else if (media_col_sizes
[i
][2] == _IPP_PHOTO_ONLY
&&
1539 strcmp(media_type_supported
[j
], "auto") &&
1540 strncmp(media_type_supported
[j
], "photographic-", 13))
1543 media_col_value
->collection
=
1544 create_media_col(media_supported
[i
], media_type_supported
[j
],
1545 media_col_sizes
[i
][0], media_col_sizes
[i
][1],
1546 media_xxx_margin_supported
[1]);
1549 if (media_col_sizes
[i
][2] != _IPP_ENV_ONLY
&&
1550 (!strcmp(media_type_supported
[j
], "auto") ||
1551 !strncmp(media_type_supported
[j
], "photographic-", 13)))
1554 * Add borderless version for this combination...
1557 media_col_value
->collection
=
1558 create_media_col(media_supported
[i
], media_type_supported
[j
],
1559 media_col_sizes
[i
][0], media_col_sizes
[i
][1],
1560 media_xxx_margin_supported
[0]);
1566 /* media-col-default */
1567 media_col_default
= create_media_col(media_supported
[0],
1568 media_type_supported
[0],
1569 media_col_sizes
[0][0],
1570 media_col_sizes
[0][1],
1571 media_xxx_margin_supported
[1]);
1573 ippAddCollection(printer
->attrs
, IPP_TAG_PRINTER
, "media-col-default",
1575 ippDelete(media_col_default
);
1577 /* media-col-supported */
1578 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1579 "media-col-supported",
1580 (int)(sizeof(media_col_supported
) /
1581 sizeof(media_col_supported
[0])), NULL
,
1582 media_col_supported
);
1585 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1586 "media-default", NULL
, media_supported
[0]);
1588 /* media-left-margin-supported */
1589 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1590 "media-left-margin-supported",
1591 (int)(sizeof(media_xxx_margin_supported
) /
1592 sizeof(media_xxx_margin_supported
[0])),
1593 media_xxx_margin_supported
);
1595 /* media-right-margin-supported */
1596 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1597 "media-right-margin-supported",
1598 (int)(sizeof(media_xxx_margin_supported
) /
1599 sizeof(media_xxx_margin_supported
[0])),
1600 media_xxx_margin_supported
);
1602 /* media-supported */
1603 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1605 (int)(sizeof(media_supported
) / sizeof(media_supported
[0])),
1606 NULL
, media_supported
);
1608 /* media-top-margin-supported */
1609 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1610 "media-top-margin-supported",
1611 (int)(sizeof(media_xxx_margin_supported
) /
1612 sizeof(media_xxx_margin_supported
[0])),
1613 media_xxx_margin_supported
);
1615 /* multiple-document-handling-supported */
1616 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1617 "multiple-document-handling-supported",
1618 sizeof(multiple_document_handling
) /
1619 sizeof(multiple_document_handling
[0]), NULL
,
1620 multiple_document_handling
);
1622 /* multiple-document-jobs-supported */
1623 ippAddBoolean(printer
->attrs
, IPP_TAG_PRINTER
,
1624 "multiple-document-jobs-supported", 0);
1626 /* natural-language-configured */
1627 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
1628 "natural-language-configured", NULL
, "en");
1630 /* number-up-default */
1631 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1632 "number-up-default", 1);
1634 /* number-up-supported */
1635 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1636 "number-up-supported", 1);
1638 /* operations-supported */
1639 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1640 "operations-supported", sizeof(ops
) / sizeof(ops
[0]), ops
);
1642 /* orientation-requested-default */
1643 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
1644 "orientation-requested-default", 0);
1646 /* orientation-requested-supported */
1647 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1648 "orientation-requested-supported", 4, orients
);
1650 /* output-bin-default */
1651 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1652 "output-bin-default", NULL
, "face-down");
1654 /* output-bin-supported */
1655 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1656 "output-bin-supported", NULL
, "face-down");
1658 /* pages-per-minute */
1659 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1660 "pages-per-minute", ppm
);
1662 /* pages-per-minute-color */
1664 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1665 "pages-per-minute-color", ppm_color
);
1667 /* pdl-override-supported */
1668 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1669 "pdl-override-supported", NULL
, "attempted");
1671 /* print-quality-default */
1672 ippAddInteger(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1673 "print-quality-default", IPP_QUALITY_NORMAL
);
1675 /* print-quality-supported */
1676 ippAddIntegers(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1677 "print-quality-supported",
1678 (int)(sizeof(print_quality_supported
) /
1679 sizeof(print_quality_supported
[0])),
1680 print_quality_supported
);
1682 /* printer-device-id */
1683 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1684 "printer-device-id", NULL
, device_id
);
1687 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1688 "printer-icons", NULL
, icons
);
1690 /* printer-is-accepting-jobs */
1691 ippAddBoolean(printer
->attrs
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
1695 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1698 /* printer-location */
1699 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1700 "printer-location", NULL
, location
);
1702 /* printer-make-and-model */
1703 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1704 "printer-make-and-model", NULL
, make_model
);
1706 /* printer-more-info */
1707 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1708 "printer-more-info", NULL
, adminurl
);
1711 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name",
1714 /* printer-resolution-default */
1715 ippAddResolution(printer
->attrs
, IPP_TAG_PRINTER
,
1716 "printer-resolution-default", IPP_RES_PER_INCH
, 600, 600);
1718 /* printer-resolution-supported */
1719 ippAddResolution(printer
->attrs
, IPP_TAG_PRINTER
,
1720 "printer-resolution-supported", IPP_RES_PER_INCH
, 600, 600);
1722 /* printer-uri-supported */
1723 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1724 "printer-uri-supported", NULL
, uri
);
1727 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1728 "sides-default", NULL
, "one-sided");
1730 /* sides-supported */
1731 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1732 "sides-supported", duplex
? 3 : 1, NULL
, sides_supported
);
1734 /* uri-authentication-supported */
1735 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1736 "uri-authentication-supported", NULL
, "none");
1738 /* uri-security-supported */
1739 ippAddString(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1740 "uri-security-supported", NULL
, "none");
1742 /* which-jobs-supported */
1743 ippAddStrings(printer
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
1744 "which-jobs-supported",
1745 sizeof(which_jobs
) / sizeof(which_jobs
[0]), NULL
, which_jobs
);
1749 debug_attributes("Printer", printer
->attrs
);
1753 * Register the printer with Bonjour...
1756 if (!register_printer(printer
, location
, make
, model
, docformats
, adminurl
,
1757 ppm_color
> 0, duplex
, regtype
))
1759 #endif /* HAVE_DNSSD */
1769 * If we get here we were unable to create the printer...
1774 delete_printer(printer
);
1780 * 'create_requested_array()' - Create an array for requested-attributes.
1783 static cups_array_t
* /* O - requested-attributes array */
1784 create_requested_array(
1785 _ipp_client_t
*client
) /* I - Client */
1787 int i
; /* Looping var */
1788 ipp_attribute_t
*requested
; /* requested-attributes attribute */
1789 cups_array_t
*ra
; /* Requested attributes array */
1790 char *value
; /* Current value */
1794 * Get the requested-attributes attribute, and return NULL if we don't
1798 if ((requested
= ippFindAttribute(client
->request
, "requested-attributes",
1799 IPP_TAG_KEYWORD
)) == NULL
)
1803 * If the attribute contains a single "all" keyword, return NULL...
1806 if (requested
->num_values
== 1 &&
1807 !strcmp(requested
->values
[0].string
.text
, "all"))
1811 * Create an array using "strcmp" as the comparison function...
1814 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
1816 for (i
= 0; i
< requested
->num_values
; i
++)
1818 value
= requested
->values
[i
].string
.text
;
1820 if (!strcmp(value
, "job-template"))
1822 cupsArrayAdd(ra
, "copies");
1823 cupsArrayAdd(ra
, "copies-default");
1824 cupsArrayAdd(ra
, "copies-supported");
1825 cupsArrayAdd(ra
, "finishings");
1826 cupsArrayAdd(ra
, "finishings-default");
1827 cupsArrayAdd(ra
, "finishings-supported");
1828 cupsArrayAdd(ra
, "job-hold-until");
1829 cupsArrayAdd(ra
, "job-hold-until-default");
1830 cupsArrayAdd(ra
, "job-hold-until-supported");
1831 cupsArrayAdd(ra
, "job-priority");
1832 cupsArrayAdd(ra
, "job-priority-default");
1833 cupsArrayAdd(ra
, "job-priority-supported");
1834 cupsArrayAdd(ra
, "job-sheets");
1835 cupsArrayAdd(ra
, "job-sheets-default");
1836 cupsArrayAdd(ra
, "job-sheets-supported");
1837 cupsArrayAdd(ra
, "media");
1838 cupsArrayAdd(ra
, "media-col");
1839 cupsArrayAdd(ra
, "media-col-default");
1840 cupsArrayAdd(ra
, "media-col-supported");
1841 cupsArrayAdd(ra
, "media-default");
1842 cupsArrayAdd(ra
, "media-source-supported");
1843 cupsArrayAdd(ra
, "media-supported");
1844 cupsArrayAdd(ra
, "media-type-supported");
1845 cupsArrayAdd(ra
, "multiple-document-handling");
1846 cupsArrayAdd(ra
, "multiple-document-handling-default");
1847 cupsArrayAdd(ra
, "multiple-document-handling-supported");
1848 cupsArrayAdd(ra
, "number-up");
1849 cupsArrayAdd(ra
, "number-up-default");
1850 cupsArrayAdd(ra
, "number-up-supported");
1851 cupsArrayAdd(ra
, "orientation-requested");
1852 cupsArrayAdd(ra
, "orientation-requested-default");
1853 cupsArrayAdd(ra
, "orientation-requested-supported");
1854 cupsArrayAdd(ra
, "page-ranges");
1855 cupsArrayAdd(ra
, "page-ranges-supported");
1856 cupsArrayAdd(ra
, "printer-resolution");
1857 cupsArrayAdd(ra
, "printer-resolution-default");
1858 cupsArrayAdd(ra
, "printer-resolution-supported");
1859 cupsArrayAdd(ra
, "print-quality");
1860 cupsArrayAdd(ra
, "print-quality-default");
1861 cupsArrayAdd(ra
, "print-quality-supported");
1862 cupsArrayAdd(ra
, "sides");
1863 cupsArrayAdd(ra
, "sides-default");
1864 cupsArrayAdd(ra
, "sides-supported");
1866 else if (!strcmp(value
, "job-description"))
1868 cupsArrayAdd(ra
, "date-time-at-completed");
1869 cupsArrayAdd(ra
, "date-time-at-creation");
1870 cupsArrayAdd(ra
, "date-time-at-processing");
1871 cupsArrayAdd(ra
, "job-detailed-status-message");
1872 cupsArrayAdd(ra
, "job-document-access-errors");
1873 cupsArrayAdd(ra
, "job-id");
1874 cupsArrayAdd(ra
, "job-impressions");
1875 cupsArrayAdd(ra
, "job-impressions-completed");
1876 cupsArrayAdd(ra
, "job-k-octets");
1877 cupsArrayAdd(ra
, "job-k-octets-processed");
1878 cupsArrayAdd(ra
, "job-media-sheets");
1879 cupsArrayAdd(ra
, "job-media-sheets-completed");
1880 cupsArrayAdd(ra
, "job-message-from-operator");
1881 cupsArrayAdd(ra
, "job-more-info");
1882 cupsArrayAdd(ra
, "job-name");
1883 cupsArrayAdd(ra
, "job-originating-user-name");
1884 cupsArrayAdd(ra
, "job-printer-up-time");
1885 cupsArrayAdd(ra
, "job-printer-uri");
1886 cupsArrayAdd(ra
, "job-state");
1887 cupsArrayAdd(ra
, "job-state-message");
1888 cupsArrayAdd(ra
, "job-state-reasons");
1889 cupsArrayAdd(ra
, "job-uri");
1890 cupsArrayAdd(ra
, "number-of-documents");
1891 cupsArrayAdd(ra
, "number-of-intervening-jobs");
1892 cupsArrayAdd(ra
, "output-device-assigned");
1893 cupsArrayAdd(ra
, "time-at-completed");
1894 cupsArrayAdd(ra
, "time-at-creation");
1895 cupsArrayAdd(ra
, "time-at-processing");
1897 else if (!strcmp(value
, "printer-description"))
1899 cupsArrayAdd(ra
, "charset-configured");
1900 cupsArrayAdd(ra
, "charset-supported");
1901 cupsArrayAdd(ra
, "color-supported");
1902 cupsArrayAdd(ra
, "compression-supported");
1903 cupsArrayAdd(ra
, "document-format-default");
1904 cupsArrayAdd(ra
, "document-format-supported");
1905 cupsArrayAdd(ra
, "generated-natural-language-supported");
1906 cupsArrayAdd(ra
, "ipp-versions-supported");
1907 cupsArrayAdd(ra
, "job-impressions-supported");
1908 cupsArrayAdd(ra
, "job-k-octets-supported");
1909 cupsArrayAdd(ra
, "job-media-sheets-supported");
1910 cupsArrayAdd(ra
, "multiple-document-jobs-supported");
1911 cupsArrayAdd(ra
, "multiple-operation-time-out");
1912 cupsArrayAdd(ra
, "natural-language-configured");
1913 cupsArrayAdd(ra
, "notify-attributes-supported");
1914 cupsArrayAdd(ra
, "notify-lease-duration-default");
1915 cupsArrayAdd(ra
, "notify-lease-duration-supported");
1916 cupsArrayAdd(ra
, "notify-max-events-supported");
1917 cupsArrayAdd(ra
, "notify-events-default");
1918 cupsArrayAdd(ra
, "notify-events-supported");
1919 cupsArrayAdd(ra
, "notify-pull-method-supported");
1920 cupsArrayAdd(ra
, "notify-schemes-supported");
1921 cupsArrayAdd(ra
, "operations-supported");
1922 cupsArrayAdd(ra
, "pages-per-minute");
1923 cupsArrayAdd(ra
, "pages-per-minute-color");
1924 cupsArrayAdd(ra
, "pdl-override-supported");
1925 cupsArrayAdd(ra
, "printer-alert");
1926 cupsArrayAdd(ra
, "printer-alert-description");
1927 cupsArrayAdd(ra
, "printer-current-time");
1928 cupsArrayAdd(ra
, "printer-driver-installer");
1929 cupsArrayAdd(ra
, "printer-info");
1930 cupsArrayAdd(ra
, "printer-is-accepting-jobs");
1931 cupsArrayAdd(ra
, "printer-location");
1932 cupsArrayAdd(ra
, "printer-make-and-model");
1933 cupsArrayAdd(ra
, "printer-message-from-operator");
1934 cupsArrayAdd(ra
, "printer-more-info");
1935 cupsArrayAdd(ra
, "printer-more-info-manufacturer");
1936 cupsArrayAdd(ra
, "printer-name");
1937 cupsArrayAdd(ra
, "printer-state");
1938 cupsArrayAdd(ra
, "printer-state-message");
1939 cupsArrayAdd(ra
, "printer-state-reasons");
1940 cupsArrayAdd(ra
, "printer-up-time");
1941 cupsArrayAdd(ra
, "printer-uri-supported");
1942 cupsArrayAdd(ra
, "queued-job-count");
1943 cupsArrayAdd(ra
, "reference-uri-schemes-supported");
1944 cupsArrayAdd(ra
, "uri-authentication-supported");
1945 cupsArrayAdd(ra
, "uri-security-supported");
1947 else if (!strcmp(value
, "printer-defaults"))
1949 cupsArrayAdd(ra
, "copies-default");
1950 cupsArrayAdd(ra
, "document-format-default");
1951 cupsArrayAdd(ra
, "finishings-default");
1952 cupsArrayAdd(ra
, "job-hold-until-default");
1953 cupsArrayAdd(ra
, "job-priority-default");
1954 cupsArrayAdd(ra
, "job-sheets-default");
1955 cupsArrayAdd(ra
, "media-default");
1956 cupsArrayAdd(ra
, "media-col-default");
1957 cupsArrayAdd(ra
, "number-up-default");
1958 cupsArrayAdd(ra
, "orientation-requested-default");
1959 cupsArrayAdd(ra
, "sides-default");
1961 else if (!strcmp(value
, "subscription-template"))
1963 cupsArrayAdd(ra
, "notify-attributes");
1964 cupsArrayAdd(ra
, "notify-charset");
1965 cupsArrayAdd(ra
, "notify-events");
1966 cupsArrayAdd(ra
, "notify-lease-duration");
1967 cupsArrayAdd(ra
, "notify-natural-language");
1968 cupsArrayAdd(ra
, "notify-pull-method");
1969 cupsArrayAdd(ra
, "notify-recipient-uri");
1970 cupsArrayAdd(ra
, "notify-time-interval");
1971 cupsArrayAdd(ra
, "notify-user-data");
1974 cupsArrayAdd(ra
, value
);
1982 * 'debug_attributes()' - Print attributes in a request or response.
1986 debug_attributes(const char *title
, /* I - Title */
1987 ipp_t
*ipp
) /* I - Request/response */
1989 ipp_tag_t group_tag
; /* Current group */
1990 ipp_attribute_t
*attr
; /* Current attribute */
1991 char buffer
[2048]; /* String buffer for value */
1997 fprintf(stderr
, "%s:\n", title
);
1998 for (attr
= ipp
->attrs
, group_tag
= IPP_TAG_ZERO
; attr
; attr
= attr
->next
)
2000 if (attr
->group_tag
!= group_tag
)
2002 group_tag
= attr
->group_tag
;
2003 fprintf(stderr
, " %s\n", ippTagString(group_tag
));
2008 _ippAttrString(attr
, buffer
, sizeof(buffer
));
2009 fprintf(stderr
, " %s (%s%s) %s\n", attr
->name
,
2010 attr
->num_values
> 1 ? "1setOf " : "",
2011 ippTagString(attr
->value_tag
), buffer
);
2018 * 'delete_client()' - Close the socket and free all memory used by a client
2023 delete_client(_ipp_client_t
*client
) /* I - Client */
2026 fprintf(stderr
, "Closing connection from %s (%s)\n", client
->http
.hostname
,
2027 client
->http
.hostaddr
->addr
.sa_family
== AF_INET
? "IPv4" : "IPv6");
2030 * Flush pending writes before closing...
2033 httpFlushWrite(&(client
->http
));
2035 if (client
->http
.fd
>= 0)
2036 close(client
->http
.fd
);
2042 httpClearCookie(&(client
->http
));
2043 httpClearFields(&(client
->http
));
2045 ippDelete(client
->request
);
2047 ippDelete(client
->response
);
2054 * 'delete_job()' - Remove from the printer and free all memory used by a job
2059 delete_job(_ipp_job_t
*job
) /* I - Job */
2062 fprintf(stderr
, "Removing job #%d from history.\n", job
->id
);
2064 ippDelete(job
->attrs
);
2069 unlink(job
->filename
);
2071 free(job
->filename
);
2079 * 'delete_printer()' - Unregister, close listen sockets, and free all memory
2080 * used by a printer object.
2084 delete_printer(_ipp_printer_t
*printer
) /* I - Printer */
2086 if (printer
->ipv4
>= 0)
2087 close(printer
->ipv4
);
2089 if (printer
->ipv6
>= 0)
2090 close(printer
->ipv6
);
2093 if (printer
->printer_ref
)
2094 DNSServiceRefDeallocate(printer
->printer_ref
);
2096 if (printer
->ipp_ref
)
2097 DNSServiceRefDeallocate(printer
->ipp_ref
);
2099 if (printer
->http_ref
)
2100 DNSServiceRefDeallocate(printer
->http_ref
);
2102 if (printer
->common_ref
)
2103 DNSServiceRefDeallocate(printer
->common_ref
);
2105 TXTRecordDeallocate(&(printer
->ipp_txt
));
2107 if (printer
->dnssd_name
)
2108 _cupsStrFree(printer
->dnssd_name
);
2109 #endif /* HAVE_DNSSD */
2112 _cupsStrFree(printer
->name
);
2114 _cupsStrFree(printer
->icon
);
2115 if (printer
->directory
)
2116 _cupsStrFree(printer
->directory
);
2117 if (printer
->hostname
)
2118 _cupsStrFree(printer
->hostname
);
2120 _cupsStrFree(printer
->uri
);
2122 ippDelete(printer
->attrs
);
2123 cupsArrayDelete(printer
->jobs
);
2131 * 'dnssd_callback()' - Handle Bonjour registration events.
2136 DNSServiceRef sdRef
, /* I - Service reference */
2137 DNSServiceFlags flags
, /* I - Status flags */
2138 DNSServiceErrorType errorCode
, /* I - Error, if any */
2139 const char *name
, /* I - Service name */
2140 const char *regtype
, /* I - Service type */
2141 const char *domain
, /* I - Domain for service */
2142 _ipp_printer_t
*printer
) /* I - Printer */
2146 fprintf(stderr
, "DNSServiceRegister for %s failed with error %d.\n",
2147 regtype
, (int)errorCode
);
2150 else if (_cups_strcasecmp(name
, printer
->dnssd_name
))
2153 fprintf(stderr
, "Now using DNS-SD service name \"%s\".\n", name
);
2155 /* No lock needed since only the main thread accesses/changes this */
2156 _cupsStrFree(printer
->dnssd_name
);
2157 printer
->dnssd_name
= _cupsStrAlloc(name
);
2160 #endif /* HAVE_DNSSD */
2164 * 'find_job()' - Find a job specified in a request.
2167 static _ipp_job_t
* /* O - Job or NULL */
2168 find_job(_ipp_client_t
*client
) /* I - Client */
2170 ipp_attribute_t
*attr
; /* job-id or job-uri attribute */
2171 _ipp_job_t key
, /* Job search key */
2172 *job
; /* Matching job, if any */
2177 if ((attr
= ippFindAttribute(client
->request
, "job-uri",
2178 IPP_TAG_URI
)) != NULL
)
2180 if (!strncmp(attr
->values
[0].string
.text
, client
->printer
->uri
,
2181 client
->printer
->urilen
) &&
2182 attr
->values
[0].string
.text
[client
->printer
->urilen
] == '/')
2183 key
.id
= atoi(attr
->values
[0].string
.text
+ client
->printer
->urilen
+ 1);
2185 else if ((attr
= ippFindAttribute(client
->request
, "job-id",
2186 IPP_TAG_INTEGER
)) != NULL
)
2187 key
.id
= attr
->values
[0].integer
;
2189 _cupsRWLockRead(&(client
->printer
->rwlock
));
2190 job
= (_ipp_job_t
*)cupsArrayFind(client
->printer
->jobs
, &key
);
2191 _cupsRWUnlock(&(client
->printer
->rwlock
));
2198 * 'html_escape()' - Write a HTML-safe string.
2202 html_escape(_ipp_client_t
*client
, /* I - Client */
2203 const char *s
, /* I - String to write */
2204 size_t slen
) /* I - Number of characters to write */
2206 const char *start
, /* Start of segment */
2207 *end
; /* End of string */
2211 end
= s
+ (slen
> 0 ? slen
: strlen(s
));
2213 while (*s
&& s
< end
)
2215 if (*s
== '&' || *s
== '<')
2218 httpWrite2(&(client
->http
), start
, s
- start
);
2221 httpWrite2(&(client
->http
), "&", 5);
2223 httpWrite2(&(client
->http
), "<", 4);
2232 httpWrite2(&(client
->http
), start
, s
- start
);
2237 * 'html_printf()' - Send formatted text to the client, quoting as needed.
2241 html_printf(_ipp_client_t
*client
, /* I - Client */
2242 const char *format
, /* I - Printf-style format string */
2243 ...) /* I - Additional arguments as needed */
2245 va_list ap
; /* Pointer to arguments */
2246 const char *start
; /* Start of string */
2247 char size
, /* Size character (h, l, L) */
2248 type
; /* Format type character */
2249 int width
, /* Width of field */
2250 prec
; /* Number of characters of precision */
2251 char tformat
[100], /* Temporary format string for sprintf() */
2252 *tptr
, /* Pointer into temporary format */
2253 temp
[1024]; /* Buffer for formatted numbers */
2254 char *s
; /* Pointer to string */
2258 * Loop through the format string, formatting as needed...
2261 va_start(ap
, format
);
2269 httpWrite2(&(client
->http
), start
, format
- start
);
2272 *tptr
++ = *format
++;
2276 httpWrite2(&(client
->http
), "%", 1);
2280 else if (strchr(" -+#\'", *format
))
2281 *tptr
++ = *format
++;
2286 * Get width from argument...
2290 width
= va_arg(ap
, int);
2292 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
2293 tptr
+= strlen(tptr
);
2299 while (isdigit(*format
& 255))
2301 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2304 width
= width
* 10 + *format
++ - '0';
2310 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2318 * Get precision from argument...
2322 prec
= va_arg(ap
, int);
2324 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
2325 tptr
+= strlen(tptr
);
2331 while (isdigit(*format
& 255))
2333 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2336 prec
= prec
* 10 + *format
++ - '0';
2341 if (*format
== 'l' && format
[1] == 'l')
2345 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
2353 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
2355 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2370 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
2379 case 'E' : /* Floating point formats */
2384 if ((width
+ 2) > sizeof(temp
))
2387 sprintf(temp
, tformat
, va_arg(ap
, double));
2389 httpWrite2(&(client
->http
), temp
, strlen(temp
));
2392 case 'B' : /* Integer formats */
2400 if ((width
+ 2) > sizeof(temp
))
2403 # ifdef HAVE_LONG_LONG
2405 sprintf(temp
, tformat
, va_arg(ap
, long long));
2407 # endif /* HAVE_LONG_LONG */
2409 sprintf(temp
, tformat
, va_arg(ap
, long));
2411 sprintf(temp
, tformat
, va_arg(ap
, int));
2413 httpWrite2(&(client
->http
), temp
, strlen(temp
));
2416 case 'p' : /* Pointer value */
2417 if ((width
+ 2) > sizeof(temp
))
2420 sprintf(temp
, tformat
, va_arg(ap
, void *));
2422 httpWrite2(&(client
->http
), temp
, strlen(temp
));
2425 case 'c' : /* Character or character array */
2428 temp
[0] = va_arg(ap
, int);
2430 html_escape(client
, temp
, 1);
2433 html_escape(client
, va_arg(ap
, char *), (size_t)width
);
2436 case 's' : /* String */
2437 if ((s
= va_arg(ap
, char *)) == NULL
)
2440 html_escape(client
, s
, strlen(s
));
2449 httpWrite2(&(client
->http
), start
, format
- start
);
2456 * 'ipp_cancel_job()' - Cancel a job.
2460 ipp_cancel_job(_ipp_client_t
*client
) /* I - Client */
2462 _ipp_job_t
*job
; /* Job information */
2469 if ((job
= find_job(client
)) == NULL
)
2473 * See if the job is already completed, canceled, or aborted; if so,
2474 * we can't cancel...
2479 case IPP_JOB_CANCELED
:
2480 respond_ipp(client
, IPP_NOT_POSSIBLE
,
2481 "Job #%d is already canceled - can\'t cancel.", job
->id
);
2484 case IPP_JOB_ABORTED
:
2485 respond_ipp(client
, IPP_NOT_POSSIBLE
,
2486 "Job #%d is already aborted - can\'t cancel.", job
->id
);
2489 case IPP_JOB_COMPLETED
:
2490 respond_ipp(client
, IPP_NOT_POSSIBLE
,
2491 "Job #%d is already completed - can\'t cancel.", job
->id
);
2499 _cupsRWLockWrite(&(client
->printer
->rwlock
));
2501 if (job
->state
== IPP_JOB_PROCESSING
||
2502 (job
->state
== IPP_JOB_HELD
&& job
->fd
>= 0))
2506 job
->state
= IPP_JOB_CANCELED
;
2507 job
->completed
= time(NULL
);
2510 _cupsRWUnlock(&(client
->printer
->rwlock
));
2512 respond_ipp(client
, IPP_OK
, NULL
);
2520 * 'ipp_create_job()' - Create a job object.
2524 ipp_create_job(_ipp_client_t
*client
) /* I - Client */
2531 * 'ipp_get_job_attributes()' - Get the attributes for a job object.
2535 ipp_get_job_attributes(
2536 _ipp_client_t
*client
) /* I - Client */
2538 _ipp_job_t
*job
; /* Job */
2539 cups_array_t
*ra
; /* requested-attributes */
2542 if ((job
= find_job(client
)) == NULL
)
2544 respond_ipp(client
, IPP_NOT_FOUND
, "Job not found.");
2548 respond_ipp(client
, IPP_OK
, NULL
);
2550 ra
= create_requested_array(client
);
2551 copy_job_attributes(client
, job
, ra
);
2552 cupsArrayDelete(ra
);
2557 * 'ipp_get_jobs()' - Get a list of job objects.
2561 ipp_get_jobs(_ipp_client_t
*client
) /* I - Client */
2563 ipp_attribute_t
*attr
; /* Current attribute */
2564 int job_comparison
; /* Job comparison */
2565 ipp_jstate_t job_state
; /* job-state value */
2566 int first_job_id
, /* First job ID */
2567 limit
, /* Maximum number of jobs to return */
2568 count
; /* Number of jobs that match */
2569 const char *username
; /* Username */
2570 _ipp_job_t
*job
; /* Current job pointer */
2571 cups_array_t
*ra
; /* Requested attributes array */
2575 * See if the "which-jobs" attribute have been specified...
2578 if ((attr
= ippFindAttribute(client
->request
, "which-jobs",
2579 IPP_TAG_KEYWORD
)) != NULL
)
2580 fprintf(stderr
, "%s Get-Jobs which-jobs=%s", client
->http
.hostname
,
2581 attr
->values
[0].string
.text
);
2583 if (!attr
|| !strcmp(attr
->values
[0].string
.text
, "not-completed"))
2585 job_comparison
= -1;
2586 job_state
= IPP_JOB_STOPPED
;
2588 else if (!strcmp(attr
->values
[0].string
.text
, "completed"))
2591 job_state
= IPP_JOB_CANCELED
;
2593 else if (!strcmp(attr
->values
[0].string
.text
, "aborted"))
2596 job_state
= IPP_JOB_ABORTED
;
2598 else if (!strcmp(attr
->values
[0].string
.text
, "all"))
2601 job_state
= IPP_JOB_PENDING
;
2603 else if (!strcmp(attr
->values
[0].string
.text
, "canceled"))
2606 job_state
= IPP_JOB_CANCELED
;
2608 else if (!strcmp(attr
->values
[0].string
.text
, "pending"))
2611 job_state
= IPP_JOB_PENDING
;
2613 else if (!strcmp(attr
->values
[0].string
.text
, "pending-held"))
2616 job_state
= IPP_JOB_HELD
;
2618 else if (!strcmp(attr
->values
[0].string
.text
, "processing"))
2621 job_state
= IPP_JOB_PROCESSING
;
2623 else if (!strcmp(attr
->values
[0].string
.text
, "processing-stopped"))
2626 job_state
= IPP_JOB_STOPPED
;
2630 respond_ipp(client
, IPP_ATTRIBUTES
,
2631 "The which-jobs value \"%s\" is not supported.",
2632 attr
->values
[0].string
.text
);
2633 ippAddString(client
->response
, IPP_TAG_UNSUPPORTED_GROUP
, IPP_TAG_KEYWORD
,
2634 "which-jobs", NULL
, attr
->values
[0].string
.text
);
2639 * See if they want to limit the number of jobs reported...
2642 if ((attr
= ippFindAttribute(client
->request
, "limit",
2643 IPP_TAG_INTEGER
)) != NULL
)
2645 limit
= attr
->values
[0].integer
;
2647 fprintf(stderr
, "%s Get-Jobs limit=%d", client
->http
.hostname
, limit
);
2652 if ((attr
= ippFindAttribute(client
->request
, "first-job-id",
2653 IPP_TAG_INTEGER
)) != NULL
)
2655 first_job_id
= attr
->values
[0].integer
;
2657 fprintf(stderr
, "%s Get-Jobs first-job-id=%d", client
->http
.hostname
,
2664 * See if we only want to see jobs for a specific user...
2669 if ((attr
= ippFindAttribute(client
->request
, "my-jobs",
2670 IPP_TAG_BOOLEAN
)) != NULL
)
2672 fprintf(stderr
, "%s Get-Jobs my-jobs=%s\n", client
->http
.hostname
,
2673 attr
->values
[0].boolean
? "true" : "false");
2675 if (attr
->values
[0].boolean
)
2677 if ((attr
= ippFindAttribute(client
->request
, "requesting-user-name",
2678 IPP_TAG_NAME
)) == NULL
)
2680 respond_ipp(client
, IPP_BAD_REQUEST
,
2681 "Need requesting-user-name with my-jobs.");
2685 username
= attr
->values
[0].string
.text
;
2687 fprintf(stderr
, "%s Get-Jobs requesting-user-name=\"%s\"\n",
2688 client
->http
.hostname
, username
);
2693 * OK, build a list of jobs for this printer...
2696 if ((ra
= create_requested_array(client
)) == NULL
&&
2697 !ippFindAttribute(client
->request
, "requested-attributes",
2701 * IPP conformance - Get-Jobs has a default requested-attributes value of
2702 * "job-id" and "job-uri".
2705 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2706 cupsArrayAdd(ra
, "job-id");
2707 cupsArrayAdd(ra
, "job-uri");
2710 respond_ipp(client
, IPP_OK
, NULL
);
2712 _cupsRWLockRead(&(client
->printer
->rwlock
));
2714 for (count
= 0, job
= (_ipp_job_t
*)cupsArrayFirst(client
->printer
->jobs
);
2715 (limit
<= 0 || count
< limit
) && job
;
2716 job
= (_ipp_job_t
*)cupsArrayNext(client
->printer
->jobs
))
2719 * Filter out jobs that don't match...
2722 if ((job_comparison
< 0 && job
->state
> job_state
) ||
2723 (job_comparison
== 0 && job
->state
!= job_state
) ||
2724 (job_comparison
> 0 && job
->state
< job_state
) ||
2725 job
->id
< first_job_id
||
2726 (username
&& job
->username
&& _cups_strcasecmp(username
, job
->username
)))
2730 ippAddSeparator(client
->response
);
2733 copy_job_attributes(client
, job
, ra
);
2736 cupsArrayDelete(ra
);
2738 _cupsRWUnlock(&(client
->printer
->rwlock
));
2743 * 'ipp_get_printer_attributes()' - Get the attributes for a printer object.
2747 ipp_get_printer_attributes(
2748 _ipp_client_t
*client
) /* I - Client */
2750 cups_array_t
*ra
; /* Requested attributes array */
2751 _ipp_printer_t
*printer
; /* Printer */
2755 * Send the attributes...
2758 ra
= create_requested_array(client
);
2759 printer
= client
->printer
;
2761 respond_ipp(client
, IPP_OK
, NULL
);
2763 _cupsRWLockRead(&(printer
->rwlock
));
2765 copy_attributes(client
->response
, printer
->attrs
, ra
, IPP_TAG_ZERO
,
2768 if (!ra
|| cupsArrayFind(ra
, "printer-state"))
2769 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2770 "printer-state", printer
->state
);
2772 if (!ra
|| cupsArrayFind(ra
, "printer-state-reasons"))
2774 if (printer
->state_reasons
== _IPP_PRINTER_NONE
)
2775 ippAddString(client
->response
, IPP_TAG_PRINTER
,
2776 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "printer-state-reasons",
2780 int num_reasons
= 0;/* Number of reasons */
2781 const char *reasons
[32]; /* Reason strings */
2783 if (printer
->state_reasons
& _IPP_PRINTER_OTHER
)
2784 reasons
[num_reasons
++] = "other";
2785 if (printer
->state_reasons
& _IPP_PRINTER_COVER_OPEN
)
2786 reasons
[num_reasons
++] = "cover-open";
2787 if (printer
->state_reasons
& _IPP_PRINTER_INPUT_TRAY_MISSING
)
2788 reasons
[num_reasons
++] = "input-tray-missing";
2789 if (printer
->state_reasons
& _IPP_PRINTER_MARKER_SUPPLY_EMPTY
)
2790 reasons
[num_reasons
++] = "marker-supply-empty-warning";
2791 if (printer
->state_reasons
& _IPP_PRINTER_MARKER_SUPPLY_LOW
)
2792 reasons
[num_reasons
++] = "marker-supply-low-report";
2793 if (printer
->state_reasons
& _IPP_PRINTER_MARKER_WASTE_ALMOST_FULL
)
2794 reasons
[num_reasons
++] = "marker-waste-almost-full-report";
2795 if (printer
->state_reasons
& _IPP_PRINTER_MARKER_WASTE_FULL
)
2796 reasons
[num_reasons
++] = "marker-waste-full-warning";
2797 if (printer
->state_reasons
& _IPP_PRINTER_MEDIA_EMPTY
)
2798 reasons
[num_reasons
++] = "media-empty-warning";
2799 if (printer
->state_reasons
& _IPP_PRINTER_MEDIA_JAM
)
2800 reasons
[num_reasons
++] = "media-jam-warning";
2801 if (printer
->state_reasons
& _IPP_PRINTER_MEDIA_LOW
)
2802 reasons
[num_reasons
++] = "media-low-report";
2803 if (printer
->state_reasons
& _IPP_PRINTER_MEDIA_NEEDED
)
2804 reasons
[num_reasons
++] = "media-needed-report";
2805 if (printer
->state_reasons
& _IPP_PRINTER_MOVING_TO_PAUSED
)
2806 reasons
[num_reasons
++] = "moving-to-paused";
2807 if (printer
->state_reasons
& _IPP_PRINTER_PAUSED
)
2808 reasons
[num_reasons
++] = "paused";
2809 if (printer
->state_reasons
& _IPP_PRINTER_SPOOL_AREA_FULL
)
2810 reasons
[num_reasons
++] = "spool-area-full";
2811 if (printer
->state_reasons
& _IPP_PRINTER_TONER_EMPTY
)
2812 reasons
[num_reasons
++] = "toner-empty-warning";
2813 if (printer
->state_reasons
& _IPP_PRINTER_TONER_LOW
)
2814 reasons
[num_reasons
++] = "toner-low-report";
2816 ippAddStrings(client
->response
, IPP_TAG_PRINTER
,
2817 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "printer-state-reasons",
2818 num_reasons
, NULL
, reasons
);
2822 if (!ra
|| cupsArrayFind(ra
, "printer-up-time"))
2823 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2824 "printer-up-time", (int)time(NULL
));
2826 if (!ra
|| cupsArrayFind(ra
, "queued-job-count"))
2827 ippAddInteger(client
->response
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2829 printer
->active_job
&&
2830 printer
->active_job
->state
< IPP_JOB_CANCELED
);
2832 _cupsRWUnlock(&(printer
->rwlock
));
2834 cupsArrayDelete(ra
);
2839 * 'ipp_print_job()' - Create a job object with an attached document.
2843 ipp_print_job(_ipp_client_t
*client
) /* I - Client */
2845 _ipp_job_t
*job
; /* New job */
2846 char filename
[1024], /* Filename buffer */
2847 buffer
[4096]; /* Copy buffer */
2848 ssize_t bytes
; /* Bytes read */
2849 cups_array_t
*ra
; /* Attributes to send in response */
2853 * Validate print job attributes...
2856 if (!valid_job_attributes(client
))
2858 httpFlush(&(client
->http
));
2863 * Do we have a file to print?
2866 if (client
->http
.state
== HTTP_POST_SEND
)
2868 respond_ipp(client
, IPP_BAD_REQUEST
, "No file in request.");
2876 if ((job
= create_job(client
)) == NULL
)
2878 respond_ipp(client
, IPP_PRINTER_BUSY
, "Currently printing another job.");
2883 * Create a file for the request data...
2886 if (!_cups_strcasecmp(job
->format
, "image/jpeg"))
2887 snprintf(filename
, sizeof(filename
), "%s/%d.jpg",
2888 client
->printer
->directory
, job
->id
);
2889 else if (!_cups_strcasecmp(job
->format
, "image/png"))
2890 snprintf(filename
, sizeof(filename
), "%s/%d.png",
2891 client
->printer
->directory
, job
->id
);
2892 else if (!_cups_strcasecmp(job
->format
, "application/pdf"))
2893 snprintf(filename
, sizeof(filename
), "%s/%d.pdf",
2894 client
->printer
->directory
, job
->id
);
2895 else if (!_cups_strcasecmp(job
->format
, "application/postscript"))
2896 snprintf(filename
, sizeof(filename
), "%s/%d.ps",
2897 client
->printer
->directory
, job
->id
);
2899 snprintf(filename
, sizeof(filename
), "%s/%d.prn",
2900 client
->printer
->directory
, job
->id
);
2902 if ((job
->fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0)
2904 job
->state
= IPP_JOB_ABORTED
;
2906 respond_ipp(client
, IPP_INTERNAL_ERROR
,
2907 "Unable to create print file: %s", strerror(errno
));
2911 while ((bytes
= httpRead2(&(client
->http
), buffer
, sizeof(buffer
))) > 0)
2913 if (write(job
->fd
, buffer
, bytes
) < bytes
)
2915 int error
= errno
; /* Write error */
2917 job
->state
= IPP_JOB_ABORTED
;
2924 respond_ipp(client
, IPP_INTERNAL_ERROR
,
2925 "Unable to write print file: %s", strerror(error
));
2933 * Got an error while reading the print data, so abort this job.
2936 job
->state
= IPP_JOB_ABORTED
;
2943 respond_ipp(client
, IPP_INTERNAL_ERROR
, "Unable to read print file.");
2949 int error
= errno
; /* Write error */
2951 job
->state
= IPP_JOB_ABORTED
;
2956 respond_ipp(client
, IPP_INTERNAL_ERROR
, "Unable to write print file: %s",
2962 job
->filename
= strdup(filename
);
2963 job
->state
= IPP_JOB_PENDING
;
2966 * Process the job...
2969 if (!_cupsThreadCreate((_cups_thread_func_t
)process_job
, job
))
2971 job
->state
= IPP_JOB_ABORTED
;
2972 respond_ipp(client
, IPP_INTERNAL_ERROR
, "Unable to process job.");
2977 * Return the job info...
2980 respond_ipp(client
, IPP_OK
, NULL
);
2982 ra
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2983 cupsArrayAdd(ra
, "job-id");
2984 cupsArrayAdd(ra
, "job-state");
2985 cupsArrayAdd(ra
, "job-state-reasons");
2986 cupsArrayAdd(ra
, "job-uri");
2988 copy_job_attributes(client
, job
, ra
);
2989 cupsArrayDelete(ra
);
2995 * 'ipp_send_document()' - Add an attached document to a job object created with
3000 ipp_send_document(_ipp_client_t
*client
)/* I - Client */
3007 * 'ipp_validate_job()' - Validate job creation attributes.
3011 ipp_validate_job(_ipp_client_t
*client
) /* I - Client */
3017 * 'process_client()' - Process client requests on a thread.
3020 static void * /* O - Exit status */
3021 process_client(_ipp_client_t
*client
) /* I - Client */
3024 * Loop until we are out of requests or timeout (30 seconds)...
3027 while (httpWait(&(client
->http
), 30000))
3028 if (!process_http(client
))
3032 * Close the conection to the client and return...
3035 delete_client(client
);
3042 * 'process_http()' - Process a HTTP request.
3045 int /* O - 1 on success, 0 on failure */
3046 process_http(_ipp_client_t
*client
) /* I - Client connection */
3048 char line
[4096], /* Line from client... */
3049 operation
[64], /* Operation code from socket */
3050 uri
[1024], /* URI */
3051 version
[64], /* HTTP version number string */
3052 *ptr
; /* Pointer into strings */
3053 int major
, minor
; /* HTTP version numbers */
3054 http_status_t status
; /* Transfer status */
3055 ipp_state_t state
; /* State of IPP transfer */
3059 * Abort if we have an error on the connection...
3062 if (client
->http
.error
)
3066 * Clear state variables...
3069 httpClearFields(&(client
->http
));
3070 ippDelete(client
->request
);
3071 ippDelete(client
->response
);
3073 client
->http
.activity
= time(NULL
);
3074 client
->http
.version
= HTTP_1_1
;
3075 client
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
3076 client
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
3077 client
->http
.data_remaining
= 0;
3078 client
->request
= NULL
;
3079 client
->response
= NULL
;
3080 client
->operation
= HTTP_WAITING
;
3083 * Read a request from the connection...
3086 while ((ptr
= httpGets(line
, sizeof(line
) - 1, &(client
->http
))) != NULL
)
3094 * Parse the request line...
3097 fprintf(stderr
, "%s %s\n", client
->http
.hostname
, line
);
3099 switch (sscanf(line
, "%63s%1023s%63s", operation
, uri
, version
))
3102 fprintf(stderr
, "%s Bad request line.\n", client
->http
.hostname
);
3103 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3107 client
->http
.version
= HTTP_0_9
;
3111 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
3113 fprintf(stderr
, "%s Bad HTTP version.\n", client
->http
.hostname
);
3114 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3120 client
->http
.version
= (http_version_t
)(major
* 100 + minor
);
3121 if (client
->http
.version
== HTTP_1_1
)
3122 client
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
3124 client
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
3128 respond_http(client
, HTTP_NOT_SUPPORTED
, NULL
, 0);
3135 * Handle full URLs in the request line...
3138 if (!strncmp(client
->uri
, "http:", 5) || !strncmp(client
->uri
, "ipp:", 4))
3140 char scheme
[32], /* Method/scheme */
3141 userpass
[128], /* Username:password */
3142 hostname
[HTTP_MAX_HOST
];/* Hostname */
3143 int port
; /* Port number */
3146 * Separate the URI into its components...
3149 if (httpSeparateURI(HTTP_URI_CODING_MOST
, uri
, scheme
, sizeof(scheme
),
3150 userpass
, sizeof(userpass
),
3151 hostname
, sizeof(hostname
), &port
,
3152 client
->uri
, sizeof(client
->uri
)) < HTTP_URI_OK
)
3154 fprintf(stderr
, "%s Bad URI \"%s\".\n", client
->http
.hostname
, uri
);
3155 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3165 if (!_httpDecodeURI(client
->uri
, uri
, sizeof(client
->uri
)))
3167 fprintf(stderr
, "%s Bad URI \"%s\".\n", client
->http
.hostname
, uri
);
3168 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3174 * Process the request...
3177 if (!strcmp(operation
, "GET"))
3178 client
->http
.state
= HTTP_GET
;
3179 else if (!strcmp(operation
, "POST"))
3180 client
->http
.state
= HTTP_POST
;
3181 else if (!strcmp(operation
, "OPTIONS"))
3182 client
->http
.state
= HTTP_OPTIONS
;
3183 else if (!strcmp(operation
, "HEAD"))
3184 client
->http
.state
= HTTP_HEAD
;
3187 fprintf(stderr
, "%s Bad operation \"%s\".\n", client
->http
.hostname
,
3189 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3193 client
->start
= time(NULL
);
3194 client
->operation
= client
->http
.state
;
3195 client
->http
.status
= HTTP_OK
;
3198 * Parse incoming parameters until the status changes...
3201 while ((status
= httpUpdate(&(client
->http
))) == HTTP_CONTINUE
);
3203 if (status
!= HTTP_OK
)
3205 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3209 if (!client
->http
.fields
[HTTP_FIELD_HOST
][0] &&
3210 client
->http
.version
>= HTTP_1_1
)
3213 * HTTP/1.1 and higher require the "Host:" field...
3216 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3221 * Handle HTTP Upgrade...
3224 if (!_cups_strcasecmp(client
->http
.fields
[HTTP_FIELD_CONNECTION
], "Upgrade"))
3226 if (!respond_http(client
, HTTP_NOT_IMPLEMENTED
, NULL
, 0))
3231 * Handle HTTP Expect...
3234 if (client
->http
.expect
&&
3235 (client
->operation
== HTTP_POST
|| client
->operation
== HTTP_PUT
))
3237 if (client
->http
.expect
== HTTP_CONTINUE
)
3240 * Send 100-continue header...
3243 if (!respond_http(client
, HTTP_CONTINUE
, NULL
, 0))
3249 * Send 417-expectation-failed header...
3252 if (!respond_http(client
, HTTP_EXPECTATION_FAILED
, NULL
, 0))
3255 httpPrintf(&(client
->http
), "Content-Length: 0\r\n");
3256 httpPrintf(&(client
->http
), "\r\n");
3257 httpFlushWrite(&(client
->http
));
3258 client
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
3263 * Handle new transfers...
3266 switch (client
->operation
)
3270 * Do HEAD/OPTIONS command...
3273 return (respond_http(client
, HTTP_OK
, NULL
, 0));
3276 if (!strcmp(client
->uri
, "/icon.png"))
3277 return (respond_http(client
, HTTP_OK
, "image/png", 0));
3278 else if (!strcmp(client
->uri
, "/"))
3279 return (respond_http(client
, HTTP_OK
, "text/html", 0));
3281 return (respond_http(client
, HTTP_NOT_FOUND
, NULL
, 0));
3285 if (!strcmp(client
->uri
, "/icon.png"))
3288 * Send PNG icon file.
3291 int fd
; /* Icon file */
3292 struct stat fileinfo
; /* Icon file information */
3293 char buffer
[4096]; /* Copy buffer */
3294 ssize_t bytes
; /* Bytes */
3296 if (!stat(client
->printer
->icon
, &fileinfo
) &&
3297 (fd
= open(client
->printer
->icon
, O_RDONLY
)) >= 0)
3299 if (!respond_http(client
, HTTP_OK
, "image/png", fileinfo
.st_size
))
3305 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
3306 httpWrite2(&(client
->http
), buffer
, bytes
);
3308 httpFlushWrite(&(client
->http
));
3313 return (respond_http(client
, HTTP_NOT_FOUND
, NULL
, 0));
3315 else if (!strcmp(client
->uri
, "/"))
3318 * Show web status page...
3321 if (!respond_http(client
, HTTP_OK
, "text/html", 0))
3325 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
3326 "\"http://www.w3.org/TR/html4/strict.dtd\">\n"
3329 "<title>%s</title>\n"
3330 "<link rel=\"SHORTCUT ICON\" href=\"/icon.png\" "
3331 "type=\"image/png\">\n"
3336 "<p>%s, %d job(s).</p>\n"
3339 client
->printer
->name
, client
->printer
->name
,
3340 client
->printer
->state
== IPP_PRINTER_IDLE
? "Idle" :
3341 client
->printer
->state
== IPP_PRINTER_PROCESSING
?
3342 "Printing" : "Stopped",
3343 cupsArrayCount(client
->printer
->jobs
));
3344 httpWrite2(&(client
->http
), "", 0);
3349 return (respond_http(client
, HTTP_NOT_FOUND
, NULL
, 0));
3353 if (client
->http
.data_remaining
< 0 ||
3354 (!client
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
3355 client
->http
.data_encoding
== HTTP_ENCODE_LENGTH
))
3358 * Negative content lengths are invalid...
3361 return (respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0));
3364 if (strcmp(client
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
],
3368 * Not an IPP request...
3371 return (respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0));
3375 * Read the IPP request...
3378 client
->request
= ippNew();
3380 while ((state
= ippRead(&(client
->http
), client
->request
)) != IPP_DATA
)
3381 if (state
== IPP_ERROR
)
3383 fprintf(stderr
, "%s IPP read error (%s).\n", client
->http
.hostname
,
3384 ippOpString(client
->request
->request
.op
.operation_id
));
3385 respond_http(client
, HTTP_BAD_REQUEST
, NULL
, 0);
3390 * Now that we have the IPP request, process the request...
3393 return (process_ipp(client
));
3396 break; /* Anti-compiler-warning-code */
3404 * 'process_ipp()' - Process an IPP request.
3407 static int /* O - 1 on success, 0 on error */
3408 process_ipp(_ipp_client_t
*client
) /* I - Client */
3410 ipp_tag_t group
; /* Current group tag */
3411 ipp_attribute_t
*attr
; /* Current attribute */
3412 ipp_attribute_t
*charset
; /* Character set attribute */
3413 ipp_attribute_t
*language
; /* Language attribute */
3414 ipp_attribute_t
*uri
; /* Printer URI attribute */
3417 debug_attributes("Request", client
->request
);
3420 * First build an empty response message for this request...
3423 client
->operation_id
= client
->request
->request
.op
.operation_id
;
3424 client
->response
= ippNew();
3426 client
->response
->request
.status
.version
[0] =
3427 client
->request
->request
.op
.version
[0];
3428 client
->response
->request
.status
.version
[1] =
3429 client
->request
->request
.op
.version
[1];
3430 client
->response
->request
.status
.request_id
=
3431 client
->request
->request
.op
.request_id
;
3434 * Then validate the request header and required attributes...
3437 if (client
->request
->request
.any
.version
[0] < 1 ||
3438 client
->request
->request
.any
.version
[0] > 2)
3441 * Return an error, since we only support IPP 1.x and 2.x.
3444 respond_ipp(client
, IPP_VERSION_NOT_SUPPORTED
,
3445 "Bad request version number %d.%d.",
3446 client
->request
->request
.any
.version
[0],
3447 client
->request
->request
.any
.version
[1]);
3449 else if (client
->request
->request
.any
.request_id
<= 0)
3450 respond_ipp(client
, IPP_BAD_REQUEST
, "Bad request-id %d.",
3451 client
->request
->request
.any
.request_id
);
3452 else if (!client
->request
->attrs
)
3453 respond_ipp(client
, IPP_BAD_REQUEST
, "No attributes in request.");
3457 * Make sure that the attributes are provided in the correct order and
3458 * don't repeat groups...
3461 for (attr
= client
->request
->attrs
, group
= attr
->group_tag
;
3464 if (attr
->group_tag
< group
&& attr
->group_tag
!= IPP_TAG_ZERO
)
3467 * Out of order; return an error...
3470 respond_ipp(client
, IPP_BAD_REQUEST
,
3471 "Attribute groups are out of order (%x < %x).",
3472 attr
->group_tag
, group
);
3476 group
= attr
->group_tag
;
3481 * Then make sure that the first three attributes are:
3483 * attributes-charset
3484 * attributes-natural-language
3485 * printer-uri/job-uri
3488 attr
= client
->request
->attrs
;
3489 if (attr
&& attr
->name
&&
3490 !strcmp(attr
->name
, "attributes-charset") &&
3491 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_CHARSET
)
3499 if (attr
&& attr
->name
&&
3500 !strcmp(attr
->name
, "attributes-natural-language") &&
3501 (attr
->value_tag
& IPP_TAG_MASK
) == IPP_TAG_LANGUAGE
)
3506 if ((attr
= ippFindAttribute(client
->request
, "printer-uri",
3507 IPP_TAG_URI
)) != NULL
)
3509 else if ((attr
= ippFindAttribute(client
->request
, "job-uri",
3510 IPP_TAG_URI
)) != NULL
)
3515 ippAddString(client
->response
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
3516 "attributes-charset", NULL
,
3517 charset
? charset
->values
[0].string
.text
: "utf-8");
3519 ippAddString(client
->response
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
3520 "attributes-natural-language", NULL
,
3521 language
? language
->values
[0].string
.text
: "en");
3524 _cups_strcasecmp(charset
->values
[0].string
.text
, "us-ascii") &&
3525 _cups_strcasecmp(charset
->values
[0].string
.text
, "utf-8"))
3528 * Bad character set...
3531 respond_ipp(client
, IPP_BAD_REQUEST
,
3532 "Unsupported character set \"%s\".",
3533 charset
->values
[0].string
.text
);
3535 else if (!charset
|| !language
|| !uri
)
3538 * Return an error, since attributes-charset,
3539 * attributes-natural-language, and printer-uri/job-uri are required
3540 * for all operations.
3543 respond_ipp(client
, IPP_BAD_REQUEST
, "Missing required attributes.");
3545 else if (strcmp(uri
->values
[0].string
.text
, client
->printer
->uri
) &&
3546 strncmp(uri
->values
[0].string
.text
, client
->printer
->uri
,
3547 client
->printer
->urilen
))
3549 respond_ipp(client
, IPP_NOT_FOUND
, "%s %s not found.", uri
->name
,
3550 uri
->values
[0].string
.text
);
3555 * Try processing the operation...
3558 if (client
->http
.expect
== HTTP_CONTINUE
)
3561 * Send 100-continue header...
3564 if (!respond_http(client
, HTTP_CONTINUE
, NULL
, 0))
3568 switch (client
->request
->request
.op
.operation_id
)
3570 case IPP_PRINT_JOB
:
3571 ipp_print_job(client
);
3574 case IPP_VALIDATE_JOB
:
3575 ipp_validate_job(client
);
3578 case IPP_CANCEL_JOB
:
3579 ipp_cancel_job(client
);
3582 case IPP_GET_JOB_ATTRIBUTES
:
3583 ipp_get_job_attributes(client
);
3587 ipp_get_jobs(client
);
3590 case IPP_GET_PRINTER_ATTRIBUTES
:
3591 ipp_get_printer_attributes(client
);
3595 respond_ipp(client
, IPP_OPERATION_NOT_SUPPORTED
,
3596 "Operation not supported.");
3604 * Send the HTTP header and return...
3607 if (client
->http
.state
!= HTTP_POST_SEND
)
3608 httpFlush(&(client
->http
)); /* Flush trailing (junk) data */
3610 return (respond_http(client
, HTTP_OK
, "application/ipp",
3611 ippLength(client
->response
)));
3616 * 'process_job()' - Process a print job.
3619 static void * /* O - Thread exit status */
3620 process_job(_ipp_job_t
*job
) /* I - Job */
3622 job
->state
= IPP_JOB_PROCESSING
;
3623 job
->printer
->state
= IPP_PRINTER_PROCESSING
;
3628 job
->state
= IPP_JOB_CANCELED
;
3630 job
->state
= IPP_JOB_COMPLETED
;
3632 job
->completed
= time(NULL
);
3633 job
->printer
->state
= IPP_PRINTER_IDLE
;
3634 job
->printer
->active_job
= NULL
;
3642 * 'register_printer()' - Register a printer object via Bonjour.
3645 static int /* O - 1 on success, 0 on error */
3647 _ipp_printer_t
*printer
, /* I - Printer */
3648 const char *location
, /* I - Location */
3649 const char *make
, /* I - Manufacturer */
3650 const char *model
, /* I - Model name */
3651 const char *formats
, /* I - Supported formats */
3652 const char *adminurl
, /* I - Web interface URL */
3653 int color
, /* I - 1 = color, 0 = monochrome */
3654 int duplex
, /* I - 1 = duplex, 0 = simplex */
3655 const char *regtype
) /* I - Service type */
3657 DNSServiceErrorType error
; /* Error from Bonjour */
3658 char make_model
[256],/* Make and model together */
3659 product
[256]; /* Product string */
3663 * Build the TXT record for IPP...
3666 snprintf(make_model
, sizeof(make_model
), "%s %s", make
, model
);
3667 snprintf(product
, sizeof(product
), "(%s)", model
);
3669 TXTRecordCreate(&(printer
->ipp_txt
), 1024, NULL
);
3670 TXTRecordSetValue(&(printer
->ipp_txt
), "txtvers", 1, "1");
3671 TXTRecordSetValue(&(printer
->ipp_txt
), "qtotal", 1, "1");
3672 TXTRecordSetValue(&(printer
->ipp_txt
), "rp", 3, "ipp");
3673 TXTRecordSetValue(&(printer
->ipp_txt
), "ty", (uint8_t)strlen(make_model
),
3675 TXTRecordSetValue(&(printer
->ipp_txt
), "adminurl", (uint8_t)strlen(adminurl
),
3677 TXTRecordSetValue(&(printer
->ipp_txt
), "note", (uint8_t)strlen(location
),
3679 TXTRecordSetValue(&(printer
->ipp_txt
), "priority", 1, "0");
3680 TXTRecordSetValue(&(printer
->ipp_txt
), "product", (uint8_t)strlen(product
),
3682 TXTRecordSetValue(&(printer
->ipp_txt
), "pdl", (uint8_t)strlen(formats
),
3684 TXTRecordSetValue(&(printer
->ipp_txt
), "Color", 1, color
? "T" : "F");
3685 TXTRecordSetValue(&(printer
->ipp_txt
), "Duplex", 1, duplex
? "T" : "F");
3686 TXTRecordSetValue(&(printer
->ipp_txt
), "usb_MFG", (uint8_t)strlen(make
),
3688 TXTRecordSetValue(&(printer
->ipp_txt
), "usb_MDL", (uint8_t)strlen(model
),
3690 TXTRecordSetValue(&(printer
->ipp_txt
), "air", 4, "none");
3693 * Create a shared service reference for Bonjour...
3696 if ((error
= DNSServiceCreateConnection(&(printer
->common_ref
)))
3697 != kDNSServiceErr_NoError
)
3699 fprintf(stderr
, "Unable to create mDNSResponder connection: %d\n", error
);
3704 * Register the _printer._tcp (LPD) service type with a port number of 0 to
3705 * defend our service name but not actually support LPD...
3708 printer
->printer_ref
= printer
->common_ref
;
3710 if ((error
= DNSServiceRegister(&(printer
->printer_ref
),
3711 kDNSServiceFlagsShareConnection
,
3712 0 /* interfaceIndex */, printer
->dnssd_name
,
3713 "_printer._tcp", NULL
/* domain */,
3714 NULL
/* host */, 0 /* port */, 0 /* txtLen */,
3715 NULL
/* txtRecord */,
3716 (DNSServiceRegisterReply
)dnssd_callback
,
3717 printer
)) != kDNSServiceErr_NoError
)
3719 fprintf(stderr
, "Unable to register \"%s._printer._tcp\": %d\n",
3720 printer
->dnssd_name
, error
);
3725 * Then register the _ipp._tcp (IPP) service type with the real port number to
3726 * advertise our IPP printer...
3729 printer
->ipp_ref
= printer
->common_ref
;
3731 if ((error
= DNSServiceRegister(&(printer
->ipp_ref
),
3732 kDNSServiceFlagsShareConnection
,
3733 0 /* interfaceIndex */, printer
->dnssd_name
,
3734 regtype
, NULL
/* domain */,
3735 NULL
/* host */, htons(printer
->port
),
3736 TXTRecordGetLength(&(printer
->ipp_txt
)),
3737 TXTRecordGetBytesPtr(&(printer
->ipp_txt
)),
3738 (DNSServiceRegisterReply
)dnssd_callback
,
3739 printer
)) != kDNSServiceErr_NoError
)
3741 fprintf(stderr
, "Unable to register \"%s.%s\": %d\n",
3742 printer
->dnssd_name
, regtype
, error
);
3747 * Similarly, register the _http._tcp,_printer (HTTP) service type with the
3748 * real port number to advertise our IPP printer...
3751 printer
->http_ref
= printer
->common_ref
;
3753 if ((error
= DNSServiceRegister(&(printer
->http_ref
),
3754 kDNSServiceFlagsShareConnection
,
3755 0 /* interfaceIndex */, printer
->dnssd_name
,
3756 "_http._tcp,_printer", NULL
/* domain */,
3757 NULL
/* host */, htons(printer
->port
),
3758 0 /* txtLen */, NULL
, /* txtRecord */
3759 (DNSServiceRegisterReply
)dnssd_callback
,
3760 printer
)) != kDNSServiceErr_NoError
)
3762 fprintf(stderr
, "Unable to register \"%s.%s\": %d\n",
3763 printer
->dnssd_name
, regtype
, error
);
3769 #endif /* HAVE_DNSSD */
3773 * 'respond_http()' - Send a HTTP response.
3776 int /* O - 1 on success, 0 on failure */
3777 respond_http(_ipp_client_t
*client
, /* I - Client */
3778 http_status_t code
, /* I - HTTP status of response */
3779 const char *type
, /* I - MIME type of response */
3780 size_t length
) /* I - Length of response */
3782 char message
[1024]; /* Text message */
3785 fprintf(stderr
, "%s %s\n", client
->http
.hostname
, httpStatus(code
));
3787 if (code
== HTTP_CONTINUE
)
3790 * 100-continue doesn't send any headers...
3793 return (httpPrintf(&(client
->http
), "HTTP/%d.%d 100 Continue\r\n\r\n",
3794 client
->http
.version
/ 100,
3795 client
->http
.version
% 100) > 0);
3799 * Format an error message...
3802 if (!type
&& !length
&& code
!= HTTP_OK
)
3804 snprintf(message
, sizeof(message
), "%d - %s\n", code
, httpStatus(code
));
3806 type
= "text/plain";
3807 length
= strlen(message
);
3813 * Send the HTTP status header...
3816 httpFlushWrite(&(client
->http
));
3818 client
->http
.data_encoding
= HTTP_ENCODE_FIELDS
;
3820 if (httpPrintf(&(client
->http
), "HTTP/%d.%d %d %s\r\n", client
->http
.version
/ 100,
3821 client
->http
.version
% 100, code
, httpStatus(code
)) < 0)
3825 * Follow the header with the response fields...
3828 if (httpPrintf(&(client
->http
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
3831 if (client
->http
.keep_alive
&& client
->http
.version
>= HTTP_1_0
)
3833 if (httpPrintf(&(client
->http
),
3834 "Connection: Keep-Alive\r\n"
3835 "Keep-Alive: timeout=10\r\n") < 0)
3839 if (code
== HTTP_METHOD_NOT_ALLOWED
|| client
->operation
== HTTP_OPTIONS
)
3841 if (httpPrintf(&(client
->http
), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0)
3847 if (!strcmp(type
, "text/html"))
3849 if (httpPrintf(&(client
->http
),
3850 "Content-Type: text/html; charset=utf-8\r\n") < 0)
3853 else if (httpPrintf(&(client
->http
), "Content-Type: %s\r\n", type
) < 0)
3857 if (length
== 0 && !message
[0])
3859 if (httpPrintf(&(client
->http
), "Transfer-Encoding: chunked\r\n\r\n") < 0)
3862 else if (httpPrintf(&(client
->http
), "Content-Length: " CUPS_LLFMT
"\r\n\r\n",
3863 CUPS_LLCAST length
) < 0)
3866 if (httpFlushWrite(&(client
->http
)) < 0)
3870 * Send the response data...
3876 * Send a plain text message.
3879 if (httpPrintf(&(client
->http
), "%s", message
) < 0)
3882 else if (client
->response
)
3885 * Send an IPP response...
3888 debug_attributes("Response", client
->response
);
3890 client
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
3891 client
->http
.data_remaining
= (off_t
)ippLength(client
->response
);
3892 client
->response
->state
= IPP_IDLE
;
3894 if (ippWrite(&(client
->http
), client
->response
) != IPP_DATA
)
3898 client
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
3901 * Flush the data and return...
3904 return (httpFlushWrite(&(client
->http
)) >= 0);
3909 * 'respond_ipp()' - Send an IPP response.
3913 respond_ipp(_ipp_client_t
*client
, /* I - Client */
3914 ipp_status_t status
, /* I - status-code */
3915 const char *message
, /* I - printf-style status-message */
3916 ...) /* I - Additional args as needed */
3918 va_list ap
; /* Pointer to additional args */
3919 char formatted
[1024]; /* Formatted errror message */
3922 client
->response
->request
.status
.status_code
= status
;
3924 if (!client
->response
->attrs
)
3926 ippAddString(client
->response
, IPP_TAG_OPERATION
,
3927 IPP_TAG_CHARSET
| IPP_TAG_COPY
, "attributes-charset", NULL
,
3929 ippAddString(client
->response
, IPP_TAG_OPERATION
,
3930 IPP_TAG_LANGUAGE
| IPP_TAG_COPY
, "attributes-natural-language",
3936 va_start(ap
, message
);
3937 vsnprintf(formatted
, sizeof(formatted
), message
, ap
);
3940 ippAddString(client
->response
, IPP_TAG_OPERATION
, IPP_TAG_TEXT
,
3941 "status-message", NULL
, formatted
);
3944 formatted
[0] = '\0';
3946 fprintf(stderr
, "%s %s %s (%s)\n", client
->http
.hostname
,
3947 ippOpString(client
->operation_id
), ippErrorString(status
), formatted
);
3952 * 'run_printer()' - Run the printer service.
3956 run_printer(_ipp_printer_t
*printer
) /* I - Printer */
3958 int num_fds
; /* Number of file descriptors */
3959 struct pollfd polldata
[3]; /* poll() data */
3960 int timeout
; /* Timeout for poll() */
3961 _ipp_client_t
*client
; /* New client */
3965 * Setup poll() data for the Bonjour service socket and IPv4/6 listeners...
3968 polldata
[0].fd
= printer
->ipv4
;
3969 polldata
[0].events
= POLLIN
;
3971 polldata
[1].fd
= printer
->ipv6
;
3972 polldata
[1].events
= POLLIN
;
3977 polldata
[num_fds
].fd
= DNSServiceRefSockFD(printer
->common_ref
);
3978 polldata
[num_fds
++].events
= POLLIN
;
3979 #endif /* HAVE_DNSSD */
3982 * Loop until we are killed or have a hard error...
3987 if (cupsArrayCount(printer
->jobs
))
3992 if (poll(polldata
, num_fds
, timeout
) < 0 && errno
!= EINTR
)
3994 perror("poll() failed");
3998 if (polldata
[0].revents
& POLLIN
)
4000 if ((client
= create_client(printer
, printer
->ipv4
)) != NULL
)
4002 if (!_cupsThreadCreate((_cups_thread_func_t
)process_client
, client
))
4004 perror("Unable to create client thread");
4005 delete_client(client
);
4010 if (polldata
[1].revents
& POLLIN
)
4012 if ((client
= create_client(printer
, printer
->ipv6
)) != NULL
)
4014 if (!_cupsThreadCreate((_cups_thread_func_t
)process_client
, client
))
4016 perror("Unable to create client thread");
4017 delete_client(client
);
4023 if (polldata
[2].revents
& POLLIN
)
4024 DNSServiceProcessResult(printer
->common_ref
);
4025 #endif /* HAVE_DNSSD */
4028 * Clean out old jobs...
4031 clean_jobs(printer
);
4037 * 'usage()' - Show program usage.
4041 usage(int status
) /* O - Exit status */
4045 puts(CUPS_SVERSION
" - Copyright 2010 by Apple Inc. All rights reserved.");
4049 puts("Usage: ippserver [options] \"name\"");
4052 puts("-2 Supports 2-sided printing (default=1-sided)");
4053 puts("-M manufacturer Manufacturer name (default=Test)");
4054 printf("-d spool-directory Spool directory "
4055 "(default=/tmp/ippserver.%d)\n", (int)getpid());
4056 puts("-f type/subtype[,...] List of supported types "
4057 "(default=application/pdf,image/jpeg)");
4058 puts("-h Show program help");
4059 puts("-i iconfile.png PNG icon file (default=printer.png)");
4060 puts("-l location Location of printer (default=empty string)");
4061 puts("-m model Model name (default=Printer)");
4062 puts("-n hostname Hostname for printer");
4063 puts("-p port Port number (default=auto)");
4064 puts("-r regtype Bonjour service type (default=_ipp._tcp)");
4065 puts("-s speed[,color-speed] Speed in pages per minute (default=10,0)");
4066 puts("-v[vvv] Be (very) verbose");
4073 * 'valid_job_attributes()' - Determine whether the job attributes are valid.
4075 * When one or more job attributes are invalid, this function adds a suitable
4076 * response and attributes to the unsupported group.
4079 static int /* O - 1 if valid, 0 if not */
4080 valid_job_attributes(
4081 _ipp_client_t
*client
) /* I - Client */
4083 int i
; /* Looping var */
4084 ipp_attribute_t
*attr
, /* Current attribute */
4085 *supported
; /* document-format-supported */
4086 const char *format
= NULL
; /* document-format value */
4087 int valid
= 1; /* Valid attributes? */
4091 * Check operation attributes...
4094 #define respond_unsupported(client, attr) \
4097 respond_ipp(client, IPP_ATTRIBUTES, "Unsupported %s %s%s value.", \
4098 attr->name, attr->num_values > 1 ? "1setOf " : "", \
4099 ippTagString(attr->value_tag)); \
4102 copy_attribute(client->response, attr, IPP_TAG_UNSUPPORTED_GROUP, 0)
4104 if ((attr
= ippFindAttribute(client
->request
, "compression",
4105 IPP_TAG_ZERO
)) != NULL
)
4108 * If compression is specified, only accept "none"...
4111 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_KEYWORD
||
4112 strcmp(attr
->values
[0].string
.text
, "none"))
4114 respond_unsupported(client
, attr
);
4117 fprintf(stderr
, "%s Print-Job compression=\"%s\"\n",
4118 client
->http
.hostname
, attr
->values
[0].string
.text
);
4122 * Is it a format we support?
4125 if ((attr
= ippFindAttribute(client
->request
, "document-format",
4126 IPP_TAG_ZERO
)) != NULL
)
4128 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_MIMETYPE
)
4130 respond_unsupported(client
, attr
);
4134 format
= attr
->values
[0].string
.text
;
4136 fprintf(stderr
, "%s %s document-format=\"%s\"\n",
4137 client
->http
.hostname
,
4138 ippOpString(client
->request
->request
.op
.operation_id
), format
);
4142 format
= "application/octet-stream";
4144 if (!strcmp(format
, "application/octet-stream") &&
4145 client
->request
->request
.op
.operation_id
!= IPP_VALIDATE_JOB
)
4148 * Auto-type the file using the first 4 bytes of the file...
4151 unsigned char header
[4]; /* First 4 bytes of file */
4153 memset(header
, 0, sizeof(header
));
4154 _httpPeek(&(client
->http
), (char *)header
, sizeof(header
));
4156 if (!memcmp(header
, "%PDF", 4))
4157 format
= "application/pdf";
4158 else if (!memcmp(header
, "%!", 2))
4159 format
= "application/postscript";
4160 else if (!memcmp(header
, "\377\330\377", 3) &&
4161 header
[3] >= 0xe0 && header
[3] <= 0xef)
4162 format
= "image/jpeg";
4163 else if (!memcmp(header
, "\211PNG", 4))
4164 format
= "image/png";
4167 fprintf(stderr
, "%s %s Auto-typed document-format=\"%s\"\n",
4168 client
->http
.hostname
,
4169 ippOpString(client
->request
->request
.op
.operation_id
), format
);
4172 ippAddString(client
->request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
,
4173 "document-format", NULL
, format
);
4176 _cupsStrFree(attr
->values
[0].string
.text
);
4177 attr
->values
[0].string
.text
= _cupsStrAlloc(format
);
4181 if ((supported
= ippFindAttribute(client
->printer
->attrs
,
4182 "document-format-supported",
4183 IPP_TAG_MIMETYPE
)) != NULL
)
4185 for (i
= 0; i
< supported
->num_values
; i
++)
4186 if (!_cups_strcasecmp(format
, supported
->values
[i
].string
.text
))
4189 if (i
>= supported
->num_values
)
4191 respond_unsupported(client
, attr
);
4196 * Check the various job template attributes...
4199 if ((attr
= ippFindAttribute(client
->request
, "copies",
4200 IPP_TAG_ZERO
)) != NULL
)
4202 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_INTEGER
||
4203 attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> 999)
4205 respond_unsupported(client
, attr
);
4209 if ((attr
= ippFindAttribute(client
->request
, "ipp-attribute-fidelity",
4210 IPP_TAG_ZERO
)) != NULL
)
4212 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_BOOLEAN
)
4214 respond_unsupported(client
, attr
);
4218 if ((attr
= ippFindAttribute(client
->request
, "job-hold-until",
4219 IPP_TAG_ZERO
)) != NULL
)
4221 if (attr
->num_values
!= 1 ||
4222 (attr
->value_tag
!= IPP_TAG_NAME
&&
4223 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
4224 attr
->value_tag
!= IPP_TAG_KEYWORD
) ||
4225 strcmp(attr
->values
[0].string
.text
, "no-hold"))
4227 respond_unsupported(client
, attr
);
4231 if ((attr
= ippFindAttribute(client
->request
, "job-name",
4232 IPP_TAG_ZERO
)) != NULL
)
4234 if (attr
->num_values
!= 1 ||
4235 (attr
->value_tag
!= IPP_TAG_NAME
&&
4236 attr
->value_tag
!= IPP_TAG_NAMELANG
))
4238 respond_unsupported(client
, attr
);
4242 if ((attr
= ippFindAttribute(client
->request
, "job-priority",
4243 IPP_TAG_ZERO
)) != NULL
)
4245 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_INTEGER
||
4246 attr
->values
[0].integer
< 1 || attr
->values
[0].integer
> 100)
4248 respond_unsupported(client
, attr
);
4252 if ((attr
= ippFindAttribute(client
->request
, "job-sheets",
4253 IPP_TAG_ZERO
)) != NULL
)
4255 if (attr
->num_values
!= 1 ||
4256 (attr
->value_tag
!= IPP_TAG_NAME
&&
4257 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
4258 attr
->value_tag
!= IPP_TAG_KEYWORD
) ||
4259 strcmp(attr
->values
[0].string
.text
, "none"))
4261 respond_unsupported(client
, attr
);
4265 if ((attr
= ippFindAttribute(client
->request
, "media",
4266 IPP_TAG_ZERO
)) != NULL
)
4268 if (attr
->num_values
!= 1 ||
4269 (attr
->value_tag
!= IPP_TAG_NAME
&&
4270 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
4271 attr
->value_tag
!= IPP_TAG_KEYWORD
))
4273 respond_unsupported(client
, attr
);
4278 i
< (int)(sizeof(media_supported
) / sizeof(media_supported
[0]));
4280 if (!strcmp(attr
->values
[0].string
.text
, media_supported
[i
]))
4283 if (i
>= (int)(sizeof(media_supported
) / sizeof(media_supported
[0])))
4285 respond_unsupported(client
, attr
);
4290 if ((attr
= ippFindAttribute(client
->request
, "media-col",
4291 IPP_TAG_ZERO
)) != NULL
)
4293 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_BEGIN_COLLECTION
)
4295 respond_unsupported(client
, attr
);
4297 /* TODO: check for valid media-col */
4300 if ((attr
= ippFindAttribute(client
->request
, "multiple-document-handling",
4301 IPP_TAG_ZERO
)) != NULL
)
4303 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_KEYWORD
||
4304 (strcmp(attr
->values
[0].string
.text
,
4305 "separate-documents-uncollated-copies") &&
4306 strcmp(attr
->values
[0].string
.text
,
4307 "separate-documents-collated-copies")))
4309 respond_unsupported(client
, attr
);
4313 if ((attr
= ippFindAttribute(client
->request
, "orientation-requested",
4314 IPP_TAG_ZERO
)) != NULL
)
4316 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_ENUM
||
4317 attr
->values
[0].integer
< IPP_PORTRAIT
||
4318 attr
->values
[0].integer
> IPP_REVERSE_PORTRAIT
)
4320 respond_unsupported(client
, attr
);
4324 if ((attr
= ippFindAttribute(client
->request
, "page-ranges",
4325 IPP_TAG_ZERO
)) != NULL
)
4327 respond_unsupported(client
, attr
);
4330 if ((attr
= ippFindAttribute(client
->request
, "print-quality",
4331 IPP_TAG_ZERO
)) != NULL
)
4333 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_ENUM
||
4334 attr
->values
[0].integer
< IPP_QUALITY_DRAFT
||
4335 attr
->values
[0].integer
> IPP_QUALITY_HIGH
)
4337 respond_unsupported(client
, attr
);
4341 if ((attr
= ippFindAttribute(client
->request
, "printer-resolution",
4342 IPP_TAG_ZERO
)) != NULL
)
4344 respond_unsupported(client
, attr
);
4347 if ((attr
= ippFindAttribute(client
->request
, "sides",
4348 IPP_TAG_ZERO
)) != NULL
)
4350 if (attr
->num_values
!= 1 || attr
->value_tag
!= IPP_TAG_KEYWORD
)
4352 respond_unsupported(client
, attr
);
4355 if ((supported
= ippFindAttribute(client
->printer
->attrs
, "sides",
4356 IPP_TAG_KEYWORD
)) != NULL
)
4358 for (i
= 0; i
< supported
->num_values
; i
++)
4359 if (!strcmp(attr
->values
[0].string
.text
,
4360 supported
->values
[i
].string
.text
))
4363 if (i
>= supported
->num_values
)
4365 respond_unsupported(client
, attr
);
4370 respond_unsupported(client
, attr
);