示例#1
0
 ///
 /// <summary> Create a new IP packet.
 /// </summary>
 public IPPacket(int lLen, byte[] bytes) : base(lLen, bytes)
 {
     // fetch the actual header length from the incoming bytes
     _ipHeaderLength = (ArrayHelper.extractInteger(_bytes, _ethOffset + Packets.IPFields_Fields.IP_VER_POS, Packets.IPFields_Fields.IP_VER_LEN) & 0xf) * 4;
     // set offset into _bytes of previous layers
     _ipOffset = _ethOffset + _ipHeaderLength;
 }
示例#2
0
        /// <summary> Convert captured packet data into an object.
        /// </summary>
        public static Packet dataToPacket(int linkType, byte[] bytes, Timeval tv)
        {
            int ethProtocol;

            // record the length of the headers associated with this link layer type.
            // this length is the offset to the header embedded in the packet.
            lLen = LinkLayer.getLinkLayerLength(linkType);

            // extract the protocol code for the type of header embedded in the
            // link-layer of the packet
            int offset = LinkLayer.getProtoOffset(linkType);

            if (offset == -1)
            {
                // if there is no embedded protocol, assume IP?
                ethProtocol = Packets.EthernetProtocols_Fields.IP;
            }
            else
            {
                ethProtocol = ArrayHelper.extractInteger(bytes, offset, Packets.EthernetFields.ETH_CODE_LEN);
            }

            // try to recognize the ethernet type..
            switch (ethProtocol)
            {
            // arp
            case Packets.EthernetProtocols_Fields.ARP:
                return(new ARPPacket(lLen, bytes, tv));

            case Packets.EthernetProtocols_Fields.IP:
                int ipProtocol = IPProtocol.extractProtocol(lLen, bytes);
                switch (ipProtocol)
                {
                // icmp
                case Packets.IPProtocols_Fields.ICMP:  return(new ICMPPacket(lLen, bytes, tv));

                // igmp

                case Packets.IPProtocols_Fields.IGMP:  return(new IGMPPacket(lLen, bytes, tv));

                // tcp

                case Packets.IPProtocols_Fields.TCP:  return(new TCPPacket(lLen, bytes, tv));

                // udp

                case Packets.IPProtocols_Fields.UDP:  return(new UDPPacket(lLen, bytes, tv));

                // unidentified ip..

                default:  return(new IPPacket(lLen, bytes, tv));
                }
            // ethernet level code not recognized, default to anonymous packet..
            //goto default;

            default:  return(new EthernetPacket(lLen, bytes, tv));
            }
        }
示例#3
0
        ///
        /// <summary> Create a new TCP packet.
        /// </summary>
        public TCPPacket(int lLen, byte[] bytes) : base(lLen, bytes)
        {
            // set TCP header length
            _tcpHeaderLength = ((ArrayHelper.extractInteger(_bytes, _ipOffset + Packets.TCPFields_Fields.TCP_FLAG_POS, Packets.TCPFields_Fields.TCP_FLAG_LEN) >> 12) & 0xf) * 4;
            // set data (payload) length based on info in headers (note: tcpdump
            //  can return extra junk bytes which bubble up to here
            int tmpLen = Length - Packets.IPFields_Fields.IP_HEADER_LEN - _tcpHeaderLength;

            _payloadDataLength = (tmpLen < 0)?0:tmpLen;
        }
示例#4
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);
        }
示例#5
0
        /// <summary> Extract a string describing an IP address from an array of bytes.
        /// *
        /// </summary>
        /// <param name="offset">the offset of the address data.
        /// </param>
        /// <param name="bytes">an array of bytes containing the IP address.
        /// </param>
        /// <returns> a string of the form "255.255.255.255"
        ///
        /// </returns>
        public static System.String extract(int offset, byte[] bytes)
        {
            return(toString(ArrayHelper.extractInteger(bytes, offset, WIDTH)));

            /*
             * StringBuffer sa = new StringBuffer();
             * for(int i=offset; i<offset + WIDTH; i++) {
             * sa.append(0xff & bytes[i]);
             * if(i != offset + WIDTH - 1)
             * sa.append('.');
             * }
             * return sa.toString();
             */
        }