Example #1
0
        static public List <IPAddress> NormalizeInterfaceChoice(InterfaceChoice choice, AddressFamily addressFamily)
        {
            List <IPAddress> list = new List <IPAddress>();

            if (choice == InterfaceChoice.Default)
            {
                list.Add(IPAddress.Parse("0.0.0.0"));
            }
            else
            {
                List <IPInterfaceProperties> tmp = GetAllAddresses(addressFamily);
                foreach (IPInterfaceProperties props in tmp)
                {
                    foreach (IPAddressInformation info in props.AnycastAddresses)
                    {
                        if (info.Address.AddressFamily == addressFamily &&
                            info.Address.ToString() != IPAddress.Parse("127.0.0.1").ToString())
                        {
                            list.Add(info.Address);
                        }
                    }
                }
            }
            return(list);
        }
        static public List <string> Find(Zeroconf zc = null, int timeout = 5,
                                         InterfaceChoice interfaces = InterfaceChoice.All)
        {
            Zeroconf localZc = null;

            if (zc != null)
            {
                localZc = zc;
            }
            else
            {
                localZc = new Zeroconf(interfaces: interfaces);
            }

            ZeroconfServiceTypes listener = new ZeroconfServiceTypes();
            ServiceBrowser       browser  = new ServiceBrowser(
                localZc, "_services._dns-sd._udp.local.",
                listener: listener
                );

            // Wait for responses
            System.Threading.Thread.Sleep(timeout * 1000);

            // Close down anything we opened
            if (zc == null)
            {
                localZc.Close();
            }
            else
            {
                browser.Cancel();
            }

            return(listener.foundServices);
        }
Example #3
0
        /// <summary>
        /// Creates an instance of the Zeroconf class, establishing
        /// multicast communications, listening and reaping threads.
        /// </summary>
        /// <param name="interfaces">Interfaces.</param>
        public Zeroconf(InterfaceChoice interfaces = InterfaceChoice.All)
        {
            this.globalDone = false;

            /* LISTEN SOCKET START */
            this.listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                                           ProtocolType.Udp);

            this.listenSocket.SetSocketOption(SocketOptionLevel.Socket,
                                              SocketOptionName.ReuseAddress,
                                              true);

            this.listenSocket.Bind(new IPEndPoint(IPAddress.Any, Constants.MDNS_PORT));

            MulticastOption mcastOption = new MulticastOption(
                Constants.MDNS_ADDRESS, IPAddress.Any
                );

            this.listenSocket.SetSocketOption(SocketOptionLevel.IP,
                                              SocketOptionName.AddMembership,
                                              mcastOption);
            /* LISTEN SOCKET END */

            /* RESPONSE SOCKET START */
            List <IPAddress> _interfaces = Utilities.NormalizeInterfaceChoice(
                interfaces,
                AddressFamily.InterNetwork);

            this.respondSockets = new List <Socket>();
            foreach (IPAddress localAddr in _interfaces)
            {
                Socket respondSocket = new Socket(AddressFamily.InterNetwork,
                                                  SocketType.Dgram, ProtocolType.Udp);

                respondSocket.SetSocketOption(SocketOptionLevel.Socket,
                                              SocketOptionName.ReuseAddress,
                                              true);

                respondSocket.Bind(new IPEndPoint(IPAddress.Any, Constants.MDNS_PORT));

                respondSocket.SetSocketOption(SocketOptionLevel.IP,
                                              SocketOptionName.AddMembership, new MulticastOption(Constants.MDNS_ADDRESS));

                respondSocket.SetSocketOption(SocketOptionLevel.IP,
                                              SocketOptionName.MulticastTimeToLive, 255);

                this.respondSockets.Add(respondSocket);
            }
            /* RESPONSE SOCKET END */

            this.listeners    = new List <object>();
            this.browsers     = new Dictionary <object, ServiceBrowser>();
            this.services     = new Dictionary <string, ServiceInfo>();
            this.serviceTypes = new Dictionary <string, int>();

            this.Cache = new DNSCache();

            this.condition = new object();

            this.engine   = new Engine(this);
            this.listener = new Listener(this);
            this.engine.AddReader(this.listener, this.listenSocket);
            this.reaper = new Reaper(this);

            // this.debug = false;
        }