All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jvnet.hudson.proxy_dhcp.DHCPOption Maven / Gradle / Ivy

The newest version!
package org.jvnet.hudson.proxy_dhcp;

import org.jvnet.hudson.pxeboot.DataInputStream2;
import org.jvnet.hudson.pxeboot.DataOutputStream2;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;

/**
 * @author Kohsuke Kawaguchi
 * @see RFC 2132 (http://www.ietf.org/rfc/rfc2132.txt)
 */
public class DHCPOption {
    public byte tag;
    public byte[] data;

    public DHCPOption() {
    }

    public DHCPOption(byte tag, byte[] data) {
        this.tag = tag;
        this.data = data;
    }

    public DHCPOption(byte tag, Inet4Address adrs) {
        this(tag,adrs.getAddress());
    }

    DHCPOption(DataInputStream2 di) throws IOException {
        tag = di.readByte();
        if(tag==OPTION_END || tag==OPTION_PAD)
            return; // these options don't have payload
        byte len = di.readByte();
        data = di.readByteArray(uint(len));
    }

    private static int uint(byte b) {
        if(b>=0)    return b;
        return ((int)b)+256;
    }

    void writeTo(DataOutputStream2 o) throws IOException {
        o.writeByte(tag);
        o.writeByte(data.length); // TODO: handle sign correctly 
        o.write(data);
    }

    public String getDataAsString() {
        try {
            // DHCP spec doesn't say if the string is supposed to be null terminated or not,
            // but the C implementation that I saw assumes that. So handle both cases
            int i=0;
            while(i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy