Java Checksum Calculate checksum_icmpv6(byte pkt[])

Here you can find the source of checksum_icmpv6(byte pkt[])

Description

checksuicmpv

License

Open Source License

Declaration

public static int checksum_icmpv6(byte pkt[]) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    final static int OFS_IP6_PLEN = 4;
    final static int OFS_IP6_PLOAD = 40;
    final static int OFS_ICMP6_CSUM = 40 + 2;

    public static int checksum_icmpv6(byte pkt[]) {
        return checksum_icmpv6(pkt, 0);
    }/*from w  ww  . ja  v  a  2s.c  om*/

    public static int checksum_icmpv6(byte pkt[], int pktofs) {
        int plen = fetch_net16(pkt, pktofs + OFS_IP6_PLEN);
        int nxth = ((int) pkt[pktofs + 6]) & 0xff;
        // Pseudo header is IPv6 src/dst (included with packet) and plen/nxth and zeroes:
        int csum = plen + nxth;
        for (int i = 8; i < OFS_IP6_PLOAD + plen; i += 2) {
            if (i != OFS_ICMP6_CSUM) {
                // Skip current checksum value
                csum += fetch_net16(pkt, pktofs + i);
            }
        }
        // No need to treat a trailing single byte: ICMPv6 has no odd packet lengths
        csum = (csum & 0xffff) + (csum >> 16);
        csum = (csum & 0xffff) + (csum >> 16);
        csum = csum ^ 0xffff; // 1's complement limited to 16 bits
        return csum;
    }

    public static int fetch_net16(byte pkt[], int ofs16) {
        int retval = ((int) pkt[ofs16]) << 8 & 0xff00;
        retval = retval + (((int) pkt[ofs16 + 1]) & 0xff);
        return retval;
    }
}

Related

  1. checksum(File file)
  2. checksum(InputStream is)
  3. checksum(InputStream is)
  4. checksum(String filename)
  5. checksum(String nmea)
  6. checksumBytesToString(byte[] digestBytes)
  7. checksumFile(File file)
  8. checksumLength(int algorithmCode, int dataLength)
  9. createChecksum(File file, String shaAlgo)