How do I send a "real" octet string

Hello all,
I was playing around with the code example for set SNMP. I noticed that “strings” and “strings” which contain an octet string have the same data type: OCTET STRING.
Here I was able to send normal strings to a service without problems and got a “success” back. However, when I tried to send a “real” octet string I only got a

Request status = SNMP: Cannot modify variable: Bad Value.

So I wanted to ask how such an octet string has to be formatted.
Mine is as follows (without final “\0”)

char* buf = “FF FF FF 03 00 05 5D 99 0A 1C C0 B1”

It was checkt and added to the vb object it is as follows:

Snmp_pp::OctetStr octetstr(buf);
if (octetstr.valid()) {
vb.set_value(octetstr);
}
pdu += vb;
status = snmp.set(pdu, *target);

(I still don’t know how to properly indent :sweat_smile:)

In another thread here in the forum I saw that an octet can also be separated with “:” instead of spaces or, that it is lowercase. So I wanted to ask if this could be relevant.

I checked the content of the octet string with a different programm I wrote using net-snmp. I guess, that I either use the “set” function wrongly for “real” octet strings or my string isn’t properly formatted.

Kind regards
schande

If you have a hex representation of an octet string, you need to use

OctetStr::from_hex_string

to construct the OctetStr.

Hello AGENTPP,
thank you for your reply.
Right now I am getting the following output (the formatted octet string seems to contain some strange non-printable characters):

String:
FF FF FF 03 00 05 31 0F 0A 1C C0 B1 
Octet:
FF FF FF 03 00 05 31 0F 0A 1C C0 B1 
Formatted:
  FF FF FF 03 00 05 31 0F 0A 1C C0 B1                 ......].....
 
20220919.01:50:02: 140642050438720: (4)DEBUG  : SNMPMessage: return value for build message: (0)
Request status = SNMP Cannot perform operation, General Error

Produced by the following Code:

char str[] = "FF FF FF 03 00 05 31 0F 0A 1C C0 B1\0";
std::cout << "String:\n" << str << std::endl;
Snmp_pp::OctetStr octetstr(str);
std::cout << "Octet:\n" << octetstr.get_printable() << std::endl;
Snmp_pp::OctetStr formattedOctStr = Snmp_pp::OctetStr::from_hex_string(octetstr);
if (formattedOctStr.valid()) {
    std::cout << "Formatted:\n" << formattedOctStr.get_printable() << std::endl;
    vb.set_value(formattedOctStr);
} else {
    std::cout << "Invalid OctetStr\n";
}
pdu += vb;
status = snmp.set(pdu, *target);

Maybe it will help to find a solution if I try to explain what I am trying to imitate.
When I try to test my target from the command line, I use the following command. (From net-snmp project)

snmpset -v1 -c user XX.XXX.XX.XXX 1.3.6.1.4.1.1206.4.2.3.6.3.0 x “FF FF FF 03 00 05 0A A1 0A 1C C0 B1”

Here I am just trying to send a hex coded string to the targets address. The “x” indicates a “Hex String” and the payload can be, but not has to be null terminated. From this, I thought it would be sufficient to just send the string as is to the target.

Kind regards
schande

It seems, that the snmpset uses an internal function to format the hex string to binary before sending it:

https://net-snmp.sourceforge.io/dev/agent/group__util.html#ga8c1a230e987560600de9f81e55480c0a

I will dig into that and share my findings here.

Kind Regards

Hi,

the code with OctetStr::from_hex_string() should work. From the error string "SNMP: Cannot perform operation, General Error" with the value #define SNMP_ERROR_GENERAL_VB_ERR 5 //!< General VB error, see error index I would assume that your Vb object is not properly created.

Are all the different HEX Strings from your posts valid values?

Kind regards,
Jochen

Hello,
I had just written a huge wall of text in which I had completely described my problem and my setup. Why I had two different strings in my posts. I told myself everything again and went through my code and the posts here again.

What can I say. I think I should buy myself a duck. :smiley:

This is how to use the from_hex_string(char* str) function properly(in my case):

const char* str = "FF FF FF 03 00 05 31 0F 0A 1C C0 B1";  
Snmp_pp::OctetStr ostr = Snmp_pp::OctetStr::from_hex_string(str);
vb.set_value(ostr);

I think what confused me, besides my not yet develloped C++ skills, was that the documentation states, that the from_hex_string() function expects a const OctetStr& hex_String, but in my case worked with a char* hex_String

Thank you for your answers and time! If I can somehow buy you a coffee, you are welcome to message me. :wink:

Kind regards
schande