]>
Commit | Line | Data |
---|---|---|
91c84a35 MS |
1 | /* |
2 | * "$Id$" | |
3 | * | |
71e16022 | 4 | * SNMP functions for CUPS. |
91c84a35 | 5 | * |
41681883 | 6 | * Copyright 2007-2010 by Apple Inc. |
91c84a35 MS |
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 | |
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 | * | |
7a14d768 MS |
19 | * _cupsSNMPClose() - Close a SNMP socket. |
20 | * _cupsSNMPCopyOID() - Copy an OID. | |
21 | * _cupsSNMPDefaultCommunity() - Get the default SNMP community name. | |
22 | * _cupsSNMPIsOID() - Test whether a SNMP response contains the | |
23 | * specified OID. | |
24 | * _cupsSNMPIsOIDPrefixed() - Test whether a SNMP response uses the | |
25 | * specified OID prefix. | |
20fbc903 | 26 | * _cupsSNMPOIDToString() - Convert an OID to a string. |
7a14d768 MS |
27 | * _cupsSNMPOpen() - Open a SNMP socket. |
28 | * _cupsSNMPRead() - Read and parse a SNMP response. | |
29 | * _cupsSNMPSetDebug() - Enable/disable debug logging to stderr. | |
20fbc903 | 30 | * _cupsSNMPStringToOID() - Convert a numeric OID string to an OID array. |
7a14d768 MS |
31 | * _cupsSNMPWalk() - Enumerate a group of OIDs. |
32 | * _cupsSNMPWrite() - Send an SNMP query packet. | |
33 | * asn1_debug() - Decode an ASN1-encoded message. | |
34 | * asn1_decode_snmp() - Decode a SNMP packet. | |
35 | * asn1_encode_snmp() - Encode a SNMP packet. | |
36 | * asn1_get_integer() - Get an integer value. | |
37 | * asn1_get_length() - Get a value length. | |
38 | * asn1_get_oid() - Get an OID value. | |
39 | * asn1_get_packed() - Get a packed integer value. | |
40 | * asn1_get_string() - Get a string value. | |
41 | * asn1_get_type() - Get a value type. | |
42 | * asn1_set_integer() - Set an integer value. | |
43 | * asn1_set_length() - Set a value length. | |
44 | * asn1_set_oid() - Set an OID value. | |
45 | * asn1_set_packed() - Set a packed integer value. | |
46 | * asn1_size_integer() - Figure out the number of bytes needed for an | |
47 | * integer value. | |
48 | * asn1_size_length() - Figure out the number of bytes needed for a | |
49 | * length value. | |
50 | * asn1_size_oid() - Figure out the numebr of bytes needed for an | |
51 | * OID value. | |
52 | * asn1_size_packed() - Figure out the number of bytes needed for a | |
53 | * packed integer value. | |
54 | * snmp_set_error() - Set the localized error for a packet. | |
91c84a35 MS |
55 | */ |
56 | ||
57 | /* | |
58 | * Include necessary headers. | |
59 | */ | |
60 | ||
71e16022 | 61 | #include "cups-private.h" |
7a14d768 | 62 | #include "snmp-private.h" |
91c84a35 MS |
63 | #ifdef HAVE_POLL |
64 | # include <sys/poll.h> | |
65 | #endif /* HAVE_POLL */ | |
66 | ||
67 | ||
68 | /* | |
69 | * Local functions... | |
70 | */ | |
71 | ||
91c84a35 MS |
72 | static void asn1_debug(const char *prefix, unsigned char *buffer, |
73 | size_t len, int indent); | |
ac884b6a MS |
74 | static int asn1_decode_snmp(unsigned char *buffer, size_t len, |
75 | cups_snmp_t *packet); | |
91c84a35 MS |
76 | static int asn1_encode_snmp(unsigned char *buffer, size_t len, |
77 | cups_snmp_t *packet); | |
78 | static int asn1_get_integer(unsigned char **buffer, | |
79 | unsigned char *bufend, | |
80 | int length); | |
81 | static int asn1_get_oid(unsigned char **buffer, | |
82 | unsigned char *bufend, | |
83 | int length, int *oid, int oidsize); | |
84 | static int asn1_get_packed(unsigned char **buffer, | |
85 | unsigned char *bufend); | |
86 | static char *asn1_get_string(unsigned char **buffer, | |
87 | unsigned char *bufend, | |
88 | int length, char *string, | |
89 | int strsize); | |
41681883 | 90 | static unsigned asn1_get_length(unsigned char **buffer, |
91c84a35 MS |
91 | unsigned char *bufend); |
92 | static int asn1_get_type(unsigned char **buffer, | |
93 | unsigned char *bufend); | |
94 | static void asn1_set_integer(unsigned char **buffer, | |
95 | int integer); | |
96 | static void asn1_set_length(unsigned char **buffer, | |
41681883 | 97 | unsigned length); |
91c84a35 MS |
98 | static void asn1_set_oid(unsigned char **buffer, |
99 | const int *oid); | |
100 | static void asn1_set_packed(unsigned char **buffer, | |
101 | int integer); | |
102 | static int asn1_size_integer(int integer); | |
103 | static int asn1_size_length(int length); | |
104 | static int asn1_size_oid(const int *oid); | |
105 | static int asn1_size_packed(int integer); | |
106 | static void snmp_set_error(cups_snmp_t *packet, | |
107 | const char *message); | |
108 | ||
109 | ||
110 | /* | |
7a14d768 | 111 | * '_cupsSNMPClose()' - Close a SNMP socket. |
91c84a35 MS |
112 | */ |
113 | ||
114 | void | |
7a14d768 | 115 | _cupsSNMPClose(int fd) /* I - SNMP socket file descriptor */ |
91c84a35 | 116 | { |
e07d4801 | 117 | DEBUG_printf(("4_cupsSNMPClose(fd=%d)", fd)); |
969307f0 | 118 | |
91c84a35 MS |
119 | #ifdef WIN32 |
120 | closesocket(fd); | |
121 | #else | |
122 | close(fd); | |
123 | #endif /* WIN32 */ | |
124 | } | |
125 | ||
126 | ||
127 | /* | |
7a14d768 | 128 | * '_cupsSNMPCopyOID()' - Copy an OID. |
ac884b6a MS |
129 | * |
130 | * The array pointed to by "src" is terminated by the value -1. | |
91c84a35 MS |
131 | */ |
132 | ||
133 | int * /* O - New OID */ | |
7a14d768 MS |
134 | _cupsSNMPCopyOID(int *dst, /* I - Destination OID */ |
135 | const int *src, /* I - Source OID */ | |
136 | int dstsize) /* I - Number of integers in dst */ | |
91c84a35 MS |
137 | { |
138 | int i; /* Looping var */ | |
139 | ||
140 | ||
e07d4801 | 141 | DEBUG_printf(("4_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)", dst, src, |
969307f0 MS |
142 | dstsize)); |
143 | ||
ac884b6a | 144 | for (i = 0, dstsize --; src[i] >= 0 && i < dstsize; i ++) |
91c84a35 MS |
145 | dst[i] = src[i]; |
146 | ||
ac884b6a | 147 | dst[i] = -1; |
91c84a35 MS |
148 | |
149 | return (dst); | |
150 | } | |
151 | ||
152 | ||
ac884b6a | 153 | /* |
7a14d768 | 154 | * '_cupsSNMPDefaultCommunity()' - Get the default SNMP community name. |
ac884b6a MS |
155 | * |
156 | * The default community name is the first community name found in the | |
157 | * snmp.conf file. If no community name is defined there, "public" is used. | |
ac884b6a MS |
158 | */ |
159 | ||
160 | const char * /* O - Default community name */ | |
7a14d768 | 161 | _cupsSNMPDefaultCommunity(void) |
ac884b6a MS |
162 | { |
163 | cups_file_t *fp; /* snmp.conf file */ | |
164 | char line[1024], /* Line from file */ | |
165 | *value; /* Value from file */ | |
166 | int linenum; /* Line number in file */ | |
167 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
168 | ||
169 | ||
e07d4801 | 170 | DEBUG_puts("4_cupsSNMPDefaultCommunity()"); |
969307f0 | 171 | |
ac884b6a MS |
172 | if (!cg->snmp_community[0]) |
173 | { | |
174 | strlcpy(cg->snmp_community, "public", sizeof(cg->snmp_community)); | |
175 | ||
176 | snprintf(line, sizeof(line), "%s/snmp.conf", cg->cups_serverroot); | |
177 | if ((fp = cupsFileOpen(line, "r")) != NULL) | |
178 | { | |
179 | linenum = 0; | |
180 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
181 | if (!strcasecmp(line, "Community") && value) | |
182 | { | |
183 | strlcpy(cg->snmp_community, value, sizeof(cg->snmp_community)); | |
184 | break; | |
185 | } | |
186 | ||
187 | cupsFileClose(fp); | |
188 | } | |
189 | } | |
190 | ||
e07d4801 | 191 | DEBUG_printf(("5_cupsSNMPDefaultCommunity: Returning \"%s\"", |
969307f0 MS |
192 | cg->snmp_community)); |
193 | ||
ac884b6a MS |
194 | return (cg->snmp_community); |
195 | } | |
196 | ||
197 | ||
91c84a35 | 198 | /* |
7a14d768 | 199 | * '_cupsSNMPIsOID()' - Test whether a SNMP response contains the specified OID. |
91c84a35 | 200 | * |
ac884b6a | 201 | * The array pointed to by "oid" is terminated by the value -1. |
91c84a35 MS |
202 | */ |
203 | ||
204 | int /* O - 1 if equal, 0 if not equal */ | |
7a14d768 MS |
205 | _cupsSNMPIsOID(cups_snmp_t *packet, /* I - Response packet */ |
206 | const int *oid) /* I - OID */ | |
91c84a35 MS |
207 | { |
208 | int i; /* Looping var */ | |
209 | ||
210 | ||
211 | /* | |
212 | * Range check input... | |
213 | */ | |
214 | ||
e07d4801 | 215 | DEBUG_printf(("4_cupsSNMPIsOID(packet=%p, oid=%p)", packet, oid)); |
969307f0 | 216 | |
91c84a35 | 217 | if (!packet || !oid) |
969307f0 | 218 | { |
e07d4801 | 219 | DEBUG_puts("5_cupsSNMPIsOID: Returning 0"); |
969307f0 | 220 | |
91c84a35 | 221 | return (0); |
969307f0 | 222 | } |
91c84a35 MS |
223 | |
224 | /* | |
225 | * Compare OIDs... | |
226 | */ | |
227 | ||
ac884b6a MS |
228 | for (i = 0; |
229 | i < CUPS_SNMP_MAX_OID && oid[i] >= 0 && packet->object_name[i] >= 0; | |
230 | i ++) | |
91c84a35 | 231 | if (oid[i] != packet->object_name[i]) |
969307f0 | 232 | { |
e07d4801 | 233 | DEBUG_puts("5_cupsSNMPIsOID: Returning 0"); |
969307f0 | 234 | |
91c84a35 | 235 | return (0); |
969307f0 MS |
236 | } |
237 | ||
e07d4801 | 238 | DEBUG_printf(("5_cupsSNMPIsOID: Returning %d", |
969307f0 | 239 | i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i])); |
91c84a35 MS |
240 | |
241 | return (i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]); | |
242 | } | |
243 | ||
244 | ||
245 | /* | |
7a14d768 MS |
246 | * '_cupsSNMPIsOIDPrefixed()' - Test whether a SNMP response uses the specified |
247 | * OID prefix. | |
91c84a35 | 248 | * |
ac884b6a | 249 | * The array pointed to by "prefix" is terminated by the value -1. |
91c84a35 MS |
250 | */ |
251 | ||
252 | int /* O - 1 if prefixed, 0 if not prefixed */ | |
7a14d768 | 253 | _cupsSNMPIsOIDPrefixed( |
91c84a35 MS |
254 | cups_snmp_t *packet, /* I - Response packet */ |
255 | const int *prefix) /* I - OID prefix */ | |
256 | { | |
257 | int i; /* Looping var */ | |
258 | ||
259 | ||
260 | /* | |
261 | * Range check input... | |
262 | */ | |
263 | ||
e07d4801 | 264 | DEBUG_printf(("4_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)", packet, |
969307f0 MS |
265 | prefix)); |
266 | ||
91c84a35 | 267 | if (!packet || !prefix) |
969307f0 | 268 | { |
e07d4801 | 269 | DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0"); |
969307f0 | 270 | |
91c84a35 | 271 | return (0); |
969307f0 | 272 | } |
91c84a35 MS |
273 | |
274 | /* | |
275 | * Compare OIDs... | |
276 | */ | |
277 | ||
278 | for (i = 0; | |
ac884b6a | 279 | i < CUPS_SNMP_MAX_OID && prefix[i] >= 0 && packet->object_name[i] >= 0; |
91c84a35 MS |
280 | i ++) |
281 | if (prefix[i] != packet->object_name[i]) | |
969307f0 | 282 | { |
e07d4801 | 283 | DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0"); |
969307f0 | 284 | |
91c84a35 | 285 | return (0); |
969307f0 MS |
286 | } |
287 | ||
e07d4801 | 288 | DEBUG_printf(("5_cupsSNMPIsOIDPrefixed: Returning %d", |
969307f0 | 289 | i < CUPS_SNMP_MAX_OID)); |
91c84a35 MS |
290 | |
291 | return (i < CUPS_SNMP_MAX_OID); | |
292 | } | |
293 | ||
294 | ||
20fbc903 MS |
295 | /* |
296 | * '_cupsSNMPOIDToString()' - Convert an OID to a string. | |
20fbc903 MS |
297 | */ |
298 | ||
299 | ||
300 | char * /* O - New string or @code NULL@ on error */ | |
301 | _cupsSNMPOIDToString(const int *src, /* I - OID */ | |
302 | char *dst, /* I - String buffer */ | |
303 | size_t dstsize) /* I - Size of string buffer */ | |
304 | { | |
305 | char *dstptr, /* Pointer into string buffer */ | |
306 | *dstend; /* End of string buffer */ | |
307 | ||
308 | ||
e07d4801 | 309 | DEBUG_printf(("4_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT ")", |
20fbc903 MS |
310 | src, dst, CUPS_LLCAST dstsize)); |
311 | ||
312 | /* | |
313 | * Range check input... | |
314 | */ | |
315 | ||
316 | if (!src || !dst || dstsize < 4) | |
317 | return (NULL); | |
318 | ||
319 | /* | |
320 | * Loop through the OID array and build a string... | |
321 | */ | |
322 | ||
323 | for (dstptr = dst, dstend = dstptr + dstsize - 1; | |
324 | *src >= 0 && dstptr < dstend; | |
325 | src ++, dstptr += strlen(dstptr)) | |
326 | snprintf(dstptr, dstend - dstptr + 1, ".%d", *src); | |
327 | ||
328 | if (*src >= 0) | |
329 | return (NULL); | |
330 | else | |
331 | return (dst); | |
332 | } | |
333 | ||
334 | ||
91c84a35 | 335 | /* |
7a14d768 | 336 | * '_cupsSNMPOpen()' - Open a SNMP socket. |
91c84a35 MS |
337 | */ |
338 | ||
339 | int /* O - SNMP socket file descriptor */ | |
7a14d768 | 340 | _cupsSNMPOpen(int family) /* I - Address family - @code AF_INET@ or @code AF_INET6@ */ |
91c84a35 MS |
341 | { |
342 | int fd; /* SNMP socket file descriptor */ | |
343 | int val; /* Socket option value */ | |
344 | ||
345 | ||
346 | /* | |
347 | * Create the SNMP socket... | |
348 | */ | |
349 | ||
e07d4801 | 350 | DEBUG_printf(("4_cupsSNMPOpen(family=%d)", family)); |
969307f0 | 351 | |
ac884b6a | 352 | if ((fd = socket(family, SOCK_DGRAM, 0)) < 0) |
969307f0 | 353 | { |
e07d4801 | 354 | DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno))); |
969307f0 | 355 | |
91c84a35 | 356 | return (-1); |
969307f0 | 357 | } |
91c84a35 MS |
358 | |
359 | /* | |
360 | * Set the "broadcast" flag... | |
361 | */ | |
362 | ||
363 | val = 1; | |
364 | ||
536bc2c6 MS |
365 | #ifdef WIN32 |
366 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *)&val, sizeof(val))) | |
367 | #else | |
91c84a35 | 368 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) |
536bc2c6 | 369 | #endif /* WIN32 */ |
91c84a35 | 370 | { |
e07d4801 | 371 | DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno))); |
969307f0 | 372 | |
91c84a35 MS |
373 | close(fd); |
374 | ||
375 | return (-1); | |
376 | } | |
377 | ||
e07d4801 | 378 | DEBUG_printf(("5_cupsSNMPOpen: Returning %d", fd)); |
969307f0 | 379 | |
91c84a35 MS |
380 | return (fd); |
381 | } | |
382 | ||
383 | ||
384 | /* | |
7a14d768 | 385 | * '_cupsSNMPRead()' - Read and parse a SNMP response. |
91c84a35 | 386 | * |
7a14d768 | 387 | * If "timeout" is negative, @code _cupsSNMPRead@ will wait for a response |
91c84a35 | 388 | * indefinitely. |
91c84a35 MS |
389 | */ |
390 | ||
ac884b6a | 391 | cups_snmp_t * /* O - SNMP packet or @code NULL@ if none */ |
7a14d768 | 392 | _cupsSNMPRead(int fd, /* I - SNMP socket file descriptor */ |
969307f0 MS |
393 | cups_snmp_t *packet, /* I - SNMP packet buffer */ |
394 | double timeout) /* I - Timeout in seconds */ | |
91c84a35 MS |
395 | { |
396 | unsigned char buffer[CUPS_SNMP_MAX_PACKET]; | |
397 | /* Data packet */ | |
398 | int bytes; /* Number of bytes received */ | |
399 | socklen_t addrlen; /* Source address length */ | |
ac884b6a | 400 | http_addr_t address; /* Source address */ |
91c84a35 MS |
401 | |
402 | ||
403 | /* | |
404 | * Range check input... | |
405 | */ | |
406 | ||
e07d4801 | 407 | DEBUG_printf(("4_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)", fd, packet, |
969307f0 MS |
408 | timeout)); |
409 | ||
91c84a35 | 410 | if (fd < 0 || !packet) |
969307f0 | 411 | { |
e07d4801 | 412 | DEBUG_puts("5_cupsSNMPRead: Returning NULL"); |
969307f0 | 413 | |
91c84a35 | 414 | return (NULL); |
969307f0 | 415 | } |
91c84a35 MS |
416 | |
417 | /* | |
418 | * Optionally wait for a response... | |
419 | */ | |
420 | ||
568fa3fa | 421 | if (timeout >= 0.0) |
91c84a35 MS |
422 | { |
423 | int ready; /* Data ready on socket? */ | |
424 | #ifdef HAVE_POLL | |
425 | struct pollfd pfd; /* Polled file descriptor */ | |
426 | ||
427 | pfd.fd = fd; | |
428 | pfd.events = POLLIN; | |
429 | ||
568fa3fa | 430 | while ((ready = poll(&pfd, 1, (int)(timeout * 1000.0))) < 0 && |
e07d4801 | 431 | (errno == EINTR || errno == EAGAIN)); |
91c84a35 MS |
432 | |
433 | #else | |
434 | fd_set input_set; /* select() input set */ | |
568fa3fa | 435 | struct timeval stimeout; /* select() timeout */ |
91c84a35 MS |
436 | |
437 | do | |
438 | { | |
439 | FD_ZERO(&input_set); | |
440 | FD_SET(fd, &input_set); | |
441 | ||
568fa3fa | 442 | stimeout.tv_sec = (int)timeout; |
536bc2c6 | 443 | stimeout.tv_usec = (int)((timeout - stimeout.tv_sec) * 1000000); |
91c84a35 | 444 | |
568fa3fa | 445 | ready = select(fd + 1, &input_set, NULL, NULL, &stimeout); |
91c84a35 MS |
446 | } |
447 | # ifdef WIN32 | |
448 | while (ready < 0 && WSAGetLastError() == WSAEINTR); | |
449 | # else | |
e07d4801 | 450 | while (ready < 0 && (errno == EINTR || errno == EAGAIN)); |
91c84a35 MS |
451 | # endif /* WIN32 */ |
452 | #endif /* HAVE_POLL */ | |
453 | ||
454 | /* | |
455 | * If we don't have any data ready, return right away... | |
456 | */ | |
457 | ||
458 | if (ready <= 0) | |
969307f0 | 459 | { |
e07d4801 | 460 | DEBUG_puts("5_cupsSNMPRead: Returning NULL (timeout)"); |
969307f0 | 461 | |
91c84a35 | 462 | return (NULL); |
969307f0 | 463 | } |
91c84a35 MS |
464 | } |
465 | ||
466 | /* | |
467 | * Read the response data... | |
468 | */ | |
469 | ||
ac884b6a | 470 | addrlen = sizeof(address); |
91c84a35 | 471 | |
ac884b6a MS |
472 | if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address, |
473 | &addrlen)) < 0) | |
969307f0 | 474 | { |
e07d4801 | 475 | DEBUG_printf(("5_cupsSNMPRead: Returning NULL (%s)", strerror(errno))); |
969307f0 | 476 | |
91c84a35 | 477 | return (NULL); |
969307f0 | 478 | } |
91c84a35 MS |
479 | |
480 | /* | |
481 | * Look for the response status code in the SNMP message header... | |
482 | */ | |
483 | ||
484 | asn1_debug("DEBUG: IN ", buffer, bytes, 0); | |
485 | ||
486 | asn1_decode_snmp(buffer, bytes, packet); | |
487 | ||
ac884b6a MS |
488 | memcpy(&(packet->address), &address, sizeof(packet->address)); |
489 | ||
91c84a35 MS |
490 | /* |
491 | * Return decoded data packet... | |
492 | */ | |
493 | ||
e07d4801 | 494 | DEBUG_puts("5_cupsSNMPRead: Returning packet"); |
969307f0 | 495 | |
91c84a35 MS |
496 | return (packet); |
497 | } | |
498 | ||
499 | ||
500 | /* | |
7a14d768 | 501 | * '_cupsSNMPSetDebug()' - Enable/disable debug logging to stderr. |
91c84a35 MS |
502 | */ |
503 | ||
504 | void | |
7a14d768 | 505 | _cupsSNMPSetDebug(int level) /* I - 1 to enable debug output, 0 otherwise */ |
91c84a35 MS |
506 | { |
507 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
508 | ||
509 | ||
e07d4801 | 510 | DEBUG_printf(("4_cupsSNMPSetDebug(level=%d)", level)); |
969307f0 | 511 | |
91c84a35 MS |
512 | cg->snmp_debug = level; |
513 | } | |
514 | ||
515 | ||
20fbc903 MS |
516 | /* |
517 | * '_cupsSNMPStringToOID()' - Convert a numeric OID string to an OID array. | |
518 | * | |
519 | * This function converts a string of the form ".N.N.N.N.N" to the | |
520 | * corresponding OID array terminated by -1. | |
521 | * | |
522 | * @code NULL@ is returned if the array is not large enough or the string is | |
523 | * not a valid OID number. | |
20fbc903 MS |
524 | */ |
525 | ||
526 | int * /* O - Pointer to OID array or @code NULL@ on error */ | |
527 | _cupsSNMPStringToOID(const char *src, /* I - OID string */ | |
528 | int *dst, /* I - OID array */ | |
529 | int dstsize)/* I - Number of integers in OID array */ | |
530 | { | |
531 | int *dstptr, /* Pointer into OID array */ | |
532 | *dstend; /* End of OID array */ | |
533 | ||
534 | ||
e07d4801 | 535 | DEBUG_printf(("4_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)", |
20fbc903 MS |
536 | src, dst, dstsize)); |
537 | ||
538 | /* | |
539 | * Range check input... | |
540 | */ | |
541 | ||
542 | if (!src || !dst || dstsize < 2) | |
543 | return (NULL); | |
544 | ||
545 | /* | |
546 | * Skip leading "."... | |
547 | */ | |
548 | ||
549 | if (*src == '.') | |
550 | src ++; | |
551 | ||
552 | /* | |
553 | * Loop to the end of the string... | |
554 | */ | |
555 | ||
556 | for (dstend = dst + dstsize - 1, dstptr = dst, *dstptr = 0; | |
557 | *src && dstptr < dstend; | |
558 | src ++) | |
559 | { | |
560 | if (*src == '.') | |
561 | { | |
562 | dstptr ++; | |
563 | *dstptr = 0; | |
564 | } | |
565 | else if (isdigit(*src & 255)) | |
566 | *dstptr = *dstptr * 10 + *src - '0'; | |
567 | else | |
568 | break; | |
569 | } | |
570 | ||
571 | if (*src) | |
572 | return (NULL); | |
573 | ||
574 | /* | |
575 | * Terminate the end of the OID array and return... | |
576 | */ | |
577 | ||
578 | dstptr[1] = -1; | |
579 | ||
580 | return (dst); | |
581 | } | |
582 | ||
583 | ||
ac884b6a | 584 | /* |
7a14d768 | 585 | * '_cupsSNMPWalk()' - Enumerate a group of OIDs. |
ac884b6a MS |
586 | * |
587 | * This function queries all of the OIDs with the specified OID prefix, | |
588 | * calling the "cb" function for every response that is received. | |
589 | * | |
590 | * The array pointed to by "prefix" is terminated by the value -1. | |
591 | * | |
7a14d768 | 592 | * If "timeout" is negative, @code _cupsSNMPWalk@ will wait for a response |
568fa3fa | 593 | * indefinitely. |
ac884b6a MS |
594 | */ |
595 | ||
596 | int /* O - Number of OIDs found or -1 on error */ | |
7a14d768 MS |
597 | _cupsSNMPWalk(int fd, /* I - SNMP socket */ |
598 | http_addr_t *address, /* I - Address to query */ | |
599 | int version, /* I - SNMP version */ | |
600 | const char *community,/* I - Community name */ | |
601 | const int *prefix, /* I - OID prefix */ | |
602 | double timeout, /* I - Timeout for each response in seconds */ | |
603 | cups_snmp_cb_t cb, /* I - Function to call for each response */ | |
604 | void *data) /* I - User data pointer that is passed to the callback function */ | |
ac884b6a MS |
605 | { |
606 | int count = 0; /* Number of OIDs found */ | |
607 | int request_id = 0; /* Current request ID */ | |
608 | cups_snmp_t packet; /* Current response packet */ | |
18ecb428 MS |
609 | int lastoid[CUPS_SNMP_MAX_OID]; |
610 | /* Last OID we got */ | |
ac884b6a MS |
611 | |
612 | ||
613 | /* | |
614 | * Range check input... | |
615 | */ | |
616 | ||
e07d4801 MS |
617 | DEBUG_printf(("4_cupsSNMPWalk(fd=%d, address=%p, version=%d, " |
618 | "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)", | |
619 | fd, address, version, community, prefix, timeout, cb, data)); | |
969307f0 | 620 | |
ac884b6a MS |
621 | if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || |
622 | !prefix || !cb) | |
969307f0 | 623 | { |
e07d4801 | 624 | DEBUG_puts("5_cupsSNMPWalk: Returning -1"); |
969307f0 | 625 | |
ac884b6a | 626 | return (-1); |
969307f0 | 627 | } |
ac884b6a MS |
628 | |
629 | /* | |
630 | * Copy the OID prefix and then loop until we have no more OIDs... | |
631 | */ | |
632 | ||
7a14d768 | 633 | _cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID); |
18ecb428 | 634 | lastoid[0] = -1; |
ac884b6a MS |
635 | |
636 | for (;;) | |
637 | { | |
638 | request_id ++; | |
639 | ||
7a14d768 | 640 | if (!_cupsSNMPWrite(fd, address, version, community, |
18ecb428 MS |
641 | CUPS_ASN1_GET_NEXT_REQUEST, request_id, |
642 | packet.object_name)) | |
969307f0 | 643 | { |
e07d4801 | 644 | DEBUG_puts("5_cupsSNMPWalk: Returning -1"); |
969307f0 | 645 | |
ac884b6a | 646 | return (-1); |
969307f0 | 647 | } |
ac884b6a | 648 | |
7a14d768 | 649 | if (!_cupsSNMPRead(fd, &packet, timeout)) |
969307f0 | 650 | { |
e07d4801 | 651 | DEBUG_puts("5_cupsSNMPWalk: Returning -1"); |
969307f0 | 652 | |
ac884b6a | 653 | return (-1); |
969307f0 | 654 | } |
ac884b6a | 655 | |
18ecb428 MS |
656 | if (!_cupsSNMPIsOIDPrefixed(&packet, prefix) || |
657 | _cupsSNMPIsOID(&packet, lastoid)) | |
969307f0 | 658 | { |
e07d4801 | 659 | DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count)); |
969307f0 | 660 | |
ac884b6a | 661 | return (count); |
969307f0 | 662 | } |
ac884b6a MS |
663 | |
664 | if (packet.error || packet.error_status) | |
969307f0 | 665 | { |
e07d4801 | 666 | DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count > 0 ? count : -1)); |
969307f0 | 667 | |
ac884b6a | 668 | return (count > 0 ? count : -1); |
969307f0 | 669 | } |
ac884b6a | 670 | |
18ecb428 MS |
671 | _cupsSNMPCopyOID(lastoid, packet.object_name, CUPS_SNMP_MAX_OID); |
672 | ||
ac884b6a MS |
673 | count ++; |
674 | ||
675 | (*cb)(&packet, data); | |
676 | } | |
677 | } | |
678 | ||
679 | ||
91c84a35 | 680 | /* |
7a14d768 | 681 | * '_cupsSNMPWrite()' - Send an SNMP query packet. |
91c84a35 | 682 | * |
ac884b6a | 683 | * The array pointed to by "oid" is terminated by the value -1. |
91c84a35 MS |
684 | */ |
685 | ||
686 | int /* O - 1 on success, 0 on error */ | |
7a14d768 | 687 | _cupsSNMPWrite( |
91c84a35 MS |
688 | int fd, /* I - SNMP socket */ |
689 | http_addr_t *address, /* I - Address to send to */ | |
690 | int version, /* I - SNMP version */ | |
691 | const char *community, /* I - Community name */ | |
692 | cups_asn1_t request_type, /* I - Request type */ | |
693 | const unsigned request_id, /* I - Request ID */ | |
694 | const int *oid) /* I - OID */ | |
695 | { | |
696 | int i; /* Looping var */ | |
697 | cups_snmp_t packet; /* SNMP message packet */ | |
698 | unsigned char buffer[CUPS_SNMP_MAX_PACKET]; | |
699 | /* SNMP message buffer */ | |
700 | int bytes; /* Size of message */ | |
1ff0402e | 701 | http_addr_t temp; /* Copy of address */ |
91c84a35 MS |
702 | |
703 | ||
ac884b6a MS |
704 | /* |
705 | * Range check input... | |
706 | */ | |
707 | ||
e07d4801 MS |
708 | DEBUG_printf(("4_cupsSNMPWrite(fd=%d, address=%p, version=%d, " |
709 | "community=\"%s\", request_type=%d, request_id=%u, oid=%p)", | |
710 | fd, address, version, community, request_type, request_id, oid)); | |
969307f0 | 711 | |
ac884b6a MS |
712 | if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || |
713 | (request_type != CUPS_ASN1_GET_REQUEST && | |
714 | request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid) | |
969307f0 | 715 | { |
e07d4801 | 716 | DEBUG_puts("5_cupsSNMPWrite: Returning 0 (bad arguments)"); |
969307f0 | 717 | |
ac884b6a | 718 | return (0); |
969307f0 | 719 | } |
ac884b6a | 720 | |
91c84a35 MS |
721 | /* |
722 | * Create the SNMP message... | |
723 | */ | |
724 | ||
725 | memset(&packet, 0, sizeof(packet)); | |
726 | ||
727 | packet.version = version; | |
728 | packet.request_type = request_type; | |
729 | packet.request_id = request_id; | |
730 | packet.object_type = CUPS_ASN1_NULL_VALUE; | |
731 | ||
732 | strlcpy(packet.community, community, sizeof(packet.community)); | |
733 | ||
ac884b6a | 734 | for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++) |
91c84a35 | 735 | packet.object_name[i] = oid[i]; |
ac884b6a MS |
736 | packet.object_name[i] = -1; |
737 | ||
738 | if (oid[i] >= 0) | |
739 | { | |
e07d4801 | 740 | DEBUG_puts("5_cupsSNMPWrite: Returning 0 (OID too big)"); |
969307f0 | 741 | |
ac884b6a MS |
742 | errno = E2BIG; |
743 | return (0); | |
744 | } | |
91c84a35 MS |
745 | |
746 | bytes = asn1_encode_snmp(buffer, sizeof(buffer), &packet); | |
747 | ||
748 | if (bytes < 0) | |
749 | { | |
e07d4801 | 750 | DEBUG_puts("5_cupsSNMPWrite: Returning 0 (request too big)"); |
969307f0 | 751 | |
91c84a35 | 752 | errno = E2BIG; |
91c84a35 MS |
753 | return (0); |
754 | } | |
755 | ||
756 | asn1_debug("DEBUG: OUT ", buffer, bytes, 0); | |
757 | ||
758 | /* | |
759 | * Send the message... | |
760 | */ | |
761 | ||
1ff0402e MS |
762 | temp = *address; |
763 | ||
91c84a35 | 764 | #ifdef AF_INET6 |
1ff0402e MS |
765 | if (temp.addr.sa_family == AF_INET6) |
766 | temp.ipv6.sin6_port = htons(CUPS_SNMP_PORT); | |
91c84a35 MS |
767 | else |
768 | #endif /* AF_INET6 */ | |
1ff0402e | 769 | temp.ipv4.sin_port = htons(CUPS_SNMP_PORT); |
91c84a35 | 770 | |
1ff0402e MS |
771 | return (sendto(fd, buffer, bytes, 0, (void *)&temp, |
772 | httpAddrLength(&temp)) == bytes); | |
91c84a35 MS |
773 | } |
774 | ||
775 | ||
ac884b6a MS |
776 | /* |
777 | * 'asn1_debug()' - Decode an ASN1-encoded message. | |
778 | */ | |
779 | ||
780 | static void | |
781 | asn1_debug(const char *prefix, /* I - Prefix string */ | |
782 | unsigned char *buffer, /* I - Buffer */ | |
783 | size_t len, /* I - Length of buffer */ | |
784 | int indent) /* I - Indentation */ | |
785 | { | |
786 | int i; /* Looping var */ | |
787 | unsigned char *bufend; /* End of buffer */ | |
788 | int integer; /* Number value */ | |
789 | int oid[CUPS_SNMP_MAX_OID]; /* OID value */ | |
790 | char string[CUPS_SNMP_MAX_STRING]; | |
791 | /* String value */ | |
792 | unsigned char value_type; /* Type of value */ | |
793 | int value_length; /* Length of value */ | |
794 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
795 | ||
796 | ||
797 | if (cg->snmp_debug <= 0) | |
798 | return; | |
799 | ||
800 | if (cg->snmp_debug > 1 && indent == 0) | |
801 | { | |
802 | /* | |
803 | * Do a hex dump of the packet... | |
804 | */ | |
805 | ||
806 | int j; | |
807 | ||
808 | fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len); | |
809 | ||
536bc2c6 | 810 | for (i = 0; i < (int)len; i += 16) |
ac884b6a MS |
811 | { |
812 | fprintf(stderr, "%s%04x:", prefix, i); | |
813 | ||
536bc2c6 | 814 | for (j = 0; j < 16 && (i + j) < (int)len; j ++) |
ac884b6a MS |
815 | { |
816 | if (j && !(j & 3)) | |
817 | fprintf(stderr, " %02x", buffer[i + j]); | |
818 | else | |
819 | fprintf(stderr, " %02x", buffer[i + j]); | |
820 | } | |
821 | ||
822 | while (j < 16) | |
823 | { | |
824 | if (j && !(j & 3)) | |
825 | fputs(" ", stderr); | |
826 | else | |
827 | fputs(" ", stderr); | |
828 | ||
829 | j ++; | |
830 | } | |
831 | ||
832 | fputs(" ", stderr); | |
833 | ||
536bc2c6 | 834 | for (j = 0; j < 16 && (i + j) < (int)len; j ++) |
ac884b6a MS |
835 | if (buffer[i + j] < ' ' || buffer[i + j] >= 0x7f) |
836 | putc('.', stderr); | |
837 | else | |
838 | putc(buffer[i + j], stderr); | |
839 | ||
840 | putc('\n', stderr); | |
841 | } | |
842 | } | |
843 | ||
844 | if (indent == 0) | |
845 | fprintf(stderr, "%sMessage:\n", prefix); | |
846 | ||
847 | bufend = buffer + len; | |
848 | ||
849 | while (buffer < bufend) | |
850 | { | |
851 | /* | |
852 | * Get value type... | |
853 | */ | |
854 | ||
855 | value_type = asn1_get_type(&buffer, bufend); | |
856 | value_length = asn1_get_length(&buffer, bufend); | |
857 | ||
858 | switch (value_type) | |
859 | { | |
860 | case CUPS_ASN1_BOOLEAN : | |
861 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
862 | ||
863 | fprintf(stderr, "%s%*sBOOLEAN %d bytes %d\n", prefix, indent, "", | |
864 | value_length, integer); | |
865 | break; | |
866 | ||
867 | case CUPS_ASN1_INTEGER : | |
868 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
869 | ||
870 | fprintf(stderr, "%s%*sINTEGER %d bytes %d\n", prefix, indent, "", | |
871 | value_length, integer); | |
872 | break; | |
873 | ||
874 | case CUPS_ASN1_COUNTER : | |
875 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
876 | ||
877 | fprintf(stderr, "%s%*sCOUNTER %d bytes %u\n", prefix, indent, "", | |
878 | value_length, (unsigned)integer); | |
879 | break; | |
880 | ||
881 | case CUPS_ASN1_GAUGE : | |
882 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
883 | ||
884 | fprintf(stderr, "%s%*sGAUGE %d bytes %u\n", prefix, indent, "", | |
885 | value_length, (unsigned)integer); | |
886 | break; | |
887 | ||
888 | case CUPS_ASN1_TIMETICKS : | |
889 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
890 | ||
891 | fprintf(stderr, "%s%*sTIMETICKS %d bytes %u\n", prefix, indent, "", | |
892 | value_length, (unsigned)integer); | |
893 | break; | |
894 | ||
895 | case CUPS_ASN1_OCTET_STRING : | |
896 | fprintf(stderr, "%s%*sOCTET STRING %d bytes \"%s\"\n", prefix, | |
897 | indent, "", value_length, | |
898 | asn1_get_string(&buffer, bufend, value_length, string, | |
899 | sizeof(string))); | |
900 | break; | |
901 | ||
902 | case CUPS_ASN1_HEX_STRING : | |
903 | asn1_get_string(&buffer, bufend, value_length, string, | |
904 | sizeof(string)); | |
905 | fprintf(stderr, "%s%*sHex-STRING %d bytes", prefix, | |
906 | indent, "", value_length); | |
907 | for (i = 0; i < value_length; i ++) | |
908 | fprintf(stderr, " %02X", string[i] & 255); | |
909 | putc('\n', stderr); | |
910 | break; | |
911 | ||
912 | case CUPS_ASN1_NULL_VALUE : | |
913 | fprintf(stderr, "%s%*sNULL VALUE %d bytes\n", prefix, indent, "", | |
914 | value_length); | |
915 | ||
916 | buffer += value_length; | |
917 | break; | |
918 | ||
919 | case CUPS_ASN1_OID : | |
920 | integer = asn1_get_oid(&buffer, bufend, value_length, oid, | |
921 | CUPS_SNMP_MAX_OID); | |
922 | ||
923 | fprintf(stderr, "%s%*sOID %d bytes ", prefix, indent, "", | |
924 | value_length); | |
925 | for (i = 0; i < integer; i ++) | |
926 | fprintf(stderr, ".%d", oid[i]); | |
927 | putc('\n', stderr); | |
928 | break; | |
929 | ||
930 | case CUPS_ASN1_SEQUENCE : | |
931 | fprintf(stderr, "%s%*sSEQUENCE %d bytes\n", prefix, indent, "", | |
932 | value_length); | |
933 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
934 | ||
935 | buffer += value_length; | |
936 | break; | |
937 | ||
938 | case CUPS_ASN1_GET_NEXT_REQUEST : | |
939 | fprintf(stderr, "%s%*sGet-Next-Request-PDU %d bytes\n", prefix, | |
940 | indent, "", value_length); | |
941 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
942 | ||
943 | buffer += value_length; | |
944 | break; | |
945 | ||
946 | case CUPS_ASN1_GET_REQUEST : | |
947 | fprintf(stderr, "%s%*sGet-Request-PDU %d bytes\n", prefix, indent, "", | |
948 | value_length); | |
949 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
950 | ||
951 | buffer += value_length; | |
952 | break; | |
953 | ||
954 | case CUPS_ASN1_GET_RESPONSE : | |
955 | fprintf(stderr, "%s%*sGet-Response-PDU %d bytes\n", prefix, indent, | |
956 | "", value_length); | |
957 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
958 | ||
959 | buffer += value_length; | |
960 | break; | |
961 | ||
962 | default : | |
963 | fprintf(stderr, "%s%*sUNKNOWN(%x) %d bytes\n", prefix, indent, "", | |
964 | value_type, value_length); | |
965 | ||
966 | buffer += value_length; | |
967 | break; | |
968 | } | |
969 | } | |
970 | } | |
971 | ||
972 | ||
91c84a35 MS |
973 | /* |
974 | * 'asn1_decode_snmp()' - Decode a SNMP packet. | |
975 | */ | |
976 | ||
977 | static int /* O - 0 on success, -1 on error */ | |
978 | asn1_decode_snmp(unsigned char *buffer, /* I - Buffer */ | |
979 | size_t len, /* I - Size of buffer */ | |
980 | cups_snmp_t *packet) /* I - SNMP packet */ | |
981 | { | |
982 | unsigned char *bufptr, /* Pointer into the data */ | |
983 | *bufend; /* End of data */ | |
984 | int length; /* Length of value */ | |
985 | ||
986 | ||
987 | /* | |
988 | * Initialize the decoding... | |
989 | */ | |
990 | ||
991 | memset(packet, 0, sizeof(cups_snmp_t)); | |
ac884b6a | 992 | packet->object_name[0] = -1; |
91c84a35 MS |
993 | |
994 | bufptr = buffer; | |
995 | bufend = buffer + len; | |
996 | ||
997 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
998 | snmp_set_error(packet, _("Packet does not start with SEQUENCE")); | |
999 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
1000 | snmp_set_error(packet, _("SEQUENCE uses indefinite length")); | |
1001 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
1002 | snmp_set_error(packet, _("No version number")); | |
1003 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
1004 | snmp_set_error(packet, _("Version uses indefinite length")); | |
1005 | else if ((packet->version = asn1_get_integer(&bufptr, bufend, length)) | |
1006 | != CUPS_SNMP_VERSION_1) | |
1007 | snmp_set_error(packet, _("Bad SNMP version number")); | |
1008 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OCTET_STRING) | |
1009 | snmp_set_error(packet, _("No community name")); | |
1010 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
1011 | snmp_set_error(packet, _("Community name uses indefinite length")); | |
1012 | else | |
1013 | { | |
1014 | asn1_get_string(&bufptr, bufend, length, packet->community, | |
1015 | sizeof(packet->community)); | |
1016 | ||
1017 | if ((packet->request_type = asn1_get_type(&bufptr, bufend)) | |
1018 | != CUPS_ASN1_GET_RESPONSE) | |
1019 | snmp_set_error(packet, _("Packet does not contain a Get-Response-PDU")); | |
1020 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
1021 | snmp_set_error(packet, _("Get-Response-PDU uses indefinite length")); | |
1022 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
1023 | snmp_set_error(packet, _("No request-id")); | |
1024 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
1025 | snmp_set_error(packet, _("request-id uses indefinite length")); | |
1026 | else | |
1027 | { | |
1028 | packet->request_id = asn1_get_integer(&bufptr, bufend, length); | |
1029 | ||
1030 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
1031 | snmp_set_error(packet, _("No error-status")); | |
1032 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
1033 | snmp_set_error(packet, _("error-status uses indefinite length")); | |
1034 | else | |
1035 | { | |
1036 | packet->error_status = asn1_get_integer(&bufptr, bufend, length); | |
1037 | ||
1038 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
1039 | snmp_set_error(packet, _("No error-index")); | |
1040 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
1041 | snmp_set_error(packet, _("error-index uses indefinite length")); | |
1042 | else | |
1043 | { | |
1044 | packet->error_index = asn1_get_integer(&bufptr, bufend, length); | |
1045 | ||
1046 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
1047 | snmp_set_error(packet, _("No variable-bindings SEQUENCE")); | |
1048 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
1049 | snmp_set_error(packet, | |
1050 | _("variable-bindings uses indefinite length")); | |
1051 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
1052 | snmp_set_error(packet, _("No VarBind SEQUENCE")); | |
1053 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
1054 | snmp_set_error(packet, _("VarBind uses indefinite length")); | |
1055 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OID) | |
1056 | snmp_set_error(packet, _("No name OID")); | |
1057 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
1058 | snmp_set_error(packet, _("Name OID uses indefinite length")); | |
1059 | else | |
1060 | { | |
1061 | asn1_get_oid(&bufptr, bufend, length, packet->object_name, | |
1062 | CUPS_SNMP_MAX_OID); | |
1063 | ||
1064 | packet->object_type = asn1_get_type(&bufptr, bufend); | |
1065 | ||
1066 | if ((length = asn1_get_length(&bufptr, bufend)) == 0 && | |
1067 | packet->object_type != CUPS_ASN1_NULL_VALUE && | |
1068 | packet->object_type != CUPS_ASN1_OCTET_STRING) | |
1069 | snmp_set_error(packet, _("Value uses indefinite length")); | |
1070 | else | |
1071 | { | |
1072 | switch (packet->object_type) | |
1073 | { | |
1074 | case CUPS_ASN1_BOOLEAN : | |
1075 | packet->object_value.boolean = | |
1076 | asn1_get_integer(&bufptr, bufend, length); | |
1077 | break; | |
1078 | ||
1079 | case CUPS_ASN1_INTEGER : | |
1080 | packet->object_value.integer = | |
1081 | asn1_get_integer(&bufptr, bufend, length); | |
1082 | break; | |
1083 | ||
1084 | case CUPS_ASN1_NULL_VALUE : | |
1085 | break; | |
1086 | ||
1087 | case CUPS_ASN1_OCTET_STRING : | |
d1c13e16 MS |
1088 | case CUPS_ASN1_BIT_STRING : |
1089 | case CUPS_ASN1_HEX_STRING : | |
1090 | packet->object_value.string.num_bytes = length; | |
91c84a35 | 1091 | asn1_get_string(&bufptr, bufend, length, |
d1c13e16 | 1092 | (char *)packet->object_value.string.bytes, |
91c84a35 MS |
1093 | CUPS_SNMP_MAX_STRING); |
1094 | break; | |
1095 | ||
1096 | case CUPS_ASN1_OID : | |
1097 | asn1_get_oid(&bufptr, bufend, length, | |
1098 | packet->object_value.oid, CUPS_SNMP_MAX_OID); | |
1099 | break; | |
1100 | ||
1101 | case CUPS_ASN1_COUNTER : | |
1102 | packet->object_value.counter = | |
1103 | asn1_get_integer(&bufptr, bufend, length); | |
1104 | break; | |
1105 | ||
1106 | case CUPS_ASN1_GAUGE : | |
1107 | packet->object_value.gauge = | |
1108 | asn1_get_integer(&bufptr, bufend, length); | |
1109 | break; | |
1110 | ||
ac884b6a MS |
1111 | case CUPS_ASN1_TIMETICKS : |
1112 | packet->object_value.timeticks = | |
1113 | asn1_get_integer(&bufptr, bufend, length); | |
1114 | break; | |
1115 | ||
91c84a35 MS |
1116 | default : |
1117 | snmp_set_error(packet, _("Unsupported value type")); | |
1118 | break; | |
1119 | } | |
1120 | } | |
1121 | } | |
1122 | } | |
1123 | } | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | return (packet->error ? -1 : 0); | |
1128 | } | |
1129 | ||
1130 | ||
91c84a35 MS |
1131 | /* |
1132 | * 'asn1_encode_snmp()' - Encode a SNMP packet. | |
1133 | */ | |
1134 | ||
1135 | static int /* O - Length on success, -1 on error */ | |
1136 | asn1_encode_snmp(unsigned char *buffer, /* I - Buffer */ | |
1137 | size_t bufsize, /* I - Size of buffer */ | |
1138 | cups_snmp_t *packet) /* I - SNMP packet */ | |
1139 | { | |
1140 | unsigned char *bufptr; /* Pointer into buffer */ | |
1141 | int total, /* Total length */ | |
1142 | msglen, /* Length of entire message */ | |
1143 | commlen, /* Length of community string */ | |
1144 | reqlen, /* Length of request */ | |
1145 | listlen, /* Length of variable list */ | |
1146 | varlen, /* Length of variable */ | |
1147 | namelen, /* Length of object name OID */ | |
1148 | valuelen; /* Length of object value */ | |
1149 | ||
1150 | ||
1151 | /* | |
1152 | * Get the lengths of the community string, OID, and message... | |
1153 | */ | |
1154 | ||
ac884b6a | 1155 | |
91c84a35 MS |
1156 | namelen = asn1_size_oid(packet->object_name); |
1157 | ||
1158 | switch (packet->object_type) | |
1159 | { | |
1160 | case CUPS_ASN1_NULL_VALUE : | |
1161 | valuelen = 0; | |
1162 | break; | |
1163 | ||
1164 | case CUPS_ASN1_BOOLEAN : | |
1165 | valuelen = asn1_size_integer(packet->object_value.boolean); | |
1166 | break; | |
1167 | ||
1168 | case CUPS_ASN1_INTEGER : | |
1169 | valuelen = asn1_size_integer(packet->object_value.integer); | |
1170 | break; | |
1171 | ||
1172 | case CUPS_ASN1_OCTET_STRING : | |
d1c13e16 | 1173 | valuelen = packet->object_value.string.num_bytes; |
91c84a35 MS |
1174 | break; |
1175 | ||
1176 | case CUPS_ASN1_OID : | |
1177 | valuelen = asn1_size_oid(packet->object_value.oid); | |
1178 | break; | |
1179 | ||
1180 | default : | |
1181 | packet->error = "Unknown object type"; | |
1182 | return (-1); | |
1183 | } | |
1184 | ||
1185 | varlen = 1 + asn1_size_length(namelen) + namelen + | |
1186 | 1 + asn1_size_length(valuelen) + valuelen; | |
1187 | listlen = 1 + asn1_size_length(varlen) + varlen; | |
1188 | reqlen = 2 + asn1_size_integer(packet->request_id) + | |
1189 | 2 + asn1_size_integer(packet->error_status) + | |
1190 | 2 + asn1_size_integer(packet->error_index) + | |
1191 | 1 + asn1_size_length(listlen) + listlen; | |
1192 | commlen = strlen(packet->community); | |
1193 | msglen = 2 + asn1_size_integer(packet->version) + | |
1194 | 1 + asn1_size_length(commlen) + commlen + | |
1195 | 1 + asn1_size_length(reqlen) + reqlen; | |
1196 | total = 1 + asn1_size_length(msglen) + msglen; | |
1197 | ||
536bc2c6 | 1198 | if (total > (int)bufsize) |
91c84a35 MS |
1199 | { |
1200 | packet->error = "Message too large for buffer"; | |
1201 | return (-1); | |
1202 | } | |
1203 | ||
1204 | /* | |
1205 | * Then format the message... | |
1206 | */ | |
1207 | ||
1208 | bufptr = buffer; | |
1209 | ||
1210 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* SNMPv1 message header */ | |
1211 | asn1_set_length(&bufptr, msglen); | |
1212 | ||
1213 | asn1_set_integer(&bufptr, packet->version); | |
1214 | /* version */ | |
1215 | ||
1216 | *bufptr++ = CUPS_ASN1_OCTET_STRING; /* community */ | |
1217 | asn1_set_length(&bufptr, commlen); | |
1218 | memcpy(bufptr, packet->community, commlen); | |
1219 | bufptr += commlen; | |
1220 | ||
ac884b6a | 1221 | *bufptr++ = packet->request_type; /* Get-Request-PDU/Get-Next-Request-PDU */ |
91c84a35 MS |
1222 | asn1_set_length(&bufptr, reqlen); |
1223 | ||
1224 | asn1_set_integer(&bufptr, packet->request_id); | |
1225 | ||
1226 | asn1_set_integer(&bufptr, packet->error_status); | |
1227 | ||
1228 | asn1_set_integer(&bufptr, packet->error_index); | |
1229 | ||
1230 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable-bindings */ | |
1231 | asn1_set_length(&bufptr, listlen); | |
1232 | ||
1233 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable */ | |
1234 | asn1_set_length(&bufptr, varlen); | |
1235 | ||
1236 | asn1_set_oid(&bufptr, packet->object_name); | |
1237 | /* ObjectName */ | |
1238 | ||
1239 | switch (packet->object_type) | |
1240 | { | |
1241 | case CUPS_ASN1_NULL_VALUE : | |
1242 | *bufptr++ = CUPS_ASN1_NULL_VALUE; | |
1243 | /* ObjectValue */ | |
1244 | *bufptr++ = 0; /* Length */ | |
1245 | break; | |
1246 | ||
1247 | case CUPS_ASN1_BOOLEAN : | |
1248 | asn1_set_integer(&bufptr, packet->object_value.boolean); | |
1249 | break; | |
1250 | ||
1251 | case CUPS_ASN1_INTEGER : | |
1252 | asn1_set_integer(&bufptr, packet->object_value.integer); | |
1253 | break; | |
1254 | ||
1255 | case CUPS_ASN1_OCTET_STRING : | |
1256 | *bufptr++ = CUPS_ASN1_OCTET_STRING; | |
1257 | asn1_set_length(&bufptr, valuelen); | |
d1c13e16 | 1258 | memcpy(bufptr, packet->object_value.string.bytes, valuelen); |
91c84a35 MS |
1259 | bufptr += valuelen; |
1260 | break; | |
1261 | ||
1262 | case CUPS_ASN1_OID : | |
1263 | asn1_set_oid(&bufptr, packet->object_value.oid); | |
1264 | break; | |
1265 | ||
1266 | default : | |
1267 | break; | |
1268 | } | |
1269 | ||
1270 | return (bufptr - buffer); | |
1271 | } | |
1272 | ||
1273 | ||
1274 | /* | |
1275 | * 'asn1_get_integer()' - Get an integer value. | |
1276 | */ | |
1277 | ||
1278 | static int /* O - Integer value */ | |
1279 | asn1_get_integer( | |
1280 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1281 | unsigned char *bufend, /* I - End of buffer */ | |
1282 | int length) /* I - Length of value */ | |
1283 | { | |
1284 | int value; /* Integer value */ | |
1285 | ||
1286 | ||
41681883 MS |
1287 | if (length > sizeof(int)) |
1288 | { | |
1289 | (*buffer) += length; | |
1290 | return (0); | |
1291 | } | |
1292 | ||
18ecb428 | 1293 | for (value = (**buffer & 0x80) ? -1 : 0; |
91c84a35 MS |
1294 | length > 0 && *buffer < bufend; |
1295 | length --, (*buffer) ++) | |
1296 | value = (value << 8) | **buffer; | |
1297 | ||
1298 | return (value); | |
1299 | } | |
1300 | ||
1301 | ||
1302 | /* | |
1303 | * 'asn1_get_length()' - Get a value length. | |
1304 | */ | |
1305 | ||
41681883 | 1306 | static unsigned /* O - Length */ |
91c84a35 MS |
1307 | asn1_get_length(unsigned char **buffer, /* IO - Pointer in buffer */ |
1308 | unsigned char *bufend) /* I - End of buffer */ | |
1309 | { | |
41681883 | 1310 | unsigned length; /* Length */ |
91c84a35 MS |
1311 | |
1312 | ||
1313 | length = **buffer; | |
1314 | (*buffer) ++; | |
1315 | ||
1316 | if (length & 128) | |
41681883 MS |
1317 | { |
1318 | int count; /* Number of bytes for length */ | |
1319 | ||
1320 | ||
1321 | if ((count = length & 127) > sizeof(unsigned)) | |
1322 | { | |
1323 | (*buffer) += count; | |
1324 | return (0); | |
1325 | } | |
1326 | ||
1327 | for (length = 0; | |
1328 | count > 0 && *buffer < bufend; | |
1329 | count --, (*buffer) ++) | |
1330 | length = (length << 8) | **buffer; | |
1331 | } | |
91c84a35 MS |
1332 | |
1333 | return (length); | |
1334 | } | |
1335 | ||
1336 | ||
1337 | /* | |
1338 | * 'asn1_get_oid()' - Get an OID value. | |
1339 | */ | |
1340 | ||
ac884b6a | 1341 | static int /* O - Number of OIDs */ |
91c84a35 MS |
1342 | asn1_get_oid( |
1343 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1344 | unsigned char *bufend, /* I - End of buffer */ | |
1345 | int length, /* I - Length of value */ | |
1346 | int *oid, /* I - OID buffer */ | |
1347 | int oidsize) /* I - Size of OID buffer */ | |
1348 | { | |
1349 | unsigned char *valend; /* End of value */ | |
ac884b6a MS |
1350 | int *oidptr, /* Current OID */ |
1351 | *oidend; /* End of OID buffer */ | |
91c84a35 MS |
1352 | int number; /* OID number */ |
1353 | ||
1354 | ||
1355 | valend = *buffer + length; | |
ac884b6a | 1356 | oidptr = oid; |
91c84a35 MS |
1357 | oidend = oid + oidsize - 1; |
1358 | ||
1359 | if (valend > bufend) | |
1360 | valend = bufend; | |
1361 | ||
1362 | number = asn1_get_packed(buffer, bufend); | |
1363 | ||
1364 | if (number < 80) | |
1365 | { | |
ac884b6a MS |
1366 | *oidptr++ = number / 40; |
1367 | number = number % 40; | |
1368 | *oidptr++ = number; | |
91c84a35 MS |
1369 | } |
1370 | else | |
1371 | { | |
ac884b6a MS |
1372 | *oidptr++ = 2; |
1373 | number -= 80; | |
1374 | *oidptr++ = number; | |
91c84a35 MS |
1375 | } |
1376 | ||
1377 | while (*buffer < valend) | |
1378 | { | |
1379 | number = asn1_get_packed(buffer, bufend); | |
1380 | ||
ac884b6a MS |
1381 | if (oidptr < oidend) |
1382 | *oidptr++ = number; | |
91c84a35 MS |
1383 | } |
1384 | ||
ac884b6a | 1385 | *oidptr = -1; |
91c84a35 | 1386 | |
ac884b6a | 1387 | return (oidptr - oid); |
91c84a35 MS |
1388 | } |
1389 | ||
1390 | ||
1391 | /* | |
1392 | * 'asn1_get_packed()' - Get a packed integer value. | |
1393 | */ | |
1394 | ||
1395 | static int /* O - Value */ | |
1396 | asn1_get_packed( | |
1397 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1398 | unsigned char *bufend) /* I - End of buffer */ | |
1399 | { | |
1400 | int value; /* Value */ | |
1401 | ||
1402 | ||
1403 | value = 0; | |
1404 | ||
1405 | while ((**buffer & 128) && *buffer < bufend) | |
1406 | { | |
1407 | value = (value << 7) | (**buffer & 127); | |
1408 | (*buffer) ++; | |
1409 | } | |
1410 | ||
1411 | if (*buffer < bufend) | |
1412 | { | |
1413 | value = (value << 7) | **buffer; | |
1414 | (*buffer) ++; | |
1415 | } | |
1416 | ||
1417 | return (value); | |
1418 | } | |
1419 | ||
1420 | ||
1421 | /* | |
1422 | * 'asn1_get_string()' - Get a string value. | |
1423 | */ | |
1424 | ||
1425 | static char * /* O - String */ | |
1426 | asn1_get_string( | |
1427 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1428 | unsigned char *bufend, /* I - End of buffer */ | |
1429 | int length, /* I - Value length */ | |
1430 | char *string, /* I - String buffer */ | |
1431 | int strsize) /* I - String buffer size */ | |
1432 | { | |
1433 | if (length < 0) | |
1434 | { | |
1435 | /* | |
1436 | * Disallow negative lengths! | |
1437 | */ | |
1438 | ||
1439 | *string = '\0'; | |
1440 | } | |
1441 | else if (length < strsize) | |
1442 | { | |
1443 | /* | |
1444 | * String is smaller than the buffer... | |
1445 | */ | |
1446 | ||
1447 | if (length > 0) | |
1448 | memcpy(string, *buffer, length); | |
1449 | ||
1450 | string[length] = '\0'; | |
1451 | } | |
1452 | else | |
1453 | { | |
1454 | /* | |
1455 | * String is larger than the buffer... | |
1456 | */ | |
1457 | ||
1458 | memcpy(string, *buffer, strsize - 1); | |
1459 | string[strsize - 1] = '\0'; | |
1460 | } | |
1461 | ||
1462 | if (length > 0) | |
1463 | (*buffer) += length; | |
1464 | ||
1465 | return (length < 0 ? NULL : string); | |
1466 | } | |
1467 | ||
1468 | ||
1469 | /* | |
1470 | * 'asn1_get_type()' - Get a value type. | |
1471 | */ | |
1472 | ||
1473 | static int /* O - Type */ | |
1474 | asn1_get_type(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1475 | unsigned char *bufend) /* I - End of buffer */ | |
1476 | { | |
1477 | int type; /* Type */ | |
1478 | ||
1479 | ||
1480 | type = **buffer; | |
1481 | (*buffer) ++; | |
1482 | ||
1483 | if ((type & 31) == 31) | |
1484 | type = asn1_get_packed(buffer, bufend); | |
1485 | ||
1486 | return (type); | |
1487 | } | |
1488 | ||
1489 | ||
1490 | /* | |
1491 | * 'asn1_set_integer()' - Set an integer value. | |
1492 | */ | |
1493 | ||
1494 | static void | |
1495 | asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */ | |
1496 | int integer) /* I - Integer value */ | |
1497 | { | |
1498 | **buffer = CUPS_ASN1_INTEGER; | |
1499 | (*buffer) ++; | |
1500 | ||
1501 | if (integer > 0x7fffff || integer < -0x800000) | |
1502 | { | |
1503 | **buffer = 4; | |
1504 | (*buffer) ++; | |
1505 | **buffer = integer >> 24; | |
1506 | (*buffer) ++; | |
1507 | **buffer = integer >> 16; | |
1508 | (*buffer) ++; | |
1509 | **buffer = integer >> 8; | |
1510 | (*buffer) ++; | |
1511 | **buffer = integer; | |
1512 | (*buffer) ++; | |
1513 | } | |
1514 | else if (integer > 0x7fff || integer < -0x8000) | |
1515 | { | |
1516 | **buffer = 3; | |
1517 | (*buffer) ++; | |
1518 | **buffer = integer >> 16; | |
1519 | (*buffer) ++; | |
1520 | **buffer = integer >> 8; | |
1521 | (*buffer) ++; | |
1522 | **buffer = integer; | |
1523 | (*buffer) ++; | |
1524 | } | |
1525 | else if (integer > 0x7f || integer < -0x80) | |
1526 | { | |
1527 | **buffer = 2; | |
1528 | (*buffer) ++; | |
1529 | **buffer = integer >> 8; | |
1530 | (*buffer) ++; | |
1531 | **buffer = integer; | |
1532 | (*buffer) ++; | |
1533 | } | |
1534 | else | |
1535 | { | |
1536 | **buffer = 1; | |
1537 | (*buffer) ++; | |
1538 | **buffer = integer; | |
1539 | (*buffer) ++; | |
1540 | } | |
1541 | } | |
1542 | ||
1543 | ||
1544 | /* | |
1545 | * 'asn1_set_length()' - Set a value length. | |
1546 | */ | |
1547 | ||
1548 | static void | |
1549 | asn1_set_length(unsigned char **buffer, /* IO - Pointer in buffer */ | |
41681883 | 1550 | unsigned length) /* I - Length value */ |
91c84a35 MS |
1551 | { |
1552 | if (length > 255) | |
1553 | { | |
1554 | **buffer = 0x82; /* 2-byte length */ | |
1555 | (*buffer) ++; | |
1556 | **buffer = length >> 8; | |
1557 | (*buffer) ++; | |
1558 | **buffer = length; | |
1559 | (*buffer) ++; | |
1560 | } | |
1561 | else if (length > 127) | |
1562 | { | |
1563 | **buffer = 0x81; /* 1-byte length */ | |
1564 | (*buffer) ++; | |
1565 | **buffer = length; | |
1566 | (*buffer) ++; | |
1567 | } | |
1568 | else | |
1569 | { | |
1570 | **buffer = length; /* Length */ | |
1571 | (*buffer) ++; | |
1572 | } | |
1573 | } | |
1574 | ||
1575 | ||
1576 | /* | |
1577 | * 'asn1_set_oid()' - Set an OID value. | |
1578 | */ | |
1579 | ||
1580 | static void | |
1581 | asn1_set_oid(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1582 | const int *oid) /* I - OID value */ | |
1583 | { | |
1584 | **buffer = CUPS_ASN1_OID; | |
1585 | (*buffer) ++; | |
1586 | ||
1587 | asn1_set_length(buffer, asn1_size_oid(oid)); | |
1588 | ||
ac884b6a MS |
1589 | if (oid[1] < 0) |
1590 | { | |
1591 | asn1_set_packed(buffer, oid[0] * 40); | |
1592 | return; | |
1593 | } | |
1594 | ||
91c84a35 MS |
1595 | asn1_set_packed(buffer, oid[0] * 40 + oid[1]); |
1596 | ||
ac884b6a | 1597 | for (oid += 2; *oid >= 0; oid ++) |
91c84a35 MS |
1598 | asn1_set_packed(buffer, *oid); |
1599 | } | |
1600 | ||
1601 | ||
1602 | /* | |
1603 | * 'asn1_set_packed()' - Set a packed integer value. | |
1604 | */ | |
1605 | ||
1606 | static void | |
1607 | asn1_set_packed(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1608 | int integer) /* I - Integer value */ | |
1609 | { | |
1610 | if (integer > 0xfffffff) | |
1611 | { | |
ac884b6a | 1612 | **buffer = ((integer >> 28) & 0x7f) | 0x80; |
91c84a35 MS |
1613 | (*buffer) ++; |
1614 | } | |
1615 | ||
1616 | if (integer > 0x1fffff) | |
1617 | { | |
ac884b6a | 1618 | **buffer = ((integer >> 21) & 0x7f) | 0x80; |
91c84a35 MS |
1619 | (*buffer) ++; |
1620 | } | |
1621 | ||
1622 | if (integer > 0x3fff) | |
1623 | { | |
ac884b6a | 1624 | **buffer = ((integer >> 14) & 0x7f) | 0x80; |
91c84a35 MS |
1625 | (*buffer) ++; |
1626 | } | |
1627 | ||
1628 | if (integer > 0x7f) | |
1629 | { | |
ac884b6a | 1630 | **buffer = ((integer >> 7) & 0x7f) | 0x80; |
91c84a35 MS |
1631 | (*buffer) ++; |
1632 | } | |
1633 | ||
1634 | **buffer = integer & 0x7f; | |
1635 | (*buffer) ++; | |
1636 | } | |
1637 | ||
1638 | ||
1639 | /* | |
1640 | * 'asn1_size_integer()' - Figure out the number of bytes needed for an | |
1641 | * integer value. | |
1642 | */ | |
1643 | ||
1644 | static int /* O - Size in bytes */ | |
1645 | asn1_size_integer(int integer) /* I - Integer value */ | |
1646 | { | |
1647 | if (integer > 0x7fffff || integer < -0x800000) | |
1648 | return (4); | |
1649 | else if (integer > 0x7fff || integer < -0x8000) | |
1650 | return (3); | |
1651 | else if (integer > 0x7f || integer < -0x80) | |
1652 | return (2); | |
1653 | else | |
1654 | return (1); | |
1655 | } | |
1656 | ||
1657 | ||
1658 | /* | |
1659 | * 'asn1_size_length()' - Figure out the number of bytes needed for a | |
1660 | * length value. | |
1661 | */ | |
1662 | ||
1663 | static int /* O - Size in bytes */ | |
1664 | asn1_size_length(int length) /* I - Length value */ | |
1665 | { | |
1666 | if (length > 0xff) | |
1667 | return (3); | |
1668 | else if (length > 0x7f) | |
1669 | return (2); | |
1670 | else | |
1671 | return (1); | |
1672 | } | |
1673 | ||
1674 | ||
1675 | /* | |
1676 | * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an | |
1677 | * OID value. | |
1678 | */ | |
1679 | ||
1680 | static int /* O - Size in bytes */ | |
1681 | asn1_size_oid(const int *oid) /* I - OID value */ | |
1682 | { | |
1683 | int length; /* Length of value */ | |
1684 | ||
1685 | ||
ac884b6a MS |
1686 | if (oid[1] < 0) |
1687 | return (asn1_size_packed(oid[0] * 40)); | |
1688 | ||
1689 | for (length = asn1_size_packed(oid[0] * 40 + oid[1]), oid += 2; | |
1690 | *oid >= 0; | |
1691 | oid ++) | |
91c84a35 MS |
1692 | length += asn1_size_packed(*oid); |
1693 | ||
1694 | return (length); | |
1695 | } | |
1696 | ||
1697 | ||
1698 | /* | |
1699 | * 'asn1_size_packed()' - Figure out the number of bytes needed for a | |
1700 | * packed integer value. | |
1701 | */ | |
1702 | ||
1703 | static int /* O - Size in bytes */ | |
1704 | asn1_size_packed(int integer) /* I - Integer value */ | |
1705 | { | |
1706 | if (integer > 0xfffffff) | |
1707 | return (5); | |
1708 | else if (integer > 0x1fffff) | |
1709 | return (4); | |
1710 | else if (integer > 0x3fff) | |
1711 | return (3); | |
1712 | else if (integer > 0x7f) | |
1713 | return (2); | |
1714 | else | |
1715 | return (1); | |
1716 | } | |
1717 | ||
1718 | ||
1719 | /* | |
1720 | * 'snmp_set_error()' - Set the localized error for a packet. | |
1721 | */ | |
1722 | ||
1723 | static void | |
1724 | snmp_set_error(cups_snmp_t *packet, /* I - Packet */ | |
1725 | const char *message) /* I - Error message */ | |
1726 | { | |
1727 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
1728 | ||
1729 | ||
1730 | if (!cg->lang_default) | |
1731 | cg->lang_default = cupsLangDefault(); | |
1732 | ||
1733 | packet->error = _cupsLangString(cg->lang_default, message); | |
1734 | } | |
1735 | ||
1736 | ||
1737 | /* | |
1738 | * End of "$Id$". | |
1739 | */ |