|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.dhcp4java.DHCPPacket
public class DHCPPacket
The basic class for manipulating DHCP packets.
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.
Field Octets Description op 1 Message op code / message type.
use constants BOOTREQUEST, BOOTREPLYhtype 1 Hardware address type, see ARP section in "Assigned Numbers" RFC
use constants HTYPE_ETHER, HTYPE_IEEE802, HTYPE_FDDIhlen 1 Hardware address length (e.g. '6' for ethernet). hops 1 Client sets to zero, optionally used by relay agents when booting via a relay agent. xid 4 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. secs 2 Filled in by client, seconds elapsed since client began address acquisition or renewal process. flags 2 Flags (see below). ciaddr 4 Client IP address; only filled in if client is in BOUND, RENEW or REBINDING state and can respond to ARP requests. yiaddr 4 'your' (client) IP address. siaddr 4 IP address of next server to use in bootstrap; returned in DHCPOFFER, DHCPACK by server. giaddr 4 Relay agent IP address, used in booting via a relay agent. chaddr 16 Client hardware address. sname 64 Optional server host name, null terminated string. file 128 Boot file name, null terminated string; "generic" name or null in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER. isDhcp 4 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.
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.
DHCPDISCOVER(1) DHCPOFFER(2) DHCPREQUEST(3) DHCPDECLINE(4) DHCPACK(5) DHCPNAK(6) DHCPRELEASE(7) DHCPINFORM(8) DHCPFORCERENEW(9) DHCPLEASEQUERY(13)
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...
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 |
---|
public DHCPPacket()
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 |
---|
public static DHCPPacket getPacket(java.net.DatagramPacket datagram) throws DHCPBadPacketException
datagram
- the UDP datagram received to be parsed
DHCPBadPacketException
- the datagram is malformed and cannot be parsed properly.
java.lang.IllegalArgumentException
- datagram is null
java.io.IOException
public static DHCPPacket getPacket(byte[] buf, int offset, int length, boolean strict) throws DHCPBadPacketException
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.
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?
DHCPBadPacketException
- the datagram is malformed.public DHCPPacket clone()
The truncated flag is reset.
clone
in class java.lang.Object
public boolean equals(java.lang.Object o)
This is a field by field comparison, except truncated which is ignored.
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
protected DHCPPacket marshall(byte[] buffer, int offset, int length, java.net.InetAddress address0, int port0, boolean strict)
buffer
- byte array to convert to a DHCPMessage objectoffset
- starting offset for the bufferlength
- length of the bufferaddress0
- the address from which the packet was sent, or nullport0
- the port from which the packet was sentstrict
- do we read in strict mode?
java.lang.IllegalArgumentException
- if buffer is null...
java.lang.IndexOutOfBoundsException
- offset..offset+length is out of buffer bounds
DHCPBadPacketException
- datagram is malformedpublic byte[] serialize()
Default max size of resulting packet is 576, which is the maximum size a client can accept without explicit notice (option XXX)
DHCPBadPacketException
- the datagram would be malformed (too small, too big...)public byte[] serialize(int minSize, int maxSize)
maxSize
- the maximum buffer size in bytes
DHCPBadPacketException
- the datagram would be malformed (too small, too big...)public java.lang.String toString()
This multi-line string details: the static, options and padding parts of the object. This is useful for debugging, but not efficient.
toString
in class java.lang.Object
public java.lang.String getComment()
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
public void setComment(java.lang.String comment)
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
comment
- The comment to set.public byte[] getChaddr()
Returns the byte[16] raw buffer. Only the first hlen bytes are valid.
public HardwareAddress getHardwareAddress()
public java.lang.String getChaddrAsHex()
Only first hlen bytes are printed, as uppercase hex string.
public void setChaddr(byte[] chaddr)
The buffer length should be between 0 and 16, otherwise an IllegalArgumentException is thrown.
If chaddr is null, the field is filled with zeros.
chaddr
- The chaddr to set.
java.lang.IllegalArgumentException
- chaddr buffer is longer than 16 bytes.public void setChaddrHex(java.lang.String hex)
hex
- the chaddr in hex formatpublic java.net.InetAddress getCiaddr()
public byte[] getCiaddrRaw()
This is the low-level maximum performance getter for this field.
public void setCiaddr(java.net.InetAddress ciaddr)
Ths ciaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.
ciaddr
- The ciaddr to set.public void setCiaddr(java.lang.String ciaddr) throws java.net.UnknownHostException
ciaddr
- The ciaddr to set.
java.net.UnknownHostException
public void setCiaddrRaw(byte[] ciaddr)
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.
ciaddr
- The ciaddr to set.public byte[] getFileRaw()
Returns the raw byte[128] buffer, containing a null terminated string.
This is the low-level maximum performance getter for this field.
public java.lang.String getFile()
public void setFile(java.lang.String file)
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.
file
- The file field to set.
java.lang.IllegalArgumentException
- string too longpublic void setFileRaw(byte[] file)
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.
file
- The file field to set.
java.lang.IllegalArgumentException
- string too longpublic short getFlags()
public void setFlags(short flags)
flags
- The flags field to set.public java.net.InetAddress getGiaddr()
public byte[] getGiaddrRaw()
This is the low-level maximum performance getter for this field.
public void setGiaddr(java.net.InetAddress giaddr)
Ths giaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.
giaddr
- The giaddr to set.public void setGiaddr(java.lang.String giaddr) throws java.net.UnknownHostException
giaddr
- The giaddr to set.
java.net.UnknownHostException
public void setGiaddrRaw(byte[] giaddr)
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.
giaddr
- The giaddr to set.public byte getHlen()
Typical value is 6 for ethernet - 6 bytes MAC address.
public void setHlen(byte hlen)
Typical value is 6 for ethernet - 6 bytes MAC address.
hlen value should be between 0 and 16, but no control is done here.
hlen
- The hlen to set.public byte getHops()
public void setHops(byte hops)
hops
- The hops to set.public byte getHtype()
Predefined values are:
HTYPE_ETHER (1) HTYPE_IEEE802 (6) HTYPE_FDDI (8)
Typical value is HTYPE_ETHER.
public void setHtype(byte htype)
Predefined values are:
HTYPE_ETHER (1) HTYPE_IEEE802 (6) HTYPE_FDDI (8)
Typical value is HTYPE_ETHER.
htype
- The htype to set.public boolean isDhcp()
It indicates the presence of the DHCP Magic Cookie at the end of the BOOTP portion.
Default is true for a brand-new object.
public void setDhcp(boolean isDhcp)
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.
isDhcp
- The isDhcp to set.public byte getOp()
Predefined values are:
BOOTREQUEST (1) BOOTREPLY (2)
public void setOp(byte op)
Predefined values are:
BOOTREQUEST (1) BOOTREPLY (2)
Default value is BOOTREPLY, suitable for server replies.
op
- The op to set.public byte[] getPadding()
This byte array follows the DHCP Options. Normally, its content is irrelevant.
public void setPadding(byte[] padding)
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.
padding
- The padding to set.public void setPaddingWithZeroes(int length)
This is a short cut for setPadding(new byte[length]).
length
- size of the padding bufferpublic short getSecs()
public void setSecs(short secs)
secs
- The secs to set.public java.net.InetAddress getSiaddr()
public byte[] getSiaddrRaw()
This is the low-level maximum performance getter for this field.
public void setSiaddr(java.net.InetAddress siaddr)
Ths siaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.
siaddr
- The siaddr to set.public void setSiaddr(java.lang.String siaddr) throws java.net.UnknownHostException
siaddr
- The siaddr to set.
java.net.UnknownHostException
public void setSiaddrRaw(byte[] siaddr)
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.
siaddr
- The siaddr to set.public byte[] getSnameRaw()
Returns the raw byte[64] buffer, containing a null terminated string.
This is the low-level maximum performance getter for this field.
public java.lang.String getSname()
public void setSname(java.lang.String sname)
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.
sname
- The sname field to set.
java.lang.IllegalArgumentException
- string too longpublic void setSnameRaw(byte[] sname)
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.
sname
- The sname field to set.
java.lang.IllegalArgumentException
- string too longpublic int getXid()
public void setXid(int xid)
This field is random generated by the client, and used by the client and server to associate requests and responses for the same transaction.
xid
- The xid to set.public java.net.InetAddress getYiaddr()
public byte[] getYiaddrRaw()
This is the low-level maximum performance getter for this field.
public void setYiaddr(java.net.InetAddress yiaddr)
Ths yiaddr field must be of Inet4Address class or an IllegalArgumentException is thrown.
yiaddr
- The yiaddr to set.public void setYiaddr(java.lang.String yiaddr) throws java.net.UnknownHostException
yiaddr
- The yiaddr to set.
java.net.UnknownHostException
public void setYiaddrRaw(byte[] yiaddr)
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.
yiaddr
- The yiaddr to set.public java.lang.Byte getDHCPMessageType()
This is a short-cut for getOptionAsByte(DHO_DHCP_MESSAGE_TYPE).
public void setDHCPMessageType(byte optionType)
This is a short-cur for setOptionAsByte(DHO_DHCP_MESSAGE_TYPE, optionType);.
optionType
- public boolean isTruncated()
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.
public java.lang.Integer getOptionAsNum(byte code)
code
- DHCP option code
public java.lang.Byte getOptionAsByte(byte code) throws java.lang.IllegalArgumentException
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)
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.
DHCPBadPacketException
- the option value in packet is of wrong size.public java.lang.Short getOptionAsShort(byte code) throws java.lang.IllegalArgumentException
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)
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.
DHCPBadPacketException
- the option value in packet is of wrong size.public java.lang.Integer getOptionAsInteger(byte code) throws java.lang.IllegalArgumentException
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)
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.
DHCPBadPacketException
- the option value in packet is of wrong size.public java.net.InetAddress getOptionAsInetAddr(byte code) throws java.lang.IllegalArgumentException
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)
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.
DHCPBadPacketException
- the option value in packet is of wrong size.public java.lang.String getOptionAsString(byte code) throws java.lang.IllegalArgumentException
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)
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.public short[] getOptionAsShorts(byte code) throws java.lang.IllegalArgumentException
This method is only allowed for the following option codes:
DHO_PATH_MTU_PLATEAU_TABLE(25) DHO_NAME_SERVICE_SEARCH(117)
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.
DHCPBadPacketException
- the option value in packet is of wrong size.public java.net.InetAddress[] getOptionAsInetAddrs(byte code) throws java.lang.IllegalArgumentException
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)
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.
DHCPBadPacketException
- the option value in packet is of wrong size.public byte[] getOptionAsBytes(byte code) throws java.lang.IllegalArgumentException
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.
code
- the option code.
java.lang.IllegalArgumentException
- the option code is not in the list above.public void setOptionAsByte(byte code, byte val)
See DHCPOption for allowed option codes.
code
- the option code.val
- the value
java.lang.IllegalArgumentException
- the option code is not in the list above.public void setOptionAsShort(byte code, short val)
See DHCPOption for allowed option codes.
code
- the option code.val
- the value
java.lang.IllegalArgumentException
- the option code is not in the list above.public void setOptionAsInt(byte code, int val)
See DHCPOption for allowed option codes.
code
- the option code.val
- the value
java.lang.IllegalArgumentException
- the option code is not in the list above.public void setOptionAsInetAddress(byte code, java.net.InetAddress val)
See DHCPOption for allowed option codes.
code
- the option code.val
- the value
java.lang.IllegalArgumentException
- the option code is not in the list above.public void setOptionAsInetAddress(byte code, java.lang.String val) throws java.net.UnknownHostException
See DHCPOption for allowed option codes.
code
- the option code in String format.val
- the value
java.net.UnknownHostException
- cannot find the address
java.lang.IllegalArgumentException
- the option code is not in the list above.public void setOptionAsInetAddresses(byte code, java.net.InetAddress[] val)
See DHCPOption for allowed option codes.
code
- the option code.val
- the value array
java.lang.IllegalArgumentException
- the option code is not in the list above.public void setOptionAsString(byte code, java.lang.String val)
See DHCPOption for allowed option codes.
code
- the option code.val
- the value
java.lang.IllegalArgumentException
- the option code is not in the list above.public byte[] getOptionRaw(byte code)
This is the low-level maximum performance getter for options. No byte[] copy is completed to increase performance.
code
- option code
public DHCPOption getOption(byte code)
This is the low-level maximum performance getter for options. This method is used by every option getter in this object.
code
- option code
public boolean containsOption(byte code)
code
- DHCP option code
public java.util.Collection<DHCPOption> getOptionsCollection()
The Collection is read-only.
public DHCPOption[] getOptionsArray()
public void setOptionRaw(byte code, byte[] buf)
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.
code
- opt option code, use DHO_* for predefined values.buf
- raw buffer value (cloned). If null, the option is removed.public void setOption(DHCPOption opt)
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.
opt
- option code, use DHO_* for predefined values.public void setOptions(DHCPOption[] opts)
opts
- array of options.public void setOptions(java.util.Collection<DHCPOption> opts)
opts
- List of options.public void removeOption(byte opt)
opt
- the option code to remove.public void removeAllOptions()
public java.net.InetAddress getAddress()
public void setAddress(java.net.InetAddress address)
address
- the InetAddress.
java.lang.IllegalArgumentException
- address is not of Inet4Address class.public int getPort()
public void setPort(int port)
port
- the port number.public java.net.InetSocketAddress getAddrPort()
public void setAddrPort(java.net.InetSocketAddress addrPort)
addrPort
- address and port, if null address is set to null and port to 0public static byte[] stringToBytes(java.lang.String str)
public static void appendHostAddress(java.lang.StringBuilder sbuf, java.net.InetAddress addr)
getHostAddress(java.net.InetAddress)
when the address is not
the only piece of information put in the string.
sbuf
- the string builderaddr
- the Internet addresspublic static java.lang.String getHostAddress(java.net.InetAddress addr)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |