blob: 902529c467ce5fe7082345474adb1083d133bbc4 [file] [log] [blame]
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 +080020
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
Jian Li5fc14292015-12-04 11:30:46 -080024import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070025import static org.onlab.packet.PacketUtils.checkInput;
26
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080027/**
28 * Implements ICMPv6 Router Solicitation packet format. (RFC 4861)
29 */
30public class RouterSolicitation extends BasePacket {
31 public static final byte HEADER_LENGTH = 4; // bytes
32
Jonathan Hart2a655752015-04-07 16:46:33 -070033 private final NeighborDiscoveryOptions options = new NeighborDiscoveryOptions();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080034
35 /**
36 * Gets the Neighbor Discovery Protocol packet options.
37 *
38 * @return the Neighbor Discovery Protocol packet options
39 */
40 public List<NeighborDiscoveryOptions.Option> getOptions() {
41 return this.options.options();
42 }
43
44 /**
45 * Adds a Neighbor Discovery Protocol packet option.
46 *
47 * @param type the option type
48 * @param data the option data
49 * @return this
50 */
51 public RouterSolicitation addOption(final byte type, final byte[] data) {
52 this.options.addOption(type, data);
53 return this;
54 }
55
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080056 @Override
57 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080058 byte[] optionsData = null;
59 if (this.options.hasOptions()) {
60 optionsData = this.options.serialize();
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080061 }
62
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080063 int optionsLength = 0;
64 if (optionsData != null) {
65 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080066 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080067
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080068 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080069 final ByteBuffer bb = ByteBuffer.wrap(data);
70
71 bb.putInt(0);
72
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080073 if (optionsData != null) {
74 bb.put(optionsData);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080075 }
76
77 return data;
78 }
79
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080080
81 /*
82 * (non-Javadoc)
83 *
84 * @see java.lang.Object#hashCode()
85 */
86 @Override
87 public int hashCode() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080088 final int prime = 5807;
89 int result = super.hashCode();
90 result = prime * result + this.options.hashCode();
91 return result;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080092 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see java.lang.Object#equals(java.lang.Object)
98 */
99 @Override
100 public boolean equals(final Object obj) {
101 if (this == obj) {
102 return true;
103 }
104 if (!super.equals(obj)) {
105 return false;
106 }
107 if (!(obj instanceof RouterSolicitation)) {
108 return false;
109 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800110 final RouterSolicitation other = (RouterSolicitation) obj;
111 if (!this.options.equals(other.options)) {
112 return false;
113 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800114 return true;
115 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700116
117 /**
118 * Deserializer function for router solicitation packets.
119 *
120 * @return deserializer function
121 */
122 public static Deserializer<RouterSolicitation> deserializer() {
123 return (data, offset, length) -> {
124 checkInput(data, offset, length, HEADER_LENGTH);
125
126 RouterSolicitation routerSolicitation = new RouterSolicitation();
127
128 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
129
130 bb.getInt();
131
Charles Chan3599d632015-09-05 14:47:51 +0800132 if (bb.limit() - bb.position() > 0) {
133 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
134 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700135
Charles Chan3599d632015-09-05 14:47:51 +0800136 for (NeighborDiscoveryOptions.Option option : options.options()) {
137 routerSolicitation.addOption(option.type(), option.data());
138 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700139 }
140
141 return routerSolicitation;
142 };
143 }
Jian Li5fc14292015-12-04 11:30:46 -0800144
145 @Override
146 public String toString() {
147 return toStringHelper(getClass())
148 .toString();
149 // TODO: need to handle options
150 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800151}