blob: f1efd7ba7943aa5ebc94216809334d0a22c4d28b [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
18/**
Ray Milkey269ffb92014-04-03 14:43:30 -070019 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080020 */
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070021package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080022
23import java.nio.ByteBuffer;
24import java.util.Arrays;
25import java.util.Collection;
26import java.util.HashMap;
27import java.util.Map;
28
29/**
30 * @author David Erickson (daviderickson@cs.stanford.edu)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031 */
32public class IPv4 extends BasePacket {
Ray Milkey269ffb92014-04-03 14:43:30 -070033 public static final int ADDRESS_LENGTH = 4;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080034 public static final byte PROTOCOL_ICMP = 0x1;
35 public static final byte PROTOCOL_TCP = 0x6;
36 public static final byte PROTOCOL_UDP = 0x11;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070037 public static final Map<Byte, Class<? extends IPacket>> PROTOCOL_CLASS_MAP;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080038
39 static {
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070040 PROTOCOL_CLASS_MAP = new HashMap<Byte, Class<? extends IPacket>>();
41 PROTOCOL_CLASS_MAP.put(PROTOCOL_ICMP, ICMP.class);
42 PROTOCOL_CLASS_MAP.put(PROTOCOL_TCP, TCP.class);
43 PROTOCOL_CLASS_MAP.put(PROTOCOL_UDP, UDP.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044 }
45
46 protected byte version;
47 protected byte headerLength;
48 protected byte diffServ;
49 protected short totalLength;
50 protected short identification;
51 protected byte flags;
52 protected short fragmentOffset;
53 protected byte ttl;
54 protected byte protocol;
55 protected short checksum;
56 protected int sourceAddress;
57 protected int destinationAddress;
58 protected byte[] options;
59
60 protected boolean isTruncated;
61
62 /**
63 * Default constructor that sets the version to 4.
64 */
65 public IPv4() {
66 super();
67 this.version = 4;
68 isTruncated = false;
69 }
70
71 /**
72 * @return the version
73 */
74 public byte getVersion() {
75 return version;
76 }
77
78 /**
79 * @param version the version to set
80 */
81 public IPv4 setVersion(byte version) {
82 this.version = version;
83 return this;
84 }
85
86 /**
87 * @return the headerLength
88 */
89 public byte getHeaderLength() {
90 return headerLength;
91 }
92
93 /**
94 * @return the diffServ
95 */
96 public byte getDiffServ() {
97 return diffServ;
98 }
99
100 /**
101 * @param diffServ the diffServ to set
102 */
103 public IPv4 setDiffServ(byte diffServ) {
104 this.diffServ = diffServ;
105 return this;
106 }
107
108 /**
109 * @return the totalLength
110 */
111 public short getTotalLength() {
112 return totalLength;
113 }
114
115 /**
116 * @return the identification
117 */
118 public short getIdentification() {
119 return identification;
120 }
121
122 public boolean isTruncated() {
123 return isTruncated;
124 }
125
126 public void setTruncated(boolean isTruncated) {
127 this.isTruncated = isTruncated;
128 }
129
130 /**
131 * @param identification the identification to set
132 */
133 public IPv4 setIdentification(short identification) {
134 this.identification = identification;
135 return this;
136 }
137
138 /**
139 * @return the flags
140 */
141 public byte getFlags() {
142 return flags;
143 }
144
145 /**
146 * @param flags the flags to set
147 */
148 public IPv4 setFlags(byte flags) {
149 this.flags = flags;
150 return this;
151 }
152
153 /**
154 * @return the fragmentOffset
155 */
156 public short getFragmentOffset() {
157 return fragmentOffset;
158 }
159
160 /**
161 * @param fragmentOffset the fragmentOffset to set
162 */
163 public IPv4 setFragmentOffset(short fragmentOffset) {
164 this.fragmentOffset = fragmentOffset;
165 return this;
166 }
167
168 /**
169 * @return the ttl
170 */
171 public byte getTtl() {
172 return ttl;
173 }
174
175 /**
176 * @param ttl the ttl to set
177 */
178 public IPv4 setTtl(byte ttl) {
179 this.ttl = ttl;
180 return this;
181 }
182
183 /**
184 * @return the protocol
185 */
186 public byte getProtocol() {
187 return protocol;
188 }
189
190 /**
191 * @param protocol the protocol to set
192 */
193 public IPv4 setProtocol(byte protocol) {
194 this.protocol = protocol;
195 return this;
196 }
197
198 /**
199 * @return the checksum
200 */
201 public short getChecksum() {
202 return checksum;
203 }
204
205 /**
206 * @param checksum the checksum to set
207 */
208 public IPv4 setChecksum(short checksum) {
209 this.checksum = checksum;
210 return this;
211 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700212
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800213 @Override
214 public void resetChecksum() {
215 this.checksum = 0;
216 super.resetChecksum();
217 }
218
219 /**
220 * @return the sourceAddress
221 */
222 public int getSourceAddress() {
223 return sourceAddress;
224 }
225
226 /**
227 * @param sourceAddress the sourceAddress to set
228 */
229 public IPv4 setSourceAddress(int sourceAddress) {
230 this.sourceAddress = sourceAddress;
231 return this;
232 }
233
234 /**
235 * @param sourceAddress the sourceAddress to set
236 */
237 public IPv4 setSourceAddress(String sourceAddress) {
238 this.sourceAddress = IPv4.toIPv4Address(sourceAddress);
239 return this;
240 }
241
242 /**
243 * @return the destinationAddress
244 */
245 public int getDestinationAddress() {
246 return destinationAddress;
247 }
248
249 /**
250 * @param destinationAddress the destinationAddress to set
251 */
252 public IPv4 setDestinationAddress(int destinationAddress) {
253 this.destinationAddress = destinationAddress;
254 return this;
255 }
256
257 /**
258 * @param destinationAddress the destinationAddress to set
259 */
260 public IPv4 setDestinationAddress(String destinationAddress) {
261 this.destinationAddress = IPv4.toIPv4Address(destinationAddress);
262 return this;
263 }
264
265 /**
266 * @return the options
267 */
268 public byte[] getOptions() {
269 return options;
270 }
271
272 /**
273 * @param options the options to set
274 */
275 public IPv4 setOptions(byte[] options) {
276 if (options != null && (options.length % 4) > 0)
277 throw new IllegalArgumentException(
278 "Options length must be a multiple of 4");
279 this.options = options;
280 return this;
281 }
282
283 /**
284 * Serializes the packet. Will compute and set the following fields if they
285 * are set to specific values at the time serialize is called:
Ray Milkey269ffb92014-04-03 14:43:30 -0700286 * -checksum : 0
287 * -headerLength : 0
288 * -totalLength : 0
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800289 */
290 public byte[] serialize() {
291 byte[] payloadData = null;
292 if (payload != null) {
293 payload.setParent(this);
294 payloadData = payload.serialize();
295 }
296
297 int optionsLength = 0;
298 if (this.options != null)
299 optionsLength = this.options.length / 4;
300 this.headerLength = (byte) (5 + optionsLength);
301
302 this.totalLength = (short) (this.headerLength * 4 + ((payloadData == null) ? 0
303 : payloadData.length));
304
305 byte[] data = new byte[this.totalLength];
306 ByteBuffer bb = ByteBuffer.wrap(data);
307
308 bb.put((byte) (((this.version & 0xf) << 4) | (this.headerLength & 0xf)));
309 bb.put(this.diffServ);
310 bb.putShort(this.totalLength);
311 bb.putShort(this.identification);
312 bb.putShort((short) (((this.flags & 0x7) << 13) | (this.fragmentOffset & 0x1fff)));
313 bb.put(this.ttl);
314 bb.put(this.protocol);
315 bb.putShort(this.checksum);
316 bb.putInt(this.sourceAddress);
317 bb.putInt(this.destinationAddress);
318 if (this.options != null)
319 bb.put(this.options);
320 if (payloadData != null)
321 bb.put(payloadData);
322
323 // compute checksum if needed
324 if (this.checksum == 0) {
325 bb.rewind();
326 int accumulation = 0;
327 for (int i = 0; i < this.headerLength * 2; ++i) {
328 accumulation += 0xffff & bb.getShort();
329 }
330 accumulation = ((accumulation >> 16) & 0xffff)
331 + (accumulation & 0xffff);
332 this.checksum = (short) (~accumulation & 0xffff);
333 bb.putShort(10, this.checksum);
334 }
335 return data;
336 }
337
338 @Override
339 public IPacket deserialize(byte[] data, int offset, int length) {
340 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
341 short sscratch;
342
343 this.version = bb.get();
344 this.headerLength = (byte) (this.version & 0xf);
345 this.version = (byte) ((this.version >> 4) & 0xf);
346 this.diffServ = bb.get();
347 this.totalLength = bb.getShort();
348 this.identification = bb.getShort();
349 sscratch = bb.getShort();
350 this.flags = (byte) ((sscratch >> 13) & 0x7);
351 this.fragmentOffset = (short) (sscratch & 0x1fff);
352 this.ttl = bb.get();
353 this.protocol = bb.get();
354 this.checksum = bb.getShort();
355 this.sourceAddress = bb.getInt();
356 this.destinationAddress = bb.getInt();
357
358 if (this.headerLength > 5) {
359 int optionsLength = (this.headerLength - 5) * 4;
360 this.options = new byte[optionsLength];
361 bb.get(this.options);
362 }
363
364 IPacket payload;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700365 if (IPv4.PROTOCOL_CLASS_MAP.containsKey(this.protocol)) {
366 Class<? extends IPacket> clazz = IPv4.PROTOCOL_CLASS_MAP.get(this.protocol);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800367 try {
368 payload = clazz.newInstance();
369 } catch (Exception e) {
370 throw new RuntimeException("Error parsing payload for IPv4 packet", e);
371 }
372 } else {
373 payload = new Data();
374 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700375 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800376 this.payload.setParent(this);
377
378 if (this.totalLength != length)
379 this.isTruncated = true;
380 else
381 this.isTruncated = false;
382
383 return this;
384 }
385
386 /**
387 * Accepts an IPv4 address of the form xxx.xxx.xxx.xxx, ie 192.168.0.1 and
388 * returns the corresponding 32 bit integer.
Ray Milkey269ffb92014-04-03 14:43:30 -0700389 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800390 * @param ipAddress
391 * @return
392 */
393 public static int toIPv4Address(String ipAddress) {
394 if (ipAddress == null)
395 throw new IllegalArgumentException("Specified IPv4 address must" +
Ray Milkey269ffb92014-04-03 14:43:30 -0700396 "contain 4 sets of numerical digits separated by periods");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800397 String[] octets = ipAddress.split("\\.");
Ray Milkey269ffb92014-04-03 14:43:30 -0700398 if (octets.length != 4)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800399 throw new IllegalArgumentException("Specified IPv4 address must" +
Ray Milkey269ffb92014-04-03 14:43:30 -0700400 "contain 4 sets of numerical digits separated by periods");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800401
402 int result = 0;
403 for (int i = 0; i < 4; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700404 result |= Integer.valueOf(octets[i]) << ((3 - i) * 8);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800405 }
406 return result;
407 }
408
409 /**
410 * Accepts an IPv4 address in a byte array and returns the corresponding
411 * 32-bit integer value.
Ray Milkey269ffb92014-04-03 14:43:30 -0700412 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800413 * @param ipAddress
414 * @return
415 */
416 public static int toIPv4Address(byte[] ipAddress) {
417 int ip = 0;
418 for (int i = 0; i < 4; i++) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700419 int t = (ipAddress[i] & 0xff) << ((3 - i) * 8);
420 ip |= t;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800421 }
422 return ip;
423 }
424
425 /**
426 * Accepts an IPv4 address and returns of string of the form xxx.xxx.xxx.xxx
427 * ie 192.168.0.1
Ray Milkey269ffb92014-04-03 14:43:30 -0700428 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800429 * @param ipAddress
430 * @return
431 */
432 public static String fromIPv4Address(int ipAddress) {
433 StringBuffer sb = new StringBuffer();
434 int result = 0;
435 for (int i = 0; i < 4; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700436 result = (ipAddress >> ((3 - i) * 8)) & 0xff;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800437 sb.append(Integer.valueOf(result).toString());
438 if (i != 3)
439 sb.append(".");
440 }
441 return sb.toString();
442 }
443
444 /**
445 * Accepts a collection of IPv4 addresses as integers and returns a single
446 * String useful in toString method's containing collections of IP
447 * addresses.
Ray Milkey269ffb92014-04-03 14:43:30 -0700448 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800449 * @param ipAddresses collection
450 * @return
451 */
452 public static String fromIPv4AddressCollection(Collection<Integer> ipAddresses) {
453 if (ipAddresses == null)
454 return "null";
455 StringBuffer sb = new StringBuffer();
456 sb.append("[");
457 for (Integer ip : ipAddresses) {
458 sb.append(fromIPv4Address(ip));
459 sb.append(",");
460 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700461 sb.replace(sb.length() - 1, sb.length(), "]");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800462 return sb.toString();
463 }
464
465 /**
466 * Accepts an IPv4 address of the form xxx.xxx.xxx.xxx, ie 192.168.0.1 and
467 * returns the corresponding byte array.
Ray Milkey269ffb92014-04-03 14:43:30 -0700468 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800469 * @param ipAddress The IP address in the form xx.xxx.xxx.xxx.
470 * @return The IP address separated into bytes
471 */
472 public static byte[] toIPv4AddressBytes(String ipAddress) {
473 String[] octets = ipAddress.split("\\.");
Ray Milkey269ffb92014-04-03 14:43:30 -0700474 if (octets.length != 4)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800475 throw new IllegalArgumentException("Specified IPv4 address must" +
Ray Milkey269ffb92014-04-03 14:43:30 -0700476 "contain 4 sets of numerical digits separated by periods");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800477
478 byte[] result = new byte[4];
479 for (int i = 0; i < 4; ++i) {
480 result[i] = Integer.valueOf(octets[i]).byteValue();
481 }
482 return result;
483 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700484
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800485 /**
486 * Accepts an IPv4 address in the form of an integer and
487 * returns the corresponding byte array.
Ray Milkey269ffb92014-04-03 14:43:30 -0700488 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800489 * @param ipAddress The IP address as an integer.
490 * @return The IP address separated into bytes.
491 */
492 public static byte[] toIPv4AddressBytes(int ipAddress) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700493 return new byte[]{
494 (byte) (ipAddress >>> 24),
495 (byte) (ipAddress >>> 16),
496 (byte) (ipAddress >>> 8),
497 (byte) ipAddress};
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800498 }
499
500 /* (non-Javadoc)
501 * @see java.lang.Object#hashCode()
502 */
503 @Override
504 public int hashCode() {
505 final int prime = 2521;
506 int result = super.hashCode();
507 result = prime * result + checksum;
508 result = prime * result + destinationAddress;
509 result = prime * result + diffServ;
510 result = prime * result + flags;
511 result = prime * result + fragmentOffset;
512 result = prime * result + headerLength;
513 result = prime * result + identification;
514 result = prime * result + Arrays.hashCode(options);
515 result = prime * result + protocol;
516 result = prime * result + sourceAddress;
517 result = prime * result + totalLength;
518 result = prime * result + ttl;
519 result = prime * result + version;
520 return result;
521 }
522
523 /* (non-Javadoc)
524 * @see java.lang.Object#equals(java.lang.Object)
525 */
526 @Override
527 public boolean equals(Object obj) {
528 if (this == obj)
529 return true;
530 if (!super.equals(obj))
531 return false;
532 if (!(obj instanceof IPv4))
533 return false;
534 IPv4 other = (IPv4) obj;
535 if (checksum != other.checksum)
536 return false;
537 if (destinationAddress != other.destinationAddress)
538 return false;
539 if (diffServ != other.diffServ)
540 return false;
541 if (flags != other.flags)
542 return false;
543 if (fragmentOffset != other.fragmentOffset)
544 return false;
545 if (headerLength != other.headerLength)
546 return false;
547 if (identification != other.identification)
548 return false;
549 if (!Arrays.equals(options, other.options))
550 return false;
551 if (protocol != other.protocol)
552 return false;
553 if (sourceAddress != other.sourceAddress)
554 return false;
555 if (totalLength != other.totalLength)
556 return false;
557 if (ttl != other.ttl)
558 return false;
559 if (version != other.version)
560 return false;
561 return true;
562 }
563}