4 * Common network APIs for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
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 * "LICENSE" which should have been included with this file. If this
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 * backendCheckSideChannel() - Check the side-channel for pending requests.
20 * backendNetworkSideCB() - Handle common network side-channel commands.
24 * Include necessary headers.
27 #include "backend-private.h"
30 # include <sys/time.h>
32 # include <sys/select.h>
37 * 'backendCheckSideChannel()' - Check the side-channel for pending requests.
42 backendCheckSideChannel(
43 int snmp_fd
, /* I - SNMP socket */
44 http_addr_t
*addr
) /* I - Address of device */
46 fd_set input
; /* Select input set */
47 struct timeval timeout
; /* Select timeout */
51 FD_SET(CUPS_SC_FD
, &input
);
53 timeout
.tv_sec
= timeout
.tv_usec
= 0;
55 if (select(CUPS_SC_FD
+ 1, &input
, NULL
, NULL
, &timeout
) > 0)
56 backendNetworkSideCB(-1, -1, snmp_fd
, addr
, 0);
61 * 'backendNetworkSideCB()' - Handle common network side-channel commands.
66 int print_fd
, /* I - Print file or -1 */
67 int device_fd
, /* I - Device file or -1 */
68 int snmp_fd
, /* I - SNMP socket */
69 http_addr_t
*addr
, /* I - Address of device */
70 int use_bc
) /* I - Use back-channel data? */
72 cups_sc_command_t command
; /* Request command */
73 cups_sc_status_t status
; /* Request/response status */
74 char data
[2048]; /* Request/response data */
75 int datalen
; /* Request/response data size */
76 const char *device_id
; /* 1284DEVICEID env var */
79 datalen
= sizeof(data
);
81 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
83 _cupsLangPuts(stderr
, _("WARNING: Failed to read side-channel request!\n"));
89 case CUPS_SC_CMD_DRAIN_OUTPUT
:
91 * Our sockets disable the Nagle algorithm and data is sent immediately.
95 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
96 else if (backendDrainOutput(print_fd
, device_fd
))
97 status
= CUPS_SC_STATUS_IO_ERROR
;
99 status
= CUPS_SC_STATUS_OK
;
104 case CUPS_SC_CMD_GET_BIDI
:
109 case CUPS_SC_CMD_SNMP_GET
:
110 case CUPS_SC_CMD_SNMP_GET_NEXT
:
111 fprintf(stderr
, "DEBUG: CUPS_SC_CMD_SNMP_%s: %d (%s)\n",
112 command
== CUPS_SC_CMD_SNMP_GET
? "GET" : "GET_NEXT", datalen
,
117 status
= CUPS_SC_STATUS_BAD_MESSAGE
;
124 cups_snmp_t packet
; /* Packet from printer */
127 if (!_cupsSNMPStringToOID(data
, packet
.object_name
, CUPS_SNMP_MAX_OID
))
129 status
= CUPS_SC_STATUS_BAD_MESSAGE
;
134 status
= CUPS_SC_STATUS_IO_ERROR
;
137 if (_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
138 _cupsSNMPDefaultCommunity(),
139 command
== CUPS_SC_CMD_SNMP_GET
?
140 CUPS_ASN1_GET_REQUEST
:
141 CUPS_ASN1_GET_NEXT_REQUEST
, 1,
144 if (_cupsSNMPRead(snmp_fd
, &packet
, 1.0))
146 char *dataptr
; /* Pointer into data */
147 int i
; /* Looping var */
150 if (!_cupsSNMPOIDToString(packet
.object_name
, data
, sizeof(data
)))
152 fputs("DEBUG: Bad OID returned!\n", stderr
);
156 datalen
= (int)strlen(data
) + 1;
157 dataptr
= data
+ datalen
;
159 switch (packet
.object_type
)
161 case CUPS_ASN1_BOOLEAN
:
162 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%d",
163 packet
.object_value
.boolean
);
166 case CUPS_ASN1_INTEGER
:
167 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%d",
168 packet
.object_value
.integer
);
171 case CUPS_ASN1_BIT_STRING
:
172 case CUPS_ASN1_OCTET_STRING
:
173 strlcpy(dataptr
, packet
.object_value
.string
,
174 sizeof(data
) - (dataptr
- data
));
178 _cupsSNMPOIDToString(packet
.object_value
.oid
, dataptr
,
179 sizeof(data
) - (dataptr
- data
));
182 case CUPS_ASN1_HEX_STRING
:
184 i
< packet
.object_value
.hex_string
.num_bytes
&&
185 dataptr
< (data
+ sizeof(data
) - 3);
187 sprintf(dataptr
, "%02X",
188 packet
.object_value
.hex_string
.bytes
[i
]);
191 case CUPS_ASN1_COUNTER
:
192 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%d",
193 packet
.object_value
.counter
);
196 case CUPS_ASN1_GAUGE
:
197 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%u",
198 packet
.object_value
.gauge
);
201 case CUPS_ASN1_TIMETICKS
:
202 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%u",
203 packet
.object_value
.timeticks
);
207 fprintf(stderr
, "DEBUG: Unknown OID value type %02X!\n",
212 fprintf(stderr
, "DEBUG: Returning %s %s\n", data
, data
+ datalen
);
214 status
= CUPS_SC_STATUS_OK
;
215 datalen
+= (int)strlen(data
+ datalen
);
218 fputs("DEBUG: SNMP read error...\n", stderr
);
221 fputs("DEBUG: SNMP write error...\n", stderr
);
225 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
229 case CUPS_SC_CMD_GET_DEVICE_ID
:
232 cups_snmp_t packet
; /* Packet from printer */
233 static const int ppmPrinterIEEE1284DeviceId
[] =
234 { CUPS_OID_ppmPrinterIEEE1284DeviceId
,1,-1 };
237 status
= CUPS_SC_STATUS_IO_ERROR
;
240 if (_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
241 _cupsSNMPDefaultCommunity(),
242 CUPS_ASN1_GET_REQUEST
, 1,
243 ppmPrinterIEEE1284DeviceId
))
245 if (_cupsSNMPRead(snmp_fd
, &packet
, 1.0) &&
246 packet
.object_type
== CUPS_ASN1_OCTET_STRING
)
248 strlcpy(data
, packet
.object_value
.string
, sizeof(data
));
249 datalen
= (int)strlen(data
);
250 status
= CUPS_SC_STATUS_OK
;
257 if ((device_id
= getenv("1284DEVICEID")) != NULL
)
259 strlcpy(data
, device_id
, sizeof(data
));
260 datalen
= (int)strlen(data
);
265 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
270 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);