org.dhcp4java
Class DHCPPacket

java.lang.Object
  extended by org.dhcp4java.DHCPPacket
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class DHCPPacket
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

The basic class for manipulating DHCP packets.

Version:
0.99

There are two basic ways to build a new DHCPPacket object.

First one is to build an object from scratch using the constructor and setters. If you need to set repeatedly the same set of parameters and options, you can create a "master" object and clone it many times.

 DHCPPacket discover = new DHCPPacket();
 discover.setOp(DHCPPacket.BOOTREQUEST);
 discover.setHtype(DHCPPacket.HTYPE_ETHER);
 discover.setHlen((byte) 6);
 discover.setHops((byte) 0);
 discover.setXid( (new Random()).nextInt() );
 ...
 
Second is to decode a DHCP datagram received from the network. In this case, the object is created through a factory.

Example: simple DHCP sniffer

 DatagramSocket socket = new DatagramSocket(67);
 while (true) {
     DatagramPacket pac = new DatagramPacket(new byte[1500], 1500);
     socket.receive(pac);
     DHCPPacket dhcp = DHCPPacket.getPacket(pac);
     System.out.println(dhcp.toString());
 }
 
In this second way, beware that a BadPacketExpcetion is thrown if the datagram contains invalid DHCP data.

Getters and Setters: methods are provided with high-level data structures wherever it is possible (String, InetAddress...). However there are also low-overhead version (suffix Raw) dealing directly with byte[] for maximum performance. They are useful in servers for copying parameters in a servers from a request to a response without any type conversion. All parameters are copies, you may modify them as you like without any side-effect on the DHCPPacket object.

DHCP datagram format description:

FieldOctetsDescription
op1 Message op code / message type.
use constants BOOTREQUEST, BOOTREPLY
htype 1Hardware address type, see ARP section in "Assigned Numbers" RFC
use constants HTYPE_ETHER, HTYPE_IEEE802, HTYPE_FDDI
hlen1Hardware address length (e.g. '6' for ethernet).
hops1Client sets to zero, optionally used by relay agents when booting via a relay agent.
xid4 Transaction ID, a random number chosen by the client, used by the client and server to associate messages and responses between a client and a server.
secs2 Filled in by client, seconds elapsed since client began address acquisition or renewal process.
flags2 Flags (see below).
ciaddr4 Client IP address; only filled in if client is in BOUND, RENEW or REBINDING state and can respond to ARP requests.
yiaddr4 'your' (client) IP address.
siaddr4 IP address of next server to use in bootstrap; returned in DHCPOFFER, DHCPACK by server.
giaddr4 Relay agent IP address, used in booting via a relay agent.
chaddr16 Client hardware address.
sname64 Optional server host name, null terminated string.
file128 Boot file name, null terminated string; "generic" name or null in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
isDhcp4 Controls whether the packet is BOOTP or DHCP. DHCP contains the "magic cookie" of 4 bytes. 0x63 0x82 0x53 0x63.
DHO_*code** Optional parameters field. See the options documents for a list of defined options. See below.
padding* Optional padding at the end of the packet.

DHCP Option

The following options are codes are supported:
 DHO_SUBNET_MASK(1)
 DHO_TIME_OFFSET(2)
 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_HOST_NAME(12)
 DHO_BOOT_SIZE(13)
 DHO_MERIT_DUMP(14)
 DHO_DOMAIN_NAME(15)
 DHO_SWAP_SERVER(16)
 DHO_ROOT_PATH(17)
 DHO_EXTENSIONS_PATH(18)
 DHO_IP_FORWARDING(19)
 DHO_NON_LOCAL_SOURCE_ROUTING(20)
 DHO_POLICY_FILTER(21)
 DHO_MAX_DGRAM_REASSEMBLY(22)
 DHO_DEFAULT_IP_TTL(23)
 DHO_PATH_MTU_AGING_TIMEOUT(24)
 DHO_PATH_MTU_PLATEAU_TABLE(25)
 DHO_INTERFACE_MTU(26)
 DHO_ALL_SUBNETS_LOCAL(27)
 DHO_BROADCAST_ADDRESS(28)
 DHO_PERFORM_MASK_DISCOVERY(29)
 DHO_MASK_SUPPLIER(30)
 DHO_ROUTER_DISCOVERY(31)
 DHO_ROUTER_SOLICITATION_ADDRESS(32)
 DHO_STATIC_ROUTES(33)
 DHO_TRAILER_ENCAPSULATION(34)
 DHO_ARP_CACHE_TIMEOUT(35)
 DHO_IEEE802_3_ENCAPSULATION(36)
 DHO_DEFAULT_TCP_TTL(37)
 DHO_TCP_KEEPALIVE_INTERVAL(38)
 DHO_TCP_KEEPALIVE_GARBAGE(39)
 DHO_NIS_SERVERS(41)
 DHO_NTP_SERVERS(42)
 DHO_VENDOR_ENCAPSULATED_OPTIONS(43)
 DHO_NETBIOS_NAME_SERVERS(44)
 DHO_NETBIOS_DD_SERVER(45)
 DHO_NETBIOS_NODE_TYPE(46)
 DHO_NETBIOS_SCOPE(47)
 DHO_FONT_SERVERS(48)
 DHO_X_DISPLAY_MANAGER(49)
 DHO_DHCP_REQUESTED_ADDRESS(50)
 DHO_DHCP_LEASE_TIME(51)
 DHO_DHCP_OPTION_OVERLOAD(52)
 DHO_DHCP_MESSAGE_TYPE(53)
 DHO_DHCP_SERVER_IDENTIFIER(54)
 DHO_DHCP_PARAMETER_REQUEST_LIST(55)
 DHO_DHCP_MESSAGE(56)
 DHO_DHCP_MAX_MESSAGE_SIZE(57)
 DHO_DHCP_RENEWAL_TIME(58)
 DHO_DHCP_REBINDING_TIME(59)
 DHO_VENDOR_CLASS_IDENTIFIER(60)
 DHO_DHCP_CLIENT_IDENTIFIER(61)
 DHO_NWIP_DOMAIN_NAME(62)
 DHO_NWIP_SUBOPTIONS(63)
 DHO_NIS_DOMAIN(64)
 DHO_NIS_SERVER(65)
 DHO_TFTP_SERVER(66)
 DHO_BOOTFILE(67)
 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_USER_CLASS(77)
 DHO_FQDN(81)
 DHO_DHCP_AGENT_OPTIONS(82)
 DHO_NDS_SERVERS(85)
 DHO_NDS_TREE_NAME(86)
 DHO_USER_AUTHENTICATION_PROTOCOL(98)
 DHO_AUTO_CONFIGURE(116)
 DHO_NAME_SERVICE_SEARCH(117)
 DHO_SUBNET_SELECTION(118)
 

These options can be set and get through basic low-level getOptionRaw and setOptionRaw passing byte[] structures. Using these functions, data formats are under your responsibility. Arrays are always passed by copies (clones) so you can modify them freely without side-effects. These functions allow maximum performance, especially when copying options from a request datagram to a response datagram.

Special case: DHO_DHCP_MESSAGE_TYPE

The DHCP Message Type (option 53) is supported for the following values
 DHCPDISCOVER(1)
 DHCPOFFER(2)
 DHCPREQUEST(3)
 DHCPDECLINE(4)
 DHCPACK(5)
 DHCPNAK(6)
 DHCPRELEASE(7)
 DHCPINFORM(8)
 DHCPFORCERENEW(9)
 DHCPLEASEQUERY(13)
 

DHCP option formats

A limited set of higher level data-structures are supported. Type checking is enforced according to rfc 2132. Check corresponding methods for a list of option codes allowed for each datatype.

Inet (4 bytes - IPv4 address)
Inets (X*4 bytes - list of IPv4 addresses)
Short (2 bytes - short)
Shorts (X*2 bytes - list of shorts)
Byte (1 byte)
Bytes (X bytes - list of 1 byte parameters)
String (X bytes - ASCII string)

Note: this class is not synchronized for maximum performance. However, it is unlikely that the same DHCPPacket is used in two different threads in real life DHPC servers or clients. Multi-threading acces to an instance of this class is at your own risk.

Limitations: this class doesn't support spanned options or options longer than 256 bytes. It does not support options stored in sname or file fields.

This API is originally a port from my PERL Net::DHCP api.

Future extensions: IPv6 support, extended data structure TODO...

Author:
Stephan Hadinger
See Also:
Serialized Form

Constructor Summary
DHCPPacket()
          Constructor for the DHCPPacket class.
 
Method Summary
static void appendHostAddress(java.lang.StringBuilder sbuf, java.net.InetAddress addr)
          Even faster version than getHostAddress(java.net.InetAddress) when the address is not the only piece of information put in the string.
 DHCPPacket clone()
          Returns a copy of this DHCPPacket.
 boolean containsOption(byte code)
          Tests whether an option code is present in the packet.
 boolean equals(java.lang.Object o)
          Returns true if 2 instances of DHCPPacket represent the same DHCP packet.
 java.net.InetAddress getAddress()
          Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.
 java.net.InetSocketAddress getAddrPort()
          Syntactic sugar for getAddress/getPort.
 byte[] getChaddr()
          Returns the chaddr field (Client hardware address - typically MAC address).
 java.lang.String getChaddrAsHex()
          Returns the chaddr field (Client hardware address - typically MAC address) as a hex string.
 java.net.InetAddress getCiaddr()
          Returns the ciaddr field (Client IP Address).
 byte[] getCiaddrRaw()
          Returns the ciaddr field (Client IP Address).
 java.lang.String getComment()
          Returns the comment associated to this packet.
 java.lang.Byte getDHCPMessageType()
          Return the DHCP Option Type.
 java.lang.String getFile()
          Returns the file field (Boot File Name) as String.
 byte[] getFileRaw()
          Returns the file field (Boot File Name).
 short getFlags()
          Returns the flags field.
 java.net.InetAddress getGiaddr()
          Returns the giaddr field (Relay agent IP address).
 byte[] getGiaddrRaw()
          Returns the giaddr field (Relay agent IP address).
 HardwareAddress getHardwareAddress()
          Return the hardware address (@MAC) as an HardwareAddress object.
 byte getHlen()
          Returns the hlen field (Hardware address length).
 byte getHops()
          Returns the hops field.
static java.lang.String getHostAddress(java.net.InetAddress addr)
          Faster version than InetAddress.getHostAddress().
 byte getHtype()
          Returns the htype field (Hardware address length).
 byte getOp()
          Returns the op field (Message op code).
 DHCPOption getOption(byte code)
          Returns the option as DHCPOption object.
 java.lang.Byte getOptionAsByte(byte code)
          Returns a DHCP Option as Byte format.
 byte[] getOptionAsBytes(byte code)
          Returns a DHCP Option as Byte array format.
 java.net.InetAddress getOptionAsInetAddr(byte code)
          Returns a DHCP Option as InetAddress format.
 java.net.InetAddress[] getOptionAsInetAddrs(byte code)
          Returns a DHCP Option as InetAddress array format.
 java.lang.Integer getOptionAsInteger(byte code)
          Returns a DHCP Option as Integer format.
 java.lang.Integer getOptionAsNum(byte code)
          Wrapper function for getValueAsNum() in DHCPOption.
 java.lang.Short getOptionAsShort(byte code)
          Returns a DHCP Option as Short format.
 short[] getOptionAsShorts(byte code)
          Returns a DHCP Option as Short array format.
 java.lang.String getOptionAsString(byte code)
          Returns a DHCP Option as String format.
 byte[] getOptionRaw(byte code)
          Returns the option as raw byte[] buffer.
 DHCPOption[] getOptionsArray()
          Return an array of all DHCP options.
 java.util.Collection<DHCPOption> getOptionsCollection()
          Return an ordered list/collection of all options.
static DHCPPacket getPacket(byte[] buf, int offset, int length, boolean strict)
          Factory for creating DHCPPacket objects by parsing a byte[] e.g. from a datagram.
static DHCPPacket getPacket(java.net.DatagramPacket datagram)
          Factory for creating DHCPPacket objects by parsing a DatagramPacket object.
 byte[] getPadding()
          Returns the padding portion of the packet.
 int getPort()
          Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.
 short getSecs()
          Returns the secs field (seconds elapsed).
 java.net.InetAddress getSiaddr()
          Returns the siaddr field (IP address of next server).
 byte[] getSiaddrRaw()
          Returns the siaddr field (IP address of next server).
 java.lang.String getSname()
          Returns the sname field (Optional server host name) as String.
 byte[] getSnameRaw()
          Returns the sname field (Optional server host name).
 int getXid()
          Returns the xid field (Transaction ID).
 java.net.InetAddress getYiaddr()
          Returns the yiaddr field ('your' IP address).
 byte[] getYiaddrRaw()
          Returns the yiaddr field ('your' IP address).
 int hashCode()
          Returns a hash code value for the object.
 boolean isDhcp()
          Returns whether the packet is DHCP or BOOTP.
 boolean isTruncated()
          Indicates that the DHCP packet has been truncated and did not finished with a 0xFF option.
protected  DHCPPacket marshall(byte[] buffer, int offset, int length, java.net.InetAddress address0, int port0, boolean strict)
          Convert a specified byte array containing a DHCP message into a DHCPMessage object.
 void removeAllOptions()
          Remove all options.
 void removeOption(byte opt)
          Remove this option from the options list.
 byte[] serialize()
          Converts the object to a byte array ready to be sent on the wire.
 byte[] serialize(int minSize, int maxSize)
          Converts the object to a byte array ready to be sent on the wire.
 void setAddress(java.net.InetAddress address)
          Sets the IP address of the machine to which this datagram is being sent.
 void setAddrPort(java.net.InetSocketAddress addrPort)
          Syntactic sugar for setAddress/setPort.
 void setChaddr(byte[] chaddr)
          Sets the chaddr field (Client hardware address - typically MAC address).
 void setChaddrHex(java.lang.String hex)
          Sets the chaddr field - from an hex String.
 void setCiaddr(java.net.InetAddress ciaddr)
          Sets the ciaddr field (Client IP Address).
 void setCiaddr(java.lang.String ciaddr)
          Sets the ciaddr field (Client IP Address).
 void setCiaddrRaw(byte[] ciaddr)
          Sets the ciaddr field (Client IP Address).
 void setComment(java.lang.String comment)
          Sets the comment associated to this packet.
 void setDhcp(boolean isDhcp)
          Sets the isDhcp flag.
 void setDHCPMessageType(byte optionType)
          Sets the DHCP Option Type.
 void setFile(java.lang.String file)
          Sets the file field (Boot File Name) as String.
 void setFileRaw(byte[] file)
          Sets the file field (Boot File Name) as String.
 void setFlags(short flags)
          Sets the flags field.
 void setGiaddr(java.net.InetAddress giaddr)
          Sets the giaddr field (Relay agent IP address).
 void setGiaddr(java.lang.String giaddr)
          Sets the giaddr field (Relay agent IP address).
 void setGiaddrRaw(byte[] giaddr)
          Sets the giaddr field (Relay agent IP address).
 void setHlen(byte hlen)
          Sets the hlen field (Hardware address length).
 void setHops(byte hops)
          Sets the hops field.
 void setHtype(byte htype)
          Sets the htype field (Hardware address length).
 void setOp(byte op)
          Sets the op field (Message op code).
 void setOption(DHCPOption opt)
          Sets the option specified for the option.
 void setOptionAsByte(byte code, byte val)
          Sets a DHCP Option as Byte format.
 void setOptionAsInetAddress(byte code, java.net.InetAddress val)
          Sets a DHCP Option as InetAddress format.
 void setOptionAsInetAddress(byte code, java.lang.String val)
          Sets a DHCP Option as InetAddress format.
 void setOptionAsInetAddresses(byte code, java.net.InetAddress[] val)
          Sets a DHCP Option as InetAddress array format.
 void setOptionAsInt(byte code, int val)
          Sets a DHCP Option as Integer format.
 void setOptionAsShort(byte code, short val)
          Sets a DHCP Option as Short format.
 void setOptionAsString(byte code, java.lang.String val)
          Sets a DHCP Option as String format.
 void setOptionRaw(byte code, byte[] buf)
          Sets the option specified for the option.
 void setOptions(java.util.Collection<DHCPOption> opts)
          Sets a Collection of options.
 void setOptions(DHCPOption[] opts)
          Sets an array of options.
 void setPadding(byte[] padding)
          Sets the padding buffer.
 void setPaddingWithZeroes(int length)
          Sets the padding buffer with length zero bytes.
 void setPort(int port)
          Sets the port number on the remote host to which this datagram is being sent.
 void setSecs(short secs)
          Sets the secs field (seconds elapsed).
 void setSiaddr(java.net.InetAddress siaddr)
          Sets the siaddr field (IP address of next server).
 void setSiaddr(java.lang.String siaddr)
          Sets the siaddr field (IP address of next server).
 void setSiaddrRaw(byte[] siaddr)
          Sets the siaddr field (IP address of next server).
 void setSname(java.lang.String sname)
          Sets the sname field (Optional server host name) as String.
 void setSnameRaw(byte[] sname)
          Sets the sname field (Optional server host name) as String.
 void setXid(int xid)
          Sets the xid field (Transaction ID).
 void setYiaddr(java.net.InetAddress yiaddr)
          Sets the yiaddr field ('your' IP address).
 void setYiaddr(java.lang.String yiaddr)
          Sets the yiaddr field ('your' IP address).
 void setYiaddrRaw(byte[] yiaddr)
          Sets the yiaddr field ('your' IP address).
static byte[] stringToBytes(java.lang.String str)
           
 java.lang.String toString()
          Returns a detailed string representation of the DHCP datagram.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DHCPPacket

public DHCPPacket()
Constructor for the DHCPPacket class.

This creates an empty DHCPPacket datagram. All data is default values and the packet is still lacking key data to be sent on the wire.

Method Detail

getPacket

public static DHCPPacket getPacket(java.net.DatagramPacket datagram)
                            throws DHCPBadPacketException
Factory for creating DHCPPacket objects by parsing a DatagramPacket object.

Parameters:
datagram - the UDP datagram received to be parsed
Returns:
the newly create DHCPPacket instance
Throws:
DHCPBadPacketException - the datagram is malformed and cannot be parsed properly.
java.lang.IllegalArgumentException - datagram is null
java.io.IOException

getPacket

public static DHCPPacket getPacket(byte[] buf,
                                   int offset,
                                   int length,
                                   boolean strict)
                            throws DHCPBadPacketException
Factory for creating DHCPPacket objects by parsing a byte[] e.g. from a datagram.

This method allows you to specify non-strict mode which is much more tolerant for packet options. By default, any problem seen during DHCP option parsing causes a DHCPBadPacketException to be thrown.

Parameters:
buf - buffer for holding the incoming datagram.
offset - the offset for the buffer.
length - the number of bytes to read.
strict - do we parse in strict mode?
Returns:
the newly create DHCPPacket instance
Throws:
DHCPBadPacketException - the datagram is malformed.

clone

public DHCPPacket clone()
Returns a copy of this DHCPPacket.

The truncated flag is reset.

Overrides:
clone in class java.lang.Object
Returns:
a copy of the DHCPPacket instance.

equals

public boolean equals(java.lang.Object o)
Returns true if 2 instances of DHCPPacket represent the same DHCP packet.

This is a field by field comparison, except truncated which is ignored.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Returns a hash code value for the object.

Overrides:
hashCode in class java.lang.Object

marshall

protected DHCPPacket marshall(byte[] buffer,
                              int offset,
                              int length,
                              java.net.InetAddress address0,
                              int port0,
                              boolean strict)
Convert a specified byte array containing a DHCP message into a DHCPMessage object.

Parameters:
buffer - byte array to convert to a DHCPMessage object
offset - starting offset for the buffer
length - length of the buffer
address0 - the address from which the packet was sent, or null
port0 - the port from which the packet was sent
strict - do we read in strict mode?
Returns:
a DHCPMessage object with information from byte array.
Throws:
java.lang.IllegalArgumentException - if buffer is null...
java.lang.IndexOutOfBoundsException - offset..offset+length is out of buffer bounds
DHCPBadPacketException - datagram is malformed

serialize

public byte[] serialize()
Converts the object to a byte array ready to be sent on the wire.

Default max size of resulting packet is 576, which is the maximum size a client can accept without explicit notice (option XXX)

Returns:
a byte array with information from DHCPMessage object.
Throws:
DHCPBadPacketException - the datagram would be malformed (too small, too big...)

serialize

public byte[] serialize(int minSize,
                        int maxSize)
Converts the object to a byte array ready to be sent on the wire.

Parameters:
maxSize - the maximum buffer size in bytes
Returns:
a byte array with information from DHCPMessage object.
Throws:
DHCPBadPacketException - the datagram would be malformed (too small, too big...)

toString

public java.lang.String toString()
Returns a detailed string representation of the DHCP datagram.

This multi-line string details: the static, options and padding parts of the object. This is useful for debugging, but not efficient.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the object.

getComment

public java.lang.String getComment()
Returns the comment associated to this packet.

This field can be used freely and has no influence on the real network datagram. It can be used to store a transaction number or any other information

Returns:
the _comment field.

setComment

public void setComment(java.lang.String comment)
Sets the comment associated to this packet.

This field can be used freely and has no influence on the real network datagram. It can be used to store a transaction number or any other information

