public void ChecksumCalculator_returns_0_for_equal_numbers()
        {
            var example = new int[3][] {
                new int[] { 3, 3, 3 },
                new int[] { 3, 3, 3 },
                new int[] { 3, 3, 3 }
            };

            var result = _calculator.CalculateChecksum(example);

            Assert.AreEqual(0, result);
        }
Example #2
0
 public void CalculateChecksumCorrectly()
 {
     Assert.AreEqual(0xC3, _checksumCalculator.CalculateChecksum(new byte[]
     {
         0x68, 0x27, 0x19, 0x40, 0x23, 0x74, 0x68, 0x68, 0x11, 0x04, 0x34, 0x35, 0xC3, 0x33, 0xC3, 0x16
     }, 0, 13));
     Assert.AreEqual(0x7C, _checksumCalculator.CalculateChecksum(new byte[]
     {
         0x68, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x68, 0x91, 0x08, 0x34, 0x35, 0xC3, 0x33, 0x5C, 0x3C, 0x33,
         0x33, 0x7C, 0x16
     }, 0, 17));
 }
Example #3
0
        /// <summary>
        /// Returns the checksum for this UDPFrame. This method works not always clean. An alternative is to set the checksum of an UDP frame to an empty byte array with the length of two.
        /// </summary>
        /// <param name="bPseudoHeader">The IP which should be included into the checksum calculation</param>
        /// <returns>The checksum data</returns>
        public byte[] CalculateChecksum(byte[] bPseudoHeader)
        {
            if (bPseudoHeader.Length % 2 != 0)
            {
                throw new ArgumentException("Pseudo header length must be a multiple of two.");
            }

            int iLength = this.Length;

            byte[] bInnerData = fEncapsulatedFrame != null ? fEncapsulatedFrame.FrameBytes : new byte[0];

            byte[] bUDPFrame = new byte[8 + bInnerData.Length + (bInnerData.Length % 2 == 0 ? 0 : 1) + bPseudoHeader.Length];

            bPseudoHeader.CopyTo(bUDPFrame, 0);

            bUDPFrame[bPseudoHeader.Length + 0] = (byte)((iSourcePort >> 8) & 0xFF);
            bUDPFrame[bPseudoHeader.Length + 1] = (byte)((iSourcePort) & 0xFF);
            bUDPFrame[bPseudoHeader.Length + 2] = (byte)((iDestinationPort >> 8) & 0xFF);
            bUDPFrame[bPseudoHeader.Length + 3] = (byte)((iDestinationPort) & 0xFF);
            bUDPFrame[bPseudoHeader.Length + 4] = (byte)((iLength >> 8) & 0xFF);
            bUDPFrame[bPseudoHeader.Length + 5] = (byte)((iLength) & 0xFF);
            bUDPFrame[bPseudoHeader.Length + 6] = 0;
            bUDPFrame[bPseudoHeader.Length + 7] = 0;

            bInnerData.CopyTo(bUDPFrame, bPseudoHeader.Length + 8);

            return(ChecksumCalculator.CalculateChecksum(bUDPFrame));
        }
Example #4
0
        public DltPacket ParseDltData(byte[] dltData)
        {
            if (dltData[0] != DltConstants.FrameStartByte)
            {
                throw new InvalidFrameStartByteException(dltData[0]);
            }
            if (dltData[7] != DltConstants.FrameStartByte)
            {
                throw new InvalidFrameStartByteException(dltData[7]);
            }

            var expectedChecksum = _checksumCalculator.CalculateChecksum(dltData, 0, dltData.Length - 3);
            var actualChecksum   = dltData[dltData.Length - 2];

            if (expectedChecksum != actualChecksum)
            {
                throw new FrameChecksumMismatchedException(expectedChecksum, actualChecksum);
            }


            if (dltData[dltData.Length - 1] != DltConstants.FrameStopByte)
            {
                throw new InvalidFrameStopByteException(dltData[dltData.Length - 1]);
            }

            return(new DltPacket
            {
                Address = ParseAddress(dltData),
                ControlByte = new ControlByte(dltData[8]),
                DataBlock = ParseDataBlock(dltData)
            });
        }
Example #5
0
        public void Day2a()
        {
            var input  = File.ReadAllText(".\\Content\\Day02.txt");
            var answer = ChecksumCalculator.CalculateChecksum(input);

            answer.Should().Be(5681);
        }
Example #6
0
        public void Day2a_Test()
        {
            var input  = "abcdef\r\nbababc\r\nabbcde\r\nabcccd\r\naabcdd\r\nabcdee\r\nababab";
            var answer = ChecksumCalculator.CalculateChecksum(input);

            answer.Should().Be(12);
        }
