📄 dhcpoption.java
字号:
* 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)
* </pre>
*
* @return the option value, <tt>null</tt> if option is not present.
* @throws IllegalArgumentException the option code is not in the list above.
* @throws DHCPBadPacketException the option value in packet is of wrong size.
*/
public InetAddress getValueAsInetAddr() throws IllegalArgumentException {
if (!isOptionAsInetAddr(code)) {
throw new IllegalArgumentException("DHCP option type ("+ this.code +") is not InetAddr");
}
if (this.value == null) {
throw new IllegalStateException("value is null");
}
if (this.value.length != 4) {
throw new DHCPBadPacketException("option " + this.code + " is wrong size:" + this.value.length + " should be 4");
}
try {
return InetAddress.getByAddress(this.value);
} catch (UnknownHostException e) {
logger.log(Level.SEVERE, "Unexpected UnknownHostException", e);
return null; // normally impossible
}
}
public static final boolean isOptionAsString(byte code) {
return OptionFormat.STRING.equals(_DHO_FORMATS.get(code));
}
/**
* Returns a DHCP Option as String format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* 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)
* </pre>
*
* @return the option value, <tt>null</tt> if option is not present.
* @throws IllegalArgumentException the option code is not in the list above.
*/
public String getValueAsString() throws IllegalArgumentException {
if (!isOptionAsString(code)) {
throw new IllegalArgumentException("DHCP option type ("+ this.code +") is not String");
}
if (this.value == null) {
throw new IllegalStateException("value is null");
}
return DHCPPacket.bytesToString(this.value);
}
public static final boolean isOptionAsShorts(byte code) {
return OptionFormat.SHORTS.equals(_DHO_FORMATS.get(code));
}
/**
* Returns a DHCP Option as Short array format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* DHO_PATH_MTU_PLATEAU_TABLE(25)
* DHO_NAME_SERVICE_SEARCH(117)
* </pre>
*
* @return the option value array, <tt>null</tt> if option is not present.
* @throws IllegalArgumentException the option code is not in the list above.
* @throws DHCPBadPacketException the option value in packet is of wrong size.
*/
public short[] getValueAsShorts() throws IllegalArgumentException {
if (!isOptionAsShorts(code)) {
throw new IllegalArgumentException("DHCP option type ("+ this.code +") is not short[]");
}
if (this.value == null) {
throw new IllegalStateException("value is null");
}
if ((this.value.length % 2) != 0) // multiple of 2
{
throw new DHCPBadPacketException("option " + this.code + " is wrong size:" + this.value.length + " should be 2*X");
}
short[] shorts = new short[this.value.length / 2];
for (int i=0, a=0; a< this.value.length; i++, a+=2) {
shorts[i] = (short) (((this.value[a] & 0xFF)<<8) | (this.value[a+1] & 0xFF));
}
return shorts;
}
public static final boolean isOptionAsInetAddrs(byte code) {
return OptionFormat.INETS.equals(_DHO_FORMATS.get(code));
}
/**
* Returns a DHCP Option as InetAddress array format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* 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)
* </pre>
*
* @return the option value array, <tt>null</tt> if option is not present.
* @throws IllegalArgumentException the option code is not in the list above.
* @throws DHCPBadPacketException the option value in packet is of wrong size.
*/
public InetAddress[] getValueAsInetAddrs() throws IllegalArgumentException {
if (!isOptionAsInetAddrs(code)) {
throw new IllegalArgumentException("DHCP option type ("+ this.code +") is not InetAddr[]");
}
if (this.value == null) {
throw new IllegalStateException("value is null");
}
if ((this.value.length % 4) != 0) // multiple of 4
{
throw new DHCPBadPacketException("option " + this.code + " is wrong size:" + this.value.length + " should be 4*X");
}
try {
byte[] addr = new byte[4];
InetAddress[] addrs = new InetAddress[this.value.length / 4];
for (int i=0, a=0; a< this.value.length; i++, a+=4) {
addr[0] = this.value[a];
addr[1] = this.value[a+1];
addr[2] = this.value[a+2];
addr[3] = this.value[a+3];
addrs[i] = InetAddress.getByAddress(addr);
}
return addrs;
} catch (UnknownHostException e) {
logger.log(Level.SEVERE, "Unexpected UnknownHostException", e);
return null; // normally impossible
}
}
public static final boolean isOptionAsBytes(byte code) {
return OptionFormat.BYTES.equals(_DHO_FORMATS.get(code));
}
/**
* Returns a DHCP Option as Byte array format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* DHO_DHCP_PARAMETER_REQUEST_LIST(55)
* </pre>
*
* <p>Note: this mehtod is similar to getOptionRaw, only with option type checking.
*
* @return the option value array, <tt>null</tt> if option is not present.
* @throws IllegalArgumentException the option code is not in the list above.
*/
public byte[] getValueAsBytes() throws IllegalArgumentException {
if (!isOptionAsBytes(code)) {
throw new IllegalArgumentException("DHCP option type ("+ this.code +") is not bytes");
}
if (this.value == null) {
throw new IllegalStateException("value is null");
}
return this.getValue();
}
/**
* Creates a DHCP Option as Short format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* DHO_BOOT_SIZE(13)
* DHO_MAX_DGRAM_REASSEMBLY(22)
* DHO_INTERFACE_MTU(26)
* DHO_DHCP_MAX_MESSAGE_SIZE(57)
* </pre>
*
* @param code the option code.
* @param val the value
* @throws IllegalArgumentException the option code is not in the list above.
*/
public static DHCPOption newOptionAsShort(byte code, short val) {
if (!isOptionAsShort(code)) {
throw new IllegalArgumentException("DHCP option type ("+code+") is not short");
}
return new DHCPOption(code, short2Bytes(val));
}
/**
* Creates a DHCP Options as Short[] format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* DHO_PATH_MTU_PLATEAU_TABLE(25)
* DHO_NAME_SERVICE_SEARCH(117)
* </pre>
*
* @param code the option code.
* @param arr the array of shorts
* @throws IllegalArgumentException the option code is not in the list above.
*/
public static DHCPOption newOptionAsShorts(byte code, short[] arr) {
if (!isOptionAsShorts(code)) {
throw new IllegalArgumentException("DHCP option type ("+code+") is not shorts");
}
byte[] buf = null;
if (arr != null) {
buf = new byte[arr.length * 2];
for (int i=0; i<arr.length; i++) {
short val = arr[i];
buf[i*2] = (byte) ((val & 0xFF00) >>> 8);
buf[i*2+1] = (byte) (val & 0XFF);
}
}
return new DHCPOption(code, buf);
}
/**
* Creates a DHCP Option as Integer format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* 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)
* </pre>
*
* @param code the option code.
* @param val the value
* @throws IllegalArgumentException the option code is not in the list above.
*/
public static DHCPOption newOptionAsInt(byte code, int val) {
if (!isOptionAsInt(code)) {
throw new IllegalArgumentException("DHCP option type ("+code+") is not int");
}
return new DHCPOption(code, int2Bytes(val));
}
/**
* Sets a DHCP Option as InetAddress format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* 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)
* </pre>
* and also as a simplified version for setOptionAsInetAddresses
* <pre>
* 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)
* </pre>
*
* @param code the option code.
* @param val the value
* @throws IllegalArgumentException the option code is not in the list above.
*/
public static DHCPOption newOptionAsInetAddress(byte code, InetAddress val) {
if ((!isOptionAsInetAddr(code)) &&
(!isOptionAsInetAddrs(code))) {
throw new IllegalArgumentException("DHCP option type ("+code+") is not InetAddress");
}
return new DHCPOption(code, inetAddress2Bytes(val));
}
/**
* Creates a DHCP Option as InetAddress array format.
*
* <p>This method is only allowed for the following option codes:
* <pre>
* 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -