FreeBSD/contrib/bsnmp/lib/snmp.c
/*
 * Encode the SNMP PDU without the variable bindings field.
 * We do this the rather uneffective way by
 * moving things around and assuming that the length field will never
 * use more than 2 bytes.
 * We need a number of pointers to apply the fixes afterwards.
 */
Encode the SNMP PDU without the variable bindings field.
We do this the rather uneffective way by
moving things around and assuming that the length field will never
use more than 2 bytes.
We need a number of pointers to apply the fixes afterwards.
Encode the SNMP PDU without the variable bindings field . We do this the rather uneffective way by moving things around and assuming that the length field will never use more than 2 bytes . We need a number of pointers to apply the fixes afterwards .
enum snmp_code
snmp_pdu_encode_header(struct asn_buf *b, struct snmp_pdu *pdu)
{
	enum asn_err err;
	u_char *v3_hdr_ptr;
	if (asn_put_temp_header(b, (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED),
	    &pdu->outer_ptr) != ASN_ERR_OK)
		return (SNMP_CODE_FAILED);
	if (pdu->version == SNMP_V1)
		err = asn_put_integer(b, 0);
	else if (pdu->version == SNMP_V2c)
		err = asn_put_integer(b, 1);
	else if (pdu->version == SNMP_V3)
		err = asn_put_integer(b, 3);
	else
		return (SNMP_CODE_BADVERS);
	if (err != ASN_ERR_OK)
		return (SNMP_CODE_FAILED);
	if (pdu->version == SNMP_V3) {
		if (asn_put_temp_header(b, (ASN_TYPE_SEQUENCE |
		    ASN_TYPE_CONSTRUCTED), &v3_hdr_ptr) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (asn_put_integer(b, pdu->identifier) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (asn_put_integer(b, pdu->engine.max_msg_size) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (pdu->type != SNMP_PDU_RESPONSE &&
		    pdu->type != SNMP_PDU_TRAP &&
		    pdu->type != SNMP_PDU_TRAP2 &&
		    pdu->type != SNMP_PDU_REPORT)
			pdu->flags |= SNMP_MSG_REPORT_FLAG;
		if (asn_put_octetstring(b, (u_char *)&pdu->flags, 1)
		    != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (asn_put_integer(b, pdu->security_model) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (asn_commit_header(b, v3_hdr_ptr, NULL) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (pdu->security_model != SNMP_SECMODEL_USM)
			return (SNMP_CODE_FAILED);
		if (pdu_encode_secparams(b, pdu) != SNMP_CODE_OK)
			return (SNMP_CODE_FAILED);
		if (asn_put_temp_header(b, (ASN_TYPE_SEQUENCE |
		    ASN_TYPE_CONSTRUCTED), &pdu->scoped_ptr) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (asn_put_octetstring(b, (u_char *)pdu->context_engine,
		    pdu->context_engine_len) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
		if (asn_put_octetstring(b, (u_char *)pdu->context_name,
		    strlen(pdu->context_name)) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
	} else {
		if (asn_put_octetstring(b, (u_char *)pdu->community,
		    strlen(pdu->community)) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
	}
	if (asn_put_temp_header(b, (ASN_TYPE_CONSTRUCTED | ASN_CLASS_CONTEXT |
	    pdu->type), &pdu->pdu_ptr) != ASN_ERR_OK)
		return (SNMP_CODE_FAILED);
	if (pdu->type == SNMP_PDU_TRAP) {
		if (pdu->version != SNMP_V1 ||
		    asn_put_objid(b, &pdu->enterprise) != ASN_ERR_OK ||
		    asn_put_ipaddress(b, pdu->agent_addr) != ASN_ERR_OK ||
		    asn_put_integer(b, pdu->generic_trap) != ASN_ERR_OK ||
		    asn_put_integer(b, pdu->specific_trap) != ASN_ERR_OK ||
		    asn_put_timeticks(b, pdu->time_stamp) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
	} else {
		if (pdu->version == SNMP_V1 && (pdu->type == SNMP_PDU_GETBULK ||
		    pdu->type == SNMP_PDU_INFORM ||
		    pdu->type == SNMP_PDU_TRAP2 ||
		    pdu->type == SNMP_PDU_REPORT))
			return (SNMP_CODE_FAILED);
		if (asn_put_integer(b, pdu->request_id) != ASN_ERR_OK ||
		    asn_put_integer(b, pdu->error_status) != ASN_ERR_OK ||
		    asn_put_integer(b, pdu->error_index) != ASN_ERR_OK)
			return (SNMP_CODE_FAILED);
	}
	if (asn_put_temp_header(b, (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED),
	    &pdu->vars_ptr) != ASN_ERR_OK)
		return (SNMP_CODE_FAILED);
	return (SNMP_CODE_OK);
}
enum snmp_code snmp_pdu_encode_header ( struct asn_buf * b , struct snmp_pdu * pdu ) { enum asn_err err ; u_char * v3_hdr_ptr ; if ( asn_put_temp_header ( b , ( ASN_TYPE_SEQUENCE | ASN_TYPE_CONSTRUCTED ) , & pdu -> outer_ptr ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( pdu -> version == SNMP_V1 ) err = asn_put_integer ( b , 0 ) ; else if ( pdu -> version == SNMP_V2c ) err = asn_put_integer ( b , 1 ) ; else if ( pdu -> version == SNMP_V3 ) err = asn_put_integer ( b , 3 ) ; else return ( SNMP_CODE_BADVERS ) ; if ( err != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( pdu -> version == SNMP_V3 ) { if ( asn_put_temp_header ( b , ( ASN_TYPE_SEQUENCE | ASN_TYPE_CONSTRUCTED ) , & v3_hdr_ptr ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( asn_put_integer ( b , pdu -> identifier ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( asn_put_integer ( b , pdu -> engine . max_msg_size ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( pdu -> type != SNMP_PDU_RESPONSE && pdu -> type != SNMP_PDU_TRAP && pdu -> type != SNMP_PDU_TRAP2 && pdu -> type != SNMP_PDU_REPORT ) pdu -> flags |= SNMP_MSG_REPORT_FLAG ; if ( asn_put_octetstring ( b , ( u_char * ) & pdu -> flags , 1 ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( asn_put_integer ( b , pdu -> security_model ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( asn_commit_header ( b , v3_hdr_ptr , NULL ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( pdu -> security_model != SNMP_SECMODEL_USM ) return ( SNMP_CODE_FAILED ) ; if ( pdu_encode_secparams ( b , pdu ) != SNMP_CODE_OK ) return ( SNMP_CODE_FAILED ) ; if ( asn_put_temp_header ( b , ( ASN_TYPE_SEQUENCE | ASN_TYPE_CONSTRUCTED ) , & pdu -> scoped_ptr ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( asn_put_octetstring ( b , ( u_char * ) pdu -> context_engine , pdu -> context_engine_len ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( asn_put_octetstring ( b , ( u_char * ) pdu -> context_name , strlen ( pdu -> context_name ) ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; } else { if ( asn_put_octetstring ( b , ( u_char * ) pdu -> community , strlen ( pdu -> community ) ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; } if ( asn_put_temp_header ( b , ( ASN_TYPE_CONSTRUCTED | ASN_CLASS_CONTEXT | pdu -> type ) , & pdu -> pdu_ptr ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; if ( pdu -> type == SNMP_PDU_TRAP ) { if ( pdu -> version != SNMP_V1 || asn_put_objid ( b , & pdu -> enterprise ) != ASN_ERR_OK || asn_put_ipaddress ( b , pdu -> agent_addr ) != ASN_ERR_OK || asn_put_integer ( b , pdu -> generic_trap ) != ASN_ERR_OK || asn_put_integer ( b , pdu -> specific_trap ) != ASN_ERR_OK || asn_put_timeticks ( b , pdu -> time_stamp ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; } else { if ( pdu -> version == SNMP_V1 && ( pdu -> type == SNMP_PDU_GETBULK || pdu -> type == SNMP_PDU_INFORM || pdu -> type == SNMP_PDU_TRAP2 || pdu -> type == SNMP_PDU_REPORT ) ) return ( SNMP_CODE_FAILED ) ; if ( asn_put_integer ( b , pdu -> request_id ) != ASN_ERR_OK || asn_put_integer ( b , pdu -> error_status ) != ASN_ERR_OK || asn_put_integer ( b , pdu -> error_index ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; } if ( asn_put_temp_header ( b , ( ASN_TYPE_SEQUENCE | ASN_TYPE_CONSTRUCTED ) , & pdu -> vars_ptr ) != ASN_ERR_OK ) return ( SNMP_CODE_FAILED ) ; return ( SNMP_CODE_OK ) ; }
Encode the SNMP PDU without the variable bindings field.
Encode the SNMP PDU without the variable bindings field .
/*  View-based Access Conntrol information */