Parameters:
comment - The comment to set.

getChaddr

public byte[] getChaddr()
Returns the chaddr field (Client hardware address - typically MAC address).

Returns the byte[16] raw buffer. Only the first hlen bytes are valid.

Returns:
the chaddr field.

getHardwareAddress

public HardwareAddress getHardwareAddress()
Return the hardware address (@MAC) as an HardwareAddress object.

Returns:
the HardwareAddress object

getChaddrAsHex

public java.lang.String getChaddrAsHex()
Returns the chaddr field (Client hardware address - typically MAC address) as a hex string.

Only first hlen bytes are printed, as uppercase hex string.

Returns:
the chaddr field as hex string.

setChaddr

public void setChaddr(byte[] chaddr)
Sets the chaddr field (Client hardware address - typically MAC address).

The buffer length should be between 0 and 16, otherwise an IllegalArgumentException is thrown.

If chaddr is null, the field is filled with zeros.

Parameters:
chaddr - The chaddr to set.
Throws:
java.lang.IllegalArgumentException - chaddr buffer is longer than 16 bytes.

setChaddrHex

public void setChaddrHex(java.lang.String hex)
Sets the chaddr field - from an hex String.

Parameters:
hex - the chaddr in hex format

getCiaddr

public java.net.InetAddress getCiaddr()
Returns the ciaddr field (Client IP Address).

Returns:
the ciaddr field converted to InetAddress object.

getCiaddrRaw

public byte[] getCiaddrRaw()
Returns the ciaddr field (Client IP Address).

This is the low-level maximum performance getter for this field.

Returns:
Returns the ciaddr as raw byte[4].

setCiaddr

public void setCiaddr(java.net.InetAddress ciaddr)
Sets the ciaddr field (Client IP Address).

Ths ciaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.

Parameters:
ciaddr - The ciaddr to set.

setCiaddr

public void setCiaddr(java.lang.String ciaddr)
               throws java.net.UnknownHostException
Sets the ciaddr field (Client IP Address).

Parameters:
ciaddr - The ciaddr to set.
Throws:
java.net.UnknownHostException

setCiaddrRaw

public void setCiaddrRaw(byte[] ciaddr)
Sets the ciaddr field (Client IP Address).

ciaddr must be a 4 bytes array, or an IllegalArgumentException is thrown.

This is the low-level maximum performance setter for this field. The array is internally copied so any further modification to ciaddr parameter has no side effect.

Parameters:
ciaddr - The ciaddr to set.

getFileRaw

public byte[] getFileRaw()
Returns the file field (Boot File Name).

Returns the raw byte[128] buffer, containing a null terminated string.

This is the low-level maximum performance getter for this field.

Returns:
the file field.

getFile

public java.lang.String getFile()
Returns the file field (Boot File Name) as String.

Returns:
the file converted to a String (transparent encoding).

setFile

public void setFile(java.lang.String file)
Sets the file field (Boot File Name) as String.

The string is first converted to a byte[] array using transparent encoding. If the resulting buffer size is > 128, an IllegalArgumentException is thrown.

If file parameter is null, the buffer is filled with zeros.

Parameters:
file - The file field to set.
Throws:
java.lang.IllegalArgumentException - string too long

setFileRaw

public void setFileRaw(byte[] file)
Sets the file field (Boot File Name) as String.

If the buffer size is > 128, an IllegalArgumentException is thrown.

If file parameter is null, the buffer is filled with zeros.

This is the low-level maximum performance setter for this field.

Parameters:
file - The file field to set.
Throws:
java.lang.IllegalArgumentException - string too long

getFlags

public short getFlags()
Returns the flags field.

Returns:
the flags field.

setFlags

public void setFlags(short flags)
Sets the flags field.

Parameters:
flags - The flags field to set.

getGiaddr

public java.net.InetAddress getGiaddr()
Returns the giaddr field (Relay agent IP address).

Returns:
the giaddr field converted to InetAddress object.

getGiaddrRaw

public byte[] getGiaddrRaw()
Returns the giaddr field (Relay agent IP address).

This is the low-level maximum performance getter for this field.

Returns:
Returns the giaddr as raw byte[4].

setGiaddr

public void setGiaddr(java.net.InetAddress giaddr)
Sets the giaddr field (Relay agent IP address).

Ths giaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.

Parameters:
giaddr - The giaddr to set.

setGiaddr

public void setGiaddr(java.lang.String giaddr)
               throws java.net.UnknownHostException
Sets the giaddr field (Relay agent IP address).

Parameters:
giaddr - The giaddr to set.
Throws:
java.net.UnknownHostException

setGiaddrRaw

public void setGiaddrRaw(byte[] giaddr)
Sets the giaddr field (Relay agent IP address).

giaddr must be a 4 bytes array, or an IllegalArgumentException is thrown.

This is the low-level maximum performance setter for this field. The array is internally copied so any further modification to ciaddr parameter has no side effect.

Parameters:
giaddr - The giaddr to set.

getHlen

public byte getHlen()
Returns the hlen field (Hardware address length).

Typical value is 6 for ethernet - 6 bytes MAC address.

Returns:
the hlen field.

setHlen

public void setHlen(byte hlen)
Sets the hlen field (Hardware address length).

Typical value is 6 for ethernet - 6 bytes MAC address.

hlen value should be between 0 and 16, but no control is done here.

Parameters:
hlen - The hlen to set.

getHops

public byte getHops()
Returns the hops field.

