2 * "$Id: sidechannel.c 7720 2008-07-11 22:46:21Z mike $"
4 * Side-channel API code for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * cupsSideChannelDoRequest() - Send a side-channel command to a backend and
20 * wait for a response.
21 * cupsSideChannelRead() - Read a side-channel message.
22 * cupsSideChannelSNMPGet() - Query a SNMP OID's value.
23 * cupsSideChannelSNMPWalk() - Query multiple SNMP OID values.
24 * cupsSideChannelWrite() - Write a side-channel message.
28 * Include necessary headers...
31 #include "sidechannel.h"
41 # include <sys/time.h>
43 # include <sys/select.h>
46 # include <sys/time.h>
49 # include <sys/poll.h>
50 #endif /* HAVE_POLL */
54 * 'cupsSideChannelDoRequest()' - Send a side-channel command to a backend and wait for a response.
56 * This function is normally only called by filters, drivers, or port
57 * monitors in order to communicate with the backend used by the current
58 * printer. Programs must be prepared to handle timeout or "not
59 * implemented" status codes, which indicate that the backend or device
60 * do not support the specified side-channel command.
62 * The "datalen" parameter must be initialized to the size of the buffer
63 * pointed to by the "data" parameter. cupsSideChannelDoRequest() will
64 * update the value to contain the number of data bytes in the buffer.
66 * @since CUPS 1.3/Mac OS X 10.5@
69 cups_sc_status_t
/* O - Status of command */
70 cupsSideChannelDoRequest(
71 cups_sc_command_t command
, /* I - Command to send */
72 char *data
, /* O - Response data buffer pointer */
73 int *datalen
, /* IO - Size of data buffer on entry, number of bytes in buffer on return */
74 double timeout
) /* I - Timeout in seconds */
76 cups_sc_status_t status
; /* Status of command */
77 cups_sc_command_t rcommand
; /* Response command */
80 if (cupsSideChannelWrite(command
, CUPS_SC_STATUS_NONE
, NULL
, 0, timeout
))
81 return (CUPS_SC_STATUS_TIMEOUT
);
83 if (cupsSideChannelRead(&rcommand
, &status
, data
, datalen
, timeout
))
84 return (CUPS_SC_STATUS_TIMEOUT
);
86 if (rcommand
!= command
)
87 return (CUPS_SC_STATUS_BAD_MESSAGE
);
94 * 'cupsSideChannelRead()' - Read a side-channel message.
96 * This function is normally only called by backend programs to read
97 * commands from a filter, driver, or port monitor program. The
98 * caller must be prepared to handle incomplete or invalid messages
99 * and return the corresponding status codes.
101 * The "datalen" parameter must be initialized to the size of the buffer
102 * pointed to by the "data" parameter. cupsSideChannelDoRequest() will
103 * update the value to contain the number of data bytes in the buffer.
105 * @since CUPS 1.3/Mac OS X 10.5@
108 int /* O - 0 on success, -1 on error */
110 cups_sc_command_t
*command
, /* O - Command code */
111 cups_sc_status_t
*status
, /* O - Status code */
112 char *data
, /* O - Data buffer pointer */
113 int *datalen
, /* IO - Size of data buffer on entry, number of bytes in buffer on return */
114 double timeout
) /* I - Timeout in seconds */
116 char buffer
[16388]; /* Message buffer */
117 int bytes
; /* Bytes read */
118 int templen
; /* Data length from message */
119 int nfds
; /* Number of file descriptors */
121 struct pollfd pfd
; /* Poll structure for poll() */
123 fd_set input_set
; /* Input set for select() */
124 struct timeval stimeout
; /* Timeout value for select() */
125 #endif /* HAVE_POLL */
128 DEBUG_printf(("cupsSideChannelRead(command=%p, status=%p, data=%p, "
129 "datalen=%p(%d), timeout=%.3f)", command
, status
, data
,
130 datalen
, datalen
? *datalen
: -1, timeout
));
133 * Range check input...
136 if (!command
|| !status
)
140 * See if we have pending data on the side-channel socket...
147 while ((nfds
= poll(&pfd
, 1,
148 timeout
< 0.0 ? -1 : (long)(timeout
* 1000))) < 0 &&
149 (errno
== EINTR
|| errno
== EAGAIN
))
154 FD_SET(CUPS_SC_FD
, &input_set
);
156 stimeout
.tv_sec
= (int)timeout
;
157 stimeout
.tv_usec
= (int)(timeout
* 1000000) % 1000000;
159 while ((nfds
= select(CUPS_SC_FD
+ 1, &input_set
, NULL
, NULL
,
160 timeout
< 0.0 ? NULL
: &stimeout
)) < 0 &&
161 (errno
== EINTR
|| errno
== EAGAIN
))
164 #endif /* HAVE_POLL */
168 *status
= nfds
==0 ? CUPS_SC_STATUS_TIMEOUT
: CUPS_SC_STATUS_IO_ERROR
;
173 * Read a side-channel message for the format:
175 * Byte(s) Description
176 * ------- -------------------------------------------
179 * 2-3 Data length (network byte order) <= 16384
183 while ((bytes
= read(CUPS_SC_FD
, buffer
, sizeof(buffer
))) < 0)
184 if (errno
!= EINTR
&& errno
!= EAGAIN
)
186 DEBUG_printf(("1cupsSideChannelRead: Read error: %s", strerror(errno
)));
187 *command
= CUPS_SC_CMD_NONE
;
188 *status
= CUPS_SC_STATUS_IO_ERROR
;
193 * Watch for EOF or too few bytes...
198 DEBUG_printf(("1cupsSideChannelRead: Short read of %d bytes", bytes
));
199 *command
= CUPS_SC_CMD_NONE
;
200 *status
= CUPS_SC_STATUS_BAD_MESSAGE
;
205 * Validate the command code in the message...
208 if (buffer
[0] < CUPS_SC_CMD_SOFT_RESET
||
209 buffer
[0] > CUPS_SC_CMD_SNMP_GET_NEXT
)
211 DEBUG_printf(("1cupsSideChannelRead: Bad command %d!", buffer
[0]));
212 *command
= CUPS_SC_CMD_NONE
;
213 *status
= CUPS_SC_STATUS_BAD_MESSAGE
;
217 *command
= (cups_sc_command_t
)buffer
[0];
220 * Validate the data length in the message...
223 templen
= ((buffer
[2] & 255) << 8) | (buffer
[3] & 255);
225 if (templen
> 0 && (!data
|| !datalen
))
228 * Either the response is bigger than the provided buffer or the
229 * response is bigger than we've read...
232 *status
= CUPS_SC_STATUS_TOO_BIG
;
234 else if (!datalen
|| templen
> *datalen
|| templen
> (bytes
- 4))
237 * Either the response is bigger than the provided buffer or the
238 * response is bigger than we've read...
241 *status
= CUPS_SC_STATUS_TOO_BIG
;
246 * The response data will fit, copy it over and provide the actual
250 *status
= (cups_sc_status_t
)buffer
[1];
253 memcpy(data
, buffer
+ 4, templen
);
256 DEBUG_printf(("1cupsSideChannelRead: Returning status=%d", *status
));
263 * 'cupsSideChannelSNMPGet()' - Query a SNMP OID's value.
265 * This function asks the backend to do a SNMP OID query on behalf of the
266 * filter, port monitor, or backend using the default community name.
268 * "oid" contains a numeric OID consisting of integers separated by periods,
269 * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not
270 * supported and must be converted to their numeric forms.
272 * On input, "data" and "datalen" provide the location and size of the
273 * buffer to hold the OID value as a string. HEX-String (binary) values are
274 * converted to hexadecimal strings representing the binary data, while
275 * NULL-Value and unknown OID types are returned as the empty string.
276 * The returned "datalen" does not include the trailing nul.
278 * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not
279 * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
280 * the printer does not respond to the SNMP query.
282 * @since CUPS 1.4/Mac OS X 10.6@
285 cups_sc_status_t
/* O - Query status */
286 cupsSideChannelSNMPGet(
287 const char *oid
, /* I - OID to query */
288 char *data
, /* I - Buffer for OID value */
289 int *datalen
, /* IO - Size of OID buffer on entry, size of value on return */
290 double timeout
) /* I - Timeout in seconds */
292 cups_sc_status_t status
; /* Status of command */
293 cups_sc_command_t rcommand
; /* Response command */
294 char real_data
[2048];/* Real data buffer for response */
295 int real_datalen
, /* Real length of data buffer */
296 real_oidlen
; /* Length of returned OID string */
299 DEBUG_printf(("cupsSideChannelSNMPGet(oid=\"%s\", data=%p, datalen=%p(%d), "
300 "timeout=%.3f)", oid
, data
, datalen
, datalen
? *datalen
: -1,
304 * Range check input...
307 if (!oid
|| !*oid
|| !data
|| !datalen
|| *datalen
< 2)
308 return (CUPS_SC_STATUS_BAD_MESSAGE
);
313 * Send the request to the backend and wait for a response...
316 if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET
, CUPS_SC_STATUS_NONE
, oid
,
317 (int)strlen(oid
) + 1, timeout
))
318 return (CUPS_SC_STATUS_TIMEOUT
);
320 real_datalen
= sizeof(real_data
);
321 if (cupsSideChannelRead(&rcommand
, &status
, real_data
, &real_datalen
, timeout
))
322 return (CUPS_SC_STATUS_TIMEOUT
);
324 if (rcommand
!= CUPS_SC_CMD_SNMP_GET
)
325 return (CUPS_SC_STATUS_BAD_MESSAGE
);
327 if (status
== CUPS_SC_STATUS_OK
)
330 * Parse the response of the form "oid\0value"...
333 real_oidlen
= strlen(real_data
) + 1;
334 real_datalen
-= real_oidlen
;
336 if ((real_datalen
+ 1) > *datalen
)
337 return (CUPS_SC_STATUS_TOO_BIG
);
339 memcpy(data
, real_data
+ real_oidlen
, real_datalen
);
340 data
[real_datalen
] = '\0';
342 *datalen
= real_datalen
;
350 * 'cupsSideChannelSNMPWalk()' - Query multiple SNMP OID values.
352 * This function asks the backend to do multiple SNMP OID queries on behalf
353 * of the filter, port monitor, or backend using the default community name.
354 * All OIDs under the "parent" OID are queried and the results are sent to
355 * the callback function you provide.
357 * "oid" contains a numeric OID consisting of integers separated by periods,
358 * for example ".1.3.6.1.2.1.43". Symbolic names from SNMP MIBs are not
359 * supported and must be converted to their numeric forms.
361 * "timeout" specifies the timeout for each OID query. The total amount of
362 * time will depend on the number of OID values found and the time required
365 * "cb" provides a function to call for every value that is found. "context"
366 * is an application-defined pointer that is sent to the callback function
367 * along with the OID and current data. The data passed to the callback is the
368 * same as returned by @link cupsSideChannelSNMPGet@.
370 * @code CUPS_SC_STATUS_NOT_IMPLEMENTED@ is returned by backends that do not
371 * support SNMP queries. @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
372 * the printer does not respond to the first SNMP query.
374 * @since CUPS 1.4/Mac OS X 10.6@
377 cups_sc_status_t
/* O - Status of first query of @code CUPS_SC_STATUS_OK@ on success */
378 cupsSideChannelSNMPWalk(
379 const char *oid
, /* I - First numeric OID to query */
380 double timeout
, /* I - Timeout for each query in seconds */
381 cups_sc_walk_func_t cb
, /* I - Function to call with each value */
382 void *context
) /* I - Application-defined pointer to send to callback */
384 cups_sc_status_t status
; /* Status of command */
385 cups_sc_command_t rcommand
; /* Response command */
386 char real_data
[2048];/* Real data buffer for response */
387 int real_datalen
, /* Real length of data buffer */
388 real_oidlen
, /* Length of returned OID string */
389 oidlen
; /* Length of first OID */
390 const char *current_oid
; /* Current OID */
391 char last_oid
[2048]; /* Last OID */
394 DEBUG_printf(("cupsSideChannelSNMPWalk(oid=\"%s\", timeout=%.3f, cb=%p, "
395 "context=%p)", oid
, timeout
, cb
, context
));
398 * Range check input...
401 if (!oid
|| !*oid
|| !cb
)
402 return (CUPS_SC_STATUS_BAD_MESSAGE
);
405 * Loop until the OIDs don't match...
409 oidlen
= (int)strlen(oid
);
415 * Send the request to the backend and wait for a response...
418 if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET_NEXT
, CUPS_SC_STATUS_NONE
,
419 current_oid
, (int)strlen(current_oid
) + 1, timeout
))
420 return (CUPS_SC_STATUS_TIMEOUT
);
422 real_datalen
= sizeof(real_data
);
423 if (cupsSideChannelRead(&rcommand
, &status
, real_data
, &real_datalen
,
425 return (CUPS_SC_STATUS_TIMEOUT
);
427 if (rcommand
!= CUPS_SC_CMD_SNMP_GET_NEXT
)
428 return (CUPS_SC_STATUS_BAD_MESSAGE
);
430 if (status
== CUPS_SC_STATUS_OK
)
433 * Parse the response of the form "oid\0value"...
436 if (strncmp(real_data
, oid
, oidlen
) || real_data
[oidlen
] != '.' ||
437 !strcmp(real_data
, last_oid
))
440 * Done with this set of OIDs...
443 return (CUPS_SC_STATUS_OK
);
446 if (real_datalen
< sizeof(real_data
))
447 real_data
[real_datalen
] = '\0';
449 real_oidlen
= strlen(real_data
) + 1;
450 real_datalen
-= real_oidlen
;
453 * Call the callback with the OID and data...
456 (*cb
)(real_data
, real_data
+ real_oidlen
, real_datalen
, context
);
459 * Update the current OID...
462 current_oid
= real_data
;
463 strlcpy(last_oid
, current_oid
, sizeof(last_oid
));
466 while (status
== CUPS_SC_STATUS_OK
);
473 * 'cupsSideChannelWrite()' - Write a side-channel message.
475 * This function is normally only called by backend programs to send
476 * responses to a filter, driver, or port monitor program.
478 * @since CUPS 1.3/Mac OS X 10.5@
481 int /* O - 0 on success, -1 on error */
482 cupsSideChannelWrite(
483 cups_sc_command_t command
, /* I - Command code */
484 cups_sc_status_t status
, /* I - Status code */
485 const char *data
, /* I - Data buffer pointer */
486 int datalen
, /* I - Number of bytes of data */
487 double timeout
) /* I - Timeout in seconds */
489 char buffer
[16388]; /* Message buffer */
490 int bytes
; /* Bytes written */
492 struct pollfd pfd
; /* Poll structure for poll() */
494 fd_set output_set
; /* Output set for select() */
495 struct timeval stimeout
; /* Timeout value for select() */
496 #endif /* HAVE_POLL */
500 * Range check input...
503 if (command
< CUPS_SC_CMD_SOFT_RESET
|| command
> CUPS_SC_CMD_SNMP_GET_NEXT
||
504 datalen
< 0 || datalen
> 16384 || (datalen
> 0 && !data
))
508 * See if we can safely write to the side-channel socket...
513 pfd
.events
= POLLOUT
;
517 if (poll(&pfd
, 1, -1) < 1)
520 else if (poll(&pfd
, 1, (long)(timeout
* 1000)) < 1)
524 FD_ZERO(&output_set
);
525 FD_SET(CUPS_SC_FD
, &output_set
);
529 if (select(CUPS_SC_FD
+ 1, NULL
, &output_set
, NULL
, NULL
) < 1)
534 stimeout
.tv_sec
= (int)timeout
;
535 stimeout
.tv_usec
= (int)(timeout
* 1000000) % 1000000;
537 if (select(CUPS_SC_FD
+ 1, NULL
, &output_set
, NULL
, &stimeout
) < 1)
540 #endif /* HAVE_POLL */
543 * Write a side-channel message in the format:
545 * Byte(s) Description
546 * ------- -------------------------------------------
549 * 2-3 Data length (network byte order) <= 16384
555 buffer
[2] = datalen
>> 8;
556 buffer
[3] = datalen
& 255;
562 memcpy(buffer
+ 4, data
, datalen
);
566 while (write(CUPS_SC_FD
, buffer
, bytes
) < 0)
567 if (errno
!= EINTR
&& errno
!= EAGAIN
)
575 * End of "$Id: sidechannel.c 7720 2008-07-11 22:46:21Z mike $".