icmpheader.java

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

JAVA
566
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// 2003 Mar 05: Changes to support response times and more platforms.//// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///// Tab Size = 8//package org.opennms.protocols.icmp;import java.util.Date;import java.util.Random;import org.opennms.protocols.ip.OC16ChecksumProducer;/** * This class defines Internet Control Message Protocol header. The header * defines the first 8 bytes of any ICMP message. Some ICMP messages may need to * override the format of the second 4 bytes, the first 4 bytes are fixed by the * RFC. The minimum message size for any ICMP message is 8 bytes. *  * @author Brian Weaver <weave@oculan.com> * @version 0.1 *  */public class ICMPHeader extends Object {    public static final byte TYPE_ECHO_REPLY = (byte) 0;    public static final byte TYPE_DESTINATION_UNREACHABLE = (byte) 3;    public static final byte CODE_NETWORK_UNREACHABLE = (byte) 0;    public static final byte CODE_HOST_UNREACHABLE = (byte) 1;    public static final byte CODE_PROTOCOL_UNREACHABLE = (byte) 2;    public static final byte CODE_PORT_UNREACHABLE = (byte) 3;    public static final byte CODE_FRAGMENTATION_NEEDED = (byte) 4;    public static final byte CODE_SOURCE_ROUTE_FAILED = (byte) 5;    public static final byte CODE_DESTINATION_NETWORK_UNKNOWN = (byte) 6;    public static final byte CODE_DESTINATION_HOST_UNKNOWN = (byte) 7;    public static final byte CODE_SOURCE_HOST_ISOLATED = (byte) 8;    public static final byte CODE_DESTINATION_NETWORK_ADMIN_PROHIBITED = (byte) 9;    public static final byte CODE_DESTINATION_HOST_ADMIN_PROHIBITED = (byte) 10;    public static final byte CODE_NETWORK_UNREACHABLE_FOR_TOS = (byte) 11;    public static final byte CODE_HOST_UNREACHABLE_FOR_TOS = (byte) 12;    public static final byte CODE_COMMUNICATIONS_ADMIN_PROHIBITIED = (byte) 13;    public static final byte CODE_HOST_PRECEDENCE_VIOLATION = (byte) 14;    public static final byte CODE_PRECEDENCE_CUTOFF_IN_EFFECT = (byte) 15;    public static final byte TYPE_SOURCE_QUENCH = (byte) 4;    public static final byte TYPE_REDIRECT = (byte) 5;    public static final byte CODE_REDIRECT_FOR_NETWORK = (byte) 0;    public static final byte CODE_REDIRECT_FOR_HOST = (byte) 1;    public static final byte CODE_REDIRECT_FOR_TYPE_OF_SERVICE_AND_NETWORK = (byte) 2;    public static final byte CODE_REDIRECT_FOR_TYPE_OF_SERVICE_AND_HOST = (byte) 3;    public static final byte TYPE_ECHO_REQUEST = (byte) 8;    public static final byte TYPE_ROUTER_ADVERTISEMENT = (byte) 9;    public static final byte TYPE_ROUTER_SOLICITATION = (byte) 10;    public static final byte TYPE_TIME_EXCEEDED = (byte) 11;    public static final byte CODE_TTL_EQ_ZERO_IN_TRANSIT = (byte) 0;    public static final byte CODE_TTL_EQ_ZERO_IN_REASSEMBLY = (byte) 1;    public static final byte TYPE_PARAMETER_PROBLEM = (byte) 12;    public static final byte CODE_BAD_IP_HEADER = (byte) 0;    public static final byte CODE_REQUIRED_OPTION_MISSING = (byte) 1;    public static final byte TYPE_TIMESTAMP_REQUEST = (byte) 13;    public static final byte TYPE_TIMESTAMP_REPLY = (byte) 14;    public static final byte TYPE_INFORMATION_REQUEST = (byte) 15;    public static final byte TYPE_INFORMATION_REPLY = (byte) 16;    public static final byte TYPE_ADDRESS_MASK_REQUEST = (byte) 17;    public static final byte TYPE_ADDRESS_MASK_REPLY = (byte) 18;    private byte m_type; // ECHO_REQUEST, ECHO_REPLY, etc...    private byte m_code; // echo code    private short m_checksum; // 16-bit one's complement checksum    private short m_ident; // 16-bit identity    private short m_sequence; // 16-bit sequence    /**     * Used to generate the sequence numbers for the class. This number is     * generated application wide.     *      */    private static short sm_seq = 0;    /**     * Returns the next 16-bit sequence identifier for the class. The method is     * synchronized to prevent duplicate identifiers from being issued.     * depending on the number of classes and how often the method is called it     * will eventually wrap. When the value wraps back to zero, a random number     * is generated and may cause a collision with an existing identifier. The     * probability is low, but possible.     *      * @return The next 16-bit sequence number, may be negative.     */    public final static synchronized short nextSequenceId() {        if (sm_seq == 0) {            Date d = new Date();            Random r = new Random(d.getTime());            sm_seq = (short) (r.nextInt());        }        return ++sm_seq;    }    /**     * Converts a byte to a short.     *      * @param b     *            The byte to convert.     *      * @return The converted byte.     *      */    protected static short byteToShort(byte b) {        short s = (short) b;        if (s < 0)            s += 256;        return s;    }    /**     * Converts a byte to an integer.     *      * @param b     *            The byte to convert.     *      * @return The converted byte.     *      */    protected static int byteToInt(byte b) {        int i = (int) b;        if (i < 0)            i += 256;        return i;    }    /**     * Initializes the header to a default value.     */    public ICMPHeader() {        m_type = 0;        m_code = 0;        m_checksum = 0;        m_ident = 0;        m_sequence = 0;    }    /**     * Initializes the header using the specified type.     *      * @param type     *            The header type.     *      */    public ICMPHeader(byte type) {        this();        m_type = type;    }    /**     * Initializes the header with the specified type and code values.     *      * @param type     *            The type value for the header     * @param code     *            The code value for the header     *      */    public ICMPHeader(byte type, byte code) {        this(type);        m_code = code;    }    /**     * Constructs an ICMP header with the specified header fields.     *      * @param type     *            The 8-bit ICMP type.     * @param code     *            The 8-bit ICMP code.     * @param checksum     *            The 16-bit checksum header.     * @param identity     *            The 16-bit identity (user).     * @param sequence     *            The 16-bit sequence id.     *      */    public ICMPHeader(byte type, byte code, short checksum, short identity, short sequence) {        m_type = type;        m_code = code;        m_checksum = checksum;        m_ident = identity;        m_sequence = sequence;    }    /**     * Constructs a duplicate ICMP header that is identical to the passed     * ICMPHeader object.     *      * @param second     *            The object to duplicate.     *      */    public ICMPHeader(ICMPHeader second) {        m_type = second.m_type;        m_code = second.m_code;        m_checksum = second.m_checksum;        m_sequence = second.m_sequence;        m_ident = second.m_ident;    }    /**     * <P>     * Constructs a new ICMP header based upon the data contained in the buffer.     * The buffer is decode in network byte ordering (big-endin) and must be at     * least a {@link #getNetworkSize minimum}number of bytes available to be

⌨️ 快捷键说明

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