Returns:
the hops field.

setHops

public void setHops(byte hops)
Sets the hops field.

Parameters:
hops - The hops to set.

getHtype

public byte getHtype()
Returns the htype field (Hardware address length).

Predefined values are:

 HTYPE_ETHER (1)
 HTYPE_IEEE802 (6)
 HTYPE_FDDI (8)
 

Typical value is HTYPE_ETHER.

Returns:
the htype field.

setHtype

public void setHtype(byte htype)
Sets the htype field (Hardware address length).

Predefined values are:

 HTYPE_ETHER (1)
 HTYPE_IEEE802 (6)
 HTYPE_FDDI (8)
 

Typical value is HTYPE_ETHER.

Parameters:
htype - The htype to set.

isDhcp

public boolean isDhcp()
Returns whether the packet is DHCP or BOOTP.

It indicates the presence of the DHCP Magic Cookie at the end of the BOOTP portion.

Default is true for a brand-new object.

Returns:
Returns the isDhcp.

setDhcp

public void setDhcp(boolean isDhcp)
Sets the isDhcp flag.

Indicates whether to generate a DHCP or a BOOTP packet. If true the DHCP Magic Cookie is added after the BOOTP portion and before the DHCP Options.

If isDhcp if false, all DHCP options are ignored when calling serialize().

Default value is true.

Parameters:
isDhcp - The isDhcp to set.

getOp

public byte getOp()
Returns the op field (Message op code).

Predefined values are:

 BOOTREQUEST (1)
 BOOTREPLY (2)
 

Returns:
the op field.

setOp

public void setOp(byte op)
Sets the op field (Message op code).

Predefined values are:

 BOOTREQUEST (1)
 BOOTREPLY (2)
 

Default value is BOOTREPLY, suitable for server replies.

Parameters:
op - The op to set.

getPadding

public byte[] getPadding()
Returns the padding portion of the packet.

This byte array follows the DHCP Options. Normally, its content is irrelevant.

Returns:
Returns the padding.

setPadding

public void setPadding(byte[] padding)
Sets the padding buffer.

This byte array follows the DHCP Options. Normally, its content is irrelevant.

If paddig is null, it is set to an empty buffer.

Padding is automatically added at the end of the datagram when calling serialize() to match DHCP minimal packet size.

Parameters:
padding - The padding to set.

setPaddingWithZeroes

public void setPaddingWithZeroes(int length)
Sets the padding buffer with length zero bytes.

This is a short cut for setPadding(new byte[length]).

Parameters:
length - size of the padding buffer

getSecs

public short getSecs()
Returns the secs field (seconds elapsed).

Returns:
the secs field.

setSecs

public void setSecs(short secs)
Sets the secs field (seconds elapsed).

Parameters:
secs - The secs to set.

getSiaddr

public java.net.InetAddress getSiaddr()
Returns the siaddr field (IP address of next server).

Returns:
the siaddr field converted to InetAddress object.

getSiaddrRaw

public byte[] getSiaddrRaw()
Returns the siaddr field (IP address of next server).

This is the low-level maximum performance getter for this field.

Returns:
Returns the siaddr as raw byte[4].

setSiaddr

public void setSiaddr(java.net.InetAddress siaddr)
Sets the siaddr field (IP address of next server).

Ths siaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.

Parameters:
siaddr - The siaddr to set.

setSiaddr

public void setSiaddr(java.lang.String siaddr)
               throws java.net.UnknownHostException
Sets the siaddr field (IP address of next server).

Parameters:
siaddr - The siaddr to set.
Throws:
java.net.UnknownHostException

setSiaddrRaw

public void setSiaddrRaw(byte[] siaddr)
Sets the siaddr field (IP address of next server).

siaddr must be a 4 bytes array, or an IllegalArgumentException is thrown.

This is the low-level maximum performance setter for this field. The array is internally copied so any further modification to ciaddr parameter has no side effect.

Parameters:
siaddr - The siaddr to set.

getSnameRaw

public byte[] getSnameRaw()
Returns the sname field (Optional server host name).

Returns the raw byte[64] buffer, containing a null terminated string.

This is the low-level maximum performance getter for this field.

Returns:
the sname field.

getSname

public java.lang.String getSname()
Returns the sname field (Optional server host name) as String.

Returns:
the sname converted to a String (transparent encoding).

setSname

public void setSname(java.lang.String sname)
Sets the sname field (Optional server host name) as String.

The string is first converted to a byte[] array using transparent encoding. If the resulting buffer size is > 64, an IllegalArgumentException is thrown.

If sname parameter is null, the buffer is filled with zeros.

Parameters:
sname - The sname field to set.
Throws:
java.lang.IllegalArgumentException - string too long

setSnameRaw

public void setSnameRaw(byte[] sname)
Sets the sname field (Optional server host name) as String.

If the buffer size is > 64, an IllegalArgumentException is thrown.

If sname parameter is null, the buffer is filled with zeros.

This is the low-level maximum performance setter for this field.

Parameters:
sname - The sname field to set.
Throws:
java.lang.IllegalArgumentException - string too long

getXid

public int getXid()
Returns the xid field (Transaction ID).

Returns:
Returns the xid.

setXid

public void setXid(int xid)
Sets the xid field (Transaction ID).

This field is random generated by the client, and used by the client and server to associate requests and responses for the same transaction.

Parameters:
xid - The xid to set.

getYiaddr

public java.net.InetAddress getYiaddr()
Returns the yiaddr field ('your' IP address).

Returns:
the yiaddr field converted to InetAddress object.

getYiaddrRaw

public byte[] getYiaddrRaw()
Returns the yiaddr field ('your' IP address).

This is the low-level maximum performance getter for this field.

Returns:
Returns the yiaddr as raw byte[4].

setYiaddr

public void setYiaddr(java.net.InetAddress yiaddr)
Sets the yiaddr field ('your' IP address).

Ths yiaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.

Parameters:
yiaddr - The yiaddr to set.

setYiaddr

public void setYiaddr(java.lang.String yiaddr)
               throws java.net.UnknownHostException
Sets the yiaddr field ('your' IP address).

Parameters:
yiaddr - The yiaddr to set.
Throws:
java.net.UnknownHostException

setYiaddrRaw

public void setYiaddrRaw(byte[] yiaddr)
Sets the yiaddr field ('your' IP address).

yiaddr must be a 4 bytes array, or an IllegalArgumentException is thrown.

This is the low-level maximum performance setter for this field. The array is internally copied so any further modification to ciaddr parameter has no side effect.

Parameters:
yiaddr - The yiaddr to set.

getDHCPMessageType

public java.lang.Byte getDHCPMessageType()
Return the DHCP Option Type.

This is a short-cut for getOptionAsByte(DHO_DHCP_MESSAGE_TYPE).

Returns:
option type, of null if not present.

setDHCPMessageType

public void setDHCPMessageType(byte optionType)
Sets the DHCP Option Type.

This is a short-cur for setOptionAsByte(DHO_DHCP_MESSAGE_TYPE, optionType);.

Parameters:
optionType -

isTruncated

public boolean isTruncated()
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).

This field is read-only and can be true only with objects created by parsing a Datagram - getPacket() methods.

This field is cleared if the object is cloned.

Returns:
the truncated field.

getOptionAsNum

public java.lang.Integer getOptionAsNum(byte code)
Wrapper function for getValueAsNum() in DHCPOption. Returns a numerical option: int, short or byte.

Parameters:
code - DHCP option code
Returns:
Integer object or null

getOptionAsByte

public java.lang.Byte getOptionAsByte(byte code)
                               throws java.lang.IllegalArgumentException
Returns a DHCP Option as Byte format. This method is only allowed for the following option codes:
 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)
 

Parameters:
code - the option code.
Returns:
the option value, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.
DHCPBadPacketException - the option value in packet is of wrong size.

getOptionAsShort

public java.lang.Short getOptionAsShort(byte code)
                                 throws java.lang.IllegalArgumentException
Returns a DHCP Option as Short format.

This method is only allowed for the following option codes:

 DHO_BOOT_SIZE(13)
 DHO_MAX_DGRAM_REASSEMBLY(22)
 DHO_INTERFACE_MTU(26)
 DHO_DHCP_MAX_MESSAGE_SIZE(57)
 

Parameters:
code - the option code.
Returns:
the option value, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.
DHCPBadPacketException - the option value in packet is of wrong size.

getOptionAsInteger

public java.lang.Integer getOptionAsInteger(byte code)
                                     throws java.lang.IllegalArgumentException
Returns a DHCP Option as Integer format.

This method is only allowed for the following option codes:

 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)
 

Parameters:
code - the option code.
Returns:
the option value, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.
DHCPBadPacketException - the option value in packet is of wrong size.

getOptionAsInetAddr

public java.net.InetAddress getOptionAsInetAddr(byte code)
                                         throws java.lang.IllegalArgumentException
Returns a DHCP Option as InetAddress format.

This method is only allowed for the following option codes:

 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)
 

Parameters:
code - the option code.
Returns:
the option value, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.
DHCPBadPacketException - the option value in packet is of wrong size.

getOptionAsString

public java.lang.String getOptionAsString(byte code)
                                   throws java.lang.IllegalArgumentException
Returns a DHCP Option as String format.

This method is only allowed for the following option codes:

 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)
 

Parameters:
code - the option code.
Returns:
the option value, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

getOptionAsShorts

public short[] getOptionAsShorts(byte code)
                          throws java.lang.IllegalArgumentException
Returns a DHCP Option as Short array format.

This method is only allowed for the following option codes:

 DHO_PATH_MTU_PLATEAU_TABLE(25)
 DHO_NAME_SERVICE_SEARCH(117)
 

Parameters:
code - the option code.
Returns:
the option value array, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.
DHCPBadPacketException - the option value in packet is of wrong size.

getOptionAsInetAddrs

public java.net.InetAddress[] getOptionAsInetAddrs(byte code)
                                            throws java.lang.IllegalArgumentException
Returns a DHCP Option as InetAddress array format.

This method is only allowed for the following option codes:

 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)
 

Parameters:
code - the option code.
Returns:
the option value array, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.
DHCPBadPacketException - the option value in packet is of wrong size.

getOptionAsBytes

public byte[] getOptionAsBytes(byte code)
                        throws java.lang.IllegalArgumentException
Returns a DHCP Option as Byte array format.

This method is only allowed for the following option codes:

 DHO_DHCP_PARAMETER_REQUEST_LIST(55)
 

Note: this mehtod is similar to getOptionRaw, only with option type checking.

Parameters:
code - the option code.
Returns:
the option value array, null if option is not present.
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

setOptionAsByte

public void setOptionAsByte(byte code,
                            byte val)
Sets a DHCP Option as Byte format.

See DHCPOption for allowed option codes.

