📄 ipheader.java
字号:
* Constructs a new, empty instance of the class. */ RouteOption(byte code) { super(code); m_addrs = new ArrayList<IPv4Address>(9); } /** * Constructs a new instance of the class with the passed addresses used * for the routing. If the set of addresses is larger than the option * can hold an exception is thrown. * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ RouteOption(byte code, IPv4Address[] addrs) { super(code); if (addrs.length > 9) throw new IndexOutOfBoundsException("Route Option List Cannot Exceed 9 Addresses"); m_addrs = new ArrayList<IPv4Address>(9); for (int i = 0; i < addrs.length; i++) m_addrs.add(addrs[i]); } /** * Constructs a new instance of the class with the passed addresses used * for the routing. If the set of addresses is larger than the option * can hold an exception is thrown. * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ RouteOption(byte code, List<IPv4Address> addrs) { super(code); if (addrs.size() > 9) throw new IndexOutOfBoundsException("Route Option List Cannot Exceed 9 Addresses"); Iterator<IPv4Address> iter = addrs.iterator(); m_addrs = new ArrayList<IPv4Address>(9); while (iter.hasNext()) m_addrs.add(iter.next()); } /** * Returns the iterator that may be used to look at the encapsulated * addresses. The class IPv4Address is used to represent the addresses * in the list. * * @return An iterator that can be used to operate on the list. */ public Iterator<IPv4Address> iterator() { return m_addrs.iterator(); } /** * Returns the number of addresses contained in the option list. */ public int size() { return m_addrs.size(); } } /** * This class represents the loose source routing options that may be part * of an IP header. The loose source route defines a set of IP addresses * that a packet should pass though. As the packet reaches each address the * packet is forwarded to the next element in the route. * * @author <A HREF="mailto:weave@oculan.com">Brian Weaver </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */ public static final class LooseSourceRouteOption extends RouteOption { /** * Constructs a new, empty instance of the class. */ LooseSourceRouteOption() { super((byte) 0x83); } /** * Constructs a new instance of the class with the passed addresses used * for the routing. If the set of addresses is larger than the option * can hold an exception is thrown. * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ public LooseSourceRouteOption(IPv4Address[] addrs) { super((byte) 0x83, addrs); } /** * Constructs a new instance of the class with the passed addresses used * for the routing. If the set of addresses is larger than the option * can hold an exception is thrown. * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ public LooseSourceRouteOption(List<IPv4Address> addrs) { super((byte) 0x83, addrs); } } /** * This class represents the strict source routing options that may be part * of an IP header. The strict source route defines a set of IP addresses * that a packet must pass though. As the packet reaches each address the * packet is forwarded to the next element in the route. * * @author <A HREF="mailto:weave@oculan.com">Brian Weaver </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */ public static final class StrictSourceRouteOption extends RouteOption { /** * Constructs an empty instance of this class */ StrictSourceRouteOption() { super((byte) 0x89); } /** * Constructs a new instance of the class with the passed addresses used * for the routing. If the set of addresses is larger than the option * can hold an exception is thrown. * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ public StrictSourceRouteOption(IPv4Address[] addrs) { super((byte) 0x89, addrs); } /** * Constructs a new instance of the class with the passed addresses used * for the routing. If the set of addresses is larger than the option * can hold an exception is thrown. * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ public StrictSourceRouteOption(List<IPv4Address> addrs) { super((byte) 0x89, addrs); } } /** * This class represents the route record option that may be part of an IP * header. The strict route record records a set of IP addresses that a * packet has passed though. As the packet reaches each address the address * is added to the the next element in the route. * * @author <A HREF="mailto:weave@oculan.com">Brian Weaver </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */ public static final class RouteRecordOption extends RouteOption { /** * Constructs an empty route record option */ RouteRecordOption() { super((byte) 0x7); } /** * Constructs an empty route record with space for <EM>capacity</EM> * addresses to be recoreded. The capacity CANNOT exceed 9. * * @param capacity * The number of addresses to record, max = 9. * */ public RouteRecordOption(int capacity) { super((byte) 0x7); for (int i = 0; i < capacity; i++) add(new IPv4Address(0)); } /** * Constructs a new instance with the give addresses set in the option * header * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ public RouteRecordOption(IPv4Address[] addrs) { super((byte) 0x7, addrs); } /** * Constructs a new instance with the given addresses stored in the * option. * * @param addrs * The list of addresses for the loose source route. * * @exception java.lang.IndexOutOfBoundsException * Thrown if the number of addresses is to large for the * option */ public RouteRecordOption(List<IPv4Address> addrs) { super((byte) 0x7, addrs); } } /** * Duplicates the array of bytes. * * @param src * The source bytes to duplicate. * * @return The duplicated array of bytes. */ private byte[] dup(byte[] src) { byte[] cpy = null; if (src != null) { cpy = new byte[src.length]; System.arraycopy(src, 0, cpy, 0, src.length); } return cpy; } /** * Converts a byte to a short, treating the byte as unsigned. * * @param b * The byte to convert. * * @return The converted value. * */ private static short byteToShort(byte b) { short r = (short) b; if (r < 0) r += 256; return r; } /** * Converts a byte to an integer, treating the byte as unsigned. * * @param b * The byte to convert. * * @return The converted value. * */ private static int byteToInt(byte b) { int r = (int) b; if (r < 0) r += 256; return r; } /** * Converts a short to an integer, treating the short as unsigned. * * @param s * The short to convert. * * @return The converted value * */ private static int shortToInt(short s) { int r = (int) s; if (r < 0) r += 0x10000; return r; } /** * Constructs a basic IP header, but the header <EM>is not</EM> valid * until a large part of the information is configured. */ public IPHeader() { m_hdrlen = 5; m_version = IP_VERSION; m_tos = 0; m_length = 20; m_identity = 0; m_flags = 0; m_fragOffset = 0; m_ttl = 30; m_protocol = 0; m_checksum = 0; m_srcAddr = 0; m_dstAddr = 0; m_options = new byte[0]; } /** * Constructs a new IP Header object that is identical to the passed * IPHeader. The new object is a complete duplicate including the option * data that is copied into a newly allocated array. * * @param second * The object to duplicate. * */ public IPHeader(IPHeader second) { m_hdrlen = second.m_hdrlen; m_version = second.m_version; m_tos = second.m_tos; m_length = second.m_length; m_identity = second.m_identity; m_flags = second.m_flags; m_fragOffset = second.m_fragOffset; m_ttl = second.m_ttl; m_protocol = second.m_protocol; m_checksum = second.m_checksum; m_srcAddr = second.m_srcAddr; m_dstAddr = second.m_dstAddr; m_options = dup(second.m_options); } /** * Constructs a new IPHeader object from the passed data buffer. The data is * gathered from the buffer starting at the location marked by offset. If * there is not sufficent data in the buffer then an exception is thrown. * * @param header * The buffer containing the header * @param offset * The offset into the buffer where the IP header is located. * * @exception java.lang.IndexOutOfBoundsException * This exception is thrown if the minimum number of bytes * are not present to represent an IPHeader object. * @exception UnknownIPVersionException * Thrown if the format of the version is unknown. * */ public IPHeader(byte[] header, int offset) { int length = header.length; if ((length - offset) < 20) throw new IndexOutOfBoundsException("Minimum IP header size is 20 bytes"); // // now start to build the header information // int ndx = 0; // // Get the header version and header length. The // header version lives in the first 4 bits of the // header. The header length lives in the next 4 bits. // NOTE: The header length is the number of 32-bit // words in the header, so the true length is m_hdrlen * 4. // m_version = (byte) (header[offset + ndx] >>> 4); m_hdrlen = (byte) (header[offset + ndx] & 0xf); ++ndx; // // check the version number // if (m_version != 4) throw new UnknownIPVersionException("Unknown IP Version, version = " + m_version); // // check to make sure there is enough data now that // we know the total length of the header // if ((length - offset) < (m_hdrlen * 4)) throw new IndexOutOfBoundsException("Insufficient data: buffer size = " + (length - offset) + " and header length = " + (m_hdrlen * 4)); // // Now get the Type Of Service flags (8-bits) // m_tos = header[offset + ndx]; ++ndx; // // Convert the 16-bit total length of the packet // in bytes. // m_length = (short) (byteToShort(header[offset + ndx]) << 8 | byteToShort(header[offset + ndx + 1])); ndx += 2; // // Next get the 16-bit identification field // m_identity = (short) (byteToShort(header[offset + ndx]) << 8 | byteToShort(header[offset + ndx + 1])); ndx += 2; // // Get the next 16-bits of information. The upper 3-bits // are the header flags. The lower 13-bits is the // fragment offset! // m_fragOffset = (short) (byteToShort(header[offset + ndx]) << 8 | byteToShort(header[offset + ndx + 1])); m_flags = (byte) (m_fragOffset >>> 13); // get the upper 3-bits m_fragOffset = (short) (m_fragOffset & 0x1fff); // mask off the upper // 3-bits ndx += 2; // // The 8-bit Time To Live (TTL) is next // m_ttl = header[offset + ndx]; ++ndx; // // The 8-bit protocol is next. This is used by the // OS to determine if the packet is TCP, UDP, etc al. // m_protocol = header[offset + ndx]; ++ndx; // // Now get the 16-bit one's compliment checksum // m_checksum = (short) (byteToShort(header[offset + ndx]) << 8 | byteToShort(header[offset + ndx + 1])); ndx += 2; // // The 32-bit IPv4 source address is next. This is the // address of the sender of the packet. // m_srcAddr = byteToInt(header[offset + ndx]) << 24 | byteToInt(header[offset + ndx + 1]) << 16 | byteToInt(header[offset + ndx + 2]) << 8 | byteToInt(header[offset + ndx + 3]); ndx += 4; // // The 32-bit IPv4 destination address. This is the address // of the interface that should receive the packet. // m_dstAddr = byteToInt(header[offset + ndx]) << 24 | byteToInt(header[offset + ndx + 1]) << 16 | byteToInt(header[offset + ndx + 2]) << 8 | byteToInt(header[offset + ndx + 3]); ndx += 4; // // get the option data now // int hl = byteToInt(m_hdrlen) << 2; // m_hdrlen * 4 ! :) if (hl > ndx) { m_options = new byte[hl - ndx]; int x = 0; while (ndx < hl) { m_options[x++] = header[offset + ndx++]; } } else { m_options = new byte[0]; } } // end IPHeader(byte[], int) /** * Used to retreive the current version of the IP Header. Currently only * version 4 is supported. * * @return The current IP version. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -