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

📄 winextdll.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 5 页
字号:
        winExtensionAgent[winExtensionAgent_num].my_handler = netsnmp_create_handler_registration("winExtDLL",            var_winExtDLL,            name,            length,            HANDLER_CAN_RWRITE);                if (!winExtensionAgent[winExtensionAgent_num].my_handler) {          snmp_log(LOG_ERR,              "malloc failed registering handler for winExtDLL");          DEBUGMSGTL(("winExtDLL", "malloc failed registering handler for winExtDLL"));          return (-1);        }        else {          DEBUGMSGTL(("winExtDLL", "handler registered\n"));        }                // Register handler with Net-SNMP        netsnmp_register_handler(winExtensionAgent[winExtensionAgent_num].my_handler);        winExtensionAgent_num++;      }     }    winExtensionAgent_num++;  }  winExtensionAgent_index = winExtensionAgent_num-1;             // Array index  DEBUGMSGTL(("winExtDLL", "winExtensionAgent_index: %d\n",winExtensionAgent_index));  /* Reverse sort array of winExtensionAgents */  i = sizeof(winExtensionAgent) / sizeof(winExtensionAgents);  //DEBUGMSGTL(("winExtDLL", "Sorting...\n"));  for (iter=0; iter < i-1; iter++) {    for (indx=0; indx < i-1; indx++) {      if (snmp_oidtree_compare(winExtensionAgent[indx].name, winExtensionAgent[indx].name_length,            winExtensionAgent[indx+1].name, winExtensionAgent[indx+1].name_length) < 0) {        winExtensionAgent_temp = winExtensionAgent[indx];        winExtensionAgent[indx] = winExtensionAgent[indx+1];        winExtensionAgent[indx+1] = winExtensionAgent_temp;      }    }  }  DEBUGMSGTL(("winExtDLL", "Dumping sorted Windows extension OIDs\n"));  for (i=0; winExtensionAgent[i].xSnmpExtensionInit; i++) {    DEBUGMSGTL(("winExtDLL", "DLL name: %s, view: ",winExtensionAgent[i].dll_name));    DEBUGMSGOID(("winExtDLL", winExtensionAgent[i].name, winExtensionAgent[i].name_length));    DEBUGMSG(("winExtDLL", "\n"));  }  DEBUGMSGTL(("winExtDLL", "Number of subagentTrapEvents: %d\n",subagentTrapEvents_index));  if (subagentTrapEvents_index) {        DEBUGMSGTL(("winExtDLL", "Setting alarm check for subagent trap events every 5 seconds\n"));    snmp_alarm_register(5,      /* seconds */        SA_REPEAT,              /* repeat (every x seconds). */        subagentTrapCheck,      /* our callback */        NULL                    /* no callback data needed */        );      }}intvar_winExtDLL(netsnmp_mib_handler *handler,              netsnmp_handler_registration *reginfo,              netsnmp_agent_request_info *reqinfo,              netsnmp_request_info *requests){    netsnmp_request_info *request = requests;    netsnmp_variable_list *var;    oid             oid_requested[MAX_OID_LEN];    size_t          oid_requested_length;        static char     ret_szbuf_temp[SZBUF_MAX];          // Holder for return strings    static oid      ret_oid[MAX_OID_LEN];               // Holder for return OIDs    static size_t   ret_oid_length = 0;                 // Holder for return OIDs    static long     ret_long;                           // Holder for all other returns    static char     set_szbuf_temp[SZBUF_MAX];          // Holder for set strings        static oid      set_oid[MAX_OID_LEN];               // Holder for set OIDs    static size_t   set_oid_length = 0;                 // Holder for set OIDs    oid             *temp_oid;    size_t          temp_oid_length;        static int      temp;    static u_long   accesses = 7;    u_char          netsnmp_ASN_type;    u_char          windows_ASN_type;    char          *stringtemp;    // WinSNMP variables:    BOOL result;       SnmpVarBind *mySnmpVarBind;    AsnInteger32 pErrorStatus;    AsnInteger32 pErrorIndex;    SnmpVarBindList pVarBindList;      int i=0;    int j=0;    DWORD (WINAPI *xSnmpExtensionQuery)(BYTE, SnmpVarBindList* ,AsnInteger32* ,AsnInteger32*);    DWORD (WINAPI *xSnmpExtensionQueryEx)(DWORD, DWORD, SnmpVarBindList*, AsnOctetString*, AsnInteger32*, AsnInteger32*);    DWORD (WINAPI *next_xSnmpExtensionQuery)(BYTE, SnmpVarBindList* ,AsnInteger32* ,AsnInteger32*);    DWORD (WINAPI *next_xSnmpExtensionQueryEx)(DWORD, DWORD, SnmpVarBindList*, AsnOctetString*, AsnInteger32*, AsnInteger32*);    AsnObjectIdentifier winExtensionAgent_pSupportedView;               // Support view of this extension agent    oid winExtensionAgent_name[MAX_OID_LEN];                            // Support view of this extension agent    size_t winExtensionAgent_name_length;                               // Support view of this extension agent    int match_winExtensionAgent_index = -1;    DEBUGMSGTL(("winExtDLL", "-----------------------------------------\n"));    DEBUGMSGTL(("winExtDLL", "var_winExtDLL handler starting, mode = %d\n",                reqinfo->mode));        switch (reqinfo->mode) {    case MODE_GET:    case MODE_GETNEXT:      if (reqinfo->mode == MODE_GET)        DEBUGMSGTL(("winExtDLL", "GET requested\n"));      else if (reqinfo->mode == MODE_GETNEXT)        DEBUGMSGTL(("winExtDLL", "GETNEXT requested\n"));            for (request = requests; request; request=request->next) {                var = request->requestvb;        // Make copy of requested OID        for (i = 0; i < (var->name_length > MAX_OID_LEN? MAX_OID_LEN: var->name_length); i++) {          oid_requested[i] = var->name[i];        }        oid_requested_length = i;                DEBUGMSGTL(("winExtDLL", "Requested: "));        DEBUGMSGOID(("winExtDLL", oid_requested, oid_requested_length));        DEBUGMSG(("winExtDLL", "\n"));               DEBUGMSGTL(("winExtDLL", "Var type requested: %d\n",var->type));        /* Loop through all the winExtensionAgent's looking for a matching handler */        xSnmpExtensionQuery = NULL;        xSnmpExtensionQueryEx = NULL;        next_xSnmpExtensionQuery = NULL;        next_xSnmpExtensionQueryEx = NULL;        DEBUGMSGTL(("winExtDLL", "Looping through all the winExtensionAgent's looking for a matching handler (exact).\n"));        // Search starting with lowest so a walk of .1.3 starts with the lowest extension        for (i = winExtensionAgent_index; winExtensionAgent[i].xSnmpExtensionInit && i >= 0; i--) {                    /*DEBUGMSGTL(("winExtDLL", "Comparing: "));          DEBUGMSGOID(("winExtDLL", var->name, var->name_length));          DEBUGMSG(("winExtDLL", "\n"));                 DEBUGMSGTL(("winExtDLL", "to:        "));          DEBUGMSGOID(("winExtDLL", winExtensionAgent[i].name, winExtensionAgent[i].name_length));          DEBUGMSG(("winExtDLL", "\n"));*/          if (snmp_oidtree_compare(var->name, var->name_length, winExtensionAgent[i].name,                 winExtensionAgent[i].name_length) == 0) {            DEBUGMSGTL(("winExtDLL", "Found exact match: "));            DEBUGMSGOID(("winExtDLL", winExtensionAgent[i].name, winExtensionAgent[i].name_length));            DEBUGMSG(("winExtDLL", "\n"));            match_winExtensionAgent_index = i;            //DEBUGMSGTL(("winExtDLL", "Index: %d\n",match_winExtensionAgent_index));            break;          }        }        if (match_winExtensionAgent_index == -1) {          DEBUGMSGTL(("winExtDLL", "Looping through all the winExtensionAgent's looking for a matching handler (next best).\n"));          for (i=0; winExtensionAgent[i].xSnmpExtensionInit && i < MAX_WINEXT_DLLS; i++) {                        /*DEBUGMSGTL(("winExtDLL", "Comparing: "));            DEBUGMSGOID(("winExtDLL", var->name, var->name_length));            DEBUGMSG(("winExtDLL", "\n"));                               DEBUGMSGTL(("winExtDLL", "to:        "));            DEBUGMSGOID(("winExtDLL", winExtensionAgent[i].name, winExtensionAgent[i].name_length));            DEBUGMSG(("winExtDLL", "\n"));                   DEBUGMSGTL(("winExtDLL", "and:       "));            DEBUGMSGOID(("winExtDLL", winExtensionAgent[i+1].name, winExtensionAgent[i+1].name_length));            DEBUGMSG(("winExtDLL", "\n"));       */            if (snmp_oidtree_compare(var->name, var->name_length, winExtensionAgent[i].name,                   winExtensionAgent[i].name_length) <= 0 &&                snmp_oidtree_compare(var->name, var->name_length, winExtensionAgent[i+1].name,                   winExtensionAgent[i+1].name_length) >= 0                      // FIXME:  Checking past the last extension?               ) {              DEBUGMSGTL(("winExtDLL", "Found best match: "));              DEBUGMSGOID(("winExtDLL", winExtensionAgent[i].name, winExtensionAgent[i].name_length));              DEBUGMSG(("winExtDLL", "\n"));              match_winExtensionAgent_index = i;              //DEBUGMSGTL(("winExtDLL", "Index: %d\n",match_winExtensionAgent_index));              break;            }          }        }        //DEBUGMSG(("winExtDLL", "Index: %d\n",match_winExtensionAgent_index));        if (match_winExtensionAgent_index == -1) {          DEBUGMSGTL(("winExtDLL","Could not find a handler for the requested OID.  This should never happen!!\n"));          return SNMP_ERR_GENERR;        }        // Make copy of current extension's OID        for (j = 0; j < (winExtensionAgent[i].name_length > MAX_OID_LEN? MAX_OID_LEN: winExtensionAgent[i].name_length); j++) {          winExtensionAgent_name[j] = winExtensionAgent[i].name[j];        }        winExtensionAgent_name_length = j;        xSnmpExtensionQuery = winExtensionAgent[i].xSnmpExtensionQuery;        xSnmpExtensionQueryEx = winExtensionAgent[i].xSnmpExtensionQueryEx;        SnmpUtilOidCpy(&winExtensionAgent_pSupportedView, &winExtensionAgent[i].pSupportedView);        if (! (xSnmpExtensionQuery || xSnmpExtensionQueryEx)) {          if (reqinfo->mode == MODE_GET) {            DEBUGMSGTL(("winExtDLL","Could not find a handler for the requested OID.  This should never happen!!\n"));            return SNMP_ERR_GENERR;          }          else {            DEBUGMSGTL(("winExtDLL","Could not find a handler for the requested OID.  Returning SNMP_ERR_NOSUCHNAME.\n"));            return SNMP_ERR_NOSUCHNAME;          }        }                // Query        pVarBindList.list = (SnmpVarBind *) SnmpUtilMemAlloc(sizeof (SnmpVarBind));        if (pVarBindList.list) {          // Convert OID from Net-SNMP to Windows                            pVarBindList.list->name.ids = (UINT *) SnmpUtilMemAlloc(sizeof (UINT) *var->name_length);                    if (pVarBindList.list->name.ids) {            // Actual copy                        for (i = 0; i < var->name_length; i++) {              pVarBindList.list->name.ids[i] = (UINT)var->name[i];            }            pVarBindList.list->name.idLength = i;                        DEBUGMSGTL(("winExtDLL", "Windows OID: "));            DEBUGMSGWINOID(("winExtDLL", pVarBindList.list));            DEBUGMSG(("winExtDLL", "\n"));                      }          else {            DEBUGMSGTL(("winExtDLL", "\nyCould not allocate memory for Windows SNMP varbind.\n"));            return (0);          }          //pVarBindList.list = pVarBindList.list;          pVarBindList.len = 1;          	}        else {          DEBUGMSGTL(("winExtDLL", "Could not allocate memory for Windows SNMP varbind list.\n"));          return (0);        }        		        if (reqinfo->mode == MODE_GET) {          DEBUGMSGTL(("winExtDLL", "win: MODE_GET\n"));          if (xSnmpExtensionQueryEx) {            DEBUGMSGTL(("winExtDLL", "Calling xSnmpExtensionQueryEx\n"));            result = xSnmpExtensionQueryEx(SNMP_PDU_GET, 1, &pVarBindList, NULL, &pErrorStatus, &pErrorIndex);          }          else {             DEBUGMSGTL(("winExtDLL", "Calling xSnmpExtensionQuery\n"));            result = xSnmpExtensionQuery(SNMP_PDU_GET, &pVarBindList, &pErrorStatus, &pErrorIndex);          }         }        else if (reqinfo->mode == MODE_GETNEXT) {          DEBUGMSGTL(("winExtDLL", "win: MODE_GETNEXT -\n"));          if (xSnmpExtensionQueryEx) {            DEBUGMSGTL(("winExtDLL", "Calling xSnmpExtensionQueryEx\n"));            result = xSnmpExtensionQueryEx(SNMP_PDU_GETNEXT, 1, &pVarBindList, NULL, &pErrorStatus, &pErrorIndex);          }          else {             DEBUGMSGTL(("winExtDLL", "Calling xSnmpExtensionQuery\n"));            result = xSnmpExtensionQuery(SNMP_PDU_GETNEXT, &pVarBindList, &pErrorStatus, &pErrorIndex);          }           DEBUGMSGTL(("winExtDLL", "Windows OID returned from xSnmpExtensionQuery(Ex): "));          DEBUGMSGWINOID(("winExtDLL", pVarBindList.list));          DEBUGMSG(("winExtDLL", "\n"));          // Convert OID from Windows to Net-SNMP so Net-SNMP has the new 'next' OID          // FIXME:  Do we need to realloc var->name or is is MAX_OID_LEN?          for (i = 0; i < (pVarBindList.list->name.idLength > MAX_OID_LEN?MAX_OID_LEN:pVarBindList.list->name.idLength); i++) {            var->name[i] = (oid)pVarBindList.list->name.ids[i];          }          var->name_length = i;          DEBUGMSGTL(("winExtDLL", "Comparing: "));          DEBUGMSGOID(("winExtDLL", var->name, var->name_length));          DEBUGMSG(("winExtDLL", "\n"));                 DEBUGMSGTL(("winExtDLL", "to:        "));          DEBUGMSGOID(("winExtDLL", winExtensionAgent_name, winExtensionAgent_name_length));          DEBUGMSG(("winExtDLL", "\n"));                           // If the OID we got back is less than or equal to what we requested, increment the current supported view          // and send it back instead.          if (snmp_oid_compare(var->name, var->name_length, oid_requested, oid_requested_length) <= 0) {            DEBUGMSGTL(("winExtDLL", "xSnmpExtensionQuery(Ex) returned an OID less than or equal to what we requested\n"));            DEBUGMSGTL(("winExtDLL", " Setting return OID to be equal to current supported view, plus one\n"));            // Set var->name to be equal to current supported view, plus one            for (i = 0; i < (winExtensionAgent_name_length > MAX_OID_LEN? MAX_OID_LEN: winExtensionAgent_name_length); i++) {              var->name[i] = winExtensionAgent_name[i];            }            var->name_length = i;            var->name[i-1]++;            //return SNMP_ERR_NOSUCHNAME;                      }

⌨️ 快捷键说明

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