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