I am working on my own SNMP agent and am having trouble processing strings. I am pretty new to SNMP as well.
I've referred to the following links for some of implementing my own agent :
http://www.net-snmp.org/dev/agent/ucdDemoPublic_8c_source.html
http://www.net-snmp.org/dev/agent/example_8h_source.html
The second link shows exactly how to handle when a user attempts to set an integer type MIB object :
line 657 shows :
intval = *((long *) var_val);
My Question : How would I go about it with a string? I've tried casting it, strncpy, snprintf, etc.
My work :
I know, or at least think, that the following is legal :
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;
}
I know it is somewhat working because when I invoke
# 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
AND
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"
This proves to me at least the last collection of code is working.
Here is the action part :
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;
The above section I cannot get to work.
I've been able to get what I want by making the object an INTEGER (ASN.1) type but I can't do that because when reading this object it returns a STRING (ASN.1).