Before a subagent can receive or send any DPI packets from/to the SNMP DPI capable agent, it must "connect" to the agent and identify itself to the agent.
The following example code returns a response. We assume that there are no errors in the request, but proper code should do the checking for that. We do proper checking for lexicographic next object, but we do no checking for ULONG_MAX, or making sure that the instance ID is indeed valid (digits and dots). If we get to the end of our dpiSimpleMIB, we must return an endOfMibView as defined by the SNMP Version 2 rules.
The function returns a negative error code if an error occurs. If the connection setup is successful, it returns a handle which represents the connection and which we must use on subsequent calls to send or await DPI packets.
The second step is to identify the subagent to the agent. This is done by making a DPI-OPEN packet, sending it to the agent, and then awaiting the response from the agent. The agent may accept or deny the OPEN request. Making a DPI-OPEN packet is done by calling mkDPIopen() which expects the following parameters:
The function returns a pointer to a static buffer holding the DPI packet if successful. If it fails, it returns a NULL pointer.
Once the DPI-OPEN packet has been created, you must send it to the agent. You can use the DPIsend_packet_to_agent() function which expects the following parameters:
This function returns DPI_RC_OK (value zero) if successful. Otherwise, an appropriate DPI_RC_xxxx error code as defined in snmp_dpi.h is returned.
Now we must wait for a response to the DPI-OPEN. To await such a response, you call the DPIawait_packet_from_agent() function which expects the following parameters:
This function returns DPI_RC_OK (value zero) if successful. Otherwise, an appropriate DPI_RC_xxxx error code as defined in snmp_dpi.h is returned.
The last step is to ensure that we received a DPI-RESPONSE back from the agent. If we did, then we must ensure that the agent accepted us as a valid subagent. This will be shown by the error_code field in the DPI response packet.
The following example code establishes a connection and "opens" it by identifying yourself to the agent.
#include <snmp_dpi.h> /* DPI 2.0 API definitions */static int handle; /* handle has global scope */
static void do_connect_and_open(char *hostname_p, char *community_p) {
unsigned char *packet_p;
int rc;
unsigned long length;
snmp_dpi_hdr *hdr_p;
if (shared_mem) { /* if shared memory wanted */
handle = /* then (SHM) connect to */
DPIconnect_to_agent_SHM(1);
/* always use 1 as queueID */
} else {
handle =
DPIconnect_to_agent_TCP(
/* (TCP) connect to agent */
hostname_p, /* on this host */
community_p); /* snmp community name */
} /* endif */
if (handle < 0) exit(1); /* If it failed, exit */
packet_p = mkDPIopen( /* Make DPI-OPEN packet */
DPI_SIMPLE_SUBAGENT,
/* Our identification */
"Simple DPI subAgent",
/* description */
10L, /* Our overall timeout */
1L, /* max varBinds/packet */
DPI_NATIVE_CSET,
/* native character set */
0L, /* password length */
(unsigned char *)0);
/* ptr to password */
if (!packet_p) exit(1); /* If it failed, exit */
rc = DPIsend_packet_to_agent(
/* send OPEN packet */
handle, /* on this connection */
packet_p, /* this is the packet */
DPI_PACKET_LEN(packet_p));
/* and this is its length */
if (rc != DPI_RC_OK) exit(1);
/* If it failed, exit */
rc = DPIawait_packet_from_agent(
/* wait for response */
handle, /* on this connection */
10, /* timeout in seconds */
&packet_p, /* receives ptr to packet */
&length); /* receives packet length */
if (rc != DPI_RC_OK) exit(1);
/* If it failed, exit */
hdr_p = pDPIpacket(packet_p);
/* parse DPI packet */
if (hdr_p == snmp_dpi_hdr_NULL_p)
/* If we fail to parse it */
exit(1); /* then exit */
if (hdr_p->packet_type != SNMP_DPI_RESPONSE) exit(1);
rc = hdr_p->data_u.
resp_p->error_code;
if (rc != SNMP_ERROR_DPI_noError) exit(1); }
/* end of do_connect_and_open() */