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

📄 dhcppacket.java

📁 DHCP 的JAVA实现
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }

    /**
     * Returns the giaddr field (Relay agent IP address).
     *
     * @return the giaddr field converted to <tt>InetAddress</tt> object.
     */
    public InetAddress getGiaddr() {
        try {
            return InetAddress.getByAddress(this.getGiaddrRaw());
        } catch (UnknownHostException e) {
            logger.log(Level.SEVERE, "Unexpected UnknownHostException", e);
            return null;    // normaly impossible
        }
    }

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

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

    /**
     * Sets the giaddr field (Relay agent IP address).
     *
     * @param giaddr The giaddr to set.
     * @throws UnknownHostException
     */
    public void setGiaddr(String giaddr) throws UnknownHostException {
        this.setGiaddr(InetAddress.getByName(giaddr));
    }

    /**
     * Sets the giaddr field (Relay agent IP address).
     *
     * <p><tt>giaddr</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 giaddr The giaddr to set.
     */
    public void setGiaddrRaw(byte[] giaddr) {
        if (giaddr.length != 4) {
            throw new IllegalArgumentException("4-byte array required");
        }
        System.arraycopy(giaddr, 0, this.giaddr, 0, 4);
    }

    /**
     * Returns the hlen field (Hardware address length).
     *
     * <p>Typical value is 6 for ethernet - 6 bytes MAC address.
     *
     * @return the hlen field.
     */
    public byte getHlen() {
        return this.hlen;
    }

    /**
     * Sets the hlen field (Hardware address length).
     *
     * <p>Typical value is 6 for ethernet - 6 bytes MAC address.
     *
     * <p>hlen value should be between 0 and 16, but no control is done here.
     *
     * @param hlen The hlen to set.
     */
    public void setHlen(byte hlen) {
        this.hlen = hlen;
    }

    /**
     * Returns the hops field.
     *
     * @return the hops field.
     */
    public byte getHops() {
        return this.hops;
    }

    /**
     * Sets the hops field.
     *
     * @param hops The hops to set.
     */
    public void setHops(byte hops) {
        this.hops = hops;
    }

    /**
     * Returns the htype field (Hardware address length).
     *
     * <p>Predefined values are:
     * <pre>
     * HTYPE_ETHER (1)
     * HTYPE_IEEE802 (6)
     * HTYPE_FDDI (8)
     * </pre>
     *
     * <p>Typical value is <tt>HTYPE_ETHER</tt>.
     *
     * @return the htype field.
     */
    public byte getHtype() {
        return this.htype;
    }

    /**
     * Sets the htype field (Hardware address length).
     *
     * <p>Predefined values are:
     * <pre>
     * HTYPE_ETHER (1)
     * HTYPE_IEEE802 (6)
     * HTYPE_FDDI (8)
     * </pre>
     *
     * <p>Typical value is <tt>HTYPE_ETHER</tt>.
     *
     * @param htype The htype to set.
     */
    public void setHtype(byte htype) {
        this.htype = htype;
    }

    /**
     * Returns whether the packet is DHCP or BOOTP.
     *
     * <p>It indicates the presence of the DHCP Magic Cookie at the end
     * of the BOOTP portion.
     *
     * <p>Default is <tt>true</tt> for a brand-new object.
     *
     * @return Returns the isDhcp.
     */
    public boolean isDhcp() {
        return this.isDhcp;
    }

    /**
     * Sets the isDhcp flag.
     *
     * <p>Indicates whether to generate a DHCP or a BOOTP packet. If <tt>true</tt>
     * the DHCP Magic Cookie is added after the BOOTP portion and before the
     * DHCP Options.
     *
     * <p>If <tt>isDhcp</tt> if false, all DHCP options are ignored when calling
     * <tt>serialize()</tt>.
     * 
     * <p>Default value is <tt>true</tt>.
     *
     * @param isDhcp The isDhcp to set.
     */
    public void setDhcp(boolean isDhcp) {
        this.isDhcp = isDhcp;
    }

    /**
     * Returns the op field (Message op code).
     *
     * <p>Predefined values are:
     * <pre>
     * BOOTREQUEST (1)
     * BOOTREPLY (2)
     * </pre>
     *
     * @return the op field.
     */
    public byte getOp() {
        return this.op;
    }

    /**
     * Sets the op field (Message op code).
     *
     * <p>Predefined values are:
     * <pre>
     * BOOTREQUEST (1)
     * BOOTREPLY (2)
     * </pre>
     *
     * <p>Default value is <tt>BOOTREPLY</tt>, suitable for server replies.
     * 
     * @param op The op to set.
     */
    public void setOp(byte op) {
        this.op = op;
    }

    /**
     * Returns the padding portion of the packet.
     *
     * <p>This byte array follows the DHCP Options.
     * Normally, its content is irrelevant.
     *
     * @return Returns the padding.
     */
    public byte[] getPadding() {
        return this.padding.clone();
    }

    /**
     * Sets the padding buffer.
     *
     * <p>This byte array follows the DHCP Options.
     * Normally, its content is irrelevant.
     *
     * <p>If <tt>paddig</tt> is null, it is set to an empty buffer.
     *
     * <p>Padding is automatically added at the end of the datagram when calling
     * <tt>serialize()</tt> to match DHCP minimal packet size.
     * 
     * @param padding The padding to set.
     */
    public void setPadding(byte[] padding) {
        this.padding = ((padding == null) ? new byte[0] : padding.clone());
    }

    /**
     * Sets the padding buffer with <tt>length</tt> zero bytes.
     * 
     * <p>This is a short cut for <tt>setPadding(new byte[length])</tt>.
     * 
     * @param length size of the padding buffer
     */
    public void setPaddingWithZeroes(int length) {
        if (length < 0) {
            length = 0;
        }
        if (length > _DHCP_MAX_MTU) {
            throw new IllegalArgumentException("length is > " + _DHCP_MAX_MTU);
        }
        this.setPadding(new byte[length]);
    }

    /**
     * Returns the secs field (seconds elapsed).
     *
     * @return the secs field.
     */
    public short getSecs() {
        return this.secs;
    }

    /**
     * Sets the secs field (seconds elapsed).
     *
     * @param secs The secs to set.
     */
    public void setSecs(short secs) {
        this.secs = secs;
    }

    /**
     * Returns the siaddr field (IP address of next server).
     *
     * @return the siaddr field converted to <tt>InetAddress</tt> object.
     */
    public InetAddress getSiaddr() {
        try {
            return InetAddress.getByAddress(this.getSiaddrRaw());
        } catch (UnknownHostException e) {
            logger.log(Level.SEVERE, "Unexpected UnknownHostException", e);
            return null;    // normaly impossible
        }
    }

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

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

    /**
     * Sets the siaddr field (IP address of next server).
     *
     * @param siaddr The siaddr to set.
     * @throws UnknownHostException
     */
    public void setSiaddr(String siaddr) throws UnknownHostException {
        this.setSiaddr(InetAddress.getByName(siaddr));
    }

    /**
     * Sets the siaddr field (IP address of next server).
     *
     * <p><tt>siaddr</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 siaddr The siaddr to set.
     */
    public void setSiaddrRaw(byte[] siaddr) {
        if (siaddr.length != 4) {
            throw new IllegalArgumentException("4-byte array required");
        }
        System.arraycopy(siaddr, 0, this.siaddr, 0, 4);
    }

    /**
     * Returns the sname field (Optional server host name).
     *
     * <p>Returns the raw byte[64] buffer, containing a null terminated string.
     *
     * <p>This is the low-level maximum performance getter for this field.
     *
     * @return the sname field.
     */
    public byte[] getSnameRaw() {
        return this.sname.clone();
    }

    /**
     * Returns the sname field (Optional server host name) as String.
     *
     * @return the sname converted to a String (transparent encoding).
     */
    public String getSname() {
        return bytesToString(this.getSnameRaw());
    }

    /**
     * Sets the sname field (Optional server host name) as String.

⌨️ 快捷键说明

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