| 1 |
package com.github.markusbernhardt.proxy.search.wpad.dhcp; |
| 2 |
|
| 3 |
import java.util.Enumeration; |
| 4 |
import java.util.Hashtable; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class DHCPOptions { |
| 15 |
public static final int OPTION_PAD = 0; |
| 16 |
public static final int OPTION_NETMASK = 1; |
| 17 |
public static final int OPTION_TIME_OFFSET = 2; |
| 18 |
public static final int OPTION_ROUTERS = 3; |
| 19 |
public static final int OPTION_TIME_SERVERS = 4; |
| 20 |
public static final int OPTION_NAME_SERVERS = 5; |
| 21 |
public static final int OPTION_DNS_SERVERS = 6; |
| 22 |
public static final int OPTION_LOG_SERVERS = 7; |
| 23 |
public static final int OPTION_COOKIE_SERVERS = 8; |
| 24 |
public static final int OPTION_LPR_SERVERS = 9; |
| 25 |
public static final int OPTION_IMPRESS_SERVERS = 10; |
| 26 |
public static final int OPTION_RESSOURCE_LOCATION_SERVERS = 11; |
| 27 |
public static final int OPTION_HOSTNAME = 12; |
| 28 |
public static final int OPTION_BOOT_FILESIZE = 13; |
| 29 |
public static final int OPTION_MERIT_DUMPFILE = 14; |
| 30 |
public static final int OPTION_DOMAIN_NAME = 15; |
| 31 |
public static final int OPTION_SWAP_SERVER = 16; |
| 32 |
public static final int OPTION_ROOT_PATH = 17; |
| 33 |
public static final int OPTION_EXTENSIONS_PATH = 18; |
| 34 |
public static final int OPTION_END = 255; |
| 35 |
|
| 36 |
public static final int OPTION_IP_HOST_FORWARDING_ENABLE = 19; |
| 37 |
public static final int OPTION_IP_HOST_NON_LOCAL_SOURCE_ROUTING_ENABLE = 20; |
| 38 |
public static final int OPTION_IP_HOST_POLICY_FILTERS = 21; |
| 39 |
public static final int OPTION_IP_HOST_MAXIMUM_DATAGRAM_REASSEMBLY_SIZE = 22; |
| 40 |
public static final int OPTION_IP_HOST_DEFAULT_TTL = 23; |
| 41 |
public static final int OPTION_IP_HOST_MTU_AGEING_TIMEOUT = 24; |
| 42 |
public static final int OPTION_IP_HOST_MTU_PLATEAU_TABLE = 25; |
| 43 |
|
| 44 |
public static final int OPTION_IP_INTERFACE_MTU = 26; |
| 45 |
public static final int OPTION_IP_INTERFACE_ALL_SUBNETS_LOCAL_ENABLE = 27; |
| 46 |
public static final int OPTION_IP_INTERFACE_BROADCAST_ADDRESS = 28; |
| 47 |
public static final int OPTION_IP_INTERFACE_PERFORM_MASK_DISCOVERY_ENABLE = 29; |
| 48 |
public static final int OPTION_IP_INTERFACE_MASK_SUPPLIER_ENABLE = 30; |
| 49 |
public static final int OPTION_IP_INTERFACE_PERFORM_ROUTER_DISCOVERY_ENABLE = 31; |
| 50 |
public static final int OPTION_IP_INTERFACE_ROUTER_SOLICITATION_ADDRESS = 32; |
| 51 |
public static final int OPTION_IP_INTERFACE_STATIC_ROUTES = 33; |
| 52 |
|
| 53 |
public static final int OPTION_LINK_TRAILER_ENCAPSULATION_ENABLE = 34; |
| 54 |
public static final int OPTION_LINK_ARP_CACHE_TIMEOUT = 35; |
| 55 |
public static final int OPTION_LINK_ETHERNET_ENCAPSULATION_ENABLE = 36; |
| 56 |
|
| 57 |
public static final int OPTION_TCP_DEFAULT_TTL = 37; |
| 58 |
public static final int OPTION_TCP_KEEP_ALIVE_INTERVAL = 38; |
| 59 |
public static final int OPTION_TCP_KEEP_ALIVE_GERBAGE_ENABLE = 39; |
| 60 |
|
| 61 |
public static final int OPTION_NIS_DOMAIN = 40; |
| 62 |
public static final int OPTION_NIS_SERVERS = 41; |
| 63 |
public static final int OPTION_NTP_SERVERS = 42; |
| 64 |
|
| 65 |
public static final int OPTION_SERVICE_VENDOR_SPECIFIC_INFORMATIONS = 43; |
| 66 |
public static final int OPTION_SERVICE_NETBOIS_NAME_SERVERS = 44; |
| 67 |
public static final int OPTION_SERVICE_NETBOIS_DATAGRAM_DISTRIBUTION_SERVERS = 45; |
| 68 |
public static final int OPTION_SERVICE_NETBOIS_NODE_TYPE = 46; |
| 69 |
public static final int OPTION_SERVICE_NETBOIS_SCOPE_TYPE = 47; |
| 70 |
public static final int OPTION_SERVICE_X_FONT_SERVERS = 48; |
| 71 |
public static final int OPTION_SERVICE_X_DISPLAY_MANAGERS = 49; |
| 72 |
|
| 73 |
public static final int OPTION_DHCP_IP_ADRESS_REQUESTED = 50; |
| 74 |
public static final int OPTION_DHCP_IP_LEASE_TIME = 51; |
| 75 |
public static final int OPTION_DHCP_OVERLOAD = 52; |
| 76 |
public static final int OPTION_DHCP_MESSAGE_TYPE = 53; |
| 77 |
public static final int OPTION_DHCP_SERVER_IDENTIFIER = 54; |
| 78 |
public static final int OPTION_DHCP_PARAMETER_REQUEST_LIST = 55; |
| 79 |
public static final int OPTION_DHCP_MESSAGE = 56; |
| 80 |
public static final int OPTION_DHCP_MAXIMUM_MESSAGE_SIZE = 57; |
| 81 |
public static final int OPTION_DHCP_RENEWAL_TIME = 58; |
| 82 |
public static final int OPTION_DHCP_REBIND_TIME = 59; |
| 83 |
public static final int OPTION_DHCP_CLASS_IDENTIFIER = 60; |
| 84 |
public static final int OPTION_DHCP_CLIENT_IDENTIFIER = 61; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
class DHCPOptionsEntry { |
| 91 |
protected byte code; |
| 92 |
protected byte length; |
| 93 |
protected byte content[]; |
| 94 |
|
| 95 |
public DHCPOptionsEntry(byte entryCode, byte entryLength, byte entryContent[]) { |
| 96 |
this.code = entryCode; |
| 97 |
this.length = entryLength; |
| 98 |
this.content = entryContent; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public String toString() { |
| 103 |
return "Code: " + this.code + "\nContent: " + new String(this.content); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private Hashtable<Byte, DHCPOptionsEntry> optionsTable = null; |
| 108 |
|
| 109 |
public DHCPOptions() { |
| 110 |
this.optionsTable = new Hashtable<Byte, DHCPOptionsEntry>(); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public void removeOption(byte entryCode) { |
| 121 |
this.optionsTable.remove(new Byte(entryCode)); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public boolean contains(byte entryCode) { |
| 132 |
return this.optionsTable.containsKey(new Byte(entryCode)); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public boolean isEmpty() { |
| 141 |
return this.optionsTable.isEmpty(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public byte[] getOption(byte entryCode) { |
| 153 |
if (this.contains(entryCode)) { |
| 154 |
DHCPOptionsEntry ent = this.optionsTable.get(new Byte(entryCode)); |
| 155 |
return ent.content; |
| 156 |
} else { |
| 157 |
return null; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public void setOption(byte entryCode, byte value[]) { |
| 170 |
DHCPOptionsEntry opt = new DHCPOptionsEntry(entryCode, (byte) value.length, value); |
| 171 |
this.optionsTable.put(new Byte(entryCode), opt); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private byte[] getArrayOption(int length, int position, byte options[]) { |
| 186 |
byte value[] = new byte[length]; |
| 187 |
for (int i = 0; i < length; i++) { |
| 188 |
value[i] = options[position + i]; |
| 189 |
} |
| 190 |
return value; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public void internalize(byte[] optionsArray) { |
| 200 |
|
| 201 |
|
| 202 |
int pos = 4; |
| 203 |
byte code, length; |
| 204 |
byte value[]; |
| 205 |
|
| 206 |
while (optionsArray[pos] != (byte) 255) { |
| 207 |
code = optionsArray[pos++]; |
| 208 |
length = optionsArray[pos++]; |
| 209 |
value = getArrayOption(length, pos, optionsArray); |
| 210 |
setOption(code, value); |
| 211 |
pos += length; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public byte[] externalize() { |
| 222 |
byte[] options = new byte[312]; |
| 223 |
|
| 224 |
options[0] = (byte) 99; |
| 225 |
options[1] = (byte) 130; |
| 226 |
options[2] = (byte) 83; |
| 227 |
options[3] = (byte) 99; |
| 228 |
|
| 229 |
int position = 4; |
| 230 |
Enumeration<DHCPOptionsEntry> e = this.optionsTable.elements(); |
| 231 |
|
| 232 |
while (e.hasMoreElements()) { |
| 233 |
DHCPOptionsEntry entry = e.nextElement(); |
| 234 |
options[position++] = entry.code; |
| 235 |
options[position++] = entry.length; |
| 236 |
for (int i = 0; i < entry.length; ++i) { |
| 237 |
options[position++] = entry.content[i]; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
options[position] = (byte) 255; |
| 242 |
return options; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public void printList() { |
| 249 |
System.out.println(this.optionsTable.toString()); |
| 250 |
} |
| 251 |
} |