Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/wpad/dhcp/DHCPOptions.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.wpad.dhcp;
2
3 import java.util.Enumeration;
4 import java.util.Hashtable;
5
6 /**
7 * This class represents a linked list of options for a DHCP message. Its
8 * purpose is to ease option handling such as add, remove or change.
9 *
10 * @author Markus Bernhardt, Copyright 2016
11 * @author Jason Goldschmidt and Simon Frankenberger
12 * @author Bernd Rosstauscher, Copyright 2009
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 * This inner class represent an entry in the Option Table
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 * Removes option with specified bytecode
115 *
116 * @param entryCode
117 * The code of option to be removed
118 */
119
120 public void removeOption(byte entryCode) {
121 this.optionsTable.remove(new Byte(entryCode));
122 }
123
124 /**
125 * Returns true if option code is set in list; false otherwise
126 *
127 * @param entryCode
128 * The node's option code
129 * @return true if option is set, otherwise false
130 */
131 public boolean contains(byte entryCode) {
132 return this.optionsTable.containsKey(new Byte(entryCode));
133 }
134
135 /**
136 * Determines if list is empty
137 *
138 * @return true if there are no options set, otherwise false
139 */
140 public boolean isEmpty() {
141 return this.optionsTable.isEmpty();
142 }
143
144 /**
145 * Fetches value of option by its option code
146 *
147 * @param entryCode
148 * The node's option code
149 * @return byte array containing the value of option entryCode. null is
150 * returned if option is not set.
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 * Changes an existing option to new value
163 *
164 * @param entryCode
165 * The node's option code
166 * @param value
167 * Content of node option
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 * Returns the option value of a specified option code in a byte array
176 *
177 * @param length
178 * Length of option content
179 * @param position
180 * Location in array of option node
181 * @param options
182 * The byte array of options
183 * @return byte array containing the value for the option
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 * Converts an options byte array to a linked list
195 *
196 * @param optionsArray
197 * The byte array representation of the options list
198 */
199 public void internalize(byte[] optionsArray) {
200
201 /* Assume options valid and correct */
202 int pos = 4; // ignore vendor magic cookie
203 byte code, length;
204 byte value[];
205
206 while (optionsArray[pos] != (byte) 255) { // until end option
207 code = optionsArray[pos++];
208 length = optionsArray[pos++];
209 value = getArrayOption(length, pos, optionsArray);
210 setOption(code, value);
211 pos += length; // increment position pointer
212 }
213 }
214
215 /**
216 * Converts a linked options list to a byte array
217 *
218 * @return array representation of optionsTable
219 */
220 // todo provide overflow return
221 public byte[] externalize() {
222 byte[] options = new byte[312];
223
224 options[0] = (byte) 99; // insert vendor magic cookie
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; // insert end option
242 return options;
243 }
244
245 /**
246 * Prints the options linked list: For testing only.
247 */
248 public void printList() {
249 System.out.println(this.optionsTable.toString());
250 }
251 }