FreeBSD/contrib/bsnmp/lib/snmp.c
/*
 * Encode a binding. Caller must ensure, that the syntax is ok for that version.
 * Be sure not to cobber b, when something fails.
 */
Encode a binding. Caller must ensure, that the syntax is ok for that version.
Be sure not to cobber b, when something fails.
Encode a binding . Caller must ensure that the syntax is ok for that version . Be sure not to cobber b when something fails .
enum asn_err
snmp_binding_encode(struct asn_buf *b, const struct snmp_value *binding)
{
	u_char *ptr;
	enum asn_err err;
	struct asn_buf save = *b;
	if ((err = asn_put_temp_header(b, (ASN_TYPE_SEQUENCE |
	    ASN_TYPE_CONSTRUCTED), &ptr)) != ASN_ERR_OK) {
		*b = save;
		return (err);
	}
	if ((err = asn_put_objid(b, &binding->var)) != ASN_ERR_OK) {
		*b = save;
		return (err);
	}
	switch (binding->syntax) {
	  case SNMP_SYNTAX_NULL:
		err = asn_put_null(b);
		break;
	  case SNMP_SYNTAX_INTEGER:
		err = asn_put_integer(b, binding->v.integer);
		break;
	  case SNMP_SYNTAX_OCTETSTRING:
		err = asn_put_octetstring(b, binding->v.octetstring.octets,
		    binding->v.octetstring.len);
		break;
	  case SNMP_SYNTAX_OID:
		err = asn_put_objid(b, &binding->v.oid);
		break;
	  case SNMP_SYNTAX_IPADDRESS:
		err = asn_put_ipaddress(b, binding->v.ipaddress);
		break;
	  case SNMP_SYNTAX_TIMETICKS:
		err = asn_put_uint32(b, ASN_APP_TIMETICKS, binding->v.uint32);
		break;
	  case SNMP_SYNTAX_COUNTER:
		err = asn_put_uint32(b, ASN_APP_COUNTER, binding->v.uint32);
		break;
	  case SNMP_SYNTAX_GAUGE:
		err = asn_put_uint32(b, ASN_APP_GAUGE, binding->v.uint32);
		break;
	  case SNMP_SYNTAX_COUNTER64:
		err = asn_put_counter64(b, binding->v.counter64);
		break;
	  case SNMP_SYNTAX_NOSUCHOBJECT:
		err = asn_put_exception(b, ASN_EXCEPT_NOSUCHOBJECT);
		break;
	  case SNMP_SYNTAX_NOSUCHINSTANCE:
		err = asn_put_exception(b, ASN_EXCEPT_NOSUCHINSTANCE);
		break;
	  case SNMP_SYNTAX_ENDOFMIBVIEW:
		err = asn_put_exception(b, ASN_EXCEPT_ENDOFMIBVIEW);
		break;
	}
	if (err != ASN_ERR_OK) {
		*b = save;
		return (err);
	}
	err = asn_commit_header(b, ptr, NULL);
	if (err != ASN_ERR_OK) {
		*b = save;
		return (err);
	}
	return (ASN_ERR_OK);
}
enum asn_err snmp_binding_encode ( struct asn_buf * b , const struct snmp_value * binding ) { u_char * ptr ; enum asn_err err ; struct asn_buf save = * b ; if ( ( err = asn_put_temp_header ( b , ( ASN_TYPE_SEQUENCE | ASN_TYPE_CONSTRUCTED ) , & ptr ) ) != ASN_ERR_OK ) { * b = save ; return ( err ) ; } if ( ( err = asn_put_objid ( b , & binding -> var ) ) != ASN_ERR_OK ) { * b = save ; return ( err ) ; } switch ( binding -> syntax ) { case SNMP_SYNTAX_NULL : err = asn_put_null ( b ) ; break ; case SNMP_SYNTAX_INTEGER : err = asn_put_integer ( b , binding -> v . integer ) ; break ; case SNMP_SYNTAX_OCTETSTRING : err = asn_put_octetstring ( b , binding -> v . octetstring . octets , binding -> v . octetstring . len ) ; break ; case SNMP_SYNTAX_OID : err = asn_put_objid ( b , & binding -> v . oid ) ; break ; case SNMP_SYNTAX_IPADDRESS : err = asn_put_ipaddress ( b , binding -> v . ipaddress ) ; break ; case SNMP_SYNTAX_TIMETICKS : err = asn_put_uint32 ( b , ASN_APP_TIMETICKS , binding -> v . uint32 ) ; break ; case SNMP_SYNTAX_COUNTER : err = asn_put_uint32 ( b , ASN_APP_COUNTER , binding -> v . uint32 ) ; break ; case SNMP_SYNTAX_GAUGE : err = asn_put_uint32 ( b , ASN_APP_GAUGE , binding -> v . uint32 ) ; break ; case SNMP_SYNTAX_COUNTER64 : err = asn_put_counter64 ( b , binding -> v . counter64 ) ; break ; case SNMP_SYNTAX_NOSUCHOBJECT : err = asn_put_exception ( b , ASN_EXCEPT_NOSUCHOBJECT ) ; break ; case SNMP_SYNTAX_NOSUCHINSTANCE : err = asn_put_exception ( b , ASN_EXCEPT_NOSUCHINSTANCE ) ; break ; case SNMP_SYNTAX_ENDOFMIBVIEW : err = asn_put_exception ( b , ASN_EXCEPT_ENDOFMIBVIEW ) ; break ; } if ( err != ASN_ERR_OK ) { * b = save ; return ( err ) ; } err = asn_commit_header ( b , ptr , NULL ) ; if ( err != ASN_ERR_OK ) { * b = save ; return ( err ) ; } return ( ASN_ERR_OK ) ; }
Encode a binding.
Encode a binding .