sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.isis.io.util; |
| 17 | |
| 18 | |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | /** |
| 22 | * Calculates checksum for ISIS LSP packets. |
| 23 | */ |
| 24 | public class ChecksumCalculator { |
| 25 | |
| 26 | /** |
| 27 | * Verifies the checksum is valid in given LSP packet bytes. |
| 28 | * |
| 29 | * @param lspPacket lsp as byte array |
| 30 | * @param lspChecksumPos1 position of checksum bit in packet |
| 31 | * @param lspChecksumPos2 position of checksum bit in packet |
| 32 | * @return true if valid else false |
| 33 | */ |
| 34 | public boolean validateLspCheckSum(byte[] lspPacket, int lspChecksumPos1, int lspChecksumPos2) { |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 35 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 36 | byte[] checksum = calculateLspChecksum(lspPacket, lspChecksumPos1, lspChecksumPos2); |
| 37 | if (lspPacket[lspChecksumPos1] == checksum[0] && lspPacket[lspChecksumPos2] == checksum[1]) { |
| 38 | return true; |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Calculates the LSP checksum. |
| 46 | * |
| 47 | * @param lspBytes as byte array |
| 48 | * @param lspChecksumPos1 position of checksum bit in packet |
| 49 | * @param lspChecksumPos2 position of checksum bit in packet |
| 50 | * @return checksum bytes |
| 51 | */ |
| 52 | public byte[] calculateLspChecksum(byte[] lspBytes, int lspChecksumPos1, int lspChecksumPos2) { |
| 53 | |
| 54 | byte[] tempLsaByte = Arrays.copyOf(lspBytes, lspBytes.length); |
| 55 | |
| 56 | int[] checksumOut = {0, 0}; |
| 57 | tempLsaByte[lspChecksumPos1] = 0; |
| 58 | tempLsaByte[lspChecksumPos2] = 0; |
| 59 | byte[] byteCheckSum = {0, 0}; |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 60 | for (int i = 12; i < tempLsaByte.length; i++) { |
| 61 | checksumOut[0] = checksumOut[0] + ((int) tempLsaByte[i] & 0xFF); |
| 62 | checksumOut[1] = checksumOut[1] + checksumOut[0]; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 63 | } |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 64 | checksumOut[0] = checksumOut[0] % 255; |
| 65 | checksumOut[1] = checksumOut[1] % 255; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 66 | int byte1 = (int) ((tempLsaByte.length - lspChecksumPos1 - 1) * checksumOut[0] - checksumOut[1]) % 255; |
| 67 | if (byte1 <= 0) { |
| 68 | byte1 += 255; |
| 69 | } |
| 70 | int byte2 = 510 - checksumOut[0] - byte1; |
| 71 | if (byte2 > 255) { |
| 72 | byte2 -= 255; |
| 73 | } |
| 74 | |
| 75 | byteCheckSum[0] = (byte) byte1; |
| 76 | byteCheckSum[1] = (byte) byte2; |
| 77 | |
| 78 | return byteCheckSum; |
| 79 | } |
| 80 | } |