0

我正在开发自己的 SNMP 代理,但在处理字符串时遇到了问题。我对 SNMP 也很陌生。

我参考了以下链接来实现我自己的代理:

http://www.net-snmp.org/dev/agent/ucdDemoPublic_8c_source.html

http://www.net-snmp.org/dev/agent/example_8h_source.html

第二个链接显示了当用户尝试设置整数类型 MIB 对象时如何处理:

第 657 行显示:

intval = *((long *) var_val);

我的问题:我将如何使用字符串?我试过铸造它,strncpy,snprintf等。

我的工作 :

我知道,或者至少认为,以下是合法的:

int
setString(int action,
                 u_char * var_val,
                 u_char var_val_type,
                 size_t var_val_len,
                 u_char * statP, oid * name, size_t name_len)
{
    unsigned char publicString[10];
    static long intval;
    char *cmd_string = NULL;

    /*
     * Define an arbitrary maximum permissible value
     */
    switch (action) {
    case RESERVE1:

        //intval = *((long *) var_val);

        /*
         *  Check that the value being set is acceptable
         */
        if (var_val_type != ASN_OCTET_STR) {
            DEBUGMSGTL(("setString", "%x not string type", var_val_type));
            return SNMP_ERR_WRONGTYPE;
        }

        if (var_val_len > 1 ) {
            DEBUGMSGTL(("setString", "wrong length %" NETSNMP_PRIz "u",
                        var_val_len));
            return SNMP_ERR_WRONGLENGTH;
        }

        if ( !(var_val[0] == '1' || var_val[0] == '0') )
        {
            DEBUGMSGTL(("setString", "wrong value %s", var_val));
            return SNMP_ERR_WRONGVALUE;
        }

我知道它有点工作,因为当我调用

# snmpset -v 2c -c xxx 10.20.30.40 1.3.6.1.4.1.54321.3.0 s 3
Error in packet.
Reason: wrongValue (The set value is illegal or unsupported in some way)
Failed object: MY-TEST-MIB::testSnmp.3.0

snmpset -v 2c -c xxx 10.20.30.40 1.3.6.1.4.1.54321.3.0 s 1
MY-TEST-MIB::testSnmp.3.0 = STRING: "1"

这至少向我证明了最后一组代码是有效的。

这是动作部分:

   case ACTION:
        /*
         *  Set the variable as requested.
         *   Note that this may need to be reversed,
         *   so save any information needed to do this.
         */

        if ( var_val[0] == '1' )
        {
            //do stuff - there realy is a script call here that does something
        }

        if ( var_val[0] == '0' )
        {
            //do stuff - there realy is a script call here that does something
        }
        break;

以上部分我无法上班。

通过将对象设为 INTEGER (ASN.1) 类型,我已经能够得到我想要的,但我不能这样做,因为在读取此对象时它返回一个 STRING (ASN.1)。

4

0 回答 0