2 * Side-channel API code for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "sidechannel.h"
21 #include "cups-private.h"
28 # include <sys/select.h>
29 # include <sys/time.h>
33 #endif /* HAVE_POLL */
37 * Buffer size for side-channel requests...
40 #define _CUPS_SC_MAX_DATA 65535
41 #define _CUPS_SC_MAX_BUFFER 65540
45 * 'cupsSideChannelDoRequest()' - Send a side-channel command to a backend and wait for a response.
47 * This function is normally only called by filters, drivers, or port
48 * monitors in order to communicate with the backend used by the current
49 * printer. Programs must be prepared to handle timeout or "not
50 * implemented" status codes, which indicate that the backend or device
51 * do not support the specified side-channel command.
53 * The "datalen" parameter must be initialized to the size of the buffer
54 * pointed to by the "data" parameter. cupsSideChannelDoRequest() will
55 * update the value to contain the number of data bytes in the buffer.
57 * @since CUPS 1.3/macOS 10.5@
60 cups_sc_status_t
/* O - Status of command */
61 cupsSideChannelDoRequest(
62 cups_sc_command_t command
, /* I - Command to send */
63 char *data
, /* O - Response data buffer pointer */
64 int *datalen
, /* IO - Size of data buffer on entry, number of bytes in buffer on return */
65 double timeout
) /* I - Timeout in seconds */
67 cups_sc_status_t status
; /* Status of command */
68 cups_sc_command_t rcommand
; /* Response command */
71 if (cupsSideChannelWrite(command
, CUPS_SC_STATUS_NONE
, NULL
, 0, timeout
))
72 return (CUPS_SC_STATUS_TIMEOUT
);
74 if (cupsSideChannelRead(&rcommand
, &status
, data
, datalen
, timeout
))
75 return (CUPS_SC_STATUS_TIMEOUT
);
77 if (rcommand
!= command
)
78 return (CUPS_SC_STATUS_BAD_MESSAGE
);
85 * 'cupsSideChannelRead()' - Read a side-channel message.
87 * This function is normally only called by backend programs to read
88 * commands from a filter, driver, or port monitor program. The
89 * caller must be prepared to handle incomplete or invalid messages
90 * and return the corresponding status codes.
92 * The "datalen" parameter must be initialized to the size of the buffer
93 * pointed to by the "data" parameter. cupsSideChannelDoRequest() will
94 * update the value to contain the number of data bytes in the buffer.
96 * @since CUPS 1.3/macOS 10.5@
99 int /* O - 0 on success, -1 on error */
101 cups_sc_command_t
*command
, /* O - Command code */
102 cups_sc_status_t
*status
, /* O - Status code */
103 char *data
, /* O - Data buffer pointer */
104 int *datalen
, /* IO - Size of data buffer on entry, number of bytes in buffer on return */
105 double timeout
) /* I - Timeout in seconds */
107 char *buffer
; /* Message buffer */
108 ssize_t bytes
; /* Bytes read */
109 int templen
; /* Data length from message */
110 int nfds
; /* Number of file descriptors */
112 struct pollfd pfd
; /* Poll structure for poll() */
114 fd_set input_set
; /* Input set for select() */
115 struct timeval stimeout
; /* Timeout value for select() */
116 #endif /* HAVE_POLL */
119 DEBUG_printf(("cupsSideChannelRead(command=%p, status=%p, data=%p, "
120 "datalen=%p(%d), timeout=%.3f)", command
, status
, data
,
121 datalen
, datalen
? *datalen
: -1, timeout
));
124 * Range check input...
127 if (!command
|| !status
)
131 * See if we have pending data on the side-channel socket...
138 while ((nfds
= poll(&pfd
, 1,
139 timeout
< 0.0 ? -1 : (int)(timeout
* 1000))) < 0 &&
140 (errno
== EINTR
|| errno
== EAGAIN
))
145 FD_SET(CUPS_SC_FD
, &input_set
);
147 stimeout
.tv_sec
= (int)timeout
;
148 stimeout
.tv_usec
= (int)(timeout
* 1000000) % 1000000;
150 while ((nfds
= select(CUPS_SC_FD
+ 1, &input_set
, NULL
, NULL
,
151 timeout
< 0.0 ? NULL
: &stimeout
)) < 0 &&
152 (errno
== EINTR
|| errno
== EAGAIN
))
155 #endif /* HAVE_POLL */
159 *command
= CUPS_SC_CMD_NONE
;
160 *status
= nfds
==0 ? CUPS_SC_STATUS_TIMEOUT
: CUPS_SC_STATUS_IO_ERROR
;
165 * Read a side-channel message for the format:
167 * Byte(s) Description
168 * ------- -------------------------------------------
171 * 2-3 Data length (network byte order)
175 if ((buffer
= _cupsBufferGet(_CUPS_SC_MAX_BUFFER
)) == NULL
)
177 *command
= CUPS_SC_CMD_NONE
;
178 *status
= CUPS_SC_STATUS_TOO_BIG
;
183 while ((bytes
= read(CUPS_SC_FD
, buffer
, _CUPS_SC_MAX_BUFFER
)) < 0)
184 if (errno
!= EINTR
&& errno
!= EAGAIN
)
186 DEBUG_printf(("1cupsSideChannelRead: Read error: %s", strerror(errno
)));
188 _cupsBufferRelease(buffer
);
190 *command
= CUPS_SC_CMD_NONE
;
191 *status
= CUPS_SC_STATUS_IO_ERROR
;
197 * Watch for EOF or too few bytes...
202 DEBUG_printf(("1cupsSideChannelRead: Short read of " CUPS_LLFMT
" bytes", CUPS_LLCAST bytes
));
204 _cupsBufferRelease(buffer
);
206 *command
= CUPS_SC_CMD_NONE
;
207 *status
= CUPS_SC_STATUS_BAD_MESSAGE
;
213 * Validate the command code in the message...
216 if (buffer
[0] < CUPS_SC_CMD_SOFT_RESET
||
217 buffer
[0] >= CUPS_SC_CMD_MAX
)
219 DEBUG_printf(("1cupsSideChannelRead: Bad command %d!", buffer
[0]));
221 _cupsBufferRelease(buffer
);
223 *command
= CUPS_SC_CMD_NONE
;
224 *status
= CUPS_SC_STATUS_BAD_MESSAGE
;
229 *command
= (cups_sc_command_t
)buffer
[0];
232 * Validate the data length in the message...
235 templen
= ((buffer
[2] & 255) << 8) | (buffer
[3] & 255);
237 if (templen
> 0 && (!data
|| !datalen
))
240 * Either the response is bigger than the provided buffer or the
241 * response is bigger than we've read...
244 *status
= CUPS_SC_STATUS_TOO_BIG
;
246 else if (!datalen
|| templen
> *datalen
|| templen
> (bytes
- 4))
249 * Either the response is bigger than the provided buffer or the
250 * response is bigger than we've read...
253 *status
= CUPS_SC_STATUS_TOO_BIG
;
258 * The response data will fit, copy it over and provide the actual
262 *status
= (cups_sc_status_t
)buffer
[1];
265 memcpy(data
, buffer
+ 4, (size_t)templen
);
268 _cupsBufferRelease(buffer
);
270 DEBUG_printf(("1cupsSideChannelRead: Returning status=%d", *status
));
277 * 'cupsSideChannelSNMPGet()' - Query a SNMP OID's value.
279 * This function asks the backend to do a SNMP OID query on behalf of the
280 * filter, port monitor, or backend using the default community name.
282 * "oid" contains a numeric OID consisting of integers separated by periods,
283 * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not
284 * supported and must be converted to their numeric forms.
286 * On input, "data" and "datalen" provide the location and size of the
287 * buffer to hold the OID value as a string. HEX-String (binary) values are
288 * converted to hexadecimal strings representing the binary data, while
289 * NULL-Value and unknown OID types are returned as the empty string.
290 * The returned "datalen" does not include the trailing nul.
292 * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not
293 * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
294 * the printer does not respond to the SNMP query.
296 * @since CUPS 1.4/macOS 10.6@
299 cups_sc_status_t
/* O - Query status */
300 cupsSideChannelSNMPGet(
301 const char *oid
, /* I - OID to query */
302 char *data
, /* I - Buffer for OID value */
303 int *datalen
, /* IO - Size of OID buffer on entry, size of value on return */
304 double timeout
) /* I - Timeout in seconds */
306 cups_sc_status_t status
; /* Status of command */
307 cups_sc_command_t rcommand
; /* Response command */
308 char *real_data
; /* Real data buffer for response */
309 int real_datalen
, /* Real length of data buffer */
310 real_oidlen
; /* Length of returned OID string */
313 DEBUG_printf(("cupsSideChannelSNMPGet(oid=\"%s\", data=%p, datalen=%p(%d), "
314 "timeout=%.3f)", oid
, data
, datalen
, datalen
? *datalen
: -1,
318 * Range check input...
321 if (!oid
|| !*oid
|| !data
|| !datalen
|| *datalen
< 2)
322 return (CUPS_SC_STATUS_BAD_MESSAGE
);
327 * Send the request to the backend and wait for a response...
330 if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET
, CUPS_SC_STATUS_NONE
, oid
,
331 (int)strlen(oid
) + 1, timeout
))
332 return (CUPS_SC_STATUS_TIMEOUT
);
334 if ((real_data
= _cupsBufferGet(_CUPS_SC_MAX_BUFFER
)) == NULL
)
335 return (CUPS_SC_STATUS_TOO_BIG
);
337 real_datalen
= _CUPS_SC_MAX_BUFFER
;
338 if (cupsSideChannelRead(&rcommand
, &status
, real_data
, &real_datalen
, timeout
))
340 _cupsBufferRelease(real_data
);
341 return (CUPS_SC_STATUS_TIMEOUT
);
344 if (rcommand
!= CUPS_SC_CMD_SNMP_GET
)
346 _cupsBufferRelease(real_data
);
347 return (CUPS_SC_STATUS_BAD_MESSAGE
);
350 if (status
== CUPS_SC_STATUS_OK
)
353 * Parse the response of the form "oid\0value"...
356 real_oidlen
= (int)strlen(real_data
) + 1;
357 real_datalen
-= real_oidlen
;
359 if ((real_datalen
+ 1) > *datalen
)
361 _cupsBufferRelease(real_data
);
362 return (CUPS_SC_STATUS_TOO_BIG
);
365 memcpy(data
, real_data
+ real_oidlen
, (size_t)real_datalen
);
366 data
[real_datalen
] = '\0';
368 *datalen
= real_datalen
;
371 _cupsBufferRelease(real_data
);
378 * 'cupsSideChannelSNMPWalk()' - Query multiple SNMP OID values.
380 * This function asks the backend to do multiple SNMP OID queries on behalf
381 * of the filter, port monitor, or backend using the default community name.
382 * All OIDs under the "parent" OID are queried and the results are sent to
383 * the callback function you provide.
385 * "oid" contains a numeric OID consisting of integers separated by periods,
386 * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not
387 * supported and must be converted to their numeric forms.
389 * "timeout" specifies the timeout for each OID query. The total amount of
390 * time will depend on the number of OID values found and the time required
393 * "cb" provides a function to call for every value that is found. "context"
394 * is an application-defined pointer that is sent to the callback function
395 * along with the OID and current data. The data passed to the callback is the
396 * same as returned by @link cupsSideChannelSNMPGet@.
398 * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not
399 * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
400 * the printer does not respond to the first SNMP query.
402 * @since CUPS 1.4/macOS 10.6@
405 cups_sc_status_t
/* O - Status of first query of @code CUPS_SC_STATUS_OK@ on success */
406 cupsSideChannelSNMPWalk(
407 const char *oid
, /* I - First numeric OID to query */
408 double timeout
, /* I - Timeout for each query in seconds */
409 cups_sc_walk_func_t cb
, /* I - Function to call with each value */
410 void *context
) /* I - Application-defined pointer to send to callback */
412 cups_sc_status_t status
; /* Status of command */
413 cups_sc_command_t rcommand
; /* Response command */
414 char *real_data
; /* Real data buffer for response */
415 int real_datalen
; /* Real length of data buffer */
416 size_t real_oidlen
, /* Length of returned OID string */
417 oidlen
; /* Length of first OID */
418 const char *current_oid
; /* Current OID */
419 char last_oid
[2048]; /* Last OID */
422 DEBUG_printf(("cupsSideChannelSNMPWalk(oid=\"%s\", timeout=%.3f, cb=%p, "
423 "context=%p)", oid
, timeout
, cb
, context
));
426 * Range check input...
429 if (!oid
|| !*oid
|| !cb
)
430 return (CUPS_SC_STATUS_BAD_MESSAGE
);
432 if ((real_data
= _cupsBufferGet(_CUPS_SC_MAX_BUFFER
)) == NULL
)
433 return (CUPS_SC_STATUS_TOO_BIG
);
436 * Loop until the OIDs don't match...
440 oidlen
= strlen(oid
);
446 * Send the request to the backend and wait for a response...
449 if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET_NEXT
, CUPS_SC_STATUS_NONE
,
450 current_oid
, (int)strlen(current_oid
) + 1, timeout
))
452 _cupsBufferRelease(real_data
);
453 return (CUPS_SC_STATUS_TIMEOUT
);
456 real_datalen
= _CUPS_SC_MAX_BUFFER
;
457 if (cupsSideChannelRead(&rcommand
, &status
, real_data
, &real_datalen
,
460 _cupsBufferRelease(real_data
);
461 return (CUPS_SC_STATUS_TIMEOUT
);
464 if (rcommand
!= CUPS_SC_CMD_SNMP_GET_NEXT
)
466 _cupsBufferRelease(real_data
);
467 return (CUPS_SC_STATUS_BAD_MESSAGE
);
470 if (status
== CUPS_SC_STATUS_OK
)
473 * Parse the response of the form "oid\0value"...
476 if (strncmp(real_data
, oid
, oidlen
) || real_data
[oidlen
] != '.' ||
477 !strcmp(real_data
, last_oid
))
480 * Done with this set of OIDs...
483 _cupsBufferRelease(real_data
);
484 return (CUPS_SC_STATUS_OK
);
487 if ((size_t)real_datalen
< sizeof(real_data
))
488 real_data
[real_datalen
] = '\0';
490 real_oidlen
= strlen(real_data
) + 1;
491 real_datalen
-= (int)real_oidlen
;
494 * Call the callback with the OID and data...
497 (*cb
)(real_data
, real_data
+ real_oidlen
, real_datalen
, context
);
500 * Update the current OID...
503 current_oid
= real_data
;
504 strlcpy(last_oid
, current_oid
, sizeof(last_oid
));
507 while (status
== CUPS_SC_STATUS_OK
);
509 _cupsBufferRelease(real_data
);
516 * 'cupsSideChannelWrite()' - Write a side-channel message.
518 * This function is normally only called by backend programs to send
519 * responses to a filter, driver, or port monitor program.
521 * @since CUPS 1.3/macOS 10.5@
524 int /* O - 0 on success, -1 on error */
525 cupsSideChannelWrite(
526 cups_sc_command_t command
, /* I - Command code */
527 cups_sc_status_t status
, /* I - Status code */
528 const char *data
, /* I - Data buffer pointer */
529 int datalen
, /* I - Number of bytes of data */
530 double timeout
) /* I - Timeout in seconds */
532 char *buffer
; /* Message buffer */
533 ssize_t bytes
; /* Bytes written */
535 struct pollfd pfd
; /* Poll structure for poll() */
537 fd_set output_set
; /* Output set for select() */
538 struct timeval stimeout
; /* Timeout value for select() */
539 #endif /* HAVE_POLL */
543 * Range check input...
546 if (command
< CUPS_SC_CMD_SOFT_RESET
|| command
>= CUPS_SC_CMD_MAX
||
547 datalen
< 0 || datalen
> _CUPS_SC_MAX_DATA
|| (datalen
> 0 && !data
))
551 * See if we can safely write to the side-channel socket...
556 pfd
.events
= POLLOUT
;
560 if (poll(&pfd
, 1, -1) < 1)
563 else if (poll(&pfd
, 1, (int)(timeout
* 1000)) < 1)
567 FD_ZERO(&output_set
);
568 FD_SET(CUPS_SC_FD
, &output_set
);
572 if (select(CUPS_SC_FD
+ 1, NULL
, &output_set
, NULL
, NULL
) < 1)
577 stimeout
.tv_sec
= (int)timeout
;
578 stimeout
.tv_usec
= (int)(timeout
* 1000000) % 1000000;
580 if (select(CUPS_SC_FD
+ 1, NULL
, &output_set
, NULL
, &stimeout
) < 1)
583 #endif /* HAVE_POLL */
586 * Write a side-channel message in the format:
588 * Byte(s) Description
589 * ------- -------------------------------------------
592 * 2-3 Data length (network byte order) <= 16384
596 if ((buffer
= _cupsBufferGet((size_t)datalen
+ 4)) == NULL
)
601 buffer
[2] = (char)(datalen
>> 8);
602 buffer
[3] = (char)(datalen
& 255);
608 memcpy(buffer
+ 4, data
, (size_t)datalen
);
612 while (write(CUPS_SC_FD
, buffer
, (size_t)bytes
) < 0)
613 if (errno
!= EINTR
&& errno
!= EAGAIN
)
615 _cupsBufferRelease(buffer
);
619 _cupsBufferRelease(buffer
);