public class DHCPOption extends Object implements Serializable
| Constructor and Description |
|---|
DHCPOption(byte code,
byte[] value)
Constructor for
DHCPOption. |
DHCPOption(byte code,
byte[] value,
boolean mirror)
Constructor for
DHCPOption. |
| Modifier and Type | Method and Description |
|---|---|
static Map<Byte,String> |
agentOptionsToMap(byte[] buf)
Converts DHO_DHCP_AGENT_OPTIONS (82) option type to a LinkedMap.
|
static String |
agentOptionsToString(byte[] buf)
Converts DHO_DHCP_AGENT_OPTIONS (82) option type to a printable string
|
static byte[] |
agentOptionToRaw(Map<Byte,String> map)
Converts Map<Byte,String> to DHO_DHCP_AGENT_OPTIONS (82) option.
|
void |
append(StringBuilder buffer)
Appends to this string builder a detailed string representation of the DHCP
datagram.
|
DHCPOption |
applyOption(DHCPPacket request)
Get the option value based on the context, i.e. the client's request.
|
static byte[] |
byte2Bytes(byte val) |
boolean |
equals(Object o)
returns true if two
DHCPOption objects are equal, i.e. have
same code and same value. |
byte |
getCode()
Return the
code field (byte). |
static Class<?> |
getOptionFormat(byte code)
Returns the type of the option based on the option code.
|
byte[] |
getValue() |
byte |
getValueAsByte()
Returns a DHCP Option as Byte format.
|
byte[] |
getValueAsBytes()
Returns a DHCP Option as Byte array format.
|
InetAddress |
getValueAsInetAddr()
Returns a DHCP Option as InetAddress format.
|
InetAddress[] |
getValueAsInetAddrs()
Returns a DHCP Option as InetAddress array format.
|
int |
getValueAsInt()
Returns a DHCP Option as Integer format.
|
Integer |
getValueAsNum()
Returns a DHCP Option as Integer format, but is usable for any numerical
type: int, short or byte.
|
short |
getValueAsShort()
Returns a DHCP Option as Short format.
|
short[] |
getValueAsShorts()
Returns a DHCP Option as Short array format.
|
String |
getValueAsString()
Returns a DHCP Option as String format.
|
byte[] |
getValueFast() |
int |
hashCode()
Returns hashcode.
|
static byte[] |
inetAddress2Bytes(InetAddress val) |
static byte[] |
inetAddresses2Bytes(InetAddress[] val) |
static byte[] |
int2Bytes(int val) |
boolean |
isMirror()
Returns whether the option is marked as "mirror", meaning it should mirror
the option value in the client request.
|
static boolean |
isOptionAsByte(byte code) |
static boolean |
isOptionAsBytes(byte code) |
static boolean |
isOptionAsInetAddr(byte code) |
static boolean |
isOptionAsInetAddrs(byte code) |
static boolean |
isOptionAsInt(byte code) |
static boolean |
isOptionAsShort(byte code) |
static boolean |
isOptionAsShorts(byte code) |
static boolean |
isOptionAsString(byte code) |
static void |
main(String[] args) |
static DHCPOption |
newOptionAsByte(byte code,
byte val)
Creates a DHCP Option as Byte format.
|
static DHCPOption |
newOptionAsInetAddress(byte code,
InetAddress val)
Sets a DHCP Option as InetAddress format.
|
static DHCPOption |
newOptionAsInetAddresses(byte code,
InetAddress[] val)
Creates a DHCP Option as InetAddress array format.
|
static DHCPOption |
newOptionAsInt(byte code,
int val)
Creates a DHCP Option as Integer format.
|
static DHCPOption |
newOptionAsShort(byte code,
short val)
Creates a DHCP Option as Short format.
|
static DHCPOption |
newOptionAsShorts(byte code,
short[] arr)
Creates a DHCP Options as Short[] format.
|
static DHCPOption |
newOptionAsString(byte code,
String val)
Creates a DHCP Option as String format.
|
static DHCPOption |
parseNewOption(byte code,
Class<?> format,
String sValue)
Parse an option from a pure string representation.
|
static byte[] |
short2Bytes(short val) |
static Class<?> |
string2Class(String className)
Simple method for converting from string to supported class format.
|
static byte[] |
stringListToUserClass(List<String> list)
Converts this list of strings to a DHO_USER_CLASS (77) option.
|
String |
toString()
Returns a detailed string representation of the DHCP datagram.
|
static List<String> |
userClassToList(byte[] buf)
Convert DHO_USER_CLASS (77) option to a List.
|
static String |
userClassToString(byte[] buf)
Converts DHO_USER_CLASS (77) option to a printable string
|
public DHCPOption(byte code,
byte[] value,
boolean mirror)
DHCPOption.
Note: you must not prefix the value by a length-byte. The length prefix will be added automatically by the API.
If value is null it is considered as an empty option. If you
add an empty option to a DHCPPacket, it removes the option from the packet.
This constructor adds a parameter to mark the option as "mirror". See comments above.
code - DHCP option codevalue - DHCP option value as a byte array.mirror - having a mirroring behaviour.public DHCPOption(byte code,
byte[] value)
DHCPOption. This is the default constructor.
Note: you must not prefix the value by a length-byte. The length prefix will be added automatically by the API.
If value is null it is considered as an empty option. If you
add an empty option to a DHCPPacket, it removes the option from the packet.
code - DHCP option codevalue - DHCP option value as a byte array.public byte getCode()
code field (byte).public boolean equals(Object o)
DHCPOption objects are equal, i.e. have
same code and same value.public int hashCode()
hashCode in class ObjectObject.hashCode()public byte[] getValue()
public byte[] getValueFast()
null. Minimal value is
byte[0].public boolean isMirror()
To be used only in servers.
public static final boolean isOptionAsByte(byte code)
public static DHCPOption newOptionAsByte(byte code, byte val)
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)
code - the option code.val - the valueIllegalArgumentException - the option code is not in the list above.public byte getValueAsByte()
throws 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)
null if option is not present.IllegalArgumentException - the option code is not in the list above.DHCPBadPacketException - the option value in packet is of wrong size.public static final boolean isOptionAsShort(byte code)
public short getValueAsShort()
throws 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)
null if option is not present.IllegalArgumentException - the option code is not in the list above.DHCPBadPacketException - the option value in packet is of wrong size.public static final boolean isOptionAsInt(byte code)
public int getValueAsInt()
throws 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)
null if option is not present.IllegalArgumentException - the option code is not in the list above.DHCPBadPacketException - the option value in packet is of wrong size.public Integer getValueAsNum()
There is no check on the option
null if option is not present, or
wrong number of bytes.public static final boolean isOptionAsInetAddr(byte code)
public InetAddress getValueAsInetAddr() throws 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)
null if option is not present.IllegalArgumentException - the option code is not in the list above.DHCPBadPacketException - the option value in packet is of wrong size.public static final boolean isOptionAsString(byte code)
public String getValueAsString() throws 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)
null if option is not present.IllegalArgumentException - the option code is not in the list above.public static final boolean isOptionAsShorts(byte code)
public short[] getValueAsShorts()
throws IllegalArgumentException
This method is only allowed for the following option codes:
DHO_PATH_MTU_PLATEAU_TABLE(25) DHO_NAME_SERVICE_SEARCH(117)
null if option is not present.IllegalArgumentException - the option code is not in the list above.DHCPBadPacketException - the option value in packet is of wrong size.public static final boolean isOptionAsInetAddrs(byte code)
public InetAddress[] getValueAsInetAddrs() throws 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)
null if option is not present.IllegalArgumentException - the option code is not in the list above.DHCPBadPacketException - the option value in packet is of wrong size.public static final boolean isOptionAsBytes(byte code)
public byte[] getValueAsBytes()
throws 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.
null if option is not present.IllegalArgumentException - the option code is not in the list above.public static DHCPOption newOptionAsShort(byte code, short val)
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.val - the valuenullIllegalArgumentException - the option code is not in the list above.public static DHCPOption newOptionAsShorts(byte code, short[] arr)
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.arr - the array of shortsnullIllegalArgumentException - the option code is not in the list above.public static DHCPOption newOptionAsInt(byte code, int val)
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.val - the valuenullIllegalArgumentException - the option code is not in the list above.public static DHCPOption newOptionAsInetAddress(byte code, InetAddress val)
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)and also as a simplified version for setOptionAsInetAddresses
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.val - the valuenullIllegalArgumentException - the option code is not in the list above.public static DHCPOption newOptionAsInetAddresses(byte code, InetAddress[] val)
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.val - the value arraynullIllegalArgumentException - the option code is not in the list above.public static DHCPOption newOptionAsString(byte code, String val)
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.val - the valuenullIllegalArgumentException - the option code is not in the list above.public DHCPOption applyOption(DHCPPacket request)
This should be the only method used with this class to get relevant values.
request - the client's DHCP requetsNullPointerException - if request is null.public void append(StringBuilder buffer)
This multi-line string details: the static, options and padding parts of the object. This is useful for debugging, but not efficient.
buffer - the string builder the string representation of this object should
be appended.public String toString()
This multi-line string details: the static, options and padding parts of the object. This is useful for debugging, but not efficient.
public static byte[] byte2Bytes(byte val)
public static byte[] short2Bytes(short val)
public static byte[] int2Bytes(int val)
public static byte[] inetAddress2Bytes(InetAddress val)
public static byte[] inetAddresses2Bytes(InetAddress[] val)
public static List<String> userClassToList(byte[] buf)
buf - option value of type User Class.public static String userClassToString(byte[] buf)
buf - option value of type User Class.public static byte[] stringListToUserClass(List<String> list)
list - the list of stringssetOptionRaw,
null if list is nullIllegalArgumentException - if List contains anything else than Stringpublic static String agentOptionsToString(byte[] buf)
buf - option value of type Agent Option.public static byte[] agentOptionToRaw(Map<Byte,String> map)
LinkedHashMap are preferred as they preserve insertion order. Regular HashMap order is random.
map - Map<Byte,String> couplessetOptionRawIllegalArgumentException - if List contains anything else than Stringpublic static final Map<Byte,String> agentOptionsToMap(byte[] buf)
Order of parameters is preserved (use avc LinkedHashmap). Keys
are of type Byte, values are of type String.
buf - byte[] buffer returned by getOptionRawnull if buf is
nullpublic static Class<?> getOptionFormat(byte code)
The type is returned as a Class object:
InetAddress.classInetAddress[].classint.classshort.classshort[].classbyte.classbyte[].classString.class
Please use getSimpleName() method of Class object
for the String representation.
code - the DHCP option codepublic static Class<?> string2Class(String className)
Support values are:
className - name of the data format (see above)Class or null if not supportedpublic static DHCPOption parseNewOption(byte code, Class<?> format, String sValue)
The expected class is passed as a parameter, and can be provided by the
string2Class() method from a string representation of the
class.
TODO examples
code - DHCP option codeformat - expected Java Class after conversionsValue - string representation of the valuepublic static void main(String[] args)
Copyright © 2018 Philip Helger. All rights reserved.