Ejemplo n.º 1
0
        //private callback invoked when icmpsendecho apis succeed
        private static void PingCallback(object state, bool signaled)
        {
            Ping ping = (Ping)state;
            PingCompletedEventArgs eventArgs           = null;
            bool               cancelled               = false;
            AsyncOperation     asyncOp                 = null;
            SendOrPostCallback onPingCompletedDelegate = null;

            try
            {
                lock (ping.lockObject) {
                    cancelled = ping.cancelled;
                    asyncOp   = ping.asyncOp;
                    onPingCompletedDelegate = ping.onPingCompletedDelegate;

                    if (!cancelled)
                    {
                        //parse reply buffer
                        SafeLocalFree buffer = ping.replyBuffer;

                        //marshals and constructs new reply
                        PingReply reply;

                        if (ping.ipv6)
                        {
                            Icmp6EchoReply icmp6Reply = (Icmp6EchoReply)Marshal.PtrToStructure(buffer.DangerousGetHandle(), typeof(Icmp6EchoReply));
                            reply = new PingReply(icmp6Reply, buffer.DangerousGetHandle(), ping.sendSize);
                        }
                        else
                        {
                            IcmpEchoReply icmpReply = (IcmpEchoReply)Marshal.PtrToStructure(buffer.DangerousGetHandle(), typeof(IcmpEchoReply));
                            reply = new PingReply(icmpReply);
                        }

                        eventArgs = new PingCompletedEventArgs(reply, null, false, asyncOp.UserSuppliedState);
                    }
                    else     //cancelled
                    {
                        eventArgs = new PingCompletedEventArgs(null, null, true, asyncOp.UserSuppliedState);
                    }
                }
            }
            // in case of failure, create a failed event arg
            catch (Exception e) {
                PingException pe = new PingException(SR.GetString(SR.net_ping), e);
                eventArgs = new PingCompletedEventArgs(null, pe, false, asyncOp.UserSuppliedState);
            }
            finally {
                ping.FreeUnmanagedStructures();
                ping.UnregisterWaitHandle();
                ping.Finish(true);
            }

            asyncOp.PostOperationCompleted(onPingCompletedDelegate, eventArgs);
        }
Ejemplo n.º 2
0
        private static void PingCallback(object state, bool signaled)
        {
            Ping ping = (Ping)state;
            PingCompletedEventArgs arg   = null;
            bool               cancelled = false;
            AsyncOperation     asyncOp   = null;
            SendOrPostCallback d         = null;

            try
            {
                lock (ping.lockObject)
                {
                    cancelled = ping.cancelled;
                    asyncOp   = ping.asyncOp;
                    d         = ping.onPingCompletedDelegate;
                    if (!cancelled)
                    {
                        PingReply     reply;
                        SafeLocalFree replyBuffer = ping.replyBuffer;
                        if (!ping.ipv6 && !ComNetOS.IsVista)
                        {
                            UnsafeNetInfoNativeMethods.IcmpParseReplies(replyBuffer.DangerousGetHandle(), 0x100ff);
                        }
                        if (ping.ipv6)
                        {
                            Icmp6EchoReply reply2 = (Icmp6EchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(Icmp6EchoReply));
                            reply = new PingReply(reply2, replyBuffer.DangerousGetHandle(), ping.sendSize);
                        }
                        else
                        {
                            IcmpEchoReply reply3 = (IcmpEchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(IcmpEchoReply));
                            reply = new PingReply(reply3);
                        }
                        arg = new PingCompletedEventArgs(reply, null, false, asyncOp.UserSuppliedState);
                    }
                    else
                    {
                        arg = new PingCompletedEventArgs(null, null, true, asyncOp.UserSuppliedState);
                    }
                }
            }
            catch (Exception exception)
            {
                PingException error = new PingException(SR.GetString("net_ping"), exception);
                arg = new PingCompletedEventArgs(null, error, false, asyncOp.UserSuppliedState);
            }
            finally
            {
                ping.FreeUnmanagedStructures();
                ping.UnregisterWaitHandle();
                ping.Finish(true);
            }
            asyncOp.PostOperationCompleted(d, arg);
        }
