2 * "$Id: snmp.c 9793 2011-05-20 03:49:49Z mike $"
4 * SNMP functions for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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
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
48 * asn1_size_length() - Figure out the number of bytes needed for a
50 * asn1_size_oid() - Figure out the numebr of bytes needed for an
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.
58 * Include necessary headers.
61 #include "cups-private.h"
62 #include "snmp-private.h"
64 # include <sys/poll.h>
65 #endif /* HAVE_POLL */
72 static void asn1_debug(const char *prefix, unsigned char *buffer,
73 size_t len, int indent);
74 static int asn1_decode_snmp(unsigned char *buffer, size_t len,
76 static int asn1_encode_snmp(unsigned char *buffer, size_t len,
78 static int asn1_get_integer(unsigned char **buffer,
79 unsigned char *bufend,
81 static int asn1_get_oid(unsigned char **buffer,
82 unsigned char *bufend,
83 int length, int *oid, int oidsize);
84 static int asn1_get_packed(unsigned char **buffer,
85 unsigned char *bufend);
86 static char *asn1_get_string(unsigned char **buffer,
87 unsigned char *bufend,
88 int length, char *string,
90 static unsigned asn1_get_length(unsigned char **buffer,
91 unsigned char *bufend);
92 static int asn1_get_type(unsigned char **buffer,
93 unsigned char *bufend);
94 static void asn1_set_integer(unsigned char **buffer,
96 static void asn1_set_length(unsigned char **buffer,
98 static void asn1_set_oid(unsigned char **buffer,
100 static void asn1_set_packed(unsigned char **buffer,
102 static int asn1_size_integer(int integer);
103 static int asn1_size_length(int length);
104 static int asn1_size_oid(const int *oid);
105 static int asn1_size_packed(int integer);
106 static void snmp_set_error(cups_snmp_t *packet,
107 const char *message);
111 * '_cupsSNMPClose()' - Close a SNMP socket.
115 _cupsSNMPClose(int fd) /* I - SNMP socket file descriptor */
117 DEBUG_printf(("4_cupsSNMPClose(fd=%d)", fd));
128 * '_cupsSNMPCopyOID()' - Copy an OID.
130 * The array pointed to by "src" is terminated by the value -1.
133 int * /* O - New OID */
134 _cupsSNMPCopyOID(int *dst, /* I - Destination OID */
135 const int *src, /* I - Source OID */
136 int dstsize) /* I - Number of integers in dst */
138 int i; /* Looping var */
141 DEBUG_printf(("4_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)", dst, src,
144 for (i = 0, dstsize --; src[i] >= 0 && i < dstsize; i ++)
154 * '_cupsSNMPDefaultCommunity()' - Get the default SNMP community name.
156 * The default community name is the first community name found in the
157 * snmp.conf file. If no community name is defined there, "public" is used.
160 const char * /* O - Default community name */
161 _cupsSNMPDefaultCommunity(void)
163 cups_file_t *fp; /* snmp.conf file */
164 char line[1024], /* Line from file */
165 *value; /* Value from file */
166 int linenum; /* Line number in file */
167 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
170 DEBUG_puts("4_cupsSNMPDefaultCommunity()");
172 if (!cg->snmp_community[0])
174 strlcpy(cg->snmp_community, "public", sizeof(cg->snmp_community));
176 snprintf(line, sizeof(line), "%s/snmp.conf", cg->cups_serverroot);
177 if ((fp = cupsFileOpen(line, "r")) != NULL)
180 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
181 if (!_cups_strcasecmp(line, "Community") && value)
183 strlcpy(cg->snmp_community, value, sizeof(cg->snmp_community));
191 DEBUG_printf(("5_cupsSNMPDefaultCommunity: Returning \"%s\"",
192 cg->snmp_community));
194 return (cg->snmp_community);
199 * '_cupsSNMPIsOID()' - Test whether a SNMP response contains the specified OID.
201 * The array pointed to by "oid" is terminated by the value -1.
204 int /* O - 1 if equal, 0 if not equal */
205 _cupsSNMPIsOID(cups_snmp_t *packet, /* I - Response packet */
206 const int *oid) /* I - OID */
208 int i; /* Looping var */
212 * Range check input...
215 DEBUG_printf(("4_cupsSNMPIsOID(packet=%p, oid=%p)", packet, oid));
219 DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
229 i < CUPS_SNMP_MAX_OID && oid[i] >= 0 && packet->object_name[i] >= 0;
231 if (oid[i] != packet->object_name[i])
233 DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
238 DEBUG_printf(("5_cupsSNMPIsOID: Returning %d",
239 i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]));
241 return (i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]);
246 * '_cupsSNMPIsOIDPrefixed()' - Test whether a SNMP response uses the specified
249 * The array pointed to by "prefix" is terminated by the value -1.
252 int /* O - 1 if prefixed, 0 if not prefixed */
253 _cupsSNMPIsOIDPrefixed(
254 cups_snmp_t *packet, /* I - Response packet */
255 const int *prefix) /* I - OID prefix */
257 int i; /* Looping var */
261 * Range check input...
264 DEBUG_printf(("4_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)", packet,
267 if (!packet || !prefix)
269 DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
279 i < CUPS_SNMP_MAX_OID && prefix[i] >= 0 && packet->object_name[i] >= 0;
281 if (prefix[i] != packet->object_name[i])
283 DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
288 DEBUG_printf(("5_cupsSNMPIsOIDPrefixed: Returning %d",
289 i < CUPS_SNMP_MAX_OID));
291 return (i < CUPS_SNMP_MAX_OID);
296 * '_cupsSNMPOIDToString()' - Convert an OID to a string.
300 char * /* O - New string or @code NULL@ on error */
301 _cupsSNMPOIDToString(const int *src, /* I - OID */
302 char *dst, /* I - String buffer */
303 size_t dstsize) /* I - Size of string buffer */
305 char *dstptr, /* Pointer into string buffer */
306 *dstend; /* End of string buffer */
309 DEBUG_printf(("4_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT ")",
310 src, dst, CUPS_LLCAST dstsize));
313 * Range check input...
316 if (!src || !dst || dstsize < 4)
320 * Loop through the OID array and build a string...
323 for (dstptr = dst, dstend = dstptr + dstsize - 1;
324 *src >= 0 && dstptr < dstend;
325 src ++, dstptr += strlen(dstptr))
326 snprintf(dstptr, dstend - dstptr + 1, ".%d", *src);
336 * '_cupsSNMPOpen()' - Open a SNMP socket.
339 int /* O - SNMP socket file descriptor */
340 _cupsSNMPOpen(int family) /* I - Address family - @code AF_INET@ or @code AF_INET6@ */
342 int fd; /* SNMP socket file descriptor */
343 int val; /* Socket option value */
347 * Create the SNMP socket...
350 DEBUG_printf(("4_cupsSNMPOpen(family=%d)", family));
352 if ((fd = socket(family, SOCK_DGRAM, 0)) < 0)
354 DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno)));
360 * Set the "broadcast" flag...
366 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *)&val, sizeof(val)))
368 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
371 DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno)));
378 DEBUG_printf(("5_cupsSNMPOpen: Returning %d", fd));
385 * '_cupsSNMPRead()' - Read and parse a SNMP response.
387 * If "timeout" is negative, @code _cupsSNMPRead@ will wait for a response
391 cups_snmp_t * /* O - SNMP packet or @code NULL@ if none */
392 _cupsSNMPRead(int fd, /* I - SNMP socket file descriptor */
393 cups_snmp_t *packet, /* I - SNMP packet buffer */
394 double timeout) /* I - Timeout in seconds */
396 unsigned char buffer[CUPS_SNMP_MAX_PACKET];
398 int bytes; /* Number of bytes received */
399 socklen_t addrlen; /* Source address length */
400 http_addr_t address; /* Source address */
404 * Range check input...
407 DEBUG_printf(("4_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)", fd, packet,
410 if (fd < 0 || !packet)
412 DEBUG_puts("5_cupsSNMPRead: Returning NULL");
418 * Optionally wait for a response...
423 int ready; /* Data ready on socket? */
425 struct pollfd pfd; /* Polled file descriptor */
430 while ((ready = poll(&pfd, 1, (int)(timeout * 1000.0))) < 0 &&
431 (errno == EINTR || errno == EAGAIN));
434 fd_set input_set; /* select() input set */
435 struct timeval stimeout; /* select() timeout */
440 FD_SET(fd, &input_set);
442 stimeout.tv_sec = (int)timeout;
443 stimeout.tv_usec = (int)((timeout - stimeout.tv_sec) * 1000000);
445 ready = select(fd + 1, &input_set, NULL, NULL, &stimeout);
448 while (ready < 0 && WSAGetLastError() == WSAEINTR);
450 while (ready < 0 && (errno == EINTR || errno == EAGAIN));
452 #endif /* HAVE_POLL */
455 * If we don't have any data ready, return right away...
460 DEBUG_puts("5_cupsSNMPRead: Returning NULL (timeout)");
467 * Read the response data...
470 addrlen = sizeof(address);
472 if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address,
475 DEBUG_printf(("5_cupsSNMPRead: Returning NULL (%s)", strerror(errno)));
481 * Look for the response status code in the SNMP message header...
484 asn1_debug("DEBUG: IN ", buffer, bytes, 0);
486 asn1_decode_snmp(buffer, bytes, packet);
488 memcpy(&(packet->address), &address, sizeof(packet->address));
491 * Return decoded data packet...
494 DEBUG_puts("5_cupsSNMPRead: Returning packet");
501 * '_cupsSNMPSetDebug()' - Enable/disable debug logging to stderr.
505 _cupsSNMPSetDebug(int level) /* I - 1 to enable debug output, 0 otherwise */
507 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
510 DEBUG_printf(("4_cupsSNMPSetDebug(level=%d)", level));
512 cg->snmp_debug = level;
517 * '_cupsSNMPStringToOID()' - Convert a numeric OID string to an OID array.
519 * This function converts a string of the form ".N.N.N.N.N" to the
520 * corresponding OID array terminated by -1.
522 * @code NULL@ is returned if the array is not large enough or the string is
523 * not a valid OID number.
526 int * /* O - Pointer to OID array or @code NULL@ on error */
527 _cupsSNMPStringToOID(const char *src, /* I - OID string */
528 int *dst, /* I - OID array */
529 int dstsize)/* I - Number of integers in OID array */
531 int *dstptr, /* Pointer into OID array */
532 *dstend; /* End of OID array */
535 DEBUG_printf(("4_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)",
539 * Range check input...
542 if (!src || !dst || dstsize < 2)
546 * Skip leading "."...
553 * Loop to the end of the string...
556 for (dstend = dst + dstsize - 1, dstptr = dst, *dstptr = 0;
557 *src && dstptr < dstend;
565 else if (isdigit(*src & 255))
566 *dstptr = *dstptr * 10 + *src - '0';
575 * Terminate the end of the OID array and return...
585 * '_cupsSNMPWalk()' - Enumerate a group of OIDs.
587 * This function queries all of the OIDs with the specified OID prefix,
588 * calling the "cb" function for every response that is received.
590 * The array pointed to by "prefix" is terminated by the value -1.
592 * If "timeout" is negative, @code _cupsSNMPWalk@ will wait for a response
596 int /* O - Number of OIDs found or -1 on error */
597 _cupsSNMPWalk(int fd, /* I - SNMP socket */
598 http_addr_t *address, /* I - Address to query */
599 int version, /* I - SNMP version */
600 const char *community,/* I - Community name */
601 const int *prefix, /* I - OID prefix */
602 double timeout, /* I - Timeout for each response in seconds */
603 cups_snmp_cb_t cb, /* I - Function to call for each response */
604 void *data) /* I - User data pointer that is passed to the callback function */
606 int count = 0; /* Number of OIDs found */
607 int request_id = 0; /* Current request ID */
608 cups_snmp_t packet; /* Current response packet */
609 int lastoid[CUPS_SNMP_MAX_OID];
610 /* Last OID we got */
614 * Range check input...
617 DEBUG_printf(("4_cupsSNMPWalk(fd=%d, address=%p, version=%d, "
618 "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)",
619 fd, address, version, community, prefix, timeout, cb, data));
621 if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
624 DEBUG_puts("5_cupsSNMPWalk: Returning -1");
630 * Copy the OID prefix and then loop until we have no more OIDs...
633 _cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID);
640 if (!_cupsSNMPWrite(fd, address, version, community,
641 CUPS_ASN1_GET_NEXT_REQUEST, request_id,
644 DEBUG_puts("5_cupsSNMPWalk: Returning -1");
649 if (!_cupsSNMPRead(fd, &packet, timeout))
651 DEBUG_puts("5_cupsSNMPWalk: Returning -1");
656 if (!_cupsSNMPIsOIDPrefixed(&packet, prefix) ||
657 _cupsSNMPIsOID(&packet, lastoid))
659 DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count));
664 if (packet.error || packet.error_status)
666 DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count > 0 ? count : -1));
668 return (count > 0 ? count : -1);
671 _cupsSNMPCopyOID(lastoid, packet.object_name, CUPS_SNMP_MAX_OID);
675 (*cb)(&packet, data);
681 * '_cupsSNMPWrite()' - Send an SNMP query packet.
683 * The array pointed to by "oid" is terminated by the value -1.
686 int /* O - 1 on success, 0 on error */
688 int fd, /* I - SNMP socket */
689 http_addr_t *address, /* I - Address to send to */
690 int version, /* I - SNMP version */
691 const char *community, /* I - Community name */
692 cups_asn1_t request_type, /* I - Request type */
693 const unsigned request_id, /* I - Request ID */
694 const int *oid) /* I - OID */
696 int i; /* Looping var */
697 cups_snmp_t packet; /* SNMP message packet */
698 unsigned char buffer[CUPS_SNMP_MAX_PACKET];
699 /* SNMP message buffer */
700 int bytes; /* Size of message */
701 http_addr_t temp; /* Copy of address */
705 * Range check input...
708 DEBUG_printf(("4_cupsSNMPWrite(fd=%d, address=%p, version=%d, "
709 "community=\"%s\", request_type=%d, request_id=%u, oid=%p)",
710 fd, address, version, community, request_type, request_id, oid));
712 if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
713 (request_type != CUPS_ASN1_GET_REQUEST &&
714 request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid)
716 DEBUG_puts("5_cupsSNMPWrite: Returning 0 (bad arguments)");
722 * Create the SNMP message...
725 memset(&packet, 0, sizeof(packet));
727 packet.version = version;
728 packet.request_type = request_type;
729 packet.request_id = request_id;
730 packet.object_type = CUPS_ASN1_NULL_VALUE;
732 strlcpy(packet.community, community, sizeof(packet.community));
734 for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++)
735 packet.object_name[i] = oid[i];
736 packet.object_name[i] = -1;
740 DEBUG_puts("5_cupsSNMPWrite: Returning 0 (OID too big)");
746 bytes = asn1_encode_snmp(buffer, sizeof(buffer), &packet);
750 DEBUG_puts("5_cupsSNMPWrite: Returning 0 (request too big)");
756 asn1_debug("DEBUG: OUT ", buffer, bytes, 0);
759 * Send the message...
764 _httpAddrSetPort(&temp, CUPS_SNMP_PORT);
766 return (sendto(fd, buffer, bytes, 0, (void *)&temp,
767 httpAddrLength(&temp)) == bytes);
772 * 'asn1_debug()' - Decode an ASN1-encoded message.
776 asn1_debug(const char *prefix, /* I - Prefix string */
777 unsigned char *buffer, /* I - Buffer */
778 size_t len, /* I - Length of buffer */
779 int indent) /* I - Indentation */
781 int i; /* Looping var */
782 unsigned char *bufend; /* End of buffer */
783 int integer; /* Number value */
784 int oid[CUPS_SNMP_MAX_OID]; /* OID value */
785 char string[CUPS_SNMP_MAX_STRING];
787 unsigned char value_type; /* Type of value */
788 int value_length; /* Length of value */
789 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
792 if (cg->snmp_debug <= 0)
795 if (cg->snmp_debug > 1 && indent == 0)
798 * Do a hex dump of the packet...
803 fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len);
805 for (i = 0; i < (int)len; i += 16)
807 fprintf(stderr, "%s%04x:", prefix, i);
809 for (j = 0; j < 16 && (i + j) < (int)len; j ++)
812 fprintf(stderr, " %02x", buffer[i + j]);
814 fprintf(stderr, " %02x", buffer[i + j]);
829 for (j = 0; j < 16 && (i + j) < (int)len; j ++)
830 if (buffer[i + j] < ' ' || buffer[i + j] >= 0x7f)
833 putc(buffer[i + j], stderr);
840 fprintf(stderr, "%sMessage:\n", prefix);
842 bufend = buffer + len;
844 while (buffer < bufend)
850 value_type = asn1_get_type(&buffer, bufend);
851 value_length = asn1_get_length(&buffer, bufend);
855 case CUPS_ASN1_BOOLEAN :
856 integer = asn1_get_integer(&buffer, bufend, value_length);
858 fprintf(stderr, "%s%*sBOOLEAN %d bytes %d\n", prefix, indent, "",
859 value_length, integer);
862 case CUPS_ASN1_INTEGER :
863 integer = asn1_get_integer(&buffer, bufend, value_length);
865 fprintf(stderr, "%s%*sINTEGER %d bytes %d\n", prefix, indent, "",
866 value_length, integer);
869 case CUPS_ASN1_COUNTER :
870 integer = asn1_get_integer(&buffer, bufend, value_length);
872 fprintf(stderr, "%s%*sCOUNTER %d bytes %u\n", prefix, indent, "",
873 value_length, (unsigned)integer);
876 case CUPS_ASN1_GAUGE :
877 integer = asn1_get_integer(&buffer, bufend, value_length);
879 fprintf(stderr, "%s%*sGAUGE %d bytes %u\n", prefix, indent, "",
880 value_length, (unsigned)integer);
883 case CUPS_ASN1_TIMETICKS :
884 integer = asn1_get_integer(&buffer, bufend, value_length);
886 fprintf(stderr, "%s%*sTIMETICKS %d bytes %u\n", prefix, indent, "",
887 value_length, (unsigned)integer);
890 case CUPS_ASN1_OCTET_STRING :
891 fprintf(stderr, "%s%*sOCTET STRING %d bytes \"%s\"\n", prefix,
892 indent, "", value_length,
893 asn1_get_string(&buffer, bufend, value_length, string,
897 case CUPS_ASN1_HEX_STRING :
898 asn1_get_string(&buffer, bufend, value_length, string,
900 fprintf(stderr, "%s%*sHex-STRING %d bytes", prefix,
901 indent, "", value_length);
902 for (i = 0; i < value_length; i ++)
903 fprintf(stderr, " %02X", string[i] & 255);
907 case CUPS_ASN1_NULL_VALUE :
908 fprintf(stderr, "%s%*sNULL VALUE %d bytes\n", prefix, indent, "",
911 buffer += value_length;
915 integer = asn1_get_oid(&buffer, bufend, value_length, oid,
918 fprintf(stderr, "%s%*sOID %d bytes ", prefix, indent, "",
920 for (i = 0; i < integer; i ++)
921 fprintf(stderr, ".%d", oid[i]);
925 case CUPS_ASN1_SEQUENCE :
926 fprintf(stderr, "%s%*sSEQUENCE %d bytes\n", prefix, indent, "",
928 asn1_debug(prefix, buffer, value_length, indent + 4);
930 buffer += value_length;
933 case CUPS_ASN1_GET_NEXT_REQUEST :
934 fprintf(stderr, "%s%*sGet-Next-Request-PDU %d bytes\n", prefix,
935 indent, "", value_length);
936 asn1_debug(prefix, buffer, value_length, indent + 4);
938 buffer += value_length;
941 case CUPS_ASN1_GET_REQUEST :
942 fprintf(stderr, "%s%*sGet-Request-PDU %d bytes\n", prefix, indent, "",
944 asn1_debug(prefix, buffer, value_length, indent + 4);
946 buffer += value_length;
949 case CUPS_ASN1_GET_RESPONSE :
950 fprintf(stderr, "%s%*sGet-Response-PDU %d bytes\n", prefix, indent,
952 asn1_debug(prefix, buffer, value_length, indent + 4);
954 buffer += value_length;
958 fprintf(stderr, "%s%*sUNKNOWN(%x) %d bytes\n", prefix, indent, "",
959 value_type, value_length);
961 buffer += value_length;
969 * 'asn1_decode_snmp()' - Decode a SNMP packet.
972 static int /* O - 0 on success, -1 on error */
973 asn1_decode_snmp(unsigned char *buffer, /* I - Buffer */
974 size_t len, /* I - Size of buffer */
975 cups_snmp_t *packet) /* I - SNMP packet */
977 unsigned char *bufptr, /* Pointer into the data */
978 *bufend; /* End of data */
979 int length; /* Length of value */
983 * Initialize the decoding...
986 memset(packet, 0, sizeof(cups_snmp_t));
987 packet->object_name[0] = -1;
990 bufend = buffer + len;
992 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE)
993 snmp_set_error(packet, _("Packet does not start with SEQUENCE"));
994 else if (asn1_get_length(&bufptr, bufend) == 0)
995 snmp_set_error(packet, _("SEQUENCE uses indefinite length"));
996 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
997 snmp_set_error(packet, _("No version number"));
998 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
999 snmp_set_error(packet, _("Version uses indefinite length"));
1000 else if ((packet->version = asn1_get_integer(&bufptr, bufend, length))
1001 != CUPS_SNMP_VERSION_1)
1002 snmp_set_error(packet, _("Bad SNMP version number"));
1003 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OCTET_STRING)
1004 snmp_set_error(packet, _("No community name"));
1005 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1006 snmp_set_error(packet, _("Community name uses indefinite length"));
1009 asn1_get_string(&bufptr, bufend, length, packet->community,
1010 sizeof(packet->community));
1012 if ((packet->request_type = asn1_get_type(&bufptr, bufend))
1013 != CUPS_ASN1_GET_RESPONSE)
1014 snmp_set_error(packet, _("Packet does not contain a Get-Response-PDU"));
1015 else if (asn1_get_length(&bufptr, bufend) == 0)
1016 snmp_set_error(packet, _("Get-Response-PDU uses indefinite length"));
1017 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
1018 snmp_set_error(packet, _("No request-id"));
1019 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1020 snmp_set_error(packet, _("request-id uses indefinite length"));
1023 packet->request_id = asn1_get_integer(&bufptr, bufend, length);
1025 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
1026 snmp_set_error(packet, _("No error-status"));
1027 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1028 snmp_set_error(packet, _("error-status uses indefinite length"));
1031 packet->error_status = asn1_get_integer(&bufptr, bufend, length);
1033 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
1034 snmp_set_error(packet, _("No error-index"));
1035 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1036 snmp_set_error(packet, _("error-index uses indefinite length"));
1039 packet->error_index = asn1_get_integer(&bufptr, bufend, length);
1041 if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE)
1042 snmp_set_error(packet, _("No variable-bindings SEQUENCE"));
1043 else if (asn1_get_length(&bufptr, bufend) == 0)
1044 snmp_set_error(packet,
1045 _("variable-bindings uses indefinite length"));
1046 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE)
1047 snmp_set_error(packet, _("No VarBind SEQUENCE"));
1048 else if (asn1_get_length(&bufptr, bufend) == 0)
1049 snmp_set_error(packet, _("VarBind uses indefinite length"));
1050 else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OID)
1051 snmp_set_error(packet, _("No name OID"));
1052 else if ((length = asn1_get_length(&bufptr, bufend)) == 0)
1053 snmp_set_error(packet, _("Name OID uses indefinite length"));
1056 asn1_get_oid(&bufptr, bufend, length, packet->object_name,
1059 packet->object_type = asn1_get_type(&bufptr, bufend);
1061 if ((length = asn1_get_length(&bufptr, bufend)) == 0 &&
1062 packet->object_type != CUPS_ASN1_NULL_VALUE &&
1063 packet->object_type != CUPS_ASN1_OCTET_STRING)
1064 snmp_set_error(packet, _("Value uses indefinite length"));
1067 switch (packet->object_type)
1069 case CUPS_ASN1_BOOLEAN :
1070 packet->object_value.boolean =
1071 asn1_get_integer(&bufptr, bufend, length);
1074 case CUPS_ASN1_INTEGER :
1075 packet->object_value.integer =
1076 asn1_get_integer(&bufptr, bufend, length);
1079 case CUPS_ASN1_NULL_VALUE :
1082 case CUPS_ASN1_OCTET_STRING :
1083 case CUPS_ASN1_BIT_STRING :
1084 case CUPS_ASN1_HEX_STRING :
1085 packet->object_value.string.num_bytes = length;
1086 asn1_get_string(&bufptr, bufend, length,
1087 (char *)packet->object_value.string.bytes,
1088 CUPS_SNMP_MAX_STRING);
1091 case CUPS_ASN1_OID :
1092 asn1_get_oid(&bufptr, bufend, length,
1093 packet->object_value.oid, CUPS_SNMP_MAX_OID);
1096 case CUPS_ASN1_COUNTER :
1097 packet->object_value.counter =
1098 asn1_get_integer(&bufptr, bufend, length);
1101 case CUPS_ASN1_GAUGE :
1102 packet->object_value.gauge =
1103 asn1_get_integer(&bufptr, bufend, length);
1106 case CUPS_ASN1_TIMETICKS :
1107 packet->object_value.timeticks =
1108 asn1_get_integer(&bufptr, bufend, length);
1112 snmp_set_error(packet, _("Unsupported value type"));
1122 return (packet->error ? -1 : 0);
1127 * 'asn1_encode_snmp()' - Encode a SNMP packet.
1130 static int /* O - Length on success, -1 on error */
1131 asn1_encode_snmp(unsigned char *buffer, /* I - Buffer */
1132 size_t bufsize, /* I - Size of buffer */
1133 cups_snmp_t *packet) /* I - SNMP packet */
1135 unsigned char *bufptr; /* Pointer into buffer */
1136 int total, /* Total length */
1137 msglen, /* Length of entire message */
1138 commlen, /* Length of community string */
1139 reqlen, /* Length of request */
1140 listlen, /* Length of variable list */
1141 varlen, /* Length of variable */
1142 namelen, /* Length of object name OID */
1143 valuelen; /* Length of object value */
1147 * Get the lengths of the community string, OID, and message...
1151 namelen = asn1_size_oid(packet->object_name);
1153 switch (packet->object_type)
1155 case CUPS_ASN1_NULL_VALUE :
1159 case CUPS_ASN1_BOOLEAN :
1160 valuelen = asn1_size_integer(packet->object_value.boolean);
1163 case CUPS_ASN1_INTEGER :
1164 valuelen = asn1_size_integer(packet->object_value.integer);
1167 case CUPS_ASN1_OCTET_STRING :
1168 valuelen = packet->object_value.string.num_bytes;
1171 case CUPS_ASN1_OID :
1172 valuelen = asn1_size_oid(packet->object_value.oid);
1176 packet->error = "Unknown object type";
1180 varlen = 1 + asn1_size_length(namelen) + namelen +
1181 1 + asn1_size_length(valuelen) + valuelen;
1182 listlen = 1 + asn1_size_length(varlen) + varlen;
1183 reqlen = 2 + asn1_size_integer(packet->request_id) +
1184 2 + asn1_size_integer(packet->error_status) +
1185 2 + asn1_size_integer(packet->error_index) +
1186 1 + asn1_size_length(listlen) + listlen;
1187 commlen = strlen(packet->community);
1188 msglen = 2 + asn1_size_integer(packet->version) +
1189 1 + asn1_size_length(commlen) + commlen +
1190 1 + asn1_size_length(reqlen) + reqlen;
1191 total = 1 + asn1_size_length(msglen) + msglen;
1193 if (total > (int)bufsize)
1195 packet->error = "Message too large for buffer";
1200 * Then format the message...
1205 *bufptr++ = CUPS_ASN1_SEQUENCE; /* SNMPv1 message header */
1206 asn1_set_length(&bufptr, msglen);
1208 asn1_set_integer(&bufptr, packet->version);
1211 *bufptr++ = CUPS_ASN1_OCTET_STRING; /* community */
1212 asn1_set_length(&bufptr, commlen);
1213 memcpy(bufptr, packet->community, commlen);
1216 *bufptr++ = packet->request_type; /* Get-Request-PDU/Get-Next-Request-PDU */
1217 asn1_set_length(&bufptr, reqlen);
1219 asn1_set_integer(&bufptr, packet->request_id);
1221 asn1_set_integer(&bufptr, packet->error_status);
1223 asn1_set_integer(&bufptr, packet->error_index);
1225 *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable-bindings */
1226 asn1_set_length(&bufptr, listlen);
1228 *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable */
1229 asn1_set_length(&bufptr, varlen);
1231 asn1_set_oid(&bufptr, packet->object_name);
1234 switch (packet->object_type)
1236 case CUPS_ASN1_NULL_VALUE :
1237 *bufptr++ = CUPS_ASN1_NULL_VALUE;
1239 *bufptr++ = 0; /* Length */
1242 case CUPS_ASN1_BOOLEAN :
1243 asn1_set_integer(&bufptr, packet->object_value.boolean);
1246 case CUPS_ASN1_INTEGER :
1247 asn1_set_integer(&bufptr, packet->object_value.integer);
1250 case CUPS_ASN1_OCTET_STRING :
1251 *bufptr++ = CUPS_ASN1_OCTET_STRING;
1252 asn1_set_length(&bufptr, valuelen);
1253 memcpy(bufptr, packet->object_value.string.bytes, valuelen);
1257 case CUPS_ASN1_OID :
1258 asn1_set_oid(&bufptr, packet->object_value.oid);
1265 return (bufptr - buffer);
1270 * 'asn1_get_integer()' - Get an integer value.
1273 static int /* O - Integer value */
1275 unsigned char **buffer, /* IO - Pointer in buffer */
1276 unsigned char *bufend, /* I - End of buffer */
1277 int length) /* I - Length of value */
1279 int value; /* Integer value */
1282 if (length > sizeof(int))
1284 (*buffer) += length;
1288 for (value = (**buffer & 0x80) ? -1 : 0;
1289 length > 0 && *buffer < bufend;
1290 length --, (*buffer) ++)
1291 value = (value << 8) | **buffer;
1298 * 'asn1_get_length()' - Get a value length.
1301 static unsigned /* O - Length */
1302 asn1_get_length(unsigned char **buffer, /* IO - Pointer in buffer */
1303 unsigned char *bufend) /* I - End of buffer */
1305 unsigned length; /* Length */
1313 int count; /* Number of bytes for length */
1316 if ((count = length & 127) > sizeof(unsigned))
1323 count > 0 && *buffer < bufend;
1324 count --, (*buffer) ++)
1325 length = (length << 8) | **buffer;
1333 * 'asn1_get_oid()' - Get an OID value.
1336 static int /* O - Number of OIDs */
1338 unsigned char **buffer, /* IO - Pointer in buffer */
1339 unsigned char *bufend, /* I - End of buffer */
1340 int length, /* I - Length of value */
1341 int *oid, /* I - OID buffer */
1342 int oidsize) /* I - Size of OID buffer */
1344 unsigned char *valend; /* End of value */
1345 int *oidptr, /* Current OID */
1346 *oidend; /* End of OID buffer */
1347 int number; /* OID number */
1350 valend = *buffer + length;
1352 oidend = oid + oidsize - 1;
1354 if (valend > bufend)
1357 number = asn1_get_packed(buffer, bufend);
1361 *oidptr++ = number / 40;
1362 number = number % 40;
1372 while (*buffer < valend)
1374 number = asn1_get_packed(buffer, bufend);
1376 if (oidptr < oidend)
1382 return (oidptr - oid);
1387 * 'asn1_get_packed()' - Get a packed integer value.
1390 static int /* O - Value */
1392 unsigned char **buffer, /* IO - Pointer in buffer */
1393 unsigned char *bufend) /* I - End of buffer */
1395 int value; /* Value */
1400 while ((**buffer & 128) && *buffer < bufend)
1402 value = (value << 7) | (**buffer & 127);
1406 if (*buffer < bufend)
1408 value = (value << 7) | **buffer;
1417 * 'asn1_get_string()' - Get a string value.
1420 static char * /* O - String */
1422 unsigned char **buffer, /* IO - Pointer in buffer */
1423 unsigned char *bufend, /* I - End of buffer */
1424 int length, /* I - Value length */
1425 char *string, /* I - String buffer */
1426 int strsize) /* I - String buffer size */
1428 if (length > (bufend - *buffer))
1429 length = bufend - *buffer;
1434 * Disallow negative lengths!
1439 else if (length < strsize)
1442 * String is smaller than the buffer...
1446 memcpy(string, *buffer, length);
1448 string[length] = '\0';
1453 * String is larger than the buffer...
1456 memcpy(string, *buffer, strsize - 1);
1457 string[strsize - 1] = '\0';
1461 (*buffer) += length;
1463 return (length < 0 ? NULL : string);
1468 * 'asn1_get_type()' - Get a value type.
1471 static int /* O - Type */
1472 asn1_get_type(unsigned char **buffer, /* IO - Pointer in buffer */
1473 unsigned char *bufend) /* I - End of buffer */
1475 int type; /* Type */
1481 if ((type & 31) == 31)
1482 type = asn1_get_packed(buffer, bufend);
1489 * 'asn1_set_integer()' - Set an integer value.
1493 asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */
1494 int integer) /* I - Integer value */
1496 **buffer = CUPS_ASN1_INTEGER;
1499 if (integer > 0x7fffff || integer < -0x800000)
1503 **buffer = integer >> 24;
1505 **buffer = integer >> 16;
1507 **buffer = integer >> 8;
1512 else if (integer > 0x7fff || integer < -0x8000)
1516 **buffer = integer >> 16;
1518 **buffer = integer >> 8;
1523 else if (integer > 0x7f || integer < -0x80)
1527 **buffer = integer >> 8;
1543 * 'asn1_set_length()' - Set a value length.
1547 asn1_set_length(unsigned char **buffer, /* IO - Pointer in buffer */
1548 unsigned length) /* I - Length value */
1552 **buffer = 0x82; /* 2-byte length */
1554 **buffer = length >> 8;
1559 else if (length > 127)
1561 **buffer = 0x81; /* 1-byte length */
1568 **buffer = length; /* Length */
1575 * 'asn1_set_oid()' - Set an OID value.
1579 asn1_set_oid(unsigned char **buffer, /* IO - Pointer in buffer */
1580 const int *oid) /* I - OID value */
1582 **buffer = CUPS_ASN1_OID;
1585 asn1_set_length(buffer, asn1_size_oid(oid));
1589 asn1_set_packed(buffer, oid[0] * 40);
1593 asn1_set_packed(buffer, oid[0] * 40 + oid[1]);
1595 for (oid += 2; *oid >= 0; oid ++)
1596 asn1_set_packed(buffer, *oid);
1601 * 'asn1_set_packed()' - Set a packed integer value.
1605 asn1_set_packed(unsigned char **buffer, /* IO - Pointer in buffer */
1606 int integer) /* I - Integer value */
1608 if (integer > 0xfffffff)
1610 **buffer = ((integer >> 28) & 0x7f) | 0x80;
1614 if (integer > 0x1fffff)
1616 **buffer = ((integer >> 21) & 0x7f) | 0x80;
1620 if (integer > 0x3fff)
1622 **buffer = ((integer >> 14) & 0x7f) | 0x80;
1628 **buffer = ((integer >> 7) & 0x7f) | 0x80;
1632 **buffer = integer & 0x7f;
1638 * 'asn1_size_integer()' - Figure out the number of bytes needed for an
1642 static int /* O - Size in bytes */
1643 asn1_size_integer(int integer) /* I - Integer value */
1645 if (integer > 0x7fffff || integer < -0x800000)
1647 else if (integer > 0x7fff || integer < -0x8000)
1649 else if (integer > 0x7f || integer < -0x80)
1657 * 'asn1_size_length()' - Figure out the number of bytes needed for a
1661 static int /* O - Size in bytes */
1662 asn1_size_length(int length) /* I - Length value */
1666 else if (length > 0x7f)
1674 * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an
1678 static int /* O - Size in bytes */
1679 asn1_size_oid(const int *oid) /* I - OID value */
1681 int length; /* Length of value */
1685 return (asn1_size_packed(oid[0] * 40));
1687 for (length = asn1_size_packed(oid[0] * 40 + oid[1]), oid += 2;
1690 length += asn1_size_packed(*oid);
1697 * 'asn1_size_packed()' - Figure out the number of bytes needed for a
1698 * packed integer value.
1701 static int /* O - Size in bytes */
1702 asn1_size_packed(int integer) /* I - Integer value */
1704 if (integer > 0xfffffff)
1706 else if (integer > 0x1fffff)
1708 else if (integer > 0x3fff)
1710 else if (integer > 0x7f)
1718 * 'snmp_set_error()' - Set the localized error for a packet.
1722 snmp_set_error(cups_snmp_t *packet, /* I - Packet */
1723 const char *message) /* I - Error message */
1725 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1728 if (!cg->lang_default)
1729 cg->lang_default = cupsLangDefault();
1731 packet->error = _cupsLangString(cg->lang_default, message);
1736 * End of "$Id: snmp.c 9793 2011-05-20 03:49:49Z mike $".