Esempio n. 1
0
 static string getPingOptions(PingOptions _pingOptions)
 {
     StringBuilder s = new StringBuilder();
     s.Append(string.Format("DF={0}, ", _pingOptions.DontFragment));
     s.Append(string.Format("TTL={0}", _pingOptions.Ttl));
     return s.ToString();
 }
Esempio n. 2
0
		/// <summary>
		/// Attempts to send an Internet Control Message Protocol (<c>ICMP</c>) echo message.
		/// </summary>
		/// <param name="hostNameOrAddress"></param>
		/// <param name="buffer"></param>
		/// <param name="timeout"></param>
		/// <param name="options"></param>
		/// <returns></returns>
		public PingReply Send(string hostNameOrAddress, byte[] buffer, int timeout, PingOptions options)
		{
			if ("".CompareTo(hostNameOrAddress) >= 0)
			{
				throw new ArgumentNullException("hostNameOrAddress");
			}

			IPAddress address;
			try
			{
				address = IPAddress.Parse(hostNameOrAddress);
			}
			catch
			{
				try
				{
					//IPHostEntry entry = Dns.Resolve(hostNameOrAddress);
                    IPHostEntry entry = Dns.GetHostEntry(hostNameOrAddress);
					address = entry.AddressList[0];
				}
				catch(Exception e)
				{
					throw new PingException("Impossible to resolve host or address.", e);
				}
			}

			return Send(address, buffer, timeout, options);
		}
Esempio n. 3
0
        public void doPing(string ipAddress, PingOptions pingOptions)
        {
            string _ipAddress = ipAddress;
            PingOptions _pingOptions = pingOptions;

            ThreadStart myThread = delegate { startPing(_ipAddress, _pingOptions); };
            thread = new Thread(myThread);
            thread.Name="ping thread";
            thread.Start();
        }
Esempio n. 4
0
 public void doPing(string ipAddress)
 {
     _ipAddress = ipAddress;
     _pingOptions = new PingOptions();
     _ipAddress = ipAddress;
     //Thread thread = new Thread(new ThreadStart(startPing));
     ThreadStart myThread = delegate { startPing(ipAddress, new PingOptions()); };
     thread = new Thread(myThread);
     thread.Start();
 }
Esempio n. 5
0
		internal PingReply(IcmpEchoReply reply)
		{
			address = new IPAddress((long) reply.address);
			ipStatus = (IPStatus) reply.status;
			options = new PingOptions(reply);
			if (this.ipStatus == IPStatus.Success)
			{
				rtt = reply.roundTripTime;
				buffer = new byte[reply.dataSize];
				Marshal.Copy(reply.data, this.buffer, 0, reply.dataSize);
			}
			else
			{
				buffer = new byte[0];
			}
		}
Esempio n. 6
0
		internal IPOptions(PingOptions options)
		{
			ttl = 0x80;
			tos = 0;
			optionsSize = 0;
			flags = 0;
			optionsData = IntPtr.Zero;

			if (options != null)
			{
				this.ttl = (byte) options.Ttl;
				if (options.DontFragment)
				{
					this.flags = DontFragmentFlag;
				}
			}
		}
Esempio n. 7
0
 public frmOptions(ref PingOptions po)
 {
     InitializeComponent();
     _PO = po;
     read();
 }