Ejemplo n.º 3
0
 internal PingReply(Icmp6EchoReply reply, IntPtr dataPtr, int sendSize)
 {
     this.address  = new IPAddress(reply.Address.Address, (long)reply.Address.ScopeID);
     this.ipStatus = (IPStatus)reply.Status;
     if (this.ipStatus == IPStatus.Success)
     {
         this.rtt    = reply.RoundTripTime;
         this.buffer = new byte[sendSize];
         Marshal.Copy(IntPtrHelper.Add(dataPtr, 0x24), this.buffer, 0, sendSize);
     }
     else
     {
         this.buffer = new byte[0];
     }
 }
 internal PingReply(Icmp6EchoReply reply, IntPtr dataPtr, int sendSize)
 {
     this.address = new IPAddress(reply.Address.Address, (long) reply.Address.ScopeID);
     this.ipStatus = (IPStatus) reply.Status;
     if (this.ipStatus == IPStatus.Success)
     {
         this.rtt = reply.RoundTripTime;
         this.buffer = new byte[sendSize];
         Marshal.Copy(IntPtrHelper.Add(dataPtr, 0x24), this.buffer, 0, sendSize);
     }
     else
     {
         this.buffer = new byte[0];
     }
 }
Ejemplo n.º 5
0
                // the main constructor for the icmpsendecho apis
        internal PingReply (Icmp6EchoReply reply, IntPtr dataPtr, int sendSize) {
            
            address = new IPAddress(reply.Address.Address,reply.Address.ScopeID);
            ipStatus = (IPStatus)reply.Status; //the icmpsendecho ip status codes

            //only copy the data if we succeed w/ the ping operation
            if (ipStatus == IPStatus.Success) {
                rtt = (long)reply.RoundTripTime;
                buffer = new byte[sendSize];
                Marshal.Copy (IntPtrHelper.Add(dataPtr, 36), buffer, 0, sendSize);
                //options = new PingOptions (reply.options);
            }
            else
                buffer = new byte[0];

        }
Ejemplo n.º 6
0
        // the main constructor for the icmpsendecho apis
        internal PingReply(Icmp6EchoReply reply, IntPtr dataPtr, int sendSize)
        {
            address  = new IPAddress(reply.Address.Address, reply.Address.ScopeID);
            ipStatus = (IPStatus)reply.Status; //the icmpsendecho ip status codes

            //only copy the data if we succeed w/ the ping operation
            if (ipStatus == IPStatus.Success)
            {
                rtt    = (long)reply.RoundTripTime;
                buffer = new byte[sendSize];
                Marshal.Copy(IntPtrHelper.Add(dataPtr, 36), buffer, 0, sendSize);
                //options = new PingOptions (reply.options);
            }
            else
            {
                buffer = new byte[0];
            }
        }
