1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
233d09a4feSsonam gupta - Sun Microsystems - Bangalore India  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: print-job.c 146 2006-03-24 00:26:54Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #include <stdio.h>
31355b4669Sjacobs #include <stdlib.h>
323d09a4feSsonam gupta - Sun Microsystems - Bangalore India #include <netdb.h>
333d09a4feSsonam gupta - Sun Microsystems - Bangalore India #include <sys/types.h>
343d09a4feSsonam gupta - Sun Microsystems - Bangalore India #include <sys/socket.h>
353d09a4feSsonam gupta - Sun Microsystems - Bangalore India #include <netinet/in.h>
363d09a4feSsonam gupta - Sun Microsystems - Bangalore India #include <arpa/inet.h>
37355b4669Sjacobs #include <papi.h>
38355b4669Sjacobs #include <ipp.h>
39355b4669Sjacobs #include <ipp-listener.h>
40355b4669Sjacobs 
41355b4669Sjacobs papi_status_t
ipp_print_job(papi_service_t svc,papi_attribute_t ** request,papi_attribute_t *** response,ipp_reader_t iread,void * fd)42355b4669Sjacobs ipp_print_job(papi_service_t svc, papi_attribute_t **request,
43355b4669Sjacobs 		papi_attribute_t ***response, ipp_reader_t iread, void *fd)
44355b4669Sjacobs {
45355b4669Sjacobs 	papi_status_t status;
46355b4669Sjacobs 	papi_stream_t s = NULL;
47355b4669Sjacobs 	papi_job_t j = NULL;
48355b4669Sjacobs 	papi_attribute_t **operational = NULL;
49355b4669Sjacobs 	papi_attribute_t **job_attributes = NULL;
50355b4669Sjacobs 	char *queue = NULL;
51355b4669Sjacobs 	ssize_t rc;
52355b4669Sjacobs 	char buf[BUFSIZ];
533d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	char *host = NULL;
54*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 	int fp = -1;
55355b4669Sjacobs 	char *keys[] = { "attributes-natural-language", "attributes-charset",
56355b4669Sjacobs 			"printer-uri", NULL };
57355b4669Sjacobs 
58355b4669Sjacobs 	/* Get operational attributes from the request */
59355b4669Sjacobs 	(void) papiAttributeListGetCollection(request, NULL,
603d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	    "operational-attributes-group", &operational);
61355b4669Sjacobs 
62355b4669Sjacobs 	/*
63355b4669Sjacobs 	 * The operational-attributes-group must contain:
64355b4669Sjacobs 	 *	printer-uri
65355b4669Sjacobs 	 */
66355b4669Sjacobs 	get_printer_id(operational, &queue, NULL);
67355b4669Sjacobs 	if (queue == NULL) {
68355b4669Sjacobs 		ipp_set_status(response, status, "printer-uri: %s",
693d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    papiStatusString(status));
70355b4669Sjacobs 		return (PAPI_BAD_REQUEST);
71355b4669Sjacobs 	}
72355b4669Sjacobs 
73355b4669Sjacobs 	/*
74355b4669Sjacobs 	 * The operational-attributes-group may contain:
75355b4669Sjacobs 	 *	job-name
76355b4669Sjacobs 	 *	ipp-attribute-fidelity
77355b4669Sjacobs 	 *	document-name
78355b4669Sjacobs 	 *	compression
79355b4669Sjacobs 	 *	document-format
80355b4669Sjacobs 	 *	document-natural-language
81355b4669Sjacobs 	 *	job-k-octets
82355b4669Sjacobs 	 *	job-impressions
83355b4669Sjacobs 	 *	job-media-sheets
84355b4669Sjacobs 	 * Simply copy the entire contents of the operational-attributes-group
85355b4669Sjacobs 	 * for the PAPI call's possible use.
86355b4669Sjacobs 	 */
87355b4669Sjacobs 	split_and_copy_attributes(keys, operational, NULL, &job_attributes);
88355b4669Sjacobs 
89355b4669Sjacobs 	/* copy any job-attributes-group attributes for the PAPI call */
90355b4669Sjacobs 	if (papiAttributeListGetCollection(request, NULL,
91286caa64SKeerthi Kondaka 	    "job-attributes-group", &operational) == PAPI_OK) {
92286caa64SKeerthi Kondaka 		char *user = NULL;
93286caa64SKeerthi Kondaka 
94355b4669Sjacobs 		copy_attributes(&job_attributes, operational);
95355b4669Sjacobs 
96286caa64SKeerthi Kondaka 		if (papiAttributeListGetString(operational, NULL,
97286caa64SKeerthi Kondaka 		    "requesting-user-name", &user) == PAPI_OK) {
98286caa64SKeerthi Kondaka 			papiAttributeListAddString(&job_attributes,
99286caa64SKeerthi Kondaka 			    PAPI_ATTR_REPLACE, "requesting-user-name", user);
100286caa64SKeerthi Kondaka 		}
101286caa64SKeerthi Kondaka 	}
102286caa64SKeerthi Kondaka 
103*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 	/* Set "job-originating-host-name" in next block */
104*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 	(void) papiAttributeListGetInteger(request, NULL,
105*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 	    "peer-socket", &fp);
106*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 
107*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 	if (fp != -1) {
108*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 		struct sockaddr_in peer;
109*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 		socklen_t peer_len = sizeof (peer);
110*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 
111*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 		/* who is our peer ? */
112*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 		if (getpeername(fp, (struct sockaddr *)&peer,
113*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 		    &peer_len) == 0) {
114*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			struct hostent *he;
115*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			int error_num;
116*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 
117*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			/*
118*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			 * get their name or return a string containing
119*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			 * their address
120*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			 */
121*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			if ((he = getipnodebyaddr((const char *)&peer.sin_addr,
122*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			    sizeof (peer.sin_addr), peer.sin_family,
123*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			    &error_num)) == NULL) {
124*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				char tmp_buf[INET6_ADDRSTRLEN];
125*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				papiAttributeListAddString(
126*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    &job_attributes,
127*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    PAPI_ATTR_REPLACE,
128*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    "job-originating-host-name",
129*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    (char *)inet_ntop(peer.sin_family,
130*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    &peer.sin_addr, tmp_buf,
131*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    sizeof (tmp_buf)));
132*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 
133*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 			} else {
134*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				if (is_localhost(he->h_name) != 0)
1353d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					papiAttributeListAddString(
1363d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    &job_attributes,
1373d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    PAPI_ATTR_REPLACE,
1383d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    "job-originating-host-name",
139*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    "localhost");
140*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				else if (he->h_name != NULL)
1413d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					papiAttributeListAddString(
1423d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    &job_attributes,
1433d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    PAPI_ATTR_REPLACE,
1443d09a4feSsonam gupta - Sun Microsystems - Bangalore India 					    "job-originating-host-name",
145*f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    he->h_name);
1463d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			}
1473d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		}
1483d09a4feSsonam gupta - Sun Microsystems - Bangalore India 	}
1493d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
150355b4669Sjacobs 	/* request job creation with a resulting stream that we can write to */
151355b4669Sjacobs 	status = papiJobStreamOpen(svc, queue, job_attributes, NULL, &s);
152355b4669Sjacobs 	papiAttributeListFree(job_attributes);
153355b4669Sjacobs 	if (status != PAPI_OK) {
154355b4669Sjacobs 		ipp_set_status(response, status, "job submission: %s",
1553d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    ipp_svc_status_mesg(svc, status));
156355b4669Sjacobs 		return (status);
157355b4669Sjacobs 	}
158355b4669Sjacobs 
159355b4669Sjacobs 	/* copy the document data from the IPP connection to the stream */
160355b4669Sjacobs 	while ((status == PAPI_OK) && ((rc = iread(fd, buf, sizeof (buf))) > 0))
161355b4669Sjacobs 		status = papiJobStreamWrite(svc, s, buf, rc);
162355b4669Sjacobs 	if (status != PAPI_OK) {
163355b4669Sjacobs 		ipp_set_status(response, status, "write job data: %s",
1643d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    ipp_svc_status_mesg(svc, status));
165355b4669Sjacobs 		return (status);
166355b4669Sjacobs 	}
167355b4669Sjacobs 
168355b4669Sjacobs 	/* close the stream, committing the job */
169355b4669Sjacobs 	status = papiJobStreamClose(svc, s, &j);
170355b4669Sjacobs 	if (status != PAPI_OK) {
171355b4669Sjacobs 		ipp_set_status(response, status, "close job stream: %s",
1723d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    ipp_svc_status_mesg(svc, status));
173355b4669Sjacobs 		papiJobFree(j);	/* we shouldn't have a job, but just in case */
174355b4669Sjacobs 		return (status);
175355b4669Sjacobs 	}
176355b4669Sjacobs 
177355b4669Sjacobs 	/* add the job attributes to the response in a job-attributes-group */
178355b4669Sjacobs 	if (j != NULL) {
179355b4669Sjacobs 		papi_to_ipp_job_group(response, request, PAPI_ATTR_REPLACE, j);
180355b4669Sjacobs 		papiJobFree(j);
181355b4669Sjacobs 	}
182355b4669Sjacobs 
183355b4669Sjacobs 	return (status);
184355b4669Sjacobs }
185