Esempio n. 8
0
        //thread to do ping
        public void startPing(string sHost, PingOptions pOptions)
        {
            System.Diagnostics.Debug.WriteLine("+++thread started");
            string ipAddress = sHost;//make a local copy
            Ping myPing = new Ping();
            PingOptions myPingOptions = pOptions;//make a local copy
            
            int iTimeout = pOptions.TimeOut;
            int _numberOfPings = pOptions.numOfPings;
            bool doNotFragment = pOptions.DontFragment;
            int bufferSize = pOptions.bufferSize;
            byte[] buf = new byte[bufferSize];

            //long sumRoundtripTime=0;
            onReply(new PingReplyEventArgs("ping started...",PingReplyTypes.info));
            replyStats _replyStats = new replyStats(ipAddress);
            PingReply reply = null;
            try
            {
                onReply(new PingReplyEventArgs(pOptions.ToString(), PingReplyTypes.info));
                //check DNS first as this may block for long time
                IPAddress address;
                HostParms hostParms = new HostParms(ipAddress, 4);
                try
                {
                    //is IP address
                    address = IPAddress.Parse(ipAddress);
                    hostParms.isValid = true;
                }
                catch
                {
                    if (checkHost(ref hostParms))
                        ipAddress = hostParms.ipAddress.ToString();
                }
                if (!hostParms.isValid)
                    throw new PingUnknownHostException("Unkown host: " + ipAddress);

                for (int ix = 0; ix < _numberOfPings; ix++)
                {
                    reply = myPing.Send(ipAddress, buf, iTimeout, myPingOptions);
                    string sReply = "";
                    if (reply.Status == IPStatus.Success)
                    {
                        //sumRoundtripTime += reply.RoundTripTime;
                        _replyStats.add(1, 1, reply.RoundTripTime);
                        sReply = myResources.getReply(reply, ipAddress);
                    }
                    else if (reply.Status == IPStatus.DestinationHostUnreachable)
                    {
                        _replyStats.add(1, 0, reply.RoundTripTime);
                        throw new PingUnknownHostException("Destination unreachable");
                    }
                    else
                    {                        
                        _replyStats.add(1, 0, reply.RoundTripTime);
                        sReply = myResources.getReply(reply, ipAddress);
                    }
                    System.Diagnostics.Debug.WriteLine(sReply);
                    onReply(new PingReplyEventArgs(sReply));
                }
                onReply(new PingReplyEventArgs(_replyStats.ToString(), PingReplyTypes.info));
            }
            catch (PingUnknownHostException ex)
            {
                System.Diagnostics.Debug.WriteLine("PingUnknownHostException: " + ex.Message);
                onReply(new PingReplyEventArgs("Unknown host: "+ ipAddress, PingReplyTypes.info));
            }
            catch (PingException ex)
            {
                System.Diagnostics.Debug.WriteLine("PingException: " + ex.Message);
            }
            catch (ThreadAbortException ex)
            {
                onReply(new PingReplyEventArgs("ping failed", PingReplyTypes.info));
                Thread.CurrentThread.Abort();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("PingException: " + ex.Message);
            }
            finally
            {
                onReply(new PingReplyEventArgs("done", PingReplyTypes.done));
                System.Diagnostics.Debug.WriteLine("---thread ended");
            }
        }
Esempio n. 9
0
        private PingReply InternalSend(IPAddress address, byte [] buffer, int timeout, PingOptions options)
        {
            if (_handlePingV4 == IntPtr.Zero)
            {
                _handlePingV4 = IcmpCreateFile();
            }
            if (_replyBuffer == IntPtr.Zero)
            {
                _replyBuffer = MarshalEx.AllocHLocal(0xffff);
            }

            IPOptions ipo = new IPOptions(options);

            InitStructure(buffer);
            IcmpSendEcho2(_handlePingV4, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)address.Address, _requestBuffer, (ushort)buffer.Length, ref ipo, _replyBuffer, 0xffff, (uint)timeout);
            FreeStructure();

            IcmpEchoReply reply = (IcmpEchoReply)Marshal.PtrToStructure(this._replyBuffer, typeof(IcmpEchoReply));

            return(new PingReply(reply));
        }
Esempio n. 10
0
		/// <summary>
		/// Attempts to send an Internet Control Message Protocol (<c>ICMP</c>) echo message.
		/// </summary>
		/// <param name="address"></param>
		/// <param name="buffer"></param>
		/// <param name="timeout"></param>
		/// <param name="options"></param>
		/// <returns></returns>
		public PingReply Send(IPAddress address, byte[] buffer, int timeout, PingOptions options)
		{
			PingReply reply;
			if (buffer == null)
			{
				throw new ArgumentNullException("buffer");
			}
			if (timeout < 0)
			{
				throw new ArgumentOutOfRangeException("timeout");
			}
			if (address == null)
			{
				throw new ArgumentNullException("address");
			}
			if (this._disposed)
			{
				throw new ObjectDisposedException(base.GetType().FullName);
			}

			try
			{
				reply = InternalSend(address, buffer, timeout, options);
			}
			catch(Exception e)
			{
				throw new PingException("Impossible to send a packet.", e);
			}

			return reply;
		}
Esempio n. 11
0
		private PingReply InternalSend(IPAddress address, byte [] buffer, int timeout, PingOptions options)
		{
			if (_handlePingV4 == IntPtr.Zero) _handlePingV4 = IcmpCreateFile();
			if (_replyBuffer == IntPtr.Zero)
			{
				_replyBuffer = MarshalEx.AllocHLocal(0xffff);
			}

			IPOptions ipo = new IPOptions(options);
			InitStructure(buffer);
			IcmpSendEcho2(_handlePingV4, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)address.Address, _requestBuffer, (ushort)buffer.Length, ref ipo, _replyBuffer, 0xffff, (uint)timeout);
			FreeStructure();

			IcmpEchoReply reply = (IcmpEchoReply)Marshal.PtrToStructure(this._replyBuffer, typeof(IcmpEchoReply));
			return new PingReply(reply);
		}