Parameters:
code - the option code.
val - the value
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

setOptionAsShort

public void setOptionAsShort(byte code,
                             short val)
Sets a DHCP Option as Short format.

See DHCPOption for allowed option codes.

Parameters:
code - the option code.
val - the value
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

setOptionAsInt

public void setOptionAsInt(byte code,
                           int val)
Sets a DHCP Option as Integer format.

See DHCPOption for allowed option codes.

Parameters:
code - the option code.
val - the value
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

setOptionAsInetAddress

public void setOptionAsInetAddress(byte code,
                                   java.net.InetAddress val)
Sets a DHCP Option as InetAddress format.

See DHCPOption for allowed option codes.

Parameters:
code - the option code.
val - the value
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

setOptionAsInetAddress

public void setOptionAsInetAddress(byte code,
                                   java.lang.String val)
                            throws java.net.UnknownHostException
Sets a DHCP Option as InetAddress format.

See DHCPOption for allowed option codes.

Parameters:
code - the option code in String format.
val - the value
Throws:
java.net.UnknownHostException - cannot find the address
java.lang.IllegalArgumentException - the option code is not in the list above.

setOptionAsInetAddresses

public void setOptionAsInetAddresses(byte code,
                                     java.net.InetAddress[] val)
Sets a DHCP Option as InetAddress array format.

See DHCPOption for allowed option codes.

Parameters:
code - the option code.
val - the value array
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

setOptionAsString

public void setOptionAsString(byte code,
                              java.lang.String val)
Sets a DHCP Option as String format.

See DHCPOption for allowed option codes.

Parameters:
code - the option code.
val - the value
Throws:
java.lang.IllegalArgumentException - the option code is not in the list above.

getOptionRaw

public byte[] getOptionRaw(byte code)
Returns the option as raw byte[] buffer.

This is the low-level maximum performance getter for options. No byte[] copy is completed to increase performance.

Parameters:
code - option code
Returns:
Returns the option as raw byte[], or null if the option is not present.

getOption

public DHCPOption getOption(byte code)
Returns the option as DHCPOption object.

This is the low-level maximum performance getter for options. This method is used by every option getter in this object.

Parameters:
code - option code
Returns:
Returns the option as DHCPOption, or null if the option is not present.

containsOption

public boolean containsOption(byte code)
Tests whether an option code is present in the packet.

Parameters:
code - DHCP option code
Returns:
true if option is present

getOptionsCollection

public java.util.Collection<DHCPOption> getOptionsCollection()
Return an ordered list/collection of all options.

The Collection is read-only.

Returns:
collection of DHCPOption.

getOptionsArray

public DHCPOption[] getOptionsArray()
Return an array of all DHCP options.

Returns:
the options array

setOptionRaw

public void setOptionRaw(byte code,
                         byte[] buf)
Sets the option specified for the option.

If buf is null, the option is cleared.

Options are sorted in creation order. Previous values are replaced.

This is the low-level maximum performance setter for options.

Parameters:
code - opt option code, use DHO_* for predefined values.
buf - raw buffer value (cloned). If null, the option is removed.

setOption

public void setOption(DHCPOption opt)
Sets the option specified for the option.

If buf is null, the option is cleared.

Options are sorted in creation order. Previous values are replaced, but their previous position is retained.

This is the low-level maximum performance setter for options. This method is called by all setter methods in this class.

Parameters:
opt - option code, use DHO_* for predefined values.

setOptions

public void setOptions(DHCPOption[] opts)
Sets an array of options. Calles repeatedly setOption on each element of the array.

Parameters:
opts - array of options.

setOptions

public void setOptions(java.util.Collection<DHCPOption> opts)
Sets a Collection of options. Calles repeatedly setOption on each element of the List.

Parameters:
opts - List of options.

removeOption

public void removeOption(byte opt)
Remove this option from the options list.

Parameters:
opt - the option code to remove.

removeAllOptions

public void removeAllOptions()
Remove all options.


getAddress

public java.net.InetAddress getAddress()
Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.

Returns:
the IP address of the machine to which this datagram is being sent or from which the datagram was received. null if no address.

setAddress

public void setAddress(java.net.InetAddress address)
Sets the IP address of the machine to which this datagram is being sent.

Parameters:
address - the InetAddress.
Throws:
java.lang.IllegalArgumentException - address is not of Inet4Address class.

getPort

public int getPort()
Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.

Returns:
the port number on the remote host to which this datagram is being sent or from which the datagram was received.

setPort

public void setPort(int port)
Sets the port number on the remote host to which this datagram is being sent.

Parameters:
port - the port number.

getAddrPort

public java.net.InetSocketAddress getAddrPort()
Syntactic sugar for getAddress/getPort.

Returns:
address + port.

setAddrPort

public void setAddrPort(java.net.InetSocketAddress addrPort)
Syntactic sugar for setAddress/setPort.

Parameters:
addrPort - address and port, if null address is set to null and port to 0

stringToBytes

public static byte[] stringToBytes(java.lang.String str)

appendHostAddress

public static void appendHostAddress(java.lang.StringBuilder sbuf,
                                     java.net.InetAddress addr)
Even faster version than getHostAddress(java.net.InetAddress) when the address is not the only piece of information put in the string.

Parameters:
sbuf - the string builder
addr - the Internet address

getHostAddress

public static java.lang.String getHostAddress(java.net.InetAddress addr)
Faster version than InetAddress.getHostAddress().

Returns:
String representation of address.