//Insert statement public static void InsertICMPRecord(IPHeader ipHeader, ICMPHeader icmpHeader) { String table_name = "ICMP_Packet"; string timestamp = DateTime.Now.ToString(); string source_ip = ipHeader.SourceAddress.ToString(); string dest_ip = ipHeader.DestinationAddress.ToString(); // IPAddress 0 for host machine, 1 for external I.P. int type = icmpHeader.Type; int code = icmpHeader.Code; string time_to_live = ipHeader.TTL; string query = string.Format("Insert into {0} values( '{1}','{2}','{3}',{4},{5}, {[6});", table_name, timestamp, source_ip, dest_ip, type, code, time_to_live); Console.WriteLine(query); //open connection MySqlCommand cmd = new MySqlCommand(query, connection); cmd.Cancel(); //Execute command cmd.ExecuteNonQueryAsync(); }
private void ParseData(byte[] byteData, int nReceived) { //Since all protocol packets are encapsulated in the IP datagram //so we start by parsing the IP header and see what protocol data //is being carried by it //Console.WriteLine("Packet Captured"); IPHeader ipHeader = new IPHeader(byteData, nReceived); //Now according to the protocol being carried by the IP datagram we parse //the data field of the datagram Console.WriteLine(ipHeader.ProtocolType.ToString()); switch (ipHeader.ProtocolType) { case Protocol.TCP: TCPHeader tcpHeader = new TCPHeader(ipHeader.Data, //IPHeader.Data stores the data being //carried by the IP datagram ipHeader.MessageLength); //Length of the data field //Console.WriteLine("TCP"); DBConn.InsertTCPRecord(ipHeader, tcpHeader); break; case Protocol.UDP: UDPHeader udpHeader = new UDPHeader(ipHeader.Data, //IPHeader.Data stores the data being //carried by the IP datagram (int)ipHeader.MessageLength); //Length of the data field //MakeUDPTreeNode(udpHeader); //Console.WriteLine("UDP Successful"); DBConn.InsertUDPRecord(ipHeader, udpHeader); break; case Protocol.ICMP: ICMPHeader icmpHeader = new ICMPHeader(ipHeader.Data, (int)ipHeader.MessageLength); // Get the IPHeader packet data segregated //MessageBox.Show("Someone is pinging on this PC"); //Insert into Database //Console.WriteLine("ICMP retrieval successful"); DBConn.InsertICMPRecord(ipHeader, icmpHeader); break; } //Thread safe adding of the nodes }