6 Replies - 483 Views - Last Post: 07 October 2011 - 02:27 PM Rate Topic: -----

#1 Madiha Ahmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-July 11

SNMP Packet is not reaching its destination

Posted 05 October 2011 - 03:07 PM

I have SNMP Class which creates the SNMP packet and then create a UDP socket to send the packet..but when I use this class to send Get Packet then I m not getting any response...i have come to this point that packet is not going to its destination because i used wireshark to check if some traffic is going from udp port 161 but I m not getting any traffic when I debug my program....here is the part of the class that creates the socket:

Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)

        sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 4500)

        ' Dim ihe As IPHostEntry = Dns.GetHostEntry(host)

        Dim iep As New IPEndPoint(IPAddress.Parse(host), 161)

        Dim ep As EndPoint = DirectCast(iep, EndPoint)

        sock.SendTo(packet, snmplen, SocketFlags.None, iep)

        udp1.Send(packet, packet.Length, iep)

        'Receive response from packet

        Try

            Dim recv As Integer = sock.ReceiveFrom(packet, ep)

        Catch generatedExceptionName As SocketException

            packet(0) = &HFF

            End Try

            Return packet



 



and this is code of which I use to send SNMP get packet


Imports System.Text

Class Form1

    Dim commlength As Integer, miblength As Integer, datatype As Integer, datalength As Integer, datastart As Integer

    Dim uptime As Integer = 0

    Dim output As String

    Dim conn As New SNMP()

    Dim response As Byte() = New Byte(1023) {}

    ' Public Shared Sub Main(ByVal argv As String())

    'End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        IPaddress.Text = "115.186.118.188"

        Community.Text = "public"



    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



        ListBox1.Items.Add("Device SNMP information:")



        ' Send sysName SNMP request

        response = conn.[get]("get", IPaddress.Text, Community.Text, "1.3.6.1.2.1.1.5.0")

        If response(0) = &HFF Then

            ListBox1.Items.Add("No response from " & IPaddress.Text)

            Return

        End If



        ' If response, get the community name and MIB lengths

        commlength = Convert.ToInt16(response(6))

        miblength = Convert.ToInt16(response(23 + commlength))



        ' Extract the MIB data from the SNMP response

        datatype = Convert.ToInt16(response(24 + commlength + miblength))

        datalength = Convert.ToInt16(response(25 + commlength + miblength))

        datastart = 26 + commlength + miblength

        output = Encoding.ASCII.GetString(response, datastart, datalength)

        ListBox1.Items.Add("  sysName - Datatype:" & datatype & "," & output)

End Class


Is This A Good Question/Topic? 0
  • +

Replies To: SNMP Packet is not reaching its destination

#2 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: SNMP Packet is not reaching its destination

Posted 05 October 2011 - 03:23 PM

You're not supposed to use any port between 1 and 1024, they're well known popular ports n aren't fir. use
Was This Post Helpful? 0
  • +
  • -

#3 Madiha Ahmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-July 11

Re: SNMP Packet is not reaching its destination

Posted 06 October 2011 - 12:10 AM

I changed my port to 1025 but it still didn't work !
Was This Post Helpful? 0
  • +
  • -

#4 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: SNMP Packet is not reaching its destination

Posted 06 October 2011 - 12:39 AM

Make sure that no antivirus or firewall block the port. I lost 2 days once with debugging because BitDefender was blocking sockets activity.
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: SNMP Packet is not reaching its destination

Posted 06 October 2011 - 12:44 AM

Check out SixOfEleven's P2P chat - Advanced! Its good, it works and it's good t have a fiddle with. Maybe it'll work for you.
Go to the VB.Net Tutorials Forum.
Was This Post Helpful? 1
  • +
  • -

#6 Madiha Ahmed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-July 11

Re: SNMP Packet is not reaching its destination

Posted 07 October 2011 - 10:32 AM

I updated my "send and receive packet" part of the code by using this program http://www.codeproje...Receive.aspx...
and now my packet is reaching its destination but the output is somewhat ambiguous...
this is the output which I m getting

Device SNMP Information :

sysName - Datatype:48, [][][]

sysLocation-Datatype:48 Value: [][][]

[][][] shows some boxes which i m getting instead of the specific system Name and Location..

Is this some encoding problem?
here is the updated code of my class

Imports System.Net

Imports System.Net.Sockets

Imports System.Text

Imports System.Windows.Forms.Form





