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