示例#1
0
        /// <summary>
        /// Implements support for the UPnP protocol, as described in:
        /// http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.0.pdf
        /// </summary>
        /// <param name="Sniffers">Sniffers.</param>
        public UPnPClient(params ISniffer[] Sniffers)
            : base(Sniffers)
        {
            Dictionary <AddressFamily, bool> GenIncoming = new Dictionary <AddressFamily, bool>();
            UdpClient Outgoing;
            UdpClient Incoming;

            foreach (NetworkInterface Interface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (Interface.OperationalStatus != OperationalStatus.Up)
                {
                    continue;
                }

                IPInterfaceProperties Properties = Interface.GetIPProperties();
                IPAddress             MulticastAddress;

                foreach (UnicastIPAddressInformation UnicastAddress in Properties.UnicastAddresses)
                {
                    if (UnicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && Socket.OSSupportsIPv4)
                    {
                        try
                        {
                            Outgoing                   = new UdpClient(AddressFamily.InterNetwork);
                            MulticastAddress           = IPAddress.Parse("239.255.255.250");
                            Outgoing.DontFragment      = true;
                            Outgoing.MulticastLoopback = false;
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    else if (UnicastAddress.Address.AddressFamily == AddressFamily.InterNetworkV6 && Socket.OSSupportsIPv6)
                    {
                        try
                        {
                            Outgoing = new UdpClient(AddressFamily.InterNetworkV6)
                            {
                                MulticastLoopback = false
                            };

                            MulticastAddress = IPAddress.Parse("[FF02::C]");
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }

                    Outgoing.EnableBroadcast   = true;
                    Outgoing.MulticastLoopback = false;
                    Outgoing.Ttl = 30;
                    Outgoing.Client.Bind(new IPEndPoint(UnicastAddress.Address, 0));
                    Outgoing.JoinMulticastGroup(MulticastAddress);

                    IPEndPoint EP = new IPEndPoint(MulticastAddress, ssdpPort);
                    this.ssdpOutgoing.AddLast(new KeyValuePair <UdpClient, IPEndPoint>(Outgoing, EP));

                    this.BeginReceiveOutgoing(Outgoing);

                    try
                    {
                        Incoming = new UdpClient(Outgoing.Client.AddressFamily)
                        {
                            ExclusiveAddressUse = false
                        };

                        Incoming.Client.Bind(new IPEndPoint(UnicastAddress.Address, ssdpPort));
                        this.BeginReceiveIncoming(Incoming);

                        this.ssdpIncoming.AddLast(Incoming);
                    }
                    catch (Exception)
                    {
                        Incoming = null;
                    }

                    if (!GenIncoming.ContainsKey(Outgoing.Client.AddressFamily))
                    {
                        GenIncoming[Outgoing.Client.AddressFamily] = true;

                        try
                        {
                            Incoming = new UdpClient(ssdpPort, Outgoing.Client.AddressFamily)
                            {
                                MulticastLoopback = false
                            };

                            Incoming.JoinMulticastGroup(MulticastAddress);
                            this.BeginReceiveIncoming(Incoming);

                            this.ssdpIncoming.AddLast(Incoming);
                        }
                        catch (Exception)
                        {
                            Incoming = null;
                        }
                    }
                }
            }
        }