blob: e4148023082d779e2c1026f08f7b9c3b988bb03a [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) {
363 if (option.getCode() == 0 || option.getCode() == 255) {
364 optionsLength += 1;
365 } else {
Ray Milkey269ffb92014-04-03 14:43:30 -0700366 optionsLength += 2 + (int) (0xff & option.getLength());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800367 }
368 }
369 int optionsPadLength = 0;
370 if (optionsLength < 60)
371 optionsPadLength = 60 - optionsLength;
372
Ray Milkey269ffb92014-04-03 14:43:30 -0700373 byte[] data = new byte[240 + optionsLength + optionsPadLength];
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800374 ByteBuffer bb = ByteBuffer.wrap(data);
375 bb.put(this.opCode);
376 bb.put(this.hardwareType);
377 bb.put(this.hardwareAddressLength);
378 bb.put(this.hops);
379 bb.putInt(this.transactionId);
380 bb.putShort(this.seconds);
381 bb.putShort(this.flags);
382 bb.putInt(this.clientIPAddress);
383 bb.putInt(this.yourIPAddress);
384 bb.putInt(this.serverIPAddress);
385 bb.putInt(this.gatewayIPAddress);
386 bb.put(this.clientHardwareAddress);
387 if (this.clientHardwareAddress.length < 16) {
388 for (int i = 0; i < (16 - this.clientHardwareAddress.length); ++i) {
389 bb.put((byte) 0x0);
390 }
391 }
392 writeString(this.serverName, bb, 64);
393 writeString(this.bootFileName, bb, 128);
394 // magic cookie
395 bb.put((byte) 0x63);
396 bb.put((byte) 0x82);
397 bb.put((byte) 0x53);
398 bb.put((byte) 0x63);
399 for (DHCPOption option : this.options) {
400 int code = option.getCode() & 0xff;
401 bb.put((byte) code);
402 if ((code != 0) && (code != 255)) {
403 bb.put(option.getLength());
404 bb.put(option.getData());
405 }
406 }
407 // assume the rest is padded out with zeroes
408 return data;
409 }
410
411 protected void writeString(String string, ByteBuffer bb, int maxLength) {
412 if (string == null) {
413 for (int i = 0; i < maxLength; ++i) {
414 bb.put((byte) 0x0);
415 }
416 } else {
417 byte[] bytes = null;
418 try {
Ray Milkey269ffb92014-04-03 14:43:30 -0700419 bytes = string.getBytes("ascii");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800420 } catch (UnsupportedEncodingException e) {
421 throw new RuntimeException("Failure encoding server name", e);
422 }
423 int writeLength = bytes.length;
424 if (writeLength > maxLength) {
425 writeLength = maxLength;
426 }
427 bb.put(bytes, 0, writeLength);
428 for (int i = writeLength; i < maxLength; ++i) {
429 bb.put((byte) 0x0);
430 }
431 }
432 }
433
434 @Override
435 public IPacket deserialize(byte[] data, int offset, int length) {
436 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
437 if (bb.remaining() < MIN_HEADER_LENGTH) {
438 return this;
439 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700440
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800441 this.opCode = bb.get();
442 this.hardwareType = bb.get();
443 this.hardwareAddressLength = bb.get();
444 this.hops = bb.get();
445 this.transactionId = bb.getInt();
446 this.seconds = bb.getShort();
447 this.flags = bb.getShort();
448 this.clientIPAddress = bb.getInt();
449 this.yourIPAddress = bb.getInt();
450 this.serverIPAddress = bb.getInt();
451 this.gatewayIPAddress = bb.getInt();
452 int hardwareAddressLength = 0xff & this.hardwareAddressLength;
453 this.clientHardwareAddress = new byte[hardwareAddressLength];
454
455 bb.get(this.clientHardwareAddress);
456 for (int i = hardwareAddressLength; i < 16; ++i)
457 bb.get();
458 this.serverName = readString(bb, 64);
459 this.bootFileName = readString(bb, 128);
460 // read the magic cookie
461 // magic cookie
462 bb.get();
463 bb.get();
464 bb.get();
465 bb.get();
466 // read options
467 while (bb.hasRemaining()) {
468 DHCPOption option = new DHCPOption();
469 int code = 0xff & bb.get(); // convert signed byte to int in range [0,255]
470 option.setCode((byte) code);
471 if (code == 0) {
472 // skip these
473 continue;
474 } else if (code != 255) {
475 if (bb.hasRemaining()) {
476 int l = 0xff & bb.get(); // convert signed byte to int in range [0,255]
477 option.setLength((byte) l);
478 if (bb.remaining() >= l) {
479 byte[] optionData = new byte[l];
480 bb.get(optionData);
481 option.setData(optionData);
482 } else {
483 // Skip the invalid option and set the END option
484 code = 0xff;
Ray Milkey269ffb92014-04-03 14:43:30 -0700485 option.setCode((byte) code);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800486 option.setLength((byte) 0);
487 }
488 } else {
489 // Skip the invalid option and set the END option
490 code = 0xff;
Ray Milkey269ffb92014-04-03 14:43:30 -0700491 option.setCode((byte) code);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800492 option.setLength((byte) 0);
493 }
494 }
495 this.options.add(option);
496 if (code == 255) {
497 // remaining bytes are supposed to be 0, but ignore them just in case
498 break;
499 }
500 }
501
502 return this;
503 }
504
505 protected String readString(ByteBuffer bb, int maxLength) {
506 byte[] bytes = new byte[maxLength];
507 bb.get(bytes);
508 String result = null;
509 try {
510 result = new String(bytes, "ascii").trim();
511 } catch (UnsupportedEncodingException e) {
512 throw new RuntimeException("Failure decoding string", e);
513 }
514 return result;
515 }
516}