using System; using System.Drawing; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Collections; using System.Diagnostics; using System.Threading; namespace Netstat { interface IDWPing { short ping(string strHostName); int Timeout{set;} int Repeats{set;} int AvgTime{get;} int AvgTTL{get;} string ErrorMsg{get;} } struct ICMPHeader { public byte type; public byte code; public ushort chksum; public ushort id; public ushort seq; public ulong timestamp; public byte[] toByteArray() { byte[] arResult = new byte[22]; arResult[0] = this.type; arResult[1] = this.code; arResult[2] = (byte)chksum; arResult[3] = (byte)(chksum >> 8); arResult[4] = (byte)(chksum >> 16); arResult[5] = (byte)(chksum >> 24); arResult[6] = (byte)id; arResult[7] = (byte)(id >> 8); arResult[8] = (byte)(id >> 16); arResult[9] = (byte)(id >> 24); arResult[10] = (byte)seq; arResult[11] = (byte)(seq >> 8); arResult[12] = (byte)(seq >> 16); arResult[13] = (byte)(seq >> 24); arResult[14] = (byte)timestamp; arResult[15] = (byte)(timestamp >> 8); arResult[16] = (byte)(timestamp >> 16); arResult[17] = (byte)(timestamp >> 24); arResult[18] = (byte)(timestamp >> 32); arResult[19] = (byte)(timestamp >> 40); arResult[20] = (byte)(timestamp >> 48); arResult[21] = (byte)(timestamp >> 56); return arResult; } } public class CDWPing : IDWPing { private int m_Timeout; private int[] m_arTime; private bool[] m_arResults; private byte[] m_arTTL; private int m_idxPing; private string m_strErrorMsg; public int Timeout{set{m_Timeout = Math.Max(value,1);}} public int Repeats {set{ int n = Math.Max(value, 1); m_arTime = new int[n]; m_arTTL = new byte[n]; m_arResults = new bool[n]; }} public int AvgTime { get{ return this.calcAvgTime(); }} public int AvgTTL { get{ return this.calcAvgTTL(); }} public string ErrorMsg { get{ return m_strErrorMsg; }} public CDWPing() { m_Timeout = 200; m_arTime = new int[1]; m_arResults = new bool[1]; m_arTTL = new Byte[1]; m_strErrorMsg = "Blah"; } public short ping(string strHostName) { m_strErrorMsg = "Doll"; this.clearStats(); short result = 0; try { IPEndPoint lep; const int echoPort = 7; if(this.isIPAddress(strHostName)) { IPAddress ipAddr = IPAddress.Parse(strHostName); lep = new IPEndPoint(ipAddr, echoPort); } else { IPHostEntry lipa = Dns.Resolve(strHostName); lep = new IPEndPoint(lipa.AddressList[0], echoPort); } for(m_idxPing = 0; m_idxPing < m_arTime.Length; m_idxPing++) { if(tryPing(lep)) { m_arResults[m_idxPing] = true; result = 1; } else { m_arResults[m_idxPing] = false; } } } catch(SocketException ex) { result = 0; m_strErrorMsg = ex.Message; } catch(Exception ex) { result = 0; m_strErrorMsg = ex.Message; } return result; } private bool tryPing(IPEndPoint lep) { bool bResult = false; Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); ICMPHeader header = new ICMPHeader(); header.type = 8; header.code = 0; header.id = Convert.ToUInt16(Process.GetCurrentProcess().Id); header.seq = 0; header.chksum = 0; DateTime startTime = DateTime.Now; header.timestamp = Convert.ToUInt64(startTime.Ticks); byte[] arHeader = header.toByteArray(); byte[] arBytes = new byte[arHeader.Length + 32]; byte fill = Convert.ToByte('E'); for(int i=0; i 1) { chksum += (ulong)((((ushort)arBytes[i+1])<<8)+(ushort)arBytes[i]); nSize -= 2; i += 2; } if(nSize >0) { chksum += arBytes[i]; } chksum = (chksum >> 16) + (chksum & 0xFFFF); chksum += (chksum >> 16); ushort result = (ushort)(~chksum); return result; } private bool isSocketReadible(Socket s) { bool bResult = false; int n = 0; while(n < m_Timeout) { if(s.Poll(200, SelectMode.SelectRead)) { bResult = true; break; } n++; } return bResult; } private bool verifyReceivedMessage(byte[] arBytes, int nReceived, int minLengthSent) { int nLengthIPHeader = arBytes[0] & 0x0F; nLengthIPHeader *= 4; if(nLengthIPHeader + minLengthSent > nReceived) { m_strErrorMsg = "Nicht genug Daten zurück gekommen"; return false; } if(arBytes[nLengthIPHeader]!=0) { m_strErrorMsg = "Falscher Typ zurück gekommen"; return false; } int nId = arBytes[nLengthIPHeader + 6] + arBytes[nLengthIPHeader + 7]*256; if(nId != Convert.ToUInt16(Process.GetCurrentProcess().Id)) { m_strErrorMsg = "Daten vom falschen Programm zurück gekommen"; return false; } return true; } private int calcAvgTime() { int result = 0; foreach(int i in m_arTime) { result+=i; } result /= m_arTime.Length; return result; } private int calcAvgTTL() { int result = 0; foreach(int i in m_arTTL) { if(m_arResults[i]) { result += i; } } result /= m_arTTL.Length; return result; } private void clearStats() { for(int i = 0; i