public void Print(IHeader header) { IPHeader newIPHeader = header as IPHeader; Console.WriteLine(string.Format("Protocol\tSource IP\tDestination IP\tTotal Length")); Console.WriteLine(string.Format(" {0}\t{1}\t{2}\t{3}", newIPHeader.Protocol, newIPHeader.SourceIP, newIPHeader.DestIP, newIPHeader.TotalLength)); }
/// <summary> /// Parses packets /// </summary> /// <param name="protocol"></param> /// <param name="flag"></param> protected virtual void ParsePacket(IPHeader protocol, FilterType filter) { try { IHeader header; switch (filter) { case FilterType.All: if (protocol.Protocol == ProtocolType.Tcp) { header = new TCPHeader(protocol.Data, Convert.ToInt32(protocol.MessageLength)); } else { header = new UDPHeader(protocol.Data, Convert.ToInt32(protocol.MessageLength)); } Print(protocol, header); break; case FilterType.TCP: if (protocol.Protocol == ProtocolType.Tcp) { header = new TCPHeader(protocol.Data, Convert.ToInt32(protocol.MessageLength)); Print(protocol, header); } break; case FilterType.UDP: if (protocol.Protocol == ProtocolType.Udp) { header = new UDPHeader(protocol.Data, Convert.ToInt32(protocol.MessageLength)); Print(protocol, header); } break; } } catch (Exception ex) { Console.WriteLine("Error: " + ex.StackTrace + " | " + ex.Message); } }
/// <summary> /// /// </summary> /// <param name="res"></param> protected override void OnReceive(IAsyncResult res) { try { int nReceived = mainSocket.EndReceive(res); ipHeader = new IPHeader(byteData, nReceived); base.ParsePacket(ipHeader, globalFilter); if (ContinueCapturing) { byteData = new byte[4096]; mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Displays specified information /// </summary> /// <param name="header"></param> protected override void Print(params IHeader[] header) { if (header.Length > 0) { IPHeader ipPacket = header[0] as IPHeader; if (filter == FilterType.All) { if (whatToFilter == SpecFilter.SourceIP) { if (ipPacket.SourceIP == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.DestIP) { if (ipPacket.DestIP == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.SourcePort) { if (header[1].SourcePort == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.DestPort) { if (header[1].DestPort == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else { Console.WriteLine("Unknown request"); } } else if (filter == FilterType.TCP) { if (ipPacket.Protocol == ProtocolType.Tcp) { if (whatToFilter == SpecFilter.SourceIP) { if (ipPacket.SourceIP == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.DestIP) { if (ipPacket.DestIP == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.SourcePort) { if (header[1].SourcePort == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.DestPort) { if (header[1].DestPort == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else { Console.WriteLine("Unknown request"); } } } else if (filter == FilterType.UDP) { if (ipPacket.Protocol == ProtocolType.Udp) { if (whatToFilter == SpecFilter.SourceIP) { if (ipPacket.SourceIP == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.DestIP) { if (ipPacket.DestIP == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.SourcePort) { if (header[1].SourcePort == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else if (whatToFilter == SpecFilter.DestPort) { if (header[1].DestPort == key) { Console.WriteLine(string.Format("{0}\t{1} : {2}\t\t{3} : {4}", ipPacket.Protocol, ipPacket.SourceIP, header[1].SourcePort, ipPacket.DestIP, header[1].DestPort)); } } else { Console.WriteLine("Unknown request"); } } } else { Console.WriteLine("Unknown packet was received"); } } else { Console.WriteLine("An error occured, nothing to display"); } }