2016-03-03 88 views
0

我一直在关注NET-SNMP教程找到here如何获得与NET-SNMP注册回调关联的变量

设置:

在这个例子中有这行代码。

netsnmp_register_long_instance("nstAgentSubagentObject", 
           nstAgentSubagentObject_oid, 
           OID_LENGTH(nstAgentSubagentObject_oid), 
           &nstAgentSubagentObject, NULL); 

做了一些调查后,我发现NULL实际上是这个签名的回调指针。

int CallBackFunction(netsnmp_mib_handler*handler, netsnmp_handler_registration*reginfo, 
netsnmp_agent_request_info*reqinfo, netsnmp_request_info*requests) 

所以,我可以改写这样的代码前行,

netsnmp_register_long_instance("nstAgentSubagentObject", 
           nstAgentSubagentObject_oid, 
           OID_LENGTH(nstAgentSubagentObject_oid), 
           &nstAgentSubagentObject, &CallBackFunction); 

我每次使用snmpget命令我的变量回调函数CallBackFunction被称为之一。这是伟大的,但我无法找到一个方法来获得与回调AKA &nstAgentSubagentObject注册的值的指针。

我查看了4个参数的数据类型,并找不到我失踪的东西。

问:

有没有办法去,将其与回调函数注册的&nstAgentSubagentObject指针?

有没有更好的方法来关联这些回调?我在net-snmp的通用回调函数here上找到了一些文档,但是如果我现在可以只用指针来获取指针,我就可以全部设置。

谢谢!

回答

1
long nstAgentSubagentObject; 
size_t long_size = sizeof(long); 

int handle_nstAgentSubagentObject(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests) 
{ 
    printf("handle_nstAgentSubagentObject\n");  

    switch(reqinfo->mode) { 
     case MODE_GET: 
      nstAgentSubagentObject = 100500; 
      snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER, 
             &nstAgentSubagentObject, 
             long_size); 
      break; 
     default: 
      snmp_log(LOG_ERR, "unknown mode (%d) in handle_totalClients\n", reqinfo->mode); 
      return SNMP_ERR_GENERR; 
    } 
    return SNMP_ERR_NOERROR; 
} 

五月将是有人

与Counter32使用(子代理的AgentX)有用:

struct counter64 createdFlowsAll; 
size_t counter64_size = sizeof(struct counter64); // sizeof(U64); 

netsnmp_register_instance (
     netsnmp_create_handler_registration("createdFlowsAll", handle_createdFlowsAll, createdFlowsAll_oid, OID_LENGTH(createdFlowsAll_oid), HANDLER_CAN_RONLY 
    )); 

int handle_createdFlowsAll(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests) 
{ 
    size_t i; 
    u_long createdFlowsAll_t; 

    switch(reqinfo->mode) { 
     case MODE_GET: 
      createdFlowsAll_t = 100500; 

      // createdFlowsAll.high = createdFlowsAll_t & 0xffffffff00000000ULL; 
      // createdFlowsAll.low = createdFlowsAll_t & 0x00000000ffffffffULL; 

      createdFlowsAll.high = createdFlowsAll_t >> 32; 
      createdFlowsAll.low = createdFlowsAll_t & 0xffffffff; 

      snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER64, 
            &createdFlowsAll, 
            counter64_size); 
      break; 
     default: 
      snmp_log(LOG_ERR, "unknown mode (%d) in handle_createdFlowsAll\n", reqinfo->mode); 
      return SNMP_ERR_GENERR; 
    } 
    return SNMP_ERR_NOERROR; 
}