blob: 3fddc5ebb9ea59fad702e566f85eb4766767b7d6 [file] [log] [blame]
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08001/*
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -08002 * Copyright 2014-2015 Open Networking Laboratory
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080016package org.onlab.packet.ndp;
17
18import org.onlab.packet.BasePacket;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080019import org.onlab.packet.IPacket;
20
21import java.nio.ByteBuffer;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080022import java.util.List;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080023
24/**
25 * Implements ICMPv6 Router Solicitation packet format. (RFC 4861)
26 */
27public class RouterSolicitation extends BasePacket {
28 public static final byte HEADER_LENGTH = 4; // bytes
29
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080030 private final NeighborDiscoveryOptions options =
31 new NeighborDiscoveryOptions();
32
33 /**
34 * Gets the Neighbor Discovery Protocol packet options.
35 *
36 * @return the Neighbor Discovery Protocol packet options
37 */
38 public List<NeighborDiscoveryOptions.Option> getOptions() {
39 return this.options.options();
40 }
41
42 /**
43 * Adds a Neighbor Discovery Protocol packet option.
44 *
45 * @param type the option type
46 * @param data the option data
47 * @return this
48 */
49 public RouterSolicitation addOption(final byte type, final byte[] data) {
50 this.options.addOption(type, data);
51 return this;
52 }
53
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080054 @Override
55 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080056 byte[] optionsData = null;
57 if (this.options.hasOptions()) {
58 optionsData = this.options.serialize();
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080059 }
60
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080061 int optionsLength = 0;
62 if (optionsData != null) {
63 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080064 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080065
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080066 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080067 final ByteBuffer bb = ByteBuffer.wrap(data);
68
69 bb.putInt(0);
70
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080071 if (optionsData != null) {
72 bb.put(optionsData);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080073 }
74
75 return data;
76 }
77
78 @Override
79 public IPacket deserialize(byte[] data, int offset, int length) {
80 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
81
82 bb.getInt();
83
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080084 this.options.deserialize(data, bb.position(),
85 bb.limit() - bb.position());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080086
87 return this;
88 }
89
90 /*
91 * (non-Javadoc)
92 *
93 * @see java.lang.Object#hashCode()
94 */
95 @Override
96 public int hashCode() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080097 final int prime = 5807;
98 int result = super.hashCode();
99 result = prime * result + this.options.hashCode();
100 return result;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800101 }
102
103 /*
104 * (non-Javadoc)
105 *
106 * @see java.lang.Object#equals(java.lang.Object)
107 */
108 @Override
109 public boolean equals(final Object obj) {
110 if (this == obj) {
111 return true;
112 }
113 if (!super.equals(obj)) {
114 return false;
115 }
116 if (!(obj instanceof RouterSolicitation)) {
117 return false;
118 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800119 final RouterSolicitation other = (RouterSolicitation) obj;
120 if (!this.options.equals(other.options)) {
121 return false;
122 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800123 return true;
124 }
125}