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