This project has moved and is read-only. For the latest updates, please go here.

SNMP Trap Receiver data encoding

Topics: usage (how to ...)
Sep 16, 2008 at 3:51 PM
Edited Sep 16, 2008 at 3:52 PM
Hi,
Thanks for the great job creating this library! It's very useful.
I am writing a school project and I have the following problem using the #SNMP library.
When I retrieve the data from an SNMP trap and write it to a log file, or send it via e-mail, I get the following text:

Variable: Id: .1.3.6.1.2.1.1.1.0; Data: 祳瑳浥搠獥牣灩楴湯


The data being sent by the device is in English. Is there a way to format the output, so it appears in English. Thanks in advance.
Sep 16, 2008 at 6:27 PM
I used release 15686 and it works fine now. Maybe the cause was UTF16 default encoding.
Sep 17, 2008 at 2:55 AM
For Change Sets later than 15687 there should be a way to get ASCII string, too. Once you received the Variable instance, you can do the following,

Variable variable;
OctetString data = (OctetString)variable.Data;
string ansi = data.ToString(Encoding.ASCII);

By providing an encoding explicitly, you should receive an English string.
Marked as answer by lextm on 10/6/2013 at 9:29 PM
Sep 17, 2008 at 7:36 AM
Thank you! I will try this.
Sep 17, 2008 at 7:44 AM
Now I can ensure you that it is Unicode encoding that causes the weird string you received from OctetString.ToString().

I wrote a short program to illustrat it,

            string test = "⁁祳瑳浥搠獥牣灩楴湯";
            byte[] bytes = Encoding.Unicode.GetBytes(test);
            string orig = Encoding.ASCII.GetString(bytes);
            Console.WriteLine(orig);

And guess what? orig = "A system description".

Therefore, if you try the sample code I provided last time, you should get the correct result from OctetString.ToString(Encoding.ASCII).

Regards,

-Lex

P.S., when a OctetString instance is initiated by OctetString(byte[]), it stores bytes without explicit encoding specified. (When #SNMP handles incoming SNMP packets, OctetString instances are initiated this way.) Therefore, if ToString() is called, those bytes are decoded as Unicode by default. In your case, ToString(Encoding.ASCII) should be called to decode the bytes as ASCII. Right now I am not yet sure which encoding should be set as default. So any comments are welcome.

You may give a vote to this poll on my blog to express your idea.
http://lextm.blogspot.com
Marked as answer by lextm on 10/6/2013 at 9:29 PM
Sep 17, 2008 at 8:16 AM
Hi,

If I follow correctly the conversation I need to transform bytes in ASCII to UTF-16 in order to obtain the data received. But I have problem to understand the opposite way.

Example:

If I need to send a community string in ASCII, for example 'public' the follow code fails:

           Variable test = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 1111, 2, 2, 1, 3, 0 }));
            List<Variable> list= new List<Variable>();
            List<Variable> res;
            list.Add(test);
            manager.DefaultVersion = VersionCode.V2;
            res = (List<Variable>)manager.Get("192.168.2.106", "public", list);

becuase "public" is transformed in Unicode (with two bytes for character). In the other extreme, the SNMP agent waits for ASCII strings. So, the question is, how I can provide the communnity string in pure ASCII with SharpSnmpLib?

Regards.
Sep 17, 2008 at 10:32 AM
Hi thanks for pointing out this practical issue kindly. Yes, it is quite important to support such "ASCII-only" agents (luckily Windows Vista SNMP agent handles Unicode community name and other stuffs well), so extreme cases should be considered.

My current plan is to provide an extra interface (a static property DefaultEncoding) in OctetString.cs so you can force it to use any encoding you prefer as default encoding. In that way, you can assign Encoding.ASCII to that property and everything goes on the ASCII way.

The drawback for this approach is that this extra property is global. May it be better if this encoding is device/agent-based? But right now I don't have a clear insight of a possible solution.

-Lex

P.S., this global property will appear soon in a new check in/Change Set.
Sep 17, 2008 at 11:12 AM
IMHO, normally we have a lot of devices, with different encoding, so the solution might be in order to be possible to change this property with every device or, eventhough, every snmp command.
Sep 22, 2008 at 12:09 PM
It is possible to provide new interface which allows to override encoding for each SNMP command. One easy way is to add an Encoding parameter to all Manager's static methods. I would consider other alternatives, too. But this is not going to be scheduled for TwinTower. 
Sep 28, 2008 at 9:01 AM
Hi I have just released TwinTower RC2 which contains a new interface for string encoding. You may check this updated post for details. I hope you find it helpful.

http://www.codeplex.com/sharpsnmplib/Release/ProjectReleases.aspx?ReleaseId=15319

http://lextm.blogspot.com/2008/09/snmp-design-breaking-changes-coming.html
Marked as answer by lextm on 10/6/2013 at 9:29 PM
Oct 5, 2008 at 9:41 AM
Dear Lex,

We have tested several functions of your product these days ago, and we think it is stable enough for our tests. I write these words for feedback, and also to congratulate on your excellent job. If I have more bugs or improvements I'll communicate to you.

Best regards.
Oct 11, 2008 at 10:28 AM
Hi joborrego,

Thanks for the feedback :)

-Lex