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