Class DHCPMessage


  • public class DHCPMessage
    extends Object
    This class represents a DHCP Message.
    Author:
    Markus Bernhardt, Copyright 2016, Jason Goldschmidt, Nick Stone and Simon Frankenberger, Bernd Rosstauscher, Copyright 2009
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static InetAddress BROADCAST_ADDR
      Broadcast Adress to send packets to
      static int CLIENT_PORT
      Default DHCP client port
      static byte DHCPACK
      Message Code representing a DHCPACK message
      static byte DHCPDECLINE
      Message Code representing a DHCPDECLINE message
      static byte DHCPDISCOVER
      Message Code representing a DHCPDISCOVER message
      static byte DHCPINFORM
      Message Code representing a DHCPINFORM message
      static byte DHCPNAK
      Message Code representing a DHCPNAK message
      static byte DHCPOFFER
      Message Code representing a DHCPOFFER message
      static byte DHCPRELEASE
      Message Code representing a DHCPRELEASE message
      static byte DHCPREQUEST
      Message Code representing a DHCPREQUEST message
      static byte OP_REPLY
      Operation for a reply
      static byte OP_REQUEST
      Operation for a request
      static int SERVER_PORT
      Default DHCP server port
    • Constructor Summary

      Constructors 
      Constructor Description
      DHCPMessage()
      Creates empty DHCPMessage object, initializes the object, sets the host to the broadcast address, the local subnet, binds to the default server port.
      DHCPMessage​(byte[] ibuf)
      Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to default host name, the local subnet, and bind to the default server port.
      DHCPMessage​(byte[] ibuf, int inPort)
      Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to broadcast address, and binds to the specified port.
      DHCPMessage​(byte[] ibuf, InetAddress inServername)
      Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to specified host name, and binds to the specified port.
      DHCPMessage​(byte[] ibuf, InetAddress inServername, int inPort)
      Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to specified host name, and binds to the specified port.
      DHCPMessage​(int inPort)
      Creates an empty DHCPMessage object, initializes the object, sets the host to the broadcast address, and binds to a specified port.
      DHCPMessage​(DHCPMessage inMessage)
      Copy constructor creates DHCPMessage from inMessage
      DHCPMessage​(DHCPMessage inMessage, InetAddress inServername)
      Copy constructor creates DHCPMessage from inMessage and sets server name.
      DHCPMessage​(DHCPMessage inMessage, InetAddress inServername, int inPort)
      Copy constructor creates DHCPMessage from inMessage and sets server and port.
      DHCPMessage​(DataInputStream inStream)
      Creates a new DHCPMessage object from the giben DataInputStream.
      DHCPMessage​(InetAddress inServername)
      Creates an empty DHCPMessage object, initializes the object, sets the host to a specified host name, and binds to the default port.
      DHCPMessage​(InetAddress inServername, int inPort)
      Creates an empty DHCPMessage object, initializes the object, sets the host to a specified host name, and binds to a specified port.
    • Field Detail

      • OP_REQUEST

        public static final byte OP_REQUEST
        Operation for a request
        See Also:
        Constant Field Values
      • DHCPDISCOVER

        public static final byte DHCPDISCOVER
        Message Code representing a DHCPDISCOVER message
        See Also:
        Constant Field Values
      • DHCPOFFER

        public static final byte DHCPOFFER
        Message Code representing a DHCPOFFER message
        See Also:
        Constant Field Values
      • DHCPREQUEST

        public static final byte DHCPREQUEST
        Message Code representing a DHCPREQUEST message
        See Also:
        Constant Field Values
      • DHCPDECLINE

        public static final byte DHCPDECLINE
        Message Code representing a DHCPDECLINE message
        See Also:
        Constant Field Values
      • DHCPACK

        public static final byte DHCPACK
        Message Code representing a DHCPACK message
        See Also:
        Constant Field Values
      • DHCPNAK

        public static final byte DHCPNAK
        Message Code representing a DHCPNAK message
        See Also:
        Constant Field Values
      • DHCPRELEASE

        public static final byte DHCPRELEASE
        Message Code representing a DHCPRELEASE message
        See Also:
        Constant Field Values
      • DHCPINFORM

        public static final byte DHCPINFORM
        Message Code representing a DHCPINFORM message
        See Also:
        Constant Field Values
      • CLIENT_PORT

        public static final int CLIENT_PORT
        Default DHCP client port
        See Also:
        Constant Field Values
      • SERVER_PORT

        public static final int SERVER_PORT
        Default DHCP server port
        See Also:
        Constant Field Values
      • BROADCAST_ADDR

        public static InetAddress BROADCAST_ADDR
        Broadcast Adress to send packets to
    • Constructor Detail

      • DHCPMessage

        public DHCPMessage()
        Creates empty DHCPMessage object, initializes the object, sets the host to the broadcast address, the local subnet, binds to the default server port.
      • DHCPMessage

        public DHCPMessage​(DHCPMessage inMessage)
        Copy constructor creates DHCPMessage from inMessage
        Parameters:
        inMessage - The message to be copied
      • DHCPMessage

        public DHCPMessage​(DHCPMessage inMessage,
                           InetAddress inServername,
                           int inPort)
        Copy constructor creates DHCPMessage from inMessage and sets server and port.
        Parameters:
        inMessage - The message to be copied
        inServername - The host name
        inPort - The port number
      • DHCPMessage

        public DHCPMessage​(DHCPMessage inMessage,
                           InetAddress inServername)
        Copy constructor creates DHCPMessage from inMessage and sets server name.
        Parameters:
        inMessage - The message to be copied
        inServername - The host name
      • DHCPMessage

        public DHCPMessage​(InetAddress inServername,
                           int inPort)
        Creates an empty DHCPMessage object, initializes the object, sets the host to a specified host name, and binds to a specified port.
        Parameters:
        inServername - The host name
        inPort - The port number
      • DHCPMessage

        public DHCPMessage​(InetAddress inServername)
        Creates an empty DHCPMessage object, initializes the object, sets the host to a specified host name, and binds to the default port.
        Parameters:
        inServername - The host name
      • DHCPMessage

        public DHCPMessage​(int inPort)
        Creates an empty DHCPMessage object, initializes the object, sets the host to the broadcast address, and binds to a specified port.
        Parameters:
        inPort - The port number
      • DHCPMessage

        public DHCPMessage​(byte[] ibuf)
        Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to default host name, the local subnet, and bind to the default server port.
        Parameters:
        ibuf - The byte array to initialize DHCPMessage object
      • DHCPMessage

        public DHCPMessage​(byte[] ibuf,
                           InetAddress inServername,
                           int inPort)
        Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to specified host name, and binds to the specified port.
        Parameters:
        ibuf - The byte array to initialize DHCPMessage object
        inServername - The hostname
        inPort - The port number
      • DHCPMessage

        public DHCPMessage​(byte[] ibuf,
                           int inPort)
        Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to broadcast address, and binds to the specified port.
        Parameters:
        ibuf - The byte array to initialize DHCPMessage object
        inPort - The port number
      • DHCPMessage

        public DHCPMessage​(byte[] ibuf,
                           InetAddress inServername)
        Creates an empty DHCPMessage object, initializes the object with a specified byte array containing DHCP message information, sets the host to specified host name, and binds to the specified port.
        Parameters:
        ibuf - The byte array to initialize DHCPMessage object
        inServername - The hostname
      • DHCPMessage

        public DHCPMessage​(DataInputStream inStream)
        Creates a new DHCPMessage object from the giben DataInputStream.
        Parameters:
        inStream - The stream to read from
    • Method Detail

      • externalize

        public byte[] externalize()
        Converts a DHCPMessage object to a byte array.
        Returns:
        A byte array with information from DHCPMessage object, ready to send.
      • internalize

        public DHCPMessage internalize​(byte[] ibuff)
        Convert a specified byte array containing a DHCP message into a DHCPMessage object.
        Parameters:
        ibuff - Byte array to convert to a DHCPMessage object
        Returns:
        A DHCPMessage object with information from byte array.
      • setOp

        public void setOp​(byte inOp)
        Set message Op code / message type.
        Parameters:
        inOp - message Op code / message type
      • setHtype

        public void setHtype​(byte inHtype)
        Set hardware address type.
        Parameters:
        inHtype - hardware address type
      • setHlen

        public void setHlen​(byte inHlen)
        Set hardware address length.
        Parameters:
        inHlen - hardware address length
      • setHops

        public void setHops​(byte inHops)
        Set hops field.
        Parameters:
        inHops - hops field
      • setXid

        public void setXid​(int inXid)
        Set transaction ID.
        Parameters:
        inXid - transactionID
      • setSecs

        public void setSecs​(short inSecs)
        Set seconds elapsed since client began address acquisition or renewal process.
        Parameters:
        inSecs - Seconds elapsed since client began address acquisition or renewal process
      • setFlags

        public void setFlags​(short inFlags)
        Set flags field.
        Parameters:
        inFlags - flags field
      • setCiaddr

        public void setCiaddr​(byte[] inCiaddr)
        Set client IP address.
        Parameters:
        inCiaddr - client IP address
      • setYiaddr

        public void setYiaddr​(byte[] inYiaddr)
        Set 'your' (client) IP address.
        Parameters:
        inYiaddr - 'your' (client) IP address
      • setSiaddr

        public void setSiaddr​(byte[] inSiaddr)
        Set address of next server to use in bootstrap.
        Parameters:
        inSiaddr - address of next server to use in bootstrap
      • setGiaddr

        public void setGiaddr​(byte[] inGiaddr)
        Set relay agent IP address.
        Parameters:
        inGiaddr - relay agent IP address
      • setChaddr

        public void setChaddr​(byte[] inChaddr)
        Set client harware address.
        Parameters:
        inChaddr - client hardware address
      • setSname

        public void setSname​(byte[] inSname)
        Set optional server host name.
        Parameters:
        inSname - server host name
      • setFile

        public void setFile​(byte[] inFile)
        Set boot file name.
        Parameters:
        inFile - boot file name
      • setPort

        public void setPort​(int inPortNum)
        Set message destination port.
        Parameters:
        inPortNum - port on message destination host
      • setDestinationHost

        public void setDestinationHost​(String inHost)
        Set message destination IP
        Parameters:
        inHost - string representation of message destination IP or hostname
      • getOp

        public byte getOp()
        Returns:
        message Op code / message type.
      • getHtype

        public byte getHtype()
        Returns:
        hardware address type.
      • getHlen

        public byte getHlen()
        Returns:
        hardware address length.
      • getHops

        public byte getHops()
        Returns:
        hops field.
      • getXid

        public int getXid()
        Returns:
        transaction ID.
      • getSecs

        public short getSecs()
        Returns:
        seconds elapsed since client began address acquisition or renewal process.
      • getFlags

        public short getFlags()
        Returns:
        flags field.
      • getCiaddr

        public byte[] getCiaddr()
        Returns:
        client IP address.
      • getYiaddr

        public byte[] getYiaddr()
        Returns:
        'your' (client) IP address.
      • getSiaddr

        public byte[] getSiaddr()
        Returns:
        address of next server to use in bootstrap.
      • getGiaddr

        public byte[] getGiaddr()
        Returns:
        relay agent IP address.
      • getChaddr

        public byte[] getChaddr()
        Returns:
        client harware address.
      • getSname

        public byte[] getSname()
        Returns:
        optional server host name.
      • getFile

        public byte[] getFile()
        Returns:
        boot file name.
      • getOptions

        public byte[] getOptions()
        Returns:
        a byte array containing options
      • getPort

        public int getPort()
        Returns:
        An interger representation of the message destination port
      • getDestinationAddress

        public String getDestinationAddress()
        Get message destination hostname
        Returns:
        A string representing the hostname of the message destination server
      • setOption

        public void setOption​(int inOptNum,
                              byte[] inOptionData)
        Sets DHCP options in DHCPMessage. If option already exists then remove old option and insert a new one.
        Parameters:
        inOptNum - option number
        inOptionData - option data
      • getOption

        public byte[] getOption​(int inOptNum)
        Returns specified DHCP option that matches the input code. Null is returned if option is not set.
        Parameters:
        inOptNum - option number
        Returns:
        the option matching input code
      • removeOption

        public void removeOption​(int inOptNum)
        Removes the specified DHCP option that matches the input code.
        Parameters:
        inOptNum - option number
      • IsOptSet

        public boolean IsOptSet​(int inOptNum)
        Report whether or not the input option is set.
        Parameters:
        inOptNum - option number
        Returns:
        is the given option set?