Class SNMP

    Dim udpClient As New UdpClient

    Public receivingUdpClient As UdpClient

    Public RemoteIpEndPoint As New System.Net.IPEndPoint(IPAddress.Parse("115.186.115.188"), 161)

    Public ThreadReceive As System.Threading.Thread



    Public Function [get](ByVal request As String, ByVal host As String, ByVal community As String, ByVal mibstring As String) As Byte()

        Dim packet As Byte() = New Byte(1023) {}

        Dim mib As Byte() = New Byte(1023) {}

        Dim snmplen As Integer

        Dim comlen As Integer = community.Length

        Dim mibvals As String() = mibstring.Split("."c)

        Dim miblen As Integer = mibvals.Length

        Dim cnt As Integer = 0, temp As Integer, i As Integer

        Dim orgmiblen As Integer = miblen

        Dim pos As Integer = 0



        ' Convert the string MIB into a byte array of integer values

        ' Unfortunately, values over 128 require multiple bytes

        ' which also increases the MIB length

        For i = 0 To orgmiblen - 1

            temp = Convert.ToInt16(mibvals(i))

            If temp > 127 Then

                mib(cnt) = Convert.ToByte(128 + (temp \ 128))

                mib(cnt + 1) = Convert.ToByte(temp - ((temp \ 128) * 128))

                cnt += 2

                miblen += 1

            Else

                mib(cnt) = Convert.ToByte(temp)

                cnt += 1

            End If

        Next

        snmplen = 29 + comlen + miblen - 1

        'Length of entire SNMP packet

        'The SNMP sequence start

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H30

        'Sequence start

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(snmplen - 2)

        'sequence size

        'SNMP version

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2

        'Integer type

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1

        'length

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0

        'SNMP version 1

        'Community name

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H4

        ' String type

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(comlen)

        'length

        'Convert community name to byte array

        Dim data As Byte() = Encoding.ASCII.GetBytes(community)

        For i = 0 To data.Length - 1

            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = data(i)

        Next



        'Add GetRequest or GetNextRequest value

        If request = "get" Then

            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &HA0

        Else

            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &HA1

        End If



        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(20 + miblen - 1)

        'Size of total MIB

        'Request ID

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2

        'Integer type

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H4

        'length

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0

        'SNMP request ID

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1



        'Error status

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2

        'Integer type

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1

        'length

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0

        'SNMP error status

        'Error index

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2

        'Integer type

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1

        'length

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0

        'SNMP error index

        'Start of variable bindings

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H30

        'Start of variable bindings sequence

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(6 + miblen - 1)

        ' Size of variable binding

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H30

        'Start of first variable bindings sequence

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(6 + miblen - 1 - 2)

        ' size

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H6

        'Object type

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(miblen - 1)

        'length

        'Start of MIB

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2B

        'Place MIB array in packet

        For i = 2 To miblen - 1

            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(mib(i))

        Next

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H5

        'Null object value

        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0

        'Null

        'Send packet to destination

        Dim IP As IPAddress

        Dim PORT As Integer

        Dim bytCommand As Byte() = New Byte() {}



        IP = IPAddress.Parse(host)

        PORT = 161



        udpClient.Connect(IP, PORT)





        Try

            receivingUdpClient = New System.Net.Sockets.UdpClient()

            ThreadReceive = New System.Threading.Thread(AddressOf packet.ToString)



        Catch generatedExceptionName As SocketException

            packet(0) = &HFF

        End Try



        Return packet

    End Function



    Public Function getnextMIB(ByVal mibin As Byte()) As String

        Dim output As String = "1.3"

        Dim commlength As Integer = mibin(6)

        Dim mibstart As Integer = 6 + commlength + 17

        'find the start of the mib section

        'The MIB length is the length defined in the SNMP packet

        ' minus 1 to remove the ending .0, which is not used

        Dim miblength As Integer = mibin(mibstart) - 1

        mibstart += 2

        'skip over the length and 0x2b values

        Dim mibvalue As Integer



        For i As Integer = mibstart To mibstart + (miblength - 1)

            mibvalue = Convert.ToInt16(mibin(i))

            If mibvalue > 128 Then

                mibvalue = (mibvalue \ 128) * 128 + Convert.ToInt16(mibin(i + 1))

                i += 1

            End If

            output += "." & mibvalue

        Next

        Return output

    End Function



End Class

Was This Post Helpful? 0
  • +
  • -

#7 Prognoob  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-August 11

Re: SNMP Packet is not reaching its destination

Posted 07 October 2011 - 02:27 PM

View PostjimmyBo, on 06 October 2011 - 12:44 AM, said:

Check out SixOfEleven's P2P chat - Advanced! Its good, it works and it's good t have a fiddle with. Maybe it'll work for you.
Go to the VB.Net Tutorials Forum.

Kewl! I checked out that tutorial and it helped me heaps! Thanks, jbo man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1