blob: 1d0e0a42f2b4c548e494685e53d226d06a8963e8 [file] [log] [blame]
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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;
Jonathan Hart2a655752015-04-07 16:46:33 -070019import org.onlab.packet.Deserializer;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080020import org.onlab.packet.IPacket;
21
22import java.nio.ByteBuffer;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080023import java.util.List;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080024
Jian Li5fc14292015-12-04 11:30:46 -080025import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070026import static org.onlab.packet.PacketUtils.checkInput;
27
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080028/**
29 * Implements ICMPv6 Router Solicitation packet format. (RFC 4861)
30 */
31public class RouterSolicitation extends BasePacket {
32 public static final byte HEADER_LENGTH = 4; // bytes
33
Jonathan Hart2a655752015-04-07 16:46:33 -070034 private final NeighborDiscoveryOptions options = new NeighborDiscoveryOptions();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080035
36 /**
37 * Gets the Neighbor Discovery Protocol packet options.
38 *
39 * @return the Neighbor Discovery Protocol packet options
40 */
41 public List<NeighborDiscoveryOptions.Option> getOptions() {
42 return this.options.options();
43 }
44
45 /**
46 * Adds a Neighbor Discovery Protocol packet option.
47 *
48 * @param type the option type
49 * @param data the option data
50 * @return this
51 */
52 public RouterSolicitation addOption(final byte type, final byte[] data) {
53 this.options.addOption(type, data);
54 return this;
55 }
56
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080057 @Override
58 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080059 byte[] optionsData = null;
60 if (this.options.hasOptions()) {
61 optionsData = this.options.serialize();
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080062 }
63
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080064 int optionsLength = 0;
65 if (optionsData != null) {
66 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080067 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080068
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080069 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080070 final ByteBuffer bb = ByteBuffer.wrap(data);
71
72 bb.putInt(0);
73
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080074 if (optionsData != null) {
75 bb.put(optionsData);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080076 }
77
78 return data;
79 }
80
81 @Override
82 public IPacket deserialize(byte[] data, int offset, int length) {
83 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
84
85 bb.getInt();
86
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080087 this.options.deserialize(data, bb.position(),
88 bb.limit() - bb.position());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080089
90 return this;
91 }
92
93 /*
94 * (non-Javadoc)
95 *
96 * @see java.lang.Object#hashCode()
97 */
98 @Override
99 public int hashCode() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800100 final int prime = 5807;
101 int result = super.hashCode();
102 result = prime * result + this.options.hashCode();
103 return result;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800104 }
105
106 /*
107 * (non-Javadoc)
108 *
109 * @see java.lang.Object#equals(java.lang.Object)
110 */
111 @Override
112 public boolean equals(final Object obj) {
113 if (this == obj) {
114 return true;
115 }
116 if (!super.equals(obj)) {
117 return false;
118 }
119 if (!(obj instanceof RouterSolicitation)) {
120 return false;
121 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800122 final RouterSolicitation other = (RouterSolicitation) obj;
123 if (!this.options.equals(other.options)) {
124 return false;
125 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800126 return true;
127 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700128
129 /**
130 * Deserializer function for router solicitation packets.
131 *
132 * @return deserializer function
133 */
134 public static Deserializer<RouterSolicitation> deserializer() {
135 return (data, offset, length) -> {
136 checkInput(data, offset, length, HEADER_LENGTH);
137
138 RouterSolicitation routerSolicitation = new RouterSolicitation();
139
140 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
141
142 bb.getInt();
143
Charles Chan3599d632015-09-05 14:47:51 +0800144 if (bb.limit() - bb.position() > 0) {
145 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
146 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700147
Charles Chan3599d632015-09-05 14:47:51 +0800148 for (NeighborDiscoveryOptions.Option option : options.options()) {
149 routerSolicitation.addOption(option.type(), option.data());
150 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700151 }
152
153 return routerSolicitation;
154 };
155 }
Jian Li5fc14292015-12-04 11:30:46 -0800156
157 @Override
158 public String toString() {
159 return toStringHelper(getClass())
160 .toString();
161 // TODO: need to handle options
162 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800163}