private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options) { if (_handlePingV4 == IntPtr.Zero) { _handlePingV4 = IcmpCreateFile(); if (_handlePingV4.ToInt32() == -1) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } uint replySize = 0xffff; byte[] replybuffer = new byte[replySize]; if (buffer.Length <= 0) { throw new ArgumentException("Buffer must be non=zero length"); } GCHandle pReplyBuffer = GCHandle.Alloc(replybuffer, GCHandleType.Pinned); GCHandle pSendBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned); IcmpEchoReply reply; try { IPOptions ipo = new IPOptions(options); uint result = IcmpSendEcho2(_handlePingV4, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, BitConverter.ToUInt32(address.GetAddressBytes(), 0), pSendBuffer.AddrOfPinnedObject(), (ushort)buffer.Length, ref ipo, pReplyBuffer.AddrOfPinnedObject(), replySize, (uint)timeout); if (result == 0) { int error = Marshal.GetLastWin32Error(); if (error == 0x57) { throw new PingException("Destination address is unreachable on this network."); } throw new Win32Exception(error); } } finally { reply = (IcmpEchoReply)Marshal.PtrToStructure(pReplyBuffer.AddrOfPinnedObject(), typeof(IcmpEchoReply)); pReplyBuffer.Free(); pSendBuffer.Free(); } return(new PingReply(reply)); }
internal static extern uint IcmpSendEcho2(IntPtr icmpHandle, IntPtr Event, IntPtr apcRoutine, IntPtr apcContext, uint ipAddress, IntPtr data, ushort dataSize, ref IPOptions options, IntPtr replyBuffer, uint replySize, uint timeout);
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)); }
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); }