1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs 
22*355b4669Sjacobs /*
23*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*355b4669Sjacobs  * Use is subject to license terms.
25*355b4669Sjacobs  *
26*355b4669Sjacobs  */
27*355b4669Sjacobs 
28*355b4669Sjacobs /* $Id: ipp.c 146 2006-03-24 00:26:54Z njacobs $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #include <stdio.h>
31*355b4669Sjacobs #include <stdarg.h>
32*355b4669Sjacobs #include <papi.h>
33*355b4669Sjacobs #include "ipp.h"
34*355b4669Sjacobs 
35*355b4669Sjacobs /*
36*355b4669Sjacobs  * IPP requests/responses are represented as attribute lists.  An IPP request
37*355b4669Sjacobs  * attribute list will contain header information attributes:
38*355b4669Sjacobs  *	version-major (int)
39*355b4669Sjacobs  *	version-minor (int)
40*355b4669Sjacobs  *	request-id (int)
41*355b4669Sjacobs  *	operation-id (int)
42*355b4669Sjacobs  * It will also contain 1 or more attribute groups (collections)
43*355b4669Sjacobs  *	operational-attribute-group
44*355b4669Sjacobs  *		...
45*355b4669Sjacobs  * this routine validates that the request falls within the guidelines of
46*355b4669Sjacobs  * the protocol specification (or some other level of conformance if the
47*355b4669Sjacobs  * restrictions have been specified at the top level of the request using
48*355b4669Sjacobs  * a "conformance" attribute.
49*355b4669Sjacobs  */
50*355b4669Sjacobs papi_status_t
ipp_validate_request(papi_attribute_t ** request,papi_attribute_t *** response)51*355b4669Sjacobs ipp_validate_request(papi_attribute_t **request, papi_attribute_t ***response)
52*355b4669Sjacobs {
53*355b4669Sjacobs 	papi_attribute_t **attributes = NULL;
54*355b4669Sjacobs 	papi_status_t result = PAPI_OK;
55*355b4669Sjacobs 	char *s;
56*355b4669Sjacobs 
57*355b4669Sjacobs 	if ((request == NULL) || (response == NULL) || (*response == NULL))
58*355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
59*355b4669Sjacobs 
60*355b4669Sjacobs 	/* validate the operational attributes group */
61*355b4669Sjacobs 	result = papiAttributeListGetCollection(request, NULL,
62*355b4669Sjacobs 				"operational-attributes-group", &attributes);
63*355b4669Sjacobs 	if (result != PAPI_OK) {
64*355b4669Sjacobs 		ipp_set_status(response, result,
65*355b4669Sjacobs 				"operational attribute group: %s",
66*355b4669Sjacobs 				papiStatusString(result));
67*355b4669Sjacobs 		return (result);
68*355b4669Sjacobs 	}
69*355b4669Sjacobs 
70*355b4669Sjacobs 	result = papiAttributeListGetString(attributes, NULL,
71*355b4669Sjacobs 				"attributes-charset", &s);
72*355b4669Sjacobs 	if (result != PAPI_OK) {
73*355b4669Sjacobs 		ipp_set_status(response, result, "attributes-charset: %s",
74*355b4669Sjacobs 				papiStatusString(result));
75*355b4669Sjacobs 		return (result);
76*355b4669Sjacobs 	}
77*355b4669Sjacobs 
78*355b4669Sjacobs 	result = papiAttributeListGetString(attributes, NULL,
79*355b4669Sjacobs 				"attributes-natural-language", &s);
80*355b4669Sjacobs 	if (result != PAPI_OK) {
81*355b4669Sjacobs 		ipp_set_status(response, result,
82*355b4669Sjacobs 				"attributes-natural-language: %s",
83*355b4669Sjacobs 				papiStatusString(result));
84*355b4669Sjacobs 		return (result);
85*355b4669Sjacobs 	}
86*355b4669Sjacobs 
87*355b4669Sjacobs 	return (result);
88*355b4669Sjacobs }
89*355b4669Sjacobs 
90*355b4669Sjacobs /*
91*355b4669Sjacobs  * Add/Modify the statuse-code and status-message in an IPP response's
92*355b4669Sjacobs  * operational attributes group.
93*355b4669Sjacobs  */
94*355b4669Sjacobs void
ipp_set_status(papi_attribute_t *** message,papi_status_t status,char * format,...)95*355b4669Sjacobs ipp_set_status(papi_attribute_t ***message, papi_status_t status,
96*355b4669Sjacobs 		char *format, ...)
97*355b4669Sjacobs {
98*355b4669Sjacobs 	if (message == NULL)
99*355b4669Sjacobs 		return;
100*355b4669Sjacobs 
101*355b4669Sjacobs 	if (format != NULL) {
102*355b4669Sjacobs 		papi_attribute_t **operational = NULL;
103*355b4669Sjacobs 		papi_attribute_t **saved;
104*355b4669Sjacobs 		char mesg[256];	/* status-message is type text(255) */
105*355b4669Sjacobs 		va_list ap;
106*355b4669Sjacobs 
107*355b4669Sjacobs 		(void) papiAttributeListGetCollection(*message, NULL,
108*355b4669Sjacobs 					"operational-attributes-group",
109*355b4669Sjacobs 					&operational);
110*355b4669Sjacobs 		saved = operational;
111*355b4669Sjacobs 
112*355b4669Sjacobs 		va_start(ap, format);
113*355b4669Sjacobs 		(void) vsnprintf(mesg, sizeof (mesg), format, ap);
114*355b4669Sjacobs 		va_end(ap);
115*355b4669Sjacobs 
116*355b4669Sjacobs 		(void) papiAttributeListAddString(&operational,
117*355b4669Sjacobs 				PAPI_ATTR_APPEND, "status-message", mesg);
118*355b4669Sjacobs 
119*355b4669Sjacobs 		/*
120*355b4669Sjacobs 		 * We need to check and see if adding the status-message caused
121*355b4669Sjacobs 		 * the operational attributes group to be relocated in memory.
122*355b4669Sjacobs 		 * If it has been, we will need to re-add the collection to
123*355b4669Sjacobs 		 * the message.
124*355b4669Sjacobs 		 */
125*355b4669Sjacobs 		if (saved != operational)
126*355b4669Sjacobs 			(void) papiAttributeListAddCollection(message,
127*355b4669Sjacobs 					PAPI_ATTR_REPLACE,
128*355b4669Sjacobs 					"operational-attributes-group",
129*355b4669Sjacobs 					operational);
130*355b4669Sjacobs 	}
131*355b4669Sjacobs 
132*355b4669Sjacobs 	(void) papiAttributeListAddInteger(message, PAPI_ATTR_APPEND,
133*355b4669Sjacobs 				"status-code", status);
134*355b4669Sjacobs }
135