Click here to Skip to main content
15,889,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to trap SNMP and have done the following code, I'm getting the data correctly but the objectID is coming out garbled, is there any reason for this

C#
int port=162;
         UdpClient listener;
        IPEndPoint groupEP;
        byte[] packet = new byte[1024];
        int commlength, miblength, datatype, datalength, datastart, Objecttype, Objectlength;
        int objectstart;
        string objectid;

        string output;

        Console.WriteLine("Initializing SNMP Listener on Port:" + port + "...");

       // try
       // {
            listener = new UdpClient(port);
            groupEP = new IPEndPoint(IPAddress.Any, port);

            while (true)
            {
                Console.WriteLine("Waiting for messages....");
                packet = listener.Receive(ref groupEP);
                Console.WriteLine("Processing new message...");
                if (packet.Length != null)
                {
                Console.Out.WriteLine("New message from {0} :\n {1}\n", groupEP.ToString(), packet);
                if (packet[0] == 0xff)
                {
                    Console.WriteLine("Invalid Packet");
                    return;
                    }

                commlength = Convert.ToInt16(packet[6]);
               miblength = Convert.ToInt16(packet[10 + commlength]);

               Objecttype = Convert.ToInt16(packet[30 + commlength + miblength]);
                Objectlength = Convert.ToInt16(packet[31 + commlength + miblength]);
                objectstart = 32 + commlength + miblength;
                datatype = Convert.ToInt16(packet[26+ Objecttype + Objectlength+commlength+ miblength]);
                datalength = Convert.ToInt16(packet[27 + Objecttype + Objectlength + commlength + miblength]);
                datastart = 28 + Objecttype + Objectlength + commlength + miblength;
                    output = Encoding.ASCII.GetString(packet, datastart, datalength);
                    objectid = Encoding.ASCII.GetString(packet, objectstart , Objectlength);
                Console.WriteLine("sysLocation - Datatype: {0}, Value: {1}", datatype,output);
                Console.WriteLine(objectid);


                }
            }



The data is stored as string hex, would the objectid be stored the same way as it contains '.' I know I can use existing libraries (freely available on the net), but I have just created myself an oppurtunity to learn.

Please advise.
Posted
Updated 11-Apr-16 16:53pm

1 solution

The OID is encoded. Microsoft says this about OID encoding:

"The OBJECT IDENTIFIER data type is encoded into a TLV triplet that begins with a Tag value of 0x06. Each integer of a dotted decimal object identifier (OID) is encoded according to the following rules:"

The rest of the article :
OBJECT IDENTIFIER (Windows)[
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900