Example #1
0
        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();
        }
Example #2
0
        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
        }