Generating a TRAP

A trap can be issued at any time after a DPI OPEN was successful. To do so, you must create a trap packet and send it to the agent. With the TRAP, you can pass all sorts of varBinds if you want. In this example, we pass two varBinds one with integer data and one with an octet string. You can also pass an Enterprise ID, but with DPI 2.0, the agent will use your subagent ID as the enterprise ID if you do not pass one with the trap. In most cases that will probably be fine.

We must first prepare a varBind list chain that contains the two variables that we want to pass along with the trap. To do so we must prepare a chain of two snmp_dpi_set_packet structures, which looks like:

struct dpi_set_packet {
  char                   *object_p;   /* ptr to OIDstring     */
  char                   *group_p;    /* ptr to sub-tree      */
  char                   *instance_p; /* ptr to rest of OID   */
  unsigned char           value_type; /* SNMP_TYPE_xxxx       */
  unsigned short          value_len;  /* value length         */
  char                   *value_p;    /* ptr to value itself  */
  struct dpi_set_packet  *next_p;     /* ptr to next in chain */
};
typedef struct dpi_set_packet        snmp_dpi_set_packet;
#define snmp_dpi_set_packet_NULL_p   ((snmp_dpi_set_packet *)0)

We can use the mkDPIset() function to prepare such a structure. This function expects the following parameters:

Memory for the varBind is dynamically allocated and the data itself is copied. Upon return, we can dispose of our own pointers and allocated memory as we please. If the call is successful, a pointer is returned as follows:

If the mkDPIset() call fails, a NULL pointer is returned.

Once we have prepared the SET-varBind data, we can create a DPI TRAP packet. To do so we can use the mkDPItrap() function which expects these parameters:

The following code creates an enterprise specific trap with specific type 1 and passes two varBinds. The first varBind with our object 1, instance 0, Integer32 value; the second varBind with our object 2, instance 0, Octet String. We pass no enterprise ID.

static int do_trap(void){
  unsigned char       *packet_p;
  int                  rc;
  snmp_dpi_set_packet *varBind_p;

  varBind_p =                        /* init the varBindchain */
     snmp_dpi_set_packet_NULL_p,     /* to a NULL pointer     */

  varBind_p = mkDPIset(              /* Make DPI set packet   */
                varBind_p,           /* ptr to varBind chain  */
                DPI_SIMPLE_MIB,      /* ptr to sub-tree       */
                DPI_SIMPLE_INTEGER,  /* ptr to rest of OID    */
                SNMP_TYPE_Integer32, /* value type Integer 32 */
                sizeof(value1),      /* length of value       */
                &value1);            /* ptr to value          */

  if (!varBind_p) return(-1);        /* If it failed, return  */

  varBind_p = mkDPIset(                 /* Make DPI set packet*/
                varBind_p,              /* ptr to varBindchain*/
                DPI_SIMPLE_MIB,         /* ptr to sub-tree    */
                DPI_SIMPLE_STRING,      /* ptr to rest of OID */
                SNMP_TYPE_DisplayString,/* value type         */
                strlen(value2_p),       /* length of value    */
                value2_p);              /* ptr to value       */

  if (!varBind_p) return(-1);        /* If it failed, return  */
  varBind_p = mkDPIset(                 /* Make DPI set packet*/
                varBind_p,              /* ptr to varBindchain*/
                DPI_SIMPLE_MIB,         /* ptr to sub-tree    */
                DPI_SIMPLE_COUNTER32,   /* ptr to rest of OID */
                SNMP_TYPE_Counter32,    /* value type         */
                sizeof(value3),         /* length of value    */
                &value3);               /* ptr to value       */

  if (!varBind_p) return(-1);        /* If it failed, return  */

  packet_p = mkDPItrap(              /* Make DPItrap packet   */
               6,                    /* enterpriseSpecific    */
               1,                    /* specific type = 1     */
               varBind_p,            /* varBind data, and use */
               (char *)0);           /* default enterpriseID  */

  if (!packet_p) return(-1);         /* If it failed, return  */

  rc  = DPIsend_packet_to_agent(     /* send TRAP packet      */
           handle,                   /* on this connection    */
           packet_p,                 /* this is the packet    */
           DPI_PACKET_LEN(packet_p));/* and this is its length*/

  return(rc);                        /* return retcode        */
} /* end of do_trap() */


[Back: Processing a CLOSE Request]
[Next: Notices]