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
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 + "...");
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.