Esempio n. 1
0
        protected internal virtual byte[] AttachPseudoIPHeader(byte[] origHeader)
        {
            bool odd        = origHeader.Length % 2 != 0;
            int  headerSize = 12 + origHeader.Length;

            if (odd)
            {
                headerSize++;
            }

            byte[] headerForChecksum = new byte[headerSize];
            // 0-7: ip src+dest addr
            Array.Copy(_bytes, _ethOffset + IPFields_Fields.IP_SRC_POS, headerForChecksum, 0, 8);
            // 8: always zero
            headerForChecksum[8] = 0;
            // 9: ip protocol
            headerForChecksum[9] = (byte)IPProtocol;
            // 10-11: header+data length
            ArrayHelper.insertLong(headerForChecksum, origHeader.Length, 10, 2);

            // prefix the pseudoHeader to the header+data
            Array.Copy(origHeader, 0, headerForChecksum, 12, origHeader.Length);
            //if not even length, pad with a zero
            if (odd)
            {
                headerForChecksum[headerForChecksum.Length - 1] = 0;
            }

            return(headerForChecksum);
        }
Esempio n. 2
0
        /// <summary> tamir:
        /// Computes the one's sum on a byte array.
        /// Based TCP/IP Illustrated Vol. 2(1995) by Gary R. Wright and W. Richard
        /// Stevens. Page 236. And on http://www.cs.utk.edu/~cs594np/unp/checksum.html
        /// </summary>
        protected internal virtual int _OnesSum(byte[] bytes, int start, int len)
        {
            int sum = 0;             /* assume 32 bit long, 16 bit short */
            int i   = start;

            len = start + len;

            while (i < len - 1)
            {
                sum += ArrayHelper.extractInteger(bytes, i, 2);
                //if ((sum & unchecked((int)0x80000000)) != 0)
                if ((sum & 0x80000000) != 0)
                {
                    /* if high order bit set, fold */
                    sum = (sum & 0xFFFF) + (sum >> 16);
                }
                i += 2;
            }

            if (i < len)
            {
                /* take care of left over byte */
                sum += ArrayHelper.extractInteger(bytes, start, 2);
            }

            while (sum >> 16 != 0)
            {
                sum = (sum & 0xFFFF) + (sum >> 16);
            }

            return(sum & 0xFFFF);
        }
Esempio n. 3
0
        /// <summary>
        /// Sets the data section of this udp packet
        /// </summary>
        /// <param name="data">the data bytes</param>
        public void SetData(byte[] data)
        {
            byte[] headers  = ArrayHelper.copy(_bytes, 0, UDPFields_Fields.UDP_HEADER_LEN + IpHeaderLength + EthernetHeaderLength);
            byte[] newBytes = ArrayHelper.join(headers, data);
            this._bytes = newBytes;
            UDPLength   = _bytes.Length - IpHeaderLength - EthernetHeaderLength;

            //update ip total length length
            IPTotalLength = IpHeaderLength + UDPFields_Fields.UDP_HEADER_LEN + data.Length;

            //update also offset and pcap header
            OnOffsetChanged();
        }
Esempio n. 4
0
        /*
         * taken from TCP/IP Illustrated Vol. 2(1995) by Gary R. Wright and W.
         * Richard Stevens. Page 236
         */

        /// <summary> Computes the IP checksum, optionally updating the IP checksum header.
        ///
        /// </summary>
        /// <param name="update">Specifies whether or not to update the IP checksum
        /// header after computing the checksum.  A value of true indicates
        /// the header should be updated, a value of false indicates it
        /// should not be updated.
        /// </param>
        /// <returns> The computed IP checksum.
        /// </returns>
        public int ComputeIPChecksum(bool update)
        {
            //copy the ip header
            byte[] ip = ArrayHelper.copy(_bytes, _ethOffset, IpHeaderLength);
            //reset the checksum field (checksum is calculated when this field is zeroed)
            ArrayHelper.insertLong(ip, 0, IPFields_Fields.IP_CSUM_POS, 2);
            //compute the one's complement sum of the ip header
            int cs = _OnesCompSum(ip, 0, ip.Length);

            if (update)
            {
                IPChecksum = cs;
            }

            return(cs);
        }
Esempio n. 5
0
        /// <summary> Computes the UDP checksum, optionally updating the UDP checksum header.
        ///
        /// </summary>
        /// <param name="update">Specifies whether or not to update the UDP checksum header
        /// after computing the checksum. A value of true indicates the
        /// header should be updated, a value of false indicates it should
        /// not be updated.
        /// </param>
        /// <returns> The computed UDP checksum.
        /// </returns>
        public int ComputeUDPChecksum(bool update)
        {
            // copy the udp section with data
            byte[] udp = IPData;
            // reset the checksum field (checksum is calculated when this field is
            // zeroed)
            ArrayHelper.insertLong(udp, 0, UDPFields_Fields.UDP_CSUM_POS, UDPFields_Fields.UDP_CSUM_LEN);
            //pseudo ip header should be attached to the udp+data
            udp = AttachPseudoIPHeader(udp);
            // compute the one's complement sum of the udp header
            int cs = _OnesCompSum(udp);

            if (update)
            {
                UDPChecksum = cs;
            }

            return(cs);
        }
Esempio n. 6
0
        public int ComputeTransportLayerChecksum(int checksumOffset, bool update, bool pseudoIPHeader)
        {
            // copy the tcp section with data
            byte[] dataToChecksum = IPData;
            // reset the checksum field (checksum is calculated when this field is
            // zeroed)
            ArrayHelper.insertLong(dataToChecksum, 0, checksumOffset, 2);
            if (pseudoIPHeader)
            {
                dataToChecksum = AttachPseudoIPHeader(dataToChecksum);
            }
            // compute the one's complement sum of the tcp header
            int cs = _OnesCompSum(dataToChecksum);

            if (update)
            {
                SetTransportLayerChecksum(cs, checksumOffset);
            }

            return(cs);
        }
Esempio n. 7
0
 /// <summary> Sets the hardware destination address.</summary>
 public virtual void setARPTargetHwAddress(long addr)
 {
     ArrayHelper.insertLong(_bytes, addr, _ethOffset + ARPFields_Fields.ARP_T_HW_ADDR_POS, 6);
 }
Esempio n. 8
0
 /// <summary> Fetch the header checksum.</summary>
 public virtual int GetTransportLayerChecksum(int pos)
 {
     return(ArrayHelper.extractInteger(_bytes, pos, 2));
 }
Esempio n. 9
0
 /// <summary> Sets the IP header checksum.</summary>
 protected internal virtual void SetChecksum(int cs, int checkSumOffset)
 {
     ArrayHelper.insertLong(_bytes, cs, checkSumOffset, 2);
 }