4 * Side-channel API code for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "sidechannel.h"
23 #include "cups-private.h"
30 # include <sys/select.h>
31 # include <sys/time.h>
35 #endif /* HAVE_POLL */
39 * Buffer size for side-channel requests...
42 #define _CUPS_SC_MAX_DATA 65535
43 #define _CUPS_SC_MAX_BUFFER 65540
47 * 'cupsSideChannelDoRequest()' - Send a side-channel command to a backend and wait for a response.
49 * This function is normally only called by filters, drivers, or port
50 * monitors in order to communicate with the backend used by the current
51 * printer. Programs must be prepared to handle timeout or "not
52 * implemented" status codes, which indicate that the backend or device
53 * do not support the specified side-channel command.
55 * The "datalen" parameter must be initialized to the size of the buffer
56 * pointed to by the "data" parameter. cupsSideChannelDoRequest() will
57 * update the value to contain the number of data bytes in the buffer.
59 * @since CUPS 1.3/OS X 10.5@
62 cups_sc_status_t
/* O - Status of command */
63 cupsSideChannelDoRequest(
64 cups_sc_command_t command
, /* I - Command to send */
65 char *data
, /* O - Response data buffer pointer */
66 int *datalen
, /* IO - Size of data buffer on entry, number of bytes in buffer on return */
67 double timeout
) /* I - Timeout in seconds */
69 cups_sc_status_t status
; /* Status of command */
70 cups_sc_command_t rcommand
; /* Response command */
73 if (cupsSideChannelWrite(command
, CUPS_SC_STATUS_NONE
, NULL
, 0, timeout
))
74 return (CUPS_SC_STATUS_TIMEOUT
);
76 if (cupsSideChannelRead(&rcommand
, &status
, data
, datalen
, timeout
))
77 return (CUPS_SC_STATUS_TIMEOUT
);
79 if (rcommand
!= command
)
80 return (CUPS_SC_STATUS_BAD_MESSAGE
);
87 * 'cupsSideChannelRead()' - Read a side-channel message.
89 * This function is normally only called by backend programs to read
90 * commands from a filter, driver, or port monitor program. The
91 * caller must be prepared to handle incomplete or invalid messages
92 * and return the corresponding status codes.
94 * The "datalen" parameter must be initialized to the size of the buffer
95 * pointed to by the "data" parameter. cupsSideChannelDoRequest() will
96 * update the value to contain the number of data bytes in the buffer.
98 * @since CUPS 1.3/OS X 10.5@
101 int /* O - 0 on success, -1 on error */
103 cups_sc_command_t
*command
, /* O - Command code */
104 cups_sc_status_t
*status
, /* O - Status code */
105 char *data
, /* O - Data buffer pointer */
106 int *datalen
, /* IO - Size of data buffer on entry, number of bytes in buffer on return */
107 double timeout
) /* I - Timeout in seconds */
109 char *buffer
; /* Message buffer */
110 ssize_t bytes
; /* Bytes read */
111 int templen
; /* Data length from message */
112 int nfds
; /* Number of file descriptors */
114 struct pollfd pfd
; /* Poll structure for poll() */
116 fd_set input_set
; /* Input set for select() */
117 struct timeval stimeout
; /* Timeout value for select() */
118 #endif /* HAVE_POLL */
121 DEBUG_printf(("cupsSideChannelRead(command=%p, status=%p, data=%p, "
122 "datalen=%p(%d), timeout=%.3f)", command
, status
, data
,
123 datalen
, datalen
? *datalen
: -1, timeout
));
126 * Range check input...
129 if (!command
|| !status
)
133 * See if we have pending data on the side-channel socket...
140 while ((nfds
= poll(&pfd
, 1,
141 timeout
< 0.0 ? -1 : (int)(timeout
* 1000))) < 0 &&
142 (errno
== EINTR
|| errno
== EAGAIN
))
147 FD_SET(CUPS_SC_FD
, &input_set
);
149 stimeout
.tv_sec
= (int)timeout
;
150 stimeout
.tv_usec
= (int)(timeout
* 1000000) % 1000000;
152 while ((nfds
= select(CUPS_SC_FD
+ 1, &input_set
, NULL
, NULL
,
153 timeout
< 0.0 ? NULL
: &stimeout
)) < 0 &&
154 (errno
== EINTR
|| errno
== EAGAIN
))
157 #endif /* HAVE_POLL */
161 *command
= CUPS_SC_CMD_NONE
;
162 *status
= nfds
==0 ? CUPS_SC_STATUS_TIMEOUT
: CUPS_SC_STATUS_IO_ERROR
;
167 * Read a side-channel message for the format:
169 * Byte(s) Description
170 * ------- -------------------------------------------
173 * 2-3 Data length (network byte order)
177 if ((buffer
= _cupsBufferGet(_CUPS_SC_MAX_BUFFER
)) == NULL
)
179 *command
= CUPS_SC_CMD_NONE
;
180 *status
= CUPS_SC_STATUS_TOO_BIG
;
185 while ((bytes
= read(CUPS_SC_FD
, buffer
, _CUPS_SC_MAX_BUFFER
)) < 0)
186 if (errno
!= EINTR
&& errno
!= EAGAIN
)
188 DEBUG_printf(("1cupsSideChannelRead: Read error: %s", strerror(errno
)));
190 _cupsBufferRelease(buffer
);
192 *command
= CUPS_SC_CMD_NONE
;
193 *status
= CUPS_SC_STATUS_IO_ERROR
;
199 * Watch for EOF or too few bytes...
204 DEBUG_printf(("1cupsSideChannelRead: Short read of " CUPS_LLFMT
" bytes", CUPS_LLCAST bytes
));
206 _cupsBufferRelease(buffer
);
208 *command
= CUPS_SC_CMD_NONE
;
209 *status
= CUPS_SC_STATUS_BAD_MESSAGE
;
215 * Validate the command code in the message...
218 if (buffer
[0] < CUPS_SC_CMD_SOFT_RESET
||
219 buffer
[0] >= CUPS_SC_CMD_MAX
)
221 DEBUG_printf(("1cupsSideChannelRead: Bad command %d!", buffer
[0]));
223 _cupsBufferRelease(buffer
);
225 *command
= CUPS_SC_CMD_NONE
;
226 *status
= CUPS_SC_STATUS_BAD_MESSAGE
;
231 *command
= (cups_sc_command_t
)buffer
[0];
234 * Validate the data length in the message...
237 templen
= ((buffer
[2] & 255) << 8) | (buffer
[3] & 255);
239 if (templen
> 0 && (!data
|| !datalen
))
242 * Either the response is bigger than the provided buffer or the
243 * response is bigger than we've read...
246 *status
= CUPS_SC_STATUS_TOO_BIG
;
248 else if (!datalen
|| templen
> *datalen
|| templen
> (bytes
- 4))
251 * Either the response is bigger than the provided buffer or the
252 * response is bigger than we've read...
255 *status
= CUPS_SC_STATUS_TOO_BIG
;
260 * The response data will fit, copy it over and provide the actual
264 *status
= (cups_sc_status_t
)buffer
[1];
267 memcpy(data
, buffer
+ 4, (size_t)templen
);
270 _cupsBufferRelease(buffer
);
272 DEBUG_printf(("1cupsSideChannelRead: Returning status=%d", *status
));
279 * 'cupsSideChannelSNMPGet()' - Query a SNMP OID's value.
281 * This function asks the backend to do a SNMP OID query on behalf of the
282 * filter, port monitor, or backend using the default community name.
284 * "oid" contains a numeric OID consisting of integers separated by periods,
285 * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not
286 * supported and must be converted to their numeric forms.
288 * On input, "data" and "datalen" provide the location and size of the
289 * buffer to hold the OID value as a string. HEX-String (binary) values are
290 * converted to hexadecimal strings representing the binary data, while
291 * NULL-Value and unknown OID types are returned as the empty string.
292 * The returned "datalen" does not include the trailing nul.
294 * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not
295 * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
296 * the printer does not respond to the SNMP query.
298 * @since CUPS 1.4/OS X 10.6@
301 cups_sc_status_t
/* O - Query status */
302 cupsSideChannelSNMPGet(
303 const char *oid
, /* I - OID to query */
304 char *data
, /* I - Buffer for OID value */
305 int *datalen
, /* IO - Size of OID buffer on entry, size of value on return */
306 double timeout
) /* I - Timeout in seconds */
308 cups_sc_status_t status
; /* Status of command */
309 cups_sc_command_t rcommand
; /* Response command */
310 char *real_data
; /* Real data buffer for response */
311 int real_datalen
, /* Real length of data buffer */
312 real_oidlen
; /* Length of returned OID string */
315 DEBUG_printf(("cupsSideChannelSNMPGet(oid=\"%s\", data=%p, datalen=%p(%d), "
316 "timeout=%.3f)", oid
, data
, datalen
, datalen
? *datalen
: -1,
320 * Range check input...
323 if (!oid
|| !*oid
|| !data
|| !datalen
|| *datalen
< 2)
324 return (CUPS_SC_STATUS_BAD_MESSAGE
);
329 * Send the request to the backend and wait for a response...
332 if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET
, CUPS_SC_STATUS_NONE
, oid
,
333 (int)strlen(oid
) + 1, timeout
))
334 return (CUPS_SC_STATUS_TIMEOUT
);
336 if ((real_data
= _cupsBufferGet(_CUPS_SC_MAX_BUFFER
)) == NULL
)
337 return (CUPS_SC_STATUS_TOO_BIG
);
339 real_datalen
= _CUPS_SC_MAX_BUFFER
;
340 if (cupsSideChannelRead(&rcommand
, &status
, real_data
, &real_datalen
, timeout
))
342 _cupsBufferRelease(real_data
);
343 return (CUPS_SC_STATUS_TIMEOUT
);
346 if (rcommand
!= CUPS_SC_CMD_SNMP_GET
)
348 _cupsBufferRelease(real_data
);
349 return (CUPS_SC_STATUS_BAD_MESSAGE
);
352 if (status
== CUPS_SC_STATUS_OK
)
355 * Parse the response of the form "oid\0value"...
358 real_oidlen
= (int)strlen(real_data
) + 1;
359 real_datalen
-= real_oidlen
;
361 if ((real_datalen
+ 1) > *datalen
)
363 _cupsBufferRelease(real_data
);
364 return (CUPS_SC_STATUS_TOO_BIG
);
367 memcpy(data
, real_data
+ real_oidlen
, (size_t)real_datalen
);
368 data
[real_datalen
] = '\0';
370 *datalen
= real_datalen
;
373 _cupsBufferRelease(real_data
);
380 * 'cupsSideChannelSNMPWalk()' - Query multiple SNMP OID values.
382 * This function asks the backend to do multiple SNMP OID queries on behalf
383 * of the filter, port monitor, or backend using the default community name.
384 * All OIDs under the "parent" OID are queried and the results are sent to
385 * the callback function you provide.
387 * "oid" contains a numeric OID consisting of integers separated by periods,
388 * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not
389 * supported and must be converted to their numeric forms.
391 * "timeout" specifies the timeout for each OID query. The total amount of
392 * time will depend on the number of OID values found and the time required
395 * "cb" provides a function to call for every value that is found. "context"
396 * is an application-defined pointer that is sent to the callback function
397 * along with the OID and current data. The data passed to the callback is the
398 * same as returned by @link cupsSideChannelSNMPGet@.
400 * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not
401 * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
402 * the printer does not respond to the first SNMP query.
404 * @since CUPS 1.4/OS X 10.6@
407 cups_sc_status_t
/* O - Status of first query of @code CUPS_SC_STATUS_OK@ on success */
408 cupsSideChannelSNMPWalk(
409 const char *oid
, /* I - First numeric OID to query */
410 double timeout
, /* I - Timeout for each query in seconds */
411 cups_sc_walk_func_t cb
, /* I - Function to call with each value */
412 void *context
) /* I - Application-defined pointer to send to callback */
414 cups_sc_status_t status
; /* Status of command */
415 cups_sc_command_t rcommand
; /* Response command */
416 char *real_data
; /* Real data buffer for response */
417 int real_datalen
; /* Real length of data buffer */
418 size_t real_oidlen
, /* Length of returned OID string */
419 oidlen
; /* Length of first OID */
420 const char *current_oid
; /* Current OID */
421 char last_oid
[2048]; /* Last OID */
424 DEBUG_printf(("cupsSideChannelSNMPWalk(oid=\"%s\", timeout=%.3f, cb=%p, "
425 "context=%p)", oid
, timeout
, cb
, context
));
428 * Range check input...
431 if (!oid
|| !*oid
|| !cb
)
432 return (CUPS_SC_STATUS_BAD_MESSAGE
);
434 if ((real_data
= _cupsBufferGet(_CUPS_SC_MAX_BUFFER
)) == NULL
)
435 return (CUPS_SC_STATUS_TOO_BIG
);
438 * Loop until the OIDs don't match...
442 oidlen
= strlen(oid
);
448 * Send the request to the backend and wait for a response...
451 if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET_NEXT
, CUPS_SC_STATUS_NONE
,
452 current_oid
, (int)strlen(current_oid
) + 1, timeout
))
454 _cupsBufferRelease(real_data
);
455 return (CUPS_SC_STATUS_TIMEOUT
);
458 real_datalen
= _CUPS_SC_MAX_BUFFER
;
459 if (cupsSideChannelRead(&rcommand
, &status
, real_data
, &real_datalen
,
462 _cupsBufferRelease(real_data
);
463 return (CUPS_SC_STATUS_TIMEOUT
);
466 if (rcommand
!= CUPS_SC_CMD_SNMP_GET_NEXT
)
468 _cupsBufferRelease(real_data
);
469 return (CUPS_SC_STATUS_BAD_MESSAGE
);
472 if (status
== CUPS_SC_STATUS_OK
)
475 * Parse the response of the form "oid\0value"...
478 if (strncmp(real_data
, oid
, oidlen
) || real_data
[oidlen
] != '.' ||
479 !strcmp(real_data
, last_oid
))
482 * Done with this set of OIDs...
485 _cupsBufferRelease(real_data
);
486 return (CUPS_SC_STATUS_OK
);
489 if ((size_t)real_datalen
< sizeof(real_data
))
490 real_data
[real_datalen
] = '\0';
492 real_oidlen
= strlen(real_data
) + 1;
493 real_datalen
-= (int)real_oidlen
;
496 * Call the callback with the OID and data...
499 (*cb
)(real_data
, real_data
+ real_oidlen
, real_datalen
, context
);
502 * Update the current OID...
505 current_oid
= real_data
;
506 strlcpy(last_oid
, current_oid
, sizeof(last_oid
));
509 while (status
== CUPS_SC_STATUS_OK
);
511 _cupsBufferRelease(real_data
);
518 * 'cupsSideChannelWrite()' - Write a side-channel message.
520 * This function is normally only called by backend programs to send
521 * responses to a filter, driver, or port monitor program.
523 * @since CUPS 1.3/OS X 10.5@
526 int /* O - 0 on success, -1 on error */
527 cupsSideChannelWrite(
528 cups_sc_command_t command
, /* I - Command code */
529 cups_sc_status_t status
, /* I - Status code */
530 const char *data
, /* I - Data buffer pointer */
531 int datalen
, /* I - Number of bytes of data */
532 double timeout
) /* I - Timeout in seconds */
534 char *buffer
; /* Message buffer */
535 ssize_t bytes
; /* Bytes written */
537 struct pollfd pfd
; /* Poll structure for poll() */
539 fd_set output_set
; /* Output set for select() */
540 struct timeval stimeout
; /* Timeout value for select() */
541 #endif /* HAVE_POLL */
545 * Range check input...
548 if (command
< CUPS_SC_CMD_SOFT_RESET
|| command
>= CUPS_SC_CMD_MAX
||
549 datalen
< 0 || datalen
> _CUPS_SC_MAX_DATA
|| (datalen
> 0 && !data
))
553 * See if we can safely write to the side-channel socket...
558 pfd
.events
= POLLOUT
;
562 if (poll(&pfd
, 1, -1) < 1)
565 else if (poll(&pfd
, 1, (int)(timeout
* 1000)) < 1)
569 FD_ZERO(&output_set
);
570 FD_SET(CUPS_SC_FD
, &output_set
);
574 if (select(CUPS_SC_FD
+ 1, NULL
, &output_set
, NULL
, NULL
) < 1)
579 stimeout
.tv_sec
= (int)timeout
;
580 stimeout
.tv_usec
= (int)(timeout
* 1000000) % 1000000;
582 if (select(CUPS_SC_FD
+ 1, NULL
, &output_set
, NULL
, &stimeout
) < 1)
585 #endif /* HAVE_POLL */
588 * Write a side-channel message in the format:
590 * Byte(s) Description
591 * ------- -------------------------------------------
594 * 2-3 Data length (network byte order) <= 16384
598 if ((buffer
= _cupsBufferGet((size_t)datalen
+ 4)) == NULL
)
603 buffer
[2] = (char)(datalen
>> 8);
604 buffer
[3] = (char)(datalen
& 255);
610 memcpy(buffer
+ 4, data
, (size_t)datalen
);
614 while (write(CUPS_SC_FD
, buffer
, (size_t)bytes
) < 0)
615 if (errno
!= EINTR
&& errno
!= EAGAIN
)
617 _cupsBufferRelease(buffer
);
621 _cupsBufferRelease(buffer
);