コード例 #1
0
        public static void  Main1(System.String[] args)
        {
            byte[] bytes = HeaderGenerator.generateRandomEthernetHeader();
            System.Console.Error.WriteLine(HexHelper.toString(bytes));

            bytes = HeaderGenerator.generateRandomIPHeader();
            System.Console.Error.WriteLine(HexHelper.toString(bytes));

            bytes = HeaderGenerator.generateRandomARPHeader();
            System.Console.Error.WriteLine(HexHelper.toString(bytes));
        }
コード例 #2
0
        /// <summary> Generate a pseudo-random network packet.
        /// </summary>
        /// <returns> an array of bytes containing a randomly generated
        /// ethernet network packet. Packet can encapsulate any known protocol.
        ///
        /// </returns>
        public static byte[] generate()
        {
            // create ethernet header
            byte[] packet = HeaderGenerator.generateRandomEthernetHeader();
            int    eProto = ArrayHelper.extractInteger(packet, Packets.EthernetFields.ETH_CODE_POS, Packets.EthernetFields.ETH_CODE_LEN);

            // figure out what type of packet should be encapsulated after the
            // newly generated ethernet header.
            switch (eProto)
            {
            case Packets.EthernetProtocols_Fields.IP:
                byte[] ipHeader = HeaderGenerator.generateRandomIPHeader();
                packet = ArrayHelper.join(packet, ipHeader);

                // figure out what type of protocol should be encapsulated after the
                // newly generated IP header.
                int ipProto = ArrayHelper.extractInteger(ipHeader, Packets.IPFields_Fields.IP_CODE_POS, Packets.IPFields_Fields.IP_CODE_LEN);
                switch (ipProto)
                {
                case Packets.IPProtocols_Fields.UDP:
                    byte[] udpHeader = HeaderGenerator.generateRandomUDPHeader();
                    packet = ArrayHelper.join(packet, udpHeader);
                    break;

                case Packets.IPProtocols_Fields.ICMP:
                    byte[] icmpHeader = HeaderGenerator.generateRandomICMPHeader();
                    packet = ArrayHelper.join(packet, icmpHeader);
                    break;

                case Packets.IPProtocols_Fields.TCP:
                    byte[] tcpHeader = HeaderGenerator.generateRandomTCPHeader();
                    packet = ArrayHelper.join(packet, tcpHeader);
                    break;

                default:
                    break;
                }
                break;

            case Packets.EthernetProtocols_Fields.ARP:
                byte[] arpHeader = HeaderGenerator.generateRandomARPHeader();
                packet = ArrayHelper.join(packet, arpHeader);
                break;

            default:
                break;
            }

            return(packet);
        }