📄 ipheader.java
字号:
//// 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.//// 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.ip;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * This class defines a loaded IP header object. It does not allow the user of * the class to set any of the values. Nor can a default object be constructed. * A copy of an existing header can be created or one can be loaded from a * collection of bytes. * * For more information on the IP header see the book "TCP/IP Illustrated, * Volume 1: The Protocols" by W. Richard Stevens. * * @author <a href="mailto:weave@oculan.com">Brian Weaver </a> * */public class IPHeader extends Object { /** * The supported version of the IP header */ public static final int IP_VERSION = 4; /** * The Type-Of-Service mask. This constant is used to mask bits that define * the type of service field. See RFC 791. */ public static final int TOS_PRECEDENCE_MASK = 0xe0; /** * Network Critical TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_NETWORK_CRITICAL = 0xe0; /** * Internetworking Control TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_INTERNETWORK_CONTROL = 0xc0; /** * Critical/ECP TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_CRITICAL_ECP = 0x90; /** * Flash Override TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_FLASH_OVERRIDE = 0x80; /** * Flash TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_FLASH = 0x60; /** * Immediate TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_IMMEDIATE = 0x40; /** * Priority TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_PRIORITY = 0x20; /** * Routine TOS. See RFC 791. */ public static final int TOS_PRECEDENCE_ROUTINE = 0x00; /** * TOS delay mask as defined by RFC 791. */ public static final int TOS_DELAY_MASK = 0x10; /** * Minimize the delay when handling packets. */ public static final int TOS_DELAY_LOW = 0x10; /** * Normal packet handling */ public static final int TOS_DELAY_NORMAL = 0x00; /** * TOS Throughput mask */ public static final int TOS_THROUGHPUT_MASK = 0x08; /** * High throughput requested */ public static final int TOS_THROUGHPUT_HIGH = 0x08; /** * Normal throughput requested */ public static final int TOS_THROUGHPUT_NORMAL = 0x00; /** * Packet reliablity mask. */ public static final int TOS_RELIBILITY_MASK = 0x04; /** * High Reliability requested. */ public static final int TOS_RELIBILITY_HIGH = 0x04; /** * Normal reliability requrested */ public static final int TOS_RELIBILITY_NORMAL = 0x00; /** * Mask of the reseered bits. */ public static final int TOS_RESERVED_MASK = 0x03; /** * The mask of the flags in the fragment field of the IP header */ public static final int FLAGS_MASK = 0xe000; /** * Don't fragment datagrams field */ public static final int FLAGS_DONT_FRAGMENT = 0x4000; /** * More fragments are necessary to reassemble this packet */ public static final int FLAGS_MORE_FRAGMENTS = 0x2000; /** * The bit(s) that define if the optiosn are copied to each datagram when * (or if) it is fragmented. */ public static final int OPTION_COPY_MASK = 0x80; /** * The option class mask */ public static final int OPTION_CLASS_MASK = 0x60; /** * The option number mask */ public static final int OPTION_NUMBER_MASK = 0x1f; /** * Option identifier for the End Of Options List option. */ public static final int OPTION_ID_EOO = 0x00; /** * Option identifier for the loose source routing option */ public static final int OPTION_ID_LOOSE_SOURCE_ROUTING = 0x83; /** * Option identifer for the the strict source routing option */ public static final int OPTION_ID_STRICT_SOURCE_ROUTING = 0x89; /** * Option identifier for the route record option */ public static final int OPTION_ID_ROUTE_RECORD = 0x07; /** * The IP version */ private byte m_version; // 4-bit value /** * The length of the IP header in 32-bit words */ private byte m_hdrlen; // 4-bit value /** * the Type-Of-Service defined for the IP datagram */ private byte m_tos; // 8-bit type of service /** * The total length of the IP datagram and it's payload */ private short m_length; // 16-bit total length of IP Packet /** * The identity of the IP dagagram. */ private short m_identity; // 16-bit identification /** * The fragmentation flags tha occupy the upper 3 bits of the fragment * offset field */ private byte m_flags; // 3-bit flags /** * The fragment offset of this packet */ private short m_fragOffset; // 13-bit fragment offset /** * The Time-To-Live for this IP packet */ private byte m_ttl; // 8-bit time-to-live /** * The protocol encapuslated by this packet */ private byte m_protocol; // 8-bit protocol /** * One's compliment 16-bit checksum of the header only. This does not * include the value for the data */ private short m_checksum; // 16-bit one's compliment checksum /** * Source address of the IP datagram */ private int m_srcAddr; // 32-bit source address /** * Destination address of the IP datagram */ private int m_dstAddr; // 32-bit destination address /** * any option data in the datagram */ private byte[] m_options; // maximum of 40-bytes /** * The Option class is used as the base class for any options that are at * the end of the IP header. * * @author <A HREF="mailto:weave@oculan.com">Brian Weaver </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */ public abstract static class Option { /** * The single byte that defiend the copied bit, class, and code for the * option */ protected int m_code; /** * Defines the code for the End-Of-Options list */ public static final int CODE_END_OF_OPTION_LIST = 0; /** * Defines the code for the loose source routing option */ public static final int CODE_LOOSE_SOURCE_ROUTE = 0x83; /** * Defines the code for the strict soruce routing option */ public static final int CODE_STRICT_SOURCE_ROUTE = 0x89; /** * Defines the code for the packet route record option. */ public static final int CODE_ROUTE_RECORD = 0x07; /** * Class constructor that is only available to the derived classes of * the Option class. * * @param code * The code for the option. */ protected Option(byte code) { m_code = (int) code & 0x000000ff; } /** * The nubmer of bytes required to represent this option in the IP * header * * @return The bytes used by this option * */ abstract int bytesRequired(); /** * Writes the option to the passed array, starting at the defined * offset. The array must have enough space or an exception is * generated. * * @param dest * The destination to write the data * @param offset * The offset of the first written byte * * @return The passed offset plus the number of required bytes. * */ abstract int writeBytes(byte[] dest, int offset); /** * Returns the class for the option. * */ public int getOptionClass() { return (int) m_code & OPTION_CLASS_MASK; } /** * Returns the option number for the instance */ public int getOptionNumber() { return (int) m_code & OPTION_NUMBER_MASK; } /** * Returns true if the copy flag is set in the options header */ public boolean isOptionCopied() { return ((m_code & OPTION_COPY_MASK) != 0); } } /** * This class is used to represent the <EM>End-Of-Option</EM> list in the * IP header. After this option, the option list is not processed any * further. * * @author <A HREF="mailto:weave@oculan.com">Brian Weaver </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */ public static final class EndOfOptions extends Option { /** * Returns the number of bytes requried to represent this option */ int bytesRequired() { return 1; } /** * Converts the option to an array of bytes and writes those bytes in to * the destiantion buffer. The bytes are written startint at the offset * passed to the method. * * @param dest * The destiantion buffer to write the bytes * @param offset * The offset to start writing in the buffer * * @return The offset plus the number of bytes written to the buffer. * * @exception java.lang.ArrayIndexOutOfBounds * Throws in there is insufficient space in the buffer. * */ int writeBytes(byte[] dest, int offset) { dest[offset++] = 0; return offset; } /** * Constructs a new End-Of-Options list instance that can be added or * found in the IP header. */ public EndOfOptions() { super((byte) 0); } } /** * This class represents routing options that may be part of an IP header. * The route defines a set of IP addresses that a packet may have or should * pass though. * * @author <A HREF="mailto:weave@oculan.com">Brian Weaver </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */ public static class RouteOption extends Option { /** * The list of addresses for the packet to hit on it's way to it's * destination */ protected List<IPv4Address> m_addrs; /** * Adds an address to the end of the set of addresses to hit on its lan * trip * * @param addr * The address to add to the loose source route * * @exception java.lang.IndexOutOfBoundsException * Thrown if the address list is full */ void add(IPv4Address addr) { if (m_addrs.size() == 9) throw new IndexOutOfBoundsException("The address could not be added, the record is full"); m_addrs.add(addr); } /** * The number of bytes required to represent this option in an IP header */ int bytesRequired() { return 3 + (4 * m_addrs.size()); } /** * This method is used to serialized the data contained in the option to * the passed array, starting at the offset passed. If an insufficient * amount of space exists then an exception is thrown. * * @param dest * The destination buffer * @param offset * The offset to start writing data * * @return The new offset after writing data * * @exception java.lang.ArrayIndexOutOfBounds * Thrown if there is not sufficent space in the passed * buffer. */ int writeBytes(byte[] dest, int offset) { dest[offset++] = (byte) m_code; dest[offset++] = (byte) bytesRequired(); dest[offset++] = (byte) 4; Iterator<IPv4Address> iter = m_addrs.iterator(); while (iter.hasNext()) { int addr = ((IPv4Address) iter.next()).getAddress(); for (int i = 3; i >= 0; i++) dest[offset++] = (byte) ((addr >> (8 * i)) & 0xff); } return offset; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -