blob: 3d51cf8b89ca7050d4c0e8af4de832f25ba040f7 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.io.UnsupportedEncodingException;
21import java.nio.ByteBuffer;
22import java.util.ArrayList;
23import java.util.List;
24import java.util.ListIterator;
25
26/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027 * @author David Erickson (daviderickson@cs.stanford.edu)
28 */
29public class DHCP extends BasePacket {
30 /**
31 * ------------------------------------------
32 * |op (1) | htype(1) | hlen(1) | hops(1) |
33 * ------------------------------------------
34 * | xid (4) |
35 * ------------------------------------------
36 * | secs (2) | flags (2) |
37 * ------------------------------------------
38 * | ciaddr (4) |
39 * ------------------------------------------
40 * | yiaddr (4) |
41 * ------------------------------------------
42 * | siaddr (4) |
43 * ------------------------------------------
44 * | giaddr (4) |
45 * ------------------------------------------
46 * | chaddr (16) |
47 * ------------------------------------------
48 * | sname (64) |
49 * ------------------------------------------
50 * | file (128) |
51 * ------------------------------------------
52 * | options (312) |
53 * ------------------------------------------
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054 */
55 // Header + magic without options
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070056 public static final int MIN_HEADER_LENGTH = 240;
57 public static final byte OPCODE_REQUEST = 0x1;
58 public static final byte OPCODE_REPLY = 0x2;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070060 public static final byte HWTYPE_ETHERNET = 0x1;
Ray Milkey269ffb92014-04-03 14:43:30 -070061
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080062 public enum DHCPOptionCode {
Ray Milkey269ffb92014-04-03 14:43:30 -070063 OptionCode_SubnetMask((byte) 1),
64 OptionCode_RequestedIP((byte) 50),
65 OptionCode_LeaseTime((byte) 51),
66 OptionCode_MessageType((byte) 53),
67 OptionCode_DHCPServerIp((byte) 54),
68 OptionCode_RequestedParameters((byte) 55),
69 OptionCode_RenewalTime((byte) 58),
70 OPtionCode_RebindingTime((byte) 59),
71 OptionCode_ClientID((byte) 61),
72 OptionCode_END((byte) 255);
73
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080074 protected byte value;
Ray Milkey269ffb92014-04-03 14:43:30 -070075
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080076 private DHCPOptionCode(byte value) {
77 this.value = value;
78 }
Ray Milkey269ffb92014-04-03 14:43:30 -070079
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080080 public byte getValue() {
81 return value;
82 }
83 }
Ray Milkey269ffb92014-04-03 14:43:30 -070084
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085 protected byte opCode;
86 protected byte hardwareType;
87 protected byte hardwareAddressLength;
88 protected byte hops;
89 protected int transactionId;
90 protected short seconds;
91 protected short flags;
92 protected int clientIPAddress;
93 protected int yourIPAddress;
94 protected int serverIPAddress;
95 protected int gatewayIPAddress;
96 protected byte[] clientHardwareAddress;
97 protected String serverName;
98 protected String bootFileName;
99 protected List<DHCPOption> options = new ArrayList<DHCPOption>();
100
101 /**
102 * @return the opCode
103 */
104 public byte getOpCode() {
105 return opCode;
106 }
107
108 /**
109 * @param opCode the opCode to set
110 */
111 public DHCP setOpCode(byte opCode) {
112 this.opCode = opCode;
113 return this;
114 }
115
116 /**
117 * @return the hardwareType
118 */
119 public byte getHardwareType() {
120 return hardwareType;
121 }
122
123 /**
124 * @param hardwareType the hardwareType to set
125 */
126 public DHCP setHardwareType(byte hardwareType) {
127 this.hardwareType = hardwareType;
128 return this;
129 }
130
131 /**
132 * @return the hardwareAddressLength
133 */
134 public byte getHardwareAddressLength() {
135 return hardwareAddressLength;
136 }
137
138 /**
139 * @param hardwareAddressLength the hardwareAddressLength to set
140 */
141 public DHCP setHardwareAddressLength(byte hardwareAddressLength) {
142 this.hardwareAddressLength = hardwareAddressLength;
143 return this;
144 }
145
146 /**
147 * @return the hops
148 */
149 public byte getHops() {
150 return hops;
151 }
152
153 /**
154 * @param hops the hops to set
155 */
156 public DHCP setHops(byte hops) {
157 this.hops = hops;
158 return this;
159 }
160
161 /**
162 * @return the transactionId
163 */
164 public int getTransactionId() {
165 return transactionId;
166 }
167
168 /**
169 * @param transactionId the transactionId to set
170 */
171 public DHCP setTransactionId(int transactionId) {
172 this.transactionId = transactionId;
173 return this;
174 }
175
176 /**
177 * @return the seconds
178 */
179 public short getSeconds() {
180 return seconds;
181 }
182
183 /**
184 * @param seconds the seconds to set
185 */
186 public DHCP setSeconds(short seconds) {
187 this.seconds = seconds;
188 return this;
189 }
190
191 /**
192 * @return the flags
193 */
194 public short getFlags() {
195 return flags;
196 }
197
198 /**
199 * @param flags the flags to set
200 */
201 public DHCP setFlags(short flags) {
202 this.flags = flags;
203 return this;
204 }
205
206 /**
207 * @return the clientIPAddress
208 */
209 public int getClientIPAddress() {
210 return clientIPAddress;
211 }
212
213 /**
214 * @param clientIPAddress the clientIPAddress to set
215 */
216 public DHCP setClientIPAddress(int clientIPAddress) {
217 this.clientIPAddress = clientIPAddress;
218 return this;
219 }
220
221 /**
222 * @return the yourIPAddress
223 */
224 public int getYourIPAddress() {
225 return yourIPAddress;
226 }
227
228 /**
229 * @param yourIPAddress the yourIPAddress to set
230 */
231 public DHCP setYourIPAddress(int yourIPAddress) {
232 this.yourIPAddress = yourIPAddress;
233 return this;
234 }
235
236 /**
237 * @return the serverIPAddress
238 */
239 public int getServerIPAddress() {
240 return serverIPAddress;
241 }
242
243 /**
244 * @param serverIPAddress the serverIPAddress to set
245 */
246 public DHCP setServerIPAddress(int serverIPAddress) {
247 this.serverIPAddress = serverIPAddress;
248 return this;
249 }
250
251 /**
252 * @return the gatewayIPAddress
253 */
254 public int getGatewayIPAddress() {
255 return gatewayIPAddress;
256 }
257
258 /**
259 * @param gatewayIPAddress the gatewayIPAddress to set
260 */
261 public DHCP setGatewayIPAddress(int gatewayIPAddress) {
262 this.gatewayIPAddress = gatewayIPAddress;
263 return this;
264 }
265
266 /**
267 * @return the clientHardwareAddress
268 */
269 public byte[] getClientHardwareAddress() {
270 return clientHardwareAddress;
271 }
272
273 /**
274 * @param clientHardwareAddress the clientHardwareAddress to set
275 */
276 public DHCP setClientHardwareAddress(byte[] clientHardwareAddress) {
277 this.clientHardwareAddress = clientHardwareAddress;
278 return this;
279 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700280
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800281 /**
282 * Gets a specific DHCP option parameter
Ray Milkey269ffb92014-04-03 14:43:30 -0700283 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800284 * @param opetionCode The option code to get
285 * @return The value of the option if it exists, null otherwise
286 */
287 public DHCPOption getOption(DHCPOptionCode optionCode) {
288 for (DHCPOption opt : options) {
289 if (opt.code == optionCode.value)
290 return opt;
291 }
292 return null;
293 }
294
295 /**
296 * @return the options
297 */
298 public List<DHCPOption> getOptions() {
299 return options;
300 }
301
302 /**
303 * @param options the options to set
304 */
305 public DHCP setOptions(List<DHCPOption> options) {
306 this.options = options;
307 return this;
308 }
309
310 /**
311 * @return the packetType base on option 53
312 */
313 public DHCPPacketType getPacketType() {
314 ListIterator<DHCPOption> lit = options.listIterator();
315 while (lit.hasNext()) {
316 DHCPOption option = lit.next();
317 // only care option 53
318 if (option.getCode() == 53) {
319 return DHCPPacketType.getType(option.getData()[0]);
320 }
321 }
322 return null;
323 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700324
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800325 /**
326 * @return the serverName
327 */
328 public String getServerName() {
329 return serverName;
330 }
331
332 /**
333 * @param serverName the serverName to set
334 */
335 public DHCP setServerName(String serverName) {
336 this.serverName = serverName;
337 return this;
338 }
339
340 /**
341 * @return the bootFileName
342 */
343 public String getBootFileName() {
344 return bootFileName;
345 }
346
347 /**
348 * @param bootFileName the bootFileName to set
349 */
350 public DHCP setBootFileName(String bootFileName) {
351 this.bootFileName = bootFileName;
352 return this;
353 }
354
355 @Override
356 public byte[] serialize() {
357 // not guaranteed to retain length/exact format
358 resetChecksum();
359
360 // minimum size 240 including magic cookie, options generally padded to 300
361 int optionsLength = 0;
362 for (DHCPOption option : this.options) {
Pavlin Radoslavov33048e82014-04-09 14:40:59 -0700363 int code = option.getCode() & 0xff;
364 if (code == 0 || code == 255) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800365 optionsLength += 1;
366 } else {
Ray Milkey269ffb92014-04-03 14:43:30 -0700367 optionsLength += 2 + (int) (0xff & option.getLength());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800368 }
369 }
370 int optionsPadLength = 0;
371 if (optionsLength < 60)
372 optionsPadLength = 60 - optionsLength;
373
Ray Milkey269ffb92014-04-03 14:43:30 -0700374 byte[] data = new byte[240 + optionsLength + optionsPadLength];
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800375 ByteBuffer bb = ByteBuffer.wrap(data);
376 bb.put(this.opCode);
377 bb.put(this.hardwareType);
378 bb.put(this.hardwareAddressLength);
379 bb.put(this.hops);
380 bb.putInt(this.transactionId);
381 bb.putShort(this.seconds);
382 bb.putShort(this.flags);
383 bb.putInt(this.clientIPAddress);
384 bb.putInt(this.yourIPAddress);
385 bb.putInt(this.serverIPAddress);
386 bb.putInt(this.gatewayIPAddress);
387 bb.put(this.clientHardwareAddress);
388 if (this.clientHardwareAddress.length < 16) {
389 for (int i = 0; i < (16 - this.clientHardwareAddress.length); ++i) {
390 bb.put((byte) 0x0);
391 }
392 }
393 writeString(this.serverName, bb, 64);
394 writeString(this.bootFileName, bb, 128);
395 // magic cookie
396 bb.put((byte) 0x63);
397 bb.put((byte) 0x82);
398 bb.put((byte) 0x53);
399 bb.put((byte) 0x63);
400 for (DHCPOption option : this.options) {
401 int code = option.getCode() & 0xff;
402 bb.put((byte) code);
403 if ((code != 0) && (code != 255)) {
404 bb.put(option.getLength());
405 bb.put(option.getData());
406 }
407 }
408 // assume the rest is padded out with zeroes
409 return data;
410 }
411
412 protected void writeString(String string, ByteBuffer bb, int maxLength) {
413 if (string == null) {
414 for (int i = 0; i < maxLength; ++i) {
415 bb.put((byte) 0x0);
416 }
417 } else {
418 byte[] bytes = null;
419 try {
Ray Milkey269ffb92014-04-03 14:43:30 -0700420 bytes = string.getBytes("ascii");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800421 } catch (UnsupportedEncodingException e) {
422 throw new RuntimeException("Failure encoding server name", e);
423 }
424 int writeLength = bytes.length;
425 if (writeLength > maxLength) {
426 writeLength = maxLength;
427 }
428 bb.put(bytes, 0, writeLength);
429 for (int i = writeLength; i < maxLength; ++i) {
430 bb.put((byte) 0x0);
431 }
432 }
433 }
434
435 @Override
436 public IPacket deserialize(byte[] data, int offset, int length) {
437 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
438 if (bb.remaining() < MIN_HEADER_LENGTH) {
439 return this;
440 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700441
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800442 this.opCode = bb.get();
443 this.hardwareType = bb.get();
444 this.hardwareAddressLength = bb.get();
445 this.hops = bb.get();
446 this.transactionId = bb.getInt();
447 this.seconds = bb.getShort();
448 this.flags = bb.getShort();
449 this.clientIPAddress = bb.getInt();
450 this.yourIPAddress = bb.getInt();
451 this.serverIPAddress = bb.getInt();
452 this.gatewayIPAddress = bb.getInt();
453 int hardwareAddressLength = 0xff & this.hardwareAddressLength;
454 this.clientHardwareAddress = new byte[hardwareAddressLength];
455
456 bb.get(this.clientHardwareAddress);
457 for (int i = hardwareAddressLength; i < 16; ++i)
458 bb.get();
459 this.serverName = readString(bb, 64);
460 this.bootFileName = readString(bb, 128);
461 // read the magic cookie
462 // magic cookie
463 bb.get();
464 bb.get();
465 bb.get();
466 bb.get();
467 // read options
468 while (bb.hasRemaining()) {
469 DHCPOption option = new DHCPOption();
470 int code = 0xff & bb.get(); // convert signed byte to int in range [0,255]
471 option.setCode((byte) code);
472 if (code == 0) {
473 // skip these
474 continue;
475 } else if (code != 255) {
476 if (bb.hasRemaining()) {
477 int l = 0xff & bb.get(); // convert signed byte to int in range [0,255]
478 option.setLength((byte) l);
479 if (bb.remaining() >= l) {
480 byte[] optionData = new byte[l];
481 bb.get(optionData);
482 option.setData(optionData);
483 } else {
484 // Skip the invalid option and set the END option
485 code = 0xff;
Ray Milkey269ffb92014-04-03 14:43:30 -0700486 option.setCode((byte) code);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800487 option.setLength((byte) 0);
488 }
489 } else {
490 // Skip the invalid option and set the END option
491 code = 0xff;
Ray Milkey269ffb92014-04-03 14:43:30 -0700492 option.setCode((byte) code);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800493 option.setLength((byte) 0);
494 }
495 }
496 this.options.add(option);
497 if (code == 255) {
498 // remaining bytes are supposed to be 0, but ignore them just in case
499 break;
500 }
501 }
502
503 return this;
504 }
505
506 protected String readString(ByteBuffer bb, int maxLength) {
507 byte[] bytes = new byte[maxLength];
508 bb.get(bytes);
509 String result = null;
510 try {
511 result = new String(bytes, "ascii").trim();
512 } catch (UnsupportedEncodingException e) {
513 throw new RuntimeException("Failure decoding string", e);
514 }
515 return result;
516 }
517}