Exemplo 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
 }
Exemplo n.º 6
0
 /// <summary> Sets the IP header checksum.</summary>
 protected internal virtual void SetChecksum(int cs, int checkSumOffset)
 {
     ArrayHelper.insertLong(_bytes, cs, checkSumOffset, 2);
 }