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