Class IPUtil

java.lang.Object
org.newsclub.net.unix.darwin.system.IPUtil

public final class IPUtil extends Object
Some IP protocol-related helper methods.
Author:
Christian Kohlschütter
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte
    The ICMP protocol.
    static final int
    The identifier for AF_INET (at least on Darwin).
    static final int
    The length (in bytes) of the "domain" header used in loopback packet systems like UTUN_CONTROL.
    static final int
    The length (in bytes) of an IPv4 header without options.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    checksumICMPheader(ByteBuffer bb, int start, int end)
    Computes the checksum for an ICMP header, and overwrites any existing checksum with the correct one.
    static int
    checksumIPv4header(ByteBuffer bb, int start, int end)
    Computes the checksum for an IPv4 header, and overwrites any existing checksum with the correct one.
    static void
    putICMPEchoResponse(ByteBuffer bb, short echoIdentifier, short sequenceNumber, ByteBuffer payload)
    Put (write) an ICMP echo response header to the given byte buffer, using the given parameters.
    static void
    putIPv4Header(ByteBuffer bb, int payloadLength, byte protocol, int srcIP, int dstIP)
    Put (write) an IPv4 header to the given byte buffer, using the given parameters.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DOMAIN_HEADER_LENGTH

      public static final int DOMAIN_HEADER_LENGTH
      The length (in bytes) of the "domain" header used in loopback packet systems like UTUN_CONTROL.
      See Also:
    • DOMAIN_AF_INET

      public static final int DOMAIN_AF_INET
      The identifier for AF_INET (at least on Darwin).
      See Also:
    • IPV4_DEFAULT_HEADER_SIZE

      public static final int IPV4_DEFAULT_HEADER_SIZE
      The length (in bytes) of an IPv4 header without options.
      See Also:
    • AF_INET_PROTOCOL_ICMP

      public static final byte AF_INET_PROTOCOL_ICMP
      The ICMP protocol.
      See Also:
  • Method Details

    • checksumIPv4header

      public static int checksumIPv4header(ByteBuffer bb, int start, int end)
      Computes the checksum for an IPv4 header, and overwrites any existing checksum with the correct one.
      Parameters:
      bb - The buffer containing the IPv4 header
      start - The beginning position of the header in the buffer.
      end - The end position (exclusive) of the header in the buffer.
      Returns:
      The computed 16-bit checksum
    • checksumICMPheader

      public static int checksumICMPheader(ByteBuffer bb, int start, int end)
      Computes the checksum for an ICMP header, and overwrites any existing checksum with the correct one. Also see RFC 792.
      Parameters:
      bb - The buffer containing the ICMP header
      start - The beginning position of the header in the buffer.
      end - The end position (exclusive) of the header in the buffer.
      Returns:
      The computed 16-bit checksum
    • putIPv4Header

      public static void putIPv4Header(ByteBuffer bb, int payloadLength, byte protocol, int srcIP, int dstIP)
      Put (write) an IPv4 header to the given byte buffer, using the given parameters. This should write exactly 20 bytes to the buffer. The buffer position then is at the end of the header.
      Parameters:
      bb - The target byte buffer.
      payloadLength - The length of the payload (excluding the IPv4 header).
      protocol - The protocol identifier.
      srcIP - The source IPv4 address.
      dstIP - The destination IPv4 address.
    • putICMPEchoResponse

      public static void putICMPEchoResponse(ByteBuffer bb, short echoIdentifier, short sequenceNumber, ByteBuffer payload)
      Put (write) an ICMP echo response header to the given byte buffer, using the given parameters.
      Parameters:
      bb - The target byte buffer.
      echoIdentifier - The identifier, from the ICMP echo request.
      sequenceNumber - The sequence number, from the ICMP echo request.
      payload - The payload, from the ICMP echo request.