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
:
105 status
= CUPS_SC_STATUS_OK
;
110 case CUPS_SC_CMD_SNMP_GET
:
111 case CUPS_SC_CMD_SNMP_GET_NEXT
:
112 fprintf(stderr
, "DEBUG: CUPS_SC_CMD_SNMP_%s: %d (%s)\n",
113 command
== CUPS_SC_CMD_SNMP_GET
? "GET" : "GET_NEXT", datalen
,
118 status
= CUPS_SC_STATUS_BAD_MESSAGE
;
125 cups_snmp_t packet
; /* Packet from printer */
128 if (!_cupsSNMPStringToOID(data
, packet
.object_name
, CUPS_SNMP_MAX_OID
))
130 status
= CUPS_SC_STATUS_BAD_MESSAGE
;
135 status
= CUPS_SC_STATUS_IO_ERROR
;
138 if (_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
139 _cupsSNMPDefaultCommunity(),
140 command
== CUPS_SC_CMD_SNMP_GET
?
141 CUPS_ASN1_GET_REQUEST
:
142 CUPS_ASN1_GET_NEXT_REQUEST
, 1,
145 if (_cupsSNMPRead(snmp_fd
, &packet
, 1.0))
147 char *dataptr
; /* Pointer into data */
148 int i
; /* Looping var */
151 if (!_cupsSNMPOIDToString(packet
.object_name
, data
, sizeof(data
)))
153 fputs("DEBUG: Bad OID returned!\n", stderr
);
157 datalen
= (int)strlen(data
) + 1;
158 dataptr
= data
+ datalen
;
160 switch (packet
.object_type
)
162 case CUPS_ASN1_BOOLEAN
:
163 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%d",
164 packet
.object_value
.boolean
);
167 case CUPS_ASN1_INTEGER
:
168 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%d",
169 packet
.object_value
.integer
);
172 case CUPS_ASN1_BIT_STRING
:
173 case CUPS_ASN1_OCTET_STRING
:
174 strlcpy(dataptr
, packet
.object_value
.string
,
175 sizeof(data
) - (dataptr
- data
));
179 _cupsSNMPOIDToString(packet
.object_value
.oid
, dataptr
,
180 sizeof(data
) - (dataptr
- data
));
183 case CUPS_ASN1_HEX_STRING
:
185 i
< packet
.object_value
.hex_string
.num_bytes
&&
186 dataptr
< (data
+ sizeof(data
) - 3);
188 sprintf(dataptr
, "%02X",
189 packet
.object_value
.hex_string
.bytes
[i
]);
192 case CUPS_ASN1_COUNTER
:
193 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%d",
194 packet
.object_value
.counter
);
197 case CUPS_ASN1_GAUGE
:
198 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%u",
199 packet
.object_value
.gauge
);
202 case CUPS_ASN1_TIMETICKS
:
203 snprintf(dataptr
, sizeof(data
) - (dataptr
- data
), "%u",
204 packet
.object_value
.timeticks
);
208 fprintf(stderr
, "DEBUG: Unknown OID value type %02X!\n",
213 fprintf(stderr
, "DEBUG: Returning %s %s\n", data
, data
+ datalen
);
215 status
= CUPS_SC_STATUS_OK
;
216 datalen
+= (int)strlen(data
+ datalen
);
219 fputs("DEBUG: SNMP read error...\n", stderr
);
222 fputs("DEBUG: SNMP write error...\n", stderr
);
226 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
230 case CUPS_SC_CMD_GET_DEVICE_ID
:
233 cups_snmp_t packet
; /* Packet from printer */
234 static const int ppmPrinterIEEE1284DeviceId
[] =
235 { CUPS_OID_ppmPrinterIEEE1284DeviceId
,1,-1 };
238 status
= CUPS_SC_STATUS_IO_ERROR
;
241 if (_cupsSNMPWrite(snmp_fd
, addr
, CUPS_SNMP_VERSION_1
,
242 _cupsSNMPDefaultCommunity(),
243 CUPS_ASN1_GET_REQUEST
, 1,
244 ppmPrinterIEEE1284DeviceId
))
246 if (_cupsSNMPRead(snmp_fd
, &packet
, 1.0) &&
247 packet
.object_type
== CUPS_ASN1_OCTET_STRING
)
249 strlcpy(data
, packet
.object_value
.string
, sizeof(data
));
250 datalen
= (int)strlen(data
);
251 status
= CUPS_SC_STATUS_OK
;
258 if ((device_id
= getenv("1284DEVICEID")) != NULL
)
260 strlcpy(data
, device_id
, sizeof(data
));
261 datalen
= (int)strlen(data
);
266 status
= CUPS_SC_STATUS_NOT_IMPLEMENTED
;
271 cupsSideChannelWrite(command
, status
, data
, datalen
, 1.0);