Broadcast Device Discovery

Tony Brix
2011-02-15
2013-05-28
  • Tony Brix
    Tony Brix
    2011-02-15

    Is there a way to send a broadcast message for device discovery?

     
  • milans
    milans
    2011-02-19

    I just have it a try and it works on the local network. Here's the code I used to test it:

    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
    
    IPEndPoint bcastTarget = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 161);
    
    SnmpV3Packet disPacket = SnmpV3Packet.DiscoveryRequest();
    byte[] outBuffer = disPacket.encode();
    
    // Send the request
    socket.SendTo(outBuffer, (EndPoint)bcastTarget);
    
    IPEndPoint inEP = new IPEndPoint(IPAddress.Any, 0);
    byte[] inbuf = new byte[32 * 1024];
    DateTime startTime = DateTime.Now;
    while((DateTime.Now - startTime).TotalSeconds < 10)
    {
        int inlen = 0;
        try
        {
            EndPoint ep = inEP;
            inlen = socket.ReceiveFrom(inbuf, ref ep);
            inEP = ep as IPEndPoint;
        }
        catch
        {
            inlen = -1;
        }
        if(inlen > 0)
        {
            if(inEP.Address.Equals(IPAddress.Broadcast))
            {
                Console.WriteLine("** Discovery response from broadcast address - ignored **");
            }
            else
            {
                try
                {
                    disPacket.decode(inbuf, inlen);
                    Console.WriteLine("SNMP agent {0}: engineID {1} boot {2} time {3}", 
                    inEP.ToString(), disPacket.USM.EngineId.ToHexString(), disPacket.USM.EngineBoots.ToString(),
                    disPacket.USM.EngineTime.ToString());
                }
                catch
                {
                    continue;
                }
            }
        }
    }
    
     
  • Tony Brix
    Tony Brix
    2011-12-13

    That works! Now I need to get the ip address of the machine and the oid: "1.3.6.1.2.1.1.1.0"

    I got the ip address but im not sure how to tell it to display the oid "1.3.6.1.2.1.1.1.0"

     
  • Tony Brix
    Tony Brix
    2011-12-13

    Also I think I need to be able to do this with SNMP V2 some of the machines dont have V3

     
  • Tony Brix
    Tony Brix
    2011-12-13

    IT WORKS! I have been looking for this for months now and it finally works.

            private void broadcast()
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
                IPEndPoint bcastTarget = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 161);
                SnmpV2Packet disPacket = new SnmpV2Packet("public");
                disPacket.Pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //System Description OID
                byte[] outBuffer = disPacket.encode();
                // Send the request
                socket.SendTo(outBuffer, (EndPoint)bcastTarget);
                IPEndPoint inEP = new IPEndPoint(IPAddress.Any, 0);
                byte[] inbuf = new byte[32 * 1024];
                DateTime startTime = DateTime.Now;
                while ((DateTime.Now - startTime).TotalSeconds < 1)
                {
                    int inlen = 0;
                    try
                    {
                        EndPoint ep = inEP;
                        inlen = socket.ReceiveFrom(inbuf, ref ep);
                        inEP = ep as IPEndPoint;
                    }
                    catch
                    {
                        inlen = -1;
                    }
                    if (inlen > 0)
                    {
                        if (!inEP.Address.Equals(IPAddress.Broadcast))
                        {
                            try
                            {
                                disPacket.decode(inbuf, inlen);
                                Console.WriteLine("Machine Name: " + disPacket.Pdu.VbList[0].Value.ToString() + 
                                "; IP Address: " + inEP.Address.ToString());
                            }
                            catch
                            {
                                continue;
                            }
                        }
                    }
                }
            }
    
     
  • Gaukhar
    Gaukhar
    2012-03-06

    I am trying to use your code. Thank you for posting it here.

    However I am no expert just trying to make my final year project work.

    I am a newbie so forgive me if this is an obvious mistake. When I try to run my code i get a SocketException which says "A connection attempt failed because the connected party did not prperly respond after a period of time, or established connection failed because conneced host failed to respond". I am obviously not setting up my network right. I have snmp service and snmp trap services running on my laptop. I am conneced to a wireless router which has another pc connected. Please can you point out the mistake?

    Thank you

     
  • Mattona
    Mattona
    2013-01-21

    Hi, I tried the code, it works with most  of the machines, but it will not get response from Win 7 and Server 2008 machines..