icmpheader.java

来自「This a java ICMP JNI interface」· Java 代码 · 共 566 行 · 第 1/2 页

JAVA
566
字号
     * decoded.     * </P>     *      * <P>     * If there is an insufficent amount of data to decode a header then an     * exception is thrown.     * </P>     *      * @param data     *            The data buffer containing the header     * @param offset     *            The offset of the header in the buffer     *      * @exception java.lang.IndexOutOfBoundsException     *                Thrown if there is not enough data to construct the     *                header.     *      */    public ICMPHeader(byte[] data, int offset) {        if ((data.length - offset) < getNetworkSize())            throw new IndexOutOfBoundsException("Insufficient number of bytes available to construct the ICMP header");        m_type = data[offset++];        m_code = data[offset++];        m_checksum = (short) (byteToShort(data[offset++]) << 8 | byteToShort(data[offset++]));        m_sequence = (short) (byteToShort(data[offset++]) << 8 | byteToShort(data[offset++]));        m_ident = (short) (byteToShort(data[offset++]) << 8 | byteToShort(data[offset++]));    }    /**     * Returns the 8-bit type code for the ICMP packet.     *      * @return The ICMP type.     */    public final byte getType() {        return m_type;    }    /**     * Sets the 8-bit type code for the packet.     *      * @param type     *            The new ICMP type.     *      */    protected void setType(byte type) {        m_type = type;    }    /**     * Returns the 8-bit code for the ICMP packet.     *      * @return The ICMP code.     *      */    public final byte getCode() {        return m_code;    }    /**     * Sets the 8-bit code for the ICMP packet     *      * @param code     *            The new ICMP code.     *      */    public final void setCode(byte code) {        m_code = code;    }    /**     * Returns the sequence identifier for the ICMP header.     *      * @return The 16-bit sequence identifier.     *      */    public final short getSequenceId() {        return m_sequence;    }    /**     * Gets the next global identifier and sets the value in the object. In     * addition the new sequence identifier is returned.     *      * @return The new 16-bit sequence identifier.     *      */    public final short setNextSequenceId() {        m_sequence = nextSequenceId();        return m_sequence;    }    /**     * Sets the headers 16-bit sequence identifier.     *      * @param id     *            The new 16-bit sequence id.     *      */    public final void setSequenceId(short id) {        m_sequence = id;    }    /**     * Used to get the headers user defined identity.     *      * @return The 16-bit identity.     *      */    public final short getIdentity() {        return m_ident;    }    /**     * Sets the header's 16-bit user defined identity value.     *      * @param identity     *            The header's new identity.     *      */    public final void setIdentity(short identity) {        m_ident = identity;    }    /**     * Used to retrieve the current checksum for the header. This is the last     * checksum computed for the header (or it's derived classes). To compute     * the new checksum a call to the method computeChecksum() must be called.     *      * @return The 16-bit one's compliment checksum.     *      */    public final short getChecksum() {        return m_checksum;    }    protected void setChecksum(short sum) {        m_checksum = sum;    }    /**     * Provides the default checksum implementation for the ICMP header. It MUST     * be overriden by any derived classes to ensure that the checksum includes     * the dervived class data. Once the checksum is calculated, a call to the     * method getChecksum() will return the calculated sum.     */    public void computeChecksum() {        OC16ChecksumProducer summer = new OC16ChecksumProducer();        computeChecksum(summer);        m_checksum = summer.getChecksum();    }    /**     * Used by derived classes to begin the checksum process. The process     * involves setting the checksum to zero and then performing the checksum     * over the various values.     *      * @param summer     *            The checksum builder object.     */    protected void computeChecksum(OC16ChecksumProducer summer) {        summer.reset();        summer.add(m_type, m_code);        // adding zero has should no effect....        // m_checksum = 0;        summer.add((short) 0);        summer.add(m_sequence);        summer.add(m_ident);    }    /**     * Writes the ICMP header out to the specified buffer at the starting     * offset. If the buffer does not have sufficent data to store the     * information then an IndexOutOfBoundsException is thrown.     *      * @param buf     *            The storage buffer.     * @param offset     *            The location to start in buf.     *      * @return The new offset after storing to the buffer.     *      * @exception IndexOutOfBoundsException     *                Thrown if the buffer does not have enough storage space.     *      */    protected int storeToBuffer(byte[] buf, int offset) {        if (buf.length < (offset + 8))            throw new IndexOutOfBoundsException("Array index overflow in buffer");        buf[offset++] = (byte) (m_type);        buf[offset++] = (byte) (m_code);        buf[offset++] = (byte) ((m_checksum >>> 8) & 0xff);        buf[offset++] = (byte) (m_checksum & 0xff);        buf[offset++] = (byte) ((m_ident >>> 8) & 0xff);        buf[offset++] = (byte) (m_ident & 0xff);        buf[offset++] = (byte) ((m_sequence >>> 8) & 0xff);        buf[offset++] = (byte) (m_sequence & 0xff);        return offset;    }    /**     * Reads the ICMP header from the specified buffer and sets the internal     * fields equal to the data. If the buffer does not have sufficent data to     * restore the header then an IndexOutOfBoundsExceptoin is thrown by the     * method.     *      * @param buf     *            The buffer to read the data from.     * @param offset     *            The offset to start reading data.     *      * @return The new offset after reading the data.     *      * @exception IndexOutOfBoundsException     *                is thrown if there is not sufficent data in the buffer.     *      */    protected int loadFromBuffer(byte[] buf, int offset) {        if (buf.length < (offset + 8))            throw new IndexOutOfBoundsException("Insufficient data to load ICMP header");        m_type = (byte) (buf[offset++]);        m_code = (byte) (buf[offset++]);        m_checksum = (short) (byteToShort(buf[offset++]) << 8 | byteToShort(buf[offset++]));        m_ident = (short) (byteToShort(buf[offset++]) << 8 | byteToShort(buf[offset++]));        m_sequence = (short) (byteToShort(buf[offset++]) << 8 | byteToShort(buf[offset++]));        return offset;    }    /**     * Returns the number of bytes required to read/write an icmp header. A     * header is composed of a fixed size number of byte. This is not expected     * to change, but this method allows derived classes to query the number of     * bytes for reading/writing an icmp header. Thus should the standard ever     * change, the derived class should be able to dynamically handle the     * change.     *      * @return The number of bytes required to read/write an icmp header.     *      */    public static int getNetworkSize() {        return 8;    }    /**     * Used to test to see if the header is an echo reply message. If it is an     * echo reply message then true is returned.     *      * @return True if the header marks an echo reply.     *      */    public final boolean isEchoReply() {        return (m_type == TYPE_ECHO_REPLY);    }    /**     * Used to test to see if the header is an echo request message. If it is an     * echo request message then true is returned.     *      * @return True if the header marks an echo request.     *      */    public final boolean isEchoRequest() {        return (m_type == TYPE_ECHO_REQUEST);    }    /**     * Converts the object to a string of bytes.     */    public byte[] toBytes() {        byte[] b = new byte[8];        storeToBuffer(b, 0);        return b;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?