]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/snmp.c
Merge changes from CUPS 1.4svn-r7594.
[thirdparty/cups.git] / cups / snmp.c
1 /*
2 * "$Id$"
3 *
4 * SNMP functions for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
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 * @since CUPS 1.4@
116 */
117
118 void
119 _cupsSNMPClose(int fd) /* I - SNMP socket file descriptor */
120 {
121 DEBUG_printf(("_cupsSNMPClose(fd=%d)\n", fd));
122
123 #ifdef WIN32
124 closesocket(fd);
125 #else
126 close(fd);
127 #endif /* WIN32 */
128 }
129
130
131 /*
132 * '_cupsSNMPCopyOID()' - Copy an OID.
133 *
134 * The array pointed to by "src" is terminated by the value -1.
135 *
136 * @since CUPS 1.4@
137 */
138
139 int * /* O - New OID */
140 _cupsSNMPCopyOID(int *dst, /* I - Destination OID */
141 const int *src, /* I - Source OID */
142 int dstsize) /* I - Number of integers in dst */
143 {
144 int i; /* Looping var */
145
146
147 DEBUG_printf(("_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)\n", dst, src,
148 dstsize));
149
150 for (i = 0, dstsize --; src[i] >= 0 && i < dstsize; i ++)
151 dst[i] = src[i];
152
153 dst[i] = -1;
154
155 return (dst);
156 }
157
158
159 /*
160 * '_cupsSNMPDefaultCommunity()' - Get the default SNMP community name.
161 *
162 * The default community name is the first community name found in the
163 * snmp.conf file. If no community name is defined there, "public" is used.
164 *
165 * @since CUPS 1.4@
166 */
167
168 const char * /* O - Default community name */
169 _cupsSNMPDefaultCommunity(void)
170 {
171 cups_file_t *fp; /* snmp.conf file */
172 char line[1024], /* Line from file */
173 *value; /* Value from file */
174 int linenum; /* Line number in file */
175 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
176
177
178 DEBUG_puts("_cupsSNMPDefaultCommunity()");
179
180 if (!cg->snmp_community[0])
181 {
182 strlcpy(cg->snmp_community, "public", sizeof(cg->snmp_community));
183
184 snprintf(line, sizeof(line), "%s/snmp.conf", cg->cups_serverroot);
185 if ((fp = cupsFileOpen(line, "r")) != NULL)
186 {
187 linenum = 0;
188 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
189 if (!strcasecmp(line, "Community") && value)
190 {
191 strlcpy(cg->snmp_community, value, sizeof(cg->snmp_community));
192 break;
193 }
194
195 cupsFileClose(fp);
196 }
197 }
198
199 DEBUG_printf(("_cupsSNMPDefaultCommunity: Returning \"%s\"\n",
200 cg->snmp_community));
201
202 return (cg->snmp_community);
203 }
204
205
206 /*
207 * '_cupsSNMPIsOID()' - Test whether a SNMP response contains the specified OID.
208 *
209 * The array pointed to by "oid" is terminated by the value -1.
210 *
211 * @since CUPS 1.4@
212 */
213
214 int /* O - 1 if equal, 0 if not equal */
215 _cupsSNMPIsOID(cups_snmp_t *packet, /* I - Response packet */
216 const int *oid) /* I - OID */
217 {
218 int i; /* Looping var */
219
220
221 /*
222 * Range check input...
223 */
224
225 DEBUG_printf(("_cupsSNMPIsOID(packet=%p, oid=%p)\n", packet, oid));
226
227 if (!packet || !oid)
228 {
229 DEBUG_puts("_cupsSNMPIsOID: Returning 0");
230
231 return (0);
232 }
233
234 /*
235 * Compare OIDs...
236 */
237
238 for (i = 0;
239 i < CUPS_SNMP_MAX_OID && oid[i] >= 0 && packet->object_name[i] >= 0;
240 i ++)
241 if (oid[i] != packet->object_name[i])
242 {
243 DEBUG_puts("_cupsSNMPIsOID: Returning 0");
244
245 return (0);
246 }
247
248 DEBUG_printf(("_cupsSNMPIsOID: Returning %d\n",
249 i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]));
250
251 return (i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]);
252 }
253
254
255 /*
256 * '_cupsSNMPIsOIDPrefixed()' - Test whether a SNMP response uses the specified
257 * OID prefix.
258 *
259 * The array pointed to by "prefix" is terminated by the value -1.
260 *
261 * @since CUPS 1.4@
262 */
263
264 int /* O - 1 if prefixed, 0 if not prefixed */
265 _cupsSNMPIsOIDPrefixed(
266 cups_snmp_t *packet, /* I - Response packet */
267 const int *prefix) /* I - OID prefix */
268 {
269 int i; /* Looping var */
270
271
272 /*
273 * Range check input...
274 */
275
276 DEBUG_printf(("_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)\n", packet,
277 prefix));
278
279 if (!packet || !prefix)
280 {
281 DEBUG_puts("_cupsSNMPIsOIDPrefixed: Returning 0");
282
283 return (0);
284 }
285
286 /*
287 * Compare OIDs...
288 */
289
290 for (i = 0;
291 i < CUPS_SNMP_MAX_OID && prefix[i] >= 0 && packet->object_name[i] >= 0;
292 i ++)
293 if (prefix[i] != packet->object_name[i])
294 {
295 DEBUG_puts("_cupsSNMPIsOIDPrefixed: Returning 0");
296
297 return (0);
298 }
299
300 DEBUG_printf(("_cupsSNMPIsOIDPrefixed: Returning %d\n",
301 i < CUPS_SNMP_MAX_OID));
302
303 return (i < CUPS_SNMP_MAX_OID);
304 }
305
306
307 /*
308 * '_cupsSNMPOIDToString()' - Convert an OID to a string.
309 *
310 * @since CUPS 1.4@
311 */
312
313
314 char * /* O - New string or @code NULL@ on error */
315 _cupsSNMPOIDToString(const int *src, /* I - OID */
316 char *dst, /* I - String buffer */
317 size_t dstsize) /* I - Size of string buffer */
318 {
319 char *dstptr, /* Pointer into string buffer */
320 *dstend; /* End of string buffer */
321
322
323 DEBUG_printf(("_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT ")\n",
324 src, dst, CUPS_LLCAST dstsize));
325
326 /*
327 * Range check input...
328 */
329
330 if (!src || !dst || dstsize < 4)
331 return (NULL);
332
333 /*
334 * Loop through the OID array and build a string...
335 */
336
337 for (dstptr = dst, dstend = dstptr + dstsize - 1;
338 *src >= 0 && dstptr < dstend;
339 src ++, dstptr += strlen(dstptr))
340 snprintf(dstptr, dstend - dstptr + 1, ".%d", *src);
341
342 if (*src >= 0)
343 return (NULL);
344 else
345 return (dst);
346 }
347
348
349 /*
350 * '_cupsSNMPOpen()' - Open a SNMP socket.
351 *
352 * @since CUPS 1.4@
353 */
354
355 int /* O - SNMP socket file descriptor */
356 _cupsSNMPOpen(int family) /* I - Address family - @code AF_INET@ or @code AF_INET6@ */
357 {
358 int fd; /* SNMP socket file descriptor */
359 int val; /* Socket option value */
360
361
362 /*
363 * Create the SNMP socket...
364 */
365
366 DEBUG_printf(("_cupsSNMPOpen(family=%d)\n", family));
367
368 if ((fd = socket(family, SOCK_DGRAM, 0)) < 0)
369 {
370 DEBUG_printf(("_cupsSNMPOpen: Returning -1 (%s)\n", strerror(errno)));
371
372 return (-1);
373 }
374
375 /*
376 * Set the "broadcast" flag...
377 */
378
379 val = 1;
380
381 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
382 {
383 DEBUG_printf(("_cupsSNMPOpen: Returning -1 (%s)\n", strerror(errno)));
384
385 close(fd);
386
387 return (-1);
388 }
389
390 DEBUG_printf(("_cupsSNMPOpen: Returning %d\n", fd));
391
392 return (fd);
393 }
394
395
396 /*
397 * '_cupsSNMPRead()' - Read and parse a SNMP response.
398 *
399 * If "timeout" is negative, @code _cupsSNMPRead@ will wait for a response
400 * indefinitely.
401 *
402 * @since CUPS 1.4@
403 */
404
405 cups_snmp_t * /* O - SNMP packet or @code NULL@ if none */
406 _cupsSNMPRead(int fd, /* I - SNMP socket file descriptor */
407 cups_snmp_t *packet, /* I - SNMP packet buffer */
408 double timeout) /* I - Timeout in seconds */
409 {
410 unsigned char buffer[CUPS_SNMP_MAX_PACKET];
411 /* Data packet */
412 int bytes; /* Number of bytes received */
413 socklen_t addrlen; /* Source address length */
414 http_addr_t address; /* Source address */
415
416
417 /*
418 * Range check input...
419 */
420
421 DEBUG_printf(("_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)\n", fd, packet,
422 timeout));
423
424 if (fd < 0 || !packet)
425 {
426 DEBUG_puts("_cupsSNMPRead: Returning NULL");
427
428 return (NULL);
429 }
430
431 /*
432 * Optionally wait for a response...
433 */
434
435 if (timeout >= 0.0)
436 {
437 int ready; /* Data ready on socket? */
438 #ifdef HAVE_POLL
439 struct pollfd pfd; /* Polled file descriptor */
440
441 pfd.fd = fd;
442 pfd.events = POLLIN;
443
444 while ((ready = poll(&pfd, 1, (int)(timeout * 1000.0))) < 0 &&
445 errno == EINTR);
446
447 #else
448 fd_set input_set; /* select() input set */
449 struct timeval stimeout; /* select() timeout */
450
451 do
452 {
453 FD_ZERO(&input_set);
454 FD_SET(fd, &input_set);
455
456 stimeout.tv_sec = (int)timeout;
457 stimeout.tv_usec = (int)((timeout - stimeout.tv_usec) * 1000000);
458
459 ready = select(fd + 1, &input_set, NULL, NULL, &stimeout);
460 }
461 # ifdef WIN32
462 while (ready < 0 && WSAGetLastError() == WSAEINTR);
463 # else
464 while (ready < 0 && errno == EINTR);
465 # endif /* WIN32 */
466 #endif /* HAVE_POLL */
467
468 /*
469 * If we don't have any data ready, return right away...
470 */
471
472 if (ready <= 0)
473 {
474 DEBUG_puts("_cupsSNMPRead: Returning NULL (timeout)");
475
476 return (NULL);
477 }
478 }
479
480 /*
481 * Read the response data...
482 */
483
484 addrlen = sizeof(address);
485
486 if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address,
487 &addrlen)) < 0)
488 {
489 DEBUG_printf(("_cupsSNMPRead: Returning NULL (%s)\n", strerror(errno)));
490
491 return (NULL);
492 }
493
494 /*
495 * Look for the response status code in the SNMP message header...
496 */
497
498 asn1_debug("DEBUG: IN ", buffer, bytes, 0);
499
500 asn1_decode_snmp(buffer, bytes, packet);
501
502 memcpy(&(packet->address), &address, sizeof(packet->address));
503
504 /*
505 * Return decoded data packet...
506 */
507
508 DEBUG_puts("_cupsSNMPRead: Returning packet");
509
510 return (packet);
511 }
512
513
514 /*
515 * '_cupsSNMPSetDebug()' - Enable/disable debug logging to stderr.
516 *
517 * @since CUPS 1.4@
518 */
519
520 void
521 _cupsSNMPSetDebug(int level) /* I - 1 to enable debug output, 0 otherwise */
522 {
523 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
524
525
526 DEBUG_printf(("_cupsSNMPSetDebug(level=%d)\n", level));
527
528 cg->snmp_debug = level;
529 }
530
531
532 /*
533 * '_cupsSNMPStringToOID()' - Convert a numeric OID string to an OID array.
534 *
535 * This function converts a string of the form ".N.N.N.N.N" to the
536 * corresponding OID array terminated by -1.
537 *
538 * @code NULL@ is returned if the array is not large enough or the string is
539 * not a valid OID number.
540 *
541 * @since CUPS 1.4@
542 */
543
544 int * /* O - Pointer to OID array or @code NULL@ on error */
545 _cupsSNMPStringToOID(const char *src, /* I - OID string */
546 int *dst, /* I - OID array */
547 int dstsize)/* I - Number of integers in OID array */
548 {
549 int *dstptr, /* Pointer into OID array */
550 *dstend; /* End of OID array */
551
552
553 DEBUG_printf(("_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)\n",
554 src, dst, dstsize));
555
556 /*
557 * Range check input...
558 */
559
560 if (!src || !dst || dstsize < 2)
561 return (NULL);
562
563 /*
564 * Skip leading "."...
565 */
566
567 if (*src == '.')
568 src ++;
569
570 /*
571 * Loop to the end of the string...
572 */
573
574 for (dstend = dst + dstsize - 1, dstptr = dst, *dstptr = 0;
575 *src && dstptr < dstend;
576 src ++)
577 {
578 if (*src == '.')
579 {
580 dstptr ++;
581 *dstptr = 0;
582 }
583 else if (isdigit(*src & 255))
584 *dstptr = *dstptr * 10 + *src - '0';
585 else
586 break;
587 }
588
589 if (*src)
590 return (NULL);
591
592 /*
593 * Terminate the end of the OID array and return...
594 */
595
596 dstptr[1] = -1;
597
598 return (dst);
599 }
600
601
602 /*
603 * '_cupsSNMPWalk()' - Enumerate a group of OIDs.
604 *
605 * This function queries all of the OIDs with the specified OID prefix,
606 * calling the "cb" function for every response that is received.
607 *
608 * The array pointed to by "prefix" is terminated by the value -1.
609 *
610 * If "timeout" is negative, @code _cupsSNMPWalk@ will wait for a response
611 * indefinitely.
612 *
613 * @since CUPS 1.4@
614 */
615
616 int /* O - Number of OIDs found or -1 on error */
617 _cupsSNMPWalk(int fd, /* I - SNMP socket */
618 http_addr_t *address, /* I - Address to query */
619 int version, /* I - SNMP version */
620 const char *community,/* I - Community name */
621 const int *prefix, /* I - OID prefix */
622 double timeout, /* I - Timeout for each response in seconds */
623 cups_snmp_cb_t cb, /* I - Function to call for each response */
624 void *data) /* I - User data pointer that is passed to the callback function */
625 {
626 int count = 0; /* Number of OIDs found */
627 int request_id = 0; /* Current request ID */
628 cups_snmp_t packet; /* Current response packet */
629
630
631 /*
632 * Range check input...
633 */
634
635 DEBUG_printf(("_cupsSNMPWalk(fd=%d, address=%p, version=%d, "
636 "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)\n",
637 fd, address, version, community ? community : "(null)",
638 prefix, timeout, cb, data));
639
640 if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
641 !prefix || !cb)
642 {
643 DEBUG_puts("_cupsSNMPWalk: Returning -1");
644
645 return (-1);
646 }
647
648 /*
649 * Copy the OID prefix and then loop until we have no more OIDs...
650 */
651
652 _cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID);
653
654 for (;;)
655 {
656 request_id ++;
657
658 if (!_cupsSNMPWrite(fd, address, version, community,
659 CUPS_ASN1_GET_NEXT_REQUEST, request_id,
660 packet.object_name))
661 {
662 DEBUG_puts("_cupsSNMPWalk: Returning -1");
663
664 return (-1);
665 }
666
667 if (!_cupsSNMPRead(fd, &packet, timeout))
668 {
669 DEBUG_puts("_cupsSNMPWalk: Returning -1");
670
671 return (-1);
672 }
673
674 if (!_cupsSNMPIsOIDPrefixed(&packet, prefix))
675 {
676 DEBUG_printf(("_cupsSNMPWalk: Returning %d\n", count));
677
678 return (count);
679 }
680
681 if (packet.error || packet.error_status)
682 {
683 DEBUG_printf(("_cupsSNMPWalk: Returning %d\n", count > 0 ? count : -1));
684
685 return (count > 0 ? count : -1);
686 }
687
688 count ++;
689
690 (*cb)(&packet, data);
691 }
692 }
693
694
695 /*
696 * '_cupsSNMPWrite()' - Send an SNMP query packet.
697 *
698 * The array pointed to by "oid" is terminated by the value -1.
699 *
700 * @since CUPS 1.4@
701 */
702
703 int /* O - 1 on success, 0 on error */
704 _cupsSNMPWrite(
705 int fd, /* I - SNMP socket */
706 http_addr_t *address, /* I - Address to send to */
707 int version, /* I - SNMP version */
708 const char *community, /* I - Community name */
709 cups_asn1_t request_type, /* I - Request type */
710 const unsigned request_id, /* I - Request ID */
711 const int *oid) /* I - OID */
712 {
713 int i; /* Looping var */
714 cups_snmp_t packet; /* SNMP message packet */
715 unsigned char buffer[CUPS_SNMP_MAX_PACKET];
716 /* SNMP message buffer */
717 int bytes; /* Size of message */
718
719
720 /*
721 * Range check input...
722 */
723
724 DEBUG_printf(("_cupsSNMPWrite(fd=%d, address=%p, version=%d, "
725 "community=\"%s\", request_type=%d, request_id=%u, oid=%p)\n",
726 fd, address, version, community ? community : "(null)",
727 request_type, request_id, oid));
728
729 if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
730 (request_type != CUPS_ASN1_GET_REQUEST &&
731 request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid)
732 {
733 DEBUG_puts("_cupsSNMPWrite: Returning 0 (bad arguments)");
734
735 return (0);
736 }
737
738 /*
739 * Create the SNMP message...
740 */
741
742 memset(&packet, 0, sizeof(packet));
743
744 packet.version = version;
745 packet.request_type = request_type;
746 packet.request_id = request_id;
747 packet.object_type = CUPS_ASN1_NULL_VALUE;
748
749 strlcpy(packet.community, community, sizeof(packet.community));
750
751 for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++)
752 packet.object_name[i] = oid[i];
753 packet.object_name[i] = -1;
754
755 if (oid[i] >= 0)
756 {
757 DEBUG_puts("_cupsSNMPWrite: Returning 0 (OID too big)");
758
759 errno = E2BIG;
760 return (0);
761 }
762
763 bytes = asn1_encode_snmp(buffer, sizeof(buffer), &packet);
764
765 if (bytes < 0)
766 {
767 DEBUG_puts("_cupsSNMPWrite: Returning 0 (request too big)");
768
769 errno = E2BIG;
770 return (0);
771 }
772
773 asn1_debug("DEBUG: OUT ", buffer, bytes, 0);
774
775 /*
776 * Send the message...
777 */
778
779 #ifdef AF_INET6
780 if (address->addr.sa_family == AF_INET6)
781 address->ipv6.sin6_port = htons(CUPS_SNMP_PORT);
782 else
783 #endif /* AF_INET6 */
784 address->ipv4.sin_port = htons(CUPS_SNMP_PORT);
785
786 return (sendto(fd, buffer, bytes, 0, (void *)address,
787 httpAddrLength(address)) == bytes);
788 }
789
790
791 /*
792 * 'asn1_debug()' - Decode an ASN1-encoded message.
793 */
794
795 static void
796 asn1_debug(const char *prefix, /* I - Prefix string */
797 unsigned char *buffer, /* I - Buffer */
798 size_t len, /* I - Length of buffer */
799 int indent) /* I - Indentation */
800 {
801 int i; /* Looping var */
802 unsigned char *bufend; /* End of buffer */
803 int integer; /* Number value */
804 int oid[CUPS_SNMP_MAX_OID]; /* OID value */
805 char string[CUPS_SNMP_MAX_STRING];
806 /* String value */
807 unsigned char value_type; /* Type of value */
808 int value_length; /* Length of value */
809 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
810
811
812 if (cg->snmp_debug <= 0)
813 return;
814
815 if (cg->snmp_debug > 1 && indent == 0)
816 {
817 /*
818 * Do a hex dump of the packet...
819 */
820
821 int j;
822
823 fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len);
824
825 for (i = 0; i < len; i += 16)
826 {
827 fprintf(stderr, "%s%04x:", prefix, i);
828
829 for (j = 0; j < 16 && (i + j) < len; j ++)
830 {
831 if (j && !(j & 3))
832 fprintf(stderr, " %02x", buffer[i + j]);
833 else
834 fprintf(stderr, " %02x", buffer[i + j]);
835 }
836
837 while (j < 16)
838 {
839 if (j && !(j & 3))
840 fputs(" ", stderr);
841 else
842 fputs(" ", stderr);
843
844 j ++;
845 }
846
847 fputs(" ", stderr);
848
849 for (j = 0; j < 16 && (i + j) < len; j ++)
850 if (buffer[i + j] < ' ' || buffer[i + j] >= 0x7f)
851 putc('.', stderr);
852 else
853 putc(buffer[i + j], stderr);
854
855 putc('\n', stderr);
856 }
857 }
858
859 if (indent == 0)
860 fprintf(stderr, "%sMessage:\n", prefix);
861
862 bufend = buffer + len;
863
864 while (buffer < bufend)
865 {
866 /*
867 * Get value type...
868 */
869
870 value_type = asn1_get_type(&buffer, bufend);
871 value_length = asn1_get_length(&buffer, bufend);
872
873 switch (value_type)
874 {
875 case CUPS_ASN1_BOOLEAN :
876 integer = asn1_get_integer(&buffer, bufend, value_length);
877
878 fprintf(stderr, "%s%*sBOOLEAN %d bytes %d\n", prefix, indent, "",
879 value_length, integer);
880 break;
881
882 case CUPS_ASN1_INTEGER :
883 integer = asn1_get_integer(&buffer, bufend, value_length);
884
885 fprintf(stderr, "%s%*sINTEGER %d bytes %d\n", prefix, indent, "",
886 value_length, integer);
887 break;
888
889 case CUPS_ASN1_COUNTER :
890 integer = asn1_get_integer(&buffer, bufend, value_length);
891
892 fprintf(stderr, "%s%*sCOUNTER %d bytes %u\n", prefix, indent, "",
893 value_length, (unsigned)integer);
894 break;
895
896 case CUPS_ASN1_GAUGE :
897 integer = asn1_get_integer(&buffer, bufend, value_length);
898
899 fprintf(stderr, "%s%*sGAUGE %d bytes %u\n", prefix, indent, "",
900 value_length, (unsigned)integer);
901 break;
902
903 case CUPS_ASN1_TIMETICKS :
904 integer = asn1_get_integer(&buffer, bufend, value_length);
905
906 fprintf(stderr, "%s%*sTIMETICKS %d bytes %u\n", prefix, indent, "",
907 value_length, (unsigned)integer);
908 break;
909
910 case CUPS_ASN1_OCTET_STRING :
911 fprintf(stderr, "%s%*sOCTET STRING %d bytes \"%s\"\n", prefix,
912 indent, "", value_length,
913 asn1_get_string(&buffer, bufend, value_length, string,
914 sizeof(string)));
915 break;
916
917 case CUPS_ASN1_HEX_STRING :
918 asn1_get_string(&buffer, bufend, value_length, string,
919 sizeof(string));
920 fprintf(stderr, "%s%*sHex-STRING %d bytes", prefix,
921 indent, "", value_length);
922 for (i = 0; i < value_length; i ++)
923 fprintf(stderr, " %02X", string[i] & 255);
924 putc('\n', stderr);
925 break;
926
927 case CUPS_ASN1_NULL_VALUE :
928 fprintf(stderr, "%s%*sNULL VALUE %d bytes\n", prefix, indent, "",
929 value_length);
930
931 buffer += value_length;
932 break;
933
934 case CUPS_ASN1_OID :
935 integer = asn1_get_oid(&buffer, bufend, value_length, oid,
936 CUPS_SNMP_MAX_OID);
937
938 fprintf(stderr, "%s%*sOID %d bytes ", prefix, indent, "",
939 value_length);
940 for (i = 0; i < integer; i ++)
941 fprintf(stderr, ".%d", oid[i]);
942 putc('\n', stderr);
943 break;
944
945 case CUPS_ASN1_SEQUENCE :
946 fprintf(stderr, "%s%*sSEQUENCE %d bytes\n", prefix, indent, "",
947 value_length);
948 asn1_debug(prefix, buffer, value_length, indent + 4);
949
950 buffer += value_length;
951 break;
952
953 case CUPS_ASN1_GET_NEXT_REQUEST :
954 fprintf(stderr, "%s%*sGet-Next-Request-PDU %d bytes\n", prefix,
955 indent, "", value_length);
956 asn1_debug(prefix, buffer, value_length, indent + 4);
957
958 buffer += value_length;
959 break;
960
961 case CUPS_ASN1_GET_REQUEST :
962 fprintf(stderr, "%s%*sGet-Request-PDU %d bytes\n", prefix, indent, "",
963 value_length);
964 asn1_debug(prefix, buffer, value_length, indent + 4);
965
966 buffer += value_length;
967 break;
968
969 case CUPS_ASN1_GET_RESPONSE :
970 fprintf(stderr, "%s%*sGet-Response-PDU %d bytes\n", prefix, indent,
971 "", value_length);
972 asn1_debug(prefix, buffer, value_length, indent + 4);
973
974 buffer += value_length;
975 break;
976
977 default :
978 fprintf(stderr, "%s%*sUNKNOWN(%x) %d bytes\n", prefix, indent, "",
979 value_type, value_length);
980
981 buffer += value_length;
982 break;
983 }
984 }
985 }
986
987
988 /*
989 * 'asn1_decode_snmp()' - Decode a SNMP packet.
990 */
991
992 static int /* O - 0 on success, -1 on error */
993 asn1_decode_snmp(unsigned char *buffer, /* I - Buffer */
994 size_t len, /* I - Size of buffer */
995 cups_snmp_t *packet) /* I - SNMP packet */
996 {
997 unsigned char *bufptr, /* Pointer into the data */
998 *bufend; /* End of data */
999 int length; /* Length of value */
1000
1001
1002 /*
1003 * Initialize the decoding...
1004 */
1005
1006 memset(packet, 0, sizeof(cups_snmp_t));
1007 packet->object_name[0] = -1;
1008
1009 bufptr = buffer;
1010 bufend = buffer + len;
1011
1012 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE)
1013 snmp_set_error(packet, _("Packet does not start with SEQUENCE"));
1014 else if (asn1_get_length(&bufptr, bufend) == 0)
1015 snmp_set_error(packet, _("SEQUENCE uses indefinite length"));
1016 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
1017 snmp_set_error(packet, _("No version number"));
1018 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1019 snmp_set_error(packet, _("Version uses indefinite length"));
1020 else if ((packet->version = asn1_get_integer(&bufptr, bufend, length))
1021 != CUPS_SNMP_VERSION_1)
1022 snmp_set_error(packet, _("Bad SNMP version number"));
1023 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OCTET_STRING)
1024 snmp_set_error(packet, _("No community name"));
1025 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1026 snmp_set_error(packet, _("Community name uses indefinite length"));
1027 else
1028 {
1029 asn1_get_string(&bufptr, bufend, length, packet->community,
1030 sizeof(packet->community));
1031
1032 if ((packet->request_type = asn1_get_type(&bufptr, bufend))
1033 != CUPS_ASN1_GET_RESPONSE)
1034 snmp_set_error(packet, _("Packet does not contain a Get-Response-PDU"));
1035 else if (asn1_get_length(&bufptr, bufend) == 0)
1036 snmp_set_error(packet, _("Get-Response-PDU uses indefinite length"));
1037 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
1038 snmp_set_error(packet, _("No request-id"));
1039 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1040 snmp_set_error(packet, _("request-id uses indefinite length"));
1041 else
1042 {
1043 packet->request_id = asn1_get_integer(&bufptr, bufend, length);
1044
1045 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
1046 snmp_set_error(packet, _("No error-status"));
1047 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1048 snmp_set_error(packet, _("error-status uses indefinite length"));
1049 else
1050 {
1051 packet->error_status = asn1_get_integer(&bufptr, bufend, length);
1052
1053 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
1054 snmp_set_error(packet, _("No error-index"));
1055 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1056 snmp_set_error(packet, _("error-index uses indefinite length"));
1057 else
1058 {
1059 packet->error_index = asn1_get_integer(&bufptr, bufend, length);
1060
1061 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE)
1062 snmp_set_error(packet, _("No variable-bindings SEQUENCE"));
1063 else if (asn1_get_length(&bufptr, bufend) == 0)
1064 snmp_set_error(packet,
1065 _("variable-bindings uses indefinite length"));
1066 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE)
1067 snmp_set_error(packet, _("No VarBind SEQUENCE"));
1068 else if (asn1_get_length(&bufptr, bufend) == 0)
1069 snmp_set_error(packet, _("VarBind uses indefinite length"));
1070 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OID)
1071 snmp_set_error(packet, _("No name OID"));
1072 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1073 snmp_set_error(packet, _("Name OID uses indefinite length"));
1074 else
1075 {
1076 asn1_get_oid(&bufptr, bufend, length, packet->object_name,
1077 CUPS_SNMP_MAX_OID);
1078
1079 packet->object_type = asn1_get_type(&bufptr, bufend);
1080
1081 if ((length = asn1_get_length(&bufptr, bufend)) == 0 &&
1082 packet->object_type != CUPS_ASN1_NULL_VALUE &&
1083 packet->object_type != CUPS_ASN1_OCTET_STRING)
1084 snmp_set_error(packet, _("Value uses indefinite length"));
1085 else
1086 {
1087 switch (packet->object_type)
1088 {
1089 case CUPS_ASN1_BOOLEAN :
1090 packet->object_value.boolean =
1091 asn1_get_integer(&bufptr, bufend, length);
1092 break;
1093
1094 case CUPS_ASN1_INTEGER :
1095 packet->object_value.integer =
1096 asn1_get_integer(&bufptr, bufend, length);
1097 break;
1098
1099 case CUPS_ASN1_NULL_VALUE :
1100 break;
1101
1102 case CUPS_ASN1_OCTET_STRING :
1103 asn1_get_string(&bufptr, bufend, length,
1104 packet->object_value.string,
1105 CUPS_SNMP_MAX_STRING);
1106 break;
1107
1108 case CUPS_ASN1_OID :
1109 asn1_get_oid(&bufptr, bufend, length,
1110 packet->object_value.oid, CUPS_SNMP_MAX_OID);
1111 break;
1112
1113 case CUPS_ASN1_HEX_STRING :
1114 packet->object_value.hex_string.num_bytes = length;
1115
1116 asn1_get_string(&bufptr, bufend, length,
1117 (char *)packet->object_value.hex_string.bytes,
1118 CUPS_SNMP_MAX_STRING);
1119 break;
1120
1121 case CUPS_ASN1_COUNTER :
1122 packet->object_value.counter =
1123 asn1_get_integer(&bufptr, bufend, length);
1124 break;
1125
1126 case CUPS_ASN1_GAUGE :
1127 packet->object_value.gauge =
1128 asn1_get_integer(&bufptr, bufend, length);
1129 break;
1130
1131 case CUPS_ASN1_TIMETICKS :
1132 packet->object_value.timeticks =
1133 asn1_get_integer(&bufptr, bufend, length);
1134 break;
1135
1136 default :
1137 snmp_set_error(packet, _("Unsupported value type"));
1138 break;
1139 }
1140 }
1141 }
1142 }
1143 }
1144 }
1145 }
1146
1147 return (packet->error ? -1 : 0);
1148 }
1149
1150
1151 /*
1152 * 'asn1_encode_snmp()' - Encode a SNMP packet.
1153 */
1154
1155 static int /* O - Length on success, -1 on error */
1156 asn1_encode_snmp(unsigned char *buffer, /* I - Buffer */
1157 size_t bufsize, /* I - Size of buffer */
1158 cups_snmp_t *packet) /* I - SNMP packet */
1159 {
1160 unsigned char *bufptr; /* Pointer into buffer */
1161 int total, /* Total length */
1162 msglen, /* Length of entire message */
1163 commlen, /* Length of community string */
1164 reqlen, /* Length of request */
1165 listlen, /* Length of variable list */
1166 varlen, /* Length of variable */
1167 namelen, /* Length of object name OID */
1168 valuelen; /* Length of object value */
1169
1170
1171 /*
1172 * Get the lengths of the community string, OID, and message...
1173 */
1174
1175
1176 namelen = asn1_size_oid(packet->object_name);
1177
1178 switch (packet->object_type)
1179 {
1180 case CUPS_ASN1_NULL_VALUE :
1181 valuelen = 0;
1182 break;
1183
1184 case CUPS_ASN1_BOOLEAN :
1185 valuelen = asn1_size_integer(packet->object_value.boolean);
1186 break;
1187
1188 case CUPS_ASN1_INTEGER :
1189 valuelen = asn1_size_integer(packet->object_value.integer);
1190 break;
1191
1192 case CUPS_ASN1_OCTET_STRING :
1193 valuelen = strlen(packet->object_value.string);
1194 break;
1195
1196 case CUPS_ASN1_OID :
1197 valuelen = asn1_size_oid(packet->object_value.oid);
1198 break;
1199
1200 default :
1201 packet->error = "Unknown object type";
1202 return (-1);
1203 }
1204
1205 varlen = 1 + asn1_size_length(namelen) + namelen +
1206 1 + asn1_size_length(valuelen) + valuelen;
1207 listlen = 1 + asn1_size_length(varlen) + varlen;
1208 reqlen = 2 + asn1_size_integer(packet->request_id) +
1209 2 + asn1_size_integer(packet->error_status) +
1210 2 + asn1_size_integer(packet->error_index) +
1211 1 + asn1_size_length(listlen) + listlen;
1212 commlen = strlen(packet->community);
1213 msglen = 2 + asn1_size_integer(packet->version) +
1214 1 + asn1_size_length(commlen) + commlen +
1215 1 + asn1_size_length(reqlen) + reqlen;
1216 total = 1 + asn1_size_length(msglen) + msglen;
1217
1218 if (total > bufsize)
1219 {
1220 packet->error = "Message too large for buffer";
1221 return (-1);
1222 }
1223
1224 /*
1225 * Then format the message...
1226 */
1227
1228 bufptr = buffer;
1229
1230 *bufptr++ = CUPS_ASN1_SEQUENCE; /* SNMPv1 message header */
1231 asn1_set_length(&bufptr, msglen);
1232
1233 asn1_set_integer(&bufptr, packet->version);
1234 /* version */
1235
1236 *bufptr++ = CUPS_ASN1_OCTET_STRING; /* community */
1237 asn1_set_length(&bufptr, commlen);
1238 memcpy(bufptr, packet->community, commlen);
1239 bufptr += commlen;
1240
1241 *bufptr++ = packet->request_type; /* Get-Request-PDU/Get-Next-Request-PDU */
1242 asn1_set_length(&bufptr, reqlen);
1243
1244 asn1_set_integer(&bufptr, packet->request_id);
1245
1246 asn1_set_integer(&bufptr, packet->error_status);
1247
1248 asn1_set_integer(&bufptr, packet->error_index);
1249
1250 *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable-bindings */
1251 asn1_set_length(&bufptr, listlen);
1252
1253 *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable */
1254 asn1_set_length(&bufptr, varlen);
1255
1256 asn1_set_oid(&bufptr, packet->object_name);
1257 /* ObjectName */
1258
1259 switch (packet->object_type)
1260 {
1261 case CUPS_ASN1_NULL_VALUE :
1262 *bufptr++ = CUPS_ASN1_NULL_VALUE;
1263 /* ObjectValue */
1264 *bufptr++ = 0; /* Length */
1265 break;
1266
1267 case CUPS_ASN1_BOOLEAN :
1268 asn1_set_integer(&bufptr, packet->object_value.boolean);
1269 break;
1270
1271 case CUPS_ASN1_INTEGER :
1272 asn1_set_integer(&bufptr, packet->object_value.integer);
1273 break;
1274
1275 case CUPS_ASN1_OCTET_STRING :
1276 *bufptr++ = CUPS_ASN1_OCTET_STRING;
1277 asn1_set_length(&bufptr, valuelen);
1278 memcpy(bufptr, packet->object_value.string, valuelen);
1279 bufptr += valuelen;
1280 break;
1281
1282 case CUPS_ASN1_OID :
1283 asn1_set_oid(&bufptr, packet->object_value.oid);
1284 break;
1285
1286 default :
1287 break;
1288 }
1289
1290 return (bufptr - buffer);
1291 }
1292
1293
1294 /*
1295 * 'asn1_get_integer()' - Get an integer value.
1296 */
1297
1298 static int /* O - Integer value */
1299 asn1_get_integer(
1300 unsigned char **buffer, /* IO - Pointer in buffer */
1301 unsigned char *bufend, /* I - End of buffer */
1302 int length) /* I - Length of value */
1303 {
1304 int value; /* Integer value */
1305
1306
1307 for (value = 0;
1308 length > 0 && *buffer < bufend;
1309 length --, (*buffer) ++)
1310 value = (value << 8) | **buffer;
1311
1312 return (value);
1313 }
1314
1315
1316 /*
1317 * 'asn1_get_length()' - Get a value length.
1318 */
1319
1320 static int /* O - Length */
1321 asn1_get_length(unsigned char **buffer, /* IO - Pointer in buffer */
1322 unsigned char *bufend) /* I - End of buffer */
1323 {
1324 int length; /* Length */
1325
1326
1327 length = **buffer;
1328 (*buffer) ++;
1329
1330 if (length & 128)
1331 length = asn1_get_integer(buffer, bufend, length & 127);
1332
1333 return (length);
1334 }
1335
1336
1337 /*
1338 * 'asn1_get_oid()' - Get an OID value.
1339 */
1340
1341 static int /* O - Number of OIDs */
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 */
1350 int *oidptr, /* Current OID */
1351 *oidend; /* End of OID buffer */
1352 int number; /* OID number */
1353
1354
1355 valend = *buffer + length;
1356 oidptr = oid;
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 {
1366 *oidptr++ = number / 40;
1367 number = number % 40;
1368 *oidptr++ = number;
1369 }
1370 else
1371 {
1372 *oidptr++ = 2;
1373 number -= 80;
1374 *oidptr++ = number;
1375 }
1376
1377 while (*buffer < valend)
1378 {
1379 number = asn1_get_packed(buffer, bufend);
1380
1381 if (oidptr < oidend)
1382 *oidptr++ = number;
1383 }
1384
1385 *oidptr = -1;
1386
1387 return (oidptr - oid);
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 */
1550 int length) /* I - Length value */
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
1589 if (oid[1] < 0)
1590 {
1591 asn1_set_packed(buffer, oid[0] * 40);
1592 return;
1593 }
1594
1595 asn1_set_packed(buffer, oid[0] * 40 + oid[1]);
1596
1597 for (oid += 2; *oid >= 0; oid ++)
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 {
1612 **buffer = ((integer >> 28) & 0x7f) | 0x80;
1613 (*buffer) ++;
1614 }
1615
1616 if (integer > 0x1fffff)
1617 {
1618 **buffer = ((integer >> 21) & 0x7f) | 0x80;
1619 (*buffer) ++;
1620 }
1621
1622 if (integer > 0x3fff)
1623 {
1624 **buffer = ((integer >> 14) & 0x7f) | 0x80;
1625 (*buffer) ++;
1626 }
1627
1628 if (integer > 0x7f)
1629 {
1630 **buffer = ((integer >> 7) & 0x7f) | 0x80;
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
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 ++)
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 */