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