Example #7
0
        /// <summary>
        /// Calculates the TCP checksum of this frame
        /// </summary>
        /// <param name="bPseudoHeader">The IP pseudo header to add to the checksum</param>
        /// <returns>The checksum data</returns>
        public byte[] CalculateChecksum(byte[] bPseudoHeader)
        {
            if (bPseudoHeader.Length % 2 != 0)
            {
                throw new ArgumentException("Pseudo header length must be a multiple of two.");
            }

            byte[] bInnerData = this.fEncapsulatedFrame != null ? this.fEncapsulatedFrame.FrameBytes : new byte[0];

            int iTotalLen = bPseudoHeader.Length + Length;

            if (iTotalLen % 2 != 0)
            {
                iTotalLen += 1;
            }

            byte[] bTCPFrame = new byte[iTotalLen];

            int iDataOffset = 20 + oOptions.OptionLength;

            if (iDataOffset % 4 != 0)
            {
                iDataOffset += 4 - (iDataOffset % 4);
            }

            bPseudoHeader.CopyTo(bTCPFrame, 0);

            bTCPFrame[bPseudoHeader.Length + 0]   = (byte)((iSourcePort >> 8) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 1]   = (byte)((iSourcePort) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 2]   = (byte)((iDestinationPort >> 8) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 3]   = (byte)((iDestinationPort) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 4]   = (byte)((iSequenceNumber >> 24) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 5]   = (byte)((iSequenceNumber >> 16) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 6]   = (byte)((iSequenceNumber >> 8) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 7]   = (byte)((iSequenceNumber) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 8]   = (byte)((iAcknowledgmentNumber >> 24) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 9]   = (byte)((iAcknowledgmentNumber >> 16) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 10]  = (byte)((iAcknowledgmentNumber >> 8) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 11]  = (byte)((iAcknowledgmentNumber) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 12]  = (byte)((((iDataOffset) / 4) << 4) & 0xF0);
            bTCPFrame[bPseudoHeader.Length + 13] |= (byte)(bUrgent ? 0x20 : 0x00);
            bTCPFrame[bPseudoHeader.Length + 13] |= (byte)(bAcknowledgement ? 0x10 : 0x00);
            bTCPFrame[bPseudoHeader.Length + 13] |= (byte)(bPush ? 0x8 : 0x00);
            bTCPFrame[bPseudoHeader.Length + 13] |= (byte)(bReset ? 0x4 : 0x00);
            bTCPFrame[bPseudoHeader.Length + 13] |= (byte)(bSynchronize ? 0x2 : 0x00);
            bTCPFrame[bPseudoHeader.Length + 13] |= (byte)(bFinish ? 0x1 : 0x00);
            bTCPFrame[bPseudoHeader.Length + 14]  = (byte)((iWindow >> 8) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 15]  = (byte)((iWindow) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 16]  = 0;
            bTCPFrame[bPseudoHeader.Length + 17]  = 0;
            bTCPFrame[bPseudoHeader.Length + 18]  = (byte)((iUrgentPointer >> 8) & 0xFF);
            bTCPFrame[bPseudoHeader.Length + 19]  = (byte)((iUrgentPointer) & 0xFF);

            oOptions.Raw.CopyTo(bTCPFrame, bPseudoHeader.Length + 20);

            bInnerData.CopyTo(bTCPFrame, bPseudoHeader.Length + iDataOffset);

            return(ChecksumCalculator.CalculateChecksum(bTCPFrame));
        }
Example #8
0
        /// <summary>
        /// Calculates an IPv6 ICMP checksum from this frame.
        /// </summary>
        /// <param name="bPseudoHeader">The IPv6 pseudo header to use for the calculation.</param>
        /// <returns>The ICMPv6 checksum.</returns>
        public byte[] CalculateChecksum(byte[] bPseudoHeader)
        {
            byte[] bChecksumData = new byte[bPseudoHeader.Length + this.Length];
            Array.Copy(bPseudoHeader, 0, bChecksumData, 0, bPseudoHeader.Length);
            Array.Copy(this.FrameBytes, 0, bChecksumData, bPseudoHeader.Length, this.Length);

            return(ChecksumCalculator.CalculateChecksum(bChecksumData));
        }
Example #9
0
        static void Main(string[] args)
        {
            var calculator = new ChecksumCalculator();
            var matrix     = calculator.LoadMatrixFromFile("day2_in.txt");

            Console.WriteLine(calculator.CalculateChecksum(matrix));

            Console.ReadKey();
        }
Example #10
0
        public byte[] GenerateDltData(DltPacket dltPacket)
        {
            var dltData = new byte[12 + dltPacket.DataBlock.Length];

            dltData[0] = DltConstants.FrameStartByte;
            AssignAddress(dltData, dltPacket.Address);
            dltData[7] = DltConstants.FrameStartByte;
            dltData[8] = dltPacket.ControlByte.Byte;
            AssignDataBlock(dltData, dltPacket.DataBlock);

            var checksum = _checksumCalculator.CalculateChecksum(dltData, 0, dltData.Length - 3);

            dltData[dltData.Length - 2] = checksum;
            dltData[dltData.Length - 1] = DltConstants.FrameStopByte;

            return(dltData);
        }