Main index | Section 3 | Options |
#include <bsnmp/asn1.h>
/* 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.
The function asn_get_header() reads the next header from the input octet stream. It returns the tag in the variable pointed to by type (note that only single byte tags are supported) and the decoded length field in the value pointed to by lenp (this is restricted to a unsigned 32-bit value). All errors in this function are fatal and stop processing.
The function asn_put_header() writes an ASN.1 header. type is the tag to write and is restricted to one byte tags (i.e., tags lesser or equal than 0x30). len is the length of the value and is restricted to 16-bit.
The functions asn_put_temp_header() and asn_commit_header() are 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 tag type and space for the maximum supported length field and sets the pointer pointed to by ptr to the begin of this length field. This pointer must then be fed into asn_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.
The function asn_get_integer_raw() is used to decode a signed integer value (32-bit). It assumes, that the header of the integer has been decoded already. len is the length obtained from the ASN.1 header and the integer will be returned in the value pointed to by res.
The function asn_get_integer() decodes a complete 32-bit signed integer including the header. If the tag is wrong ASN_ERR_TAG is returned. The function asn_put_integer() encodes a 32-bit signed integer.
The function asn_get_octetstring_raw() decodes the value field of an ASN.1 octet string. The length obtained from the header must be fed into the len argument and out must point to a buffer to receive the octet string. On entry to the function outsize must point to the size of the buffer. On exit outsize will point to the number of octets decoded (if no error occurs this will be equal to len, ). The function asn_get_octetstring() decodes an octetstring including the header. out must point to a buffer to receive the string, outsize must point to the size of the buffer. On exit of the function outsize will point to the number of octets decoded. The function asn_put_octetstring() encodes an octetstring (including the header). str points to the string to encode and strsize is the length of the string (the string may contain embedded NULs).
The function asn_get_null_raw() decodes a null value. len is the length obtained from the header and must be 0. The function asn_get_null() decodes a null including the header and the function asn_put_null() encodes a null.
The function asn_put_exception() is used to encode an SNMPv2 exception. The exception type is type.
The function asn_get_objid_raw() is used to decode an OID value. len must be the value length obtained from the header and oid will receive the decoded OID. The function asn_get_objid() decodes a complete OID (including the header) and the function asn_put_objid() encodes a complete OID.
The function asn_get_sequence() decodes a sequence header. The length of the sequence value will be stored in the value pointed to by lenp.
The function asn_get_ipaddress_raw() decodes an IP address value. len is the length from the header and must be 4. ipa will receive the decoded IP address and must point to a buffer of at least four bytes. The function asn_get_ipaddress() decodes a complete IP address (including the header) and asn_put_ipaddress() encodes an IP address.
The function asn_get_uint32_raw() decodes an unsigned 32-bit integer value. len is the length from the header and res will get the decoded value. The function asn_put_uint32() encodes an unsigned 32-bit integer value and inserts the tag given in type into the header.
The function asn_get_counter64_raw() decodes an unsigned 64-bit integer value. len must be the value length from the header. The resulting value is stored into the variable pointed to by res. The function asn_put_counter64() encodes a complete unsigned 64-bit value.
The function asn_get_timeticks() decodes an ASN.1 object of type TIMETICKS and the function asn_put_timeticks() encodes such an object.
The function asn_skip() can be used to skip len bytes in the input buffer.
The function asn_slice_oid() splits a part out from an OID. It takes all the subids from the OID pointed to by src starting with the subid at position from (the first subid being subid 0) up to, but not including, subid to and generates a new OID in dest. If to is less or equal to from the resulting OID will have a length of zero.
The function asn_append_oid() appends the OID from to the OID to given that the resulting OID is not too long. If the maximum length is exceeded the result is undefined.
The function asn_compare_oid() compares two oids and returns the values -1, 0 or +1 when oid1 is lesser than, equal, or larger than oid2 resp.
The function asn_is_suboid() returns 1 if oid1 is equal to the leading part of oid2. It returns 0 otherwise.
The function asn_oid2str_r() makes a printable string from oid. The buffer pointed to by str must be large enough to hold the result. The constant ASN_OIDSTRLEN is defined to be the length of the maximum string generated by this function (including the trailing NUL). The function asn_oid2str() makes a printable string from oid into a private buffer that is overwritten by each call.
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.
ASN1 (3) | October 4, 2005 |
Main index | Section 3 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | Ken Thompson was once asked by a reporter what he would have changed about Unix if he had it all to do over again. His answer: “I would spell creat with an ‘e.'” | ” |