Ejemplo n.º 7
0
        private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options, bool async)
        {
            int       num;
            PingReply reply;

            this.ipv6     = address.AddressFamily == AddressFamily.InterNetworkV6;
            this.sendSize = buffer.Length;
            if (!this.ipv6 && (this.handlePingV4 == null))
            {
                this.handlePingV4 = UnsafeNetInfoNativeMethods.IcmpCreateFile();
                if (this.handlePingV4.IsInvalid)
                {
                    this.handlePingV4 = null;
                    throw new Win32Exception();
                }
            }
            else if (this.ipv6 && (this.handlePingV6 == null))
            {
                this.handlePingV6 = UnsafeNetInfoNativeMethods.Icmp6CreateFile();
                if (this.handlePingV6.IsInvalid)
                {
                    this.handlePingV6 = null;
                    throw new Win32Exception();
                }
            }
            IPOptions options2 = new IPOptions(options);

            if (this.replyBuffer == null)
            {
                this.replyBuffer = SafeLocalFree.LocalAlloc(0x100ff);
            }
            try
            {
                if (async)
                {
                    if (this.pingEvent == null)
                    {
                        this.pingEvent = new ManualResetEvent(false);
                    }
                    else
                    {
                        this.pingEvent.Reset();
                    }
                    this.registeredWait = ThreadPool.RegisterWaitForSingleObject(this.pingEvent, new WaitOrTimerCallback(Ping.PingCallback), this, -1, true);
                }
                this.SetUnmanagedStructures(buffer);
                if (!this.ipv6)
                {
                    if (async)
                    {
                        num = (int)UnsafeNetInfoNativeMethods.IcmpSendEcho2(this.handlePingV4, this.pingEvent.SafeWaitHandle, IntPtr.Zero, IntPtr.Zero, (uint)address.m_Address, this.requestBuffer, (ushort)buffer.Length, ref options2, this.replyBuffer, 0x100ff, (uint)timeout);
                    }
                    else
                    {
                        num = (int)UnsafeNetInfoNativeMethods.IcmpSendEcho2(this.handlePingV4, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)address.m_Address, this.requestBuffer, (ushort)buffer.Length, ref options2, this.replyBuffer, 0x100ff, (uint)timeout);
                    }
                }
                else
                {
                    SocketAddress address2            = new IPEndPoint(address, 0).Serialize();
                    byte[]        sourceSocketAddress = new byte[0x1c];
                    if (async)
                    {
                        num = (int)UnsafeNetInfoNativeMethods.Icmp6SendEcho2(this.handlePingV6, this.pingEvent.SafeWaitHandle, IntPtr.Zero, IntPtr.Zero, sourceSocketAddress, address2.m_Buffer, this.requestBuffer, (ushort)buffer.Length, ref options2, this.replyBuffer, 0x100ff, (uint)timeout);
                    }
                    else
                    {
                        num = (int)UnsafeNetInfoNativeMethods.Icmp6SendEcho2(this.handlePingV6, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, sourceSocketAddress, address2.m_Buffer, this.requestBuffer, (ushort)buffer.Length, ref options2, this.replyBuffer, 0x100ff, (uint)timeout);
                    }
                }
            }
            catch
            {
                this.UnregisterWaitHandle();
                throw;
            }
            if (num == 0)
            {
                num = Marshal.GetLastWin32Error();
                if (async && (num == 0x3e5L))
                {
                    return(null);
                }
                this.FreeUnmanagedStructures();
                this.UnregisterWaitHandle();
                if ((async || (num < 0x2afa)) || (num > 0x2b25))
                {
                    throw new Win32Exception(num);
                }
                return(new PingReply((IPStatus)num));
            }
            if (async)
            {
                return(null);
            }
            this.FreeUnmanagedStructures();
            if (this.ipv6)
            {
                Icmp6EchoReply reply2 = (Icmp6EchoReply)Marshal.PtrToStructure(this.replyBuffer.DangerousGetHandle(), typeof(Icmp6EchoReply));
                reply = new PingReply(reply2, this.replyBuffer.DangerousGetHandle(), this.sendSize);
            }
            else
            {
                IcmpEchoReply reply3 = (IcmpEchoReply)Marshal.PtrToStructure(this.replyBuffer.DangerousGetHandle(), typeof(IcmpEchoReply));
                reply = new PingReply(reply3);
            }
            GC.KeepAlive(this.replyBuffer);
            return(reply);
        }
