ASN1(3)
πΈ NAME πΈ
okay so bestie, this is ASN1(3) from the Library Functions Manual β the whole ASN.1 library for SNMP situation. The cast list is long, and I'm not skipping a single one, periodt:
asn_get_headerasn_put_headerasn_put_temp_headerasn_commit_headerasn_get_integer_rawasn_get_integerasn_put_integerasn_get_octetstring_rawasn_get_octetstringasn_put_octetstringasn_get_null_rawasn_get_nullasn_put_nullasn_put_exceptionasn_get_objid_rawasn_get_objidasn_put_objidasn_get_sequenceasn_get_ipaddress_rawasn_get_ipaddressasn_put_ipaddressasn_get_uint32_rawasn_put_uint32asn_get_counter64_rawasn_put_counter64asn_get_timeticksasn_put_timeticksasn_skipasn_slice_oidasn_append_oidasn_compare_oidasn_is_suboidasn_oid2str_rasn_oid2str
and the whole vibe is: ASN.1 library for SNMP. π
π LIBRARY π
It's the Begemot SNMP library (libbsnmp, -lbsnmp). That's the shelf it lives on, bestie. π«Ά
β¨ SYNOPSIS β¨
okay so first you invite the header file in, like:
#include <bsnmp/asn1.h>
then the prototypes are right here, copied exactly, no edits, no cap:
enum asn_err
asn_get_header(struct asn_buf *buf, u_char *type, asn_len_t *lenp);
enum asn_err
asn_put_header(struct asn_buf *buf, u_char type, asn_len_t len);
enum asn_err
asn_put_temp_header(struct asn_buf *buf, u_char type, u_char **ptr);
enum asn_err
asn_commit_header(struct asn_buf *buf, u_char *ptr);
enum asn_err
asn_get_integer_raw(struct asn_buf *buf, asn_len_t len, int32_t *res);
enum asn_err
asn_get_integer(struct asn_buf *buf, int32_t *res);
enum asn_err
asn_put_integer(struct asn_buf *buf, int32_t arg);
enum asn_err
asn_get_octetstring_raw(struct asn_buf *buf, asn_len_t len, u_char *out,
u_int *outsize);
enum asn_err
asn_get_octetstring(struct asn_buf *buf, u_char *out, u_int *outsize);
enum asn_err
asn_put_octetstring(struct asn_buf *buf, const u_char *str,
u_int strsize);
enum asn_err
asn_get_null_raw(struct asn_buf *buf, asn_len_t len);
enum asn_err
asn_get_null(struct asn_buf *buf);
enum asn_err
asn_put_null(struct asn_buf *buf);
enum asn_err
asn_put_exception(struct asn_buf *buf, u_int type);
enum asn_err
asn_get_objid_raw(struct asn_buf *buf, asn_len_t len,
struct asn_oid *oid);
enum asn_err
asn_get_objid(struct asn_buf *buf, struct asn_oid *oid);
enum asn_err
asn_put_objid(struct asn_buf *buf, const struct asn_oid *oid);
enum asn_err
asn_get_sequence(struct asn_buf *buf, asn_len_t *lenp);
enum asn_err
asn_get_ipaddress_raw(struct asn_buf *buf, asn_len_t len, u_char *ipa);
enum asn_err
asn_get_ipaddress(struct asn_buf *buf, u_char *ipa);
enum asn_err
asn_put_ipaddress(struct asn_buf *buf, const u_char *ipa);
enum asn_err
asn_get_uint32_raw(struct asn_buf *buf, asn_len_t len, u_int32_t *res);
enum asn_err
asn_put_uint32(struct asn_buf *buf, u_char type, u_int32_t val);
enum asn_err
asn_get_counter64_raw(struct asn_buf *buf, asn_len_t len,
u_int64_t *res);
enum asn_err
asn_put_counter64(struct asn_buf *buf, u_int64_t val);
enum asn_err
asn_get_timeticks(struct asn_buf *buf, u_int32_t *valp);
enum asn_err
asn_put_timeticks(struct asn_buf *buf, u_int32_t val);
enum asn_err
asn_skip(struct asn_buf *buf, asn_len_t len);
void
asn_slice_oid(struct asn_oid *dest, const struct asn_oid *src,
u_int from, u_int to);
void
asn_append_oid(struct asn_oid *to, const struct asn_oid *from);
int
asn_compare_oid(const struct asn_oid *oid1, const struct asn_oid *oid2);
int
asn_is_suboid(const struct asn_oid *oid1, const struct asn_oid *oid2);
char *
asn_oid2str_r(const struct asn_oid *oid, char *buf);
char *
asn_oid2str(const struct asn_oid *oid);
β¨ DESCRIPTION β¨
okay so the ASN.1 library is basically the routine crew that handles ASN.1 encoding for SNMP, bestie. It supports only the restricted form of ASN.1 as required by SNMP. There are two basic structures used throughout the library:
/* these restrictions are in the SMI */
#define ASN_MAXID 0xffffffff
#define ASN_MAXOIDLEN 128
/* type of subidentifiers */
typedef u_int32_t asn_subid_t;
struct asn_oid {
u_int len;
asn_subid_t subs[ASN_MAXOIDLEN];
};
This structure represents an OID with the restrictions defined in the SNMP SMI. len holds the current length of the OID and subs holds the elements of the OID.
struct asn_buf {
union {
u_char *ptr;
const u_char *cptr;
} asn_u;
size_t asn_len;
};
#define asn_cptr asn_u.cptr
#define asn_ptr asn_u.ptr
This structure is used to encode and decode ASN.1. It describes the output buffer for encoding routines and the input buffer for decoding routines. For encoding, asn_len holds the number of remaining free octets in the buffer. The first free byte is pointed to by asn_ptr. For decoding, asn_len holds the number of remaining bytes to decode. The next byte to decode is pointed to by asn_cptr.
Most of the functions return an error code enum asn_error:
enum asn_err {
/* conversion was ok */
ASN_ERR_OK = 0,
/* conversion failed and stopped */
ASN_ERR_FAILED = 1 | 0x1000,
/* length field bad, value skipped */
ASN_ERR_BADLEN = 2,
/* out of buffer, stopped */
ASN_ERR_EOBUF = 3 | 0x1000,
/* length ok, but value is out of range */
ASN_ERR_RANGE = 4,
/* not the expected tag, stopped */
ASN_ERR_TAG = 5 | 0x1000,
};
#define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0)
If ASN_ERR_STOPPED() returns true, the error was fatal and processing has stopped at the point of error.
Hereβs the function-by-function tea, no details skipped:
asn_get_header()β reads the next header from the input octet stream. It returns the tag in the variable pointed to bytype(note that only single byte tags are supported) and the decoded length field in the value pointed to bylenp(this is restricted to an unsigned 32-bit value). All errors in this function are fatal and stop processing.asn_put_header()β writes an ASN.1 header.typeis the tag to write and is restricted to one byte tags (i.e., tags lesser or equal than0x30).lenis the length of the value and is restricted to 16-bit.asn_put_temp_header()andasn_commit_header()β used to write a header when the length of the value is not known in advance, for example, for sequences.asn_put_temp_header()writes a header with the given tagtypeand space for the maximum supported length field and sets the pointer pointed to byptrto the begin of this length field. This pointer must then be fed intoasn_commit_header()directly after writing the value to the buffer. The function will compute the length, insert it into the right place and shift the value if the resulting length field is shorter than the estimated one.asn_get_integer_raw()β used to decode a signed integer value (32-bit). It assumes that the header of the integer has been decoded already.lenis the length obtained from the ASN.1 header and the integer will be returned in the value pointed to byres.asn_get_integer()β decodes a complete 32-bit signed integer including the header. If the tag is wrongASN_ERR_TAGis returned.asn_put_integer()encodes a 32-bit signed integer.asn_get_octetstring_raw()β decodes the value field of an ASN.1 octet string. The length obtained from the header must be fed into thelenargument andoutmust point to a buffer to receive the octet string. On entry to the functionoutsizemust point to the size of the buffer. On exitoutsizewill point to the number of octets decoded (if no error occurs this will be equal tolen).asn_get_octetstring()decodes an octetstring including the header.outmust point to a buffer to receive the string,outsizemust point to the size of the buffer. On exit of the functionoutsizewill point to the number of octets decoded.asn_put_octetstring()encodes an octetstring (including the header).strpoints to the string to encode andstrsizeis the length of the string (the string may contain embedded NULs).asn_get_null_raw()β decodes a null value.lenis the length obtained from the header and must be0.asn_get_null()decodes a null including the header andasn_put_null()encodes a null.asn_put_exception()β used to encode an SNMPv2 exception. The exception type istype.asn_get_objid_raw()β used to decode an OID value.lenmust be the value length obtained from the header andoidwill receive the decoded OID.asn_get_objid()decodes a complete OID (including the header) andasn_put_objid()encodes a complete OID.asn_get_sequence()β decodes a sequence header. The length of the sequence value will be stored in the value pointed to bylenp.asn_get_ipaddress_raw()β decodes an IP address value.lenis the length from the header and must be4.ipawill receive the decoded IP address and must point to a buffer of at least four bytes.asn_get_ipaddress()decodes a complete IP address (including the header) andasn_put_ipaddress()encodes an IP address.asn_get_uint32_raw()β decodes an unsigned 32-bit integer value.lenis the length from the header andreswill get the decoded value.asn_put_uint32()encodes an unsigned 32-bit integer value and inserts the tag given intypeinto the header.asn_get_counter64_raw()β decodes an unsigned 64-bit integer value.lenmust be the value length from the header. The resulting value is stored into the variable pointed to byres.asn_put_counter64()encodes a complete unsigned 64-bit value.asn_get_timeticks()β decodes an ASN.1 object of typeTIMETICKSandasn_put_timeticks()encodes such an object.asn_skip()β can be used to skiplenbytes in the input buffer.asn_slice_oid()β splits a part out from an OID. It takes all the subids from the OID pointed to bysrcstarting with the subid at positionfrom(the first subid being subid0) up to, but not including, subidtoand generates a new OID indest. Iftois less or equal tofromthe resulting OID will have a length of zero.asn_append_oid()β appends the OIDfromto the OIDtogiven that the resulting OID is not too long. If the maximum length is exceeded the result is undefined.asn_compare_oid()β compares two oids and returns the values-1,0or+1whenoid1is lesser than, equal, or larger thanoid2resp.asn_is_suboid()β returns1ifoid1is equal to the leading part ofoid2. It returns0otherwise.asn_oid2str_r()β makes a printable string fromoid. The buffer pointed to bystrmust be large enough to hold the result. The constantASN_OIDSTRLENis defined to be the length of the maximum string generated by this function (including the trailing NUL).asn_oid2str()makes a printable string fromoidinto a private buffer that is overwritten by each call.
π¨ DIAGNOSTICS π¨
When an error occurs in any of the function the function pointed to by the global pointer
extern void (*asn_error)(const struct asn_buf *, const char *, ...);
is called with the current buffer (this may be NULL) and a printf(3) style format string. There is a default error handler in the library that prints a message starting with βASN.1:β followed by the error message and an optional dump of the buffer.
π SEE ALSO π
π STANDARDS π
This implementation conforms to the applicable IETF RFCs and ITU-T recommendations.
π AUTHORS π
Hartmut Brandt <harti@FreeBSD.org>
okay thatβs the whole page, bestie β source/version Debian, date October 4, 2005, and itβs the ASN1(3) page. mwah πβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2004-2005 Hartmut Brandt
- Copyright (c) 2001-2003 Fraunhofer Institute for Open Communication Systems (FhG Fokus)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms