📄 ipheader.java
字号:
*/ public byte getVersion() { return m_version; } /** * Used to get the current length of the IP Header. * * @return The current IP header length. * */ public int getHeaderLength() { return (4 * byteToInt(m_hdrlen)); } /** * Retreives the current TOS field from the header. * * @return The current TOS. * */ public byte getTypeOfService() { return m_tos; } /** * Sets the TOS flags for the IP header. * * @param tos * The new TOS for the IP header */ public void setTypeOfService(byte tos) { m_tos = tos; } /** * Use to test individual bits in the TOS fields. If the field is set then a * value of true is returned. If the field is not set then a false value is * returned. * * @param bit * The bit to validate. Valid values are 0 - 7. * * @return True if the bit is set, false otherwise. * */ public boolean getTypeOfService(int bit) { if (bit >= 0 && bit < 8) return ((m_tos & (1 << bit)) != 0); return false; } /** * Returns the length of the IP packet, including the header, in bytes. * * @return The total packet length * */ public int getPacketLength() { return shortToInt(m_length); } /** * Sets the length for IP packet, including the header. When setting this * value the size of the IP header must be accounted for. * * @param length * The length of the IP header plus the data contained within */ public void setPacketLength(short length) { m_length = length; } /** * Used to retreive the 16-bit identity of the header. * * @return The header's identity. * */ public short getIdentity() { return m_identity; } /** * Sets the identity of the IP header * * @param ident * The new identity of the IP header */ public void setIdentity(short ident) { m_identity = ident; } /** * Used to get the 3-bit flags from the header. The flags are located in the * 3 least significant bits of the returned byte. * * @return The byte containing the three flags. * */ public byte getFlags() { return m_flags; } /** * Sets the flags contained in the upper 3 bits of the short value for the * fragmentation offset. The passed bits should occupy the lower 3 bits of * the passed byte. * * @param flags * The flag bits, set in the lower 3 bits of the value. */ public void setFlags(byte flags) { m_flags = flags; } /** * Used to get an individual flag from the flags field. The bit must be in * the range of [0..3). * * @param bit * The flag to retreive. * * @return True if the bit is set, false otherwise. * */ public boolean getFlag(int bit) { if (bit >= 0 && bit < 3) return ((m_flags & (1 << bit)) != 0); return false; } /** * Returns the 13-bit fragment offset field from the IP header. * * @return The 13-bit fragment offset. * */ public short getFragmentOffset() { return m_fragOffset; } /** * Sets the fragmentation index for this packet */ public void setFragmentOffset(short offset) { m_fragOffset = offset; } /** * Gets the 8-bit Time To Live (TTL) of the packet. * * @return The packet's ttl. * */ public byte getTTL() { return m_ttl; } /** * Sets the time to live for the IP header */ public void setTTL(byte ttl) { m_ttl = ttl; } /** * Gets the protocol for the IP datagram. * * @return The 8-bit protocol field. * */ public byte getProtocol() { return m_protocol; } /** * Sets the protocol for the IP header. * * @param protocol * The IP protocol. */ public void setProtocol(byte protocol) { m_protocol = protocol; } /** * Gets the 16-bit ones compliment checksum for the IP header. * * @return The 16-bit ones compliment checksum. */ public short getChecksum() { return m_checksum; } /** * Sets the checksum for the IP header. * * @param sum * The IP header checksum. * */ public void setChecksum(short sum) { m_checksum = sum; } /** * Returns the dotted decimal string address of the source IP address. * * @return The 32-bit IPv4 address */ public int getSourceAddress() { return m_srcAddr; } /** * Sets the IP headers source address. * * @param addr * The soruce address for the header. */ public void setSourceAddr(int addr) { m_srcAddr = addr; } /** * Returns the dotted decimal string address of the destination IP address. * * @return The 32-bit IPv4 address. */ public int getDestinationAddress() { return m_dstAddr; } /** * Sets the IP headers destination address. * * @param addr * The destination address * */ public void setDestinationAddress(int addr) { m_dstAddr = addr; } /** * Retrieves the IP header options from the header. The data is treated as a * varaiable length of option data. The IPHeader object does not attempt to * interpert the data. * * @return The IP header option data, null if there is none. */ public byte[] getOptionData() { return m_options; } /** * Sets the current option data for the header. * * @param options * The new options data. * */ public void setOptionData(byte[] options) { m_options = options; m_hdrlen = (byte) ((20 + m_options.length) / 4); } /** * Returns a list of options that are associated with the IP header. * * @return The list of current options. */ public List<Option> getOptions() throws InstantiationException { // // check for null data first // if (m_options == null) return new ArrayList<Option>(); // // Process the options // List<Option> options = new ArrayList<Option>(); int offset = 0; while (offset < m_options.length) { switch ((int) m_options[offset++] & 0xff) { case Option.CODE_END_OF_OPTION_LIST: options.add(new EndOfOptions()); break; case Option.CODE_LOOSE_SOURCE_ROUTE: { LooseSourceRouteOption opt = new LooseSourceRouteOption(); int addrs = ((int) m_options[offset] & 0xff) - 3; offset += 2; for (int i = 0; i < addrs / 4; i++) { int ip = 0; for (int j = 0; j < 4; j++) ip = ip << 8 + ((int) m_options[offset++] & 0xff); opt.add(new IPv4Address(ip)); } options.add(opt); } break; case Option.CODE_STRICT_SOURCE_ROUTE: { StrictSourceRouteOption opt = new StrictSourceRouteOption(); int addrs = ((int) m_options[offset] & 0xff) - 3; offset += 2; for (int i = 0; i < addrs / 4; i++) { int ip = 0; for (int j = 0; j < 4; j++) ip = ip << 8 + ((int) m_options[offset++] & 0xff); opt.add(new IPv4Address(ip)); } options.add(opt); } break; case Option.CODE_ROUTE_RECORD: { LooseSourceRouteOption opt = new LooseSourceRouteOption(); int addrs = ((int) m_options[offset] & 0xff) - 3; offset += 2; for (int i = 0; i < addrs / 4; i++) { int ip = 0; for (int j = 0; j < 4; j++) ip = ip << 8 + ((int) m_options[offset++] & 0xff); opt.add(new IPv4Address(ip)); } options.add(opt); } break; default: throw new InstantiationException("Unsupported Option Type"); } // end switch } return options; } // end method /** * Adds an option to the IP header. * * @param opt * The option to add to the header. * */ public void addOption(Option opt) { int origLen = 0; if (m_options == null) { int len = opt.bytesRequired(); if ((len % 4) != 0) len = 4 - (len % 4); m_options = new byte[opt.bytesRequired()]; int off = opt.writeBytes(m_options, 0); while (off < len) m_options[off++] = (byte) 0; } else { origLen = m_options.length; if (origLen + opt.bytesRequired() > 40) throw new IndexOutOfBoundsException("Option List is too long, must be less than 40 bytes"); int len = origLen + opt.bytesRequired(); if ((len % 4) != 0) len += 4 - (len % 4); byte[] ndata = new byte[len]; System.arraycopy(m_options, 0, ndata, 0, origLen); int off = opt.writeBytes(ndata, origLen); while (off < len) ndata[off++] = (byte) 0; } m_hdrlen = (byte) ((20 + m_options.length) / 4); } /** * Stores the IP header as an array of bytes into the passed data buffer. * The IP header is written starting at the specified offset, and the new * offset is returned to the caller. * * @param data * The location to write the data * @param offset * The offset to start storing information. * * @return The new offset just beyond the last written byte. * */ public int writeBytes(byte[] data, int offset) { data[offset++] = (byte) ((m_version << 4) | (m_hdrlen & 0xf)); data[offset++] = (byte) m_tos; data[offset++] = (byte) ((m_length >> 8) & 0xff); data[offset++] = (byte) (m_length & 0xff); data[offset++] = (byte) ((m_identity >> 8) & 0xff); data[offset++] = (byte) (m_identity & 0xff); data[offset++] = (byte) ((m_flags << 5) | ((m_fragOffset >> 8) & 0xff)); data[offset++] = (byte) (m_fragOffset & 0xff); data[offset++] = (byte) m_ttl; data[offset++] = (byte) m_protocol; data[offset++] = (byte) ((m_checksum >> 8) & 0xff); data[offset++] = (byte) (m_checksum & 0xff); data[offset++] = (byte) ((m_srcAddr >> 24) & 0xff); data[offset++] = (byte) ((m_srcAddr >> 16) & 0xff); data[offset++] = (byte) ((m_srcAddr >> 8) & 0xff); data[offset++] = (byte) (m_srcAddr & 0xff); data[offset++] = (byte) ((m_dstAddr >> 24) & 0xff); data[offset++] = (byte) ((m_dstAddr >> 16) & 0xff); data[offset++] = (byte) ((m_dstAddr >> 8) & 0xff); data[offset++] = (byte) (m_dstAddr & 0xff); System.arraycopy(m_options, 0, data, offset, m_options.length); offset += m_options.length; return offset; } /** * Converts the passed 32-bit IPv4 address to a dotted decimal IP address * string. * * @param ipv4Addr * The 32-bit address * * @return The dotted decimal address in the format "xxx.xxx.xxx.xxx" where * 0 <= xxx < 256 * */ public static String addressToString(int ipv4Addr) { StringBuffer buf = new StringBuffer(); buf.append((ipv4Addr >> 24) & 0xff); buf.append('.'); buf.append((ipv4Addr >> 16) & 0xff); buf.append('.'); buf.append((ipv4Addr >> 8) & 0xff); buf.append('.'); buf.append(ipv4Addr & 0xff); return buf.toString(); } /** * Converts the passed IPv4 address buffer to a dotted decimal IP address * string. * * @param buf * The 4 byte buffer * * @return The dotted decimal address in the format "xxx.xxx.xxx.xxx" where * 0 <= xxx < 256 * * @exception IllegalArgumentException * Thrown if the buffer is not exactly 4 bytes in length. */ public static String addressToString(byte[] buf) { if (buf.length != 4) throw new IllegalArgumentException("IPv4 Address must be 4-bytes in length"); int a = (buf[0] < 0) ? (int) buf[0] + 256 : (int) buf[0]; int b = (buf[1] < 0) ? (int) buf[1] + 256 : (int) buf[1]; int c = (buf[2] < 0) ? (int) buf[2] + 256 : (int) buf[2]; int d = (buf[3] < 0) ? (int) buf[3] + 256 : (int) buf[3]; StringBuffer sbuf = new StringBuffer(); sbuf.append(a).append('.').append(b).append('.').append(c).append('.').append(d); return sbuf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -