]>
Commit | Line | Data |
---|---|---|
ae71f5de | 1 | /* |
503b54c9 | 2 | * cupsGetDevices implementation for CUPS. |
ae71f5de | 3 | * |
e7a78c92 | 4 | * Copyright 2008-2016 by Apple Inc. |
ae71f5de | 5 | * |
503b54c9 MS |
6 | * These coded instructions, statements, and computer programs are the |
7 | * property of Apple Inc. and are protected by Federal copyright | |
8 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
9 | * which should have been included with this file. If this file is | |
10 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ae71f5de | 11 | * |
503b54c9 | 12 | * This file is subject to the Apple OS-Developed Software exception. |
ae71f5de MS |
13 | */ |
14 | ||
15 | /* | |
16 | * Include necessary headers... | |
17 | */ | |
18 | ||
71e16022 | 19 | #include "cups-private.h" |
e7a78c92 | 20 | #include "adminutil.h" |
ae71f5de MS |
21 | |
22 | ||
23 | /* | |
24 | * 'cupsGetDevices()' - Get available printer devices. | |
25 | * | |
26 | * This function sends a CUPS-Get-Devices request and streams the discovered | |
27 | * devices to the specified callback function. The "timeout" parameter controls | |
ed6e7faf MS |
28 | * how long the request lasts, while the "include_schemes" and "exclude_schemes" |
29 | * parameters provide comma-delimited lists of backends to include or omit from | |
30 | * the request respectively. | |
ae71f5de | 31 | * |
8072030b | 32 | * @since CUPS 1.4/macOS 10.6@ |
ae71f5de MS |
33 | */ |
34 | ||
35 | ipp_status_t /* O - Request status - @code IPP_OK@ on success. */ | |
36 | cupsGetDevices( | |
37 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
38 | int timeout, /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */ | |
ed6e7faf | 39 | const char *include_schemes, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */ |
ae71f5de MS |
40 | const char *exclude_schemes, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */ |
41 | cups_device_cb_t callback, /* I - Callback function */ | |
42 | void *user_data) /* I - User data pointer */ | |
43 | { | |
44 | ipp_t *request, /* CUPS-Get-Devices request */ | |
45 | *response; /* CUPS-Get-Devices response */ | |
46 | ipp_attribute_t *attr; /* Current attribute */ | |
47 | const char *device_class, /* device-class value */ | |
48 | *device_id, /* device-id value */ | |
49 | *device_info, /* device-info value */ | |
749b1e90 | 50 | *device_location, /* device-location value */ |
ae71f5de MS |
51 | *device_make_and_model, /* device-make-and-model value */ |
52 | *device_uri; /* device-uri value */ | |
53 | int blocking; /* Current blocking-IO mode */ | |
ed6e7faf | 54 | cups_option_t option; /* in/exclude-schemes option */ |
ae71f5de MS |
55 | http_status_t status; /* HTTP status of request */ |
56 | ipp_state_t state; /* IPP response state */ | |
57 | ||
58 | ||
59 | /* | |
60 | * Range check input... | |
61 | */ | |
62 | ||
807315e6 | 63 | DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", exclude_schemes=\"%s\", callback=%p, user_data=%p)", (void *)http, timeout, include_schemes, exclude_schemes, (void *)callback, user_data)); |
e07d4801 | 64 | |
ae71f5de | 65 | if (!callback) |
cb7f98ee | 66 | return (IPP_STATUS_ERROR_INTERNAL); |
ae71f5de MS |
67 | |
68 | if (!http) | |
69 | http = _cupsConnect(); | |
70 | ||
71 | if (!http) | |
cb7f98ee | 72 | return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE); |
ae71f5de MS |
73 | |
74 | /* | |
75 | * Create a CUPS-Get-Devices request... | |
76 | */ | |
77 | ||
cb7f98ee | 78 | request = ippNewRequest(IPP_OP_CUPS_GET_DEVICES); |
ae71f5de MS |
79 | |
80 | if (timeout > 0) | |
81 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout", | |
82 | timeout); | |
83 | ||
ed6e7faf MS |
84 | if (include_schemes) |
85 | { | |
86 | option.name = "include-schemes"; | |
87 | option.value = (char *)include_schemes; | |
88 | ||
89 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
90 | } | |
91 | ||
ae71f5de MS |
92 | if (exclude_schemes) |
93 | { | |
94 | option.name = "exclude-schemes"; | |
95 | option.value = (char *)exclude_schemes; | |
96 | ||
97 | cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION); | |
98 | } | |
99 | ||
100 | /* | |
5180a04c | 101 | * Send the request and do any necessary authentication... |
ae71f5de MS |
102 | */ |
103 | ||
5180a04c MS |
104 | do |
105 | { | |
106 | DEBUG_puts("2cupsGetDevices: Sending request..."); | |
107 | status = cupsSendRequest(http, request, "/", ippLength(request)); | |
108 | ||
109 | DEBUG_puts("2cupsGetDevices: Waiting for response status..."); | |
cb7f98ee | 110 | while (status == HTTP_STATUS_CONTINUE) |
5180a04c MS |
111 | status = httpUpdate(http); |
112 | ||
cb7f98ee | 113 | if (status != HTTP_STATUS_OK) |
5180a04c MS |
114 | { |
115 | httpFlush(http); | |
116 | ||
cb7f98ee | 117 | if (status == HTTP_STATUS_UNAUTHORIZED) |
5180a04c MS |
118 | { |
119 | /* | |
120 | * See if we can do authentication... | |
121 | */ | |
122 | ||
123 | DEBUG_puts("2cupsGetDevices: Need authorization..."); | |
124 | ||
125 | if (!cupsDoAuthentication(http, "POST", "/")) | |
cb7f98ee | 126 | httpReconnect2(http, 30000, NULL); |
5180a04c MS |
127 | else |
128 | { | |
cb7f98ee | 129 | status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
5180a04c MS |
130 | break; |
131 | } | |
132 | } | |
133 | ||
134 | #ifdef HAVE_SSL | |
cb7f98ee | 135 | else if (status == HTTP_STATUS_UPGRADE_REQUIRED) |
5180a04c MS |
136 | { |
137 | /* | |
138 | * Force a reconnect with encryption... | |
139 | */ | |
140 | ||
141 | DEBUG_puts("2cupsGetDevices: Need encryption..."); | |
142 | ||
cb7f98ee MS |
143 | if (!httpReconnect2(http, 30000, NULL)) |
144 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); | |
5180a04c MS |
145 | } |
146 | #endif /* HAVE_SSL */ | |
147 | } | |
148 | } | |
cb7f98ee MS |
149 | while (status == HTTP_STATUS_UNAUTHORIZED || |
150 | status == HTTP_STATUS_UPGRADE_REQUIRED); | |
ae71f5de | 151 | |
e07d4801 | 152 | DEBUG_printf(("2cupsGetDevices: status=%d", status)); |
ae71f5de MS |
153 | |
154 | ippDelete(request); | |
155 | ||
cb7f98ee | 156 | if (status != HTTP_STATUS_OK) |
ae71f5de MS |
157 | { |
158 | _cupsSetHTTPError(status); | |
159 | return (cupsLastError()); | |
160 | } | |
161 | ||
162 | /* | |
163 | * Read the response in non-blocking mode... | |
164 | */ | |
165 | ||
166 | blocking = httpGetBlocking(http); | |
167 | httpBlocking(http, 0); | |
168 | ||
169 | response = ippNew(); | |
170 | device_class = NULL; | |
171 | device_id = NULL; | |
172 | device_info = NULL; | |
749b1e90 | 173 | device_location = ""; |
ae71f5de MS |
174 | device_make_and_model = NULL; |
175 | device_uri = NULL; | |
176 | attr = NULL; | |
177 | ||
e07d4801 | 178 | DEBUG_puts("2cupsGetDevices: Reading response..."); |
ae71f5de MS |
179 | |
180 | do | |
181 | { | |
cb7f98ee | 182 | if ((state = ippRead(http, response)) == IPP_STATE_ERROR) |
ae71f5de MS |
183 | break; |
184 | ||
807315e6 | 185 | DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, (void *)response->last)); |
ae71f5de MS |
186 | |
187 | if (!response->attrs) | |
188 | continue; | |
189 | ||
190 | while (attr != response->last) | |
191 | { | |
192 | if (!attr) | |
193 | attr = response->attrs; | |
194 | else | |
195 | attr = attr->next; | |
196 | ||
e07d4801 MS |
197 | DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d", |
198 | attr->name, attr->value_tag)); | |
ae71f5de MS |
199 | |
200 | if (!attr->name) | |
201 | { | |
202 | if (device_class && device_id && device_info && device_make_and_model && | |
203 | device_uri) | |
204 | (*callback)(device_class, device_id, device_info, | |
749b1e90 MS |
205 | device_make_and_model, device_uri, device_location, |
206 | user_data); | |
ae71f5de MS |
207 | |
208 | device_class = NULL; | |
209 | device_id = NULL; | |
210 | device_info = NULL; | |
749b1e90 | 211 | device_location = ""; |
ae71f5de MS |
212 | device_make_and_model = NULL; |
213 | device_uri = NULL; | |
214 | } | |
215 | else if (!strcmp(attr->name, "device-class") && | |
216 | attr->value_tag == IPP_TAG_KEYWORD) | |
217 | device_class = attr->values[0].string.text; | |
218 | else if (!strcmp(attr->name, "device-id") && | |
219 | attr->value_tag == IPP_TAG_TEXT) | |
220 | device_id = attr->values[0].string.text; | |
221 | else if (!strcmp(attr->name, "device-info") && | |
222 | attr->value_tag == IPP_TAG_TEXT) | |
223 | device_info = attr->values[0].string.text; | |
749b1e90 MS |
224 | else if (!strcmp(attr->name, "device-location") && |
225 | attr->value_tag == IPP_TAG_TEXT) | |
226 | device_location = attr->values[0].string.text; | |
ae71f5de MS |
227 | else if (!strcmp(attr->name, "device-make-and-model") && |
228 | attr->value_tag == IPP_TAG_TEXT) | |
229 | device_make_and_model = attr->values[0].string.text; | |
230 | else if (!strcmp(attr->name, "device-uri") && | |
231 | attr->value_tag == IPP_TAG_URI) | |
232 | device_uri = attr->values[0].string.text; | |
233 | } | |
234 | } | |
cb7f98ee | 235 | while (state != IPP_STATE_DATA); |
ae71f5de | 236 | |
807315e6 | 237 | DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state, (void *)response->last)); |
ae71f5de MS |
238 | |
239 | if (device_class && device_id && device_info && device_make_and_model && | |
240 | device_uri) | |
241 | (*callback)(device_class, device_id, device_info, | |
749b1e90 | 242 | device_make_and_model, device_uri, device_location, user_data); |
ae71f5de MS |
243 | |
244 | /* | |
245 | * Set the IPP status and return... | |
246 | */ | |
247 | ||
248 | httpBlocking(http, blocking); | |
8922323b | 249 | httpFlush(http); |
ae71f5de | 250 | |
cb7f98ee MS |
251 | if (status == HTTP_STATUS_ERROR) |
252 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0); | |
ae71f5de MS |
253 | else |
254 | { | |
255 | attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); | |
256 | ||
e07d4801 | 257 | DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"", |
ae71f5de MS |
258 | ippErrorString(response->request.status.status_code), |
259 | attr ? attr->values[0].string.text : "")); | |
260 | ||
261 | _cupsSetError(response->request.status.status_code, | |
749b1e90 MS |
262 | attr ? attr->values[0].string.text : |
263 | ippErrorString(response->request.status.status_code), 0); | |
ae71f5de MS |
264 | } |
265 | ||
266 | ippDelete(response); | |
267 | ||
268 | return (cupsLastError()); | |
269 | } |