Yortw / RSSDP

Really Simple Service Discovery Protocol - a 100% .Net implementation of the SSDP protocol for publishing custom/basic devices, and discovering all device types on a network.
http://yortw.github.io/RSSDP/
MIT License
288 stars 67 forks source link

uPnP discovery suddenly stopp working? #93

Open ghost opened 5 years ago

ghost commented 5 years ago

I am trying this but it does not always work:

using Rssdp;
using System;

namespace Discovery {
  class Program {
    static void Main(string[] args) {
      SearchForDevices();
      Console.ReadKey();
    }

    public static async void SearchForDevices() {
      using (var deviceLocator = new SsdpDeviceLocator()) {
        var foundDevices = await deviceLocator.SearchAsync();

        foreach (var foundDevice in foundDevices) {
          Console.WriteLine("Found " + foundDevice.Usn + " at " + foundDevice.DescriptionLocation.ToString());
        }
      }
    }
  }
}

It only shows output for the first 3 runs or so. Then for the rest of the day I get nothing. I have checked the firewall settings in windows and allowed UDP in and out on all ports. Still the same. Not even sure how to debug.

I am running this from .NET Core Console App, in Windows 10.

marchewek commented 4 years ago

UDP packets' delivery is not guaranteed - both broadcast message and response message can get lost in transfer. In general discovery should be treated as convenience improvement and implemented side-by-side with manual configuration. And since discovery can be used as a vector of DDoS attacks, I'd also recommend implementing possibility to turn discovery completely off (enabling/disabling firewall rule; I'd additionally restrict the rule to local LAN). If you want to see what is going on in the network, you could install any sniffing tool, e.g. Wireshark

Yortw commented 4 years ago

Also, I notice you have no error handling in your SearchForDevices method, and it's async void... so if an exception is occuring for some reason it's likely being swallowed silently. I would add a try catch in that method with some logging or a debugger break etc. and see if you get errors when it fails.

RSSDP does follow the SSDP spec and send most packets multiple times with a delay between each in order to 'work around' the occasional packet loss, but obviously that's still only best effort and doesn't guarantee anything. However since it works for you on the first ~3 runs and then fails, it seems a little odd to be accidental packet loss. Could be something deliberately dropping the packets due to some sort of security measure (after it sees X number of packets in a short time), but it's really hard to diagnose remotely without the ability to repro.

Yortw commented 4 years ago

Wireshark or something to what's happening on your network is probably the next step, as suggested by @marchewek

winkmichael commented 4 years ago

I've observed similar to @mohasi and numerous locations. In the end our solution was to run the query every 15 minutes via a cron job and build a MySQL data and then we display sorted by age. I tried compiling / running on Linux and Windows and found the results similar.

Yortw commented 4 years ago

That is odd, I wish I could reproduce so I had some chance of fixing it. I have it running in multiple networks and haven't seen this symptom. Debugging with a network tool like Wireshark on a problem network might help, or trying to track down common factors/configurations across those problem networks.

winkmichael commented 4 years ago

I'll pull out my code in the next few days and do some sniffing! FYI, when I first observed it I just thought somehow this library wasn't doing as good as of a job at finding devices as my Ubiquti Edge Router or UPnP Analyser in the MS Store - I'd left the tool running via a 1 minute cronjob piping to file and found that with time it would get the same results as the other tools.

I will post back once I've done some additional digging.

Yortw commented 4 years ago

Is it possible those other tools use a larger wait time on the search command? If you're only waiting say 1 second for the search and some devices take longer than that to respond, those responses will get dropped. If those other systems are waiting 5 seconds on each search, they might drop fewer responses. Maybe.

Just spitballing ideas here.

winkmichael commented 4 years ago

Yes, good idea. I just did a super unscientific test and ran the UPNP discovery on my Ubiquiti, without knowing how the thing works, two observations; 1. Nothing appears in the list for at least 15 - 20 seconds, 2. The list of devices found populates over a several minute period.

DeenAbd commented 3 years ago

I have the same problem with V4.0.4, I don't think it is a timing problem because I left the program running over night. I still can't find a solution.

wartek69 commented 1 year ago

Anyone found a solution for this problem? Same issue here