Logo
Home page Net-SNMP

Archive Search:

Require all words?

Site Search:
Google
agent_trap.h File Reference
Main Page   Data Structures   File List   Data Fields   Globals  

agent_trap.h File Reference

Go to the source code of this file.


Data Structures

struct  agent_add_trap_args

Functions

void send_easy_trap (int, int)
void send_trap_pdu (struct snmp_pdu *)
void send_v2trap (struct variable_list *)
void send_trap_vars (int, int, struct variable_list *)
void send_enterprise_trap_vars (int trap, int specific, oid *enterprise, int enterprise_length, struct variable_list *vars)
void snmpd_parse_config_authtrap (const char *, char *)
void snmpd_parse_config_trapsink (const char *, char *)
void snmpd_parse_config_trap2sink (const char *, char *)
void snmpd_parse_config_informsink (const char *, char *)
void snmpd_parse_config_trapsess (const char *, char *)
void snmpd_free_trapsinks (void)
void snmpd_parse_config_trapcommunity (const char *, char *)
void snmpd_free_trapcommunity (void)
void send_trap_to_sess (struct snmp_session *sess, struct snmp_pdu *template_pdu)
int create_trap_session (char *, u_short, char *, int, int)
int add_trap_session (struct snmp_session *, int, int, int)
int remove_trap_session (struct snmp_session *)

Function Documentation

int add_trap_session struct snmp_session *    ss,
int    pdutype,
int    confirm,
int    version
 

Definition at line 129 of file agent_trap.c.

Referenced by create_trap_session(), and snmpd_parse_config_trapsess().

00130 {
00131     if (snmp_callback_available(SNMP_CALLBACK_APPLICATION,
00132                                 SNMPD_CALLBACK_REGISTER_NOTIFICATIONS) ==
00133         SNMPERR_SUCCESS) {
00134         /* something else wants to handle notification registrations */
00135         struct agent_add_trap_args args;
00136         DEBUGMSGTL(("add_trap_session","adding callback trap sink\n"));
00137         args.ss = ss;
00138         args.confirm = confirm;
00139         snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
00140                        SNMPD_CALLBACK_REGISTER_NOTIFICATIONS,
00141                        (void *) &args);
00142     } else {
00143         /* no other support exists, handle it ourselves. */
00144         struct trap_sink *new_sink;
00145     
00146         DEBUGMSGTL(("add_trap_session","adding internal trap sink\n"));
00147         new_sink = (struct trap_sink *) malloc (sizeof (*new_sink));
00148         if ( new_sink == NULL )
00149             return 0;
00150 
00151         new_sink->sesp    = ss;
00152         new_sink->pdutype = pdutype;
00153         new_sink->version = version;
00154         new_sink->next    = sinks;
00155         sinks = new_sink;
00156     }
00157     return 1;
00158 }

int create_trap_session char *    sink,
u_short    sinkport,
char *    com,
int    version,
int    pdutype
 

Definition at line 178 of file agent_trap.c.

00181 {
00182     struct snmp_session  session, *sesp;
00183 
00184     memset (&session, 0, sizeof (struct snmp_session));
00185     session.peername = sink;
00186     session.version = version;
00187     if (com) {
00188         session.community = (u_char *)com;
00189         session.community_len = strlen (com);
00190     }
00191     session.remote_port = sinkport;
00192     sesp = snmp_open (&session);
00193 
00194     if (sesp) {
00195         return( add_trap_session( sesp, pdutype, (pdutype==SNMP_MSG_INFORM), version ));
00196     }
00197 
00198     /* diagnose snmp_open errors with the input struct snmp_session pointer */
00199     snmp_sess_perror("snmpd: create_trap_session", &session);
00200     return 0;
00201 }

int remove_trap_session struct snmp_session *    ss
 

Definition at line 160 of file agent_trap.c.

00160                                                    {
00161     struct trap_sink *sp = sinks, *prev = 0;
00162     while (sp) {
00163         if (sp->sesp == ss) {
00164             if (prev) {
00165                 prev->next = sp->next;
00166             } else {
00167                 sinks = sp->next;
00168             }
00169             free_trap_session(sp);
00170             return 1;
00171         }
00172         prev = sp;
00173         sp = sp->next;
00174     }
00175     return 0;
00176 }

void send_easy_trap int    trap,
int    specific
 

Definition at line 528 of file agent_trap.c.

Referenced by handle_snmp_packet(), and main().

00530 {
00531     send_trap_vars( trap, specific, NULL );
00532 }

void send_enterprise_trap_vars int    trap,
int    specific,
oid *    enterprise,
int    enterprise_length,
struct variable_list *    vars
 

Definition at line 283 of file agent_trap.c.

Referenced by send_trap_vars().

00287 {
00288     struct variable_list uptime_var, snmptrap_var, enterprise_var;
00289     struct variable_list *v2_vars, *last_var=NULL;
00290     struct snmp_pdu     *template_pdu;
00291     struct timeval       now;
00292     long uptime;
00293     in_addr_t *pdu_in_addr_t;
00294     struct trap_sink *sink;
00295     oid temp_oid[MAX_OID_LEN];
00296     
00297                 /*
00298                  * Initialise SNMPv2 required variables
00299                  */
00300     gettimeofday(&now, NULL);
00301     uptime = calculate_time_diff(&now, &starttime);
00302     memset (&uptime_var, 0, sizeof (struct variable_list));
00303     snmp_set_var_objid( &uptime_var, sysuptime_oid, OID_LENGTH(sysuptime_oid));
00304     snmp_set_var_value( &uptime_var, (u_char *)&uptime, sizeof(uptime) );
00305     uptime_var.type           = ASN_TIMETICKS;
00306     uptime_var.next_variable  = &snmptrap_var;
00307 
00308     memset (&snmptrap_var, 0, sizeof (struct variable_list));
00309     snmp_set_var_objid( &snmptrap_var, snmptrap_oid, OID_LENGTH(snmptrap_oid));
00310         /* value set later .... */
00311     snmptrap_var.type           = ASN_OBJECT_ID;
00312     if ( vars )
00313         snmptrap_var.next_variable  = vars;
00314     else
00315         snmptrap_var.next_variable  = &enterprise_var;
00316 
00317                         /* find end of provided varbind list,
00318                            ready to append the enterprise info if necessary */
00319     last_var = vars;
00320     while ( last_var && last_var->next_variable )
00321         last_var = last_var->next_variable;
00322 
00323     memset (&enterprise_var, 0, sizeof (struct variable_list));
00324     snmp_set_var_objid( &enterprise_var,
00325                  snmptrapenterprise_oid, OID_LENGTH(snmptrapenterprise_oid));
00326     snmp_set_var_value( &enterprise_var, (u_char *)enterprise, enterprise_length*sizeof(oid));
00327     enterprise_var.type           = ASN_OBJECT_ID;
00328     enterprise_var.next_variable  = NULL;
00329 
00330     v2_vars = &uptime_var;
00331 
00332                 /*
00333                  *  Create a template PDU, ready for sending
00334                  */
00335     template_pdu = snmp_pdu_create( SNMP_MSG_TRAP );
00336     if ( template_pdu == NULL ) {
00337                 /* Free memory if value stored dynamically */
00338         snmp_set_var_value( &enterprise_var, NULL, 0);
00339         return;
00340     }
00341     template_pdu->trap_type     = trap;
00342     template_pdu->specific_type = specific;
00343     if ( snmp_clone_mem((void **)&template_pdu->enterprise,
00344                                 enterprise, enterprise_length*sizeof(oid))) {
00345         snmp_free_pdu( template_pdu );
00346         snmp_set_var_value( &enterprise_var, NULL, 0);
00347         return;
00348     }
00349     template_pdu->enterprise_length = enterprise_length;
00350     template_pdu->flags |= UCD_MSG_FLAG_FORCE_PDU_COPY;
00351     
00352     pdu_in_addr_t = (in_addr_t *)template_pdu->agent_addr;
00353     *pdu_in_addr_t = get_myaddr();
00354     template_pdu->time = uptime;
00355 
00356                 /*
00357                  *  Now use the parameters to determine
00358                  *    which v2 variables are needed,
00359                  *    and what values they should take.
00360                  */
00361     switch ( trap ) {
00362         case -1:        /*
00363                          *      SNMPv2 only
00364                          *  Check to see whether the variables provided
00365                          *    are sufficient for SNMPv2 notifications
00366                          */
00367                 if (vars && snmp_oid_compare(vars->name, vars->name_length,
00368                                 sysuptime_oid, OID_LENGTH(sysuptime_oid)) == 0 )
00369                         v2_vars = vars;
00370                 else
00371                 if (vars && snmp_oid_compare(vars->name, vars->name_length,
00372                                 snmptrap_oid, OID_LENGTH(snmptrap_oid)) == 0 )
00373                         uptime_var.next_variable = vars;
00374                 else {
00375                         /* Hmmm... we don't seem to have a value - oops! */
00376                         snmptrap_var.next_variable = vars;
00377                 }
00378                 last_var = NULL;        /* Don't need enterprise info */
00379                 convert_v2_to_v1( vars, template_pdu );
00380                 break;
00381 
00382                         /* "Standard" SNMPv1 traps */
00383 
00384         case SNMP_TRAP_COLDSTART:
00385                 snmp_set_var_value( &snmptrap_var,
00386                                     (u_char *)cold_start_oid,
00387                                     sizeof(cold_start_oid));
00388                 break;
00389         case SNMP_TRAP_WARMSTART:
00390                 snmp_set_var_value( &snmptrap_var,
00391                                     (u_char *)warm_start_oid,
00392                                     sizeof(warm_start_oid));
00393                 break;
00394         case SNMP_TRAP_LINKDOWN:
00395                 snmp_set_var_value( &snmptrap_var,
00396                                     (u_char *)link_down_oid,
00397                                     sizeof(link_down_oid));
00398                 break;
00399         case SNMP_TRAP_LINKUP:
00400                 snmp_set_var_value( &snmptrap_var,
00401                                     (u_char *)link_up_oid,
00402                                     sizeof(link_up_oid));
00403                 break;
00404         case SNMP_TRAP_AUTHFAIL:
00405                 if (snmp_enableauthentraps == SNMP_AUTHENTICATED_TRAPS_DISABLED) {
00406                     snmp_free_pdu(template_pdu);
00407                     snmp_set_var_value( &enterprise_var, NULL, 0);
00408                     return;
00409                 }
00410                 snmp_set_var_value( &snmptrap_var,
00411                                     (u_char *)auth_fail_oid,
00412                                     sizeof(auth_fail_oid));
00413                 break;
00414         case SNMP_TRAP_EGPNEIGHBORLOSS:
00415                 snmp_set_var_value( &snmptrap_var,
00416                                     (u_char *)egp_xxx_oid,
00417                                     sizeof(egp_xxx_oid));
00418                 break;
00419 
00420         case SNMP_TRAP_ENTERPRISESPECIFIC:
00421                 memcpy( temp_oid,
00422                                     (char *)enterprise,
00423                                     (enterprise_length)*sizeof(oid));
00424                 temp_oid[ enterprise_length   ] = 0;
00425                 temp_oid[ enterprise_length+1 ] = specific;
00426                 snmp_set_var_value( &snmptrap_var,
00427                                     (u_char *)temp_oid,
00428                                     (enterprise_length+2)*sizeof(oid));
00429                 snmptrap_var.next_variable  = vars;
00430                 last_var = NULL;        /* Don't need version info */
00431                 break;
00432     }
00433     
00434 
00435                 /*
00436                  *  Now loop through the list of trap sinks,
00437                  *   sending an appropriately formatted PDU to each
00438                  */
00439     for ( sink = sinks ; sink ; sink=sink->next ) {
00440         if ( sink->version == SNMP_VERSION_1 && trap == -1 )
00441                 continue;       /* Skip v1 sinks for v2 only traps */
00442         template_pdu->command = sink->pdutype;
00443 
00444         if ( sink->version != SNMP_VERSION_1 ) {
00445             template_pdu->variables = v2_vars;
00446             if ( last_var )
00447                 last_var->next_variable = &enterprise_var;
00448         }
00449         else
00450             template_pdu->variables = vars;
00451 
00452         send_trap_to_sess(sink->sesp, template_pdu);
00453 
00454         if ( sink->version != SNMP_VERSION_1 && last_var )
00455             last_var->next_variable = NULL;
00456     }
00457 
00458     /* send stuff to registered callbacks */
00459     /* v2 traps/informs */
00460     template_pdu->variables = v2_vars;
00461     if ( last_var )
00462         last_var->next_variable = &enterprise_var;
00463 
00464     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_SEND_TRAP2,
00465                         template_pdu);
00466     
00467     if ( last_var )
00468         last_var->next_variable = NULL;
00469 
00470     /* v1 traps */
00471     template_pdu->command = SNMP_MSG_TRAP;
00472     template_pdu->variables = vars;
00473     
00474     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_SEND_TRAP1,
00475                         template_pdu);
00476 
00477                 /* Free memory if values stored dynamically */
00478     snmp_set_var_value( &enterprise_var, NULL, 0);
00479     snmp_set_var_value( &snmptrap_var, NULL, 0);
00480         /* Ensure we don't free anything we shouldn't */
00481     if ( last_var )
00482         last_var->next_variable = NULL;
00483     template_pdu->variables = NULL;
00484     snmp_free_pdu( template_pdu );
00485 }

void send_trap_pdu struct snmp_pdu *    pdu
 

Definition at line 540 of file agent_trap.c.

00541 {
00542     send_trap_vars( -1, -1, pdu->variables );
00543 }

void send_trap_to_sess struct snmp_session *    sess,
struct snmp_pdu *    template_pdu
 

Definition at line 489 of file agent_trap.c.

Referenced by send_enterprise_trap_vars().

00490                                                       {
00491     struct snmp_pdu *pdu;
00492 
00493     if (!sess || !template_pdu)
00494         return;
00495 
00496     DEBUGMSGTL(("send_trap_to_sess","sending trap type=%d, version=%d\n",
00497                 template_pdu->command, sess->version));
00498     
00499     if (sess->version == SNMP_VERSION_1 &&
00500         (template_pdu->command == SNMP_MSG_TRAP2 ||
00501          template_pdu->command == SNMP_MSG_INFORM))
00502         return; /* Skip v1 sinks for v2 only traps */
00503     template_pdu->version = sess->version;
00504     pdu = snmp_clone_pdu( template_pdu );
00505     pdu->sessid = sess->sessid; /* AgentX only ? */
00506     if ( snmp_send( sess, pdu) == 0 ) {
00507         snmp_sess_perror ("snmpd: send_trap", sess);
00508         snmp_free_pdu( pdu );
00509     }
00510     else {
00511         snmp_increment_statistic(STAT_SNMPOUTTRAPS);
00512         snmp_increment_statistic(STAT_SNMPOUTPKTS);
00513     }
00514 }

void send_trap_vars int    trap,
int    specific,
struct variable_list *    vars
 

Definition at line 516 of file agent_trap.c.

Referenced by send_easy_trap(), send_trap_pdu(), and send_v2trap().

00519 {
00520     if ( trap == SNMP_TRAP_ENTERPRISESPECIFIC )
00521         send_enterprise_trap_vars( trap, specific, objid_enterprisetrap,
00522                         OID_LENGTH(objid_enterprisetrap), vars );
00523     else
00524         send_enterprise_trap_vars( trap, specific, version_id,
00525                         OID_LENGTH(version_id), vars );
00526 }

void send_v2trap struct variable_list *    vars
 

Definition at line 534 of file agent_trap.c.

00535 {
00536     send_trap_vars( -1, -1, vars );
00537 }

void snmpd_free_trapcommunity void   
 

Definition at line 718 of file agent_trap.c.

00719 {
00720     if (snmp_trapcommunity) {
00721         free(snmp_trapcommunity);
00722         snmp_trapcommunity = NULL;
00723     }
00724 }

void snmpd_free_trapsinks void   
 

Definition at line 225 of file agent_trap.c.

00226 {
00227     struct trap_sink *sp = sinks;
00228     while (sp) {
00229         sinks = sinks->next;
00230         free_trap_session(sp);
00231         sp = sinks;
00232     }
00233 }

void snmpd_parse_config_authtrap const char *    token,
char *    cptr
 

Definition at line 553 of file agent_trap.c.

00555 {
00556     int i;
00557 
00558     i = atoi(cptr);
00559     if ( i == 0 ) {
00560         if ( !strcmp( cptr, "enable" ))
00561             i = SNMP_AUTHENTICATED_TRAPS_ENABLED;
00562         else if ( !strcmp( cptr, "disable" ))
00563             i = SNMP_AUTHENTICATED_TRAPS_DISABLED;
00564     }
00565     if (i < 1 || i > 2)
00566         config_perror("authtrapenable must be 1 or 2");
00567     else
00568         snmp_enableauthentraps = i;
00569 }

void snmpd_parse_config_informsink const char *    word,
char *    cptr
 

Definition at line 627 of file agent_trap.c.

00628 {
00629     char tmpbuf[1024];
00630     char *sp, *cp, *pp = NULL;
00631     u_short sinkport;
00632   
00633     if (!snmp_trapcommunity) snmp_trapcommunity = strdup("public");
00634     sp = strtok(cptr, " \t\n");
00635     cp = strtok(NULL, " \t\n");
00636     if (cp) pp = strtok(NULL, " \t\n");
00637     if (cp && pp) {
00638         sinkport = atoi(pp);
00639         if ((sinkport < 1) || (sinkport > 0xffff)) {
00640             config_perror("trapsink port out of range");
00641             sinkport = SNMP_TRAP_PORT;
00642         }
00643     } else {
00644         sinkport = SNMP_TRAP_PORT;
00645     }
00646     if (create_v2_inform_session(sp, sinkport,
00647                                  cp ? cp : snmp_trapcommunity) == 0) {
00648         sprintf(tmpbuf,"cannot create informsink: %s", cptr);
00649         config_perror(tmpbuf);
00650     }
00651 }

void snmpd_parse_config_trap2sink const char *    word,
char *    cptr
 

Definition at line 600 of file agent_trap.c.

00601 {
00602     char tmpbuf[1024];
00603     char *sp, *cp, *pp = NULL;
00604     u_short sinkport;
00605   
00606     if (!snmp_trapcommunity) snmp_trapcommunity = strdup("public");
00607     sp = strtok(cptr, " \t\n");
00608     cp = strtok(NULL, " \t\n");
00609     if (cp) pp = strtok(NULL, " \t\n");
00610     if (cp && pp) {
00611         sinkport = atoi(pp);
00612         if ((sinkport < 1) || (sinkport > 0xffff)) {
00613             config_perror("trapsink port out of range");
00614             sinkport = SNMP_TRAP_PORT;
00615         }
00616     } else {
00617         sinkport = SNMP_TRAP_PORT;
00618     }
00619     if (create_v2_trap_session(sp, sinkport,
00620                                cp ? cp : snmp_trapcommunity) == 0) {
00621         sprintf(tmpbuf,"cannot create trap2sink: %s", cptr);
00622         config_perror(tmpbuf);
00623     }
00624 }

void snmpd_parse_config_trapcommunity const char *    word,
char *    cptr
 

Definition at line 711 of file agent_trap.c.

00712 {
00713     if (snmp_trapcommunity) free(snmp_trapcommunity);
00714     snmp_trapcommunity = (char *)malloc (strlen(cptr)+1);
00715     copy_nword(cptr, snmp_trapcommunity, strlen(cptr)+1);
00716 }

void snmpd_parse_config_trapsess const char *    word,
char *    cptr
 

Definition at line 678 of file agent_trap.c.

00678                                                           {
00679     char *argv[MAX_ARGS];
00680     int argn, arg;
00681     struct snmp_session session, *ss;
00682 
00683     /* inform or trap?  default to trap */
00684     traptype = SNMP_MSG_TRAP2;
00685 
00686     /* create the argv[] like array */
00687     argv[0] = strdup("snmpd-trapsess"); /* bogus entry for getopt() */
00688     for(argn = 1; cptr && argn < MAX_ARGS; argn++) {
00689         argv[argn] = strdup(cptr); /* more than enough room */
00690     }
00691 
00692     arg = snmp_parse_args(argn, argv, &session, "C:", trapOptProc);
00693     do { free(argv[--argn]); } while (argn > 0);
00694 
00695     ss = snmp_open (&session);
00696 
00697     if (!ss) {
00698         config_perror("snmpd: failed to parse this line");
00699         snmp_sess_perror("snmpd: snmpd_parse_config_trapsess()", &session);
00700         return;
00701     }
00702     
00703     if (ss->version == SNMP_VERSION_1) {
00704         add_trap_session(ss, SNMP_MSG_TRAP, 0, SNMP_VERSION_1);
00705     } else {
00706         add_trap_session( ss, traptype, (traptype==SNMP_MSG_INFORM), ss->version );
00707     }
00708 }

void snmpd_parse_config_trapsink const char *    token,
char *    cptr
 

Definition at line 571 of file agent_trap.c.

00573 {
00574     char tmpbuf[1024];
00575     char *sp, *cp, *pp = NULL;
00576     u_short sinkport;
00577     
00578     if (!snmp_trapcommunity) snmp_trapcommunity = strdup("public");
00579     sp = strtok(cptr, " \t\n");
00580     cp = strtok(NULL, " \t\n");
00581     if (cp) pp = strtok(NULL, " \t\n");
00582     if (cp && pp) {
00583         sinkport = atoi(pp);
00584         if ((sinkport < 1) || (sinkport > 0xffff)) {
00585             config_perror("trapsink port out of range");
00586             sinkport = SNMP_TRAP_PORT;
00587         }
00588     } else {
00589         sinkport = SNMP_TRAP_PORT;
00590     }
00591     if (create_v1_trap_session(sp, sinkport,
00592                                cp ? cp : snmp_trapcommunity) == 0) {
00593         sprintf(tmpbuf,"cannot create trapsink: %s", cptr);
00594         config_perror(tmpbuf);
00595     }
00596 }

Generated on Sat Nov 10 14:09:54 2001 for net-snmp by doxygen1.2.11 written by Dimitri van Heesch, © 1997-2001
[an error occurred while processing this directive] [an error occurred while processing this directive]

Valid CSS!


Last modified: Wednesday, 01-Aug-2018 04:41:28 UTC
For questions regarding web content and site functionality, please write to the net-snmp-users mail list.