Ejemplo n.º 8
0
        // internal method responsible for sending echo request on win2k and higher

        private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options, bool async)
        {
            ipv6     = (address.AddressFamily == AddressFamily.InterNetworkV6)?true:false;
            sendSize = buffer.Length;

            //get and cache correct handle
            if (!ipv6 && handlePingV4 == null)
            {
                handlePingV4 = UnsafeNetInfoNativeMethods.IcmpCreateFile();
                if (handlePingV4.IsInvalid)
                {
                    handlePingV4 = null;
                    throw new Win32Exception(); // Gets last error
                }
            }
            else if (ipv6 && handlePingV6 == null)
            {
                handlePingV6 = UnsafeNetInfoNativeMethods.Icmp6CreateFile();
                if (handlePingV6.IsInvalid)
                {
                    handlePingV6 = null;
                    throw new Win32Exception(); // Gets last error
                }
            }


            //setup the options
            IPOptions ipOptions = new IPOptions(options);

            //setup the reply buffer
            if (replyBuffer == null)
            {
                replyBuffer = SafeLocalFree.LocalAlloc(MaxUdpPacket);
            }

            //queue the event
            int error;

            try
            {
                if (async)
                {
                    if (pingEvent == null)
                    {
                        pingEvent = new ManualResetEvent(false);
                    }
                    else
                    {
                        pingEvent.Reset();
                    }

                    registeredWait = ThreadPool.RegisterWaitForSingleObject(pingEvent, new WaitOrTimerCallback(PingCallback), this, -1, true);
                }

                //Copy user dfata into the native world
                SetUnmanagedStructures(buffer);

                if (!ipv6)
                {
                    if (async)
                    {
                        error = (int)UnsafeNetInfoNativeMethods.IcmpSendEcho2(handlePingV4, pingEvent.SafeWaitHandle, IntPtr.Zero, IntPtr.Zero, (uint)address.m_Address, requestBuffer, (ushort)buffer.Length, ref ipOptions, replyBuffer, MaxUdpPacket, (uint)timeout);
                    }
                    else
                    {
                        error = (int)UnsafeNetInfoNativeMethods.IcmpSendEcho2(handlePingV4, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)address.m_Address, requestBuffer, (ushort)buffer.Length, ref ipOptions, replyBuffer, MaxUdpPacket, (uint)timeout);
                    }
                }
                else
                {
                    IPEndPoint    ep         = new IPEndPoint(address, 0);
                    SocketAddress remoteAddr = ep.Serialize();
                    byte[]        sourceAddr = new byte[28];
                    if (async)
                    {
                        error = (int)UnsafeNetInfoNativeMethods.Icmp6SendEcho2(handlePingV6, pingEvent.SafeWaitHandle, IntPtr.Zero, IntPtr.Zero, sourceAddr, remoteAddr.m_Buffer, requestBuffer, (ushort)buffer.Length, ref ipOptions, replyBuffer, MaxUdpPacket, (uint)timeout);
                    }
                    else
                    {
                        error = (int)UnsafeNetInfoNativeMethods.Icmp6SendEcho2(handlePingV6, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, sourceAddr, remoteAddr.m_Buffer, requestBuffer, (ushort)buffer.Length, ref ipOptions, replyBuffer, MaxUdpPacket, (uint)timeout);
                    }
                }
            }
            catch
            {
                UnregisterWaitHandle();
                throw;
            }

            //need this if something is bogus.
            if (error == 0)
            {
                error = (int)Marshal.GetLastWin32Error();

                // Only skip Async IO Pending error value
                if (async && error == UnsafeNclNativeMethods.ErrorCodes.ERROR_IO_PENDING)
                {
                    return(null); // Expected async return value
                }
                // Cleanup
                FreeUnmanagedStructures();
                UnregisterWaitHandle();

                if (async || // No IPStatus async errors
                    error < (int)IPStatus.DestinationNetworkUnreachable || // Min
                    error > (int)IPStatus.DestinationScopeMismatch)    // Max // Out of IPStatus range
                {
                    throw new Win32Exception(error);
                }

                return(new PingReply((IPStatus)error)); // Synchronous IPStatus errors
            }

            if (async)
            {
                return(null);
            }

            FreeUnmanagedStructures();

            //return the reply
            PingReply reply;

            if (ipv6)
            {
                Icmp6EchoReply icmp6Reply = (Icmp6EchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(Icmp6EchoReply));
                reply = new PingReply(icmp6Reply, replyBuffer.DangerousGetHandle(), sendSize);
            }
            else
            {
                IcmpEchoReply icmpReply = (IcmpEchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(IcmpEchoReply));
                reply = new PingReply(icmpReply);
            }

            // IcmpEchoReply still has an unsafe IntPtr reference into replybuffer
            // and replybuffer was being freed prematurely by the GC, causing AccessViolationExceptions.
            GC.KeepAlive(replyBuffer);

            return(reply);
        }