⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcppacket.java

📁 DHCP 的JAVA实现
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     *
     * <p>The string is first converted to a byte[] array using transparent
     * encoding. If the resulting buffer size is > 64, an <tt>IllegalArgumentException</tt>
     * is thrown.
     *
     * <p>If <tt>sname</tt> parameter is null, the buffer is filled with zeros.
     *
     * @param sname The sname field to set.
     * @throws IllegalArgumentException string too long
     */
    public void setSname(String sname) {
        this.setSnameRaw(stringToBytes(sname));
    }

    /**
     * Sets the sname field (Optional server host name) as String.
     *
     * <p>If the buffer size is > 64, an <tt>IllegalArgumentException</tt>
     * is thrown.
     *
     * <p>If <tt>sname</tt> parameter is null, the buffer is filled with zeros.
     *
     * <p>This is the low-level maximum performance setter for this field.
     *
     * @param sname The sname field to set.
     * @throws IllegalArgumentException string too long
     */
    public void setSnameRaw(byte[] sname) {
        if (sname != null) {
            if (sname.length > this.sname.length) {
                throw new IllegalArgumentException("Sname is too long:" + sname.length + " max is:" + this.sname.length);
            }
            Arrays.fill(this.sname, (byte) 0);
            System.arraycopy(sname, 0, this.sname, 0, sname.length);
        } else {
            Arrays.fill(this.sname, (byte) 0);
        }
    }

    /**
     * Returns the xid field (Transaction ID).
     *
     * @return Returns the xid.
     */
    public int getXid() {
        return this.xid;
    }

    /**
     * Sets the xid field (Transaction ID).
     *
     * <p>This field is random generated by the client, and used by the client and
     * server to associate requests and responses for the same transaction.
     *
     * @param xid The xid to set.
     */
    public void setXid(int xid) {
        this.xid = xid;
    }

    /**
     * Returns the yiaddr field ('your' IP address).
     *
     * @return the yiaddr field converted to <tt>InetAddress</tt> object.
     */
    public InetAddress getYiaddr() {
        try {
            return InetAddress.getByAddress(this.getYiaddrRaw());
        } catch (UnknownHostException e) {
            logger.log(Level.SEVERE, "Unexpected UnknownHostException", e);
            return null;    // normaly impossible
        }
    }

    /**
     * Returns the yiaddr field ('your' IP address).
     *
     * <p>This is the low-level maximum performance getter for this field.
     *
     * @return Returns the yiaddr as raw byte[4].
     */
    public byte[] getYiaddrRaw() {
        return this.yiaddr.clone();
    }

    /**
     * Sets the yiaddr field ('your' IP address).
     *
     * <p>Ths <tt>yiaddr</tt> field must be of <tt>Inet4Address</tt> class or
     * an <tt>IllegalArgumentException</tt> is thrown.
     *
     * @param yiaddr The yiaddr to set.
     */
    public void setYiaddr(InetAddress yiaddr) {
        if (!(yiaddr instanceof Inet4Address)) {
            throw new IllegalArgumentException("Inet4Address required");
        }
        this.setYiaddrRaw(yiaddr.getAddress());
    }

    /**
     * Sets the yiaddr field ('your' IP address).
     *
     * @param yiaddr The yiaddr to set.
     * @throws UnknownHostException
     */
    public void setYiaddr(String yiaddr) throws UnknownHostException {
        this.setYiaddr(InetAddress.getByName(yiaddr));
    }

    /**
     * Sets the yiaddr field ('your' IP address).
     *
     * <p><tt>yiaddr</tt> must be a 4 bytes array, or an <tt>IllegalArgumentException</tt>
     * is thrown.
     *
     * <p>This is the low-level maximum performance setter for this field.
     * The array is internally copied so any further modification to <tt>ciaddr</tt>
     * parameter has no side effect.
     *
     * @param yiaddr The yiaddr to set.
     */
    public void setYiaddrRaw(byte[] yiaddr) {
        if (yiaddr.length != 4) {
            throw new IllegalArgumentException("4-byte array required");
        }
        System.arraycopy(yiaddr, 0, this.yiaddr, 0, 4);
    }
    /**
     * Return the DHCP Option Type.
     * 
     * <p>This is a short-cut for <tt>getOptionAsByte(DHO_DHCP_MESSAGE_TYPE)</tt>.
     * 
     * @return option type, of <tt>null</tt> if not present.
     */
    public Byte getDHCPMessageType() {
        return this.getOptionAsByte(DHO_DHCP_MESSAGE_TYPE);
    }

    /**
     * Sets the DHCP Option Type.
     * 
     * <p>This is a short-cur for <tt>setOptionAsByte(DHO_DHCP_MESSAGE_TYPE, optionType);</tt>.
     * 
     * @param optionType
     */
    public void setDHCPMessageType(byte optionType) {
        this.setOptionAsByte(DHO_DHCP_MESSAGE_TYPE, optionType);
    }

    /**
     * Indicates that the DHCP packet has been truncated and did not finished
     * with a 0xFF option. This parameter is set only when parsing packets in
     * non-strict mode (which is not the default behaviour).
     *
     * <p>This field is read-only and can be <tt>true</tt> only with objects created
     * by parsing a Datagram - getPacket() methods.
     *
     * <p>This field is cleared if the object is cloned.
     *
     * @return the truncated field.
     */
    public boolean isTruncated() {
        return this.truncated;
    }

    /**
     * Wrapper function for getValueAsNum() in DHCPOption. Returns a numerical option: int, short or byte.
     * 
     * @param code DHCP option code
     * @return Integer object or <tt>null</tt>
     */
    public Integer getOptionAsNum(byte code) {
    	DHCPOption opt = this.getOption(code);
    	return (opt != null) ? opt.getValueAsNum() : null; 
    }
    
    /**
     * Returns a DHCP Option as Byte format.
     *
     * This method is only allowed for the following option codes:
     * <pre>
     * DHO_IP_FORWARDING(19)
     * DHO_NON_LOCAL_SOURCE_ROUTING(20)
     * DHO_DEFAULT_IP_TTL(23)
     * DHO_ALL_SUBNETS_LOCAL(27)
     * DHO_PERFORM_MASK_DISCOVERY(29)
     * DHO_MASK_SUPPLIER(30)
     * DHO_ROUTER_DISCOVERY(31)
     * DHO_TRAILER_ENCAPSULATION(34)
     * DHO_IEEE802_3_ENCAPSULATION(36)
     * DHO_DEFAULT_TCP_TTL(37)
     * DHO_TCP_KEEPALIVE_GARBAGE(39)
     * DHO_NETBIOS_NODE_TYPE(46)
     * DHO_DHCP_OPTION_OVERLOAD(52)
     * DHO_DHCP_MESSAGE_TYPE(53)
     * DHO_AUTO_CONFIGURE(116)
     * </pre>
     *
     * @param code the option code.
     * @return the option value, <tt>null</tt> if option is not present.
     * @throws IllegalArgumentException the option code is not in the list above.
     * @throws DHCPBadPacketException the option value in packet is of wrong size.
     */
    public Byte getOptionAsByte(byte code) throws IllegalArgumentException {
        DHCPOption opt = this.getOption(code);
        return (opt == null) ? null : opt.getValueAsByte();
    }

    /**
     * Returns a DHCP Option as Short format.
     *
     * <p>This method is only allowed for the following option codes:
     * <pre>
     * DHO_BOOT_SIZE(13)
     * DHO_MAX_DGRAM_REASSEMBLY(22)
     * DHO_INTERFACE_MTU(26)
     * DHO_DHCP_MAX_MESSAGE_SIZE(57)
     * </pre>
     *
     * @param code the option code.
     * @return the option value, <tt>null</tt> if option is not present.
     * @throws IllegalArgumentException the option code is not in the list above.
     * @throws DHCPBadPacketException the option value in packet is of wrong size.
     */
    public Short getOptionAsShort(byte code) throws IllegalArgumentException {
        DHCPOption opt = this.getOption(code);
        return (opt == null) ? null : opt.getValueAsShort();
    }

    /**
     * Returns a DHCP Option as Integer format.
     * 
     * <p>This method is only allowed for the following option codes:
     * <pre>
     * DHO_TIME_OFFSET(2)
     * DHO_PATH_MTU_AGING_TIMEOUT(24)
     * DHO_ARP_CACHE_TIMEOUT(35)
     * DHO_TCP_KEEPALIVE_INTERVAL(38)
     * DHO_DHCP_LEASE_TIME(51)
     * DHO_DHCP_RENEWAL_TIME(58)
     * DHO_DHCP_REBINDING_TIME(59)
     * </pre>
     * 
     * @param code the option code.
     * @return the option value, <tt>null</tt> if option is not present.
     * @throws IllegalArgumentException the option code is not in the list above.
     * @throws DHCPBadPacketException the option value in packet is of wrong size.
     */
    public Integer getOptionAsInteger(byte code) throws IllegalArgumentException {
        DHCPOption opt = this.getOption(code);
        return (opt == null) ? null : opt.getValueAsInt();
    }

    /**
     * Returns a DHCP Option as InetAddress format.
     *
     * <p>This method is only allowed for the following option codes:
     * <pre>
     * DHO_SUBNET_MASK(1)
     * DHO_SWAP_SERVER(16)
     * DHO_BROADCAST_ADDRESS(28)
     * DHO_ROUTER_SOLICITATION_ADDRESS(32)
     * DHO_DHCP_REQUESTED_ADDRESS(50)
     * DHO_DHCP_SERVER_IDENTIFIER(54)
     * DHO_SUBNET_SELECTION(118)
     * </pre>
     *
     * @param code the option code.
     * @return the option value, <tt>null</tt> if option is not present.
     * @throws IllegalArgumentException the option code is not in the list above.
     * @throws DHCPBadPacketException the option value in packet is of wrong size.
     */
    public InetAddress getOptionAsInetAddr(byte code) throws IllegalArgumentException {
        DHCPOption opt = this.getOption(code);
        return (opt == null) ? null : opt.getValueAsInetAddr();
    }

    /**
     * Returns a DHCP Option as String format.
     *
     * <p>This method is only allowed for the following option codes:
     * <pre>
     * DHO_HOST_NAME(12)
     * DHO_MERIT_DUMP(14)
     * DHO_DOMAIN_NAME(15)
     * DHO_ROOT_PATH(17)
     * DHO_EXTENSIONS_PATH(18)
     * DHO_NETBIOS_SCOPE(47)
     * DHO_DHCP_MESSAGE(56)
     * DHO_VENDOR_CLASS_IDENTIFIER(60)
     * DHO_NWIP_DOMAIN_NAME(62)
     * DHO_NIS_DOMAIN(64)
     * DHO_NIS_SERVER(65)
     * DHO_TFTP_SERVER(66)
     * DHO_BOOTFILE(67)
     * DHO_NDS_TREE_NAME(86)
     * DHO_USER_AUTHENTICATION_PROTOCOL(98)
     * </pre>
     *
     * @param code the option code.
     * @return the option value, <tt>null</tt> if option is not present.
     * @throws IllegalArgumentException the option code is not in the list above.
     */
    public String getOptionAsString(byte code) throws IllegalArgumentException {
        DHCPOption opt = this.getOption(code);
        return (opt == null) ? null : opt.getValueAsString();
    }

    /**
     * Returns a DHCP Option as Short array format.
     *
     * <p>This method is only allowed for the following option codes:
     * <pre>
     * DHO_PATH_MTU_PLATEAU_TABLE(25)
     * DHO_NAME_SERVICE_SEARCH(117)
     * </pre>
     *
     * @param code the option code.
     * @return the option value array, <tt>null</tt> if option is not present.
     * @throws IllegalArgumentException the option code is not in the list above.
     * @throws DHCPBadPacketException the option value in packet is of wrong size.
     */
    public short[] getOptionAsShorts(byte code) throws IllegalArgumentException {
        DHCPOption opt = this.getOption(code);
        return (opt == null) ? null : opt.getValueAsShorts();
    }

    /**
     * Returns a DHCP Option as InetAddress array format.
     *
     * <p>This method is only allowed for the following option codes:
     * <pre>
     * DHO_ROUTERS(3)
     * DHO_TIME_SERVERS(4)
     * DHO_NAME_SERVERS(5)
     * DHO_DOMAIN_NAME_SERVERS(6)
     * DHO_LOG_SERVERS(7)
     * DHO_COOKIE_SERVERS(8)
     * DHO_LPR_SERVERS(9)
     * DHO_IMPRESS_SERVERS(10)
     * DHO_RESOURCE_LOCATION_SERVERS(11)
     * DHO_POLICY_FILTER(21)
     * DHO_STATIC_ROUTES(33)
     * DHO_NIS_SERVERS(41)
     * DHO_NTP_SERVERS(42)
     * DHO_NETBIOS_NAME_SERVERS(44)
     * DHO_NETBIOS_DD_SERVER(45)
     * DHO_FONT_SERVERS(48)
     * DHO_X_DISPLAY_MANAGER(49)
     * DHO_MOBILE_IP_HOME_AGENT(68)
     * DHO_SMTP_SERVER(69)
     * DHO_POP3_SERVER(70)
     * DHO_NNTP_SERVER(71)
     * DHO_WWW_SERVER(72)
     * DHO_FINGER_SERVER(73)
     * DHO_IRC_SERVER(74)
     * DHO_STREETTALK_SERVER(75)
     * DHO_STDA_SERVER(76)
     * DHO_NDS_SERVERS(85)
     * </pre>
     *
     * @param code the option code.
     * @return the option value array, <tt>null</tt> if option is not present.
     * @throws IllegalArgumentException the option code is not in the list above.
     * @throws DHCPBadPacketException the option value in packet is of wrong size.
     */
    public InetAddress[] getOptionAsInetAdd

⌨️ 快捷键说明

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