示例#1
0
        private void OKClicked(object sender, RoutedEventArgs e)
        {
            List <string> errors = new List <string>();

            Location location;

            using (new WaitCursor())
            {
                location = this.TryGetLocation(errors);
                if (location == null && errors.Count == 0)
                {
                    errors.Add("Unable to create new location instance.");
                }
            }

            if (this.IsValid(errors))
            {
                bool      pingOk           = true;
                const int WaitMilliseconds = 200;
                using (Pinger pinger = new Pinger(TimeSpan.FromMilliseconds(WaitMilliseconds)))
                {
                    if (!(pinger.TryPing(location.Address) ?? false))
                    {
                        // Occasionally, a ping will be lost, and it's ok. The user might also want to configure
                        // a site to watch that is known to be intermittently available.
                        pingOk = WindowsUtility.ShowQuestion(this, "The specified address did not respond to a ping. Continue?", null, false);
                    }
                }

                if (pingOk)
                {
                    this.DialogResult = true;
                }
            }
        }
示例#2
0
文件: Pinger.cs 项目: menees/WirePeep
        public static IPAddress GetAddressAtTtl(IPAddress searchAddress, int ttl, TimeSpan wait)
        {
            IPAddress result = null;

            using (Pinger pinger = new Pinger(wait, ttl))
            {
                PingReply reply = pinger.TrySend(searchAddress);
                if (reply?.Status == IPStatus.Success || reply?.Status == IPStatus.TtlExpired)
                {
                    result = reply?.Address;
                }
            }

            return(result);
        }
示例#3
0
        public bool?Update(DateTime utcNow, long counter, ConnectionState?simulateConnection)
        {
            bool?result = null;

            PeerGroup peerGroup = this.Location.PeerGroup;

            if (peerGroup.CanPoll(utcNow, this.lastPolled))
            {
                ConnectionState priorConnection = this.Connection;

                using (Pinger pinger = new Pinger(peerGroup.Wait))
                {
                    TimeSpan roundtripTime = TimeSpan.Zero;
                    if (simulateConnection != null)
                    {
                        this.Connection = simulateConnection.Value;
                    }
                    else
                    {
                        bool?ping = pinger.TryPing(this.Location.Address, out roundtripTime);
                        switch (ping)
                        {
                        case true:
                            this.Connection = ConnectionState.Connected;
                            break;

                        case false:
                            this.Connection = ConnectionState.Disconnected;
                            break;

                        default:
                            this.Connection = ConnectionState.Unavailable;
                            break;
                        }
                    }

                    this.RoundtripTime = roundtripTime;
                    this.UpdateCounter = counter;
                }

                result          = this.Connection != priorConnection;
                this.lastPolled = utcNow;
            }

            return(result);
        }
示例#4
0
        public bool?Update(DateTime utcNow, long counter, bool simulateFailure)
        {
            bool?result = null;

            PeerGroup peerGroup = this.Location.PeerGroup;

            if (peerGroup.CanPoll(utcNow, this.lastPolled))
            {
                bool?wasConnected = this.IsConnected;

                using (Pinger pinger = new Pinger(peerGroup.Wait))
                {
                    TimeSpan roundtripTime = TimeSpan.Zero;
                    this.IsConnected   = !simulateFailure && pinger.TryPing(this.Location.Address, out roundtripTime);
                    this.RoundtripTime = roundtripTime;
                    this.UpdateCounter = counter;
                }

                result          = this.IsConnected != wasConnected;
                this.lastPolled = utcNow;
            }

            return(result);
        }
示例#5
0
        private void LoadDefaults()
        {
            void AddLocation(Location location)
            {
                // Don't add a duplicate address (e.g., both router and modem if they're the same).
                if (!this.Locations.Any(l => l.Address.Equals(location.Address)))
                {
                    if (!this.PeerGroups.Contains(location.PeerGroup))
                    {
                        this.PeerGroups.Add(location.PeerGroup);
                    }

                    this.Locations.Add(location);
                }
            }

            XElement root = XElement.Parse(Properties.Resources.DefaultProfileXml);

            foreach (XElement groupElement in root.Elements(nameof(PeerGroup)))
            {
                string groupName = groupElement.GetAttributeValue("Name");
#pragma warning disable MEN010 // Avoid magic numbers. Default times are clear in context.
                int failSeconds      = groupElement.GetAttributeValue("FailSeconds", 10);
                int pollSeconds      = groupElement.GetAttributeValue("PollSeconds", 5);
                int waitMilliseconds = groupElement.GetAttributeValue("WaitMilliseconds", 200);
#pragma warning restore MEN010 // Avoid magic numbers

                PeerGroup group = new PeerGroup(
                    groupName,
                    TimeSpan.FromSeconds(failSeconds),
                    TimeSpan.FromSeconds(pollSeconds),
                    TimeSpan.FromMilliseconds(waitMilliseconds));

                foreach (XElement locationElement in groupElement.Elements(nameof(Location)))
                {
                    string locationName = locationElement.GetAttributeValue("Name");
                    int    ordinal      = 1;
                    foreach (XElement addressElement in locationElement.Elements("Address"))
                    {
                        if (IPAddress.TryParse(addressElement.Value, out IPAddress address))
                        {
                            string   nameSuffix = ordinal == 1 ? string.Empty : $" #{ordinal}";
                            Location location   = new Location(group, locationName + nameSuffix, address);
                            AddLocation(location);
                            ordinal++;
                        }
                    }
                }

                foreach (XElement findElement in groupElement.Elements("Find"))
                {
                    string    findName = findElement.GetAttributeValue("Name");
                    string    findType = findElement.GetAttributeValue("Type");
                    IPAddress address;
                    switch (findType)
                    {
                    case "DefaultGateway":
                        // The first IP address returned by a traceroute should be the gateway. https://stackoverflow.com/a/29494180/1882616
                        // We'll simulate that by doing a ping with TTL = 1. https://stackoverflow.com/a/45565253/1882616
                        address = Pinger.GetAddressAtTtl(IPAddress.Parse("8.8.8.8"), 1, group.Wait);
                        break;

                    case "CableModem":
                        using (Pinger pinger = new Pinger(group.Wait))
                        {
                            address = findElement.Elements("Address").Select(e => IPAddress.Parse(e.Value)).FirstOrDefault(a => pinger.TryPing(a) ?? false);
                        }

                        break;

                    default:
                        throw Exceptions.NewInvalidOperationException($"Unsupported Find Type: {findType}");
                    }

                    if (address != null)
                    {
                        Location location = new Location(group, findName, address);
                        AddLocation(location);
                    }
                }
            }
        }