|
|
#include <snmp/snmp.h>short build_pdu(pdu_ptr); Pdu *pdu_ptr; /* pointer to PDU struct returned by make_pdu(SLIB-osr5) */
void free_pdu(pdu_otr); Pdu *pdu_ptr;
Pdu * make_pdu(type, request_id, error_status, error_index, enterprise, agent_addr, generic_trap, specific_trap, time_ticks) short type; /* PDU type: GET_REQUEST_TYPE, SET_REQUEST_TYPE, ... */ long request_id; /* SNMP request id number */ long error_status; /* SNMP packet error status to send in PDU */ long error_index; /* index of the erroneous varbind in the PDU */ OID enterprise; /* Enterprise Object Identifier for trap PDUs */ OctetString *agent_addr;/* agent IP addr for trap PDUs */ long generic_trap; /* generic trap type */ long specific_trap; /* enterprise specific trap type */ long time_ticks; /* Agent's time stamp for trap */
Pdu * parse_pdu(packet, length) u_char **packet; /* pointer to the packet after removing headers */ long *length; /* length of the packet after removing headers */
free_pdu frees all memory associated with the PDU header data structure, including the actual packlet and all VarBind structures that were linked to the PDU.
make_pdu is called to create the initial header block for building the SNMP ASN.1 data structure, which upon completion is used to build the actual SNMP packet. It returns a pointer to a malloc'ed data structure of type PDU:
typedef struct _Pdu { OctetString *packlet; /* BER encoding of the SNMP PDU, filled by build_pdu(SLIB-osr5) */ short type; /* the type of SNMP PDU*/ union { NormPdu normpdu; /* holds any non-TRAP_TYPE PDUs */ TrapPdu trappdu; /* holds SNMPv1 TRAP_TYPE PDUs */ } u; VarBind *var_bind_list; /* pointer to list of var_binds */ VarBind *var_bind_end_ptr; /* pointer to end of the list */ } Pdu;
The type is one of GET_REQUEST_TYPE, GET_NEXT_REQUEST_TYPE, GET_RESPONSE_TYPE, SET_REQUEST_TYPE, or TRAP_TYPE. The request_id is the identification number assigned to the particular packet by the application. Since the application is UDP based, retry is controlled solely by the network management application. The error_status is set to other than 0 only for type GET_RESPONSE_TYPE, indicating that this response is in reply to a bad request. The error_index is used only by GET_RESPONSE_TYPE and points to the VarBind entry in the PDU that offends the agent. The enterprise is used by TRAP_TYPE PDUs and is an object identifier associated with the entity generating the trap. The agent_addr is used by the TRAP_TYPE PDU and consists of an octet string containing the IP address of the entity generating the trap. The generic_trap and specific_trap are used by the TRAP_TYPE PDU and consist of integers that indicate which type of trap this PDU represents. The time_ticks is the TRAP_TYPE emitting entity's sense of time since the agent has restarted.
This routine is called once for each packet to be generated. The PDU pointer is then passed repeatedly to the routine link_varbind(SLIB-osr5) to string VarBinds into the packet. build_pdu(SLIB-osr5) is then called to perform the ASN.1 encoding of the PDU and place the result in the PDU pointer's packlet field. After the packlet has been wrapped in an authentication envelope, it is freed by passing the pointer to free_pdu(SLIB-osr5).
parse_pdu takes pointers to a serialized packet and its length and parses the information into the library's internal PDU format, including all VarBindList structures. This routine is usually called with pointers to a BER encoded packet (remaining after extracting the headers) and its length. The (pointer to the) PDU returned from this call is the same state as the PDU in a build phase after build_pdu has been called. If this routine fails, it returns a NULL.