📄 snmp_e.c
字号:
ALENGTH_T alength;rp->pdu_length = 2 /* Tag and length of request_id (an integer) */ + A_SizeOfObjectId(&(rp->pdu.trap_pdu.enterprise_objid)) + 2 /* Tag and length of net_address (a string) */ + 4 /* Size of IP address in SMI */ + 2 /* Tag and length of generic_trap (an integer) */ + A_SizeOfInt(rp->pdu.trap_pdu.generic_trap) + 2 /* Tag and length of specific_trap (an integer) */ + A_SizeOfInt(rp->pdu.trap_pdu.specific_trap) + 2 /* Tag and length of trap_time_ticks (an uinteger) */ + A_SizeOfUnsignedInt(rp->pdu.trap_pdu.trap_time_ticks) + set_vbl_sizes(&(rp->pdu.trap_pdu.trap_vbl));alength = A_SizeOfOctetString(EBufferUsed(&(rp->community)));rp->overall_length = 1 /* Size of tag on the PDU sequences */ + A_SizeOfLength(rp->pdu_length) + rp->pdu_length + 2 /* Tag and length of snmp_version (an integer) */ + A_SizeOfInt(rp->snmp_version) + 1 /* Tag for the community octetstring */ + A_SizeOfLength(alength) + alength;alength = rp->overall_length + 1 /* Size of tag for overall Message sequence */ + A_SizeOfLength(rp->overall_length);return alength;}/**************************************************************************** ENCODE_SNMP_TRAP_PDU****************************************************************************/static void encode_snmp_trap_pdu(SNMP_PKT_T *rp, EBUFFER_T *ebuffp){/* Generate the PDU header */A_EncodeType(rp->pdu_type, A_DEFAULT_SCOPE | A_CONSTRUCTOR, A_EncodeHelper, (OCTET_T *)ebuffp);A_EncodeLength(rp->pdu_length, A_EncodeHelper, (OCTET_T *)ebuffp);/* Encode enterprise */A_EncodeObjectId(A_OBJECTID, A_UNIVERSAL | A_PRIMITIVE, &(rp->pdu.trap_pdu.enterprise_objid), A_EncodeHelper, (OCTET_T *)ebuffp);/* Encode agent-addr */A_EncodeOctetString(VT_IPADDRESS & ~A_IDCF_MASK, VT_IPADDRESS & A_IDCF_MASK, rp->pdu.trap_pdu.net_address, 4, A_EncodeHelper, (OCTET_T *)ebuffp);/* Encode generic-trap */A_EncodeInt(A_INTEGER, A_UNIVERSAL | A_PRIMITIVE, rp->pdu.trap_pdu.generic_trap, A_EncodeHelper, (OCTET_T *)ebuffp);/* Encode specific-trap */A_EncodeInt(A_INTEGER, A_UNIVERSAL | A_PRIMITIVE, rp->pdu.trap_pdu.specific_trap, A_EncodeHelper, (OCTET_T *)ebuffp);/* Encode time-stamp */A_EncodeUnsignedInt(VT_TIMETICKS & ~A_IDCF_MASK, VT_TIMETICKS & A_IDCF_MASK, rp->pdu.trap_pdu.trap_time_ticks, A_EncodeHelper, (OCTET_T *)ebuffp);encode_var_bind_list(&(rp->pdu.trap_pdu.trap_vbl), ebuffp);}#endif /* #if (INSTALL_ENVOY_SNMP_VERSION_1) *//********************************************************************************* SNMP_Bufsize_For_Packet - determine the space an encoded packet requires* SYNOPSIS** \cs* ALENGTH_T SNMP_Bufsize_For_Packet* (* SNMP_PKT_T * rp * )* \ce** DESCRIPTION** This routine examines a packet and determines how much space is required to * encode it as an octet string.** \&NOTE: Run this routine before encoding the packet because this routine may * change the sizing information in packet.** IPARAMETERS* \is* \i <*rp>* Point to the packet to be encoded.* \ie** RETURNS: If successful, this routine returns the buffer size required for * encoding. Otherwise, it returns 0.** ERRNO: N/A** SEE ALSO: 'SNMP_Encode_Pkt_With_Siz()'*/ALENGTH_T SNMP_Bufsize_For_Packet(SNMP_PKT_T *rp){ switch (rp->snmp_version) {#if INSTALL_ENVOY_SNMP_VERSION_1 case SNMP_VERSION_1: if (rp->pdu_type == TRAP_PDU) return bufsize_for_trap_pkt(rp); else return bufsize_for_normal_pkt(rp);#endif /* #if INSTALL_ENVOY_SNMP_VERSION_1 */#if INSTALL_ENVOY_SNMP_VERSION_2 case SNMP_VERSION_2: return bufsize_for_normal_pkt(rp);#endif /* #if INSTALL_ENVOY_SNMP_VERSION_2 */#if INSTALL_ENVOY_SNMP_VERSION_3 case SNMP_VERSION_3: DYNCFG_IFCFGVBL_BEGIN(snmpv3_component) return DYNCFG_FUNCALL(bufsize_for_v3_pkt)(rp); DYNCFG_IFCFGVBL_END(snmpv3_component)#endif /* #if INSTALL_ENVOY_SNMP_VERSION_3 */ default: /* incorrect version number */ return(0); }}/********************************************************************************* SNMP_Encode_Packet - encode an in-memory packet and determines the size* SYNOPSIS** \cs* int SNMP_Encode_Packet * ( * SNMP_PKT_T * rp, * EBUFFER_T * rebuffp* )* \ce** DESCRIPTION** This routine encodes an in-memory packet structure. The result is a buffer * that contains a packet image ready for transmission.* This routine is a shell routine around SNMP_Encode_Pkt_With_Siz(). It * functions much in the same way except it is able to determine the length of * the encoded packet itself and provides an additional method for allocating * memory for the response packet.* There are three ways to allocate memory for the response packet:* \ml* \m -* The caller can supply the buffer in <rebuffp>.* \m -* If rebuffp is empty and the user-exit SNMP_user_get_encode_buffer() is * defined, this routine calls it to get the buffer space. If the user-exit does * supply the memory for the buffer, it must also set the buffer\抯 * BFL_IS_STATIC or BFL_IS_DYNAMIC flag.* \m -* If the user-exit SNMP_user_get_encode_buffer() returns 0, * 'SNMP_Encode_Pkt_With_Siz() 'allocates the buffer.* \me** \&NOTE: Since SNMP agents and servers use Process_Rcvd_SNMP_Packet_Async() to * handle incoming requests, most agents only use this function to encode 'TRAP' * PDUs.** PARAMETERS* \is* \i <*rp>* Point to the packet to be encoded.* \i <*rebuffp>* Point to a 'EBUFFER_T' structure to be used to define the buffer in which the * resulting PDU, if any, is to be placed.* \ie** RETURNS: This routine returns 0 when there is a good response in the response * buffer. Otherwise, it returns -1.** ERRNO: N/A** SEE ALSO: Process_Rcvd_SNMP_Packet_Async(), SNMP_Create_Request2(), * SNMP_Create_Request_V3(),SNMP_Create_Trap(), SNMP_Decode_Packet_WER(), * 'SNMP_Encode_Pkt_With_Siz(),' SNMP_user_get_encode_buffer()*/int SNMP_Encode_Packet(SNMP_PKT_T *rp, EBUFFER_T *ebuffp){ ALENGTH_T need; need = SNMP_Bufsize_For_Packet(rp); /* Give the user a chance to allocate space from other areas */#if defined(SNMP_user_get_encode_buffer) /* does the user want to get the space for us */ if (ebuffp->start_bp == 0) { if (SNMP_user_get_encode_buffer(rp, need, ebuffp) != 0) return (-1); }#endif /* translate this call into an encode packet with size call and add the size argument */ return(SNMP_Encode_Pkt_With_Siz(rp, ebuffp, need));}/********************************************************************************* SNMP_Encode_Pkt_With_Siz - encode an in-memory packet structure* SYNOPSIS** \cs* int SNMP_Encode_Pkt_With_Siz * ( * SNMP_PKT_T * rp, * EBUFFER_T * ebuffp * ALENGTH_T need * )* \ce** DESCRIPTION** This routine encodes an in-memory packet structure. The result is a buffer * containing a packet image ready for transmission. When compiled for SNMPv1, * SNMPv2, or SNMPV3, this routine is able to encode the version and choose the * correct format based on the version field in the packet.* The calling routine must determine how much space the encoded packet will * require and, may either allocate the space and attach it to <ebuffp> or pass * an initialized but empty <ebuffp>. In the later case, Envoy attempts to * allocate space using SNMP_memory_alloc(), SNMP_memory_free() How much space * is required is determined by calling SNMP_Bufsize_For_Packet(). Always run * the size routine before attempting to encode a packet, because it updates * sizing information that the encode routines use.** \&NOTE: Since SNMP agents and servers use Process_Rcvd_SNMP_Packet_Async() to * handle incoming requests, customer applications often use this function to * encode 'TRAP' PDUs.** PARAMETERS* \is* \i <*rp>* Point to the packet to be encoded.* \i <*ebuffp>* Point to a 'EBUFFER_T' structure to be used to define the buffer in which the * resulting PDU, if any, is to be placed.* \i <need>* Specify how long the packet will be when it is encoded.* \ie** RETURNS: If successful, this routine returns 0. Otherwise, it returns -1.** ERRNO: N/A** SEE ALSO: Process_Rcvd_SNMP_Packet_Async(), SNMP_Bufsize_For_Packet(), * SNMP_memory_alloc(), SNMP_memory_free()*/int SNMP_Encode_Pkt_With_Siz(SNMP_PKT_T *rp, EBUFFER_T *ebuffp, ALENGTH_T need){ /* Sanity check the space required variable */ if (need == 0) return (-1); /* Allocate some space if necessary */ if (ebuffp->start_bp == 0) { OCTET_T *buffp; /* Obtain space for the packet */ if ((buffp = (OCTET_T *)SNMP_memory_alloc(need)) == 0) return (-1); EBufferSetup(BFL_IS_DYNAMIC, ebuffp, buffp, need); } else { /* Make sure there is enough space in the buffer the user gave us */ if (EBufferRemaining(ebuffp) < need) return (-1); } /* figure out what version we have and call the proper routines to do the encoding */ switch (rp->snmp_version) {#if INSTALL_ENVOY_SNMP_VERSION_1 case SNMP_VERSION_1: /* encode the common header */ encode_snmp_common(ebuffp, rp->overall_length, rp->snmp_version, &(rp->community)); /* encode the pdu */ if (rp->pdu_type != TRAP_PDU) encode_snmp_normal_pdu(rp, ebuffp); else encode_snmp_trap_pdu(rp, ebuffp); return 0;#endif /* #if INSTALL_ENVOY_SNMP_VERSION_1 */#if INSTALL_ENVOY_SNMP_VERSION_2 case SNMP_VERSION_2: /* encode the common header */ encode_snmp_common(ebuffp, rp->overall_length, rp->snmp_version, &(rp->community)); /* encode the pdu */ encode_snmp_normal_pdu(rp, ebuffp); return 0;#endif /* #if INSTALL_ENVOY_SNMP_VERSION_2 */#if INSTALL_ENVOY_SNMP_VERSION_3 case SNMP_VERSION_3: DYNCFG_IFCFGVBL_BEGIN(snmpv3_component) return(DYNCFG_FUNCALL(encode_snmp_v3)(rp, ebuffp)); DYNCFG_IFCFGVBL_END(snmpv3_component)#endif /* #if INSTALL_ENVOY_SNMP_VERSION_3 */ default: /* incorrect version number */ return(-1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -