⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpnotifytable.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * This file was generated by mib2c and is intended for use as * a mib module for the ucd-snmp snmpd agent.  *//* * This should always be included first before anything else  */#include <net-snmp/net-snmp-config.h>#include <sys/types.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif/* * minimal include directives  */#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "header_complex.h"#include "snmpNotifyTable.h"#include "snmpNotifyFilterProfileTable.h"#include "target/snmpTargetParamsEntry.h"#include "target/snmpTargetAddrEntry.h"#include "target/target.h"#include "snmp-notification-mib/snmpNotifyFilterTable/snmpNotifyFilterTable.h"#include <net-snmp/agent/agent_callbacks.h>#include <net-snmp/agent/agent_trap.h>#include <net-snmp/agent/mib_module_config.h>#ifdef USING_NOTIFICATION_LOG_MIB_NOTIFICATION_LOG_MODULE#   include "notification-log-mib/notification_log.h"#endifSNMPCallback    store_snmpNotifyTable;/* * snmpNotifyTable_variables_oid: *   this is the top level oid that we want to register under.  This *   is essentially a prefix, with the suffix appearing in the *   variable below. */oid             snmpNotifyTable_variables_oid[] =    { 1, 3, 6, 1, 6, 3, 13, 1, 1 };/* * variable2 snmpNotifyTable_variables: *   this variable defines function callbacks and type return information  *   for the snmpNotifyTable mib section  */struct variable2 snmpNotifyTable_variables[] = {    /*     * magic number        , variable type , ro/rw , callback fn  , L, oidsuffix      */#define   SNMPNOTIFYTAG         4    {SNMPNOTIFYTAG, ASN_OCTET_STR, RWRITE, var_snmpNotifyTable, 2, {1, 2}},#define   SNMPNOTIFYTYPE        5    {SNMPNOTIFYTYPE, ASN_INTEGER, RWRITE, var_snmpNotifyTable, 2, {1, 3}},#define   SNMPNOTIFYSTORAGETYPE  6    {SNMPNOTIFYSTORAGETYPE, ASN_INTEGER, RWRITE, var_snmpNotifyTable, 2,     {1, 4}},#define   SNMPNOTIFYROWSTATUS   7    {SNMPNOTIFYROWSTATUS, ASN_INTEGER, RWRITE, var_snmpNotifyTable, 2,     {1, 5}},};/* * (L = length of the oidsuffix)  *//* * global storage of our data, saved in and configured by header_complex()  */static struct header_complex_index *snmpNotifyTableStorage = NULL;static int_checkFilter(const char* paramName, netsnmp_pdu *pdu){    /*     * find appropriate filterProfileEntry     */    netsnmp_variable_list *var, *trap_var;    char                  *profileName;    size_t                 profileNameLen;    struct vacm_viewEntry *vp, *head;    int                    vb_oid_excluded = 0;    extern oid             snmptrap_oid[];    extern size_t          snmptrap_oid_len;    netsnmp_assert(NULL != paramName);    netsnmp_assert(NULL != pdu);    DEBUGMSGTL(("send_notifications", "checking filters...\n"));    /*   A notification originator uses the snmpNotifyFilterTable to filter   notifications.  A notification filter profile may be associated with   a particular entry in the snmpTargetParamsTable.  The associated   filter profile is identified by an entry in the   snmpNotifyFilterProfileTable whose index is equal to the index of the   entry in the snmpTargetParamsTable.  If no such entry exists in the   snmpNotifyFilterProfileTable, no filtering is performed for that   management target.    */    profileName = get_FilterProfileName(paramName, strlen(paramName),                                        &profileNameLen);    if (NULL == profileName) {        DEBUGMSGTL(("send_notifications", "  no matching profile\n"));        return 0;    }    /*   If such an entry does exist, the value of snmpNotifyFilterProfileName   of the entry is compared with the corresponding portion of the index   of all active entries in the snmpNotifyFilterTable.  All such entries   for which this comparison results in an exact match are used for   filtering a notification generated using the associated   snmpTargetParamsEntry.  If no such entries exist, no filtering is   performed, and a notification may be sent to the management target.    */    head = snmpNotifyFilterTable_vacm_view_subtree(profileName);    if (NULL == head) {        DEBUGMSGTL(("send_notifications", "  no matching filters\n"));        return 0;    }    /*   Otherwise, if matching entries do exist, a notification may be sent   if the NOTIFICATION-TYPE OBJECT IDENTIFIER of the notification (this   is the value of the element of the variable bindings whose name is   snmpTrapOID.0, i.e., the second variable binding) is specifically   included, and none of the object instances to be included in the   variable-bindings of the notification are specifically excluded by   the matching entries.     */    trap_var = find_varbind_in_list( pdu->variables,                                snmptrap_oid,                                snmptrap_oid_len);    if (NULL != trap_var) {        /*                             For a notification name, if none match,   then the notification name is considered excluded, and the   notification should not be sent to this management target.         */        vp = netsnmp_view_get(head, profileName, trap_var->val.objid,                              trap_var->val_len / sizeof(oid), VACM_MODE_FIND);        if ((NULL == vp) || (SNMP_VIEW_INCLUDED != vp->viewType)) {            DEBUGMSGTL(("send_notifications", "  filtered (snmpTrapOID.0 "));            DEBUGMSGOID(("send_notifications",trap_var->val.objid,                         trap_var->val_len / sizeof(oid)));            DEBUGMSG(("send_notifications", " not included)\n"));            free(head);            return 1;        }    }    /*     * check varbinds     */    for(var = pdu->variables; var; var = var->next_variable) {        /*                                                               For an   object instance, if none match, the object instance is considered   included, and the notification may be sent to this management target.         */        if (var == trap_var) {            continue;        }        vp = netsnmp_view_get(head, profileName, var->name,                              var->name_length, VACM_MODE_FIND);        if ((NULL != vp) && (SNMP_VIEW_EXCLUDED == vp->viewType)) {            DEBUGMSGTL(("send_notifications","  filtered (varbind "));            DEBUGMSGOID(("send_notifications",var->name, var->name_length));            DEBUGMSG(("send_notifications", " excluded)\n"));            vb_oid_excluded = 1;            break;        }    }    free(head);    return vb_oid_excluded;}intsend_notifications(int major, int minor, void *serverarg, void *clientarg){    struct header_complex_index *hptr;    struct snmpNotifyTable_data *nptr;    netsnmp_session *sess, *sptr;    netsnmp_pdu    *template_pdu = (netsnmp_pdu *) serverarg;    int             count = 0, send = 0;    DEBUGMSGTL(("send_notifications", "starting: pdu=%x, vars=%x\n",                template_pdu, template_pdu->variables));    for (hptr = snmpNotifyTableStorage; hptr; hptr = hptr->next) {        nptr = (struct snmpNotifyTable_data *) hptr->data;        if (nptr->snmpNotifyRowStatus != RS_ACTIVE)            continue;        if (!nptr->snmpNotifyTag)            continue;        sess = get_target_sessions(nptr->snmpNotifyTag, NULL, NULL);        /*         * filter appropriately, per section 6 of RFC 3413         */        for (sptr = sess; sptr; sptr = sptr->next) {            send = 0;#ifndef NETSNMP_DISABLE_SNMPV1            if (sptr->version == SNMP_VERSION_1 &&                minor == SNMPD_CALLBACK_SEND_TRAP1) {                send = 1;            } else#endif            if ((sptr->version == SNMP_VERSION_3#ifndef NETSNMP_DISABLE_SNMPV2C                 || sptr->version == SNMP_VERSION_2c#endif                ) && minor == SNMPD_CALLBACK_SEND_TRAP2) {                if (nptr->snmpNotifyType == SNMPNOTIFYTYPE_INFORM) {                    template_pdu->command = SNMP_MSG_INFORM;                } else {                    template_pdu->command = SNMP_MSG_TRAP2;                }                send = 1;            }            if (send && sess->paramName) {                int filter = _checkFilter(sess->paramName, template_pdu);                if (filter)                    send = 0;            }            if (send) {                send_trap_to_sess(sptr, template_pdu);                ++count;            } /* session to send to */        } /* for(sptr) */    } /* for(hptr) */    DEBUGMSGTL(("send_notifications", "sent %d notifications\n", count));#ifdef USING_NOTIFICATION_LOG_MIB_NOTIFICATION_LOG_MODULE    if (count)        log_notification(template_pdu, NULL);#endif    return 0;}#define MAX_ENTRIES 1024intnotifyTable_register_notifications(int major, int minor,                                   void *serverarg, void *clientarg){    struct targetAddrTable_struct *ptr;    struct targetParamTable_struct *pptr;    struct snmpNotifyTable_data *nptr;    int             confirm, i;    char            buf[SNMP_MAXBUF_SMALL];    netsnmp_transport *t = NULL;    struct agent_add_trap_args *args =        (struct agent_add_trap_args *) serverarg;    netsnmp_session *ss;    if (!args || !(args->ss)) {        return (0);    }    confirm = args->confirm;    ss = args->ss;    /*     * XXX: START move target creation to target code      */    for (i = 0; i < MAX_ENTRIES; i++) {        sprintf(buf, "internal%d", i);        if (get_addrForName(buf) == NULL && get_paramEntry(buf) == NULL)            break;    }    if (i == MAX_ENTRIES) {        snmp_log(LOG_ERR,                 "Can't register new trap destination: max limit reached: %d",                 MAX_ENTRIES);        snmp_sess_close(ss);        return (0);    }    /*     * address      */    ptr = snmpTargetAddrTable_create();    ptr->name = strdup(buf);    t = snmp_sess_transport(snmp_sess_pointer(ss));    memcpy(ptr->tDomain, t->domain, t->domain_length * sizeof(oid));    ptr->tDomainLen = t->domain_length;    ptr->tAddressLen = t->remote_length;    ptr->tAddress = t->remote;    ptr->timeout = ss->timeout / 1000;    ptr->retryCount = ss->retries;    SNMP_FREE(ptr->tagList);    ptr->tagList = strdup(ptr->name);    ptr->params = strdup(ptr->name);    ptr->storageType = ST_READONLY;    ptr->rowStatus = RS_ACTIVE;    ptr->sess = ss;    DEBUGMSGTL(("trapsess", "adding to trap table\n"));    snmpTargetAddrTable_add(ptr);    /*     * param      */    pptr = snmpTargetParamTable_create();    pptr->paramName = strdup(buf);    pptr->mpModel = ss->version;    if (ss->version == SNMP_VERSION_3) {        pptr->secModel = ss->securityModel;        pptr->secLevel = ss->securityLevel;        pptr->secName = (char *) malloc(ss->securityNameLen + 1);        memcpy((void *) pptr->secName, (void *) ss->securityName,               ss->securityNameLen);        pptr->secName[ss->securityNameLen] = 0;    }#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)       else {        pptr->secModel = #ifndef NETSNMP_DISABLE_SNMPV1            ss->version == SNMP_VERSION_1 ?  SNMP_SEC_MODEL_SNMPv1 : #endif                                             SNMP_SEC_MODEL_SNMPv2c;        pptr->secLevel = SNMP_SEC_LEVEL_NOAUTH;        pptr->secName = NULL;        if (ss->community && (ss->community_len > 0)) {            pptr->secName = (char *) malloc(ss->community_len + 1);            memcpy((void *) pptr->secName, (void *) ss->community,                   ss->community_len);            pptr->secName[ss->community_len] = 0;        }    }#endif    pptr->storageType = ST_READONLY;    pptr->rowStatus = RS_ACTIVE;    snmpTargetParamTable_add(pptr);    /*     * XXX: END move target creation to target code      */    /*     * notify table      */    nptr = SNMP_MALLOC_STRUCT(snmpNotifyTable_data);    nptr->snmpNotifyName = strdup(buf);    nptr->snmpNotifyNameLen = strlen(buf);    nptr->snmpNotifyTag = strdup(buf);    nptr->snmpNotifyTagLen = strlen(buf);    nptr->snmpNotifyType = confirm ?        SNMPNOTIFYTYPE_INFORM : SNMPNOTIFYTYPE_TRAP;    nptr->snmpNotifyStorageType = ST_READONLY;    nptr->snmpNotifyRowStatus = RS_ACTIVE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -