4 * SNMP functions for CUPS.
6 * Copyright 2007-2013 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 * Include necessary headers.
22 #include "cups-private.h"
23 #include "snmp-private.h"
26 #endif /* HAVE_POLL */
33 static void asn1_debug(const char *prefix
, unsigned char *buffer
,
34 size_t len
, int indent
);
35 static int asn1_decode_snmp(unsigned char *buffer
, size_t len
,
37 static int asn1_encode_snmp(unsigned char *buffer
, size_t len
,
39 static int asn1_get_integer(unsigned char **buffer
,
40 unsigned char *bufend
,
42 static int asn1_get_oid(unsigned char **buffer
,
43 unsigned char *bufend
,
44 int length
, int *oid
, int oidsize
);
45 static int asn1_get_packed(unsigned char **buffer
,
46 unsigned char *bufend
);
47 static char *asn1_get_string(unsigned char **buffer
,
48 unsigned char *bufend
,
49 int length
, char *string
,
51 static unsigned asn1_get_length(unsigned char **buffer
,
52 unsigned char *bufend
);
53 static int asn1_get_type(unsigned char **buffer
,
54 unsigned char *bufend
);
55 static void asn1_set_integer(unsigned char **buffer
,
57 static void asn1_set_length(unsigned char **buffer
,
59 static void asn1_set_oid(unsigned char **buffer
,
61 static void asn1_set_packed(unsigned char **buffer
,
63 static int asn1_size_integer(int integer
);
64 static int asn1_size_length(int length
);
65 static int asn1_size_oid(const int *oid
);
66 static int asn1_size_packed(int integer
);
67 static void snmp_set_error(cups_snmp_t
*packet
,
72 * '_cupsSNMPClose()' - Close a SNMP socket.
76 _cupsSNMPClose(int fd
) /* I - SNMP socket file descriptor */
78 DEBUG_printf(("4_cupsSNMPClose(fd=%d)", fd
));
80 httpAddrClose(NULL
, fd
);
85 * '_cupsSNMPCopyOID()' - Copy an OID.
87 * The array pointed to by "src" is terminated by the value -1.
90 int * /* O - New OID */
91 _cupsSNMPCopyOID(int *dst
, /* I - Destination OID */
92 const int *src
, /* I - Source OID */
93 int dstsize
) /* I - Number of integers in dst */
95 int i
; /* Looping var */
98 DEBUG_printf(("4_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)", dst
, src
,
101 for (i
= 0, dstsize
--; src
[i
] >= 0 && i
< dstsize
; i
++)
111 * '_cupsSNMPDefaultCommunity()' - Get the default SNMP community name.
113 * The default community name is the first community name found in the
114 * snmp.conf file. If no community name is defined there, "public" is used.
117 const char * /* O - Default community name */
118 _cupsSNMPDefaultCommunity(void)
120 cups_file_t
*fp
; /* snmp.conf file */
121 char line
[1024], /* Line from file */
122 *value
; /* Value from file */
123 int linenum
; /* Line number in file */
124 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
127 DEBUG_puts("4_cupsSNMPDefaultCommunity()");
129 if (!cg
->snmp_community
[0])
131 strlcpy(cg
->snmp_community
, "public", sizeof(cg
->snmp_community
));
133 snprintf(line
, sizeof(line
), "%s/snmp.conf", cg
->cups_serverroot
);
134 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
137 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
138 if (!_cups_strcasecmp(line
, "Community") && value
)
140 strlcpy(cg
->snmp_community
, value
, sizeof(cg
->snmp_community
));
148 DEBUG_printf(("5_cupsSNMPDefaultCommunity: Returning \"%s\"",
149 cg
->snmp_community
));
151 return (cg
->snmp_community
);
156 * '_cupsSNMPIsOID()' - Test whether a SNMP response contains the specified OID.
158 * The array pointed to by "oid" is terminated by the value -1.
161 int /* O - 1 if equal, 0 if not equal */
162 _cupsSNMPIsOID(cups_snmp_t
*packet
, /* I - Response packet */
163 const int *oid
) /* I - OID */
165 int i
; /* Looping var */
169 * Range check input...
172 DEBUG_printf(("4_cupsSNMPIsOID(packet=%p, oid=%p)", packet
, oid
));
176 DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
186 i
< CUPS_SNMP_MAX_OID
&& oid
[i
] >= 0 && packet
->object_name
[i
] >= 0;
188 if (oid
[i
] != packet
->object_name
[i
])
190 DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
195 DEBUG_printf(("5_cupsSNMPIsOID: Returning %d",
196 i
< CUPS_SNMP_MAX_OID
&& oid
[i
] == packet
->object_name
[i
]));
198 return (i
< CUPS_SNMP_MAX_OID
&& oid
[i
] == packet
->object_name
[i
]);
203 * '_cupsSNMPIsOIDPrefixed()' - Test whether a SNMP response uses the specified
206 * The array pointed to by "prefix" is terminated by the value -1.
209 int /* O - 1 if prefixed, 0 if not prefixed */
210 _cupsSNMPIsOIDPrefixed(
211 cups_snmp_t
*packet
, /* I - Response packet */
212 const int *prefix
) /* I - OID prefix */
214 int i
; /* Looping var */
218 * Range check input...
221 DEBUG_printf(("4_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)", packet
,
224 if (!packet
|| !prefix
)
226 DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
236 i
< CUPS_SNMP_MAX_OID
&& prefix
[i
] >= 0 && packet
->object_name
[i
] >= 0;
238 if (prefix
[i
] != packet
->object_name
[i
])
240 DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
245 DEBUG_printf(("5_cupsSNMPIsOIDPrefixed: Returning %d",
246 i
< CUPS_SNMP_MAX_OID
));
248 return (i
< CUPS_SNMP_MAX_OID
);
253 * '_cupsSNMPOIDToString()' - Convert an OID to a string.
257 char * /* O - New string or @code NULL@ on error */
258 _cupsSNMPOIDToString(const int *src
, /* I - OID */
259 char *dst
, /* I - String buffer */
260 size_t dstsize
) /* I - Size of string buffer */
262 char *dstptr
, /* Pointer into string buffer */
263 *dstend
; /* End of string buffer */
266 DEBUG_printf(("4_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT
")",
267 src
, dst
, CUPS_LLCAST dstsize
));
270 * Range check input...
273 if (!src
|| !dst
|| dstsize
< 4)
277 * Loop through the OID array and build a string...
280 for (dstptr
= dst
, dstend
= dstptr
+ dstsize
- 1;
281 *src
>= 0 && dstptr
< dstend
;
282 src
++, dstptr
+= strlen(dstptr
))
283 snprintf(dstptr
, dstend
- dstptr
+ 1, ".%d", *src
);
293 * '_cupsSNMPOpen()' - Open a SNMP socket.
296 int /* O - SNMP socket file descriptor */
297 _cupsSNMPOpen(int family
) /* I - Address family - @code AF_INET@ or @code AF_INET6@ */
299 int fd
; /* SNMP socket file descriptor */
300 int val
; /* Socket option value */
304 * Create the SNMP socket...
307 DEBUG_printf(("4_cupsSNMPOpen(family=%d)", family
));
309 if ((fd
= socket(family
, SOCK_DGRAM
, 0)) < 0)
311 DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno
)));
317 * Set the "broadcast" flag...
322 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, CUPS_SOCAST
&val
, sizeof(val
)))
324 DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno
)));
331 DEBUG_printf(("5_cupsSNMPOpen: Returning %d", fd
));
338 * '_cupsSNMPRead()' - Read and parse a SNMP response.
340 * If "timeout" is negative, @code _cupsSNMPRead@ will wait for a response
344 cups_snmp_t
* /* O - SNMP packet or @code NULL@ if none */
345 _cupsSNMPRead(int fd
, /* I - SNMP socket file descriptor */
346 cups_snmp_t
*packet
, /* I - SNMP packet buffer */
347 double timeout
) /* I - Timeout in seconds */
349 unsigned char buffer
[CUPS_SNMP_MAX_PACKET
];
351 int bytes
; /* Number of bytes received */
352 socklen_t addrlen
; /* Source address length */
353 http_addr_t address
; /* Source address */
357 * Range check input...
360 DEBUG_printf(("4_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)", fd
, packet
,
363 if (fd
< 0 || !packet
)
365 DEBUG_puts("5_cupsSNMPRead: Returning NULL");
371 * Optionally wait for a response...
376 int ready
; /* Data ready on socket? */
378 struct pollfd pfd
; /* Polled file descriptor */
383 while ((ready
= poll(&pfd
, 1, (int)(timeout
* 1000.0))) < 0 &&
384 (errno
== EINTR
|| errno
== EAGAIN
));
387 fd_set input_set
; /* select() input set */
388 struct timeval stimeout
; /* select() timeout */
393 FD_SET(fd
, &input_set
);
395 stimeout
.tv_sec
= (int)timeout
;
396 stimeout
.tv_usec
= (int)((timeout
- stimeout
.tv_sec
) * 1000000);
398 ready
= select(fd
+ 1, &input_set
, NULL
, NULL
, &stimeout
);
401 while (ready
< 0 && WSAGetLastError() == WSAEINTR
);
403 while (ready
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
405 #endif /* HAVE_POLL */
408 * If we don't have any data ready, return right away...
413 DEBUG_puts("5_cupsSNMPRead: Returning NULL (timeout)");
420 * Read the response data...
423 addrlen
= sizeof(address
);
425 if ((bytes
= recvfrom(fd
, buffer
, sizeof(buffer
), 0, (void *)&address
,
428 DEBUG_printf(("5_cupsSNMPRead: Returning NULL (%s)", strerror(errno
)));
434 * Look for the response status code in the SNMP message header...
437 asn1_debug("DEBUG: IN ", buffer
, bytes
, 0);
439 asn1_decode_snmp(buffer
, bytes
, packet
);
441 memcpy(&(packet
->address
), &address
, sizeof(packet
->address
));
444 * Return decoded data packet...
447 DEBUG_puts("5_cupsSNMPRead: Returning packet");
454 * '_cupsSNMPSetDebug()' - Enable/disable debug logging to stderr.
458 _cupsSNMPSetDebug(int level
) /* I - 1 to enable debug output, 0 otherwise */
460 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
463 DEBUG_printf(("4_cupsSNMPSetDebug(level=%d)", level
));
465 cg
->snmp_debug
= level
;
470 * '_cupsSNMPStringToOID()' - Convert a numeric OID string to an OID array.
472 * This function converts a string of the form ".N.N.N.N.N" to the
473 * corresponding OID array terminated by -1.
475 * @code NULL@ is returned if the array is not large enough or the string is
476 * not a valid OID number.
479 int * /* O - Pointer to OID array or @code NULL@ on error */
480 _cupsSNMPStringToOID(const char *src
, /* I - OID string */
481 int *dst
, /* I - OID array */
482 int dstsize
)/* I - Number of integers in OID array */
484 int *dstptr
, /* Pointer into OID array */
485 *dstend
; /* End of OID array */
488 DEBUG_printf(("4_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)",
492 * Range check input...
495 if (!src
|| !dst
|| dstsize
< 2)
499 * Skip leading "."...
506 * Loop to the end of the string...
509 for (dstend
= dst
+ dstsize
- 1, dstptr
= dst
, *dstptr
= 0;
510 *src
&& dstptr
< dstend
;
518 else if (isdigit(*src
& 255))
519 *dstptr
= *dstptr
* 10 + *src
- '0';
528 * Terminate the end of the OID array and return...
538 * '_cupsSNMPWalk()' - Enumerate a group of OIDs.
540 * This function queries all of the OIDs with the specified OID prefix,
541 * calling the "cb" function for every response that is received.
543 * The array pointed to by "prefix" is terminated by the value -1.
545 * If "timeout" is negative, @code _cupsSNMPWalk@ will wait for a response
549 int /* O - Number of OIDs found or -1 on error */
550 _cupsSNMPWalk(int fd
, /* I - SNMP socket */
551 http_addr_t
*address
, /* I - Address to query */
552 int version
, /* I - SNMP version */
553 const char *community
,/* I - Community name */
554 const int *prefix
, /* I - OID prefix */
555 double timeout
, /* I - Timeout for each response in seconds */
556 cups_snmp_cb_t cb
, /* I - Function to call for each response */
557 void *data
) /* I - User data pointer that is passed to the callback function */
559 int count
= 0; /* Number of OIDs found */
560 int request_id
= 0; /* Current request ID */
561 cups_snmp_t packet
; /* Current response packet */
562 int lastoid
[CUPS_SNMP_MAX_OID
];
563 /* Last OID we got */
567 * Range check input...
570 DEBUG_printf(("4_cupsSNMPWalk(fd=%d, address=%p, version=%d, "
571 "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)",
572 fd
, address
, version
, community
, prefix
, timeout
, cb
, data
));
574 if (fd
< 0 || !address
|| version
!= CUPS_SNMP_VERSION_1
|| !community
||
577 DEBUG_puts("5_cupsSNMPWalk: Returning -1");
583 * Copy the OID prefix and then loop until we have no more OIDs...
586 _cupsSNMPCopyOID(packet
.object_name
, prefix
, CUPS_SNMP_MAX_OID
);
593 if (!_cupsSNMPWrite(fd
, address
, version
, community
,
594 CUPS_ASN1_GET_NEXT_REQUEST
, request_id
,
597 DEBUG_puts("5_cupsSNMPWalk: Returning -1");
602 if (!_cupsSNMPRead(fd
, &packet
, timeout
))
604 DEBUG_puts("5_cupsSNMPWalk: Returning -1");
609 if (!_cupsSNMPIsOIDPrefixed(&packet
, prefix
) ||
610 _cupsSNMPIsOID(&packet
, lastoid
))
612 DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count
));
617 if (packet
.error
|| packet
.error_status
)
619 DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count
> 0 ? count
: -1));
621 return (count
> 0 ? count
: -1);
624 _cupsSNMPCopyOID(lastoid
, packet
.object_name
, CUPS_SNMP_MAX_OID
);
628 (*cb
)(&packet
, data
);
634 * '_cupsSNMPWrite()' - Send an SNMP query packet.
636 * The array pointed to by "oid" is terminated by the value -1.
639 int /* O - 1 on success, 0 on error */
641 int fd
, /* I - SNMP socket */
642 http_addr_t
*address
, /* I - Address to send to */
643 int version
, /* I - SNMP version */
644 const char *community
, /* I - Community name */
645 cups_asn1_t request_type
, /* I - Request type */
646 const unsigned request_id
, /* I - Request ID */
647 const int *oid
) /* I - OID */
649 int i
; /* Looping var */
650 cups_snmp_t packet
; /* SNMP message packet */
651 unsigned char buffer
[CUPS_SNMP_MAX_PACKET
];
652 /* SNMP message buffer */
653 int bytes
; /* Size of message */
654 http_addr_t temp
; /* Copy of address */
658 * Range check input...
661 DEBUG_printf(("4_cupsSNMPWrite(fd=%d, address=%p, version=%d, "
662 "community=\"%s\", request_type=%d, request_id=%u, oid=%p)",
663 fd
, address
, version
, community
, request_type
, request_id
, oid
));
665 if (fd
< 0 || !address
|| version
!= CUPS_SNMP_VERSION_1
|| !community
||
666 (request_type
!= CUPS_ASN1_GET_REQUEST
&&
667 request_type
!= CUPS_ASN1_GET_NEXT_REQUEST
) || request_id
< 1 || !oid
)
669 DEBUG_puts("5_cupsSNMPWrite: Returning 0 (bad arguments)");
675 * Create the SNMP message...
678 memset(&packet
, 0, sizeof(packet
));
680 packet
.version
= version
;
681 packet
.request_type
= request_type
;
682 packet
.request_id
= request_id
;
683 packet
.object_type
= CUPS_ASN1_NULL_VALUE
;
685 strlcpy(packet
.community
, community
, sizeof(packet
.community
));
687 for (i
= 0; oid
[i
] >= 0 && i
< (CUPS_SNMP_MAX_OID
- 1); i
++)
688 packet
.object_name
[i
] = oid
[i
];
689 packet
.object_name
[i
] = -1;
693 DEBUG_puts("5_cupsSNMPWrite: Returning 0 (OID too big)");
699 bytes
= asn1_encode_snmp(buffer
, sizeof(buffer
), &packet
);
703 DEBUG_puts("5_cupsSNMPWrite: Returning 0 (request too big)");
709 asn1_debug("DEBUG: OUT ", buffer
, bytes
, 0);
712 * Send the message...
717 _httpAddrSetPort(&temp
, CUPS_SNMP_PORT
);
719 return (sendto(fd
, buffer
, bytes
, 0, (void *)&temp
,
720 httpAddrLength(&temp
)) == bytes
);
725 * 'asn1_debug()' - Decode an ASN1-encoded message.
729 asn1_debug(const char *prefix
, /* I - Prefix string */
730 unsigned char *buffer
, /* I - Buffer */
731 size_t len
, /* I - Length of buffer */
732 int indent
) /* I - Indentation */
734 int i
; /* Looping var */
735 unsigned char *bufend
; /* End of buffer */
736 int integer
; /* Number value */
737 int oid
[CUPS_SNMP_MAX_OID
]; /* OID value */
738 char string
[CUPS_SNMP_MAX_STRING
];
740 unsigned char value_type
; /* Type of value */
741 int value_length
; /* Length of value */
742 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
745 if (cg
->snmp_debug
<= 0)
748 if (cg
->snmp_debug
> 1 && indent
== 0)
751 * Do a hex dump of the packet...
756 fprintf(stderr
, "%sHex Dump (%d bytes):\n", prefix
, (int)len
);
758 for (i
= 0; i
< (int)len
; i
+= 16)
760 fprintf(stderr
, "%s%04x:", prefix
, i
);
762 for (j
= 0; j
< 16 && (i
+ j
) < (int)len
; j
++)
765 fprintf(stderr
, " %02x", buffer
[i
+ j
]);
767 fprintf(stderr
, " %02x", buffer
[i
+ j
]);
782 for (j
= 0; j
< 16 && (i
+ j
) < (int)len
; j
++)
783 if (buffer
[i
+ j
] < ' ' || buffer
[i
+ j
] >= 0x7f)
786 putc(buffer
[i
+ j
], stderr
);
793 fprintf(stderr
, "%sMessage:\n", prefix
);
795 bufend
= buffer
+ len
;
797 while (buffer
< bufend
)
803 value_type
= asn1_get_type(&buffer
, bufend
);
804 value_length
= asn1_get_length(&buffer
, bufend
);
808 case CUPS_ASN1_BOOLEAN
:
809 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
811 fprintf(stderr
, "%s%*sBOOLEAN %d bytes %d\n", prefix
, indent
, "",
812 value_length
, integer
);
815 case CUPS_ASN1_INTEGER
:
816 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
818 fprintf(stderr
, "%s%*sINTEGER %d bytes %d\n", prefix
, indent
, "",
819 value_length
, integer
);
822 case CUPS_ASN1_COUNTER
:
823 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
825 fprintf(stderr
, "%s%*sCOUNTER %d bytes %u\n", prefix
, indent
, "",
826 value_length
, (unsigned)integer
);
829 case CUPS_ASN1_GAUGE
:
830 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
832 fprintf(stderr
, "%s%*sGAUGE %d bytes %u\n", prefix
, indent
, "",
833 value_length
, (unsigned)integer
);
836 case CUPS_ASN1_TIMETICKS
:
837 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
839 fprintf(stderr
, "%s%*sTIMETICKS %d bytes %u\n", prefix
, indent
, "",
840 value_length
, (unsigned)integer
);
843 case CUPS_ASN1_OCTET_STRING
:
844 fprintf(stderr
, "%s%*sOCTET STRING %d bytes \"%s\"\n", prefix
,
845 indent
, "", value_length
,
846 asn1_get_string(&buffer
, bufend
, value_length
, string
,
850 case CUPS_ASN1_HEX_STRING
:
851 asn1_get_string(&buffer
, bufend
, value_length
, string
,
853 fprintf(stderr
, "%s%*sHex-STRING %d bytes", prefix
,
854 indent
, "", value_length
);
855 for (i
= 0; i
< value_length
; i
++)
856 fprintf(stderr
, " %02X", string
[i
] & 255);
860 case CUPS_ASN1_NULL_VALUE
:
861 fprintf(stderr
, "%s%*sNULL VALUE %d bytes\n", prefix
, indent
, "",
864 buffer
+= value_length
;
868 integer
= asn1_get_oid(&buffer
, bufend
, value_length
, oid
,
871 fprintf(stderr
, "%s%*sOID %d bytes ", prefix
, indent
, "",
873 for (i
= 0; i
< integer
; i
++)
874 fprintf(stderr
, ".%d", oid
[i
]);
878 case CUPS_ASN1_SEQUENCE
:
879 fprintf(stderr
, "%s%*sSEQUENCE %d bytes\n", prefix
, indent
, "",
881 asn1_debug(prefix
, buffer
, value_length
, indent
+ 4);
883 buffer
+= value_length
;
886 case CUPS_ASN1_GET_NEXT_REQUEST
:
887 fprintf(stderr
, "%s%*sGet-Next-Request-PDU %d bytes\n", prefix
,
888 indent
, "", value_length
);
889 asn1_debug(prefix
, buffer
, value_length
, indent
+ 4);
891 buffer
+= value_length
;
894 case CUPS_ASN1_GET_REQUEST
:
895 fprintf(stderr
, "%s%*sGet-Request-PDU %d bytes\n", prefix
, indent
, "",
897 asn1_debug(prefix
, buffer
, value_length
, indent
+ 4);
899 buffer
+= value_length
;
902 case CUPS_ASN1_GET_RESPONSE
:
903 fprintf(stderr
, "%s%*sGet-Response-PDU %d bytes\n", prefix
, indent
,
905 asn1_debug(prefix
, buffer
, value_length
, indent
+ 4);
907 buffer
+= value_length
;
911 fprintf(stderr
, "%s%*sUNKNOWN(%x) %d bytes\n", prefix
, indent
, "",
912 value_type
, value_length
);
914 buffer
+= value_length
;
922 * 'asn1_decode_snmp()' - Decode a SNMP packet.
925 static int /* O - 0 on success, -1 on error */
926 asn1_decode_snmp(unsigned char *buffer
, /* I - Buffer */
927 size_t len
, /* I - Size of buffer */
928 cups_snmp_t
*packet
) /* I - SNMP packet */
930 unsigned char *bufptr
, /* Pointer into the data */
931 *bufend
; /* End of data */
932 int length
; /* Length of value */
936 * Initialize the decoding...
939 memset(packet
, 0, sizeof(cups_snmp_t
));
940 packet
->object_name
[0] = -1;
943 bufend
= buffer
+ len
;
945 if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_SEQUENCE
)
946 snmp_set_error(packet
, _("Packet does not start with SEQUENCE"));
947 else if (asn1_get_length(&bufptr
, bufend
) == 0)
948 snmp_set_error(packet
, _("SEQUENCE uses indefinite length"));
949 else if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_INTEGER
)
950 snmp_set_error(packet
, _("No version number"));
951 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
952 snmp_set_error(packet
, _("Version uses indefinite length"));
953 else if ((packet
->version
= asn1_get_integer(&bufptr
, bufend
, length
))
954 != CUPS_SNMP_VERSION_1
)
955 snmp_set_error(packet
, _("Bad SNMP version number"));
956 else if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_OCTET_STRING
)
957 snmp_set_error(packet
, _("No community name"));
958 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
959 snmp_set_error(packet
, _("Community name uses indefinite length"));
962 asn1_get_string(&bufptr
, bufend
, length
, packet
->community
,
963 sizeof(packet
->community
));
965 if ((packet
->request_type
= asn1_get_type(&bufptr
, bufend
))
966 != CUPS_ASN1_GET_RESPONSE
)
967 snmp_set_error(packet
, _("Packet does not contain a Get-Response-PDU"));
968 else if (asn1_get_length(&bufptr
, bufend
) == 0)
969 snmp_set_error(packet
, _("Get-Response-PDU uses indefinite length"));
970 else if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_INTEGER
)
971 snmp_set_error(packet
, _("No request-id"));
972 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
973 snmp_set_error(packet
, _("request-id uses indefinite length"));
976 packet
->request_id
= asn1_get_integer(&bufptr
, bufend
, length
);
978 if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_INTEGER
)
979 snmp_set_error(packet
, _("No error-status"));
980 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
981 snmp_set_error(packet
, _("error-status uses indefinite length"));
984 packet
->error_status
= asn1_get_integer(&bufptr
, bufend
, length
);
986 if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_INTEGER
)
987 snmp_set_error(packet
, _("No error-index"));
988 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
989 snmp_set_error(packet
, _("error-index uses indefinite length"));
992 packet
->error_index
= asn1_get_integer(&bufptr
, bufend
, length
);
994 if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_SEQUENCE
)
995 snmp_set_error(packet
, _("No variable-bindings SEQUENCE"));
996 else if (asn1_get_length(&bufptr
, bufend
) == 0)
997 snmp_set_error(packet
,
998 _("variable-bindings uses indefinite length"));
999 else if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_SEQUENCE
)
1000 snmp_set_error(packet
, _("No VarBind SEQUENCE"));
1001 else if (asn1_get_length(&bufptr
, bufend
) == 0)
1002 snmp_set_error(packet
, _("VarBind uses indefinite length"));
1003 else if (asn1_get_type(&bufptr
, bufend
) != CUPS_ASN1_OID
)
1004 snmp_set_error(packet
, _("No name OID"));
1005 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
1006 snmp_set_error(packet
, _("Name OID uses indefinite length"));
1009 asn1_get_oid(&bufptr
, bufend
, length
, packet
->object_name
,
1012 packet
->object_type
= asn1_get_type(&bufptr
, bufend
);
1014 if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0 &&
1015 packet
->object_type
!= CUPS_ASN1_NULL_VALUE
&&
1016 packet
->object_type
!= CUPS_ASN1_OCTET_STRING
)
1017 snmp_set_error(packet
, _("Value uses indefinite length"));
1020 switch (packet
->object_type
)
1022 case CUPS_ASN1_BOOLEAN
:
1023 packet
->object_value
.boolean
=
1024 asn1_get_integer(&bufptr
, bufend
, length
);
1027 case CUPS_ASN1_INTEGER
:
1028 packet
->object_value
.integer
=
1029 asn1_get_integer(&bufptr
, bufend
, length
);
1032 case CUPS_ASN1_NULL_VALUE
:
1035 case CUPS_ASN1_OCTET_STRING
:
1036 case CUPS_ASN1_BIT_STRING
:
1037 case CUPS_ASN1_HEX_STRING
:
1038 packet
->object_value
.string
.num_bytes
= length
;
1039 asn1_get_string(&bufptr
, bufend
, length
,
1040 (char *)packet
->object_value
.string
.bytes
,
1041 sizeof(packet
->object_value
.string
.bytes
));
1044 case CUPS_ASN1_OID
:
1045 asn1_get_oid(&bufptr
, bufend
, length
,
1046 packet
->object_value
.oid
, CUPS_SNMP_MAX_OID
);
1049 case CUPS_ASN1_COUNTER
:
1050 packet
->object_value
.counter
=
1051 asn1_get_integer(&bufptr
, bufend
, length
);
1054 case CUPS_ASN1_GAUGE
:
1055 packet
->object_value
.gauge
=
1056 asn1_get_integer(&bufptr
, bufend
, length
);
1059 case CUPS_ASN1_TIMETICKS
:
1060 packet
->object_value
.timeticks
=
1061 asn1_get_integer(&bufptr
, bufend
, length
);
1065 snmp_set_error(packet
, _("Unsupported value type"));
1075 return (packet
->error
? -1 : 0);
1080 * 'asn1_encode_snmp()' - Encode a SNMP packet.
1083 static int /* O - Length on success, -1 on error */
1084 asn1_encode_snmp(unsigned char *buffer
, /* I - Buffer */
1085 size_t bufsize
, /* I - Size of buffer */
1086 cups_snmp_t
*packet
) /* I - SNMP packet */
1088 unsigned char *bufptr
; /* Pointer into buffer */
1089 int total
, /* Total length */
1090 msglen
, /* Length of entire message */
1091 commlen
, /* Length of community string */
1092 reqlen
, /* Length of request */
1093 listlen
, /* Length of variable list */
1094 varlen
, /* Length of variable */
1095 namelen
, /* Length of object name OID */
1096 valuelen
; /* Length of object value */
1100 * Get the lengths of the community string, OID, and message...
1104 namelen
= asn1_size_oid(packet
->object_name
);
1106 switch (packet
->object_type
)
1108 case CUPS_ASN1_NULL_VALUE
:
1112 case CUPS_ASN1_BOOLEAN
:
1113 valuelen
= asn1_size_integer(packet
->object_value
.boolean
);
1116 case CUPS_ASN1_INTEGER
:
1117 valuelen
= asn1_size_integer(packet
->object_value
.integer
);
1120 case CUPS_ASN1_OCTET_STRING
:
1121 valuelen
= packet
->object_value
.string
.num_bytes
;
1124 case CUPS_ASN1_OID
:
1125 valuelen
= asn1_size_oid(packet
->object_value
.oid
);
1129 packet
->error
= "Unknown object type";
1133 varlen
= 1 + asn1_size_length(namelen
) + namelen
+
1134 1 + asn1_size_length(valuelen
) + valuelen
;
1135 listlen
= 1 + asn1_size_length(varlen
) + varlen
;
1136 reqlen
= 2 + asn1_size_integer(packet
->request_id
) +
1137 2 + asn1_size_integer(packet
->error_status
) +
1138 2 + asn1_size_integer(packet
->error_index
) +
1139 1 + asn1_size_length(listlen
) + listlen
;
1140 commlen
= strlen(packet
->community
);
1141 msglen
= 2 + asn1_size_integer(packet
->version
) +
1142 1 + asn1_size_length(commlen
) + commlen
+
1143 1 + asn1_size_length(reqlen
) + reqlen
;
1144 total
= 1 + asn1_size_length(msglen
) + msglen
;
1146 if (total
> (int)bufsize
)
1148 packet
->error
= "Message too large for buffer";
1153 * Then format the message...
1158 *bufptr
++ = CUPS_ASN1_SEQUENCE
; /* SNMPv1 message header */
1159 asn1_set_length(&bufptr
, msglen
);
1161 asn1_set_integer(&bufptr
, packet
->version
);
1164 *bufptr
++ = CUPS_ASN1_OCTET_STRING
; /* community */
1165 asn1_set_length(&bufptr
, commlen
);
1166 memcpy(bufptr
, packet
->community
, commlen
);
1169 *bufptr
++ = packet
->request_type
; /* Get-Request-PDU/Get-Next-Request-PDU */
1170 asn1_set_length(&bufptr
, reqlen
);
1172 asn1_set_integer(&bufptr
, packet
->request_id
);
1174 asn1_set_integer(&bufptr
, packet
->error_status
);
1176 asn1_set_integer(&bufptr
, packet
->error_index
);
1178 *bufptr
++ = CUPS_ASN1_SEQUENCE
; /* variable-bindings */
1179 asn1_set_length(&bufptr
, listlen
);
1181 *bufptr
++ = CUPS_ASN1_SEQUENCE
; /* variable */
1182 asn1_set_length(&bufptr
, varlen
);
1184 asn1_set_oid(&bufptr
, packet
->object_name
);
1187 switch (packet
->object_type
)
1189 case CUPS_ASN1_NULL_VALUE
:
1190 *bufptr
++ = CUPS_ASN1_NULL_VALUE
;
1192 *bufptr
++ = 0; /* Length */
1195 case CUPS_ASN1_BOOLEAN
:
1196 asn1_set_integer(&bufptr
, packet
->object_value
.boolean
);
1199 case CUPS_ASN1_INTEGER
:
1200 asn1_set_integer(&bufptr
, packet
->object_value
.integer
);
1203 case CUPS_ASN1_OCTET_STRING
:
1204 *bufptr
++ = CUPS_ASN1_OCTET_STRING
;
1205 asn1_set_length(&bufptr
, valuelen
);
1206 memcpy(bufptr
, packet
->object_value
.string
.bytes
, valuelen
);
1210 case CUPS_ASN1_OID
:
1211 asn1_set_oid(&bufptr
, packet
->object_value
.oid
);
1218 return (bufptr
- buffer
);
1223 * 'asn1_get_integer()' - Get an integer value.
1226 static int /* O - Integer value */
1228 unsigned char **buffer
, /* IO - Pointer in buffer */
1229 unsigned char *bufend
, /* I - End of buffer */
1230 int length
) /* I - Length of value */
1232 int value
; /* Integer value */
1235 if (length
> sizeof(int))
1237 (*buffer
) += length
;
1241 for (value
= (**buffer
& 0x80) ? -1 : 0;
1242 length
> 0 && *buffer
< bufend
;
1243 length
--, (*buffer
) ++)
1244 value
= (value
<< 8) | **buffer
;
1251 * 'asn1_get_length()' - Get a value length.
1254 static unsigned /* O - Length */
1255 asn1_get_length(unsigned char **buffer
, /* IO - Pointer in buffer */
1256 unsigned char *bufend
) /* I - End of buffer */
1258 unsigned length
; /* Length */
1266 int count
; /* Number of bytes for length */
1269 if ((count
= length
& 127) > sizeof(unsigned))
1276 count
> 0 && *buffer
< bufend
;
1277 count
--, (*buffer
) ++)
1278 length
= (length
<< 8) | **buffer
;
1286 * 'asn1_get_oid()' - Get an OID value.
1289 static int /* O - Number of OIDs */
1291 unsigned char **buffer
, /* IO - Pointer in buffer */
1292 unsigned char *bufend
, /* I - End of buffer */
1293 int length
, /* I - Length of value */
1294 int *oid
, /* I - OID buffer */
1295 int oidsize
) /* I - Size of OID buffer */
1297 unsigned char *valend
; /* End of value */
1298 int *oidptr
, /* Current OID */
1299 *oidend
; /* End of OID buffer */
1300 int number
; /* OID number */
1303 valend
= *buffer
+ length
;
1305 oidend
= oid
+ oidsize
- 1;
1307 if (valend
> bufend
)
1310 number
= asn1_get_packed(buffer
, bufend
);
1314 *oidptr
++ = number
/ 40;
1315 number
= number
% 40;
1325 while (*buffer
< valend
)
1327 number
= asn1_get_packed(buffer
, bufend
);
1329 if (oidptr
< oidend
)
1335 return (oidptr
- oid
);
1340 * 'asn1_get_packed()' - Get a packed integer value.
1343 static int /* O - Value */
1345 unsigned char **buffer
, /* IO - Pointer in buffer */
1346 unsigned char *bufend
) /* I - End of buffer */
1348 int value
; /* Value */
1353 while ((**buffer
& 128) && *buffer
< bufend
)
1355 value
= (value
<< 7) | (**buffer
& 127);
1359 if (*buffer
< bufend
)
1361 value
= (value
<< 7) | **buffer
;
1370 * 'asn1_get_string()' - Get a string value.
1373 static char * /* O - String */
1375 unsigned char **buffer
, /* IO - Pointer in buffer */
1376 unsigned char *bufend
, /* I - End of buffer */
1377 int length
, /* I - Value length */
1378 char *string
, /* I - String buffer */
1379 int strsize
) /* I - String buffer size */
1381 if (length
> (bufend
- *buffer
))
1382 length
= bufend
- *buffer
;
1387 * Disallow negative lengths!
1392 else if (length
< strsize
)
1395 * String is smaller than the buffer...
1399 memcpy(string
, *buffer
, length
);
1401 string
[length
] = '\0';
1406 * String is larger than the buffer...
1409 memcpy(string
, *buffer
, strsize
- 1);
1410 string
[strsize
- 1] = '\0';
1414 (*buffer
) += length
;
1416 return (length
< 0 ? NULL
: string
);
1421 * 'asn1_get_type()' - Get a value type.
1424 static int /* O - Type */
1425 asn1_get_type(unsigned char **buffer
, /* IO - Pointer in buffer */
1426 unsigned char *bufend
) /* I - End of buffer */
1428 int type
; /* Type */
1434 if ((type
& 31) == 31)
1435 type
= asn1_get_packed(buffer
, bufend
);
1442 * 'asn1_set_integer()' - Set an integer value.
1446 asn1_set_integer(unsigned char **buffer
,/* IO - Pointer in buffer */
1447 int integer
) /* I - Integer value */
1449 **buffer
= CUPS_ASN1_INTEGER
;
1452 if (integer
> 0x7fffff || integer
< -0x800000)
1456 **buffer
= integer
>> 24;
1458 **buffer
= integer
>> 16;
1460 **buffer
= integer
>> 8;
1465 else if (integer
> 0x7fff || integer
< -0x8000)
1469 **buffer
= integer
>> 16;
1471 **buffer
= integer
>> 8;
1476 else if (integer
> 0x7f || integer
< -0x80)
1480 **buffer
= integer
>> 8;
1496 * 'asn1_set_length()' - Set a value length.
1500 asn1_set_length(unsigned char **buffer
, /* IO - Pointer in buffer */
1501 unsigned length
) /* I - Length value */
1505 **buffer
= 0x82; /* 2-byte length */
1507 **buffer
= length
>> 8;
1512 else if (length
> 127)
1514 **buffer
= 0x81; /* 1-byte length */
1521 **buffer
= length
; /* Length */
1528 * 'asn1_set_oid()' - Set an OID value.
1532 asn1_set_oid(unsigned char **buffer
, /* IO - Pointer in buffer */
1533 const int *oid
) /* I - OID value */
1535 **buffer
= CUPS_ASN1_OID
;
1538 asn1_set_length(buffer
, asn1_size_oid(oid
));
1542 asn1_set_packed(buffer
, oid
[0] * 40);
1546 asn1_set_packed(buffer
, oid
[0] * 40 + oid
[1]);
1548 for (oid
+= 2; *oid
>= 0; oid
++)
1549 asn1_set_packed(buffer
, *oid
);
1554 * 'asn1_set_packed()' - Set a packed integer value.
1558 asn1_set_packed(unsigned char **buffer
, /* IO - Pointer in buffer */
1559 int integer
) /* I - Integer value */
1561 if (integer
> 0xfffffff)
1563 **buffer
= ((integer
>> 28) & 0x7f) | 0x80;
1567 if (integer
> 0x1fffff)
1569 **buffer
= ((integer
>> 21) & 0x7f) | 0x80;
1573 if (integer
> 0x3fff)
1575 **buffer
= ((integer
>> 14) & 0x7f) | 0x80;
1581 **buffer
= ((integer
>> 7) & 0x7f) | 0x80;
1585 **buffer
= integer
& 0x7f;
1591 * 'asn1_size_integer()' - Figure out the number of bytes needed for an
1595 static int /* O - Size in bytes */
1596 asn1_size_integer(int integer
) /* I - Integer value */
1598 if (integer
> 0x7fffff || integer
< -0x800000)
1600 else if (integer
> 0x7fff || integer
< -0x8000)
1602 else if (integer
> 0x7f || integer
< -0x80)
1610 * 'asn1_size_length()' - Figure out the number of bytes needed for a
1614 static int /* O - Size in bytes */
1615 asn1_size_length(int length
) /* I - Length value */
1619 else if (length
> 0x7f)
1627 * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an
1631 static int /* O - Size in bytes */
1632 asn1_size_oid(const int *oid
) /* I - OID value */
1634 int length
; /* Length of value */
1638 return (asn1_size_packed(oid
[0] * 40));
1640 for (length
= asn1_size_packed(oid
[0] * 40 + oid
[1]), oid
+= 2;
1643 length
+= asn1_size_packed(*oid
);
1650 * 'asn1_size_packed()' - Figure out the number of bytes needed for a
1651 * packed integer value.
1654 static int /* O - Size in bytes */
1655 asn1_size_packed(int integer
) /* I - Integer value */
1657 if (integer
> 0xfffffff)
1659 else if (integer
> 0x1fffff)
1661 else if (integer
> 0x3fff)
1663 else if (integer
> 0x7f)
1671 * 'snmp_set_error()' - Set the localized error for a packet.
1675 snmp_set_error(cups_snmp_t
*packet
, /* I - Packet */
1676 const char *message
) /* I - Error message */
1678 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1681 if (!cg
->lang_default
)
1682 cg
->lang_default
= cupsLangDefault();
1684 packet
->error
= _cupsLangString(cg
->lang_default
, message
);