protected bool check_if_one_of_our_packets(byte[] data)
        {
            easy_socket.ip_header.ipv4_header iph = new easy_socket.ip_header.ipv4_header();

            /*
             * error_success=0;
             * error_datagram_null=1;
             * error_datagram_internet_header_length_too_small=2;
             * error_datagram_total_length_too_small=3;
             * error_datagram_not_complete=4;
             */
            byte b = iph.decode(data);

            if ((b == easy_socket.ip_header.ipv4_header.error_datagram_null) ||
                (b == easy_socket.ip_header.ipv4_header.error_datagram_internet_header_length_too_small) ||
                (b == easy_socket.ip_header.ipv4_header.error_datagram_total_length_too_small))
            {
                return(false);
            }
            if (iph.protocol != easy_socket.ip_header.ipv4_header.protocol_icmp)
            {
                return(false);
            }
            // error_success || error_datagram_not_complete
            if (iph.data == null)
            {
                return(false);
            }
            if (iph.data.Length < 8) // 8=icmp_echo header size
            {
                return(false);
            }
            if (iph.data[0] != 8) // it's not a reply to an echo msg
            {
                return(false);
            }
            easy_socket.icmp.icmp_echo ie = new easy_socket.icmp.icmp_echo();
            ie.decode(iph.data);

            int id = (ie.identifier << 16) + ie.sequence_number;

            return(this.check_identifier(id));
        }
        public FormPing(string host, string delay, string ttl, string nb_ping, string delay_between_ping_sending, bool looping, bool b_may_multiple_replies)
        {
            InitializeComponent();

            Tools.GUI.XPStyle.MakeXPStyle(this);

            this.textBox_icmp_ip.Text = host;
            this.textBox_icmp_delay_dor_reply.Text            = delay;
            this.textBox_icmp_packet_ttl.Text                 = ttl;
            this.textBox_icmp_ping_number.Text                = nb_ping;
            this.textBox_icmp_delay_between_ping_sending.Text = delay_between_ping_sending;
            this.checkBox_icmp_looping_ping.Checked           = looping;
            this.checkBox_may_broadcast.Checked               = b_may_multiple_replies;
            this.textBox_icmp_ping_number.Enabled             = !this.checkBox_icmp_looping_ping.Checked;
            al = new System.Collections.ArrayList(9);
            // create echo
            this.icmp_echo = new easy_socket.icmp.icmp_echo();
            // create echo server
            this.icmp_server = new easy_socket.icmp.icmp_server();
            // add events
            this.add_events();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// used to get original datagram info on icmp troubles msg. the icmp msg should contain internet header and 64 bits of original datagram
        /// </summary>
        /// <param name="raw_databool"></param>
        /// <param name="b_most_important_info_only"></param>
        /// <returns></returns>
        private static string decode_internet_header_and_64_bits_of_original_datagram(byte[] raw_data, bool b_most_important_info_only)
        {
            if (raw_data == null)
            {
                return("");
            }
            string ret = "";
            byte   ret_decode;

            // decode
            easy_socket.ip_header.ipv4_header ipv4h = new easy_socket.ip_header.ipv4_header();
            ret_decode = ipv4h.decode(raw_data);
            if ((ret_decode != easy_socket.ip_header.ipv4_header.error_success) && (ret_decode != easy_socket.ip_header.ipv4_header.error_datagram_not_complete) && (ret_decode != easy_socket.ip_header.ipv4_header.error_datagram_length_not_matching))
            {
                return(easy_socket.hexa_convert.byte_to_hexa(raw_data));
            }
            string[] data = null;
            // case of no data in original datagram
            if (ipv4h.data == null)
            {
                data = packet_ToStringArray.ip_raw(ref ipv4h, b_most_important_info_only);
            }
            else if (ipv4h.data.Length < 1)
            {
                data = packet_ToStringArray.ip_raw(ref ipv4h, b_most_important_info_only);
            }
            // assume there's data in original ip datagram
            else// data !=null && length>1
            {
                switch (ipv4h.protocol)
                {
                // if icmp protocol
                case easy_socket.ip_header.ipv4_header.protocol_icmp:
                    switch (ipv4h.data[0])// switch type (see icmp protocol)
                    {
                    case easy_socket.icmp.icmp.EchoReply:
                        easy_socket.icmp.icmp_echo_reply icmper = new easy_socket.icmp.icmp_echo_reply();
                        if (icmper.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_echo_reply(ref ipv4h, ref icmper, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.DestinationUnreachable:
                        easy_socket.icmp.icmp_destination_unreachable icmpdu = new easy_socket.icmp.icmp_destination_unreachable();
                        if (icmpdu.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_destination_unreachable(ref ipv4h, ref icmpdu, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.SourceQuench:
                        easy_socket.icmp.icmp_source_quench icmpsq = new easy_socket.icmp.icmp_source_quench();
                        if (icmpsq.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_source_quench(ref ipv4h, ref icmpsq, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.Redirect:
                        easy_socket.icmp.icmp_redirect icmpr = new easy_socket.icmp.icmp_redirect();
                        if (icmpr.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_redirect(ref ipv4h, ref icmpr, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.Echo:
                        easy_socket.icmp.icmp_echo icmpe = new easy_socket.icmp.icmp_echo();
                        if (icmpe.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_echo(ref ipv4h, ref icmpe, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.TimeExceeded:
                        easy_socket.icmp.icmp_time_exceeded_message icmptem = new easy_socket.icmp.icmp_time_exceeded_message();
                        if (icmptem.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_time_exceeded_message(ref ipv4h, ref icmptem, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.ParameterProblem:
                        easy_socket.icmp.icmp_parameter_problem icmppp = new easy_socket.icmp.icmp_parameter_problem();
                        if (icmppp.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_parameter_problem(ref ipv4h, ref icmppp, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.Timestamp:
                        easy_socket.icmp.icmp_timestamp icmpt = new easy_socket.icmp.icmp_timestamp();
                        if (icmpt.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_timestamp(ref ipv4h, ref icmpt, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.TimestampReply:
                        easy_socket.icmp.icmp_timestamp_reply icmptr = new easy_socket.icmp.icmp_timestamp_reply();
                        if (icmptr.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_timestamp_reply(ref ipv4h, ref icmptr, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.InformationRequest:
                        easy_socket.icmp.icmp_information_request icmpirequest = new easy_socket.icmp.icmp_information_request();
                        if (icmpirequest.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_information_request(ref ipv4h, ref icmpirequest, b_most_important_info_only);
                        }
                        break;

                    case easy_socket.icmp.icmp.InformationReply:
                        easy_socket.icmp.icmp_information_reply icmpireply = new easy_socket.icmp.icmp_information_reply();
                        if (icmpireply.decode(ipv4h.data) == easy_socket.icmp.icmp.error_success)
                        {
                            data = packet_ToStringArray.icmp_information_reply(ref ipv4h, ref icmpireply, b_most_important_info_only);
                        }
                        break;
                        //default:
                    }// end of icmp switch
                    break;

                // if udp protocol
                case easy_socket.ip_header.ipv4_header.protocol_udp:
                    easy_socket.udp_header.udp_header udph = new easy_socket.udp_header.udp_header();
                    udph.decode(ipv4h.source_address, ipv4h.destination_address, ipv4h.data, false); // don't check checksum
                    data = packet_ToStringArray.udp(ref ipv4h, ref udph, false);                     // show max info of original packet
                    break;

                // if tcp protocol
                case easy_socket.ip_header.ipv4_header.protocol_tcp:
                    easy_socket.tcp_header.tcp_header tcph = new easy_socket.tcp_header.tcp_header();
                    tcph.decode(ipv4h.source_address, ipv4h.destination_address, ipv4h.data, false);  // don't check checksum
                    data = packet_ToStringArray.tcp(ref ipv4h, ref tcph, b_most_important_info_only); // show max info of original packet
                    break;

                // other protocol
                default:
                    data = packet_ToStringArray.ip_raw(ref ipv4h, b_most_important_info_only);
                    break;
                } // end of protocol switch
            }     // end of ip data test
            // Join data (string[]) to a string
            if (data != null)
            {
                ret = System.String.Join(" ", data);
            }
            return(ret);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ipv4h"></param>
        /// <param name="icmp"></param>
        /// <param name="b_most_important_info_only"></param>
        /// <returns>ip info-ip data(length=4),"","",info,data</returns>
        public static string[] icmp_echo(ref easy_socket.ip_header.ipv4_header ipv4h, ref easy_socket.icmp.icmp_echo icmp, bool b_most_important_info_only)
        {
            string str_global_info;

            str_global_info = "Echo";
            if (!b_most_important_info_only)
            {
                str_global_info += ", Type:" + icmp.Type;
                str_global_info += ", Code:" + icmp.code;
                str_global_info += ", Checksum:" + icmp.Checksum;
            }
            str_global_info += ", Identifier:" + icmp.Identifier;
            str_global_info += ", SeqNum:" + icmp.SequenceNumber;

            string[] ret = new string[4 + 4];
            System.Array.Copy(packet_ToStringArray.ip_raw(ref ipv4h, b_most_important_info_only), 0, ret, 0, 4);
            ret[4] = "";
            ret[5] = "";
            ret[6] = str_global_info;
            ret[7] = easy_socket.hexa_convert.byte_to_hexa(icmp.data);
            return(ret);
        }