コード例 #1
0
        private void btn_send_packet_Click(object sender, EventArgs e)
        {
            if (txt_src_mac_enabled)
            {
                try { packetBuilder.src_mac = txt_src_mac.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid source MAC address: " + ex.Message);
                    MessageBox.Show("Invalid source MAC address", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (txt_src_ip_enabled)
            {
                try { packetBuilder.src_ip = txt_src_ip.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid source IP address: " + ex.Message);
                    MessageBox.Show("Invalid source IP address", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (txt_dst_mac_enabled)
            {
                try { packetBuilder.dst_mac = txt_dst_mac.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid destination MAC address: " + ex.Message);
                    MessageBox.Show("Invalid destination MAC address", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (txt_dst_ip_enabled)
            {
                try { packetBuilder.dst_ip = txt_dst_ip.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid destination IP address: " + ex.Message);
                    MessageBox.Show("Invalid destination IP address", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if (txt_src_port_enabled)
            {
                try
                { packetBuilder.src_port = UInt16.Parse(txt_src_port.Text); }

                catch (Exception ex) {
                    Console.WriteLine("Invalid source port: " + ex.Message);
                    MessageBox.Show("Invalid source port", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (txt_dst_port_enabled)
            {
                try { packetBuilder.dst_port = UInt16.Parse(txt_dst_port.Text); }


                catch (Exception ex)
                {
                    Console.WriteLine("Invalid destination port: " + ex.Message);
                    MessageBox.Show("Invalid destination port", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (txt_data_enabled)
            {
                try { packetBuilder.data = _data;

                      if (packetBuilder.data.Length == 0)
                      {
                          Console.WriteLine("Data field is empty");
                          MessageBox.Show("Data field is empty", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                          return;
                      }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Data field is empty: " + ex.Message);
                    MessageBox.Show("Data field is empty", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (txt_dcp_set_ip_enabled)
            {
                try { packetBuilder.PN_DCP_Block_IP_Address = txt_PN_DCP_Set_IP_Address.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid PN-DCP IP address: " + ex.Message);
                    MessageBox.Show("Invalid PN-DCP IP address", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                try { packetBuilder.PN_DCP_Block_IP_Subnet = txt_PN_DCP_Set_IP_Subnet.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid PN-DCP Subnet Mask: " + ex.Message);
                    MessageBox.Show("Invalid PN-DCP Subnet Mask", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                try { packetBuilder.PN_DCP_Block_IP_Gateway = txt_PN_DCP_Set_IP_Gateway.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid PN-DCP Default Gateway: " + ex.Message);
                    MessageBox.Show("Invalid PN-DCP Default Gateway", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (txt_dcp_set_devicename_enabled)
            {
                try { packetBuilder.PN_DCP_Block_DeviceName = txt_dcp_devicename.Text; }
                catch (Exception ex)
                {
                    Console.WriteLine("Invalid PN-DCP Device Name: " + ex.Message);
                    MessageBox.Show("Invalid PN-DCP Device Name", "Packet Sender - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            switch (int_curr_protocol)
            {
            case 0:     // ARP
                packetBuilder.BuildPacket_ARP();
                packetBuilder.SendPacket();
                break;

            case 1:     // RARP
                packetBuilder.BuildPacket_RARP();
                packetBuilder.SendPacket();
                break;

            case 2:     // ICMP
                packetBuilder.BuildPacket_ICMP();
                packetBuilder.SendPacket();
                break;

            case 50:     // TCP/IP
                packetBuilder.BuildPacket_TCP_IP();
                packetBuilder.SendPacket();
                break;

            case 51:     // UDP/IP
                packetBuilder.BuildPacket_UDP_IP();
                packetBuilder.SendPacket();
                break;

            case 100:     // PN-DCP, Ident Req, ALL
                switch (int_curr_sub_protocol)
                {
                case 0:         // Ident Req, ALL
                    packetBuilder.BuildPacket_PN_DCP_ID_REQ_All();
                    packetBuilder.SendPacket();
                    break;

                //case 1: // Ident Req (IP-address)
                //case 2: // Ident Req (Device name)
                case 3:         // Set Req (IP-address)
                    packetBuilder.BuildPacket_PN_DCP_SET_IP();
                    packetBuilder.SendPacket();
                    break;

                case 4:         // Set Req (Device name)
                    packetBuilder.BuildPacket_PN_DCP_SET_DEVICENAME();
                    packetBuilder.SendPacket();
                    break;
                }
                break;

            case 200:     // Raw Packet (DATA)
                switch (int_curr_sub_protocol)
                {
                case 0:
                    packetBuilder.BuildPacket_RAW_DATA();
                    packetBuilder.SendPacket();
                    break;

                case 1:         // Raw Packet (MAC + DATA)
                    packetBuilder.BuildPacket_RAW_MAC_DATA();
                    packetBuilder.SendPacket();
                    break;

                case 2:         // Raw Packet (MAC + IP + DATA)
                    packetBuilder.BuildPacket_RAW_MAC_IP_DATA();
                    packetBuilder.SendPacket();
                    break;
                }
                break;
            }
            MessageBox.Show("Message send succesfull", "Packet Sender", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }