Exemplo n.º 1
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();
        }
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);
        }