]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/network.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / backend / network.c
CommitLineData
7a14d768
MS
1/*
2 * "$Id$"
3 *
4 * Common network APIs for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
7a14d768
MS
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
a0f6818e
MS
19 * backendCheckSideChannel() - Check the side-channel for pending requests.
20 * backendNetworkSideCB() - Handle common network side-channel commands.
7a14d768
MS
21 */
22
23/*
24 * Include necessary headers.
25 */
26
27#include "backend-private.h"
28#include <limits.h>
29#ifdef __hpux
30# include <sys/time.h>
31#else
32# include <sys/select.h>
33#endif /* __hpux */
7a14d768
MS
34
35
7a14d768
MS
36/*
37 * 'backendCheckSideChannel()' - Check the side-channel for pending requests.
38 */
39
40
41void
42backendCheckSideChannel(
43 int snmp_fd, /* I - SNMP socket */
44 http_addr_t *addr) /* I - Address of device */
45{
46 fd_set input; /* Select input set */
47 struct timeval timeout; /* Select timeout */
48
49
50 FD_ZERO(&input);
51 FD_SET(CUPS_SC_FD, &input);
52
53 timeout.tv_sec = timeout.tv_usec = 0;
54
55 if (select(CUPS_SC_FD + 1, &input, NULL, NULL, &timeout) > 0)
56 backendNetworkSideCB(-1, -1, snmp_fd, addr, 0);
57}
58
59
60/*
61 * 'backendNetworkSideCB()' - Handle common network side-channel commands.
62 */
63
64void
65backendNetworkSideCB(
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? */
71{
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 */
77
78
79 datalen = sizeof(data);
80
81 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
82 {
83 _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
84 return;
85 }
86
87 switch (command)
88 {
89 case CUPS_SC_CMD_DRAIN_OUTPUT :
90 /*
91 * Our sockets disable the Nagle algorithm and data is sent immediately.
92 */
93
94 if (device_fd < 0)
95 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
96 else if (backendDrainOutput(print_fd, device_fd))
97 status = CUPS_SC_STATUS_IO_ERROR;
98 else
99 status = CUPS_SC_STATUS_OK;
100
101 datalen = 0;
102 break;
103
104 case CUPS_SC_CMD_GET_BIDI :
105 data[0] = use_bc;
106 datalen = 1;
107 break;
108
20fbc903
MS
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,
113 data);
114
115 if (datalen < 2)
116 {
117 status = CUPS_SC_STATUS_BAD_MESSAGE;
118 datalen = 0;
119 break;
120 }
121
122 if (snmp_fd >= 0)
123 {
124 cups_snmp_t packet; /* Packet from printer */
125
126
127 if (!_cupsSNMPStringToOID(data, packet.object_name, CUPS_SNMP_MAX_OID))
128 {
129 status = CUPS_SC_STATUS_BAD_MESSAGE;
130 datalen = 0;
131 break;
132 }
133
134 status = CUPS_SC_STATUS_IO_ERROR;
135 datalen = 0;
136
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,
142 packet.object_name))
143 {
144 if (_cupsSNMPRead(snmp_fd, &packet, 1.0))
145 {
146 char *dataptr; /* Pointer into data */
147 int i; /* Looping var */
148
149
150 if (!_cupsSNMPOIDToString(packet.object_name, data, sizeof(data)))
151 {
152 fputs("DEBUG: Bad OID returned!\n", stderr);
153 break;
154 }
155
156 datalen = (int)strlen(data) + 1;
157 dataptr = data + datalen;
158
159 switch (packet.object_type)
160 {
161 case CUPS_ASN1_BOOLEAN :
162 snprintf(dataptr, sizeof(data) - (dataptr - data), "%d",
163 packet.object_value.boolean);
164 break;
165
166 case CUPS_ASN1_INTEGER :
167 snprintf(dataptr, sizeof(data) - (dataptr - data), "%d",
168 packet.object_value.integer);
169 break;
170
171 case CUPS_ASN1_BIT_STRING :
172 case CUPS_ASN1_OCTET_STRING :
173 strlcpy(dataptr, packet.object_value.string,
174 sizeof(data) - (dataptr - data));
175 break;
176
177 case CUPS_ASN1_OID :
178 _cupsSNMPOIDToString(packet.object_value.oid, dataptr,
179 sizeof(data) - (dataptr - data));
180 break;
181
182 case CUPS_ASN1_HEX_STRING :
183 for (i = 0;
184 i < packet.object_value.hex_string.num_bytes &&
185 dataptr < (data + sizeof(data) - 3);
186 i ++, dataptr += 2)
187 sprintf(dataptr, "%02X",
188 packet.object_value.hex_string.bytes[i]);
189 break;
190
191 case CUPS_ASN1_COUNTER :
192 snprintf(dataptr, sizeof(data) - (dataptr - data), "%d",
193 packet.object_value.counter);
194 break;
195
196 case CUPS_ASN1_GAUGE :
197 snprintf(dataptr, sizeof(data) - (dataptr - data), "%u",
198 packet.object_value.gauge);
199 break;
200
201 case CUPS_ASN1_TIMETICKS :
202 snprintf(dataptr, sizeof(data) - (dataptr - data), "%u",
203 packet.object_value.timeticks);
204 break;
205
206 default :
207 fprintf(stderr, "DEBUG: Unknown OID value type %02X!\n",
208 packet.object_type);
209 break;
210 }
211
212 fprintf(stderr, "DEBUG: Returning %s %s\n", data, data + datalen);
213
214 status = CUPS_SC_STATUS_OK;
215 datalen += (int)strlen(data + datalen);
216 }
217 else
218 fputs("DEBUG: SNMP read error...\n", stderr);
219 }
220 else
221 fputs("DEBUG: SNMP write error...\n", stderr);
222 break;
223 }
224
225 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
226 datalen = 0;
227 break;
228
7a14d768
MS
229 case CUPS_SC_CMD_GET_DEVICE_ID :
230 if (snmp_fd >= 0)
231 {
232 cups_snmp_t packet; /* Packet from printer */
233 static const int ppmPrinterIEEE1284DeviceId[] =
234 { CUPS_OID_ppmPrinterIEEE1284DeviceId,1,-1 };
235
20fbc903
MS
236
237 status = CUPS_SC_STATUS_IO_ERROR;
238 datalen = 0;
239
240 if (_cupsSNMPWrite(snmp_fd, addr, CUPS_SNMP_VERSION_1,
241 _cupsSNMPDefaultCommunity(),
7a14d768
MS
242 CUPS_ASN1_GET_REQUEST, 1,
243 ppmPrinterIEEE1284DeviceId))
244 {
245 if (_cupsSNMPRead(snmp_fd, &packet, 1.0) &&
246 packet.object_type == CUPS_ASN1_OCTET_STRING)
247 {
248 strlcpy(data, packet.object_value.string, sizeof(data));
249 datalen = (int)strlen(data);
20fbc903 250 status = CUPS_SC_STATUS_OK;
7a14d768
MS
251 }
252 }
20fbc903
MS
253
254 break;
7a14d768
MS
255 }
256
257 if ((device_id = getenv("1284DEVICEID")) != NULL)
258 {
259 strlcpy(data, device_id, sizeof(data));
260 datalen = (int)strlen(data);
261 break;
262 }
263
264 default :
265 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
266 datalen = 0;
267 break;
268 }
269
270 cupsSideChannelWrite(command, status, data, datalen, 1.0);
271}
272
273
7a14d768
MS
274/*
275 * End of "$Id$".
276 */