public static void InsertUDPRecord(IPHeader ipHeader, UDPHeader uDPHeader) { String table_name = "UDP_Packet"; string timestamp = DateTime.Now.ToString(); string source_ip = ipHeader.SourceAddress.ToString(); string dest_ip = ipHeader.DestinationAddress.ToString(); string header_length = uDPHeader.Length; string checksum = uDPHeader.Checksum; string ttl = ipHeader.TTL; // IPAddress 0 for host machine, 1 for external I.P. int source_port = uDPHeader.SourcePort; int dest_port = uDPHeader.DestinationPort; string time_to_live = ipHeader.TTL; //String query = "Insert into " + table_name + " values('" + timestamp + "','" + source_ip + "'," + source_port + ",'" + dest_ip + "'," + dest_port + ");"; string query = string.Format("Insert into {0} values( '{1}','{2}',{3},'{4}',{5},{6}, {7});", table_name, timestamp, source_ip, source_port, dest_ip, dest_port, time_to_live, header_length); //string query = string.Format("Insert into {0} values( '{1}','{2}',{3},'{4}',{5}, {6}, 0 , 1 , 0 ,,,,, {7},'{8}',,);", table_name, timestamp, source_ip, source_port, dest_ip, dest_port, time_to_live, header_length, checksum); Console.WriteLine(query); //open connection //Console.WriteLine(query); MySqlCommand cmd = new MySqlCommand(query, connection); //Execute command cmd.ExecuteNonQueryAsync(); }
//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 }
public static void InsertTCPRecord(IPHeader ipHeader, TCPHeader tCPHeader) { String table_name = "TCP_Packet"; string timestamp = DateTime.Now.ToString(); string source_ip = ipHeader.SourceAddress.ToString(); string dest_ip = ipHeader.DestinationAddress.ToString(); string ttl = ipHeader.TTL; string header_length = tCPHeader.HeaderLength; TCPFlags obj = tCPHeader.Flags; int FIN = Convert.ToInt32(obj.FIN); int SYN = Convert.ToInt32(obj.SYN); int ACK = Convert.ToInt32(obj.ACK); /* * var IPv4Addresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(al => al.AddressFamily == AddressFamily.InterNetwork).AsEnumerable(); * foreach (IPAddress ip in IPv4Addresses) * { * if (ip.ToString().Equals(source_ip_in_address_format)) * source_ip = "0"; * else if (ip.ToString().Equals(dest_ip_in_address_format)) * dest_ip = "0"; * * } * */ // IPAddress 0 for host machine, 1 for external I.P. string source_port = tCPHeader.SourcePort; string dest_port = tCPHeader.DestinationPort; string time_to_live = ipHeader.TTL; //string connstring = string.Format("Server=localhost; database={0}; UID=UserName; password=your password", databaseName); string query = string.Format("Insert into {0} values( '{1}','{2}',{3},'{4}',{5},{6}, {7}, {8}, {9});", table_name, timestamp, source_ip, source_port, dest_ip, dest_port, time_to_live, SYN, ACK, FIN); //string query = string.Format("Insert into {0} values( '{1}','{2}',{3},'{4}',{5},{6}, 1 , 0 , 0 ,{7}, {8} , {9} , {10},,'',,);", table_name, timestamp, source_ip, source_port, dest_ip, dest_port, time_to_live, header_length, SYN, ACK, FIN); //String query = "Insert into " + table_name + " values('" + timestamp + "','" + source_ip + "'," + source_port + ",'" + dest_ip + "'," + dest_port + ");"; Console.WriteLine(query); //open connection MySqlCommand cmd = new MySqlCommand(query, connection); //Execute command cmd.ExecuteNonQueryAsync(); //cmd.ExecuteNonQuery(); }