/// <summary> /// /// </summary> /// <param name="reply"></param> internal PingReply(ICMP_ECHO_REPLY reply) { Address = new IPAddress(reply.Address); Status = (IPStatus)reply.Status; Options = new PingOptions(reply); if (Status == IPStatus.Success) { RoundTripTime = reply.RoundTripTime; Buffer = new byte[reply.DataSize]; Marshal.Copy(reply.Data, Buffer, 0, reply.DataSize); } else { Buffer = new byte[0]; } }
internal IP_OPTION_INFORMATION(PingOptions options) { Ttl = 0x80; Tos = 0; Flags = 0; OptionsSize = 0; OptionsData = IntPtr.Zero; if (options != null) { Ttl = (byte)options.Ttl; if (options.DontFragment) { Flags = 2; } } }
private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options) { if (handle == IntPtr.Zero) { handle = NativeMethods.IcmpCreateFile(); if (handle.ToInt32() == -1) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } ICMP_ECHO_REPLY reply; var replyBuffer = new byte[65536]; var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); var replyHandle = GCHandle.Alloc(replyBuffer, GCHandleType.Pinned); try { var ipoption = new IP_OPTION_INFORMATION(options); if (NativeMethods.IcmpSendEcho2(handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, BitConverter.ToUInt32(address.GetAddressBytes(), 0), bufferHandle.AddrOfPinnedObject(), (ushort)buffer.Length, ref ipoption, replyHandle.AddrOfPinnedObject(), (uint)replyBuffer.Length, (uint)timeout) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { reply = (ICMP_ECHO_REPLY)Marshal.PtrToStructure(replyHandle.AddrOfPinnedObject(), typeof(ICMP_ECHO_REPLY)); replyHandle.Free(); bufferHandle.Free(); } return new PingReply(reply); }
/// <summary> /// /// </summary> /// <param name="address"></param> /// <param name="timeout"></param> /// <param name="buffer"></param> /// <param name="options"></param> /// <returns></returns> public PingReply Send(IPAddress address, int timeout, byte[] buffer, PingOptions options) { if (buffer == null) { buffer = SendBuffer; } if (buffer.Length <= 0) { throw new ArgumentOutOfRangeException("buffer"); } if (timeout < 0) { throw new ArgumentOutOfRangeException("timeout"); } if (address == null) { throw new ArgumentNullException("address"); } if (disposed) { throw new ObjectDisposedException(GetType().FullName); } PingReply reply; try { reply = InternalSend(address, buffer, timeout, options); } catch (PingException) { throw; } catch (Exception ex) { throw new PingException("Impossible to send packet.", ex); } return reply; }
/// <summary> /// /// </summary> /// <param name="hostNameOrAddress"></param> /// <param name="timeout"></param> /// <param name="buffer"></param> /// <param name="options"></param> /// <returns></returns> public PingReply Send(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options) { if (String.IsNullOrEmpty(hostNameOrAddress)) { throw new ArgumentNullException("hostNameOrAddress"); } IPAddress address; try { address = IPAddress.Parse(hostNameOrAddress); } catch (FormatException) { try { address = Dns.GetHostEntry(hostNameOrAddress).AddressList[0]; } catch (Exception ex) { throw new PingException("Impossible to resolve hostname.", ex); } } return Send(address, timeout, buffer, options); }
private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options) { if (handle == IntPtr.Zero) { handle = NativeMethods.IcmpCreateFile(); if (handle.ToInt32() == -1) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } ICMP_ECHO_REPLY reply; var replyBuffer = new byte[65536]; var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); var replyHandle = GCHandle.Alloc(replyBuffer, GCHandleType.Pinned); try { var ipoption = new IP_OPTION_INFORMATION(options); if (NativeMethods.IcmpSendEcho2(handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, BitConverter.ToUInt32(address.GetAddressBytes(), 0), bufferHandle.AddrOfPinnedObject(), (ushort)buffer.Length, ref ipoption, replyHandle.AddrOfPinnedObject(), (uint)replyBuffer.Length, (uint)timeout) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { reply = (ICMP_ECHO_REPLY)Marshal.PtrToStructure(replyHandle.AddrOfPinnedObject(), typeof(ICMP_ECHO_REPLY)); replyHandle.Free(); bufferHandle.Free(); } return(new PingReply(reply)); }